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): 补齐发送锁阶段诊断透传 (#4657)

FicoWangcda52ff0eab8
diff --git a/apps/desktop/src/main/__tests__/makerSendToSessionOrdering.test.ts b/apps/desktop/src/main/__tests__/makerSendToSessionOrdering.test.ts
index 57e009c171..1a0a8489ef 100644
--- a/apps/desktop/src/main/__tests__/makerSendToSessionOrdering.test.ts
+++ b/apps/desktop/src/main/__tests__/makerSendToSessionOrdering.test.ts
@@ -627,12 +627,19 @@ describe('sendToSession ordering', () => {
'selection,',
);
expect(source).toContain('withSessionLock: withSendToSessionLock,');
- expect(directSendSwitchBlock).toContain('const release = await acquireSendToSessionLock(sessionId);');
+ expect(directSendSwitchBlock).toContain('const release = await acquireSendToSessionLock(sessionId, undefined, () => stage);');
expectOrder(
directSendSwitchBlock,
- 'const release = await acquireSendToSessionLock(sessionId);',
+ 'const release = await acquireSendToSessionLock(sessionId, undefined, () => stage);',
'applyPendingAgentSwitchIfIdle(',
);
+ for (const operation of [
+ 'reconcileBotModelRoute', 'applyPendingAgentSwitchIfIdle',
+ 'applyScheduledModelSelection', 'prepareUnhealthySession',
+ ]) {
+ expectOrder(directSendSwitchBlock, `stage = 'direct-send:${operation}'`, `${operation}(`);
+ }
+ expectOrder(directSendSwitchBlock, 'prepareUnhealthySession', "stage = 'direct-send:caller-dispatch'");
expectOrder(directSendSwitchBlock, 'applyPendingAgentSwitchIfIdle(', 'prepareUnhealthySession');
expectOrder(directSendSwitchBlock, 'prepareUnhealthySession', 'return { release, selection: resolvedSelection };');
});
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/sendToSessionLock.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/sendToSessionLock.test.ts
index b62c5c617b..13c5989006 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/sendToSessionLock.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/sendToSessionLock.test.ts
@@ -153,6 +153,52 @@ describe('sendToSessionLock', () => {
expect(hasSendToSessionLock('s6')).toBe(false);
});
+ it.each(['lease', 'wrapper'] as const)('reports the current %s holder stage at warn and bail', async (form) => {
+ const id = `staged-${form}`;
+ let stage = 'prepare';
+ let release!: () => void;
+ const gate = new Promise<void>((resolve) => { release = resolve; });
+ const run = form === 'lease'
+ ? acquireSendToSessionLock(id, undefined, () => stage).then(async (unlock) => {
+ await gate;
+ unlock();
+ })
+ : withSendToSessionLock(id, () => gate, () => stage);
+ await vi.advanceTimersByTimeAsync(30_000);
+ expect(warnMock).toHaveBeenLastCalledWith(
+ 'sendToSession lock still held after expected budget',
+ { sessionId: id, heldMs: 30_000, stage: 'prepare' },
+ );
+ stage = 'dispatch';
+ await vi.advanceTimersByTimeAsync(270_000);
+ expect(warnMock).toHaveBeenLastCalledWith(
+ 'sendToSession lock bailed out; later senders proceed while the stuck holder finishes',
+ { sessionId: id, heldMs: 300_000, stage: 'dispatch' },
+ );
+ expect(hasSendToSessionLock(id)).toBe(false);
+ release();
+ await run;
+ });
+
+ it('samples only the acquired holder, then stops sampling after release', async () => {
+ const firstStage = vi.fn(() => 'first');
+ const secondStage = vi.fn(() => 'second');
+ const first = await acquireSendToSessionLock('staged-queue', undefined, firstStage);
+ const secondPending = acquireSendToSessionLock('staged-queue', undefined, secondStage);
+ await vi.advanceTimersByTimeAsync(30_000);
+ expect(firstStage).toHaveBeenCalledTimes(1);
+ expect(secondStage).not.toHaveBeenCalled();
+ first();
+ const second = await secondPending;
+ await vi.advanceTimersByTimeAsync(30_000);
+ expect(firstStage).toHaveBeenCalledTimes(1);
+ expect(secondStage).toHaveBeenCalledTimes(1);
+ second();
+ await vi.advanceTimersByTimeAsync(300_000);
+ expect(secondStage).toHaveBeenCalledTimes(1);
+ expect(hasSendToSessionLock('staged-queue')).toBe(false);
+ });
+
it('starts each watchdog after acquisition so a stalled holder cannot release all queued sends', async () => {
const first = await acquireSendToSessionLock('queued-watchdogs');
let secondRelease: (() => void) | undefined;
diff --git a/apps/desktop/src/main/maker-ipc/register.ts b/apps/desktop/src/main/maker-ipc/register.ts
index 4ee252d759..02468f305e 100644
--- a/apps/desktop/src/main/maker-ipc/register.ts
+++ b/apps/desktop/src/main/maker-ipc/register.ts
@@ -8336,15 +8336,19 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)
log,
});
pendingAgentSwitchApplyHolder = async (sessionId, signal, selection) => {
- const release = await acquireSendToSessionLock(sessionId);
+ let stage = 'direct-send:acquire';
+ const release = await acquireSendToSessionLock(sessionId, undefined, () => stage);
try {
+ stage = 'direct-send:reconcileBotModelRoute';
await reconcileBotModelRoute(sessionId, true);
+ stage = 'direct-send:applyPendingAgentSwitchIfIdle';
await applyPendingAgentSwitchIfIdle(agentSwitchDeps, sessionId, {
bootstrapAfterSwitch: true,
signal,
});
let resolvedSelection: ScheduledModelSelection | undefined;
if (selection) {
+ stage = 'direct-send:applyScheduledModelSelection';
resolvedSelection = await applyScheduledModelSelection(selection, {
getTarget: async () => {
const row = await agentSwitchDeps.getSessionRow(sessionId);
@@ -8376,7 +8380,10 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)
},
});
}
+ stage = 'direct-send:prepareUnhealthySession';
await contextOverflowRolloverHolder?.prepareUnhealthySession(sessionId);
+ // The caller retains the lease through runtime refresh and Session.send.
+ stage = 'direct-send:caller-dispatch';
return { release, selection: resolvedSelection };
} catch (err) {
release();
diff --git a/apps/desktop/src/main/maker-ipc/sendToSessionLock.ts b/apps/desktop/src/main/maker-ipc/sendToSessionLock.ts
index 7282bcd922..17db9477c0 100644
--- a/apps/desktop/src/main/maker-ipc/sendToSessionLock.ts
+++ b/apps/desktop/src/main/maker-ipc/sendToSessionLock.ts
@@ -164,11 +164,14 @@ function installSendToSessionLockEntry(
*
* Direct-send callers need this lease form because applying a deferred agent switch,
* refreshing the resulting live Session, and calling Session.send happen in different
- * modules but must remain one atomic route decision.
+ * modules but must remain one atomic route decision. `getStage` is sampled when
+ * a watchdog fires, so callers can identify the current await without logging
+ * task content. Keep the getter alive until release, including lease handoffs.
*/
export async function acquireSendToSessionLock(
sessionId: string,
restartToken?: symbol,
+ getStage?: () => string | undefined,
): Promise<() => void> {
assertSessionNotRestarting(sessionId, restartToken);
const previous = sendToSessionLocks.get(sessionId);
@@ -178,7 +181,7 @@ export async function acquireSendToSessionLock(
releaseGate = resolve;
});
const run = waitPrevious.then(() => gate);
- installSendToSessionLockEntry(sessionId, run);
+ installSendToSessionLockEntry(sessionId, run, getStage);
try {
await waitForSendToSessionLock(sessionId, previous, restartToken);
} catch (error) {
@@ -198,8 +201,9 @@ export async function acquireSendToSessionLock(
export async function withSendToSessionLock<T>(
sessionId: string,
task: () => Promise<T>,
+ getStage?: () => string | undefined,
): Promise<T> {
- const release = await acquireSendToSessionLock(sessionId);
+ const release = await acquireSendToSessionLock(sessionId, undefined, getStage);
try {
return await task();
} finally {