Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(companions): release import transport chunks before parsing

Chris18b01297c06a
diff --git a/packages/maker-shared/src/__tests__/companionImport.test.ts b/packages/maker-shared/src/__tests__/companionImport.test.ts
index b85ff5e0e3..a7712879e5 100644
--- a/packages/maker-shared/src/__tests__/companionImport.test.ts
+++ b/packages/maker-shared/src/__tests__/companionImport.test.ts
@@ -67,3 +67,25 @@ it('keeps concrete import failures through IPC wrappers without displaying priva
expect(companionImportReasonKey('PRIVATE_UNKNOWN')).toBe('itemFailed');
expect(companionImportErrorCode(new Error('private source contents'))).toBeUndefined();
});
+
+it('reads a multi-megabyte preview without losing escaped names, descriptions or selection indexes', async () => {
+ const entries = Array.from({ length: 12_000 }, (_, index) => ({
+ id: `entry-${index}`, name: `Memory ${index}: "quoted" \\ path 🙂`, category: 'memory',
+ description: 'Unicode 段落\n'.repeat(40), selected: true,
+ }));
+ const preview = { id: 'large-preview', source: { id: 'source', name: 'Ada', kind: 'hermes' }, name: 'Ada', selectionRanges: true, entries };
+ const serialized = JSON.stringify({ preview });
+ expect(serialized.length).toBeGreaterThan(5 * 1024 * 1024);
+ let reads = 0;
+ const api = remoteCompanionImportApi(async id => {
+ reads++;
+ const offset = id.startsWith('chunk:') ? Number(id.split(':')[2]) : 0;
+ return { blocks: [{ primitive: 'companion-import', data: { chunk: {
+ id: 'large-preview', offset, total: serialized.length, text: serialized.slice(offset, offset + COMPANION_IMPORT_CHUNK_LENGTH),
+ } } }] };
+ }, async () => {});
+ const received = await api.preview('source');
+ expect(received).toEqual(preview);
+ expect(reads).toBe(Math.ceil(serialized.length / COMPANION_IMPORT_CHUNK_LENGTH));
+ expect(compactCompanionImportSelection(received, received.entries.map(entry => entry.id))).toEqual({ entryIds: [], entryRanges: [[0, 11999]] });
+});
diff --git a/packages/maker-shared/src/companionImport.ts b/packages/maker-shared/src/companionImport.ts
index 5980a63ec1..337a9ec00e 100644
--- a/packages/maker-shared/src/companionImport.ts
+++ b/packages/maker-shared/src/companionImport.ts
@@ -164,7 +164,13 @@ export function remoteCompanionImportApi(
if (chunk.text.length !== COMPANION_IMPORT_CHUNK_LENGTH) throw new Error('INVALID_IMPORT_RESPONSE');
part = await readData(`chunk:${token}:${offset}`);
}
- const result: unknown = JSON.parse(pieces.join(''));
+ const serialized = pieces.join('');
+ // Release transport chunks before materializing the full projection. Keeping
+ // pieces alive through JSON.parse retains three copies of a large preview
+ // (chunks, joined text and objects) on the controlling phone at once.
+ pieces.length = 0;
+ part = {};
+ const result: unknown = JSON.parse(serialized);
if (!result || typeof result !== 'object' || Array.isArray(result)) throw new Error('INVALID_IMPORT_RESPONSE');
return result as Record<string, unknown>;
};