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(desktop): fail closed on unreadable migration markers

Yuewei-LL35d414fe03d3
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts
index f84df88118..3a1633ddd7 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts
@@ -1281,6 +1281,32 @@ describe('GhostManager · review 第 6 轮回归(P0/P1 修复钉住)', () => {
expect(manager.list()[0].approval.state).toBe('legacy-unapproved');
});
+ it('P0-2: unreadable per-id migration marker keeps deleted receipt fail-closed', async () => {
+ await manager.install(await makeCindy('a.cindy', goodManifest()));
+ await fs.promises.rm(path.join(workDir, 'ghosts-install-state', 'hello.json'));
+
+ const marker = path.join(workDir, 'ghosts-install-state', '.migrated-hello');
+ const realLstatSync = fs.lstatSync;
+ const spy = vi.spyOn(fs, 'lstatSync').mockImplementation((target, options) => {
+ if (path.resolve(String(target)) === path.resolve(marker)) {
+ throw Object.assign(new Error('EIO: migration marker unreadable'), { code: 'EIO' });
+ }
+ return realLstatSync(target, options as never);
+ });
+ try {
+ expect(await manager.migrateLegacyApprovalsOnce()).toEqual({
+ migrated: [],
+ skipped: ['hello'],
+ failed: [],
+ retryPending: [],
+ });
+ } finally {
+ spy.mockRestore();
+ }
+
+ expect(manager.list()[0].approval.state).toBe('legacy-unapproved');
+ });
+
it('P0-2:安装不自动落 migration ledger —— 落账失败也不影响安装', async () => {
// With the ledger auto-close removed, the install path no longer touches
// the migration ledger at all. A ledger I/O failure cannot block the
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/ghostInstallReceipt.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/ghostInstallReceipt.test.ts
index 6246964432..e2914078b6 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/ghostInstallReceipt.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/ghostInstallReceipt.test.ts
@@ -4,7 +4,10 @@ import path from 'node:path';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
-import { GhostInstallReceiptStore } from '../ghostInstallReceipt';
+import {
+ createGhostInstallReceipt,
+ GhostInstallReceiptStore,
+} from '../ghostInstallReceipt';
describe('GhostInstallReceiptStore cleanup', () => {
let workDir: string;
@@ -64,6 +67,59 @@ describe('GhostInstallReceiptStore cleanup', () => {
expect(() => store.removeSync('hello')).toThrow('state root unreadable');
});
+ it('treats an unreadable migration marker as present', () => {
+ const marker = path.join(stateRoot, '.migrated-hello');
+ const realLstatSync = fs.lstatSync;
+ vi.spyOn(fs, 'lstatSync').mockImplementation((target, options) => {
+ if (path.resolve(String(target)) === path.resolve(marker)) {
+ throw Object.assign(new Error('migration marker unreadable'), { code: 'EACCES' });
+ }
+ return realLstatSync(target, options as never);
+ });
+
+ expect(store.hasMigrationMarker('hello')).toBe(true);
+ });
+
+ it('does not publish a receipt when migration marker state is unavailable', async () => {
+ const marker = path.join(stateRoot, '.migrated-hello');
+ const realLstatSync = fs.lstatSync;
+ vi.spyOn(fs, 'lstatSync').mockImplementation((target, options) => {
+ if (path.resolve(String(target)) === path.resolve(marker)) {
+ throw Object.assign(new Error('migration marker unavailable'), { code: 'EIO' });
+ }
+ return realLstatSync(target, options as never);
+ });
+
+ const receipt = createGhostInstallReceipt({
+ manifest: {
+ schemaVersion: 2,
+ id: 'hello',
+ name: 'Hello',
+ version: '1.0.0',
+ kind: 'chip',
+ entry: 'main.js',
+ slots: ['tool'],
+ tools: [{ name: 'do_thing', description: 'Do something' }],
+ },
+ localeResources: {},
+ enabled: true,
+ trust: {
+ level: 'unverified',
+ publisherSigned: false,
+ publisherVerified: false,
+ reviewed: false,
+ },
+ skillContentSha256: {},
+ });
+
+ await expect(store.write(receipt)).rejects.toThrow('migration marker unavailable');
+ expect(fs.existsSync(path.join(stateRoot, 'hello.json'))).toBe(false);
+ });
+
+ it('treats only a missing migration marker as absent', () => {
+ expect(store.hasMigrationMarker('hello')).toBe(false);
+ });
+
it('rejects a linked snapshot root without touching its target', async () => {
const external = path.join(workDir, 'external');
const sentinel = path.join(external, 'hello', 'sentinel.txt');
diff --git a/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts b/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts
index 8f5be45020..a41f1f694c 100644
--- a/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts
+++ b/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts
@@ -548,7 +548,9 @@ export class GhostInstallReceiptStore {
// ERROR: temp write or link/rename failed for a reason other than
// EEXIST — fail the receipt write, do not publish the receipt
// without the per-id migration guard.
- if (this.hasMigrationMarker(id)) return false;
+ const initialState = this.migrationMarkerState(id);
+ if (initialState.state === 'present') return false;
+ if (initialState.state === 'unavailable') throw initialState.error;
const root = this.rootDir();
await fs.promises.mkdir(root, { recursive: true });
const target = this.migrationMarkerPath(id);
@@ -569,7 +571,9 @@ export class GhostInstallReceiptStore {
}
if ((error as NodeJS.ErrnoException).code === 'EXDEV') {
// Cross-device: re-check before rename so we don't clobber.
- if (this.hasMigrationMarker(id)) return false;
+ const stateBeforeRename = this.migrationMarkerState(id);
+ if (stateBeforeRename.state === 'present') return false;
+ if (stateBeforeRename.state === 'unavailable') throw stateBeforeRename.error;
await fs.promises.rename(temp, target);
} else {
throw error;
@@ -592,11 +596,25 @@ export class GhostInstallReceiptStore {
/** 是否存在 per-id 迁移标记(表明此 id 是通过新模型安装,而非 legacy)。 */
hasMigrationMarker(id: string): boolean {
+ // The migration coordinator only re-opens legacy candidacy on definite
+ // absence. Unknown marker state must keep backfill closed.
+ return this.migrationMarkerState(id).state !== 'missing';
+ }
+
+ private migrationMarkerState(
+ id: string,
+ ):
+ | { state: 'present' }
+ | { state: 'missing' }
+ | { state: 'unavailable'; error: unknown } {
try {
fs.lstatSync(this.migrationMarkerPath(id));
- return true;
- } catch {
- return false;
+ return { state: 'present' };
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
+ return { state: 'missing' };
+ }
+ return { state: 'unavailable', error };
}
}