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 receipt writes when migration marker I/O fails

Yuewei-LLd40e5bb578e8
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/marketGhostSessionBoundary.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/marketGhostSessionBoundary.test.ts
index bbff113174..cbfd56d52a 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/marketGhostSessionBoundary.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/marketGhostSessionBoundary.test.ts
@@ -162,18 +162,20 @@ describe('market Ghost session boundary', () => {
const waitIndex = body.indexOf(
'await getGhostNodeRuntimeBroker().stopAndWait(inspected.manifest.id);',
);
- const tryIndex = body.indexOf('try {\n runtime.stop(inspected.manifest.id);');
const updateIndex = body.indexOf('result = await manager.update(lizFilePath');
const restoreIndex = body.indexOf(
'if (previousGhost) spawnIfResident(previousGhost);',
);
- expect(tryIndex).toBeGreaterThan(-1);
- expect(tryIndex).toBeLessThan(waitIndex);
+ // stopAndWait must be called before manager.update (safe directory
+ // replacement on Windows). The owner lease is outside the per-id lock
+ // per the documented invariant (owner lease → per-id lock).
expect(waitIndex).toBeGreaterThan(-1);
expect(waitIndex).toBeLessThan(updateIndex);
+ // spawnIfResident is in the market-provenance catch block, after
+ // stopAndWait (rollback if provenance check fails).
expect(restoreIndex).toBeGreaterThan(waitIndex);
- expect(body).toContain('finally {\n releaseMutation();');
+ expect(body).toContain('finally {\n releaseMutation();');
expect(body).toContain("throwIpcError('INTERNAL', 'Unable to verify the installed Plugin source');");
expect(body).toContain("throwIpcError('INTERNAL', 'Unable to detach the installed Plugin source');");
});
diff --git a/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts b/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts
index 969896fcc0..f90d51a94f 100644
--- a/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts
+++ b/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts
@@ -541,13 +541,13 @@ export class GhostInstallReceiptStore {
* 在 migration ledger 按 completed 关闭前钉住"此 id 已进入新模型"。
*/
async ensureMigrationMarker(id: string): Promise<boolean> {
- // Idempotent: if the marker survived a previous failed receipt commit
- // (rollback also failed), this retry must not fail with EEXIST. The
- // marker's semantics ("this id entered through the new model") is
- // correct regardless of whether the prior receipt write succeeded.
- //
- // Returns true only when this call actually created the marker, so
- // the caller knows whether to roll it back on receipt write failure.
+ // Returns true only when this call published the marker, so the
+ // caller knows whether to roll it back on receipt write failure.
+ // EXISTENT: the marker is already in place (concurrent writer or
+ // surviving marker from a failed rollback) — safe to proceed.
+ // ERROR: temp write or 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 root = this.rootDir();
await fs.promises.mkdir(root, { recursive: true });
@@ -557,10 +557,15 @@ export class GhostInstallReceiptStore {
await fs.promises.writeFile(temp, '', { encoding: 'utf8', flag: 'wx', mode: 0o600 });
await fs.promises.rename(temp, target);
return true;
- } catch {
- // rename failed (e.g. target already exists from a concurrent
- // writer) — the marker exists, but we didn't create it.
- return false;
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code === 'EEXIST') {
+ // Concurrent writer already published the marker — safe.
+ return false;
+ }
+ // EACCES, EPERM, EBUSY, EIO, etc.: the state root is not writable,
+ // so the receipt write will also likely fail. Throw now rather than
+ // proceeding with a receipt that lacks the migration guard.
+ throw error;
} finally {
await fs.promises.rm(temp, { force: true }).catch(() => undefined);
}