The Open Framework for Building Agents
feat(eve)!: hold the turn on sign-ins and approvals, and stop repeated sign-in prompts (#4135)
Andrew Barbaddc8e12ff9ef
diff --git a/.changeset/connection-search-never-prompts.md b/.changeset/connection-search-never-prompts.mdnew file mode 100644index 000000000..889f5750d--- /dev/null+++ b/.changeset/connection-search-never-prompts.md@@ -0,0 +1,5 @@+---+"eve": patch+---++A plain `connection_search` no longer asks the user to sign in: connections they have not authorized appear under `unavailable` with `requiresSignIn: true`, and the model signs in to one connection at a time with `connection_search({ connection, signIn: true })`. A turn waiting on several sign-ins resumes once all of them are done.diff --git a/.changeset/held-sign-in-and-approval.md b/.changeset/held-sign-in-and-approval.mdnew file mode 100644index 000000000..dad8457ac--- /dev/null+++ b/.changeset/held-sign-in-and-approval.md@@ -0,0 +1,5 @@+---+"eve": minor+---++A connection sign-in or tool approval now holds the turn open, like a question or task: the stream emits `turn.waiting` instead of `turn.completed` and `session.waiting`, and the same turn resumes once the person acts. Every `turn.waiting` now carries `on`: `"input"` when a person must act, `"tasks"` when the turn waits on its own work. A message from that person steers the turn and cancels the pending sign-in or approval, messages from anyone else wait for the turn to end, and cancelling the turn withdraws both, so an approval no longer stays answerable after a cancel. A message sent while a session-limit prompt holds the turn waits in that turn and runs once the budget is granted. Slack's sign-in prompt gains a Cancel button.diff --git a/.changeset/one-sign-in-per-connect-grant.md b/.changeset/one-sign-in-per-connect-grant.mdnew file mode 100644index 000000000..3b4de57a7--- /dev/null+++ b/.changeset/one-sign-in-per-connect-grant.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Tools and connections that share one Vercel Connect connector now ask the user to sign in once. Before, each tool that called `ctx.getToken(connect(...))` and each connection using the same connector posted its own sign-in prompt for the same grant.diff --git a/.changeset/sign-in-resumes-as-requester.md b/.changeset/sign-in-resumes-as-requester.mdnew file mode 100644index 000000000..da5c81fd1--- /dev/null+++ b/.changeset/sign-in-resumes-as-requester.md@@ -0,0 +1,5 @@+---+"eve": patch+---++A completed sign-in now resumes the turn as the user who started it. Before, when someone else posted in a shared thread while the sign-in was pending, the resumed turn ran as that last speaker and failed with "the service rejected the token immediately after authorization".diff --git a/.changeset/slack-approval-cleanup.md b/.changeset/slack-approval-cleanup.mdnew file mode 100644index 000000000..0366468d0--- /dev/null+++ b/.changeset/slack-approval-cleanup.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Slack now cleans up after a tool approval. Once it resolves, the separate tool-input post is deleted, a direct-message approval's `Waiting on approval` note in the thread shows the outcome, and the card loses its buttons even when the approval ends without a click. The `Checking whether you can respond to this approval…` notice is no longer posted, since Slack cannot remove it afterwards.diff --git a/.changeset/slack-installation-team-principal.md b/.changeset/slack-installation-team-principal.mdnew file mode 100644index 000000000..c03cf64a7--- /dev/null+++ b/.changeset/slack-installation-team-principal.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Slack now identifies a person by the app installation workspace on both messages and button clicks, as `slack:<installation team>:<user>`. Before, a Slack Connect or Enterprise Grid user could get one principal from their messages and another from approval clicks, so they were asked to sign in to the same connection again. Their principal changes once with this release, so they sign in to user-scoped connections one more time.diff --git a/.changeset/slack-sign-in-prompt-copy.md b/.changeset/slack-sign-in-prompt-copy.mdnew file mode 100644index 000000000..658ba63d4--- /dev/null+++ b/.changeset/slack-sign-in-prompt-copy.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Slack sign-in prompts now say which service they are for and why. The private message reads `Connect Linear: To continue, I need access to your Linear account` with a `Connect Linear` button, and a device code, when the provider issues one, appears as a short hint below the button instead of leading with `Use code … when prompted`. The thread status names who it is waiting on, as in `Waiting for @alice to connect Linear…`.diff --git a/docs/channels/slack.mdx b/docs/channels/slack.mdxindex dabb5df24..1a4bf9f32 100644--- a/docs/channels/slack.mdx+++ b/docs/channels/slack.mdx@@ -235,6 +235,8 @@ Override every enabled inbound handler; leaving `onDirectMessage` out would retaKeep authorization checks inside sensitive tools too. The inbound allowlist controls who can start a turn; tool-level checks control what that authenticated Slack principal may read or change.+`defaultSlackAuth` and `ctx.defaultAuth` identify a person as `slack:<team>:<user>`, where `<team>` is the app installation workspace when Slack reports one, otherwise the event's team. Messages and button clicks resolve the same team, so a Slack Connect user keeps one principal, and one connection sign-in, whether they post in the thread or click an approval.+`onInputResponse` runs after eve decodes a built-in HITL answer but before the parked session resumes. Return `{ auth }` to accept the answer. Returning `null` or throwing rejects it, leaves the request pending, and keeps the Slack controls active. `ctx.defaultAuth` identifies the Slack user who submitted the signed interaction; it does not authorize that user by itself.If you omit `onInputResponse`, Slack accepts HITL answers with the submitting user's auth, regardless of which message or event handlers you define. Define `onInputResponse` when those handlers enforce an invocation policy that must also apply to HITL answers. Put sensitive prompts in conversations limited to the intended approvers, and re-check `ctx.session.auth.current` inside a sensitive tool before performing its side effect.@@ -685,6 +687,8 @@ export default slackChannel({The callback receives the `InputRequest` and current `SessionContext`, so it can switch on `kind`, `action`, or presentation properties. Omit it to keep every request in the thread, or return `"direct-message"` unconditionally to make every request private. If eve cannot resolve the triggering Slack user for a direct message, it logs the undelivered request and posts no fallback, so private delivery fails closed. The existing `onInputResponse` or tool approval response policy still decides whether the person who clicks may respond.+When a tool approval resolves, eve retires the messages it posted for it. The card loses its buttons and shows the answer and who gave it, the separate tool-input post is deleted, and a direct-message request's `Waiting on approval` note in the thread changes to the outcome. This also happens when the approval ends without a click, for example when the person replies in the thread instead, in which case the card reads `No longer needed`.+A renderer's `input.requested` handler can extend delivery and then hand the requests on to eve with `next()`. For example, an application can add non-sensitive context to the Slack thread before eve renders the actionable controls. The configured `approvalChannel` still sends sensitive reviews to a DM and keeps ordinary requests in the thread:```ts title="agent/channels/slack.ts"@@ -738,10 +742,12 @@ deriveHitlResponse({ actionId: button.action_id, value: button.value });// { response: { optionId: "approve", requestId: request.requestId } }```-Use them to test a custom renderer against eve's wire format, or to post eve's controls from your own renderer. A click on these controls in the session's Slack thread resumes the session through `onInputResponse`, the same as eve's own controls. To pass `response` to `respond()` yourself, validate it first with `parseInputResponses` from `eve/client`.+Use them to test a custom renderer against eve's wire format, or to post eve's controls from your own renderer. A click on these controls in the session's Slack thread resumes the session through `onInputResponse`, the same as eve's own controls. eve does not track controls you post yourself, so update or remove those messages from your renderer's `approval.settled` and `input.resolved` handlers. To pass `response` to `respond()` yourself, validate it first with `parseInputResponses` from `eve/client`.Authorization prompts split public status from private credentials. A sign-in challenge (OAuth URL, device code) is a credential. Anyone who completes it binds their identity to the session's connection. The default `authorization.required` handler posts a public, link-free status in the thread, delivers the actual challenge ephemerally to the Slack user behind the event's `principalId`, device code included, and then updates that public status when `authorization.completed` fires. A renderer's `authorization.required` handler receives a private-delivery context with `postEphemeral`, `postDirectMessage` (needs the `im:write` scope), and `state`. There is, intentionally, no public `post` and no raw API access. Its `next()` reaches eve's default, which posts the public status; skip `next` to deliver the challenge yourself, as this handler does by direct message. `state.slackUsersByPrincipal` maps each principal eve has seen in the thread, through a message or an input response, to its Slack user ID. If the principal has no entry, the challenge is not sent privately.+The default private prompt names the service, says the agent is paused, and pairs a **Connect** button with **Cancel**. Cancel ends the held turn and removes the prompt; the public status, "Paused: waiting for @user to connect Linear…", then reads "Linear sign-in cancelled". Approval cards say the agent is paused until someone approves or cancels.+```tsrenderers: [{diff --git a/docs/concepts/built-in-tools.md b/docs/concepts/built-in-tools.mdindex 2a472c139..e24e429f2 100644--- a/docs/concepts/built-in-tools.md+++ b/docs/concepts/built-in-tools.md@@ -269,8 +269,9 @@ export default disableTool();`connection_search` and `connection_execute` give the model every tool from the agent's [connections](../connections) without adding each tool to the model's tool list. eve adds both when the agent has a static connection or a dynamic connection resolver, even when `defaultTools` is `false`, so there is no add command.-- `connection_search({ query?, connection?, limit?, offset? })` returns matching tools with their connection, name, description, and a TypeScript signature rendered from the tool's schemas. Omit `query` to list every tool, or pair it with `connection` to list one connection's tools.-- `connection_execute({ connection, tool, input })` checks `input` against the tool's input schema, calls the tool, and returns its result. The stream reports the call as a nested action named `<connection>__<tool>`, such as `linear__list_issues`, whose `parentCallId` is the `connection_execute` call id.+- `connection_search({ query?, connection?, signIn?, limit?, offset? })` returns matching tools with their connection, name, description, and a TypeScript signature rendered from the tool's schemas. Omit `query` to list every tool, or pair it with `connection` to list one connection's tools. A plain search never asks the user to sign in. For a connection the user has not signed in to, the result tells the model that sign-in is needed before its tools can be listed: the connection appears under `unavailable` with `requiresSignIn: true` and an error that points to `signIn: true`. Tools from the other connections are still returned.+- `connection_search({ connection, signIn: true, query? })` asks the user to sign in to that one connection when they have not yet, then returns its matching tools. Without `connection` it fails, so the user is asked about one service at a time.+- `connection_execute({ connection, tool, input })` checks `input` against the tool's input schema, calls the tool, and returns its result. When the connection needs the user's authorization, it also asks the user to sign in and the call parks until sign-in completes. The stream reports the call as a nested action named `<connection>__<tool>`, such as `linear__list_issues`, whose `parentCallId` is the `connection_execute` call id.The definitions of both tools never change during a session. eve lists connection names and descriptions in append-only context messages rather than in the system prompt, so finding a tool, signing in, or resolving a dynamic connection keeps the cached prompt prefix.diff --git a/docs/concepts/sessions-runs-and-streaming.md b/docs/concepts/sessions-runs-and-streaming.mdindex 4ac6892f9..3edc1a67e 100644--- a/docs/concepts/sessions-runs-and-streaming.md+++ b/docs/concepts/sessions-runs-and-streaming.md@@ -93,7 +93,7 @@ The stream is newline-delimited JSON (NDJSON), one event per line:| `authorization.completed` | A connection's authorization resolved; carries `outcome`. || `step.completed` | A model step finished; carries `finishReason` and usage. || `step.failed` | A model step failed; carries `{ code, message, details? }`. |-| `turn.waiting` | The open turn parked while its tasks work or a call it runs waits on input; carries `turnId`. The turn has not ended. |+| `turn.waiting` | The open turn parked; carries `turnId` and `on`: `"input"` while a person must act on a sign-in, approval, or question, `"tasks"` while work it started runs. The turn has not ended. || `turn.completed` | The turn finished. || `turn.failed` | The turn failed; carries `{ code, message, details? }`. || `turn.cancelled` | The turn was cancelled before finishing; always followed by `session.waiting`. |@@ -123,7 +123,7 @@ Note: consider the privacy, confidentiality, and user-experience implications foA delegated subagent publishes progress on its own child-session stream. When a subagent tool call or a workflow tool's `ctx.agent` opens a session, the parent emits `agent.started` with the call's `callId` and `turnId`, its `taskId` when the call runs as a task, the child's `sessionId`, and a `streamPath` that a client follows with `session.agent(started).stream()`. A subagent tool call is a [task](#task-events) whose `task.started` has `kind: "agent"`: its `task.started` and `task.settled` carry the call, and `task.settled.data.output` is the child's reply. A call that fails before the child session opens has no `agent.started`. A settled call does not end the child session; later calls with the same `taskId` reach it.-A question or sign-in from inside a running call does not end the turn. This covers a workflow tool's `ctx.ask()`, including the built-in `ask_question` tool, and a delegated subagent's question or sign-in. The stream emits `input.requested` or `authorization.required`, then `turn.waiting` with the open turn's `turnId`. After the answer or sign-in, the turn resumes with the next `step.started` for the same `turnId`, and only `turn.completed`, `turn.failed`, or `turn.cancelled` ends it. When you answer a question that belongs to a workflow tool or a subagent, the session you answer on emits `input.resolved` for it as it routes the answer down. If the workflow tool run that relayed a question or approval ends before anyone answers, that session emits `input.resolved` with `outcome: "cancelled"` for it. This covers the run's own `ctx.ask()` questions and requests from sessions it opened with `ctx.agent`. A cancelled turn does the same for every question or approval the session relays. A tool approval the turn requests still ends the turn: `input.requested`, `turn.completed`, then `session.waiting`.+A question or sign-in from inside a running call does not end the turn. This covers a workflow tool's `ctx.ask()`, including the built-in `ask_question` tool, and a delegated subagent's question or sign-in. The stream emits `input.requested` or `authorization.required`, then `turn.waiting` with `on: "input"` and the open turn's `turnId`. After the answer or sign-in, the turn resumes with the next `step.started` for the same `turnId`, and only `turn.completed`, `turn.failed`, or `turn.cancelled` ends it. When you answer a question that belongs to a workflow tool or a subagent, the session you answer on emits `input.resolved` for it as it routes the answer down. If the workflow tool run that relayed a question or approval ends before anyone answers, that session emits `input.resolved` with `outcome: "cancelled"` for it. This covers the run's own `ctx.ask()` questions and requests from sessions it opened with `ctx.agent`. A cancelled turn does the same for every question or approval the session relays. A sign-in or tool approval the turn raises itself holds it the same way, also with `on: "input"`. A message from the same person steers the turn and cancels the request: a sign-in reports `authorization.completed` with `outcome: "declined"`, and an approval reports `input.resolved` with `outcome: "ignored"`. Messages from other people wait until the turn ends. Cancelling the turn ends both: a held sign-in reports `authorization.completed` with `outcome: "declined"`, and a held approval reports `input.resolved` with `outcome: "cancelled"`.### Task events@@ -217,7 +217,7 @@ curl -X POST http://127.0.0.1:2000/eve/v1/session/<sessionId> \Message sends default to `"steer"`. Before assistant output begins, eve interrupts pending model generation and continues the same turn with the correction. Reasoning and provider search progress do not count as assistant output. An executing eve tool finishes safely, and its result is preserved before the correction reaches the next model call. A steering message aborts the [`ctx.abortSignal`](/docs/tools/workflows#stop-early-for-a-new-message) of each `execute` workflow tool call the turn is waiting on: its questions are withdrawn, `sleep` and `ask_question` stop early, and the call settles with what its body returns. A steering message ends a `task_wait` but never interrupts a task. Only the turn's own caller steers it; another caller's message waits for the turn to end. After assistant output starts, steering applies at the next committed workflow boundary; text already streamed remains visible. Channels and TypeScript `Session.send(...)` calls can select `turnPolicy: "queue"` when the active turn should finish first. Structured `inputResponses` answer their addressed requests.-If the session is waiting on a human-in-the-loop approval, respond with the channel’s Approve or Cancel controls. Text messages do not decide an approval; unrelated text starts an ordinary turn while the approval stays pending and answerable. A later structured `inputResponses` answer keyed by its `requestId` still resumes the original tool call, even after intervening turns.+If the session is waiting on a human-in-the-loop approval, respond with the channel’s Approve or Cancel controls. Text that doesn't match an option doesn't approve the call. From the person the turn serves, it steers the turn and cancels the approval, even when sent with `turnPolicy: "queue"`, since the turn can't end until that person acts; from anyone else, it waits until the turn ends. If they had already approved some calls in the batch, those calls still run. Cancelling the turn withdraws the approval, and a later answer to it approves nothing.A pending `ctx.ask()` question from a tool, such as `ask_question`, can be answered with plain text. When it is the only pending question, a message that matches an option answers it, and so does any message when the question allows free text. Otherwise eve does not guess which question the text addresses: the message follows the normal `turnPolicy`. A steering message withdraws the questions of the `execute` workflow tool calls the turn waits on, which resolve as `cancelled`; a task's questions stay open. Questions from subagents always need a structured response. Use structured responses to target requests unambiguously.diff --git a/docs/connections/overview.mdx b/docs/connections/overview.mdxindex cb6016f59..3ce7085af 100644--- a/docs/connections/overview.mdx+++ b/docs/connections/overview.mdx@@ -227,6 +227,10 @@ export default defineMcpClientConnection({Use the connector UID returned by `vercel connect create`; the display name passed to `--name` is not the UID. `connect("linear/myagent")` is shorthand for a user-scoped interactive OAuth connection: eve resolves a token for the active user before each tool call, emits `authorization.required` when that user has not authorized yet, and resumes the parked turn after the callback completes.+Discovering tools never posts a sign-in prompt for a service the answer does not need. A plain `connection_search` lists connections the user has not authorized yet under `unavailable` with `requiresSignIn: true`. Most OAuth MCP servers reject even tool listing without the user's token, so the model asks for sign-in explicitly, one connection at a time, with `connection_search({ connection, signIn: true })`, which returns that connection's tools once the user signs in. Calling a tool with `connection_execute` on a connection the user has not authorized also asks for sign-in.++A sign-in holds the turn open until the user connects, the turn is cancelled, or the user sends another message. That message steers the turn: eve cancels the sign-in, reports `authorization.completed` with `outcome: "declined"`, and the model reads the message next. Messages from other people in a shared thread wait until the turn ends, and the completed sign-in resumes the turn as the person who started it.+When a local subagent needs interactive authorization, eve surfaces the authorization lifecycle on the root session's channel, including through nested subagent chains. The challenge still points to the child session's callback, so completing it resumes the child directly while the parent continues waiting for its result. The parent's turn stays open: it emits `turn.waiting` after the proxied `authorization.required` and resumes under the same `turnId` once the subagent returns.That means the channel that creates or continues the session must authenticate a real user. For a web app, configure `agent/channels/eve.ts` so your app session maps to `principalType: "user"`; for platform channels, use the built-in channel auth that maps the sender to a user principal. If no authenticated user is attached to the session, the first user-scoped connection call fails with `reason: "principal_required"`.@@ -273,7 +277,7 @@ export default defineMcpClientConnection({// Probed before every tool call. Return a token to run the tool;// throw `Required` to start the consent flow.getToken: async ({ principal }) => {- const token = await lookupCachedToken(principal);+ const token = await loadStoredToken(principal);if (!token) throw new ConnectionAuthorizationRequiredError("linear");return { token };},@@ -289,15 +293,17 @@ export default defineMcpClientConnection({// Runs when the provider redirects to the callback URL. `resume` is// typed as `{ verifier: string } | undefined`; `callback.params`// holds the IdP's returned query/body params.- completeAuthorization: async ({ resume, callback }) => {+ // Persist the token here so `getToken` finds it on later tool calls.+ completeAuthorization: async ({ principal, resume, callback }) => {const token = await exchangeCode(resume!.verifier, callback.params.code!);+ await saveStoredToken(principal, token);return { token };},}),});```-`getToken` runs before every tool call. `startAuthorization` and `completeAuthorization` are both-or-neither: provide one without the other and you get a definition error. The `challenge` rides along verbatim on the `authorization.required` event. Its fields:+`getToken` runs before every tool call. eve does not store tokens for you: the token returned from `completeAuthorization` is reused only within the current workflow step, so it never lands in durable session state. Save it in `completeAuthorization` (or let your OAuth provider keep it) and read it back in `getToken`. If `getToken` cannot find the token on a later call, eve asks the user to sign in again. `startAuthorization` and `completeAuthorization` are both-or-neither: provide one without the other and you get a definition error. The `challenge` rides along verbatim on the `authorization.required` event. Its fields:| Field | Purpose || -------------- | ----------------------------------------------------------------------------------------- |diff --git a/docs/guides/client/streaming.mdx b/docs/guides/client/streaming.mdxindex 75a5f479c..b35ba2d25 100644--- a/docs/guides/client/streaming.mdx+++ b/docs/guides/client/streaming.mdx@@ -56,11 +56,12 @@ This consumes the stream until the current turn boundary:- `session.waiting`- `session.completed`- `session.failed`-- `turn.waiting` while an `input.requested` the response read is unanswered+- `turn.waiting` with `on: "input"`, which the turn emits while a person must+ act on an approval or question. A sign-in emits it too, but the response stays+ attached through it; see [authorization pauses](#authorization-pauses)-A `turn.waiting` without a pending request is informational: the turn stays-open, and `result()` keeps reading to its final reply. This includes a sign-in-a running call needs, such as a subagent's connection. When a call the turn+A `turn.waiting` with `on: "tasks"` is informational: the turn stays open, and+`result()` keeps reading to its final reply. When a call the turnruns asks a question, such as a workflow tool's `ctx.ask()`, `result()` returns`status: "waiting"` with the question in `inputRequests`. Answer it with`respond()`, which keeps reading the same turn. A question stays pending until@@ -68,7 +69,7 @@ its `input.resolved`, which the session you answer on also emits for asubagent's or workflow tool's question.A turn that waits on its [tasks](/docs/tools/workflows#run-calls-as-tasks-task)-also emits `turn.waiting` without a pending request. The text the model wrote+emits `turn.waiting` with `on: "tasks"`. The text the model wrotebefore the wait is not the reply: `result()` resolves with the final replyafter the tasks finish.@@ -166,7 +167,7 @@ client-resolvable `http(s)` and `data:` URLs.`authorization.required` is different from the normal `session.waiting` boundary. It means a connection needs OAuth or another authorization challenge before the parked turn can continue. Chat UIs should render the authorization prompt, disable ordinary text input for that session, and persist the event with the rest of the chat history.-The stream can emit an interim `session.waiting` while the authorization callback is pending. An active `send()` or `respond()` response stays attached across that parking boundary until the authorization resolves and the resumed turn reaches its next boundary. If you support refresh while an authorization prompt is pending, keep the session cursor from the started session and rehydrate the saved events on load; the callback or a structured decline resumes the same eve session.+The sign-in holds the turn open, and the stream emits `turn.waiting` with `on: "input"` while the authorization callback is pending. An active `send()` or `respond()` response stays attached across it until the authorization resolves and the resumed turn reaches its next boundary. If you support refresh while an authorization prompt is pending, keep the session cursor from the started session and rehydrate the saved events on load; the callback or a structured decline resumes the same eve session.## Reconnectiondiff --git a/docs/guides/dynamic-capabilities.md b/docs/guides/dynamic-capabilities.mdindex 7d961ca0f..4b7ec8b60 100644--- a/docs/guides/dynamic-capabilities.md+++ b/docs/guides/dynamic-capabilities.md@@ -362,7 +362,7 @@ A dynamic connection, tool, or skill whose name matches an **authored** one **ovAt `turn.started`, model, tool, skill, and subagent resolvers receive the visible conversation history and incoming message in `ctx.messages`, oldest first. Request context is included, and history projection still applies. Read these messages from the handler's second argument; the event itself contains turn metadata. Instruction resolvers use the separate snapshot described under [Dynamic instructions](#dynamic-instructions).-This also applies while a session-limit prompt keeps the incoming message queued and when authorization completes. Authorization callbacks without new or queued input receive the visible history. When memory recall runs, its results appear in the projected snapshot before incoming input.+A message that steers a held turn, or waits behind its session-limit prompt, joins that turn instead of starting one, as does a completed sign-in, so `turn.started` resolvers do not see it; `step.started` resolvers do. When memory recall runs, its results appear in the projected snapshot before incoming input.### Execution orderdiff --git a/docs/guides/frontend/overview.mdx b/docs/guides/frontend/overview.mdxindex 8c7b3afe8..9c81f0b24 100644--- a/docs/guides/frontend/overview.mdx+++ b/docs/guides/frontend/overview.mdx@@ -226,7 +226,7 @@ received attachment as a `file` part on the user message. The part includes `med## Human-in-the-loop prompts-Tools opt into approval with `approval`, and workflow tools such as the opt-in `ask_question` tool ask the user a question with `ctx.ask()` — see [Human-in-the-loop](/docs/human-in-the-loop) for the server-side model. Either way the stream emits an `input.requested` event. The default projection keeps open requests in `data.inputs`, including requests introduced on earlier turns while unrelated work continues. Render from that ledger and answer through the same session with `respond()`. The request also appears on its originating `dynamic-tool` part at `part.toolMetadata?.eve?.inputRequest` for inline display:+Tools opt into approval with `approval`, and workflow tools such as the opt-in `ask_question` tool ask the user a question with `ctx.ask()` — see [Human-in-the-loop](/docs/human-in-the-loop) for the server-side model. Either way the stream emits an `input.requested` event. The default projection keeps open requests in `data.inputs`, including a request that sits on an earlier message than the latest one. Render from that ledger and answer through the same session with `respond()`. The request also appears on its originating `dynamic-tool` part at `part.toolMetadata?.eve?.inputRequest` for inline display:```tsximport { openConversationInputs } from "eve/react";diff --git a/docs/tools/human-in-the-loop.md b/docs/tools/human-in-the-loop.mdindex 6b229ba1b..c69595941 100644--- a/docs/tools/human-in-the-loop.md+++ b/docs/tools/human-in-the-loop.md@@ -9,7 +9,7 @@ Human-in-the-loop (HITL) is any point where the agent durably pauses and waits f- **Approvals** — a tool policy allows, denies, or pauses a call for a person to review. The agent decides to call the tool; the policy decides whether it runs automatically or needs a human decision.- **Questions** — the agent itself asks the user a clarifying question or a choice mid-turn, and parks until they answer.-An approval ends the turn and the session parks at `session.waiting`. A question keeps the turn open, and the stream reports `turn.waiting`. Either way the run waits durably, for as long as it takes — seconds or days — and picks back up exactly where it left off once the answer arrives. Channels render the request for you.+Both keep the turn open, and the stream reports `turn.waiting`. The run waits durably, for as long as it takes — seconds or days — and picks back up exactly where it left off once the answer arrives. Channels render the request for you.## Approvals@@ -212,7 +212,7 @@ Approvals and questions share one protocol:1. A tool call needs approval, or a workflow tool such as `ask_question` calls `ctx.ask()`.2. eve emits an `input.requested` stream event carrying the pending requests.-3. The run parks durably, for as long as it takes. An approval ends the turn with `turn.completed`, then `session.waiting`. A question keeps the turn open: the stream emits `turn.waiting`, and after the answer the turn resumes under the same `turnId`.+3. The run parks durably, for as long as it takes. The turn stays open: the stream emits `turn.waiting`, and after the answer the turn resumes under the same `turnId`. That `turn.waiting` carries `on: "input"`, since a person must act.4. The client answers with `inputResponses` (structured, keyed by `requestId`) or a normal follow-up `message`. A follow-up whose text matches an option ID, option label, or numeric option index resolves automatically, including approval options such as `approve` and `cancel`.For `ctx.ask()` questions from tools, a follow-up message answers the question only when exactly one question is pending. The message must match an option, or the question must allow free text. Otherwise the message follows the session's `turnPolicy`. A steering message, the default, aborts the `ctx.abortSignal` of each `execute` workflow tool call the turn waits on, so a question such a call asked, such as `ask_question`'s, is withdrawn and resolves as `cancelled`. The model reads the message once those calls settle. Questions from subagents need a structured response.@@ -226,7 +226,7 @@ The run picks back up exactly where it parked. Because the pause is durable, notWhen a subagent requests input, eve emits the same `input.requested` event on its parent session. Answering through that parent session routes the response directly to the blocked child without invoking the parent model.-For approval requests, unrelated follow-up text does not deny the tool call. eve keeps the approval pending and records that pending state in model-visible session history. Follow-up turns run normally and may call other tools while the approval remains unresolved. Once it is answered, eve settles the original tool call exactly once.+For approval requests, a follow-up message that doesn't match an option steers the turn instead of answering it. eve cancels the turn's pending approval, so the call doesn't run and `input.resolved` reports `outcome: "ignored"`, and the model reads the message next. This happens even when the message is sent with `turnPolicy: "queue"`, because a turn held on a person can't end until they act. Calls the person already approved in the same batch still run. A message from someone other than the person the turn serves waits until the turn ends. Cancelling the turn withdraws its approval: the call doesn't run, `input.resolved` reports `outcome: "cancelled"`, and a later answer to it approves nothing.See [Sessions, runs & streaming](/docs/concepts/sessions-runs-and-streaming) for the full event and resume contract that this builds on.diff --git a/docs/tools/tasks.md b/docs/tools/tasks.mdindex d921b635b..168134180 100644--- a/docs/tools/tasks.md+++ b/docs/tools/tasks.md@@ -233,8 +233,8 @@ continues once the person answers or signs in.In every session, `turn.completed` comes only when the turn really ends. The TypeScript client's`send(...).result()` and the MCP channel's `agent_get` read past `turn.waiting` and report thefinal reply rather than the text written before the wait. `result()` stops at `turn.waiting` only-while a question is pending, and returns `status: "waiting"` with it; `respond()` then reads the-same turn to its end. See [Aggregate a turn](/docs/guides/client/streaming#aggregate-a-turn).+while a question is pending or the turn waits on a sign-in or approval, and returns+`status: "waiting"`; `respond()` then reads the same turn to its end. See [Aggregate a turn](/docs/guides/client/streaming#aggregate-a-turn).Channels such as [Slack](/docs/channels/slack) post a root session's text before the wait as anordinary reply, then post the reply after the results as another message. Slack also shows eachturn's tasks in a live [task card](/docs/channels/slack#task-card).@@ -263,12 +263,12 @@ same `taskId` continues where it left off. A `serve` body that doesn't return to## Stream events-| Event | When | Data |-| --------------- | ------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------ |-| `task.started` | A call starts a task, or reaches a resumable task by its `taskId` | `taskId`, `callId`, `turnId`, the tool `name`, and `kind` |-| `task.settled` | A reply, return, failure, or cancel settles one call | `taskId`, `callId`, `turnId`, `name`, `kind`, `status`, and `output`, `error`, or `cancel` |-| `agent.started` | A workflow run, including an agent tool's, opens a session with an agent | `callId`, `turnId`, `taskId`, `name`, `sessionId`, `streamPath` |-| `turn.waiting` | An open turn parks on its tasks, a `task_wait`, a question, or a sign-in | `turnId` and `sequence` |+| Event | When | Data |+| --------------- | ------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------ |+| `task.started` | A call starts a task, or reaches a resumable task by its `taskId` | `taskId`, `callId`, `turnId`, the tool `name`, and `kind` |+| `task.settled` | A reply, return, failure, or cancel settles one call | `taskId`, `callId`, `turnId`, `name`, `kind`, `status`, and `output`, `error`, or `cancel` |+| `agent.started` | A workflow run, including an agent tool's, opens a session with an agent | `callId`, `turnId`, `taskId`, `name`, `sessionId`, `streamPath` |+| `turn.waiting` | An open turn parks on its tasks, a `task_wait`, a question, a sign-in, or an approval | `turnId`, `sequence`, and `on`: `"input"` when a person must act, otherwise `"tasks"` |`task.started` and `task.settled` come once each per call, and `(taskId, callId)` identifies thecall. Both carry that call's `turnId`, which for a resumable task's later call can be a later turndiff --git a/e2e/fixtures/agent-subagents-hitl/evals/mixed-runtime-action-approval.eval.ts b/e2e/fixtures/agent-subagents-hitl/evals/mixed-runtime-action-approval.eval.tsindex 6d5053fc1..c4e239805 100644--- a/e2e/fixtures/agent-subagents-hitl/evals/mixed-runtime-action-approval.eval.ts+++ b/e2e/fixtures/agent-subagents-hitl/evals/mixed-runtime-action-approval.eval.ts@@ -25,7 +25,7 @@ export default defineEval({parked.eventOrder([{ type: "actions.requested" },{ type: "input.requested" },- { type: "session.waiting" },+ { type: "turn.waiting" },]);session.requireInputRequest({ display: "confirmation", toolName: "collision-gate" });diff --git a/e2e/fixtures/agent-subagents/evals/remote-direct-authorization.eval.ts b/e2e/fixtures/agent-subagents/evals/remote-direct-authorization.eval.tsindex 3a24cadac..dff2614ea 100644--- a/e2e/fixtures/agent-subagents/evals/remote-direct-authorization.eval.ts+++ b/e2e/fixtures/agent-subagents/evals/remote-direct-authorization.eval.ts@@ -13,27 +13,38 @@ export default defineEval({const live = await session.start(`${DIRECT_AUTHORIZATION}: Alice asks her remote agent to authorize the release checklist.`,);- const required = await live.waitForEvent("authorization.required");- if (required.data.webhookUrl === undefined)+ // The child's sign-in holds the parent's turn, so the response stops there.+ const held = await live.result();+ const required = held.events.find((event) => event.type === "authorization.required");+ if (required?.type !== "authorization.required" || required.data.webhookUrl === undefined)throw new Error("Direct remote authorization has no callback URL.");- const callback = new URL(required.data.webhookUrl);- callback.searchParams.set("code", "direct-release-code");- const response = await fetch(callback);- if (!response.ok) throw new Error(`Direct authorization callback returned ${response.status}.`);- const turn = await live.result();- turn.expectOk();- turn.event("authorization.required", {+ held.event("turn.waiting", { data: { on: "input" } });+ held.event("authorization.required", {data: {name: "direct-release-authorization",authorization: { userCode: "direct-release-code" },principalId: ALICE,},});+ const resumed = t.target.watchTurn(live.session.sessionId, {+ startIndex: live.session.state?.streamIndex,+ });+ const callback = new URL(required.data.webhookUrl);+ callback.searchParams.set("code", "direct-release-code");+ const response = await fetch(callback);+ if (!response.ok) throw new Error(`Direct authorization callback returned ${response.status}.`);+ const turn = await resumed.result();+ turn.expectOk();+ turn.notEvent("turn.started");turn.event("authorization.completed", {data: { name: "direct-release-authorization", outcome: "authorized", principalId: ALICE },});turn.messageIncludes("PARENT-DIRECT-COMPLETE: DIRECT-AUTHORIZATION-COMPLETE");- t.calledSubagent("remote-loopback", { status: "completed", count: 1 });+ // The call starts in the held segment and settles in the resumed one.+ turn.event("task.settled", {+ count: 1,+ data: { name: "remote-loopback", status: "completed" },+ });t.noFailedActions();},});diff --git a/e2e/fixtures/agent-subagents/evals/remote-nested-authorization.eval.ts b/e2e/fixtures/agent-subagents/evals/remote-nested-authorization.eval.tsindex 7b35dec69..459a9bc02 100644--- a/e2e/fixtures/agent-subagents/evals/remote-nested-authorization.eval.ts+++ b/e2e/fixtures/agent-subagents/evals/remote-nested-authorization.eval.ts@@ -13,20 +13,27 @@ export default defineEval({const live = await session.start(`${NESTED_AUTHORIZATION}: Alice asks the remote agent to authorize her release checklist.`,);- const required = await live.waitForEvent("authorization.required");- if (required.data.webhookUrl === undefined) {+ // The nested worker's sign-in holds the parent's turn, so the response stops there.+ const held = await live.result();+ const required = held.events.find((event) => event.type === "authorization.required");+ if (required?.type !== "authorization.required" || required.data.webhookUrl === undefined) {throw new Error("Nested authorization challenge has no callback URL.");}+ held.event("turn.waiting", { data: { on: "input" } });+ held.event("authorization.required", {+ data: { name: AUTHORIZATION_NAME, authorization: { userCode: AUTHORIZATION_CODE } },+ });+ const resumed = t.target.watchTurn(live.session.sessionId, {+ startIndex: live.session.state?.streamIndex,+ });const callback = new URL(required.data.webhookUrl);callback.searchParams.set("code", AUTHORIZATION_CODE);const response = await fetch(callback);if (!response.ok) throw new Error(`Nested authorization callback returned ${response.status}.`);- const turn = await live.result();+ const turn = await resumed.result();turn.expectOk();- turn.event("authorization.required", {- data: { name: AUTHORIZATION_NAME, authorization: { userCode: AUTHORIZATION_CODE } },- });+ turn.notEvent("turn.started");turn.event("authorization.completed", {data: { name: AUTHORIZATION_NAME, outcome: "authorized" },});diff --git a/e2e/fixtures/agent-tools-hitl/evals/auth/authorization-nonblocking.eval.ts b/e2e/fixtures/agent-tools-hitl/evals/auth/authorization-nonblocking.eval.tsindex d3cd7251c..b57bfff59 100644--- a/e2e/fixtures/agent-tools-hitl/evals/auth/authorization-nonblocking.eval.ts+++ b/e2e/fixtures/agent-tools-hitl/evals/auth/authorization-nonblocking.eval.ts@@ -1,34 +1,34 @@import { defineEval } from "eve/evals";/**- * An open authorization challenge must not wedge the session: an ordinary- * message runs as a normal turn while the challenge waits. Callback completion- * and tool re-execution are covered deterministically by- * `workflow-entry.integration.test.ts`.+ * A sign-in holds the turn, as a question or task does. A message from the+ * same person steers that turn and cancels the sign-in, so the turn moves on+ * instead of waiting. Callback completion and tool re-execution are covered+ * deterministically by `entry-authorization.integration.test.ts`.*/export default defineEval({- description: "An ordinary message runs while an authorization challenge stays open.",+ description: "A message from the same person steers a turn held on sign-in and cancels it.",async test(t) {- const parked = await t.send(+ const held = await t.send('Call the auth-probe tool exactly once with marker "nonblocking". Include its result.',);- const session = parked.session;- parked.event("authorization.required", { count: 1 });- parked.notEvent("authorization.completed");- parked.event("session.waiting", { count: 1 });+ const session = held.session;+ held.event("authorization.required", { count: 1 });+ held.notEvent("authorization.completed");+ held.event("turn.waiting", { count: 1 });+ held.notEvent("session.waiting");- const message = await session.send(- "Do not call any tools. Reply with exactly AUTH-OPEN-MESSAGE-OK.",+ const steered = await session.send(+ "Never mind the probe. Do not call any tools. Reply with exactly AUTH-OPEN-MESSAGE-OK.",);- message.expectOk();- if (message.sessionId !== parked.sessionId) {- throw new Error("Message while authorization was open changed session identity.");+ steered.expectOk();+ if (steered.sessionId !== held.sessionId) {+ throw new Error("Steering the held turn changed session identity.");}- message.event("message.received", { count: 1 });- message.event("message.completed", { count: 1 });- message.notEvent("authorization.completed");- message.event("session.waiting", { count: 1 });- message.messageIncludes("AUTH-OPEN-MESSAGE-OK");- message.usedNoTools();+ steered.event("authorization.completed", { count: 1, data: { outcome: "declined" } });+ steered.notEvent("turn.started");+ steered.event("turn.completed", { count: 1 });+ steered.event("session.waiting", { count: 1 });+ steered.messageIncludes("AUTH-OPEN-MESSAGE-OK");},});diff --git a/e2e/fixtures/agent-tools-hitl/evals/auth/authorization-principal-after-other-speaker.eval.ts b/e2e/fixtures/agent-tools-hitl/evals/auth/authorization-principal-after-other-speaker.eval.tsindex e349bfc69..a29dd0d5f 100644--- a/e2e/fixtures/agent-tools-hitl/evals/auth/authorization-principal-after-other-speaker.eval.ts+++ b/e2e/fixtures/agent-tools-hitl/evals/auth/authorization-principal-after-other-speaker.eval.ts@@ -5,13 +5,12 @@ const BOB = "principal-binding-bob";const as = (principalId: string) => ({ headers: { "x-eve-fixture-user": principalId } });/**- * Alice starts a sign-in, then Bob posts in the same session before Alice- * finishes. Bob is now the session's latest caller, so a channel that picks- * the recipient from the current caller would send Alice's challenge or- * completion status to Bob. Both events name Alice instead.+ * Alice starts a sign-in, which holds her turn. Bob posts in the same session+ * before Alice finishes, so his message queues behind her held turn. Alice's+ * completion resumes her turn as Alice, and Bob's message runs after it.*/export default defineEval({- description: "A sign-in stays bound to the person who started it after someone else speaks.",+ description: "A sign-in holds Alice's turn; Bob's message waits behind it and runs after.",async test(t) {const started = await t.send('Call the auth-probe tool exactly once with marker "principal-binding".',@@ -19,7 +18,8 @@ export default defineEval({);const session = started.session;started.event("authorization.required", { count: 1, data: { principalId: ALICE } });- started.event("session.waiting", { count: 1 });+ started.event("turn.waiting", { count: 1 });+ started.notEvent("session.waiting");const required = started.events.find((event) => event.type === "authorization.required");if (required?.type !== "authorization.required" ||@@ -27,24 +27,26 @@ export default defineEval({)throw new Error("Expected Alice's sign-in challenge URL.");- const bob = await session.send(+ const resumed = t.target.watchTurn(session.sessionId, {+ startIndex: session.state?.streamIndex,+ });+ const bob = await session.start("Do not call any tools. Reply with exactly BOB-STATUS-OK.",as(BOB),);- bob.expectOk();- bob.messageIncludes("BOB-STATUS-OK");- bob.notEvent("authorization.required");- bob.notEvent("authorization.completed");-- const callbackTurn = t.target.watchTurn(session.sessionId, {- startIndex: session.state?.streamIndex,- });const callback = await fetch(new URL(required.data.authorization.url));if (!callback.ok) throw new Error(`Fixture sign-in callback failed (${callback.status}).`);- const completed = await callbackTurn.result();++ const completed = await resumed.result();completed.event("authorization.completed", {count: 1,data: { attemptId: required.data.attemptId, outcome: "authorized", principalId: ALICE },});+ completed.notEvent("turn.started");++ const bobTurn = await bob.result();+ bobTurn.expectOk();+ bobTurn.messageIncludes("BOB-STATUS-OK");+ bobTurn.notEvent("authorization.required");},});diff --git a/e2e/fixtures/agent-tools-hitl/evals/hitl/approval-response-settlement.eval.ts b/e2e/fixtures/agent-tools-hitl/evals/hitl/approval-response-settlement.eval.tsindex 6fc6ea6dd..e24d6b4a2 100644--- a/e2e/fixtures/agent-tools-hitl/evals/hitl/approval-response-settlement.eval.ts+++ b/e2e/fixtures/agent-tools-hitl/evals/hitl/approval-response-settlement.eval.ts@@ -45,16 +45,14 @@ async function expectAnswerable(callId: string,requestId: string,) {- await t.require(store.snapshot.status, equals("ready"));+ // The approval holds its turn open, and the store keeps following it live.+ await t.require(store.snapshot.status, equals("streaming"));await t.require(store.snapshot.error, equals(undefined));const part = toolPart(store, callId);await t.require(part.state, equals("approval-requested"));await t.require(part.approval?.id, equals(requestId));await t.require(part.toolMetadata?.eve?.inputRequest?.requestId, equals(requestId));await t.require(part.toolMetadata?.eve?.inputResponse, equals(undefined));- const cancel = await settles("Idle store cancellation", store.cancel());- await t.require(cancel.status, equals("no_active_turn"));- await t.require(store.snapshot.status, equals("ready"));}async function refuse(t: EveEvalContext, store: Store, callId: string, requestId: string) {@@ -76,13 +74,14 @@ async function refuse(t: EveEvalContext, store: Store, callId: string, requestIdequals(["pending", "rejected"]),);await t.require(candidates.at(-1)?.data.reason, equals("Wrong responder."));+ // The approval holds the turn, so a refused answer leaves it held.await t.require(events- .filter((event) => event.type === "approval.candidate" || event.type === "session.waiting")+ .filter((event) => event.type === "approval.candidate" || event.type === "turn.waiting").map((event) => event.type),- equals(["approval.candidate", "approval.candidate", "session.waiting"]),+ equals(["approval.candidate", "approval.candidate", "turn.waiting"]),);- await t.require(events.at(-1)?.type, equals("session.waiting"));+ await t.require(events.at(-1)?.type, equals("turn.waiting"));await t.require(events.filter((event) =>diff --git a/e2e/fixtures/agent-tools-hitl/evals/hitl/authored-always-unrelated-input.eval.ts b/e2e/fixtures/agent-tools-hitl/evals/hitl/authored-always-unrelated-input.eval.tsindex 8a369d9bc..4dc67c692 100644--- a/e2e/fixtures/agent-tools-hitl/evals/hitl/authored-always-unrelated-input.eval.ts+++ b/e2e/fixtures/agent-tools-hitl/evals/hitl/authored-always-unrelated-input.eval.ts@@ -3,15 +3,20 @@ import { defineEval } from "eve/evals";const MARKER = "authored-always-unrelated-input-P7M2";const TOOL_NAME = "gate";-/** Regression reproduction for https://github.com/vercel/eve/issues/533. */+/**+ * Regression reproduction for https://github.com/vercel/eve/issues/533: an+ * unrelated message must never replay the unresolved authored tool call. The+ * approval holds the turn, so the message steers it and cancels the approval;+ * the call is resolved as ignored and never runs.+ */export default defineEval({tags: ["real-model"],description:- "HITL repro (#533): unrelated input does not replay an unresolved authored tool call.",+ "HITL repro (#533): unrelated input cancels, and never replays, an unresolved authored tool call.",async test(t) {- const parked = await t.send(`Call the \`${TOOL_NAME}\` tool with marker "${MARKER}".`);- const session = parked.session;- parked.calledTool(TOOL_NAME, { status: "pending", count: 1 });+ const held = await t.send(`Call the \`${TOOL_NAME}\` tool with marker "${MARKER}".`);+ const session = held.session;+ held.calledTool(TOOL_NAME, { status: "pending", count: 1 });const approval = session.requireInputRequest({display: "confirmation",toolName: TOOL_NAME,@@ -21,33 +26,21 @@ export default defineEval({"Note this unrelated marker and do not call any tools: ORBITAL-PINE-6C3R.",);- // The unrelated message runs as an ordinary turn; the point of #533 is- // that it must not replay or resolve the unresolved authored tool call.unrelated.expectOk();- unrelated.notEvent("action.result", {- data: { result: { toolName: TOOL_NAME } },- });- unrelated.event("session.waiting", { count: 1 });-- const approved = await session.respond([- {- optionId: "approve",- requestId: approval.requestId,- },- ]);-- approved.expectOk();- approved.event("action.result", {+ unrelated.event("input.resolved", {+ count: 1,data: {- result: {- kind: "tool-result",- output: new RegExp(MARKER),- toolName: TOOL_NAME,- },- status: "completed",+ resolutions: (resolutions) =>+ resolutions.some(+ (resolution) =>+ resolution.requestId === approval.requestId && resolution.outcome === "ignored",+ ),},- count: 1,});+ unrelated.notEvent("action.result", {+ data: { result: { output: new RegExp(MARKER), toolName: TOOL_NAME } },+ });+ unrelated.event("session.waiting", { count: 1 });t.succeeded();},});diff --git a/e2e/fixtures/agent-tools-hitl/evals/hitl/authorized-response-oauth-cancel.eval.ts b/e2e/fixtures/agent-tools-hitl/evals/hitl/authorized-response-oauth-cancel.eval.tsindex 06b2f4bdf..ca2905bf2 100644--- a/e2e/fixtures/agent-tools-hitl/evals/hitl/authorized-response-oauth-cancel.eval.ts+++ b/e2e/fixtures/agent-tools-hitl/evals/hitl/authorized-response-oauth-cancel.eval.ts@@ -19,14 +19,12 @@ export default defineEval({{ headers: { "x-eve-fixture-user": "oauth-cancel-responder" } },);const required = await approvalTurn.waitForEvent("authorization.required");+ // The responder's sign-in holds the turn, so this read stops there.+ const held = await approvalTurn.result();- const cancelTurn = await approvalTurn.session.startRespond([+ const cancelled = await held.session.respond([{ optionId: "cancel", requestId: approval.requestId },]);- await cancelTurn.waitForEvent("approval.settled", {- data: { outcome: "cancelled", requestId: approval.requestId },- });- const cancelled = await cancelTurn.result();cancelled.event("approval.settled", {count: 1,data: { outcome: "cancelled", requestId: approval.requestId },@@ -46,7 +44,7 @@ export default defineEval({if (!callback.ok)throw new Error(`Late fixture OAuth callback failed (${String(callback.status)}).`);- const late = await cancelTurn.session.send("Confirm the cancelled action did not execute.");+ const late = await cancelled.session.send("Confirm the cancelled action did not execute.");late.notEvent("approval.settled", { data: { outcome: "approved" } });late.notEvent("action.result", {data: { result: { kind: "tool-result", toolName: TOOL_NAME }, status: "completed" },diff --git a/e2e/fixtures/agent-tools-hitl/evals/hitl/authorized-response-oauth-nonblocking.eval.ts b/e2e/fixtures/agent-tools-hitl/evals/hitl/authorized-response-oauth-nonblocking.eval.tsindex db10ee98b..91f362d76 100644--- a/e2e/fixtures/agent-tools-hitl/evals/hitl/authorized-response-oauth-nonblocking.eval.ts+++ b/e2e/fixtures/agent-tools-hitl/evals/hitl/authorized-response-oauth-nonblocking.eval.ts@@ -4,12 +4,13 @@ const MARKER = "authorized-response-oauth-nonblocking-K3T9";const TOOL_NAME = "oauth-authorized-gate";const REQUESTER = "oauth-nonblocking-requester";const RESPONDER = "oauth-nonblocking-responder";+const BYSTANDER = "oauth-nonblocking-bystander";const as = (principalId: string) => ({ headers: { "x-eve-fixture-user": principalId } });export default defineEval({tags: ["real-model"],description:- "A message runs while candidate OAuth stays open, then OAuth settles the approval. The sign-in names the responder, not the requester who speaks while it waits.",+ "Carol's message waits while the responder's OAuth holds the turn, then OAuth settles the approval as the responder, not the latest speaker, and Carol's message runs.",async test(t) {const { session: conversation } = await t.send(`Call the \`${TOOL_NAME}\` tool with marker "${MARKER}".`,@@ -24,17 +25,16 @@ export default defineEval({as(RESPONDER),);const required = await approvalTurn.waitForEvent("authorization.required");+ // The responder's sign-in holds the turn, so this read stops there.+ const held = await approvalTurn.result();+ const resumeIndex = held.session.state.streamIndex;- const message = await approvalTurn.session.send(+ // Someone other than the requester speaks while the sign-in is open: the+ // message waits for the held turn instead of steering it.+ const queued = await held.session.start("Do not call tools. Reply with exactly CANDIDATE-OAUTH-OPEN-OK.",- as(REQUESTER),+ as(BYSTANDER),);- message.expectOk();- message.messageIncludes("CANDIDATE-OAUTH-OPEN-OK");- message.notEvent("approval.settled");- message.notEvent("action.result", {- data: { result: { kind: "tool-result", toolName: TOOL_NAME }, status: "completed" },- });if (required.type !== "authorization.required" ||@@ -48,10 +48,7 @@ export default defineEval({);}const callbackUrl = new URL(required.data.authorization.url);- const callbackTurn = t.target.watchTurn(approvalTurn.sessionId, {- startIndex: conversation.state?.streamIndex,- });- await new Promise((resolve) => setTimeout(resolve, 250));+ const callbackTurn = t.target.watchTurn(held.sessionId, { startIndex: resumeIndex });const callback = await fetch(callbackUrl);if (!callback.ok)throw new Error(`Fixture OAuth callback failed (${String(callback.status)}).`);@@ -71,6 +68,14 @@ export default defineEval({status: "completed",},});+ resumed.notEvent("message.received", { data: { message: /CANDIDATE-OAUTH-OPEN-OK/ } });++ const message = await queued.result();+ message.expectOk();+ message.messageIncludes("CANDIDATE-OAUTH-OPEN-OK");+ message.notEvent("action.result", {+ data: { result: { kind: "tool-result", toolName: TOOL_NAME }, status: "completed" },+ });t.succeeded();},});diff --git a/e2e/fixtures/agent-tools-hitl/evals/hitl/authorized-response-oauth.eval.ts b/e2e/fixtures/agent-tools-hitl/evals/hitl/authorized-response-oauth.eval.tsindex 4b3b7dafa..e08849251 100644--- a/e2e/fixtures/agent-tools-hitl/evals/hitl/authorized-response-oauth.eval.ts+++ b/e2e/fixtures/agent-tools-hitl/evals/hitl/authorized-response-oauth.eval.ts@@ -27,6 +27,18 @@ export default defineEval({if (callbackUrl.origin !== new URL(t.target.url).origin) {throw new Error("Fixture OAuth callback targeted an unexpected origin.");}+ // The responder's sign-in holds the turn, so this read stops there; the+ // callback resumes the same turn.+ const held = await approvalTurn.result();+ held.event("approval.candidate", {+ data: { outcome: "pending", requestId: approval.requestId },+ count: 1,+ });+ held.event("authorization.required", { count: 1 });+ held.event("turn.waiting", { data: { on: "input" } });+ const resumedTurn = t.target.watchTurn(held.sessionId, {+ startIndex: held.session.state.streamIndex,+ });const callback = await fetch(callbackUrl);if (!callback.ok) {throw new Error(@@ -34,12 +46,7 @@ export default defineEval({);}- const resumed = await approvalTurn.result();- resumed.event("approval.candidate", {- data: { outcome: "pending", requestId: approval.requestId },- count: 1,- });- resumed.event("authorization.required", { count: 1 });+ const resumed = await resumedTurn.result();resumed.expectOk();resumed.event("authorization.completed", {data: { candidateId: required.data.candidateId, outcome: "authorized" },diff --git a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/README.md b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/README.mdindex 6869ee1ee..80a410484 100644--- a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/README.md+++ b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/README.md@@ -1,12 +1,14 @@# User-message continuation-Every accepted user message must reach its own answer, required input request, or explicit failure/cancellation. An older unanswered request cannot silently stop that message's work.+Every accepted user message must reach its own answer, required input request, or explicit failure/cancellation. An unanswered request cannot silently stop that message's work.++A sign-in or tool approval holds its turn open until it is answered. A message from the same person steers the held turn: it cancels the requests still waiting (they resolve as `ignored` and their calls never run) and the turn carries on with the message. Answers already given, as in a partial approval, keep their effect. This holds whatever `turnPolicy` the client sends, because a queued message would wait on a turn that waits on that person.This directory contains **37 scripted evals: 23 regressions and 14 controls**. They exercise the HTTP session, approval, tool, workflow, and event-stream paths. A deterministic model chooses calls and constructs answers from the results it receives. There is no model judge and no manually seeded pending state.These sessions select the scripted model through a fixture-only header in **every** CI world, including Local. The sibling [`pending-approval-tool-followup.eval.ts`](../pending-approval-tool-followup.eval.ts) adds two cases using the CI-selected model: real models in Local, the fixture mock in Postgres and Vercel. Passing scripted cases does not establish live-model coverage of the whole matrix.-Start with [`read.eval.ts`](./read.eval.ts): leave a change's approval pending, ask for a draft status, require its answer, then approve the saved change. Other cases vary the pending requests, delivery order, or result type.+Start with [`read.eval.ts`](./read.eval.ts): leave a change's approval pending, ask for a draft status, require its answer, then check that the message cancelled the change. Other cases vary the pending requests, delivery order, or result type.## Scenario syntax and classification@@ -16,34 +18,34 @@ The tags are filters, not expected verdicts: every case must pass. `eve eval --t## What makes a passing answer-[`expectReply`](./helpers.ts) requires exactly one matching `message.completed`, followed by exactly one `turn.completed`, both attributed to the same turn. A tool result, unrelated reply, or completion without an answer cannot pass. New messages use their own `message.received` turn ID. Approval responses require successful resolution of the saved request before its matching reply and completion in the delivery's resumed turn. Multiple responses accepted together can resolve across several steps of that one turn; a later resolution need not emit another `turn.started`.+[`expectReply`](./helpers.ts) requires exactly one matching `message.completed`, followed by exactly one `turn.completed`, both attributed to the same turn. A tool result, unrelated reply, or completion without an answer cannot pass. New messages use their own `message.received` turn ID. Approval responses require successful resolution of the saved request before its matching reply and completion in the held turn, with no new `turn.started`. A message that steers a held turn joins that turn, so its `message.received` carries the held turn's ID.-Each approval ID is saved when emitted. The driver's latest-turn request list is not treated as durable pending state. Tests check that unrelated replies do not execute the old change, then generally approve the saved request and require its execution and completed reply. Budget cases require the next budget request instead of an answer beyond the granted limit.+Each approval ID is saved when emitted. The driver's latest-turn request list is not treated as durable pending state. Tests check that a steering message cancels the saved request (`ignored`, with a not-run result) and that the change never executes. Budget cases require the next budget request instead of an answer beyond the granted limit.## RegressionsEach row maps to exactly one eval. “Response authorization” here means the fixture checks the authenticated responder's principal; it does not exercise an OAuth callback.-| Conversation | Required outcome | Eval |-| ------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------- | -------------------------------------------------------------------- |-| Read while A waits | Report returned status; A remains answerable | [read](./read.eval.ts) |-| Write while A waits | Confirm exactly one write; A remains answerable | [write](./write.eval.ts) |-| Parallel read and write while A waits | Report both results | [parallel tools](./parallel-tools.eval.ts) |-| Tool throws while A waits | Explain the actual error | [tool error](./tool-error.eval.ts) |-| Invalid tool input while A waits | Correct input and report the result | [invalid input](./invalid-input.eval.ts) |-| Response-authorized approval waits | Finish an unrelated read | [authorized pending](./authorized-pending.eval.ts) |-| Approve newer B while A waits | Execute B, read, and reply | [approve sibling](./approve-sibling.eval.ts) |-| Cancel newer B while A waits | Leave B unexecuted, read, and reply | [cancel sibling](./cancel-sibling.eval.ts) |-| Approve newer response-authorized request while A waits | Settle authorization, read, and reply | [authorized sibling](./authorized-sibling.eval.ts) |-| Approve older response-authorized request while newer A waits | Read and reply; A remains answerable | [authorized older sibling](./authorized-older-sibling.eval.ts) |-| Approve older ordinary A while a newer response-authorized request waits | Reply for A; newer request remains answerable | [ordinary older sibling](./ordinary-older-sibling.eval.ts) |-| Workflow completes while A waits | Interpret its result | [workflow result](./workflow-result.eval.ts) |-| Provider supplies a tool result while A waits | Report the distinct provider result | [provider result](./provider-result.eval.ts) |-| Submit A from a same-batch A+B pair, then request a read | Complete the read; later B executes both changes once and completes a reply | [partial approval, tool](./partial-approval-tool.eval.ts) |-| Submit A from a same-batch A+B pair, then request text only | Complete a reply without tools; later B executes both changes once and completes a reply | [partial approval, text](./partial-approval-text.eval.ts) |-| Repeat a resolved response while A waits | Process the new input without authorizing A | [stale response](./stale-response.eval.ts) |-| Grant another budget window while A waits | Run one tool, then request the next required grant | [budget grant](./budget-grant.eval.ts) |-| Submit A and B from one batch in separate deliveries | Accumulate both responses, execute both once, and reply | [separate approval responses](./separate-approval-responses.eval.ts) |+| Conversation | Required outcome | Eval |+| ------------------------------------------------------------------ | --------------------------------------------------------- | -------------------------------------------------------------------- |+| Read while A waits | Report returned status; A is cancelled | [read](./read.eval.ts) |+| Write while A waits | Confirm exactly one write; A is cancelled | [write](./write.eval.ts) |+| Parallel read and write while A waits | Report both results | [parallel tools](./parallel-tools.eval.ts) |+| Tool throws while A waits | Explain the actual error | [tool error](./tool-error.eval.ts) |+| Invalid tool input while A waits | Correct input and report the result | [invalid input](./invalid-input.eval.ts) |+| Response-authorized approval waits | Finish an unrelated read | [authorized pending](./authorized-pending.eval.ts) |+| Approve newer B while A waits | Execute B, read, and reply | [approve sibling](./approve-sibling.eval.ts) |+| Cancel newer B while A waits | Leave B unexecuted, read, and reply | [cancel sibling](./cancel-sibling.eval.ts) |+| Approve newer response-authorized request while A waits | Settle authorization, read, and reply | [authorized sibling](./authorized-sibling.eval.ts) |+| Ask for A while a response-authorized request holds the turn | The request is cancelled; approving A runs it and replies | [authorized older sibling](./authorized-older-sibling.eval.ts) |+| Answer cancelled A while a newer response-authorized request holds | The stale answer steers like a message; nothing runs | [ordinary older sibling](./ordinary-older-sibling.eval.ts) |+| Workflow completes while A waits | Interpret its result | [workflow result](./workflow-result.eval.ts) |+| Provider supplies a tool result while A waits | Report the distinct provider result | [provider result](./provider-result.eval.ts) |+| Submit A from a same-batch A+B pair, then request a read | A runs once, B is cancelled, and the read completes | [partial approval, tool](./partial-approval-tool.eval.ts) |+| Submit A from a same-batch A+B pair, then request text only | A runs once, B is cancelled, and the reply completes | [partial approval, text](./partial-approval-text.eval.ts) |+| Repeat a resolved response while A waits | Process the new input without authorizing A | [stale response](./stale-response.eval.ts) |+| Grant another budget window while A waits | Run one tool, then request the next required grant | [budget grant](./budget-grant.eval.ts) |+| Submit A and B from one batch in separate deliveries | Accumulate both responses, execute both once, and reply | [separate approval responses](./separate-approval-responses.eval.ts) |## Controls@@ -57,14 +59,14 @@ Each row maps to exactly one eval. “Response authorization” here means the f| Workflow without older input | Interpret its result | [workflow result](./workflow-result.control.eval.ts) || Provider result without older input | Report distinct provider result | [provider result](./provider-result.control.eval.ts) || Budget renewal without older approval | Run one tool, then request next grant | [budget grant](./budget-grant.control.eval.ts) |-| Text-only message while A waits | Reply without tools; A remains answerable | [text only](./text-only.control.eval.ts) |+| Text-only message while A waits | Reply without new tool calls; A is cancelled | [text only](./text-only.control.eval.ts) || Resolve the only approval | Execute, read, and reply | [resolve only approval](./resolve-only-approval.control.eval.ts) || Approve both calls together | Execute each once and reply | [approve both](./approve-both.control.eval.ts) || Workflow finishes beside an approval from the same turn | Observe workflow completion before approval; require approval before final reply | [same-turn workflow](./same-turn-workflow.control.eval.ts) |## Evidence boundaries-The [adversarial integration probes](../../../../../../packages/eve/src/harness/issue-3494-adversarial.integration.test.ts) also cover `final_output` with an older approval, multiple independent approvals plus an internally deferred message, and a complete independent batch beside a partially answered batch. Those exact scenarios are **integration-only**, not extra E2E cases. Integration workflow results are injected at runtime boundaries; the E2E cases execute fixture tools through the durable runtime.+The [adversarial integration probes](../../../../../../packages/eve/src/harness/issue-3494-adversarial.integration.test.ts) cover the same held-turn rules at the harness boundary, plus a responder's sign-in expiring and a message waiting behind a budget prompt. Integration workflow results are injected at runtime boundaries; the E2E cases execute fixture tools through the durable runtime.Partial approvals in E2E use an accepted HTTP response followed by a separate user message; the API rejects combined message/response payloads. The provider cases supply a provider-executed result at the scripted model stream boundary; they do not contact a provider that performs the tool. Budget scripts report synthetic token usage against the fixture's one-million-output-token limit.diff --git a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/approve-sibling.eval.ts b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/approve-sibling.eval.tsindex 51cf21ec3..ae84361d9 100644--- a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/approve-sibling.eval.ts+++ b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/approve-sibling.eval.ts@@ -1,7 +1,7 @@import { defineEval } from "eve/evals";import {+ expectApprovalCancelled,scriptedSession,- approveSavedChange,expectChangeStillUnexecuted,expectResponseReply,expectToolResult,@@ -26,11 +26,11 @@ export default defineEval({{ requestId: current.requestId, optionId: "approve" },]);- // Then B executes once and the read gets a completed reply; A stays unexecuted and answerable.+ // Then B executes once and the read gets a completed reply; the message cancelled A, which never runs.await expectToolResult(t, live, "read-draft");const reply = await expectResponseReply(t, live, "Draft status: ready.", current.requestId);reply.calledTool("change-b", { status: "completed", output: { executions: 1 }, count: 1 });expectChangeStillUnexecuted(session);- await approveSavedChange(t, session, approvalA);+ expectApprovalCancelled(session, approvalA);},});diff --git a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/authorized-older-sibling.eval.ts b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/authorized-older-sibling.eval.tsindex a91e91fe6..df93f7831 100644--- a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/authorized-older-sibling.eval.ts+++ b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/authorized-older-sibling.eval.ts@@ -1,47 +1,34 @@import { defineEval } from "eve/evals";import {scriptedSession,- approveSavedChange,- expectChangeStillUnexecuted,+ expectApprovalCancelled,expectResponseReply,- expectToolResult,requestFrom,} from "./helpers.ts";export default defineEval({description:- "Approving the older response-authorized request completes its read and reply while newer approval A stays answerable.",+ "Asking for change A cancels a held response-authorized change, and approving A still completes.",tags: ["hitl", "continuation", "regression", "input-response", "authorization"],timeoutMs: 60_000,async test(t) {- // Given a response-authorized change is pending before an ordinary approval A.+ // Given a response-authorized change holds the turn.const first = await t.send("Prepare an authorized change, then read the draft status.",scriptedSession,);- const current = requestFrom(first, "authorized-change");+ const authorized = requestFrom(first, "authorized-change");const session = first.session;+ // When the user asks for change A instead, which steers the held turn.const second = await session.send("Prepare change A.");const approvalA = requestFrom(second, "change-a");-- // When the authorized responder approves the older change.+ // Then the authorized change is cancelled and never runs.+ expectApprovalCancelled(session, authorized);+ // And approving A runs it once and completes the held turn.const live = await session.startRespond([- { requestId: current.requestId, optionId: "approve" },+ { requestId: approvalA.requestId, optionId: "approve" },]);-- // Then authorization settles, the change executes once, and the read gets a reply; A stays answerable.- await expectToolResult(t, live, "read-draft");- const reply = await expectResponseReply(t, live, "Draft status: ready.", current.requestId);- reply.calledTool("authorized-change", {- status: "completed",- output: { executions: 1 },- count: 1,- });- reply.event("approval.settled", {- data: { requestId: current.requestId, outcome: "approved" },- count: 1,- });- expectChangeStillUnexecuted(session);- await approveSavedChange(t, session, approvalA);+ const reply = await expectResponseReply(t, live, "Change A resolved.", approvalA.requestId);+ reply.calledTool("change-a", { status: "completed", output: { executions: 1 }, count: 1 });},});diff --git a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/authorized-pending.eval.ts b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/authorized-pending.eval.tsindex f200d2334..33e273a87 100644--- a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/authorized-pending.eval.ts+++ b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/authorized-pending.eval.ts@@ -1,7 +1,7 @@import { defineEval } from "eve/evals";import {+ expectApprovalCancelled,scriptedSession,- approveSavedChange,expectChangeStillUnexecuted,expectReply,expectToolResult,@@ -22,10 +22,10 @@ export default defineEval({// When the user asks to read the draft.const live = await session.start("Read the draft status.");- // Then the reply reports ready and completes; the authorized change stays answerable.+ // Then the reply reports ready and completes; the message cancelled the authorized change, which never runs.await expectToolResult(t, live, "read-draft");await expectReply(t, live, "Draft status: ready.");expectChangeStillUnexecuted(session, "authorized-change");- await approveSavedChange(t, session, approval);+ expectApprovalCancelled(session, approval);},});diff --git a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/authorized-sibling.eval.ts b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/authorized-sibling.eval.tsindex e0b6e0dfc..441e1a8ea 100644--- a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/authorized-sibling.eval.ts+++ b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/authorized-sibling.eval.ts@@ -1,7 +1,7 @@import { defineEval } from "eve/evals";import {+ expectApprovalCancelled,scriptedSession,- approveSavedChange,expectChangeStillUnexecuted,expectResponseReply,expectToolResult,@@ -26,7 +26,7 @@ export default defineEval({{ requestId: current.requestId, optionId: "approve" },]);- // Then authorization settles, the change executes once, and the read gets a reply; A stays answerable.+ // Then authorization settles, the change executes once, and the read gets a reply; the message cancelled A, which never runs.await expectToolResult(t, live, "read-draft");const reply = await expectResponseReply(t, live, "Draft status: ready.", current.requestId);reply.calledTool("authorized-change", {@@ -39,6 +39,6 @@ export default defineEval({count: 1,});expectChangeStillUnexecuted(session);- await approveSavedChange(t, session, approvalA);+ expectApprovalCancelled(session, approvalA);},});diff --git a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/cancel-sibling.eval.ts b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/cancel-sibling.eval.tsindex a63a22432..bf371b7f9 100644--- a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/cancel-sibling.eval.ts+++ b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/cancel-sibling.eval.ts@@ -1,7 +1,7 @@import { defineEval } from "eve/evals";import {+ expectApprovalCancelled,scriptedSession,- approveSavedChange,expectChangeStillUnexecuted,expectResponseReply,expectToolResult,@@ -24,13 +24,13 @@ export default defineEval({// When the user cancels B only.const live = await session.startRespond([{ requestId: current.requestId, optionId: "cancel" }]);- // Then B does not execute and the read gets a completed reply; A stays unexecuted and answerable.+ // Then B does not execute and the read gets a completed reply; the message cancelled A, which never runs.await expectToolResult(t, live, "read-draft");const reply = await expectResponseReply(t, live, "Draft status: ready.", current.requestId);reply.notEvent("action.result", {data: { status: "completed", result: { toolName: "change-b" } },});expectChangeStillUnexecuted(session);- await approveSavedChange(t, session, approvalA);+ expectApprovalCancelled(session, approvalA);},});diff --git a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/helpers.ts b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/helpers.tsindex 05fb398b2..e2e03fcf3 100644--- a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/helpers.ts+++ b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/helpers.ts@@ -64,13 +64,14 @@ export async function expectResponseReply(expected: string | RegExp,requestId: string,): Promise<EveEvalTurn> {- t.log(`Accepted response for ${requestId}; awaiting resolution and its resumed turn.`);- await live.waitForEvent("input.resolved", {+ t.log(`Accepted response for ${requestId}; awaiting resolution and the held turn's reply.`);+ const resolved = await live.waitForEvent("input.resolved", {data: { resolutions: (items) => items.some((item) => item.requestId === requestId) },});- // Several responses in one delivery can resolve during the same resumed turn.- const resumed = await live.waitForEvent("turn.started");- const turn = await expectReply(t, live, expected, resumed.data.turnId);+ // The approval held its turn, so the answer resumes that turn instead of starting one.+ const turnId = resolved.data.turnId;+ const turn = await expectReply(t, live, expected, turnId);+ turn.notEvent("turn.started");turn.eventOrder([{type: "input.resolved",@@ -85,24 +86,31 @@ export async function expectResponseReply(},count: 1,},- {- type: "message.completed",- data: { turnId: resumed.data.turnId, message: expected },- count: 1,- },- { type: "turn.completed", data: { turnId: resumed.data.turnId }, count: 1 },- ]);- turn.eventOrder([- { type: "turn.started", data: { turnId: resumed.data.turnId }, count: 1 },- {- type: "message.completed",- data: { turnId: resumed.data.turnId, message: expected },- count: 1,- },+ { type: "message.completed", data: { turnId, message: expected }, count: 1 },+ { type: "turn.completed", data: { turnId }, count: 1 },]);return turn;}+/**+ * A message steered the turn held on this approval, which cancels it: the+ * request resolves as ignored and its call never runs.+ */+export function expectApprovalCancelled(session: EveEvalSession, request: InputRequest) {+ session.event("input.resolved", {+ data: {+ resolutions: (items) =>+ items.some((item) => item.requestId === request.requestId && item.outcome === "ignored"),+ },+ count: 1,+ });+ session.event("action.result", {+ data: { status: "rejected", result: { toolName: request.action.toolName } },+ count: 1,+ });+ expectChangeStillUnexecuted(session, request.action.toolName);+}+export async function expectToolResult(t: EveEvalContext, live: EveEvalLiveTurn, toolName: string) {t.log(`Accepted input in ${live.sessionId}; awaiting ${toolName}.`);const event = await live.waitForEvent("action.result", { data: { result: { toolName } } });@@ -111,30 +119,25 @@ export async function expectToolResult(t: EveEvalContext, live: EveEvalLiveTurn,}export function expectChangeStillUnexecuted(session: EveEvalSession, toolName = "change-a") {- session.notEvent("action.result", { data: { result: { toolName } } });+ session.notEvent("action.result", { data: { status: "completed", result: { toolName } } });}// A partial approval has no turn boundary to await. Await the real HTTP// acceptance, then send the next message on that same session's ordered inbox.+/**+ * Approves one request of a batch. The rest of the batch is still open, so the+ * turn stays held and says so again.+ */export async function submitPartialApproval(t: EveEvalContext,session: EveEvalSession,request: InputRequest,) {- const response = await t.target.fetch(- `/eve/v1/session/${encodeURIComponent(session.sessionId)}`,- {- method: "POST",- headers: { "content-type": "application/json" },- signal: t.signal,- body: JSON.stringify({- inputResponses: [{ requestId: request.requestId, optionId: "approve" }],- }),- },- );- await t.require(response.status, equals(202));- const accepted = await response.json();- t.log(`Partial approval accepted: ${JSON.stringify(accepted)}`);+ const held = await session.respond([{ requestId: request.requestId, optionId: "approve" }]);+ t.log(`Partial approval accepted for ${request.requestId}; the turn is still held.`);+ held.event("turn.waiting", { data: { on: "input" }, count: 1 });+ held.notEvent("input.resolved");+ held.notEvent("turn.started");}export async function approveSavedChange(diff --git a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/invalid-input.eval.ts b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/invalid-input.eval.tsindex 580c3fa5c..8537edb98 100644--- a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/invalid-input.eval.ts+++ b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/invalid-input.eval.ts@@ -1,7 +1,7 @@import { defineEval } from "eve/evals";import {+ expectApprovalCancelled,scriptedSession,- approveSavedChange,expectChangeStillUnexecuted,expectReply,requestFrom,@@ -23,8 +23,9 @@ export default defineEval({"Try a numeric draft ID, then correct it and read the status.",);- // Then the model corrects the input and reports ready plus the validation error; A stays answerable.- const rejectedStep = await live.waitForEvent("step.completed", { data: { stepIndex: 0 } });+ // Then the model corrects the input and reports ready plus the validation error; the message cancelled A, which never runs.+ // The message steers the held turn, so its first step continues that turn's numbering.+ const rejectedStep = await live.waitForEvent("step.completed");t.log(`Initial tool-call step completed; awaiting correction: ${JSON.stringify(rejectedStep.data)}`,);@@ -35,6 +36,6 @@ export default defineEval({count: 1,});expectChangeStillUnexecuted(session);- await approveSavedChange(t, session, approval);+ expectApprovalCancelled(session, approval);},});diff --git a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/ordinary-older-sibling.eval.ts b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/ordinary-older-sibling.eval.tsindex ada67dd15..ccc59d4ff 100644--- a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/ordinary-older-sibling.eval.ts+++ b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/ordinary-older-sibling.eval.ts@@ -1,42 +1,34 @@import { defineEval } from "eve/evals";+import { equals } from "eve/evals/expect";import {- approveSavedChange,+ expectApprovalCancelled,expectChangeStillUnexecuted,- expectResponseReply,requestFrom,scriptedSession,} from "./helpers.ts";export default defineEval({description:- "An older ordinary approval completes while a newer response-authorized approval stays answerable.",+ "Answering an approval that steering cancelled runs nothing and steers the turn like a message.",tags: ["hitl", "continuation", "regression", "input-response", "approval", "authorization"],timeoutMs: 60_000,async test(t) {- // Given A awaits approval and a newer authorized change also awaits approval.+ // Given asking for an authorized change cancelled A's held approval.const first = await t.send("Prepare change A.", scriptedSession);const approvalA = requestFrom(first, "change-a");const session = first.session;const second = await session.send("Prepare an authorized change, then read the draft status.");const newer = requestFrom(second, "authorized-change");-- // When the user resolves the older request first.- const live = await session.startRespond([- { requestId: approvalA.requestId, optionId: "approve" },- ]);- const resolved = await expectResponseReply(t, live, "Change A resolved.", approvalA.requestId);-- // Then A executes once, and the newer request remains answerable.- resolved.calledTool("change-a", {- status: "completed",- output: { executions: 1 },- count: 1,- });- resolved.event("input.resolved", {- data: { resolutions: [{ requestId: approvalA.requestId }] },- count: 1,- });- expectChangeStillUnexecuted(session, "authorized-change");- await approveSavedChange(t, session, newer);+ expectApprovalCancelled(session, approvalA);+ // When the user still answers A, from an old prompt.+ const stale = await session.respond([{ requestId: approvalA.requestId, optionId: "approve" }]);+ // Then the stale answer becomes new input, which steers the held turn like any message:+ // the newer change is cancelled too, and nothing runs.+ stale.expectOk();+ stale.notEvent("turn.started");+ stale.event("message.completed", { count: 1 });+ expectApprovalCancelled(session, newer);+ expectChangeStillUnexecuted(session);+ t.check(session.pendingInputRequests.length, equals(0));},});diff --git a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/parallel-tools.eval.ts b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/parallel-tools.eval.tsindex 3c9e91a3f..ccda799d9 100644--- a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/parallel-tools.eval.ts+++ b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/parallel-tools.eval.ts@@ -1,7 +1,7 @@import { defineEval } from "eve/evals";import {+ expectApprovalCancelled,scriptedSession,- approveSavedChange,expectChangeStillUnexecuted,expectReply,requestFrom,@@ -21,12 +21,12 @@ export default defineEval({// When the user asks to read and save in parallel.const live = await session.start("Read and save the draft in parallel.");- // Then the reply reports both results and completes; A stays unexecuted and answerable.+ // Then the reply reports both results and completes; the message cancelled A, which never runs.await expectToolResult(t, live, "save-draft");const turn = await expectReply(t, live, 'Draft status: ready. Draft saved: {"writes":1}.');turn.calledTool("read-draft", { status: "completed", count: 1 });turn.calledTool("save-draft", { status: "completed", count: 1 });expectChangeStillUnexecuted(session);- await approveSavedChange(t, session, approval);+ expectApprovalCancelled(session, approval);},});diff --git a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/partial-approval-text.eval.ts b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/partial-approval-text.eval.tsindex 0b2254ab4..c19af0e94 100644--- a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/partial-approval-text.eval.ts+++ b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/partial-approval-text.eval.ts@@ -1,9 +1,9 @@import { defineEval } from "eve/evals";+import { equals } from "eve/evals/expect";import {scriptedSession,expectChangeStillUnexecuted,expectReply,- expectResponseReply,requestFrom,submitPartialApproval,} from "./helpers.ts";@@ -20,32 +20,24 @@ export default defineEval({const approvalB = requestFrom(parked, "change-b");const session = parked.session;await submitPartialApproval(t, session, approvalA);-- // When the user asks what is waiting without requesting tools.+ // When the user sends a message instead of answering B, which steers the held turn.const live = await session.start("Explain what is waiting, without calling any tools.");-- // Then the text reply completes; the batch stays unexecuted until B is approved.const reply = await expectReply(t, live, "Your changes are waiting for approval.");- reply.usedNoTools();- expectChangeStillUnexecuted(session, "change-a");- expectChangeStillUnexecuted(session, "change-b");-- const approved = await expectResponseReply(- t,- await session.startRespond([{ requestId: approvalB.requestId, optionId: "approve" }]),- "Both changes resolved.",- approvalB.requestId,- );- approved.event("input.resolved", {+ // Then A keeps its approval and runs once, B is cancelled, and the message gets its reply.+ reply.event("input.resolved", {data: {resolutions: (items) =>- [approvalA.requestId, approvalB.requestId].every((id) =>- items.some((item) => item.requestId === id && item.outcome === "approved"),+ items.some(+ (item) => item.requestId === approvalA.requestId && item.outcome === "approved",+ ) &&+ items.some(+ (item) => item.requestId === approvalB.requestId && item.outcome === "ignored",),},count: 1,});- approved.calledTool("change-a", { status: "completed", output: { executions: 1 }, count: 1 });- approved.calledTool("change-b", { status: "completed", output: { executions: 1 }, count: 1 });+ reply.calledTool("change-a", { status: "completed", output: { executions: 1 }, count: 1 });+ expectChangeStillUnexecuted(session, "change-b");+ t.check(session.pendingInputRequests.length, equals(0));},});diff --git a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/partial-approval-tool.eval.ts b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/partial-approval-tool.eval.tsindex 5d8097d38..39e902c13 100644--- a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/partial-approval-tool.eval.ts+++ b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/partial-approval-tool.eval.ts@@ -1,9 +1,9 @@import { defineEval } from "eve/evals";+import { equals } from "eve/evals/expect";import {scriptedSession,expectChangeStillUnexecuted,expectReply,- expectResponseReply,expectToolResult,requestFrom,submitPartialApproval,@@ -21,32 +21,25 @@ export default defineEval({const approvalB = requestFrom(parked, "change-b");const session = parked.session;await submitPartialApproval(t, session, approvalA);-- // When the user asks to read the draft.+ // When the user sends a message instead of answering B, which steers the held turn.const live = await session.start("Read the draft status.");-- // Then the read gets a completed reply; the batch stays unexecuted until B is approved.await expectToolResult(t, live, "read-draft");- await expectReply(t, live, "Draft status: ready.");- expectChangeStillUnexecuted(session, "change-a");- expectChangeStillUnexecuted(session, "change-b");-- const approved = await expectResponseReply(- t,- await session.startRespond([{ requestId: approvalB.requestId, optionId: "approve" }]),- "Both changes resolved.",- approvalB.requestId,- );- approved.event("input.resolved", {+ const reply = await expectReply(t, live, "Draft status: ready.");+ // Then A keeps its approval and runs once, B is cancelled, and the message gets its reply.+ reply.event("input.resolved", {data: {resolutions: (items) =>- [approvalA.requestId, approvalB.requestId].every((id) =>- items.some((item) => item.requestId === id && item.outcome === "approved"),+ items.some(+ (item) => item.requestId === approvalA.requestId && item.outcome === "approved",+ ) &&+ items.some(+ (item) => item.requestId === approvalB.requestId && item.outcome === "ignored",),},count: 1,});- approved.calledTool("change-a", { status: "completed", output: { executions: 1 }, count: 1 });- approved.calledTool("change-b", { status: "completed", output: { executions: 1 }, count: 1 });+ reply.calledTool("change-a", { status: "completed", output: { executions: 1 }, count: 1 });+ expectChangeStillUnexecuted(session, "change-b");+ t.check(session.pendingInputRequests.length, equals(0));},});diff --git a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/provider-result.eval.ts b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/provider-result.eval.tsindex 13e685a88..1050f629f 100644--- a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/provider-result.eval.ts+++ b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/provider-result.eval.ts@@ -1,7 +1,7 @@import { defineEval } from "eve/evals";import {+ expectApprovalCancelled,scriptedSession,- approveSavedChange,expectChangeStillUnexecuted,expectReply,expectToolResult,@@ -22,7 +22,7 @@ export default defineEval({// When the user asks the provider to look up the draft.const live = await session.start("Look up the draft with the provider and report its status.");- // Then the reply reports provider-ready and completes; A stays unexecuted and answerable.+ // Then the reply reports provider-ready and completes; the message cancelled A, which never runs.await expectToolResult(t, live, "read-draft");const reply = await expectReply(t, live, "Provider draft status: provider-ready.");reply.calledTool("read-draft", {@@ -31,6 +31,6 @@ export default defineEval({count: 1,});expectChangeStillUnexecuted(session);- await approveSavedChange(t, session, approval);+ expectApprovalCancelled(session, approval);},});diff --git a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/read.eval.ts b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/read.eval.tsindex 48f262ef9..29fad62a3 100644--- a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/read.eval.ts+++ b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/read.eval.ts@@ -1,7 +1,7 @@import { defineEval } from "eve/evals";import {+ expectApprovalCancelled,scriptedSession,- approveSavedChange,expectChangeStillUnexecuted,expectReply,requestFrom,@@ -21,11 +21,11 @@ export default defineEval({// When the user asks to read the draft.const live = await session.start("Read the draft status.");- // Then the reply reports ready and completes; A stays unexecuted and answerable.+ // Then the reply reports ready and completes; the message cancelled A, which never runs.await expectToolResult(t, live, "read-draft");const turn = await expectReply(t, live, "Draft status: ready.");turn.calledTool("read-draft", { status: "completed", count: 1 });expectChangeStillUnexecuted(session);- await approveSavedChange(t, session, approval);+ expectApprovalCancelled(session, approval);},});diff --git a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/stale-response.eval.ts b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/stale-response.eval.tsindex 5d08675d7..756dfd6d7 100644--- a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/stale-response.eval.ts+++ b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/stale-response.eval.ts@@ -1,7 +1,7 @@import { defineEval } from "eve/evals";import {scriptedSession,- approveSavedChange,+ expectApprovalCancelled,expectChangeStillUnexecuted,expectReply,expectResponseReply,@@ -15,7 +15,7 @@ export default defineEval({tags: ["hitl", "continuation", "regression", "input-response", "stale-response"],timeoutMs: 60_000,async test(t) {- // Given A awaits approval and B has already been cancelled and acknowledged.+ // Given asking for B cancelled A's held approval, and B has been cancelled and acknowledged.const first = await t.send("Prepare change A.", scriptedSession);const approvalA = requestFrom(first, "change-a");const session = first.session;@@ -39,6 +39,6 @@ export default defineEval({session.notEvent("action.result", {data: { status: "completed", result: { toolName: "change-b" } },});- await approveSavedChange(t, session, approvalA);+ expectApprovalCancelled(session, approvalA);},});diff --git a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/text-only.control.eval.ts b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/text-only.control.eval.tsindex 0679a7605..deea1dcfa 100644--- a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/text-only.control.eval.ts+++ b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/text-only.control.eval.ts@@ -1,7 +1,7 @@import { defineEval } from "eve/evals";import {+ expectApprovalCancelled,scriptedSession,- approveSavedChange,expectChangeStillUnexecuted,expectReply,requestFrom,@@ -21,10 +21,11 @@ export default defineEval({// When the user asks what is waiting without requesting tools.const live = await session.start("Explain what is waiting, without calling any tools.");- // Then the text reply completes; A stays unexecuted and answerable.+ // Then the text reply completes; the message cancelled A, which never runs.const reply = await expectReply(t, live, "Your changes are waiting for approval.");- reply.usedNoTools();+ // The cancelled approval reports a not-run result; the reply itself calls no tools.+ reply.notEvent("actions.requested");expectChangeStillUnexecuted(session);- await approveSavedChange(t, session, approval);+ expectApprovalCancelled(session, approval);},});diff --git a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/tool-error.eval.ts b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/tool-error.eval.tsindex 8c6c0bf58..a1cf93281 100644--- a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/tool-error.eval.ts+++ b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/tool-error.eval.ts@@ -1,7 +1,7 @@import { defineEval } from "eve/evals";import {+ expectApprovalCancelled,scriptedSession,- approveSavedChange,expectChangeStillUnexecuted,expectReply,requestFrom,@@ -21,7 +21,7 @@ export default defineEval({// When the user asks to read from a failing tool.const live = await session.start("Try the unavailable draft store and explain the error.");- // Then the reply explains the actual error and completes; A stays unexecuted and answerable.+ // Then the reply explains the actual error and completes; the message cancelled A, which never runs.await expectToolResult(t, live, "unavailable-draft");const turn = await expectReply(t,@@ -30,6 +30,6 @@ export default defineEval({);turn.calledTool("unavailable-draft", { status: "failed", count: 1 });expectChangeStillUnexecuted(session);- await approveSavedChange(t, session, approval);+ expectApprovalCancelled(session, approval);},});diff --git a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/workflow-result.eval.ts b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/workflow-result.eval.tsindex 81a161c03..c9bf0bb51 100644--- a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/workflow-result.eval.ts+++ b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/workflow-result.eval.ts@@ -1,7 +1,7 @@import { defineEval } from "eve/evals";import {+ expectApprovalCancelled,scriptedSession,- approveSavedChange,expectChangeStillUnexecuted,expectReply,requestFrom,@@ -21,11 +21,11 @@ export default defineEval({// When the user asks a workflow to read the draft.const live = await session.start("Read the draft through a workflow.");- // Then the reply reports the workflow result and completes; A stays unexecuted and answerable.+ // Then the reply reports the workflow result and completes; the message cancelled A, which never runs.await expectToolResult(t, live, "workflow-draft");const turn = await expectReply(t, live, "Workflow draft status: ready.");turn.calledTool("workflow-draft", { status: "completed", count: 1 });expectChangeStillUnexecuted(session);- await approveSavedChange(t, session, approval);+ expectApprovalCancelled(session, approval);},});diff --git a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/write.eval.ts b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/write.eval.tsindex d6769f0ce..e873308b9 100644--- a/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/write.eval.ts+++ b/e2e/fixtures/agent-tools-hitl/evals/hitl/continuation/write.eval.ts@@ -1,7 +1,7 @@import { defineEval } from "eve/evals";import {+ expectApprovalCancelled,scriptedSession,- approveSavedChange,expectChangeStillUnexecuted,expectReply,requestFrom,@@ -21,11 +21,11 @@ export default defineEval({// When the user asks to save the draft.const live = await session.start("Save the draft and report how many times it was written.");- // Then the reply confirms one write and completes; A stays unexecuted and answerable.+ // Then the reply confirms one write and completes; the message cancelled A, which never runs.await expectToolResult(t, live, "save-draft");const turn = await expectReply(t, live, 'Draft saved: {"writes":1}.');turn.calledTool("save-draft", { status: "completed", count: 1 });expectChangeStillUnexecuted(session);- await approveSavedChange(t, session, approval);+ expectApprovalCancelled(session, approval);},});diff --git a/e2e/fixtures/agent-tools-hitl/evals/hitl/pending-approval-followup-dedup.eval.ts b/e2e/fixtures/agent-tools-hitl/evals/hitl/pending-approval-followup-dedup.eval.tsindex 1160f8f7c..e0b481945 100644--- a/e2e/fixtures/agent-tools-hitl/evals/hitl/pending-approval-followup-dedup.eval.ts+++ b/e2e/fixtures/agent-tools-hitl/evals/hitl/pending-approval-followup-dedup.eval.ts@@ -1,4 +1,5 @@import { defineEval } from "eve/evals";+import { equals } from "eve/evals/expect";const MARKER = "followup-dedup-H4K8";const TOOL_NAME = "gate";@@ -10,50 +11,52 @@ const FOLLOW_UP_QUESTIONS = ["What will happen after I approve the request?",] as const;-/** Regression coverage for https://github.com/vercel/eve/issues/2217. */+/**+ * Regression coverage for https://github.com/vercel/eve/issues/2217: follow-up+ * questions never create another call or approval. The approval holds the+ * turn, so the first follow-up steers it and cancels the approval.+ */export default defineEval({tags: ["real-model"],- description: "One pending approval stays singular across many follow-up questions.",+ description: "Follow-up questions about a held approval never create another one.",async test(t) {- const parked = await t.send(`Call the ${TOOL_NAME} tool exactly once with marker "${MARKER}".`);- const session = parked.session;- parked.calledTool(TOOL_NAME, { status: "pending", count: 1 });+ const held = await t.send(`Call the ${TOOL_NAME} tool exactly once with marker "${MARKER}".`);+ const session = held.session;+ held.calledTool(TOOL_NAME, { status: "pending", count: 1 });const approval = session.requireInputRequest({display: "confirmation",toolName: TOOL_NAME,});- for (const question of FOLLOW_UP_QUESTIONS) {+ for (const [index, question] of FOLLOW_UP_QUESTIONS.entries()) {const followup = await session.send(question);followup.expectOk();- followup.usedNoTools();+ followup.notEvent("actions.requested");followup.notEvent("input.requested");+ if (index === 0) {+ // The steer cancels the held call, so its not-run result lands in this turn.+ followup.calledTool(TOOL_NAME, { status: "completed", count: 0 });+ followup.event("input.resolved", {+ count: 1,+ data: {+ resolutions: (resolutions) =>+ resolutions.some(+ (resolution) =>+ resolution.requestId === approval.requestId && resolution.outcome === "ignored",+ ),+ },+ });+ } else {+ followup.usedNoTools();+ }followup.event("session.waiting", { count: 1 });}- const approved = await session.respond([- {- optionId: "approve",- requestId: approval.requestId,- },- ]);-- approved.expectOk();- approved.event("action.result", {- data: {- result: {- kind: "tool-result",- output: new RegExp(MARKER),- toolName: TOOL_NAME,- },- status: "completed",- },- count: 1,- });t.succeeded();- // The original request completes once; each scoped follow-up above proves- // that no intervening turn created another tool call or approval.- t.calledTool(TOOL_NAME, { count: 1 });+ t.check(session.pendingInputRequests.length, equals(0));+ t.notEvent("action.result", {+ data: { result: { output: new RegExp(MARKER), toolName: TOOL_NAME } },+ });},});diff --git a/e2e/fixtures/agent-tools-hitl/evals/hitl/pending-approval-tool-followup.eval.ts b/e2e/fixtures/agent-tools-hitl/evals/hitl/pending-approval-tool-followup.eval.tsindex 74036095d..5f506ac52 100644--- a/e2e/fixtures/agent-tools-hitl/evals/hitl/pending-approval-tool-followup.eval.ts+++ b/e2e/fixtures/agent-tools-hitl/evals/hitl/pending-approval-tool-followup.eval.ts@@ -53,7 +53,7 @@ export default [}),defineEval({description:- "An ungated tool follow-up completes while an older approval remains answerable (#3494).",+ "An ungated tool follow-up steers a held approval, cancels it, and completes (#3494).",tags: ["hitl", "continuation", "regression", "user-message", "tool-result"],timeoutMs: 120_000,async test(t) {@@ -67,9 +67,10 @@ export default [const approval = requestFrom(parked, "gate");t.log(`Original gate approval is pending: ${approval.requestId}`);- // When the user leaves that approval pending and asks to read the draft status.+ // When the user moves on and asks to read the draft status instead. The+ // approval holds the turn, so this message steers it.const live = await session.start(- `Alice will review the account change later. Leave its approval pending. ${READ_STATUS}`,+ `Alice will review the account change later. ${READ_STATUS}`,);// Then the tool result reaches a completed reply without executing the account change.const result = await live.waitForEvent("action.result", {@@ -84,7 +85,12 @@ export default [followup.messageIncludes(MARKER);followup.messageIncludes("ready");followup.eventOrder([- { type: "message.received", data: { turnId: received.data.turnId }, count: 1 },+ // The follow-up joins the held turn, which also received the original request.+ {+ type: "message.received",+ data: { turnId: received.data.turnId, sequence: received.data.sequence },+ count: 1,+ },{type: "action.result",data: {@@ -105,19 +111,20 @@ export default [},{ type: "turn.completed", data: { turnId: received.data.turnId }, count: 1 },]);- followup.notEvent("action.result", { data: { result: { toolName: "gate" } } });+ followup.notEvent("action.result", {+ data: { status: "completed", result: { toolName: "gate" } },+ });followup.notEvent("input.requested");-- // When the user later approves the original account change.- const approved = await session.respond([- { requestId: approval.requestId, optionId: "approve" },- ]);- // Then that saved approval executes exactly once.- approved.expectOk();- approved.calledTool("gate", { status: "completed", count: 1 });- session.event("action.result", {+ // Then the steer cancelled the account change instead of leaving it open.+ followup.event("input.resolved", {count: 1,- data: { status: "completed", result: { toolName: "gate" } },+ data: {+ resolutions: (resolutions) =>+ resolutions.some(+ (resolution) =>+ resolution.requestId === approval.requestId && resolution.outcome === "ignored",+ ),+ },});t.check(session.pendingInputRequests.length, equals(0));},diff --git a/e2e/fixtures/agent-tools-hitl/evals/hitl/unrelated-message-runs.eval.ts b/e2e/fixtures/agent-tools-hitl/evals/hitl/unrelated-message-runs.eval.tsindex 823b5deab..2591e6924 100644--- a/e2e/fixtures/agent-tools-hitl/evals/hitl/unrelated-message-runs.eval.ts+++ b/e2e/fixtures/agent-tools-hitl/evals/hitl/unrelated-message-runs.eval.ts@@ -3,61 +3,45 @@ import { defineEval } from "eve/evals";import { GUARDED_ECHO_TOKEN } from "./shared";/**- * HITL flow: unrelated text sent while an approval is pending must not deny- * the approval and must not wait behind it. The message runs as an ordinary- * turn, the approval stays answerable, and a later structured approval still- * runs the original tool call exactly once.+ * HITL flow: a tool approval holds the turn, as a question or task does. A+ * message from the same person steers that turn and cancels the approval, so+ * the turn moves on with the message and the parked call never runs.*/export default defineEval({tags: ["real-model"],- description: "HITL smoke: unrelated message during approval runs immediately.",+ description: "HITL smoke: a message during an approval steers the turn and cancels it.",async test(t) {- const parked = await t.send('Call the guarded-echo tool with note "open-approval".');- const session = parked.session;- parked.calledTool("guarded-echo", { status: "pending", count: 1 });+ const held = await t.send('Call the guarded-echo tool with note "open-approval".');+ const session = held.session;+ held.calledTool("guarded-echo", { status: "pending", count: 1 });+ held.event("turn.waiting", { count: 1 });+ held.notEvent("session.waiting");const request = session.requireInputRequest({display: "confirmation",toolName: "guarded-echo",});- // The message runs now; the approval is untouched.- const message = await session.send(- "Leave the pending approval alone. Do not call any tools. Reply with exactly OPEN-APPROVAL-MSG-OK.",+ const steered = await session.send(+ "Never mind the echo. Do not call any tools. Reply with exactly OPEN-APPROVAL-MSG-OK.",);- message.expectOk();- message.event("message.received", { count: 1 });- message.event("message.completed", { count: 1 });- message.messageIncludes(/OPEN-APPROVAL-MSG-OK/i);- // The parked call must not execute — and the intervening turn must not- // run any tool of its own either.- message.notEvent("action.result", {- data: { result: { toolName: "guarded-echo" } },- });- message.usedNoTools();- message.event("session.waiting", { count: 1 });-- // The approval still resolves afterwards and runs the tool once.- const approved = await session.respond([- {- requestId: request.requestId,- optionId: "approve",- },- ]);- approved.expectOk();- approved.event("action.result", {+ steered.expectOk();+ steered.event("input.resolved", {+ count: 1,data: {- result: {- kind: "tool-result",- output: new RegExp(GUARDED_ECHO_TOKEN),- toolName: "guarded-echo",- },- status: "completed",+ resolutions: (resolutions) =>+ resolutions.some(+ (resolution) =>+ resolution.requestId === request.requestId && resolution.outcome === "ignored",+ ),},- count: 1,});- approved.event("session.waiting", { count: 1 });+ steered.notEvent("turn.started");+ steered.messageIncludes(/OPEN-APPROVAL-MSG-OK/i);+ steered.notEvent("action.result", {+ data: { result: { output: new RegExp(GUARDED_ECHO_TOKEN), toolName: "guarded-echo" } },+ });+ steered.event("session.waiting", { count: 1 });t.succeeded();- t.calledTool("guarded-echo", { status: "completed", count: 1 });},});diff --git a/e2e/fixtures/agent-workflow-tools/agent/agent.ts b/e2e/fixtures/agent-workflow-tools/agent/agent.tsindex 30114a20b..3eb04f559 100644--- a/e2e/fixtures/agent-workflow-tools/agent/agent.ts+++ b/e2e/fixtures/agent-workflow-tools/agent/agent.ts@@ -69,7 +69,9 @@ async function respond(request: MockModelRequest): Promise<MockModelResponse | sconst scenario = respondToTaskScenario(request, directiveOf(message));if (scenario !== undefined) return scenario;if (message.includes("private-catalog")) {- const search = request.toolResults.find((entry) => entry.name === "connection_search");+ const search = [...request.toolResults]+ .reverse()+ .find((entry) => entry.name === "connection_search");if (search === undefined) {return {toolCalls: [@@ -80,7 +82,29 @@ async function respond(request: MockModelRequest): Promise<MockModelResponse | s],};}- return JSON.stringify(search.output);+ // A plain search reports the connection as requiring sign-in; `signIn` asks the user.+ if (JSON.stringify(search.output).includes('"requiresSignIn":true')) {+ return {+ toolCalls: [+ {+ name: "connection_search",+ input: { connection: "private-catalog", query: "items", signIn: true },+ },+ ],+ };+ }+ const execute = request.toolResults.find((entry) => entry.name === "connection_execute");+ if (execute === undefined) {+ return {+ toolCalls: [+ {+ name: "connection_execute",+ input: { connection: "private-catalog", tool: "list_items", input: {} },+ },+ ],+ };+ }+ return JSON.stringify(execute.output);}const stepAuth = /WORKFLOW-STEP-AUTH-(IMPLICIT|EXPLICIT|REJECTED)/u.exec(message);diff --git a/e2e/fixtures/agent-workflow-tools/agent/channels/catalog.ts b/e2e/fixtures/agent-workflow-tools/agent/channels/catalog.tsindex 282d061a4..cc0635f8f 100644--- a/e2e/fixtures/agent-workflow-tools/agent/channels/catalog.ts+++ b/e2e/fixtures/agent-workflow-tools/agent/channels/catalog.ts@@ -48,6 +48,16 @@ export default defineChannel({});}+ if (method === "tools/call") {+ return Response.json({+ jsonrpc: "2.0",+ id,+ result: {+ content: [{ type: "text", text: JSON.stringify({ items: ["Alice's lamp"] }) }],+ },+ });+ }+return Response.json({jsonrpc: "2.0",id,diff --git a/e2e/fixtures/agent-workflow-tools/agent/connections/private-catalog.ts b/e2e/fixtures/agent-workflow-tools/agent/connections/private-catalog.tsindex 0fadc20c7..9775f2e08 100644--- a/e2e/fixtures/agent-workflow-tools/agent/connections/private-catalog.ts+++ b/e2e/fixtures/agent-workflow-tools/agent/connections/private-catalog.ts@@ -10,7 +10,7 @@ export default defineDynamic({description: "Private catalog that requires sign-in before discovering its tools.",url: fixtureUrl("/fixture-catalog/mcp").href,instanceKey: "fixture-private-catalog",- auth: createFakeAuthProvider({ expiredToken: false }),+ auth: createFakeAuthProvider({ expiredToken: false, rememberSignIns: true }),}),},});diff --git a/e2e/fixtures/agent-workflow-tools/agent/lib/fake-auth-provider.ts b/e2e/fixtures/agent-workflow-tools/agent/lib/fake-auth-provider.tsindex 19bbaf834..41ba007aa 100644--- a/e2e/fixtures/agent-workflow-tools/agent/lib/fake-auth-provider.ts+++ b/e2e/fixtures/agent-workflow-tools/agent/lib/fake-auth-provider.ts@@ -1,16 +1,33 @@import { ConnectionAuthorizationRequiredError } from "eve/connections";import type { ToolAuthProvider } from "eve/tools";+/**+ * Tokens kept after sign-in, by user. Module scope because a dynamic+ * connection builds a new provider each time it resolves.+ */+const rememberedTokens = new Map<string, string>();+/** Simulates the external auth service; the tool uses eve's real ctx auth methods. */export function createFakeAuthProvider({expiredToken,+ rememberSignIns = false,}: {expiredToken: boolean;+ /**+ * Keep each user's token after sign-in, as a real provider does. eve caches a+ * token only for the step that completed the sign-in, so later steps need it.+ */+ rememberSignIns?: boolean;}): ToolAuthProvider {return {principalType: "user",- async getToken() {+ async getToken({ principal }) {if (expiredToken) return { token: "expired-fixture-token" };+ const token =+ rememberSignIns && principal.type === "user"+ ? rememberedTokens.get(principal.id)+ : undefined;+ if (token !== undefined) return { token };throw new ConnectionAuthorizationRequiredError("workflow-step");},async startAuthorization({ principal, callbackUrl }) {@@ -27,7 +44,9 @@ export function createFakeAuthProvider({) {throw new Error("Authorization did not match the workflow requester");}- return { token: "authorized-fixture-token" };+ const token = "authorized-fixture-token";+ if (rememberSignIns) rememberedTokens.set(principal.id, token);+ return { token };},};}diff --git a/e2e/fixtures/agent-workflow-tools/evals/agent-probe.shared.ts b/e2e/fixtures/agent-workflow-tools/evals/agent-probe.shared.tsindex ed9bbbaf2..f6790ad20 100644--- a/e2e/fixtures/agent-workflow-tools/evals/agent-probe.shared.ts+++ b/e2e/fixtures/agent-workflow-tools/evals/agent-probe.shared.ts@@ -83,8 +83,8 @@ async function waitForMarker(}/**- * A sign-in inside a running call parks the turn without ending it, so read- * one live turn: complete the sign-in when it is requested, then read the+ * A sign-in inside a running call holds the turn: the response stops at its+ * `turn.waiting` (`on: "input"`). Complete the sign-in, then read the same* turn to its end.*/async function completeSignIn(@@ -94,13 +94,18 @@ async function completeSignIn(): Promise<{ readonly callbackUrl: URL; readonly turn: EveEvalTurn }> {const session = await t.session();const live = await session.start(message);- const required = await live.waitForEvent("authorization.required");+ const held = await live.result();+ const required = held.events.find((event) => event.type === "authorization.required");+ if (required?.type !== "authorization.required") {+ throw new Error("Expected the held turn to request a sign-in.");+ }const callbackUrl = toCallbackUrl(required.data.authorization?.url);+ const resumed = watchNext(t, live.session);const response = await fetch(callbackUrl);if (!response.ok) {throw new Error(`Authorization callback failed (${response.status}).`);}- return { callbackUrl, turn: await live.result() };+ return { callbackUrl, turn: await resumed.result() };}function requireAuthorizationOutcome(diff --git a/e2e/fixtures/agent-workflow-tools/evals/connection-tools.authorization.eval.ts b/e2e/fixtures/agent-workflow-tools/evals/connection-tools.authorization.eval.tsindex 72b50854e..2bf0657a7 100644--- a/e2e/fixtures/agent-workflow-tools/evals/connection-tools.authorization.eval.ts+++ b/e2e/fixtures/agent-workflow-tools/evals/connection-tools.authorization.eval.ts@@ -3,27 +3,46 @@ import { z } from "zod";import { fixtureAuthorizationCallback } from "../agent/lib/fake-service.ts";-const searchResult = z.object({- tools: z.array(z.object({ connection: z.string(), tool: z.string(), signature: z.string() })),+const unavailableResult = z.object({+ tools: z.array(z.unknown()).length(0),+ unavailable: z.array(+ z.object({ connection: z.string(), error: z.string(), requiresSignIn: z.literal(true) }),+ ),});+const toolsResult = z.object({ tools: z.array(z.object({ tool: z.string() })) });+const executeResult = z.object({ items: z.array(z.string()) });export default defineEval({- description: "Connection search pauses for sign-in, then finds tools over authenticated HTTP.",+ description:+ "A plain connection search reports that private-catalog requires sign-in without prompting; searching it with signIn prompts, then lists and runs its tools over authenticated HTTP.",timeoutMs: 90_000,async test(t) {+ // A fresh principal per run: the fixture provider remembers each user's token.+ const alice = { "x-eve-forwarded-principal-id": `catalog-alice-${crypto.randomUUID()}` };const started = await t.send(- "Alice wants to see which tools are available in private-catalog. Search that connection for its items tools, then report the available tool names.",+ "Alice wants to see the items in private-catalog. Search that connection for its items tools, then list the items and report them.",+ { headers: alice },);const session = started.session;started.expectOk();+ started.calledTool("connection_search", {+ count: 1,+ input: { connection: "private-catalog" },+ output: (value) =>+ unavailableResult+ .safeParse(value)+ .data?.unavailable.some((entry) => entry.connection === "private-catalog") === true,+ });started.event("authorization.required", { count: 1 });started.notEvent("authorization.completed");- started.event("session.waiting", { count: 1 });+ // The sign-in holds the turn; the callback resumes it.+ started.event("turn.waiting", { count: 1 });+ started.notEvent("session.waiting");const required = started.events.find((event) => event.type === "authorization.required");if (required?.type !== "authorization.required") {- throw new Error("Connection search did not produce an authorization challenge.");+ throw new Error("Connection search with signIn did not produce an authorization challenge.");}const callback = fixtureAuthorizationCallback(t.target.url, required.data.authorization?.url);if (session.sessionId === undefined || session.state === undefined) {@@ -40,6 +59,7 @@ export default defineEval({const completed = await resumed.result();completed.expectOk();+ completed.notEvent("turn.started");completed.noFailedActions();completed.notEvent("authorization.required");completed.event("authorization.completed", {@@ -48,21 +68,16 @@ export default defineEval({});completed.calledTool("connection_search", {count: 1,- output: (value) => {- const result = searchResult.safeParse(value);- return (- result.success &&- result.data.tools.some(- (entry) =>- entry.connection === "private-catalog" &&- entry.tool === "list_items" &&- entry.signature.startsWith("list_items("),- )- );- },+ input: { connection: "private-catalog", signIn: true },+ output: (value) =>+ toolsResult.safeParse(value).data?.tools.some((entry) => entry.tool === "list_items") ===+ true,+ });+ completed.calledTool("connection_execute", {+ count: 1,+ output: (value) =>+ executeResult.safeParse(value).data?.items.includes("Alice's lamp") === true,});- // Calling the tool needs the token in a later step. A real provider persists- // it; this fixture's fake provider keeps none after sign-in.- completed.messageIncludes("list_items");+ completed.messageIncludes("Alice's lamp");},});diff --git a/e2e/fixtures/agent-workflow-tools/evals/gated-deploy.approval.eval.ts b/e2e/fixtures/agent-workflow-tools/evals/gated-deploy.approval.eval.tsindex dd81b0a68..cde46066f 100644--- a/e2e/fixtures/agent-workflow-tools/evals/gated-deploy.approval.eval.ts+++ b/e2e/fixtures/agent-workflow-tools/evals/gated-deploy.approval.eval.ts@@ -26,10 +26,13 @@ export default defineEval({parked.calledTool("gated_deploy", { count: 1, status: "pending" });parked.notEvent("action.partial");parked.notEvent("action.result");+ // The approval holds the turn, so answering it resumes that turn.+ parked.event("turn.waiting", { count: 1 });+ parked.notEvent("session.waiting");const resumed = await session.respondAll(decision);resumed.expectOk();- resumed.event("turn.started", { count: 1 });+ resumed.notEvent("turn.started");resumed.event("input.resolved", { count: 1 });resumed.messageIncludes("WORKFLOW-APPROVAL-RESULT");if (decision === "approve") {@@ -39,7 +42,7 @@ export default defineEval({});resumed.calledTool("gated_deploy", { status: "completed", output: /api/u });resumed.eventOrder([- { type: "turn.started" },+ { type: "input.resolved" },{ type: "action.partial" },{ type: "action.result" },{ type: "turn.completed" },diff --git a/packages/eve/extension-contracts/compatibility/channel/v44.ts b/packages/eve/extension-contracts/compatibility/channel/v44.tsnew file mode 100644index 000000000..2cd3c402c--- /dev/null+++ b/packages/eve/extension-contracts/compatibility/channel/v44.ts@@ -0,0 +1,12 @@+import { defineChannel } from "#public/channels/index.js";++// Epoch 44 `turn.waiting` events had no `on`; epoch 45 adds it.+// Channels that read only `turnId` keep working.+export default defineChannel({+ routes: [],+ events: {+ "turn.waiting"(event) {+ console.info("turn waiting", { turnId: event.turnId });+ },+ },+});diff --git a/packages/eve/extension-contracts/compatibility/connection/v37.ts b/packages/eve/extension-contracts/compatibility/connection/v37.tsnew file mode 100644index 000000000..9fd125cdc--- /dev/null+++ b/packages/eve/extension-contracts/compatibility/connection/v37.ts@@ -0,0 +1,10 @@+import { defineMcpClientConnection } from "#public/connections/index.js";++// Epoch 37 `turn.waiting` events had no `on`; epoch 38 adds it.+export default defineMcpClientConnection({+ description: "Search the incident tracker.",+ url: "https://incidents.example.com/mcp",+ approval({ toolName }) {+ return toolName.endsWith("__search") ? "approved" : "user-approval";+ },+});diff --git a/packages/eve/extension-contracts/compatibility/dynamicInstructions/v31.ts b/packages/eve/extension-contracts/compatibility/dynamicInstructions/v31.tsnew file mode 100644index 000000000..088fc1fc7--- /dev/null+++ b/packages/eve/extension-contracts/compatibility/dynamicInstructions/v31.ts@@ -0,0 +1,11 @@+import { defineDynamic, defineInstructions } from "#public/instructions/index.js";++// Epoch 31 `turn.waiting` events had no `on`; epoch 32 adds it.+export default defineDynamic({+ events: {+ "turn.started": (_event, ctx) =>+ defineInstructions({+ markdown: `Triage the incident reported in session ${ctx.session.id}.`,+ }),+ },+});diff --git a/packages/eve/extension-contracts/compatibility/dynamicSkill/v30.ts b/packages/eve/extension-contracts/compatibility/dynamicSkill/v30.tsnew file mode 100644index 000000000..3ea8ffa4c--- /dev/null+++ b/packages/eve/extension-contracts/compatibility/dynamicSkill/v30.ts@@ -0,0 +1,12 @@+import { defineDynamic, defineSkill } from "#public/skills/index.js";++// Epoch 30 `turn.waiting` events had no `on`; epoch 31 adds it.+export default defineDynamic({+ events: {+ "turn.started": (_event, ctx) =>+ defineSkill({+ description: "Triage the active incident.",+ markdown: `Triage the incident reported in session ${ctx.session.id}.`,+ }),+ },+});diff --git a/packages/eve/extension-contracts/compatibility/dynamicTool/v69.ts b/packages/eve/extension-contracts/compatibility/dynamicTool/v69.tsnew file mode 100644index 000000000..616fddc65--- /dev/null+++ b/packages/eve/extension-contracts/compatibility/dynamicTool/v69.ts@@ -0,0 +1,14 @@+import { defineDynamic, defineTool } from "#public/tools/index.js";++// Epoch 69 `turn.waiting` events had no `on`; epoch 70 adds it.+export default defineDynamic({+ events: {+ "turn.started": (_event, ctx) => ({+ session: defineTool({+ description: "Return the active session identifier.",+ inputSchema: { type: "object", properties: {} },+ execute: () => ({ sessionId: ctx.session.id }),+ }),+ }),+ },+});diff --git a/packages/eve/extension-contracts/compatibility/hook/v36.ts b/packages/eve/extension-contracts/compatibility/hook/v36.tsnew file mode 100644index 000000000..00cfd016b--- /dev/null+++ b/packages/eve/extension-contracts/compatibility/hook/v36.ts@@ -0,0 +1,11 @@+import { defineHook } from "#public/hooks/index.js";++// Epoch 36 `turn.waiting` events had no `on`; epoch 37 adds it.+// Hooks that read only `turnId` keep working.+export default defineHook({+ events: {+ "turn.waiting"(event, ctx) {+ console.info("turn waiting", { sessionId: ctx.session.id, turnId: event.data.turnId });+ },+ },+});diff --git a/packages/eve/extension-contracts/compatibility/schedule/v25.ts b/packages/eve/extension-contracts/compatibility/schedule/v25.tsnew file mode 100644index 000000000..f3d0b50ff--- /dev/null+++ b/packages/eve/extension-contracts/compatibility/schedule/v25.ts@@ -0,0 +1,22 @@+import { defineChannel } from "#public/channels/index.js";+import { defineSchedule } from "#public/schedules/index.js";++// Epoch 25 `turn.waiting` events had no `on`; epoch 26 adds it.+const reports = defineChannel({+ routes: [],+ events: {+ "turn.waiting"(event) {+ console.info("report turn waiting", { turnId: event.turnId });+ },+ },+ receive(input, { from }) {+ return from("nightly-report").send(input.message, { auth: input.auth });+ },+});++export default defineSchedule({+ cron: "0 2 * * *",+ async run({ to, appAuth, waitUntil }) {+ waitUntil(to(reports, {}).send("Prepare the nightly report.", { auth: appAuth }));+ },+});diff --git a/packages/eve/extension-contracts/compatibility/subagent/v28.ts b/packages/eve/extension-contracts/compatibility/subagent/v28.tsnew file mode 100644index 000000000..b31352f49--- /dev/null+++ b/packages/eve/extension-contracts/compatibility/subagent/v28.ts@@ -0,0 +1,7 @@+import { defineAgent } from "#public/index.js";++// Epoch 28 `turn.waiting` events had no `on`; epoch 29 adds it.+export default defineAgent({+ description: "Research the incidents behind a metric change.",+ model: "openai/gpt-5.6-sol",+});diff --git a/packages/eve/extension-contracts/compatibility/tool/v72.ts b/packages/eve/extension-contracts/compatibility/tool/v72.tsnew file mode 100644index 000000000..08d10e77f--- /dev/null+++ b/packages/eve/extension-contracts/compatibility/tool/v72.ts@@ -0,0 +1,11 @@+import { z } from "zod";+import { defineTool } from "#public/tools/index.js";++// Epoch 72 `turn.waiting` events had no `on`; epoch 73 adds it.+// Tools that need approval keep working.+export default defineTool({+ description: "Summarize the open incidents for a service.",+ inputSchema: z.object({ service: z.string() }),+ outputSchema: z.object({ service: z.string(), summary: z.string() }),+ execute: ({ service }) => ({ service, summary: `No open incidents for ${service}.` }),+});diff --git a/packages/eve/extension-contracts/reports/channel/v45.json b/packages/eve/extension-contracts/reports/channel/v45.jsonnew file mode 100644index 000000000..0661702f6--- /dev/null+++ b/packages/eve/extension-contracts/reports/channel/v45.json@@ -0,0 +1,21 @@+{+ "kind": "eve-extension-capability-contract",+ "capability": "channel",+ "epoch": 45,+ "sha256": "6cec94190091cbd08fbdc319c16c2eee9f6ebae21e4bac875814dad85f16a270",+ "exports": [+ "DELETE",+ "GET",+ "HEAD",+ "OPTIONS",+ "PATCH",+ "POST",+ "PUT",+ "WS",+ "createWebSocketUpgradeServer",+ "defineChannel",+ "disableRoute",+ "isChannel",+ "isDisabledRouteSentinel"+ ]+}diff --git a/packages/eve/extension-contracts/reports/connection/v38.json b/packages/eve/extension-contracts/reports/connection/v38.jsonnew file mode 100644index 000000000..de3db23ae--- /dev/null+++ b/packages/eve/extension-contracts/reports/connection/v38.json@@ -0,0 +1,16 @@+{+ "kind": "eve-extension-capability-contract",+ "capability": "connection",+ "epoch": 38,+ "sha256": "c59b98d02693353eb17496370965b2e7dfb013bd3fa01b4bfe143b8ec1f44c64",+ "exports": [+ "ConnectionAuthorizationFailedError",+ "ConnectionAuthorizationRequiredError",+ "defineDynamic",+ "defineInteractiveAuthorization",+ "defineMcpClientConnection",+ "defineOpenAPIConnection",+ "isConnectionAuthorizationFailedError",+ "isConnectionAuthorizationRequiredError"+ ]+}diff --git a/packages/eve/extension-contracts/reports/dynamicInstructions/v32.json b/packages/eve/extension-contracts/reports/dynamicInstructions/v32.jsonnew file mode 100644index 000000000..a530b3083--- /dev/null+++ b/packages/eve/extension-contracts/reports/dynamicInstructions/v32.json@@ -0,0 +1,7 @@+{+ "kind": "eve-extension-capability-contract",+ "capability": "dynamicInstructions",+ "epoch": 32,+ "sha256": "c423755d899dd76a549b6c3510d1a72997e17aa03da0cabfa9775305686045a0",+ "exports": ["defineDynamic"]+}diff --git a/packages/eve/extension-contracts/reports/dynamicSkill/v31.json b/packages/eve/extension-contracts/reports/dynamicSkill/v31.jsonnew file mode 100644index 000000000..ab54aab1f--- /dev/null+++ b/packages/eve/extension-contracts/reports/dynamicSkill/v31.json@@ -0,0 +1,7 @@+{+ "kind": "eve-extension-capability-contract",+ "capability": "dynamicSkill",+ "epoch": 31,+ "sha256": "332c758f8c0f1063bae84c2dbac01538e8752cd1c35fed3f7893d17215cca330",+ "exports": ["defineDynamic"]+}diff --git a/packages/eve/extension-contracts/reports/dynamicTool/v70.json b/packages/eve/extension-contracts/reports/dynamicTool/v70.jsonnew file mode 100644index 000000000..0b45298b9--- /dev/null+++ b/packages/eve/extension-contracts/reports/dynamicTool/v70.json@@ -0,0 +1,16 @@+{+ "kind": "eve-extension-capability-contract",+ "capability": "dynamicTool",+ "epoch": 70,+ "sha256": "638783cffe7483b30611dc36f2bb2034b5cc515d0e8200225c563406be1fea26",+ "exports": [+ "DynamicToolEntry",+ "DynamicToolEvents",+ "DynamicToolResult",+ "DynamicToolSet",+ "auto",+ "defineDurableCallback",+ "defineDurableSchema",+ "defineDynamic"+ ]+}diff --git a/packages/eve/extension-contracts/reports/hook/v37.json b/packages/eve/extension-contracts/reports/hook/v37.jsonnew file mode 100644index 000000000..be064846b--- /dev/null+++ b/packages/eve/extension-contracts/reports/hook/v37.json@@ -0,0 +1,7 @@+{+ "kind": "eve-extension-capability-contract",+ "capability": "hook",+ "epoch": 37,+ "sha256": "c35b5f9255d87278eb0495252161d2d68197fe630a071bb479d93e46834bbed1",+ "exports": ["defineHook"]+}diff --git a/packages/eve/extension-contracts/reports/schedule/v26.json b/packages/eve/extension-contracts/reports/schedule/v26.jsonnew file mode 100644index 000000000..fddc66b46--- /dev/null+++ b/packages/eve/extension-contracts/reports/schedule/v26.json@@ -0,0 +1,15 @@+{+ "kind": "eve-extension-capability-contract",+ "capability": "schedule",+ "epoch": 26,+ "sha256": "74b48388caa986aff196d6fd92f0bf0315a268c06c8de4e199fae0e9d6266c69",+ "exports": [+ "ScheduleDefinition",+ "ScheduleHandlerArgs",+ "ScheduleRunHandler",+ "ScheduleToFn",+ "TypedReceiveTarget",+ "defineSchedule",+ "isScheduleAuth"+ ]+}diff --git a/packages/eve/extension-contracts/reports/subagent/v29.json b/packages/eve/extension-contracts/reports/subagent/v29.jsonnew file mode 100644index 000000000..2b6f18d9c--- /dev/null+++ b/packages/eve/extension-contracts/reports/subagent/v29.json@@ -0,0 +1,23 @@+{+ "kind": "eve-extension-capability-contract",+ "capability": "subagent",+ "epoch": 29,+ "sha256": "816364d8a8b9443393f0b62c006205303d7b504649ec485343d3e8aad6d87470",+ "exports": [+ "AgentCompactionDefinition",+ "AgentDefinition",+ "AgentModelDefinition",+ "AgentStaticModelDefinition",+ "DefinedAgent",+ "DynamicLocalSubagentDefinition",+ "DynamicSubagentDefinition",+ "RemoteAgentDefinition",+ "RemoteAgentDefinitionInput",+ "WorkspaceAgentDefinition",+ "WorkspaceAgentTransport",+ "defineAgent",+ "defineDynamic",+ "defineRemoteAgent",+ "defineWorkspaceAgent"+ ]+}diff --git a/packages/eve/extension-contracts/reports/tool/v73.json b/packages/eve/extension-contracts/reports/tool/v73.jsonnew file mode 100644index 000000000..18aca3702--- /dev/null+++ b/packages/eve/extension-contracts/reports/tool/v73.json@@ -0,0 +1,29 @@+{+ "kind": "eve-extension-capability-contract",+ "capability": "tool",+ "epoch": 73,+ "sha256": "ee3c7f3e7acbba3917c139eade1d6c1cce9c33684ed4b21ff768975595ee5e62",+ "exports": [+ "AgentRouterInput",+ "AgentRouterTool",+ "WorkflowStepToolContext",+ "WorkflowTool",+ "WorkflowToolDefinition",+ "WorkflowToolInput",+ "WorkflowToolOptions",+ "agentRouter",+ "defaultWebSearch",+ "defineTool",+ "defineWorkflowTool",+ "disableTool",+ "evaluate",+ "isDisabledToolSentinel",+ "isWebSearchToolDefinition",+ "serializeModelInputSchema",+ "toolOutput",+ "toolOutputPart",+ "toolResultFrom",+ "webSearch",+ "workflow"+ ]+}diff --git a/packages/eve/src/cli/dev/tui/runner.integration.test.ts b/packages/eve/src/cli/dev/tui/runner.integration.test.tsindex e15a11b3b..715a9a237 100644--- a/packages/eve/src/cli/dev/tui/runner.integration.test.ts+++ b/packages/eve/src/cli/dev/tui/runner.integration.test.ts@@ -247,7 +247,7 @@ describe("eve dev conversation", () => {taskId: "task_1",turnId: "turn_1",}),- createTurnWaitingEvent({ usage: TEST_USAGE, sequence: 1, turnId: "turn_1" }),+ createTurnWaitingEvent({ on: "input", usage: TEST_USAGE, sequence: 1, turnId: "turn_1" }),],"delivery_1",);@@ -360,7 +360,7 @@ describe("eve dev conversation", () => {}),]),approval("task_1"),- createTurnWaitingEvent({ usage: TEST_USAGE, sequence: 2, turnId: "turn_1" }),+ createTurnWaitingEvent({ on: "input", usage: TEST_USAGE, sequence: 2, turnId: "turn_1" }),],"delivery_1",);@@ -369,7 +369,7 @@ describe("eve dev conversation", () => {server.emit([approval("task_2"),- createTurnWaitingEvent({ usage: TEST_USAGE, sequence: 2, turnId: "turn_1" }),+ createTurnWaitingEvent({ on: "input", usage: TEST_USAGE, sequence: 2, turnId: "turn_1" }),],"delivery_1",);diff --git a/packages/eve/src/cli/dev/tui/terminal-renderer.test.ts b/packages/eve/src/cli/dev/tui/terminal-renderer.test.tsindex 110899759..90fd4333b 100644--- a/packages/eve/src/cli/dev/tui/terminal-renderer.test.ts+++ b/packages/eve/src/cli/dev/tui/terminal-renderer.test.ts@@ -3836,7 +3836,9 @@ describe("TerminalRenderer conversation", () => {turnId: "turn_1",}),),- stamped(createTurnWaitingEvent({ usage: TEST_USAGE, sequence: 3, turnId: "turn_1" })),+ stamped(+ createTurnWaitingEvent({ on: "tasks", usage: TEST_USAGE, sequence: 3, turnId: "turn_1" }),+ ),];renderer.renderConversation(conversationOf(working, { working: true }));const during = screen.snapshot();diff --git a/packages/eve/src/cli/dev/tui/transcript.test.ts b/packages/eve/src/cli/dev/tui/transcript.test.tsindex 8a2501deb..f5c36364f 100644--- a/packages/eve/src/cli/dev/tui/transcript.test.ts+++ b/packages/eve/src/cli/dev/tui/transcript.test.ts@@ -305,7 +305,9 @@ describe("ConversationTranscript", () => {taskStarted("call_1", "summarize"),toolResult("call_1", "summarize"),toolCall("wait_1", "task_wait"),- event(createTurnWaitingEvent({ usage: TEST_USAGE, sequence: 3, turnId: "turn_1" })),+ event(+ createTurnWaitingEvent({ on: "tasks", usage: TEST_USAGE, sequence: 3, turnId: "turn_1" }),+ ),]);const transcript = new ConversationTranscript();const working = view(state, true);diff --git a/packages/eve/src/client/conversation-reducer.test.ts b/packages/eve/src/client/conversation-reducer.test.tsindex 626232050..a57ed83d1 100644--- a/packages/eve/src/client/conversation-reducer.test.ts+++ b/packages/eve/src/client/conversation-reducer.test.ts@@ -172,7 +172,7 @@ describe("conversation reducer tasks and agent sessions", () => {it("marks a parked turn waiting until it resumes or ends", () => {const parked = reduce([createTurnStartedEvent({ sequence: 0, turnId: "t1" }),- createTurnWaitingEvent({ usage: TEST_USAGE, sequence: 0, turnId: "t1" }),+ createTurnWaitingEvent({ on: "tasks", usage: TEST_USAGE, sequence: 0, turnId: "t1" }),]);expect(parked.turns.t1).toEqual({ turnId: "t1", status: "active", waiting: true });expect(parked.activeTurnId).toBe("t1");@@ -183,7 +183,7 @@ describe("conversation reducer tasks and agent sessions", () => {expect(resumed.turns.t1).toEqual({ turnId: "t1", status: "active" });const ended = reduce([- createTurnWaitingEvent({ usage: TEST_USAGE, sequence: 0, turnId: "t1" }),+ createTurnWaitingEvent({ on: "tasks", usage: TEST_USAGE, sequence: 0, turnId: "t1" }),createTurnCompletedEvent({ sequence: 0, turnId: "t1" }),],resumed,diff --git a/packages/eve/src/client/eve-agent-store.test.ts b/packages/eve/src/client/eve-agent-store.test.tsindex a63e5594a..79e81036c 100644--- a/packages/eve/src/client/eve-agent-store.test.ts+++ b/packages/eve/src/client/eve-agent-store.test.ts@@ -456,7 +456,7 @@ describe("EveAgentStore held turns", () => {taskId: "task_1",turnId: "turn_1",}),- createTurnWaitingEvent({ usage: TEST_USAGE, sequence: 0, turnId: "turn_1" }),+ createTurnWaitingEvent({ on: "input", usage: TEST_USAGE, sequence: 0, turnId: "turn_1" }),]).map((event) => ({ ...event, meta: { ...event.meta, deliveryIds: ["delivery_1"] } }));for (const event of parked) live.emit(event);await sending;diff --git a/packages/eve/src/client/index.ts b/packages/eve/src/client/index.tsindex a50230c12..9449ab054 100644--- a/packages/eve/src/client/index.ts+++ b/packages/eve/src/client/index.ts@@ -149,6 +149,7 @@ export type {TurnFailedStreamEvent,TurnStartedStreamEvent,TurnFailureStreamEvent,+ TurnWaitingOn,TurnWaitingStreamEvent,} from "#protocol/message.js";diff --git a/packages/eve/src/client/session-utils.test.ts b/packages/eve/src/client/session-utils.test.tsindex c55fda665..8f1d72049 100644--- a/packages/eve/src/client/session-utils.test.ts+++ b/packages/eve/src/client/session-utils.test.ts@@ -2,7 +2,7 @@ import { describe, expect, it } from "vitest";import type { UnstampedMessageStreamEvent } from "#protocol/message.js";-import { collectTurnEvents, summarizeTurnEvents } from "./session-utils.js";+import { collectTurnEvents, summarizeTurnEvents, TurnSegment } from "./session-utils.js";const eventData = { sequence: 1, stepIndex: 0, turnId: "turn_1" };@@ -171,3 +171,60 @@ describe("collectTurnEvents", () => {]);});});++describe("TurnSegment", () => {+ const signIn: UnstampedMessageStreamEvent = {+ type: "authorization.required",+ data: { ...eventData, description: "Sign in", name: "linear", webhookUrl: "https://auth" },+ };+ const held: UnstampedMessageStreamEvent = {+ type: "turn.waiting",+ data: { on: "input", sequence: 1, turnId: "turn_1" },+ };++ it("reads past a held sign-in only while following its callback", () => {+ const resumed: UnstampedMessageStreamEvent[] = [+ {+ type: "authorization.completed",+ data: { ...eventData, name: "linear", outcome: "authorized" },+ },+ {+ type: "session.waiting",+ data: { continuationToken: "session-id", wait: "next-user-message" },+ },+ ];+ const following = new TurnSegment({ followCallbacks: true });+ expect([signIn, held, ...resumed].map((event) => following.observe(event))).toEqual([+ false,+ false,+ false,+ true,+ ]);++ const plain = new TurnSegment();+ expect([signIn, held].map((event) => plain.observe(event))).toEqual([false, true]);+ });++ it("stops at a held sign-in when an approval also waits on the person", () => {+ const segment = new TurnSegment({ followCallbacks: true });+ const approval: UnstampedMessageStreamEvent = {+ type: "input.requested",+ data: {+ ...eventData,+ requests: [+ {+ action: { callId: "call_1", input: {}, kind: "tool-call", toolName: "deploy" },+ kind: "tool-approval",+ prompt: "Approve deploy?",+ requestId: "req_1",+ },+ ],+ },+ };+ expect([approval, signIn, held].map((event) => segment.observe(event))).toEqual([+ false,+ false,+ true,+ ]);+ });+});diff --git a/packages/eve/src/client/session-utils.ts b/packages/eve/src/client/session-utils.tsindex 9d8af0d25..a78f8773d 100644--- a/packages/eve/src/client/session-utils.ts+++ b/packages/eve/src/client/session-utils.ts@@ -76,16 +76,19 @@ export async function collectTurnEvents(/*** Returns true when a read of a session's events ends at `event`: at a current-turn boundary, or- * at `turn.waiting` while a request is unanswered. The turn stays open there until a person- * answers; otherwise `turn.waiting` is informational and reading goes on to the turn's real end.- * A reader that follows sign-in callbacks also reads past `session.waiting` while one is- * outstanding, because the callback resumes the same turn.+ * at a `turn.waiting` on `"input"` (or while a request read in the segment is unanswered). The+ * turn stays open there until a person acts; a `turn.waiting` on `"tasks"` is informational and+ * reading goes on to the turn's real end.+ * A reader that follows sign-in callbacks reads past a sign-in's `turn.waiting` or+ * `session.waiting` while its callback is outstanding, because the callback resumes the same turn.*/export function endsTurnSegment(event: UnstampedMessageStreamEvent,open: { readonly callbacks: boolean; readonly requests: boolean },): boolean {- if (event.type === "turn.waiting") return open.requests;+ if (event.type === "turn.waiting") {+ return open.requests || (event.data.on === "input" && !open.callbacks);+ }return isCurrentTurnBoundaryEvent(event) && (event.type !== "session.waiting" || !open.callbacks);}diff --git a/packages/eve/src/client/session.test.ts b/packages/eve/src/client/session.test.tsindex a867cbbb2..6b0e57b1d 100644--- a/packages/eve/src/client/session.test.ts+++ b/packages/eve/src/client/session.test.ts@@ -1204,6 +1204,35 @@ describe("ClientSession", () => {expect(result.inputRequests.at(-1)?.requestId).toBe("request_2");});+ it("stops at a turn.waiting on input when its request was read earlier", async () => {+ const turn = { sequence: 0, stepIndex: 0, turnId: "turn_0" };+ vi.spyOn(globalThis, "fetch").mockImplementation(async (_request, init) =>+ (init?.method ?? "GET") === "POST"+ ? createAcceptedResponse()+ : createStreamResponse([+ {+ type: "approval.candidate",+ data: { ...turn, candidateId: "c1", outcome: "rejected", requestId: "request_1" },+ },+ {+ type: "turn.waiting",+ data: { on: "input", sequence: 0, turnId: "turn_0" },+ },+ { type: "turn.completed", data: { sequence: 0, turnId: "turn_0" } },+ ]),+ );++ const result = await (+ await createSession().respond([{ optionId: "approve", requestId: "request_1" }])+ ).result();++ expect(result.events.map((event) => event.type)).toEqual([+ "approval.candidate",+ "turn.waiting",+ ]);+ expect(result.status).toBe("waiting");+ });+it("honors an explicit idle reconnect limit for an active turn", async () => {const fetchMock = vi.spyOn(globalThis, "fetch").mockImplementation(async (_request, init) => {return (init?.method ?? "GET") === "POST"diff --git a/packages/eve/src/compiler/extension-compatibility.ts b/packages/eve/src/compiler/extension-compatibility.tsindex fe9efe275..77c274f10 100644--- a/packages/eve/src/compiler/extension-compatibility.ts+++ b/packages/eve/src/compiler/extension-compatibility.ts@@ -22,10 +22,10 @@ interface ExtensionCapabilityContract {const EXTENSION_CAPABILITY_CONTRACTS = {extension: { current: 1, supported: [1], dropped: {} },tool: {- current: 72,+ current: 73,supported: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 29, 30, 31, 32, 34, 35, 54, 55, 63, 64, 65, 66, 67,- 68, 69, 70, 71, 72,+ 68, 69, 70, 71, 72, 73,],dropped: {14: "TaskExec.delegated was removed; migrate to workflow-backed background tools",@@ -72,10 +72,10 @@ const EXTENSION_CAPABILITY_CONTRACTS = {},},dynamicTool: {- current: 69,+ current: 70,supported: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 22, 31, 32, 33, 52, 54,- 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,+ 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,],dropped: {21: "Message and reasoning append events now expose deltas instead of cumulative snapshots.",@@ -114,10 +114,10 @@ const EXTENSION_CAPABILITY_CONTRACTS = {},},channel: {- current: 44,+ current: 45,supported: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 29, 31, 36, 37, 38, 39, 40, 41, 42,- 43, 44,+ 43, 44, 45,],dropped: {12: "Message and reasoning append events now expose deltas instead of cumulative snapshots.",@@ -139,8 +139,8 @@ const EXTENSION_CAPABILITY_CONTRACTS = {},},schedule: {- current: 25,- supported: [1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18, 19, 20, 21, 22, 23, 24, 25],+ current: 26,+ supported: [1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18, 19, 20, 21, 22, 23, 24, 25, 26],dropped: {5: "Message and reasoning append events now expose deltas instead of cumulative snapshots.",16: 'Background task delivery was removed: sends no longer accept taskDeliveryPolicy and message.completed always carries text. input.resolved reports outcome "cancelled" when a workflow tool withdraws a ctx.ask() request, and stream events gained agent.started, published when a workflow run opens a ctx.agent session, and turn.waiting, which parks the open turn instead of emitting turn.completed and session.waiting.',@@ -148,8 +148,8 @@ const EXTENSION_CAPABILITY_CONTRACTS = {},},subagent: {- current: 28,- supported: [3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 19, 23, 24, 25, 26, 27, 28],+ current: 29,+ supported: [3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 19, 23, 24, 25, 26, 27, 28, 29],dropped: {1: "Persistent subagent sessions are now the default and the experimental opt-in was removed",2: "Persistent subagent sessions are now the default and the experimental opt-in was removed",@@ -162,10 +162,10 @@ const EXTENSION_CAPABILITY_CONTRACTS = {},},connection: {- current: 37,+ current: 38,supported: [1, 2, 3, 4, 5, 6, 7, 8, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 24, 26, 32, 33, 34,- 35, 36, 37,+ 35, 36, 37, 38,],dropped: {9: "Dynamic connection resolvers no longer receive conversation or channel continuation data",@@ -180,8 +180,8 @@ const EXTENSION_CAPABILITY_CONTRACTS = {},},hook: {- current: 36,- supported: [20, 21, 22, 23, 25, 27, 31, 32, 33, 34, 35, 36],+ current: 37,+ supported: [20, 21, 22, 23, 25, 27, 31, 32, 33, 34, 35, 36, 37],dropped: {1: "Model identity moved from session.started runtime metadata to step.started call attribution.",2: "Model identity moved from session.started runtime metadata to step.started call attribution.",@@ -211,9 +211,10 @@ const EXTENSION_CAPABILITY_CONTRACTS = {},skill: { current: 2, supported: [1, 2], dropped: {} },dynamicSkill: {- current: 30,+ current: 31,supported: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 25, 26, 27, 28, 29, 30,+ 31,],dropped: {13: "Message and reasoning append events now expose deltas instead of cumulative snapshots.",@@ -224,10 +225,10 @@ const EXTENSION_CAPABILITY_CONTRACTS = {},instructions: { current: 2, supported: [1, 2], dropped: {} },dynamicInstructions: {- current: 31,+ current: 32,supported: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 19, 20, 21, 22, 26, 27, 28, 29, 30,- 31,+ 31, 32,],dropped: {14: "Message and reasoning append events now expose deltas instead of cumulative snapshots.",diff --git a/packages/eve/src/evals/runner/derive-run-facts.test.ts b/packages/eve/src/evals/runner/derive-run-facts.test.tsindex 626858168..97f779e41 100644--- a/packages/eve/src/evals/runner/derive-run-facts.test.ts+++ b/packages/eve/src/evals/runner/derive-run-facts.test.ts@@ -495,7 +495,7 @@ describe("deriveRunFacts", () => {expect(derive([sessionWaiting(USAGE),- { type: "turn.waiting", data: { sequence: 2, turnId: "t2", usage: parked } },+ { type: "turn.waiting", data: { on: "input", sequence: 2, turnId: "t2", usage: parked } },]).usage,).toEqual(parked);});diff --git a/packages/eve/src/execution/session/active-turn.ts b/packages/eve/src/execution/session/active-turn.tsnew file mode 100644index 000000000..fac67fc3d--- /dev/null+++ b/packages/eve/src/execution/session/active-turn.ts@@ -0,0 +1,283 @@+import type { DeliverHookPayload, TurnCaller } from "#channel/types.js";+import { routeDeliverToChildren } from "#execution/route-child-delivery.js";+import { admitSessionInboxPayload } from "#execution/session/admission.js";+import {+ isSteeringMessage,+ type SteeringOptions,+ type SteeringTurn,+} from "#execution/session/input-queue.js";+import { routeSelectedDelivery } from "#execution/session/route-selected-delivery.js";+import type { SessionExecutionInput } from "#execution/session/turn.js";+import type { SessionInboxPayload } from "#execution/session-inbox/inbox.js";+import { decodeSessionInboxPayload } from "#execution/session-inbox/protocol.js";+import type { WorkflowToolRunMessage } from "#execution/tools/workflow/messages.js";+import { activeTurnId } from "#harness/active-turn-id.js";+import { coalesceDeliveries } from "#harness/messages.js";+import { TurnCancelledError } from "#harness/turn-cancellation.js";+import type { RuntimeActionResult } from "#shared/action-types.js";++export type RuntimeEvent =+ | { readonly kind: "runtime-action-result"; readonly results: readonly RuntimeActionResult[] }+ | { readonly kind: "workflow"; readonly message: WorkflowToolRunMessage }+ /** A steering message that answered no pending request arrived during the wait. */+ | { readonly kind: "steering" }+ /** A `task_wait` call's timeout passed. */+ | { readonly kind: "timeout"; readonly callId: string }+ /** A delivery or sign-in callback was admitted; a held request may be answered. */+ | { readonly kind: "input" }+ | "cancelled";++/**+ * Admission policy, cancellation, and steering for one active turn.+ * Deliveries admitted while a model step runs stay in the shared queue until+ * the turn reaches a committed boundary; a runtime-action wait routes them+ * eagerly so a proxied child can receive the answer it is blocked on.+ *+ * The pump signals cancellation and eligible steering immediately. Cancellation+ * aborts the turn; steering only interrupts generation before assistant output+ * or local tool execution. Both retain ordered admission at the next boundary.+ */+export class ActiveTurn {+ private readonly admitted = new Set<number>();+ private readonly routedToChildren = new Set<number>();+ private readonly runtimeResults: RuntimeEvent[] = [];+ private readonly controller = new AbortController();+ private readonly expectedTurnId: string;+ private readonly input: SessionExecutionInput;+ /** Who alone steers the turn: its principal, or its delegated caller. */+ private readonly identity: SteeringTurn;+ private readonly unsubscribe: () => void;+ private unsubscribeDelivery: () => void;+ private steeringController = new AbortController();+ /** The delegated caller of the latest message the turn read. */+ caller: TurnCaller | undefined;++ constructor(+ input: SessionExecutionInput,+ owner: { readonly caller: TurnCaller | undefined; readonly principal: string },+ ) {+ this.input = input;+ this.caller = owner.caller;+ this.identity = { callerCallId: owner.caller?.callId, principal: owner.principal };+ this.expectedTurnId = activeTurnId(input.cursor.sessionState.emissionState);+ this.unsubscribe = input.inbox.onInterrupt((payload) => {+ if (this.cancelsThisTurn(payload)) this.abort();+ });+ this.unsubscribeDelivery = input.inbox.onDelivery(this.signalSteering);+ }++ private readonly signalSteering = (payload: SessionInboxPayload): void => {+ let delivery;+ try {+ delivery = decodeSessionInboxPayload(payload);+ } catch {+ return;+ }+ if (+ delivery.kind === "deliver" &&+ isSteeringMessage(delivery, this.identity) &&+ !this.input.cursor.sessionState.hasProxyInputRequests+ )+ this.steeringController.abort();+ };++ get signal(): AbortSignal {+ return this.controller.signal;+ }++ dispose(): void {+ this.unsubscribe();+ this.unsubscribeDelivery();+ }++ get steeringSignal(): AbortSignal {+ return this.steeringController.signal;+ }++ private resetSteering(): void {+ if (!this.steeringController.signal.aborted) return;+ this.unsubscribeDelivery();+ this.steeringController = new AbortController();+ // Admission can yield while new deliveries are pumped. Replaying the+ // unread inbox keeps those arrivals attached to the next generation.+ this.unsubscribeDelivery = this.input.inbox.onDelivery(this.signalSteering);+ }++ /** Admits everything the pump accepted while the last step ran. */+ async admitBoundary(): Promise<void> {+ const pending = this.input.inbox.drain();+ for (const payload of pending) await this.admit(payload);+ }++ /**+ * Steering admitted during this turn, routed to children first and coalesced.+ * The next step reads it as input, so its signal must not interrupt that+ * step; only deliveries still unread re-signal the next generation.+ */+ async takeSteering(options?: SteeringOptions): Promise<DeliverHookPayload | undefined> {+ const steering: DeliverHookPayload[] = [];+ while (true) {+ const selection = this.input.queue.takeSteering(this.admitted, this.identity, options);+ if (selection === undefined) break;+ for (const sequence of selection.sequences) this.admitted.delete(sequence);+ const routed = await routeSelectedDelivery(selection, this.input.cursor);+ if (routed.kind === "cancel-turn") {+ this.abort();+ break;+ }+ if (routed.kind === "turn") steering.push(routed.delivery);+ }+ this.resetSteering();+ if (steering.length === 0) return undefined;+ const delivery = steering.length === 1 ? steering[0]! : coalesceDeliveries(steering);+ if (delivery.caller !== undefined) this.caller = delivery.caller;+ return delivery;+ }++ /**+ * Removes the admitted run messages the session applies at once instead of+ * in a runtime wait, or only those of `kind`.+ */+ takeBoundaryMessages(kind?: "agent-started"): WorkflowToolRunMessage[] {+ const taken: WorkflowToolRunMessage[] = [];+ const kept: RuntimeEvent[] = [];+ for (const event of this.runtimeResults) {+ const message = asBoundaryMessage(event);+ if (message === undefined || (kind !== undefined && message.kind !== kind)) kept.push(event);+ else taken.push(message);+ }+ this.runtimeResults.splice(0, this.runtimeResults.length, ...kept);+ return taken;+ }++ /**+ * Next runtime result, workflow message, steering, or timeout, admitting+ * inbox traffic while waiting. Deliveries admitted before the wait began are+ * routed first, so a message that arrived as the calls started still steers.+ * `timers` are durable sleeps raced against the inbox.+ */+ async nextRuntimeEvent(timers: readonly Promise<string>[]): Promise<RuntimeEvent> {+ while (true) {+ const steered = await this.routeAdmittedToChildren();+ if (this.signal.aborted) return "cancelled";+ if (steered) return { kind: "steering" };+ const event = this.runtimeResults.shift();+ if (event !== undefined) return event;+ const elapsed = await this.waitForInboxOrTimer(timers);+ if (elapsed !== undefined) return { callId: elapsed, kind: "timeout" };+ const payload = await this.input.inbox.next();+ if (payload === undefined)+ throw new Error("Session inbox closed before runtime actions completed.");+ if (await this.admit(payload)) return { kind: "input" };+ }+ }++ /**+ * Takes one admitted delivery that answers a pending input request, from any+ * responder: an approver need not be the turn's own person. Returns+ * `undefined` when no admitted delivery answers one.+ */+ takeInputResponses(requestIds: ReadonlySet<string>): DeliverHookPayload | undefined {+ for (const sequence of this.admitted) {+ const delivery = this.input.queue.delivery(sequence);+ if (delivery === undefined) continue;+ const answers = delivery.payloads.some((payload) =>+ payload.inputResponses?.some((response) => requestIds.has(response.requestId)),+ );+ if (!answers) continue;+ this.admitted.delete(sequence);+ this.input.queue.replaceDelivery(sequence, undefined);+ return delivery;+ }+ return undefined;+ }++ /** Resolves with the id of the call whose timer won, or `undefined` once inbox input is ready. */+ private async waitForInboxOrTimer(+ timers: readonly Promise<string>[],+ ): Promise<string | undefined> {+ if (timers.length === 0 || this.input.inbox.hasPending()) return undefined;+ const inboxReady = this.input.inbox.whenPending().then(() => undefined);+ return await Promise.race([inboxReady, ...timers]);+ }++ private cancelsThisTurn(payload: SessionInboxPayload): boolean {+ if (payload.kind === "reset") return true;+ if (payload.kind !== "cancel") return false;+ return payload.turnId === undefined || payload.turnId === this.expectedTurnId;+ }++ /** Admits one payload; returns whether it was a delivery or sign-in callback. */+ private async admit(value: SessionInboxPayload): Promise<boolean> {+ const admitted = await admitSessionInboxPayload(value, this.input);+ switch (admitted.kind) {+ case "delivery":+ this.admitted.add(admitted.admission.sequence);+ return true;+ case "runtime-action-result":+ this.runtimeResults.push({+ kind: "runtime-action-result",+ results: admitted.payload.results,+ });+ return false;+ case "workflow":+ this.runtimeResults.push({ kind: "workflow", message: admitted.message });+ return false;+ case "cancel":+ if (this.cancelsThisTurn(value)) this.abort();+ return false;+ case "consumed":+ return value.kind === "authorization-callback";+ }+ }++ /**+ * During a runtime wait, descendant-bound answers cannot wait for the+ * boundary. Returns whether a newly routed delivery still steers: it+ * answered nothing, so its message is for the model.+ */+ private async routeAdmittedToChildren(): Promise<boolean> {+ let steered = false;+ for (const sequence of this.admitted) {+ if (this.routedToChildren.has(sequence)) continue;+ const delivery = this.input.queue.delivery(sequence);+ if (delivery === undefined) {+ this.admitted.delete(sequence);+ continue;+ }+ this.routedToChildren.add(sequence);+ const routed = await this.input.cursor.advance((state) =>+ routeDeliverToChildren({ delivery, ...state }),+ );+ if (routed.kind === "cancel-turn") {+ this.input.queue.replaceDelivery(sequence, undefined);+ this.admitted.delete(sequence);+ this.abort();+ return false;+ }+ this.input.queue.replaceDelivery(sequence, routed.remainder);+ if (routed.remainder === undefined) {+ this.admitted.delete(sequence);+ continue;+ }+ if (isSteeringMessage(routed.remainder, this.identity)) steered = true;+ }+ return steered;+ }++ private abort(): void {+ if (this.controller.signal.aborted) return;+ this.controller.abort(new TurnCancelledError());+ }+}++function asBoundaryMessage(event: RuntimeEvent): WorkflowToolRunMessage | undefined {+ if (event === "cancelled" || event.kind !== "workflow") return undefined;+ // Tasks run beside the turn, so everything their runs send, such as a+ // question, reaches the session at the next boundary rather than at turn end.+ if (event.message.from.taskId !== undefined) return event.message;+ // An `execute` run can open a session after its call returns, when the turn+ // may have no wait left to take the message, and publishing it needs none.+ if (event.message.kind === "agent-started") return event.message;+ return undefined;+}diff --git a/packages/eve/src/execution/session/entry-authorization.integration.test.ts b/packages/eve/src/execution/session/entry-authorization.integration.test.tsindex 5c540d85e..997fbbc8c 100644--- a/packages/eve/src/execution/session/entry-authorization.integration.test.ts+++ b/packages/eve/src/execution/session/entry-authorization.integration.test.ts@@ -11,7 +11,6 @@ import { resumeHook, start } from "#internal/workflow/runtime.js";import { filterEventsByType } from "#internal/testing/events.js";import { createTestRuntime } from "#internal/testing/app-harness.js";import { waitForHook } from "#internal/testing/workflow-test-helpers.js";-import { waitForParkedTurnStep } from "#internal/testing/session-test-helpers.js";import { workflowEntry } from "#execution/session/entry.js";import {sessionCommandHookToken,@@ -173,13 +172,15 @@ describe("workflowEntry integration", () => {authorization: { displayName: "Weather" },});- // The authorization park closes its turn boundary so stream- // consumers do not hang on the parked turn.- const parkBoundary = await stream.nextUntil(- "authorization park boundary",- (event) => event.type === "session.waiting",+ // The sign-in holds the turn open, as a question or task does.+ const held = await stream.nextUntil(+ "authorization hold",+ (event) => event.type === "turn.waiting",);- expect(parkBoundary.at(-1)?.type).toBe("session.waiting");+ expect(held.map((event) => event.type)).toEqual(["turn.waiting"]);+ expect(filterEventsByType(held, "turn.waiting")[0]?.data).toMatchObject({+ turnId: "turn_0",+ });await expectHookClaims(run.runId, [sessionCommandHookToken(run.runId), continuationToken]);await resumeHook(sessionInboxHookToken(sessionCommandHookToken(run.runId)), {@@ -205,7 +206,11 @@ describe("workflowEntry integration", () => {const completed = filterEventsByType(authorizedTurn, "authorization.completed");expect(completeCalls()).toBe(1);- expectSingleTurn(authorizedTurn, "turn_1");+ // The held turn resumes: no new turn starts, and the same turn completes.+ expect(filterEventsByType(authorizedTurn, "turn.started")).toHaveLength(0);+ expect(filterEventsByType(authorizedTurn, "turn.completed")[0]?.data).toMatchObject({+ turnId: "turn_0",+ });expect(authorizedTurn.at(-1)?.type).toBe("session.waiting");expect(completed).toHaveLength(1);expect(completed[0]?.data).toMatchObject({@@ -251,32 +256,14 @@ describe("workflowEntry integration", () => {});});- it("runs ordinary deliveries while an authorization challenge stays open", async () => {+ it("queues another person's message behind a held sign-in", async () => {const { completeCalls, completedPrincipals, runtime } = await createWeatherAuthRuntime(- "workflow-entry-auth-open",+ "workflow-entry-auth-queued",);- const continuationToken = "http:workflow-entry-auth-open";+ const continuationToken = "http:workflow-entry-auth-queued";await runtime.run(async () => {- const run = await start(workflowEntry, [- {- kind: "initial",- ownerDeploymentId: "dpl_inline",- input: { message: "Use the get_weather tool to check the weather in Lisbon." },- serializedContext: buildSerializedContext({- auth: {- attributes: {},- authenticator: "test-idp",- issuer: "test-idp",- principalId: "user-1",- principalType: "user",- },- channelKind: "http",- continuationToken,- }),- },- ]);-+ const run = await startWeatherRun(continuationToken);const stream = captureEvents(run);try {@@ -284,109 +271,59 @@ describe("workflowEntry integration", () => {"initial auth-required event",(event) => event.type === "authorization.required",);- // Consume the park's own turn boundary so the next wait delimits- // the intervening message turn.- await stream.nextUntil(- "authorization park boundary",- (event) => event.type === "session.waiting",- );+ await stream.nextUntil("authorization hold", (event) => event.type === "turn.waiting");- // An ordinary message while the challenge is open runs as a normal- // turn instead of queueing behind the callback.await waitForHook({ runId: run.runId },{ token: sessionInboxHookToken(continuationToken) },);await resumeHook(sessionInboxHookToken(continuationToken), {- auth: {- attributes: {},- authenticator: "test-idp",- issuer: "test-idp",- principalId: "user-2",- principalType: "user",- },+ auth: userAuth("user-2"),kind: "send",payload: { message: "Quick status note while I sign in." },});-- const interveningTurn = await stream.nextUntil(- "intervening message turn",- (event) => event.type === "session.waiting",- );- expect(- interveningTurn.some(- (event) =>- event.type === "message.completed" &&- event.data.message?.includes("Quick status note while I sign in.") === true,- ),- ).toBe(true);- expect(filterEventsByType(interveningTurn, "authorization.completed")).toHaveLength(0);- expect(completeCalls()).toBe(0);-- // The callback still lands on the retained read and closes the- // challenge exactly once.await resumeHook(sessionInboxHookToken(sessionCommandHookToken(run.runId)), {kind: "authorization-callback",- payloads: [- {- authorizationCallback: {- attemptId: authorizationAttemptId(firstTurn),- callback: {- method: "GET",- params: { code: "oauth-code" },- },- connectionName: "weather",- },- },- ],+ payloads: [weatherCallback(authorizationAttemptId(firstTurn))],});- const callbackTurn = await stream.nextUntil(- "authorization callback turn",+ // The held turn resumes first, as user-1, and finishes the tool call.+ const resumed = await stream.nextUntil(+ "resumed held turn",(event) => event.type === "session.waiting",);- const completed = filterEventsByType(callbackTurn, "authorization.completed");- expectSingleTurn(callbackTurn, "turn_2");- expect(completed).toHaveLength(1);- // The attempt stays bound to user-1 even though user-2 spoke last.- expect(filterEventsByType(firstTurn, "authorization.required")[0]?.data.principalId).toBe(- "user-1",- );- expect(completed[0]?.data).toMatchObject({+ expect(filterEventsByType(resumed, "turn.started")).toHaveLength(0);+ expect(filterEventsByType(resumed, "authorization.completed")[0]?.data).toMatchObject({name: "weather",outcome: "authorized",principalId: "user-1",});-- // The granted authorization serves the next explicit tool request.- // (No waitForHook here: it only reports never-received hooks, and- // the continuation hook already received the intervening message.)- await resumeHook(sessionInboxHookToken(continuationToken), {- auth: {- attributes: {},- authenticator: "test-idp",- issuer: "test-idp",- principalId: "user-1",- principalType: "user",- },- kind: "send",- payload: { message: "Use the get_weather tool to check the weather in Lisbon." },+ expect(filterEventsByType(resumed, "turn.completed")[0]?.data).toMatchObject({+ turnId: "turn_0",});-- const toolTurn = await stream.nextUntil(- "post-authorization tool turn",- (event) => event.type === "session.waiting",- );+ expect(+ resumed.some(+ (event) =>+ event.type === "message.completed" &&+ event.data.message?.includes("authorized with weather-token") === true,+ ),+ ).toBe(true);expect(completeCalls()).toBe(1);expect(completedPrincipals()).toEqual([expect.objectContaining({ id: "user-1", type: "user" }),]);++ // user-2's message waited behind the held turn and runs next.+ const queued = await stream.nextUntil(+ "queued message turn",+ (event) => event.type === "session.waiting",+ );+ expectSingleTurn(queued, "turn_1");expect(- toolTurn.some(+ queued.some((event) =>event.type === "message.completed" &&- event.data.message?.includes("Used local weather tool for Lisbon") === true &&- event.data.message.includes("authorized with weather-token"),+ event.data.message?.includes("Quick status note while I sign in.") === true,),).toBe(true);} finally {@@ -396,31 +333,14 @@ describe("workflowEntry integration", () => {});});- it("ignores stale and duplicate callbacks after a challenge is replaced", async () => {+ it("cancels a held sign-in when the same person steers the turn, ignoring its late callback", async () => {const { completeCalls, runtime } = await createWeatherAuthRuntime(- "workflow-entry-auth-replaced",+ "workflow-entry-auth-steered",);- const continuationToken = "http:workflow-entry-auth-replaced";+ const continuationToken = "http:workflow-entry-auth-steered";await runtime.run(async () => {- const run = await start(workflowEntry, [- {- kind: "initial",- ownerDeploymentId: "dpl_inline",- input: { message: "Use the get_weather tool to check the weather in Lisbon." },- serializedContext: buildSerializedContext({- auth: {- attributes: {},- authenticator: "test-idp",- issuer: "test-idp",- principalId: "user-1",- principalType: "user",- },- channelKind: "http",- continuationToken,- }),- },- ]);+ const run = await startWeatherRun(continuationToken);const stream = captureEvents(run);try {@@ -428,65 +348,51 @@ describe("workflowEntry integration", () => {"first auth-required event",(event) => event.type === "authorization.required",);- await stream.nextUntil(- "first authorization park",- (event) => event.type === "session.waiting",- );+ await stream.nextUntil("first hold", (event) => event.type === "turn.waiting");await waitForHook({ runId: run.runId },{ token: sessionInboxHookToken(continuationToken) },);await resumeHook(sessionInboxHookToken(continuationToken), {+ auth: userAuth("user-1"),kind: "send",payload: { message: "Use the get_weather tool to check the weather in Lisbon." },});+ // The steer cancels the open sign-in, and the model's retry asks again+ // in the same turn.const replacementAttempt = await stream.nextUntil("replacement auth-required event",(event) => event.type === "authorization.required",);+ expect(filterEventsByType(replacementAttempt, "turn.started")).toHaveLength(0);expect(filterEventsByType(replacementAttempt, "authorization.completed")).toMatchObject([- { data: { name: "weather", outcome: "failed" } },+ { data: { name: "weather", outcome: "declined" } },]);- await stream.nextUntil(- "replacement authorization park",- (event) => event.type === "session.waiting",- );-- const stalePayload = {- authorizationCallback: {- attemptId: authorizationAttemptId(firstAttempt),- callback: { method: "GET", params: { code: "stale-oauth-code" } },- connectionName: "weather",- },- };- await resumeHook(sessionInboxHookToken(sessionCommandHookToken(run.runId)), {- kind: "authorization-callback",- payloads: [stalePayload],- });- await resumeHook(sessionInboxHookToken(sessionCommandHookToken(run.runId)), {- kind: "authorization-callback",- payloads: [stalePayload],- });-+ await stream.nextUntil("replacement hold", (event) => event.type === "turn.waiting");++ const stale = weatherCallback(authorizationAttemptId(firstAttempt), "stale-oauth-code");+ for (let i = 0; i < 2; i++) {+ await resumeHook(sessionInboxHookToken(sessionCommandHookToken(run.runId)), {+ kind: "authorization-callback",+ payloads: [stale],+ });+ }await resumeHook(sessionInboxHookToken(sessionCommandHookToken(run.runId)), {kind: "authorization-callback",- payloads: [- {- authorizationCallback: {- attemptId: authorizationAttemptId(replacementAttempt),- callback: { method: "GET", params: { code: "oauth-code" } },- connectionName: "weather",- },- },- ],+ payloads: [weatherCallback(authorizationAttemptId(replacementAttempt))],});- const callbackTurn = await stream.nextUntil(- "replacement authorization callback turn",+ const resumed = await stream.nextUntil(+ "resumed held turn",(event) => event.type === "session.waiting",);- expect(filterEventsByType(callbackTurn, "authorization.completed")).toHaveLength(1);+ expect(filterEventsByType(resumed, "authorization.completed")).toMatchObject([+ { data: { outcome: "authorized" } },+ ]);+ expect(filterEventsByType(resumed, "turn.completed")[0]?.data).toMatchObject({+ turnId: "turn_0",+ });expect(completeCalls()).toBe(1);} finally {stream.dispose();@@ -495,30 +401,12 @@ describe("workflowEntry integration", () => {});});- it("completes the challenge after a no-op cancel consumed the parked wait", async () => {+ it("cancelling a held turn drops its sign-in, so a late callback does nothing", async () => {const { completeCalls, runtime } = await createWeatherAuthRuntime("workflow-entry-auth-cancel");const continuationToken = "http:workflow-entry-auth-cancel";await runtime.run(async () => {- const run = await start(workflowEntry, [- {- kind: "initial",- ownerDeploymentId: "dpl_inline",- input: { message: "Use the get_weather tool to check the weather in Lisbon." },- serializedContext: buildSerializedContext({- auth: {- attributes: {},- authenticator: "test-idp",- issuer: "test-idp",- principalId: "user-1",- principalType: "user",- },- channelKind: "http",- continuationToken,- }),- },- ]);-+ const run = await startWeatherRun(continuationToken);const stream = captureEvents(run);try {@@ -526,57 +414,37 @@ describe("workflowEntry integration", () => {"initial auth-required event",(event) => event.type === "authorization.required",);- await stream.nextUntil(- "authorization park boundary",- (event) => event.type === "session.waiting",- );-- await waitForParkedTurnStep(run.runId);+ await stream.nextUntil("authorization hold", (event) => event.type === "turn.waiting");- // A cancel with no active turn is consumed by the parked wait- // without producing a parent turn. The callback must still surface- // in the continued wait instead of stalling until unrelated- // session activity re-parks the owner.await waitForHook({ runId: run.runId },{ token: sessionInboxHookToken(continuationToken) },);await resumeHook(sessionInboxHookToken(continuationToken), { kind: "cancel" });- // Let the owner consume the no-op cancel and re-enter the parked- // wait before the callback fires; back-to-back resumes could- // otherwise surface the callback in the first wait iteration and- // mask a wait that ignores callbacks after a consumed cancel.- await new Promise((resolve) => setTimeout(resolve, 250));+ const cancelled = await stream.nextUntil(+ "cancelled held turn",+ (event) => event.type === "session.waiting",+ );+ expect(filterEventsByType(cancelled, "authorization.completed")).toMatchObject([+ { data: { name: "weather", outcome: "declined" } },+ ]);+ expect(filterEventsByType(cancelled, "turn.cancelled")).toHaveLength(1);await resumeHook(sessionInboxHookToken(sessionCommandHookToken(run.runId)), {kind: "authorization-callback",- payloads: [- {- authorizationCallback: {- attemptId: authorizationAttemptId(firstTurn),- callback: {- method: "GET",- params: { code: "oauth-code" },- },- connectionName: "weather",- },- },- ],+ payloads: [weatherCallback(authorizationAttemptId(firstTurn))],});-- const callbackTurn = await stream.nextUntil(- "authorization callback turn",+ await resumeHook(sessionInboxHookToken(continuationToken), {+ auth: userAuth("user-1"),+ kind: "send",+ payload: { message: "follow up after cancel" },+ });+ const followup = await stream.nextUntil(+ "follow-up turn",(event) => event.type === "session.waiting",);- const completed = filterEventsByType(callbackTurn, "authorization.completed");-- expect(completeCalls()).toBe(1);- expect(completed).toHaveLength(1);- expect(completed[0]?.data).toMatchObject({- name: "weather",- outcome: "authorized",- });- expect(filterEventsByType(callbackTurn, "turn.cancelled")).toHaveLength(0);+ expect(filterEventsByType(followup, "authorization.completed")).toHaveLength(0);+ expect(completeCalls()).toBe(0);} finally {stream.dispose();await run.cancel();@@ -697,3 +565,38 @@ function authorizationAttemptId(events: readonly MessageStreamEvent[]): string {}return decodeURIComponent(attemptId);}++function userAuth(principalId: string) {+ return {+ attributes: {},+ authenticator: "test-idp",+ issuer: "test-idp",+ principalId,+ principalType: "user" as const,+ };+}++async function startWeatherRun(continuationToken: string) {+ return await start(workflowEntry, [+ {+ kind: "initial",+ ownerDeploymentId: "dpl_inline",+ input: { message: "Use the get_weather tool to check the weather in Lisbon." },+ serializedContext: buildSerializedContext({+ auth: userAuth("user-1"),+ channelKind: "http",+ continuationToken,+ }),+ },+ ]);+}++function weatherCallback(attemptId: string, code = "oauth-code") {+ return {+ authorizationCallback: {+ attemptId,+ callback: { method: "GET", params: { code } },+ connectionName: "weather",+ },+ };+}diff --git a/packages/eve/src/execution/session/entry.integration.test.ts b/packages/eve/src/execution/session/entry.integration.test.tsindex 5f1f527fa..3b9b2d426 100644--- a/packages/eve/src/execution/session/entry.integration.test.ts+++ b/packages/eve/src/execution/session/entry.integration.test.ts@@ -21,8 +21,11 @@ import { defineHook } from "#public/definitions/hook.js";import { sessions } from "#public/server/index.js";import type { MessageStreamEvent } from "#protocol/message.js";import { isEventId } from "#internal/testing/event-id.js";+import type { Approval } from "#approval/definition.js";import { always } from "#tools/approval/policies.js";import { defineTool } from "#tools/definition.js";+import { ConnectionAuthorizationRequiredError } from "#connections/errors.js";+import { defineInteractiveAuthorization } from "#shared/connection-types.js";import { SessionTitleKey } from "#context/keys.js";import {buildSerializedContext,@@ -668,6 +671,277 @@ describe("workflowEntry integration", () => {});}, 60_000);+ it("holds the turn on an approval and resumes it when a responder the policy allows answers", async () => {+ const executions: string[] = [];+ const approval = { request: always(), response: async () => ({ status: "allowed" as const }) };+ await withHeldApprovalRun(+ {+ agent: { name: "workflow-entry-held-approval" },+ modules: [gatedTool("approve_change", executions, approval)],+ },+ async ({ commandInbox, stream }) => {+ const asked = await withTimeout(stream.nextTurn(), "approval turn");+ const request = filterEventsByType(asked, "input.requested")[0]?.data.requests[0];+ expect(request?.kind).toBe("tool-approval");+ expect(asked.at(-1)).toMatchObject({ data: { on: "input" }, type: "turn.waiting" });+ expect(filterEventsByType(asked, "turn.completed")).toHaveLength(0);++ await resumeHook(commandInbox, {+ auth: BOB,+ kind: "send",+ payload: { inputResponses: [{ optionId: "approve", requestId: request!.requestId }] },+ });+ const answered = await withTimeout(stream.nextTurn(), "approved turn");+ expect(filterEventsByType(answered, "turn.started")).toHaveLength(0);+ expect(filterEventsByType(answered, "approval.settled")).toMatchObject([+ { data: { outcome: "approved", requestId: request!.requestId } },+ ]);+ expect(filterEventsByType(answered, "turn.completed")).toHaveLength(1);+ expect(answered.at(-1)?.type).toBe("session.waiting");+ expect(executions).toEqual(["approve_change"]);+ },+ );+ }, 60_000);++ it("resumes a held approval after the responder signs in for its response policy", async () => {+ const executions: string[] = [];+ await withHeldApprovalRun(+ {+ agent: { name: "workflow-entry-held-policy-sign-in" },+ modules: [gatedTool("approve_change", executions, reviewerSignInApproval())],+ },+ async ({ commandInbox, stream }) => {+ const asked = await withTimeout(stream.nextTurn(), "approval turn");+ const request = filterEventsByType(asked, "input.requested")[0]?.data.requests[0];++ await resumeHook(commandInbox, {+ auth: BOB,+ kind: "send",+ payload: { inputResponses: [{ optionId: "approve", requestId: request!.requestId }] },+ });+ const signIn = await withTimeout(stream.nextTurn(), "responder sign-in");+ const required = filterEventsByType(signIn, "authorization.required")[0];+ expect(required?.data.candidateId).toBeDefined();+ expect(signIn.at(-1)).toMatchObject({ data: { on: "input" }, type: "turn.waiting" });++ await resumeHook(commandInbox, reviewerCallback(required!));+ const approved = await withTimeout(stream.nextTurn(), "approved turn");+ expect(filterEventsByType(approved, "authorization.completed")).toMatchObject([+ { data: { outcome: "authorized" } },+ ]);+ expect(filterEventsByType(approved, "approval.settled")).toMatchObject([+ { data: { outcome: "approved", requestId: request!.requestId } },+ ]);+ expect(executions).toEqual(["approve_change"]);+ },+ );+ }, 60_000);++ it("withdraws the responder's sign-in when the requester steers past its approval", async () => {+ const executions: string[] = [];+ await withHeldApprovalRun(+ {+ agent: { name: "workflow-entry-steer-responder-sign-in" },+ modules: [gatedTool("approve_change", executions, reviewerSignInApproval())],+ },+ async ({ commandInbox, stream }) => {+ const asked = await withTimeout(stream.nextTurn(), "approval turn");+ const request = filterEventsByType(asked, "input.requested")[0]?.data.requests[0];+ await resumeHook(commandInbox, {+ auth: BOB,+ kind: "send",+ payload: { inputResponses: [{ optionId: "approve", requestId: request!.requestId }] },+ });+ const signIn = await withTimeout(stream.nextTurn(), "responder sign-in");+ const required = filterEventsByType(signIn, "authorization.required")[0]!;++ // The requester moves on before the reviewer finishes signing in.+ await resumeHook(commandInbox, {+ kind: "send",+ payload: { message: "Never mind, just say hello." },+ });+ const steered = await withTimeout(stream.nextTurn(), "steered turn");+ expect(filterEventsByType(steered, "authorization.completed")).toMatchObject([+ { data: { candidateId: required.data.candidateId, outcome: "declined" } },+ ]);+ expect(filterEventsByType(steered, "input.resolved")).toMatchObject([+ { data: { resolutions: [{ outcome: "ignored", requestId: request!.requestId }] } },+ ]);+ expect(steered.at(-1)?.type).toBe("session.waiting");++ // The reviewer's late sign-in settles nothing and runs nothing.+ await resumeHook(commandInbox, reviewerCallback(required));+ await resumeHook(commandInbox, { kind: "send", payload: { message: "Thanks." } });+ const next = await withTimeout(stream.nextTurn(), "next turn");+ expect(filterEventsByType(next, "authorization.completed")).toHaveLength(0);+ expect(filterEventsByType(next, "approval.settled")).toHaveLength(0);+ expect(filterEventsByType(next, "turn.started")).toHaveLength(1);+ expect(executions).toEqual([]);+ },+ );+ }, 60_000);++ it("re-holds a steered turn on a new approval and resets cleanly after it completes", async () => {+ const executions: string[] = [];+ await withHeldApprovalRun(+ {+ agent: { name: "workflow-entry-steer-rehold" },+ message: "Use the change_a tool exactly once.",+ modules: [gatedTool("change_a", executions), gatedTool("change_b", executions)],+ },+ async ({ commandInbox, run, stream }) => {+ const heldA = await withTimeout(stream.nextTurn(), "change_a approval");+ expect(heldA.at(-1)?.type).toBe("turn.waiting");++ await resumeHook(commandInbox, {+ kind: "send",+ payload: { message: "Use the change_b tool exactly once." },+ });+ const heldB = await withTimeout(stream.nextTurn(), "change_b approval");+ const requestB = filterEventsByType(heldB, "input.requested")[0]?.data.requests[0];+ expect(requestB?.action.toolName).toBe("change_b");+ expect(heldB.at(-1)?.type).toBe("turn.waiting");++ await resumeHook(commandInbox, {+ kind: "send",+ payload: { inputResponses: [{ optionId: "approve", requestId: requestB!.requestId }] },+ });+ const answered = await withTimeout(stream.nextTurn(), "change_b approved");+ expect(answered.at(-1)?.type).toBe("session.waiting");+ expect(executions).toEqual(["change_b"]);++ await resetAndRelease(run);+ return "released";+ },+ );+ }, 60_000);++ it("lets the turn's own person steer a held approval even with a queue turn policy", async () => {+ const executions: string[] = [];+ await withHeldApprovalRun(+ {+ agent: { name: "workflow-entry-steer-held-auth" },+ auth: ALICE,+ modules: [gatedTool("approve_change", executions)],+ },+ async ({ sessionInbox, stream }) => {+ const held = await withTimeout(stream.nextTurn(), "approval hold");+ const request = filterEventsByType(held, "input.requested")[0]?.data.requests[0];+ expect(held.at(-1)?.type).toBe("turn.waiting");++ await resumeHook(sessionInbox, {+ auth: ALICE,+ kind: "send",+ payload: { message: "Never mind, just say hello." },+ // Clients such as the TUI and eval sessions queue by default. Queued+ // behind a turn that waits on Alice, it would never run.+ turnPolicy: "queue",+ });+ const steered = await withTimeout(stream.nextTurn(), "steered turn");+ expect(filterEventsByType(steered, "input.resolved")).toMatchObject([+ { data: { resolutions: [{ outcome: "ignored", requestId: request!.requestId }] } },+ ]);+ expect(filterEventsByType(steered, "turn.started")).toHaveLength(0);+ expect(steered.at(-1)?.type).toBe("session.waiting");+ expect(executions).toEqual([]);+ },+ );+ }, 60_000);++ it("runs a steering message after the budget prompt that followed its cancelled approval", async () => {+ await withHeldApprovalRun(+ {+ agent: {+ limits: { maxInputTokensPerSession: 1 },+ name: "workflow-entry-steer-held-limit",+ },+ auth: ALICE,+ modules: [gatedTool("approve_change", [])],+ },+ async ({ sessionInbox, stream }) => {+ const held = await withTimeout(stream.nextTurn(), "approval hold");+ expect(held.at(-1)?.type).toBe("turn.waiting");++ await resumeHook(sessionInbox, {+ auth: ALICE,+ kind: "send",+ payload: { message: "Never mind, just say hello." },+ turnPolicy: "queue",+ });+ const limited = await withTimeout(stream.nextTurn(), "budget prompt");+ const budget = filterEventsByType(limited, "input.requested")+ .flatMap((event) => event.data.requests)+ .find((request) => request.kind === "session-limit");+ expect(budget).toBeDefined();++ await resumeHook(sessionInbox, {+ auth: ALICE,+ kind: "send",+ payload: { inputResponses: [{ optionId: "continue", requestId: budget!.requestId }] },+ turnPolicy: "queue",+ });+ const resumed = await withTimeout(stream.nextTurn(), "granted turn");+ // The message joined the step that cancelled the approval; the grant+ // resumes with one model call that answers it.+ expect(filterEventsByType(limited, "message.received").map((e) => e.data.message)).toEqual([+ "Never mind, just say hello.",+ ]);+ expect(filterEventsByType(resumed, "message.received")).toHaveLength(0);+ expect(filterEventsByType(resumed, "step.started")).toHaveLength(1);+ expect(filterEventsByType(resumed, "message.completed")).toHaveLength(1);+ },+ );+ }, 60_000);++ it("releases a session reset while its turn is held on an approval", async () => {+ await withHeldApprovalRun(+ {+ agent: { name: "workflow-entry-reset-held" },+ modules: [gatedTool("approve_change", [])],+ },+ async ({ run, stream }) => {+ const held = await withTimeout(stream.nextTurn(), "approval hold");+ expect(held.at(-1)?.type).toBe("turn.waiting");++ await resetAndRelease(run);+ return "released";+ },+ );+ }, 60_000);++ it("withdraws a held tool approval when its turn is cancelled", async () => {+ const executions: string[] = [];+ await withHeldApprovalRun(+ {+ agent: { name: "workflow-entry-cancel-held-approval" },+ modules: [gatedTool("approve_change", executions)],+ },+ async ({ commandInbox, sessionInbox, stream }) => {+ const asked = await withTimeout(stream.nextTurn(), "approval turn");+ const request = filterEventsByType(asked, "input.requested")[0]?.data.requests[0];+ expect(asked.at(-1)?.type).toBe("turn.waiting");++ await resumeHook(sessionInbox, { kind: "cancel" });+ const cancelled = await withTimeout(stream.nextTurn(), "cancelled turn");+ expect(filterEventsByType(cancelled, "input.resolved")).toMatchObject([+ { data: { resolutions: [{ outcome: "cancelled", requestId: request!.requestId }] } },+ ]);+ expect(filterEventsByType(cancelled, "turn.cancelled")).toHaveLength(1);++ // A late answer approves nothing: the call can only run after a new approval.+ await resumeHook(commandInbox, {+ kind: "send",+ payload: { inputResponses: [{ optionId: "approve", requestId: request!.requestId }] },+ });+ const late = await withTimeout(stream.nextTurn(), "late answer turn");+ expect(filterEventsByType(late, "session.failed")).toHaveLength(0);+ const reasked = filterEventsByType(late, "input.requested")[0]?.data.requests[0];+ expect(reasked?.requestId).not.toBe(request!.requestId);+ expect(executions).toEqual([]);+ },+ );+ }, 60_000);+it("exits a competing continuation owner before its first turn", async () => {const runtime = await createTestRuntime({ agent: { name: "workflow-entry-hook-owner" } });const continuationToken = "http:workflow-entry-hook-owner";@@ -987,3 +1261,157 @@ function hasSubagentResult(value: unknown, callId: string): boolean {result.callId === callId,);}++const ALICE = testUser("alice");+const BOB = testUser("bob");++function testUser(principalId: string) {+ return {+ attributes: {},+ authenticator: "test",+ issuer: "test",+ principalId,+ principalType: "user" as const,+ };+}++/** A tool that records each run in `executions` once its approval allows it. */+function gatedTool(name: string, executions: string[], approval: Approval = always()) {+ return {+ loadNamespace: async () => ({+ default: defineTool({+ approval,+ description: `Apply ${name} after it is approved.`,+ execute: () => {+ executions.push(name);+ return { applied: name };+ },+ inputSchema: {},+ }),+ }),+ logicalPath: `tools/${name}.ts`,+ };+}++/** An approval whose responder must sign in to a reviewer service before it counts. */+function reviewerSignInApproval(): Approval {+ const tokens = new Map<string, string>();+ const reviewerOAuth = defineInteractiveAuthorization<{ principalId: string }>({+ displayName: "Reviewer OAuth",+ async getToken({ principal }) {+ const token = principal.type === "user" ? tokens.get(principal.id) : undefined;+ if (token === undefined) throw new ConnectionAuthorizationRequiredError("reviewer-oauth");+ return { providerSubject: principal.type === "user" ? principal.id : "", token };+ },+ async startAuthorization({ callbackUrl, principal }) {+ const url = new URL(callbackUrl);+ url.searchParams.set("code", "reviewer-code");+ return {+ challenge: { url: url.href },+ resume: { principalId: principal.type === "user" ? principal.id : "" },+ };+ },+ async completeAuthorization({ resume }) {+ tokens.set(resume!.principalId, "reviewer-token");+ return { providerSubject: resume!.principalId, token: "reviewer-token" };+ },+ });+ return {+ request: always(),+ async response({ auth, response }) {+ const credential = await auth.getToken(reviewerOAuth, { authKey: "reviewer-oauth" });+ return credential.providerSubject === response.principal.principalId+ ? { status: "allowed" }+ : { status: "rejected", reason: "Reviewer identity mismatch." };+ },+ };+}++function reviewerCallback(required: {+ readonly data: { readonly attemptId?: string; readonly name: string };+}) {+ return {+ kind: "authorization-callback" as const,+ payloads: [+ {+ authorizationCallback: {+ attemptId: required.data.attemptId,+ callback: { method: "GET" as const, params: { code: "reviewer-code" } },+ connectionName: required.data.name,+ },+ },+ ],+ };+}++interface HeldApprovalRun {+ readonly commandInbox: string;+ readonly run: Awaited<ReturnType<typeof start>>;+ readonly sessionInbox: string;+ readonly stream: ReturnType<typeof captureTurnEvents>;+}++/**+ * Starts an HTTP session on `message` and hands `body` its stream and inboxes.+ * The run is cancelled afterwards unless `body` released it.+ */+async function withHeldApprovalRun(+ options: {+ readonly agent: NonNullable<Parameters<typeof createTestRuntime>[0]>["agent"] & {+ readonly name: string;+ };+ readonly auth?: ReturnType<typeof testUser>;+ readonly message?: string;+ readonly modules: NonNullable<Parameters<typeof createTestRuntime>[0]>["modules"];+ },+ body: (held: HeldApprovalRun) => Promise<"released" | void>,+): Promise<void> {+ const runtime = await createTestRuntime({ agent: options.agent, modules: options.modules });+ const continuationToken = `http:${options.agent.name}`;+ const context: Parameters<typeof buildSerializedContext>[0] = {+ channelKind: "http",+ continuationToken,+ };+ if (options.auth !== undefined) context.auth = options.auth;+ await runtime.run(async () => {+ const run = await start(workflowEntry, [+ {+ kind: "initial",+ ownerDeploymentId: "dpl_inline",+ input: { message: options.message ?? "Use the approve_change tool exactly once." },+ serializedContext: {+ ...buildSerializedContext(context),+ "eve.capabilities": { requestInput: true },+ },+ },+ ]);+ const stream = captureTurnEvents(run);+ let released = false;+ try {+ released =+ (await body({+ commandInbox: sessionInboxHookToken(sessionCommandHookToken(run.runId)),+ run,+ sessionInbox: sessionInboxHookToken(continuationToken),+ stream,+ })) === "released";+ } finally {+ stream.dispose();+ if (!released) await run.cancel();+ }+ });+}++async function resetAndRelease(run: HeldApprovalRun["run"]): Promise<void> {+ const workflowRuntime = createWorkflowRuntime({+ compiledArtifactsSource: createBundledRuntimeCompiledArtifactsSource(),+ });+ await withTimeout(+ workflowRuntime.dispatchSession({+ command: { kind: "reset", reason: "Test cleanup" },+ sessionId: run.runId,+ }),+ "reset",+ );+ await withTimeout(run.returnValue, "session release");+}diff --git a/packages/eve/src/execution/session/input-queue.test.ts b/packages/eve/src/execution/session/input-queue.test.tsnew file mode 100644index 000000000..b6e259e67--- /dev/null+++ b/packages/eve/src/execution/session/input-queue.test.ts@@ -0,0 +1,30 @@+import { describe, expect, it } from "vitest";++import { SessionInputQueue } from "#execution/session/input-queue.js";++function signInCallback(attemptId: string) {+ return {+ authorizationCallback: {+ attemptId,+ callback: { method: "GET" as const, params: { code: "oauth-code" } },+ connectionName: attemptId.split("-")[0]!,+ },+ };+}++describe("SessionInputQueue", () => {+ it("resumes a turn holding several sign-ins only once every one has called back", () => {+ const queue = new SessionInputQueue();+ const held = new Set(["linear-1", "notion-1"]);++ queue.enqueueAuthorization([signInCallback("linear-1")]);+ expect(queue.takeAuthorizations(held)).toBeUndefined();++ queue.enqueueAuthorization([signInCallback("notion-1")]);+ expect(queue.takeAuthorizations(held)).toEqual([+ signInCallback("linear-1"),+ signInCallback("notion-1"),+ ]);+ expect(queue.takeAuthorizations(held)).toBeUndefined();+ });+});diff --git a/packages/eve/src/execution/session/input-queue.ts b/packages/eve/src/execution/session/input-queue.tsindex b6dbf4679..17440ae6a 100644--- a/packages/eve/src/execution/session/input-queue.ts+++ b/packages/eve/src/execution/session/input-queue.ts@@ -43,8 +43,7 @@ export interface TurnSelection {export type SessionInputSelection =| TurnSelection- | { readonly control: SessionControl; readonly kind: "control" }- | { readonly kind: "authorization-resume"; readonly payloads: readonly DeliverPayload[] };+ | { readonly control: SessionControl; readonly kind: "control" };/*** Ordered, admitted session input. Entries are private; callers receive typed@@ -89,6 +88,25 @@ export class SessionInputQueue {}}+ /**+ * Removes and returns the queued callbacks for `attemptIds` once every one+ * has arrived, so a turn holding several sign-ins resumes when all are done.+ */+ takeAuthorizations(attemptIds: ReadonlySet<string>): DeliverPayload[] | undefined {+ const taken = this.entries.filter(+ (entry): entry is QueuedAuthorization =>+ entry.kind === "authorization" && attemptIds.has(entry.attemptId),+ );+ if (+ taken.length === 0 ||+ new Set(taken.map((entry) => entry.attemptId)).size < attemptIds.size+ ) {+ return undefined;+ }+ this.retain((entry) => !taken.includes(entry as QueuedAuthorization));+ return taken.map((entry) => entry.payload);+ }+delivery(sequence: number): DeliverHookPayload | undefined {const entry = this.entries.find((candidate): candidate is QueuedDelivery =>@@ -109,12 +127,16 @@ export class SessionInputQueue {this.entries[index] = { delivery, kind: "delivery", sequence };}- takeSteering(admitted: ReadonlySet<number>, turn: SteeringTurn): TurnSelection | undefined {+ takeSteering(+ admitted: ReadonlySet<number>,+ turn: SteeringTurn,+ options?: SteeringOptions,+ ): TurnSelection | undefined {const steering = this.entries.filter((entry): entry is QueuedDelivery =>entry.kind === "delivery" &&admitted.has(entry.sequence) &&- isSteeringDelivery(entry.delivery, turn),+ isSteeringDelivery(entry.delivery, turn, options),);if (steering.length === 0) return undefined;this.retain((entry) => entry.kind !== "delivery" || !steering.includes(entry));@@ -126,53 +148,27 @@ export class SessionInputQueue {};}+ /**+ * Takes the next turn or control. Sign-ins end with the turn that raised+ * them, so a callback still queued between turns is stale and dropped.+ */takeNext(options?: {- /**- * Attempt ids of the open authorization challenge. Callbacks for other- * attempts are stale and dropped; once every expected attempt has- * reported, the collected payloads resume the challenge ahead of- * ordinary input.- */- readonly expectedAttemptIds?: ReadonlySet<string>;/** Sequence of a delivery admitted while nothing else was pending. */readonly freshSequence?: number;}): SessionInputSelection | undefined {- const expected = options?.expectedAttemptIds ?? new Set<string>();- this.retain((entry) => entry.kind !== "authorization" || expected.has(entry.attemptId));- if (expected.size > 0) {- const collected = new Map(- this.entries.flatMap((entry) =>- entry.kind === "authorization" ? [[entry.attemptId, entry.payload] as const] : [],- ),- );- if ([...expected].every((attemptId) => collected.has(attemptId))) {- this.retain((entry) => entry.kind !== "authorization");- return {- kind: "authorization-resume",- payloads: [...expected].map((attemptId) => collected.get(attemptId)!),- };- }- }- const index = this.entries.findIndex((entry) => entry.kind !== "authorization");- if (index < 0) return undefined;- return this.takeSelectionAt(index, options?.freshSequence);- }-- private takeSelectionAt(index: number, freshSequence: number | undefined): SessionInputSelection {- const first = this.entries.splice(index, 1)[0]!;+ this.retain((entry) => entry.kind !== "authorization");+ const first = this.entries.shift() as QueuedDelivery | QueuedControl | undefined;+ if (first === undefined) return undefined;if (first.kind === "control") return { control: first.control, kind: "control" };- if (first.kind === "authorization") {- return { kind: "authorization-resume", payloads: [first.payload] };- }- const turnEntries = [first, ...this.takeFollowingDeliveriesFrom(first, index)];+ const turnEntries = [first, ...this.takeFollowingDeliveriesFrom(first)];const sequences = turnEntries.map(({ sequence }) => sequence);const combined = combine(turnEntries);return {delivery: combined,handoffEligible:sequences.length === 1 &&- sequences[0] === freshSequence &&+ sequences[0] === options?.freshSequence &&combined.caller === undefined &&this.entries.length === 0,kind: "turn",@@ -185,12 +181,12 @@ export class SessionInputQueue {* call share a turn, as they would steer it. Claims may change between* them; the turn runs with the latest.*/- private takeFollowingDeliveriesFrom(first: QueuedDelivery, index: number): QueuedDelivery[] {+ private takeFollowingDeliveriesFrom(first: QueuedDelivery): QueuedDelivery[] {const principal = principalOf(first.delivery.auth);const following: QueuedDelivery[] = [];let callId = first.delivery.caller?.callId;- while (this.entries.length > index) {- const next = this.entries[index];+ while (true) {+ const next = this.entries[0];if (next?.kind !== "delivery" ||principal === ANONYMOUS_PRINCIPAL ||@@ -201,7 +197,8 @@ export class SessionInputQueue {) {break;}- following.push(this.entries.splice(index, 1)[0] as QueuedDelivery);+ following.push(next);+ this.entries.shift();callId ??= next.delivery.caller?.callId;}return following;@@ -220,14 +217,27 @@ export interface SteeringTurn {readonly principal: string;}+export interface SteeringOptions {+ /**+ * The turn waits on its own person for a sign-in or approval. Their message+ * steers even with `turnPolicy: "queue"`: queued, it would wait for a turn+ * that cannot end until they act.+ */+ readonly heldOnPerson?: boolean;+}+/*** Only the turn's own principal steers it; another principal's message waits* for the turn to end. A delivery without auth acts as the session's current* identity, which is the turn's.*/-export function isSteeringDelivery(delivery: DeliverHookPayload, turn: SteeringTurn): boolean {+export function isSteeringDelivery(+ delivery: DeliverHookPayload,+ turn: SteeringTurn,+ options?: SteeringOptions,+): boolean {return (- (delivery.turnPolicy ?? "steer") === "steer" &&+ (options?.heldOnPerson === true || (delivery.turnPolicy ?? "steer") === "steer") &&(delivery.caller === undefined || delivery.caller.callId === turn.callerCallId) &&(delivery.auth === undefined || principalOf(delivery.auth) === turn.principal));diff --git a/packages/eve/src/execution/session/next-input.test.ts b/packages/eve/src/execution/session/next-input.test.tsindex 401c86a57..dafaccbbc 100644--- a/packages/eve/src/execution/session/next-input.test.ts+++ b/packages/eve/src/execution/session/next-input.test.ts@@ -71,12 +71,6 @@ function authorizationRead(): ScriptedRead {return { result: { done: false, value: authorizationCallbackPayload } };}-function cancelRead(): ScriptedRead {- return {- result: { done: false, value: { kind: "cancel" } },- };-}-function messageRead(message: string): ScriptedRead {return {result: { done: false, value: { kind: "send", payload: { message } } },@@ -94,7 +88,6 @@ type WaitInput = {function waitInput(inbox: SessionInbox): WaitInput {const cursor = createCursor(inbox);return {- expectedAttemptIds: new Set(["attempt-1"]),hasWorkingTasks: () => false,inbox: inbox,cursor,@@ -269,38 +262,18 @@ describe("nextTurnDelivery", () => {expect(input.queue.pendingCount).toBe(1);});- it("surfaces an authorization callback as its own instruction", async () => {- const inbox = createMockInbox([authorizationRead()]);-- const next = await nextTurnDelivery(waitInput(inbox));-- expect(next.kind).toBe("authorization-resume");- if (next.kind !== "authorization-resume") throw new Error("unreachable");- expect(next.payloads).toEqual(authorizationCallbackPayload.payloads);- });-- it("resumes authorization after a consumed no-op cancel", async () => {- // A cancel with no active turn is consumed without producing a parent- // turn; the wait continues and the callback must still resume the challenge.- const inbox = createMockInbox([cancelRead(), authorizationRead()]);+ it("drops a sign-in callback that arrives between turns and waits for the next message", async () => {+ const inbox = createMockInbox([authorizationRead(), messageRead("next question")]);- const next = await nextTurnDelivery(waitInput(inbox));+ const next = await nextTurnDelivery({ ...batchingInputFor([]), inbox });- expect(next.kind).toBe("authorization-resume");- });-- it("does not let buffered deliveries bypass a ready authorization callback", async () => {- const inbox = createMockInbox([]);- const queue = queueOf({ kind: "deliver", payloads: [{ message: "later" }] });- queue.enqueueAuthorization(authorizationCallbackPayload.payloads);-- const next = await nextTurnDelivery({ ...waitInput(inbox), queue });-- expect(next.kind).toBe("authorization-resume");- expect(queue.pendingCount).toBe(1);+ expect(next).toMatchObject({+ delivery: { payloads: [{ message: "next question" }] },+ kind: "turn",+ });});- it("reports session closure while waiting for authorization", async () => {+ it("reports session closure while parked", async () => {const inbox = createMockInbox([{ result: { done: true, value: undefined } }]);const next = await nextTurnDelivery(waitInput(inbox));@@ -323,10 +296,7 @@ describe("nextTurnDelivery", () => {stateDelta: {},});- const next = await nextTurnDelivery({- ...waitInput(inbox),- expectedAttemptIds: undefined,- });+ const next = await nextTurnDelivery(waitInput(inbox));expect(vi.mocked(routeDeliverToChildren).mock.calls[1]?.[0].sessionState).toBe(retiredState);expect(next).toMatchObject({diff --git a/packages/eve/src/execution/session/next-input.ts b/packages/eve/src/execution/session/next-input.tsindex bb36a210a..d82faf6ca 100644--- a/packages/eve/src/execution/session/next-input.ts+++ b/packages/eve/src/execution/session/next-input.ts@@ -1,4 +1,3 @@-import type { DeliverPayload } from "#channel/types.js";import { routeSelectedDelivery } from "#execution/session/route-selected-delivery.js";import type {SessionControl,@@ -12,7 +11,6 @@ import type { WorkflowToolRunMessage } from "#execution/tools/workflow/messages.export type NextTurnInstruction =| { readonly kind: "workflow"; readonly message: WorkflowToolRunMessage }- | { readonly kind: "authorization-resume"; readonly payloads: readonly DeliverPayload[] }| { readonly kind: SessionControl }| { readonly kind: "closed" }| { readonly kind: "cancel-turn" }@@ -21,16 +19,12 @@ export type NextTurnInstruction =| TurnSelection;/**- * Waits for the next input the parked owner must act on. While an- * authorization challenge is open, its callbacks collect in the queue and- * resume the challenge once every expected attempt has reported; ordinary- * deliveries keep starting turns in the meantime. Fully routed descendant- * deliveries leave nothing for the parent, so the wait continues.+ * Waits for the next input the parked owner must act on. Fully routed+ * descendant deliveries leave nothing for the parent, so the wait continues.*/export async function nextTurnDelivery(input: {readonly inbox: SessionInboxReader;readonly cursor: SessionStateCursor;- readonly expectedAttemptIds?: ReadonlySet<string>;readonly hasWorkingTasks: () => boolean;readonly queue: SessionInputQueue;}): Promise<NextTurnInstruction> {@@ -40,11 +34,9 @@ export async function nextTurnDelivery(input: {let freshSequence: number | undefined;while (true) {const selected = queue.takeNext({- expectedAttemptIds: input.expectedAttemptIds,freshSequence: inbox.hasPending() ? undefined : freshSequence,});if (selected?.kind === "control") return { kind: selected.control };- if (selected?.kind === "authorization-resume") return selected;if (selected?.kind === "turn") {const routed = await routeSelectedDelivery(selected, cursor);if (routed.kind === "cancel-turn") return routed;diff --git a/packages/eve/src/execution/session/program.ts b/packages/eve/src/execution/session/program.tsindex aeaaab3f5..69e60620e 100644--- a/packages/eve/src/execution/session/program.ts+++ b/packages/eve/src/execution/session/program.ts@@ -211,13 +211,12 @@ async function runSessionLoop(sessionId: boot.sessionId,});- const nextParkedActivity = async (- expectedAttemptIds: ReadonlySet<string>,- ): Promise<Exclude<NextTurnInstruction, { kind: "workflow" | "cancel-working-tasks" }>> => {+ const nextParkedActivity = async (): Promise<+ Exclude<NextTurnInstruction, { kind: "workflow" | "cancel-working-tasks" }>+ > => {while (true) {const next = await nextTurnDelivery({cursor,- expectedAttemptIds,hasWorkingTasks: () => workingTasks(sessionTaskTable(cursor)).length > 0,inbox,queue,@@ -268,7 +267,7 @@ async function runSessionLoop(};const awaitPrewarmedAction = async (): Promise<SessionActionResult> => {while (true) {- const next = await nextParkedActivity(new Set());+ const next = await nextParkedActivity();switch (next.kind) {case "expired":case "reset":@@ -280,7 +279,6 @@ async function runSessionLoop(case "turn":return await runDeliveredTurn(next);case "cancel-turn":- case "authorization-resume":continue;}}@@ -329,17 +327,9 @@ async function runSessionLoop(progress.caller = undefined;}- // An open authorization challenge must not wedge the session:- // ordinary deliveries keep starting normal turns while the challenge- // waits for its callback. The pending challenge survives intervening- // turns because every park re-derives `authorizationAttemptIds` from- // durable session state.- const next = await nextParkedActivity(new Set(action.authorizationAttemptIds ?? []));+ const next = await nextParkedActivity();switch (next.kind) {- case "authorization-resume":- action = await runTurn({ delivery: { kind: "deliver", payloads: next.payloads } });- continue;case "expired":case "reset":case "closed":diff --git a/packages/eve/src/execution/session/turn-connection-approval.integration.test.ts b/packages/eve/src/execution/session/turn-connection-approval.integration.test.tsindex 1c791acef..0d1a5bec4 100644--- a/packages/eve/src/execution/session/turn-connection-approval.integration.test.ts+++ b/packages/eve/src/execution/session/turn-connection-approval.integration.test.ts@@ -367,85 +367,6 @@ describe("turn connection approval restoration", () => {expect(last).toContain("- second-notes: Save notes");});- it("authorizes requests from two originating turns independently in one cold delivery", async () => {- const fixture = setup();- await fixture.step({- delivery: { kind: "deliver", payloads: [{ message: "Prepare Alice's first note." }] },- });- const first = await fixture.step();- const firstBatch = getPendingInputBatches(readDurableSession(first.sessionState).state)[0]!;- fixture.doStream.mockImplementationOnce(() => modelResponse("save-2"));- await fixture.step({- delivery: {- kind: "deliver",- payloads: [{ message: "Prepare Alice's second independent note." }],- },- });- const second = await fixture.step();- const batches = getPendingInputBatches(readDurableSession(second.sessionState).state);- expect(batches).toHaveLength(2);- expect(batches[1]!.event!.turnId).not.toBe(firstBatch.event!.turnId);- clearDurableDynamicCallbacks(sessionId);- await fixture.step({- delivery: {- kind: "deliver",- auth: bob,- payloads: [- {- inputResponses: batches.flatMap((batch) =>- batch.requests.map((request) => ({- requestId: request.requestId,- optionId: "approve",- })),- ),- },- ],- },- });- clearDurableDynamicCallbacks(sessionId);- const resumed = await fixture.step();- expect(fixture.policyTurns).toEqual(batches.map((batch) => batch.event!.turnId));- expect(fixture.response).toHaveBeenCalledTimes(2);- expect(- getApprovalAuditState(readDurableSession(resumed.sessionState).state).settlements,- ).toHaveLength(2);- expect(fixture.fetch).toHaveBeenCalled();- expect(fixture.events.filter((event) => event.type === "session.failed")).toEqual([]);- });- it.each([false, true])(- "fails an approved call whose connection changed before it ran (cold: %s)",- async (cold) => {- const fixture = setup("turn.started", false, "destination");- await fixture.step({- delivery: { kind: "deliver", payloads: [{ message: "Prepare Alice's note." }] },- });- const parked = await fixture.step();- const request = getPendingInputBatches(readDurableSession(parked.sessionState).state)[0]!- .requests[0]!;- if (cold) clearDurableDynamicCallbacks(sessionId);- await fixture.step({- delivery: {- kind: "deliver",- auth: bob,- payloads: [{ inputResponses: [{ requestId: request.requestId, optionId: "approve" }] }],- },- });- if (cold) clearDurableDynamicCallbacks(sessionId);- await fixture.step();- expect(fixture.response).toHaveBeenCalledOnce();- expect(fixture.fetch).not.toHaveBeenCalled();- expect(- JSON.stringify(- fixture.events.filter(- (event) =>- event.type === "action.result" &&- JSON.stringify(event.data).includes(request.action.callId),- ),- ),- ).toContain("connection for this tool call changed or is unavailable");- },- );-it("rejects only the approved call whose instance pin was evicted", async () => {const fixture = setup("turn.started", false, "request-only");// One more parked call than the pin map holds evicts the first call's pin.@@ -490,64 +411,6 @@ describe("turn connection approval restoration", () => {expect(fixture.fetch).toHaveBeenCalledTimes(50);});- it.each([false, true])(- "fails an approved call whose connection is gone while running an available one (cold: %s)",- async (cold) => {- const fixture = setup("turn.started", false, "name");- await fixture.step({- delivery: { kind: "deliver", payloads: [{ message: "Prepare Alice's first note." }] },- });- await fixture.step();- fixture.doStream.mockImplementationOnce(() => modelResponse("save-2", "second-notes"));- await fixture.step({- delivery: { kind: "deliver", payloads: [{ message: "Prepare Alice's second note." }] },- });- const second = await fixture.step();- const batches = getPendingInputBatches(readDurableSession(second.sessionState).state);- expect(batches).toHaveLength(2);- expect(- batches.map(- (batch) => (batch.requests[0]!.action.input as { connection: string }).connection,- ),- ).toEqual(["notes", "second-notes"]);- if (cold) clearDurableDynamicCallbacks(sessionId);- await fixture.step({- delivery: {- kind: "deliver",- auth: bob,- payloads: [- {- inputResponses: batches.flatMap((batch) =>- batch.requests.map((request) => ({- requestId: request.requestId,- optionId: "approve",- })),- ),- },- ],- },- });- if (cold) clearDurableDynamicCallbacks(sessionId);- await fixture.step();- const older = batches[0]!.requests[0]!;- // Each approval is authorized against its originating turn's connections;- // the older call then fails because "notes" is gone when it runs.- expect(fixture.policyTurns).toEqual(batches.map((batch) => batch.event!.turnId));- expect(fixture.events).toContainEqual(- expect.objectContaining({- type: "action.result",- data: expect.objectContaining({- status: "failed",- result: expect.objectContaining({- callId: older.action.callId,- output: expect.stringContaining('Connection "notes" is not available'),- }),- }),- }),- );- },- );-it("fails an approved call whose connection is gone without a response policy", async () => {const fixture = setup("turn.started", false, "request-only");await fixture.step({@@ -741,11 +604,11 @@ describe("turn connection approval restoration", () => {expect(fixture.policyTurns).toEqual([batch.event!.turnId]);expect(fixture.response).toHaveBeenCalledOnce();expect(fixture.fetch).toHaveBeenCalledOnce();+ // The approval held its turn, so the responder's sign-in resumes it+ // without starting another.const events = fixture.events.slice(start);- expect(events.findIndex((event) => event.type === "authorization.completed")).toBeLessThan(- events.findIndex((event) => event.type === "turn.started"),- );- expect(events.filter((event) => event.type === "turn.started")).toHaveLength(1);+ expect(events.some((event) => event.type === "authorization.completed")).toBe(true);+ expect(events.filter((event) => event.type === "turn.started")).toHaveLength(0);});it.each([false, true])(@@ -788,7 +651,8 @@ describe("turn connection approval restoration", () => {expect(fixture.fetch).toHaveBeenCalledOnce();expect(getPendingInputBatches(readDurableSession(resumed.sessionState).state)).toEqual([]);expect(resumed.serializedContext).not.toHaveProperty("eve.pendingConnectionCalls");- expect(fixture.events.filter((event) => event.type === "turn.started")).toHaveLength(2);+ // The approval held its turn, so approving it resumes the same turn.+ expect(fixture.events.filter((event) => event.type === "turn.started")).toHaveLength(1);// The connection call is reported as a nested action of the approved call.const nestedCallId = `${request.action.callId}:1`;expect(fixture.events).toContainEqual(diff --git a/packages/eve/src/execution/session/turn-step-result.ts b/packages/eve/src/execution/session/turn-step-result.tsindex 3c25f0130..0d4fab4bb 100644--- a/packages/eve/src/execution/session/turn-step-result.ts+++ b/packages/eve/src/execution/session/turn-step-result.ts@@ -1,6 +1,7 @@import { createDurableSessionValues } from "#execution/durable-session-store.js";import { derivePendingState } from "#execution/session/pending-turn-state.js";import type { DurableStepResult } from "#execution/session/turn-step-types.js";+import { getPendingInputBatches } from "#harness/pending-input-batches.js";import { getTurnUsageState, takeSessionUsageDelta, toUsage } from "#harness/turn-tag-state.js";import type { StepResult } from "#harness/types.js";@@ -30,12 +31,28 @@ export function resolveSessionStepResult(};}- if (stepResult.held !== undefined) {- return { action: "held", ...values, taskIds: stepResult.held.taskIds };+ if (stepResult.held?.kind === "tasks") {+ return { action: "held", hold: "tasks", ...values, taskIds: stepResult.held.taskIds };+ }+ if (stepResult.held?.kind === "request") {+ const pending = derivePendingState(stepResult.session);+ return {+ action: "held",+ authorizationAttemptIds: pending.authorizationAttemptIds ?? [],+ hasPendingInputBatch: pending.hasPendingInputBatch,+ hold: "request",+ inputRequestIds: getPendingInputBatches(stepResult.session.state).flatMap((batch) =>+ batch.requests.map((request) => request.requestId),+ ),+ ...values,+ };}if (stepResult.next === null) {- const pending = derivePendingState(stepResult.session);+ const { pendingCoordinationCallIds, pendingTaskToolCalls } = derivePendingState(+ stepResult.session,+ );+ const pending = { pendingCoordinationCallIds, pendingTaskToolCalls };// Usage stays unreported until the turn settles, so the caller's result includes all of it.if (stepResult.settledTurn !== undefined) {diff --git a/packages/eve/src/execution/session/turn-step-types.ts b/packages/eve/src/execution/session/turn-step-types.tsindex 1704caf49..9b92b080d 100644--- a/packages/eve/src/execution/session/turn-step-types.ts+++ b/packages/eve/src/execution/session/turn-step-types.ts@@ -54,12 +54,18 @@ export type DurableStepResult = (}| { readonly action: "cancelled" | "steered" }/** The model ended the turn while tasks work; the turn waits for them. */- | { readonly action: "held"; readonly taskIds: readonly string[] }+ | { readonly action: "held"; readonly hold: "tasks"; readonly taskIds: readonly string[] }+ /** The turn waits on a sign-in or tool approval it raised. */| {- readonly action: "park";- readonly authorizationAttemptIds?: readonly string[];- readonly hasPendingAuthorization: boolean;+ readonly action: "held";+ readonly authorizationAttemptIds: readonly string[];readonly hasPendingInputBatch: boolean;+ readonly hold: "request";+ /** Pending input request ids an answer can resolve. */+ readonly inputRequestIds: readonly string[];+ }+ | {+ readonly action: "park";readonly pendingCoordinationCallIds?: readonly string[];readonly pendingTaskToolCalls?: readonly TaskToolCall[];readonly settled?: SettledTurn;@@ -86,7 +92,6 @@ export type TurnOutcome = {readonly usageDelta?: TokenUsage;}| {- readonly authorizationAttemptIds?: readonly string[];readonly cancelled?: true;readonly kind: "park";readonly settled?: SettledTurn;diff --git a/packages/eve/src/execution/session/turn-step.test.ts b/packages/eve/src/execution/session/turn-step.test.tsindex a7d99f3f2..3bd8ab54b 100644--- a/packages/eve/src/execution/session/turn-step.test.ts+++ b/packages/eve/src/execution/session/turn-step.test.ts@@ -1447,6 +1447,68 @@ describe("turnStep", () => {expect(observed).toEqual(expected);});+ it("resumes a sign-in callback as the user who started it, not the last speaker", async () => {+ const alice: SessionAuthContext = {+ attributes: {},+ authenticator: "slack-webhook",+ issuer: "slack",+ principalId: "slack:alice",+ principalType: "user",+ };+ const bob: SessionAuthContext = { ...alice, principalId: "slack:bob" };+ installSessionStoreMocks([+ createStubSession({+ state: setPendingAuthorization(undefined, {+ challenges: [+ {+ attemptId: "attempt-linear",+ challenge: { url: "https://idp.example/authorize" },+ hookUrl: "https://agent.example/callback",+ name: "linear",+ principal: { id: "slack:alice", issuer: "slack", type: "user" },+ principalId: "slack:alice",+ requester: alice,+ },+ ],+ }),+ }),+ ]);+ const ctx = new ContextContainer();+ ctx.set(AuthKey, bob);+ ctx.set(BundleKey, createStubBundle());+ ctx.set(ChannelKey, threadContextAdapter);+ ctx.set(ContinuationTokenKey, "http:shared-thread");+ ctx.set(SessionIdKey, "session-1");++ let observed: SessionAuthContext | null | undefined;+ vi.mocked(createExecutionNodeStep).mockImplementation(() => {+ return async (session): Promise<StepResult> => {+ observed = loadContext().get(AuthKey);+ return { next: null, session };+ };+ });++ await turnStep({+ input: {+ kind: "deliver",+ payloads: [+ {+ authorizationCallback: {+ attemptId: "attempt-linear",+ callback: { method: "GET", params: { code: "oauth-code" } },+ connectionName: "linear",+ },+ },+ ],+ },+ sessionWritable: createTestWritable(),+ serializedContext: serializeContext(ctx),+ sessionState: createStubSessionState(),+ });++ expect(observed).toEqual(alice);+ });+it("keeps a session-scoped dynamic model selection when the first turn is cancelled", async () => {const announcement = "Available skills\n- policy: Tenant policy";const session = createStubSession({@@ -2060,118 +2122,6 @@ describe("turnStep", () => {});});- it("carries a settled turn while an older input batch remains pending", async () => {- const session = appendPendingInputBatch({- requests: [- {- action: {- callId: "call-existing-input",- input: {},- kind: "tool-call",- toolName: "confirm",- },- kind: "question",- prompt: "Continue?",- requestId: "request-existing-input",- },- ],- responseMessages: [],- session: createStubSession(),- });- installSessionStoreMocks([session]);- vi.mocked(createExecutionNodeStep).mockImplementation(() => {- return async (stepSession): Promise<StepResult> => ({- next: null,- session: stepSession,- settledTurn: { output: "settled while approval remains open" },- });- });-- const result = await turnStep({- input: {- kind: "deliver",- payloads: [{ message: "unrelated message" }],- },- sessionWritable: createTestWritable(),- serializedContext: createSerializedContext(),- sessionState: createStubSessionState(),- });-- expect(result).toMatchObject({- action: "park",- hasPendingInputBatch: true,- settled: { output: "settled while approval remains open" },- });- });-- it.each([- {- name: "authorization",- withPending: (session: HarnessSession): HarnessSession => ({- ...session,- state: setPendingAuthorization(session.state, {- challenges: [- {- attemptId: "attempt-statuspage",- challenge: {- instructions: "Sign in to continue",- url: "https://idp.example/authorize",- },- hookUrl: "https://app.example/callback",- name: "statuspage",- principal: { type: "app" },- },- ],- }),- }),- },- {- name: "input batch",- withPending: (session: HarnessSession): HarnessSession =>- appendPendingInputBatch({- requests: [- {- action: {- callId: "call-input",- input: {},- kind: "tool-call",- toolName: "confirm",- },- kind: "question",- prompt: "Continue?",- requestId: "request-input",- },- ],- responseMessages: [],- session,- }),- },- ])("does not infer settled output from a pending $name", async ({ withPending }) => {- const session = createStubSession();- installSessionStoreMocks([session]);- vi.mocked(createExecutionNodeStep).mockImplementation(() => {- return async (stepSession): Promise<StepResult> => ({- next: null,- session: withPending(stepSession),- });- });-- const result = await turnStep({- input: {- kind: "deliver",- payloads: [{ message: "hello" }],- },- sessionWritable: createTestWritable(),- serializedContext: createSerializedContext(),- sessionState: createStubSessionState(),- });-- expect(result.action).toBe("park");- if (result.action === "park") {- expect(result.settled).toBeUndefined();- }- });-it("reads the durable session from normalized turn-step input", async () => {const session = createStubSession({continuationToken: "http:turn-step",@@ -2699,13 +2649,7 @@ describe("turnStep", () => {expect(observedStepInput).toEqual(inputKind === "current" ? { message: `thread=unset; user=${turnInput.message}` } : undefined,);- expect(result).toMatchObject({- action: "park",- hasPendingAuthorization: false,- });- if (result.action === "park") {- expect(result.authorizationAttemptIds).toBeUndefined();- }+ expect(result.action).toBe("park");const persistedSession = vi.mocked(createDurableSessionValues).mock.calls.at(-1)?.[0];expect(persistedSession?.state?.retained).toBe("yes");expect(getPendingAuthorization(persistedSession?.state)).toBeUndefined();diff --git a/packages/eve/src/execution/session/turn-step.ts b/packages/eve/src/execution/session/turn-step.tsindex 55e32aa80..49c18657b 100644--- a/packages/eve/src/execution/session/turn-step.ts+++ b/packages/eve/src/execution/session/turn-step.ts@@ -165,6 +165,15 @@ async function runSessionStep(input: TurnStepInput): Promise<DurableStepResult>ctx.set(AuthKey, delivery.auth ?? null);if (!ctx.has(InitiatorAuthKey)) ctx.set(InitiatorAuthKey, delivery.auth ?? null);}+ // A sign-in callback carries no identity. Resume as the user who started the+ // sign-in, not whoever spoke in the session while it was pending. Approval+ // sign-ins (`candidateId`) bind their responder separately.+ const requester =+ delivery === undefined+ ? completedAuths?.find(({ challenge }) => challenge.candidateId === undefined)?.challenge+ .requester+ : undefined;+ if (requester !== undefined) ctx.set(AuthKey, requester);const initialSession: HarnessSession = {...hydrateDurableSession({compactionOverrides: {diff --git a/packages/eve/src/execution/session/turn-waiting-step.ts b/packages/eve/src/execution/session/turn-waiting-step.tsindex fd866402d..ad01e1391 100644--- a/packages/eve/src/execution/session/turn-waiting-step.ts+++ b/packages/eve/src/execution/session/turn-waiting-step.ts@@ -16,6 +16,8 @@ export async function publishTurnWaitingStep(return await withSessionStateDelta(target, async (input) => {const { sequence, turnId } = input.sessionState.emissionState;const usage = getSessionUsage(readDurableSession(input.sessionState));- return await publishSessionEvents(input, [createTurnWaitingEvent({ sequence, turnId, usage })]);+ return await publishSessionEvents(input, [+ createTurnWaitingEvent({ on: "tasks", sequence, turnId, usage }),+ ]);});}diff --git a/packages/eve/src/execution/session/turn.test.ts b/packages/eve/src/execution/session/turn.test.tsindex bdb02e83a..d436d74ba 100644--- a/packages/eve/src/execution/session/turn.test.ts+++ b/packages/eve/src/execution/session/turn.test.ts@@ -450,8 +450,6 @@ describe("SessionExecution checkpoints", () => {turnStepWork(async () => ({action: "park",pendingCoordinationCallIds: ["hold-call"],- hasPendingAuthorization: false,- hasPendingInputBatch: false,serializedContext: {},sessionState,})),@@ -554,8 +552,6 @@ describe("SessionExecution checkpoints", () => {.mockImplementationOnce(turnStepWork(async () => ({action: "park",- hasPendingAuthorization: false,- hasPendingInputBatch: false,pendingCoordinationCallIds: ["hold-call"],serializedContext: {},sessionState,@@ -618,8 +614,6 @@ describe("SessionExecution checkpoints", () => {.mockImplementation(turnStepWork(async (input) => ({action: "park",- hasPendingAuthorization: false,- hasPendingInputBatch: false,serializedContext: input.serializedContext,sessionState: input.sessionState,settled: { output: "Done." },@@ -640,16 +634,16 @@ describe("SessionExecution checkpoints", () => {});it.each([- { capabilities: undefined, parks: false, serializedContext: {} },- { capabilities: { requestInput: true }, parks: true, serializedContext: {} },+ { capabilities: undefined, holds: false, serializedContext: {} },+ { capabilities: { requestInput: true }, holds: true, serializedContext: {} },{capabilities: undefined,- parks: true,+ holds: true,serializedContext: { "eve.sessionCallback": { callId: "call_1", token: "parent" } },},])(- "parks on pending input only when someone can answer it: %o",- async ({ capabilities, parks, serializedContext }) => {+ "holds on pending input only when someone can answer it: %o",+ async ({ capabilities, holds, serializedContext }) => {const inbox: SessionInbox = {claimedTokens: [],claimSessionHook: vi.fn(),@@ -672,9 +666,11 @@ describe("SessionExecution checkpoints", () => {.mockReset().mockImplementation(turnStepWork(async (input) => ({- action: "park",- hasPendingAuthorization: false,+ action: "held",+ authorizationAttemptIds: [],hasPendingInputBatch: true,+ hold: "request",+ inputRequestIds: ["request_1"],serializedContext: input.serializedContext,sessionState: input.sessionState,})),@@ -683,8 +679,16 @@ describe("SessionExecution checkpoints", () => {const turn = execution.runTurn({delivery: { kind: "deliver", payloads: [{ message: "Deploy the release." }] },});- if (parks) {- await expect(turn).resolves.toMatchObject({ kind: "park" });+ if (holds) {+ // Someone can answer, so the turn waits for them.+ const outcome = await Promise.race([+ turn.then(+ () => "settled",+ () => "rejected",+ ),+ new Promise((resolve) => setTimeout(() => resolve("waiting"), 20)),+ ]);+ expect(outcome).toBe("waiting");} else {await expect(turn).rejects.toThrow("cannot request human input");}@@ -726,8 +730,6 @@ describe("SessionExecution checkpoints", () => {expect(input.abortSignal?.aborted).toBe(true);return {action: "park",- hasPendingAuthorization: false,- hasPendingInputBatch: false,serializedContext: input.serializedContext,sessionState: completedState,settled,@@ -767,8 +769,6 @@ describe("SessionExecution checkpoints", () => {vi.mocked(turnStep).mockImplementation(turnStepWork(async () => ({action: "park",- hasPendingAuthorization: false,- hasPendingInputBatch: false,pendingCoordinationCallIds: ["child-call"],serializedContext: {},sessionState,@@ -844,8 +844,6 @@ describe("SessionExecution checkpoints", () => {.mockImplementation(turnStepWork(async () => ({action: "park",- hasPendingAuthorization: false,- hasPendingInputBatch: false,pendingCoordinationCallIds: ["deploy-call"],serializedContext: {},sessionState,@@ -929,8 +927,6 @@ describe("SessionExecution checkpoints", () => {.mockImplementation(turnStepWork(async () => ({action: "park",- hasPendingAuthorization: false,- hasPendingInputBatch: false,pendingCoordinationCallIds: ["wait-call"],pendingTaskToolCalls: [{ callId: "wait-call", kind: "task_wait" }],serializedContext: {},@@ -1013,8 +1009,6 @@ describe("SessionExecution checkpoints", () => {.mockImplementation(turnStepWork(async () => ({action: "park",- hasPendingAuthorization: false,- hasPendingInputBatch: false,pendingCoordinationCallIds: ["hold-call"],serializedContext: {},sessionState,@@ -1108,8 +1102,6 @@ describe("SessionExecution checkpoints", () => {.mockImplementationOnce(turnStepWork(async () => ({action: "park",- hasPendingAuthorization: false,- hasPendingInputBatch: false,pendingCoordinationCallIds: tools.map((name) => `${name}-call`),serializedContext: {},sessionState,@@ -1193,8 +1185,6 @@ describe("SessionExecution checkpoints", () => {.mockImplementationOnce(turnStepWork(async () => ({action: "park",- hasPendingAuthorization: false,- hasPendingInputBatch: false,pendingCoordinationCallIds: ["wait-call"],pendingTaskToolCalls: [{ callId: "wait-call", kind: "task_wait" }],serializedContext: {},@@ -1229,8 +1219,6 @@ describe("SessionExecution checkpoints", () => {.mockImplementationOnce(turnStepWork(async (input) => ({action: "park",- hasPendingAuthorization: false,- hasPendingInputBatch: false,serializedContext: input.serializedContext,sessionState: input.sessionState,settled: { output: "Done." },@@ -1293,8 +1281,6 @@ describe("SessionExecution checkpoints", () => {turnStepWork(async () => ({action: "park",pendingCoordinationCallIds: ["hold-call"],- hasPendingAuthorization: false,- hasPendingInputBatch: false,serializedContext: {},sessionState,})),diff --git a/packages/eve/src/execution/session/turn.ts b/packages/eve/src/execution/session/turn.tsindex 3d2bcfc86..7a305760c 100644--- a/packages/eve/src/execution/session/turn.ts+++ b/packages/eve/src/execution/session/turn.ts@@ -3,13 +3,7 @@ import { sleep } from "#compiled/@workflow/core/index.js";import type { DeliverHookPayload, SessionCapabilities, TurnCaller } from "#channel/types.js";import { cancelDescendantTurnsStep } from "#execution/cancel-descendant-turns-step.js";import { dispatchCoordinationStep } from "#execution/coordination-dispatch-step.js";-import { routeDeliverToChildren } from "#execution/route-child-delivery.js";-import { routeSelectedDelivery } from "#execution/session/route-selected-delivery.js";-import {- isSteeringMessage,- type SessionInputQueue,- type SteeringTurn,-} from "#execution/session/input-queue.js";+import type { SessionInputQueue } from "#execution/session/input-queue.js";import { taskToolResult, type TaskToolCall } from "#execution/tasks/calls.js";import { TASK_WAIT_TOOL_NAME } from "#protocol/task-tools.js";import { resolveTurnPrincipal } from "#execution/session/principal.js";@@ -26,9 +20,8 @@ import {sessionCommandHookToken,sessionInboxHookToken,} from "#execution/session-inbox/address.js";-import type { SessionInboxPayload, SessionInboxReader } from "#execution/session-inbox/inbox.js";+import type { SessionInboxReader } from "#execution/session-inbox/inbox.js";import { publishTurnWaitingStep } from "#execution/session/turn-waiting-step.js";-import { admitSessionInboxPayload } from "#execution/session/admission.js";import type { SessionStateCursor } from "#execution/session/state-cursor.js";import {batchAgentStarts,@@ -43,10 +36,7 @@ import type {TurnStepPayload,} from "#execution/session/turn-step-types.js";import { turnStep } from "#execution/session/turn-step.js";-import { activeTurnId } from "#harness/active-turn-id.js";-import { coalesceDeliveries } from "#harness/messages.js";-import { TurnCancelledError } from "#harness/turn-cancellation.js";-import { decodeSessionInboxPayload } from "#execution/session-inbox/protocol.js";+import { ActiveTurn } from "#execution/session/active-turn.js";import {findBlockingWorkflowToolRun,isInboxToolResultFromRecordedWorkflowToolRun,@@ -181,6 +171,20 @@ export class SessionExecution {};}+ if (result.action === "held" && result.hold === "request") {+ if (+ result.hasPendingInputBatch &&+ this.input.capabilities?.requestInput !== true &&+ !hasDelegatedCallerContext(this.input.cursor.serializedContext)+ ) {+ throw new Error(NO_INPUT_CAPABILITY_ERROR_MESSAGE);+ }+ const woke = await this.waitForHeldRequest(turn, result);+ if (woke === "cancelled") return await this.finishCancelledTurn(turn);+ nextStepInput = { delivery: woke };+ continue;+ }+if (result.action === "held") {const woke = await this.waitForHeldTurn(turn);if (woke === "cancelled") return await this.finishCancelledTurn(turn);@@ -215,20 +219,7 @@ export class SessionExecution {continue;}- if (result.action === "park") {- if (- result.hasPendingInputBatch &&- this.input.capabilities?.requestInput !== true &&- !hasDelegatedCallerContext(this.input.cursor.serializedContext)- ) {- throw new Error(NO_INPUT_CAPABILITY_ERROR_MESSAGE);- }- return {- authorizationAttemptIds: result.authorizationAttemptIds,- kind: "park",- settled: result.settled,- };- }+ if (result.action === "park") return { kind: "park", settled: result.settled };const steering = await turn.takeSteering();nextStepInput = steering === undefined ? undefined : { delivery: steering };@@ -280,6 +271,7 @@ export class SessionExecution {case "workflow":await this.handleWorkflowMessage(next.message);continue;+ case "input":case "runtime-action-result":case "timeout":continue;@@ -287,6 +279,34 @@ export class SessionExecution {}}+ /**+ * The turn waits on a sign-in or tool approval it raised. It wakes with the+ * delivery its next step reads: the sign-in callback, an approval answer+ * from any responder, or a message from the turn's own person, which steers+ * the turn and cancels the request. Anyone else's message queues behind it.+ */+ private async waitForHeldRequest(+ turn: ActiveTurn,+ held: {+ readonly authorizationAttemptIds: readonly string[];+ readonly inputRequestIds: readonly string[];+ },+ ): Promise<DeliverHookPayload | "cancelled"> {+ const attemptIds = new Set(held.authorizationAttemptIds);+ const requestIds = new Set(held.inputRequestIds);+ while (true) {+ const callbacks = this.input.queue.takeAuthorizations(attemptIds);+ if (callbacks !== undefined) return { kind: "deliver", payloads: callbacks };+ const answer = turn.takeInputResponses(requestIds);+ if (answer !== undefined) return answer;+ const steering = await turn.takeSteering({ heldOnPerson: true });+ if (steering !== undefined) return steering;+ const next = await turn.nextRuntimeEvent([]);+ if (next === "cancelled") return next;+ if (next.kind === "workflow") await this.handleWorkflowMessage(next.message);+ }+ }+private async waitForRuntimeActionResults(input: {readonly initialAcceptedAtMs: number | undefined;readonly initialResults: readonly RuntimeActionResult[];@@ -338,6 +358,7 @@ export class SessionExecution {}continue;}+ if (next.kind === "input") continue;if (next.kind === "steering") {// Only the first steering message interrupts; later ones wait for the calls anyway.if (!interrupted) await this.interruptWaitedWorkflowCalls(input.pendingCallIds, results);@@ -445,246 +466,3 @@ function startTaskWait(call: Extract<TaskToolCall, { readonly kind: "task_wait"const timer = timeoutMs === undefined ? undefined : sleep(timeoutMs).then(() => callId);return { callId, startedAtMs: Date.now(), timedOut: false, timer };}--type RuntimeEvent =- | { readonly kind: "runtime-action-result"; readonly results: readonly RuntimeActionResult[] }- | { readonly kind: "workflow"; readonly message: WorkflowToolRunMessage }- /** A steering message that answered no pending request arrived during the wait. */- | { readonly kind: "steering" }- /** A `task_wait` call's timeout passed. */- | { readonly kind: "timeout"; readonly callId: string }- | "cancelled";--/**- * Admission policy, cancellation, and steering for one active turn.- * Deliveries admitted while a model step runs stay in the shared queue until- * the turn reaches a committed boundary; a runtime-action wait routes them- * eagerly so a proxied child can receive the answer it is blocked on.- *- * The pump signals cancellation and eligible steering immediately. Cancellation- * aborts the turn; steering only interrupts generation before assistant output- * or local tool execution. Both retain ordered admission at the next boundary.- */-class ActiveTurn {- private readonly admitted = new Set<number>();- private readonly routedToChildren = new Set<number>();- private readonly runtimeResults: RuntimeEvent[] = [];- private readonly controller = new AbortController();- private readonly expectedTurnId: string;- private readonly input: SessionExecutionInput;- /** Who alone steers the turn: its principal, or its delegated caller. */- private readonly identity: SteeringTurn;- private readonly unsubscribe: () => void;- private unsubscribeDelivery: () => void;- private steeringController = new AbortController();- /** The delegated caller of the latest message the turn read. */- caller: TurnCaller | undefined;-- constructor(- input: SessionExecutionInput,- owner: { readonly caller: TurnCaller | undefined; readonly principal: string },- ) {- this.input = input;- this.caller = owner.caller;- this.identity = { callerCallId: owner.caller?.callId, principal: owner.principal };- this.expectedTurnId = activeTurnId(input.cursor.sessionState.emissionState);- this.unsubscribe = input.inbox.onInterrupt((payload) => {- if (this.cancelsThisTurn(payload)) this.abort();- });- this.unsubscribeDelivery = input.inbox.onDelivery(this.signalSteering);- }-- private readonly signalSteering = (payload: SessionInboxPayload): void => {- let delivery;- try {- delivery = decodeSessionInboxPayload(payload);- } catch {- return;- }- if (- delivery.kind === "deliver" &&- isSteeringMessage(delivery, this.identity) &&- !this.input.cursor.sessionState.hasProxyInputRequests- )- this.steeringController.abort();- };-- get signal(): AbortSignal {- return this.controller.signal;- }-- dispose(): void {- this.unsubscribe();- this.unsubscribeDelivery();- }-- get steeringSignal(): AbortSignal {- return this.steeringController.signal;- }-- private resetSteering(): void {- if (!this.steeringController.signal.aborted) return;- this.unsubscribeDelivery();- this.steeringController = new AbortController();- // Admission can yield while new deliveries are pumped. Replaying the- // unread inbox keeps those arrivals attached to the next generation.- this.unsubscribeDelivery = this.input.inbox.onDelivery(this.signalSteering);- }-- /** Admits everything the pump accepted while the last step ran. */- async admitBoundary(): Promise<void> {- const pending = this.input.inbox.drain();- for (const payload of pending) await this.admit(payload);- }-- /**- * Steering admitted during this turn, routed to children first and coalesced.- * The next step reads it as input, so its signal must not interrupt that- * step; only deliveries still unread re-signal the next generation.- */- async takeSteering(): Promise<DeliverHookPayload | undefined> {- const steering: DeliverHookPayload[] = [];- while (true) {- const selection = this.input.queue.takeSteering(this.admitted, this.identity);- if (selection === undefined) break;- for (const sequence of selection.sequences) this.admitted.delete(sequence);- const routed = await routeSelectedDelivery(selection, this.input.cursor);- if (routed.kind === "cancel-turn") {- this.abort();- break;- }- if (routed.kind === "turn") steering.push(routed.delivery);- }- this.resetSteering();- if (steering.length === 0) return undefined;- const delivery = steering.length === 1 ? steering[0]! : coalesceDeliveries(steering);- if (delivery.caller !== undefined) this.caller = delivery.caller;- return delivery;- }-- /**- * Removes the admitted run messages the session applies at once instead of- * in a runtime wait, or only those of `kind`.- */- takeBoundaryMessages(kind?: "agent-started"): WorkflowToolRunMessage[] {- const taken: WorkflowToolRunMessage[] = [];- const kept: RuntimeEvent[] = [];- for (const event of this.runtimeResults) {- const message = asBoundaryMessage(event);- if (message === undefined || (kind !== undefined && message.kind !== kind)) kept.push(event);- else taken.push(message);- }- this.runtimeResults.splice(0, this.runtimeResults.length, ...kept);- return taken;- }-- /**- * Next runtime result, workflow message, steering, or timeout, admitting- * inbox traffic while waiting. Deliveries admitted before the wait began are- * routed first, so a message that arrived as the calls started still steers.- * `timers` are durable sleeps raced against the inbox.- */- async nextRuntimeEvent(timers: readonly Promise<string>[]): Promise<RuntimeEvent> {- while (true) {- const steered = await this.routeAdmittedToChildren();- if (this.signal.aborted) return "cancelled";- if (steered) return { kind: "steering" };- const event = this.runtimeResults.shift();- if (event !== undefined) return event;- const elapsed = await this.waitForInboxOrTimer(timers);- if (elapsed !== undefined) return { callId: elapsed, kind: "timeout" };- const payload = await this.input.inbox.next();- if (payload === undefined)- throw new Error("Session inbox closed before runtime actions completed.");- await this.admit(payload);- }- }-- /** Resolves with the id of the call whose timer won, or `undefined` once inbox input is ready. */- private async waitForInboxOrTimer(- timers: readonly Promise<string>[],- ): Promise<string | undefined> {- if (timers.length === 0 || this.input.inbox.hasPending()) return undefined;- const inboxReady = this.input.inbox.whenPending().then(() => undefined);- return await Promise.race([inboxReady, ...timers]);- }-- private cancelsThisTurn(payload: SessionInboxPayload): boolean {- if (payload.kind === "reset") return true;- if (payload.kind !== "cancel") return false;- return payload.turnId === undefined || payload.turnId === this.expectedTurnId;- }-- private async admit(value: SessionInboxPayload): Promise<void> {- const admitted = await admitSessionInboxPayload(value, this.input);- switch (admitted.kind) {- case "delivery":- this.admitted.add(admitted.admission.sequence);- return;- case "runtime-action-result":- this.runtimeResults.push({- kind: "runtime-action-result",- results: admitted.payload.results,- });- return;- case "workflow":- this.runtimeResults.push({ kind: "workflow", message: admitted.message });- return;- case "cancel":- if (this.cancelsThisTurn(value)) this.abort();- return;- case "consumed":- return;- }- }-- /**- * During a runtime wait, descendant-bound answers cannot wait for the- * boundary. Returns whether a newly routed delivery still steers: it- * answered nothing, so its message is for the model.- */- private async routeAdmittedToChildren(): Promise<boolean> {- let steered = false;- for (const sequence of this.admitted) {- if (this.routedToChildren.has(sequence)) continue;- const delivery = this.input.queue.delivery(sequence);- if (delivery === undefined) {- this.admitted.delete(sequence);- continue;- }- this.routedToChildren.add(sequence);- const routed = await this.input.cursor.advance((state) =>- routeDeliverToChildren({ delivery, ...state }),- );- if (routed.kind === "cancel-turn") {- this.input.queue.replaceDelivery(sequence, undefined);- this.admitted.delete(sequence);- this.abort();- return false;- }- this.input.queue.replaceDelivery(sequence, routed.remainder);- if (routed.remainder === undefined) {- this.admitted.delete(sequence);- continue;- }- if (isSteeringMessage(routed.remainder, this.identity)) steered = true;- }- return steered;- }-- private abort(): void {- if (this.controller.signal.aborted) return;- this.controller.abort(new TurnCancelledError());- }-}--function asBoundaryMessage(event: RuntimeEvent): WorkflowToolRunMessage | undefined {- if (event === "cancelled" || event.kind !== "workflow") return undefined;- // Tasks run beside the turn, so everything their runs send, such as a- // question, reaches the session at the next boundary rather than at turn end.- if (event.message.from.taskId !== undefined) return event.message;- // An `execute` run can open a session after its call returns, when the turn- // may have no wait left to take the message, and publishing it needs none.- if (event.message.kind === "agent-started") return event.message;- return undefined;-}diff --git a/packages/eve/src/execution/settle-cancelled-turn-step.ts b/packages/eve/src/execution/settle-cancelled-turn-step.tsindex dcb3a6ec2..e263c7ab0 100644--- a/packages/eve/src/execution/settle-cancelled-turn-step.ts+++ b/packages/eve/src/execution/settle-cancelled-turn-step.ts@@ -14,6 +14,12 @@ import {} from "#execution/session/state-delta.js";import { relayWithdrawnRequests } from "#execution/tools/workflow/withdraw-step.js";import { emitCancelledTurn } from "#harness/cancelled-turn-emission.js";+import { declinedSignInEvents, withdrawHeldSignIns } from "#harness/held-requests.js";+import {+ cancelApprovalInputBatches,+ getPendingApprovalRequests,+} from "#harness/hitl/approval-input-requests.js";+import { createInputResolvedEvent } from "#protocol/message.js";import type { HarnessModelMessage } from "#harness/messages.js";import { clearPendingSessionLimitPrompt } from "#harness/input-requests.js";import { getHarnessEmissionState, setHarnessEmissionState } from "#harness/emission.js";@@ -25,6 +31,8 @@ import {} from "#harness/turn-tag-state.js";import type { TokenUsage } from "#shared/token-usage.js";+const CANCELLED_REASON = "Cancelled.";+export interface CancelledTurnSettleResult {readonly serializedContext: Record<string, unknown>;readonly sessionState: DurableSessionState;@@ -67,15 +75,42 @@ export async function settleCancelledTurn(history: input.history,};const durableState = step.durableSession.state;+ // Every request the turn held ends with it: its sign-ins and its approvals.+ const withdraw = { completedAt: Date.now(), reason: CANCELLED_REASON };+ const withdrawal = withdrawHeldSignIns(durableState, withdraw);+ const cancelledApprovals = getPendingApprovalRequests(durableState);const { published, result: usage } = await publishFromSessionStep(step, {origin: "own",- publish: (emit) =>- emitCancelledTurn(- emit,- getHarnessEmissionState(durableState),- getSessionUsage(step.durableSession),- ),- updateSession(session, emissionState) {+ async publish(emit) {+ const emissionState = getHarnessEmissionState(durableState);+ for (const event of declinedSignInEvents(+ withdrawal.withdrawn,+ CANCELLED_REASON,+ emissionState,+ )) {+ await emit(event);+ }+ if (cancelledApprovals.length > 0) {+ await emit(+ createInputResolvedEvent({+ resolutions: cancelledApprovals.map((request) => ({+ kind: request.kind,+ outcome: "cancelled",+ requestId: request.requestId,+ })),+ sequence: emissionState.sequence,+ stepIndex: emissionState.stepIndex,+ turnId: emissionState.turnId,+ }),+ );+ }+ return await emitCancelledTurn(emit, emissionState, getSessionUsage(step.durableSession));+ },+ updateSession(baseSession, emissionState) {+ const session = {+ ...baseSession,+ state: withdrawHeldSignIns(baseSession.state, withdraw).state,+ };// `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@@ -87,8 +122,12 @@ export async function settleCancelledTurn(input.sessionState.emissionState.turnId;const cancelledSession = setHarnessEmissionState(clearPendingSessionLimitPrompt(- commitCancelledCoordinationBatch(- removeBlockingWorkflowToolRuns({ ...session, outputSchema: undefined }, owningTurnId),+ // After the coordination batch, which owns an assistant response it+ // shares with approvals raised beside its calls.+ cancelApprovalInputBatches(+ commitCancelledCoordinationBatch(+ removeBlockingWorkflowToolRuns({ ...session, outputSchema: undefined }, owningTurnId),+ ),),),emissionState,diff --git a/packages/eve/src/execution/tools/connection-tools.test.ts b/packages/eve/src/execution/tools/connection-tools.test.tsnew file mode 100644index 000000000..dca4badd7--- /dev/null+++ b/packages/eve/src/execution/tools/connection-tools.test.ts@@ -0,0 +1,156 @@+import { describe, expect, it, vi } from "vitest";++import { ConnectionAuthorizationRequiredError } from "#connections/errors.js";+import { ContextContainer, contextStorage } from "#context/container.js";+import { AuthKey, SessionIdKey } from "#context/keys.js";+import { ConnectionRegistryKey } from "#context/providers/connection-key.js";+import {+ CallbackBaseUrlKey,+ isAuthorizationSignal,+ PendingAuthorizationResultKey,+} from "#harness/authorization.js";+import type { ConnectionRegistry } from "#runtime/connections/registry-types.js";+import type { ResolvedConnectionDefinition } from "#runtime/types.js";+import {+ type ConnectionClient,+ type ConnectionToolMetadata,+ defineInteractiveAuthorization,+} from "#shared/connection-types.js";++import { CONNECTION_EXECUTE_TOOL_NAME } from "./connection-target.js";+import { CONNECTION_SEARCH_TOOL_NAME, resolveConnectionTools } from "./connection-tools.js";++const listIssues: ConnectionToolMetadata = {+ description: "List open issues.",+ inputSchema: { type: "object", properties: {} },+ name: "list_issues",+};++function setup() {+ const startAuthorization = vi.fn(async ({ callbackUrl }: { callbackUrl: string }) => ({+ challenge: { url: `https://idp.example.com/authorize?redirect=${callbackUrl}` },+ }));+ const authorization = {+ ...defineInteractiveAuthorization({+ getToken: async () => {+ throw new ConnectionAuthorizationRequiredError("unused");+ },+ startAuthorization,+ completeAuthorization: async () => ({ token: "token" }),+ }),+ vercelConnect: { connector: "workspace/agent" },+ };+ const connections = ["github", "linear", "notion"].map(+ (connectionName) =>+ ({+ authorization: connectionName === "github" ? undefined : authorization,+ connectionName,+ description: connectionName,+ protocol: "mcp",+ url: `https://${connectionName}.example.com/mcp`,+ }) as ResolvedConnectionDefinition,+ );+ const client = (connectionName: string): ConnectionClient => ({+ close: async () => {},+ connect: async () => {},+ executeTool: async () => ({ content: [] }),+ getToolMetadata: async () => {+ if (connectionName === "github") return [listIssues];+ throw new ConnectionAuthorizationRequiredError(connectionName);+ },+ });+ const registry: ConnectionRegistry = {+ dispose: async () => {},+ getClient: client,+ getConnectionApproval: () => undefined,+ getConnectionNames: () => connections.map((connection) => connection.connectionName),+ getConnections: () => connections,+ };++ const ctx = new ContextContainer();+ ctx.set(AuthKey, {+ attributes: {},+ authenticator: "test",+ principalId: "alice",+ principalType: "user",+ });+ ctx.set(SessionIdKey, "connection-tools");+ ctx.set(CallbackBaseUrlKey, "https://agent.example.com");+ ctx.set(ConnectionRegistryKey, registry);++ async function call(toolName: string, input: unknown): Promise<unknown> {+ return await contextStorage.run(ctx, async () => {+ const execute = resolveConnectionTools()?.[toolName]?.execute;+ if (execute === undefined) throw new Error(`${toolName} is not defined.`);+ return await execute(input, { callId: "call-1", messages: [] } as never);+ });+ }++ return { call, ctx, startAuthorization };+}++describe("connection tools authorization", () => {+ it("prompts only for one named connection with signIn, never for a plain search", async () => {+ const { call, startAuthorization } = setup();+ const signIn = (connection: string) => ({+ connection,+ error: `Sign-in required: the user has not signed in to "${connection}", so its tools cannot be listed. If the request needs "${connection}", call connection_search with connection "${connection}" and signIn: true to ask the user to sign in.`,+ requiresSignIn: true,+ });++ const unscoped = await call(CONNECTION_SEARCH_TOOL_NAME, { query: "open issues" });+ expect(isAuthorizationSignal(unscoped)).toBe(false);+ expect(unscoped).toMatchObject({+ tools: [{ connection: "github", tool: "list_issues" }],+ unavailable: [signIn("linear"), signIn("notion")],+ });+ const scoped = await call(CONNECTION_SEARCH_TOOL_NAME, { connection: "linear" });+ expect(scoped).toEqual({ tools: [], total: 0, unavailable: [signIn("linear")] });+ await expect(call(CONNECTION_SEARCH_TOOL_NAME, { signIn: true })).rejects.toThrow(+ "connection_search with signIn: true requires `connection`.",+ );+ const signedIn = await call(CONNECTION_SEARCH_TOOL_NAME, {+ connection: "github",+ query: "issues",+ signIn: true,+ });+ expect(signedIn).toMatchObject({ tools: [{ tool: "list_issues" }], total: 1 });+ expect(startAuthorization).not.toHaveBeenCalled();++ const requested = await call(CONNECTION_SEARCH_TOOL_NAME, {+ connection: "linear",+ query: "issues",+ signIn: true,+ });+ expect(requested).toMatchObject({+ challenges: [+ { grant: "workspace/agent", name: "linear", requester: { principalId: "alice" } },+ ],+ });+ expect(startAuthorization).toHaveBeenCalledOnce();++ const executed = await call(CONNECTION_EXECUTE_TOOL_NAME, {+ connection: "notion",+ tool: "list_issues",+ });+ expect(executed).toMatchObject({ challenges: [{ name: "notion" }] });+ expect(startAuthorization).toHaveBeenCalledTimes(2);+ });++ it("reports a token refused right after sign-in instead of asking again", async () => {+ const { call, ctx, startAuthorization } = setup();+ // The user finished signing in to linear, but the service still refuses the token.+ ctx.set(PendingAuthorizationResultKey, [+ {+ callback: { method: "GET", params: { code: "ok" } },+ hookUrl: "https://agent.example.com/callback",+ name: "linear",+ },+ ]);++ await expect(call(CONNECTION_SEARCH_TOOL_NAME, { connection: "linear" })).rejects.toThrow(+ 'Authorization failed for "linear": the service rejected the token immediately after authorization.',+ );+ expect(startAuthorization).not.toHaveBeenCalled();+ });+});diff --git a/packages/eve/src/execution/tools/connection-tools.ts b/packages/eve/src/execution/tools/connection-tools.tsindex 81e886c2d..d0fdaf322 100644--- a/packages/eve/src/execution/tools/connection-tools.ts+++ b/packages/eve/src/execution/tools/connection-tools.ts@@ -12,12 +12,7 @@ import {} from "#connections/errors.js";import { loadContext } from "#context/container.js";import { ConnectionRegistryKey } from "#context/providers/connection-key.js";-import {- getAuthorizationResults,- requestAuthorization,- type AuthorizationChallenge,- type AuthorizationSignal,-} from "#harness/authorization.js";+import { getAuthorizationResults, type AuthorizationSignal } from "#harness/authorization.js";import { reportNestedToolAction } from "#harness/nested-actions.js";import { createLogger } from "#internal/logging.js";import type { ConnectionRegistry } from "#runtime/connections/registry-types.js";@@ -71,6 +66,8 @@ const CONNECTION_SEARCH_DESCRIPTION = ["Find tools in your connected services (MCP servers and OpenAPI APIs).","Returns each matching tool's connection, name, description, and TypeScript signature.","Omit `query` to list a connection's tools. Call a found tool with connection_execute.",+ "Connections the user has not signed in to are listed under `unavailable` with `requiresSignIn`;",+ "when the request needs one, search it again with `connection` and `signIn: true` to ask the user to sign in.","Prefer connected services over web search or general knowledge when a request relates to them.",].join(" ");@@ -89,6 +86,11 @@ const CONNECTION_SEARCH_INPUT_SCHEMA: JsonObject = {"Words describing the capability, such as 'list open issues'. Omit to list every tool.",},connection: { type: "string", description: "Only search this connection." },+ signIn: {+ type: "boolean",+ description:+ "Ask the user to sign in to `connection` first, then search it. Requires `connection`. Use only for a connection listed with `requiresSignIn` that the request needs.",+ },limit: {type: "integer",minimum: 1,@@ -119,6 +121,7 @@ interface ConnectionSearchInput {readonly limit?: number;readonly offset?: number;readonly query?: string;+ readonly signIn?: boolean;}interface ConnectionSearchMatch {@@ -131,6 +134,8 @@ interface ConnectionSearchMatch {interface UnavailableConnection {readonly connection: string;readonly error: string;+ /** Present when `signIn: true` can make the connection available. */+ readonly requiresSignIn?: true;/** Whether the failure is final rather than waiting on authorization. */readonly terminal: boolean;}@@ -187,9 +192,10 @@ function connectionToolLabel(_closure: object, input: unknown): string {function connectionSearchLabel(_closure: object, input: unknown): string {const connection = isObject(input) ? input.connection : undefined;- return typeof connection === "string" && connection !== ""- ? `Search ${displayProperName(connection)} tools`- : "Search connected tools";+ if (typeof connection !== "string" || connection === "") return "Search connected tools";+ return isObject(input) && input.signIn === true+ ? `Connect ${displayProperName(connection)}`+ : `Search ${displayProperName(connection)} tools`;}// ---------------------------------------------------------------------------@@ -202,27 +208,34 @@ async function searchConnectionTools(): Promise<ConnectionSearchOutput | AuthorizationSignal> {const input = (isObject(rawInput) ? rawInput : {}) as ConnectionSearchInput;const registry = requireRegistry();+ const connectionName =+ input.connection === undefined || input.connection === "" ? undefined : input.connection;+ if (input.signIn === true && connectionName === undefined) {+ throw new Error(+ "connection_search with signIn: true requires `connection`. Ask the user to sign in to one connection at a time.",+ );+ }const targets =- input.connection === undefined || input.connection === ""+ connectionName === undefined? registry.getConnections()- : [requireConnection(registry, input.connection)];+ : [requireConnection(registry, connectionName)];+ // Finishing a sign-in the user already completed never prompts. Starting one+ // is reserved for `signIn: true` on one named connection and connection_execute.const auth = createAuthorizationExecution();await completePendingAuthorizations(registry, targets, auth);- const challenges: AuthorizationChallenge[] = [];const unavailable: UnavailableConnection[] = [];const candidates: RankCandidate[] = [];for (const connection of targets) {- const listed = await listConnectionTools(registry, connection, auth);- if ("challenges" in listed) challenges.push(...listed.challenges);- else if ("unavailable" in listed) unavailable.push(listed.unavailable);+ const listed = await listConnectionTools(registry, connection, auth, input.signIn === true);+ if ("signIn" in listed) return listed.signIn;+ if ("unavailable" in listed) unavailable.push(listed.unavailable);else {for (const tool of listed.tools) candidates.push({ connection, tool });}}- if (challenges.length > 0) return requestAuthorization(challenges);const terminal = unavailable.filter((entry) => entry.terminal);if (targets.length > 0 && terminal.length === targets.length) {throw new Error(terminal.map((entry) => entry.error).join("\n"));@@ -246,20 +259,22 @@ async function searchConnectionTools(total: ranked.length,};if (unavailable.length > 0) {- output.unavailable = unavailable.map(({ connection, error }) => ({ connection, error }));+ output.unavailable = unavailable.map(({ terminal: _terminal, ...entry }) => entry);}return output;}type ListedConnectionTools =| { readonly tools: readonly ConnectionToolMetadata[] }- | { readonly challenges: readonly AuthorizationChallenge[] }+ | { readonly signIn: AuthorizationSignal }| { readonly unavailable: UnavailableConnection };+/** Lists a connection's tools, starting its sign-in instead when `signIn` asks for it. */async function listConnectionTools(registry: ConnectionRegistry,connection: ResolvedConnectionDefinition,auth: ReturnType<typeof createAuthorizationExecution>,+ signIn: boolean,): Promise<ListedConnectionTools> {const name = connection.connectionName;try {@@ -267,29 +282,33 @@ async function listConnectionTools(} catch (error) {if (isConnectionAuthorizationRequiredError(error)) {const scoped = await resolveInteractiveAuthorization(registry, name);- if (scoped === undefined) {+ // The token the user just signed in with was refused. Asking again would+ // loop, so report the failure instead.+ if (scoped !== undefined && auth.isJustAuthorized(scoped)) {return {unavailable: {connection: name,- error: `"${name}" requires authorization and cannot start interactive sign-in.`,- terminal: false,- },- };- }- try {- return { challenges: (await auth.handleError(error, scoped)).challenges };- } catch (startError) {- log.warn("connection authorization failed", { connection: name, error: startError });- return {- unavailable: {- connection: name,- error: isConnectionAuthorizationFailedError(startError)- ? startError.message- : `Failed to start authorization for "${name}": ${toErrorMessage(startError)}`,+ error: `Authorization failed for "${name}": the service rejected the token immediately after authorization.`,terminal: true,},};}+ if (scoped === undefined) {+ const cannotSignIn = `"${name}" requires authorization and cannot start interactive sign-in.`;+ if (signIn) throw new Error(cannotSignIn);+ return { unavailable: { connection: name, error: cannotSignIn, terminal: false } };+ }+ if (signIn) return { signIn: await auth.handleError(error, scoped) };+ return {+ unavailable: {+ connection: name,+ error:+ `Sign-in required: the user has not signed in to "${name}", so its tools cannot be listed. ` ++ `If the request needs "${name}", call connection_search with connection "${name}" and signIn: true to ask the user to sign in.`,+ requiresSignIn: true,+ terminal: false,+ },+ };}log.warn("failed to load connection tools", { connection: name, error });return {diff --git a/packages/eve/src/harness/authorization.test.ts b/packages/eve/src/harness/authorization.test.tsindex 5467fb725..d47813e1a 100644--- a/packages/eve/src/harness/authorization.test.ts+++ b/packages/eve/src/harness/authorization.test.ts@@ -8,6 +8,7 @@ import {consumeAuthorizationResult,getPendingAuthorization,getHookUrl,+ getSupersededAuthorizationChallenges,PendingAuthorizationResultKey,resolveActiveAuthorizationChallenges,setPendingAuthorization,@@ -207,6 +208,29 @@ describe("pending authorization attempts", () => {).toEqual([otherPrincipal, latest]);});+ it("shows one sign-in per Vercel Connect grant across tools and connections", () => {+ const alice = { id: "alice", issuer: "idp", type: "user" } as const;+ const bob = { id: "bob", issuer: "idp", type: "user" } as const;+ const grant = (name: string, attemptId: string, principal: ConnectionPrincipal = alice) => ({+ ...challenge(name, attemptId, principal),+ grant: "linear/myagent",+ });+ const pending = setPendingAuthorization(undefined, {+ challenges: [grant("linear", "connection")],+ });+ const listTool = grant("list_issues__linear_myagent", "list-tool");+ const createTool = grant("create_issue__linear_myagent", "create-tool");+ const bobTool = grant("create_issue__linear_myagent", "bob-tool", bob);+ const approval = { ...grant("candidate-1:linear", "approval"), candidateId: "candidate-1" };++ expect(resolveActiveAuthorizationChallenges([listTool, createTool, bobTool, approval])).toEqual(+ [createTool, bobTool, approval],+ );+ expect(getSupersededAuthorizationChallenges(pending, [createTool])).toEqual([+ grant("linear", "connection"),+ ]);+ });+it("clears by exact attempt identity", () => {const state = setPendingAuthorization(undefined, {challenges: [challenge("linear", "linear-2"), challenge("github", "github-1")],diff --git a/packages/eve/src/harness/authorization.ts b/packages/eve/src/harness/authorization.tsindex 8a7444fbd..d10bc6f03 100644--- a/packages/eve/src/harness/authorization.ts+++ b/packages/eve/src/harness/authorization.ts@@ -37,7 +37,7 @@ import {import { loadContext } from "#context/container.js";import { ContextKey } from "#context/key.js";-import { SessionIdKey } from "#context/keys.js";+import { SessionIdKey, type SessionAuthContext } from "#context/keys.js";import { createWorkflowCallbackUrl } from "#execution/workflow-callback-url.js";import type { ConnectionAuthorizationChallenge } from "#connections/errors.js";import type { AuthorizationCallback, ConnectionPrincipal } from "#shared/connection-types.js";@@ -56,6 +56,12 @@ export interface AuthorizationChallenge {/** Opaque identity of this exact authorization attempt. */readonly attemptId?: string;readonly candidateId?: string;+ /**+ * Credential this sign-in grants, shared by every scope that uses it (the+ * Vercel Connect connector). Attempts for the same grant and principal are+ * one sign-in, so only one is shown.+ */+ readonly grant?: string;/** Opaque resolved connection identity; omitted for tool-hosted authorization. */readonly instanceId?: string;readonly name: string;@@ -65,6 +71,12 @@ export interface AuthorizationChallenge {readonly principal?: ConnectionPrincipal;/** Session principal that started this attempt; projected onto authorization events. */readonly principalId?: string;+ /**+ * Session identity that started this attempt. The callback carries no+ * identity, so the resumed turn runs as this caller. Never projected onto+ * events or model-facing output.+ */+ readonly requester?: SessionAuthContext;/*** Opaque resume value from the strategy's `startAuthorization`,* journaled across the park. Absent for provider-owned flows.@@ -311,37 +323,43 @@ export function setPendingAuthorization(};}-/** Keeps the last challenge for each authorization name and principal scope. */+/** Keeps the last challenge for each sign-in and principal. */export function resolveActiveAuthorizationChallenges(challenges: readonly AuthorizationChallenge[],): readonly AuthorizationChallenge[] {return challenges.filter((candidate, index) =>- !challenges- .slice(index + 1)- .some(- (replacement) =>- candidate.name === replacement.name &&- samePrincipal(candidate.principal, replacement.principal),- ),+ !challenges.slice(index + 1).some((replacement) => sameSignIn(candidate, replacement)),);}-/** Existing same-scope attempts replaced by newer attempts for the same principal. */+/** Existing attempts replaced by newer attempts for the same sign-in and principal. */export function getSupersededAuthorizationChallenges(sessionState: Record<string, unknown> | undefined,replacements: readonly AuthorizationChallenge[],): readonly AuthorizationChallenge[] {const previous = getPendingAuthorization(sessionState)?.challenges ?? [];return previous.filter((candidate) =>- replacements.some(- (replacement) =>- candidate.name === replacement.name &&- samePrincipal(candidate.principal, replacement.principal),- ),+ replacements.some((replacement) => sameSignIn(candidate, replacement)),);}+/**+ * Same scope, or the same grant from another scope, such as two tools and a+ * connection that all use one Vercel Connect connector. Approval sign-ins+ * stay per candidate because each one settles its own approval.+ */+function sameSignIn(+ left: Pick<AuthorizationChallenge, "candidateId" | "grant" | "name" | "principal">,+ right: Pick<AuthorizationChallenge, "candidateId" | "grant" | "name" | "principal">,+): boolean {+ const sameGrant =+ left.grant !== undefined &&+ left.grant === right.grant &&+ left.candidateId === right.candidateId;+ return (left.name === right.name || sameGrant) && samePrincipal(left.principal, right.principal);+}+function samePrincipal(left: ConnectionPrincipal | undefined,right: ConnectionPrincipal | undefined,diff --git a/packages/eve/src/harness/held-requests.ts b/packages/eve/src/harness/held-requests.tsnew file mode 100644index 000000000..c3fafb52c--- /dev/null+++ b/packages/eve/src/harness/held-requests.ts@@ -0,0 +1,57 @@+import { finishApprovalCandidate, getApprovalAuditState } from "#harness/approval-candidates.js";+import { authorizationEventFields } from "#harness/authorization-event-fields.js";+import {+ clearPendingAuthorization,+ getPendingAuthorization,+ type AuthorizationChallenge,+} from "#harness/authorization.js";+import type { HarnessEmissionState } from "#harness/emission-state.js";+import {+ createAuthorizationCompletedEvent,+ type AuthorizationCompletedStreamEvent,+} from "#protocol/message.js";++/**+ * Ends the sign-ins a held turn waits on when the person moves on, by steering+ * it with a message or cancelling it: the turn's own sign-ins, and the sign-ins+ * of responders still checking one of its approvals, whose candidates end as+ * stale. The approvals themselves resolve with the steering step or the cancel.+ */+export function withdrawHeldSignIns(+ state: Record<string, unknown> | undefined,+ input: { readonly completedAt: number; readonly reason: string },+): {+ readonly state: Record<string, unknown> | undefined;+ readonly withdrawn: readonly AuthorizationChallenge[];+} {+ const withdrawn = getPendingAuthorization(state)?.challenges ?? [];+ let next = withdrawn.length === 0 ? state : clearPendingAuthorization(state);+ for (const candidate of getApprovalAuditState(next).activeCandidates) {+ next = finishApprovalCandidate({+ candidateId: candidate.candidateId,+ completedAt: input.completedAt,+ reason: input.reason,+ state: next,+ status: "stale",+ });+ }+ return { state: next, withdrawn };+}++/** The `authorization.completed` events reporting withdrawn sign-ins as declined. */+export function declinedSignInEvents(+ withdrawn: readonly AuthorizationChallenge[],+ reason: string,+ emissionState: HarnessEmissionState,+): AuthorizationCompletedStreamEvent[] {+ return withdrawn.map((challenge) =>+ createAuthorizationCompletedEvent({+ ...authorizationEventFields(challenge),+ outcome: "declined",+ reason,+ sequence: emissionState.sequence,+ stepIndex: emissionState.stepIndex,+ turnId: emissionState.turnId,+ }),+ );+}diff --git a/packages/eve/src/harness/hitl/approval-input-requests.ts b/packages/eve/src/harness/hitl/approval-input-requests.tsindex d5fc6bedc..fd89cbbf3 100644--- a/packages/eve/src/harness/hitl/approval-input-requests.ts+++ b/packages/eve/src/harness/hitl/approval-input-requests.ts@@ -27,6 +27,7 @@ import type {ToolResponsePart,} from "#harness/hitl/pending-input-resolution.js";import type { HarnessSession } from "#harness/types.js";+import { validateHarnessModelMessages } from "#harness/messages.js";const APPROVED_TOOLS_KEY = "eve.runtime.hitl.approvedTools";const TOOL_EXECUTION_DENIED_CODE = "TOOL_EXECUTION_DENIED";@@ -67,17 +68,8 @@ export function resolveApprovalInputBatches(session: queueDeferredStepInput(input.session, compactStepInput(input.resolvedStepInput)),};}-- const session =- leftoverResponses.length === 0- ? input.session- : queueDeferredStepInput(input.session, { inputResponses: leftoverResponses });- return {- consumedMessage: input.resolvedStepInput.messageConsumed,- outcome: "continue",- messages: [...input.baseHistory],- session,- };+ // A message instead of an answer steers the held turn past its approval.+ return ignoreApprovalInputBatch(input);}const approval = resolveApprovalBatch({@@ -100,6 +92,75 @@ export function resolveApprovalInputBatches(});}+/**+ * Resolves the first open approval batch when the person steered the held turn+ * with a message instead of finishing it. Requests they already answered, as in+ * a partial approval, keep that answer; the rest report `ignored`. Later+ * batches stay open, as with answered batches.+ */+export function ignoreApprovalInputBatch(+ input: InputDomainResolverInput & {+ readonly resolveApprovalKey?: (request: InputRequest) => string | undefined;+ },+): ResolvePendingInputResult {+ const batch = input.batches[0]!;+ const answers = responsesForBatches(input.responses, [batch]);+ const approval = resolveApprovalBatch({+ batch,+ messages: [...input.baseHistory],+ resolveApprovalKey: input.resolveApprovalKey,+ responses: answers,+ session: input.session,+ });+ const resolved = buildResolvedInputBatch(batch, answers);+ return finishResolvedInput({+ // Calls that will not run already have their results, so the message joins+ // this step. An approved call runs through AI SDK, which needs its approval+ // response last, so the message replays after it.+ deferTurnInput: answers.some((answer) => resolveApprovalOutcome(answer).approved),+ leftoverResponses: responsesForBatches(input.responses, input.batches.slice(1)),+ messages: approval.messages,+ rejectedActions: approval.rejectedActions,+ resolvedInputs: resolved === undefined ? [] : [resolved],+ resolvedStepInput: input.resolvedStepInput,+ session: removePendingInputBatches(approval.session, [batch]),+ });+}++const CANCELLED_APPROVAL_REASON = "Cancelled before anyone answered.";++/**+ * Withdraws every pending tool approval when its turn is cancelled. Each held+ * call goes into history with a not-run result, so no call is left without one.+ */+export function cancelApprovalInputBatches(session: HarnessSession): HarnessSession {+ const batches = pendingApprovalBatches(session.state);+ if (batches.length === 0) return session;+ const messages: ModelMessage[] = [...session.history];+ for (const batch of batches) {+ appendResolvedBatchTranscript(+ messages,+ batch,+ buildApprovalBatchToolResponseParts(batch, [], CANCELLED_APPROVAL_REASON),+ );+ }+ return {+ ...removePendingInputBatches(session, batches),+ history: validateHarnessModelMessages(messages),+ };+}++/** Tool approvals still waiting for an answer, which cancelling their turn withdraws. */+export function getPendingApprovalRequests(+ state: HarnessSession["state"],+): readonly InputRequest[] {+ return pendingApprovalBatches(state).flatMap((batch) => batch.requests);+}++function pendingApprovalBatches(state: HarnessSession["state"]): readonly PendingInputBatch[] {+ return getPendingInputBatches(state).filter((batch) => batch.requests.every(isApprovalRequest));+}+/** Returns recorded approval keys that have no matching request still pending. */export function getApprovedTools(session: HarnessSession,@@ -206,6 +267,8 @@ function buildRejectedActionBatch(function buildApprovalBatchToolResponseParts(batch: PendingInputBatch,responses: readonly InputResponse[],+ /** Why an unanswered request did not run, when not because the user moved on. */+ unansweredReason?: string,): ToolResponsePart[] {const responseMap = new Map(responses.map((response) => [response.requestId, response]));const parts: ToolResponsePart[] = [];@@ -214,7 +277,11 @@ function buildApprovalBatchToolResponseParts(switch (request.kind) {case "tool-approval":parts.push(- ...buildApprovalToolResponseParts(request as ToolApprovalInputRequest, response),+ ...buildApprovalToolResponseParts(+ request as ToolApprovalInputRequest,+ response,+ unansweredReason,+ ),);break;case "question":@@ -234,8 +301,11 @@ function buildApprovalBatchToolResponseParts(function buildApprovalToolResponseParts(request: ToolApprovalInputRequest,response: InputResponse | undefined,+ unansweredReason: string | undefined,): ToolResponsePart[] {- const { approved, reason } = resolveApprovalOutcome(response);+ const outcome = resolveApprovalOutcome(response);+ const approved = outcome.approved;+ const reason = response === undefined ? (unansweredReason ?? outcome.reason) : outcome.reason;const parts: ToolResponsePart[] = [{ approvalId: request.requestId, approved, reason, type: "tool-approval-response" },];diff --git a/packages/eve/src/harness/input-requests.test.ts b/packages/eve/src/harness/input-requests.test.tsindex 9e552de97..b312d0d63 100644--- a/packages/eve/src/harness/input-requests.test.ts+++ b/packages/eve/src/harness/input-requests.test.ts@@ -617,7 +617,7 @@ describe("resolvePendingInput", () => {]);});- it("keeps a pending approval open while an unrelated follow-up message continues", () => {+ it("steers past a pending approval when a follow-up message arrives instead of an answer", () => {const session = appendPendingInputBatch({event: { sequence: 7, stepIndex: 2, turnId: "turn_1" },requests: [@@ -648,12 +648,15 @@ describe("resolvePendingInput", () => {session,});- // The message runs as an ordinary turn; the approval stays answerable.- expect(result.outcome).toBe("continue");- expect(result.rejectedActions).toBeUndefined();- expect(result.messages).toEqual([{ content: "previous", kind: "user", role: "user" }]);+ // The message moves the turn on: the call never runs, and the model reads+ // the message in the same step.+ expect(result.outcome).toBe("resolved");+ expect(result.resolvedInputs).toMatchObject([+ { inputs: [{ outcome: "ignored", request: { requestId: "approval-1" } }] },+ ]);+ expect(result.rejectedActions?.[0]?.results).toMatchObject([{ callId: "approval-call" }]);expect(getDeferredStepInput(result.session)).toBeUndefined();- expect(getPendingInputRequestIds(result.session.state)).toEqual(new Set(["approval-1"]));+ expect(getPendingInputRequestIds(result.session.state)).toEqual(new Set());});it("preserves context-only input while a pending batch stays open", () => {@@ -1045,43 +1048,7 @@ describe("pending input batch collection", () => {expect(getPendingInputRequestIds(second.session.state)).toEqual(new Set(["approval-2"]));});- it("leaves every batch open when a message arrives with several batches pending", () => {- let session = appendPendingInputBatch({- requests: [approvalRequest("approval-1", "call-1")],- responseMessages: [batchOutput("call-1", "bash")],- session: createHarnessSession(),- });- session = appendPendingInputBatch({- requests: [approvalRequest("approval-2", "call-2")],- responseMessages: [batchOutput("call-2", "bash")],- session,- });-- const result = resolvePendingInput({ session, stepInput: { message: "keep going" } });-- expect(result.outcome).toBe("continue");- expect(result.messages).toEqual([{ content: "previous", kind: "user", role: "user" }]);- expect(getDeferredStepInput(result.session)).toBeUndefined();- expect(getPendingInputRequestIds(result.session.state)).toEqual(- new Set(["approval-1", "approval-2"]),- );- });-- it("continues an internal step past HITL emitted by an older turn", () => {- const session = appendPendingInputBatch({- event: { sequence: 5, stepIndex: 1, turnId: "turn_0" },- requests: [approvalRequest("approval-1", "call-1")],- responseMessages: [batchOutput("call-1", "bash")],- session: createHarnessSession(),- });-- const result = resolvePendingInput({ activeTurnId: "turn_1", internalStep: true, session });-- expect(result.outcome).toBe("continue");- expect(getPendingInputRequestIds(result.session.state)).toEqual(new Set(["approval-1"]));- });-- it("parks when the current turn emitted the pending HITL", () => {+ it("holds on a pending approval when the step brings no answer or message", () => {const session = appendPendingInputBatch({event: { sequence: 5, stepIndex: 1, turnId: "turn_1" },requests: [approvalRequest("approval-1", "call-1")],@@ -1089,7 +1056,7 @@ describe("pending input batch collection", () => {session: createHarnessSession(),});- const result = resolvePendingInput({ activeTurnId: "turn_1", internalStep: true, session });+ const result = resolvePendingInput({ session });expect(result.outcome).toBe("unresolved");expect(getPendingInputRequestIds(result.session.state)).toEqual(new Set(["approval-1"]));@@ -1293,13 +1260,8 @@ describe("clearPendingSessionLimitPrompt", () => {});const kept = clearPendingSessionLimitPrompt(session);- const result = resolvePendingInput({ session: kept, stepInput: { message: "and then this" } });- // The approval batch survives the limit-prompt sweep and stays- // answerable; the follow-up message continues as an ordinary turn.- expect(getPendingInputRequestIds(result.session.state)).toEqual(new Set(["approval-1"]));- expect(result.outcome).toBe("continue");- expect(getDeferredStepInput(result.session)).toBeUndefined();+ expect(getPendingInputRequestIds(kept.state)).toEqual(new Set(["approval-1"]));});it("is a no-op without a pending batch", () => {diff --git a/packages/eve/src/harness/input-requests.ts b/packages/eve/src/harness/input-requests.tsindex 3e3f9a156..1838752dc 100644--- a/packages/eve/src/harness/input-requests.ts+++ b/packages/eve/src/harness/input-requests.ts@@ -105,10 +105,6 @@ export function selectApprovalReplayBatch(* requirement.*/export function resolvePendingInput(input: {- /** The turn currently advancing through the harness tool loop. */- readonly activeTurnId?: string;- /** True while the harness has an open turn to continue. */- readonly internalStep?: boolean;readonly history?: readonly ModelMessage[];readonly resolveApprovalKey?: (request: InputRequest) => string | undefined;readonly session: HarnessSession;@@ -140,26 +136,6 @@ export function resolvePendingInput(input: {: resolveTextMessageInput(textResolutionBatch, input.stepInput);const responses = canonicalizeInputResponses(resolvedStepInput?.inputResponses ?? []);- if (- input.internalStep === true &&- canContinuePastHistoricalInput({- activeTurnId: input.activeTurnId,- batches,- responses,- route,- }) &&- resolvedStepInput?.message === undefined- ) {- return {- outcome: "continue",- messages: baseHistory,- session:- resolvedStepInput === undefined- ? input.session- : queueDeferredStepInput(input.session, compactStepInput(resolvedStepInput)),- };- }-if (responses.length === 0 && resolvedStepInput?.message === undefined) {const deferredInput = compactStepInput(resolvedStepInput);const session =@@ -190,29 +166,6 @@ export function resolvePendingInput(input: {}}-/**- * An internal tool-loop step must not be parked by input emitted by an older- * turn. The current turn can still park on its own HITL request; session-limit- * prompts remain a harness gate regardless of the turn that created them.- */-function canContinuePastHistoricalInput(input: {- readonly activeTurnId?: string;- readonly batches: readonly PendingInputBatch[];- readonly responses: readonly InputResponse[];- readonly route: PendingInputRoute;-}): boolean {- if (input.activeTurnId === undefined || input.route.kind === "session-limit") return false;- if (- input.responses.length > 0 &&- findAnsweredApprovalBatches(input.batches, input.responses).length > 0- ) {- return false;- }- return input.batches.every(- (batch) => batch.event !== undefined && batch.event.turnId !== input.activeTurnId,- );-}-type PendingInputRoute =| { readonly batch: PendingInputBatch; readonly kind: "session-limit" }| { readonly kind: "approvals" };diff --git a/packages/eve/src/harness/issue-3494-adversarial.integration.test.ts b/packages/eve/src/harness/issue-3494-adversarial.integration.test.tsindex e17f3f154..2bd2cdae1 100644--- a/packages/eve/src/harness/issue-3494-adversarial.integration.test.ts+++ b/packages/eve/src/harness/issue-3494-adversarial.integration.test.ts@@ -235,7 +235,8 @@ function fixture(pending: () => getPendingInputBatches(session.state).flatMap((b) => b.requests),async gate(...names: string[]) {script.push(calls(...names));- await drive({ message: `Prepare ${names.join(" and ")}.` });+ const parked = await drive({ message: `Prepare ${names.join(" and ")}.` });+ expect(parked.held).toEqual({ kind: "request" });expect(this.pending().filter((r) => r.kind === "tool-approval")).toHaveLength(names.length);expect(executions).toHaveLength(0);},@@ -259,85 +260,12 @@ function fixture(};}-for (const variant of [- "read",- "write",- "fail",- "parallel",- "invalid",- "response-authorized",-] as const) {- it(`finishes unrelated ${variant} tool turn while an approval stays open`, async () => {- const logs = captureLogRecords();- const f = fixture(variant, variant === "response-authorized");- await f.gate("gateA");- f.script.push(- variant === "parallel"- ? calls("read", "write")- : variant === "invalid"- ? [{ toolName: "read", input: { n: "invalid" } }]- : calls(variant === "response-authorized" ? "read" : variant),- "FINAL",- );- const result = await f.drive({ message: "Do the unrelated work." });- expect(f.pending()).toHaveLength(1);- expect(f.executions).not.toContain("gateA");- const expectedExecutions = {- read: ["read"],- write: ["write"],- fail: ["fail"],- parallel: ["read", "write"],- invalid: [],- "response-authorized": ["read"],- }[variant];- expect([...f.executions].sort()).toEqual(expectedExecutions.sort());- expect(result.settledTurn?.output).toBe("FINAL");- expect(- logs.records.filter((record) => record.message === "tool execution failed"),- ).toHaveLength(variant === "fail" ? 1 : 0);- });-}--for (const variant of ["approve", "cancel"] as const) {- it(`continues after ${variant} of one independent approval while another remains`, async () => {- const f = fixture(`sibling-${variant}`);- await f.gate("gateA");- f.script.push(calls("gateB"));- await f.drive({ message: "Also prepare B." });- expect(f.pending()).toHaveLength(2);- f.script.push(calls("read"), "FINAL");- const result = await f.drive(f.respond("gateB", variant));- expect(f.pending().map((r) => r.action.toolName)).toEqual(["gateA"]);- expect(f.executions.filter((x) => x === "gateB")).toHaveLength(variant === "approve" ? 1 : 0);- expect(result.settledTurn?.output).toBe("FINAL");- });-}--it("interprets a completed workflow result while an earlier approval remains", async () => {- const f = fixture("workflow");- await f.gate("gateA");- f.script.push(calls("workflow"), "FINAL");- await f.drive({ message: "Run unrelated workflow." });- const result = await f.finishRuntime();- expect(JSON.stringify(result.session.history)).toContain("runtime-RESULT");- expect(f.pending()).toHaveLength(1);- expect(result.settledTurn?.output).toBe("FINAL");-});-it("completes a plain tool turn without any pending input [control]", async () => {const f = fixture("control-no-pending");f.script.push(calls("read"), "FINAL");expect((await f.drive({ message: "Read the status." })).settledTurn?.output).toBe("FINAL");});-it("completes a text-only follow-up with a pending approval [control]", async () => {- const f = fixture("control-text");- await f.gate("gateA");- f.script.push("FINAL");- expect((await f.drive({ message: "Explain the change." })).settledTurn?.output).toBe("FINAL");- expect(f.pending()).toHaveLength(1);-});-it("finishes after resolving the only approval [control]", async () => {const f = fixture("control-resolve-all");await f.gate("gateA");@@ -364,19 +292,6 @@ it("accumulates approvals from one batch across separate deliveries [control]",expect(f.executions.filter((x) => x.startsWith("gate"))).toHaveLength(2);});-it("finishes an unrelated tool turn carrying a partial approval response", async () => {- const f = fixture("partial-response-with-followup");- await f.gate("gateA", "gateB");- f.script.push(calls("read"), "FINAL");- const result = await f.drive({- ...f.respond("gateA"),- message: "While B waits, read the status.",- });- expect(f.executions).toEqual(["read"]);- expect(f.pending()).toHaveLength(2);- expect(result.settledTurn?.output).toBe("FINAL");-});-it("finishes after a stale approval response becomes a follow-up message", async () => {const f = fixture("stale-response");await f.gate("gateA");@@ -391,16 +306,26 @@ it("finishes after a stale approval response becomes a follow-up message", asyncexpect(result.settledTurn?.output).toBe("FINAL");});-it("reaches the next budget prompt after a grant while an earlier approval remains", async () => {- const f = fixture("session-limit-grant", false, 1);- await f.gate("gateA");- await f.drive({ message: "Read the status." });- expect(f.pending().map((r) => r.kind)).toContain("session-limit");- f.script.push(calls("read"));- const result = await f.drive(f.respond("session_limit_continuation", "continue"));+it("runs the steering message after a budget grant that followed the cancelled approval", async () => {+ const f = fixture("steer-then-session-limit", false, 1);+ f.script.push(calls("gateA"));+ expect((await f.drive({ message: "Prepare gateA." })).held).toEqual({ kind: "request" });+ await f.drive({ message: "Read the status instead." });+ expect(f.pending().map((r) => r.kind)).toEqual(["session-limit"]);+ f.script.push(calls("read"), "FINAL");+ await f.drive(f.respond("session_limit_continuation", "continue"));+ // The message joined the step that cancelled the approval, so it reaches the+ // model once, without an extra model call spent before it.+ const userTexts = f.session.history+ .filter((message) => message.role === "user")+ .map((message) => JSON.stringify(message.content));+ expect(userTexts.filter((text) => text.includes("Read the status instead."))).toHaveLength(1);+ expect(+ f.events+ .filter((event) => event.type === "message.received")+ .map((event) => event.data.message),+ ).toEqual(["Prepare gateA.", "Read the status instead."]);expect(f.executions).toEqual(["read"]);- expect(result.next).toBeNull();- expect(f.pending().map((r) => r.kind)).toEqual(["tool-approval", "session-limit"]);});for (const variant of ["fail", "invalid"]) {@@ -419,21 +344,6 @@ for (const variant of ["fail", "invalid"]) {});}-it("settles both independent approval batches and a deferred follow-up [control]", async () => {- const f = fixture("control-resolve-multiple");- await f.gate("gateA");- f.script.push(calls("gateB"));- await f.drive({ message: "Also prepare B." });- f.script.push("First approved.", "Second approved.", calls("read"), "FINAL");- const result = await f.drive({- inputResponses: [...f.respond("gateA").inputResponses!, ...f.respond("gateB").inputResponses!],- message: "Then read the status.",- });- expect(f.executions.filter((x) => x.startsWith("gate"))).toHaveLength(2);- expect(f.pending()).toHaveLength(0);- expect(result.settledTurn?.output).toBe("FINAL");-});-it("keeps same-turn approval blocked when its parallel workflow finishes [control]", async () => {const f = fixture("control-same-turn-workflow-and-approval");f.script.push(calls("gateA", "workflow"));@@ -448,17 +358,27 @@ it("keeps same-turn approval blocked when its parallel workflow finishes [controexpect(f.executions).toEqual(["gateA"]);});-it("settles a text-only follow-up carrying a partial approval response", async () => {- const f = fixture("partial-response-with-text");- await f.gate("gateA", "gateB");- f.script.push("FINAL");- const result = await f.drive({- ...f.respond("gateA"),- message: "Explain the change while B waits.",- });- expect(f.executions).toHaveLength(0);- expect(f.events.some((e) => e.type === "message.completed")).toBe(true);- expect(result.settledTurn?.output).toBe("FINAL");+it("keeps a message waiting behind a budget prompt and runs it after the grant", async () => {+ const f = fixture("message-behind-session-limit", false, 1);+ f.script.push("Initial text.");+ await f.drive({ message: "Say hello." });+ await f.drive({ message: "Read the status." });+ expect(f.pending().map((r) => r.kind)).toEqual(["session-limit"]);++ const waitingStart = f.events.length;+ expect((await f.drive({ message: "Also say goodbye." })).held).toEqual({ kind: "request" });+ // The prompt still holds the turn; the message is announced when it runs.+ expect(f.events.slice(waitingStart).map((event) => event.type)).toEqual(["turn.waiting"]);+ expect(f.pending().map((r) => r.kind)).toEqual(["session-limit"]);++ f.script.push(calls("read"), "FINAL");+ await f.drive(f.respond("session_limit_continuation", "continue"));+ expect(f.executions).toEqual(["read"]);+ expect(+ f.events+ .filter((event) => event.type === "message.received")+ .map((event) => event.data.message),+ ).toEqual(["Say hello.", "Read the status.", "Also say goodbye."]);});it("reaches the next budget prompt after a grant without an older approval [control]", async () => {@@ -489,75 +409,56 @@ for (const pending of [true, false]) {});}-it("settles final_output alongside an ordinary tool with older approval [control]", async () => {- const f = fixture("control-final-output", false, undefined, {- type: "object",- properties: { status: { type: "string" } },- required: ["status"],- additionalProperties: false,- });- await f.gate("gateA");- f.script.push([{ toolName: "read" }, { toolName: "final_output", input: { status: "ready" } }]);- const result = await f.drive({- message: "Read the status and return it.",- });- expect(result.settledTurn?.output).toEqual({ status: "ready" });- expect(f.executions).toEqual(["read"]);- expect(f.pending()).toHaveLength(1);-});--it("continues after responder-authorized approval with an older approval still open", async () => {- const f = fixture("authorized-sibling", true);- await f.gate("gateA");- f.script.push(calls("gateB"));- await f.drive({ message: "Also prepare B." });- f.script.push(calls("read"), "FINAL");- const result = await f.drive({- attributedInputResponses: f.respond("gateB").inputResponses!.map((response) => ({- response,- auth: {- attributes: {},- authenticator: "test",- issuer: "test",- principalId: "user-1",- principalType: "user" as const,- },- })),- });- expect(f.executions).toEqual(["gateB", "read"]);- expect(f.pending().map((r) => r.action.toolName)).toEqual(["gateA"]);+it("cancels a held approval when the same person steers the turn", async () => {+ const f = fixture("steer-cancels-held-approval");+ f.script.push(calls("gateA"));+ const parked = await f.drive({ message: "Prepare gateA." });+ expect(parked.held).toEqual({ kind: "request" });+ f.script.push("FINAL", "FINAL");+ const result = await f.drive({ message: "Never mind, skip it." });+ expect(f.pending()).toHaveLength(0);+ expect(f.executions).toEqual([]);+ expect(f.events.filter((event) => event.type === "input.resolved")).toMatchObject([+ { data: { resolutions: [{ kind: "tool-approval", outcome: "ignored" }] } },+ ]);+ expect(f.events.filter((event) => event.type === "turn.started")).toHaveLength(1);+ // The steering message replays after the cancelled call's result, and is announced once.+ expect(+ f.events+ .filter((event) => event.type === "message.received")+ .map((event) => event.data.message),+ ).toEqual(["Prepare gateA.", "Never mind, skip it."]);expect(result.settledTurn?.output).toBe("FINAL");});-it("continues after responder-authorized approval of the older batch", async () => {- const f = fixture("authorized-older-sibling", true);- await f.gate("gateA");- f.script.push(calls("gateB"));- await f.drive({ message: "Also prepare B." });+it("runs an approved call and cancels the rest when a partial approval is steered", async () => {+ const f = fixture("steer-partial-approval");+ f.script.push(calls("gateA", "gateB"));+ const parked = await f.drive({ message: "Prepare gateA and gateB." });+ expect(parked.held).toEqual({ kind: "request" });+ const partialStart = f.events.length;+ const partial = await f.drive(f.respond("gateA"));+ // The partial answer leaves gateB open, so the turn stays held on it.+ expect(partial.held).toEqual({ kind: "request" });+ expect(f.events.slice(partialStart).map((event) => event.type)).toEqual(["turn.waiting"]);+ expect(f.executions).toEqual([]);expect(f.pending().map((request) => request.action.toolName)).toEqual(["gateA", "gateB"]);- f.script.push(calls("read"), "FINAL");- const response = f.respond("gateA").inputResponses![0]!;- const result = await f.drive({- attributedInputResponses: [- {- response,- auth: {- attributes: {},- authenticator: "test",- issuer: "test",- principalId: "user-1",- principalType: "user",- },- },- ],- });- expect(f.executions).toEqual(["gateA", "read"]);- expect(f.pending().map((request) => request.action.toolName)).toEqual(["gateB"]);++ f.script.push("FINAL", "FINAL");+ const result = await f.drive({ message: "Skip gateB for now." });+ expect(f.executions).toEqual(["gateA"]);+ expect(f.pending()).toEqual([]);+ expect(+ f.events+ .filter((event) => event.type === "input.resolved")+ .flatMap((event) => event.data.resolutions)+ .map((resolution) => resolution.outcome),+ ).toEqual(["approved", "ignored"]);expect(result.settledTurn?.output).toBe("FINAL");});it.each(["rejected", "failed", "timed-out"] as const)(- "finishes a %s response attempt without starting a turn and permits retry",+ "keeps holding the turn after a %s response and resumes it on retry",async (outcome) => {let allowed = false;const policy = vi.fn(() => {@@ -588,14 +489,16 @@ it.each(["rejected", "failed", "timed-out"] as const)(outcome === "timed-out"? vi.spyOn(Date, "now").mockReturnValue(Date.now() + 600_001): undefined;+ let refused: StepResult;try {- await f.drive();+ refused = await f.drive();} finally {clock?.mockRestore();}+ // The turn stays held, and `turn.waiting` gives the responder's send a boundary.const events = f.events.slice(start);- expect(events.at(-1)?.type).toBe("session.waiting");- expect(events.filter((event) => event.type === "session.waiting")).toHaveLength(1);+ expect(refused.held).toEqual({ kind: "request" });+ expect(events.at(-1)).toMatchObject({ data: { on: "input" }, type: "turn.waiting" });expect(events.some((event) => event.type === "turn.started")).toBe(false);expect(getApprovalAuditState(f.session.state).candidateHistory.at(-1)?.status).toBe(outcome);expect(f.pending()).toHaveLength(1);@@ -603,17 +506,15 @@ it.each(["rejected", "failed", "timed-out"] as const)(allowed = true;f.script.push("Bob approved the task.");- const retryStart = f.events.length;- await f.drive(input);+ const resumed = await f.drive(input);expect(f.pending()).toEqual([]);expect(f.executions).toEqual(["gateA"]);- expect(- f.events.slice(retryStart).filter((event) => event.type === "session.waiting"),- ).toHaveLength(1);+ expect(resumed.settledTurn?.output).toBe("Bob approved the task.");+ expect(f.events.filter((event) => event.type === "turn.started")).toHaveLength(1);},);-it("does not announce waiting while a candidate needs sign-in, then completes on expiry", async () => {+it("keeps the turn held while a responder signs in, and fails the sign-in on expiry", async () => {const policy = vi.fn(() => ({ status: "allowed" as const }));const f = fixture("candidate-sign-in", policy);await f.gate("gateA");@@ -650,19 +551,20 @@ it("does not announce waiting while a candidate needs sign-in, then completes on),}));const start = f.events.length;- await f.drive();- expect(f.events.slice(start)).toEqual([]);+ expect((await f.drive()).held).toEqual({ kind: "request" });+ expect(f.events.slice(start).map((event) => event.type)).toEqual(["turn.waiting"]);expect(policy).not.toHaveBeenCalled();+ const expiry = f.events.length;const clock = vi.spyOn(Date, "now").mockReturnValue(candidate.expiresAt + 1);try {await f.drive();} finally {clock.mockRestore();}- expect(f.events.slice(start).map((event) => event.type)).toEqual([- "authorization.completed",- "approval.candidate",- "session.waiting",+ expect(f.events.slice(expiry)).toMatchObject([+ { data: { outcome: "failed" }, type: "authorization.completed" },+ { data: { outcome: "timed-out" }, type: "approval.candidate" },+ { data: { on: "input" }, type: "turn.waiting" },]);expect(f.pending()).toHaveLength(1);});@@ -707,62 +609,3 @@ it("announces a candidate's sign-in with the attempt its completion will name",data: { attemptId: "attempt_alice", candidateId: candidate.candidateId, name: "notes" },});});--it("uses the normal turn boundary for a mixed accepted and refused delivery", async () => {- const f = fixture("mixed-response", ({ request }) =>- request.toolName === "gateA"- ? { status: "allowed" }- : { status: "rejected", reason: "Bob must approve this task." },- );- await f.gate("gateA");- f.script.push(calls("gateB"));- await f.drive({ message: "Prepare Bob's independent task." });- const start = f.events.length;- f.script.push("Alice's task is complete.");- await f.drive({- attributedInputResponses: f.pending().map((request) => ({- response: { requestId: request.requestId, optionId: "approve" },- auth: {- attributes: {},- authenticator: "test",- issuer: "test",- principalId: "alice",- principalType: "user" as const,- },- })),- });- expect(f.executions).toEqual(["gateA"]);- expect(f.pending().map((request) => request.action.toolName)).toEqual(["gateB"]);- expect(f.events.slice(start).filter((event) => event.type === "session.waiting")).toHaveLength(1);-});--it("resumes a complete batch while another batch has only a partial approval [control]", async () => {- const f = fixture("complete-and-partial-approvals");- // Given A and B need approval together, and a second independent B is pending.- await f.gate("gateA", "gateB");- const approvalA = f.respond("gateA");- const approvalB = f.respond("gateB");- f.script.push(calls("gateB"));- await f.drive({ message: "Prepare another independent B." });- const independentB = f.pending().at(-1)!;- const independentTurn = getPendingInputBatches(f.session.state)[1]!.event!.turnId;-- // When one delivery answers A and the independent B.- f.script.push("Independent B approved.");- const result = await f.drive({- inputResponses: [- ...approvalA.inputResponses!,- { requestId: independentB.requestId, optionId: "approve" },- ],- });-- // Then only the complete batch executes and replies, without another model call.- expect(result.settledTurn?.output).toBe("Independent B approved.");- expect(f.restoredTurns).toEqual([independentTurn]);- expect(f.executions).toEqual(["gateB"]);- expect(f.pending()).toHaveLength(2);- f.script.push("Both approved.");- await f.drive(approvalB);- expect(f.executions).toEqual(["gateB", "gateA", "gateB"]);- expect(f.pending()).toHaveLength(0);-});diff --git a/packages/eve/src/harness/issue-3494-diagnosis.integration.test.ts b/packages/eve/src/harness/issue-3494-diagnosis.integration.test.tsdeleted file mode 100644index 09ec9e76e..000000000--- a/packages/eve/src/harness/issue-3494-diagnosis.integration.test.ts+++ /dev/null@@ -1,191 +0,0 @@-import { writeFileSync } from "node:fs";-import { join } from "node:path";-import { tmpdir } from "node:os";-import { jsonSchema, simulateReadableStream } from "ai";-import { MockLanguageModelV4 } from "ai/test";-import { expect, it, vi } from "vitest";-import { ContextContainer, contextStorage } from "#context/container.js";-import { SessionKey } from "#context/keys.js";-import { getHarnessEmissionState } from "#harness/emission.js";-import type { HarnessToolDefinition } from "#harness/execute-tool.js";-import { resolvePendingInput } from "#harness/input-requests.js";-import {- getPendingInputBatches,- removePendingInputBatches,-} from "#harness/pending-input-batches.js";-import { createToolLoopHarness } from "#harness/tool-loop.js";-import type { HarnessSession, StepInput, ToolLoopHarnessConfig } from "#harness/types.js";-import type { UnstampedMessageStreamEvent } from "#protocol/message.js";-import { always } from "#tools/approval/policies.js";--// The harness runs outside a workflow body here, where run attributes cannot-// be written; the attribute contract is covered by emit.test.ts.-vi.mock("#runtime/attributes/emit.js", () => ({ setEveAttributes: vi.fn(async () => {}) }));--it("continues the second step past an older approval batch", async () => {- const events: UnstampedMessageStreamEvent[] = [];- const gate = vi.fn(async () => ({ changed: true }));- const read = vi.fn(async () => ({ status: "ready" }));- let modelCalls = 0;- const model = new MockLanguageModelV4({- doStream: async () => {- const call = modelCalls++;- return {- stream: simulateReadableStream({- initialDelayInMs: null,- chunkDelayInMs: null,- chunks: [- { type: "stream-start", warnings: [] },- ...(call < 2- ? [- {- type: "tool-call" as const,- toolCallId: `call-${call}`,- toolName: call === 0 ? "gate" : "read",- input: "{}",- },- ]- : [- { type: "text-start" as const, id: "reply" },- { type: "text-delta" as const, id: "reply", delta: "Your draft is ready." },- { type: "text-end" as const, id: "reply" },- ]),- {- type: "finish",- finishReason: { unified: call < 2 ? "tool-calls" : "stop", raw: "stop" },- usage: {- inputTokens: { total: 1, noCache: 1, cacheRead: 0, cacheWrite: 0 },- outputTokens: { total: 1, text: 1, reasoning: 0 },- },- },- ],- }),- };- },- });- const config: ToolLoopHarnessConfig = {- tools: new Map<string, HarnessToolDefinition>([- [- "gate",- {- name: "gate",- description: "Change account",- inputSchema: jsonSchema({ type: "object" }),- approval: always(),- execute: gate,- },- ],- [- "read",- {- name: "read",- description: "Read draft status",- inputSchema: jsonSchema({ type: "object" }),- execute: read,- },- ],- ]),- resolveModel: async () => model,- handleEvent: async (event) => {- events.push(event);- },- };- const initial: HarnessSession = {- agent: { system: "Test", tools: [], modelReference: { id: "test/model" } },- compaction: { recentWindowSize: 10, threshold: 100_000 },- continuationToken: "diagnosis",- sessionId: "diagnosis-3494",- history: [],- };- const run = async (session: HarnessSession, input?: StepInput) => {- const state = getHarnessEmissionState(session.state);- const ctx = new ContextContainer();- ctx.set(SessionKey, {- sessionId: session.sessionId,- auth: { current: null, initiator: null },- turn: { id: state.turnId || `turn_${state.sequence}`, sequence: state.sequence },- });- return contextStorage.run(ctx, () => createToolLoopHarness(config)(session, input));- };-- const parked = await run(initial, { message: "Prepare the account change." });- expect(parked.next).toBeNull();- expect(gate).not.toHaveBeenCalled();- const [approval] = getPendingInputBatches(parked.session.state);- expect(approval?.event?.turnId).toBe("turn_0");- expect(approval?.requests[0]?.kind).toBe("tool-approval");-- const first = await run(parked.session, { message: "What is the draft status?" });- expect(typeof first.next).toBe("function");- expect(read).toHaveBeenCalledTimes(1);- expect(modelCalls).toBe(2);- expect(getHarnessEmissionState(first.session.state)).toMatchObject({- turnId: "turn_1",- stepIndex: 1,- });- expect(first.session.history.at(-1)?.role).toBe("tool");- const before = events.length;-- const pendingDecision = resolvePendingInput({ session: first.session });- expect(pendingDecision.outcome).toBe("unresolved");- const continued = await run(first.session);- expect(continued.settledTurn?.output).toBe("Your draft is ready.");- expect(continued.next).toBeNull();- expect(modelCalls).toBe(3);- expect(events.slice(before).some((event) => event.type === "turn.completed")).toBe(true);- expect(getPendingInputBatches(continued.session.state)).toEqual([approval]);- const continuedObservation = {- phase: "continued",- modelCalls,- emission: getHarnessEmissionState(continued.session.state),- pendingOwner: approval?.event,- emittedBySecondStep: events.slice(before).map((event) => event.type),- firstTurnEvents: events- .filter(- (event) =>- "data" in event &&- event.data !== undefined &&- "turnId" in event.data &&- event.data.turnId === "turn_1",- )- .map((event) => event.type),- };-- // Remove the batch only from a cloned state to isolate causality; this is not a fix.- const control = removePendingInputBatches(first.session, [approval!]);- expect(control.history).toBe(first.session.history);- expect(getHarnessEmissionState(control.state)).toEqual(- getHarnessEmissionState(first.session.state),- );- expect(resolvePendingInput({ session: control }).outcome).toBe("continue");- const beforeControl = events.length;- await run(control);- expect(modelCalls).toBe(4);- expect(events.slice(beforeControl).some((event) => event.type === "turn.completed")).toBe(true);- expect(gate).not.toHaveBeenCalled();- expect(getPendingInputBatches(first.session.state)).toEqual([approval]);- const controlObservation = {- phase: "clone-without-batch",- modelCalls,- events: events.slice(beforeControl).map((event) => event.type),- originalStillPending: getPendingInputBatches(first.session.state).length,- gateExecutions: gate.mock.calls.length,- };-- const beforeApproval = events.length;- const approved = await run(continued.session, {- inputResponses: [{ requestId: approval!.requests[0]!.requestId, optionId: "approve" }],- });- expect(gate).toHaveBeenCalledTimes(1);- expect(getPendingInputBatches(approved.session.state)).toHaveLength(0);- const approvalObservation = {- phase: "approve-original-session",- gateExecutions: gate.mock.calls.length,- pending: getPendingInputBatches(approved.session.state).length,- events: events.slice(beforeApproval).map((event) => event.type),- };- writeFileSync(- join(tmpdir(), "eve-3494-diagnosis.json"),- JSON.stringify({ continuedObservation, controlObservation, approvalObservation }, null, 2),- );-});diff --git a/packages/eve/src/harness/tool-loop-steering.integration.test.ts b/packages/eve/src/harness/tool-loop-steering.integration.test.tsindex 16d0f9ad2..272fd351e 100644--- a/packages/eve/src/harness/tool-loop-steering.integration.test.ts+++ b/packages/eve/src/harness/tool-loop-steering.integration.test.ts@@ -91,7 +91,7 @@ describe("generation steering with the real AI SDK", () => {}});- it.each(["input.requested", "turn.completed", "step.failed"] as const)(+ it.each(["input.requested", "turn.waiting", "step.failed"] as const)("finishes committing %s when a correction arrives during publication",async (boundary) => {const logs = captureLogRecords();@@ -153,7 +153,14 @@ describe("generation steering with the real AI SDK", () => {expect(result.steered).toBeUndefined();expect(result.next).toBeNull();expect(events.filter((event) => event.type === boundary)).toHaveLength(1);- expect(events.filter((event) => event.type === "session.waiting")).toHaveLength(1);+ // A failed step ends the turn; an approval holds it open.+ const endsTurn = boundary === "step.failed";+ expect(events.filter((event) => event.type === "session.waiting")).toHaveLength(+ endsTurn ? 1 : 0,+ );+ expect(events.filter((event) => event.type === "turn.waiting")).toHaveLength(+ endsTurn ? 0 : 1,+ );expect(events.filter((event) => event.type === "message.appended")).toHaveLength(0);const parked = logs.records.filter((record) => record.message === "model call failed — parking session for retry by the user",diff --git a/packages/eve/src/harness/tool-loop.test.ts b/packages/eve/src/harness/tool-loop.test.tsindex 55b5992ce..15838c793 100644--- a/packages/eve/src/harness/tool-loop.test.ts+++ b/packages/eve/src/harness/tool-loop.test.ts@@ -12,9 +12,6 @@ import { afterEach, describe, expect, it, vi } from "vitest";import { ContextContainer, contextStorage } from "#context/container.js";import { DynamicModelSelectionError } from "#context/dynamic-model-lifecycle.js";import { dispatchDynamicInstructionEvent } from "#context/dynamic-instruction-lifecycle.js";-import { dispatchDynamicSkillEvent } from "#context/dynamic-skill-lifecycle.js";-import { dispatchMemoryLifecycleEvent } from "#context/memory-event-lifecycle.js";-import { defineMemory } from "#public/memory/index.js";import {AuthKey,ChannelInstrumentationKey,@@ -64,12 +61,8 @@ import {modelFacingAuthorizationOutput,requestAuthorization,} from "#harness/authorization.js";-import {- getPendingInputRequestIds,- hasPendingInputBatch,- appendPendingInputBatch,-} from "#harness/input-requests.js";-import { getDeferredStepInput, getPendingInputBatches } from "#harness/pending-input-batches.js";+import { hasPendingInputBatch, appendPendingInputBatch } from "#harness/input-requests.js";+import { getPendingInputBatches } from "#harness/pending-input-batches.js";import { getPendingCoordinationBatch, pendingCoordinationCallIds } from "#harness/coordination.js";import { PendingSkillAnnouncementKey } from "#context/dynamic-skill-lifecycle.js";import { deserializeContext, serializeContext } from "#context/serialize.js";@@ -1615,7 +1608,8 @@ describe("createToolLoopHarness", () => {expect(hasPendingInputBatch(reparked.session.state)).toBe(true);const toolMessages = reparked.session.history.filter((message) => message.role === "tool");expect(JSON.stringify(toolMessages)).toContain("delegated-done");- expect(events.at(-1)?.type).toBe("session.waiting");+ expect(reparked.held).toEqual({ kind: "request" });+ expect(events.at(-1)?.type).toBe("turn.waiting");});it("waits on a task_wait parked beside an approval, but not on an invalid task_cancel", async () => {@@ -1932,119 +1926,6 @@ describe("createToolLoopHarness", () => {const LIMIT_REQUEST_ID = "test-session:limit:input:12";- it.each([false, true])(- "passes deferred input and history to turn resolvers while a session limit is open (memory: %s)",- async (withMemory) => {- const ctx = new ContextContainer();- ctx.set(SandboxKey, mockSandbox().access);- ctx.set(SessionIdKey, "test-session");- ctx.set(SessionKey, {- sessionId: "test-session",- auth: { current: null, initiator: null },- turn: { id: "turn_0", sequence: 0 },- });- const hidden = { content: "Hidden history", kind: "user" as const, role: "user" as const };- const snapshots: (readonly ModelMessage[])[] = [];- const recall = vi.fn(async () => ({- messages: [{ content: "Remembered context", id: "item" }],- }));- const memories = withMemory- ? [- {- ...defineMemory({- namespace: "test",- scope: "alice",- provider: { recall: { "turn.started": recall } },- }),- logicalPath: "memory/profile.ts",- slot: "profile",- sourceId: "memory/profile.ts",- sourceKind: "module" as const,- visibility: "scope" as const,- },- ]- : [];- const runStep = createToolLoopHarness(- createTestConfig(- async (event, messages) => {- const lifecycleMessages = await dispatchMemoryLifecycleEvent({- appRoot: "/app",- ctx,- event,- memories,- messages,- nodeId: "__root__",- });- await dispatchDynamicSkillEvent({- ctx,- event,- messages: lifecycleMessages,- resolvers: [- {- eventNames: ["turn.started"],- events: {- "turn.started": (_event, context) => {- snapshots.push((context as DynamicResolveContext).messages);- return null;- },- },- logicalPath: "skills/context.ts",- slug: "context",- sourceId: "skills/context.ts",- sourceKind: "module",- },- ],- });- },- { historyProjector: ({ messages }) => messages.filter((message) => message !== hidden) },- ),- );- const first = await contextStorage.run(ctx, () =>- runStep({ ...createLimitReachedSession(), history: [hidden] }, { message: "Hi again" }),- );- const second = await contextStorage.run(ctx, () =>- runStep(- first.session,- attachClientContext(- {- context: ["Current context"],- message: "Alice has a follow-up question.",- },- ["Current page"],- ),- ),- );-- expect(snapshots).toHaveLength(2);- expect(snapshots[1]).toContainEqual({ content: "Hi again", kind: "user", role: "user" });- expect(snapshots[1]).not.toContain(hidden);- expect(snapshots[1]?.slice(-3)).toEqual([- { content: "Current page", kind: "context.instruction", role: "user" },- { content: "Current context", kind: "context.instruction", role: "user" },- { content: "Alice has a follow-up question.", kind: "user", role: "user" },- ]);- expect(- second.session.history.some(- (message) => message.content === "Alice has a follow-up question.",- ),- ).toBe(false);- expect(second.next).toBeNull();- expect(ToolLoopAgent).not.toHaveBeenCalled();- if (withMemory) {- expect(recall).toHaveBeenCalledTimes(2);- expect(snapshots[1]).toContainEqual({- content: "Remembered context",- kind: "memory.load",- role: "user",- });- expect(- second.session.history.some((message) => message.content === "Remembered context"),- ).toBe(true);- expect(second.session.state?.["eve.memory"]).toBeDefined();- }- },- );-it("parks on a deterministic continuation prompt when the session reaches its token limit", async () => {const { emit, events } = createEventCollector();const runStep = createToolLoopHarness(createTestConfig(emit));@@ -3321,13 +3202,15 @@ describe("createToolLoopHarness", () => {},type: "input.requested",});- expect(events.filter((event) => event.type === "turn.completed").at(-1)).toEqual({+ expect(events.filter((event) => event.type === "turn.waiting").at(-1)).toMatchObject({data: {+ on: "input",sequence: 0,turnId: "turn_0",},- type: "turn.completed",+ type: "turn.waiting",});+ expect(events.some((event) => event.type === "turn.completed")).toBe(false);});it.each(["", "Alice's inventory list is"])(@@ -4801,7 +4684,7 @@ describe("createToolLoopHarness", () => {}),);expect(events.at(-1)).toEqual({- data: { sequence: 0, turnId: "turn_0", usage: DELEGATED_SPEND },+ data: { on: "tasks", sequence: 0, turnId: "turn_0", usage: DELEGATED_SPEND },type: "turn.waiting",});expect(events.map((event) => event.type)).not.toContain("session.waiting");@@ -7941,8 +7824,7 @@ describe("createToolLoopHarness", () => {"actions.requested","step.completed","input.requested",- "turn.completed",- "session.waiting",+ "turn.waiting",]);expect(events.find((event) => event.type === "actions.requested")).toEqual({data: {@@ -8100,396 +7982,6 @@ describe("createToolLoopHarness", () => {expect(events.filter((event) => event.type === "action.result")).toHaveLength(1);});- it("keeps one pending approval across many follow-up questions", async () => {- const isPendingApprovalProjection = (message: ModelMessage) => {- const content = JSON.stringify(message.content);- return (- message.role === "user" &&- /pending/iu.test(content) &&- content.includes("approval-1") &&- content.includes("bash")- );- };- const pendingApprovalInstructions = (index: number): string =>- JSON.stringify(vi.mocked(ToolLoopAgent).mock.calls[index]?.[0].instructions);- const followupQuestions = [- "What is the current status of my request?",- "Has the requested action executed yet?",- "What are you waiting for before continuing?",- "Can you summarize what remains blocked?",- "What will happen after I approve the request?",- ];- const agentResults = [- pendingBashApprovalResult(),- ...followupQuestions.map((_, index) => ({- finishReason: "stop",- response: {- messages: [{ content: `Follow-up ${String(index + 1)} answered.`, role: "assistant" }],- },- text: `Follow-up ${String(index + 1)} answered.`,- toolCalls: [],- toolResults: [],- })),- {- finishReason: "stop",- response: {- messages: [{ content: "Okay, I will not run that command.", role: "assistant" }],- },- text: "Okay, I will not run that command.",- toolCalls: [],- toolResults: [],- },- ] satisfies Record<string, unknown>[];- setupMockAgentSequence(agentResults);-- const readGenerateMessages = (index: number): ModelMessage[] => {- const agent = vi.mocked(ToolLoopAgent).mock.results[index]?.value;- const call = agent === undefined ? undefined : vi.mocked(agent.generate).mock.calls[0]?.[0];- if (call === undefined) {- throw new Error(`ToolLoopAgent instance ${String(index)} did not generate.`);- }- return call.messages;- };-- const runStep = createToolLoopHarness(- createTestConfig(undefined, {- tools: new Map([- [- "bash",- {- description: "Run shell commands",- execute: vi.fn().mockResolvedValue("ok"),- inputSchema: jsonSchema({ type: "object" }),- name: "bash",- },- ],- ]),- }),- );- const initialSession = createTestSession({- agent: {- modelReference: { id: "test-model" },- system: "You are a test assistant.",- tools: [- { description: "Run shell commands", name: "bash", inputSchema: { type: "object" } },- ],- },- });- const parked = await runStep(initialSession, {- message: "Delete the temp directory.",- });-- expect(parked.next).toBeNull();- expect(vi.mocked(ToolLoopAgent).mock.calls[0]?.[0].toolChoice).not.toBe("none");- expect(parked.session.history.filter(isPendingApprovalProjection)).toHaveLength(1);- expect(hasPendingInputBatch(parked.session.state)).toBe(true);-- // Every follow-up runs as an ordinary turn with tools available while the- // original approval remains unresolved.- let pendingSession = parked.session;- for (const [index, question] of followupQuestions.entries()) {- const followup = await runStep(pendingSession, {- message: question,- });- const callIndex = index + 1;- const messages = readGenerateMessages(callIndex);-- expect(followup.next).toBeNull();- expect(vi.mocked(ToolLoopAgent)).toHaveBeenCalledTimes(callIndex + 1);- const settings = vi.mocked(ToolLoopAgent).mock.calls[callIndex]?.[0];- expect(settings?.toolChoice).not.toBe("none");- expect(settings?.tools).toHaveProperty("bash");- expect(messages.at(-1)).toEqual({ content: question, kind: "user" as const, role: "user" });- expect(messages.every((message) => message.role !== "tool")).toBe(true);- expect(messages.filter(isPendingApprovalProjection)).toHaveLength(1);- expect(messages.findIndex(isPendingApprovalProjection)).toBe(1);- expect(pendingApprovalInstructions(callIndex)).toContain("Trusted eve runtime state");- expect(pendingApprovalInstructions(callIndex)).toContain("approval-1");- expect(pendingApprovalInstructions(callIndex)).toContain("bash");- expect(pendingApprovalInstructions(callIndex)).not.toContain("rm -rf /tmp/demo");- expect(followup.session.history.filter(isPendingApprovalProjection)).toHaveLength(1);- expect(getDeferredStepInput(followup.session)).toBeUndefined();- expect(hasPendingInputBatch(followup.session.state)).toBe(true);- pendingSession = followup.session;- }-- // The late denial still restores the withheld batch behind the- // intervening exchange and resolves it.- const deniedResult = await runStep(pendingSession, {- inputResponses: [{ requestId: "approval-1", optionId: "cancel" }],- });-- const denialIndex = followupQuestions.length + 1;- const denialMessages = readGenerateMessages(denialIndex);- const denialSettings = vi.mocked(ToolLoopAgent).mock.calls[denialIndex]?.[0];- expect(vi.mocked(ToolLoopAgent)).toHaveBeenCalledTimes(denialIndex + 1);- expect(denialSettings?.toolChoice).not.toBe("none");- expect(denialSettings?.tools).toHaveProperty("bash");- expect(denialMessages.slice(-2)).toEqual([- {- content: [- { text: "Need approval first.", type: "text" },- {- input: { command: "rm -rf /tmp/demo" },- toolCallId: "call-1",- toolName: "bash",- type: "tool-call",- },- {- approvalId: "approval-1",- toolCallId: "call-1",- type: "tool-approval-request",- },- ],- role: "assistant",- },- {- content: [- {- approvalId: "approval-1",- approved: false,- reason: "Tool execution was denied.",- type: "tool-approval-response",- },- {- output: {- type: "execution-denied",- reason: "Tool execution was denied.",- },- toolCallId: "call-1",- toolName: "bash",- type: "tool-result",- },- ],- role: "tool",- },- ]);- expect(denialMessages[0]).toEqual({- content: "Delete the temp directory.",- kind: "user" as const,- role: "user",- });- expect(hasPendingInputBatch(deniedResult.session.state)).toBe(false);- expect(deniedResult.session.history.at(-1)).toEqual({- content: "Okay, I will not run that command.",- role: "assistant",- });- });-- it("executes an unrelated tool while an earlier approval stays open", async () => {- const bashExecute = vi.fn().mockResolvedValue("deleted");- const statusExecute = vi.fn().mockResolvedValue("ready");-- vi.mocked(ToolLoopAgent).mockImplementation(function (- this: MockAgentInstance,- settings: MockAgentSettings & {- tools?: Record<- string,- {- execute?: (- input: unknown,- options: { toolCallId: string },- ) => Promise<unknown> | unknown;- }- >;- },- ) {- const { onStepEnd, prepareStep } = settings;- const executeStatus = settings.tools?.["status"]?.execute;- if (executeStatus === undefined) {- throw new Error("status tool was not executable.");- }-- this.generate = vi.fn().mockImplementation(async (options: { messages: unknown[] }) => {- if (prepareStep) {- await prepareStep({- context: undefined,- messages: options.messages,- model: {},- stepNumber: 0,- steps: [],- });- }-- const toolCall = {- input: {},- toolCallId: "status-call",- toolName: "status",- type: "tool-call" as const,- };- const output = await executeStatus(toolCall.input, {- toolCallId: toolCall.toolCallId,- });- const toolResult = {- input: toolCall.input,- output,- toolCallId: toolCall.toolCallId,- toolName: toolCall.toolName,- type: "tool-result" as const,- };- const result = {- finishReason: "tool-calls",- response: {- messages: [- { content: [toolCall], role: "assistant" as const },- { content: [toolResult], role: "tool" as const },- ],- },- text: "",- toolCalls: [toolCall],- toolResults: [toolResult],- };- if (onStepEnd) await onStepEnd(result);- return createMockGenerateResult(result);- });- return this;- } as MockAgentConstructor);-- const pendingSession = createPendingBashApprovalSession();- const session = {- ...pendingSession,- agent: {- ...pendingSession.agent,- tools: [- ...pendingSession.agent.tools,- { description: "Read status", name: "status", inputSchema: { type: "object" } },- ],- },- };- const config = createTestConfig(undefined, {- tools: new Map([- [- "bash",- {- description: "Run shell commands",- execute: bashExecute,- inputSchema: jsonSchema({ type: "object" }),- name: "bash",- },- ],- [- "status",- {- description: "Read status",- execute: statusExecute,- inputSchema: jsonSchema({ type: "object" }),- name: "status",- },- ],- ]),- });-- const result = await createToolLoopHarness(config)(session, {- message: "Check the status while that waits.",- });-- expect(statusExecute).toHaveBeenCalledOnce();- expect(bashExecute).not.toHaveBeenCalled();- expect(getPendingInputRequestIds(result.session.state)).toEqual(new Set(["approval-1"]));- });-- it("releases a message wedged behind an approval by an earlier version", async () => {- const generateCalls: Array<Array<{ role: string; content: unknown }>> = [];- const stillHere = {- finishReason: "stop",- response: { messages: [{ content: "Still here!", role: "assistant" }] },- text: "Still here!",- toolCalls: [],- toolResults: [],- };- vi.mocked(ToolLoopAgent).mockImplementation(function (- this: MockAgentInstance,- settings: MockAgentSettings,- ) {- const { onStepEnd, prepareStep } = settings;- this.generate = vi.fn().mockImplementation(async (input: { messages: unknown[] }) => {- if (prepareStep) {- await prepareStep({- messages: input.messages,- steps: [],- stepNumber: 0,- model: {},- context: undefined,- });- }- generateCalls.push(input.messages as Array<{ role: string; content: unknown }>);- if (onStepEnd) await onStepEnd(stillHere);- return createMockGenerateResult(stillHere);- });- return this;- } as MockAgentConstructor);-- // A session wedged by the pre-collection deferral: the singleton batch- // key plus a message swallowed into the deferred step input.- const session = createTestSession({- agent: {- modelReference: { id: "test-model" },- system: "You are a test assistant.",- tools: [- { description: "Run shell commands", name: "bash", inputSchema: { type: "object" } },- ],- },- });- const wedged = {- ...session,- state: {- "eve.runtime.deferredStepInput": { message: "Wedged hello." },- "eve.runtime.pendingInputBatch": {- requests: [- {- action: {- callId: "call-1",- input: { command: "rm -rf /tmp/demo" },- kind: "tool-call",- toolName: "bash",- },- allowFreeform: false,- display: "confirmation",- kind: "tool-approval",- options: [- { id: "approve", label: "Yes" },- { id: "cancel", label: "No" },- ],- prompt: "Approve tool call: bash",- requestId: "approval-1",- },- ],- responseMessages: [- {- content: [- {- input: { command: "rm -rf /tmp/demo" },- toolCallId: "call-1",- toolName: "bash",- type: "tool-call",- },- {- approvalId: "approval-1",- toolCallId: "call-1",- type: "tool-approval-request",- },- ],- role: "assistant",- },- ],- },- },- };-- const result = await createToolLoopHarness(createTestConfig())(wedged, {- message: "Are you there?",- });-- // The wedged message finally runs as an ordinary turn; the approval- // stays open and answerable.- expect(generateCalls).toHaveLength(1);- const lastMessages = (generateCalls[0] ?? []).filter((message) => message.role === "user");- expect(JSON.stringify(lastMessages)).toContain("Wedged hello.");- expect(JSON.stringify(lastMessages)).toContain("Are you there?");- expect(getDeferredStepInput(result.session)).toBeUndefined();- expect(getPendingInputRequestIds(result.session.state)).toEqual(new Set(["approval-1"]));- });-it("consumes text approval shortcuts without appending them as user messages", async () => {const generateCalls: unknown[] = [];@@ -8742,160 +8234,6 @@ describe("createToolLoopHarness", () => {});});- it("follow-up message precedes a late approval denial in committed history", async () => {- // Step 1: pending approval + user sends a follow-up message. The message- // runs as an ordinary turn while the approval stays open. Step 2: the- // user denies the approval; the withheld batch is restored behind the- // intervening exchange and resolves as denied.- const generateCalls: Array<Array<{ role: string; content: unknown }>> = [];- const agentResults = [- {- finishReason: "stop",- response: { messages: [{ content: "Sure, here you go.", role: "assistant" }] },- text: "Sure, here you go.",- toolCalls: [],- toolResults: [],- },- {- finishReason: "stop",- response: {- messages: [{ content: "I will not run that command.", role: "assistant" }],- },- text: "I will not run that command.",- toolCalls: [],- toolResults: [],- },- ] satisfies Record<string, unknown>[];- let instanceIndex = 0;-- vi.mocked(ToolLoopAgent).mockImplementation(function (- this: Record<string, unknown>,- settings: MockAgentSettings,- ) {- const result = agentResults[instanceIndex];- instanceIndex += 1;- if (result === undefined) {- throw new Error("ToolLoopAgent mock exhausted its scripted results.");- }- const { onStepEnd, prepareStep } = settings;- this.generate = vi.fn().mockImplementation(async (input: { messages: unknown[] }) => {- if (prepareStep) {- await prepareStep({- messages: input.messages,- steps: [],- stepNumber: 0,- model: {},- context: undefined,- });- }- generateCalls.push(input.messages as Array<{ role: string; content: unknown }>);- if (onStepEnd) await onStepEnd(result);- return createMockGenerateResult(result);- });- return this as unknown as ToolLoopAgent;- } as unknown as ConstructorParameters<typeof ToolLoopAgent> extends [infer S]- ? (settings: S) => ToolLoopAgent- : never);-- const session = appendPendingInputBatch({- requests: [- {- action: {- callId: "call-1",- input: { command: "deploy --force" },- kind: "tool-call",- toolName: "bash",- },- allowFreeform: false,- display: "confirmation",- kind: "tool-approval",- options: [- { id: "approve", label: "Approve" },- { id: "cancel", label: "Cancel" },- ],- prompt: "Approve tool call: bash",- requestId: "approval-1",- },- ],- responseMessages: [- {- content: [- {- input: { command: "deploy --force" },- toolCallId: "call-1",- toolName: "bash",- type: "tool-call",- },- {- approvalId: "approval-1",- toolCallId: "call-1",- type: "tool-approval-request",- },- ],- role: "assistant",- },- ],- session: createTestSession({- agent: {- modelReference: { id: "test-model" },- system: "You are a test assistant.",- tools: [- { description: "Run shell commands", name: "bash", inputSchema: { type: "object" } },- ],- },- }),- });-- const config = createTestConfig(undefined, {- tools: new Map([- [- "bash",- {- description: "Run shell commands",- execute: vi.fn().mockResolvedValue("ok"),- inputSchema: jsonSchema({ type: "object" }),- name: "bash",- },- ],- ]),- });-- // Step 1: the follow-up message runs immediately; the approval stays open.- const firstResult = await createToolLoopHarness(config)(session, {- message: "Do something else",- });- expect(firstResult.next).toBeNull();- expect(generateCalls).toHaveLength(1);- expect(generateCalls[0]?.at(-1)).toEqual({- content: "Do something else",- kind: "user" as const,- role: "user",- });- expect(hasPendingInputBatch(firstResult.session.state)).toBe(true);-- // Step 2: the late denial restores the withheld batch behind the- // intervening exchange; the model sees the denial as the tail.- const deniedResult = await createToolLoopHarness(config)(firstResult.session, {- inputResponses: [{ requestId: "approval-1", optionId: "cancel" }],- });- expect(generateCalls).toHaveLength(2);- expect(generateCalls[1]?.at(-1)?.role).toBe("tool");-- // Committed history keeps the intervening exchange before the batch.- const history = deniedResult.session.history;- expect(history[0]).toEqual({- content: "Do something else",- kind: "user" as const,- role: "user",- });- expect(history[1]).toEqual({ content: "Sure, here you go.", role: "assistant" });- expect(history.at(-1)).toEqual({- content: "I will not run that command.",- role: "assistant",- });- expect(hasPendingInputBatch(deniedResult.session.state)).toBe(false);- });-it("emits compaction.requested and compaction.completed when compaction triggers", async () => {vi.mocked(shouldCompact).mockReturnValue(true);vi.mocked(compactMessages).mockResolvedValue([diff --git a/packages/eve/src/harness/tool-loop.ts b/packages/eve/src/harness/tool-loop.tsindex 98a38d382..8938ee9b6 100644--- a/packages/eve/src/harness/tool-loop.ts+++ b/packages/eve/src/harness/tool-loop.ts@@ -1,3 +1,4 @@+import { declinedSignInEvents, withdrawHeldSignIns } from "#harness/held-requests.js";import { setTimeout as delay } from "node:timers/promises";import { GenerationSteering } from "#harness/generation-steering.js";import { interruptStreamOnFailure } from "#harness/interruptible-stream.js";@@ -107,7 +108,6 @@ import {emitTurnEpilogue,emitTurnPreamble,getHarnessEmissionState,- isHarnessBetweenTurns,setHarnessEmissionState,} from "#harness/emission.js";import { extractToolApprovalInputRequests } from "#harness/input-extraction.js";@@ -158,7 +158,6 @@ import {frameworkMessageKindForStepInput,normalizeModelMessages,normalizeUserContent,- createTurnInputMessages,followsToolResults,resolveAssistantStepText,TOOL_RESULT_BOUNDARY,@@ -674,7 +673,35 @@ export function createToolLoopHarness(config: ToolLoopHarnessConfig): StepFn {stepInput: stepInput.input,}),});- const effectiveStepInput = staleConversion.stepInput;+ let effectiveStepInput = staleConversion.stepInput;+ // A new message reaching an open turn steers it: the turn moves past the+ // sign-ins it waits on, and its unanswered approvals resolve below.+ if (input?.message !== undefined || staleConversion.kind === "converted") {+ const withdrawal = withdrawHeldSignIns(session.state, {+ completedAt: Date.now(),+ reason: STEERED_SIGN_IN_REASON,+ });+ session = { ...session, state: withdrawal.state };+ if (withdrawal.withdrawn.length > 0) {+ if (emit) {+ for (const event of declinedSignInEvents(+ withdrawal.withdrawn,+ STEERED_SIGN_IN_REASON,+ emissionState,+ )) {+ await emit(event);+ }+ }+ const names = [...new Set(withdrawal.withdrawn.map((challenge) => challenge.name))];+ effectiveStepInput = {+ ...effectiveStepInput,+ context: [+ ...(effectiveStepInput?.context ?? []),+ `Sign-in to ${names.join(", ")} was cancelled because the user sent a new message instead. Ask to sign in again only if the new message still needs it.`,+ ],+ };+ }+ }const preambleStepInput =staleConversion.kind === "converted"? { ...effectiveStepInput, message: staleConversion.displayMessage }@@ -823,6 +850,7 @@ export function createToolLoopHarness(config: ToolLoopHarnessConfig): StepFn {return { next: runStep, session: continuedSession };}if (coordinated.challenges.length > 0) {+ // A responder's sign-in for the turn's approval holds the turn.if (emit) {for (const challenge of coordinated.challenges) {await emit(@@ -837,20 +865,22 @@ export function createToolLoopHarness(config: ToolLoopHarnessConfig): StepFn {}),);}+ emissionState = await holdTurnForRequest(emit, emissionState, session);}const parkedSession =coordinated.stepInput === undefined? session: queueDeferredStepInput(session, coordinated.stepInput);- return {- next: null,- session: {+ const challengeSession = setHarnessEmissionState(+ {...parkedSession,state: setPendingAuthorization(parkedSession.state, {challenges: coordinated.challenges,}),},- };+ emissionState,+ );+ return { held: { kind: "request" }, next: null, session: challengeSession };}// Approved siblings of a finished workflow run with their approval turn's tools.@@ -861,9 +891,7 @@ export function createToolLoopHarness(config: ToolLoopHarnessConfig): StepFn {const responseAuthorizationTools =replayBatch === undefined ? config.tools : await prepareApprovalTools(replayBatch);const pending = resolvePendingInput({- activeTurnId: pendingCoordination?.event.turnId ?? activeTurnId(emissionState),history: resolvedCoordination.messages,- internalStep: !isHarnessBetweenTurns(session),resolveApprovalKey: resolveApprovalKeyFromTools(responseAuthorizationTools),session,stepInput: coordinated.stepInput,@@ -879,97 +907,13 @@ export function createToolLoopHarness(config: ToolLoopHarnessConfig): StepFn {}: pending.session;- if (emit && pending.deferredMessage === true && hasStepInput(input)) {- const deferredInput = createTurnInputMessages(effectiveStepInput);- if (store !== undefined) {- prepareDynamicInstructionPreamble(- store,- projectHistory(parkedSession.history, parkedSession.state),- );- prepareMemoryPreamble(store, {- history: parkedSession.history,- input: deferredInput,- projector: config.historyProjector,- state: parkedSession.state,- });- }- let instructionMessages: UserModelMessage[] = [];- let memoryCommit: ReturnType<typeof drainMemoryCommit> = undefined;- try {- const traceContext = await preparePreambleTrace();- emissionState = await emitTurnPreamble(- emit,- preambleStepInput ?? {},- emissionState,- projectHistory([...parkedSession.history, ...deferredInput], parkedSession.state),- config.runtimeIdentity,- traceContext,- );- } catch (error) {- instructionMessages =- store === undefined ? [] : drainDynamicInstructionUserMessages(store);- memoryCommit = store === undefined ? undefined : drainMemoryCommit(store);- parkedSession = {- ...parkedSession,- history: validateHarnessModelMessages([- ...parkedSession.history,- ...(memoryCommit?.recalledMessages ?? []),- ...instructionMessages,- ]),- state: memoryCommit?.state ?? parkedSession.state,- };- session = parkedSession;- return failBoundaryEvent(error, {- sessionStarted: true,- sequence: emissionState.sequence,- stepIndex: 0,- turnId: activeTurnId(emissionState),- });- }- instructionMessages = store === undefined ? [] : drainDynamicInstructionUserMessages(store);- memoryCommit = store === undefined ? undefined : drainMemoryCommit(store);- parkedSession = {- ...parkedSession,- history: validateHarnessModelMessages([- ...parkedSession.history,- ...(memoryCommit?.recalledMessages ?? []),- ...instructionMessages,- ]),- state: memoryCommit?.state ?? parkedSession.state,- };- emissionState = await emitTurnEpilogue(- emit,- emissionState,- parkedSession.history,- getSessionUsage(parkedSession),- );- return {- next: null,- session: setHarnessEmissionState(parkedSession, emissionState),- };- }-- if (resolvedCoordination.outcome === "resolved") {- if (emit) {- emissionState = await emitTurnEpilogue(- emit,- emissionState,- parkedSession.history,- getSessionUsage(parkedSession),- );- parkedSession = setHarnessEmissionState(parkedSession, emissionState);- }- return { next: null, session: parkedSession };- }-- if (- coordinated.kind === "responses-completed" &&- isHarnessBetweenTurns(pending.session) &&- getPendingAuthorization(pending.session.state) === undefined- ) {- await emit?.(createSessionWaitingEvent(getSessionUsage(pending.session)));+ // The turn's request is still open (a partial answer, a refused responder,+ // or a message waiting behind a budget prompt), so the turn stays held.+ if (emit) {+ emissionState = await holdTurnForRequest(emit, emissionState, parkedSession);+ parkedSession = setHarnessEmissionState(parkedSession, emissionState);}- return { next: null, session: pending.session };+ return { held: { kind: "request" }, next: null, session: parkedSession };}if (pending.resolvedInputs !== undefined) {@@ -1069,7 +1013,10 @@ export function createToolLoopHarness(config: ToolLoopHarnessConfig): StepFn {const traceContext = await preparePreambleTrace();emissionState = await emitTurnPreamble(emit,- preambleStepInput ?? {},+ // A deferred message replays on a later step, which announces it then.+ pending.deferredMessage === true+ ? { ...preambleStepInput, message: undefined }+ : (preambleStepInput ?? {}),emissionState,projectHistory([...pending.messages, ...ephemeralContextMessages, ...preparedTurnInput],@@ -2750,6 +2697,7 @@ async function handleStepResult(input: {session: { ...baseSession, history: parkedInputHistory },});+ const runsDeferredInput = hasRunnableDeferredStepInput(parkedSession);if (emit) {await emit(createInputRequestedEvent({@@ -2759,20 +2707,14 @@ async function handleStepResult(input: {turnId: emissionState.turnId,}),);-- emissionState = await emitTurnEpilogue(- emit,- emissionState,- parkedSession.history,- getSessionUsage(parkedSession),- );- parkedSession = setHarnessEmissionState(parkedSession, emissionState);+ if (!runsDeferredInput) {+ emissionState = await holdTurnForRequest(emit, emissionState, parkedSession);+ parkedSession = setHarnessEmissionState(parkedSession, emissionState);+ }}- return {- next: hasRunnableDeferredStepInput(parkedSession) ? runStep : null,- session: parkedSession,- };+ if (runsDeferredInput) return { next: runStep, session: parkedSession };+ return { held: { kind: "request" }, next: null, session: parkedSession };}// --- Park on authorization request ------------------------------------------@@ -2813,19 +2755,11 @@ async function handleStepResult(input: {);}- // An authorization park is a between-turn wait like the input park- // above: the session keeps serving ordinary turns while the challenge- // is open, so the stream must close its turn boundary — clients wait- // on `session.waiting` and would otherwise hang on the parked turn.- emissionState = await emitTurnEpilogue(- emit,- emissionState,- authorizationHistory,- getSessionUsage(baseSession),- );+ emissionState = await holdTurnForRequest(emit, emissionState, baseSession);}return {+ held: { kind: "request" },next: null,session: setHarnessEmissionState({@@ -2886,13 +2820,14 @@ async function handleStepResult(input: {if (emit) {await emit(createTurnWaitingEvent({+ on: "tasks",sequence: emissionState.sequence,turnId: emissionState.turnId,usage: getSessionUsage(nextSession),}),);}- return { held: { taskIds: workingTasks }, next: null, session: nextSession };+ return { held: { kind: "tasks", taskIds: workingTasks }, next: null, session: nextSession };}return { next: runStep, session: nextSession };}@@ -2909,6 +2844,30 @@ async function handleStepResult(input: {});}+/**+ * A sign-in or tool approval the turn raised holds it open, as a task or+ * `ctx.ask` does: the turn reports `turn.waiting` and resumes in the same turn+ * once the person answers, steers, or cancels.+ */+async function holdTurnForRequest(+ emit: NonNullable<ToolLoopHarnessConfig["handleEvent"]>,+ emissionState: ReturnType<typeof getHarnessEmissionState>,+ session: HarnessSession,+): Promise<ReturnType<typeof getHarnessEmissionState>> {+ const next = advanceStep(emissionState);+ await emit(+ createTurnWaitingEvent({+ on: "input",+ sequence: next.sequence,+ turnId: next.turnId,+ usage: getSessionUsage(session),+ }),+ );+ return next;+}++const STEERED_SIGN_IN_REASON = "Cancelled because a new message arrived.";+/** Appended to the model-facing description of every tool with `endsTurn: true`. */const ENDS_TURN_TOOL_NOTE ="Calling this tool ends your turn once it succeeds: do not write a reply or call other tools in the same step. If it fails, you will see the error and can continue.";diff --git a/packages/eve/src/harness/types.ts b/packages/eve/src/harness/types.tsindex 6f51e3a41..863d16a39 100644--- a/packages/eve/src/harness/types.ts+++ b/packages/eve/src/harness/types.ts@@ -226,12 +226,17 @@ export interface StepResult {*/readonly settledTurn?: SettledTurn;/**- * Present when the model ended the turn while tasks are working. The turn- * stays open until one settles.+ * Present when the turn stays open: the model ended it while tasks work, or+ * it waits on a sign-in or tool approval it raised. It resumes when a task+ * settles, or when the person answers, steers, or cancels.*/- readonly held?: { readonly taskIds: readonly string[] };+ readonly held?: TurnHold;}+export type TurnHold =+ | { readonly kind: "tasks"; readonly taskIds: readonly string[] }+ | { readonly kind: "request" };+/*** A single step of AI work. Takes the current session and optional user input,* returns the updated session and an instruction for the runtime.diff --git a/packages/eve/src/harness/workflow-approval-resume.integration.test.ts b/packages/eve/src/harness/workflow-approval-resume.integration.test.tsindex 43bdb9a40..5ddb17c84 100644--- a/packages/eve/src/harness/workflow-approval-resume.integration.test.ts+++ b/packages/eve/src/harness/workflow-approval-resume.integration.test.ts@@ -114,7 +114,7 @@ function resultCallIds(session: HarnessSession) {}describe("workflow approval resume (real AI SDK)", () => {- it("opens one resume turn before dispatch and replays accompanying input after completion", async () => {+ it("resumes the held turn before dispatch and replays accompanying input after completion", async () => {const fixture = setup([workflow("deploy", always())]);const initial = await fixture.step(fixture.session, { message: "Deploy Alice's release." });expect(getPendingCoordinationBatch(initial.session.state)).toBeUndefined();@@ -128,20 +128,19 @@ describe("workflow approval resume (real AI SDK)", () => {expect(getPendingCoordinationBatch(approved.session.state)?.tasks.map((task) => task.callId),).toEqual(["call-0"]);+ // The approval held its turn, so answering it resumes that turn.const resumedTurn = getHarnessEmissionState(approved.session.state).turnId;+ expect(resumedTurn).toBe(getHarnessEmissionState(initial.session.state).turnId);expect(resumedTurn).not.toBe("");expect(fixture.events.slice(start).filter((event) => event.type === "turn.started"),- ).toMatchObject([{ data: { turnId: resumedTurn } }]);+ ).toHaveLength(0);const completed = await fixture.step(approved.session, workflowResult());expect(resultCallIds(completed.session)).toEqual(["call-0"]);expect(fixture.model.doStreamCalls).toHaveLength(2);expect(fixture.events.slice(start).filter((event) => event.type === "step.started"),- ).toMatchObject([- { data: { turnId: resumedTurn, stepIndex: 0 } },- { data: { turnId: resumedTurn, stepIndex: 1 } },- ]);+ ).toMatchObject([{ data: { turnId: resumedTurn } }, { data: { turnId: resumedTurn } }]);expect(JSON.stringify(fixture.model.doStreamCalls[1]!.prompt)).not.toContain("Tell Alice");expect(typeof completed.next).toBe("function");const followUp = await fixture.step(completed.session);diff --git a/packages/eve/src/protocol/message.ts b/packages/eve/src/protocol/message.tsindex 53e1f39e0..d3ff6b8a4 100644--- a/packages/eve/src/protocol/message.ts+++ b/packages/eve/src/protocol/message.ts@@ -609,6 +609,9 @@ export interface TurnCompletedStreamEvent {type: "turn.completed";}+/** What an open turn waits on when it parks; see {@link TurnWaitingStreamEvent}. */+export type TurnWaitingOn = "input" | "tasks";+/*** Stream event emitted each time an open turn parks, such as when a call it is* running asks a question. The turn stays open: the next `step.started` with@@ -617,6 +620,12 @@ export interface TurnCompletedStreamEvent {*/export interface TurnWaitingStreamEvent {data: {+ /**+ * What the turn waits on. `"input"`: a person must act on a sign-in,+ * approval, or question; clients stop reading here. `"tasks"`: work the+ * turn started is still running, and the turn resumes on its own.+ */+ on: TurnWaitingOn;sequence: number;turnId: string;/**@@ -1707,12 +1716,14 @@ export function createTurnCompletedEvent(input: {* Creates the `turn.waiting` event for one open turn that parked.*/export function createTurnWaitingEvent(input: {+ readonly on: TurnWaitingOn;readonly sequence: number;readonly turnId: string;readonly usage: TokenUsage | undefined;}): TurnWaitingStreamEvent {return {data: {+ on: input.on,sequence: input.sequence,turnId: input.turnId,...(input.usage !== undefined && { usage: input.usage }),diff --git a/packages/eve/src/public/channels/slack/approval-cards.ts b/packages/eve/src/public/channels/slack/approval-cards.tsnew file mode 100644index 000000000..fa39a5573--- /dev/null+++ b/packages/eve/src/public/channels/slack/approval-cards.ts@@ -0,0 +1,325 @@+/**+ * eve's default Slack rendering for human-input requests: posting approval+ * and question controls, and retiring an approval's messages once it resolves.+ */++import { createLogger, logError } from "#internal/logging.js";+import {+ slackUserIdForPrincipal,+ slackUserIdFromAuthContext,+} from "#public/channels/slack/auth.js";+import {+ buildAnsweredBlocks,+ decodeHitlActionId,+ renderInputRequestPostParts,+ type SlackInputRequestPostPart,+} from "#public/channels/slack/hitl.js";+import {+ SLACK_MAX_BLOCKS_PER_MESSAGE,+ truncateMessageText,+} from "#public/channels/slack/limits.js";+import { deliverPrivateInputRequest } from "#public/channels/slack/private-approval-delivery.js";+import type {+ SlackApprovalChannelResolver,+ SlackChannelInternalEvents,+ SlackChannelState,+ SlackEventContext,+ SlackPendingApprovalCard,+} from "#public/channels/slack/slackChannel.js";+import type { InputRequest } from "#shared/input.js";++const log = createLogger("slack.defaults");++/**+ * Default `input.requested` handler — renders each pending HITL+ * request as Slack `block_actions`. Buttons by default; radio for+ * ≤6-option select requests; static_select for >6-option select+ * requests. Batches split into multiple posts when they would exceed+ * Slack's 50-block message cap.+ */+export function defaultInputRequestedHandler(+ approvalChannel?: SlackApprovalChannelResolver,+): NonNullable<SlackChannelInternalEvents["input.requested"]> {+ return async (data, channel, ctx) => {+ const directMessageRequests: InputRequest[] = [];+ const threadRequests: InputRequest[] = [];+ for (const request of data.requests) {+ const destination =+ approvalChannel === undefined ? "thread" : await approvalChannel(request, ctx);+ (destination === "direct-message" ? directMessageRequests : threadRequests).push(request);+ }++ await postPublicInputRequests(threadRequests, channel);+ for (const request of directMessageRequests) {+ const reviewer =+ slackUserIdFromAuthContext(ctx.session.auth.current) ?? channel.state.triggeringUserId;+ if (!reviewer) {+ log.warn("direct-message input request not delivered because no reviewer was resolved", {+ requestId: request.requestId,+ sessionId: ctx.session.id,+ });+ continue;+ }+ const card = await deliverPrivateInputRequest({+ previewMessageTs: channel.state.triggeringMessageTs ?? channel.slack.threadTs,+ request,+ reviewer,+ slack: channel.slack,+ });+ recordApprovalCards(channel.state, [request], card);+ try {+ const announcement = await channel.thread.post(+ `Waiting on ${request.kind === "tool-approval" ? "approval" : "a response"} from <@${reviewer}>…`,+ );+ if (announcement.id) {+ recordApprovalCards(channel.state, [request], {+ ...card,+ announcementTs: announcement.id,+ });+ }+ } catch (error) {+ logError(log, "failed to announce private input request", error, {+ requestId: request.requestId,+ sessionId: ctx.session.id,+ });+ }+ }+ };+}++async function postPublicInputRequests(+ requests: readonly InputRequest[],+ channel: SlackEventContext,+): Promise<void> {+ const detailsByRequest = new Map<string, string>();+ for (const post of buildInputRequestPosts(requests)) {+ const message = await channel.thread.post({ blocks: post.blocks, text: post.text });+ if (post.kind === "details") {+ if (message.id) {+ for (const request of post.requests) detailsByRequest.set(request.requestId, message.id);+ }+ continue;+ }+ for (const request of post.requests) {+ const detailsMessageTs = detailsByRequest.get(request.requestId);+ recordApprovalCards(channel.state, [request], {+ detailsMessageTs,+ messageBlocks: post.blocks,+ messageTs: message.id,+ });+ }+ }+}++function recordApprovalCards(+ state: SlackChannelState,+ requests: readonly InputRequest[],+ card: SlackPendingApprovalCard,+): void {+ if (!card.messageTs) return;+ const cards = { ...state.pendingApprovalCards };+ for (const request of requests) {+ if (request.kind === "tool-approval") cards[request.requestId] = card;+ }+ state.pendingApprovalCards = cards;+}++/**+ * Retires an approval's Slack messages once it resolves, however it resolved:+ * the card loses its buttons, the tool-input details post is deleted when no+ * other pending approval shares it, and a direct-message announcement in the+ * thread reports the outcome. Slack failures are logged, and the card is+ * forgotten either way so a failed update is never retried forever.+ */+async function settleApprovalCard(+ channel: SlackEventContext,+ requestId: string,+ answer: { readonly announcement: string; readonly label: string; readonly userId?: string },+): Promise<void> {+ const cards = channel.state.pendingApprovalCards ?? {};+ const card = cards[requestId];+ if (card === undefined) return;+ const next = { ...cards };+ delete next[requestId];+ channel.state.pendingApprovalCards = next;+ const messageChannelId = card.messageChannelId ?? channel.state.channelId;+ if (messageChannelId === null) return;++ const blocks = card.messageBlocks.flatMap((block) => {+ if (!blockContainsRequestAction(block, requestId)) return [block];+ if (typeof block !== "object" || block === null) return [];+ const candidate = block as Record<string, unknown>;+ if (candidate.type !== "card") {+ return buildAnsweredBlocks({+ answerLabel: answer.label,+ promptBlocks: [],+ userId: answer.userId,+ });+ }+ const { actions: _actions, subtext: _subtext, ...withoutActions } = candidate;+ return buildAnsweredBlocks({+ answerLabel: answer.label,+ promptBlocks: [withoutActions],+ userId: answer.userId,+ });+ });+ for (const [otherId, pendingCard] of Object.entries(next)) {+ if (pendingCard.messageTs === card.messageTs) {+ next[otherId] = { ...pendingCard, messageBlocks: blocks };+ }+ }+ await settleRequest(channel, "chat.update", requestId, {+ blocks,+ channel: messageChannelId,+ text: `Answered: ${answer.label}`,+ ts: card.messageTs,+ });++ const details = card.detailsMessageTs;+ if (details !== undefined && !Object.values(next).some((c) => c.detailsMessageTs === details)) {+ await settleRequest(channel, "chat.delete", requestId, {+ channel: messageChannelId,+ ts: details,+ });+ }+ if (card.announcementTs !== undefined && channel.state.channelId !== null) {+ await settleRequest(channel, "chat.update", requestId, {+ channel: channel.state.channelId,+ text: answer.announcement,+ ts: card.announcementTs,+ });+ }+}++async function settleRequest(+ channel: SlackEventContext,+ operation: "chat.delete" | "chat.update",+ requestId: string,+ body: Record<string, unknown>,+): Promise<void> {+ try {+ const response = await channel.slack.request(operation, body);+ if (response.ok !== true) {+ log.warn("failed to retire approval message", {+ error: response.error,+ operation,+ requestId,+ });+ }+ } catch (error) {+ logError(log, "failed to retire approval message", error, { operation, requestId });+ }+}++/**+ * Groups HITL requests into `chat.postMessage` payloads that stay under+ * Slack's block-count cap. Tool input details are posted before interactive+ * approval controls so Slack's callback body cannot grow with the input.+ */+function buildInputRequestPosts(requests: readonly InputRequest[]): Array<{+ blocks: unknown[];+ kind: "controls" | "details";+ requests: InputRequest[];+ text: string;+}> {+ const details: Array<SlackInputRequestPostPart & { readonly request: InputRequest }> = [];+ const controls: Array<SlackInputRequestPostPart & { readonly request: InputRequest }> = [];+ for (const request of requests) {+ const parts = renderInputRequestPostParts(request);+ if (parts.details) details.push({ ...parts.details, request });+ controls.push({ ...parts.controls, request });+ }++ return [+ ...groupInputRequestPostParts(details).map((post) => ({ ...post, kind: "details" as const })),+ ...groupInputRequestPostParts(controls).map((post) => ({ ...post, kind: "controls" as const })),+ ];+}++function groupInputRequestPostParts(+ parts: readonly (SlackInputRequestPostPart & { readonly request: InputRequest })[],+): Array<{ blocks: unknown[]; requests: InputRequest[]; text: string }> {+ const groups: Array<{ blocks: unknown[]; fallbacks: string[]; requests: InputRequest[] }> = [];+ for (const part of parts) {+ const current = groups.at(-1);+ if (current && current.blocks.length + part.blocks.length <= SLACK_MAX_BLOCKS_PER_MESSAGE) {+ current.blocks.push(...part.blocks);+ current.fallbacks.push(part.text);+ current.requests.push(part.request);+ } else {+ groups.push({ blocks: [...part.blocks], fallbacks: [part.text], requests: [part.request] });+ }+ }+ return groups.map((group) => ({+ blocks: group.blocks,+ requests: group.requests,+ text: truncateMessageText(group.fallbacks.join("\n")),+ }));+}++function blockContainsRequestAction(block: unknown, requestId: string): boolean {+ if (typeof block !== "object" || block === null) return false;+ const candidate = block as { actions?: unknown; elements?: unknown };+ return [candidate.actions, candidate.elements].some(+ (entries) =>+ Array.isArray(entries) &&+ entries.some((entry) => {+ if (typeof entry !== "object" || entry === null) return false;+ const actionId = (entry as { action_id?: unknown }).action_id;+ return (+ typeof actionId === "string" && decodeHitlActionId(actionId)?.requestId === requestId+ );+ }),+ );+}++/** Default handlers for the approval lifecycle events. */+export const approvalEvents: Pick<+ SlackChannelInternalEvents,+ "approval.candidate" | "approval.settled" | "input.resolved"+> = {+ // A pending candidate gets no notice: an ephemeral cannot be removed once the+ // approval settles, and the card itself reports the outcome moments later.+ async "approval.candidate"(event, channel, _ctx) {+ if (event.outcome !== "rejected" && event.outcome !== "failed") return;+ const userId = slackUserIdForPrincipal(channel.state, event.responderPrincipalId);+ if (userId === undefined) return;+ await channel.thread.postEphemeral(+ userId,+ event.reason ?? "We couldn’t verify your response. Please try again.",+ );+ },++ async "approval.settled"(event, channel, _ctx) {+ const approved = event.outcome === "approved";+ const userId = slackUserIdForPrincipal(channel.state, event.responderPrincipalId);+ const by = userId === undefined ? "" : ` by <@${userId}>`;+ await settleApprovalCard(channel, event.requestId, {+ announcement: `${approved ? "Approved" : "Cancelled"}${by}.`,+ label: approved ? "Approve" : "Cancel",+ userId,+ });+ },++ // `approval.settled` runs first in the same step and retires clicked cards+ // with the responder. Approvals that end any other way (the user replied+ // instead, or the request was withdrawn) are retired here.+ async "input.resolved"(event, channel, _ctx) {+ for (const resolution of event.resolutions) {+ if (resolution.kind !== "tool-approval") continue;+ const label =+ resolution.outcome === "approved"+ ? "Approve"+ : resolution.outcome === "denied"+ ? "Cancel"+ : "No longer needed";+ await settleApprovalCard(channel, resolution.requestId, {+ announcement:+ label === "No longer needed"+ ? "This approval is no longer needed."+ : `${label === "Approve" ? "Approved" : "Cancelled"}.`,+ label,+ });+ }+ },+};diff --git a/packages/eve/src/public/channels/slack/auth.ts b/packages/eve/src/public/channels/slack/auth.tsindex ec5ad30c0..4a2f1f3ff 100644--- a/packages/eve/src/public/channels/slack/auth.ts+++ b/packages/eve/src/public/channels/slack/auth.ts@@ -4,6 +4,8 @@ import type { SlackChannelState } from "#public/channels/slack/slackChannel.js";interface SlackAuthContextInput {readonly channelId: string;readonly fullName?: string;+ /** Workspace whose app installation received the event. Keys the principal when known. */+ readonly installationTeamId?: string | null;readonly isBot?: boolean;readonly teamId?: string | null;readonly threadTs: string;@@ -32,10 +34,14 @@ export function slackUserIdForPrincipal(*/export function buildSlackAuthContext(input: SlackAuthContextInput): SessionAuthContext {const isBot = input.isBot === true;- const principalId = input.teamId+ // Messages and button clicks report different team fields for Slack Connect+ // and Enterprise Grid users. The installation team is the same on both, so+ // one person keeps one principal (and one connection grant) per thread.+ const identityTeamId = input.installationTeamId || input.teamId;+ const principalId = identityTeamId? isBot- ? `slack:${input.teamId}:bot:${input.userId}`- : `slack:${input.teamId}:${input.userId}`+ ? `slack:${identityTeamId}:bot:${input.userId}`+ : `slack:${identityTeamId}:${input.userId}`: isBot? `slack:bot:${input.userId}`: `slack:${input.userId}`;@@ -53,7 +59,7 @@ export function buildSlackAuthContext(input: SlackAuthContextInput): SessionAuthreturn {attributes,authenticator: "slack-webhook",- issuer: input.teamId ? `slack:${input.teamId}` : "slack",+ issuer: identityTeamId ? `slack:${identityTeamId}` : "slack",principalId,principalType: isBot ? "service" : "user",};diff --git a/packages/eve/src/public/channels/slack/connections.test.ts b/packages/eve/src/public/channels/slack/connections.test.tsindex ea9ff74b4..ca5f867d2 100644--- a/packages/eve/src/public/channels/slack/connections.test.ts+++ b/packages/eve/src/public/channels/slack/connections.test.ts@@ -6,6 +6,10 @@ import {buildAuthRequiredPublicText,formatConnectionDisplayName,} from "#public/channels/slack/connections.js";+import {+ parseSignInCancelTarget,+ SIGN_IN_CANCEL_ACTION_ID,+} from "#public/channels/slack/sign-in-cancel.js";describe("formatConnectionDisplayName", () => {it("title-cases the first character", () => {@@ -22,15 +26,15 @@ describe("formatConnectionDisplayName", () => {});describe("buildAuthRequiredPublicText", () => {- it("invites the recipient to connect when the link was sent privately", () => {- expect(buildAuthRequiredPublicText({ displayName: "Linear", hasRecipient: true })).toBe(- "Connect with Linear to continue",+ it("names who the thread is waiting on when the link was sent privately", () => {+ expect(buildAuthRequiredPublicText({ displayName: "Linear", recipientUserId: "U777" })).toBe(+ "Paused: waiting for <@U777> to connect Linear…",);});it("notes when the link could not be sent privately", () => {- expect(buildAuthRequiredPublicText({ displayName: "Linear", hasRecipient: false })).toBe(- "Authorization required for Linear (couldn't send the sign-in link privately)",+ expect(buildAuthRequiredPublicText({ displayName: "Linear", recipientUserId: null })).toBe(+ "Linear needs to be connected to continue, but the sign-in link couldn't be sent privately.",);});});@@ -49,59 +53,98 @@ describe("buildAuthCompletedText", () => {});it("appends an optional reason in parentheses", () => {+ expect(+ buildAuthCompletedText({+ displayName: "Linear",+ outcome: "timed-out",+ reason: "the code expired",+ }),+ ).toBe(":x: Linear authorization timed-out (the code expired)");+ });++ it("reports a cancelled sign-in plainly", () => {expect(buildAuthCompletedText({displayName: "Linear",outcome: "declined",- reason: "user declined consent",+ reason: "Cancelled because a new message arrived.",}),- ).toBe(":x: Linear authorization declined (user declined consent)");+ ).toBe("Linear sign-in cancelled");});});describe("buildAuthEphemeralBlocks", () => {- it("produces an actions block with a link button to the challenge URL", () => {+ it("names the service and why before a Connect button to the challenge URL", () => {+ expect(+ buildAuthEphemeralBlocks({+ displayName: "Linear",+ url: "https://connect.example.com/authorize/sca_abc",+ }),+ ).toEqual([+ {+ type: "section",+ text: {+ type: "mrkdwn",+ text: "*Connect Linear*\nTo continue, I need access to your Linear account. Only you can see this message.",+ },+ },+ {+ type: "actions",+ elements: [+ {+ type: "button",+ text: { type: "plain_text", text: "Connect Linear" },+ url: "https://connect.example.com/authorize/sca_abc",+ style: "primary",+ },+ ],+ },+ ]);+ });++ it("says the turn is paused and offers Cancel when the sign-in holds a turn", () => {+ const target = { channelId: "C01", threadTs: "1700000000.000001", turnId: "turn_0" };const blocks = buildAuthEphemeralBlocks({+ cancel: target,displayName: "Linear",url: "https://connect.example.com/authorize/sca_abc",});- expect(blocks).toHaveLength(1);- const actions = blocks[0] as { type: string; elements: Array<Record<string, unknown>> };- expect(actions.type).toBe("actions");- expect(actions.elements).toHaveLength(1);- const button = actions.elements[0] as {- type: string;- text: { text: string };- url: string;- style: string;- };- expect(button.type).toBe("button");- expect(button.text.text).toBe("Sign in");- expect(button.url).toBe("https://connect.example.com/authorize/sca_abc");- expect(button.style).toBe("primary");+ expect((blocks[0] as { text: { text: string } }).text.text).toBe(+ "*Connect Linear*\nI've paused until you connect your Linear account or cancel. Only you can see this message.",+ );+ const cancel = (blocks[1] as { elements: Array<Record<string, unknown>> }).elements[1];+ expect(cancel).toMatchObject({+ action_id: SIGN_IN_CANCEL_ACTION_ID,+ text: { text: "Cancel", type: "plain_text" },+ });+ expect(parseSignInCancelTarget(cancel?.value)).toEqual(target);});it("keeps the button label within Slack's limit for long display names", () => {const blocks = buildAuthEphemeralBlocks({- displayName: "x".repeat(63),+ displayName: "x".repeat(90),url: "https://connect.example.com/authorize/sca_abc",});- const actions = blocks[0] as { elements: Array<{ text: { text: string } }> };- const button = actions.elements[0] as { text: { text: string } };- expect(button.text.text).toBe("Sign in");- expect(button.text.text.length).toBeLessThanOrEqual(75);+ const actions = blocks[1] as { elements: Array<{ text: { text: string } }> };+ expect(actions.elements[0]!.text.text.length).toBeLessThanOrEqual(75);});- it("prepends a section with the device user code when one is provided", () => {+ it("adds the device code as a quiet hint after the button", () => {const blocks = buildAuthEphemeralBlocks({displayName: "Notion",url: "https://connect.example.com/authorize/sca_abc",userCode: "OTB-DGO",});- expect(blocks).toHaveLength(2);- const section = blocks[0] as { type: string; text: { text: string } };- expect(section.type).toBe("section");- expect(section.text.text).toBe("Use code `OTB-DGO` when prompted.");- expect((blocks[1] as { type: string }).type).toBe("actions");+ expect(blocks.map((block) => (block as { type: string }).type)).toEqual([+ "section",+ "actions",+ "context",+ ]);+ expect(blocks[2]).toEqual({+ type: "context",+ elements: [+ { type: "mrkdwn", text: "If Notion asks for a confirmation code, enter `OTB-DGO`." },+ ],+ });});});diff --git a/packages/eve/src/public/channels/slack/connections.ts b/packages/eve/src/public/channels/slack/connections.tsindex bbf51e629..76ed462cf 100644--- a/packages/eve/src/public/channels/slack/connections.ts+++ b/packages/eve/src/public/channels/slack/connections.ts@@ -16,8 +16,12 @@* surface the outcome (`authorized` / `declined` / `failed` /* `timed-out`).*/-import type { ConnectionAuthorizationOutcome } from "#protocol/message.js";+import { truncatePlainText } from "#public/channels/slack/limits.js";+import {+ SIGN_IN_CANCEL_ACTION_ID,+ type SignInCancelTarget,+} from "#public/channels/slack/sign-in-cancel.js";import { displayProperName } from "#shared/display-name.js";export type { ConnectionAuthorizationOutcome };@@ -36,12 +40,12 @@ export function formatConnectionDisplayName(connectionName: string): string {*/export function buildAuthRequiredPublicText(input: {readonly displayName: string;- readonly hasRecipient: boolean;+ readonly recipientUserId: string | null;}): string {- if (!input.hasRecipient) {- return `Authorization required for ${input.displayName} (couldn't send the sign-in link privately)`;+ if (input.recipientUserId === null) {+ return `${input.displayName} needs to be connected to continue, but the sign-in link couldn't be sent privately.`;}- return `Connect with ${input.displayName} to continue`;+ return `Paused: waiting for <@${input.recipientUserId}> to connect ${input.displayName}…`;}/**@@ -57,39 +61,82 @@ export function buildAuthCompletedText(input: {if (input.outcome === "authorized") {return `:white_check_mark: ${input.displayName} connected`;}+ if (input.outcome === "declined") {+ return `${input.displayName} sign-in cancelled`;+ }const tail = input.reason !== undefined ? ` (${input.reason})` : "";return `:x: ${input.displayName} authorization ${input.outcome}${tail}`;}/**- * Block Kit blocks for the ephemeral "Sign in" link button.- * Device-code flows carry a `userCode` the user must enter after- * following the link, so it is rendered alongside the button. Slack- * ephemerals accept the same block list shape as regular messages so the- * helper returns blocks directly.+ * Block Kit blocks for the private sign-in prompt. It names the service and+ * why it is asking before the button, because the user may not remember which+ * request needed it. A device-code flow's `userCode` is a fallback the+ * provider shows only sometimes, so it is a quiet hint below the button.*/export function buildAuthEphemeralBlocks(input: {+ /** Lets the person cancel the held turn instead of connecting. */+ readonly cancel?: SignInCancelTarget;readonly displayName: string;readonly url: string;readonly userCode?: string;}): unknown[] {- const blocks: unknown[] = [];+ const blocks: unknown[] = [+ {+ type: "section",+ text: {+ type: "mrkdwn",+ text:+ input.cancel === undefined+ ? `*Connect ${input.displayName}*\nTo continue, I need access to your ${input.displayName} account. Only you can see this message.`+ : `*Connect ${input.displayName}*\nI've paused until you connect your ${input.displayName} account or cancel. Only you can see this message.`,+ },+ },+ {+ type: "actions",+ elements: [+ {+ type: "button",+ text: { type: "plain_text", text: truncatePlainText(`Connect ${input.displayName}`) },+ url: input.url,+ style: "primary",+ },+ ...(input.cancel === undefined+ ? []+ : [+ {+ type: "button",+ action_id: SIGN_IN_CANCEL_ACTION_ID,+ text: { type: "plain_text", text: "Cancel" },+ value: JSON.stringify(input.cancel),+ },+ ]),+ ],+ },+ ];if (input.userCode !== undefined && input.userCode.length > 0) {blocks.push({- type: "section",- text: { type: "mrkdwn", text: `Use code \`${input.userCode}\` when prompted.` },+ type: "context",+ elements: [+ {+ type: "mrkdwn",+ text: `If ${input.displayName} asks for a confirmation code, enter \`${input.userCode}\`.`,+ },+ ],});}- blocks.push({- type: "actions",- elements: [- {- type: "button",- text: { type: "plain_text", text: "Sign in" },- url: input.url,- style: "primary",- },- ],- });return blocks;}++/** Notification text for the private sign-in prompt, for clients that show no blocks. */+export function buildAuthEphemeralText(input: {+ readonly displayName: string;+ readonly url: string;+ readonly userCode?: string;+}): string {+ const code =+ input.userCode !== undefined && input.userCode.length > 0+ ? ` If asked for a confirmation code, enter ${input.userCode}.`+ : "";+ return `Connect your ${input.displayName} account to continue: ${input.url}${code}`;+}diff --git a/packages/eve/src/public/channels/slack/defaults.test.ts b/packages/eve/src/public/channels/slack/defaults.test.tsindex e3619bf9c..e1c9de9bf 100644--- a/packages/eve/src/public/channels/slack/defaults.test.ts+++ b/packages/eve/src/public/channels/slack/defaults.test.ts@@ -1,7 +1,8 @@import { describe, expect, it, vi } from "vitest";import type { SessionContext } from "#public/definitions/callback-context.js";-import { defaultEvents, defaultInputRequestedHandler } from "#public/channels/slack/defaults.js";+import { defaultInputRequestedHandler } from "#public/channels/slack/approval-cards.js";+import { defaultEvents } from "#public/channels/slack/defaults.js";import type { SlackChannelState, SlackEventContext } from "#public/channels/slack/slackChannel.js";import { captureLogRecords } from "#internal/testing/log-records.js";@@ -241,6 +242,7 @@ describe("defaultInputRequestedHandler private input requests", () => {it("previews the triggering message and updates the routed DM card after settlement", async () => {const { channel, post, postDirectMessage, request } = buildChannelStub({+ slackUsersByPrincipal: { "slack:T1:U_REVIEWER": "U_REVIEWER" },triggeringMessageTs: "111.333",triggeringUserId: "U_REVIEWER",});@@ -296,11 +298,63 @@ describe("defaultInputRequestedHandler private input requests", () => {blocks?: unknown[];};expect(JSON.stringify(update.blocks)).not.toContain("eve_input:route:");+ expect(JSON.stringify(update.blocks)).not.toContain("I've paused");+ expect(request).toHaveBeenCalledWith("chat.delete", { channel: "D123", ts: "dm2" });+ expect(request).toHaveBeenCalledWith("chat.update", {+ channel: "C123",+ text: "Approved by <@U_REVIEWER>.",+ ts: "ts1",+ });+ expect(channel.state.pendingApprovalCards).toEqual({});+ });++ it("retires a thread approval that resolves without a click, even when Slack fails", async () => {+ const logs = captureLogRecords();+ const { channel, post, request } = buildChannelStub();+ post+ .mockResolvedValueOnce({ id: "details-ts", raw: { ok: true } })+ .mockResolvedValueOnce({ id: "card-ts", raw: { ok: true } });++ await defaultInputRequestedHandler()(+ { requests: [approvalRequest()], sequence: 1, stepIndex: 0, turnId: "turn-1" },+ channel,+ sessionCtx,+ );+ expect(channel.state.pendingApprovalCards?.["approval-1"]).toMatchObject({+ detailsMessageTs: "details-ts",+ messageTs: "card-ts",+ });++ request.mockRejectedValueOnce(new Error("Slack unavailable"));+ await defaultEvents["input.resolved"]!(+ {+ resolutions: [{ kind: "tool-approval", outcome: "ignored", requestId: "approval-1" }],+ sequence: 2,+ stepIndex: 0,+ turnId: "turn-2",+ },+ channel,+ sessionCtx,+ );++ expect(request).toHaveBeenCalledWith(+ "chat.update",+ expect.objectContaining({+ channel: "C123",+ text: "Answered: No longer needed",+ ts: "card-ts",+ }),+ );+ expect(request).toHaveBeenCalledWith("chat.delete", { channel: "C123", ts: "details-ts" });+ expect(channel.state.pendingApprovalCards).toEqual({});+ expect(logs.records).toContainEqual(+ expect.objectContaining({ level: "error", message: "failed to retire approval message" }),+ );});});describe("defaultEvents approval lifecycle", () => {- it("sends candidate progress privately", async () => {+ it("posts no notice that would outlive a pending candidate", async () => {const { channel, postEphemeral } = buildChannelStub({slackUsersByPrincipal: { "slack:T1:U777": "U777" },});@@ -325,13 +379,10 @@ describe("defaultEvents approval lifecycle", () => {ctx,);- expect(postEphemeral).toHaveBeenCalledWith(- "U777",- "Checking whether you can respond to this approval…",- );+ expect(postEphemeral).not.toHaveBeenCalled();});- it("routes candidate progress from event identity instead of ambient auth", async () => {+ it("routes candidate feedback from event identity instead of ambient auth", async () => {const { channel, postEphemeral } = buildChannelStub({slackUsersByPrincipal: { "slack:T1:U777": "U777" },teamId: "T1",@@ -346,7 +397,8 @@ describe("defaultEvents approval lifecycle", () => {await defaultEvents["approval.candidate"]!({candidateId: "candidate-1",- outcome: "pending",+ outcome: "rejected",+ reason: "GitHub write access is required.",requestId: "approval-1",responderPrincipalId: "slack:T1:U777",sequence: 1,@@ -357,10 +409,7 @@ describe("defaultEvents approval lifecycle", () => {wrongAmbientUser,);- expect(postEphemeral).toHaveBeenCalledWith(- "U777",- "Checking whether you can respond to this approval…",- );+ expect(postEphemeral).toHaveBeenCalledWith("U777", "GitHub write access is required.");});it("delivers an immediate rejection through the responder mapping", async () => {@@ -526,12 +575,16 @@ describe("defaultEvents authorization.required", () => {expect(post).toHaveBeenCalledTimes(1);const publicText = post.mock.calls[0]?.[0] as string;- expect(publicText).toBe("Connect with Notion to continue");+ expect(publicText).toBe("Paused: waiting for <@U777> to connect Notion…");expect(publicText).not.toContain("https://");expect(postEphemeral).toHaveBeenCalledTimes(1);expect(postEphemeral.mock.calls[0]?.[0]).toBe("U777");const message = postEphemeral.mock.calls[0]?.[1] as { text: string; blocks: unknown[] };expect(message.text).toContain("https://connect.example.com/a/sca_1");+ // The sign-in holds the turn, so the prompt can cancel it.+ expect(JSON.stringify(message.blocks)).toContain(+ JSON.stringify(JSON.stringify({ channelId: "C123", threadTs: "111.222", turnId: "turn_0" })),+ );expect(channel.state.pendingAuthMessageTs).toEqual({ notion: "ts1" });});@@ -546,6 +599,8 @@ describe("defaultEvents authorization.required", () => {expect(post).not.toHaveBeenCalled();expect(postEphemeral).toHaveBeenCalledTimes(1);+ // A responder's sign-in belongs to an approval, not a turn it could cancel.+ expect(JSON.stringify(postEphemeral.mock.calls[0]?.[1])).not.toContain("eve_sign_in:cancel");});it("targets the event principal instead of the current caller or stale channel state", async () => {@@ -608,7 +663,9 @@ describe("defaultEvents authorization.required", () => {const message = postEphemeral.mock.calls[0]?.[1] as { text: string; blocks: unknown[] };expect(JSON.stringify(message.blocks)).toContain("OTB-DGO");- expect(message.text).toContain("(code: OTB-DGO)");+ expect(message.text).toBe(+ "Connect your Notion account to continue: https://connect.example.com/a/sca_1 If asked for a confirmation code, enter OTB-DGO.",+ );});it("renders the challenge displayName instead of the title-cased connection name", async () => {@@ -620,9 +677,11 @@ describe("defaultEvents authorization.required", () => {sessionCtx,);- expect(post.mock.calls[0]?.[0]).toBe("Connect with Notion Workspace to continue");+ expect(post.mock.calls[0]?.[0]).toBe(+ "Paused: waiting for <@U777> to connect Notion Workspace…",+ );const message = postEphemeral.mock.calls[0]?.[1] as { text: string };- expect(message.text).toContain("Sign in with Notion Workspace");+ expect(message.text).toContain("Connect your Notion Workspace account");});it("posts a link-free public status when the principal has no Slack user", async () => {@@ -634,7 +693,7 @@ describe("defaultEvents authorization.required", () => {expect(post).toHaveBeenCalledTimes(1);const publicText = post.mock.calls[0]?.[0] as string;expect(publicText).toBe(- "Authorization required for Notion (couldn't send the sign-in link privately)",+ "Notion needs to be connected to continue, but the sign-in link couldn't be sent privately.",);expect(publicText).not.toContain("https://");expect(channel.state.pendingAuthMessageTs).toEqual({ notion: "ts1" });@@ -649,7 +708,7 @@ describe("defaultEvents authorization.required", () => {expect(post).toHaveBeenCalledTimes(1);const publicText = post.mock.calls[0]?.[0] as string;- expect(publicText).toBe("Connect with Notion to continue");+ expect(publicText).toBe("Paused: waiting for <@U777> to connect Notion…");expect(publicText).not.toContain("https://");expect(channel.state.pendingAuthMessageTs).toEqual({ notion: "ts1" });expect(logs.records).toContainEqual(diff --git a/packages/eve/src/public/channels/slack/defaults.ts b/packages/eve/src/public/channels/slack/defaults.tsindex a8c531d62..9a88feb8f 100644--- a/packages/eve/src/public/channels/slack/defaults.ts+++ b/packages/eve/src/public/channels/slack/defaults.ts@@ -1,44 +1,28 @@import { workingTaskNames } from "#channel/task-card.js";import type { SessionAuthContext } from "#channel/types.js";-import { createLogger, extractErrorId, formatErrorHint, logError } from "#internal/logging.js";+import { createLogger, extractErrorId, formatErrorHint } from "#internal/logging.js";import { describeActionRequests, waitingOnTasks } from "#public/channels/slack/action-status.js";import { isTaskControlTool } from "#protocol/task-tools.js";-import {- buildSlackAuthContext,- slackUserIdForPrincipal,- slackUserIdFromAuthContext,-} from "#public/channels/slack/auth.js";+import { buildSlackAuthContext, slackUserIdForPrincipal } from "#public/channels/slack/auth.js";import {buildAuthCompletedText,buildAuthEphemeralBlocks,+ buildAuthEphemeralText,buildAuthRequiredPublicText,formatConnectionDisplayName,type ConnectionAuthorizationOutcome,} from "#public/channels/slack/connections.js";-import {- buildAnsweredBlocks,- decodeHitlActionId,- renderInputRequestPostParts,- type SlackInputRequestPostPart,-} from "#public/channels/slack/hitl.js";import type { SlackMessage } from "#public/channels/slack/inbound.js";-import { deliverPrivateInputRequest } from "#public/channels/slack/private-approval-delivery.js";+import { approvalEvents } from "#public/channels/slack/approval-cards.js";import { deliverCompletedSlackReply } from "#public/channels/slack/reply-delivery.js";-import {- SLACK_MAX_BLOCKS_PER_MESSAGE,- truncateMessageText,- truncateTypingStatus,-} from "#public/channels/slack/limits.js";+import { truncateTypingStatus } from "#public/channels/slack/limits.js";import type {- SlackApprovalChannelResolver,SlackChannelInternalEvents,SlackChannelState,SlackContext,- SlackEventContext,SlackMentionResult,} from "#public/channels/slack/slackChannel.js";-import type { InputRequest } from "#shared/input.js";const log = createLogger("slack.defaults");const REASONING_TYPING_REFRESH_INTERVAL_MS = 5_000;@@ -124,22 +108,6 @@ function formatSemanticErrorReply(input: {].join("\n");}-function blockContainsRequestAction(block: unknown, requestId: string): boolean {- if (typeof block !== "object" || block === null) return false;- const candidate = block as { actions?: unknown; elements?: unknown };- return [candidate.actions, candidate.elements].some(- (entries) =>- Array.isArray(entries) &&- entries.some((entry) => {- if (typeof entry !== "object" || entry === null) return false;- const actionId = (entry as { action_id?: unknown }).action_id;- return (- typeof actionId === "string" && decodeHitlActionId(actionId)?.requestId === requestId- );- }),- );-}-/*** Workspace-scoped projection of the Slack actor that produced* `message`, derived into a {@link SessionAuthContext}. Used by both@@ -157,6 +125,7 @@ export function defaultSlackAuth(return buildSlackAuthContext({channelId: ctx.slack.channelId,fullName: author.fullName,+ installationTeamId: message.installationTeamId,isBot: author.isBot,teamId: message.teamId,threadTs: ctx.slack.threadTs,@@ -192,123 +161,6 @@ function firstNonEmptyLine(text: string): string | undefined {return undefined;}-/**- * Default `input.requested` handler — renders each pending HITL- * request as Slack `block_actions`. Buttons by default; radio for- * ≤6-option select requests; static_select for >6-option select- * requests. Batches split into multiple posts when they would exceed- * Slack's 50-block message cap.- */-export function defaultInputRequestedHandler(- approvalChannel?: SlackApprovalChannelResolver,-): NonNullable<SlackChannelInternalEvents["input.requested"]> {- return async (data, channel, ctx) => {- const directMessageRequests: InputRequest[] = [];- const threadRequests: InputRequest[] = [];- for (const request of data.requests) {- const destination =- approvalChannel === undefined ? "thread" : await approvalChannel(request, ctx);- (destination === "direct-message" ? directMessageRequests : threadRequests).push(request);- }-- await postPublicInputRequests(threadRequests, channel);- for (const request of directMessageRequests) {- const reviewer =- slackUserIdFromAuthContext(ctx.session.auth.current) ?? channel.state.triggeringUserId;- if (!reviewer) {- log.warn("direct-message input request not delivered because no reviewer was resolved", {- requestId: request.requestId,- sessionId: ctx.session.id,- });- continue;- }- const card = await deliverPrivateInputRequest({- previewMessageTs: channel.state.triggeringMessageTs ?? channel.slack.threadTs,- request,- reviewer,- slack: channel.slack,- });- recordApprovalCards(channel.state, [request], card);- try {- await channel.thread.post(- `Waiting on ${request.kind === "tool-approval" ? "approval" : "a response"} from <@${reviewer}>…`,- );- } catch (error) {- logError(log, "failed to announce private input request", error, {- requestId: request.requestId,- sessionId: ctx.session.id,- });- }- }- };-}--async function postPublicInputRequests(- requests: readonly InputRequest[],- channel: SlackEventContext,-): Promise<void> {- for (const post of buildInputRequestPosts(requests)) {- const message = await channel.thread.post({ blocks: post.blocks, text: post.text });- recordApprovalCards(channel.state, post.requests, {- messageBlocks: post.blocks,- messageTs: message.id,- });- }-}--function recordApprovalCards(- state: SlackChannelState,- requests: readonly InputRequest[],- card: NonNullable<SlackChannelState["pendingApprovalCards"]>[string],-): void {- if (!card.messageTs) return;- const cards = { ...state.pendingApprovalCards };- for (const request of requests) {- if (request.kind === "tool-approval") cards[request.requestId] = card;- }- state.pendingApprovalCards = cards;-}--/**- * Groups HITL requests into `chat.postMessage` payloads that stay under- * Slack's block-count cap. Tool input details are posted before interactive- * approval controls so Slack's callback body cannot grow with the input.- */-function buildInputRequestPosts(- requests: readonly InputRequest[],-): Array<{ blocks: unknown[]; requests: InputRequest[]; text: string }> {- const details: Array<SlackInputRequestPostPart & { readonly request: InputRequest }> = [];- const controls: Array<SlackInputRequestPostPart & { readonly request: InputRequest }> = [];- for (const request of requests) {- const parts = renderInputRequestPostParts(request);- if (parts.details) details.push({ ...parts.details, request });- controls.push({ ...parts.controls, request });- }-- return [...groupInputRequestPostParts(details), ...groupInputRequestPostParts(controls)];-}--function groupInputRequestPostParts(- parts: readonly (SlackInputRequestPostPart & { readonly request: InputRequest })[],-): Array<{ blocks: unknown[]; requests: InputRequest[]; text: string }> {- const groups: Array<{ blocks: unknown[]; fallbacks: string[]; requests: InputRequest[] }> = [];- for (const part of parts) {- const current = groups.at(-1);- if (current && current.blocks.length + part.blocks.length <= SLACK_MAX_BLOCKS_PER_MESSAGE) {- current.blocks.push(...part.blocks);- current.fallbacks.push(part.text);- current.requests.push(part.request);- } else {- groups.push({ blocks: [...part.blocks], fallbacks: [part.text], requests: [part.request] });- }- }- return groups.map((group) => ({- blocks: group.blocks,- requests: group.requests,- text: truncateMessageText(group.fallbacks.join("\n")),- }));-}-/*** eve's default Slack event rendering: status lines, replies, errors, and the* connection-authorization flow. It is the innermost link of every channel's@@ -317,62 +169,7 @@ function groupInputRequestPostParts(* authored renderers cannot express.*/export const defaultEvents: SlackChannelInternalEvents = {- async "approval.candidate"(event, channel, _ctx) {- const userId = slackUserIdForPrincipal(channel.state, event.responderPrincipalId);- if (event.outcome === "pending" && userId !== undefined) {- await channel.thread.postEphemeral(- userId,- "Checking whether you can respond to this approval…",- );- return;- }- if (userId === undefined) return;- if (event.outcome === "rejected" || event.outcome === "failed") {- await channel.thread.postEphemeral(- userId,- event.reason ?? "We couldn’t verify your response. Please try again.",- );- }- },-- async "approval.settled"(event, channel, _ctx) {- const cards = channel.state.pendingApprovalCards ?? {};- const card = cards[event.requestId];- if (card === undefined) return;- const messageChannelId = card.messageChannelId ?? channel.state.channelId;- if (messageChannelId === null) return;- const answerLabel = event.outcome === "approved" ? "Approve" : "Cancel";- const userId = slackUserIdForPrincipal(channel.state, event.responderPrincipalId);- const blocks = card.messageBlocks.flatMap((block) => {- if (!blockContainsRequestAction(block, event.requestId)) return [block];- if (typeof block !== "object" || block === null) return [];- const candidate = block as Record<string, unknown>;- if (candidate.type !== "card") {- return buildAnsweredBlocks({ answerLabel, promptBlocks: [], userId });- }- const { actions: _actions, ...withoutActions } = candidate;- return buildAnsweredBlocks({- answerLabel,- promptBlocks: [withoutActions],- userId,- });- });- await channel.slack.request("chat.update", {- blocks,- channel: messageChannelId,- text: `Answered: ${answerLabel}`,- ts: card.messageTs,- });- const next = { ...cards };- delete next[event.requestId];- for (const [requestId, pendingCard] of Object.entries(next)) {- if (pendingCard.messageTs === card.messageTs) {- next[requestId] = { ...pendingCard, messageBlocks: blocks };- }- }- channel.state.pendingApprovalCards = next;- },-+ ...approvalEvents,async "turn.waiting"(event, channel, _ctx) {const working = workingTaskNames(channel.state.taskCards?.[event.turnId]?.turn);if (working.length > 0) await channel.thread.startTyping(waitingOnTasks(working));@@ -546,10 +343,7 @@ export const defaultEvents: SlackChannelInternalEvents = {// itself remains private.const pending = channel.state.pendingAuthMessageTs ?? {};if (event.candidateId === undefined && pending[event.name] === undefined) {- const publicText = buildAuthRequiredPublicText({- displayName,- hasRecipient: recipientUserId !== null,- });+ const publicText = buildAuthRequiredPublicText({ displayName, recipientUserId });try {const sent = await channel.thread.post(publicText);if (sent.id) {@@ -570,19 +364,22 @@ export const defaultEvents: SlackChannelInternalEvents = {// must only ever be visible to the person who started the sign-in, never posted into// the shared thread.if (recipientUserId && challengeUrl) {- const userCode = event.authorization?.userCode;+ const { channelId, threadTs } = channel.state;+ // The turn's own sign-in holds it, so the prompt can cancel that turn.+ const cancel =+ event.candidateId === undefined && channelId && threadTs && event.turnId+ ? { channelId, threadTs, turnId: event.turnId }+ : undefined;+ const prompt = {+ cancel,+ displayName,+ url: challengeUrl,+ userCode: event.authorization?.userCode,+ };try {await channel.thread.postEphemeral(recipientUserId, {- blocks: buildAuthEphemeralBlocks({- displayName,- url: challengeUrl,- userCode,- }),- // Fallback text mirrors the blocks: clients that render only the- // notification text still get everything needed to complete the flow.- text: userCode- ? `Sign in with ${displayName}: ${challengeUrl} (code: ${userCode})`- : `Sign in with ${displayName}: ${challengeUrl}`,+ blocks: buildAuthEphemeralBlocks(prompt),+ text: buildAuthEphemeralText(prompt),});} catch (error) {log.error("Slack auth ephemeral delivery failed", {diff --git a/packages/eve/src/public/channels/slack/hitl.test.ts b/packages/eve/src/public/channels/slack/hitl.test.tsindex 621a3a09a..c4e29a0d7 100644--- a/packages/eve/src/public/channels/slack/hitl.test.ts+++ b/packages/eve/src/public/channels/slack/hitl.test.ts@@ -220,6 +220,7 @@ describe("renderInputRequestBlocks", () => {expect(card).toMatchObject({type: "card",body: { type: "mrkdwn", text: "*Approve tool call: mongodb-mutate*" },+ subtext: { type: "mrkdwn", text: "I've paused until someone approves or cancels." },});expect(card.actions).toMatchObject([{diff --git a/packages/eve/src/public/channels/slack/hitl.ts b/packages/eve/src/public/channels/slack/hitl.tsindex ae70a87c3..47ed11880 100644--- a/packages/eve/src/public/channels/slack/hitl.ts+++ b/packages/eve/src/public/channels/slack/hitl.ts@@ -420,7 +420,7 @@ function renderInputRequestCardBlock(request: InputRequest,actionId: string,): Record<string, unknown> {- return {+ const card: Record<string, unknown> = {type: "card",body: {type: "mrkdwn",@@ -429,6 +429,11 @@ function renderInputRequestCardBlock(},actions: cardButtonOptions(request).map((opt, index) => buildCardButton(opt, actionId, index)),};+ // A tool approval holds the turn until someone acts, so say so on the card.+ if (isApprovalRequest(request)) {+ card.subtext = { type: "mrkdwn", text: "I've paused until someone approves or cancels." };+ }+ return card;}function cardButtonOptions(request: InputRequest): CardButtonOption[] {diff --git a/packages/eve/src/public/channels/slack/inbound.ts b/packages/eve/src/public/channels/slack/inbound.tsindex 588bcae9a..7f8008be4 100644--- a/packages/eve/src/public/channels/slack/inbound.ts+++ b/packages/eve/src/public/channels/slack/inbound.ts@@ -73,6 +73,11 @@ export interface SlackMessage {readonly channelId: string;/** Slack team id, when the envelope carried one. */readonly teamId: string | undefined;+ /**+ * Workspace whose app installation received the message, when Slack reported+ * one. eve pairs it with the author's user id to identify the author.+ */+ readonly installationTeamId?: string;/** Author of the message. May be `undefined` for system events. */readonly author: SlackAuthor | undefined;/** File / image attachments on the inbound message. */@@ -181,7 +186,7 @@ export function parseAppMentionEvent(envelope: SlackEventCallback): SlackMessageif (envelope.type !== "event_callback") return null;const event = envelope.event;if (!event || event.type !== "app_mention") return null;- return buildSlackMessage(event as SlackAppMentionEvent, envelope.team_id);+ return buildSlackMessage(event as SlackAppMentionEvent, envelope);}/**@@ -250,7 +255,7 @@ export function parseMessageEvent(envelope: SlackEventCallback): SlackMessage |if (!event || event.type !== "message") return null;const message = event as SlackMessageEvent;if (!isPostedMessageSubtype(message.subtype)) return null;- return buildSlackMessage(message, envelope.team_id);+ return buildSlackMessage(message, envelope);}// Change events such as `message_changed` carry the edit's own `ts` and nest@@ -275,7 +280,7 @@ export function parseDirectMessageEvent(envelope: SlackEventCallback): SlackMessif (message.channel_type !== "im") return null;if (!isHumanMessage(message)) return null;- return buildSlackMessage(message, envelope.team_id);+ return buildSlackMessage(message, envelope);}function isHumanMessage(message: SlackMessageEvent): boolean {@@ -291,6 +296,7 @@ function isHumanMessage(message: SlackMessageEvent): boolean {export function slackMessageFromWebhookPayload(payload: SlackAppMentionPayload | SlackDirectMessagePayload,+ installationTeamId?: string,): SlackMessage | null {if (payload.kind === "direct_message") {if (@@ -312,6 +318,7 @@ export function slackMessageFromWebhookPayload(threadTs: payload.threadTs,channelId: payload.channelId,teamId: payload.teamId,+ installationTeamId,author: parsePayloadAuthor(payload),attachments: parsePayloadAttachments(payload.files),raw: payload.raw,@@ -320,7 +327,7 @@ export function slackMessageFromWebhookPayload(function buildSlackMessage(event: SlackAppMentionEvent | SlackMessageEvent,- envelopeTeamId: string | undefined,+ envelope: SlackEventCallback,): SlackMessage | null {const channelId = typeof event.channel === "string" ? event.channel : "";const ts = typeof event.ts === "string" ? event.ts : "";@@ -329,7 +336,8 @@ function buildSlackMessage(const topLevelText = typeof event.text === "string" ? event.text : "";const text = resolveSlackInboundMrkdwn(topLevelText, event as Record<string, unknown>);const threadTs = typeof event.thread_ts === "string" ? event.thread_ts : ts;- const teamId = typeof envelopeTeamId === "string" ? envelopeTeamId : undefined;+ const teamId = typeof envelope.team_id === "string" ? envelope.team_id : undefined;+ const installationTeamId = slackEventInstallationTeamId(envelope);return {text,@@ -338,6 +346,7 @@ function buildSlackMessage(threadTs,channelId,teamId,+ installationTeamId,author: parseAuthor(event),attachments: parseAttachments(event.files),raw: event as Record<string, unknown>,diff --git a/packages/eve/src/public/channels/slack/input-response.ts b/packages/eve/src/public/channels/slack/input-response.tsindex 5c0476506..92814af6f 100644--- a/packages/eve/src/public/channels/slack/input-response.ts+++ b/packages/eve/src/public/channels/slack/input-response.ts@@ -25,6 +25,7 @@ export async function authorizeInputResponse(input: {}): Promise<SlackInputResponseResult> {const defaultAuth = buildSlackAuthContext({channelId: input.channelId,+ installationTeamId: input.installationTeamId,teamId: input.teamId,threadTs: input.threadTs,userId: input.submission.user.id,diff --git a/packages/eve/src/public/channels/slack/interactions.ts b/packages/eve/src/public/channels/slack/interactions.tsindex d9c658b5d..6cbfadf98 100644--- a/packages/eve/src/public/channels/slack/interactions.ts+++ b/packages/eve/src/public/channels/slack/interactions.ts@@ -23,6 +23,7 @@ import {slackContinuationToken,} from "#public/channels/slack/api.js";import { buildSlackAuthContext } from "#public/channels/slack/auth.js";+import { dispatchSignInCancel } from "#public/channels/slack/sign-in-cancel.js";import {buildFreeformModalView,decodeFreeformHitlActionId,@@ -292,6 +293,8 @@ export async function handleInteractionPost(return ack;}+ if (dispatchSignInCancel(payload.raw, ctx)) return ack;+const interaction = parseBlockActionsPayload(payload);if (!interaction) return ack;@@ -344,6 +347,7 @@ export async function handleInteractionPost(address: slackContinuationToken(interaction.channelId, interaction.threadTs),defaultAuth: buildSlackAuthContext({channelId: interaction.channelId,+ installationTeamId: interaction.installationTeamId,teamId: interaction.teamId,threadTs: interaction.threadTs,userId: actionUser.id,diff --git a/packages/eve/src/public/channels/slack/private-approval-delivery.ts b/packages/eve/src/public/channels/slack/private-approval-delivery.tsindex bf89a1700..9f083bbdb 100644--- a/packages/eve/src/public/channels/slack/private-approval-delivery.ts+++ b/packages/eve/src/public/channels/slack/private-approval-delivery.ts@@ -39,16 +39,16 @@ export async function deliverPrivateInputRequest(input: {}),);}+ let detailsMessageTs: string | undefined;if (parts.details !== undefined) {- postedMessageIds.push(- await postMessage(input.slack, {- blocks: parts.details.blocks,- channel: messageChannelId,- text: parts.details.text,- unfurl_links: false,- unfurl_media: false,- }),- );+ detailsMessageTs = await postMessage(input.slack, {+ blocks: parts.details.blocks,+ channel: messageChannelId,+ text: parts.details.text,+ unfurl_links: false,+ unfurl_media: false,+ });+ postedMessageIds.push(detailsMessageTs);}const messageTs = await postMessage(input.slack, {blocks: parts.controls.blocks,@@ -58,7 +58,12 @@ export async function deliverPrivateInputRequest(input: {unfurl_media: false,});postedMessageIds.push(messageTs);- return { messageBlocks: parts.controls.blocks, messageChannelId, messageTs };+ return {+ detailsMessageTs,+ messageBlocks: parts.controls.blocks,+ messageChannelId,+ messageTs,+ };} catch (error) {await Promise.allSettled(postedMessageIds.map(async (ts) => {diff --git a/packages/eve/src/public/channels/slack/sign-in-cancel.ts b/packages/eve/src/public/channels/slack/sign-in-cancel.tsnew file mode 100644index 000000000..cf7286c11--- /dev/null+++ b/packages/eve/src/public/channels/slack/sign-in-cancel.ts@@ -0,0 +1,101 @@+import type { ChannelFrom } from "#channel/channel-operations.js";+import { createLogger } from "#internal/logging.js";+import type { SlackChannelState } from "#public/channels/slack/slackChannel.js";+import { slackContinuationToken } from "#public/channels/slack/api.js";++const log = createLogger("slack.interactions");++/** `action_id` of the sign-in prompt's Cancel button; outside eve's `eve_input:` HITL prefix. */+export const SIGN_IN_CANCEL_ACTION_ID = "eve_sign_in:cancel";++/** The held turn a sign-in prompt's Cancel button cancels. */+export interface SignInCancelTarget {+ readonly channelId: string;+ readonly threadTs: string;+ readonly turnId: string;+}++/** Reads a Cancel click's target from a sign-in prompt's button value. */+export function parseSignInCancelTarget(value: unknown): SignInCancelTarget | undefined {+ if (typeof value !== "string") return undefined;+ let parsed: unknown;+ try {+ parsed = JSON.parse(value);+ } catch {+ return undefined;+ }+ if (typeof parsed !== "object" || parsed === null) return undefined;+ const { channelId, threadTs, turnId } = parsed as Record<string, unknown>;+ if (typeof channelId !== "string" || typeof threadTs !== "string" || typeof turnId !== "string") {+ return undefined;+ }+ return { channelId, threadTs, turnId };+}++/**+ * Handles a click on a sign-in prompt's Cancel button in the background.+ * Returns false for any other interaction.+ */+export function dispatchSignInCancel(+ raw: Record<string, unknown>,+ ctx: {+ readonly from: ChannelFrom<SlackChannelState>;+ readonly waitUntil: (task: Promise<unknown>) => void;+ },+): boolean {+ const cancel = readSignInCancel(raw);+ if (cancel === undefined) return false;+ ctx.waitUntil(cancelHeldSignIn(cancel, ctx));+ return true;+}++interface SignInCancel {+ readonly responseUrl: string | undefined;+ readonly target: SignInCancelTarget;+}++/**+ * Reads a click on a sign-in prompt's Cancel button. The prompt is ephemeral,+ * so the payload has no message; its target rides on the button value.+ */+function readSignInCancel(raw: Record<string, unknown>): SignInCancel | undefined {+ const actions = Array.isArray(raw.actions) ? (raw.actions as unknown[]) : [];+ const action = actions.find(+ (entry): entry is Record<string, unknown> =>+ isObjectRecord(entry) && entry.action_id === SIGN_IN_CANCEL_ACTION_ID,+ );+ const target = parseSignInCancelTarget(action?.value);+ if (target === undefined) return undefined;+ return {+ responseUrl: typeof raw.response_url === "string" ? raw.response_url : undefined,+ target,+ };+}++/** Cancels the turn held on the sign-in, then removes the private prompt. */+async function cancelHeldSignIn(+ cancel: SignInCancel,+ ctx: { readonly from: ChannelFrom<SlackChannelState> },+): Promise<void> {+ const { channelId, threadTs, turnId } = cancel.target;+ try {+ await ctx.from(slackContinuationToken(channelId, threadTs)).cancel({ turnId });+ } catch (error) {+ log.error("sign-in cancel delivery failed", { error });+ return;+ }+ if (cancel.responseUrl === undefined) return;+ try {+ await fetch(cancel.responseUrl, {+ body: JSON.stringify({ delete_original: true }),+ headers: { "content-type": "application/json" },+ method: "POST",+ });+ } catch (error) {+ log.warn("failed to remove the cancelled sign-in prompt", { error });+ }+}++function isObjectRecord(value: unknown): value is Record<string, unknown> {+ return typeof value === "object" && value !== null && !Array.isArray(value);+}diff --git a/packages/eve/src/public/channels/slack/slackChannel.test.ts b/packages/eve/src/public/channels/slack/slackChannel.test.tsindex 1dd452d55..b9de190a9 100644--- a/packages/eve/src/public/channels/slack/slackChannel.test.ts+++ b/packages/eve/src/public/channels/slack/slackChannel.test.ts@@ -1088,6 +1088,9 @@ describe("slackChannel() default event handlers", () => {THREAD_STATE,);const ctx = buildAdapterContext(adapter, stubAccessor());+ fetchMock.mockImplementationOnce(async () =>+ Response.json({ ok: true, ts: "1700000000.000900" }),+ );await callEvent(adapter,@@ -1208,6 +1211,7 @@ describe("slackChannel() default event handlers", () => {]);expect(ctx.state.pendingApprovalCards).toEqual({approval_abc123: {+ detailsMessageTs: "1700000000.000900",messageBlocks: controlsBody.blocks,messageTs: "1700000001.000001",},@@ -4310,7 +4314,7 @@ describe("slackChannel() HITL interaction pipeline", () => {expect(send).not.toHaveBeenCalled();});- it("keeps interaction auth on the actor workspace and credentials on the installation", async () => {+ it("gives a Slack Connect user one principal across messages and button clicks", async () => {const botToken = vi.fn((_context: { readonly teamId?: string }) => "xoxb-test");fetchMock.mockImplementation(async () =>@@ -4327,6 +4331,29 @@ describe("slackChannel() HITL interaction pipeline", () => {},});+ const { send: mentionSend } = await firePost(+ channel,+ buildSignedRequest({+ body: buildEventBody(+ {+ channel: "C01",+ event_ts: "1700000000.000001",+ text: "hello",+ ts: "1700000000.000001",+ type: "app_mention",+ user: "U01",+ },+ {+ authorizations: [{ is_bot: true, team_id: "T_INSTALLATION", user_id: "U_BOT" }],+ teamId: "T_ACTOR",+ },+ ),+ }),+ );+ expect(mentionSend.mock.calls[0]?.[1]).toMatchObject({+ auth: { issuer: "slack:T_INSTALLATION", principalId: "slack:T_INSTALLATION:U01" },+ });+const { send } = await firePost(channel,buildSignedInteractionRequest({@@ -4345,11 +4372,48 @@ describe("slackChannel() HITL interaction pipeline", () => {expect(botToken).toHaveBeenCalledWith({ teamId: "T_INSTALLATION" });expect(send.mock.calls[0]?.[1]).toMatchObject({- auth: { principalId: "slack:T_ACTOR:U01" },+ auth: { issuer: "slack:T_INSTALLATION", principalId: "slack:T_INSTALLATION:U01" },state: { installationTeamId: "T_INSTALLATION", teamId: "T_ACTOR" },});});+ it("cancels the held turn and removes the private prompt from a sign-in Cancel click", async () => {+ fetchMock.mockImplementation(async () => new Response("ok"));+ const channel = slackChannel({ credentials: { botToken: "xoxb-test" } });++ const { cancel, send } = await firePost(+ channel,+ buildSignedInteractionRequest({+ type: "block_actions",+ team: { id: "T01" },+ user: { id: "U01", username: "ada", team_id: "T01" },+ container: { type: "message", is_ephemeral: true, channel_id: "C01" },+ response_url: "https://hooks.slack.com/actions/T01/1/abc",+ actions: [+ {+ action_id: "eve_sign_in:cancel",+ text: { type: "plain_text", text: "Cancel" },+ value: JSON.stringify({+ channelId: "C01",+ threadTs: "1700000000.000001",+ turnId: "turn_0",+ }),+ },+ ],+ }),+ );++ expect(cancel).toHaveBeenCalledWith({+ continuationToken: "C01:1700000000.000001",+ turnId: "turn_0",+ });+ expect(send).not.toHaveBeenCalled();+ const removal = fetchMock.mock.calls.find(+ ([url]) => String(url) === "https://hooks.slack.com/actions/T01/1/abc",+ );+ expect(JSON.parse(String(removal?.[1]?.body))).toEqual({ delete_original: true });+ });+it("resets the interaction's Slack thread from onInteraction", async () => {const channel = slackChannel({credentials: { botToken: "xoxb-test" },@@ -4432,8 +4496,8 @@ describe("slackChannel() HITL interaction pipeline", () => {user_name: "ada",},authenticator: "slack-webhook",- issuer: "slack:T_ACTOR",- principalId: "slack:T_ACTOR:U_APPROVER",+ issuer: "slack:T_INSTALLATION",+ principalId: "slack:T_INSTALLATION:U_APPROVER",principalType: "user",},inputResponses: [{ optionId: "approve", requestId: "approval_abc123" }],@@ -4734,19 +4798,6 @@ describe("slackChannel() HITL interaction pipeline", () => {expect(ctx.state.slackUsersByPrincipal).toEqual({ "slack:T01:U_APPROVER": "U_APPROVER" });- await callEvent(- adapter,- makeEvent("approval.candidate", {- candidateId: "candidate-1",- outcome: "pending",- requestId: "approval_abc123",- responderPrincipalId: "slack:T01:U_APPROVER",- sequence: 1,- stepIndex: 0,- turnId: "turn-1",- }),- ctx,- );await callEvent(adapter,makeEvent("approval.candidate", {@@ -4766,18 +4817,13 @@ describe("slackChannel() HITL interaction pipeline", () => {parseSlackRequestBody(init),);expect(ephemeralBodies).toEqual([- expect.objectContaining({- channel: "C01",- markdown_text: "Checking whether you can respond to this approval…",- user: "U_APPROVER",- }),expect.objectContaining({channel: "C01",markdown_text: "Test policy: all approval responses are rejected.",user: "U_APPROVER",}),]);- expect(slackOperations(fetchMock)).toEqual(["chat.postEphemeral", "chat.postEphemeral"]);+ expect(slackOperations(fetchMock)).toEqual(["chat.postEphemeral"]);});it("keeps HITL pending when the input-response hook rejects or throws", async () => {@@ -5082,8 +5128,8 @@ describe("slackChannel() HITL interaction pipeline", () => {user_name: "grace",},authenticator: "slack-webhook",- issuer: "slack:T_ACTOR",- principalId: "slack:T_ACTOR:U_SUBMITTER",+ issuer: "slack:T_INSTALLATION",+ principalId: "slack:T_INSTALLATION:U_SUBMITTER",principalType: "user",},inputResponses: [{ requestId: "call_abc123", text: "approved with context" }],diff --git a/packages/eve/src/public/channels/slack/slackChannel.ts b/packages/eve/src/public/channels/slack/slackChannel.tsindex c8ddbf173..01166d1e3 100644--- a/packages/eve/src/public/channels/slack/slackChannel.ts+++ b/packages/eve/src/public/channels/slack/slackChannel.ts@@ -41,9 +41,9 @@ import {collectInboundFileParts,createSlackFetchFile,} from "#public/channels/slack/attachments.js";+import { defaultInputRequestedHandler } from "#public/channels/slack/approval-cards.js";import {defaultEvents,- defaultInputRequestedHandler,defaultOnMessage,defaultReceived,} from "#public/channels/slack/defaults.js";@@ -199,6 +199,10 @@ type SlackSessionFailedHandler = (* `JSON.stringify` / `JSON.parse`.*/export interface SlackPendingApprovalCard {+ /** Thread post announcing a direct-message approval; updated with the outcome. */+ readonly announcementTs?: string;+ /** Separate tool-input details post; deleted once none of its approvals are pending. */+ readonly detailsMessageTs?: string;/** Channel containing the approval card; differs from the session channel for DM delivery. */readonly messageChannelId?: string;readonly messageBlocks: readonly unknown[];@@ -1117,7 +1121,7 @@ async function handleEventPost(input: {if (payload.kind === "app_mention" || payload.kind === "direct_message") {const kind = payload.kind;- const message = slackMessageFromWebhookPayload(payload);+ const message = slackMessageFromWebhookPayload(payload, installationTeamId);if (message !== null && !isSelfAuthoredSlackMessage({ appId, botUserId }, message)) {const specialized = kind === "app_mention" ? config.onAppMention : config.onDirectMessage;const handler = specialized ?? config.onMessage;@@ -1232,6 +1236,7 @@ async function dispatchSlackMessage(input: {: buildSlackAuthContext({channelId: input.message.channelId,fullName: author.fullName,+ installationTeamId: input.installationTeamId,isBot: author.isBot,teamId: input.message.teamId,threadTs: input.message.threadTs,diff --git a/packages/eve/src/runtime/connections/scoped-authorization.ts b/packages/eve/src/runtime/connections/scoped-authorization.tsindex d541a9162..38fe76f8e 100644--- a/packages/eve/src/runtime/connections/scoped-authorization.ts+++ b/packages/eve/src/runtime/connections/scoped-authorization.ts@@ -90,6 +90,10 @@ export function createAuthorizationExecution(return {complete,+ /** Whether this execution completed a sign-in for `scoped`. */+ isJustAuthorized(scoped: ScopedAuthorization): boolean {+ return justAuthorized.has(scoped.instanceId ?? scoped.scope);+ },async getToken(scoped: ScopedAuthorization): Promise<TokenResult> {await complete(scoped);try {@@ -325,8 +329,7 @@ async function startScopedAuthorization(const interactive = authorization as InteractiveAuthorizationDefinition<JsonValue>;const principal = resolveScopedPrincipal(input);- const principalId = (input.boundResponder ?? contextStorage.getStore()?.get(AuthKey))- ?.principalId;+ const requester = input.boundResponder ?? contextStorage.getStore()?.get(AuthKey) ?? undefined;const callbackUrl = resolveAuthorizationCallbackUrl({authorization,callbackUrl: attempt.hookUrl,@@ -340,11 +343,13 @@ async function startScopedAuthorization({attemptId: attempt.attemptId,challenge: stampChallengeDisplayName(challenge, authorization),+ grant: authorization.vercelConnect?.connector,hookUrl: callbackUrl,instanceId: input.instanceId,name: scope,principal,- principalId,+ principalId: requester?.principalId,+ requester,resume,},]);diff --git a/packages/eve/src/subagents/hitl-proxy.integration.test.ts b/packages/eve/src/subagents/hitl-proxy.integration.test.tsindex 076a83296..52e044a99 100644--- a/packages/eve/src/subagents/hitl-proxy.integration.test.ts+++ b/packages/eve/src/subagents/hitl-proxy.integration.test.ts@@ -269,6 +269,7 @@ describe("subagent HITL proxy → Slack-style text-approve regression (Finding #expect(events.slice(1)).toEqual([{data: {+ on: "input",sequence: 3,turnId: "turn_3",usage: { cacheReadTokens: 0, cacheWriteTokens: 0, inputTokens: 0, outputTokens: 0 },diff --git a/packages/eve/src/subagents/hitl-proxy.ts b/packages/eve/src/subagents/hitl-proxy.tsindex 69c5e9036..86e37412e 100644--- a/packages/eve/src/subagents/hitl-proxy.ts+++ b/packages/eve/src/subagents/hitl-proxy.ts@@ -72,6 +72,7 @@ async function emitTurnWaiting(emit: HarnessEmitFn, session: HarnessSessionBase)const turn = getHarnessEmissionState(session.state);await emit(createTurnWaitingEvent({+ on: "input",sequence: turn.sequence,turnId: turn.turnId,usage: getSessionUsage(session),diff --git a/packages/eve/test/tui-client/tui-connection-auth-user.ts b/packages/eve/test/tui-client/tui-connection-auth-user.tsindex 7b3d19bc0..5261d22c9 100644--- a/packages/eve/test/tui-client/tui-connection-auth-user.ts+++ b/packages/eve/test/tui-client/tui-connection-auth-user.ts@@ -315,7 +315,12 @@ runEnvironment("tui-connection-auth-user", async ({ cleanup, target: resolveTargthrow new Error(`Follow-up session failed: ${event.data.code} ${event.data.message}`);}- if (event.type === "session.waiting" || event.type === "session.completed") {+ // The fresh sign-in holds the second principal's turn (`on: "input"`).+ if (+ event.type === "session.waiting" ||+ event.type === "session.completed" ||+ (event.type === "turn.waiting" && event.data.on === "input")+ ) {followupBoundary = event.type;break;}diff --git a/research/connection-execute.md b/research/connection-execute.mdindex a7f14dd31..bfabf84cc 100644--- a/research/connection-execute.md+++ b/research/connection-execute.md@@ -1,7 +1,7 @@---issue: TBDstatus: proposed-last_updated: "2026-09-30"+last_updated: "2026-10-01"---# Cache-stable connection tools@@ -135,9 +135,11 @@ connection_execute({ connection, tool, input }) ← model-visible action`minimum`, `maximum`, and `pattern` as comments.- An output with no schema renders as `Promise<unknown>`.- The raw JSON Schema is not repeated, so each schema is paid for once.-- **Connection failures.** A connection that needs sign-in starts- authorization, as today. A connection that fails is returned in- `unavailable` with its `error`.+- **Connection failures.** A plain search never prompts. A connection that needs+ sign-in is returned in `unavailable` with `requiresSignIn: true`.+ `connection_search({ connection, signIn: true })` starts authorization for+ that one connection and returns its tools once the user signs in. A connection+ that fails is returned in `unavailable` with its `error`.- **Ranking.** Prefix word matching, weighted by field: tool name, thenconnection name, input property names, tool description, and lastproperty descriptions and the connection description. The ranking candiff --git a/research/eve-tasks.md b/research/eve-tasks.mdindex 39ccba791..3bb11bc48 100644--- a/research/eve-tasks.md+++ b/research/eve-tasks.md@@ -1,7 +1,7 @@---issue: https://github.com/vercel/eve/issues/1084status: draft-last_updated: "2026-09-28"+last_updated: "2026-10-01"---# eve tasks@@ -442,9 +442,9 @@ includes `ask_question`) asks a question or needs a sign-in, right after its `in`authorization.required`. `turn.waiting` is informational, never a responseboundary: the next `step.started` with the same `turnId` means the turn resumed, and only`turn.completed`, `turn.failed`, or `turn.cancelled` end it. `session.waiting` keeps one meaning,-the turn has ended and the session awaits another message, and carries no `turnId`. A tool-approval the turn requests still ends that turn, as today: `input.requested`, `turn.completed`,-then `session.waiting`. A root turn's held text step completes normally as+the turn has ended and the session awaits another message, and carries no `turnId`. A sign-in+or tool approval the turn raises itself holds it the same way, and that `turn.waiting` carries+`on: "input"`; see [held sign-in and approval](./held-sign-in-and-approval.md). A root turn's held text step completes normally as`"stop"`, and a person can keep writing. A child turn (its session has a caller) and a schedule'sturn (`ScheduleIdKey`, `harness/tool-loop.ts`) report their held text step as`finishReason: "tool-calls"`, which channels already skip, so the run posts once. History and@@ -452,7 +452,7 @@ traces keep the model's own finish reason.**Where a response ends.** `send()`, the helpers that collect a turn's events, and MCP stop at`session.waiting`, `session.completed`, or `session.failed`, and at `turn.waiting` only when-questions are pending in what they read, the way pending sign-ins already gate+questions are pending in what they read or it is on `"input"`, the way pending sign-ins already gate`session.waiting`. Then `result()` returns `status: "waiting"` with the questions, and`respond()` keeps reading the same open turn to its real end. Without pending questions,`turn.waiting` is not a boundary, so `send().result()` and an MCP `agent_get` see the final reply;diff --git a/research/held-sign-in-and-approval.md b/research/held-sign-in-and-approval.mdnew file mode 100644index 000000000..e4584e037--- /dev/null+++ b/research/held-sign-in-and-approval.md@@ -0,0 +1,76 @@+---+issue: https://github.com/vercel/eve/issues/4132+status: proposed+last_updated: "2026-10-01"+---++# Sign-ins and tool approvals hold the turn++## Problem++A turn that asks a question through `ctx.ask`, or waits on a task, stays+open: the stream emits `turn.waiting`, and the same turn resumes once the+answer arrives. A turn that needs a connection sign-in or a tool approval+does the opposite. It emits `turn.completed` and `session.waiting`, and the+callback or approval later starts a new turn. The same "waiting on a person"+state has two lifecycles, sign-in and approval prompts outlive the turn that+needed them, and nothing ends a prompt nobody answers.++## Behavior++A sign-in (`authorization.required`) or tool approval (`input.requested`+for a `tool-approval`) the turn itself raises holds the turn, exactly as a+task or `ctx.ask` does:++```+turn.started+ … tool call needs sign-in or approval …+authorization.required | input.requested+turn.waiting ← on: "input"; same turnId+ … the person acts …+authorization.completed | input.resolved+step.started ← same turnId, no new turn.started+ …+turn.completed+session.waiting+```++While the turn is held:++| Input | Result |+| ---------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |+| Sign-in callback for the held attempt | Resumes the held turn. |+| Approval answer (any responder the policy accepts) | Resumes the held turn. |+| Approval answer the response policy refuses | Keeps the turn held and emits `turn.waiting` again, so the responder's request ends; another responder can still answer. |+| Message from the turn's own person | Steers the turn and cancels the held request. A sign-in reports `authorization.completed` with `outcome: "declined"`; an approval resolves as `ignored`. The model reads the message with a note that the request was cancelled. |+| Message from anyone else | Queues until the held turn ends, as it does behind a task. |+| `session.cancel()` (for example a Slack **Cancel** button) | Cancels the turn and withdraws every request it held: sign-ins report `authorization.completed` `declined`, approvals `input.resolved` `cancelled`, and each held call gets a not-run result. |++There is no timeout: an answer, a steering message, or a cancel ends the+hold, as with tasks.++Sign-ins raised while settling an approval response policy (`candidateId`)+also hold the turn they run in.++## Where a response ends++Every `turn.waiting` says what the turn waits on: `on: "input"` when a+person must act (a sign-in, approval, or question, including ones proxied from+a workflow tool or subagent), and `on: "tasks"` when work the turn started is+still running. `"input"` wins when both apply. Clients stop reading at+`"input"`, which matters when the request was read earlier, such as a refused+approval answer or a resumed stream. Other human-in-the-loop paths, such as+the session-limit prompt, adopt `"input"` when they move to holding the turn.++## Channels++Slack's private sign-in prompt and approval cards say the agent is paused+until the person acts, and the turn's own sign-in prompt has a **Cancel** button+that cancels the held turn and removes the prompt. A responder's sign-in for+an approval has no Cancel button, since it belongs to the approval rather+than a turn the responder could cancel.++## Out of scope++- Sign-ins and questions inside tasks and workflow tools already hold the+ turn and are unchanged.