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): close plugin runtime recovery races

Yuewei-LL42049429ca54
diff --git a/apps/desktop/src/main/cindy-brain/GhostManager.ts b/apps/desktop/src/main/cindy-brain/GhostManager.ts
index d567e1126c..8cecbd46b0 100644
--- a/apps/desktop/src/main/cindy-brain/GhostManager.ts
+++ b/apps/desktop/src/main/cindy-brain/GhostManager.ts
@@ -3567,6 +3567,14 @@ export class GhostManager {
}
this.options.recordBuiltinTombstone(id);
} catch (err) {
+ // Tombstone persistence is part of the uninstall transaction. If it
+ // fails, roll back the pending journal and in-process quarantine so a
+ // reported failure cannot be completed by startup recovery later.
+ const journalCleared = await this.receiptStore
+ .clearPendingMutation(id)
+ .then(() => true)
+ .catch(() => false);
+ if (journalCleared) this.untrustedApprovals.delete(this.isolationKey(id));
return {
rejection: { code: 'io', reason: err instanceof Error ? err.message : String(err) },
};
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 dce413dac0..fab3da2221 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts
@@ -2910,7 +2910,7 @@ describe('GhostManager · uninstall', () => {
expect(manager.list()).toEqual([]);
});
- it('keeps builtin uninstall journal and bytes quarantined when tombstone persistence fails', async () => {
+ it('rolls back builtin uninstall journal and quarantine when tombstone persistence fails', async () => {
await manager.install(await makeCindy('a.cindy', goodManifest()));
trustedBundledIds.add('hello');
recordBuiltinTombstone.mockImplementationOnce(() => {
@@ -2927,17 +2927,12 @@ describe('GhostManager · uninstall', () => {
expect(fs.existsSync(path.join(workDir, 'ghosts-install-state', 'hello.json'))).toBe(true);
expect(manager.list()[0]).toMatchObject({
manifest: { id: 'hello' },
- enabled: false,
- approval: { state: 'invalid' },
+ enabled: true,
+ approval: { state: 'approved' },
});
- expect(
- JSON.parse(
- await fs.promises.readFile(
- path.join(workDir, 'ghosts-install-state', '.pending-hello.json'),
- 'utf8',
- ),
- ),
- ).toMatchObject({ kind: 'uninstall', builtinTombstone: true });
+ expect(fs.existsSync(path.join(workDir, 'ghosts-install-state', '.pending-hello.json'))).toBe(
+ false,
+ );
});
it('recovers a crashed builtin uninstall by persisting its tombstone before deletion', async () => {
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/nodeRuntimeBroker.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/nodeRuntimeBroker.test.ts
index 3b3922abaf..fa3d112d76 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/nodeRuntimeBroker.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/nodeRuntimeBroker.test.ts
@@ -142,6 +142,45 @@ describe('nodeRuntimeBroker owner boundary races', () => {
expect(child.killed).toBe(true);
expect(invalidated).toHaveBeenCalledWith('node-ghost');
});
+
+ it('a stale owner callback does not stop a fresh worker for the same ghost', async () => {
+ let generation = 1;
+ const ghost = fakeGhost({ lifecycle: 'resident' });
+ const staleChild = new FakeNodeProcess(undefined, false);
+ const freshChild = new FakeNodeProcess(undefined, false);
+ const invalidated = vi.fn();
+ const spawnProcess = vi.fn(
+ () => (spawnProcess.mock.calls.length === 1 ? staleChild : freshChild) as unknown as NodeWorkerProcess,
+ );
+ const broker = new GhostNodeRuntimeBroker({
+ getGhost: () => ghost,
+ spawnProcess,
+ ownerScope: {
+ capture: () => generation,
+ isCurrent: (scope) => scope === generation,
+ isStable: (scope) => scope === generation,
+ onInvalidated: invalidated,
+ },
+ });
+
+ const firstStart = broker.startResident(ghost);
+ await vi.waitFor(() => expect(spawnProcess).toHaveBeenCalledTimes(1));
+ staleChild.emit('spawn');
+ await firstStart;
+ generation = 2;
+ broker.stop('node-ghost');
+ const secondStart = broker.startResident(ghost);
+ await vi.waitFor(() => expect(spawnProcess.mock.calls.length).toBeGreaterThanOrEqual(2));
+ freshChild.emit('spawn');
+ await secondStart;
+
+ staleChild.stderr.write('stale owner callback\n');
+ await vi.waitFor(() => expect(staleChild.killed).toBe(true));
+
+ expect(freshChild.killed).toBe(false);
+ expect(broker.stateOf('node-ghost')).toBe('running');
+ expect(invalidated).not.toHaveBeenCalled();
+ });
});
function makeAutoReplyProcess(methods?: string[]) {
diff --git a/apps/desktop/src/main/cindy-brain/nodeRuntimeBroker.ts b/apps/desktop/src/main/cindy-brain/nodeRuntimeBroker.ts
index 12c7c7cbcc..ab0caafc0d 100644
--- a/apps/desktop/src/main/cindy-brain/nodeRuntimeBroker.ts
+++ b/apps/desktop/src/main/cindy-brain/nodeRuntimeBroker.ts
@@ -1408,8 +1408,19 @@ export class GhostNodeRuntimeBroker {
private ownerScopeUsable(ghostId: string, captured: unknown): boolean {
if (isGhostOwnerScopeUsable(this.deps.ownerScope, captured)) return true;
- this.deps.ownerScope?.onInvalidated?.(ghostId);
- this.stop(ghostId);
+ let hasCurrentWorker = false;
+ // Tear down every stale worker for this ghost, but preserve a worker that
+ // already belongs to the new owner. The runtime-level invalidation callback
+ // is only safe when no fresh generation exists for the same ghost.
+ for (const [key, entry] of [...this.workers]) {
+ if (entry.ghost.manifest.id !== ghostId) continue;
+ if (isGhostOwnerScopeUsable(this.deps.ownerScope, entry.ownerScopeSnapshot)) {
+ hasCurrentWorker = true;
+ continue;
+ }
+ this.stopWorker(key, entry);
+ }
+ if (!hasCurrentWorker) this.deps.ownerScope?.onInvalidated?.(ghostId);
return false;
}