Skip to content

Commit 375ba22

Browse files
committed
feat(codex): support async questions
1 parent e01c153 commit 375ba22

17 files changed

Lines changed: 846 additions & 19 deletions

File tree

‎apps/mobile/src/lib/threadActivity.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,29 @@ const nativeQuestion = {
8888
} as const;
8989

9090
describe("pending user input answers", () => {
91+
it("accepts free-text answers to async questions without options", () => {
92+
const question = {
93+
id: "0",
94+
header: "Question",
95+
question: "What should it be named?",
96+
options: [],
97+
allowCustomAnswer: true,
98+
multiSelect: false,
99+
};
100+
const requested = makeActivity({
101+
id: EventId.make("async-question"),
102+
kind: "user-input.requested",
103+
summary: "User input requested",
104+
createdAt: "2026-09-03T00:00:00.000Z",
105+
payload: { requestId: "async-1", responseMode: "message", questions: [question] },
106+
});
107+
const questions = derivePendingUserInputs([requested])[0]?.questions;
108+
expect(questions).toEqual([question]);
109+
expect(buildPendingUserInputAnswers(questions!, { "0": { customAnswer: "Example" } })).toEqual({
110+
"0": "Example",
111+
});
112+
});
113+
91114
it("preserves native choice values and custom-answer rules from activities", () => {
92115
const requested = makeActivity({
93116
id: EventId.make("native-question"),

‎apps/mobile/src/lib/threadActivity.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ function parseUserInputQuestions(
245245
};
246246
})
247247
.filter((option): option is UserInputQuestion["options"][number] => option !== null);
248-
if (options.length === 0) {
248+
if (options.length === 0 && question.allowCustomAnswer === false) {
249249
return null;
250250
}
251251
return {

‎apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts‎

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import {
2+
ApprovalRequestId,
3+
EventId,
24
CheckpointRef,
35
CommandId,
46
DEFAULT_PROVIDER_INTERACTION_MODE,
@@ -99,6 +101,168 @@ const hasMetricSnapshot = (
99101
);
100102

101103
describe("OrchestrationEngine", () => {
104+
it.each(["running", "stopped"] as const)(
105+
"sends async answers as messages with a %s session and rejects duplicate replies",
106+
async (status) => {
107+
const system = await createOrchestrationSystem();
108+
const threadId = ThreadId.make("async-thread");
109+
const projectId = ProjectId.make("async-project");
110+
const requestId = ApprovalRequestId.make("codex-async:question-1");
111+
try {
112+
await system.run(
113+
system.engine.dispatch({
114+
type: "project.create",
115+
commandId: CommandId.make("async-project"),
116+
projectId,
117+
title: "Async questions",
118+
workspaceRoot: "/tmp/async-questions",
119+
createdAt: now(),
120+
}),
121+
);
122+
await system.run(
123+
system.engine.dispatch({
124+
type: "thread.create",
125+
commandId: CommandId.make("async-thread"),
126+
threadId,
127+
projectId,
128+
title: "Async questions",
129+
modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
130+
runtimeMode: "full-access",
131+
interactionMode: "default",
132+
branch: null,
133+
worktreePath: null,
134+
createdAt: now(),
135+
}),
136+
);
137+
await system.run(
138+
system.engine.dispatch({
139+
type: "thread.session.set",
140+
commandId: CommandId.make("async-session"),
141+
threadId,
142+
createdAt: now(),
143+
session: {
144+
threadId,
145+
status,
146+
providerName: "codex",
147+
runtimeMode: "full-access",
148+
activeTurnId: status === "running" ? TurnId.make("turn-1") : null,
149+
lastError: null,
150+
updatedAt: now(),
151+
},
152+
}),
153+
);
154+
await system.run(
155+
system.engine.dispatch({
156+
type: "thread.activity.append",
157+
commandId: CommandId.make("async-question"),
158+
threadId,
159+
createdAt: now(),
160+
activity: {
161+
id: EventId.make("async-question"),
162+
kind: "user-input.requested",
163+
summary: "User input requested",
164+
tone: "info",
165+
turnId: TurnId.make("turn-1"),
166+
createdAt: now(),
167+
payload: {
168+
requestId,
169+
responseMode: "message",
170+
questions: [
171+
{
172+
id: "0",
173+
header: "Question",
174+
question: "Which package manager?",
175+
options: [{ label: "pnpm", description: "" }],
176+
},
177+
{
178+
id: "1",
179+
header: "Question",
180+
question: "What should it be named?",
181+
options: [],
182+
},
183+
],
184+
},
185+
},
186+
}),
187+
);
188+
// The question must stay available when later work fills the activity window.
189+
for (let index = 0; index < 501; index += 1) {
190+
await system.run(
191+
system.engine.dispatch({
192+
type: "thread.activity.append",
193+
commandId: CommandId.make(`work-${index}`),
194+
threadId,
195+
createdAt: "2026-01-01T00:00:01.000Z",
196+
activity: {
197+
id: EventId.make(`work-${index}`),
198+
kind: "tool.completed",
199+
summary: "Work continued",
200+
payload: {},
201+
tone: "info",
202+
turnId: TurnId.make("turn-1"),
203+
createdAt: "2026-01-01T00:00:01.000Z",
204+
},
205+
}),
206+
);
207+
}
208+
const before = await system.readModel();
209+
expect(
210+
before.threads[0]?.activities.some((activity) => activity.id === "async-question"),
211+
).toBe(true);
212+
const response = {
213+
type: "thread.user-input.respond" as const,
214+
commandId: CommandId.make("async-response"),
215+
threadId,
216+
requestId,
217+
answers: { "0": "pnpm", "1": "Example" },
218+
createdAt: "2026-01-01T00:00:02.000Z",
219+
};
220+
await expect(
221+
system.run(
222+
system.engine.dispatch({
223+
...response,
224+
commandId: CommandId.make("incomplete-answer"),
225+
answers: { "0": "pnpm" },
226+
}),
227+
),
228+
).rejects.toThrow("Answer each question before sending.");
229+
await system.run(system.engine.dispatch(response));
230+
const after = await system.readModel();
231+
const userMessages = after.threads[0]?.messages.filter(
232+
(message) => message.role === "user",
233+
);
234+
expect(userMessages).toHaveLength(1);
235+
expect(userMessages?.[0]?.text).toBe(
236+
"Which package manager?\npnpm\n\nWhat should it be named?\nExample",
237+
);
238+
expect(
239+
after.threads[0]?.activities.find((activity) => activity.kind === "user-input.resolved")
240+
?.payload,
241+
).toMatchObject({ requestId, responseMode: "message", answers: response.answers });
242+
const events = await system.run(Stream.runCollect(system.engine.readEvents(0)));
243+
expect(
244+
Array.from(events)
245+
.filter((event) => event.commandId === response.commandId)
246+
.map((event) => event.type),
247+
).toEqual([
248+
"thread.activity-appended",
249+
"thread.message-sent",
250+
"thread.turn-start-requested",
251+
]);
252+
await expect(
253+
system.run(
254+
system.engine.dispatch({
255+
...response,
256+
commandId: CommandId.make("second-client-reply"),
257+
}),
258+
),
259+
).rejects.toThrow("This question has already been answered.");
260+
} finally {
261+
await system.dispose();
262+
}
263+
},
264+
);
265+
102266
it("bootstraps command handling from persisted projections without reading the full snapshot", async () => {
103267
let nextSequence = 8;
104268
const eventStore: OrchestrationEventStoreShape = {

‎apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2108,6 +2108,56 @@ describe("ProviderRuntimeIngestion", () => {
21082108
expect(message?.streaming).toBe(false);
21092109
});
21102110

2111+
it("keeps streaming while an async question is pending", async () => {
2112+
const harness = await createHarness({ serverSettings: { enableLegacyTokenStreaming: true } });
2113+
const base = {
2114+
provider: ProviderDriverKind.make("codex"),
2115+
createdAt: "2026-01-01T00:00:00.000Z",
2116+
threadId: asThreadId("thread-1"),
2117+
turnId: asTurnId("turn-async"),
2118+
};
2119+
harness.emit({ ...base, type: "turn.started", eventId: asEventId("async-start") });
2120+
harness.emit({
2121+
...base,
2122+
type: "content.delta",
2123+
eventId: asEventId("async-before"),
2124+
itemId: asItemId("message-1"),
2125+
payload: { streamKind: "assistant_text", delta: "Before. " },
2126+
});
2127+
harness.emit({
2128+
...base,
2129+
type: "user-input.requested",
2130+
eventId: asEventId("async-request"),
2131+
requestId: ApprovalRequestId.make("codex-async:question-1"),
2132+
payload: {
2133+
responseMode: "message",
2134+
questions: [
2135+
{
2136+
id: "0",
2137+
header: "Question",
2138+
question: "Which name?",
2139+
options: [],
2140+
allowCustomAnswer: true,
2141+
},
2142+
],
2143+
},
2144+
});
2145+
harness.emit({
2146+
...base,
2147+
type: "content.delta",
2148+
eventId: asEventId("async-after"),
2149+
itemId: asItemId("message-1"),
2150+
payload: { streamKind: "assistant_text", delta: "After." },
2151+
});
2152+
await harness.drain();
2153+
const thread = (await harness.readModel()).threads[0];
2154+
expect(thread?.session?.status).toBe("running");
2155+
expect(thread?.messages).toMatchObject([{ text: "Before. After.", streaming: true }]);
2156+
expect(
2157+
thread?.activities.find((activity) => activity.kind === "user-input.requested")?.payload,
2158+
).toMatchObject({ responseMode: "message", requestId: "codex-async:question-1" });
2159+
});
2160+
21112161
it("does not create assistant segments for whitespace-only buffered text at approval boundaries", async () => {
21122162
const harness = await createHarness();
21132163
const startedAt = "2026-03-28T06:28:00.000Z";

‎apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ export function runtimeEventToActivities(
520520
payload: {
521521
...(event.requestId ? { requestId: event.requestId } : {}),
522522
questions: event.payload.questions,
523+
...(event.payload.responseMode ? { responseMode: event.payload.responseMode } : {}),
523524
},
524525
turnId: toTurnId(event.turnId) ?? null,
525526
...maybeSequence,
@@ -1739,7 +1740,8 @@ const make = Effect.gen(function* () {
17391740
}
17401741

17411742
const pauseForUserTurnId =
1742-
event.type === "request.opened" || event.type === "user-input.requested"
1743+
event.type === "request.opened" ||
1744+
(event.type === "user-input.requested" && event.payload.responseMode !== "message")
17431745
? toTurnId(event.turnId)
17441746
: undefined;
17451747
if (pauseForUserTurnId) {

0 commit comments

Comments
 (0)