Repository navigation
fix(run): forward wrapper signals, log folded agent keys, opt-in --max-tokens budget #1655
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
RaghwendraSingh
wants to merge
3
commits into
main
Choose a base branch
from
fix/run-token-budget-and-signals
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
3 commits
Select commit
Hold shift + click to select a range
dc52adb
fix(bin): forward termination signals to the bun child
RaghwendraSingh 5d074ae
fix(config): log the agent config keys that are folded into options
RaghwendraSingh e8a47a7
feat(run): optional --max-tokens budget across the run and its task s…
RaghwendraSingh 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { describe, expect, test } from "bun:test" | ||
| import { Schema } from "effect" | ||
| import { ConfigAgentV1 } from "../../src/v1/config/agent" | ||
|
|
||
| const decode = Schema.decodeUnknownSync(ConfigAgentV1.Info) | ||
|
|
||
| describe("ConfigAgentV1 top-level keys", () => { | ||
| test("a known key is accepted as itself", () => { | ||
| const agent = decode({ model: "test/model", steps: 3, description: "Known keys only" }) | ||
| expect(agent).toMatchObject({ model: "test/model", steps: 3, description: "Known keys only" }) | ||
| expect(agent.options).toEqual({}) | ||
| expect(ConfigAgentV1.unknownKeys({ model: "test/model", steps: 3 })).toEqual([]) | ||
| }) | ||
|
|
||
| test("an unknown top-level key is folded into options and can be named", () => { | ||
| const raw = { model: "test/model", budget: 5, audience: "executive" } | ||
| const agent = decode(raw) | ||
| expect(agent.options).toEqual({ budget: 5, audience: "executive" }) | ||
| expect(agent).not.toHaveProperty("steps") | ||
| expect(ConfigAgentV1.unknownKeys(raw)).toEqual(["budget", "audience"]) | ||
| }) | ||
|
|
||
| test("keys nested under options keep working and are not reported", () => { | ||
| const raw = { model: "test/model", options: { audience: "executive", reasoningEffort: "high" } } | ||
| const agent = decode(raw) | ||
| expect(agent.options).toEqual({ audience: "executive", reasoningEffort: "high" }) | ||
| expect(ConfigAgentV1.unknownKeys(raw)).toEqual([]) | ||
| }) | ||
|
|
||
| test("the known-key list is the schema's own", () => { | ||
| for (const key of ["name", "model", "variant", "prompt", "steps", "maxSteps", "options", "permission", "tools"]) { | ||
| expect(ConfigAgentV1.knownKeys).toContain(key) | ||
| } | ||
| expect(ConfigAgentV1.unknownKeys(undefined)).toEqual([]) | ||
| expect(ConfigAgentV1.unknownKeys(["budget"])).toEqual([]) | ||
| }) | ||
| }) | ||
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,103 @@ | ||
| // altimate_change start — fork-only token budget for `run` (--max-tokens) | ||
| // Sums the billed tokens of a run across its root session and every session | ||
| // descended from it (task children, and their children), and says when the | ||
| // running total reaches the limit. Pure: it observes the SDK event stream and | ||
| // never talks to the server, so the decision is testable without one. | ||
|
|
||
| /** Token counts as a `step-finish` part reports them. */ | ||
| export type StepTokens = { | ||
| input: number | ||
| output: number | ||
| cache: { read: number; write: number } | ||
| } | ||
|
|
||
| /** Billed weight of one step: a cache read costs a tenth of an input token. */ | ||
| export function billedTokens(tokens: StepTokens): number { | ||
| return tokens.input + tokens.output + tokens.cache.write + 0.1 * tokens.cache.read | ||
| } | ||
|
|
||
| /** A `--max-tokens` value the budget can enforce: a positive integer. */ | ||
| export function isValidMaxTokens(value: unknown): boolean { | ||
| return typeof value === "number" && Number.isInteger(value) && value >= 1 | ||
| } | ||
|
|
||
| type Event = { type: string; properties: Record<string, any> } | ||
|
|
||
| export function create(rootSessionID: string, limit: number | undefined) { | ||
| // Sessions known to descend from the root. A child announces itself through | ||
| // `session.created`/`session.updated` (parentID) or through the parent's | ||
| // task tool part (state.metadata.sessionId); either may arrive first, so | ||
| // edges are kept until their parent is admitted. | ||
| const sessions = new Set<string>([rootSessionID]) | ||
| const parents = new Map<string, string>() | ||
| const counted = new Set<string>() | ||
| let total = 0 | ||
| let exhausted = false | ||
|
|
||
| function admit() { | ||
| let changed = true | ||
| while (changed) { | ||
| changed = false | ||
| for (const [id, parentID] of parents) { | ||
| if (sessions.has(id) || !sessions.has(parentID)) continue | ||
| sessions.add(id) | ||
| changed = true | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function link(id: unknown, parentID: unknown) { | ||
| if (typeof id !== "string" || typeof parentID !== "string" || !id || !parentID) return | ||
| if (id === rootSessionID) return | ||
| parents.set(id, parentID) | ||
| admit() | ||
| } | ||
|
|
||
| return { | ||
| get total() { | ||
| return total | ||
| }, | ||
| get exhausted() { | ||
| return exhausted | ||
| }, | ||
| get limit() { | ||
| return limit | ||
| }, | ||
| isDescendant(sessionID: string) { | ||
| return sessions.has(sessionID) | ||
| }, | ||
| /** | ||
| * Feed one SDK event. Returns true exactly once: on the step whose usage | ||
| * takes the total to (or past) the limit. Without a limit it only counts. | ||
| */ | ||
| observe(event: Event): boolean { | ||
| const props = event.properties | ||
| if (!props) return false | ||
| if (event.type === "session.created" || event.type === "session.updated") { | ||
| link(props.info?.id, props.info?.parentID) | ||
| } | ||
| if (event.type !== "message.part.updated") return false | ||
| const part = props.part | ||
| if (!part || typeof part.sessionID !== "string") return false | ||
| if (part.type === "tool" && part.tool === "task") { | ||
| link(part.state?.metadata?.sessionId ?? part.state?.input?.task_id, part.sessionID) | ||
| } | ||
| if (part.type !== "step-finish" || !sessions.has(part.sessionID)) return false | ||
| const tokens = part.tokens as Partial<StepTokens> | undefined | ||
| if (!tokens || typeof part.id !== "string" || counted.has(part.id)) return false | ||
| counted.add(part.id) | ||
| total += billedTokens({ | ||
| input: tokens.input ?? 0, | ||
| output: tokens.output ?? 0, | ||
| cache: { read: tokens.cache?.read ?? 0, write: tokens.cache?.write ?? 0 }, | ||
| }) | ||
| if (limit === undefined || exhausted || total < limit) return false | ||
| exhausted = true | ||
| return true | ||
| }, | ||
| } | ||
| } | ||
| export type Info = ReturnType<typeof create> | ||
|
|
||
| export * as RunTokenBudget from "./run-token-budget" | ||
| // altimate_change end |
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.
P2: This known-key assertion omits several recognized fields, so it will not catch false unknown-key warnings for
temperature,top_p,mode,hidden,color, ordisable. Include the completeKNOWN_KEYSset in the assertion.Prompt for AI agents