Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(companions): bound manifest reads and publish setup completion
zqchris07d7b9cf27a7
diff --git a/apps/desktop/src/main/bot-import/__tests__/host.test.ts b/apps/desktop/src/main/bot-import/__tests__/host.test.tsindex 729e10fab2..5734cec123 100644--- a/apps/desktop/src/main/bot-import/__tests__/host.test.ts+++ b/apps/desktop/src/main/bot-import/__tests__/host.test.ts@@ -9,6 +9,8 @@ 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 { createMessage } from '../../localDb/ipc/messages.js';+import { t } from '../../i18n.js';import { createBotProfile, getBotRemoteResourceSource } from '../../localDb/ipc/bots.js';import type { ImportSnapshot } from '../types.js';import { CompanionImportError } from '../types.js';@@ -80,6 +82,7 @@ beforeEach(async () => {vi.mocked(verifyImportedAutomation).mockClear();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);vi.mocked(decodeBotAvatarImage).mockReset();vi.mocked(discoverImportSources).mockReset().mockImplementation(async () => [h.snapshot.source]);@@ -942,3 +945,37 @@ it.each(['directory', undefined] as const)('retries a repaired memory directoryawait startCompanionImport(selection, 'phone');expect(h.importDocument).toHaveBeenCalledTimes(3);});++it.each([false, true])('publishes completion after deferred setup without duplicate notices (legacy setup: %s)', async legacySetup => {+ const requestId = 'fixture-setup-to-ready';+ const rows = new Map<string, string>();+ if (legacySetup) rows.set(`chat:companion-import:${requestId}`, t('bots.import.chatSetup'));+ // Match createMessage's persisted (sessionId, clientId) uniqueness contract.+ vi.mocked(createMessage).mockImplementation(async (sessionId, body) => {+ const key = `${sessionId}:${body.clientId}`;+ if (typeof body.content !== 'string') throw new Error('Expected a text import notice');+ if (!rows.has(key)) rows.set(key, body.content);+ return {} as never;+ });+ const { continueCompanionImport } = await import('../host.js');+ const [source] = await listCompanionImportSources('fixture');+ const preview = await previewCompanionImport(source!.id, 'fixture');+ const selection = { requestId, previewId: preview.id, name: 'Ada', entryIds: ['task'], takeover: true, deferSetup: true };+ await startCompanionImport(selection, 'fixture');+ await vi.waitFor(async () => expect((await getCompanionImportResult(requestId))?.status).toBe('needs-attention'));+ await vi.waitFor(() => expect(rows.size).toBe(1));+ const result = (await getCompanionImportResult(requestId))!;+ // An unsuccessful setup retry must not spam another setup notice.+ await continueCompanionImport(result.botId, h.root, () => {});+ await vi.waitFor(async () => expect((await getCompanionImportResult(requestId))?.status).toBe('needs-attention'));+ expect(rows.size).toBe(1);+ h.verified = true;+ await continueCompanionImport(result.botId, h.root, () => {});+ await vi.waitFor(async () => expect((await getCompanionImportResult(requestId))?.status).toBe('complete'));+ await vi.waitFor(() => expect(rows.size).toBe(2));+ expect([...rows.values()]).toEqual([t('bots.import.chatSetup'), t('bots.import.chatReady')]);+ expect([...rows.keys()]).toEqual([`chat:companion-import:${requestId}`, `chat:companion-import:${requestId}:ready`]);+ await startCompanionImport(selection, 'fixture');+ expect(rows.size).toBe(2);+ expect(h.pause).toHaveBeenCalledTimes(1);+});diff --git a/apps/desktop/src/main/bot-import/__tests__/sources.test.ts b/apps/desktop/src/main/bot-import/__tests__/sources.test.tsindex 5563dda44c..b3a14483f8 100644--- a/apps/desktop/src/main/bot-import/__tests__/sources.test.ts+++ b/apps/desktop/src/main/bot-import/__tests__/sources.test.ts@@ -5,6 +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 { createImportBudget } from '../files.js';let home: string;@@ -458,3 +459,44 @@ it.each(['hermes', 'openclaw'] as const)('records a failed %s memory subtree sepexpect(snapshot.items.find(item => item.view.name === 'bad.md')?.sourceFile?.kind).toBe('file');expect(snapshot.items.find(item => item.view.name === 'healthy.md')?.text).toBe('Keep healthy');});++it.each(['package.json', 'openclaw.plugin.json'])('bounds %s before allocating or parsing during skill discovery', async filename => {+ const directory = filename === 'package.json' ? 'bin/node_modules/openclaw' : 'plugin';+ const manifest = path.join(home, directory, filename);+ await write(`${directory}/${filename}`, '{}');+ await fs.truncate(manifest, 16 * 1024 * 1024 + 1);+ const source = { kind: 'openclaw' as const, agentId: 'main', name: 'Ada', root: path.join(home, '.openclaw'), workspace: path.join(home, '.openclaw/workspace'), configFile: path.join(home, '.openclaw/openclaw.json') };+ const config = { plugins: { load: { paths: [path.join(home, 'plugin')] } } };+ const env = { PATH: path.join(home, 'bin') };+ const allocate = vi.spyOn(Buffer, 'alloc');+ const parse = vi.spyOn(JSON, 'parse');+ await expect(discoverImportSkills(source, config, home, env, createImportBudget())).rejects.toThrow('SOURCE_FILE_TOO_LARGE');+ expect(allocate).not.toHaveBeenCalled();+ expect(parse).not.toHaveBeenCalled();+ allocate.mockRestore(); parse.mockRestore();+ await write(`${directory}/${filename}`, JSON.stringify(filename === 'package.json' ? { name: 'openclaw' } : { id: 'plugin', skills: ['skills'] }));+ await write(`${directory}/skills/report/SKILL.md`, '# Report');+ const skills = await discoverImportSkills(source, config, home, env, createImportBudget());+ expect(skills.map(item => item.view.name)).toEqual(['report']);+});++it('charges installed-package and plugin manifests to the same source budget before parsing the next one', async () => {+ await write('bin/node_modules/openclaw/package.json', JSON.stringify({ name: 'openclaw', padding: 'x'.repeat(600) }));+ await write('plugin/openclaw.plugin.json', JSON.stringify({ id: 'plugin', skills: ['skills'], padding: 'x'.repeat(600) }));+ const source = { kind: 'openclaw' as const, agentId: 'main', name: 'Ada', root: path.join(home, '.openclaw'), workspace: path.join(home, '.openclaw/workspace'), configFile: path.join(home, '.openclaw/openclaw.json') };+ const parse = vi.spyOn(JSON, 'parse');+ await expect(discoverImportSkills(source, { plugins: { load: { paths: [path.join(home, 'plugin')] } } }, home,+ { PATH: path.join(home, 'bin') }, createImportBudget(1500))).rejects.toThrow('SOURCE_SNAPSHOT_TOO_LARGE');+ expect(parse).toHaveBeenCalledTimes(1);+});++// Windows file symlinks require Developer Mode/admin; size/budget cases run on every OS.+it.skipIf(process.platform === 'win32')('preserves native manifest file symlinks while charging their contents', async () => {+ await write('shared/plugin.json', JSON.stringify({ id: 'plugin', skills: ['skills'] }));+ await write('plugin/skills/report/SKILL.md', '# Report');+ await fs.symlink(path.join(home, 'shared/plugin.json'), path.join(home, 'plugin/openclaw.plugin.json'));+ const source = { kind: 'openclaw' as const, agentId: 'main', name: 'Ada', root: path.join(home, '.openclaw'), workspace: path.join(home, '.openclaw/workspace'), configFile: path.join(home, '.openclaw/openclaw.json') };+ const config = { plugins: { load: { paths: [path.join(home, 'plugin')] } } };+ expect((await discoverImportSkills(source, config, home, {}, createImportBudget())).map(item => item.view.name)).toEqual(['report']);+ await expect(discoverImportSkills(source, config, home, {}, createImportBudget(10))).rejects.toThrow('SOURCE_SNAPSHOT_TOO_LARGE');+});diff --git a/apps/desktop/src/main/bot-import/host.ts b/apps/desktop/src/main/bot-import/host.tsindex 3d4f62cc3f..d6cbc73ca0 100644--- a/apps/desktop/src/main/bot-import/host.ts+++ b/apps/desktop/src/main/bot-import/host.ts@@ -580,8 +580,11 @@ export async function startCompanionImport(selection: CompanionImportSelection,result.status = 'needs-attention';}if (selection.deferSetup && result.saved && result.canonicalSessionId) {- await createMessage(result.canonicalSessionId, { clientId: `companion-import:${selection.requestId}`, role: 'assistant',- content: t(`bots.import.${result.checks.some(check => check.status === 'needs-attention') ? 'chatSetup' : 'chatReady'}`) });+ const pending = result.checks.some(check => check.status === 'needs-attention');+ // Keep the legacy setup ID across upgrades, but do not let its+ // idempotency key swallow the later successful completion message.+ await createMessage(result.canonicalSessionId, { clientId: `companion-import:${selection.requestId}${pending ? '' : ':ready'}`, role: 'assistant',+ content: 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; });diff --git a/apps/desktop/src/main/bot-import/skills.ts b/apps/desktop/src/main/bot-import/skills.tsindex 99cd46dd89..a57667bf30 100644--- a/apps/desktop/src/main/bot-import/skills.ts+++ b/apps/desktop/src/main/bot-import/skills.ts@@ -15,26 +15,31 @@ function sourcePath(value: string, base: string, home: string, env: NodeJS.Procereturn path.resolve(base, expanded === '~' ? home : expanded.startsWith('~/') ? path.join(home, expanded.slice(2)) : expanded);}-async function readJson(file: string): Promise<Record<string, unknown>> {- try { return object(JSON.parse(await fs.readFile(file, 'utf8'))); }+async function readJson(file: string, budget: ImportReadBudget): Promise<Record<string, unknown>> {+ try {+ // Native package managers may symlink the manifest itself. Preserve that+ // discovery behavior while bounding the resolved file before allocation.+ const real = await fs.realpath(file);+ return object(JSON.parse((await readImportFile(path.dirname(real), real, budget)).bytes.toString('utf8')));+ }catch (error) { if (['ENOENT', 'ENOTDIR'].includes((error as NodeJS.ErrnoException).code ?? '') || error instanceof SyntaxError) return {}; throw error; }}/** Locate the installed package through its executable; don't run source code during discovery. */-async function openClawInstall(env: NodeJS.ProcessEnv): Promise<string | undefined> {+async function openClawInstall(env: NodeJS.ProcessEnv, budget: ImportReadBudget): Promise<string | undefined> {const candidates = (env.PATH ?? '').split(path.delimiter).filter(Boolean).flatMap(dir => [path.join(dir, 'openclaw'), path.join(dir, 'node_modules', 'openclaw', 'package.json')]);for (const candidate of candidates) {let real: string;try { real = await fs.realpath(candidate); } catch { continue; }let dir = path.dirname(real);for (let depth = 0; depth < 5; depth++) {- if ((await readJson(path.join(dir, 'package.json'))).name === 'openclaw') return dir;+ if ((await readJson(path.join(dir, 'package.json'), budget)).name === 'openclaw') return dir;const parent = path.dirname(dir); if (parent === dir) break; dir = parent;}}}-async function rootsFor(source: ImportSource, values: Record<string, unknown>, home: string, env: NodeJS.ProcessEnv): Promise<SkillRoot[]> {+async function rootsFor(source: ImportSource, values: Record<string, unknown>, home: string, env: NodeJS.ProcessEnv, budget: ImportReadBudget): Promise<SkillRoot[]> {const config = object(values.skills);const resolve = (v: string) => sourcePath(v, source.root, home, env);if (source.kind === 'hermes') return [path.join(source.root, 'skills'), ...strings(config.external_dirs).map(resolve)].map(directory => ({ directory, links: 'any' }));@@ -47,7 +52,7 @@ async function rootsFor(source: ImportSource, values: Record<string, unknown>, h{ directory: path.join(source.root, 'skills'), links: 'any' },{ directory: path.join(source.root, 'agents', source.agentId, 'agent', 'workshop-skills'), links: trusted },];- const install = await openClawInstall(env);+ const install = await openClawInstall(env, budget);if (install) roots.push({ directory: path.join(install, 'skills'), links: trusted, bundled: true });for (const directory of strings(load.extraDirs).map(resolve)) roots.push({ directory, links: trusted });// A plugin's manifest declares its skill roots. Disabled plugins stay out of the catalog.@@ -60,7 +65,7 @@ async function rootsFor(source: ImportSource, values: Record<string, unknown>, hcatch (error) { if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error; }}for (const location of new Set(locations)) {- const manifest = await readJson(path.join(location, 'openclaw.plugin.json'));+ const manifest = await readJson(path.join(location, 'openclaw.plugin.json'), budget);const id = string(manifest.id) || path.basename(location);if (object(object(plugins.entries)[id]).enabled === false || strings(plugins.deny).includes(id)) continue;if (Array.isArray(plugins.allow) && !strings(plugins.allow).includes(id)) continue;@@ -82,7 +87,7 @@ export async function discoverImportSkills(source: ImportSource, values: Record<const disabled = new Set(strings(config.disabled));const platformDisabled = object(config.platform_disabled);for (const name of strings(platformDisabled.cli)) disabled.add(name);- for (const root of await rootsFor(source, values, home, env)) {+ for (const root of await rootsFor(source, values, home, env, budget)) {let realRoot: string;try { realRoot = await fs.realpath(root.directory); }catch (error) { if ((error as NodeJS.ErrnoException).code === 'ENOENT') continue; throw error; }