Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): Greptile #7011 三条 P2(#7011)
Zhang Yunjin8b0f6c56c7e3
diff --git a/apps/desktop/src/renderer/components/status/ContextWindowBudgetChip.tsx b/apps/desktop/src/renderer/components/status/ContextWindowBudgetChip.tsxindex 9036c18ec6..9abeb15986 100644--- a/apps/desktop/src/renderer/components/status/ContextWindowBudgetChip.tsx+++ b/apps/desktop/src/renderer/components/status/ContextWindowBudgetChip.tsx@@ -110,6 +110,13 @@ export function ContextWindowBudgetChip({}: ContextWindowBudgetChipProps) {const { t } = useTranslation();const [committing, setCommitting] = useState(false);+ // 被控端不支持预算通道(老版本):卡转只读直到会话换设备。+ const [remoteUnsupported, setRemoteUnsupported] = useState(false);+ useEffect(() => {+ // 换设备(远程→本地 / 换被控端)后重新探测支持状态。+ // 必须挂在组件的提前 return(约 :395)之前,否则 hooks 顺序随渲染路径漂移。+ setRemoteUnsupported(false);+ }, [deviceId]);// 卡片开合:**指针悬浮**展开(与货币 chip 的「用量明细」卡同一节奏:延迟 300ms 开、离开宽限// 200ms 关),键盘仍走 Radix 自己的 Enter/Space 开、Esc 关。const [cardOpen, setCardOpen] = useState(false);@@ -365,9 +372,10 @@ export function ContextWindowBudgetChip({if (base.some((option) => option.tokens === current)) return base;// 已存值可能不在比例档里(旧固定档、或目录上限变过):只按绝对值补一项,// **不套百分比标签**(避免「显示 50%、实际 48.8%」的误导)。- return [...base, { tokens: current, kind: 'current' as const }].sort(+ const result = [...base, { tokens: current, kind: 'current' as const }].sort((a, b) => a.tokens - b.tokens,);+ return result;}, [storedBudget, effectiveDefaultWindow, effectiveMaxWindow, effectiveWindowsReported, reportedMaxWindow, modelLimit, ceiling]);useEffect(() => {@@ -426,10 +434,27 @@ export function ContextWindowBudgetChip({const commit = async (tokens: number): Promise<void> => {// 选中「有效默认档」上报 null(跟随默认,不快照当前值)。const next = tokens === effectiveDefaultWindow ? null : tokens;- if (next === storedBudget) return;if (committing) return;setCommitting(true);try {+ // 按缓存判「等于已存值」时先直查复核:跨窗口缓存不会互相失效,另一窗口+ // 刚保存/清除过时,本地新鲜缓存仍是旧值,会被 no-op 跳过(Greptile P2,+ // 2026-10-24 —— 点「模型默认」清不掉另一窗口保存的预算)。真变更不走这次+ // 直查:提交后的 refresh 本来就会重拉,多查只会打乱时序。直查失败(老被控端)+ // 退回缓存值判定,保持旧行为。+ let current = storedBudget;+ if (next === storedBudget) {+ try {+ const fresh = await queryAuthoritativeBounds(deviceId, sessionId, {+ agent: makerAgent, providerId: providerId ?? null, model,+ });+ if (fresh) current = typeof fresh.budget === 'number' ? fresh.budget : null;+ } catch {+ // 老被控端不认识查询通道:用缓存值判等(与提交通道能力一致,见下)。+ }+ }+ if (next === current) return;+if (deviceId) {// device-link 远程任务:会话的库与运行实例都在被控端,必须走隧道命令;// 写本机的镜像库不会生效,还会被下一次镜像覆盖。@@ -447,6 +472,9 @@ export function ContextWindowBudgetChip({refreshBounds();} catch (error) {const unsupported = extractIpcError(error)?.code === 'DEVICE_LINK_CHANNEL_NOT_ALLOWED';+ // 被控端不支持该通道时记住状态:整卡转只读并说明原因,档位不再可点 ——+ // 反复提交必然失败的修改只会制造更多错误 toast(Greptile P2,2026-10-24)。+ if (unsupported) setRemoteUnsupported(true);toast.error(unsupported? t('ccAgent.contextWindowBudget.remoteUnsupported')@@ -462,7 +490,7 @@ export function ContextWindowBudgetChip({}};- const isDisabled = disabled || committing;+ const isDisabled = disabled || committing || remoteUnsupported;@@ -607,6 +635,12 @@ export function ContextWindowBudgetChip({{t('ccAgent.contextWindowBudget.readOnlyHint')}</p>)}+ {remoteUnsupported && (+ // 被控端不支持预算通道:整卡只读并说明原因,避免反复提交必然失败的修改。+ <p className="mt-0.5 text-12 font-normal leading-[1.5] text-[var(--text-secondary)]">+ {t('ccAgent.contextWindowBudget.remoteUnsupported')}+ </p>+ )}</div><div role="radiogroup" aria-label={t('ccAgent.contextWindowBudget.menuTitle')}>{options.map((option, index) => {@@ -824,6 +858,25 @@ function notifyBoundsReload(exceptId: number, key: string): void {}}+/**+ * 绕过缓存的权威值直查(提交路径专用):boundsReloadListeners 只在当前渲染进程内+ * 生效,另一个窗口刚保存/清除预算时,本窗口 TTL 内的「新鲜」缓存仍是旧值 ——+ * 点「模型默认」会被 no-op 判定跳过,预算永远清不掉(Greptile P2,2026-10-24)。+ * 提交是罕见操作,直查一次即可;菜单展示仍走 TTL 缓存。+ */+async function queryAuthoritativeBounds(+ deviceId: string | undefined,+ sessionId: string,+ route: { agent: MakerAgentKindWire; providerId: string | null; model: string },+): Promise<SessionContextWindowBounds | null> {+ const raw = deviceId+ ? await window.electronAPI.deviceLink.invoke(+ deviceId, 'maker:get-context-window-bounds', [sessionId, route],+ )+ : await window.electronAPI.maker.getSessionContextWindowBounds(sessionId, route);+ return normalizeSessionContextWindowBounds(raw);+}+export function useSessionContextWindowBounds(deviceId: string | undefined,sessionId: string,@@ -872,6 +925,19 @@ export function useSessionContextWindowBounds(};}, [key]);+ // 目录/模型级上限变化(用户改了「上下文上限」)→ 权威边界必须重查:缓存的+ // authoritativeBounds 会继续给出旧上限与旧档位表,保存后 main 却按新上限收敛,+ // 菜单与实际生效不一致(Greptile P2,2026-10-24)。远程边界来自被控端,本机+ // 目录变化与它无关,不订阅。+ useEffect(() => {+ if (deviceId || !key) return;+ const unsubscribe = window.electronAPI?.maker?.onProvidersChanged?.(() => {+ boundsCache.delete(key);+ setReloadToken((token) => token + 1);+ });+ return () => unsubscribe?.();+ }, [deviceId, key]);+useEffect(() => {// eslint-disable-next-line no-consoleactiveKeyRef.current = key;@@ -889,18 +955,15 @@ export function useSessionContextWindowBounds(// 路由随查询一起上报:被控端/主进程按**显示路由**解析边界,而不是按会话行。const route = { agent: agentKind, providerId: providerId ?? null, model };try {- const view = deviceId- ? await window.electronAPI.deviceLink.invoke(- deviceId, 'maker:get-context-window-bounds', [sessionId, route],- )- : await window.electronAPI.maker.getSessionContextWindowBounds(sessionId, route);+ const view = await queryAuthoritativeBounds(deviceId, sessionId, route);// 已取消的请求不得写共享缓存:否则保存预算触发的**新**查询之后,迟到的旧响应// 会把旧预算写回缓存并盖上新 fetchedAt,后挂载的卡片把旧数据当新数据用// (Greptile P2,2026-10-17)。取消的响应什么都不写。- // 已取消的请求不得写共享缓存(Greptile P2):否则保存预算后的新查询之后,- // 迟到的旧响应会把旧预算写回缓存并盖上新 fetchedAt。if (cancelled) return;- const resolved = normalizeSessionContextWindowBounds(view);+ // helper 已归一化;这里**不得**再 normalize 一遍:normalize 非幂等+ // (effectiveWindowsReported 靠「键是否存在」判定,首遍会把缺省键补成+ // 存在键,第二遍就把老被控端翻成「已申报」,档位表随之放开到 modelLimit)。+ const resolved = view;const fetchedAt = Date.now();boundsCache.set(key, { value: resolved, fetchedAt });if (!cancelled && activeKeyRef.current === key) setBoundsState({ key, value: resolved, fetchedAt });diff --git a/apps/desktop/src/renderer/components/status/__tests__/contextWindowBudgetChip.test.tsx b/apps/desktop/src/renderer/components/status/__tests__/contextWindowBudgetChip.test.tsxindex a91a9e543c..ff83f05cb2 100644--- a/apps/desktop/src/renderer/components/status/__tests__/contextWindowBudgetChip.test.tsx+++ b/apps/desktop/src/renderer/components/status/__tests__/contextWindowBudgetChip.test.tsx@@ -1222,3 +1222,90 @@ describe('isBoundsCacheEntryStale(重开菜单刷新判定的纯函数)', ()expect(isBoundsCacheEntryStale(entry, 10_000 + 61_000)).toBe(true);});});+++describe('ContextWindowBudgetChip — Greptile 2026-10-24 三条 P2', () => {+ afterEach(() => {+ cleanup();+ __clearAllBoundsCachesForTest();+ });++ it('re-checks the authoritative budget before skipping a no-op commit (stale cache from another window)', async () => {+ // P2「其他窗口仍显示旧预算」:跨窗口缓存不会互相失效。窗口 A 保存了 500K 后,+ // 窗口 B 的缓存仍是 null —— 点「模型默认」按缓存判 no-op 就会把清除请求整个吞掉。+ // 提交前必须直查复核:发现实际 500K ≠ null,照常提交 null(清除)。+ installElectronApi(vi.fn()+ .mockResolvedValueOnce(boundsView({ budget: null, defaultEffectiveWindow: 1_000_000 })) // 挂载查询+ .mockResolvedValue(boundsView({ budget: 500_000, budgetCustomized: true, defaultEffectiveWindow: 1_000_000 }))); // 提交复核直查+ renderChip({ providers: null });+ const options = await openTierCard();+ // 选中态 = 缓存里的 null → 默认档;用户点「模型默认」(= 清除预算,上报 null)。+ const defaultRow = options.find((el) => el.getAttribute('data-token') === '1000000')!;+ fireEvent.click(defaultRow);+ await waitFor(() => {+ expect(mockUpdate).toHaveBeenCalledWith(SESSION_ID, { contextWindowBudget: null });+ });+ expect(mockToastError).not.toHaveBeenCalled();+ });++ it('re-queries the authoritative bounds when the provider catalog changes', async () => {+ // P2「菜单仍使用旧上限」:用户改了模型级上限后,缓存的 authoritativeBounds 还是+ // 旧值 —— 档位表必须随 providers 变化事件失效重查,否则保存后 main 按新上限收敛、+ // 菜单却还在卖旧档位。+ let answer: ReturnType<typeof boundsView> = boundsView({ defaultEffectiveWindow: 1_000_000, maxEffectiveWindow: 1_000_000 });+ let fireProvidersChanged: (() => void) | null = null;+ const getBounds = vi.fn(async () => answer);+ (window as unknown as { electronAPI: unknown }).electronAPI = {+ deviceLink: { invoke: vi.fn().mockResolvedValue(null) },+ maker: {+ getSessionContextWindowBounds: getBounds,+ onProvidersChanged: (cb: () => void) => {+ fireProvidersChanged = cb;+ return () => { fireProvidersChanged = null; };+ },+ },+ };+ renderChip({ providers: null });+ const trigger = document.querySelector('[data-context-window-budget-chip]')!;+ fireEvent.pointerEnter(trigger);+ await waitFor(() => {+ expect(tierTokens()).toEqual(['250000', '500000', '1000000']);+ });+ // 上限 1M → 200K:事件触发后档位表必须跟着收敛(200K 下只剩默认档)。+ answer = boundsView({ defaultEffectiveWindow: 200_000, maxEffectiveWindow: 200_000 });+ act(() => { fireProvidersChanged?.(); });+ await waitFor(() => {+ expect(tierTokens()).toEqual(['200000']);+ });+ expect(getBounds.mock.calls.length).toBeGreaterThanOrEqual(2);+ });++ it('locks the card read-only after the device rejects the budget channel', async () => {+ // P2「不支持的设备仍可编辑」:CHANNEL_NOT_ALLOWED 之后只弹 toast 会让用户反复提交+ // 必然失败的修改。第二次失败前就必须整卡只读 + 卡内说明原因。+ const invoke = vi.fn((deviceId: string, channel: string) =>+ channel === 'maker:get-context-window-bounds'+ ? Promise.resolve(boundsView({+ budget: 150_000, budgetCustomized: true,+ defaultEffectiveWindow: 200_000, maxEffectiveWindow: 200_000,+ }))+ : Promise.reject(Object.assign(new Error('channel not allowed'), {+ code: 'DEVICE_LINK_CHANNEL_NOT_ALLOWED',+ })));+ (window as unknown as { electronAPI: { deviceLink: { invoke: typeof invoke } } }).electronAPI = {+ deviceLink: { invoke },+ };+ renderChip({ deviceId: 'device-a', providers: null });+ const options = await openTierCard();+ expect(options.length).toBeGreaterThan(0);+ // 点「模型默认」档(≠ 已存 150K):真变更路径直达提交通道,必然被拒。+ fireEvent.click(options.find((el) => el.getAttribute('data-token') === '200000')!);+ await waitFor(() => {+ expect(screen.getByText('ccAgent.contextWindowBudget.remoteUnsupported')).toBeTruthy();+ });+ // 整卡转只读:所有档位行 disabled,不再可反复提交。+ await waitFor(() => {+ expect(screen.queryAllByRole('radio').every((row) => (row as HTMLButtonElement).disabled)).toBe(true);+ });+ });+});