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): 合并进待处理切换时也登记预算重放意图(#4598)

Zhang Yunjinc463aee78953
diff --git a/apps/desktop/src/main/__tests__/makerEventHotPathOrdering.test.ts b/apps/desktop/src/main/__tests__/makerEventHotPathOrdering.test.ts
index 14faeb3a93..5ac53bdee6 100644
--- a/apps/desktop/src/main/__tests__/makerEventHotPathOrdering.test.ts
+++ b/apps/desktop/src/main/__tests__/makerEventHotPathOrdering.test.ts
@@ -30,7 +30,7 @@ const goalStorageSource = readFileSync(goalStorageSourcePath, 'utf8').replace(/\
*/
function loadBudgetReconciler() {
const start = source.indexOf(' const reconcileSessionContextWindowBudgetUnderLock = async (');
- const end = source.indexOf(' sessionContextWindowBudgetApplier = async (sessionId, change) => {', start);
+ const end = source.indexOf(' setModelContextRuntimeRefreshListener(', start);
if (start < 0 || end < 0) throw new Error('budget reconciler source changed');
const code = source.slice(start, end);
const js = transpileModule(code, { compilerOptions: { target: ScriptTarget.ES2022 } }).outputText;
@@ -1272,6 +1272,50 @@ describe('session context window budget reconciler', () => {
else expect(pending.has('task-1')).toBe(true);
});
+ it('keeps the budget intent when it merges into an already pending route switch', async () => {
+ // 已有待处理的模型/来源切换时,改预算只补 forceSessionRebuild 后返回。用户随后重选
+ // 当前模型 → SET_MODEL 清掉那条 pending → 没人再应用预算(Greptile P1,2026-10-24)。
+ const { js } = loadBudgetReconciler();
+ const applied = new Map<string, number | null>();
+ const pending = new Set<string>();
+ const registerPendingCredentialSwitchForSession = vi.fn(async () => {});
+ const deps = {
+ getMakerIfReady: () => ({ getSession: () => ({ model: 'grok-4.6' }) }),
+ getDbClient: () => ({
+ drizzle: {
+ select: () => ({
+ from: () => ({ where: () => ({ limit: async () => [{ agentKind: 'cc', model: 'grok-4.6', providerId: 'xd' }] }) }),
+ }),
+ },
+ }),
+ sessions: { agentKind: 'agentKind', model: 'model', providerId: 'providerId', id: 'id' },
+ eq: () => ({}),
+ dbToMakerAgentKind: () => 'claude-code',
+ getSessionProvider: () => 'xd',
+ getActiveCatalog: () => ({}),
+ readStoredSessionContextWindowBudget: async () => 300_000,
+ appliedContextWindowBudgets: applied,
+ pendingSessionContextWindowBudgetReconciles: pending,
+ contextWindowBudgetChangesEffectiveWindow: () => true,
+ getPendingCredentialSwitchTarget: () => ({ model: 'grok-4.6', providerId: 'xd' }),
+ registerPendingCredentialSwitchForSession,
+ applyRuntimeSetModelChange: vi.fn(),
+ runtimeMutationDeps: () => ({}),
+ withSendToSessionLock: (_id: string, fn: () => Promise<void>) => fn(),
+ log: { debug: vi.fn(), info: vi.fn() },
+ };
+ const applier = new Function(
+ ...Object.keys(deps),
+ `${js}; return sessionContextWindowBudgetApplier;`,
+ )(...Object.values(deps)) as (sessionId: string, change: { previous: number | null }) => Promise<void>;
+
+ await applier('task-1', { previous: null });
+ // 合并不重建:不记 dedup,但意图必须留下,等发送边界重放。
+ expect(registerPendingCredentialSwitchForSession).toHaveBeenCalledOnce();
+ expect(applied.has('task-1')).toBe(false);
+ 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 缺失」重放,首条直发会把刚配置好的实例再关一次
diff --git a/apps/desktop/src/main/maker-ipc/register.ts b/apps/desktop/src/main/maker-ipc/register.ts
index fc0465015f..d6e4e13be5 100644
--- a/apps/desktop/src/main/maker-ipc/register.ts
+++ b/apps/desktop/src/main/maker-ipc/register.ts
@@ -6522,6 +6522,7 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)
const latestBudget = await readStoredSessionContextWindowBudget(sessionId);
if (appliedContextWindowBudgets.has(sessionId)
&& appliedContextWindowBudgets.get(sessionId) === latestBudget) {
+ pendingSessionContextWindowBudgetReconciles.delete(sessionId);
log.debug('session context window budget already applied', {
sessionId, budget: latestBudget,
});
@@ -6549,6 +6550,7 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)
catalog, agent, providerId, modelId: row.model,
previous, next: latestBudget,
})) {
+ pendingSessionContextWindowBudgetReconciles.delete(sessionId);
log.debug('session context window budget does not change the effective window', {
sessionId, previous, next: latestBudget,
});
@@ -6567,6 +6569,9 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)
...pendingTarget,
forceSessionRebuild: true,
});
+ // 意图必须一起登记:那条 pending 随时可能被「重选当前模型」清掉(SET_MODEL 成功
+ // 路径 agentSwitchPending.clear),清掉之后没人再应用预算(Greptile P1,2026-10-24)。
+ pendingSessionContextWindowBudgetReconciles.add(sessionId);
log.info('session context window budget merged into the pending route switch', {
sessionId, previous,
next: latestBudget, targetModel: pendingTarget.model,