Repository navigation
fix(usage): measure server-side fallback replies by the last run (#877) #884
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
iceteaSA
wants to merge
2
commits into
cortexkit:master
Choose a base branch
from
iceteaSA:fix/server-fallback-usage
base: master
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
45 changes: 45 additions & 0 deletions
45
packages/plugin/src/shared/provider-response-completion.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,45 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import * as completion from "./provider-response-completion"; | ||
|
|
||
| for (const location of ["native", "state"]) { | ||
| test(`detects usage and boundary markers in ${location}.signature`, () => { | ||
| for (const signature of [ | ||
| 'cortexkit-server-fallback-usage-v1:{"iterations":[]}', | ||
| "cortexkit-server-fallback-v1:primary|fallback", | ||
| ]) { | ||
| expect( | ||
| completion.hasServerFallbackUsage([ | ||
| { type: "reasoning", [location]: { signature } }, | ||
| ]), | ||
| ).toBe(true); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| test("detects server-fallback usage only in the marked reasoning part", () => { | ||
| const content = [ | ||
| { type: "reasoning", text: "thinking", native: { signature: "real-signature" } }, | ||
| { | ||
| type: "reasoning", | ||
| text: " ", | ||
| native: { | ||
| signature: "cortexkit-server-fallback-v1:claude-opus-5-5|claude-opus-4-8", | ||
| }, | ||
| }, | ||
| { type: "text", text: "answer" }, | ||
| { type: "tool", name: "read" }, | ||
| ]; | ||
| expect(completion.hasServerFallbackUsage(content)).toBe(true); | ||
| expect(completion.hasServerFallbackUsage([content[0], content[2]])).toBe(false); | ||
| for (const invalid of [ | ||
| undefined, | ||
| null, | ||
| {}, | ||
| [null, {}, { type: "reasoning", native: { signature: 3 } }], | ||
| [{ type: "text", native: content[1]!.native }], | ||
| [{ type: "reasoning", native: { signature: "cortexkit-server-fallback-v10:models" } }], | ||
| [{ type: "reasoning", native: { signature: "other-cortexkit-server-fallback-v1:models" } }], | ||
| ]) { | ||
| expect(completion.hasServerFallbackUsage(invalid)).toBe(false); | ||
| } | ||
| }); | ||
64 changes: 64 additions & 0 deletions
64
packages/plugin/src/shared/provider-response-completion.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
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
115 changes: 115 additions & 0 deletions
115
packages/plugin/src/v2/hooks/server-fallback-usage.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,115 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { getOrCreateSessionMeta } from "../../features/magic-context/storage"; | ||
| import { oc2ContextFixture } from "./__tests__/oc2-context.fixture"; | ||
|
|
||
| const markers = [ | ||
| "boundary", | ||
| "usage-native", | ||
| "usage-state", | ||
| "invalid-json", | ||
| "missing-fallback", | ||
| "none", | ||
| ]; | ||
| for (const marker of markers) { | ||
| const marked = marker !== "none"; | ||
| const usage = marker.startsWith("usage"); | ||
| test(`OC2 newest ${marker} reply ${marked ? "admits at per-run pressure without execute" : "enters the force band"}`, async () => { | ||
| const f = await oc2ContextFixture(801 + markers.indexOf(marker), { | ||
| area: "server-fallback-usage", | ||
| provider: "anthropic", | ||
| model: "claude-opus-5-5", | ||
| cacheTtl: "1h", | ||
| }); | ||
| try { | ||
| await f.pass(); | ||
| f.respond(1, 507_000); | ||
| await f.pass(); | ||
| const before = getOrCreateSessionMeta(f.db, f.session); | ||
| expect(before.lastInputTokens).toBe(507_000); | ||
| f.respond(1, 963_300); | ||
| const latest = f.rows.at(-1)!; | ||
| const row = f.raw | ||
| .prepare("SELECT data FROM session_message WHERE id=?") | ||
| .get(latest.id) as { data: string }; | ||
| const data = JSON.parse(row.data); | ||
| data.content.unshift({ | ||
| type: "reasoning", | ||
| text: " ", | ||
| [marker === "usage-state" ? "state" : "native"]: { | ||
| signature: usage | ||
| ? `cortexkit-server-fallback-usage-v1:${JSON.stringify({ | ||
| iterations: [ | ||
| { | ||
| type: "message", | ||
| model: "primary", | ||
| input_tokens: 453_300, | ||
| output_tokens: 50, | ||
| }, | ||
| { | ||
| type: "fallback_message", | ||
| model: "fallback", | ||
| input_tokens: 100_000, | ||
| output_tokens: 50, | ||
| cache_read_input_tokens: 400_000, | ||
| cache_creation_input_tokens: 10_000, | ||
| }, | ||
| ], | ||
| })}` | ||
| : marker === "invalid-json" | ||
| ? "cortexkit-server-fallback-usage-v1:{" | ||
| : marker === "missing-fallback" | ||
| ? 'cortexkit-server-fallback-usage-v1:{"iterations":[]}' | ||
| : marked | ||
| ? "cortexkit-server-fallback-v1:claude-opus-5-5|claude-opus-4-8" | ||
| : "real-signature", | ||
| }, | ||
| }); | ||
| if (usage) | ||
| data.content.unshift({ | ||
| type: "reasoning", | ||
| native: { | ||
| signature: "cortexkit-server-fallback-v1:primary|fallback", | ||
| }, | ||
| }); | ||
| f.raw | ||
| .prepare("UPDATE session_message SET data=? WHERE id=?") | ||
| .run(JSON.stringify(data), latest.id); | ||
| const result = await f.attempt(); | ||
| const logs = result.logs.join("\n"); | ||
| const meta = getOrCreateSessionMeta(f.db, f.session); | ||
| if (marked) { | ||
| expect(result.error).toBeUndefined(); | ||
| expect(meta.lastInputTokens).toBe(usage ? 510_000 : 507_000); | ||
| expect(meta.lastContextPercentage).toBeCloseTo( | ||
| usage | ||
| ? (510_000 / before.lastUsageContextLimit) * 100 | ||
| : before.lastContextPercentage, | ||
| 5, | ||
| ); | ||
| expect(meta.lastUsageContextLimit).toBe(before.lastUsageContextLimit); | ||
| expect(meta.lastResponseTime).toBe(latest.created + 1000); | ||
| expect(logs).toMatch(/transform scheduler: .*decision=defer/); | ||
| expect(logs).not.toMatch(/transform scheduler: .*decision=execute/); | ||
| if (!usage) | ||
| expect(logs).toContain("ignored server-fallback reply inputTokens=963300"); | ||
| if (marker === "invalid-json" || marker === "missing-fallback") { | ||
| expect( | ||
| result.logs.filter((line) => | ||
| line.includes("invalid server-fallback usage marker"), | ||
| ), | ||
| ).toHaveLength(1); | ||
| const repeat = await f.attempt(); | ||
| expect(repeat.logs.join("\n")).not.toContain( | ||
| "invalid server-fallback usage marker", | ||
| ); | ||
| } | ||
| } else { | ||
| expect(meta.lastInputTokens).toBe(963_300); | ||
| expect(meta.lastContextPercentage).toBeGreaterThan(95); | ||
| expect(logs).toMatch(/transform scheduler: .*decision=execute/); | ||
| } | ||
| } finally { | ||
| await f.cleanup(); | ||
| } | ||
| }); | ||
| } |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P3: The first test is titled "detects usage" but its usage marker payload
{"iterations":[]}is invalid, so it only passes through the invalid-usage boundary path. Use a payload with afallback_messageentry so the test covers a valid usage marker.Prompt for AI agents