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: address review — preserve Goal retry owner

Chris7c48e3002947
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 81dcc268e9..7614b25c59 100644
--- a/apps/desktop/src/main/goal-host/__tests__/controller.test.ts
+++ b/apps/desktop/src/main/goal-host/__tests__/controller.test.ts
@@ -705,6 +705,91 @@ describe('GoalController', () => {
}
});
+ it.each(['create', 'edit'] as const)(
+ 'preserves the %s lifecycle when the final status read fails after provider rejection',
+ async (mode) => {
+ vi.useFakeTimers();
+ vi.setSystemTime(1_000);
+ const local = makeController({ now: () => Date.now() });
+ const originalGet = local.storage.get.bind(local.storage);
+ let failNextRead = false;
+ vi.spyOn(local.storage, 'get').mockImplementation(async (sessionId) => {
+ if (failNextRead) {
+ failNextRead = false;
+ throw new Error('final status read unavailable');
+ }
+ return originalGet(sessionId);
+ });
+ let attempts = 0;
+ vi.spyOn(local.session, 'send').mockImplementation(async (
+ message: Parameters<FakeSession['send']>[0],
+ opts: Parameters<FakeSession['send']>[1],
+ ): Promise<SessionSendResult> => {
+ const content = typeof message === 'string' ? message : message.content;
+ local.session.sends.push({ content, originKind: opts?.origin?.kind });
+ attempts += 1;
+ opts?.onDispatching?.();
+ if (attempts === 1) {
+ failNextRead = true;
+ return { accepted: false, reason: 'provider-rejected-before-dispatch' };
+ }
+ return { accepted: true };
+ });
+ const internals = local.controller as unknown as {
+ turns: Map<string, unknown>;
+ timers: Map<string, ReturnType<typeof setTimeout>>;
+ unsubscribers: Map<string, () => void>;
+ dispatchRejectionRetries: Map<string, unknown>;
+ };
+
+ try {
+ if (mode === 'edit') {
+ await local.storage.set(seededGoal({ status: 'paused', objective: 'old objective' }));
+ }
+
+ await expect(
+ local.controller.setGoal({ sessionId: 's1', objective: 'replacement objective' }),
+ ).rejects.toThrow('final status read unavailable');
+
+ expect(local.session.sends).toHaveLength(1);
+ expect(internals.turns.has('s1')).toBe(true);
+ expect(internals.timers.has('s1')).toBe(true);
+ expect(internals.unsubscribers.has('s1')).toBe(true);
+ expect(internals.dispatchRejectionRetries.has('s1')).toBe(true);
+ expect(await originalGet('s1')).toMatchObject({
+ status: 'active',
+ objective: 'replacement objective',
+ turnsUsed: 0,
+ });
+
+ // Opening an already managed Goal must not bypass the preserved timer.
+ await local.controller.resumeOnOpen('s1');
+ expect(local.session.sends).toHaveLength(1);
+ await vi.advanceTimersByTimeAsync(499);
+ expect(local.session.sends).toHaveLength(1);
+ await vi.advanceTimersByTimeAsync(1);
+ expect(local.session.sends).toHaveLength(2);
+ expect(internals.dispatchRejectionRetries.has('s1')).toBe(false);
+ expect(internals.unsubscribers.has('s1')).toBe(true);
+
+ local.session.emitGoalTurn({
+ toolUse: true,
+ verdictJson: '```json\n{"goal_status":"complete","reason":"done"}\n```',
+ tokens: 10,
+ });
+ await vi.waitFor(async () => {
+ expect(await originalGet('s1')).toBeNull();
+ });
+ expect(internals.turns.has('s1')).toBe(false);
+ expect(internals.timers.has('s1')).toBe(false);
+ expect(internals.unsubscribers.has('s1')).toBe(false);
+ } finally {
+ local.controller.dispose();
+ vi.useRealTimers();
+ }
+ },
+ );
+
it('backs off persistent provider rejection and blocks after four confirmed non-dispatches', async () => {
vi.useFakeTimers();
vi.setSystemTime(1_000);
diff --git a/apps/desktop/src/main/goal-host/controller.ts b/apps/desktop/src/main/goal-host/controller.ts
index e36576ab97..83ea5e5189 100644
--- a/apps/desktop/src/main/goal-host/controller.ts
+++ b/apps/desktop/src/main/goal-host/controller.ts
@@ -580,6 +580,7 @@ export class GoalController {
);
this.turns.set(sessionId, editBoundary);
let editObjectivePersisted = false;
+ let updatedState: GoalState | null = null;
try {
if (sessionWasBusy) {
await session.abort();
@@ -614,6 +615,7 @@ export class GoalController {
this.turns.delete(sessionId);
return null;
}
+ updatedState = updated;
this.resetTurn(sessionId);
const activeBoundary = this.turns.get(sessionId);
this.attachListener(sessionId);
@@ -621,7 +623,6 @@ export class GoalController {
if (this.turns.get(sessionId) === activeBoundary) {
await this.fireTurn(sessionId, { throwOnRestoreFailure: true });
}
- return (await this.deps.storage.get(sessionId)) ?? updated;
} catch (error) {
if (this.turns.get(sessionId) === editBoundary) {
if (rejectionTakeover) {
@@ -639,6 +640,9 @@ export class GoalController {
}
throw error;
}
+ // This is only a return-value refresh. The Goal lifecycle is already
+ // established, so a read failure must not tear down its owner or retry.
+ return (await this.deps.storage.get(sessionId)) ?? updatedState;
}
const limits = input.limits ?? this.deps.getDefaults();
@@ -650,6 +654,7 @@ export class GoalController {
previousBoundary?.pendingCompletion ?? null,
);
this.turns.set(sessionId, createBoundary);
+ let createdState: GoalState | null = null;
try {
// 先活化(resume)会话,再据活化后的会话定 agentKind:dormant(重启后尚未活化)会话此刻
// getSession 为空,若直接 fallback 'claude-code' 会把 Codex 目标错存成 claude-code,后续
@@ -681,6 +686,7 @@ export class GoalController {
startedAt: ts,
updatedAt: ts,
};
+ createdState = state;
await this.trackPersistence(createBoundary, this.deps.storage.upsert(state), async () => {
this.clarificationApplied.delete(sessionId);
// 目标创建 → 落一条目标文案作对话起点(updated:false),**只此一次**。
@@ -697,11 +703,13 @@ export class GoalController {
if (this.turns.get(sessionId) === activeBoundary) {
await this.fireTurn(sessionId, { throwOnRestoreFailure: true });
}
- return (await this.deps.storage.get(sessionId)) ?? state;
} catch (error) {
if (this.turns.get(sessionId) === createBoundary) this.turns.delete(sessionId);
throw error;
}
+ // Same as the edit path: post-dispatch status refresh is observational and
+ // cannot revoke a lifecycle that may already own a rejection retry.
+ return (await this.deps.storage.get(sessionId)) ?? createdState;
}
async updateGoal(sessionId: string, patch: GoalUpdatePatch): Promise<GoalState | null> {