Repository navigation
fix(loops): meter the usage an adapter error terminal carries #7199
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
Closed
vadymhimself
wants to merge
4
commits into
lidge-jun:dev
from
vadymhimself:fix/loop-error-iteration-usage
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7e53d2b
fix(web-search): meter every loop iteration in the usage ledger
vadymhimself 5c1f571
fix(web-search): report hidden iterations' usage on translator-budget…
vadymhimself 3dbe643
fix(web-search): hidden iterations add counters only to the final usa…
vadymhimself b514f6e
fix(loops): meter the usage an adapter error terminal carries
vadymhimself 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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { afterEach, expect, spyOn, test } from "bun:test"; | ||
| import type { ProviderAdapter } from "../../src/adapters/base"; | ||
| import type { AdapterEvent, OcxParsedRequest } from "../../src/types"; | ||
| import { runWithWebSearch } from "../../src/web-search/loop"; | ||
| import { buildWebSearchTool } from "../../src/web-search/synthetic-tool"; | ||
| import { createTestTranslatorBudget } from "../helpers/translator-budget"; | ||
|
|
||
| const originalFetch = globalThis.fetch; | ||
| afterEach(() => { globalThis.fetch = originalFetch; }); | ||
|
|
||
| // A pass that ends in an adapter error was billed; the request log must meter it, and the turn | ||
| // must still fail with the same error. | ||
| test("an errored iteration's usage reaches the request log beside the search pass", async () => { | ||
| spyOn(globalThis, "fetch").mockResolvedValue(Response.json({ results: [] })); | ||
| const passes: AdapterEvent[][] = [ | ||
| [ | ||
| { type: "tool_call_start", id: "ws1", name: "web_search" }, | ||
| { type: "tool_call_delta", arguments: JSON.stringify({ query: "docs" }) }, | ||
| { type: "tool_call_end" }, | ||
| { type: "done", usage: { inputTokens: 1000, outputTokens: 50 } }, | ||
| ], | ||
| [{ type: "text_delta", text: "partial" }, { type: "error", message: "provider blew up", usage: { inputTokens: 1400, outputTokens: 30 } }], | ||
| ]; | ||
| const adapter: ProviderAdapter = { | ||
| name: "errored-iteration", | ||
| buildRequest: () => ({ url: "https://routed.test/v1", method: "POST", headers: {}, body: "{}" }), | ||
| fetchResponse: async () => new Response("wire"), | ||
| async *parseStream() { yield* passes.shift()!; }, | ||
| }; | ||
| const parsed: OcxParsedRequest = { | ||
| modelId: "fixture", stream: true, options: {}, context: { messages: [], tools: [buildWebSearchTool()] }, | ||
| }; | ||
| const reported: unknown[] = []; | ||
| const response = await runWithWebSearch({ | ||
| parsed, adapter, backend: "exa", exaApiKey: "fixture", hostedTool: { type: "web_search" }, | ||
| settings: { model: "fixture", reasoning: "low", timeoutMs: 1000 }, maxSearches: 1, | ||
| incomingMeta: { headers: new Headers(), translatorBudget: createTestTranslatorBudget() }, | ||
| onUsage: usage => reported.push(usage), | ||
| }); | ||
| const text = await response.text(); | ||
| expect(text).toContain("response.failed"); | ||
| expect(text).toContain("Provider stream error: provider blew up"); | ||
| expect(reported).toEqual([expect.objectContaining({ inputTokens: 2400, outputTokens: 80 })]); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.