The Open Framework for Building Agents
fix(eve): repair session token limit continuation (#1262)
Rui28417c42a24e
diff --git a/.changeset/decline-reraise-prompt.md b/.changeset/decline-reraise-prompt.mdnew file mode 100644index 000000000..8928396ff--- /dev/null+++ b/.changeset/decline-reraise-prompt.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Declining a session token-limit prompt no longer leaves a stale copy of the prompt in the parked session. Previously the cancelled turn settled with a snapshot that resurrected the already-answered prompt, so every follow-up message was queued behind it and never re-raised the prompt; follow-ups now reach the budget gate, which re-raises a fresh prompt while the session is over budget.diff --git a/.changeset/plain-runtime-limits.md b/.changeset/plain-runtime-limits.mdnew file mode 100644index 000000000..e95902d29--- /dev/null+++ b/.changeset/plain-runtime-limits.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Session token limits are now tracked as a runtime limit — an absolute lifetime-usage ceiling that each approved continuation re-anchors to `usage + configured limit` — replacing the window-baseline bookkeeping. Behavior is unchanged except the continuation prompt's `usedTokens` now reports the absolute session total instead of the window-relative amount.diff --git a/.changeset/tidy-limits-continue.md b/.changeset/tidy-limits-continue.mdnew file mode 100644index 000000000..dd5fa7e68--- /dev/null+++ b/.changeset/tidy-limits-continue.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Keep one session token-limit prompt pending while concurrent input queues behind it. Approving restores the configured budget window, delegated sessions inherit from the fresh window, and zero-quota child tasks no longer raise continuation prompts that cannot grant tokens.diff --git a/docs/agent-config.md b/docs/agent-config.mdindex d8d425e1c..20e84d7e5 100644--- a/docs/agent-config.md+++ b/docs/agent-config.md@@ -155,12 +155,14 @@ still over budget, the next message re-raises the prompt. Declining adelegated child's prompt cancels the root turn, which cascades to the wholedelegation tree — the delegating parent never receives an error result itcould retry against a fresh quota share. A reply that answers neither option-re-raises the prompt; the reply stays in history and is processed once the-budget is granted.+is queued while the existing prompt stays pending; eve does not raise another+copy. The reply is processed once the budget is granted.Sessions that cannot reach a human — task-mode runs such as schedules andsubagents without input proxying — skip the prompt and fail the next model-call with `SESSION_TOKEN_LIMIT_REACHED`.+call with `SESSION_TOKEN_LIMIT_REACHED`. A delegated task with no inherited+quota also fails instead of raising a continuation prompt that could only+grant another zero-token window.When `maxInputTokensPerSession` is omitted, root sessions apply a defaultinput budget of `40_000_000` provider-reported input tokens.@@ -169,11 +171,13 @@ to `false` uncaps that axis — the session never stops on it.Delegated subagent sessions have no fixed default. Each child receives ashare of the delegating parent's remaining quota at dispatch time — the-remainder (limit minus accumulated usage) split evenly across the batch's-local subagent calls — and a completed child's usage counts against the-parent's quota, so a delegation tree can never outspend the budget configured-at its root. An authored child limit applies only when it is tighter than the-parent's grant; an uncapped parent delegates uncapped children.+remainder in the current budget window split evenly across the batch's local+subagent calls — and a completed child's usage counts against the parent's+quota, so a delegation tree can never outspend the budget configured at its+root. Approving a continuation opens a fresh parent window for later child+grants without erasing lifetime usage. An authored child limit applies only+when it is tighter than the parent's grant; an uncapped parent delegates+uncapped children.## Workflow worlddiff --git a/e2e/fixtures/agent-session-limits/agent/tools/hold-open.ts b/e2e/fixtures/agent-session-limits/agent/tools/hold-open.tsnew file mode 100644index 000000000..4d563a886--- /dev/null+++ b/e2e/fixtures/agent-session-limits/agent/tools/hold-open.ts@@ -0,0 +1,16 @@+import { defineTool } from "eve/tools";+import { z } from "zod";++const HOLD_OPEN_MS = 4_000;++export default defineTool({+ description:+ "Test-only tool that keeps a model turn active long enough to deliver concurrent follow-up messages. Call it exactly once only when the user explicitly requests `hold-open`.",+ inputSchema: z.object({+ marker: z.string(),+ }),+ async execute({ marker }) {+ await new Promise((resolve) => setTimeout(resolve, HOLD_OPEN_MS));+ return { marker };+ },+});diff --git a/e2e/fixtures/agent-session-limits/evals/limits/session-token-limit-approve.eval.ts b/e2e/fixtures/agent-session-limits/evals/limits/session-token-limit-approve.eval.tsnew file mode 100644index 000000000..56569ba8e--- /dev/null+++ b/e2e/fixtures/agent-session-limits/evals/limits/session-token-limit-approve.eval.ts@@ -0,0 +1,108 @@+import { defineEval } from "eve/evals";+import { equals, satisfies } from "eve/evals/expect";++/**+ * Approving a session token-limit continuation over HTTP while messages queue+ * behind an active model turn.+ */+export default defineEval({+ description:+ "Messages sent during an active turn produce one limit prompt, and approve resets the budget.",+ timeoutMs: 90_000,+ async test(t) {+ const active = await t.start(+ 'Call the `hold-open` tool exactly once with marker "limit-race". Wait for its result, then reply with exactly "approved".',+ );+ await active.waitForEvent("actions.requested");++ const continuationToken = t.state.continuationToken;+ if (continuationToken === undefined) {+ throw new Error("The active session did not expose a continuation token.");+ }++ const deliverWhileActive = async (message: string): Promise<void> => {+ const response = await t.target.fetch(+ `/eve/v1/session/${encodeURIComponent(active.sessionId)}`,+ {+ body: JSON.stringify({ continuationToken, message }),+ headers: { "content-type": "application/json" },+ method: "POST",+ },+ );+ if (!response.ok) {+ throw new Error(`Concurrent delivery failed (${String(response.status)}).`);+ }+ };++ await deliverWhileActive('Queued message A: preserve the original reply "approved".');+ await deliverWhileActive('Queued message B: preserve the original reply "approved".');++ const prompted = await active.result();+ prompted.event("input.requested", { count: 1 });+ const request = t.requireInputRequest({+ display: "confirmation",+ optionIds: ["continue", "stop"],+ toolName: "session_limit_continuation",+ });++ const queuedSession = t.target.watchTurn(active.sessionId, {+ startIndex: t.state.streamIndex,+ });+ const queuedA = await queuedSession.result();+ queuedA.event("message.received", { count: 1 });+ queuedA.notEvent("input.requested");+ queuedA.eventsSatisfy("delivers message A while preserving the pending prompt", (events) => {+ const received = events.find((event) => event.type === "message.received");+ return received !== undefined && received.data.message.includes("Queued message A");+ });+ const queuedBSession = t.target.watchTurn(active.sessionId, {+ startIndex: queuedSession.session.state.streamIndex,+ });+ const queuedB = await queuedBSession.result();+ queuedB.event("message.received", { count: 1 });+ queuedB.notEvent("input.requested");+ queuedB.eventsSatisfy("delivers message B while preserving the pending prompt", (events) => {+ const received = events.find((event) => event.type === "message.received");+ return received !== undefined && received.data.message.includes("Queued message B");+ });+ t.check(+ [...prompted.events, ...queuedA.events, ...queuedB.events].filter(+ (event) => event.type === "input.requested",+ ).length,+ equals(1),+ );++ const resumed = await queuedBSession.session.respond({+ optionId: "continue",+ requestId: request.requestId,+ });+ resumed.expectOk();+ resumed.event("step.started");+ resumed.notEvent("input.requested");+ resumed.messageIncludes("approved");+ t.check(resumed.sessionId, equals(active.sessionId));+ t.check(queuedBSession.session.state.sessionId, equals(active.sessionId));++ const nextPrompt = await queuedBSession.session.send(+ 'Post-approval probe: reply with exactly "same session".',+ );+ nextPrompt.event("input.requested", { count: 1 });+ nextPrompt.notEvent("message.completed");+ t.check(nextPrompt.sessionId, equals(active.sessionId));+ t.check(nextPrompt.status, equals("waiting"));+ t.check(queuedBSession.session.state.sessionId, equals(active.sessionId));++ const nextRequest = queuedBSession.session.requireInputRequest({+ display: "confirmation",+ optionIds: ["continue", "stop"],+ toolName: "session_limit_continuation",+ });+ t.check(+ nextRequest.requestId,+ satisfies(+ (requestId: string) => requestId !== request.requestId,+ "the next token window creates a distinct continuation request",+ ),+ );+ },+});diff --git a/e2e/fixtures/agent-session-limits/package.json b/e2e/fixtures/agent-session-limits/package.jsonindex d0b263535..0ad51e2e9 100644--- a/e2e/fixtures/agent-session-limits/package.json+++ b/e2e/fixtures/agent-session-limits/package.json@@ -14,7 +14,8 @@"@opentelemetry/core": "catalog:","@opentelemetry/sdk-trace-base": "catalog:","@vercel/otel": "catalog:",- "eve": "workspace:*"+ "eve": "workspace:*",+ "zod": "catalog:"},"devDependencies": {"@types/node": "catalog:",diff --git a/packages/eve/src/execution/session-limit-cancellation.integration.test.ts b/packages/eve/src/execution/session-limit-cancellation.integration.test.tsindex ecc076404..b829a427f 100644--- a/packages/eve/src/execution/session-limit-cancellation.integration.test.ts+++ b/packages/eve/src/execution/session-limit-cancellation.integration.test.ts@@ -1,5 +1,5 @@import { describe, expect, it } from "vitest";-import { getWorld, resumeHook, start } from "#internal/workflow/runtime.js";+import { resumeHook, start } from "#internal/workflow/runtime.js";import { createTestRuntime } from "#internal/testing/app-harness.js";import {@@ -8,7 +8,6 @@ import {filterEventsByType,} from "#internal/testing/events.js";import { createBundledRuntimeCompiledArtifactsSource } from "#runtime/compiled-artifacts-source.js";-import { sessionCancelHookToken } from "#execution/turn-cancellation-token.js";import { workflowEntry } from "#execution/workflow-entry.js";import type { UnstampedMessageStreamEvent } from "#protocol/message.js";@@ -67,37 +66,6 @@ async function deliver(}}-/** Polls the world for a hook row by token (hooks are per-run; the token is global). */-async function waitForHookByToken(token: string, timeout = 15_000): Promise<{ runId: string }> {- const world = await getWorld();- const deadline = Date.now() + timeout;- while (Date.now() < deadline) {- try {- const hook = await world.hooks.getByToken(token);- if (hook !== null && hook !== undefined) {- return hook;- }- } catch {- // Not registered yet.- }- await new Promise((resolve) => setTimeout(resolve, 50));- }- throw new Error(`Timed out waiting for hook token "${token}".`);-}--/** Polls the world until the given run reaches `completed`. */-async function waitForRunCompletion(runId: string, timeout = 15_000): Promise<void> {- const world = await getWorld();- const deadline = Date.now() + timeout;- while (Date.now() < deadline) {- const page = await world.runs.list({ pagination: { limit: 100 } });- const row = page.data.find((entry: { runId?: string }) => entry.runId === runId);- if (row?.status === "completed") return;- await new Promise((resolve) => setTimeout(resolve, 50));- }- throw new Error(`Timed out waiting for run "${runId}" to complete.`);-}-function requestIdFromPromptTurn(events: readonly UnstampedMessageStreamEvent[]): string {const requested = filterEventsByType(events, "input.requested");expect(requested).toHaveLength(1);@@ -182,7 +150,7 @@ describe("session-limit continuation decline integration", () => {});}, 60_000);- it("cancels the root turn when the user declines a delegated child's prompt", async () => {+ it("fails a zero-quota delegation fast and declines the root's own prompt", async () => {const runtime = createTestRuntime({agent: { limits: { maxInputTokensPerSession: 1 }, name: "limit-decline-child" },});@@ -205,30 +173,38 @@ describe("session-limit continuation decline integration", () => {const stream = captureTurnEvents(run);try {- // The root's first model call spends the budget, so the delegated- // child inherits a zero remainder and parks on its continuation- // prompt before any model call. The proxy epilogue streams this- // turn's waiting boundary while the root keeps waiting on the child.+ // The root's first model call spends the whole budget, so the+ // delegated child inherits a zero remainder and fails fast --+ // approving a zero-token window could never grant tokens, so no+ // child prompt is raised (`violation.limit > 0` in+ // `enforceSessionTokenLimit`). The root receives the child's error+ // result, reaches its own pre-model gate, and parks on its OWN+ // continuation prompt.const hitlTurn = await stream.nextTurn();expect(hitlTurn.at(-1)?.type).toBe("session.waiting");expect(filterEventsByType(hitlTurn, "subagent.called")).toHaveLength(1);+ expect(filterEventsByType(hitlTurn, "input.requested")).toHaveLength(1);const requestId = requestIdFromPromptTurn(hitlTurn);+ // The prompt belongs to the root, not the delegated child.+ expect(requestId.startsWith(`${run.runId}:limit:`)).toBe(true);- const cancelHook = await waitForHookByToken(sessionCancelHookToken(run.runId));-- // Declining the child's prompt cancels the root turn. The waiting- // boundary is already on the stream, so settling emits nothing new;- // the turn run completing is the settle barrier.+ // Declining the root's own prompt is a user action the stream must+ // show: the turn settles as cancelled and the session stays+ // resumable -- same contract as the direct-decline case above.await deliver(continuationToken, {kind: "deliver",payloads: [{ inputResponses: [{ optionId: "stop", requestId }] }],});- await waitForRunCompletion(cancelHook.runId);+ const declinedTurn = await stream.nextTurn();+ expect(declinedTurn.at(-1)?.type).toBe("session.waiting");+ expect(filterEventsByType(declinedTurn, "turn.cancelled")).toHaveLength(1);+ expect(filterEventsByType(declinedTurn, "session.completed")).toHaveLength(0);+ expect(filterEventsByType(declinedTurn, "subagent.called")).toHaveLength(0);+ expectNoFailureEvents(declinedTurn);- // The session accepts the next message; the root itself is over- // budget, so it re-raises its own prompt. Anything from the old- // decline path — a parent-visible subagent error, a retry- // re-dispatch, a model reply — would surface here instead.+ // The session accepts the next message; the root is still over+ // budget, so it re-raises its own prompt (fail-closed) instead of+ // running a model call or re-dispatching the delegation.await deliver(continuationToken, {kind: "deliver",payloads: [{ message: "follow up after decline" }],@@ -240,6 +216,7 @@ describe("session-limit continuation decline integration", () => {expect(filterEventsByType(followUpTurn, "subagent.called")).toHaveLength(0);expect(filterEventsByType(followUpTurn, "message.completed")).toHaveLength(0);expect(filterEventsByType(followUpTurn, "session.completed")).toHaveLength(0);+ expect(filterEventsByType(followUpTurn, "turn.cancelled")).toHaveLength(0);expectNoFailureEvents(followUpTurn);} finally {stream.dispose();diff --git a/packages/eve/src/execution/settle-cancelled-turn-step.ts b/packages/eve/src/execution/settle-cancelled-turn-step.tsindex 131a3ff44..9465e6591 100644--- a/packages/eve/src/execution/settle-cancelled-turn-step.ts+++ b/packages/eve/src/execution/settle-cancelled-turn-step.ts@@ -13,6 +13,7 @@ import { hydrateDurableSession } from "#execution/session.js";import { reconcileSessionContinuationToken } from "#execution/reconcile-session-continuation-token.js";import { activeTurnId } from "#harness/active-turn-id.js";import { emitCancelledTurn } from "#harness/cancelled-turn-emission.js";+import { clearPendingSessionLimitPrompt } from "#harness/input-requests.js";import {getHarnessEmissionState,isHarnessBetweenTurns,@@ -110,11 +111,19 @@ export async function settleCancelledTurnStep(input: {}}+ // `clearPendingSessionLimitPrompt`: cancellation settles with the step's+ // input snapshot, which can resurrect an already-answered session-limit+ // prompt (the decline that cancelled this turn consumed the answer in the+ // discarded turn state). The pre-model gate re-raises the prompt while the+ // violation holds, so the next delivery gets a fresh prompt instead of+ // queueing forever behind a stale one.const cancelledSession = reconcileSessionContinuationToken(ctx,setHarnessEmissionState(- clearAllProxyInputRequests(- clearPendingWorkflowInterrupt(clearPendingRuntimeActionBatch(session)),+ clearPendingSessionLimitPrompt(+ clearAllProxyInputRequests(+ clearPendingWorkflowInterrupt(clearPendingRuntimeActionBatch(session)),+ ),),emissionState,),diff --git a/packages/eve/src/harness/input-request-class.test.ts b/packages/eve/src/harness/input-request-class.test.tsnew file mode 100644index 000000000..5b9e1094a--- /dev/null+++ b/packages/eve/src/harness/input-request-class.test.ts@@ -0,0 +1,97 @@+import { describe, expect, it } from "vitest";++import { classifyInputRequest, isApprovalRequest } from "#harness/input-request-class.js";+import { createSessionLimitContinuationRequest } from "#harness/session-limit-continuation.js";+describe("classifyInputRequest", () => {+ const action = {+ callId: "call_1",+ input: {},+ kind: "tool-call",+ toolName: "ask_user",+ } as const;++ it("requires an explicit answer for tool approvals", () => {+ expect(+ classifyInputRequest({+ action,+ options: [+ { id: "approve", label: "Approve" },+ { id: "deny", label: "Deny" },+ ],+ prompt: "Run the tool?",+ requestId: "call_1",+ }),+ ).toBe("required");+ });++ it("requires an explicit answer for session-limit continuations", () => {+ expect(+ classifyInputRequest(+ createSessionLimitContinuationRequest({+ sessionId: "sess-test",+ violation: { kind: "input", limit: 12, usedTokens: 12 },+ }),+ ),+ ).toBe("required");+ });++ it("classifies model questions as dismissable", () => {+ expect(+ classifyInputRequest({+ action,+ allowFreeform: true,+ options: [{ id: "blue", label: "Blue" }],+ prompt: "Which color?",+ requestId: "call_1",+ }),+ ).toBe("dismissable");+ });++ it("does not mistake non-approval two-option requests for approvals", () => {+ expect(+ classifyInputRequest({+ action,+ options: [+ { id: "blue", label: "Blue" },+ { id: "red", label: "Red" },+ ],+ prompt: "Which color?",+ requestId: "call_1",+ }),+ ).toBe("dismissable");+ });+});++describe("isApprovalRequest", () => {+ const action = {+ callId: "call_1",+ input: {},+ kind: "tool-call",+ toolName: "bash",+ } as const;++ it("matches exactly the approve/deny option pair, in order", () => {+ const options = (ids: readonly string[]) => ids.map((id) => ({ id, label: id }));++ expect(+ isApprovalRequest({+ action,+ options: options(["approve", "deny"]),+ prompt: "?",+ requestId: "r",+ }),+ ).toBe(true);+ expect(+ isApprovalRequest({+ action,+ options: options(["deny", "approve"]),+ prompt: "?",+ requestId: "r",+ }),+ ).toBe(false);+ expect(+ isApprovalRequest({ action, options: options(["approve"]), prompt: "?", requestId: "r" }),+ ).toBe(false);+ expect(isApprovalRequest({ action, prompt: "?", requestId: "r" })).toBe(false);+ });+});diff --git a/packages/eve/src/harness/input-request-class.ts b/packages/eve/src/harness/input-request-class.tsnew file mode 100644index 000000000..ad834c858--- /dev/null+++ b/packages/eve/src/harness/input-request-class.ts@@ -0,0 +1,51 @@+/**+ * Shape and behavior classification for pending input requests, shared by+ * the harness resolution path and protocol clients (e.g. the dev TUI's+ * rendering decisions).+ */+import type { InputRequest } from "#runtime/input/types.js";+import { isSessionLimitContinuationRequest } from "#harness/session-limit-continuation.js";++/** Shared approval predicate: a request whose options are exactly `approve` / `deny`. */+export function isApprovalRequest(request: InputRequest): boolean {+ return (+ request.options?.length === 2 &&+ request.options[0]?.id === "approve" &&+ request.options[1]?.id === "deny"+ );+}++/**+ * Behavioral class of a pending input request.+ *+ * `"required"` — the request must be explicitly answered before the turn can+ * move on. While one is unanswered, any other arriving input is queued and+ * replayed on the step after the answer. The pending batch never churns, so+ * the request ids the user is answering stay valid.+ *+ * `"dismissable"` — a plain follow-up message counts as the user moving on.+ * The request resolves as a real `tool-result` with `status: "ignored"` and+ * the model continues with the message in the same step.+ */+export type InputRequestClass = "dismissable" | "required";++/** Classifies one pending request; see {@link InputRequestClass}. */+export function classifyInputRequest(request: InputRequest): InputRequestClass {+ // A tool approval gates a model-emitted tool call. AI SDK requires the+ // approval response to resolve in isolation, and skipping it would leave+ // the intercepted call permanently un-adjudicated.+ if (isApprovalRequest(request)) {+ return "required";+ }+ // A session-limit continuation guards the next model call itself:+ // "ignore and continue" cannot continue -- the pre-model gate re-parks+ // while the violation holds -- and, being harness-authored, the request+ // has no history anchor on which an ignored outcome could be recorded.+ if (isSessionLimitContinuationRequest(request)) {+ return "required";+ }+ // Everything else wraps a model-emitted tool call whose ignored outcome+ // is expressible as a tool-result, so dismissal is always representable.+ // There is no third class.+ return "dismissable";+}diff --git a/packages/eve/src/harness/input-requests.test.ts b/packages/eve/src/harness/input-requests.test.tsindex 65fc4586c..2ed85801d 100644--- a/packages/eve/src/harness/input-requests.test.ts+++ b/packages/eve/src/harness/input-requests.test.ts@@ -7,6 +7,7 @@ import { once } from "#public/tools/approval/approval-helpers.js";import type { InputRequest } from "#runtime/input/types.js";import type { HarnessToolDefinition } from "#harness/execute-tool.js";import {+ clearPendingSessionLimitPrompt,consumeDeferredStepInput,createRuntimeToolCallActionFromToolCall,getApprovedTools,@@ -872,7 +873,6 @@ describe("resolvePendingInput with a session-limit continuation batch", () => {requests: [createSessionLimitContinuationRequest({sessionId: "sess-test",- totalUsedTokens: 12,violation: { kind: "input", limit: 12, usedTokens: 12 },}),],@@ -909,14 +909,78 @@ describe("resolvePendingInput with a session-limit continuation batch", () => {expect(result.messages).toEqual([{ content: "previous", role: "user" }]);});- it("treats a plain follow-up message as ignoring the prompt", () => {+ it("keeps the prompt pending and queues a plain follow-up message", () => {const result = resolvePendingInput({session: createLimitBatchSession(),stepInput: { message: "also do this other thing" },});- expect(result.outcome).toBe("resolved");+ expect(result.outcome).toBe("unresolved");expect(result.limitContinuation).toBeUndefined();expect(result.messages).toEqual([{ content: "previous", role: "user" }]);+ expect(hasDeferredStepInput(result.session)).toBe(true);++ const deferred = consumeDeferredStepInput({ session: result.session });+ expect(deferred.input).toEqual({ message: "also do this other thing" });+ });+});++describe("clearPendingSessionLimitPrompt", () => {+ it("drops a pending batch made only of session-limit continuation prompts", () => {+ const session = setPendingInputBatch({+ requests: [+ createSessionLimitContinuationRequest({+ sessionId: "sess-test",+ violation: { kind: "input", limit: 12, usedTokens: 12 },+ }),+ ],+ responseMessages: [],+ session: createHarnessSession(),+ });++ const cleared = clearPendingSessionLimitPrompt(session);+ const result = resolvePendingInput({ session: cleared, stepInput: { message: "try again" } });++ // No stale batch left: the follow-up message flows to the step (where+ // the pre-model gate re-raises the prompt) instead of deferring forever.+ expect(result.outcome).toBe("continue");+ expect(hasDeferredStepInput(cleared)).toBe(false);+ });++ it("keeps model-anchored batches (tool approvals) intact", () => {+ const session = setPendingInputBatch({+ requests: [+ {+ action: {+ callId: "approval-call",+ input: { command: "rm -rf /tmp/demo" },+ kind: "tool-call",+ toolName: "bash",+ },+ allowFreeform: false,+ display: "confirmation",+ options: [+ { id: "approve", label: "Yes" },+ { id: "deny", label: "No" },+ ],+ prompt: "Approve tool call: bash",+ requestId: "approval-1",+ },+ ],+ responseMessages: [],+ session: createHarnessSession(),+ });++ const kept = clearPendingSessionLimitPrompt(session);+ const result = resolvePendingInput({ session: kept, stepInput: { message: "and then this" } });++ // The approval still gates the turn: the follow-up defers behind it.+ expect(result.outcome).toBe("unresolved");+ expect(hasDeferredStepInput(result.session)).toBe(true);+ });++ it("is a no-op without a pending batch", () => {+ const session = createHarnessSession();+ expect(clearPendingSessionLimitPrompt(session)).toBe(session);});});diff --git a/packages/eve/src/harness/input-requests.ts b/packages/eve/src/harness/input-requests.tsindex 7d412e222..d9a61541b 100644--- a/packages/eve/src/harness/input-requests.ts+++ b/packages/eve/src/harness/input-requests.ts@@ -6,6 +6,7 @@ import type {} from "#runtime/actions/types.js";import type { InputRequest, InputResponse } from "#runtime/input/types.js";import { resolveTextToResponses } from "#channel/resolve-text.js";+import { classifyInputRequest, isApprovalRequest } from "#harness/input-request-class.js";import { coalesceTurnInputs } from "#harness/messages.js";import { resolveToolCallInputObject } from "#harness/runtime-actions.js";import {@@ -121,13 +122,13 @@ export function hasDeferredStepInput(session: HarnessSession): boolean {/*** Resolves pending input at the start of a harness step.*- * When the pending batch contains tool-approval requests and the step input- * also carries a follow-up user message, the message is deferred to the next- * internal harness step rather than appended to the current turn. This is- * necessary because AI SDK cannot process tool-approval responses and a new- * user message in the same request -- the approval must be resolved in- * isolation first, and the user message replayed on the subsequent step via- * {@link consumeDeferredStepInput}.+ * Each pending request is either `"required"` or `"dismissable"` — see+ * {@link classifyInputRequest}. While a required request is unanswered,+ * follow-up input is deferred instead of dismissing and recreating the+ * request; {@link consumeDeferredStepInput} replays it on the subsequent+ * step. Tool approval responses additionally resolve in isolation because+ * AI SDK cannot process an approval response and a new user message in the+ * same request.*/export function resolvePendingInput(input: {readonly history?: readonly ModelMessage[];@@ -155,15 +156,15 @@ export function resolvePendingInput(input: {return { outcome: "unresolved", messages: baseHistory, session };}- if (resolvesApprovalBatch && hasUnansweredApproval({ pendingBatch, responses })) {+ if (hasUnansweredRequiredRequest({ pendingBatch, responses })) {session = queueDeferredStepInput(session, compactStepInput(resolvedStepInput));return { deferredMessage: true, outcome: "unresolved", messages: baseHistory, session };}if (responses.length === 0 && resolvedStepInput?.message !== undefined) {- // A follow-up message arrived for question-only input with no explicit- // responses. Keep the existing question semantics: mark unanswered- // question requests ignored so the model can continue with the message.+ // A follow-up message arrived and every pending request is dismissable+ // (a required one would have deferred above): mark the unanswered+ // requests ignored so the model can continue with the message.const toolParts = buildToolResponseParts(pendingBatch, []);const messages: ModelMessage[] = [...baseHistory, ...pendingBatch.responseMessages];if (toolParts.length > 0) {@@ -302,13 +303,37 @@ function compactStepInput(return result;}-function hasUnansweredApproval(input: {+/**+ * Drops a pending session-limit continuation prompt from a parked session.+ *+ * A cancelled turn settles with the step's input snapshot, which can+ * resurrect a continuation prompt the user already answered — the decline+ * that cancelled the turn consumed the answer inside the discarded turn+ * state. Left in place, the stale prompt would queue every follow-up+ * message behind an answer that will never come. Harness-authored prompts+ * are deterministically re-raised by the pre-model gate, so dropping is+ * always safe; model-anchored batches (tool approvals, questions) are kept+ * because their tool calls still require resolution.+ */+export function clearPendingSessionLimitPrompt(session: HarnessSession): HarnessSession {+ const pendingBatch = getPendingInputBatch(session.state);+ if (pendingBatch === undefined || pendingBatch.requests.length === 0) {+ return session;+ }+ if (!pendingBatch.requests.every((request) => isSessionLimitContinuationRequest(request))) {+ return session;+ }+ return clearPendingInputBatch(session);+}++function hasUnansweredRequiredRequest(input: {readonly pendingBatch: PendingInputBatch;readonly responses: readonly InputResponse[];}): boolean {const responseIds = new Set(input.responses.map((response) => response.requestId));return input.pendingBatch.requests.some(- (request) => isApprovalRequest(request) && !responseIds.has(request.requestId),+ (request) =>+ classifyInputRequest(request) === "required" && !responseIds.has(request.requestId),);}@@ -643,15 +668,6 @@ function buildToolResponsePartsForRequest(];}-/** Shared approval predicate: a request whose options are exactly `approve` / `deny`. */-export function isApprovalRequest(request: InputRequest): boolean {- return (- request.options?.length === 2 &&- request.options[0]?.id === "approve" &&- request.options[1]?.id === "deny"- );-}-// ---------------------------------------------------------------------------// Tool call helpers// ---------------------------------------------------------------------------diff --git a/packages/eve/src/harness/session-limit-continuation.test.ts b/packages/eve/src/harness/session-limit-continuation.test.tsindex 40f97627c..1d62988d5 100644--- a/packages/eve/src/harness/session-limit-continuation.test.ts+++ b/packages/eve/src/harness/session-limit-continuation.test.ts@@ -12,7 +12,6 @@ const VIOLATION = { kind: "input", limit: 40_000_000, usedTokens: 40_120_500 } afunction createTestRequest() {return createSessionLimitContinuationRequest({sessionId: "sess-test",- totalUsedTokens: 40_120_500,violation: VIOLATION,});}@@ -58,7 +57,6 @@ describe("createSessionLimitContinuationRequest", () => {const promptFor = (limit: number): string =>createSessionLimitContinuationRequest({sessionId: "sess-test",- totalUsedTokens: limit + 1,violation: { kind: "input", limit, usedTokens: limit + 1 },}).prompt;@@ -74,8 +72,7 @@ describe("createSessionLimitContinuationRequest", () => {// response to an earlier prompt never resolves a later one.const later = createSessionLimitContinuationRequest({sessionId: "sess-test",- totalUsedTokens: 80_500_000,- violation: VIOLATION,+ violation: { ...VIOLATION, usedTokens: 80_500_000 },});expect(later.requestId).not.toBe(createTestRequest().requestId);diff --git a/packages/eve/src/harness/session-limit-continuation.ts b/packages/eve/src/harness/session-limit-continuation.tsindex 904a64663..f0865c1a5 100644--- a/packages/eve/src/harness/session-limit-continuation.ts+++ b/packages/eve/src/harness/session-limit-continuation.ts@@ -25,21 +25,16 @@ export const SESSION_LIMIT_STOP_OPTION_ID = "stop";*/export function createSessionLimitContinuationRequest(input: {readonly sessionId: string;- /**- * Absolute session total of the violated kind (not the window-relative- * `violation.usedTokens`, which can repeat across grants — e.g. a second- * violation can land on exactly the same window usage). The absolute total- * is strictly increasing across violations, so each prompt gets its own id- * while staying deterministic: stale chat controls from an earlier,- * already-resolved prompt carry an unknown requestId, resolve nothing, and- * the harness harmlessly re-prompts instead of letting an old "Stop"- * button end a freshly granted session.- */- readonly totalUsedTokens: number;readonly violation: SessionTokenLimitViolation;}): InputRequest {- const { sessionId, totalUsedTokens, violation } = input;- const requestId = `${sessionId}:limit:${violation.kind}:${String(totalUsedTokens)}`;+ const { sessionId, violation } = input;+ // `usedTokens` is the absolute session total, which is strictly increasing+ // across violations, so each prompt gets its own id while staying+ // deterministic: stale chat controls from an earlier, already-resolved+ // prompt carry an unknown requestId, resolve nothing, and the harness+ // harmlessly re-prompts instead of letting an old "Stop" button end a+ // freshly granted session.+ const requestId = `${sessionId}:limit:${violation.kind}:${String(violation.usedTokens)}`;return {action: {diff --git a/packages/eve/src/harness/session-limit-enforcement.ts b/packages/eve/src/harness/session-limit-enforcement.tsindex 049f56799..a3c45d49c 100644--- a/packages/eve/src/harness/session-limit-enforcement.ts+++ b/packages/eve/src/harness/session-limit-enforcement.ts@@ -24,7 +24,7 @@ import { setPendingInputBatch } from "#harness/input-requests.js";import { createSessionLimitContinuationRequest } from "#harness/session-limit-continuation.js";import { SessionLimitDeclinedError } from "#harness/turn-cancellation.js";import {- extendSessionTokenBudget,+ bumpSessionRuntimeTokenLimits,getSessionTokenLimitViolation,getSessionTokenUsage,type SessionTokenLimitViolation,@@ -43,8 +43,9 @@ interface SessionLimitPolicyInput {/*** Acts on a resolved session-limit continuation answer.*- * Granted: resets the token budget windows via- * {@link extendSessionTokenBudget} and lets the step continue transparently.+ * Granted: bumps the runtime token limits via+ * {@link bumpSessionRuntimeTokenLimits} and lets the step continue+ * transparently.* Declined: a user decision, not an error — the decline cancels the* in-flight turn tree through the standard cancellation path, settling as* `turn.cancelled` → `session.waiting` with no failure surfaced anywhere.@@ -66,7 +67,7 @@ export async function applySessionLimitContinuation(}if (input.limitContinuation.granted) {- return { result: null, session: extendSessionTokenBudget(input.session) };+ return { result: null, session: bumpSessionRuntimeTokenLimits(input.session) };}// A session parked on the continuation prompt always satisfies the@@ -108,7 +109,11 @@ export async function enforceSessionTokenLimit(}const { emit } = input;+ // A zero limit is an exhausted quota inherited by a delegated task.+ // Approving would bump the runtime limit by the configured limit -- zero --+ // so fail the child and let its parent reach the resumable limit gate.if (+ violation.limit > 0 &&emit !== undefined &&(input.config.mode === "conversation" || input.config.capabilities?.requestInput === true)) {@@ -132,10 +137,8 @@ async function parkOnSessionTokenLimit(input: {readonly session: HarnessSession;readonly violation: SessionTokenLimitViolation;}): Promise<StepResult> {- const usage = getSessionTokenUsage(input.session);const request = createSessionLimitContinuationRequest({sessionId: input.session.sessionId,- totalUsedTokens: input.violation.kind === "input" ? usage.inputTokens : usage.outputTokens,violation: input.violation,});let emissionState = input.emissionState;diff --git a/packages/eve/src/harness/session-token-limits.test.ts b/packages/eve/src/harness/session-token-limits.test.tsnew file mode 100644index 000000000..02b6ccb2b--- /dev/null+++ b/packages/eve/src/harness/session-token-limits.test.ts@@ -0,0 +1,105 @@+import { describe, expect, it } from "vitest";++import {+ findRuntimeTokenLimitViolation,+ grantRuntimeTokenLimits,+ remainingRuntimeTokenQuota,+ resolveRuntimeTokenLimits,+} from "#harness/session-token-limits.js";++const CONFIGURED = { inputTokens: 100, outputTokens: 50 };++describe("session token-limit transitions", () => {+ it.each([+ { configured: {}, expected: {}, stored: undefined },+ { configured: CONFIGURED, expected: CONFIGURED, stored: undefined },+ {+ configured: CONFIGURED,+ expected: { inputTokens: 250, outputTokens: 50 },+ stored: { inputTokens: 250 },+ },+ ])("resolves configured and granted ceilings", ({ configured, expected, stored }) => {+ expect(resolveRuntimeTokenLimits({ configured, stored })).toEqual(expected);+ });++ it.each([+ {+ configured: CONFIGURED,+ expected: { inputTokens: 100, outputTokens: 50 },+ usage: { inputTokens: 0, outputTokens: 0 },+ },+ {+ configured: CONFIGURED,+ expected: { inputTokens: 450, outputTokens: 170 },+ usage: { inputTokens: 350, outputTokens: 120 },+ },+ {+ configured: { inputTokens: 100 },+ expected: { inputTokens: 140 },+ usage: { inputTokens: 40, outputTokens: 999 },+ },+ ])("grants one full window from current usage", ({ configured, expected, usage }) => {+ expect(grantRuntimeTokenLimits({ configured, usage })).toEqual(expected);+ });++ it.each([+ {+ expected: { inputTokens: false, outputTokens: false },+ runtime: {},+ usage: { inputTokens: 1_000, outputTokens: 1_000 },+ },+ {+ expected: { inputTokens: 60, outputTokens: 40 },+ runtime: CONFIGURED,+ usage: { inputTokens: 40, outputTokens: 10 },+ },+ {+ expected: { inputTokens: 0, outputTokens: 0 },+ runtime: CONFIGURED,+ usage: { inputTokens: 150, outputTokens: 80 },+ },+ ])("computes remaining quota without negative values", ({ expected, runtime, usage }) => {+ expect(remainingRuntimeTokenQuota({ runtime, usage })).toEqual(expected);+ });++ it.each([+ {+ configured: CONFIGURED,+ expected: null,+ runtime: CONFIGURED,+ usage: { inputTokens: 99, outputTokens: 49 },+ },+ {+ configured: CONFIGURED,+ expected: { kind: "input", limit: 100, usedTokens: 100 },+ runtime: CONFIGURED,+ usage: { inputTokens: 100, outputTokens: 50 },+ },+ {+ configured: CONFIGURED,+ expected: { kind: "output", limit: 50, usedTokens: 50 },+ runtime: CONFIGURED,+ usage: { inputTokens: 0, outputTokens: 50 },+ },+ {+ configured: { inputTokens: 0 },+ expected: { kind: "input", limit: 0, usedTokens: 0 },+ runtime: { inputTokens: 0 },+ usage: { inputTokens: 0, outputTokens: 0 },+ },+ {+ configured: CONFIGURED,+ expected: { kind: "input", limit: 100, usedTokens: 200 },+ runtime: { inputTokens: 200, outputTokens: 100 },+ usage: { inputTokens: 200, outputTokens: 0 },+ },+ ])("finds the first exhausted axis", ({ configured, expected, runtime, usage }) => {+ expect(+ findRuntimeTokenLimitViolation({+ configured,+ runtime,+ usage,+ }),+ ).toEqual(expected);+ });+});diff --git a/packages/eve/src/harness/session-token-limits.ts b/packages/eve/src/harness/session-token-limits.tsnew file mode 100644index 000000000..91afae671--- /dev/null+++ b/packages/eve/src/harness/session-token-limits.ts@@ -0,0 +1,104 @@+export interface SessionTokenAmounts {+ readonly inputTokens: number;+ readonly outputTokens: number;+}++export interface SessionRuntimeTokenLimits {+ readonly inputTokens?: number;+ readonly outputTokens?: number;+}++export type SessionTokenLimitViolation =+ | {+ readonly kind: "input";+ readonly limit: number;+ readonly usedTokens: number;+ }+ | {+ readonly kind: "output";+ readonly limit: number;+ readonly usedTokens: number;+ };++/** Resolves per-axis lifetime ceilings, preferring a previously granted ceiling. */+export function resolveRuntimeTokenLimits(input: {+ readonly configured: SessionRuntimeTokenLimits;+ readonly stored?: SessionRuntimeTokenLimits;+}): SessionRuntimeTokenLimits {+ const limits: { inputTokens?: number; outputTokens?: number } = {};+ const inputTokens = input.stored?.inputTokens ?? input.configured.inputTokens;+ if (inputTokens !== undefined) {+ limits.inputTokens = inputTokens;+ }+ const outputTokens = input.stored?.outputTokens ?? input.configured.outputTokens;+ if (outputTokens !== undefined) {+ limits.outputTokens = outputTokens;+ }+ return limits;+}++/** Grants one full configured window from the current lifetime usage. */+export function grantRuntimeTokenLimits(input: {+ readonly configured: SessionRuntimeTokenLimits;+ readonly usage: SessionTokenAmounts;+}): SessionRuntimeTokenLimits {+ const limits: { inputTokens?: number; outputTokens?: number } = {};+ if (input.configured.inputTokens !== undefined) {+ limits.inputTokens = input.usage.inputTokens + input.configured.inputTokens;+ }+ if (input.configured.outputTokens !== undefined) {+ limits.outputTokens = input.usage.outputTokens + input.configured.outputTokens;+ }+ return limits;+}++/** Returns the unspent quota for each capped axis. */+export function remainingRuntimeTokenQuota(input: {+ readonly runtime: SessionRuntimeTokenLimits;+ readonly usage: SessionTokenAmounts;+}): {+ readonly inputTokens: number | false;+ readonly outputTokens: number | false;+} {+ return {+ inputTokens:+ input.runtime.inputTokens === undefined+ ? false+ : Math.max(0, input.runtime.inputTokens - input.usage.inputTokens),+ outputTokens:+ input.runtime.outputTokens === undefined+ ? false+ : Math.max(0, input.runtime.outputTokens - input.usage.outputTokens),+ };+}++/** Returns the first exhausted axis, with input taking precedence. */+export function findRuntimeTokenLimitViolation(input: {+ readonly configured: SessionRuntimeTokenLimits;+ readonly runtime: SessionRuntimeTokenLimits;+ readonly usage: SessionTokenAmounts;+}): SessionTokenLimitViolation | null {+ if (+ input.runtime.inputTokens !== undefined &&+ input.configured.inputTokens !== undefined &&+ input.usage.inputTokens >= input.runtime.inputTokens+ ) {+ return {+ kind: "input",+ limit: input.configured.inputTokens,+ usedTokens: input.usage.inputTokens,+ };+ }+ if (+ input.runtime.outputTokens !== undefined &&+ input.configured.outputTokens !== undefined &&+ input.usage.outputTokens >= input.runtime.outputTokens+ ) {+ return {+ kind: "output",+ limit: input.configured.outputTokens,+ usedTokens: input.usage.outputTokens,+ };+ }+ return null;+}diff --git a/packages/eve/src/harness/stale-input-responses.ts b/packages/eve/src/harness/stale-input-responses.tsindex 0bc351fa0..8700703b6 100644--- a/packages/eve/src/harness/stale-input-responses.ts+++ b/packages/eve/src/harness/stale-input-responses.ts@@ -1,7 +1,7 @@import type { ModelMessage, UserContent } from "ai";import { extractHistoricalInputRequests } from "#harness/input-extraction.js";-import { isApprovalRequest } from "#harness/input-requests.js";+import { isApprovalRequest } from "#harness/input-request-class.js";import { appendUserContent, normalizeUserContent } from "#harness/messages.js";import { isSessionLimitContinuationRequestId } from "#harness/session-limit-continuation.js";import type { StepInput } from "#harness/types.js";diff --git a/packages/eve/src/harness/subagent-token-budget.test.ts b/packages/eve/src/harness/subagent-token-budget.test.tsindex 8a3a5eb9a..1a3fb7543 100644--- a/packages/eve/src/harness/subagent-token-budget.test.ts+++ b/packages/eve/src/harness/subagent-token-budget.test.ts@@ -1,7 +1,7 @@import { describe, expect, it } from "vitest";import { resolveRemainingSessionTokenLimits } from "#harness/subagent-token-budget.js";-import { setTurnUsageState } from "#harness/turn-tag-state.js";+import { bumpSessionRuntimeTokenLimits, setTurnUsageState } from "#harness/turn-tag-state.js";import type { HarnessSession, SessionLimits } from "#harness/types.js";function createSessionWithUsage(input: {@@ -69,6 +69,20 @@ describe("resolveRemainingSessionTokenLimits", () => {});});+ it("measures a delegated grant from the bumped runtime limit", () => {+ const exhausted = createSessionWithUsage({+ limits: { maxInputTokensPerSession: 1_000_000, maxOutputTokensPerSession: 100_000 },+ usedInputTokens: 1_100_000,+ usedOutputTokens: 110_000,+ });+ const continued = bumpSessionRuntimeTokenLimits(exhausted);++ expect(resolveRemainingSessionTokenLimits(continued)).toEqual({+ maxInputTokensPerSession: 1_000_000,+ maxOutputTokensPerSession: 100_000,+ });+ });+it("clamps an overspent axis to zero", () => {const session = createSessionWithUsage({limits: { maxInputTokensPerSession: 100_000 },diff --git a/packages/eve/src/harness/subagent-token-budget.ts b/packages/eve/src/harness/subagent-token-budget.tsindex 9f1c87030..271a0e133 100644--- a/packages/eve/src/harness/subagent-token-budget.ts+++ b/packages/eve/src/harness/subagent-token-budget.ts@@ -1,41 +1,31 @@import type { RunSessionLimits } from "#channel/types.js";-import { getSessionTokenUsage } from "#harness/turn-tag-state.js";+import { getSessionRemainingTokenQuota } from "#harness/turn-tag-state.js";import type { HarnessSession } from "#harness/types.js";/**- * Computes the session token limits a delegated child inherits from its parent:- * the remaining quota (configured limit minus accumulated usage) split evenly- * across the batch's delegated calls, per axis, at dispatch time. `false`- * marks an axis with no inherited cap.+ * Computes the session token limits a delegated child inherits from its+ * parent: the parent's remaining runtime-limit quota split evenly across the+ * batch's delegated calls, per axis, at dispatch time. `false` marks an axis+ * with no inherited cap.** Splitting by `fanoutSize` makes one dispatch batch collectively* bounded by the parent's remainder. N parallel children cannot each spend* the full remainder. Sequential batches see the quota net of completed* children because their usage folds back into the parent's session totals.+ * A granted continuation bumps the parent's runtime limit, so children+ * dispatched after a grant draw from the fresh window.*/export function resolveRemainingSessionTokenLimits(session: Pick<HarnessSession, "limits" | "state">,fanoutSize = 1,): RunSessionLimits {const normalizedFanoutSize = Math.max(1, Math.floor(fanoutSize));- const usage = getSessionTokenUsage(session);- const maxInputTokensPerSession = grantShare(- remainingQuota(session.limits?.maxInputTokensPerSession, usage.inputTokens),- normalizedFanoutSize,- );- const maxOutputTokensPerSession = grantShare(- remainingQuota(session.limits?.maxOutputTokensPerSession, usage.outputTokens),- normalizedFanoutSize,- );+ const remaining = getSessionRemainingTokenQuota(session);- return { maxInputTokensPerSession, maxOutputTokensPerSession };-}--function remainingQuota(limit: number | undefined, used: number): number | false {- if (limit === undefined) {- return false;- }- return Math.max(0, limit - used);+ return {+ maxInputTokensPerSession: grantShare(remaining.inputTokens, normalizedFanoutSize),+ maxOutputTokensPerSession: grantShare(remaining.outputTokens, normalizedFanoutSize),+ };}function grantShare(remaining: number | false, fanOut: number): number | false {diff --git a/packages/eve/src/harness/tool-loop.test.ts b/packages/eve/src/harness/tool-loop.test.tsindex 6a689cf09..15fdef915 100644--- a/packages/eve/src/harness/tool-loop.test.ts+++ b/packages/eve/src/harness/tool-loop.test.ts@@ -1322,7 +1322,33 @@ describe("createToolLoopHarness", () => {expect(events.some((event) => event.type.endsWith(".failed"))).toBe(false);});- it("re-raises the limit prompt when the user replies without answering it", async () => {+ it("fails a zero-budget task instead of raising a continuation that cannot grant tokens", async () => {+ const { emit, events } = createEventCollector();+ const runStep = createToolLoopHarness(+ createTestConfig("task", emit, { capabilities: { requestInput: true } }),+ );+ const session = createTestSession({ limits: { maxInputTokensPerSession: 0 } });++ const result = await runStep(session, { message: "Hi again" });++ expect(vi.mocked(ToolLoopAgent)).not.toHaveBeenCalled();+ expect(result.next).toEqual({+ done: true,+ isError: true,+ output: "The session reached its configured input token limit.",+ });+ expect(events.some((event) => event.type === "input.requested")).toBe(false);+ });++ it("keeps one limit prompt pending when the user replies without answering it", async () => {+ setupMockAgent({+ finishReason: "stop",+ response: { messages: [{ content: "Hello!", role: "assistant" }] },+ text: "Hello!",+ toolCalls: [],+ toolResults: [],+ usage: { inputTokens: 7, outputTokens: 3 },+ });const { emit, events } = createEventCollector();const runStep = createToolLoopHarness(createTestConfig("conversation", emit));@@ -1331,7 +1357,17 @@ describe("createToolLoopHarness", () => {expect(vi.mocked(ToolLoopAgent)).not.toHaveBeenCalled();expect(reparked.next).toBeNull();- expect(events.filter((event) => event.type === "input.requested")).toHaveLength(2);+ expect(events.filter((event) => event.type === "input.requested")).toHaveLength(1);++ const resumed = await runStep(reparked.session, {+ inputResponses: [{ optionId: "continue", requestId: LIMIT_REQUEST_ID }],+ });++ expect(vi.mocked(ToolLoopAgent)).toHaveBeenCalledTimes(1);+ expect(resumed.session.history).toContainEqual({+ content: "also do this other thing",+ role: "user",+ });});it("preserves approval gates on step-scoped dynamic tools", async () => {diff --git a/packages/eve/src/harness/turn-tag-state.test.ts b/packages/eve/src/harness/turn-tag-state.test.tsindex 2bd61b90b..ecca6e5f5 100644--- a/packages/eve/src/harness/turn-tag-state.test.ts+++ b/packages/eve/src/harness/turn-tag-state.test.ts@@ -3,7 +3,9 @@ import { describe, expect, it } from "vitest";import {accumulateSessionUsage,accumulateTurnUsage,- extendSessionTokenBudget,+ bumpSessionRuntimeTokenLimits,+ getSessionRemainingTokenQuota,+ getSessionRuntimeTokenLimits,getSessionTokenLimitViolation,getSessionTokenUsage,getTurnUsageState,@@ -325,7 +327,7 @@ describe("session token limits", () => {);});- it("measures limits from the granted budget baseline after extendSessionTokenBudget", () => {+ it("checks usage against the bumped runtime limit after a grant", () => {const usage = {cacheReadTokens: 0,cacheWriteTokens: 0,@@ -345,22 +347,84 @@ describe("session token limits", () => {usedTokens: 10,});- const extended = extendSessionTokenBudget(session);+ const bumped = bumpSessionRuntimeTokenLimits(session);- // Both windows reset together so a session near two limits gets one prompt.- expect(getSessionTokenLimitViolation({ ...extended, limits: session.limits })).toBeNull();+ // Both axes bump together so a session near two limits gets one prompt.+ expect(getSessionRuntimeTokenLimits(bumped)).toEqual({ inputTokens: 20, outputTokens: 6 });+ expect(getSessionTokenLimitViolation({ ...bumped, limits: session.limits })).toBeNull();const laterUsage = { ...usage, inputTokens: 20, outputTokens: 3 };- const later = setTurnUsageState(extended, {+ const later = setTurnUsageState(bumped, {turnId: "turn_1",...laterUsage,session: laterUsage,});+ // `limit` reports the configured window size; `usedTokens` the lifetime total.expect(getSessionTokenLimitViolation({ ...later, limits: session.limits })).toEqual({kind: "input",limit: 10,- usedTokens: 10,+ usedTokens: 20,+ });+ });++ it("every grant moves the runtime limit, even after a large overshoot", () => {+ const usage = {+ cacheReadTokens: 0,+ cacheWriteTokens: 0,+ costUsd: 0,+ inputTokens: 35,+ outputTokens: 0,+ sawCost: false,+ };+ // A single model call overshot the 10-token window by several windows.+ const session = {+ ...setTurnUsageState(makeSession(), { turnId: "turn_0", ...usage, session: usage }),+ limits: { maxInputTokensPerSession: 10 },+ };++ expect(getSessionTokenLimitViolation(session)?.usedTokens).toBe(35);++ // One approval always unblocks: the runtime limit re-anchors to+ // usage + configured limit rather than incrementing by the limit.+ const bumped = bumpSessionRuntimeTokenLimits(session);+ expect(getSessionRuntimeTokenLimits(bumped)).toEqual({ inputTokens: 45 });+ expect(getSessionTokenLimitViolation({ ...bumped, limits: session.limits })).toBeNull();++ // A second grant cycle moves the ceiling again -- never idempotent.+ const laterUsage = { ...usage, inputTokens: 45 };+ const later = setTurnUsageState(bumped, {+ turnId: "turn_1",+ ...laterUsage,+ session: laterUsage,+ });+ expect(getSessionTokenLimitViolation({ ...later, limits: session.limits })).not.toBeNull();+ const bumpedAgain = bumpSessionRuntimeTokenLimits(later);+ expect(getSessionRuntimeTokenLimits(bumpedAgain)).toEqual({ inputTokens: 55 });+ expect(getSessionTokenLimitViolation({ ...bumpedAgain, limits: session.limits })).toBeNull();+ });++ it("reports remaining quota from the runtime limit", () => {+ const usage = {+ cacheReadTokens: 0,+ cacheWriteTokens: 0,+ costUsd: 0,+ inputTokens: 4,+ outputTokens: 9,+ sawCost: false,+ };+ const session = {+ ...setTurnUsageState(makeSession(), { turnId: "turn_0", ...usage, session: usage }),+ limits: { maxInputTokensPerSession: 10 },+ };++ expect(getSessionRemainingTokenQuota(session)).toEqual({+ inputTokens: 6,+ outputTokens: false,+ });+ expect(getSessionRemainingTokenQuota(makeSession())).toEqual({+ inputTokens: false,+ outputTokens: false,});});});diff --git a/packages/eve/src/harness/turn-tag-state.ts b/packages/eve/src/harness/turn-tag-state.tsindex cf553a672..5c34c6fd9 100644--- a/packages/eve/src/harness/turn-tag-state.ts+++ b/packages/eve/src/harness/turn-tag-state.ts@@ -23,10 +23,20 @@* session total crosses into it.*/import type { HarnessSession, SessionStateMap } from "#harness/types.js";+import {+ findRuntimeTokenLimitViolation,+ grantRuntimeTokenLimits,+ remainingRuntimeTokenQuota,+ resolveRuntimeTokenLimits,+ type SessionRuntimeTokenLimits,+ type SessionTokenLimitViolation,+} from "#harness/session-token-limits.js";import type { TokenUsage } from "#shared/token-usage.js";+export type { SessionRuntimeTokenLimits, SessionTokenLimitViolation };+const HARNESS_TURN_USAGE_STATE_KEY = "eve.harness.turnUsage";-const SESSION_TOKEN_BUDGET_BASELINE_KEY = "eve.harness.sessionTokenBudgetBaseline";+const SESSION_RUNTIME_TOKEN_LIMIT_KEY = "eve.harness.sessionRuntimeTokenLimit";export interface TokenUsageTotals {readonly cacheReadTokens: number;@@ -65,18 +75,6 @@ export function getTurnUsageState(state: SessionStateMap | undefined): TurnUsagereturn state?.[HARNESS_TURN_USAGE_STATE_KEY] as TurnUsageState | undefined;}-export type SessionTokenLimitViolation =- | {- readonly kind: "input";- readonly limit: number;- readonly usedTokens: number;- }- | {- readonly kind: "output";- readonly limit: number;- readonly usedTokens: number;- };-export function getSessionTokenUsage(session: Pick<HarnessSession, "state">): TokenUsageTotals {return getTurnUsageState(session.state)?.session ?? ZERO_TOKEN_USAGE;}@@ -92,79 +90,80 @@ export function toUsage(totals: TokenUsageTotals): TokenUsage {}/**- * Usage totals recorded when the user last granted a session token budget- * continuation. Limits are checked against usage measured from this- * baseline, so each grant opens a fresh window of the configured size.+ * The lifetime-usage ceilings currently in force, per axis. An axis is+ * absent when the configured limit leaves it uncapped. Before any granted+ * continuation the runtime limit equals the configured limit; each grant+ * re-anchors it to `usage + configured limit` via+ * {@link bumpSessionRuntimeTokenLimits}.*/-interface SessionTokenBudgetBaseline {- readonly inputTokens: number;- readonly outputTokens: number;-}--const ZERO_BUDGET_BASELINE: SessionTokenBudgetBaseline = {- inputTokens: 0,- outputTokens: 0,-};--function getSessionTokenBudgetBaseline(- state: SessionStateMap | undefined,-): SessionTokenBudgetBaseline {- return (- (state?.[SESSION_TOKEN_BUDGET_BASELINE_KEY] as SessionTokenBudgetBaseline | undefined) ??- ZERO_BUDGET_BASELINE- );+export function getSessionRuntimeTokenLimits(+ session: Pick<HarnessSession, "limits" | "state">,+): SessionRuntimeTokenLimits {+ const stored = session.state?.[SESSION_RUNTIME_TOKEN_LIMIT_KEY] as+ | SessionRuntimeTokenLimits+ | undefined;+ return resolveRuntimeTokenLimits({+ configured: configuredSessionTokenLimits(session),+ stored,+ });}/**- * Resets the session token budget windows to the current usage totals.- *- * Called when the user grants a continuation after a session token limit was- * reached. The configured limits keep their values and act as the size of the- * next budget window, measured from the moment of the grant. Both windows- * reset together so a session near two limits gets one prompt, not two- * back-to-back.+ * Bumps the runtime token limits after the user grants a continuation:+ * each capped axis is re-anchored to `current usage + configured limit`, so+ * one approval always buys one full configured window from the moment of+ * the grant (even when the last model call overshot by more than a window).+ * Both axes bump together so a session near two limits gets one prompt, not+ * two back-to-back. The configured limits never change.*/-export function extendSessionTokenBudget(session: HarnessSession): HarnessSession {+export function bumpSessionRuntimeTokenLimits(session: HarnessSession): HarnessSession {const usage = getSessionTokenUsage(session);+ const bumped = grantRuntimeTokenLimits({+ configured: configuredSessionTokenLimits(session),+ usage,+ });return {...session,state: {...session.state,- [SESSION_TOKEN_BUDGET_BASELINE_KEY]: {- inputTokens: usage.inputTokens,- outputTokens: usage.outputTokens,- } satisfies SessionTokenBudgetBaseline,+ [SESSION_RUNTIME_TOKEN_LIMIT_KEY]: bumped satisfies SessionRuntimeTokenLimits,},};}+/**+ * Remaining lifetime-token quota under the runtime limits, per axis.+ * `false` marks an uncapped axis. This is the pool a delegated child's+ * budget is granted from.+ */+export function getSessionRemainingTokenQuota(session: Pick<HarnessSession, "limits" | "state">): {+ inputTokens: number | false;+ outputTokens: number | false;+} {+ const usage = getSessionTokenUsage(session);+ const runtime = getSessionRuntimeTokenLimits(session);+ return remainingRuntimeTokenQuota({ runtime, usage });+}+export function getSessionTokenLimitViolation(session: Pick<HarnessSession, "limits" | "state">,): SessionTokenLimitViolation | null {const usage = getSessionTokenUsage(session);- const baseline = getSessionTokenBudgetBaseline(session.state);- const maxInputTokensPerSession = session.limits?.maxInputTokensPerSession;- const maxOutputTokensPerSession = session.limits?.maxOutputTokensPerSession;- const inputTokensInWindow = usage.inputTokens - baseline.inputTokens;- const outputTokensInWindow = usage.outputTokens - baseline.outputTokens;- if (maxInputTokensPerSession !== undefined && inputTokensInWindow >= maxInputTokensPerSession) {- return {- kind: "input",- limit: maxInputTokensPerSession,- usedTokens: inputTokensInWindow,- };- }- if (- maxOutputTokensPerSession !== undefined &&- outputTokensInWindow >= maxOutputTokensPerSession- ) {- return {- kind: "output",- limit: maxOutputTokensPerSession,- usedTokens: outputTokensInWindow,- };- }- return null;+ const runtime = getSessionRuntimeTokenLimits(session);+ return findRuntimeTokenLimitViolation({+ configured: configuredSessionTokenLimits(session),+ runtime,+ usage,+ });+}++function configuredSessionTokenLimits(+ session: Pick<HarnessSession, "limits">,+): SessionRuntimeTokenLimits {+ return {+ inputTokens: session.limits?.maxInputTokensPerSession,+ outputTokens: session.limits?.maxOutputTokensPerSession,+ };}/** Writes per-turn token state onto a new copy of the session. */diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 2e6cfdac5..da0a62e02 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -718,6 +718,9 @@ importers:eve:specifier: workspace:*version: link:../../../packages/eve+ zod:+ specifier: 'catalog:'+ version: 4.4.3devDependencies:'@types/node':specifier: 'catalog:'