Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion .aiox-core/core/orchestration/context-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,31 @@

const fs = require('fs-extra');
const path = require('path');
const { isPlainObject } = require('../config/merge-utils');

function mergeMetadata(target, source) {
const base = isPlainObject(target) ? target : {};

if (!isPlainObject(source)) {
return { ...base };
}

const result = { ...base };

for (const [key, value] of Object.entries(source)) {
result[key] = isPlainObject(value) && isPlainObject(result[key])
? mergeMetadata(result[key], value)
: value;
}

return result;
}

function describeMetadataInput(value) {
if (value === null) return 'null';
if (Array.isArray(value)) return 'array';
return typeof value;
}

/**
* Manages workflow execution context and state persistence
Expand Down Expand Up @@ -224,8 +249,14 @@ class ContextManager {
* @param {Object} metadata - Metadata to merge
*/
async updateMetadata(metadata) {
if (!isPlainObject(metadata)) {
throw new TypeError(
`updateMetadata expects a plain object, received: ${describeMetadataInput(metadata)}`,
);
}

const state = await this.loadState();
state.metadata = { ...state.metadata, ...metadata };
state.metadata = mergeMetadata(state.metadata, metadata);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
this._stateCache = state;
await this._saveState();
}
Expand Down
6 changes: 3 additions & 3 deletions .aiox-core/install-manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# - File types for categorization
#
version: 5.1.15
generated_at: "2026-05-08T04:49:28.006Z"
generated_at: "2026-05-08T05:14:31.637Z"
generator: scripts/generate-install-manifest.js
file_count: 1111
files:
Expand Down Expand Up @@ -845,9 +845,9 @@ files:
type: core
size: 12722
- path: core/orchestration/context-manager.js
hash: sha256:7bf273723a2c08cb84e670b9d4c55aacec51819b1fbd5f3b0c46c4ccfa2ec192
hash: sha256:b6ed0e4eb069558998f95ab0c68fa040b1b8c2aab74ab44e26ab682fe9e247b4
type: core
size: 16842
size: 17620
- path: core/orchestration/dashboard-integration.js
hash: sha256:8f2dd7d3885a9eaf58957505d312923e149f2771ae3ca0cda879631683f826c9
type: core
Expand Down
52 changes: 52 additions & 0 deletions tests/core/orchestration/context-manager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,58 @@ describe('ContextManager', () => {
expect(manager._stateCache.metadata.projectRoot).toBe('/outro/caminho');
});

test('preserva metadados aninhados ao aplicar merge profundo', async () => {
await manager.updateMetadata({
observability: {
attempts: {
total: 1,
lastPhase: 'planning',
},
tags: ['initial'],
nullable: 'keep',
},
});

await manager.updateMetadata({
observability: {
attempts: {
failed: 1,
},
lastError: {
code: 'EACCES',
},
tags: ['latest'],
nullable: null,
},
});

expect(manager._stateCache.metadata.observability).toEqual({
attempts: {
total: 1,
lastPhase: 'planning',
failed: 1,
},
lastError: {
code: 'EACCES',
},
tags: ['latest'],
nullable: null,
});
});

test.each([
['null', null],
['undefined', undefined],
['array', []],
['string', 'metadata'],
])('rejeita metadata inválido: %s', async (_label, invalidMetadata) => {
await expect(manager.updateMetadata(invalidMetadata)).rejects.toThrow(
'updateMetadata expects a plain object',
);

expect(fs.writeJson).not.toHaveBeenCalled();
});

test('persiste estado no disco', async () => {
await manager.updateMetadata({ foo: 'bar' });

Expand Down
Loading