Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): 只在有未完成预算意图时于发送边界重放(#4598)
Zhang Yunjin9c85aaece54f
diff --git a/apps/desktop/src/main/__tests__/makerEventHotPathOrdering.test.ts b/apps/desktop/src/main/__tests__/makerEventHotPathOrdering.test.tsindex 42e0cfcd67..14faeb3a93 100644--- a/apps/desktop/src/main/__tests__/makerEventHotPathOrdering.test.ts+++ b/apps/desktop/src/main/__tests__/makerEventHotPathOrdering.test.ts@@ -46,6 +46,7 @@ describe('maker:event hot path ordering', () => {// 任务级窗口预算的应用去重表也在这一段的 `delete` 里(见 register.ts 的// cleanupClosedSessionRuntime):沙箱必须把它当依赖注入,否则抽样执行会 ReferenceError。const appliedContextWindowBudgets = new Map<string, number | null>([['departing-task', 300_000]]);+ const pendingSessionContextWindowBudgetReconciles = new Set<string>(['departing-task']);const deps = {pendingCredentialSwitchHolder: null, deferredCodexRestartHolder: null,gatewayCodexRestartHolder: { onSessionSettled: vi.fn() },@@ -54,6 +55,7 @@ describe('maker:event hot path ordering', () => {gitSnapshotCoordinator: { onSessionClosed: vi.fn() },clearOrcaMcpHydrated: vi.fn(), knownNonOrcaSessionIds: new Set(),appliedContextWindowBudgets,+ pendingSessionContextWindowBudgetReconciles,lastReportedCostUsdBySession: new Map(), lastReportedModelUsageBySession: new Map(),turnModelPromiseBySession: new Map(), turnUsageContextBySession: new Map(),productTurnWallClockTracker: { clear: cleared }, productTurnUsageTargetTracker: { clear: vi.fn() },@@ -67,6 +69,7 @@ describe('maker:event hot path ordering', () => {expect(cleared).toHaveBeenCalledWith('departing-task');// 关闭任务时必须把窗口预算的去重记账一起清掉,否则同 id 的替换实例会沿用旧值。expect(appliedContextWindowBudgets.has('departing-task')).toBe(false);+ expect(pendingSessionContextWindowBudgetReconciles.has('departing-task')).toBe(false);expect(deps.clearSessionPersistState).toHaveBeenCalledWith('departing-task');expect(deps.handleAgentIslandSessionClosedAfterCleanup).toHaveBeenCalledWith('departing-task', 'process-closed');});@@ -1228,6 +1231,7 @@ describe('session context window budget reconciler', () => {// agentSwitchPending.clear),预算就永久不生效(Greptile P1,2026-10-24)。const { code, js } = loadBudgetReconciler();const applied = new Map<string, number | null>();+ const pending = new Set<string>();const applyRuntimeSetModelChange = vi.fn(async () => ({ status }));const deps = {getMakerIfReady: () => ({ getSession: () => ({ model: 'grok-4.6' }) }),@@ -1245,6 +1249,7 @@ describe('session context window budget reconciler', () => {getActiveCatalog: () => ({}),readStoredSessionContextWindowBudget: async () => 300_000,appliedContextWindowBudgets: applied,+ pendingSessionContextWindowBudgetReconciles: pending,contextWindowBudgetChangesEffectiveWindow: () => true,getPendingCredentialSwitchTarget: () => null,registerPendingCredentialSwitchForSession: vi.fn(),@@ -1262,5 +1267,18 @@ describe('session context window budget reconciler', () => {expect(applyRuntimeSetModelChange).toHaveBeenCalledOnce();if (recorded) expect(applied.get('task-1')).toBe(300_000);else expect(applied.has('task-1')).toBe(false);+ // 意图集合才是重放的唯一开关:deferred 留下,applied 清掉。+ if (recorded) expect(pending.has('task-1')).toBe(false);+ else expect(pending.has('task-1')).toBe(true);+ });++ it('replays a budget at the send boundary only when a rebuild is still pending', () => {+ // 任务启动时 create opts 已带上偏好预算(applySessionContextWindowBudgetToCreateOpts),+ // 此时 dedup 还没有条目 —— 若按「dedup 缺失」重放,首条直发会把刚配置好的实例再关一次+ // (首条消息多等一次冷启动;Greptile P2,2026-10-24)。+ const boundary = source.slice(source.indexOf("stage = 'direct-send:"));+ expect(boundary).toContain('if (pendingSessionContextWindowBudgetReconciles.has(sessionId)) {');+ expect(boundary.indexOf('pendingSessionContextWindowBudgetReconciles.has(sessionId)'))+ .toBeLessThan(boundary.indexOf('reconcileSessionContextWindowBudgetUnderLock('));});});diff --git a/apps/desktop/src/main/maker-ipc/__tests__/orcaModelSelectionIntent.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/orcaModelSelectionIntent.test.tsindex 7cce5d87d8..23db76b3f6 100644--- a/apps/desktop/src/main/maker-ipc/__tests__/orcaModelSelectionIntent.test.ts+++ b/apps/desktop/src/main/maker-ipc/__tests__/orcaModelSelectionIntent.test.ts@@ -205,6 +205,7 @@ describe('complete selection commit boundaries', () => {// 发送边界的预算对账(register.ts):本用例只关心路由选择,注入 no-op。reconcileSessionContextWindowBudgetUnderLock: vi.fn(async () => {}),appliedContextWindowBudgets: new Map<string, number | null>(),+ pendingSessionContextWindowBudgetReconciles: new Set<string>(),});const directSend = compile(directSendEntry, { pendingAgentSwitchApplyHolder: adapter });const lease = await directSend('scheduled-task', undefined,@@ -242,7 +243,8 @@ describe('complete selection commit boundaries', () => {reconcileBotModelRoute: vi.fn(), applyPendingAgentSwitchIfIdle, isUserPendingAgentSwitchIntent,agentSwitchDeps: deps, contextOverflowRolloverHolder: null,reconcileSessionContextWindowBudgetUnderLock: vi.fn(async () => {}),- appliedContextWindowBudgets: new Map<string, number | null>() });+ appliedContextWindowBudgets: new Map<string, number | null>(),+ pendingSessionContextWindowBudgetReconciles: new Set<string>() });const sending = directSend('queued-picker');finishPicker();await picker;@@ -342,6 +344,7 @@ describe('Orca model-only picker intent admission', () => {applyPendingAgentSwitchIfIdle, isUserPendingAgentSwitchIntent, agentSwitchDeps: deps, contextOverflowRolloverHolder: null,reconcileSessionContextWindowBudgetUnderLock: vi.fn(async () => {}),appliedContextWindowBudgets: new Map<string, number | null>(),+ pendingSessionContextWindowBudgetReconciles: new Set<string>(),});if (mode === 'cross-engine' || mode === 'relocation') {const stageAxis = compile(stageAxisAdapter, { ...axisCatalogBindings(row), agentSwitchPending: pending, agentSwitchDeps: deps,diff --git a/apps/desktop/src/main/maker-ipc/register.ts b/apps/desktop/src/main/maker-ipc/register.tsindex f95c8c7beb..fc0465015f 100644--- a/apps/desktop/src/main/maker-ipc/register.ts+++ b/apps/desktop/src/main/maker-ipc/register.ts@@ -4114,6 +4114,16 @@ const pendingFailedTurnAssistantPersistId = new Map<string, string>();*/const appliedContextWindowBudgets = new Map<string, number | null>();+/**+ * 已登记、但还没真正落到活实例的预算重建意图。+ *+ * 只在这条集合里重放(发送边界),而不是「dedup 缺失就重放」:任务启动时+ * `applySessionContextWindowBudgetToCreateOpts` 已把偏好预算带给引擎,此时 dedup 里+ * 还没有条目,靠「缺失」判断会误判窗口变化,把刚配置好的实例再关一次+ * (首条直发多等一次冷启动;Greptile P2,2026-10-24)。+ */+const pendingSessionContextWindowBudgetReconciles = new Set<string>();+/*** 跟踪每个 session 的逻辑 turn 与后台节流 keepalive。外部 running guard* 通过 isSessionInTurn 查询真实 busy;mainWindow 后台节流订阅 keepalive 聚合状态。@@ -6010,6 +6020,7 @@ function cleanupClosedSessionRuntime(session: WiredSession): void {clearOrcaMcpHydrated(session.id);knownNonOrcaSessionIds.delete(session.id);appliedContextWindowBudgets.delete(session.id);+ pendingSessionContextWindowBudgetReconciles.delete(session.id);lastReportedCostUsdBySession.delete(session.id);lastReportedModelUsageBySession.delete(session.id);turnModelPromiseBySession.delete(session.id);@@ -6501,7 +6512,9 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)): Promise<void> => {const live = getMakerIfReady()?.getSession(sessionId);if (!live) {+ // 实例已关:下次启动会按偏好文件注入预算,没有需要重放的意图了。appliedContextWindowBudgets.delete(sessionId);+ pendingSessionContextWindowBudgetReconciles.delete(sessionId);return;}// 已应用(偏好文件里的当前值 == 记账值)→ 立即返回:发送边界每次都会调这里,@@ -6579,11 +6592,14 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)// 不记的话发送边界的对账会一直重试,直到新实例确实采用预算。if (result.status === 'applied') {appliedContextWindowBudgets.set(sessionId, latestBudget);+ pendingSessionContextWindowBudgetReconciles.delete(sessionId);log.info('session context window budget applied via runtime rebuild', {sessionId, previous, next: latestBudget,});return;}+ // 只登记意图、不记 dedup:发送边界会重试,直到新实例确实采用预算。+ pendingSessionContextWindowBudgetReconciles.add(sessionId);log.info('session context window budget rebuild deferred to the send boundary', {sessionId, previous, next: latestBudget,});@@ -10764,6 +10780,8 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)cancelPendingAgentSwitchHolder = (sessionId) => {agentSwitchPending.clear(sessionId);appliedContextWindowBudgets.delete(sessionId);+ // 预算重放意图**不**在这里清:取消的只是路由切换,预算仍要按当前 live 路由生效+ // (清掉就退回 Greptile P1 的原场景:pending 被清 → 预算永久不生效)。broadcastSessionPatched(sessionId, {agentSwitchIntent: null,agentSwitchIntentCanceled: true,@@ -11432,12 +11450,16 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)}// 预算对账的重放:回合中登记的预算重建意图可能被「重选当前模型」清掉,而预算// 变更通知只来一次 —— 不在这里补一次,活实例会一直按旧窗口跑(Greptile P1)。- // 已应用(dedup == 偏好值)时内部立即返回,多发一条消息只多一次内存比较。- stage = 'direct-send:reconcileContextWindowBudget';- await reconcileSessionContextWindowBudgetUnderLock(- sessionId,- appliedContextWindowBudgets.get(sessionId) ?? null,- );+ // 只在**有未完成意图**时重放:任务启动本就把偏好预算带进了 create opts,此+ // 时 dedup 还没有条目,按「dedup 缺失」判断会把刚配置好的实例再关一次+ // (Greptile P2,2026-10-24)。+ if (pendingSessionContextWindowBudgetReconciles.has(sessionId)) {+ stage = 'direct-send:reconcileContextWindowBudget';+ await reconcileSessionContextWindowBudgetUnderLock(+ sessionId,+ appliedContextWindowBudgets.get(sessionId) ?? null,+ );+ }let resolvedSelection: ScheduledModelSelection | undefined;if (selection) {stage = 'direct-send:applyScheduledModelSelection';