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。

refactor(teammates): judge a queued task message only before its row is persisted

Dash8dd3e97d7b7e
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/agent-input-coordinator.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/agent-input-coordinator.test.ts
index 164972a0bf..0b89ca1efb 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/agent-input-coordinator.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/agent-input-coordinator.test.ts
@@ -120,17 +120,19 @@ describe('task action dispatch authorization', () => {
expect(h.coordinator.getProjection(sid)).toMatchObject({ error: null, pendingQueue: [] });
});
- // Reviews on #7969: a check that merely failed (group transport, database not ready) is not
- // a revocation. The guard runs three times per dispatch: the drain pre-check, then in the send
- // transaction before and after the row is persisted. While the message can still be retried it
- // stays a queue head; once persisted a task action cannot be retried (its guard is bound to the
- // clientId), so it is delivered on the strength of the earlier successful checks.
+ // Reviews on #7969. A queued task message is judged until its row is persisted: the drain
+ // pre-check, then the send transaction's re-check right before persistence. A proven revocation
+ // leaves the queue without a trace; a check that merely failed (group transport, database not
+ // ready) keeps it as a retryable queue head. Once persisted the message is delivered: a refusal
+ // there could only leave a user row with a failed reply, and a queued task action cannot be
+ // retried (its guard is bound to the original clientId).
it.each([
- { failsAt: 'the pre-check', failingCall: 1, delivered: false },
- { failsAt: 'the re-check before persistence', failingCall: 2, delivered: false },
- { failsAt: 'the re-check after persistence', failingCall: 3, delivered: true },
- ])('a delivery check failing at $failsAt delivers=$delivered', async ({ failingCall, delivered }) => {
- const h = createHarness(), sid = `task-action-check-failed-${failingCall}`;
+ { name: 'a failed pre-check', call: 1, error: 'failed', outcome: 'retained' },
+ { name: 'a failed re-check before persistence', call: 2, error: 'failed', outcome: 'retained' },
+ { name: 'a revocation before persistence', call: 2, error: 'revoked', outcome: 'discarded' },
+ { name: 'a revocation after persistence', call: 3, error: 'revoked', outcome: 'delivered' },
+ ] as const)('$name is $outcome', async ({ call, error, outcome }) => {
+ const h = createHarness(), sid = `task-action-judged-${call}-${error}`;
const dispatched = vi.fn();
h.sendToAgent.mockImplementation(async (_id, _message, _opts, sendOpts) => {
await sendOpts[MCP_APP_MESSAGE_GUARD]?.();
@@ -144,24 +146,32 @@ describe('task action dispatch authorization', () => {
let calls = 0;
const queued = makeItem('automatic', 'Continue');
bindTaskActionInput(sid, queued, async () => {
- if (++calls === failingCall)
- throw Object.assign(new Error('verification unavailable'), { code: 'GROUP_AUTHORIZATION_UNAVAILABLE' });
+ if (++calls !== call) return;
+ throw error === 'revoked'
+ ? new Error('TASK_ACTION_AUTHORIZATION_EXPIRED')
+ : Object.assign(new Error('verification unavailable'), { code: 'GROUP_AUTHORIZATION_UNAVAILABLE' });
});
h.coordinator.enqueue(sid, queued);
await flush();
h.setRunning(false);
h.coordinator.wakeSession(sid, 'idle');
- if (delivered) {
+ if (outcome === 'delivered') {
await vi.waitFor(() => expect(dispatched).toHaveBeenCalledOnce());
await flush();
- expect(h.onUndispatchedUserTurn).not.toHaveBeenCalled();
- expect(h.coordinator.getProjection(sid)).toMatchObject({ error: null, pendingQueue: [] });
+ expect(calls).toBe(2); // pre-check and the re-check before persistence; none afterwards
+ expect(h.onDiscardedQueuedMessage).not.toHaveBeenCalled();
+ } else if (outcome === 'discarded') {
+ await vi.waitFor(() => expect(h.onDiscardedQueuedMessage).toHaveBeenCalledOnce());
+ expect(dispatched).not.toHaveBeenCalled();
+ expect(h.coordinator.getProjection(sid)).toMatchObject({ error: null, recovery: null, pendingQueue: [] });
} else {
await vi.waitFor(() => expect(h.coordinator.getProjection(sid).recovery).toEqual({ kind: 'queue-head', clientId: 'automatic' }));
expect(dispatched).not.toHaveBeenCalled();
+ expect(h.onDiscardedQueuedMessage).not.toHaveBeenCalled();
expect(h.coordinator.getProjection(sid).pendingQueue).toHaveLength(1);
}
- expect(h.onDiscardedQueuedMessage).not.toHaveBeenCalled();
+ // No outcome may leave a persisted user row with a failed reply behind.
+ expect(h.onUndispatchedUserTurn).not.toHaveBeenCalled();
});
});
diff --git a/apps/desktop/src/main/maker-ipc/agent-input-coordinator.ts b/apps/desktop/src/main/maker-ipc/agent-input-coordinator.ts
index b4ad57f69b..77fbcd9136 100644
--- a/apps/desktop/src/main/maker-ipc/agent-input-coordinator.ts
+++ b/apps/desktop/src/main/maker-ipc/agent-input-coordinator.ts
@@ -5161,21 +5161,16 @@ export class AgentInputCoordinator {
appMessageGuardRejected = true;
throw error;
}
+ // A task action is judged until its row is persisted. Afterwards a refusal could only
+ // leave a user row with a failed reply, and a queued task action cannot be retried (its
+ // guard is bound to the original clientId), so the message is delivered instead.
+ if (active.persisted) return;
try {
await taskActionGuard?.();
} catch (error) {
- if (isTaskActionRevoked(error)) {
- appMessageGuardRejected = true;
- throw error;
- }
- // A failed check is not a revocation. Before the row is persisted the ordinary
- // failure path keeps the message as a retryable queue head. Afterwards a task action
- // cannot be retried (its guard is bound to this clientId) and the pre-check already
- // passed: deliver rather than leave a failed reply behind.
- if (!active.persisted) throw error;
- log.warn('task action re-check failed after the row was persisted; delivering on the earlier check', {
- sessionId, clientId: head.clientId, reason: errorMessage(error),
- });
+ // Only a proven revocation discards; a failed check keeps the message retryable.
+ if (isTaskActionRevoked(error)) appMessageGuardRejected = true;
+ throw error;
}
} } : {}),
...(head.supersedesUserClientId ? { retryUserClientId: head.supersedesUserClientId } : {}),
diff --git a/docs/product-rules/teammate-authority.md b/docs/product-rules/teammate-authority.md
index dba2809826..75c8827340 100644
--- a/docs/product-rules/teammate-authority.md
+++ b/docs/product-rules/teammate-authority.md
@@ -52,9 +52,10 @@
清空或删除、伙伴被暂停或归档、委派执行代次或 Orca 团队变化、群授权失效时才不再投递;这类消息在
落库前移出队列,不在接收任务里留下失败回复。清空以协调器的内存边界和数据库 `cleared_at` 中较新者
为准(`/clear` 先改内存、后写库,写库可能失败);重启后晚加载的旧边界不算新的清空。校验本身失败
- (群授权传输中断、数据库未就绪等)不算撤权,不销毁消息:消息落库前失败(派发前预检、事务内第一次
- 复核)时保留为可重试的队首;落库之后(派发前最后一次复核)才失败时,排队的任务消息无法重试(守卫
- 绑定了 clientId),而前面的校验刚刚通过,所以照常派发,不留失败回复。实现见
+ (群授权传输中断、数据库未就绪等)不算撤权,不销毁消息,保留为可重试的队首。任务动作守卫只在消息
+ 落库之前判定(派发前预检,以及紧贴落库的那次复核):确认撤权则不落库、不留痕地移出队列;落库之后
+ 不再由它截断,照常派发——此时拒绝只会留下用户行和失败回复,而排队的任务消息无法重试(重试会换新的
+ clientId,守卫绑定的是原 clientId)。实现见
`taskActionHost.ts` 的 `assertDeliveryCurrent`、`taskActionAuthorization.ts` 的 `isTaskActionRevoked`
与 `agent-input-coordinator.ts` 的派发前复核;回归见 `taskActionAuthorization.test.ts` 与
`agent-input-coordinator.test.ts`。