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): 续跑时把新 vendor generation 绑定回输入协调器 (#5057)

Yunjin Zhang0ce888f20a1d
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 4229ff8224..5915de7ee0 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
@@ -8169,6 +8169,204 @@ describe('AgentInputCoordinator steer transaction', () => {
}
});
+ it('settles leftover activeTurn after a host continuation adopts the new vendor generation', async () => {
+ // silent-stop 自动续跑用 sendHostTurnContinuation 绕过 send 事务;它必须在预约时
+ // 把新 vendor generation 交给协调器,否则续跑的真实 done 会被 ownership 守卫丢弃,
+ // 残留 activeTurn 永久挡住队列(2026-09-24 僵尸 activeTurn 事故)。
+ vi.useFakeTimers();
+ try {
+ const h = createHarness();
+ const sid = 'host-continuation-generation-adopted';
+ h.coordinator.enqueue(sid, makeItem('q-1', 'first'));
+ await flush();
+
+ h.setTurnGeneration(1);
+ h.coordinator.noteHostTurnContinuation(sid, 1);
+ h.setRunning(false);
+ h.setObservedCurrentTurnTerminal({ kind: 'done', generation: 1 });
+
+ h.coordinator.enqueue(sid, makeItem('q-2', 'queued-after-continuation'));
+ await flush();
+ // 续跑的终态还没到 → 队列仍被 activeTurn 挡住。
+ expect(h.sendToAgent).toHaveBeenCalledTimes(1);
+
+ h.coordinator.onTurnEvent(sid, 'done', undefined, undefined, {
+ sessionTurnGeneration: 1,
+ sessionInstanceId: 'harness-session',
+ });
+ await flush();
+
+ expect(h.sendToAgent).toHaveBeenCalledTimes(2);
+ expect(h.sendToAgent.mock.calls[1]?.[1]).toEqual({
+ type: 'user',
+ content: 'queued-after-continuation',
+ });
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
+ it('restores the pre-continuation binding when the host continuation send fails before dispatch', async () => {
+ // 续跑预约后 send 在派发确认前失败:Session 回滚 turnGeneration,
+ // settleSilentStopDone 的合成 done 没有 generation,绑定必须同步回滚,
+ // 否则它会被 ownership 守卫丢弃,形成反向僵尸。
+ vi.useFakeTimers();
+ try {
+ const h = createHarness();
+ const sid = 'host-continuation-send-rejected-rollback';
+ h.coordinator.enqueue(sid, makeItem('q-1', 'first'));
+ await flush();
+
+ h.setTurnGeneration(1);
+ h.coordinator.noteHostTurnContinuation(sid, 1);
+ h.setTurnGeneration(0);
+ h.coordinator.noteHostTurnContinuationFailed(sid, 1);
+
+ h.setRunning(false);
+ h.setObservedCurrentTurnTerminal({ kind: 'done', generation: 0 });
+ h.coordinator.onTurnEvent(sid, 'done');
+ h.coordinator.enqueue(sid, makeItem('q-2', 'queued-after-failed-continuation'));
+ await flush();
+
+ expect(h.sendToAgent).toHaveBeenCalledTimes(2);
+ expect(h.sendToAgent.mock.calls[1]?.[1]).toEqual({
+ type: 'user',
+ content: 'queued-after-failed-continuation',
+ });
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
+ it('does not restore a binding that no longer matches the failed continuation', async () => {
+ vi.useFakeTimers();
+ try {
+ const h = createHarness();
+ const sid = 'host-continuation-rollback-superseded';
+ h.coordinator.enqueue(sid, makeItem('q-1', 'first'));
+ await flush();
+
+ h.setTurnGeneration(1);
+ h.coordinator.noteHostTurnContinuation(sid, 1);
+ // 绑定又被改写成 2 之后,针对 1 的失败回滚必须放手。
+ h.setTurnGeneration(2);
+ h.coordinator.noteHostTurnContinuation(sid, 2);
+ h.coordinator.noteHostTurnContinuationFailed(sid, 1);
+
+ h.setRunning(false);
+ h.setObservedCurrentTurnTerminal({ kind: 'done', generation: 2 });
+ h.coordinator.enqueue(sid, makeItem('q-2', 'queued-after-superseded-rollback'));
+ await flush();
+ h.coordinator.onTurnEvent(sid, 'done', undefined, undefined, {
+ sessionTurnGeneration: 2,
+ sessionInstanceId: 'harness-session',
+ });
+ await flush();
+
+ expect(h.sendToAgent).toHaveBeenCalledTimes(2);
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
+ it('keeps ignoring the pre-continuation generation terminal after the host continuation adopts a new one', async () => {
+ vi.useFakeTimers();
+ try {
+ const h = createHarness();
+ const sid = 'host-continuation-old-generation-still-ignored';
+ h.coordinator.enqueue(sid, makeItem('q-1', 'first'));
+ await flush();
+
+ h.setTurnGeneration(1);
+ h.coordinator.noteHostTurnContinuation(sid, 1);
+ h.setRunning(false);
+ h.coordinator.enqueue(sid, makeItem('q-2', 'queued-after-old-generation-done'));
+ await flush();
+
+ // 改绑必须是一次单向采纳:旧代 (0) 的迟到 done 仍然不得结清 leftover。
+ h.coordinator.onTurnEvent(sid, 'done', undefined, undefined, {
+ sessionTurnGeneration: 0,
+ sessionInstanceId: 'harness-session',
+ });
+ await flush();
+ expect(h.sendToAgent).toHaveBeenCalledTimes(1);
+
+ h.setObservedCurrentTurnTerminal({ kind: 'done', generation: 1 });
+ h.coordinator.onTurnEvent(sid, 'done', undefined, undefined, {
+ sessionTurnGeneration: 1,
+ sessionInstanceId: 'harness-session',
+ });
+ await flush();
+ expect(h.sendToAgent).toHaveBeenCalledTimes(2);
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
+ it('host continuation notification does not adopt a generation before the vendor dispatch is confirmed', async () => {
+ // sending 形态仍属 #3383 的 fail-closed 窗口:派发未确认前不得改写绑定。
+ vi.useFakeTimers();
+ try {
+ const h = createHarness();
+ const sid = 'host-continuation-pre-dispatch';
+ let releaseSend!: () => void;
+ h.sendToAgent.mockImplementationOnce(async (sessionId, _message, _createOpts, sendOpts) => {
+ sendOpts.onVendorTurnReserved?.(1);
+ await new Promise<void>((resolve) => {
+ releaseSend = resolve;
+ });
+ return { kind: 'session-dispatch', dispatched: true } as never;
+ });
+ h.coordinator.enqueue(sid, makeItem('q-1', 'first'));
+ await flush();
+
+ h.coordinator.noteHostTurnContinuation(sid, 7);
+ releaseSend();
+ await flush();
+
+ h.setRunning(false);
+ h.setObservedCurrentTurnTerminal({ kind: 'done', generation: 1 });
+ h.coordinator.enqueue(sid, makeItem('q-2', 'queued-after-dispatch'));
+ await flush();
+
+ h.coordinator.onTurnEvent(sid, 'done', undefined, undefined, {
+ sessionTurnGeneration: 1,
+ sessionInstanceId: 'harness-session',
+ });
+ await flush();
+
+ expect(h.sendToAgent).toHaveBeenCalledTimes(2);
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
+ it('host continuation notification without a dispatched activeTurn does not hijack the next dispatch generation', async () => {
+ vi.useFakeTimers();
+ try {
+ const h = createHarness();
+ const sid = 'host-continuation-idle-noop';
+ // 空闲期误调(理论上不该发生):不得留下待生效的绑定。
+ h.coordinator.noteHostTurnContinuation(sid, 3);
+
+ h.coordinator.enqueue(sid, makeItem('q-1', 'first'));
+ await flush();
+
+ h.setRunning(false);
+ h.setObservedCurrentTurnTerminal({ kind: 'done', generation: 0 });
+ h.coordinator.onTurnEvent(sid, 'done', undefined, undefined, {
+ sessionTurnGeneration: 0,
+ sessionInstanceId: 'harness-session',
+ });
+ h.coordinator.enqueue(sid, makeItem('q-2', 'after-idle-notification'));
+ await flush();
+
+ expect(h.sendToAgent).toHaveBeenCalledTimes(2);
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
it('does not recover leftover activeTurn from a later-generation error callback', async () => {
vi.useFakeTimers();
try {
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/silentStopContinuationBinding.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/silentStopContinuationBinding.test.ts
new file mode 100644
index 0000000000..a137868394
--- /dev/null
+++ b/apps/desktop/src/main/maker-ipc/__tests__/silentStopContinuationBinding.test.ts
@@ -0,0 +1,194 @@
+/**
+ * silentStopContinuationBinding.test.ts
+ * ---------------------------------------------------------------------------
+ * silent-stop 自动续跑走 Session.sendHostTurnContinuation(),不经过 send 事务
+ * (makerSendTransaction),因此不会触发把 onTurnReserved 转发给输入协调器的常规
+ * 接线。这条旁路必须自己完成两件事(背景见 silentStopContinuationBinding.ts 文件头,
+ * 2026-09-24 僵尸 activeTurn 实报):
+ * 1. 预约时把新 vendor generation 交给协调器(noteHostTurnContinuation);
+ * 2. send 在派发确认前失败(Session 回滚 turnGeneration)时回滚绑定
+ * (noteHostTurnContinuationFailed)。
+ *
+ * 行为部分用真实 maker-core Session + fake handle 驱动 `sendHostTurnContinuation`:
+ * 覆盖预约回调时序(先于 provider dispatch)、成功派发、未派发收口回滚、
+ * 派发前取消(无预约时回滚必须 no-op)。
+ * register 侧接线由源码契约守住(register.ts 依赖 Electron main,无法在单测导入,
+ * 仓库内所有既有测试同样 mock register)。
+ */
+
+import { readFileSync } from 'node:fs';
+import { resolve } from 'node:path';
+import { describe, expect, it, vi } from 'vitest';
+import { Session, type AgentEvent, type AgentSessionHandle } from '@cindy/maker-core';
+import {
+ bindSilentStopContinuationGeneration,
+ type SilentStopContinuationBindingDeps,
+} from '../silentStopContinuationBinding.js';
+
+const registerSource = readFileSync(resolve(__dirname, '..', 'register.ts'), 'utf8').replace(
+ /\r\n?/g,
+ '\n',
+);
+
+function extractBetween(source: string, startMarker: string, endMarker: string): string {
+ const start = source.indexOf(startMarker);
+ const end = source.indexOf(endMarker, start + startMarker.length);
+ if (start < 0 || end < 0) {
+ throw new Error(`source markers not found: ${startMarker} .. ${endMarker}`);
+ }
+ return source.slice(start, end);
+}
+
+describe('silent-stop continuation generation binding (wiring contract)', () => {
+ it('binds the reserved generation via the extracted helper and rolls back on send failure', () => {
+ const resumeBlock = extractBetween(
+ registerSource,
+ 'async function handleSilentStopTurnEnd(',
+ 'function isFencedStaleProductTerminal(',
+ );
+ // 预约/回滚接线集中到 helper;sendOpts 必须真的并入续跑调用。
+ expect(resumeBlock).toContain('bindSilentStopContinuationGeneration(session.id');
+ expect(resumeBlock).toContain('...binding.sendOpts,');
+ // 未派发(not accepted)与抛出两条失败收口都要回滚绑定。
+ expect(resumeBlock.match(/binding\.rollbackBinding\(\);/g)).toHaveLength(2);
+ });
+});
+
+describe('silent-stop continuation generation binding (behavior on a real Session)', () => {
+ const SID = 'silent-stop-binding';
+
+ function createHarness() {
+ const calls: string[] = [];
+ const queued: AgentEvent[] = [];
+ let releaseEvents: (() => void) | null = null;
+ const noteHostTurnContinuation = vi.fn();
+ const noteHostTurnContinuationFailed = vi.fn();
+ const deps: SilentStopContinuationBindingDeps = {
+ noteHostTurnContinuation: (sessionId, generation) => {
+ calls.push('reserved');
+ noteHostTurnContinuation(sessionId, generation);
+ },
+ noteHostTurnContinuationFailed,
+ };
+ const handle = {
+ id: 'provider-thread',
+ agentKind: 'pi',
+ model: 'test-model',
+ send: vi.fn(async () => {
+ calls.push('send');
+ }),
+ abort: vi.fn(async () => {}),
+ close: vi.fn(async () => {}),
+ setInteractionResolver: vi.fn(),
+ async *events() {
+ // 事件泵随 Session 常驻;收到 close 后由 close 兜底结束流。
+ try {
+ while (true) {
+ while (queued.length > 0) yield queued.shift()!;
+ await new Promise<void>((resolve) => {
+ releaseEvents = resolve;
+ });
+ }
+ } finally {
+ releaseEvents = null;
+ }
+ },
+ } as unknown as AgentSessionHandle;
+ const emit = (event: AgentEvent) => {
+ queued.push(event);
+ releaseEvents?.();
+ releaseEvents = null;
+ };
+ const logger = {
+ trace: vi.fn(),
+ debug: vi.fn(),
+ info: vi.fn(),
+ warn: vi.fn(),
+ error: vi.fn(),
+ fatal: vi.fn(),
+ child: () => logger,
+ };
+ const session = new Session({
+ id: SID,
+ agentKind: 'pi',
+ workDir: process.cwd(),
+ handle,
+ capabilities: {} as never,
+ logger,
+ turnStallMs: 0,
+ });
+ return {
+ session,
+ handle,
+ emit,
+ calls,
+ noteHostTurnContinuation,
+ noteHostTurnContinuationFailed,
+ binding: bindSilentStopContinuationGeneration(SID, deps),
+ };
+ }
+
+ it('hands the reserved generation to the coordinator before the provider turn starts', async () => {
+ const h = createHarness();
+ try {
+ const result = await h.session.sendHostTurnContinuation(
+ { type: 'user', content: 'continue' },
+ { ...h.binding.sendOpts },
+ );
+ expect(result.accepted).toBe(true);
+ // 时序契约:预约回调先于 provider dispatch —— 协调器的新绑定在 provider
+ // 开始前落位,任何早期终态事件都落在新 generation 上。
+ expect(h.calls).toEqual(['reserved', 'send']);
+ expect(h.noteHostTurnContinuation).toHaveBeenCalledWith(SID, 1);
+ expect(h.session.getTurnGeneration()).toBe(1);
+ } finally {
+ h.emit({ type: 'status', data: { isRunning: false }, source: 'pi' } as AgentEvent);
+ h.emit({ type: 'done', data: {}, source: 'pi' } as AgentEvent);
+ await h.session.close().catch(() => {});
+ }
+ });
+
+ it('restores the binding through rollbackBinding when the provider rejects the send', async () => {
+ const h = createHarness();
+ h.handle.send = vi.fn(async () => {
+ throw new Error('provider rejected');
+ });
+ try {
+ await expect(
+ h.session.sendHostTurnContinuation({ type: 'user', content: 'continue' }, { ...h.binding.sendOpts }),
+ ).rejects.toThrow('provider rejected');
+
+ // 失败前提:预约回调已经用 N+1 绑定过,Session 把 turnGeneration 回滚到 0。
+ expect(h.calls).toContain('reserved');
+ expect(h.noteHostTurnContinuation).toHaveBeenCalledWith(SID, 1);
+ expect(h.session.getTurnGeneration()).toBe(0);
+
+ // register 失败收口调用的正是 rollbackBinding;协调器回滚后,
+ // settleSilentStopDone 的合成 done(无 generation)才能匹配绑定。
+ h.binding.rollbackBinding();
+ expect(h.noteHostTurnContinuationFailed).toHaveBeenCalledWith(SID, 1);
+ } finally {
+ await h.session.close().catch(() => {});
+ }
+ });
+
+ it('keeps rollbackBinding a no-op when the send was cancelled before reservation', async () => {
+ const h = createHarness();
+ try {
+ const controller = new AbortController();
+ controller.abort();
+ const result = await h.session.sendHostTurnContinuation(
+ { type: 'user', content: 'continue' },
+ { ...h.binding.sendOpts, signal: controller.signal },
+ );
+ expect(result.accepted).toBe(false);
+ expect(h.calls).not.toContain('reserved');
+ expect(h.noteHostTurnContinuation).not.toHaveBeenCalled();
+ // 预约回调根本没触发:回滚必须 no-op,不得误回滚协调器绑定。
+ h.binding.rollbackBinding();
+ expect(h.noteHostTurnContinuationFailed).not.toHaveBeenCalled();
+ } finally {
+ await h.session.close().catch(() => {});
+ }
+ });
+});
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 57e83f26d6..7fa0e6599b 100644
--- a/apps/desktop/src/main/maker-ipc/agent-input-coordinator.ts
+++ b/apps/desktop/src/main/maker-ipc/agent-input-coordinator.ts
@@ -598,6 +598,12 @@ interface ActiveTurn {
controlKind?: 'compact';
/** maker-core turn generation captured at vendor dispatch; leftover reclaim must match it. */
vendorTurnGeneration: number | null;
+ /**
+ * `vendorTurnGeneration` before a host continuation adopted a newer one
+ * (见 noteHostTurnContinuation)。续跑 send 在派发确认前失败时用它还原绑定;
+ * `undefined` 表示当前没有待回滚的采纳。
+ */
+ preContinuationVendorTurnGeneration?: number | null;
}
interface PendingCompactRequest {
@@ -3378,6 +3384,65 @@ export class AgentInputCoordinator {
};
}
+ /**
+ * Host-owned continuation (silent-stop auto-resume) keeps the same product turn on a
+ * new vendor generation. `sendHostTurnContinuation` bypasses the send transaction that
+ * normally forwards `onTurnReserved` to `captureReservedVendorGeneration`, so without
+ * this callback a dispatched leftover activeTurn stays bound to the pre-continuation
+ * generation. The continuation's real terminal then fails the generation-ownership
+ * guard in `onTurnEvent`, and `isDispatchBoundaryBusy` keeps blocking every later
+ * input — the session never leaves "running" (2026-09-24 zombie activeTurn incident).
+ * With no dispatched activeTurn the continuation's terminal settles through the
+ * ordinary path, so this is deliberately a no-op there. A rejected continuation send
+ * must be paired with `noteHostTurnContinuationFailed`.
+ */
+ noteHostTurnContinuation(sessionId: string, vendorTurnGeneration: number): void {
+ const active = this.getState(sessionId).activeTurn;
+ if (!active || !isActiveTurnDispatched(active)) return;
+ if (active.vendorTurnGeneration === vendorTurnGeneration) return;
+ if (!this.isActiveTurnCurrent(sessionId, active)) return;
+ log.info('host turn continuation adopted new vendor generation', {
+ sessionId,
+ clientId: active.item?.clientId ?? null,
+ previousGeneration: active.vendorTurnGeneration,
+ vendorTurnGeneration,
+ });
+ active.preContinuationVendorTurnGeneration = active.vendorTurnGeneration;
+ active.vendorTurnGeneration = vendorTurnGeneration;
+ }
+
+ /**
+ * Undo `noteHostTurnContinuation` when the continuation send failed before the vendor
+ * confirmed the dispatch. `Session.dispatchSend` rolls `turnGeneration` back in that
+ * case, while the failure settlement (`settleSilentStopDone`) synthesizes a `done`
+ * without a generation: pointed at the rolled-back generation the ownership guard
+ * would reject that synthesized terminal (observed N vs bound N+1) and the input
+ * boundary would wedge — the mirror image of the zombie adoption removes. Restore the
+ * captured pre-continuation binding instead of re-reading the Session so an owner
+ * switch during the failed send cannot leak a foreign generation into the leftover.
+ */
+ noteHostTurnContinuationFailed(
+ sessionId: string,
+ adoptedVendorTurnGeneration: number,
+ ): void {
+ const active = this.getState(sessionId).activeTurn;
+ if (!active || !isActiveTurnDispatched(active)) return;
+ // 绑定已被别的路径改写(或本就是新 turn):失败回滚必须放手。
+ if (active.vendorTurnGeneration !== adoptedVendorTurnGeneration) return;
+ if (!this.isActiveTurnCurrent(sessionId, active)) return;
+ const previous = active.preContinuationVendorTurnGeneration;
+ if (previous === undefined) return;
+ active.preContinuationVendorTurnGeneration = undefined;
+ if (previous === active.vendorTurnGeneration) return;
+ log.info('host turn continuation send failed; restored leftover vendor generation', {
+ sessionId,
+ clientId: active.item?.clientId ?? null,
+ adoptedVendorTurnGeneration,
+ restoredGeneration: previous,
+ });
+ active.vendorTurnGeneration = previous;
+ }
+
onTurnEvent(
sessionId: string,
type: 'done' | 'error',
diff --git a/apps/desktop/src/main/maker-ipc/register.ts b/apps/desktop/src/main/maker-ipc/register.ts
index d8ba668aeb..de15e722e5 100644
--- a/apps/desktop/src/main/maker-ipc/register.ts
+++ b/apps/desktop/src/main/maker-ipc/register.ts
@@ -641,6 +641,7 @@ import {
import { dbToMakerAgentKind, makerToDbAgentKind } from '../../shared/agentKindConversion.js';
import { readWorkflowProgressForSession } from '../workflow-progress/reader.js';
import { AgentInputCoordinator } from './agent-input-coordinator.js';
+import { bindSilentStopContinuationGeneration } from './silentStopContinuationBinding.js';
import { notePromptPredictionSessionStopped } from './promptPredictionStopLedger.js';
import {
estimateReferenceTokens,
@@ -4300,6 +4301,11 @@ async function surfaceSilentStopExhaustedBanner(sessionId: string): Promise<void
log.warn('silent-stop auto-resume exhausted — surfaced continue banner', { sessionId });
}
+/**
+ * silent-stop 续跑的 generation 绑定/回滚接线已抽到 silentStopContinuationBinding.ts
+ * (可单测,行为测试用真实 sendHostTurnContinuation 驱动)。
+ */
+
async function handleSilentStopTurnEnd(
session: NonNullable<ReturnType<Maker['getSession']>>,
doneAt: number,
@@ -4323,6 +4329,19 @@ async function handleSilentStopTurnEnd(
}
const decision = silentStopAutoResumeGuard.onSilentStop(session.id, doneAt);
if (decision.action === 'resume') {
+ // 续跑绕过 send 事务:generation 的绑定/失败回滚接线集中在 binding 对象
+ // (bindSilentStopContinuationGeneration,见该文件头)。漏掉任一半边会让
+ // 协调器残留的 activeTurn 与续跑的真实 done 永久失配,输入边界卡在忙
+ // (僵尸 activeTurn)。声明在 try 外,未派发/抛出两条失败收口都要用。
+ const binding = bindSilentStopContinuationGeneration(session.id, {
+ noteHostTurnContinuation: (bindingSessionId, generation) =>
+ agentInputCoordinatorHolder?.noteHostTurnContinuation(bindingSessionId, generation),
+ noteHostTurnContinuationFailed: (bindingSessionId, adoptedGeneration) =>
+ agentInputCoordinatorHolder?.noteHostTurnContinuationFailed(
+ bindingSessionId,
+ adoptedGeneration,
+ ),
+ });
try {
// The next Claude running boundary belongs to the same user-visible turn.
// Mark it before send(), which may synchronously emit status events.
@@ -4333,6 +4352,9 @@ async function handleSilentStopTurnEnd(
{
origin: turnOrigin,
onDispatching: () => advanceRuntimeRecoveryNotice(session),
+ // 预约(onTurnReserved)在 binding.sendOpts;语义与不变量见
+ // silentStopContinuationBinding.ts 文件头。
+ ...binding.sendOpts,
onAccepted: async () => {
await createDbMessage(session.id, {
clientId,
@@ -4368,6 +4390,10 @@ async function handleSilentStopTurnEnd(
});
if (!outcome.dispatched) {
silentStopAutoResumeGuard.noteResumeSendFailed(session.id);
+ // Session 在派发确认前失败会回滚 turnGeneration;绑定必须跟着回滚,
+ // 否则失败收口合成的 done 会因 generation 不匹配被 ownership 守卫丢弃,
+ // 形成与本次修复对称的反向僵尸(输入边界永久忙)。
+ binding.rollbackBinding();
log.warn('silent-stop auto-resume send not accepted', {
sessionId: session.id,
reason: outcome.reason,
@@ -4379,6 +4405,7 @@ async function handleSilentStopTurnEnd(
}
} catch (err) {
silentStopAutoResumeGuard.noteResumeSendFailed(session.id);
+ binding.rollbackBinding();
log.warn('silent-stop auto-resume send failed', {
sessionId: session.id,
error: err instanceof Error ? err.message : String(err),
diff --git a/apps/desktop/src/main/maker-ipc/silentStopContinuationBinding.ts b/apps/desktop/src/main/maker-ipc/silentStopContinuationBinding.ts
new file mode 100644
index 0000000000..2c76901c4d
--- /dev/null
+++ b/apps/desktop/src/main/maker-ipc/silentStopContinuationBinding.ts
@@ -0,0 +1,64 @@
+/**
+ * silentStopContinuationBinding.ts
+ * ---------------------------------------------------------------------------
+ * silent-stop 自动续跑走 Session.sendHostTurnContinuation(),绕过 send 事务
+ * (makerSendTransaction),因此不会触发把 onTurnReserved 转发给输入协调器的常规
+ * 接线。这两条约定必须在 host 续跑侧自己完成,否则协调器里残留的已派发
+ * activeTurn 会与真实终态永久失配,输入边界卡在忙(僵尸 activeTurn,2026-09-24
+ * 实报:任务永远显示运行中、插话/新消息只能排队不派发):
+ * 1. 预约时(onTurnReserved)把新 vendor generation 交给协调器改绑;
+ * 2. send 在派发确认前失败时(Session 会同步回滚 turnGeneration)回滚绑定,
+ * 否则失败收口 `settleSilentStopDone` 合成的无 generation done 会被同一
+ * 归属守卫丢弃,形成镜像的反向僵尸。
+ *
+ * 这里把这两条接线收口成一个可单测的装配:行为测试用真实的
+ * `Session.sendHostTurnContinuation` 驱动本函数产出的 sendOpts(见
+ * __tests__/silentStopContinuationBinding.test.ts),让预约回调时序与失败回滚
+ * 被真实流程覆盖;register 侧另有源码契约守住两条失败收口确实调用了
+ * `rollbackBinding()`。
+ */
+
+export interface SilentStopContinuationBindingDeps {
+ /** 预约回调:协调器侧 noteHostTurnContinuation(通常经 holder 可选转发)。 */
+ noteHostTurnContinuation: (sessionId: string, vendorTurnGeneration: number) => void;
+ /** 失败回滚:协调器侧 noteHostTurnContinuationFailed。 */
+ noteHostTurnContinuationFailed: (
+ sessionId: string,
+ adoptedVendorTurnGeneration: number,
+ ) => void;
+}
+
+export interface SilentStopContinuationBinding {
+ /**
+ * 并入 `session.sendHostTurnContinuation` 的 opts。onTurnReserved 在 Session
+ * 预约本轮 vendor generation 时同步触发(先于 provider dispatch,任何早期
+ * 终态事件也随之落在新绑定上)。
+ */
+ sendOpts: { onTurnReserved: (reservedGeneration: number) => void };
+ /**
+ * send 未派发(accepted:false)或抛出时的收口动作:把协调器绑定还原为预约前的
+ * generation。尚无预约(回调根本没触发,如派发前被取消)时是 no-op;成功派发
+ * 后不得调用。
+ */
+ rollbackBinding: () => void;
+}
+
+export function bindSilentStopContinuationGeneration(
+ sessionId: string,
+ deps: SilentStopContinuationBindingDeps,
+): SilentStopContinuationBinding {
+ // 预约到的 generation;失败收口据此回滚(Session 回滚后 bound 必须跟随)。
+ let reservedGeneration: number | null = null;
+ return {
+ sendOpts: {
+ onTurnReserved: (reservedTurnGeneration) => {
+ reservedGeneration = reservedTurnGeneration;
+ deps.noteHostTurnContinuation(sessionId, reservedTurnGeneration);
+ },
+ },
+ rollbackBinding: () => {
+ if (reservedGeneration === null) return;
+ deps.noteHostTurnContinuationFailed(sessionId, reservedGeneration);
+ },
+ };
+}