Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(cost): the contextDecay notaus lands on the 2.6.5 window, not a third one

David0a5cb5923845
diff --git a/src/hooks/useAgentChat.ts b/src/hooks/useAgentChat.ts
index aae3a121..e96a05b1 100644
--- a/src/hooks/useAgentChat.ts
+++ b/src/hooks/useAgentChat.ts
@@ -769,7 +769,7 @@ export function useAgentChat() {
// moves every step is a prompt prefix that is never the same twice.
// Whole messages are dropped here, never shortened: decay stays on
// the send copy alone, so the store keeps every result complete.
- agentMessages = trimWorkingHistory(agentMessages, sendWindow, { enabled: decayOn }).messages
+ agentMessages = trimWorkingHistory(agentMessages, sendWindow, { enabled: decayOn, hysteresis: decayOn }).messages
const built = buildRequestMessages(agentMessages, {
budgetTokens: sendWindow,
enabled: decayOn,
diff --git a/src/hooks/useCodex.ts b/src/hooks/useCodex.ts
index bfb5905f..7b7559f1 100644
--- a/src/hooks/useCodex.ts
+++ b/src/hooks/useCodex.ts
@@ -892,7 +892,7 @@ export function useCodex() {
// moves every step is a prompt prefix that is never the same twice.
// Whole messages are dropped here, never shortened: decay stays on
// the send copy alone, so the store keeps every result complete.
- messages = trimWorkingHistory(messages, sendWindow, { enabled: decayOn }).messages
+ messages = trimWorkingHistory(messages, sendWindow, { enabled: decayOn, hysteresis: decayOn }).messages
const built = buildRequestMessages(messages, {
budgetTokens: sendWindow,
enabled: decayOn,
diff --git a/src/lib/__tests__/compaction-hysteresis.test.ts b/src/lib/__tests__/compaction-hysteresis.test.ts
index dfc07396..33ffb714 100644
--- a/src/lib/__tests__/compaction-hysteresis.test.ts
+++ b/src/lib/__tests__/compaction-hysteresis.test.ts
@@ -205,6 +205,48 @@ describe('A3: the prefix holds still at the plateau, with decay running', () =>
}
})
+ it('the notaus lands on the plain budget, not on a third behaviour', () => {
+ // Flipping contextDecay off is a support move. It has to reproduce what
+ // 2.6.5 did: full results, and a window trimmed to the budget itself
+ // rather than to 70 percent of it.
+ let history: DecayMessage[] = [
+ { role: 'system', content: 'SYSTEM' },
+ { role: 'user', content: 'go' },
+ ]
+ for (let step = 1; step <= 12; step++) {
+ history = appendStep(history, step, 12000)
+ history = trimWorkingHistory(history, BUDGET, { enabled: false, hysteresis: false }).messages
+ }
+ const off = buildRequestMessages(history, { budgetTokens: BUDGET, enabled: false, hysteresis: false })
+ expect(off.trimmedCount).toBe(0)
+ expect(off.prunedPlans).toBe(0)
+ for (const m of off.messages) {
+ expect(String(m.content ?? '')).not.toContain('truncated')
+ }
+ expect(off.promptTokens).toBeLessThanOrEqual(BUDGET)
+ })
+
+ it('the notaus trims at the budget while the band waits for 15 percent more', () => {
+ // A history sitting between the budget and the trigger is exactly where
+ // the two behaviours part company.
+ let history: DecayMessage[] = [
+ { role: 'system', content: 'SYSTEM' },
+ { role: 'user', content: 'go' },
+ ]
+ let step = 0
+ while (estimateMessageTokens(history as never) <= BUDGET && step < 40) {
+ history = appendStep(history, ++step, 12000)
+ }
+ const raw = estimateMessageTokens(history as never)
+ expect(raw).toBeGreaterThan(BUDGET)
+ expect(raw).toBeLessThan(BUDGET * COMPACT_TRIGGER_RATIO)
+
+ const banded = trimWorkingHistory(history, BUDGET, { enabled: false, hysteresis: true })
+ const plain = trimWorkingHistory(history, BUDGET, { enabled: false, hysteresis: false })
+ expect(banded.dropped).toBe(0)
+ expect(plain.dropped).toBeGreaterThan(0)
+ })
+
it('trimming never opens the window on an orphan tool result', () => {
let history: DecayMessage[] = [
{ role: 'system', content: 'SYSTEM' },
diff --git a/src/lib/context-decay.ts b/src/lib/context-decay.ts
index 4a7862ef..35a070da 100644
--- a/src/lib/context-decay.ts
+++ b/src/lib/context-decay.ts
@@ -360,18 +360,23 @@ export function pruneSupersededPlans<T extends DecayMessage>(
*
* Stateless: the decision comes from the array being carried, never from a
* watermark, so a step aborted halfway strands nothing.
+ *
+ * With `hysteresis` off the band collapses to the plain budget, which is what
+ * the contextDecay notaus wants: a support case that flips the switch should
+ * land on the 2.6.5 window, not on a third behaviour nobody has ever seen.
*/
export function trimWorkingHistory<T extends DecayMessage>(
messages: T[],
budgetTokens: number,
- opts: DecayOptions<T> = {},
+ opts: DecayOptions<T> & { hysteresis?: boolean } = {},
): { messages: T[]; dropped: number } {
const sizeOf = (list: T[]) =>
estimateMessageTokens(
applyToolResultDecay(list, opts).messages as unknown as Parameters<typeof estimateMessageTokens>[0],
)
- const trigger = Math.floor(budgetTokens * COMPACT_TRIGGER_RATIO)
- const target = Math.floor(budgetTokens * COMPACT_TARGET_RATIO)
+ const banded = opts.hysteresis !== false
+ const trigger = banded ? Math.floor(budgetTokens * COMPACT_TRIGGER_RATIO) : budgetTokens
+ const target = banded ? Math.floor(budgetTokens * COMPACT_TARGET_RATIO) : budgetTokens
if (budgetTokens <= 0 || sizeOf(messages) <= trigger) return { messages, dropped: 0 }
const systemMsg = messages[0]?.role === 'system' ? messages[0] : null