-
Notifications
You must be signed in to change notification settings - Fork 68
fix(compaction): stabilize request-local reduction cache #901
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
codeg-dev
wants to merge
2
commits into
code-yeongyu:main
Choose a base branch
from
codeg-dev:fix/context-reduction-cache-stability-pr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
packages/coding-agent/test/suite/regressions/900-context-reduction-cache-stability.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import { createHash } from "node:crypto"; | ||
| import type { AgentMessage } from "@earendil-works/pi-agent-core"; | ||
| import type { AssistantMessage, ToolResultMessage, Usage, UserMessage } from "@earendil-works/pi-ai"; | ||
| import { describe, expect, it } from "vitest"; | ||
| import { | ||
| BUILTIN_CONTEXT_REDUCTION_OPTIONS, | ||
| type ContextReductionLatch, | ||
| createContextReductionLatch, | ||
| reduceContextMessages, | ||
| resetContextReductionLatch, | ||
| shouldApplyContextReduction, | ||
| } from "../../../src/core/extensions/builtin/compaction/context-reduction.ts"; | ||
|
|
||
| const CONTEXT_WINDOW = 1_000_000; | ||
| const EXPOSURE_REQUESTS = 507; | ||
| const OBSERVED_THRESHOLD_CROSSINGS = 319; | ||
|
|
||
| function emptyUsage(): Usage { | ||
| return { | ||
| input: 0, | ||
| output: 0, | ||
| cacheRead: 0, | ||
| cacheWrite: 0, | ||
| totalTokens: 0, | ||
| cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, | ||
| }; | ||
| } | ||
|
|
||
| function assistantToolCall(id: string, timestamp: number): AssistantMessage { | ||
| return { | ||
| role: "assistant", | ||
| content: [{ type: "toolCall", id, name: "bash", arguments: { command: `probe-${id}` } }], | ||
| api: "faux-completion", | ||
| provider: "faux", | ||
| model: "faux-model", | ||
| usage: emptyUsage(), | ||
| stopReason: "toolUse", | ||
| timestamp, | ||
| }; | ||
| } | ||
|
|
||
| function toolResult(id: string, timestamp: number): ToolResultMessage { | ||
| return { | ||
| role: "toolResult", | ||
| toolCallId: id, | ||
| toolName: "bash", | ||
| content: [{ type: "text", text: `result-${id}-${"x".repeat(4_000)}` }], | ||
| isError: false, | ||
| timestamp, | ||
| }; | ||
| } | ||
|
|
||
| function userMessage(text: string, timestamp: number): UserMessage { | ||
| return { role: "user", content: text, timestamp }; | ||
| } | ||
|
|
||
| function thresholdControlHistory(): AgentMessage[] { | ||
| const messages: AgentMessage[] = [userMessage("sanitized threshold-control fixture", 1)]; | ||
| for (let index = 0; index < 12; index += 1) { | ||
| const id = `call-${index}`; | ||
| messages.push(assistantToolCall(id, index * 2 + 2), toolResult(id, index * 2 + 3)); | ||
| } | ||
| messages.push(userMessage("latest request", 100)); | ||
| return messages; | ||
| } | ||
|
|
||
| function payloadHash(messages: AgentMessage[]): string { | ||
| return createHash("sha256").update(JSON.stringify(messages)).digest("hex"); | ||
| } | ||
|
|
||
| describe("request-local context reduction cache stability", () => { | ||
| it("keeps one payload shape across the 507-request threshold-control fixture", () => { | ||
| // Given: the incident had a one-million-token window, 507 proxy-exposed | ||
| // requests, and 319 observed threshold crossings. This intentionally small | ||
| // control fixture isolates state-machine oscillation without claiming to | ||
| // reproduce payload scale or that the proxy cohort measures caused overhead. | ||
| // Payload-scale reducer behavior is pinned by the companion lifecycle test. | ||
| const messages = thresholdControlHistory(); | ||
| const latch = { engaged: false } satisfies ContextReductionLatch; | ||
| const usageSeries = Array.from({ length: EXPOSURE_REQUESTS }, (_, index) => { | ||
| if (index > OBSERVED_THRESHOLD_CROSSINGS) return 499_000; | ||
| return index % 2 === 0 ? 501_000 : 499_000; | ||
| }); | ||
|
|
||
| // When: every provider request independently assembles context from the | ||
| // same stored history while computed usage crosses the 50% gate. | ||
| const hashes = usageSeries.map((usageTokens) => { | ||
| const shouldReduce = shouldApplyContextReduction({ usageTokens, contextWindow: CONTEXT_WINDOW }, latch); | ||
| const outgoing = shouldReduce | ||
| ? reduceContextMessages(messages, BUILTIN_CONTEXT_REDUCTION_OPTIONS).messages | ||
| : messages; | ||
| return payloadHash(outgoing); | ||
| }); | ||
|
|
||
| // Then: once reduction engages, request-local payloads retain one stable | ||
| // cacheable shape until persisted compaction resets the latch. | ||
| expect(new Set(hashes).size).toBe(1); | ||
| expect(hashes.every((hash) => hash !== payloadHash(messages))).toBe(true); | ||
| }); | ||
|
|
||
| it("resets the sticky reduction state after accepted persisted compaction", () => { | ||
| const latch = createContextReductionLatch(); | ||
|
|
||
| expect(shouldApplyContextReduction({ usageTokens: 501_000, contextWindow: CONTEXT_WINDOW }, latch)).toBe(true); | ||
| expect(shouldApplyContextReduction({ usageTokens: 499_000, contextWindow: CONTEXT_WINDOW }, latch)).toBe(true); | ||
|
|
||
| resetContextReductionLatch(latch); | ||
|
|
||
| expect(shouldApplyContextReduction({ usageTokens: 499_000, contextWindow: CONTEXT_WINDOW }, latch)).toBe(false); | ||
| }); | ||
|
|
||
| it("preserves the provider-native compaction bypass while sticky", () => { | ||
| const latch = { engaged: true } satisfies ContextReductionLatch; | ||
| const shouldReduce = shouldApplyContextReduction( | ||
| { | ||
| usageTokens: 900_000, | ||
| contextWindow: CONTEXT_WINDOW, | ||
| isProviderNativeCompactionPath: true, | ||
| }, | ||
| latch, | ||
| ); | ||
| expect(shouldReduce).toBe(false); | ||
| expect(latch.engaged).toBe(true); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.