Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(companions): recover partial imports and private command output
Chrise4cd174e4971
diff --git a/apps/desktop/src/main/bot-import/__tests__/automationRuntime.test.ts b/apps/desktop/src/main/bot-import/__tests__/automationRuntime.test.tsindex 334221176e..46b4a47504 100644--- a/apps/desktop/src/main/bot-import/__tests__/automationRuntime.test.ts+++ b/apps/desktop/src/main/bot-import/__tests__/automationRuntime.test.ts@@ -206,7 +206,22 @@ it('runs a native command with literal argv, cwd, stdin and private env only aftawait expect(fs.access(marker)).rejects.toThrow();await shared.store.update(root, 'bot', () => {}, env => { env.automations!.routine!.handover = 'ready'; });const result = await prepareImportedAutomation(root, routine, 'cmd-run', signal, () => {});- expect(JSON.parse(result!.direct!)).toEqual([arg, await fs.realpath(root), 'test stdin', '[imported_credential_0]']);+ expect(JSON.parse(result!.direct!)).toEqual([expect.stringMatching(/^\[command_literal_/), expect.stringMatching(/^\[command_literal_/), expect.stringMatching(/^\[command_literal_/), '[imported_credential_0]']);+ for (const literal of [arg, root, await fs.realpath(root), 'test stdin']) expect(result!.direct).not.toContain(literal);expect(await prepareImportedAutomation(root, routine, 'cmd-run', signal, () => {})).toEqual(result);expect(await fs.readFile(marker, 'utf8')).toBe('x');});++it('masks argv-only credentials again before publishing legacy command results', async () => {+ const routine = { id: 'routine', botId: 'bot' } as Routine;+ const original = { payload: { kind: 'command', argv: [process.execPath, '--token=fixture-argv-secret'], input: 'fixture-stdin-secret', cwd: root } };+ await shared.store.write(root, 'bot', { version: 1, env: {}, mcp: [], credentials: [], automations: {+ routine: { kind: 'openclaw', handover: 'ready', original, sourceRoot: root },+ } }, () => {});+ await finishImportedAutomation(root, routine, 'chat', 'legacy-run', `fixture-argv-secret fixture-stdin-secret ${root}`, true, new AbortController().signal, () => {});+ const published = shared.message.mock.calls[0]![1].content;+ expect(published).not.toContain('fixture-argv-secret');+ expect(published).not.toContain('fixture-stdin-secret');+ expect(published).not.toContain(root);+ expect(published).toContain('[command_literal_');+});diff --git a/apps/desktop/src/main/bot-import/__tests__/files.test.ts b/apps/desktop/src/main/bot-import/__tests__/files.test.tsindex 02c1787514..fc71360968 100644--- a/apps/desktop/src/main/bot-import/__tests__/files.test.ts+++ b/apps/desktop/src/main/bot-import/__tests__/files.test.ts@@ -77,12 +77,15 @@ it('hashes binary contents and checkpoints compactly without invoking Buffer.toJit('reads every resource in a skill with more than 4096 small files', async () => {const root = await fs.mkdtemp(path.join(os.tmpdir(), 'cindy-import-many-files-'));try {- for (let index = 0; index < 4100; index++) await fs.writeFile(path.join(root, `resource-${index}.txt`), `Resource ${index}`);+ for (let first = 0; first < 4100; first += 32) await Promise.all(Array.from({ length: Math.min(32, 4100 - first) }, (_, offset) => {+ const index = first + offset;+ return fs.writeFile(path.join(root, `resource-${index}.txt`), `Resource ${index}`);+ }));const files = await readImportTree(root, undefined, createImportBudget());expect(files).toHaveLength(4100);expect(files.find(file => file.name === 'resource-4099.txt')?.bytes.toString()).toBe('Resource 4099');} finally { await fs.rm(root, { recursive: true, force: true }); }-});+}, 180_000);it('charges metadata for empty files and stops streaming instead of retaining unlimited errors', async () => {const root = await fs.mkdtemp(path.join(os.tmpdir(), 'cindy-import-empty-files-'));diff --git a/apps/desktop/src/main/bot-import/__tests__/host.test.ts b/apps/desktop/src/main/bot-import/__tests__/host.test.tsindex 1583ddcd76..031a254792 100644--- a/apps/desktop/src/main/bot-import/__tests__/host.test.ts+++ b/apps/desktop/src/main/bot-import/__tests__/host.test.ts@@ -23,7 +23,7 @@ import { createBotMemoryService } from '../../maker-ipc/botMemoryService.js';const h = vi.hoisted(() => ({ root: '', botId: '', created: false, verified: false, sourceEnabled: true, failReadyWrite: false, boundary: false,snapshot: null as unknown as ImportSnapshot, store: null as unknown as ReturnType<typeof createCompanionEnvironmentStore>,routines: [] as Routine[], pause: vi.fn(), lifecycle: vi.fn(), sourceEnvironment: {} as Record<string, string>,- writeProfile: vi.fn(), importDocument: vi.fn(), readName: vi.fn(), secretValues: new Map<string, string>(),+ profile: { config: {} } as Record<string, unknown>, importMedia: vi.fn(), writeProfile: vi.fn(), importDocument: vi.fn(), readName: vi.fn(), secretValues: new Map<string, string>(),}));vi.mock('electron', () => ({ app: { getPath: () => h.root } }));vi.mock('../../appSessionState.js', () => ({ activeOwnerScopeKey: () => h.root, ownerScopedUserDataPath: () => h.root, getActiveAppSession: () => ({ dataOwnerId: 'fixture-owner' }), isAppSessionBoundaryPending: () => h.boundary }));@@ -34,7 +34,7 @@ vi.mock('../../localDb/ipc/bots.js', () => ({getBotMemoryService: () => ({ importDocument: h.importDocument }), reconcileBotProfileFolder: async () => {},}));vi.mock('../../localDb/ipc/botAvatarSelection.js', () => ({ validateBotAvatarBuffer: vi.fn(), decodeBotAvatarImage: vi.fn() }));-vi.mock('../../maker-ipc/botProfileFolder.js', () => ({ BOT_PROFILE_TEXT_MAX_BYTES: 100000, readBotProfileFolder: async () => ({ config: {} }), writeBotProfileFolder: h.writeProfile,+vi.mock('../../maker-ipc/botProfileFolder.js', () => ({ BOT_PROFILE_TEXT_MAX_BYTES: 100000, readBotProfileFolder: async () => h.profile, writeBotProfileFolder: h.writeProfile,ensureBotWorkspaceDir: async () => { const directory = path.join(h.root, 'bots', h.botId, 'workspace'); await fs.mkdir(directory, { recursive: true }); return directory; },}));vi.mock('@cindy/mcps', () => ({ resolveLiziMcpSessionContext: () => ({ sessionId: 'chat' }) }));@@ -51,6 +51,7 @@ vi.mock('../runtime.js', () => ({ recoverCompanionEnvironmentRemovals: vi.fn(asystageRemoval: (...args: Parameters<typeof h.store.stageRemoval>) => h.store.stageRemoval(...args),finishRemoval: (...args: Parameters<typeof h.store.finishRemoval>) => h.store.finishRemoval(...args),} }));+vi.mock('../memoryMedia.js', () => ({ importMemoryMedia: h.importMedia }));vi.mock('../../localDb/ipc/messages.js', () => ({ createMessage: vi.fn() }));vi.mock('../../routines/service.js', () => ({updateBotRoutineLifecycle: h.lifecycle,@@ -87,7 +88,8 @@ beforeEach(async () => {vi.mocked(getBotRemoteResourceSource).mockReset().mockImplementation(async () => { if (!h.created) throw new Error('[NOT_FOUND]'); return { canonicalSessionId: 'chat' } as never; });vi.mocked(createBotProfile).mockReset().mockImplementation(async input => { h.created = true; h.botId = (input as { id: string }).id; return {} as never; });vi.mocked(createMessage).mockReset();- h.writeProfile.mockReset().mockResolvedValue(undefined); h.importDocument.mockReset().mockResolvedValue(undefined);+ h.profile = { config: {} }; h.importMedia.mockReset().mockResolvedValue('cindy-media://blobs/fixture.png');+ h.writeProfile.mockReset().mockImplementation(async (_root, _bot, patch) => { h.profile = { ...h.profile, ...patch }; }); h.importDocument.mockReset().mockResolvedValue(undefined);vi.mocked(decodeBotAvatarImage).mockReset();vi.mocked(discoverImportSources).mockReset().mockImplementation(async () => [h.snapshot.source]);vi.mocked(inspectImportSource).mockReset().mockImplementation(async () => h.snapshot);@@ -1149,8 +1151,8 @@ it.each([0, 1])('counts earlier recovered documents when a later document failsconst selection = { requestId: 'fixture-directory-progress', previewId: preview.id, name: 'Ada', entryIds: ['memory-directory'], takeover: false, deferSetup: true };const accepted = await startCompanionImport(selection, 'fixture');const first = await withBotProfileLocks([accepted.botId], () => getCompanionImportResult(selection.requestId));- expect(first?.checks).toContainEqual({ entryId: 'memory-directory', status: 'needs-attention', message: 'IMPORT_DISK_FULL', progress: { saved: 2 + failedAfter, total: 5 } });- expect(await storage.list()).toHaveLength(2 + failedAfter);+ expect(first?.checks).toContainEqual({ entryId: 'memory-directory', status: 'needs-attention', message: 'IMPORT_DISK_FULL', progress: { saved: 3 + failedAfter, total: 5 } });+ expect(await storage.list()).toHaveLength(3 + failedAfter);fail = false;await startCompanionImport(selection, 'reconnected');const completed = await withBotProfileLocks([accepted.botId], () => getCompanionImportResult(selection.requestId));@@ -1247,3 +1249,58 @@ it('archives unsupported automation definitions and blocks both management and rawait withBotProfileLocks([accepted.botId], () => getCompanionImportResult(selection.requestId));expect(h.routines).toHaveLength(1);});++it('continues healthy recovered documents when a sibling attachment fails', async () => {+ h.importMedia.mockRejectedValueOnce(Object.assign(new Error('media failed'), { code: 'ENOSPC' }));+ h.snapshot.items = [{ view: { id: 'mixed', name: 'Recovered', category: 'memory', selected: true },+ documents: [{ id: 'healthy', name: 'healthy.md', text: 'Keep this note' }],+ files: [{ name: 'image.png', bytes: Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]), executable: false }] }];+ const [source] = await listCompanionImportSources('fixture');+ const preview = await previewCompanionImport(source!.id, 'fixture');+ const selection = { requestId: 'mixed-directory-retry', previewId: preview.id, name: 'Ada', entryIds: ['mixed'], takeover: false, deferSetup: true };+ const accepted = await startCompanionImport(selection, 'fixture');+ 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 } });+ await startCompanionImport(selection, 'fixture');+ const result = await withBotProfileLocks([accepted.botId], () => getCompanionImportResult(selection.requestId));+ expect(result?.savedEntryIds).toContain('mixed');+});++it('cleans a completed checkpoint after a crash without replaying import work', async () => {+ h.snapshot.items = [{ view: { id: 'memory', name: 'note', category: 'memory', selected: true }, text: 'Original' }];+ const [source] = await listCompanionImportSources('fixture'); const preview = await previewCompanionImport(source!.id, 'fixture');+ const selection = { requestId: 'completed-checkpoint-recovery', previewId: preview.id, name: 'Ada', entryIds: ['memory'], takeover: false, deferSetup: true };+ const accepted = await startCompanionImport(selection, 'fixture');+ await withBotProfileLocks([accepted.botId], () => getCompanionImportResult(selection.requestId));+ await h.store.update(h.root, accepted.botId, () => {}, env => { env.pendingImport = { selection, snapshotJson: 'old encrypted source snapshot' }; });+ const receiptPath = path.join(h.root, 'companion-imports', `${selection.requestId}.json`);+ const receipt = JSON.parse(await fs.readFile(receiptPath, 'utf8')); delete receipt.checkpointCleared;+ await fs.writeFile(receiptPath, JSON.stringify(receipt));+ h.importDocument.mockClear();+ await recoverCompanionImports();+ expect((await h.store.read(h.root, accepted.botId, () => {}))?.pendingImport).toBeUndefined();+ expect(h.importDocument).not.toHaveBeenCalled();+});++it.each(['identity', 'user', 'instructions'] as const)('reapplies a repaired %s source to the profile, while preserving user edits', async role => {+ const file = path.join(h.root, 'repaired.md');+ h.snapshot.items = [{ view: { id: 'role-source', name: 'source.md', category: 'personality', selected: true }, role,+ sourceFile: { root: h.root, file, kind: 'file' }, captureIssue: 'IMPORT_PERMISSION_DENIED' }];+ const [source] = await listCompanionImportSources('fixture'); const preview = await previewCompanionImport(source!.id, 'fixture');+ const selection = { requestId: `repaired-role-${role}-fixture`, previewId: preview.id, name: 'Ada', entryIds: ['role-source'], takeover: false, deferSetup: true };+ const accepted = await startCompanionImport(selection, 'fixture');+ await withBotProfileLocks([accepted.botId], () => getCompanionImportResult(selection.requestId));+ const field = { identity: 'identitySource', user: 'userContextSource', instructions: 'systemPromptOverride' }[role];+ h.profile[field] = 'User edit';+ await fs.writeFile(file, 'Repaired original');+ await startCompanionImport(selection, 'fixture');+ const conflict = await withBotProfileLocks([accepted.botId], () => getCompanionImportResult(selection.requestId));+ expect(conflict?.savedEntryIds).not.toContain('role-source');+ expect(h.profile[field]).toBe('User edit');+ h.profile[field] = '';+ await startCompanionImport(selection, 'fixture');+ const saved = await withBotProfileLocks([accepted.botId], () => getCompanionImportResult(selection.requestId));+ expect(saved?.savedEntryIds).toContain('role-source');+ expect(h.profile[field]).toBe('Repaired original');+});diff --git a/apps/desktop/src/main/bot-import/__tests__/sources.test.ts b/apps/desktop/src/main/bot-import/__tests__/sources.test.tsindex 71d73f4c45..2aa76a9012 100644--- a/apps/desktop/src/main/bot-import/__tests__/sources.test.ts+++ b/apps/desktop/src/main/bot-import/__tests__/sources.test.ts@@ -5,7 +5,7 @@ import path from 'node:path';import { createImportSourceReader, discoverImportSources, inspectImportSource } from '../sources.js';import { transferCompanion, validateImportSelection, type TransferDeps } from '../transfer.js';import { resolveImportReferences, selectedImportEnvironment } from '../environmentSelection.js';-import { discoverImportSkills } from '../skills.js';+import { discoverImportSkills, readImportSkillTree } from '../skills.js';import { createImportBudget } from '../files.js';let home: string;@@ -525,6 +525,23 @@ it.each(['workspace', 'extraDirs', 'plugin', 'hermes'])('bounds empty %s directoawait expect(handle.read()).rejects.toMatchObject({ code: 'ERR_DIR_CLOSED' });});+it('bounds native venv enumeration even when entries are not interpreter aliases', async () => {+ const directory = path.join(home, '.venv', process.platform === 'win32' ? 'Scripts' : 'bin');+ await fs.mkdir(directory, { recursive: true });+ await write('.venv/pyvenv.cfg', `home = ${home}\n`);+ for (let index = 0; index < 40; index++) await fs.writeFile(path.join(directory, `ordinary-${index}`), '');+ const budget = createImportBudget(1500);+ const reserve = vi.spyOn(budget, 'reserve');+ const open = vi.spyOn(fs, 'opendir');+ const readdir = vi.spyOn(fs, 'readdir');+ await expect(readImportSkillTree(home, undefined, budget)).rejects.toThrow('SOURCE_SNAPSHOT_TOO_LARGE');+ expect(readdir).not.toHaveBeenCalled();+ expect(reserve.mock.calls.length).toBeLessThan(40);+ const opened = open.mock.results.filter((_, index) => String(open.mock.calls[index]![0]) === directory);+ expect(opened).toHaveLength(1);+ await expect((await opened[0]!.value).read()).rejects.toMatchObject({ code: 'ERR_DIR_CLOSED' });+});+it('preserves sorted skill-name precedence after streaming directory entries', async () => {await write('.hermes/skills/z-last/SKILL.md', '---\nname: report\n---\nLast');await write('.hermes/skills/a-first/SKILL.md', '---\nname: report\n---\nFirst');diff --git a/apps/desktop/src/main/bot-import/automationRuntime.ts b/apps/desktop/src/main/bot-import/automationRuntime.tsindex 1314b1537c..53b989c4cd 100644--- a/apps/desktop/src/main/bot-import/automationRuntime.ts+++ b/apps/desktop/src/main/bot-import/automationRuntime.ts@@ -11,8 +11,26 @@ import { importedProcessEnvironment, redactEnvironmentValues, runImportedProcessimport { sendImportedDelivery } from './delivery.js';import { importedScriptName, importedScriptInterpreter } from './scripts.js';import { importedContentRedactions } from './connectionCatalog.js';+import type { CompanionEnvironment } from './environment.js';import { importedCommand } from './commandAutomation.js';+/** Command literals are private too, including values supplied without env names. */+async function outputSecrets(environment: CompanionEnvironment, job: Record<string, unknown>, command: ReturnType<typeof importedCommand>) {+ const secrets = importedContentRedactions({ ...environment,+ mcp: [...environment.mcp, ...(command ? [{ name: 'command', env: command.env }] : [])],+ }, job.monitor_url ? [string(job.monitor_url)] : []);+ if (!command) return secrets;+ const cwd = command.cwd ? await fs.realpath(command.cwd).catch(() => undefined) : undefined;+ const values = [command.command, command.cwd, cwd, command.input,+ ...command.args.flatMap(arg => [arg, /^--?[\w-]+=(.+)$/s.exec(arg)?.[1]])].filter((value): value is string => !!value);+ for (const [index, value] of [...new Set(values)].entries()) {+ secrets[`command_literal_${index}`] = value;+ const escaped = JSON.stringify(value).slice(1, -1);+ if (escaped !== value) secrets[`command_literal_${index}_json`] = escaped;+ }+ return secrets;+}+function repeatExhausted(binding: { original: Record<string, unknown>; completed?: number }): boolean {const repeat = object(binding.original.repeat);return Number(repeat.times) > 0 && (binding.completed ?? Number(repeat.completed ?? 0)) >= Number(repeat.times);@@ -43,9 +61,7 @@ export async function prepareImportedAutomation(root: string, routine: Routine,// Also repair a crash between committing the last delivery and disabling the// routine. A stale prepared result must not bypass an exhausted counter.if (repeatExhausted(binding)) return { runId, prompt: '', skipped: true, exhausted: true };- const secrets = importedContentRedactions({ ...environment,- mcp: [...environment.mcp, ...(command ? [{ name: 'command', env: command.env }] : [])],- }, job.monitor_url ? [string(job.monitor_url)] : []);+ const secrets = await outputSecrets(environment, job, command); assertOwner();const redact = (text: string) => redactEnvironmentValues(text, secrets);// A retry or the next occurrence finishes the captured result first, without// rerunning the script/model and changing the remaining message chunks.@@ -118,9 +134,7 @@ export async function finishImportedAutomation(root: string, routine: Routine, slet progress = binding.deliveryProgress;text = progress?.text ?? text;const command = binding.kind === 'openclaw' ? importedCommand(binding.original) : undefined;- text = redactEnvironmentValues(text, importedContentRedactions({ ...env,- mcp: [...env.mcp, ...(command ? [{ name: 'command', env: command.env }] : [])],- }, binding.original.monitor_url ? [string(binding.original.monitor_url)] : []));+ text = redactEnvironmentValues(text, await outputSecrets(env, binding.original, command)); assertOwner();if (!progress) {progress = { runId, text, direct, deliveries: binding.deliveries ?? [], next: 0 };const captured = progress;diff --git a/apps/desktop/src/main/bot-import/host.ts b/apps/desktop/src/main/bot-import/host.tsindex 2c6a92747e..2c55c209a4 100644--- a/apps/desktop/src/main/bot-import/host.ts+++ b/apps/desktop/src/main/bot-import/host.ts@@ -303,6 +303,19 @@ export async function cancelCompanionImportsForDeletion(botId: string): Promise<}}+/** Caller holds the profile lock; acknowledge only after cleanup has committed. */+async function clearCompletedCheckpoint(root: string, receipt: ImportReceipt, assertOwner: () => void) {+ if (receipt.cancelled || receipt.result.status !== 'complete' || receipt.checkpointCleared) return;+ const { botId, requestId } = receipt.result;+ const environment = await companionEnvironmentStore.read(root, botId, assertOwner);+ if (environment?.pendingImport?.selection.requestId === requestId) await companionEnvironmentStore.update(root, botId, assertOwner, env => {+ if (env.pendingImport?.selection.requestId === requestId) delete env.pendingImport;+ });+ assertOwner();+ receipt.checkpointCleared = true;+ await saveReceipt(root, receipt); assertOwner();+}+export async function getCompanionImportResult(requestId: string): Promise<CompanionImportResult | undefined> {const scope = owner();let receipt = await readReceipt(scope.root, requestId); scope.assert();@@ -355,6 +368,10 @@ export async function getCompanionImportResult(requestId: string): Promise<Compaawait saveReceipt(scope.root, receipt);});}+ if (receipt?.result.status === 'complete' && !receipt.cancelled && !receipt.checkpointCleared) await withBotProfileLocks([receipt.result.botId], async () => {+ const latest = await readReceipt(scope.root, requestId); scope.assert();+ if (latest) await clearCompletedCheckpoint(scope.root, latest, scope.assert);+ });return receipt?.result;}@@ -453,6 +470,38 @@ export async function startCompanionImport(selection: CompanionImportSelection,const running = jobs.get(jobKey);if (running) return accepted(running, scope.root, selection.requestId);let skillConfigReader: { budget: Parameters<NonNullable<TransferDeps['readSkillConfig']>>[1]; reader: ReturnType<typeof createImportSourceReader> } | undefined;+ const applyProfile = async (botId: string, items: ImportItem[], baseline?: Record<string, string>, originals?: Record<string, string>) => {+ const roleText = (role: 'identity' | 'user' | 'instructions', originals?: Record<string, string>) => {+ const text = redactText(items.flatMap(item => [...(item.role === role ? [originals ? originals[item.view.id] : item.text] : []), ...(item.documents ?? []).filter(document => document.role === role).map(document => originals ? originals[document.id] : document.text)]).join('\n\n'));+ if (Buffer.byteLength(text, 'utf8') <= BOT_PROFILE_TEXT_MAX_BYTES) return text;+ // Keep the original profile text without synthesizing new system instructions.+ // The entire source remains in personal memory and the encrypted document archive.+ let bytes = 0, end = 0;+ for (const char of text) {+ bytes += Buffer.byteLength(char, 'utf8');+ if (bytes > BOT_PROFILE_TEXT_MAX_BYTES) break;+ end += char.length;+ }+ return text.slice(0, end);+ };+ const folder = await readBotProfileFolder(scope.root, botId); scope.assert();+ const fields = { identity: 'identitySource', user: 'userContextSource', instructions: 'systemPromptOverride' } as const;+ const patch: { identitySource?: string; userContextSource?: string; systemPromptOverride?: string } = {};+ for (const [role, field] of Object.entries(fields) as Array<[keyof typeof fields, typeof fields[keyof typeof fields]]>) {+ const text = roleText(role, originals);+ if (!text || baseline && text === roleText(role, baseline)) continue;+ if (baseline && (folder[field] ?? '') !== roleText(role, baseline) && folder[field] !== text) throw new CompanionImportError('PROFILE_CHANGED');+ patch[field] = text;+ }+ if (!baseline || Object.keys(patch).length) {+ await writeBotProfileFolder(scope.root, botId, {+ config: baseline ? folder.config : { ...folder.config, mcpMode: 'allowlist', mcpServers: [...new Set([+ ...(Array.isArray(folder.config.mcpServers) ? folder.config.mcpServers : []), 'companion_connections',+ ])] }, ...patch,+ }); scope.assert();+ await reconcileBotProfileFolder(botId); scope.assert();+ }+ };const transferDeps: TransferDeps = {assertOwner: scope.assert,validateItems(items) {@@ -506,40 +555,52 @@ export async function startCompanionImport(selection: CompanionImportSelection,} else if (item.view.category === 'memory' || item.view.category === 'personality') {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;for (const file of attachments) {- // Older checkpoints classified every non-md/txt file as media. Use- // the same byte classification on retry without changing entry IDs.- const content = memoryFileContent({ ...file, executable: false });- if (content.kind === 'empty') continue;- if (content.kind === 'text') {- documents.push({ id: `memory-${fingerprint([item.view.id, file.name]).slice(0, 32)}`, name: file.name, text: content.text, role: undefined });- continue;+ try {+ // Old checkpoints may classify textual memory as an attachment.+ const content = memoryFileContent({ ...file, executable: false });+ if (content.kind === 'empty') continue;+ if (content.kind === 'text') {+ documents.push({ id: `memory-${fingerprint([item.view.id, file.name]).slice(0, 32)}`, name: file.name, text: content.text, role: undefined });+ continue;+ }+ const sessionId = await transferDeps.createConversation(botId);+ const url = await importMemoryMedia(botId, sessionId, file.bytes, scope.assert);+ documents.push({ id: `memory-${fingerprint([item.view.id, file.name]).slice(0, 32)}`, name: file.name,+ text: `![${redactText(file.name).replace(/[\[\]\r\n]/g, '')}](${url})`, role: undefined });+ } catch (error) {+ scope.assert();+ firstFailure ??= error; failedAttachments++;}- const sessionId = await transferDeps.createConversation(botId);- const url = await importMemoryMedia(botId, sessionId, file.bytes, scope.assert);- documents.push({ id: `memory-${fingerprint([item.view.id, file.name]).slice(0, 32)}`, name: file.name,- text: `![${redactText(file.name).replace(/[\[\]\r\n]/g, '')}](${url})`, role: undefined });}- const prepared = documents.map(document => {- const text = redactText(document.text);- return { ...document, text, parts: splitImportedMemoryText(text).length };- });- const total = prepared.reduce((sum, document) => sum + document.parts, 0);- let saved = 0;- for (const document of prepared) {+ let total = failedAttachments, saved = 0;+ for (const document of documents) {+ let parts = 1; total++;try {scope.assert();- await getBotMemoryService().importDocument(botId, document.id, redactText(document.name), document.text, document.role === 'user' ? 'user' : 'reference');- saved += document.parts;+ const text = redactText(document.text);+ parts = splitImportedMemoryText(text).length;+ total += parts - 1;+ await getBotMemoryService().importDocument(botId, document.id, redactText(document.name), text, document.role === 'user' ? 'user' : 'reference');+ saved += parts;} catch (error) {- if (error instanceof Error) {- const progress = (error as Error & { importProgress?: { saved: number; total: number } }).importProgress;- Object.assign(error, { importProgress: { saved: saved + (progress?.saved ?? 0),- total: total + (progress ? progress.total - document.parts : 0) } });- }- throw error;+ scope.assert();+ const progress = (error as { importProgress?: { saved: number; total: number } })?.importProgress;+ if (progress) { saved += progress.saved; total += progress.total - parts; }+ firstFailure ??= error;}}+ if (firstFailure) {+ const error = firstFailure instanceof Error ? firstFailure : new CompanionImportError('IMPORT_ITEM_FAILED');+ throw Object.assign(error, { importProgress: { saved, total } });+ }+ }+ if (prior?.environmentSaved && (item.role || item.documents?.some(document => document.role))) {+ const environment = await companionEnvironmentStore.read(scope.root, botId, scope.assert);+ const previous = environment?.documents ?? {};+ await applyProfile(botId, selected.map(candidate => candidate.view.id === item.view.id ? item : candidate), previous,+ { ...previous, ...Object.fromEntries(documentEntries(item)) });}if (prior?.environmentSaved) await companionEnvironmentStore.update(scope.root, botId, scope.assert, environment => {// A formerly unreadable manifest may only now supply its own settings.@@ -595,30 +656,7 @@ export async function startCompanionImport(selection: CompanionImportSelection,pendingImport: previous?.pendingImport ?? { selection, snapshotJson: await serializeImportSnapshotAsync({ ...snapshot, avatarImageBase64: selection.avatarImageBase64, items, publicationRedactions: publicationRedactions(items) }, scope.assert) },automations: previous?.automations ?? {},}, scope.assert);- const roleText = (role: 'identity' | 'user' | 'instructions') => {- const text = redactText(items.flatMap(item => [...(item.role === role ? [item.text] : []), ...(item.documents ?? []).filter(document => document.role === role).map(document => document.text)]).join('\n\n'));- if (Buffer.byteLength(text, 'utf8') <= BOT_PROFILE_TEXT_MAX_BYTES) return text;- // Keep the original profile text without synthesizing new system instructions.- // The entire source remains in personal memory and the encrypted document archive.- let bytes = 0, end = 0;- for (const char of text) {- bytes += Buffer.byteLength(char, 'utf8');- if (bytes > BOT_PROFILE_TEXT_MAX_BYTES) break;- end += char.length;- }- return text.slice(0, end);- };- const folder = await readBotProfileFolder(scope.root, botId); scope.assert();- await writeBotProfileFolder(scope.root, botId, {- config: { ...folder.config, mcpMode: 'allowlist', mcpServers: [...new Set([- ...(Array.isArray(folder.config.mcpServers) ? folder.config.mcpServers : []),- 'companion_connections',- ])] },- ...(roleText('identity') ? { identitySource: roleText('identity') } : {}),- ...(roleText('user') ? { userContextSource: roleText('user') } : {}),- ...(roleText('instructions') ? { systemPromptOverride: roleText('instructions') } : {}),- }); scope.assert();- await reconcileBotProfileFolder(botId); scope.assert();+ await applyProfile(botId, items);},async createConversation(botId) {const source = await getBotRemoteResourceSource(botId); scope.assert();@@ -705,7 +743,10 @@ export async function startCompanionImport(selection: CompanionImportSelection,content: unsaved.size ? `${t('bots.import.chatPartial')}\n\n${details}${failures.length > 20 ? `\n… (${failures.length})` : ''}` : t(`bots.import.${pending ? 'chatSetup' : 'chatReady'}`) });scope.assert();}- if (result.status === 'complete') await companionEnvironmentStore.update(scope.root, result.botId, scope.assert, env => { delete env.pendingImport; });+ if (result.status === 'complete') {+ const completed = await readReceipt(scope.root, selection.requestId); scope.assert();+ if (completed) await clearCompletedCheckpoint(scope.root, completed, scope.assert);+ }return result;});if (result.status !== 'running') return result;diff --git a/apps/desktop/src/main/bot-import/skills.ts b/apps/desktop/src/main/bot-import/skills.tsindex 4b6e984c6a..208c1618a8 100644--- a/apps/desktop/src/main/bot-import/skills.ts+++ b/apps/desktop/src/main/bot-import/skills.ts@@ -102,10 +102,11 @@ export async function readImportSkillTree(root: string, include?: (name: string)const realHome = await fs.realpath(home).catch(() => undefined);if (!realHome) continue;const bin = path.join(root, folder, process.platform === 'win32' ? 'Scripts' : 'bin');- let entries: Dirent[];- try { entries = await fs.readdir(bin, { withFileTypes: true }); }+ let entries: Awaited<ReturnType<typeof fs.opendir>>;+ try { entries = await fs.opendir(bin); }catch (error) { if ((error as NodeJS.ErrnoException).code === 'ENOENT') continue; throw error; }- for (const entry of entries) {+ for await (const entry of entries) {+ budget?.reserve(128 + Buffer.byteLength(path.join(bin, entry.name)));if (!entry.isSymbolicLink() || !/^python(?:[23](?:\.\d+)?)?(?:\.exe)?$/.test(entry.name)) continue;const real = await fs.realpath(path.join(bin, entry.name));if (path.dirname(real) !== realHome || !/^python(?:[23](?:\.\d+)?)?(?:\.exe)?$/.test(path.basename(real))) continue;diff --git a/apps/desktop/src/main/bot-import/transfer.ts b/apps/desktop/src/main/bot-import/transfer.tsindex 1f4cffd6c0..414c2db76f 100644--- a/apps/desktop/src/main/bot-import/transfer.ts+++ b/apps/desktop/src/main/bot-import/transfer.ts@@ -16,6 +16,8 @@ export interface ImportReceipt {copied: string[];environmentSaved?: boolean;checkpointSaved?: boolean;+ /** Completion cleanup acknowledged after the private pending snapshot was removed. */+ checkpointCleared?: boolean;companionCreated?: true;/** Terminal, non-secret rejection; retained so lost acknowledgements unlock callers. */creationRejected?: 'IMPORT_NAME_EXISTS';