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): separate memory attachments from execution assets

Chris09e3319aff31
diff --git a/apps/desktop/src/main/bot-import/__tests__/automationRuntime.test.ts b/apps/desktop/src/main/bot-import/__tests__/automationRuntime.test.ts
index 46b4a47504..7decde47a9 100644
--- a/apps/desktop/src/main/bot-import/__tests__/automationRuntime.test.ts
+++ b/apps/desktop/src/main/bot-import/__tests__/automationRuntime.test.ts
@@ -27,7 +27,7 @@ it.skipIf(process.platform === 'win32')('executes the default imported script an
const files = {
'config.yaml': 'name: Ada\n',
'cron/jobs.json': JSON.stringify([{ id: 'report', script: 'reports/report.sh', monitor_script: 'monitor/check.sh', no_agent: true, schedule: { kind: 'interval', minutes: 5 } }]),
- 'scripts/reports/report.sh': '. ./helper.sh\nreport\n',
+ 'scripts/reports/report.sh': 'test ! -e "$HERMES_HOME/memory/image.png" || exit 91\ntest -f "$HERMES_HOME/scripts/unrelated/unused.sh" || exit 92\n. ./helper.sh\nreport\n',
'scripts/reports/helper.sh': 'report() { cat data/report.txt; }\n',
'scripts/reports/data/report.txt': 'copied report resource',
'scripts/monitor/check.sh': '. ./helper.sh\nmonitor\n',
@@ -45,6 +45,7 @@ it.skipIf(process.platform === 'win32')('executes the default imported script an
expect(Object.keys(assets)).toHaveLength(7);
expect(assets['scripts/unrelated/unused.sh']).toBe(Buffer.from('exit 99').toString('base64'));
expect(task.view.dependsOn).not.toContain(snapshot.items.find(item => item.asset?.name === 'scripts/unrelated/unused.sh')!.view.id);
+ assets['memory/image.png'] = Buffer.from('legacy-private-image').toString('base64');
await shared.store.write(root, 'bot', { version: 1, env: {}, mcp: [], credentials: [], files: assets, automations: {
routine: { kind: 'hermes', handover: 'ready', original: task.automation!.original, sourceRoot, deliveries: [] },
} }, () => {});
@@ -56,6 +57,23 @@ it.skipIf(process.platform === 'win32')('executes the default imported script an
expect(await fs.readdir(path.join(root, 'bots/bot/import-executions'))).toEqual([]);
});
+it('does not materialize legacy media or archived memory for a native command', async () => {
+ const routine = { id: 'routine', botId: 'bot', prompt: 'Fixture command' } as Routine;
+ const directory = path.join(root, 'bots', 'bot', 'import-executions');
+ const code = 'const fs=require("node:fs"),path=require("node:path");const dirs=fs.readdirSync(process.argv[1]);console.log(dirs.every(name=>!fs.existsSync(path.join(process.argv[1],name,"memory"))&&fs.existsSync(path.join(process.argv[1],name,"scripts/unused.py")))?"clean":"incorrect assets");';
+ const original = { payload: { kind: 'command', argv: [process.execPath, '-e', code, directory], cwd: root } };
+ const legacyFiles = { 'memory/image.png': Buffer.from('legacy-media-bytes').toString('base64'), 'scripts/unused.py': Buffer.from('unused').toString('base64') };
+ await shared.store.write(root, 'bot', { version: 1, env: {}, mcp: [], credentials: [], files: legacyFiles,
+ memoryFiles: { 'memory/.report.sent': '' }, automations: { routine: { kind: 'openclaw', handover: 'ready', original, sourceRoot: root } },
+ }, () => {});
+ const result = await prepareImportedAutomation(root, routine, 'no-media-command', new AbortController().signal, () => {});
+ expect(result?.direct).toBe('clean');
+ const preserved = await shared.store.read(root, 'bot', () => {});
+ expect(preserved?.files).toEqual(legacyFiles);
+ expect(preserved?.memoryFiles).toEqual({ 'memory/.report.sent': '' });
+ expect(await fs.readdir(directory)).toEqual([]);
+});
+
it('uses the original monitor URL but masks echoed path/query credentials, previous output and legacy retry caches', async () => {
const requests: string[] = [];
const endpoint = createServer((req, res) => {
diff --git a/apps/desktop/src/main/bot-import/__tests__/host.test.ts b/apps/desktop/src/main/bot-import/__tests__/host.test.ts
index 031a254792..22ee15f69f 100644
--- a/apps/desktop/src/main/bot-import/__tests__/host.test.ts
+++ b/apps/desktop/src/main/bot-import/__tests__/host.test.ts
@@ -8,7 +8,7 @@ import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { afterEach, beforeEach, expect, it, vi } from 'vitest';
import type { Routine, RoutineInput } from '@cindy/maker-scheduler';
import { companionEnvironmentKey, createCompanionEnvironmentStore } from '../environment.js';
-import { fingerprint } from '../files.js';
+import { deserializeImportSnapshotAsync, fingerprint } from '../files.js';
import { createMessage } from '../../localDb/ipc/messages.js';
import { t } from '../../i18n.js';
import { createBotProfile, getBotRemoteResourceSource } from '../../localDb/ipc/bots.js';
@@ -1192,7 +1192,9 @@ it('retries legacy text and empty memory assets without passing them to the medi
expect(result).toMatchObject({ status: 'complete', savedEntryIds: ['old-json', 'old-marker'] });
expect(h.importDocument).toHaveBeenCalledOnce();
expect(h.importDocument).toHaveBeenCalledWith(first.botId, expect.stringMatching(/^memory-[a-f0-9]{32}$/), 'memory/state.json', '{"cursor":7}\n', 'reference');
- expect((await h.store.read(h.root, first.botId, () => {}))?.files?.['memory/.report.sent']).toBe('');
+ const environment = await h.store.read(h.root, first.botId, () => {});
+ expect(environment?.memoryFiles).toEqual({ 'memory/state.json': Buffer.from('{"cursor":7}\n').toString('base64'), 'memory/.report.sent': '' });
+ expect(environment?.files).toEqual({});
await startCompanionImport(selection, 'phone');
expect(h.importDocument).toHaveBeenCalledOnce();
});
@@ -1262,9 +1264,42 @@ it('continues healthy recovered documents when a sibling attachment fails', asyn
const first = await withBotProfileLocks([accepted.botId], () => getCompanionImportResult(selection.requestId));
expect(h.importDocument).toHaveBeenCalledWith(accepted.botId, 'healthy', 'healthy.md', 'Keep this note', 'reference');
expect(first?.checks).toContainEqual({ entryId: 'mixed', status: 'needs-attention', message: 'IMPORT_DISK_FULL', progress: { saved: 1, total: 2 } });
+ const pending = await h.store.read(h.root, accepted.botId, () => {});
+ expect(pending?.files).toEqual({});
+ expect(pending?.memoryFiles).toEqual({});
+ const checkpoint = await deserializeImportSnapshotAsync(pending!.pendingImport!.snapshotJson, () => {});
+ const original = h.snapshot.items[0]!.files![0]!;
+ expect(checkpoint.items[0]!.files![0]!.bytes).toEqual(original.bytes);
+ // An old failed import may already have mixed attachment bytes into files.
+ await h.store.update(h.root, accepted.botId, () => {}, env => { env.files = { [original.name]: original.bytes.toString('base64') }; });
await startCompanionImport(selection, 'fixture');
const result = await withBotProfileLocks([accepted.botId], () => getCompanionImportResult(selection.requestId));
expect(result?.savedEntryIds).toContain('mixed');
+ const completed = await h.store.read(h.root, accepted.botId, () => {});
+ expect(completed?.files).toEqual({});
+ expect(completed?.pendingImport).toBeUndefined();
+ expect(h.importMedia).toHaveBeenLastCalledWith(accepted.botId, 'chat', original.bytes, expect.any(Function));
+});
+
+it('retains managed media by ledger reference without copying its bytes into execution assets', async () => {
+ const image = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]);
+ const script = Buffer.from('print("fixture")');
+ h.snapshot.items = [
+ { view: { id: 'image', name: 'image.png', category: 'memory', selected: true }, asset: { name: 'memory/image.png', bytes: image } },
+ { view: { id: 'script', name: 'report.py', category: 'connections', selected: true }, asset: { name: 'scripts/report.py', bytes: script } },
+ ];
+ const [source] = await listCompanionImportSources('fixture'); const preview = await previewCompanionImport(source!.id, 'fixture');
+ const selection = { requestId: 'media-ledger-only-fixture', previewId: preview.id, name: 'Ada', entryIds: ['image', 'script'], takeover: false, deferSetup: true };
+ const accepted = await startCompanionImport(selection, 'fixture');
+ const result = await withBotProfileLocks([accepted.botId], () => getCompanionImportResult(selection.requestId));
+ expect(result?.status).toBe('complete');
+ expect(h.importMedia).toHaveBeenCalledWith(accepted.botId, 'chat', image, expect.any(Function));
+ expect(h.importDocument).toHaveBeenCalledWith(accepted.botId, expect.any(String), 'memory/image.png', '![memory/image.png](cindy-media://blobs/fixture.png)', 'reference');
+ const environment = await h.store.read(h.root, accepted.botId, () => {});
+ expect(environment?.files).toEqual({ 'scripts/report.py': script.toString('base64') });
+ expect(environment?.memoryFiles).toEqual({});
+ expect(environment?.pendingImport).toBeUndefined();
+ expect(JSON.stringify(environment)).not.toContain(image.toString('base64'));
});
it('cleans a completed checkpoint after a crash without replaying import work', async () => {
diff --git a/apps/desktop/src/main/bot-import/automationRuntime.ts b/apps/desktop/src/main/bot-import/automationRuntime.ts
index 53b989c4cd..adf42efb2f 100644
--- a/apps/desktop/src/main/bot-import/automationRuntime.ts
+++ b/apps/desktop/src/main/bot-import/automationRuntime.ts
@@ -84,7 +84,10 @@ export async function prepareImportedAutomation(root: string, routine: Routine,
return redact(result.stdout.trim());
};
try {
- await writeImportFiles(directory, Object.entries(environment.files ?? {}).map(([name, bytes]) => ({ name, bytes: Buffer.from(bytes, 'base64'), executable: false })));
+ // Old environments mixed memory attachments into files. The source script
+ // namespace retains all helpers/resources, including cross-directory imports.
+ await writeImportFiles(directory, Object.entries(environment.files ?? {}).filter(([name]) => name.startsWith('scripts/'))
+ .map(([name, bytes]) => ({ name, bytes: Buffer.from(bytes, 'base64'), executable: false })));
let prompt = redact(routine.prompt); let direct: string | undefined; let skipped = false;
let monitorOutput: string | undefined;
if (command) {
diff --git a/apps/desktop/src/main/bot-import/environment.ts b/apps/desktop/src/main/bot-import/environment.ts
index 7dcabfcbbb..a3071787de 100644
--- a/apps/desktop/src/main/bot-import/environment.ts
+++ b/apps/desktop/src/main/bot-import/environment.ts
@@ -13,7 +13,10 @@ export interface CompanionEnvironment {
env: Record<string, string>;
mcp: ImportedMcpServer[];
credentials: Array<{ id: string; format: string; value: unknown }>;
+ /** Source script assets. Legacy imports may also contain memory attachments; execution filters by the scripts/ namespace. */
files?: Record<string, string>;
+ /** Non-media memory originals (including empty markers); never execution assets. Media originals live in the media ledger. */
+ memoryFiles?: Record<string, string>;
/** Originals behind redacted skill projections; only materialized for authorized host commands. */
skillFiles?: Record<string, Array<{ name: string; bytes: string; executable: boolean; interpreterLink?: string }>>;
/** Original selected documents; model-readable profile/memory copies redact known credentials. */
diff --git a/apps/desktop/src/main/bot-import/host.ts b/apps/desktop/src/main/bot-import/host.ts
index 2c55c209a4..c3fcbec348 100644
--- a/apps/desktop/src/main/bot-import/host.ts
+++ b/apps/desktop/src/main/bot-import/host.ts
@@ -208,6 +208,19 @@ function documentEntries(item: ImportItem): Array<[string, string]> {
...(item.documents ?? []).map(document => [document.id, document.text] as [string, string])];
}
+function isMemoryItem(item: ImportItem): boolean {
+ return item.view.category === 'memory' || item.view.category === 'personality';
+}
+
+function memoryFileEntries(item: ImportItem): Array<[string, string]> {
+ if (!isMemoryItem(item)) return [];
+ // Managed media is owned by the ledger and referenced by imported memory.
+ // Failed attachments remain in pendingImport until they can be retried.
+ return [...(item.asset ? [item.asset] : []), ...item.files ?? []]
+ .filter(file => memoryFileContent({ ...file, executable: false }).kind !== 'attachment')
+ .map(file => [file.name, file.bytes.toString('base64')]);
+}
+
function receiptFile(root: string, requestId: string) {
if (!/^[A-Za-z0-9_-]{16,100}$/.test(requestId)) throw new CompanionImportError('INVALID_REQUEST');
return path.join(root, 'companion-imports', `${requestId}.json`);
@@ -552,7 +565,7 @@ export async function startCompanionImport(selection: CompanionImportSelection,
const slug = skillSlug(item);
const projected = projectImportedSkill(item.files ?? [], slug, contentSecrets);
await importBotSkillFiles(scope.root, botId, slug, projected.files, scope.assert, item.view.enabled !== false);
- } else if (item.view.category === 'memory' || item.view.category === 'personality') {
+ } else if (isMemoryItem(item)) {
const documents = [...(item.text ? [{ id: item.view.id, name: item.view.name, text: item.text, role: item.role }] : []), ...item.documents ?? []];
const attachments = [...(item.asset ? [item.asset] : []), ...(item.files ?? [])];
let firstFailure: unknown; let failedAttachments = 0;
@@ -605,8 +618,14 @@ export async function startCompanionImport(selection: CompanionImportSelection,
if (prior?.environmentSaved) await companionEnvironmentStore.update(scope.root, botId, scope.assert, environment => {
// A formerly unreadable manifest may only now supply its own settings.
if (item.env) environment.env = { ...environment.env, ...item.env };
- const attachments = [...(item.asset ? [item.asset] : []), ...(['memory', 'personality'].includes(item.view.category) ? item.files ?? [] : [])];
- if (attachments.length) environment.files = { ...environment.files, ...Object.fromEntries(attachments.map(file => [file.name, file.bytes.toString('base64')])) };
+ if (isMemoryItem(item)) {
+ environment.memoryFiles = { ...environment.memoryFiles, ...Object.fromEntries(memoryFileEntries(item)) };
+ // A successful retry may upgrade an earlier mixed execution archive.
+ // Remove only the exact copy whose original has just been preserved.
+ for (const file of [...(item.asset ? [item.asset] : []), ...item.files ?? []]) {
+ if (environment.files?.[file.name] === file.bytes.toString('base64')) delete environment.files[file.name];
+ }
+ } else if (item.asset) environment.files = { ...environment.files, [item.asset.name]: item.asset.bytes.toString('base64') };
if (item.credential) environment.credentials = [...environment.credentials.filter(value => value.id !== item.view.id), { id: item.view.id, ...item.credential }];
for (const [id, text] of documentEntries(item)) { environment.documents ??= {}; environment.documents[id] = text; }
if (item.view.category === 'skills') {
@@ -645,10 +664,8 @@ export async function startCompanionImport(selection: CompanionImportSelection,
return [server];
}),
credentials: items.flatMap(item => item.credential && !item.view.dependsOn?.some(id => !chosen.has(id)) && (!item.view.issues?.length || item.credential.format !== 'telegram') ? [{ id: item.view.id, ...item.credential, ...(item.credential.format === 'telegram' ? { value: resolveReferences(item.credential.value) } : {}) }] : []),
- files: Object.fromEntries(items.flatMap(item => [
- ...(item.asset ? [[item.asset.name, item.asset.bytes.toString('base64')]] : []),
- ...(['memory', 'personality'].includes(item.view.category) ? (item.files ?? []).map(file => [file.name, file.bytes.toString('base64')]) : []),
- ])),
+ files: Object.fromEntries(items.flatMap(item => item.asset && !isMemoryItem(item) ? [[item.asset.name, item.asset.bytes.toString('base64')]] : [])),
+ memoryFiles: Object.fromEntries(items.flatMap(memoryFileEntries)),
skillFiles,
documents: Object.fromEntries(items.flatMap(documentEntries)),
contentRedactions: publicationRedactions(items),