Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

fix(eve): record a text answer to ctx.ask as message.received (#4305)

e0-gh-vercel-connect[bot]12ced990b0cd
diff --git a/.changeset/text-answer-message-received.md b/.changeset/text-answer-message-received.md
new file mode 100644
index 000000000..a95b162b3
--- /dev/null
+++ b/.changeset/text-answer-message-received.md
@@ -0,0 +1,5 @@
+---
+"eve": patch
+---
+
+A plain-text message that answers a pending `ctx.ask()` question is now recorded as `message.received`, stamped with its delivery id, before the `input.resolved` it produces. Clients rendering from the session stream now show what the person typed, and an optimistic copy of the message reconciles instead of lingering. The answer joins the open turn like a steering message, so the turn's later events carry its delivery id too. Model history is unchanged.
diff --git a/docs/concepts/sessions-runs-and-streaming.md b/docs/concepts/sessions-runs-and-streaming.md
index cbcd11718..361edd02a 100644
--- a/docs/concepts/sessions-runs-and-streaming.md
+++ b/docs/concepts/sessions-runs-and-streaming.md
@@ -219,7 +219,7 @@ Message sends default to `"steer"`. Before assistant output begins, eve interrup
If the session is waiting on a human-in-the-loop approval, respond with the channel’s Approve or Cancel controls. Text that doesn't match an option doesn't approve the call. From the person the turn serves, it steers the turn and cancels the approval, even when sent with `turnPolicy: "queue"`, since the turn can't end until that person acts; from anyone else, it waits until the turn ends. If they had already approved some calls in the batch, those calls still run. Cancelling the turn withdraws the approval, and a later answer to it approves nothing.
-A pending `ctx.ask()` question from a tool, such as `ask_question`, can be answered with plain text. When it is the only pending question, a message that matches an option answers it, and so does any message when the question allows free text. Otherwise eve does not guess which question the text addresses: the message follows the normal `turnPolicy`. A steering message withdraws the questions of the `execute` workflow tool calls the turn waits on, which resolve as `cancelled`; a task's questions stay open. Questions from subagents always need a structured response. Use structured responses to target requests unambiguously.
+A pending `ctx.ask()` question from a tool, such as `ask_question`, can be answered with plain text. When it is the only pending question, a message that matches an option answers it, and so does any message when the question allows free text. The stream records that message as `message.received`, carrying its delivery id, just before the `input.resolved` it produces. Otherwise eve does not guess which question the text addresses: the message follows the normal `turnPolicy`. A steering message withdraws the questions of the `execute` workflow tool calls the turn waits on, which resolve as `cancelled`; a task's questions stay open. Questions from subagents always need a structured response. Use structured responses to target requests unambiguously.
A structured response matches any currently pending request by ID, not only the newest batch. It becomes stale only after that request was answered, cleared, or cancelled. eve delivers a stale response to the model as a new user message, and the model decides whether the old selection still matters. A stale approval never authorizes the earlier tool call; the model must request the action and approval again if they are still needed.
diff --git a/packages/eve/src/execution/proxied-deliver-step.ts b/packages/eve/src/execution/proxied-deliver-step.ts
index d1029d0dd..0c4a1e2dd 100644
--- a/packages/eve/src/execution/proxied-deliver-step.ts
+++ b/packages/eve/src/execution/proxied-deliver-step.ts
@@ -2,7 +2,7 @@ import type { SessionInboxAddress } from "#execution/session-inbox/address.js";
import { hasDelegatedSessionContext } from "#execution/delegated-session-context.js";
import { buildAdapterContext } from "#channel/adapter-context.js";
import type { DeliverHookPayload, DeliverPayload } from "#channel/types.js";
-import { AuthKey } from "#context/keys.js";
+import { AuthKey, TurnDeliveryIdsKey } from "#context/keys.js";
import { setChannelContext } from "#execution/channel-context.js";
import { coalesceDeliverPayloads } from "#execution/deliver-payloads.js";
import {
@@ -10,7 +10,12 @@ import {
readDurableSession,
replaceDurableSessionSnapshot,
} from "#execution/durable-session-store.js";
-import { relaySessionEvents, type SessionStepState } from "#execution/publish-session-events.js";
+import {
+ publishSessionEvents,
+ relaySessionEvents,
+ type PublishedSessionEvents,
+ type SessionStepState,
+} from "#execution/publish-session-events.js";
import {
withSessionStateDelta,
type WithSessionStateDelta,
@@ -31,6 +36,7 @@ import type { PendingInputBatchEvent } from "#harness/pending-input-batches.js";
import type { WorkflowAskRoute } from "#harness/proxy-input-requests.js";
import {
createInputResolvedEvent,
+ createMessageReceivedEvent,
type InputResolution,
type UnstampedMessageStreamEvent,
} from "#protocol/message.js";
@@ -95,6 +101,10 @@ async function routeProxiedDeliver(
// resolved by an earlier payload is hidden from later ones; its run takes
// one answer, and later messages must reach the parent instead.
const resolvedQuestions = new Set<string>();
+ // A message that answers a question is still the person's turn in the
+ // conversation, so the stream records it with its delivery ids.
+ const answerMessages: UnstampedMessageStreamEvent[] = [];
+ const answerDeliveryIds: string[] = [];
for (const [sourcePayloadIndex, payload] of sourceDelivery.payloads.entries()) {
const routed = routeDeliverPayload({
@@ -110,6 +120,17 @@ async function routeProxiedDeliver(
if (forChild.workflowAsk !== undefined) {
for (const { requestId } of forChild.resolved.resolutions) resolvedQuestions.add(requestId);
}
+ if (forChild.message !== undefined) {
+ const { sequence, turnId } = forChild.resolved.event;
+ answerMessages.push(
+ createMessageReceivedEvent({ message: forChild.message, sequence, turnId }),
+ );
+ for (const metadata of sourceDelivery.deliveryMetadata ?? []) {
+ if (metadata.payloadIndex === sourcePayloadIndex) {
+ answerDeliveryIds.push(metadata.deliveryId);
+ }
+ }
+ }
const key = JSON.stringify([
forChild.childContinuationToken,
forChild.childSessionInbox?.sessionId ?? "",
@@ -206,14 +227,26 @@ async function routeProxiedDeliver(
resolvedEvents.push(createTurnWaitingOnInputEvent(durableSession));
}
+ let published: PublishedSessionEvents = {
+ serializedContext,
+ sessionState: retired
+ ? replaceDurableSessionSnapshot({ session: durableSession, state: input.sessionState })
+ : input.sessionState,
+ };
+ if (answerMessages.length > 0) {
+ // Like a steering message, the answer joins the open turn, so the turn's
+ // later events carry its delivery ids too.
+ published = await publishSessionEvents(
+ {
+ serializedContext: joinTurnDeliveryIds(published.serializedContext, answerDeliveryIds),
+ sessionState: published.sessionState,
+ sessionWritable: input.sessionWritable,
+ },
+ answerMessages,
+ );
+ }
const context = await relaySessionEvents(
- {
- serializedContext,
- sessionState: retired
- ? replaceDurableSessionSnapshot({ session: durableSession, state: input.sessionState })
- : input.sessionState,
- sessionWritable: input.sessionWritable,
- },
+ { ...published, sessionWritable: input.sessionWritable },
resolvedEvents,
);
if (parentAction !== undefined) return { ...context, ...parentAction };
@@ -234,6 +267,19 @@ async function routeProxiedDeliver(
return { ...context, kind: "continue", remainder };
}
+function joinTurnDeliveryIds(
+ serializedContext: Record<string, unknown>,
+ deliveryIds: readonly string[],
+): Record<string, unknown> {
+ if (deliveryIds.length === 0) return serializedContext;
+ const current =
+ (serializedContext[TurnDeliveryIdsKey.name] as readonly string[] | undefined) ?? [];
+ return {
+ ...serializedContext,
+ [TurnDeliveryIdsKey.name]: [...new Set([...current, ...deliveryIds])],
+ };
+}
+
/**
* Maps a delivery's channel-specific answers to the requests a held turn waits
* on, so the turn can tell they answer it. Returns the mapped delivery, or
diff --git a/packages/eve/src/execution/tools/workflow/workflow-tool-run.integration.test.ts b/packages/eve/src/execution/tools/workflow/workflow-tool-run.integration.test.ts
index 79d3a477a..b697ff753 100644
--- a/packages/eve/src/execution/tools/workflow/workflow-tool-run.integration.test.ts
+++ b/packages/eve/src/execution/tools/workflow/workflow-tool-run.integration.test.ts
@@ -251,6 +251,74 @@ describe("workflow tools", () => {
expect(output.unexpected(workflowSdkNotice.unpinnedDelivery)).toEqual([]);
}, 60_000);
+ it("records the message that answers a pending ctx.ask question", async () => {
+ const runtime = await createWorkflowToolRuntime({
+ agentName: "workflow-tool-text-answer",
+ execute: confirmDeployWorkflow,
+ toolName: "confirm_deploy",
+ });
+
+ await runtime.run(async () => {
+ const run = await start(workflowEntry, [
+ {
+ kind: "initial",
+ ownerDeploymentId: "dpl_inline",
+ input: { message: 'Run confirm_deploy with service "api"' },
+ serializedContext: buildWorkflowToolSerializedContext({
+ continuationToken: "http:workflow-tool-text-answer",
+ requestInput: true,
+ }),
+ },
+ ]);
+ const stream = captureTurnEvents(run);
+ try {
+ const asked = await stream.nextTurn();
+ const request = (
+ filterEventsByType(asked, "input.requested")[0] as InputRequestedStreamEvent
+ ).data.requests[0]!;
+ await resumeSessionInbox(sessionCommandHookToken(run.runId), {
+ delivery: { channelKind: "http", channelName: "test", deliveryId: "delivery-answer" },
+ kind: "send",
+ payload: { message: "Deploy" },
+ });
+
+ const answered = await stream.nextTurn();
+ expect(filterEventsByType(answered, "input.resolved")).toMatchObject([
+ {
+ data: {
+ resolutions: [
+ {
+ outcome: "answered",
+ requestId: request.requestId,
+ response: { optionId: "approve" },
+ },
+ ],
+ },
+ },
+ ]);
+ // A client renders the person's answer from the stream and reconciles
+ // its optimistic copy by delivery id, so the message must be recorded.
+ expect(
+ filterEventsByType(answered, "message.received"),
+ JSON.stringify(answered.map((event) => [event.type, event.meta.deliveryIds])),
+ ).toMatchObject([
+ { data: { message: "Deploy" }, meta: { deliveryIds: ["delivery-answer"] } },
+ ]);
+ expect(answered.slice(0, 2).map((event) => event.type)).toEqual([
+ "message.received",
+ "input.resolved",
+ ]);
+ // `send()` follows its delivery id to the turn boundary.
+ expect(answered.find((event) => event.type === "turn.completed")?.meta).toMatchObject({
+ deliveryIds: ["delivery-answer"],
+ });
+ } finally {
+ stream.dispose();
+ await run.cancel();
+ }
+ });
+ }, 60_000);
+
it("exposes the principal that answered ctx.ask", async () => {
const alice = {
attributes: {},
diff --git a/packages/eve/src/subagents/hitl-proxy.ts b/packages/eve/src/subagents/hitl-proxy.ts
index 45409ca41..21718ab82 100644
--- a/packages/eve/src/subagents/hitl-proxy.ts
+++ b/packages/eve/src/subagents/hitl-proxy.ts
@@ -91,6 +91,8 @@ export interface RoutedChildDelivery {
readonly inputSource?: string;
readonly childContinuationToken: string;
readonly childSessionInbox?: SessionInboxAddress;
+ /** The person's message whose text answered this bucket's question. */
+ readonly message?: DeliverPayload["message"];
readonly payload: { readonly inputResponses: readonly InputResponse[] };
/** What forwarding this bucket resolves on the routing session. */
readonly resolved: ProxiedInputResolutions;
@@ -158,6 +160,7 @@ export function routeDeliverPayload(input: {
payload: input.payload,
routable,
});
+ const [textAnswer] = message.responses;
const inputResponses = [...(input.payload.inputResponses ?? []), ...message.responses];
const responsesByChild = new Map<string, ChildResponseBucket>();
@@ -248,6 +251,8 @@ export function routeDeliverPayload(input: {
resolutions: resolveRetiredRequests({ entries, responses, retireRequestIds }),
},
...(childSessionInbox !== undefined && { childSessionInbox }),
+ ...(textAnswer !== undefined &&
+ responses.includes(textAnswer) && { message: input.payload.message }),
...(workflowAsk !== undefined && { workflowAsk }),
...(remote !== undefined && { remote }),
...(routes[0]?.inputSource !== undefined && { inputSource: routes[0].inputSource }),