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): reset goal controller on account switch (#3117) (#3298)

Tsai Yuan893109077e72
diff --git a/apps/desktop/src/main/bootstrap-electron.ts b/apps/desktop/src/main/bootstrap-electron.ts
index 8761a5ae3a..e05827596a 100644
--- a/apps/desktop/src/main/bootstrap-electron.ts
+++ b/apps/desktop/src/main/bootstrap-electron.ts
@@ -1588,6 +1588,14 @@ async function teardownAuthAccountBoundary(reason: string): Promise<void> {
// 后续 owner 的正常收尾写全部吞掉,中断横幅会在每个正常完成的任务上误弹。
const releaseEndedSuppression = beginSessionTurnEndedSuppression();
try {
+ // GoalController captures the Maker and owner DB at construction time.
+ // Dispose it before replacing the Maker so a relogin cannot dispatch
+ // goals through a shutting-down runtime from the previous account.
+ try {
+ await resetGoalController();
+ } catch (err) {
+ authBoundaryLog.error(`resetGoalController on ${reason} failed (non-fatal):`, err);
+ }
// Same fence, same ordering argument, as quit and the update relaunch: raised
// before `maker.shutdown` so it covers the shutdown *and* the sweep below.
// `Maker.shutdown` reports per-session detach failures rather than throwing,
diff --git a/apps/desktop/src/main/goal-host/__tests__/controller.test.ts b/apps/desktop/src/main/goal-host/__tests__/controller.test.ts
index ec6c593761..a49937ff85 100644
--- a/apps/desktop/src/main/goal-host/__tests__/controller.test.ts
+++ b/apps/desktop/src/main/goal-host/__tests__/controller.test.ts
@@ -240,6 +240,10 @@ class FakeSession implements SessionLike {
};
}
+ hasListener(): boolean {
+ return this.listener !== null;
+ }
+
isTurnRunning(): boolean {
return this.running;
}
@@ -3241,6 +3245,47 @@ describe('GoalController', () => {
expect(h.session.sends).toHaveLength(0);
});
+ it('does not let a startup resume scan repopulate runtime state after disposal', async () => {
+ const local = makeController();
+ const active = seededGoal({ status: 'active', objective: 'old account goal' });
+ await local.storage.set(active);
+ let releaseList!: (states: GoalState[]) => void;
+ const blockedList = new Promise<GoalState[]>((resolve) => {
+ releaseList = resolve;
+ });
+ vi.spyOn(local.storage, 'listActive').mockReturnValueOnce(blockedList);
+
+ const startupResume = local.controller.resumeActiveGoals();
+ local.controller.dispose();
+ releaseList([active]);
+ await startupResume;
+
+ expect(local.session.hasListener()).toBe(false);
+ expect(local.session.sends).toHaveLength(0);
+ expect(local.updates).toHaveLength(0);
+ });
+
+ it('does not let a per-goal startup lookup attach after disposal', async () => {
+ const local = makeController();
+ const active = seededGoal({ status: 'active', objective: 'old account goal' });
+ await local.storage.set(active);
+ let releaseGet!: (state: GoalState | null) => void;
+ const blockedGet = new Promise<GoalState | null>((resolve) => {
+ releaseGet = resolve;
+ });
+ vi.spyOn(local.storage, 'get').mockReturnValueOnce(blockedGet);
+
+ const startupResume = local.controller.resumeActiveGoals();
+ await vi.waitFor(() => expect(local.storage.get).toHaveBeenCalledWith('s1'));
+ local.controller.dispose();
+ releaseGet(active);
+ await startupResume;
+
+ expect(local.session.hasListener()).toBe(false);
+ expect(local.session.sends).toHaveLength(0);
+ expect(local.updates).toHaveLength(0);
+ });
+
it('resumeOnOpen 在释放 route 锁前挂 listener,并在会话随即关闭后迁移到重建会话', async () => {
const firstSession = new FakeSession('s1', 'claude-code');
const recreatedSession = new FakeSession('s1', 'claude-code');
diff --git a/apps/desktop/src/main/goal-host/controller.ts b/apps/desktop/src/main/goal-host/controller.ts
index a3746c5ccb..442d6b0abc 100644
--- a/apps/desktop/src/main/goal-host/controller.ts
+++ b/apps/desktop/src/main/goal-host/controller.ts
@@ -1504,16 +1504,16 @@ export class GoalController {
// switch can dispose the controller while either storage query is waiting;
// check the terminal fence after every await so the old maker cannot be
// reattached or receive a resumed turn after teardown.
- if (this.disposed) return;
+ if (this.disposed || this.disposing) return;
const active = await this.deps.storage.listActive();
- if (this.disposed) return;
+ if (this.disposed || this.disposing) return;
let resumed = 0;
for (const snapshot of active) {
- if (this.disposed) return;
+ if (this.disposed || this.disposing) return;
// listActive 是启动扫描快照;并发 Stop 可能已经立 cancelled boundary 或写成 paused。
if (this.turns.has(snapshot.sessionId)) continue;
const state = await this.deps.storage.get(snapshot.sessionId);
- if (this.disposed) return;
+ if (this.disposed || this.disposing) return;
if (!state || state.status !== 'active' || this.turns.has(snapshot.sessionId)) continue;
// 保守:只对**已经活着**的会话重挂 + 续跑;不在启动时强行 spawn agent
//(开机就偷偷跑目标过于激进)。没活的留 dormant,等用户重发 /goal 时由
@@ -1544,12 +1544,12 @@ export class GoalController {
// usageLimited 行:重启后 timer 丢了,按存档的 usageResetAt 重排自动续跑
//(已过点 → delay 0 触发;未知 resetAt → 不排,留待手动 resume)。
- if (this.disposed) return;
+ if (this.disposed || this.disposing) return;
const limited = await this.deps.storage.listUsageLimited();
- if (this.disposed) return;
+ if (this.disposed || this.disposing) return;
let rescheduled = 0;
for (const g of limited) {
- if (this.disposed) return;
+ if (this.disposed || this.disposing) return;
if (g.usageResetAt == null) continue;
this.scheduleUsageResume(g.sessionId, g.usageResetAt);
rescheduled += 1;
diff --git a/apps/desktop/src/main/maker-host/__tests__/accountProviderReadinessWiring.test.ts b/apps/desktop/src/main/maker-host/__tests__/accountProviderReadinessWiring.test.ts
index 582694c6b8..7fa27d385a 100644
--- a/apps/desktop/src/main/maker-host/__tests__/accountProviderReadinessWiring.test.ts
+++ b/apps/desktop/src/main/maker-host/__tests__/accountProviderReadinessWiring.test.ts
@@ -229,6 +229,22 @@ describe('account provider readiness wiring', () => {
expect(clearAfterJoin).toBeGreaterThan(joinPrevious);
});
+ it('resets the goal controller before the outgoing Maker is shut down', () => {
+ const teardown = bootstrapSource.indexOf('async function teardownAuthAccountBoundary');
+ const resetGoal = bootstrapSource.indexOf('resetGoalController();', teardown);
+ const drainRecreatedGoal = bootstrapSource.indexOf('await resetGoalController();', resetGoal + 1);
+ const makerShutdown = bootstrapSource.indexOf(
+ "await maker.shutdown({ reason: 'account-boundary' })",
+ teardown,
+ );
+
+ expect(teardown).toBeGreaterThanOrEqual(0);
+ expect(resetGoal).toBeGreaterThan(teardown);
+ expect(drainRecreatedGoal).toBeGreaterThan(resetGoal);
+ expect(drainRecreatedGoal).toBeLessThan(makerShutdown);
+ expect(resetGoal).toBeLessThan(makerShutdown);
+ });
+
it('stops every PI Subagent runner of the outgoing owner at the account boundary', () => {
const teardown = bootstrapSource.indexOf('async function teardownAuthAccountBoundary');
const makerShutdown = bootstrapSource.indexOf(