|
1 | 1 | import { |
| 2 | + ApprovalRequestId, |
| 3 | + EventId, |
2 | 4 | CheckpointRef, |
3 | 5 | CommandId, |
4 | 6 | DEFAULT_PROVIDER_INTERACTION_MODE, |
@@ -99,6 +101,168 @@ const hasMetricSnapshot = ( |
99 | 101 | ); |
100 | 102 |
|
101 | 103 | 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 | + |
102 | 266 | it("bootstraps command handling from persisted projections without reading the full snapshot", async () => { |
103 | 267 | let nextSequence = 8; |
104 | 268 | const eventStore: OrchestrationEventStoreShape = { |
|
0 commit comments