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(bots): isolate in-flight group start cards by run

Chris6dc5fab70852
diff --git a/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts b/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts
index e73a2699ed..689f675c72 100644
--- a/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts
+++ b/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts
@@ -4302,6 +4302,49 @@ describe('Bot Session task end-to-end runtime', () => {
} finally { release(); runtime.dispose(); }
});
+ it('keeps a resumed run start in flight when an older run backoff fires', async () => {
+ await seedPair(); seedGroupLane();
+ vi.useFakeTimers();
+ const release = registerGroupToolAuthority('group-lane', { botId: 'bot-a', mode: 'owner', isCurrent: () => true, validate: async () => {} });
+ let releaseStart: () => void = () => {};
+ const startLanded = new Promise<void>((resolve) => { releaseStart = resolve; });
+ const landed: string[] = [];
+ const postGroupTaskMessage = vi.fn(async (input: { clientId: string }) => {
+ if (input.clientId.endsWith(':2:started')) await startLanded;
+ else if (input.clientId.endsWith(':started')) {
+ return { ok: false as const, errorCode: 'GROUP_CHANNEL_UNAVAILABLE', retryable: true };
+ }
+ landed.push(input.clientId);
+ return { ok: true as const, messageId: `posted:${input.clientId}` };
+ });
+ const runtime = createDelegationRuntime({ postGroupTaskMessage });
+ try {
+ const task = await runtime.delegation.startSessionTask({ callerSessionId: 'group-lane', objective: 'First pass' });
+ if (!task.ok) throw new Error(task.message);
+ const id = task.delegationId;
+ await runtime.runPendingTurns();
+ expect(landed).toEqual([`bot-delegation-task:${id}:1:result`]);
+ expect(await runtime.delegation.messageSessionTask('group-lane', id, { kind: 'message', text: 'One more pass.' }))
+ .toMatchObject({ ok: true, resumed: true });
+ // Run 1's scheduled retry observes run 2 and exits while run 2's start is still pending.
+ await vi.advanceTimersByTimeAsync(1_000);
+ expect(postGroupTaskMessage.mock.calls.map(([input]) => input.clientId)).toEqual([
+ `bot-delegation-task:${id}:started`, `bot-delegation-task:${id}:1:result`,
+ `bot-delegation-task:${id}:2:started`,
+ ]);
+ const settled = runtime.runPendingTurns();
+ await vi.advanceTimersByTimeAsync(50);
+ expect(landed).toEqual([`bot-delegation-task:${id}:1:result`]);
+ expect(postGroupTaskMessage).not.toHaveBeenCalledWith(expect.objectContaining({ clientId: `bot-delegation-task:${id}:2:result` }));
+ releaseStart();
+ await settled;
+ expect(landed).toEqual([
+ `bot-delegation-task:${id}:1:result`, `bot-delegation-task:${id}:2:started`,
+ `bot-delegation-task:${id}:2:result`,
+ ]);
+ } finally { releaseStart(); release(); runtime.dispose(); vi.useRealTimers(); }
+ });
+
it('clips a long group task title without splitting an emoji', async () => {
await seedPair(); seedGroupLane();
const release = registerGroupToolAuthority('group-lane', { botId: 'bot-a', mode: 'owner', isCurrent: () => true, validate: async () => {} });
diff --git a/apps/desktop/src/main/maker-ipc/botDelegationService.ts b/apps/desktop/src/main/maker-ipc/botDelegationService.ts
index d1f6d8efbd..7d302e439d 100644
--- a/apps/desktop/src/main/maker-ipc/botDelegationService.ts
+++ b/apps/desktop/src/main/maker-ipc/botDelegationService.ts
@@ -870,8 +870,9 @@ export function createBotDelegationService(deps: BotDelegationServiceDeps) {
};
const groupPostTimers = new Set<ReturnType<typeof setTimeout>>();
- /** Start-card posts still on their way, by task: a later card waits for them (see `afterGroupTaskStart`). */
+ /** Start-card posts still on their way, by task and run: old retries cannot replace a resumed run. */
const groupStartPosts = new Map<string, Promise<void>>();
+ const groupStartPostKey = (row: Pick<GroupTaskRow, 'id' | 'runSequence'>): string => `${row.id}:${row.runSequence}`;
/**
* The 「进行中」 card is posted with backoff for as long as the task runs (a group outage may
* outlast any fixed number of tries); `restore` posts it again after a restart. The post is
@@ -881,6 +882,7 @@ export function createBotDelegationService(deps: BotDelegationServiceDeps) {
* client id it always had.
*/
const postGroupTaskStart = (home: { groupId: string }, row: GroupTaskRow, attempt = 0): Promise<void> => {
+ const key = groupStartPostKey(row);
const run = (async () => {
try {
if (attempt > 0) {
@@ -908,9 +910,9 @@ export function createBotDelegationService(deps: BotDelegationServiceDeps) {
timer.unref?.();
groupPostTimers.add(timer);
})().finally(() => {
- if (groupStartPosts.get(row.id) === run) groupStartPosts.delete(row.id);
+ if (groupStartPosts.get(key) === run) groupStartPosts.delete(key);
});
- groupStartPosts.set(row.id, run);
+ groupStartPosts.set(key, run);
return run;
};
/**
@@ -918,7 +920,8 @@ export function createBotDelegationService(deps: BotDelegationServiceDeps) {
* group before the 「进行中」 card still in flight: later cards wait for that post to settle.
* A retry scheduled for later re-checks the task state itself and stays away once it ended.
*/
- const afterGroupTaskStart = (delegationId: string): Promise<void> => groupStartPosts.get(delegationId) ?? Promise.resolve();
+ const afterGroupTaskStart = (row: Pick<GroupTaskRow, 'id' | 'runSequence'>): Promise<void> =>
+ groupStartPosts.get(groupStartPostKey(row)) ?? Promise.resolve();
const collaborationMeta = async (
row: Pick<DelegationRow,
@@ -1121,7 +1124,7 @@ export function createBotDelegationService(deps: BotDelegationServiceDeps) {
.select({ snapshot: botDelegations.permissionSnapshotJson }).from(botDelegations)
.where(eq(botDelegations.id, params.id)).get())?.snapshot);
if (home) {
- await afterGroupTaskStart(params.id);
+ await afterGroupTaskStart(params);
const posted = await postGroupTaskEvent(home, params, {
clientId: `bot-delegation-task:${params.id}:${params.runSequence}:result`,
text: params.resultSummary ?? '',
@@ -1640,7 +1643,7 @@ export function createBotDelegationService(deps: BotDelegationServiceDeps) {
let privateFallback = !home;
if (home) {
try {
- await afterGroupTaskStart(row.id);
+ await afterGroupTaskStart(row);
const posted = await postGroupTaskEvent(home, row, {
clientId: `bot-delegation-task:${row.id}:interaction:${pending.requestId}`,
text: '',