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 Yunjinb755ee77fd3e
diff --git a/apps/desktop/src/main/localDb/ipc/__tests__/sessionsUpdate.test.ts b/apps/desktop/src/main/localDb/ipc/__tests__/sessionsUpdate.test.ts
index b2b86dab22..8a6d3d09c7 100644
--- a/apps/desktop/src/main/localDb/ipc/__tests__/sessionsUpdate.test.ts
+++ b/apps/desktop/src/main/localDb/ipc/__tests__/sessionsUpdate.test.ts
@@ -935,6 +935,18 @@ describe('local-db:sessions:update handler wiring', () => {
expect(readSessionContextWindowBudget('cc-local')).toBeNull();
});
+ it('fails a budget-only update when the budget store refuses to write', async () => {
+ // 纯预算请求只有这一个意图:吞掉错误就是「看着成功、实际还是旧窗口」,
+ // 用户想靠缩小窗口省钱时会被骗(Greptile P1,2026-10-24)。
+ vi.mocked(writeSessionContextWindowBudget).mockImplementationOnce(() => {
+ throw new Error('session context budget settings file is unreadable');
+ });
+ await expect(invokeUpdate('cc-local', { contextWindowBudget: 300_000 })).rejects.toThrow(
+ /unreadable/,
+ );
+ expect(readSessionContextWindowBudget('cc-local')).toBeNull();
+ });
+
it('does not write the context window budget into the next account after an owner change', async () => {
// 偏好文件按「当前」账号的 userData 解析,而 DB 更新是 await 出来的:保存期间切账号
// 时,过期请求不得把原任务的预算写进新账号的文件(Greptile P1,2026-10-24)。
diff --git a/apps/desktop/src/main/localDb/ipc/sessions.ts b/apps/desktop/src/main/localDb/ipc/sessions.ts
index 2455576988..cbe0f714ea 100644
--- a/apps/desktop/src/main/localDb/ipc/sessions.ts
+++ b/apps/desktop/src/main/localDb/ipc/sessions.ts
@@ -917,6 +917,8 @@ export async function persistSessionFields(
}
// 预算写偏好文件放在行更新**之后**:偏好文件不可读时 store 会拒绝写(保留坏文件给
// 人工修),若先写就会让同一个 patch 里的其它字段(改名、归档…)整包失败。
+ // 纯预算请求不能降级:那是用户唯一的意图,吞掉错误等于「看着成功、实际还是旧窗口」。
+ const budgetOnlyPatch = Object.keys(rowPatch).length === 0;
let budgetWritten = false;
let nextContextWindowBudget: number | null = null;
if (hasBudgetChange) {
@@ -926,10 +928,14 @@ export async function persistSessionFields(
);
budgetWritten = true;
} catch (error) {
+ if (budgetOnlyPatch) throw error;
+ // 混合更新:其它字段已落库,只降级为「预算未保存」,并从广播里剔除这个键 ——
+ // 否则 UI 会显示一个没存下去的值(Greptile P1,2026-10-24)。
console.warn('[sessions] write context window budget failed; other fields already saved', {
sessionId,
error: error instanceof Error ? error.message : String(error),
});
+ delete clean.contextWindowBudget;
}
}
let runtimeProjection = {};
@@ -2522,6 +2528,9 @@ export async function updateSessionInDb(
nextContextWindowBudget = writeSessionContextWindowBudget(sid, p.contextWindowBudget ?? null);
budgetWritten = true;
} catch (error) {
+ // 纯预算请求(`maker:set-context-window-budget` 就是这种):这是用户唯一的
+ // 意图,必须让它失败,否则「看着成功、实际还是旧窗口」(Greptile P1,2026-10-24)。
+ if (Object.keys(p).every((key) => key === 'contextWindowBudget')) throw error;
console.warn('[sessions] write context window budget failed; row update already applied', {
sessionId: sid,
error: error instanceof Error ? error.message : String(error),