Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(companions): serialize skill catalog builds across cache eviction
Chris5871fcba51d9
diff --git a/apps/desktop/src/main/bot-import/README.md b/apps/desktop/src/main/bot-import/README.mdindex 76e71ffda5..e801485665 100644--- a/apps/desktop/src/main/bot-import/README.md+++ b/apps/desktop/src/main/bot-import/README.md@@ -101,6 +101,9 @@ omitting it retains normal companion defaults, and a rejected request can be cordirectory identity checks cover hand edits, moves and replacement. Watchererrors fall back to rebuilding, and missing generated catalogs are recreated.The cache retains at most 32 roots, not a maximum number of stored Skills.+ In-flight catalog writers are serialized per root independently of cache+ eviction, so an evicted snapshot cannot overwrite its newer replacement.+ Failed writers release their successor; idle writer records are removed.Initial hydration uses `opendir` and bounded header reads, writing JSONL chunksas entries arrive. Only the native-mount byte budget is retained/sorted; a largeshelf is never collected into a runtime array. Giant metadata lines keep shortdiff --git a/apps/desktop/src/main/maker-ipc/__tests__/botSkillRuntimeCache.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/botSkillRuntimeCache.test.tsindex 74f7996884..e7d5b4093f 100644--- a/apps/desktop/src/main/maker-ipc/__tests__/botSkillRuntimeCache.test.ts+++ b/apps/desktop/src/main/maker-ipc/__tests__/botSkillRuntimeCache.test.ts@@ -3,10 +3,11 @@ import os from 'node:os';import path from 'node:path';import { afterEach, beforeEach, expect, it, vi } from 'vitest';import { cachedBotSkillRuntime, invalidateBotSkillRuntime } from '../botSkillRuntimeCache';+import { projectBotSkillMounts } from '../botSkillRuntimeProjection';let root: string;beforeEach(async () => { root = await fs.mkdtemp(path.join(os.tmpdir(), 'cindy-skill-cache-')); });-afterEach(async () => { await fs.rm(root, { recursive: true, force: true }); });+afterEach(async () => { vi.restoreAllMocks(); await fs.rm(root, { recursive: true, force: true }); });const projection = (name: string) => ({ pluginRoot: root,skills: [{ name, description: name, path: root, filePath: path.join(root, 'SKILL.md') }] });@@ -46,3 +47,106 @@ it('retries failed catalog builds and never caches an unwatchable source', asyncawait cachedBotSkillRuntime(missing, uncached);expect(uncached).toHaveBeenCalledTimes(2);});++it.each([false, true])('serializes catalog writers after eviction, including failed predecessors (%s)', async (failOld) => {+ let release!: () => void;+ const held = new Promise<void>(resolve => { release = resolve; });+ const events: string[] = [];+ const item = (slug: string) => ({ slug, name: slug, description: 'Fixture', updatedAt: '',+ dirPath: path.join(root, 'skills', slug), filePath: path.join(root, 'skills', slug, 'SKILL.md'),+ frontmatterBytes: 20_000, bodyStartLine: 5 });+ const oldBuild = vi.fn(async () => {+ async function* oldSnapshot() {+ yield item('before');+ events.push('old-reading');+ await held;+ if (failOld) throw new Error('old build failed');+ }+ try { return await projectBotSkillMounts(root, oldSnapshot()); }+ finally { events.push('old-finished'); }+ });+ // Capture failures immediately, so the failed-predecessor case cannot produce+ // an unhandled rejection while its replacement is still queued.+ const first = cachedBotSkillRuntime(root, oldBuild).then(value => ({ value }), error => ({ error }));+ let second: ReturnType<typeof cachedBotSkillRuntime> | undefined;+ try {+ await vi.waitFor(() => expect(events).toEqual(['old-reading']));+ // More roots than the LRU can retain; the old writer stays in flight.+ await Promise.all(Array.from({ length: 33 }, async (_, index) => {+ const other = path.join(root, `other-${index}`);+ await fs.mkdir(other);+ return cachedBotSkillRuntime(other, async () => ({ pluginRoot: other, skills: [] }));+ }));+ const replacement = vi.fn(async () => {+ events.push('new-reading');+ return projectBotSkillMounts(root, [item('before'), item('added')]);+ });+ const stats = vi.spyOn(fs, 'stat');+ second = cachedBotSkillRuntime(root, replacement);+ // Wait for this hydration's source checks and then one event-loop turn. No+ // source I/O remains between those checks and entering the writer queue.+ await vi.waitFor(() => expect(stats.mock.calls.some(([file]) => file === root)).toBe(true));+ await Promise.allSettled(stats.mock.results.map(result => result.value));+ await new Promise<void>(resolve => setImmediate(resolve));+ expect(replacement).not.toHaveBeenCalled();+ release();+ await second;+ const outcome = await first;+ if (failOld) expect(outcome).toMatchObject({ error: new Error('old build failed') });+ else expect(outcome).toHaveProperty('value');+ expect(events).toEqual(['old-reading', 'old-finished', 'new-reading']);+ const catalog = path.join(root, '.runtime-skills', 'catalog.jsonl');+ expect((await fs.readFile(catalog, 'utf8')).trim().split('\n').map(line => JSON.parse(line).slug))+ .toEqual(['before', 'added']);+ expect((await fs.readdir(path.dirname(catalog))).some(file => file.endsWith('.tmp'))).toBe(false);+ // The evicted build's completion/failure must not clear the replacement.+ await cachedBotSkillRuntime(root, replacement);+ expect(replacement).toHaveBeenCalledTimes(1);+ invalidateBotSkillRuntime(root);+ await cachedBotSkillRuntime(root, replacement);+ expect(replacement).toHaveBeenCalledTimes(2);+ } finally {+ release();+ await Promise.allSettled([first, ...(second ? [second] : [])]);+ }+});++it('rejoins the active entry when evicted during a cached artifact check', async () => {+ const pluginRoot = path.join(root, '.runtime-skills');+ await fs.mkdir(pluginRoot);+ const catalog = path.join(pluginRoot, 'catalog.jsonl');+ const filePath = path.join(pluginRoot, 'SKILL.md');+ await fs.writeFile(filePath, 'Fixture');+ const build = (name: string) => async () => {+ await fs.writeFile(catalog, name);+ return { pluginRoot, skills: [{ name, description: name, path: pluginRoot, filePath }] };+ };+ const initial = vi.fn(build('old'));+ await cachedBotSkillRuntime(root, initial);+ let release!: () => void;+ const held = new Promise<void>(resolve => { release = resolve; });+ const access = fs.access;+ const checks = vi.spyOn(fs, 'access').mockImplementationOnce(async (...args) => {+ await held;+ return access(...args);+ });+ const warm = cachedBotSkillRuntime(root, initial);+ try {+ await vi.waitFor(() => expect(checks).toHaveBeenCalled());+ await Promise.all(Array.from({ length: 33 }, async (_, index) => {+ const other = path.join(root, `other-${index}`);+ await fs.mkdir(other);+ await cachedBotSkillRuntime(other, async () => ({ pluginRoot: other, skills: [] }));+ }));+ const replacement = vi.fn(build('new'));+ await cachedBotSkillRuntime(root, replacement);+ release();+ expect((await warm).skills[0].name).toBe('new');+ expect(await fs.readFile(catalog, 'utf8')).toBe('new');+ expect(initial).toHaveBeenCalledTimes(1);+ expect(replacement).toHaveBeenCalledTimes(1);+ } finally {+ release();+ await warm;+ }+});diff --git a/apps/desktop/src/main/maker-ipc/botSkillRuntimeCache.ts b/apps/desktop/src/main/maker-ipc/botSkillRuntimeCache.tsindex e07fbb3e9d..64260201a0 100644--- a/apps/desktop/src/main/maker-ipc/botSkillRuntimeCache.ts+++ b/apps/desktop/src/main/maker-ipc/botSkillRuntimeCache.ts@@ -17,6 +17,10 @@ type Entry = {// This bounds idle watcher handles across owners/bots without limiting Skills.const entries = new Map<string, Entry>();const MAX_CACHED_ROOTS = 32;+// Writer ownership outlives LRU entries: an evicted build can still rename its+// staged catalog. Queue replacements per root until that build has fully settled.+// Only pending work is retained here; idle roots still use the bounded LRU above.+const writers = new Map<string, Promise<Projection>>();export function invalidateBotSkillRuntime(root: string): void {const entry = entries.get(path.resolve(root));@@ -96,12 +100,19 @@ export async function cachedBotSkillRuntime(root: string, build: () => Promise<Pfs.access(path.join(current.value.pluginRoot, 'catalog.jsonl')),fs.access(current.value.skills[0].filePath),]).then(() => true, () => false);+ // Eviction may happen while checking generated files. Rejoin the active+ // entry rather than enqueueing work from a now-discarded cache entry.+ if (entries.get(root) !== current) return cachedBotSkillRuntime(root, build);if (present && current.loadedRevision === current.revision) return structuredClone(current.value);if (!present) current.revision++;}// Another hydration may have started rebuilding during the artifact check.if (current.loading) return structuredClone(await current.loading);- current.loading = (async () => {+ const previous = writers.get(root);+ const loading = (async () => {+ // A failed predecessor has already cleaned up its staging files; it must+ // neither poison this retry nor race its final writes against this build.+ await previous?.catch(() => undefined);let value: Projection;let revision: number;do {@@ -112,7 +123,12 @@ export async function cachedBotSkillRuntime(root: string, build: () => Promise<Pcurrent.loadedRevision = revision;return structuredClone(value);})();- try { return await current.loading; }+ current.loading = loading;+ writers.set(root, loading);+ try { return await loading; }catch (error) { discard(root, current); throw error; }- finally { current.loading = undefined; }+ finally {+ if (writers.get(root) === loading) writers.delete(root);+ current.loading = undefined;+ }}