The Open Framework for Building Agents
[1/3] feat(eve): answer several pending prompts by text, one at a time (#4411)
Ben Pankow17d5b11bc458
diff --git a/.changeset/text-prompts-one-at-a-time.md b/.changeset/text-prompts-one-at-a-time.mdnew file mode 100644index 000000000..af8910bf4--- /dev/null+++ b/.changeset/text-prompts-one-at-a-time.md@@ -0,0 +1,7 @@+---+"eve": patch+---++When a turn waits on several questions or approvals, a typed reply now answers the first open one instead of none, so people can answer them one message at a time. Twilio, GitHub, Linear, and Chat SDK channels (including Linq and Photon) show one prompt at a time and post the next once it is answered. An approval raised alongside a workflow tool call such as `ask_question` is now requested after that call finishes, since it can't take effect before then.++On Twilio, GitHub, and Linear, the built-in prompt queue spans the `input.requested`, `input.resolved`, and `approval.settled` handlers. If you override any one of them, override all three, or the built-in handlers will post prompts twice or stop posting later ones. If you render prompts yourself from `input.requested`, make `input.resolved` and `approval.settled` no-ops.diff --git a/.changeset/typed-policy-approval.md b/.changeset/typed-policy-approval.mdnew file mode 100644index 000000000..e9dbfd29e--- /dev/null+++ b/.changeset/typed-policy-approval.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Typing `approve` or `cancel` now answers a tool approval that has an `approval.response` policy. The policy checks the person who typed the reply, just as it checks someone who presses a button. Before, the reply reached the model as an ordinary message and the approval stayed pending.diff --git a/docs/tools/human-in-the-loop.md b/docs/tools/human-in-the-loop.mdindex 6052b8ebf..61304c217 100644--- a/docs/tools/human-in-the-loop.md+++ b/docs/tools/human-in-the-loop.md@@ -219,7 +219,7 @@ Approvals and questions share one protocol: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.+For `ctx.ask()` questions from tools, a follow-up message answers the first open request, as described in [Several requests at once](#several-requests-at-once). 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.Each request includes a `kind` discriminator: `tool-approval`, `question`, or`session-limit`. Clients should use `kind` to choose behavior and presentation.@@ -234,6 +234,16 @@ If a tool is approved by another user, only the approved call runs with that useFor 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.+### Several requests at once++A turn can wait on more than one request, such as two `ctx.ask()` questions a workflow tool asks with `Promise.all`, or approvals for two tool calls the model made in one step. A follow-up message answers only the first open request: a [runtime limit](/docs/agent-config#runtime-limits) continuation prompt if one is open, and otherwise the request eve asked for first, in the order of its `input.requested` events.++The message answers that request when it matches one of its options, or when the request accepts free text. Otherwise the message is not an answer and follows the rules above. To answer several requests by text, send one message per request.++An approval for a tool call made in the same step as a workflow tool call, such as `ask_question`, is not requested until the workflow tool call finishes, because the approved call can't run before then. The person answers the question first, then sees the approval.++Channels that show only text show one request at a time, in this order, and post the next once the current one is answered, so a reply answers the request the person sees. Twilio, GitHub, Linear, and Chat SDK channels work this way. Chat SDK can't tell whether an adapter shows buttons, so every Chat SDK channel shows requests one at a time, including Linq, Photon, and adapters with buttons. Native channels with buttons, such as Slack, show every open request so a person can press any of them, and a typed reply still answers the first.+See [Sessions, runs & streaming](/docs/concepts/sessions-runs-and-streaming) for the full event and resume contract that this builds on.## Answering from a client or channeldiff --git a/packages/eve/src/channel/prompt-queue.test.ts b/packages/eve/src/channel/prompt-queue.test.tsnew file mode 100644index 000000000..667624595--- /dev/null+++ b/packages/eve/src/channel/prompt-queue.test.ts@@ -0,0 +1,94 @@+import { describe, expect, it, vi } from "vitest";++import { promptQueueEvents, type PromptQueueState } from "#channel/prompt-queue.js";+import { ContextContainer, contextStorage } from "#context/container.js";+import { enterSessionProjection, recordPublishedEvent } from "#harness/session-machine/current.js";+import type { UnstampedMessageStreamEvent } from "#protocol/message.js";+import type { InputRequest } from "#shared/input.js";++const AT = { sequence: 0, stepIndex: 0, turnId: "turn_0" };++function question(requestId: string): InputRequest {+ return {+ action: { callId: requestId, input: {}, kind: "tool-call", toolName: "ask" },+ display: "text",+ kind: "question",+ prompt: requestId,+ requestId,+ };+}++function requested(...requests: InputRequest[]): UnstampedMessageStreamEvent {+ return { data: { requests, ...AT }, type: "input.requested" };+}++function resolved(requestId: string): UnstampedMessageStreamEvent {+ return {+ data: { resolutions: [{ kind: "question", outcome: "answered", requestId }], ...AT },+ type: "input.resolved",+ };+}++/** Publishes `events` as a session does: each handler runs, then the session records it. */+async function publish(+ show: (channel: unknown, request: InputRequest) => Promise<boolean | void>,+ events: readonly UnstampedMessageStreamEvent[],+) {+ const handlers = promptQueueEvents(show) as Record<+ string,+ (data: unknown, channel: { state: PromptQueueState }) => Promise<void>+ >;+ const channel: { state: PromptQueueState } = { state: {} };+ const ctx = new ContextContainer();+ enterSessionProjection(ctx, undefined);+ await contextStorage.run(ctx, async () => {+ for (const event of events) {+ await handlers[event.type]?.("data" in event ? event.data : undefined, channel);+ recordPublishedEvent(ctx, event);+ }+ });+}++describe("promptQueueEvents", () => {+ it("shows a budget prompt ahead of a request already shown, then returns to it", async () => {+ const show = vi.fn(async (_channel: unknown, _request: InputRequest) => {});+ await publish(show, [+ requested(question("day")),+ requested({ ...question("budget"), kind: "session-limit" }),+ resolved("budget"),+ ]);+ expect(show.mock.calls.map(([, request]) => request.requestId)).toEqual([+ "day",+ "budget",+ "day",+ ]);+ });++ it("never shows a request again once it closed, even when it is requested again", async () => {+ const show = vi.fn(async (_channel: unknown, _request: InputRequest) => {});+ await publish(show, [requested(question("day")), resolved("day"), requested(question("day"))]);+ expect(show.mock.calls.map(([, request]) => request.requestId)).toEqual(["day"]);+ });++ it("throws instead of showing nothing when the step's projection is missing", async () => {+ const show = vi.fn(async (_channel: unknown, _request: InputRequest) => {});+ const handlers = promptQueueEvents(show);+ const channel = { ctx: new ContextContainer(), state: { shownPromptId: "day" } };+ await expect(+ handlers["input.resolved"](+ { resolutions: [{ kind: "question", outcome: "answered", requestId: "day" }], ...AT },+ channel,+ ),+ ).rejects.toThrow("initialized projection");+ expect(channel.state.shownPromptId).toBe("day");+ expect(show).not.toHaveBeenCalled();+ });++ it("tries a request it could not show again on the next event", async () => {+ const show = vi+ .fn(async (_channel: unknown, _request: InputRequest): Promise<boolean> => true)+ .mockResolvedValueOnce(false);+ await publish(show, [requested(question("day")), requested(question("time"))]);+ expect(show.mock.calls.map(([, request]) => request.requestId)).toEqual(["day", "day"]);+ });+});diff --git a/packages/eve/src/channel/prompt-queue.ts b/packages/eve/src/channel/prompt-queue.tsnew file mode 100644index 000000000..d26148973--- /dev/null+++ b/packages/eve/src/channel/prompt-queue.ts@@ -0,0 +1,72 @@+import { contextStorage } from "#context/container.js";+import type { ContextReader } from "#context/key.js";+import { firstOpenInput } from "#harness/open-input-request.js";+import { currentProjection } from "#harness/session-machine/current.js";+import { foldSession } from "#protocol/session-projection.js";+import type { UnstampedMessageStreamEvent } from "#protocol/message.js";+import type { InputRequest } from "#shared/input.js";++/**+ * Channel state for showing a session's open requests one at a time. A typed+ * reply answers the first open request, so a channel that can only show text+ * shows that one and holds the rest until it is answered.+ */+export interface PromptQueueState {+ /** The request the channel last showed, which a typed reply answers. */+ shownPromptId?: string;+}++/**+ * Event handlers that call `show` with the request a typed reply answers now,+ * each time that request changes. They read which requests are open from the+ * session's own record of the events it published, so a request that closed+ * never shows, and one the channel failed to show is tried again on the next+ * event. `show` returns `false` when it could not show the request.+ */+export function promptQueueEvents<TChannel extends { state: PromptQueueState }>(+ show: (channel: TChannel, request: InputRequest) => Promise<boolean | void>,+) {+ async function refresh(channel: TChannel, event: UnstampedMessageStreamEvent) {+ // The session records the event only after its handlers run.+ const first = firstOpenInput(foldSession(publishedProjection(channel), event))?.request;+ if (first === undefined) {+ delete channel.state.shownPromptId;+ return;+ }+ if (first.requestId === channel.state.shownPromptId) return;+ if ((await show(channel, first)) === false) return;+ channel.state.shownPromptId = first.requestId;+ }++ return {+ async "input.requested"(+ data: Extract<UnstampedMessageStreamEvent, { type: "input.requested" }>["data"],+ channel: TChannel,+ ): Promise<void> {+ await refresh(channel, { data, type: "input.requested" });+ },+ async "input.resolved"(+ data: Extract<UnstampedMessageStreamEvent, { type: "input.resolved" }>["data"],+ channel: TChannel,+ ): Promise<void> {+ await refresh(channel, { data, type: "input.resolved" });+ },+ async "approval.settled"(+ data: Extract<UnstampedMessageStreamEvent, { type: "approval.settled" }>["data"],+ channel: TChannel,+ ): Promise<void> {+ await refresh(channel, { data, type: "approval.settled" });+ },+ };+}++/**+ * The session's record as of the last event it published. Throws rather than read an empty+ * record when the step's projection is missing: an empty one would show nothing open, so the+ * next prompt would silently never post.+ */+function publishedProjection(channel: object) {+ // A handler's channel context carries the context of the step that publishes the event.+ const ctx = (channel as { readonly ctx?: ContextReader }).ctx ?? contextStorage.getStore();+ return currentProjection(ctx);+}diff --git a/packages/eve/src/harness/hitl/approvals.ts b/packages/eve/src/harness/hitl/approvals.tsindex 85f6e726d..4ffe5f6cc 100644--- a/packages/eve/src/harness/hitl/approvals.ts+++ b/packages/eve/src/harness/hitl/approvals.ts@@ -108,7 +108,12 @@ export function parkOnApprovals(...committed,...(snippet === undefined ? [] : [createFrameworkUserMessage("context.state", snippet)]),],- events: [createInputRequestedEvent({ requests: input.requests, ...input.event })],+ // The approvals can't settle before the step's tasks finish, so asking for them waits until+ // then; `settle` asks once they have.+ events:+ input.tasks.length > 0+ ? []+ : [createInputRequestedEvent({ requests: input.requests, ...input.event })],turn,};}@@ -251,13 +256,9 @@ export function answer(return done({ input: delivery, next: "continue" });}- const textBatch =- limit !== undefined- ? { requests: [limit.request] }- : answerable.length === 1- ? answerable[0]- : undefined;- const resolved = textBatch === undefined ? delivery : resolveTextInput(textBatch, delivery);+ // The approval coordinator already answered a typed approval; see `resolveTypedApproval`.+ const resolved =+ limit === undefined ? delivery : resolveTextInput({ requests: [limit.request] }, delivery);const responses = canonicalize(resolved?.inputResponses ?? []);const byId = new Map(responses.map((response) => [response.requestId, response]));const answered = answerable.filter((step) =>@@ -511,16 +512,15 @@ export function grantedApprovalKeys(/*** The steps a delivery approves calls of, whose tools the calls run with: those it answers in- * full, approving at least one call, or the only pending step a plain-text answer approves.+ * full, approving at least one call.*/export function approvingSteps(view: SessionView,stepInput: StepInput | undefined,): readonly SuspendedStep[] {const pending = view.turn.suspended.filter((step) => step.requests.length > 0);- const resolved = pending.length === 1 ? resolveTextInput(pending[0]!, stepInput) : stepInput;const options = new Map(- (resolved?.inputResponses ?? []).map((response) => [response.requestId, response.optionId]),+ (stepInput?.inputResponses ?? []).map((response) => [response.requestId, response.optionId]),);return pending.filter((step) =>diff --git a/packages/eve/src/harness/hitl/coordinator.test.ts b/packages/eve/src/harness/hitl/coordinator.test.tsindex a11fc48ae..7ce8729b9 100644--- a/packages/eve/src/harness/hitl/coordinator.test.ts+++ b/packages/eve/src/harness/hitl/coordinator.test.ts@@ -14,8 +14,10 @@ import { settleDirectApprovalResponse } from "#harness/hitl/candidates.js";import { coordinateApprovalDelivery } from "#harness/hitl/coordinator.js";import { approvingSteps } from "#harness/hitl/approvals.js";import { sessionView } from "#harness/session-machine/commit.js";+import { resolveTypedApproval } from "#harness/hitl/delivery.js";import { storedProjection } from "#harness/session-machine/view.js";-import { parkedSteps, withParkedStep } from "#internal/testing/session-machine.js";+import { parkedSteps, withParkedStep, withPublished } from "#internal/testing/session-machine.js";+import { createApprovalSettledEvent } from "#protocol/message.js";import type { HarnessSession } from "#harness/types.js";import type { InputRequest } from "#shared/input.js";@@ -39,6 +41,10 @@ const responder: SessionAuthContext = {principalType: "user",};+function viewOf(session: HarnessSession) {+ return sessionView(storedProjection(session.state), session.state);+}+function parkedSession(requester?: SessionAuthContext): HarnessSession {return withParkedStep({@@ -331,6 +337,35 @@ describe("coordinateApprovalDelivery", () => {]);});+ it("asks the response policy about the person who typed approve", async () => {+ const ctx = new ContextContainer();+ ctx.set(SessionKey, {+ auth: { current: responder, initiator: responder },+ sessionId: "session-1",+ turn: { id: "turn-1", sequence: 1 },+ });+ const session = parkedSession();+ const ingested = await contextStorage.run(ctx, () =>+ coordinateApprovalDelivery({+ now: 100,+ session,+ stepInput: resolveTypedApproval(viewOf(session), { message: "approve" }),+ tools: new Map(),+ }),+ );+ expect(ingested.kind).toBe("continue-coordination");+ expect(ingested.stepInput?.message).toBeUndefined();++ const response = vi.fn<ApprovalResponsePolicy>(() => ({ status: "allowed" }));+ const settled = await authorize(ingested.session, response);+ expect(response).toHaveBeenCalledWith(+ expect.objectContaining({ response: { decision: "approve", principal: responder } }),+ );+ expect(settled.stepInput?.inputResponses).toEqual([+ { optionId: "approve", requestId: request.requestId },+ ]);+ });+it("forwards an unrelated message while a response-authorized approval remains pending", async () => {const messageAuth: SessionAuthContext = { ...responder, principalId: "user-2" };const result = await coordinateApprovalDelivery({@@ -405,8 +440,8 @@ describe("text approval replay preparation", () => {session: HarnessSession;stepInput?: import("#harness/types.js").StepInput;}) {- const view = sessionView(storedProjection(input.session.state), input.session.state);- return approvingSteps(view, input.stepInput).length > 0;+ const view = viewOf(input.session);+ return approvingSteps(view, resolveTypedApproval(view, input.stepInput)).length > 0;}function sessionWithRequests(requests: InputRequest[] = [request],@@ -440,23 +475,41 @@ describe("text approval replay preparation", () => {).toBe(false);});- it("does not bypass responder authorization with text", () => {- expect(- shouldPrepareApprovalReplayTools({- session: sessionWithRequests([request], [request.requestId]),- stepInput: { message: "approve" },- }),- ).toBe(false);- });-- it("does not interpret text when multiple batches are pending", () => {+ it("answers only the first open approval by text", () => {+ const second = { ...request, requestId: "approval-2" };const session = withParkedStep(sessionWithRequests(), {event: { sequence: 2, stepIndex: 0, turnId: "turn-2" },- requests: [{ ...request, requestId: "approval-2" }],+ requests: [second],});- expect(shouldPrepareApprovalReplayTools({ session, stepInput: { message: "approve" } })).toBe(- false,- );+ expect(resolveTypedApproval(viewOf(session), { message: "approve" })?.inputResponses).toEqual([+ { optionId: "approve", requestId: request.requestId },+ ]);+ expect(+ resolveTypedApproval(viewOf(session), {+ inputResponses: [{ optionId: "approve", requestId: request.requestId }],+ message: "cancel",+ })?.inputResponses,+ ).toEqual([+ { optionId: "approve", requestId: request.requestId },+ { optionId: "cancel", requestId: second.requestId },+ ]);+ });++ it("skips an approval that already settled on its own", () => {+ const second = { ...request, requestId: "approval-2" };+ const session = withPublished(sessionWithRequests([request, second]), [+ createApprovalSettledEvent({+ outcome: "approved",+ requestId: request.requestId,+ responderPrincipalId: responder.principalId,+ sequence: 0,+ stepIndex: 0,+ turnId: "turn_0",+ }),+ ]);+ expect(resolveTypedApproval(viewOf(session), { message: "cancel" })?.inputResponses).toEqual([+ { optionId: "cancel", requestId: second.requestId },+ ]);});it("preserves an explicit cancellation over approval text", () => {diff --git a/packages/eve/src/harness/hitl/delivery.ts b/packages/eve/src/harness/hitl/delivery.tsindex c678ff71a..fa8c5edfd 100644--- a/packages/eve/src/harness/hitl/delivery.ts+++ b/packages/eve/src/harness/hitl/delivery.ts@@ -1,11 +1,13 @@import type { UserContent } from "ai";-import { resolveTextToResponses } from "#channel/resolve-text.js";+import { resolveTextToResponse, resolveTextToResponses } from "#channel/resolve-text.js";import { coalesceTurnInputs } from "#harness/messages.js";import {convertStaleResponsesToUserMessage,dropStaleSessionLimitContinuationResponses,} from "#harness/hitl/stale-responses.js";+import { isApprovalRequest } from "#harness/input-request-class.js";+import { firstOpenInput } from "#harness/open-input-request.js";import type { StepInput } from "#harness/types.js";import { attachClientContext, readClientContext } from "#internal/client-context.js";import { readAnswerText } from "#internal/input-text.js";@@ -61,13 +63,13 @@ export function deliver(export type ResolvedStepInput = StepInput & { readonly messageConsumed?: boolean };/**- * Plain text answers the only pending batch, unless a response policy must decide it. A channel+ * Plain text answers a budget prompt; `resolveTypedApproval` answers approvals. A channel* that wraps the typed text in an envelope for the model attaches the text itself to answer with.* The answer consumes the message and the context the channel sent about it: the model reads* neither.*/export function resolveTextInput(- batch: Pick<SuspendedStep, "requests" | "responseAuthRequiredRequestIds">,+ batch: Pick<SuspendedStep, "requests">,stepInput: StepInput | undefined,): ResolvedStepInput | undefined {const text = readAnswerText(stepInput);@@ -76,11 +78,7 @@ export function resolveTextInput(if (stepInput.inputResponses?.some((response) => batchRequestIds.has(response.requestId))) {return stepInput;}- const policyDecides = new Set(batch.responseAuthRequiredRequestIds ?? []);- const responses = resolveTextToResponses(- text,- batch.requests.filter((request) => !policyDecides.has(request.requestId)),- );+ const responses = resolveTextToResponses(text, batch.requests);if (responses.length === 0) return stepInput;return compactInput({...stepInput,@@ -91,6 +89,40 @@ export function resolveTextInput(});}+/**+ * Turns a typed reply into a response to the first open request when that request is one of+ * this turn's approvals. The approval coordinator then settles it, or asks its response policy+ * about the person who typed, as it does for a press. A batch's other approvals stay open for+ * later replies.+ */+export function resolveTypedApproval(+ view: Pick<SessionView, "projection" | "turn">,+ stepInput: StepInput | undefined,+): ResolvedStepInput | undefined {+ const text = readAnswerText(stepInput);+ if (stepInput === undefined || text === undefined) return stepInput;+ const answered = new Set(+ [+ ...(stepInput.inputResponses ?? []),+ ...(stepInput.attributedInputResponses ?? []).map(({ response }) => response),+ ].map(({ requestId }) => requestId),+ );+ const first = firstOpenInput(view.projection, (requestId) => answered.has(requestId))?.request;+ const parked = view.turn.suspended.some((step) =>+ step.requests.some((request) => request.requestId === first?.requestId),+ );+ if (first === undefined || !parked || !isApprovalRequest(first)) return stepInput;+ const response = resolveTextToResponse(text, first);+ if (response === undefined) return stepInput;+ return {+ ...stepInput,+ context: undefined,+ inputResponses: [...(stepInput.inputResponses ?? []), response],+ message: undefined,+ messageConsumed: true,+ };+}+export function canonicalize(responses: readonly InputResponse[]): readonly InputResponse[] {const byRequestId = new Map<string, InputResponse>();for (const response of responses) byRequestId.set(response.requestId, response);diff --git a/packages/eve/src/harness/hitl/intake.ts b/packages/eve/src/harness/hitl/intake.tsindex 551d63942..70c235d19 100644--- a/packages/eve/src/harness/hitl/intake.ts+++ b/packages/eve/src/harness/hitl/intake.ts@@ -30,7 +30,7 @@ import {import { runApprovedCalls } from "./approved-calls.js";import { getApprovalAuditState, retireActiveCandidates } from "./candidates.js";import { coordinateApprovalDelivery } from "./coordinator.js";-import { deliver, turnInputOnly, withoutTurnInput } from "./delivery.js";+import { deliver, resolveTypedApproval, turnInputOnly, withoutTurnInput } from "./delivery.js";import { approversOf, setApprovedCallCallers } from "./approved-call-callers.js";import type { InputRequest } from "#shared/input.js";import type { InstrumentationAttempt } from "#instrumentation/runtime.js";@@ -80,12 +80,15 @@ export async function acceptHumanInput(const completions = config.signInCompletions ?? [];if (completions.length > 0) await step.apply(completeSignIn(step.view(), { completions }));const delivered = deliver(step.view(), input, options);+ // A typed approval answers like a press, so the approval's response policy decides it.+ const typed = resolveTypedApproval(step.view(), delivered.input);// A new message reaching the held turn steers it: the sign-ins it waits on end, and its- // unanswered approvals resolve with the answers below.+ // unanswered approvals resolve with the answers below. A typed answer is not a new message.const steered =- input?.message !== undefined || delivered.displayMessage !== undefined- ? await withdrawSteeredSignIns(step, delivered.input)- : delivered.input;+ (input?.message !== undefined && typed?.message !== undefined) ||+ delivered.displayMessage !== undefined+ ? await withdrawSteeredSignIns(step, typed)+ : typed;// Restoring a turn's tools runs its resolvers, so a step's tools are restored once, and again// only after another step's.const restoredTools = new Map<string, HarnessToolMap>();diff --git a/packages/eve/src/harness/open-input-request.ts b/packages/eve/src/harness/open-input-request.tsnew file mode 100644index 000000000..b6a2ab4cb--- /dev/null+++ b/packages/eve/src/harness/open-input-request.ts@@ -0,0 +1,19 @@+import {+ openInputs,+ type SessionInput,+ type SessionProjection,+} from "#protocol/session-projection.js";++/**+ * The request a typed reply answers: the first open request nobody has answered yet. A budget+ * prompt comes first, since the session settles it before anything else; the rest follow the+ * order of the session's `input.requested` events. Channels that can only show text show this+ * request alone, so a reply answers the request the person sees.+ */+export function firstOpenInput(+ projection: SessionProjection,+ answered: (requestId: string) => boolean = () => false,+): SessionInput | undefined {+ const open = openInputs(projection).filter((input) => !answered(input.request.requestId));+ return open.find((input) => input.request.kind === "session-limit") ?? open[0];+}diff --git a/packages/eve/src/harness/session-machine/current.ts b/packages/eve/src/harness/session-machine/current.tsindex 5174a86c4..4f982db27 100644--- a/packages/eve/src/harness/session-machine/current.ts+++ b/packages/eve/src/harness/session-machine/current.ts@@ -10,8 +10,15 @@ import type { MessageStreamEvent, UnstampedMessageStreamEvent } from "#protocol/import { SESSION_PROJECTION_STATE_KEY, storedProjection } from "./view.js";// A step-local holder for the projection as the step publishes. It survives provider scope-// resets but never serializes with context; the step saves it into session state.-const liveProjections = new WeakMap<ContextReader, { projection: SessionProjection }>();+// resets but never serializes with context; the step saves it into session state. Rooted on+// `globalThis`, like the context key registry, so every bundled copy of eve in a step, such as+// a channel's, reads the same one.+const LIVE_PROJECTIONS_GLOBAL_KEY = Symbol.for("eve.live-session-projections");+type LiveProjectionsGlobal = typeof globalThis & {+ [LIVE_PROJECTIONS_GLOBAL_KEY]?: WeakMap<ContextReader, { projection: SessionProjection }>;+};+const liveProjections = ((globalThis as LiveProjectionsGlobal)[LIVE_PROJECTIONS_GLOBAL_KEY] ??=+ new WeakMap());export function enterSessionProjection(ctx: ContextReader,diff --git a/packages/eve/src/harness/session-machine/machine.test.ts b/packages/eve/src/harness/session-machine/machine.test.tsindex d92ce265a..fa05d379f 100644--- a/packages/eve/src/harness/session-machine/machine.test.ts+++ b/packages/eve/src/harness/session-machine/machine.test.ts@@ -24,7 +24,7 @@ import {requireSignIn,type ResponsePolicyPass,} from "#harness/hitl/approvals.js";-import { deliver } from "#harness/hitl/delivery.js";+import { deliver, resolveTypedApproval } from "#harness/hitl/delivery.js";import { applyTransition, sessionView, type Transition } from "./commit.js";import { saveProjection } from "./current.js";import {@@ -139,11 +139,13 @@ const noPolicy = (stepInput?: StepInput): ResponsePolicyPass => ({/** Answers through `deliver` and `answer`, as the tool loop does with no response policy. */async function respond(machine: Machine, input: StepInput) {const delivered = deliver(machine.view(), input, { takeQueued: true });+ // The intake answers a typed approval before the policy pass, as a press.+ const typed = resolveTypedApproval(machine.view(), delivered.input);return machine.apply(answer(machine.view(), {approvalKey: () => undefined,- delivery: delivered.input,- policy: noPolicy(delivered.input),+ delivery: typed,+ policy: noPolicy(typed),takeQueued: delivered.takeQueued,}),);diff --git a/packages/eve/src/harness/session-machine/transitions.ts b/packages/eve/src/harness/session-machine/transitions.tsindex 7063a20a5..8f4f73e0f 100644--- a/packages/eve/src/harness/session-machine/transitions.ts+++ b/packages/eve/src/harness/session-machine/transitions.ts@@ -54,6 +54,7 @@ import {activeTurnId,answeredCallIds,nextStepIndex,+ runningTasks,turnPosition,type SessionView,} from "./view.js";@@ -268,6 +269,13 @@ export function settle(view: SessionView, input: { readonly results: readonly Seevents.push(createActionResultEvent({ result, ...step.event }));}}+ // A step that parked approvals beside its tasks asks for them once the tasks finish.+ for (const step of steps) {+ if (step.tasks.length === 0 || step.requests.length === 0) continue;+ if (runningTasks(step).length > 0) continue;+ if (step.requests.some((request) => request.requestId in view.projection.inputs)) continue;+ events.push(createInputRequestedEvent({ requests: step.requests, ...step.event }));+ }const complete = steps.filter(isComplete);return {commit: complete.flatMap((step) => step.messages),diff --git a/packages/eve/src/harness/tool-loop.test.ts b/packages/eve/src/harness/tool-loop.test.tsindex 9eb215570..0704c3c2a 100644--- a/packages/eve/src/harness/tool-loop.test.ts+++ b/packages/eve/src/harness/tool-loop.test.ts@@ -57,7 +57,8 @@ import {modelFacingAuthorizationOutput,requestAuthorization,} from "#harness/authorization.js";-import { sessionView } from "#harness/session-machine/commit.js";+import { applyTransition, sessionView } from "#harness/session-machine/commit.js";+import { requireSignIn } from "#harness/hitl/approvals.js";import { createAuthorizationRequiredEvent } from "#protocol/message.js";import { ownOpenRequestIds } from "#harness/session-machine/transitions.js";import { runtimeWait, storedProjection } from "#harness/session-machine/view.js";@@ -1705,8 +1706,9 @@ describe("createToolLoopHarness", () => {expect(runtimeWait(parked.session.state)?.tasks).toEqual([expect.objectContaining({ callId: "delegate-1", kind: "workflow-task" }),]);- expect(openRequestIds(parked.session).size > 0).toBe(true);- expect(events.filter((event) => event.type === "input.requested")).toHaveLength(1);+ // The approval can't settle before the workflow task finishes, so asking waits until then.+ expect(parkedSteps(parked.session).some((step) => step.requests.length > 0)).toBe(true);+ expect(events.filter((event) => event.type === "input.requested")).toHaveLength(0);expect(parked.session.history.flatMap((message) =>Array.isArray(message.content)@@ -1735,6 +1737,7 @@ describe("createToolLoopHarness", () => {expect(JSON.stringify(parkedSteps(reparked.session)[0]?.messages)).toContain("delegated-done");expect(JSON.stringify(reparked.session.history)).not.toContain("delegated-done");expect(reparked.held).toEqual({ kind: "request" });+ expect(events.filter((event) => event.type === "input.requested")).toHaveLength(1);expect(events.at(-1)?.type).toBe("turn.waiting");});@@ -7940,6 +7943,76 @@ describe("createToolLoopHarness", () => {]);});+ // Alice's turn waits on two approvals and on Bob signing in to statuspage.+ async function parkedOnApprovalsAndSignIn(): Promise<HarnessSession> {+ const approval = (requestId: string, callId: string) => ({+ action: { callId, input: {}, kind: "tool-call" as const, toolName: "guarded_echo" },+ allowFreeform: false,+ display: "confirmation" as const,+ kind: "tool-approval" as const,+ options: [+ { id: "approve", label: "Approve" },+ { id: "cancel", label: "Cancel" },+ ],+ prompt: "Approve tool call: guarded_echo",+ requestId,+ });+ const parked = parkedOnApproval({+ requests: [approval("approval-1", "call-1"), approval("approval-2", "call-2")],+ responseMessages: [],+ session: createTestSession(),+ });+ const signIn = requireSignIn(sessionView(storedProjection(parked.state), parked.state), {+ challenges: [+ {+ attemptId: "attempt-statuspage",+ challenge: { url: "https://idp.example/authorize" },+ hookUrl: "https://app.example/eve/v1/connections/statuspage/callback",+ name: "statuspage",+ },+ ],+ });+ return withPublished(await applyTransition(parked, signIn, async () => {}), signIn.events);+ }++ it("answers a typed approval without withdrawing the turn's pending sign-ins", async () => {+ const session = await parkedOnApprovalsAndSignIn();+ const { emit, events } = createEventCollector();++ const result = await createToolLoopHarness(createTestConfig(emit))(session, {+ message: "approve",+ });++ expect(events.filter((event) => event.type === "authorization.completed")).toEqual([]);+ const view = sessionView(storedProjection(result.session.state), result.session.state);+ expect(view.signIns.map((challenge) => challenge.name)).toEqual(["statuspage"]);+ // The answer to the first approval waits for the second, as a press would.+ expect(view.turn.queued?.inputResponses).toEqual([+ { optionId: "approve", requestId: "approval-1" },+ ]);+ });++ it("withdraws the turn's pending sign-ins when a new message steers it", async () => {+ setupMockAgent({+ finishReason: "stop",+ response: { messages: [{ content: "Sure.", role: "assistant" }] },+ text: "Sure.",+ toolCalls: [],+ toolResults: [],+ });+ const { emit, events } = createEventCollector();++ await createToolLoopHarness(createTestConfig(emit))(await parkedOnApprovalsAndSignIn(), {+ message: "Actually, can you check the weather first?",+ });++ expect(+ events+ .filter((event) => event.type === "authorization.completed")+ .map((event) => [event.data.name, event.data.outcome]),+ ).toEqual([["statuspage", "declined"]]);+ });+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/internal/testing/channel-conformance/MATRIX.md b/packages/eve/src/internal/testing/channel-conformance/MATRIX.mdindex 00f2d2581..f9d3578ed 100644--- a/packages/eve/src/internal/testing/channel-conformance/MATRIX.md+++ b/packages/eve/src/internal/testing/channel-conformance/MATRIX.md@@ -24,7 +24,7 @@ pnpm --filter eve exec vitest run --config vitest.unit.config.ts channel-conform| a text reply answers an open-ended question with the person's words | ✅ | ✅ | ✅ | ✅ | ✅ | —<sup>[2](#note-2)</sup> | —<sup>[2](#note-2)</sup> | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ || pressing an option of an answered question sends it to the agent as new input | —<sup>[3](#note-3)</sup> | —<sup>[4](#note-4)</sup> | ✅ | | —<sup>[1](#note-1)</sup> | ✅ | | —<sup>[1](#note-1)</sup> | | ✅ | | ✅ | | ✅ | | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> || pressing options of two pending questions answers each with its own option | ✅ | ✅ | ✅ | | —<sup>[1](#note-1)</sup> | ✅ | | —<sup>[1](#note-1)</sup> | | ✅ | | ✅ | | ✅ | | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> |-| a text reply matching an option does not answer either of two pending questions | ✅ | ✅ | ✅ | ✅ | ✅ | —<sup>[2](#note-2)</sup> | —<sup>[2](#note-2)</sup> | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |+| text replies answer two pending questions one at a time, in the order shown | —<sup>[5](#note-5)</sup> | ✅ | ✅ | ✅ | ✅ | —<sup>[2](#note-2)</sup> | —<sup>[2](#note-2)</sup> | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |## Tool approvals@@ -35,22 +35,24 @@ pnpm --filter eve exec vitest run --config vitest.unit.config.ts channel-conform| pressing Approve twice runs the gated tool once | ✅ | ✅ | ✅ | | —<sup>[1](#note-1)</sup> | ✅ | | —<sup>[1](#note-1)</sup> | | ✅ | | ✅ | | ✅ | | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> || pressing Approve on one of two pending approvals runs only that tool | ✅ | ✅ | ✅ | | —<sup>[1](#note-1)</sup> | ✅ | | —<sup>[1](#note-1)</sup> | | ✅ | | ✅ | | ✅ | | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> || answering an approval and a question pending together settles both | ✅ | ✅ | ✅ | | —<sup>[1](#note-1)</sup> | ✅ | | —<sup>[1](#note-1)</sup> | | ✅ | | ✅ | | ✅ | | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> |+| text replies answer two pending approvals one at a time, in the order shown | —<sup>[6](#note-6)</sup> | —<sup>[7](#note-7)</sup> | ✅ | ✅ | ✅ | —<sup>[2](#note-2)</sup> | —<sup>[2](#note-2)</sup> | ✅ | ✅ | ❌<sup>[8](#note-8)</sup> | ❌<sup>[8](#note-8)</sup> | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |+| text replies answer a question and an approval raised together, in the order shown | —<sup>[6](#note-6)</sup> | ✅ | ✅ | | ✅ | —<sup>[2](#note-2)</sup> | | ✅ | | ✅ | | ✅ | | ✅ | | ✅ | ✅ | ✅ | ✅ || pressing Cancel stops the gated tool without running it | ✅ | ✅ | ✅ | | —<sup>[1](#note-1)</sup> | ✅ | | —<sup>[1](#note-1)</sup> | | ✅ | | ✅ | | ✅ | | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> |-| a text reply of approve runs the gated tool | —<sup>[5](#note-5)</sup> | ✅ | ✅ | ✅ | ✅ | —<sup>[2](#note-2)</sup> | —<sup>[2](#note-2)</sup> | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |-| a text reply of cancel stops the gated tool without running it | —<sup>[5](#note-5)</sup> | ✅ | ✅ | ✅ | ✅ | —<sup>[2](#note-2)</sup> | —<sup>[2](#note-2)</sup> | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |+| a text reply of approve runs the gated tool | —<sup>[6](#note-6)</sup> | ✅ | ✅ | ✅ | ✅ | —<sup>[2](#note-2)</sup> | —<sup>[2](#note-2)</sup> | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |+| a text reply of cancel stops the gated tool without running it | —<sup>[6](#note-6)</sup> | ✅ | ✅ | ✅ | ✅ | —<sup>[2](#note-2)</sup> | —<sup>[2](#note-2)</sup> | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |## Answered prompts| Rule | `tui` | `web chat` | `chat-sdk` | `chat-sdk-dm` | `chat-sdk-text` | `discord` | `discord-dm` | `linq` | `linq-dm` | `slack` | `slack-dm` | `teams` | `teams-dm` | `telegram` | `telegram-dm` | `github` | `linear` | `photon` | `twilio` || --- | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: || pressing an option clears the question's buttons | ✅ | ✅ | ✅ | | —<sup>[1](#note-1)</sup> | ✅ | | —<sup>[1](#note-1)</sup> | | ✅ | | ✅ | | ✅ | | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> |-| answering a question by text clears its buttons | ✅ | ✅ | ✅ | ✅ | —<sup>[1](#note-1)</sup> | —<sup>[2](#note-2)</sup> | —<sup>[2](#note-2)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | ❌<sup>[6](#note-6)</sup> | ❌<sup>[6](#note-6)</sup> | ✅ | ✅ | ✅ | ✅ | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> |-| pressing an option names who answered on the question | —<sup>[7](#note-7)</sup> | —<sup>[8](#note-8)</sup> | ❌<sup>[9](#note-9)</sup> | | —<sup>[1](#note-1)</sup> | ❌<sup>[9](#note-9)</sup> | | —<sup>[1](#note-1)</sup> | | ✅ | | ❌<sup>[9](#note-9)</sup> | | ❌<sup>[9](#note-9)</sup> | | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> |-| answering a question by text names who answered on the question | —<sup>[7](#note-7)</sup> | —<sup>[8](#note-8)</sup> | ❌<sup>[9](#note-9)</sup> | ❌<sup>[9](#note-9)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[2](#note-2)</sup> | —<sup>[2](#note-2)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | ❌<sup>[6](#note-6)</sup> | ❌<sup>[6](#note-6)</sup> | ❌<sup>[9](#note-9)</sup> | ❌<sup>[9](#note-9)</sup> | ❌<sup>[9](#note-9)</sup> | ❌<sup>[9](#note-9)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> |+| answering a question by text clears its buttons | ✅ | ✅ | ✅ | ✅ | —<sup>[1](#note-1)</sup> | —<sup>[2](#note-2)</sup> | —<sup>[2](#note-2)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | ❌<sup>[9](#note-9)</sup> | ❌<sup>[9](#note-9)</sup> | ✅ | ✅ | ✅ | ✅ | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> |+| pressing an option names who answered on the question | —<sup>[10](#note-10)</sup> | —<sup>[11](#note-11)</sup> | ❌<sup>[12](#note-12)</sup> | | —<sup>[1](#note-1)</sup> | ❌<sup>[12](#note-12)</sup> | | —<sup>[1](#note-1)</sup> | | ✅ | | ❌<sup>[12](#note-12)</sup> | | ❌<sup>[12](#note-12)</sup> | | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> |+| answering a question by text names who answered on the question | —<sup>[10](#note-10)</sup> | —<sup>[11](#note-11)</sup> | ❌<sup>[12](#note-12)</sup> | ❌<sup>[12](#note-12)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[2](#note-2)</sup> | —<sup>[2](#note-2)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | ❌<sup>[9](#note-9)</sup> | ❌<sup>[9](#note-9)</sup> | ❌<sup>[12](#note-12)</sup> | ❌<sup>[12](#note-12)</sup> | ❌<sup>[12](#note-12)</sup> | ❌<sup>[12](#note-12)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> || pressing Approve clears the approval's buttons | ✅ | ✅ | ✅ | | —<sup>[1](#note-1)</sup> | ✅ | | —<sup>[1](#note-1)</sup> | | ✅ | | ✅ | | ✅ | | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> |-| approving by text clears the approval's buttons | —<sup>[5](#note-5)</sup> | ✅ | ✅ | ✅ | —<sup>[1](#note-1)</sup> | —<sup>[2](#note-2)</sup> | —<sup>[2](#note-2)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> |-| pressing Approve names who approved on the approval | —<sup>[7](#note-7)</sup> | —<sup>[8](#note-8)</sup> | ❌<sup>[9](#note-9)</sup> | | —<sup>[1](#note-1)</sup> | ❌<sup>[9](#note-9)</sup> | | —<sup>[1](#note-1)</sup> | | ✅ | | ✅ | | ❌<sup>[9](#note-9)</sup> | | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> |-| approving by text names who approved on the approval | —<sup>[5](#note-5)</sup> | —<sup>[8](#note-8)</sup> | ❌<sup>[9](#note-9)</sup> | ❌<sup>[9](#note-9)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[2](#note-2)</sup> | —<sup>[2](#note-2)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | ❌<sup>[10](#note-10)</sup> | ❌<sup>[10](#note-10)</sup> | ❌<sup>[9](#note-9)</sup> | ❌<sup>[9](#note-9)</sup> | ❌<sup>[9](#note-9)</sup> | ❌<sup>[9](#note-9)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> |+| approving by text clears the approval's buttons | —<sup>[6](#note-6)</sup> | ✅ | ✅ | ✅ | —<sup>[1](#note-1)</sup> | —<sup>[2](#note-2)</sup> | —<sup>[2](#note-2)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> |+| pressing Approve names who approved on the approval | —<sup>[10](#note-10)</sup> | —<sup>[11](#note-11)</sup> | ❌<sup>[12](#note-12)</sup> | | —<sup>[1](#note-1)</sup> | ❌<sup>[12](#note-12)</sup> | | —<sup>[1](#note-1)</sup> | | ✅ | | ✅ | | ❌<sup>[12](#note-12)</sup> | | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> |+| approving by text names who approved on the approval | —<sup>[6](#note-6)</sup> | —<sup>[11](#note-11)</sup> | ❌<sup>[12](#note-12)</sup> | ❌<sup>[12](#note-12)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[2](#note-2)</sup> | —<sup>[2](#note-2)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | ✅ | ✅ | ❌<sup>[12](#note-12)</sup> | ❌<sup>[12](#note-12)</sup> | ❌<sup>[12](#note-12)</sup> | ❌<sup>[12](#note-12)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> |## Budget prompts@@ -67,28 +69,28 @@ pnpm --filter eve exec vitest run --config vitest.unit.config.ts channel-conform| Rule | `tui` | `web chat` | `chat-sdk` | `chat-sdk-dm` | `chat-sdk-text` | `discord` | `discord-dm` | `linq` | `linq-dm` | `slack` | `slack-dm` | `teams` | `teams-dm` | `telegram` | `telegram-dm` | `github` | `linear` | `photon` | `twilio` || --- | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |-| a sign-in names the service and shows its sign-in link | ✅ | ✅ | ✅ | ✅ | ✅ | ❌<sup>[11](#note-11)</sup> | ❌<sup>[11](#note-11)</sup> | ❌<sup>[12](#note-12)</sup> | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | —<sup>[13](#note-13)</sup> | ✅ | ✅ | ✅ |-| a sign-in shows its confirmation code | ✅ | ✅ | ✅ | ✅ | ✅ | ❌<sup>[11](#note-11)</sup> | ❌<sup>[11](#note-11)</sup> | ❌<sup>[12](#note-12)</sup> | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | —<sup>[13](#note-13)</sup> | ✅ | ✅ | ✅ |-| only the person signing in sees the sign-in link and code | —<sup>[14](#note-14)</sup> | —<sup>[14](#note-14)</sup> | ✅ | —<sup>[14](#note-14)</sup> | —<sup>[14](#note-14)</sup> | ✅ | —<sup>[14](#note-14)</sup> | ✅ | —<sup>[14](#note-14)</sup> | ✅ | —<sup>[14](#note-14)</sup> | ❌<sup>[15](#note-15)</sup> | —<sup>[14](#note-14)</sup> | ✅ | —<sup>[14](#note-14)</sup> | ✅ | ❌<sup>[16](#note-16)</sup> | —<sup>[14](#note-14)</sup> | —<sup>[14](#note-14)</sup> |-| a sign-in without a link shows its instructions | ✅ | ✅ | ✅ | ✅ | ✅ | ❌<sup>[11](#note-11)</sup> | ❌<sup>[11](#note-11)</sup> | ❌<sup>[12](#note-12)</sup> | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌<sup>[11](#note-11)</sup> | ✅ | ✅ | ✅ |+| a sign-in names the service and shows its sign-in link | ✅ | ✅ | ✅ | ✅ | ✅ | ❌<sup>[13](#note-13)</sup> | ❌<sup>[13](#note-13)</sup> | ❌<sup>[14](#note-14)</sup> | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | —<sup>[15](#note-15)</sup> | ✅ | ✅ | ✅ |+| a sign-in shows its confirmation code | ✅ | ✅ | ✅ | ✅ | ✅ | ❌<sup>[13](#note-13)</sup> | ❌<sup>[13](#note-13)</sup> | ❌<sup>[14](#note-14)</sup> | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | —<sup>[15](#note-15)</sup> | ✅ | ✅ | ✅ |+| only the person signing in sees the sign-in link and code | —<sup>[16](#note-16)</sup> | —<sup>[16](#note-16)</sup> | ✅ | —<sup>[16](#note-16)</sup> | —<sup>[16](#note-16)</sup> | ✅ | —<sup>[16](#note-16)</sup> | ✅ | —<sup>[16](#note-16)</sup> | ✅ | —<sup>[16](#note-16)</sup> | ❌<sup>[17](#note-17)</sup> | —<sup>[16](#note-16)</sup> | ✅ | —<sup>[16](#note-16)</sup> | ✅ | ❌<sup>[18](#note-18)</sup> | —<sup>[16](#note-16)</sup> | —<sup>[16](#note-16)</sup> |+| a sign-in without a link shows its instructions | ✅ | ✅ | ✅ | ✅ | ✅ | ❌<sup>[13](#note-13)</sup> | ❌<sup>[13](#note-13)</sup> | ❌<sup>[14](#note-14)</sup> | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌<sup>[13](#note-13)</sup> | ✅ | ✅ | ✅ || after signing in, the agent carries on with the request | ✅ | ✅ | ✅ | | ✅ | ✅ | | ✅ | | ✅ | | ✅ | | ✅ | | ✅ | ✅ | ✅ | ✅ |-| completing a sign-in tells the person it succeeded | ✅ | ✅ | ✅ | ✅ | ✅ | ❌<sup>[11](#note-11)</sup> | ❌<sup>[11](#note-11)</sup> | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌<sup>[11](#note-11)</sup> | ✅ | ✅ | ✅ |+| completing a sign-in tells the person it succeeded | ✅ | ✅ | ✅ | ✅ | ✅ | ❌<sup>[13](#note-13)</sup> | ❌<sup>[13](#note-13)</sup> | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌<sup>[13](#note-13)</sup> | ✅ | ✅ | ✅ || message after ignored sign-in gets an answer, signing in late doesn't run | ✅ | ✅ | ✅ | ✅ | ✅ | —<sup>[2](#note-2)</sup> | —<sup>[2](#note-2)</sup> | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |-| message after ignored sign-in tells user it was cancelled | ✅ | ✅ | ✅ | ✅ | ✅ | —<sup>[2](#note-2)</sup> | —<sup>[2](#note-2)</sup> | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌<sup>[11](#note-11)</sup> | ✅ | ✅ | ✅ |+| message after ignored sign-in tells user it was cancelled | ✅ | ✅ | ✅ | ✅ | ✅ | —<sup>[2](#note-2)</sup> | —<sup>[2](#note-2)</sup> | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌<sup>[13](#note-13)</sup> | ✅ | ✅ | ✅ || the requester pressing Approve on a requester-only approval runs the tool | ✅ | ✅ | ✅ | | —<sup>[1](#note-1)</sup> | ✅ | | —<sup>[1](#note-1)</sup> | | ✅ | | ✅ | | ✅ | | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> | —<sup>[1](#note-1)</sup> |-| another person pressing Approve on a requester-only approval leaves it pending | —<sup>[17](#note-17)</sup> | —<sup>[17](#note-17)</sup> | ✅ | | —<sup>[18](#note-18)</sup> | ✅ | | —<sup>[18](#note-18)</sup> | | ✅ | | ✅ | | ✅ | | —<sup>[18](#note-18)</sup> | —<sup>[18](#note-18)</sup> | —<sup>[18](#note-18)</sup> | —<sup>[18](#note-18)</sup> |-| another person pressing Cancel on a requester-only approval leaves it pending | —<sup>[17](#note-17)</sup> | —<sup>[17](#note-17)</sup> | ✅ | | —<sup>[18](#note-18)</sup> | ✅ | | —<sup>[18](#note-18)</sup> | | ✅ | | ✅ | | ✅ | | —<sup>[18](#note-18)</sup> | —<sup>[18](#note-18)</sup> | —<sup>[18](#note-18)</sup> | —<sup>[18](#note-18)</sup> |-| another person's rejected press leaves the approval's buttons in place | —<sup>[17](#note-17)</sup> | —<sup>[17](#note-17)</sup> | ✅ | | —<sup>[18](#note-18)</sup> | ✅ | | —<sup>[18](#note-18)</sup> | | ✅ | | ✅ | | ✅ | | —<sup>[18](#note-18)</sup> | —<sup>[18](#note-18)</sup> | —<sup>[18](#note-18)</sup> | —<sup>[18](#note-18)</sup> |-| another person pressing Approve on an open approval runs the tool | —<sup>[17](#note-17)</sup> | —<sup>[17](#note-17)</sup> | ✅ | | —<sup>[18](#note-18)</sup> | ✅ | | —<sup>[18](#note-18)</sup> | | ✅ | | ✅ | | ✅ | | —<sup>[18](#note-18)</sup> | —<sup>[18](#note-18)</sup> | —<sup>[18](#note-18)</sup> | —<sup>[18](#note-18)</sup> |+| another person pressing Approve on a requester-only approval leaves it pending | —<sup>[19](#note-19)</sup> | —<sup>[19](#note-19)</sup> | ✅ | | —<sup>[20](#note-20)</sup> | ✅ | | —<sup>[20](#note-20)</sup> | | ✅ | | ✅ | | ✅ | | —<sup>[20](#note-20)</sup> | —<sup>[20](#note-20)</sup> | —<sup>[20](#note-20)</sup> | —<sup>[20](#note-20)</sup> |+| another person pressing Cancel on a requester-only approval leaves it pending | —<sup>[19](#note-19)</sup> | —<sup>[19](#note-19)</sup> | ✅ | | —<sup>[20](#note-20)</sup> | ✅ | | —<sup>[20](#note-20)</sup> | | ✅ | | ✅ | | ✅ | | —<sup>[20](#note-20)</sup> | —<sup>[20](#note-20)</sup> | —<sup>[20](#note-20)</sup> | —<sup>[20](#note-20)</sup> |+| another person's rejected press leaves the approval's buttons in place | —<sup>[19](#note-19)</sup> | —<sup>[19](#note-19)</sup> | ✅ | | —<sup>[20](#note-20)</sup> | ✅ | | —<sup>[20](#note-20)</sup> | | ✅ | | ✅ | | ✅ | | —<sup>[20](#note-20)</sup> | —<sup>[20](#note-20)</sup> | —<sup>[20](#note-20)</sup> | —<sup>[20](#note-20)</sup> |+| another person pressing Approve on an open approval runs the tool | —<sup>[19](#note-19)</sup> | —<sup>[19](#note-19)</sup> | ✅ | | —<sup>[20](#note-20)</sup> | ✅ | | —<sup>[20](#note-20)</sup> | | ✅ | | ✅ | | ✅ | | —<sup>[20](#note-20)</sup> | —<sup>[20](#note-20)</sup> | —<sup>[20](#note-20)</sup> | —<sup>[20](#note-20)</sup> |## Attachments| Rule | `tui` | `web chat` | `chat-sdk` | `chat-sdk-dm` | `chat-sdk-text` | `discord` | `discord-dm` | `linq` | `linq-dm` | `slack` | `slack-dm` | `teams` | `teams-dm` | `telegram` | `telegram-dm` | `github` | `linear` | `photon` | `twilio` || --- | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |-| an image a person sends reaches the agent with its bytes and type | —<sup>[19](#note-19)</sup> | —<sup>[19](#note-19)</sup> | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | —<sup>[19](#note-19)</sup> | ✅ | ✅ | ✅ |-| a PDF a person sends reaches the agent with its bytes and type | —<sup>[19](#note-19)</sup> | —<sup>[19](#note-19)</sup> | ✅ | | ✅ | ✅ | | ✅ | | ✅ | | ✅ | | ✅ | | —<sup>[19](#note-19)</sup> | ✅ | ✅ | ✅ |-| a file that can't be downloaded reaches the agent as a note, not a link, and the next message still works | —<sup>[19](#note-19)</sup> | —<sup>[19](#note-19)</sup> | ✅ | | ✅ | ✅ | | ✅ | | ✅ | | ✅ | | ✅ | | —<sup>[19](#note-19)</sup> | ✅ | ✅ | ✅ |-| a file sent earlier in the conversation is still there on a later message | —<sup>[19](#note-19)</sup> | —<sup>[19](#note-19)</sup> | ✅ | | ✅ | —<sup>[20](#note-20)</sup> | | ✅ | | ✅ | | ✅ | | ✅ | | —<sup>[19](#note-19)</sup> | ✅ | ✅ | ✅ |+| an image a person sends reaches the agent with its bytes and type | —<sup>[21](#note-21)</sup> | —<sup>[21](#note-21)</sup> | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | —<sup>[21](#note-21)</sup> | ✅ | ✅ | ✅ |+| a PDF a person sends reaches the agent with its bytes and type | —<sup>[21](#note-21)</sup> | —<sup>[21](#note-21)</sup> | ✅ | | ✅ | ✅ | | ✅ | | ✅ | | ✅ | | ✅ | | —<sup>[21](#note-21)</sup> | ✅ | ✅ | ✅ |+| a file that can't be downloaded reaches the agent as a note, not a link, and the next message still works | —<sup>[21](#note-21)</sup> | —<sup>[21](#note-21)</sup> | ✅ | | ✅ | ✅ | | ✅ | | ✅ | | ✅ | | ✅ | | —<sup>[21](#note-21)</sup> | ✅ | ✅ | ✅ |+| a file sent earlier in the conversation is still there on a later message | —<sup>[21](#note-21)</sup> | —<sup>[21](#note-21)</sup> | ✅ | | ✅ | —<sup>[22](#note-22)</sup> | | ✅ | | ✅ | | ✅ | | ✅ | | —<sup>[21](#note-21)</sup> | ✅ | ✅ | ✅ |## Notes@@ -96,19 +98,21 @@ pnpm --filter eve exec vitest run --config vitest.unit.config.ts channel-conform2. <a id="note-2"></a>the platform has no plain-text replies3. <a id="note-3"></a>an answered question's drawer closes, so nothing is left to press4. <a id="note-4"></a>an answered question disables its options, so nothing is left to press-5. <a id="note-5"></a>the approval drawer holds the keyboard; a person answers it with y or n-6. <a id="note-6"></a>only the button interaction handler edits a question; a typed answer leaves it-7. <a id="note-7"></a>one person answers at their own terminal; there's nobody else to tell-8. <a id="note-8"></a>one person answers in their own browser tab; there's nobody else to tell-9. <a id="note-9"></a>a resolved prompt doesn't say who answered; input.resolved carries no responder-10. <a id="note-10"></a>the card loses its buttons after a typed approval but doesn't say who approved-11. <a id="note-11"></a>the channel has no default sign-in renderer-12. <a id="note-12"></a>Linq's openDM needs the person's phone handle, but a message names its sender by an opaque handle id, so the bot can only say to continue in a direct message-13. <a id="note-13"></a>the rule applies only where the conversation is shared or private, and this one is public-14. <a id="note-14"></a>the rule applies only where the conversation is public or shared, and this one is private-15. <a id="note-15"></a>the sign-in prompt, link included, is posted to the whole thread-16. <a id="note-16"></a>the code is in the elicitation body the whole issue sees; who sees the auth signal's link is unverified-17. <a id="note-17"></a>the platform has no second person who can act-18. <a id="note-18"></a>the platform has no second person who can act or buttons a person can press-19. <a id="note-19"></a>the platform has no files a person can send-20. <a id="note-20"></a>each slash command starts its own session, so no later message shares one with the file+5. <a id="note-5"></a>each open request has its own drawer, and typing a message dismisses them all+6. <a id="note-6"></a>the approval drawer holds the keyboard; a person answers it with y or n+7. <a id="note-7"></a>a reply sent while approvals wait goes out as a steering message, which sometimes restarts the turn instead of answering+8. <a id="note-8"></a>one card shows every approval a step raises, so a typed reply can't say which it answers+9. <a id="note-9"></a>only the button interaction handler edits a question; a typed answer leaves it+10. <a id="note-10"></a>one person answers at their own terminal; there's nobody else to tell+11. <a id="note-11"></a>one person answers in their own browser tab; there's nobody else to tell+12. <a id="note-12"></a>a resolved prompt doesn't say who answered; input.resolved carries no responder+13. <a id="note-13"></a>the channel has no default sign-in renderer+14. <a id="note-14"></a>Linq's openDM needs the person's phone handle, but a message names its sender by an opaque handle id, so the bot can only say to continue in a direct message+15. <a id="note-15"></a>the rule applies only where the conversation is shared or private, and this one is public+16. <a id="note-16"></a>the rule applies only where the conversation is public or shared, and this one is private+17. <a id="note-17"></a>the sign-in prompt, link included, is posted to the whole thread+18. <a id="note-18"></a>the code is in the elicitation body the whole issue sees; who sees the auth signal's link is unverified+19. <a id="note-19"></a>the platform has no second person who can act+20. <a id="note-20"></a>the platform has no second person who can act or buttons a person can press+21. <a id="note-21"></a>the platform has no files a person can send+22. <a id="note-22"></a>each slash command starts its own session, so no later message shares one with the filediff --git a/packages/eve/src/internal/testing/channel-conformance/conformance.ts b/packages/eve/src/internal/testing/channel-conformance/conformance.tsindex 3c09b9819..d19e686e1 100644--- a/packages/eve/src/internal/testing/channel-conformance/conformance.ts+++ b/packages/eve/src/internal/testing/channel-conformance/conformance.ts@@ -167,9 +167,10 @@ const SLACK_BROKEN = {"only the button interaction handler edits a question; a typed answer leaves it",["questionText"],),- "approving by text names who approved on the approval": {- reason: "the card loses its buttons after a typed approval but doesn't say who approved",- symptom: /the answered prompt never names who answered/,+ "text replies answer two pending approvals one at a time, in the order shown": {+ reason:+ "one card shows every approval a step raises, so a typed reply can't say which it answers",+ symptom: /were shown in one message, so a typed reply can't say which it answers/,},} satisfies Partial<Record<ContractRuleName, BrokenCell>>;@@ -196,6 +197,8 @@ const TELEGRAM_BROKEN = {const TUI_TYPED_APPROVAL ="the approval drawer holds the keyboard; a person answers it with y or n";+const TUI_TYPED_REPLIES =+ "each open request has its own drawer, and typing a message dismisses them all";const TUI_SINGLE_PERSON = "one person answers at their own terminal; there's nobody else to tell";const WEB_CHAT_SINGLE_PERSON ="one person answers in their own browser tab; there's nobody else to tell";@@ -270,6 +273,12 @@ const channelConformance = {"pressing Approve names who approved on the approval": TUI_SINGLE_PERSON,"pressing an option names who answered on the question": TUI_SINGLE_PERSON,"answering a question by text names who answered on the question": TUI_SINGLE_PERSON,+ "text replies answer two pending questions one at a time, in the order shown":+ TUI_TYPED_REPLIES,+ "text replies answer two pending approvals one at a time, in the order shown":+ TUI_TYPED_APPROVAL,+ "text replies answer a question and an approval raised together, in the order shown":+ TUI_TYPED_APPROVAL,},},],@@ -284,6 +293,9 @@ const channelConformance = {"approving by text names who approved on the approval": WEB_CHAT_SINGLE_PERSON,"pressing an option names who answered on the question": WEB_CHAT_SINGLE_PERSON,"answering a question by text names who answered on the question": WEB_CHAT_SINGLE_PERSON,+ // Flaky rather than failing, so it can't be recorded as broken: see the reason.+ "text replies answer two pending approvals one at a time, in the order shown":+ "a reply sent while approvals wait goes out as a steering message, which sometimes restarts the turn instead of answering",},},],diff --git a/packages/eve/src/internal/testing/channel-conformance/contract.ts b/packages/eve/src/internal/testing/channel-conformance/contract.tsindex ed1010d05..b7633fb68 100644--- a/packages/eve/src/internal/testing/channel-conformance/contract.ts+++ b/packages/eve/src/internal/testing/channel-conformance/contract.ts@@ -448,17 +448,18 @@ const questionRules = [},},{- rule: "a text reply matching an option does not answer either of two pending questions",- source: "docs/tools/human-in-the-loop.md#how-pause-and-resume-works",+ rule: "text replies answer two pending questions one at a time, in the order shown",+ source: "docs/tools/human-in-the-loop.md#several-requests-at-once",requires: ["text-replies"],variesByConversation: true,async run(conversation) {await conversation.say(PLAN_REVIEW);- await conversation.waitForRequest(DAY_PROMPT);- await conversation.waitForRequest(TIME_PROMPT);- await conversation.say("Saturday");- // Answering only the day question would leave the tool waiting, with no reply.- await conversation.waitForReplyTo("Saturday");+ await conversation.replyToEach({ [DAY_PROMPT]: "Saturday", [TIME_PROMPT]: "Afternoon" });+ const output = await conversation.waitForToolResult(TWO_QUESTIONS_TOOL);+ expect(output, `${TWO_QUESTIONS_TOOL} returned ${JSON.stringify(output)}`).toEqual({+ day: "Saturday",+ time: "Afternoon",+ });},},] as const satisfies readonly ContractRule[];@@ -537,6 +538,29 @@ const approvalRules = [await expectDeployed(conversation);},},+ {+ rule: "text replies answer two pending approvals one at a time, in the order shown",+ source: "docs/tools/human-in-the-loop.md#several-requests-at-once",+ requires: ["text-replies"],+ variesByConversation: true,+ async run(conversation) {+ await conversation.say(DEPLOY_AND_PUBLISH);+ await conversation.replyToEach({ [APPROVAL_PROMPT]: "approve", [PUBLISH_PROMPT]: "cancel" });+ await expectDeployed(conversation);+ expect(conversation.runsOf(SECOND_GATED_TOOL)).toBe(0);+ },+ },+ {+ rule: "text replies answer a question and an approval raised together, in the order shown",+ source: "docs/tools/human-in-the-loop.md#several-requests-at-once",+ requires: ["text-replies"],+ async run(conversation) {+ await conversation.say(ASK_AND_DEPLOY);+ await conversation.replyToEach({ [PROMPT]: "Saturday", [APPROVAL_PROMPT]: "approve" });+ // The turn replies only once both calls settle.+ await expectDeployed(conversation);+ },+ },{rule: "pressing Cancel stops the gated tool without running it",source: "docs/tools/human-in-the-loop.md#approvals",diff --git a/packages/eve/src/internal/testing/channel-conformance/harness.ts b/packages/eve/src/internal/testing/channel-conformance/harness.tsindex 15c80d18d..9e96a63a1 100644--- a/packages/eve/src/internal/testing/channel-conformance/harness.ts+++ b/packages/eve/src/internal/testing/channel-conformance/harness.ts@@ -198,10 +198,11 @@ export interface ChannelConversation {choose: (prompt: string, options: readonly RenderedOption[]) => RenderedOption,): Promise<void>;/**- * Waits until the turn holds for `prompt`, whether or not the client shows it- * yet. A client may show several pending requests one at a time.+ * Types the reply `replies` gives for each of its prompts, in the order the+ * client shows them. Two prompts shown in one message fail the conversation:+ * a typed reply couldn't say which one it answers.*/- waitForRequest(prompt: string): Promise<void>;+ replyToEach(replies: Readonly<Record<string, string>>): Promise<void>;/** `person`, Alice unless given, presses one rendered choice. */press(option: RenderedOption, person?: Person): Promise<void>;/** Waits until `tool` returns, as visible in the bot's reply, and returns its output. */@@ -289,6 +290,8 @@ export interface ClientView {export interface ShownQuestion {readonly options: readonly RenderedOption[];readonly prompt: string;+ /** Other prompts it was asked for that the same message shows. */+ readonly alongside?: readonly string[];}/**@@ -498,7 +501,10 @@ function webhookView(// After a prompt is asked, an optionless match is an edit of the answered message.if (promptsFrom.has(prompt) && options.length === 0) continue;promptsFrom.set(prompt, index + 1);- return { options, prompt };+ const alongside = prompts.filter(+ (other) => other !== prompt && driver.findOptions(call, other) !== undefined,+ );+ return { alongside, options, prompt };}}return undefined;@@ -808,7 +814,23 @@ async function converse(remaining.splice(remaining.indexOf(prompt), 1);}},- waitForRequest: (prompt) => holdForInput(prompt),+ async replyToEach(replies) {+ const remaining = Object.keys(replies);+ while (remaining.length > 0) {+ const { alongside = [], prompt } = await view.waitForQuestion(remaining);+ if (alongside.length > 0) {+ throw new Error(+ `${JSON.stringify([prompt, ...alongside])} were shown in one message, so a typed reply can't say which it answers.`,+ );+ }+ await holdForInput(prompt);+ await conversation.say(replies[prompt]!);+ // A person reads the bot's next prompt before typing again; replies sent faster+ // join one delivery, which answers nothing.+ await settledFor(prompt);+ remaining.splice(remaining.indexOf(prompt), 1);+ }+ },waitForToolResult: (tool) =>replyWait(`${tool} to return`, (reply) => readMockToolReply(reply, tool)),waitForReplyTo: (message) =>@@ -912,6 +934,20 @@ async function converse();}+ /** Waits until the session settled the request that asked `prompt`. */+ async function settledFor(prompt: string): Promise<void> {+ await wait(+ `the session to settle "${prompt}"`,+ async () => {+ for (const session of sessions.values()) {+ if (await settles(session, prompt)) return true;+ }+ return undefined;+ },+ () => "",+ );+ }+// The test file's workflow world closes after its last test, so a session// still writing then fails with an unhandled rejection.const settle = async () => {@@ -1031,6 +1067,35 @@ const asks = (prompt: string) => (event: MessageStreamEvent) =>event.type === "input.requested" &&event.data.requests.some((request) => request.prompt === prompt);+/** Whether `session` settled the request that asked `prompt`, answered or withdrawn. */+async function settles(session: Session, prompt: string): Promise<boolean> {+ const tail = await session.getStreamTailIndex();+ if (tail < 0) return false;+ const reader = (await session.getEventStream({ startIndex: 0 })).getReader();+ const requestIds = new Set<string>();+ try {+ for (let index = 0; index <= tail; index += 1) {+ const { done, value } = await reader.read();+ if (done) break;+ if (value.type === "input.requested") {+ for (const request of value.data.requests) {+ if (request.prompt === prompt) requestIds.add(request.requestId);+ }+ } else if (value.type === "approval.settled" && requestIds.has(value.data.requestId)) {+ return true;+ } else if (+ value.type === "input.resolved" &&+ value.data.resolutions.some((resolution) => requestIds.has(resolution.requestId))+ ) {+ return true;+ }+ }+ } finally {+ await reader.cancel();+ }+ return false;+}+/** Whether an event asks the person to sign in. */const isSignIn = (event: MessageStreamEvent) => event.type === "authorization.required";diff --git a/packages/eve/src/public/channels/chat-sdk/chatSdkChannel.test.ts b/packages/eve/src/public/channels/chat-sdk/chatSdkChannel.test.tsindex 7b60cbb24..7c94237fd 100644--- a/packages/eve/src/public/channels/chat-sdk/chatSdkChannel.test.ts+++ b/packages/eve/src/public/channels/chat-sdk/chatSdkChannel.test.ts@@ -6,6 +6,7 @@ import { callAdapterEventHandler, type ChannelAdapter } from "#channel/adapter.jimport { isCompiledChannel, type CompiledChannel } from "#channel/compiled-channel.js";import { isHttpRouteDefinition } from "#channel/routes.js";import { ContextContainer, contextStorage } from "#context/container.js";+import { enterSessionProjection, recordPublishedEvent } from "#harness/session-machine/current.js";import { SessionKey } from "#context/keys.js";import { mockChannelContext } from "#internal/testing/mocks/mock-channel-operations.js";import type { UnstampedMessageStreamEvent } from "#protocol/message.js";@@ -65,7 +66,10 @@ function withState(adapter: ChannelAdapter<any>, state: ChatSdkChannelState): Ch}function stubAccessor() {- return { get: () => undefined, set: () => {} } as any;+ const accessor = { get: () => undefined, set: () => {} } as any;+ // A step enters its projection before it publishes.+ enterSessionProjection(accessor, undefined);+ return accessor;}const stubAlsContext = (() => {@@ -86,6 +90,12 @@ function callEvent(return contextStorage.run(stubAlsContext, () => callAdapterEventHandler(adapter, event, ctx));}+/** Delivers `event` as a session publishes it: the handler runs, then the session records it. */+async function publishEvent(adapter: ChannelAdapter, event: UnstampedMessageStreamEvent, ctx: any) {+ await callEvent(adapter, event, ctx);+ recordPublishedEvent(ctx.ctx, event);+}+function makeEvent<T extends UnstampedMessageStreamEvent["type"]>(type: T,data: unknown,@@ -913,9 +923,12 @@ describe("chatSdkChannel", () => {const channelAdapter = withState(getAdapter(bridge.channel), {thread: serializedThread(),});- const ctx = buildAdapterContext(channelAdapter, stubAccessor());+ const accessor = stubAccessor();+ const ctx = buildAdapterContext(channelAdapter, accessor);+ // The channel reads the session's record of what it published.+ enterSessionProjection(accessor, undefined);- await callEvent(+ await publishEvent(channelAdapter,makeEvent("input.requested", {requests: [@@ -941,20 +954,38 @@ describe("chatSdkChannel", () => {ctx,);- const posted = adapter.posted[0]?.message as AdapterPostableMessage;- expect(posted).toMatchObject({+ expect(adapter.posted.map(({ message }) => message)).toMatchObject([+ {+ card: {+ children: [+ { content: "Which region?", type: "text" },+ { content: "Reply with your answer.", type: "text" },+ ],+ },+ fallbackText: "Which region?\n\nReply with your answer.",+ },+ ]);++ // A reply can only answer the request it sees, so the next one waits its turn.+ await publishEvent(+ channelAdapter,+ makeEvent("input.resolved", {+ resolutions: [{ kind: "question", outcome: "answered", requestId: "request-1" }],+ sequence: 1,+ stepIndex: 0,+ turnId: "turn-1",+ }),+ ctx,+ );+ expect(adapter.posted.at(-1)?.message).toMatchObject({card: {children: [- { content: "Which region?", type: "text" },- { content: "Reply with your answer.", type: "text" },{ content: "Which zone?", type: "text" },{ type: "actions" },{ content: "Or reply with your own answer.", type: "text" },],},- fallbackText:- "Which region?\n\nReply with your answer.\n\n" +- "Which zone?\n\n1. Washington\n\nReply with a number, or with your own answer.",+ fallbackText: "Which zone?\n\n1. Washington\n\nReply with a number, or with your own answer.",});});});diff --git a/packages/eve/src/public/channels/chat-sdk/chatSdkChannel.ts b/packages/eve/src/public/channels/chat-sdk/chatSdkChannel.tsindex 02f8d7ddb..c33a3be31 100644--- a/packages/eve/src/public/channels/chat-sdk/chatSdkChannel.ts+++ b/packages/eve/src/public/channels/chat-sdk/chatSdkChannel.ts@@ -1,3 +1,4 @@+import { type PromptQueueState, promptQueueEvents } from "#channel/prompt-queue.js";import type { UserContent } from "ai";import type {@@ -65,7 +66,7 @@ interface ActiveWebhookContext {const ActiveWebhookKey = new ContextKey<ActiveWebhookContext>("chat-sdk.active-webhook");/** Durable Chat SDK thread state plus default-handler streaming bookkeeping. */-export interface ChatSdkChannelState extends Record<string, unknown> {+export interface ChatSdkChannelState extends Record<string, unknown>, PromptQueueState {thread: SerializedThread | null;/** Message id of the in-flight streamed assistant post (edit fallback). */anchorMessageId?: string | null;@@ -378,6 +379,60 @@ export function chatSdkChannel<TAdapters extends ChatSdkAdapters>(function defaultEvents<TAdapters extends ChatSdkAdapters>(inputActionPrefix: string,): ChatSdkChannelEvents<TAdapters> {+ type EventChannel = Parameters<+ NonNullable<ChatSdkChannelEvents<TAdapters>["input.requested"]>+ >[1];++ async function showPrompt(channel: EventChannel, request: InputRequest) {+ if (!channel.thread) return false;+ const posted = await channel.thread.post(renderInputRequests([request], inputActionPrefix));+ if (!posted.id || channel.state.editSupported === false) return;+ channel.state.pendingInputCards = {+ ...channel.state.pendingInputCards,+ [posted.id]: { requests: [request], resolved: {} },+ };+ }++ async function clearAnsweredCards(+ event: { readonly resolutions: readonly InputResolution[] },+ channel: EventChannel,+ ) {+ const thread = channel.thread;+ if (!thread) return;+ for (const [messageId, card] of Object.entries(channel.state.pendingInputCards ?? {})) {+ const resolutions = event.resolutions.filter((resolution) =>+ card.requests.some((request) => request.requestId === resolution.requestId),+ );+ if (resolutions.length === 0) continue;+ const resolved = {+ ...card.resolved,+ ...Object.fromEntries(resolutions.map((resolution) => [resolution.requestId, resolution])),+ };+ const { [messageId]: _, ...rest } = channel.state.pendingInputCards ?? {};+ channel.state.pendingInputCards = card.requests.every(+ (request) => resolved[request.requestId] !== undefined,+ )+ ? rest+ : { ...rest, [messageId]: { requests: card.requests, resolved } };+ try {+ await thread.adapter.editMessage(+ thread.id,+ messageId,+ renderInputRequests(card.requests, inputActionPrefix, resolved),+ );+ } catch (error) {+ if (!isNotImplemented(error)) {+ log.warn("answered input card edit failed", { error, messageId });+ continue;+ }+ channel.state.editSupported = false;+ channel.state.pendingInputCards = {};+ return;+ }+ }+ }++ const prompts = promptQueueEvents(showPrompt);return {...defaultAuthorizationEvents(),async "turn.started"(_event, channel, _ctx) {@@ -425,54 +480,13 @@ function defaultEvents<TAdapters extends ChatSdkAdapters>(clearStream(channel.state);}},- async "input.requested"(event, channel, _ctx) {- if (!channel.thread || event.requests.length === 0) return;- const posted = await channel.thread.post(- renderInputRequests(event.requests, inputActionPrefix),- );- if (!posted.id || channel.state.editSupported === false) return;- channel.state.pendingInputCards = {- ...channel.state.pendingInputCards,- [posted.id]: { requests: event.requests, resolved: {} },- };- },+ // Some adapters show only text, where a reply can answer only the request+ // it sees, so cards post one at a time.+ ...prompts,// Covers every way a request ends: a press, a typed answer, or a withdrawal.async "input.resolved"(event, channel, _ctx) {- const thread = channel.thread;- if (!thread) return;- for (const [messageId, card] of Object.entries(channel.state.pendingInputCards ?? {})) {- const resolutions = event.resolutions.filter((resolution) =>- card.requests.some((request) => request.requestId === resolution.requestId),- );- if (resolutions.length === 0) continue;- const resolved = {- ...card.resolved,- ...Object.fromEntries(- resolutions.map((resolution) => [resolution.requestId, resolution]),- ),- };- const { [messageId]: _, ...rest } = channel.state.pendingInputCards ?? {};- channel.state.pendingInputCards = card.requests.every(- (request) => resolved[request.requestId] !== undefined,- )- ? rest- : { ...rest, [messageId]: { requests: card.requests, resolved } };- try {- await thread.adapter.editMessage(- thread.id,- messageId,- renderInputRequests(card.requests, inputActionPrefix, resolved),- );- } catch (error) {- if (!isNotImplemented(error)) {- log.warn("answered input card edit failed", { error, messageId });- continue;- }- channel.state.editSupported = false;- channel.state.pendingInputCards = {};- return;- }- }+ await clearAnsweredCards(event, channel);+ await prompts["input.resolved"](event, channel);},async "message.completed"(event, channel, _ctx) {if (event.finishReason === "tool-calls") {diff --git a/packages/eve/src/public/channels/github/defaults.ts b/packages/eve/src/public/channels/github/defaults.tsindex 920f86fe4..15c202fef 100644--- a/packages/eve/src/public/channels/github/defaults.ts+++ b/packages/eve/src/public/channels/github/defaults.ts@@ -3,6 +3,7 @@ import type { SandboxSession } from "#shared/sandbox-session.js";type NetworkPolicySandboxSession = SandboxSession & {setNetworkPolicy(policy: SandboxNetworkPolicy): Promise<void>;};+import { promptQueueEvents } from "#channel/prompt-queue.js";import { renderTextInputRequest } from "#channel/resolve-text.js";import type { SessionAuthContext } from "#channel/types.js";@@ -19,6 +20,7 @@ import {} from "#public/channels/github/inbound.js";import type {GitHubChannelEvents,+ GitHubEventContext,GitHubInboundContext,GitHubInboundResult,GitHubProgressConfig,@@ -91,6 +93,13 @@ interface GitHubDefaultEventOptions {/** Builds GitHub's built-in event handlers for acknowledgement and terminal output. */export function createDefaultEvents(options: GitHubDefaultEventOptions = {}): GitHubChannelEvents {+ async function showPrompt(channel: GitHubEventContext, request: InputRequest): Promise<void> {+ const sections = [renderInputRequest(request)];+ const replyInstruction = renderReplyInstruction(request, await options.botName?.());+ if (replyInstruction !== undefined) sections.push(replyInstruction);+ await postCommentChunks(channel, sections.join("\n\n"));+ }+return {async "turn.started"(_event, channel, ctx) {if (options.progress?.reactions !== false) {@@ -109,13 +118,8 @@ export function createDefaultEvents(options: GitHubDefaultEventOptions = {}): Giawait postCommentChunks(channel, event.message);},- async "input.requested"(event, channel, _ctx) {- if (event.requests.length === 0) return;- const sections = event.requests.map(renderInputRequest);- const replyInstruction = renderReplyInstruction(event.requests, await options.botName?.());- if (replyInstruction !== undefined) sections.push(replyInstruction);- await postCommentChunks(channel, sections.join("\n\n"));- },+ // A comment can only answer the prompt it sees, so prompts post one at a time.+ ...promptQueueEvents(showPrompt),async "session.failed"(event, channel) {const hint = formatErrorHint(event);@@ -153,13 +157,12 @@ function renderInputRequest(request: InputRequest): string {// The default onComment hook only dispatches comments that @mention the bot,// so a prompt without this instruction invites replies that are silently ignored.function renderReplyInstruction(- requests: readonly InputRequest[],+ request: InputRequest,botName: string | undefined,): string | undefined {const name = botName?.trim();if (!name) return undefined;- const firstOption = requests.find((request) => (request.options?.length ?? 0) > 0)?.options?.[0];- const example = firstOption?.label ?? "<your answer>";+ const example = request.options?.[0]?.label ?? "<your answer>";return `Answer by mentioning me in a reply, e.g. \`@${name} ${example}\`.`;}diff --git a/packages/eve/src/public/channels/github/githubChannel.test.ts b/packages/eve/src/public/channels/github/githubChannel.test.tsindex 5c36d8a8c..0169bf3dc 100644--- a/packages/eve/src/public/channels/github/githubChannel.test.ts+++ b/packages/eve/src/public/channels/github/githubChannel.test.ts@@ -5,6 +5,7 @@ import { callAdapterEventHandler, type ChannelAdapter } from "#channel/adapter.jimport { isCompiledChannel, type CompiledChannel } from "#channel/compiled-channel.js";import { isHttpRouteDefinition } from "#channel/routes.js";import { ContextContainer, contextStorage } from "#context/container.js";+import { enterSessionProjection } from "#harness/session-machine/current.js";import { SandboxKey, SessionKey } from "#context/keys.js";import { mockChannelContext } from "#internal/testing/mocks/mock-channel-operations.js";import { mockSandbox, type MockSandbox } from "#internal/testing/mocks/mock-sandbox.js";@@ -62,7 +63,10 @@ function withState(}function stubAccessor() {- return { get: () => undefined, set: () => {} } as any;+ const accessor = { get: () => undefined, set: () => {} } as any;+ // A step enters its projection before it publishes.+ enterSessionProjection(accessor, undefined);+ return accessor;}function createAlsContext(sandbox?: MockSandbox): ContextContainer {diff --git a/packages/eve/src/public/channels/github/githubChannel.ts b/packages/eve/src/public/channels/github/githubChannel.tsindex 303ca3052..0c4a977f3 100644--- a/packages/eve/src/public/channels/github/githubChannel.ts+++ b/packages/eve/src/public/channels/github/githubChannel.ts@@ -133,7 +133,8 @@ type GitHubSessionFailedHandler = (/*** Event handlers for `githubChannel({ events })`. The channel installs built-in* handlers for `turn.started` (eyes reaction plus repo checkout),- * `message.completed` (posts the reply), `input.requested` (posts the prompt),+ * `message.completed` (posts the reply), `input.requested`, `input.resolved`, and+ * `approval.settled` (post each prompt in turn),* and `session.failed`/`turn.failed` (posts an error comment). A handler supplied* here replaces the built-in for that key rather than running alongside it.*/@@ -144,6 +145,8 @@ export interface GitHubChannelEvents {readonly "authorization.completed"?: GitHubEventHandler<"authorization.completed">;readonly "authorization.required"?: GitHubEventHandler<"authorization.required">;readonly "input.requested"?: GitHubEventHandler<"input.requested">;+ readonly "input.resolved"?: GitHubEventHandler<"input.resolved">;+ readonly "approval.settled"?: GitHubEventHandler<"approval.settled">;readonly "message.appended"?: GitHubEventHandler<"message.appended">;readonly "message.completed"?: GitHubEventHandler<"message.completed">;readonly "session.completed"?: GitHubEventHandler<"session.completed">;diff --git a/packages/eve/src/public/channels/github/state.ts b/packages/eve/src/public/channels/github/state.tsindex 9c34b9162..1a455e351 100644--- a/packages/eve/src/public/channels/github/state.ts+++ b/packages/eve/src/public/channels/github/state.ts@@ -1,3 +1,4 @@+import type { PromptQueueState } from "#channel/prompt-queue.js";import {githubContinuationToken,type GitHubCiEvent,@@ -23,7 +24,7 @@ import {* null. The default `turn.started` handler fills them in once it checks out* the repository, which is why the fields are mutable.*/-export interface GitHubChannelState {+export interface GitHubChannelState extends PromptQueueState {baseRef: string | null;baseSha: string | null;checkoutPath: string | null;diff --git a/packages/eve/src/public/channels/linear/defaults.ts b/packages/eve/src/public/channels/linear/defaults.tsindex a128fb920..10e514cb6 100644--- a/packages/eve/src/public/channels/linear/defaults.ts+++ b/packages/eve/src/public/channels/linear/defaults.ts@@ -1,3 +1,4 @@+import { promptQueueEvents } from "#channel/prompt-queue.js";import type { SessionAuthContext } from "#channel/types.js";import { extractErrorId, formatErrorHint } from "#internal/logging.js";@@ -9,8 +10,10 @@ import {} from "#public/channels/linear/hitl.js";import type { LinearAgentSessionEvent, LinearUser } from "#public/channels/linear/inbound.js";import type { SessionContext } from "#public/definitions/callback-context.js";+import type { InputRequest } from "#shared/input.js";import type {LinearChannelEvents,+ LinearEventContext,LinearInboundResult,LinearSessionContext,} from "#public/channels/linear/linearChannel.js";@@ -64,6 +67,15 @@ interface LinearDefaultEventOptions {/** Built-in Linear event handlers for Agent Activity progress, replies, HITL, and errors. */export function createDefaultEvents(options: LinearDefaultEventOptions = {}): LinearChannelEvents {+ async function showPrompt(channel: LinearEventContext, request: InputRequest): Promise<void> {+ await postActivity(+ channel,+ options,+ { body: renderLinearInputRequests([request]), type: "elicitation" },+ linearInputRequestSignal([request]),+ );+ }+return {async "turn.started"(_event, channel, _ctx) {channel.state.pendingToolCallMessage = null;@@ -131,18 +143,8 @@ export function createDefaultEvents(options: LinearDefaultEventOptions = {}): Li}},- async "input.requested"(event, channel, _ctx) {- const signal = linearInputRequestSignal(event.requests);- await postActivity(- channel,- options,- {- body: renderLinearInputRequests(event.requests),- type: "elicitation",- },- signal,- );- },+ // A reply can only answer the elicitation it sees, so they post one at a time.+ ...promptQueueEvents(showPrompt),async "authorization.required"(event, channel, ctx) {const displayName = authorizationDisplayName(event.name, event.authorization?.displayName);diff --git a/packages/eve/src/public/channels/linear/linearChannel.test.ts b/packages/eve/src/public/channels/linear/linearChannel.test.tsindex c6478c803..07afea3e6 100644--- a/packages/eve/src/public/channels/linear/linearChannel.test.ts+++ b/packages/eve/src/public/channels/linear/linearChannel.test.ts@@ -5,6 +5,7 @@ import { callAdapterEventHandler, type ChannelAdapter } from "#channel/adapter.jimport { isCompiledChannel, type CompiledChannel } from "#channel/compiled-channel.js";import { isHttpRouteDefinition } from "#channel/routes.js";import { ContextContainer, contextStorage } from "#context/container.js";+import { enterSessionProjection } from "#harness/session-machine/current.js";import { SessionKey } from "#context/keys.js";import { mockChannelContext } from "#internal/testing/mocks/mock-channel-operations.js";import type { UnstampedMessageStreamEvent } from "#protocol/message.js";@@ -34,7 +35,10 @@ function withState(}function stubAccessor() {- return { get: () => undefined, set: () => {} } as any;+ const accessor = { get: () => undefined, set: () => {} } as any;+ // A step enters its projection before it publishes.+ enterSessionProjection(accessor, undefined);+ return accessor;}const stubAlsContext = (() => {diff --git a/packages/eve/src/public/channels/linear/linearChannel.ts b/packages/eve/src/public/channels/linear/linearChannel.tsindex 10a265cc2..50aa43e0d 100644--- a/packages/eve/src/public/channels/linear/linearChannel.ts+++ b/packages/eve/src/public/channels/linear/linearChannel.ts@@ -1,3 +1,4 @@+import type { PromptQueueState } from "#channel/prompt-queue.js";import type { SessionHandle } from "#channel/session.js";import type { SessionAuthContext, TurnPolicy } from "#channel/types.js";import type { ChannelFrom } from "#channel/channel-operations.js";@@ -43,7 +44,7 @@ type EventData<T extends UnstampedMessageStreamEvent["type"]> =Extract<UnstampedMessageStreamEvent, { type: T }> extends { data: infer D } ? D : undefined;/** JSON-serializable state for one Linear Agent Session conversation. */-export interface LinearChannelState {+export interface LinearChannelState extends PromptQueueState {readonly agentSessionId: string | null;readonly agentSessionUrl?: string | null;readonly commentId?: string | null;@@ -144,6 +145,8 @@ export interface LinearChannelEvents {readonly "message.completed"?: LinearEventHandler<"message.completed">;readonly "message.appended"?: LinearEventHandler<"message.appended">;readonly "input.requested"?: LinearEventHandler<"input.requested">;+ readonly "input.resolved"?: LinearEventHandler<"input.resolved">;+ readonly "approval.settled"?: LinearEventHandler<"approval.settled">;readonly "turn.failed"?: LinearEventHandler<"turn.failed">;readonly "turn.completed"?: LinearEventHandler<"turn.completed">;readonly "turn.cancelled"?: LinearEventHandler<"turn.cancelled">;diff --git a/packages/eve/src/public/channels/twilio/defaults.ts b/packages/eve/src/public/channels/twilio/defaults.tsindex 3bcafbbe3..593b063d2 100644--- a/packages/eve/src/public/channels/twilio/defaults.ts+++ b/packages/eve/src/public/channels/twilio/defaults.ts@@ -1,3 +1,4 @@+import { promptQueueEvents } from "#channel/prompt-queue.js";import { renderTextInputRequest } from "#channel/resolve-text.js";import type { SessionAuthContext } from "#channel/types.js";@@ -10,6 +11,7 @@ import type {import type {TwilioChannelEvents,TwilioContext,+ TwilioEventContext,TwilioInboundResult,TwilioVoiceResult,} from "#public/channels/twilio/twilioChannel.js";@@ -78,10 +80,8 @@ export const defaultEvents: TwilioChannelEvents = {await channel.twilio.sendMessage(event.message);},- async "input.requested"(event, channel, _ctx) {- if (event.requests.length === 0) return;- await channel.twilio.sendMessage(renderTwilioInputRequests(event.requests));- },+ // SMS has no buttons, so a reply can only answer the request it sees.+ ...promptQueueEvents(showPrompt),// An SMS thread is one person's, so the link and code can go in the message.async "authorization.required"(event, channel, _ctx) {@@ -137,23 +137,17 @@ export const defaultEvents: TwilioChannelEvents = {},};-// SMS has no buttons, so options are numbered. The batch goes out as one message-// because a text reply resolves against every pending request at once.-function renderTwilioInputRequests(requests: readonly InputRequest[]): string {- const sections = requests.map(renderTextInputRequest);- const hasOptions = requests.some((request) => (request.options ?? []).length > 0);- if (hasOptions) {- const freeform = requests.some((request) => request.allowFreeform === true);- sections.push(- [- freeform- ? "Reply with a number, or with your own answer."- : "Reply with a number to choose.",- ...(requests.length > 1 ? ["Your reply answers each of these."] : []),- ].join(" "),- );+async function showPrompt(channel: TwilioEventContext, request: InputRequest): Promise<void> {+ const body = renderTextInputRequest(request);+ if ((request.options ?? []).length === 0) {+ await channel.twilio.sendMessage(body);+ return;}- return sections.join("\n\n");+ const instruction =+ request.allowFreeform === true+ ? "Reply with a number, or with your own answer."+ : "Reply with a number to choose.";+ await channel.twilio.sendMessage(`${body}\n\n${instruction}`);}function renderAuthorizationOutcome(input: {diff --git a/packages/eve/src/public/channels/twilio/twilioChannel.test.ts b/packages/eve/src/public/channels/twilio/twilioChannel.test.tsindex 9611447c7..c3904d99f 100644--- a/packages/eve/src/public/channels/twilio/twilioChannel.test.ts+++ b/packages/eve/src/public/channels/twilio/twilioChannel.test.ts@@ -5,6 +5,7 @@ import { callAdapterEventHandler, type ChannelAdapter } from "#channel/adapter.jimport { isCompiledChannel, type CompiledChannel } from "#channel/compiled-channel.js";import { isHttpRouteDefinition } from "#channel/routes.js";import { ContextContainer, contextStorage } from "#context/container.js";+import { enterSessionProjection, recordPublishedEvent } from "#harness/session-machine/current.js";import { SessionKey } from "#context/keys.js";import { mockChannelContext } from "#internal/testing/mocks/mock-channel-operations.js";import type { UnstampedMessageStreamEvent } from "#protocol/message.js";@@ -56,6 +57,12 @@ function callEvent(return contextStorage.run(stubAlsContext, () => callAdapterEventHandler(adapter, event, ctx));}+/** Delivers `event` as a session publishes it: the handler runs, then the session records it. */+async function publishEvent(adapter: ChannelAdapter, event: UnstampedMessageStreamEvent, ctx: any) {+ await callEvent(adapter, event, ctx);+ recordPublishedEvent(ctx.ctx, event);+}+function makeEvent<T extends UnstampedMessageStreamEvent["type"]>(type: T,data: unknown,@@ -667,7 +674,7 @@ describe("twilioChannel() default event handlers", () => {});});- it("input.requested sends a batch of approvals as one SMS, since one reply answers them all", async () => {+ it("sends a batch of approvals one SMS at a time, since a reply answers only the one it sees", async () => {const bodies: string[] = [];const fetchMock: typeof fetch = async (_input, init) => {bodies.push(new URLSearchParams(String(init?.body)).get("Body") ?? "");@@ -685,8 +692,12 @@ describe("twilioChannel() default event handlers", () => {),{ from: "+15551234567", lastCallSid: null, lastMessageSid: "SM123", to: "+15557654321" },);+ const accessor = stubAccessor();+ const ctx = buildAdapterContext(adapter, accessor);+ // The channel reads the session's record of what it published.+ enterSessionProjection(accessor, undefined);- await callEvent(+ await publishEvent(adapter,makeEvent("input.requested", {requests: [@@ -719,16 +730,43 @@ describe("twilioChannel() default event handlers", () => {stepIndex: 0,turnId: "t1",}),- buildAdapterContext(adapter, stubAccessor()),+ ctx,);-expect(bodies).toEqual([- [- "Approve Deploy release?\n\n1. Approve\n2. Cancel",- "Approve Rotate keys?\n\n1. Approve\n2. Cancel",- "Reply with a number to choose. Your reply answers each of these.",- ].join("\n\n"),+ "Approve Deploy release?\n\n1. Approve\n2. Cancel\n\nReply with a number to choose.",]);++ await publishEvent(+ adapter,+ makeEvent("approval.settled", {+ outcome: "approved",+ requestId: "approval_1",+ responderPrincipalId: "twilio:+15551234567",+ sequence: 0,+ stepIndex: 0,+ turnId: "t1",+ }),+ ctx,+ );+ expect(bodies.at(-1)).toBe(+ "Approve Rotate keys?\n\n1. Approve\n2. Cancel\n\nReply with a number to choose.",+ );++ // The batch resolving later reports both requests again; nothing more is sent.+ await publishEvent(+ adapter,+ makeEvent("input.resolved", {+ resolutions: [+ { kind: "tool-approval", outcome: "approved", requestId: "approval_1" },+ { kind: "tool-approval", outcome: "approved", requestId: "approval_2" },+ ],+ sequence: 0,+ stepIndex: 0,+ turnId: "t1",+ }),+ ctx,+ );+ expect(bodies).toHaveLength(2);});it("receive starts a phone-pair session with an explicit continuation token", async () => {diff --git a/packages/eve/src/public/channels/twilio/twilioChannel.ts b/packages/eve/src/public/channels/twilio/twilioChannel.tsindex fd50b4056..82011640a 100644--- a/packages/eve/src/public/channels/twilio/twilioChannel.ts+++ b/packages/eve/src/public/channels/twilio/twilioChannel.ts@@ -1,3 +1,4 @@+import type { PromptQueueState } from "#channel/prompt-queue.js";import type { SessionHandle } from "#channel/session.js";import type { SessionAuthContext, TurnPolicy } from "#channel/types.js";import type { RouteHandler } from "#channel/routes.js";@@ -67,7 +68,7 @@ interface TwilioChannelContext extends TwilioContext {export interface TwilioEventContext extends TwilioChannelContext, ChannelContinuationOps {}/** JSON-serializable state for the phone-number conversation. */-export interface TwilioChannelState {+export interface TwilioChannelState extends PromptQueueState {/** Caller / sender phone number. */from: string | null;/** Twilio number or sender that received the latest session-starting webhook. */@@ -164,6 +165,8 @@ export interface TwilioChannelEvents {readonly "message.completed"?: TwilioEventHandler<"message.completed">;readonly "message.appended"?: TwilioEventHandler<"message.appended">;readonly "input.requested"?: TwilioEventHandler<"input.requested">;+ readonly "input.resolved"?: TwilioEventHandler<"input.resolved">;+ readonly "approval.settled"?: TwilioEventHandler<"approval.settled">;readonly "turn.failed"?: TwilioEventHandler<"turn.failed">;readonly "turn.completed"?: TwilioEventHandler<"turn.completed">;readonly "turn.cancelled"?: TwilioEventHandler<"turn.cancelled">;diff --git a/packages/eve/src/subagents/hitl-proxy.test.ts b/packages/eve/src/subagents/hitl-proxy.test.tsindex 92a45d6e2..e55e5ef4f 100644--- a/packages/eve/src/subagents/hitl-proxy.test.ts+++ b/packages/eve/src/subagents/hitl-proxy.test.ts@@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest";import { upsertProxyInputRequests } from "#harness/proxy-input-requests.js";import type { HarnessSession } from "#harness/types.js";+import { withParkedStep } from "#internal/testing/session-machine.js";import { routeDeliverPayload } from "#subagents/hitl-proxy.js";const REQUEST_EVENT = { sequence: 0, stepIndex: 0, turnId: "turn_0" };@@ -337,9 +338,9 @@ describe("routeDeliverPayload message resolution", () => {]);});- it("keeps a message for the turn when several questions are pending", () => {+ it("answers the first of several pending questions", () => {const routed = routeDeliverPayload({- payload: { message: "Actually, check the logs first." },+ payload: { message: "production" },resolveMessage: true,state: askSession([["ask-1", {}],@@ -347,20 +348,76 @@ describe("routeDeliverPayload message resolution", () => {]).state,});+ expect(routed.forSelf).toBeUndefined();+ expect(routed.forChildren.map((child) => child.payload.inputResponses)).toEqual([+ [{ optionId: "2", requestId: "ask-1" }],+ ]);+ });++ it("keeps a message for the turn when it doesn't answer the first question", () => {+ const routed = routeDeliverPayload({+ payload: { message: "Actually, check the logs first." },+ resolveMessage: true,+ state: askSession([+ ["ask-1", {}],+ ["ask-2", { allowFreeform: true }],+ ]).state,+ });+expect(routed.forSelf).toEqual({ message: "Actually, check the logs first." });expect(routed.forChildren).toEqual([]);});- it("does not answer a question while a subagent question is also pending", () => {+ it("leaves a message for the turn while one of its approvals comes first", () => {+ const session = withParkedStep(askSession([["ask-1", { allowFreeform: true }]]), {+ requests: [+ {+ action: { callId: "call-1", input: {}, kind: "tool-call", toolName: "deploy" },+ display: "confirmation",+ kind: "tool-approval",+ options: [+ { id: "approve", label: "Approve" },+ { id: "cancel", label: "Cancel" },+ ],+ prompt: "Approve deploy?",+ requestId: "approval-1",+ },+ ],+ });+ const routed = routeDeliverPayload({+ payload: { message: "approve" },+ resolveMessage: true,+ state: session.state,+ });++ expect(routed.forSelf).toEqual({ message: "approve" });+ expect(routed.forChildren).toEqual([]);+ });++ it("does not answer a question while a subagent question comes first", () => {const session = upsertProxyInputRequests({entries: [[- "child-ask",- { childContinuationToken: "child-token", event: REQUEST_EVENT, kind: "question" },+ "ask-1",+ {+ workflowAsk: { control: "control-ask-1", question: { allowFreeform: true } },+ childContinuationToken: "hook-ask-1",+ event: REQUEST_EVENT,+ kind: "question",+ },],],- forChildContinuationToken: "child-token",- session: askSession([["ask-1", { allowFreeform: true }]]),+ forChildContinuationToken: "hook-ask-1",+ session: upsertProxyInputRequests({+ entries: [+ [+ "child-ask",+ { childContinuationToken: "child-token", event: REQUEST_EVENT, kind: "question" },+ ],+ ],+ forChildContinuationToken: "child-token",+ session: createSession(),+ }),});const routed = routeDeliverPayload({payload: { message: "Use the canary pool" },diff --git a/packages/eve/src/subagents/hitl-proxy.ts b/packages/eve/src/subagents/hitl-proxy.tsindex d5113dd7d..83e5c33ae 100644--- a/packages/eve/src/subagents/hitl-proxy.ts+++ b/packages/eve/src/subagents/hitl-proxy.ts@@ -1,6 +1,8 @@import type { SessionInboxAddress } from "#execution/session-inbox/address.js";import type { DeliverPayload } from "#channel/types.js";import { resolveInputOutcome } from "#harness/input-request-resolution.js";+import { firstOpenInput } from "#harness/open-input-request.js";+import { storedProjection } from "#harness/session-machine/view.js";import type { StepCoordinates as PendingInputBatchEvent } from "#harness/session-machine/view.js";import { getProxyInputRequests } from "#harness/proxy-input-requests.js";import type { WorkflowAskRoute, ProxyInputRequest } from "#harness/proxy-input-requests.js";@@ -90,6 +92,7 @@ export function routeDeliverPayload(input: {entries,payload: input.payload,routable,+ state: input.state,});const [textAnswer] = message.responses;const inputResponses = [...(input.payload.inputResponses ?? []), ...message.responses];@@ -246,6 +249,7 @@ function resolveMessageAgainstQuestions(input: {readonly entries: ReadonlyMap<string, ProxyInputRequest>;readonly payload: DeliverPayload;readonly routable: (requestId: string, route: ProxyInputRequest) => boolean;+ readonly state: SessionStateMap | undefined;}): {readonly consumed: boolean;readonly responses: readonly InputResponse[];@@ -253,27 +257,26 @@ function resolveMessageAgainstQuestions(input: {const none = { consumed: false, responses: [] };// An explicit structured answer means the client already chose what to answer.if (!input.enabled || (input.payload.inputResponses?.length ?? 0) > 0) return none;- if (input.payload.message === undefined) return none;+ const text = readAnswerText(input.payload);+ if (text === undefined) return none;+ // A request an earlier payload answered is no longer open to this one.+ const answered = (id: string) => {+ const route = input.entries.get(id);+ return route !== undefined && !input.routable(id, route);+ };+ // A relay the session recorded without publishing it falls back to the order it was recorded.+ const requestId =+ firstOpenInput(storedProjection(input.state), answered)?.request.requestId ??+ [...input.entries.keys()].find((id) => !answered(id));+ const route = requestId === undefined ? undefined : input.entries.get(requestId);+ if (requestId === undefined || route?.kind !== "question") return none;// Task and subagent questions carry no `ctx.ask()` metadata, so plain text- // cannot resolve them, but they still make the message ambiguous.- const pending = [...input.entries].filter(- ([requestId, route]) => route.kind === "question" && input.routable(requestId, route),- );- const questions = pending.flatMap(([requestId, route]) => {- const question = route.workflowAsk?.question ?? route.question;- return question !== undefined ? [{ requestId, ...question }] : [];- });- if (questions.length === 0) return none;-- const [only] = questions;- const text = readAnswerText(input.payload);+ // cannot resolve them.+ const question = route.workflowAsk?.question ?? route.question;const answer =- pending.length === 1 && only !== undefined && text !== undefined- ? resolveTextToResponse(text, only)- : undefined;- if (answer !== undefined) return { consumed: true, responses: [answer] };- return none;+ question === undefined ? undefined : resolveTextToResponse(text, { requestId, ...question });+ return answer === undefined ? none : { consumed: true, responses: [answer] };}function batchResolves(input: {