The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import { assert, it } from "@effect/vitest"; |
| 2 | import * as TestClock from "effect/testing/TestClock"; |
| 3 | import { |
| 4 | ChatAttachmentId, |
| 5 | COMPOSER_CONTEXT_TERMINAL_TEXT_MAX_CHARS, |
| 6 | ComposerContextId, |
| 7 | CommandId, |
| 8 | EnvironmentId, |
| 9 | MessageId, |
| 10 | EventId, |
| 11 | NodeId, |
| 12 | ProjectId, |
| 13 | ProviderDriverKind, |
| 14 | ProviderInstanceId, |
| 15 | ProviderThreadId, |
| 16 | ProviderTurnId, |
| 17 | PROVIDER_SEND_TURN_MAX_INPUT_CHARS, |
| 18 | ScheduledTaskId, |
| 19 | ThreadId, |
| 20 | TurnItemId, |
| 21 | type ModelSelection, |
| 22 | type OrchestrationV2DomainEvent, |
| 23 | } from "@t3tools/contracts"; |
| 24 | import { |
| 25 | expandAssistantCitationsForProvider, |
| 26 | serializeAssistantCitation, |
| 27 | } from "@t3tools/shared/assistantCitations"; |
| 28 | import { projectComposerContextForProvider } from "@t3tools/shared/composerContextReferences"; |
| 29 | import * as DateTime from "effect/DateTime"; |
| 30 | import * as Deferred from "effect/Deferred"; |
| 31 | import * as Effect from "effect/Effect"; |
| 32 | import * as Fiber from "effect/Fiber"; |
| 33 | import * as Layer from "effect/Layer"; |
| 34 | import * as Queue from "effect/Queue"; |
| 35 | import * as Stream from "effect/Stream"; |
| 36 | import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts"; |
| 37 | import * as EffectWorker from "./EffectWorker.ts"; |
| 38 | import * as EffectOutbox from "./EffectOutbox.ts"; |
| 39 | import * as EventSink from "./EventSink.ts"; |
| 40 | import * as ThreadCommandExecutor from "./ThreadCommandExecutor.ts"; |
| 41 | import * as Orchestrator from "./Orchestrator.ts"; |
| 42 | import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts"; |
| 43 | import * as ProviderReplayHarness from "./testkit/ProviderReplayHarness.ts"; |
| 44 | import * as ThreadManagementService from "./ThreadManagementService.ts"; |
| 45 | import * as ServerSettings from "../serverSettings.ts"; |
| 46 | import * as SqlitePersistence from "../persistence/Sqlite.ts"; |
| 47 | import * as CommandReceiptStore from "./CommandReceiptStore.ts"; |
| 48 | import { userFacingDispatchErrorMessage } from "./UserFacingErrors.ts"; |
| 49 | import { checkpointWorkspace } from "@t3tools/provider-testing/replayWorkspace"; |
| 50 | import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter"; |
| 51 | import * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests"; |
| 52 | |
| 53 | const driver = ProviderDriverKind.make("codex"); |
| 54 | const instanceId = ProviderInstanceId.make("codex"); |
| 55 | const modelSelection = { instanceId, model: "test-model" }; |
| 56 | |
| 57 | it.effect.each( |
| 58 | ( |
| 59 | [ |
| 60 | [false, "during delivery"], |
| 61 | [false, "before dispatch"], |
| 62 | [true, "after delivery"], |
| 63 | [true, "without native steering"], |
| 64 | [true, "with interrupting native steering"], |
| 65 | [true, "settled only"], |
| 66 | [false, "during continuation"], |
| 67 | ["question", "during delivery"], |
| 68 | ] as const |
| 69 | ).map(([kind, timing]) => ({ |
| 70 | mailbox: kind === true, |
| 71 | question: kind === "question", |
| 72 | timing, |
| 73 | label: |
| 74 | kind === "question" ? "delegated task question" : kind ? "mailbox notification" : "steering", |
| 75 | })), |
| 76 | )("delivers $label when completion wins $timing", ({ mailbox, question, timing }) => |
| 77 | Effect.gen(function* () { |
| 78 | const cwd = yield* checkpointWorkspace(`steering-completion-${timing.replaceAll(" ", "-")}`); |
| 79 | const events = yield* Queue.unbounded<ProviderAdapter.ProviderAdapterV2Event>(); |
| 80 | const started: ProviderAdapter.ProviderAdapterV2TurnInput[] = []; |
| 81 | const steerEntered = yield* Deferred.make<void>(); |
| 82 | const rejectSteer = yield* Deferred.make<void>(); |
| 83 | let steerCalls = 0; |
| 84 | const capabilities = { |
| 85 | ...CodexProviderCapabilitiesV2, |
| 86 | turns: { |
| 87 | ...CodexProviderCapabilitiesV2.turns, |
| 88 | supportsActiveSteering: timing !== "without native steering", |
| 89 | activeSteeringInterruptsTools: timing === "with interrupting native steering", |
| 90 | }, |
| 91 | }; |
| 92 | const adapter: ProviderAdapter.ProviderAdapterV2["Service"] = { |
| 93 | instanceId, |
| 94 | driver, |
| 95 | getCapabilities: () => Effect.succeed(capabilities), |
| 96 | planSelectionTransition: () => Effect.succeed({ type: "apply_on_next_turn" }), |
| 97 | openSession: (input) => |
| 98 | Effect.gen(function* () { |
| 99 | const now = yield* DateTime.now; |
| 100 | return { |
| 101 | instanceId, |
| 102 | driver, |
| 103 | providerSessionId: input.providerSessionId, |
| 104 | providerSession: { |
| 105 | id: input.providerSessionId, |
| 106 | driver, |
| 107 | providerInstanceId: instanceId, |
| 108 | status: "ready", |
| 109 | cwd, |
| 110 | model: modelSelection.model, |
| 111 | capabilities, |
| 112 | createdAt: now, |
| 113 | updatedAt: now, |
| 114 | lastError: null, |
| 115 | }, |
| 116 | events: Stream.fromQueue(events), |
| 117 | ensureThread: ({ threadId }) => |
| 118 | Effect.succeed({ |
| 119 | id: ProviderThreadId.make(`provider-thread:${threadId}`), |
| 120 | driver, |
| 121 | providerInstanceId: instanceId, |
| 122 | providerSessionId: input.providerSessionId, |
| 123 | appThreadId: threadId, |
| 124 | ownerNodeId: null, |
| 125 | nativeThreadRef: { driver, nativeId: "native-thread", strength: "strong" }, |
| 126 | nativeConversationHeadRef: null, |
| 127 | status: "idle", |
| 128 | firstRunOrdinal: null, |
| 129 | lastRunOrdinal: null, |
| 130 | handoffIds: [], |
| 131 | forkedFrom: null, |
| 132 | createdAt: now, |
| 133 | updatedAt: now, |
| 134 | }), |
| 135 | resumeThread: ({ providerThread }) => Effect.succeed(providerThread), |
| 136 | startTurn: (turn) => |
| 137 | Effect.gen(function* () { |
| 138 | started.push(turn); |
| 139 | yield* Queue.offer(events, { |
| 140 | type: "provider_turn.updated", |
| 141 | driver, |
| 142 | providerTurn: { |
| 143 | id: ProviderTurnId.make(`provider-turn:${turn.attemptId}`), |
| 144 | providerThreadId: turn.providerThread.id, |
| 145 | nodeId: turn.rootNodeId, |
| 146 | runAttemptId: turn.attemptId, |
| 147 | nativeTurnRef: { |
| 148 | driver, |
| 149 | nativeId: `native:${turn.attemptId}`, |
| 150 | strength: "strong", |
| 151 | }, |
| 152 | ordinal: turn.providerTurnOrdinal, |
| 153 | status: "running", |
| 154 | startedAt: now, |
| 155 | completedAt: null, |
| 156 | }, |
| 157 | }); |
| 158 | }), |
| 159 | steerTurn: (turn) => |
| 160 | Effect.gen(function* () { |
| 161 | steerCalls += 1; |
| 162 | if (timing === "after delivery" || timing === "during continuation") return; |
| 163 | yield* Deferred.succeed(steerEntered, undefined); |
| 164 | yield* Deferred.await(rejectSteer); |
| 165 | return yield* new ProviderAdapter.ProviderAdapterSteerRunError({ |
| 166 | driver, |
| 167 | providerThreadId: turn.providerThread.id, |
| 168 | providerTurnId: turn.providerTurnId, |
| 169 | cause: "turn already completed", |
| 170 | }); |
| 171 | }), |
| 172 | interruptTurn: () => Effect.void, |
| 173 | respondToRuntimeRequest: () => Effect.void, |
| 174 | readThreadSnapshot: () => Effect.die("unused"), |
| 175 | rollbackThread: () => Effect.die("unused"), |
| 176 | forkThread: () => Effect.die("unused"), |
| 177 | }; |
| 178 | }), |
| 179 | }; |
| 180 | yield* Effect.gen(function* () { |
| 181 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 182 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 183 | const threadId = ThreadId.make("thread:steering-completion"); |
| 184 | const watch = (predicate: (event: OrchestrationV2DomainEvent) => boolean) => |
| 185 | orchestrator.streamDomainEvents.pipe( |
| 186 | Stream.filter(predicate), |
| 187 | Stream.take(1), |
| 188 | Stream.runDrain, |
| 189 | Effect.forkScoped, |
| 190 | ); |
| 191 | yield* orchestrator.dispatch({ |
| 192 | type: "thread.create", |
| 193 | commandId: CommandId.make("create"), |
| 194 | threadId, |
| 195 | projectId: ProjectId.make("project:steering-completion"), |
| 196 | title: "Steering race", |
| 197 | modelSelection, |
| 198 | runtimeMode: "full-access", |
| 199 | interactionMode: "default", |
| 200 | branch: null, |
| 201 | worktreePath: cwd, |
| 202 | createdBy: "user", |
| 203 | creationSource: "web", |
| 204 | }); |
| 205 | yield* orchestrator.dispatch({ |
| 206 | type: "message.dispatch", |
| 207 | commandId: CommandId.make("first"), |
| 208 | threadId, |
| 209 | messageId: MessageId.make("message:first"), |
| 210 | text: "first", |
| 211 | attachments: [], |
| 212 | dispatchMode: { type: "start_immediately" }, |
| 213 | createdBy: "user", |
| 214 | creationSource: "web", |
| 215 | }); |
| 216 | const running = yield* watch( |
| 217 | (event) => event.type === "provider-turn.updated" && event.payload.status === "running", |
| 218 | ); |
| 219 | yield* worker.drain(); |
| 220 | yield* Fiber.join(running); |
| 221 | const first = started[0]!; |
| 222 | const messageId = MessageId.make("message:steering"); |
| 223 | const taskId = NodeId.make("task:mailbox"); |
| 224 | if (mailbox) { |
| 225 | const sink = yield* EventSink.EventSinkV2; |
| 226 | const current = yield* orchestrator.getThreadProjection(threadId); |
| 227 | const parentRun = current.runs.find((run) => run.id === first.runId)!; |
| 228 | const now = yield* DateTime.now; |
| 229 | yield* sink.write({ |
| 230 | events: [ |
| 231 | { |
| 232 | id: EventId.make("mailbox:cohort"), |
| 233 | type: "run.updated", |
| 234 | threadId, |
| 235 | runId: first.runId, |
| 236 | occurredAt: now, |
| 237 | payload: { |
| 238 | ...parentRun, |
| 239 | delegatedCompletion: { |
| 240 | disposition: "open", |
| 241 | nextGeneration: 2, |
| 242 | delivery: { generation: 1, messageId, taskIds: [taskId] }, |
| 243 | }, |
| 244 | }, |
| 245 | }, |
| 246 | { |
| 247 | id: EventId.make("mailbox:task"), |
| 248 | type: "subagent.updated", |
| 249 | threadId, |
| 250 | runId: first.runId, |
| 251 | nodeId: taskId, |
| 252 | occurredAt: now, |
| 253 | payload: { |
| 254 | id: taskId, |
| 255 | threadId, |
| 256 | runId: first.runId, |
| 257 | parentNodeId: first.rootNodeId, |
| 258 | origin: "app_owned", |
| 259 | createdBy: "agent", |
| 260 | driver, |
| 261 | providerInstanceId: instanceId, |
| 262 | providerThreadId: null, |
| 263 | childThreadId: null, |
| 264 | nativeTaskRef: null, |
| 265 | prompt: "Do background work", |
| 266 | title: "Background test", |
| 267 | model: null, |
| 268 | completionWake: timing === "settled only" ? "settled_only" : "always", |
| 269 | completionDelivery: { state: "claimed", observedByRunId: null }, |
| 270 | status: "completed", |
| 271 | result: "done", |
| 272 | startedAt: now, |
| 273 | completedAt: now, |
| 274 | updatedAt: now, |
| 275 | }, |
| 276 | }, |
| 277 | ], |
| 278 | }); |
| 279 | } |
| 280 | if (question) { |
| 281 | const { thread } = yield* orchestrator.getThreadProjection(threadId); |
| 282 | yield* (yield* EventSink.EventSinkV2).write({ |
| 283 | events: [ |
| 284 | { |
| 285 | id: EventId.make("event:composer-selection"), |
| 286 | type: "thread.model-selection-updated", |
| 287 | threadId, |
| 288 | providerInstanceId: instanceId, |
| 289 | occurredAt: yield* DateTime.now, |
| 290 | payload: { ...thread, modelSelection: composerSelection }, |
| 291 | }, |
| 292 | ], |
| 293 | }); |
| 294 | } |
| 295 | const dispatchSteer = orchestrator.dispatch({ |
| 296 | type: "message.dispatch", |
| 297 | commandId: CommandId.make("steer"), |
| 298 | threadId, |
| 299 | messageId, |
| 300 | text: "fix the popover", |
| 301 | attachments: [ |
| 302 | { |
| 303 | type: "image", |
| 304 | id: "steering-screenshot", |
| 305 | name: "image.png", |
| 306 | mimeType: "image/png", |
| 307 | sizeBytes: 10, |
| 308 | }, |
| 309 | ], |
| 310 | dispatchMode: |
| 311 | mailbox || question |
| 312 | ? { type: "queue_after_active" } |
| 313 | : { type: "steer_active", targetRunId: first.runId }, |
| 314 | createdBy: mailbox || question ? "agent" : "user", |
| 315 | creationSource: mailbox || question ? "server" : "web", |
| 316 | ...(question |
| 317 | ? { |
| 318 | notification: { |
| 319 | source: { kind: "delegated_task", taskIds: [taskId] }, |
| 320 | outcome: "updated", |
| 321 | summary: "Delegated task has a question", |
| 322 | }, |
| 323 | } |
| 324 | : {}), |
| 325 | ...(mailbox |
| 326 | ? { |
| 327 | delegatedCompletion: { |
| 328 | parentRunId: first.runId, |
| 329 | generation: 1, |
| 330 | taskIds: [taskId], |
| 331 | }, |
| 332 | } |
| 333 | : {}), |
| 334 | }); |
| 335 | if (timing !== "before dispatch") yield* dispatchSteer; |
| 336 | if (timing === "during continuation") { |
| 337 | const projection = yield* orchestrator.getThreadProjection(threadId); |
| 338 | const turn = projection.providerTurns[0]!; |
| 339 | const failed = yield* watch( |
| 340 | (event) => event.type === "provider-turn.updated" && event.payload.status === "failed", |
| 341 | ); |
| 342 | yield* Queue.offer(events, { |
| 343 | type: "provider_turn.updated", |
| 344 | driver, |
| 345 | providerTurn: { ...turn, status: "failed", completedAt: yield* DateTime.now }, |
| 346 | }); |
| 347 | yield* Fiber.join(failed); |
| 348 | yield* worker.drain(); |
| 349 | assert.equal(steerCalls, 1); |
| 350 | assert.lengthOf(started, 1); |
| 351 | const continued = yield* orchestrator.getThreadProjection(threadId); |
| 352 | assert.equal(continued.runs[0]?.status, "running"); |
| 353 | assert.equal( |
| 354 | continued.messages.find((message) => message.id === messageId)?.runId, |
| 355 | first.runId, |
| 356 | ); |
| 357 | return; |
| 358 | } |
| 359 | if (timing === "after delivery") yield* worker.drain(); |
| 360 | const delivery = |
| 361 | timing === "during delivery" ? yield* worker.runOnce().pipe(Effect.forkScoped) : null; |
| 362 | if (delivery !== null) yield* Deferred.await(steerEntered); |
| 363 | const completed = yield* watch( |
| 364 | (event) => |
| 365 | event.type === "run.updated" && |
| 366 | event.payload.id === first.runId && |
| 367 | event.payload.status === "waiting", |
| 368 | ); |
| 369 | const projection = yield* orchestrator.getThreadProjection(threadId); |
| 370 | const turn = projection.providerTurns[0]!; |
| 371 | yield* Queue.offer(events, { |
| 372 | type: "provider_turn.updated", |
| 373 | driver, |
| 374 | providerTurn: { ...turn, status: "completed", completedAt: yield* DateTime.now }, |
| 375 | }); |
| 376 | yield* Queue.offer(events, { |
| 377 | type: "turn.terminal", |
| 378 | driver, |
| 379 | providerThreadId: turn.providerThreadId, |
| 380 | providerTurnId: turn.id, |
| 381 | runOrdinal: first.runOrdinal, |
| 382 | status: "completed", |
| 383 | failure: null, |
| 384 | threadDisposition: "reusable", |
| 385 | }); |
| 386 | yield* Fiber.join(completed); |
| 387 | if (delivery !== null) { |
| 388 | yield* Deferred.succeed(rejectSteer, undefined); |
| 389 | yield* Fiber.join(delivery); |
| 390 | } |
| 391 | if (delivery !== null && !question) { |
| 392 | const requeued = yield* orchestrator.getThreadProjection(threadId); |
| 393 | assert.isTrue(requeued.runs.some((run) => run.status === "queued")); |
| 394 | assert.isFalse( |
| 395 | requeued.visibleTurnItems.some( |
| 396 | ({ item }) => item.type === "user_message" && item.messageId === messageId, |
| 397 | ), |
| 398 | "a steer that fell back to the queue must not appear delivered", |
| 399 | ); |
| 400 | const item = requeued.turnItems.find( |
| 401 | (item) => item.type === "user_message" && item.messageId === messageId, |
| 402 | ); |
| 403 | assert.equal(item?.status, "pending"); |
| 404 | assert.isNull(item?.startedAt); |
| 405 | assert.isNull(item?.completedAt); |
| 406 | } |
| 407 | if (timing === "before dispatch") yield* dispatchSteer; |
| 408 | yield* worker.drain(); |
| 409 | yield* orchestrator.resumeQueuedRuns; |
| 410 | yield* worker.drain(); |
| 411 | if (timing === "after delivery") { |
| 412 | assert.equal(steerCalls, 1); |
| 413 | assert.equal(started.length, 1); |
| 414 | if (mailbox) { |
| 415 | const delivered = yield* orchestrator.getThreadProjection(threadId); |
| 416 | assert.equal(delivered.subagents[0]?.completionDelivery?.state, "delivered"); |
| 417 | assert.equal(delivered.subagents[0]?.completionDelivery?.observedByRunId, null); |
| 418 | assert.equal(delivered.runs[0]?.delegatedCompletion?.delivery, null); |
| 419 | assert.equal( |
| 420 | delivered.turnItems.filter((item) => item.type === "notification").length, |
| 421 | 1, |
| 422 | ); |
| 423 | yield* orchestrator.dispatch({ |
| 424 | type: "notification.delivery.accept", |
| 425 | commandId: CommandId.make("duplicate-acceptance"), |
| 426 | threadId, |
| 427 | messageId, |
| 428 | }); |
| 429 | yield* worker.drain(); |
| 430 | assert.equal(steerCalls, 1); |
| 431 | yield* orchestrator.dispatch({ |
| 432 | type: "delegated_task.completion-delivery.acknowledge", |
| 433 | commandId: CommandId.make("read-result"), |
| 434 | parentThreadId: threadId, |
| 435 | taskId, |
| 436 | observedByRunId: first.runId, |
| 437 | }); |
| 438 | const acknowledged = yield* orchestrator.getThreadProjection(threadId); |
| 439 | assert.equal(acknowledged.subagents[0]?.completionDelivery?.state, "acknowledged"); |
| 440 | } |
| 441 | return; |
| 442 | } |
| 443 | assert.equal(started.length, 2); |
| 444 | assert.equal(started[1]?.message.messageId, messageId); |
| 445 | if (mailbox) assert.include(started[1]?.message.text ?? "", String(taskId)); |
| 446 | else assert.equal(started[1]?.message.text, "fix the popover"); |
| 447 | assert.deepEqual(started[1]?.message.attachments, [ |
| 448 | { |
| 449 | type: "image", |
| 450 | id: "steering-screenshot", |
| 451 | name: "image.png", |
| 452 | mimeType: "image/png", |
| 453 | sizeBytes: 10, |
| 454 | }, |
| 455 | ]); |
| 456 | assert.equal(steerCalls, timing === "during delivery" ? 1 : 0); |
| 457 | const final = yield* orchestrator.getThreadProjection(threadId); |
| 458 | if (question) { |
| 459 | assert.deepEqual(final.thread.modelSelection, composerSelection); |
| 460 | assert.deepEqual(started[1]?.modelSelection, composerSelection); |
| 461 | } |
| 462 | if (!mailbox && !question) { |
| 463 | assert.equal( |
| 464 | final.turnItems.find( |
| 465 | (item) => item.type === "user_message" && item.messageId === messageId, |
| 466 | )?.status, |
| 467 | "completed", |
| 468 | ); |
| 469 | } |
| 470 | assert.equal(final.messages.filter((message) => message.id === messageId).length, 1); |
| 471 | assert.equal( |
| 472 | final.messages.find((message) => message.id === messageId)?.runId, |
| 473 | started[1]?.runId, |
| 474 | ); |
| 475 | assert.equal( |
| 476 | final.turnItems.filter((item) => |
| 477 | mailbox || question |
| 478 | ? item.type === "notification" |
| 479 | : item.type === "user_message" && item.messageId === messageId, |
| 480 | ).length, |
| 481 | 1, |
| 482 | ); |
| 483 | if (!mailbox && !question) { |
| 484 | assert.equal( |
| 485 | final.visibleTurnItems.filter( |
| 486 | ({ item }) => item.type === "user_message" && item.messageId === messageId, |
| 487 | ).length, |
| 488 | 1, |
| 489 | ); |
| 490 | } |
| 491 | yield* worker.drain(); |
| 492 | assert.equal(started.length, 2); |
| 493 | }).pipe( |
| 494 | Effect.provide( |
| 495 | ProviderReplayHarness.layerWithRegistry( |
| 496 | { name: `steering-completion-${timing}` }, |
| 497 | ProviderAdapterRegistry.layerSingle(adapter), |
| 498 | { runEffectWorker: false }, |
| 499 | ), |
| 500 | ), |
| 501 | ); |
| 502 | }), |
| 503 | ); |
| 504 | |
| 505 | const runSelection = { |
| 506 | instanceId, |
| 507 | model: "test-model", |
| 508 | options: [{ id: "effort", value: "xhigh" }], |
| 509 | }; |
| 510 | const composerSelection = { |
| 511 | ...runSelection, |
| 512 | options: [...runSelection.options, { id: "fastMode", value: false }], |
| 513 | }; |
| 514 | |
| 515 | const nextTurnSelectionHarness = Effect.fn("nextTurnSelectionHarness")(function* ( |
| 516 | name: string, |
| 517 | canRestart = false, |
| 518 | canSteer = true, |
| 519 | interruptTurn: Effect.Effect<void> = Effect.void, |
| 520 | steeringAcceptsAttachments = true, |
| 521 | nativeGoals?: boolean, |
| 522 | onSteer: ( |
| 523 | turn: ProviderAdapter.ProviderAdapterV2SteerInput, |
| 524 | ) => Effect.Effect<void, ProviderAdapter.ProviderAdapterV2Error> = () => Effect.void, |
| 525 | extraAdapters: ReadonlyArray<ProviderAdapter.ProviderAdapterV2["Service"]> = [], |
| 526 | ) { |
| 527 | const driver = ProviderDriverKind.make(nativeGoals === undefined ? "codex" : "opencode"); |
| 528 | const cwd = yield* checkpointWorkspace(name); |
| 529 | const events = yield* Queue.unbounded<ProviderAdapter.ProviderAdapterV2Event>(); |
| 530 | const started: ProviderAdapter.ProviderAdapterV2TurnInput[] = []; |
| 531 | const steered: string[] = []; |
| 532 | const steeredSelections: Array<ModelSelection | undefined> = []; |
| 533 | const interrupted: ProviderTurnId[] = []; |
| 534 | const capabilities = { |
| 535 | ...CodexProviderCapabilitiesV2, |
| 536 | turns: { |
| 537 | ...CodexProviderCapabilitiesV2.turns, |
| 538 | supportsActiveSteering: canSteer, |
| 539 | supportsSteeringByInterruptRestart: canRestart, |
| 540 | activeSteeringAcceptsAttachments: steeringAcceptsAttachments, |
| 541 | ...(nativeGoals === undefined ? {} : { supportsNativeGoals: nativeGoals }), |
| 542 | }, |
| 543 | }; |
| 544 | const adapter: ProviderAdapter.ProviderAdapterV2["Service"] = { |
| 545 | instanceId, |
| 546 | driver, |
| 547 | getCapabilities: () => Effect.succeed(capabilities), |
| 548 | planSelectionTransition: () => Effect.succeed({ type: "apply_on_next_turn" }), |
| 549 | openSession: (input) => |
| 550 | Effect.gen(function* () { |
| 551 | const now = yield* DateTime.now; |
| 552 | const turnUpdated = ( |
| 553 | turn: ProviderAdapter.ProviderAdapterV2TurnInput, |
| 554 | status: "running" | "interrupted", |
| 555 | ) => |
| 556 | Queue.offer(events, { |
| 557 | type: "provider_turn.updated", |
| 558 | driver, |
| 559 | providerTurn: { |
| 560 | id: ProviderTurnId.make(`provider-turn:${turn.attemptId}`), |
| 561 | providerThreadId: turn.providerThread.id, |
| 562 | nodeId: turn.rootNodeId, |
| 563 | runAttemptId: turn.attemptId, |
| 564 | nativeTurnRef: { |
| 565 | driver, |
| 566 | nativeId: `native:${turn.attemptId}`, |
| 567 | strength: "strong", |
| 568 | }, |
| 569 | ordinal: turn.providerTurnOrdinal, |
| 570 | status, |
| 571 | startedAt: now, |
| 572 | completedAt: status === "running" ? null : now, |
| 573 | }, |
| 574 | }); |
| 575 | const startTurn = (turn: ProviderAdapter.ProviderAdapterV2TurnInput) => |
| 576 | Effect.gen(function* () { |
| 577 | started.push(turn); |
| 578 | yield* turnUpdated(turn, "running"); |
| 579 | }); |
| 580 | return { |
| 581 | instanceId, |
| 582 | driver, |
| 583 | providerSessionId: input.providerSessionId, |
| 584 | providerSession: { |
| 585 | id: input.providerSessionId, |
| 586 | driver, |
| 587 | providerInstanceId: instanceId, |
| 588 | status: "ready", |
| 589 | cwd, |
| 590 | model: runSelection.model, |
| 591 | capabilities, |
| 592 | createdAt: now, |
| 593 | updatedAt: now, |
| 594 | lastError: null, |
| 595 | }, |
| 596 | events: Stream.fromQueue(events), |
| 597 | ensureThread: ({ threadId }) => |
| 598 | Effect.succeed({ |
| 599 | id: ProviderThreadId.make(`provider-thread:${threadId}`), |
| 600 | driver, |
| 601 | providerInstanceId: instanceId, |
| 602 | providerSessionId: input.providerSessionId, |
| 603 | appThreadId: threadId, |
| 604 | ownerNodeId: null, |
| 605 | nativeThreadRef: { driver, nativeId: "native-thread", strength: "strong" }, |
| 606 | nativeConversationHeadRef: null, |
| 607 | status: "idle", |
| 608 | firstRunOrdinal: null, |
| 609 | lastRunOrdinal: null, |
| 610 | handoffIds: [], |
| 611 | forkedFrom: null, |
| 612 | createdAt: now, |
| 613 | updatedAt: now, |
| 614 | }), |
| 615 | resumeThread: ({ providerThread }) => Effect.succeed(providerThread), |
| 616 | startTurn, |
| 617 | compactThread: startTurn, |
| 618 | steerTurn: (turn) => |
| 619 | Effect.sync(() => { |
| 620 | steered.push(turn.message.text); |
| 621 | steeredSelections.push(turn.modelSelection); |
| 622 | }).pipe(Effect.andThen(onSteer(turn))), |
| 623 | interruptTurn: ({ providerTurnId }) => |
| 624 | Effect.sync(() => { |
| 625 | interrupted.push(providerTurnId); |
| 626 | }).pipe( |
| 627 | Effect.andThen(interruptTurn), |
| 628 | Effect.andThen( |
| 629 | Effect.forEach( |
| 630 | started.filter( |
| 631 | (turn) => |
| 632 | ProviderTurnId.make(`provider-turn:${turn.attemptId}`) === providerTurnId, |
| 633 | ), |
| 634 | (turn) => |
| 635 | turnUpdated(turn, "interrupted").pipe( |
| 636 | Effect.andThen( |
| 637 | Queue.offer(events, { |
| 638 | type: "turn.terminal", |
| 639 | driver, |
| 640 | providerThreadId: turn.providerThread.id, |
| 641 | providerTurnId, |
| 642 | runOrdinal: turn.runOrdinal, |
| 643 | status: "interrupted", |
| 644 | failure: null, |
| 645 | threadDisposition: "reusable", |
| 646 | }), |
| 647 | ), |
| 648 | ), |
| 649 | { discard: true }, |
| 650 | ), |
| 651 | ), |
| 652 | ), |
| 653 | respondToRuntimeRequest: () => Effect.void, |
| 654 | readThreadSnapshot: () => Effect.die("unused"), |
| 655 | rollbackThread: () => Effect.die("unused"), |
| 656 | forkThread: () => Effect.die("unused"), |
| 657 | }; |
| 658 | }), |
| 659 | }; |
| 660 | const layer = ProviderReplayHarness.layerWithRegistry( |
| 661 | { name }, |
| 662 | ProviderAdapterRegistry.layerFromAdapters([adapter, ...extraAdapters]), |
| 663 | { runEffectWorker: false }, |
| 664 | ); |
| 665 | const startFirstTurn = Effect.gen(function* () { |
| 666 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 667 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 668 | const threadId = ThreadId.make(`thread:${name}`); |
| 669 | yield* orchestrator.dispatch({ |
| 670 | type: "thread.create", |
| 671 | commandId: CommandId.make("create"), |
| 672 | threadId, |
| 673 | projectId: ProjectId.make(`project:${name}`), |
| 674 | title: "Steer with changed options", |
| 675 | modelSelection: runSelection, |
| 676 | runtimeMode: "full-access", |
| 677 | interactionMode: "default", |
| 678 | branch: null, |
| 679 | worktreePath: cwd, |
| 680 | createdBy: "user", |
| 681 | creationSource: "web", |
| 682 | }); |
| 683 | const running = yield* orchestrator.streamDomainEvents.pipe( |
| 684 | Stream.filter( |
| 685 | (event) => event.type === "provider-turn.updated" && event.payload.status === "running", |
| 686 | ), |
| 687 | Stream.take(1), |
| 688 | Stream.runDrain, |
| 689 | Effect.forkScoped, |
| 690 | ); |
| 691 | yield* orchestrator.dispatch({ |
| 692 | type: "message.dispatch", |
| 693 | commandId: CommandId.make("first"), |
| 694 | threadId, |
| 695 | messageId: MessageId.make("message:first"), |
| 696 | text: "first", |
| 697 | attachments: [], |
| 698 | modelSelection: runSelection, |
| 699 | dispatchMode: { type: "start_immediately" }, |
| 700 | createdBy: "user", |
| 701 | creationSource: "web", |
| 702 | }); |
| 703 | yield* worker.drain(); |
| 704 | yield* Fiber.join(running); |
| 705 | // Ingestion checks turn ownership under this lock after committing the running turn. |
| 706 | yield* (yield* ThreadCommandExecutor.ThreadCommandExecutor).withLock(threadId, Effect.void); |
| 707 | return threadId; |
| 708 | }); |
| 709 | return { cwd, events, started, steered, steeredSelections, interrupted, layer, startFirstTurn }; |
| 710 | }); |
| 711 | |
| 712 | it.effect.each([ |
| 713 | { notice: "delegated task question", goal: null, steers: true }, |
| 714 | { notice: "background command", goal: "active", steers: true }, |
| 715 | { notice: "background command", goal: null, steers: false }, |
| 716 | ] as const)( |
| 717 | "a $notice notice steers into the running turn: $steers (goal $goal)", |
| 718 | ({ notice, goal, steers }) => |
| 719 | Effect.gen(function* () { |
| 720 | const { started, steered, layer, startFirstTurn } = yield* nextTurnSelectionHarness( |
| 721 | `notice-steer-${notice.replaceAll(" ", "-")}-${goal ?? "none"}`, |
| 722 | ); |
| 723 | yield* Effect.gen(function* () { |
| 724 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 725 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 726 | const threadId = yield* startFirstTurn; |
| 727 | const { thread } = yield* orchestrator.getThreadProjection(threadId); |
| 728 | yield* (yield* EventSink.EventSinkV2).write({ |
| 729 | events: [ |
| 730 | { |
| 731 | id: EventId.make("event:composer-selection"), |
| 732 | type: "thread.model-selection-updated", |
| 733 | threadId, |
| 734 | providerInstanceId: instanceId, |
| 735 | occurredAt: yield* DateTime.now, |
| 736 | payload: { ...thread, modelSelection: composerSelection }, |
| 737 | }, |
| 738 | ], |
| 739 | }); |
| 740 | if (goal !== null) { |
| 741 | const providerThread = (yield* orchestrator.getThreadProjection(threadId)) |
| 742 | .providerThreads[0]!; |
| 743 | yield* (yield* EventSink.EventSinkV2).write({ |
| 744 | events: [ |
| 745 | { |
| 746 | id: EventId.make("event:goal-active"), |
| 747 | type: "provider-thread.updated", |
| 748 | threadId, |
| 749 | occurredAt: yield* DateTime.now, |
| 750 | payload: { ...providerThread, goal: { objective: "Ship it", status: goal } }, |
| 751 | }, |
| 752 | ], |
| 753 | }); |
| 754 | } |
| 755 | const messageId = MessageId.make("message:notice"); |
| 756 | const notification = |
| 757 | notice === "delegated task question" |
| 758 | ? { |
| 759 | source: { |
| 760 | kind: "delegated_task" as const, |
| 761 | taskIds: [NodeId.make("task:question")], |
| 762 | childThreadId: ThreadId.make("thread:question-child"), |
| 763 | }, |
| 764 | outcome: "updated" as const, |
| 765 | summary: "Delegated task has a question", |
| 766 | } |
| 767 | : { |
| 768 | source: { kind: "command" as const }, |
| 769 | outcome: "completed" as const, |
| 770 | summary: "Background command finished", |
| 771 | }; |
| 772 | yield* orchestrator.dispatch({ |
| 773 | type: "message.dispatch", |
| 774 | commandId: CommandId.make("notice-wake"), |
| 775 | threadId, |
| 776 | messageId, |
| 777 | text: `${notification.summary}\nexit 0: built 12 files`, |
| 778 | notification, |
| 779 | attachments: [], |
| 780 | dispatchMode: { type: "queue_after_active" }, |
| 781 | createdBy: "agent", |
| 782 | creationSource: "server", |
| 783 | }); |
| 784 | yield* worker.drain(); |
| 785 | const after = yield* orchestrator.getThreadProjection(threadId); |
| 786 | assert.deepEqual( |
| 787 | steered, |
| 788 | steers ? [`${notification.summary}\nexit 0: built 12 files`] : [], |
| 789 | ); |
| 790 | assert.lengthOf(started, 1); |
| 791 | assert.equal( |
| 792 | after.runs.some((run) => run.status === "queued"), |
| 793 | !steers, |
| 794 | ); |
| 795 | const message = after.messages.find((candidate) => candidate.id === messageId); |
| 796 | assert.deepEqual(message?.notification, notification); |
| 797 | if (steers) assert.equal(message?.runId, started[0]?.runId); |
| 798 | assert.deepEqual(after.thread.modelSelection, composerSelection); |
| 799 | }).pipe(Effect.provide(layer)); |
| 800 | }), |
| 801 | ); |
| 802 | |
| 803 | it.effect("sends a selected held message once and preserves the remaining queue order", () => |
| 804 | Effect.gen(function* () { |
| 805 | const { started, steered, layer, startFirstTurn } = |
| 806 | yield* nextTurnSelectionHarness("send-held-message"); |
| 807 | yield* Effect.gen(function* () { |
| 808 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 809 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 810 | const threadId = yield* startFirstTurn; |
| 811 | yield* Effect.forEach(["queued-first", "selected", "queued-last"], (text) => |
| 812 | orchestrator.dispatch({ |
| 813 | type: "message.dispatch", |
| 814 | commandId: CommandId.make(text), |
| 815 | threadId, |
| 816 | messageId: MessageId.make(`message:${text}`), |
| 817 | text, |
| 818 | attachments: [], |
| 819 | modelSelection: runSelection, |
| 820 | dispatchMode: { type: "queue_after_active" }, |
| 821 | createdBy: "user", |
| 822 | creationSource: "web", |
| 823 | }), |
| 824 | ); |
| 825 | const before = yield* orchestrator.getThreadProjection(threadId); |
| 826 | const selected = before.runs.find( |
| 827 | (run) => run.userMessageId === MessageId.make("message:selected"), |
| 828 | )!; |
| 829 | const sink = yield* EventSink.EventSinkV2; |
| 830 | const now = yield* DateTime.now; |
| 831 | // Restart recovery cancels the active run and holds every queued message. |
| 832 | yield* sink.write({ |
| 833 | events: [ |
| 834 | ...before.runs.map((run) => ({ |
| 835 | id: EventId.make(`restart:${run.id}`), |
| 836 | type: "run.updated" as const, |
| 837 | threadId, |
| 838 | runId: run.id, |
| 839 | occurredAt: now, |
| 840 | payload: |
| 841 | run.status === "queued" |
| 842 | ? { ...run, queueHeld: true } |
| 843 | : { ...run, status: "cancelled" as const, completedAt: now }, |
| 844 | })), |
| 845 | ...before.providerTurns.map((turn) => ({ |
| 846 | id: EventId.make(`restart:${turn.id}`), |
| 847 | type: "provider-turn.updated" as const, |
| 848 | threadId, |
| 849 | occurredAt: now, |
| 850 | payload: { ...turn, status: "cancelled" as const, completedAt: now }, |
| 851 | })), |
| 852 | ], |
| 853 | }); |
| 854 | const command = { |
| 855 | type: "queued-run.start" as const, |
| 856 | commandId: CommandId.make("send-selected"), |
| 857 | threadId, |
| 858 | runId: selected.id, |
| 859 | }; |
| 860 | yield* orchestrator.dispatch(command); |
| 861 | yield* worker.drain(); |
| 862 | yield* orchestrator.dispatch(command); |
| 863 | yield* worker.drain(); |
| 864 | const after = yield* orchestrator.getThreadProjection(threadId); |
| 865 | assert.equal(after.runs.find((run) => run.id === selected.id)?.status, "running"); |
| 866 | assert.deepEqual( |
| 867 | started.map((input) => input.message.text), |
| 868 | ["first", "selected"], |
| 869 | ); |
| 870 | assert.equal(started[1]?.runId, selected.id); |
| 871 | assert.equal(started[1]?.message.messageId, selected.userMessageId); |
| 872 | assert.deepEqual(started[1]?.modelSelection, runSelection); |
| 873 | assert.deepEqual(steered, []); |
| 874 | assert.lengthOf( |
| 875 | after.messages.filter((message) => message.id === selected.userMessageId), |
| 876 | 1, |
| 877 | ); |
| 878 | assert.deepEqual( |
| 879 | after.runs.filter((run) => run.status === "queued").map((run) => run.userMessageId), |
| 880 | [MessageId.make("message:queued-first"), MessageId.make("message:queued-last")], |
| 881 | ); |
| 882 | assert.isFalse(after.runs.some((run) => run.status === "queued" && run.queueHeld)); |
| 883 | const duplicate = yield* orchestrator |
| 884 | .dispatch({ |
| 885 | ...command, |
| 886 | commandId: CommandId.make("send-selected-again"), |
| 887 | }) |
| 888 | .pipe(Effect.flip); |
| 889 | assert.equal(duplicate._tag, "OrchestratorDispatchError"); |
| 890 | }).pipe(Effect.provide(layer)); |
| 891 | }), |
| 892 | ); |
| 893 | |
| 894 | it.effect.each( |
| 895 | (["preparing", "starting", "running"] as const).flatMap((status) => |
| 896 | [false, true].map((direct) => ({ status, direct })), |
| 897 | ), |
| 898 | )("delivers a deferred steer for a $status run (direct: $direct)", ({ status, direct }) => |
| 899 | Effect.gen(function* () { |
| 900 | const restarting = status === "starting" && !direct; |
| 901 | const { started, steered, layer, startFirstTurn, events } = yield* nextTurnSelectionHarness( |
| 902 | `deferred-steer-${status}-${direct}`, |
| 903 | restarting, |
| 904 | !restarting, |
| 905 | ); |
| 906 | yield* Effect.gen(function* () { |
| 907 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 908 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 909 | const threadId = yield* startFirstTurn; |
| 910 | const before = yield* orchestrator.getThreadProjection(threadId); |
| 911 | const run = before.runs[0]!; |
| 912 | const turn = before.providerTurns[0]!; |
| 913 | const now = yield* DateTime.now; |
| 914 | yield* (yield* EventSink.EventSinkV2).write({ |
| 915 | events: [ |
| 916 | { |
| 917 | id: EventId.make("setup-run"), |
| 918 | type: "run.updated", |
| 919 | threadId, |
| 920 | occurredAt: now, |
| 921 | payload: { ...run, status }, |
| 922 | }, |
| 923 | { |
| 924 | id: EventId.make("setup-turn"), |
| 925 | type: "provider-turn.updated", |
| 926 | threadId, |
| 927 | occurredAt: now, |
| 928 | payload: { ...turn, status: "pending" }, |
| 929 | }, |
| 930 | ], |
| 931 | }); |
| 932 | if (status === "preparing" && !direct) { |
| 933 | yield* orchestrator.dispatch({ |
| 934 | type: "message.dispatch", |
| 935 | commandId: CommandId.make("edited-steer"), |
| 936 | threadId, |
| 937 | messageId: MessageId.make("message:edited-steer"), |
| 938 | text: "edit this steer", |
| 939 | attachments: [], |
| 940 | deliveryIntent: "steer", |
| 941 | dispatchMode: { type: "queue_after_active" }, |
| 942 | createdBy: "user", |
| 943 | creationSource: "web", |
| 944 | }); |
| 945 | const edited = (yield* orchestrator.getThreadProjection(threadId)).runs.find( |
| 946 | (candidate) => candidate.userMessageId === MessageId.make("message:edited-steer"), |
| 947 | )!; |
| 948 | yield* orchestrator.dispatch({ |
| 949 | type: "queued-run.edit", |
| 950 | commandId: CommandId.make("edit-steer-to-compact"), |
| 951 | threadId, |
| 952 | runId: edited.id, |
| 953 | text: "/compact", |
| 954 | }); |
| 955 | } |
| 956 | if (direct) { |
| 957 | yield* orchestrator.dispatch({ |
| 958 | type: "message.dispatch", |
| 959 | commandId: CommandId.make("earlier-queue"), |
| 960 | threadId, |
| 961 | messageId: MessageId.make("message:earlier-queue"), |
| 962 | text: "earlier queued selection", |
| 963 | modelSelection: composerSelection, |
| 964 | attachments: [], |
| 965 | dispatchMode: { type: "queue_after_active" }, |
| 966 | createdBy: "user", |
| 967 | creationSource: "web", |
| 968 | }); |
| 969 | } |
| 970 | yield* orchestrator.dispatch({ |
| 971 | type: "message.dispatch", |
| 972 | commandId: CommandId.make("queue"), |
| 973 | threadId, |
| 974 | messageId: MessageId.make("message:steer-after-setup"), |
| 975 | text: "steer after setup", |
| 976 | attachments: [], |
| 977 | dispatchMode: { type: "queue_after_active" }, |
| 978 | ...(direct ? { deliveryIntent: "steer" as const } : {}), |
| 979 | createdBy: "user", |
| 980 | creationSource: "web", |
| 981 | }); |
| 982 | const queued = (yield* orchestrator.getThreadProjection(threadId)).runs.find( |
| 983 | (candidate) => candidate.userMessageId === MessageId.make("message:steer-after-setup"), |
| 984 | )!; |
| 985 | assert.deepEqual(queued.modelSelection, runSelection); |
| 986 | for (const text of ["/compact", "/logout"]) { |
| 987 | yield* orchestrator.dispatch({ |
| 988 | type: "message.dispatch", |
| 989 | commandId: CommandId.make(`queue-${text}`), |
| 990 | threadId, |
| 991 | messageId: MessageId.make(`message:${text}`), |
| 992 | text, |
| 993 | attachments: [], |
| 994 | dispatchMode: { type: "queue_after_active" }, |
| 995 | createdBy: "user", |
| 996 | creationSource: "web", |
| 997 | }); |
| 998 | const maintenance = (yield* orchestrator.getThreadProjection(threadId)).runs.find( |
| 999 | (candidate) => candidate.userMessageId === MessageId.make(`message:${text}`), |
| 1000 | )!; |
| 1001 | const error = yield* orchestrator |
| 1002 | .dispatch({ |
| 1003 | type: "queued-message.promote-to-steer", |
| 1004 | commandId: CommandId.make(`promote-${text}`), |
| 1005 | threadId, |
| 1006 | queuedRunId: maintenance.id, |
| 1007 | targetRunId: run.id, |
| 1008 | }) |
| 1009 | .pipe(Effect.flip); |
| 1010 | assert.equal(error._tag, "OrchestratorDispatchError"); |
| 1011 | } |
| 1012 | const command = { |
| 1013 | type: "queued-message.promote-to-steer" as const, |
| 1014 | commandId: CommandId.make("promote"), |
| 1015 | threadId, |
| 1016 | queuedRunId: queued.id, |
| 1017 | targetRunId: run.id, |
| 1018 | }; |
| 1019 | if (!direct) { |
| 1020 | yield* orchestrator.dispatch(command); |
| 1021 | yield* orchestrator.dispatch(command); |
| 1022 | } |
| 1023 | let secondQueuedId: typeof queued.id | undefined; |
| 1024 | if (restarting) { |
| 1025 | yield* orchestrator.dispatch({ |
| 1026 | type: "message.dispatch", |
| 1027 | commandId: CommandId.make("second"), |
| 1028 | threadId, |
| 1029 | messageId: MessageId.make("message:second"), |
| 1030 | text: "second steer", |
| 1031 | attachments: [], |
| 1032 | dispatchMode: { type: "queue_after_active" }, |
| 1033 | createdBy: "user", |
| 1034 | creationSource: "web", |
| 1035 | }); |
| 1036 | secondQueuedId = (yield* orchestrator.getThreadProjection(threadId)).runs.find( |
| 1037 | (candidate) => candidate.userMessageId === MessageId.make("message:second"), |
| 1038 | )!.id; |
| 1039 | yield* orchestrator.dispatch({ |
| 1040 | ...command, |
| 1041 | commandId: CommandId.make("promote-second"), |
| 1042 | queuedRunId: secondQueuedId, |
| 1043 | }); |
| 1044 | } |
| 1045 | const secondReDeferred = yield* orchestrator.streamDomainEvents.pipe( |
| 1046 | Stream.filter( |
| 1047 | (event) => |
| 1048 | event.type === "run.updated" && |
| 1049 | event.payload.id === secondQueuedId && |
| 1050 | event.payload.status === "queued", |
| 1051 | ), |
| 1052 | Stream.take(1), |
| 1053 | Stream.runDrain, |
| 1054 | Effect.forkScoped, |
| 1055 | ); |
| 1056 | const secondPromoted = yield* orchestrator.streamDomainEvents.pipe( |
| 1057 | Stream.filter( |
| 1058 | (event) => |
| 1059 | event.type === "run.updated" && |
| 1060 | event.payload.id === secondQueuedId && |
| 1061 | event.payload.status === "cancelled", |
| 1062 | ), |
| 1063 | Stream.take(1), |
| 1064 | Stream.runDrain, |
| 1065 | Effect.forkScoped, |
| 1066 | ); |
| 1067 | yield* worker.drain(); |
| 1068 | assert.deepEqual(steered, []); |
| 1069 | const promoted = yield* orchestrator.streamDomainEvents.pipe( |
| 1070 | Stream.filter( |
| 1071 | (event) => |
| 1072 | event.type === "run.updated" && |
| 1073 | event.payload.id === queued.id && |
| 1074 | event.payload.status === "cancelled", |
| 1075 | ), |
| 1076 | Stream.take(1), |
| 1077 | Stream.runDrain, |
| 1078 | Effect.forkScoped, |
| 1079 | ); |
| 1080 | yield* (yield* EventSink.EventSinkV2).write({ |
| 1081 | events: [ |
| 1082 | { |
| 1083 | id: EventId.make("ready-run"), |
| 1084 | type: "run.updated", |
| 1085 | threadId, |
| 1086 | occurredAt: now, |
| 1087 | payload: run, |
| 1088 | }, |
| 1089 | ], |
| 1090 | }); |
| 1091 | yield* Queue.offer(events, { type: "provider_turn.updated", driver, providerTurn: turn }); |
| 1092 | yield* Fiber.join(promoted); |
| 1093 | if (restarting) yield* Fiber.join(secondReDeferred); |
| 1094 | yield* worker.drain(); |
| 1095 | if (restarting) { |
| 1096 | yield* Fiber.join(secondPromoted); |
| 1097 | yield* worker.drain(); |
| 1098 | assert.deepEqual( |
| 1099 | started.map((turn) => turn.message.text), |
| 1100 | ["first", "steer after setup", "second steer"], |
| 1101 | ); |
| 1102 | } else { |
| 1103 | assert.deepEqual(steered, ["steer after setup"]); |
| 1104 | assert.lengthOf(started, 1); |
| 1105 | } |
| 1106 | const after = yield* orchestrator.getThreadProjection(threadId); |
| 1107 | assert.equal( |
| 1108 | after.messages.find((message) => message.id === queued.userMessageId)?.runId, |
| 1109 | run.id, |
| 1110 | ); |
| 1111 | assert.equal(after.runs.find((candidate) => candidate.id === run.id)?.status, "running"); |
| 1112 | }).pipe(Effect.provide(layer)); |
| 1113 | }), |
| 1114 | ); |
| 1115 | |
| 1116 | it.effect("force sends multiple queued messages and can force send a requeued message again", () => |
| 1117 | Effect.gen(function* () { |
| 1118 | const { started, steered, layer, startFirstTurn } = yield* nextTurnSelectionHarness( |
| 1119 | "multiple-promoted-messages", |
| 1120 | ); |
| 1121 | yield* Effect.gen(function* () { |
| 1122 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 1123 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 1124 | const threadId = yield* startFirstTurn; |
| 1125 | const queue = (text: string, commandId: string) => |
| 1126 | orchestrator.dispatch({ |
| 1127 | type: "message.dispatch", |
| 1128 | commandId: CommandId.make(commandId), |
| 1129 | threadId, |
| 1130 | messageId: MessageId.make(`message:${text}`), |
| 1131 | text, |
| 1132 | attachments: [], |
| 1133 | dispatchMode: { type: "queue_after_active" }, |
| 1134 | createdBy: "user", |
| 1135 | creationSource: "web", |
| 1136 | }); |
| 1137 | for (const text of ["one", "two", "remaining"]) yield* queue(text, `queue-${text}`); |
| 1138 | const promote = Effect.fn("test.promote")(function* (text: string, commandId: string) { |
| 1139 | const projection = yield* orchestrator.getThreadProjection(threadId); |
| 1140 | const queued = projection.runs.find( |
| 1141 | (run) => |
| 1142 | run.status === "queued" && run.userMessageId === MessageId.make(`message:${text}`), |
| 1143 | )!; |
| 1144 | yield* orchestrator.dispatch({ |
| 1145 | type: "queued-message.promote-to-steer", |
| 1146 | commandId: CommandId.make(commandId), |
| 1147 | threadId, |
| 1148 | queuedRunId: queued.id, |
| 1149 | targetRunId: started[0]!.runId, |
| 1150 | }); |
| 1151 | }); |
| 1152 | yield* promote("one", "promote-one"); |
| 1153 | yield* promote("two", "promote-two"); |
| 1154 | yield* worker.drain(); |
| 1155 | assert.deepEqual(steered, ["one", "two"]); |
| 1156 | |
| 1157 | // The completion-race fallback redispatches the same message into the queue. |
| 1158 | yield* queue("one", "requeue-one"); |
| 1159 | const requeued = yield* orchestrator.getThreadProjection(threadId); |
| 1160 | assert.equal( |
| 1161 | requeued.turnItems.find( |
| 1162 | (item) => item.type === "user_message" && item.messageId === "message:one", |
| 1163 | )?.status, |
| 1164 | "pending", |
| 1165 | ); |
| 1166 | yield* promote("one", "promote-one-again"); |
| 1167 | yield* worker.drain(); |
| 1168 | assert.deepEqual(steered, ["one", "two", "one"]); |
| 1169 | assert.lengthOf(started, 1); |
| 1170 | const final = yield* orchestrator.getThreadProjection(threadId); |
| 1171 | assert.deepEqual( |
| 1172 | final.runs.filter((run) => run.status === "queued").map((run) => run.userMessageId), |
| 1173 | [MessageId.make("message:remaining")], |
| 1174 | ); |
| 1175 | for (const text of ["one", "two"]) { |
| 1176 | const items = final.turnItems.filter( |
| 1177 | (item) => item.type === "user_message" && item.messageId === `message:${text}`, |
| 1178 | ); |
| 1179 | assert.lengthOf(items, 1); |
| 1180 | assert.equal(items[0]?.status, "completed"); |
| 1181 | } |
| 1182 | }).pipe(Effect.provide(layer)); |
| 1183 | }), |
| 1184 | ); |
| 1185 | |
| 1186 | it.effect("keeps a message queued when send now races with a running turn", () => |
| 1187 | Effect.gen(function* () { |
| 1188 | const { started, layer, startFirstTurn } = yield* nextTurnSelectionHarness("send-queued-race"); |
| 1189 | yield* Effect.gen(function* () { |
| 1190 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 1191 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 1192 | const threadId = yield* startFirstTurn; |
| 1193 | yield* orchestrator.dispatch({ |
| 1194 | type: "message.dispatch", |
| 1195 | commandId: CommandId.make("queue"), |
| 1196 | threadId, |
| 1197 | messageId: MessageId.make("message:queued"), |
| 1198 | text: "queued", |
| 1199 | attachments: [], |
| 1200 | dispatchMode: { type: "queue_after_active" }, |
| 1201 | createdBy: "user", |
| 1202 | creationSource: "web", |
| 1203 | }); |
| 1204 | const before = yield* orchestrator.getThreadProjection(threadId); |
| 1205 | const queued = before.runs.find((run) => run.status === "queued")!; |
| 1206 | const error = yield* orchestrator |
| 1207 | .dispatch({ |
| 1208 | type: "queued-run.start", |
| 1209 | commandId: CommandId.make("send-now"), |
| 1210 | threadId, |
| 1211 | runId: queued.id, |
| 1212 | }) |
| 1213 | .pipe(Effect.flip); |
| 1214 | yield* worker.drain(); |
| 1215 | assert.equal(error._tag, "OrchestratorDispatchError"); |
| 1216 | assert.equal(started.length, 1); |
| 1217 | const after = yield* orchestrator.getThreadProjection(threadId); |
| 1218 | assert.deepEqual(after.runs, before.runs); |
| 1219 | }).pipe(Effect.provide(layer)); |
| 1220 | }), |
| 1221 | ); |
| 1222 | |
| 1223 | // regression: #19210 a provider that can restart the turn must still steer, since restarting ends its native subagents |
| 1224 | it.effect( |
| 1225 | "steers a changed turn-scoped selection into a running turn the provider could restart", |
| 1226 | () => |
| 1227 | Effect.gen(function* () { |
| 1228 | const { started, steered, steeredSelections, layer, startFirstTurn } = |
| 1229 | yield* nextTurnSelectionHarness("steering-selection-change", true); |
| 1230 | yield* Effect.gen(function* () { |
| 1231 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 1232 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 1233 | const threadId = yield* startFirstTurn; |
| 1234 | const steer = (id: string, selection: ModelSelection) => |
| 1235 | orchestrator |
| 1236 | .dispatch({ |
| 1237 | type: "message.dispatch", |
| 1238 | commandId: CommandId.make(id), |
| 1239 | threadId, |
| 1240 | messageId: MessageId.make(`message:${id}`), |
| 1241 | text: id, |
| 1242 | attachments: [], |
| 1243 | modelSelection: selection, |
| 1244 | dispatchMode: { type: "steer_active", targetRunId: started[0]!.runId }, |
| 1245 | createdBy: "user", |
| 1246 | creationSource: "web", |
| 1247 | }) |
| 1248 | .pipe(Effect.andThen(worker.drain())); |
| 1249 | |
| 1250 | yield* steer("steer-changed", composerSelection); |
| 1251 | const changed = yield* orchestrator.getThreadProjection(threadId); |
| 1252 | assert.deepEqual(steered, ["steer-changed"]); |
| 1253 | assert.equal(started.length, 1); |
| 1254 | assert.lengthOf(changed.attempts, 1); |
| 1255 | assert.equal(changed.runs[0]?.status, "running"); |
| 1256 | assert.deepEqual(changed.runs[0]?.modelSelection, runSelection); |
| 1257 | assert.deepEqual(changed.thread.modelSelection, composerSelection); |
| 1258 | |
| 1259 | yield* steer("steer-reverted", runSelection); |
| 1260 | const reverted = yield* orchestrator.getThreadProjection(threadId); |
| 1261 | assert.deepEqual(steered, ["steer-changed", "steer-reverted"]); |
| 1262 | assert.lengthOf(reverted.attempts, 1); |
| 1263 | assert.deepEqual(reverted.thread.modelSelection, runSelection); |
| 1264 | |
| 1265 | const otherSelection = { |
| 1266 | instanceId: ProviderInstanceId.make("codex-work"), |
| 1267 | model: "other", |
| 1268 | }; |
| 1269 | const sink = yield* EventSink.EventSinkV2; |
| 1270 | yield* sink.write({ |
| 1271 | events: [ |
| 1272 | { |
| 1273 | id: EventId.make("switched-away"), |
| 1274 | type: "thread.provider-switched", |
| 1275 | threadId, |
| 1276 | providerInstanceId: otherSelection.instanceId, |
| 1277 | occurredAt: yield* DateTime.now, |
| 1278 | payload: { |
| 1279 | ...reverted.thread, |
| 1280 | providerInstanceId: otherSelection.instanceId, |
| 1281 | modelSelection: otherSelection, |
| 1282 | }, |
| 1283 | }, |
| 1284 | ], |
| 1285 | }); |
| 1286 | yield* steer("steer-back", runSelection); |
| 1287 | const back = yield* orchestrator.getThreadProjection(threadId); |
| 1288 | assert.deepEqual(steered, ["steer-changed", "steer-reverted", "steer-back"]); |
| 1289 | // The adapter gets each steer's selection so it can switch the live turn, and back. |
| 1290 | assert.deepEqual(steeredSelections, [composerSelection, runSelection, runSelection]); |
| 1291 | assert.equal(back.thread.providerInstanceId, instanceId); |
| 1292 | assert.deepEqual(back.thread.modelSelection, runSelection); |
| 1293 | }).pipe(Effect.provide(layer)); |
| 1294 | }), |
| 1295 | ); |
| 1296 | |
| 1297 | it.effect("restarts the turn for an image when native steering takes text only", () => |
| 1298 | Effect.gen(function* () { |
| 1299 | const { started, steered, layer, startFirstTurn } = yield* nextTurnSelectionHarness( |
| 1300 | "steering-text-only-attachment", |
| 1301 | true, |
| 1302 | true, |
| 1303 | Effect.void, |
| 1304 | false, |
| 1305 | ); |
| 1306 | yield* Effect.gen(function* () { |
| 1307 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 1308 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 1309 | const threadId = yield* startFirstTurn; |
| 1310 | const before = yield* orchestrator.getThreadProjection(threadId); |
| 1311 | const originalStart = before.runs[0]!.startedAt; |
| 1312 | assert.isNotNull(originalStart); |
| 1313 | yield* TestClock.adjust("10 seconds"); |
| 1314 | yield* orchestrator.dispatch({ |
| 1315 | type: "message.dispatch", |
| 1316 | commandId: CommandId.make("steer-image"), |
| 1317 | threadId, |
| 1318 | messageId: MessageId.make("message:steer-image"), |
| 1319 | text: "look at this", |
| 1320 | attachments: [ |
| 1321 | { |
| 1322 | type: "image", |
| 1323 | id: ChatAttachmentId.make("steer-image"), |
| 1324 | name: "screenshot.png", |
| 1325 | mimeType: "image/png", |
| 1326 | sizeBytes: 4, |
| 1327 | }, |
| 1328 | ], |
| 1329 | modelSelection: runSelection, |
| 1330 | dispatchMode: { type: "steer_active", targetRunId: started[0]!.runId }, |
| 1331 | createdBy: "user", |
| 1332 | creationSource: "web", |
| 1333 | }); |
| 1334 | yield* worker.drain(); |
| 1335 | const after = yield* orchestrator.getThreadProjection(threadId); |
| 1336 | assert.equal(after.runs[0]?.id, before.runs[0]?.id); |
| 1337 | assert.deepEqual(after.runs[0]?.startedAt, originalStart); |
| 1338 | assert.equal(after.attempts[0]?.status, "superseded"); |
| 1339 | const restartedAt = yield* DateTime.now; |
| 1340 | assert.deepEqual(after.attempts[1]?.startedAt, restartedAt); |
| 1341 | assert.deepEqual( |
| 1342 | after.nodes.find((node) => node.id === after.runs[0]?.rootNodeId)?.startedAt, |
| 1343 | restartedAt, |
| 1344 | ); |
| 1345 | assert.deepEqual(steered, []); |
| 1346 | assert.deepEqual( |
| 1347 | started.map((turn) => [turn.message.text, turn.message.attachments.length]), |
| 1348 | [ |
| 1349 | ["first", 0], |
| 1350 | ["look at this", 1], |
| 1351 | ], |
| 1352 | ); |
| 1353 | }).pipe(Effect.provide(layer)); |
| 1354 | }), |
| 1355 | ); |
| 1356 | |
| 1357 | it.effect("starts a steer that missed the turn on the saved next-turn selection", () => |
| 1358 | Effect.gen(function* () { |
| 1359 | const { events, started, steered, layer, startFirstTurn } = yield* nextTurnSelectionHarness( |
| 1360 | "steering-selection-follow-up", |
| 1361 | ); |
| 1362 | yield* Effect.gen(function* () { |
| 1363 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 1364 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 1365 | const threadId = yield* startFirstTurn; |
| 1366 | const first = started[0]!; |
| 1367 | yield* orchestrator.dispatch({ |
| 1368 | type: "message.dispatch", |
| 1369 | commandId: CommandId.make("steer"), |
| 1370 | threadId, |
| 1371 | messageId: MessageId.make("message:steer"), |
| 1372 | text: "late steer", |
| 1373 | attachments: [], |
| 1374 | modelSelection: composerSelection, |
| 1375 | dispatchMode: { type: "steer_active", targetRunId: first.runId }, |
| 1376 | createdBy: "user", |
| 1377 | creationSource: "web", |
| 1378 | }); |
| 1379 | const settled = yield* orchestrator.streamDomainEvents.pipe( |
| 1380 | Stream.filter( |
| 1381 | (event) => |
| 1382 | event.type === "run.updated" && |
| 1383 | event.payload.id === first.runId && |
| 1384 | event.payload.status === "waiting", |
| 1385 | ), |
| 1386 | Stream.take(1), |
| 1387 | Stream.runDrain, |
| 1388 | Effect.forkScoped, |
| 1389 | ); |
| 1390 | const turn = (yield* orchestrator.getThreadProjection(threadId)).providerTurns[0]!; |
| 1391 | yield* Queue.offer(events, { |
| 1392 | type: "provider_turn.updated", |
| 1393 | driver, |
| 1394 | providerTurn: { ...turn, status: "completed", completedAt: yield* DateTime.now }, |
| 1395 | }); |
| 1396 | yield* Queue.offer(events, { |
| 1397 | type: "turn.terminal", |
| 1398 | driver, |
| 1399 | providerThreadId: turn.providerThreadId, |
| 1400 | providerTurnId: turn.id, |
| 1401 | runOrdinal: first.runOrdinal, |
| 1402 | status: "completed", |
| 1403 | failure: null, |
| 1404 | threadDisposition: "reusable", |
| 1405 | }); |
| 1406 | yield* Fiber.join(settled); |
| 1407 | yield* worker.drain(); |
| 1408 | yield* orchestrator.resumeQueuedRuns; |
| 1409 | yield* worker.drain(); |
| 1410 | |
| 1411 | const projection = yield* orchestrator.getThreadProjection(threadId); |
| 1412 | assert.deepEqual(steered, []); |
| 1413 | assert.equal(started.length, 2); |
| 1414 | assert.equal(started[1]?.message.messageId, MessageId.make("message:steer")); |
| 1415 | assert.deepEqual(started[1]?.modelSelection, composerSelection); |
| 1416 | assert.deepEqual(projection.thread.modelSelection, composerSelection); |
| 1417 | const message = projection.turnItems.find( |
| 1418 | (item) => |
| 1419 | item.type === "user_message" && item.messageId === MessageId.make("message:steer"), |
| 1420 | ); |
| 1421 | assert.equal(message?.runId, started[1]!.runId); |
| 1422 | assert.isAbove( |
| 1423 | message!.ordinal, |
| 1424 | Math.max( |
| 1425 | ...projection.turnItems |
| 1426 | .filter((item) => item.runId === first.runId) |
| 1427 | .map((item) => item.ordinal), |
| 1428 | ), |
| 1429 | ); |
| 1430 | }).pipe(Effect.provide(layer)); |
| 1431 | }), |
| 1432 | ); |
| 1433 | |
| 1434 | const declinedSteerTimings = [ |
| 1435 | "complete", |
| 1436 | "stop-before-decline", |
| 1437 | "stop-after-decline", |
| 1438 | "queued-model", |
| 1439 | "two-steers", |
| 1440 | ] as const; |
| 1441 | |
| 1442 | it.effect.each(declinedSteerTimings)( |
| 1443 | "persists a declined steer without blocking the worker: %s", |
| 1444 | (timing) => |
| 1445 | Effect.gen(function* () { |
| 1446 | let beforeDecline: Effect.Effect<void> = Effect.void; |
| 1447 | const { events, started, layer, startFirstTurn } = yield* nextTurnSelectionHarness( |
| 1448 | `steering-declined-${timing}`, |
| 1449 | false, |
| 1450 | true, |
| 1451 | Effect.void, |
| 1452 | true, |
| 1453 | undefined, |
| 1454 | (turn) => |
| 1455 | Effect.suspend(() => beforeDecline).pipe( |
| 1456 | Effect.andThen( |
| 1457 | Effect.fail( |
| 1458 | new ProviderAdapter.ProviderAdapterSteerDeclinedError({ |
| 1459 | driver, |
| 1460 | providerThreadId: turn.providerThread.id, |
| 1461 | providerTurnId: turn.providerTurnId, |
| 1462 | }), |
| 1463 | ), |
| 1464 | ), |
| 1465 | ), |
| 1466 | ); |
| 1467 | yield* Effect.gen(function* () { |
| 1468 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 1469 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 1470 | const threadId = yield* startFirstTurn; |
| 1471 | const first = started[0]!; |
| 1472 | const stop = orchestrator |
| 1473 | .dispatch({ |
| 1474 | type: "run.interrupt", |
| 1475 | commandId: CommandId.make("stop"), |
| 1476 | threadId, |
| 1477 | runId: first.runId, |
| 1478 | holdQueue: true, |
| 1479 | }) |
| 1480 | .pipe(Effect.asVoid, Effect.orDie); |
| 1481 | if (timing === "stop-before-decline") beforeDecline = stop; |
| 1482 | const steerSelection = { ...runSelection, model: "steer-model" }; |
| 1483 | if (timing === "queued-model") { |
| 1484 | yield* orchestrator.dispatch({ |
| 1485 | type: "message.dispatch", |
| 1486 | commandId: CommandId.make("queued-model"), |
| 1487 | threadId, |
| 1488 | messageId: MessageId.make("message:queued-model"), |
| 1489 | text: "already queued", |
| 1490 | attachments: [], |
| 1491 | modelSelection: composerSelection, |
| 1492 | dispatchMode: { type: "queue_after_active" }, |
| 1493 | createdBy: "user", |
| 1494 | creationSource: "web", |
| 1495 | }); |
| 1496 | beforeDecline = Effect.forEach(["queued-later", "steer-later", "queued-last"], (id) => |
| 1497 | orchestrator.dispatch({ |
| 1498 | type: "message.dispatch", |
| 1499 | commandId: CommandId.make(id), |
| 1500 | threadId, |
| 1501 | messageId: MessageId.make(`message:${id}`), |
| 1502 | text: id, |
| 1503 | attachments: [], |
| 1504 | dispatchMode: |
| 1505 | id === "steer-later" |
| 1506 | ? { type: "steer_active", targetRunId: first.runId } |
| 1507 | : { type: "queue_after_active" }, |
| 1508 | createdBy: "user", |
| 1509 | creationSource: "web", |
| 1510 | }), |
| 1511 | ).pipe(Effect.asVoid, Effect.orDie); |
| 1512 | } |
| 1513 | if (timing === "two-steers") { |
| 1514 | // The second steer arrives before the first is declined. |
| 1515 | beforeDecline = orchestrator |
| 1516 | .dispatch({ |
| 1517 | type: "message.dispatch", |
| 1518 | commandId: CommandId.make("steer-second"), |
| 1519 | threadId, |
| 1520 | messageId: MessageId.make("message:steer-second"), |
| 1521 | text: "second", |
| 1522 | attachments: [], |
| 1523 | dispatchMode: { type: "steer_active", targetRunId: first.runId }, |
| 1524 | createdBy: "user", |
| 1525 | creationSource: "web", |
| 1526 | }) |
| 1527 | .pipe( |
| 1528 | Effect.andThen(Effect.sync(() => (beforeDecline = Effect.void))), |
| 1529 | Effect.asVoid, |
| 1530 | Effect.orDie, |
| 1531 | ); |
| 1532 | } |
| 1533 | const messageId = MessageId.make("message:declined"); |
| 1534 | yield* orchestrator.dispatch({ |
| 1535 | type: "message.dispatch", |
| 1536 | commandId: CommandId.make("steer"), |
| 1537 | threadId, |
| 1538 | messageId, |
| 1539 | text: "follow up", |
| 1540 | attachments: [], |
| 1541 | ...(timing === "queued-model" ? { modelSelection: steerSelection } : {}), |
| 1542 | dispatchMode: { type: "steer_active", targetRunId: first.runId }, |
| 1543 | createdBy: "user", |
| 1544 | creationSource: "web", |
| 1545 | }); |
| 1546 | yield* worker.drain(); |
| 1547 | const pending = yield* orchestrator.getThreadProjection(threadId); |
| 1548 | const queued = pending.runs.find( |
| 1549 | (run) => run.userMessageId === messageId && run.id !== first.runId, |
| 1550 | ); |
| 1551 | const effects = yield* (yield* EffectOutbox.EffectOutboxV2).listByCommandId( |
| 1552 | CommandId.make("steer"), |
| 1553 | ); |
| 1554 | assert.deepEqual( |
| 1555 | effects.map((effect) => [effect.request.type, effect.status]), |
| 1556 | [["provider-turn.steer", "succeeded"]], |
| 1557 | ); |
| 1558 | assert.equal(started.length, 1); |
| 1559 | if (timing === "stop-before-decline") { |
| 1560 | assert.isUndefined(queued); |
| 1561 | return; |
| 1562 | } |
| 1563 | assert.equal(queued?.status, "queued"); |
| 1564 | assert.equal(queued?.userMessageId, messageId); |
| 1565 | if (timing === "queued-model") { |
| 1566 | assert.deepEqual(queued?.modelSelection, steerSelection); |
| 1567 | assert.deepEqual( |
| 1568 | pending.runs |
| 1569 | .filter((run) => run.status === "queued") |
| 1570 | .toSorted( |
| 1571 | (left, right) => |
| 1572 | (left.queuePosition ?? left.ordinal) - (right.queuePosition ?? right.ordinal), |
| 1573 | ) |
| 1574 | .map((run) => run.userMessageId), |
| 1575 | [ |
| 1576 | MessageId.make("message:queued-model"), |
| 1577 | messageId, |
| 1578 | MessageId.make("message:queued-later"), |
| 1579 | MessageId.make("message:steer-later"), |
| 1580 | MessageId.make("message:queued-last"), |
| 1581 | ], |
| 1582 | ); |
| 1583 | return; |
| 1584 | } |
| 1585 | if (timing === "two-steers") { |
| 1586 | assert.deepEqual( |
| 1587 | pending.runs |
| 1588 | .filter((run) => run.status === "queued") |
| 1589 | .toSorted( |
| 1590 | (left, right) => |
| 1591 | (left.queuePosition ?? left.ordinal) - (right.queuePosition ?? right.ordinal), |
| 1592 | ) |
| 1593 | .map((run) => run.userMessageId), |
| 1594 | [messageId, MessageId.make("message:steer-second")], |
| 1595 | ); |
| 1596 | return; |
| 1597 | } |
| 1598 | if (timing === "stop-after-decline") { |
| 1599 | yield* stop; |
| 1600 | yield* worker.drain(); |
| 1601 | yield* orchestrator.resumeQueuedRuns; |
| 1602 | yield* worker.drain(); |
| 1603 | const held = yield* orchestrator.getThreadProjection(threadId); |
| 1604 | assert.equal(held.runs.find((run) => run.id === queued?.id)?.queueHeld, true); |
| 1605 | assert.equal(started.length, 1); |
| 1606 | return; |
| 1607 | } |
| 1608 | const settled = yield* orchestrator.streamDomainEvents.pipe( |
| 1609 | Stream.filter( |
| 1610 | (event) => |
| 1611 | event.type === "run.updated" && |
| 1612 | event.payload.id === first.runId && |
| 1613 | event.payload.status === "waiting", |
| 1614 | ), |
| 1615 | Stream.take(1), |
| 1616 | Stream.runDrain, |
| 1617 | Effect.forkScoped, |
| 1618 | ); |
| 1619 | const turn = pending.providerTurns[0]!; |
| 1620 | yield* Queue.offer(events, { |
| 1621 | type: "provider_turn.updated", |
| 1622 | driver, |
| 1623 | providerTurn: { ...turn, status: "completed", completedAt: yield* DateTime.now }, |
| 1624 | }); |
| 1625 | yield* Queue.offer(events, { |
| 1626 | type: "turn.terminal", |
| 1627 | driver, |
| 1628 | providerThreadId: turn.providerThreadId, |
| 1629 | providerTurnId: turn.id, |
| 1630 | runOrdinal: first.runOrdinal, |
| 1631 | status: "completed", |
| 1632 | failure: null, |
| 1633 | threadDisposition: "reusable", |
| 1634 | }); |
| 1635 | yield* Fiber.join(settled); |
| 1636 | yield* worker.drain(); |
| 1637 | yield* orchestrator.resumeQueuedRuns; |
| 1638 | yield* worker.drain(); |
| 1639 | assert.equal(started.length, 2); |
| 1640 | assert.equal(started[1]?.message.messageId, messageId); |
| 1641 | assert.equal(started[1]?.runId, queued?.id); |
| 1642 | }).pipe(Effect.provide(layer)); |
| 1643 | }), |
| 1644 | ); |
| 1645 | |
| 1646 | // regression: #26695 cancelling a Copilot prompt to steer it kills its background agents |
| 1647 | it.effect("queues a steer until the turn ends when the provider cannot steer it", () => |
| 1648 | Effect.gen(function* () { |
| 1649 | const { events, started, steered, interrupted, layer, startFirstTurn } = |
| 1650 | yield* nextTurnSelectionHarness("steering-unsupported", false, false); |
| 1651 | yield* Effect.gen(function* () { |
| 1652 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 1653 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 1654 | const threadId = yield* startFirstTurn; |
| 1655 | const first = started[0]!; |
| 1656 | const messageId = MessageId.make("message:steer"); |
| 1657 | yield* orchestrator.dispatch({ |
| 1658 | type: "message.dispatch", |
| 1659 | commandId: CommandId.make("steer"), |
| 1660 | threadId, |
| 1661 | messageId, |
| 1662 | text: "follow up", |
| 1663 | attachments: [], |
| 1664 | modelSelection: runSelection, |
| 1665 | dispatchMode: { type: "start_immediately" }, |
| 1666 | deliveryIntent: "steer", |
| 1667 | createdBy: "user", |
| 1668 | creationSource: "web", |
| 1669 | }); |
| 1670 | yield* worker.drain(); |
| 1671 | |
| 1672 | const held = yield* orchestrator.getThreadProjection(threadId); |
| 1673 | assert.deepEqual(steered, []); |
| 1674 | assert.deepEqual(interrupted, []); |
| 1675 | assert.equal(started.length, 1); |
| 1676 | assert.equal(held.runs.find((run) => run.id === first.runId)?.status, "running"); |
| 1677 | const queued = held.runs.find((run) => run.userMessageId === messageId); |
| 1678 | assert.equal(queued?.status, "queued"); |
| 1679 | |
| 1680 | const settled = yield* orchestrator.streamDomainEvents.pipe( |
| 1681 | Stream.filter( |
| 1682 | (event) => |
| 1683 | event.type === "run.updated" && |
| 1684 | event.payload.id === first.runId && |
| 1685 | event.payload.status === "waiting", |
| 1686 | ), |
| 1687 | Stream.take(1), |
| 1688 | Stream.runDrain, |
| 1689 | Effect.forkScoped, |
| 1690 | ); |
| 1691 | const turn = held.providerTurns[0]!; |
| 1692 | yield* Queue.offer(events, { |
| 1693 | type: "provider_turn.updated", |
| 1694 | driver, |
| 1695 | providerTurn: { ...turn, status: "completed", completedAt: yield* DateTime.now }, |
| 1696 | }); |
| 1697 | yield* Queue.offer(events, { |
| 1698 | type: "turn.terminal", |
| 1699 | driver, |
| 1700 | providerThreadId: turn.providerThreadId, |
| 1701 | providerTurnId: turn.id, |
| 1702 | runOrdinal: first.runOrdinal, |
| 1703 | status: "completed", |
| 1704 | failure: null, |
| 1705 | threadDisposition: "reusable", |
| 1706 | }); |
| 1707 | yield* Fiber.join(settled); |
| 1708 | yield* worker.drain(); |
| 1709 | yield* orchestrator.resumeQueuedRuns; |
| 1710 | yield* worker.drain(); |
| 1711 | |
| 1712 | assert.deepEqual(interrupted, []); |
| 1713 | assert.equal(started.length, 2); |
| 1714 | assert.equal(started[1]?.runId, queued?.id); |
| 1715 | assert.equal(started[1]?.message.text, "follow up"); |
| 1716 | }).pipe(Effect.provide(layer)); |
| 1717 | }), |
| 1718 | ); |
| 1719 | |
| 1720 | // regression: #39404 a later agent message steers ahead of one queued behind /compact |
| 1721 | it.effect("delivers agent messages queued behind /compact before a later one steers", () => |
| 1722 | Effect.gen(function* () { |
| 1723 | const { events, started, steered, layer, startFirstTurn } = yield* nextTurnSelectionHarness( |
| 1724 | "agent-order-after-compact", |
| 1725 | ); |
| 1726 | yield* Effect.gen(function* () { |
| 1727 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 1728 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 1729 | const threadId = yield* startFirstTurn; |
| 1730 | const nextRunning = Effect.fn("nextRunning")(function* () { |
| 1731 | return yield* orchestrator.streamDomainEvents.pipe( |
| 1732 | Stream.filter( |
| 1733 | (event) => event.type === "provider-turn.updated" && event.payload.status === "running", |
| 1734 | ), |
| 1735 | Stream.take(1), |
| 1736 | Stream.runDrain, |
| 1737 | Effect.forkScoped, |
| 1738 | ); |
| 1739 | }); |
| 1740 | const lock = (yield* ThreadCommandExecutor.ThreadCommandExecutor).withLock( |
| 1741 | threadId, |
| 1742 | Effect.void, |
| 1743 | ); |
| 1744 | const finishTurn = Effect.fn("finishTurn")(function* (index: number) { |
| 1745 | const turnInput = started[index]!; |
| 1746 | const turn = (yield* orchestrator.getThreadProjection(threadId)).providerTurns.find( |
| 1747 | (candidate) => candidate.runAttemptId === turnInput.attemptId, |
| 1748 | )!; |
| 1749 | const settled = yield* orchestrator.streamDomainEvents.pipe( |
| 1750 | Stream.filter( |
| 1751 | (event) => |
| 1752 | event.type === "run.updated" && |
| 1753 | event.payload.id === turnInput.runId && |
| 1754 | (event.payload.status === "waiting" || event.payload.status === "completed"), |
| 1755 | ), |
| 1756 | Stream.take(1), |
| 1757 | Stream.runDrain, |
| 1758 | Effect.forkScoped, |
| 1759 | ); |
| 1760 | yield* Queue.offer(events, { |
| 1761 | type: "provider_turn.updated", |
| 1762 | driver, |
| 1763 | providerTurn: { ...turn, status: "completed", completedAt: yield* DateTime.now }, |
| 1764 | }); |
| 1765 | yield* Queue.offer(events, { |
| 1766 | type: "turn.terminal", |
| 1767 | driver, |
| 1768 | providerThreadId: turn.providerThreadId, |
| 1769 | providerTurnId: turn.id, |
| 1770 | runOrdinal: turnInput.runOrdinal, |
| 1771 | status: "completed", |
| 1772 | failure: null, |
| 1773 | threadDisposition: "reusable", |
| 1774 | }); |
| 1775 | yield* Fiber.join(settled); |
| 1776 | yield* worker.drain(); |
| 1777 | yield* orchestrator.resumeQueuedRuns; |
| 1778 | yield* worker.drain(); |
| 1779 | }); |
| 1780 | yield* finishTurn(0); |
| 1781 | const compactRunning = yield* nextRunning(); |
| 1782 | yield* orchestrator.dispatch({ |
| 1783 | type: "message.dispatch", |
| 1784 | commandId: CommandId.make("compact"), |
| 1785 | threadId, |
| 1786 | messageId: MessageId.make("message:compact"), |
| 1787 | text: "/compact", |
| 1788 | attachments: [], |
| 1789 | dispatchMode: { type: "start_immediately" }, |
| 1790 | createdBy: "user", |
| 1791 | creationSource: "web", |
| 1792 | }); |
| 1793 | yield* worker.drain(); |
| 1794 | yield* Fiber.join(compactRunning); |
| 1795 | yield* lock; |
| 1796 | const sendFromAgent = (codeword: string) => |
| 1797 | Effect.gen(function* () { |
| 1798 | const active = (yield* orchestrator.getThreadProjection(threadId)).runs.find( |
| 1799 | (run) => run.status === "running", |
| 1800 | )!; |
| 1801 | yield* orchestrator.dispatch({ |
| 1802 | type: "message.dispatch", |
| 1803 | commandId: CommandId.make(`send-${codeword}`), |
| 1804 | threadId, |
| 1805 | messageId: MessageId.make(`message:${codeword}`), |
| 1806 | text: codeword, |
| 1807 | attachments: [], |
| 1808 | dispatchMode: { type: "steer_active", targetRunId: active.id }, |
| 1809 | deliveryIntent: "auto", |
| 1810 | createdBy: "agent", |
| 1811 | creationSource: "mcp", |
| 1812 | senderThreadId: ThreadId.make("thread:orchestrator"), |
| 1813 | }); |
| 1814 | yield* worker.drain(); |
| 1815 | }); |
| 1816 | yield* sendFromAgent("ALPHA"); |
| 1817 | yield* sendFromAgent("BRAVO"); |
| 1818 | const queued = (yield* orchestrator.getThreadProjection(threadId)).runs.filter( |
| 1819 | (run) => run.status === "queued", |
| 1820 | ); |
| 1821 | assert.lengthOf(queued, 2); |
| 1822 | assert.deepEqual(steered, []); |
| 1823 | |
| 1824 | const alphaRunning = yield* nextRunning(); |
| 1825 | yield* finishTurn(1); |
| 1826 | yield* Fiber.join(alphaRunning); |
| 1827 | yield* lock; |
| 1828 | assert.deepEqual( |
| 1829 | started.map((turn) => turn.message.text.split("\n").at(-1)), |
| 1830 | ["first", "/compact", "ALPHA"], |
| 1831 | ); |
| 1832 | |
| 1833 | yield* sendFromAgent("CHARLIE"); |
| 1834 | const delivered = [...steered, ...started.slice(3).map((turn) => turn.message.text)] |
| 1835 | .map((text) => text.split("\n").at(-1)) |
| 1836 | .filter((text) => text === "BRAVO" || text === "CHARLIE"); |
| 1837 | // Whatever reaches the agent so far does so in send order: CHARLIE never before BRAVO. |
| 1838 | assert.deepEqual(delivered, ["BRAVO", "CHARLIE"].slice(0, delivered.length)); |
| 1839 | }).pipe(Effect.provide(layer)); |
| 1840 | }), |
| 1841 | ); |
| 1842 | |
| 1843 | // regression: #33454 a steer that restarts a completion wake run left its cohort claimed forever |
| 1844 | const restartCompletionWake = Effect.fn("restartCompletionWake")(function* ( |
| 1845 | harness: Pick< |
| 1846 | Effect.Success<ReturnType<typeof nextTurnSelectionHarness>>, |
| 1847 | "events" | "started" | "startFirstTurn" |
| 1848 | >, |
| 1849 | siblingFinishesFirst: boolean, |
| 1850 | userRunBeforeWake = false, |
| 1851 | intentNamesTarget = false, |
| 1852 | ) { |
| 1853 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 1854 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 1855 | const sink = yield* EventSink.EventSinkV2; |
| 1856 | const threadId = yield* harness.startFirstTurn; |
| 1857 | const watchRun = (runId: string, status: string) => |
| 1858 | orchestrator.streamDomainEvents.pipe( |
| 1859 | Stream.filter( |
| 1860 | (event) => |
| 1861 | event.type === "run.updated" && |
| 1862 | event.payload.id === runId && |
| 1863 | event.payload.status === status, |
| 1864 | ), |
| 1865 | Stream.take(1), |
| 1866 | Stream.runDrain, |
| 1867 | Effect.forkScoped, |
| 1868 | ); |
| 1869 | const watchTurnRunning = (ordinal: number) => |
| 1870 | orchestrator.streamDomainEvents.pipe( |
| 1871 | Stream.filter( |
| 1872 | (event) => |
| 1873 | event.type === "provider-turn.updated" && |
| 1874 | event.payload.status === "running" && |
| 1875 | event.payload.ordinal === ordinal, |
| 1876 | ), |
| 1877 | Stream.take(1), |
| 1878 | Stream.runDrain, |
| 1879 | Effect.forkScoped, |
| 1880 | ); |
| 1881 | const finishTurn = (turn: ProviderAdapter.ProviderAdapterV2TurnInput) => |
| 1882 | Effect.gen(function* () { |
| 1883 | const providerTurn = (yield* orchestrator.getThreadProjection(threadId)).providerTurns.find( |
| 1884 | (candidate) => candidate.runAttemptId === turn.attemptId, |
| 1885 | )!; |
| 1886 | const settled = yield* watchRun(turn.runId, "waiting"); |
| 1887 | yield* Queue.offer(harness.events, { |
| 1888 | type: "provider_turn.updated", |
| 1889 | driver, |
| 1890 | providerTurn: { |
| 1891 | ...providerTurn, |
| 1892 | status: "completed", |
| 1893 | completedAt: yield* DateTime.now, |
| 1894 | }, |
| 1895 | }); |
| 1896 | yield* Queue.offer(harness.events, { |
| 1897 | type: "turn.terminal", |
| 1898 | driver, |
| 1899 | providerThreadId: providerTurn.providerThreadId, |
| 1900 | providerTurnId: providerTurn.id, |
| 1901 | runOrdinal: turn.runOrdinal, |
| 1902 | status: "completed", |
| 1903 | failure: null, |
| 1904 | threadDisposition: "reusable", |
| 1905 | }); |
| 1906 | yield* Fiber.join(settled); |
| 1907 | yield* worker.drain(); |
| 1908 | }); |
| 1909 | |
| 1910 | const origin = harness.started[0]!; |
| 1911 | const firstTaskId = NodeId.make("task:steered-wake-first"); |
| 1912 | const siblingTaskId = NodeId.make("task:steered-wake-sibling"); |
| 1913 | const now = yield* DateTime.now; |
| 1914 | yield* sink.write({ |
| 1915 | events: [firstTaskId, siblingTaskId].map((taskId) => ({ |
| 1916 | id: EventId.make(`event:seed:${taskId}`), |
| 1917 | type: "subagent.updated" as const, |
| 1918 | threadId, |
| 1919 | runId: origin.runId, |
| 1920 | nodeId: taskId, |
| 1921 | occurredAt: now, |
| 1922 | payload: { |
| 1923 | id: taskId, |
| 1924 | threadId, |
| 1925 | runId: origin.runId, |
| 1926 | parentNodeId: origin.rootNodeId, |
| 1927 | origin: "app_owned" as const, |
| 1928 | createdBy: "agent" as const, |
| 1929 | driver, |
| 1930 | providerInstanceId: instanceId, |
| 1931 | providerThreadId: null, |
| 1932 | childThreadId: null, |
| 1933 | remoteChild: { |
| 1934 | environmentId: EnvironmentId.make("environment:box"), |
| 1935 | threadId: ThreadId.make(`thread:remote:${taskId}`), |
| 1936 | label: "box", |
| 1937 | }, |
| 1938 | nativeTaskRef: null, |
| 1939 | prompt: "Do background work", |
| 1940 | title: null, |
| 1941 | model: null, |
| 1942 | completionWake: "always" as const, |
| 1943 | completionDelivery: { state: "pending" as const, observedByRunId: null }, |
| 1944 | status: "running" as const, |
| 1945 | result: null, |
| 1946 | startedAt: now, |
| 1947 | completedAt: null, |
| 1948 | updatedAt: now, |
| 1949 | }, |
| 1950 | })), |
| 1951 | }); |
| 1952 | yield* finishTurn(origin); |
| 1953 | // The wake queues behind a user run that started after the origin run finished. |
| 1954 | if (userRunBeforeWake) { |
| 1955 | const userRunning = yield* watchTurnRunning(2); |
| 1956 | yield* orchestrator.dispatch({ |
| 1957 | type: "message.dispatch", |
| 1958 | commandId: CommandId.make("command:user-run"), |
| 1959 | threadId, |
| 1960 | messageId: MessageId.make("message:user-run"), |
| 1961 | text: "unrelated question", |
| 1962 | attachments: [], |
| 1963 | modelSelection: runSelection, |
| 1964 | dispatchMode: { type: "start_immediately" }, |
| 1965 | createdBy: "user", |
| 1966 | creationSource: "web", |
| 1967 | }); |
| 1968 | yield* worker.drain(); |
| 1969 | yield* Fiber.join(userRunning); |
| 1970 | } |
| 1971 | |
| 1972 | const completeTask = (taskId: NodeId) => |
| 1973 | orchestrator.dispatch({ |
| 1974 | type: "delegated_task.remote.complete", |
| 1975 | commandId: CommandId.make(`command:remote-complete:${taskId}`), |
| 1976 | parentThreadId: threadId, |
| 1977 | taskId, |
| 1978 | status: "completed", |
| 1979 | result: `${taskId} finished`, |
| 1980 | stoppedThere: "ran-out", |
| 1981 | }); |
| 1982 | yield* completeTask(firstTaskId); |
| 1983 | const firstDelivery = (yield* orchestrator.getThreadProjection(threadId)).runs.find( |
| 1984 | (run) => run.id === origin.runId, |
| 1985 | )!.delegatedCompletion!.delivery!; |
| 1986 | assert.deepEqual(firstDelivery.taskIds, [firstTaskId]); |
| 1987 | yield* orchestrator.dispatch({ |
| 1988 | type: "message.dispatch", |
| 1989 | commandId: CommandId.make("command:first-wake"), |
| 1990 | threadId, |
| 1991 | messageId: firstDelivery.messageId, |
| 1992 | text: "Delegated task completed", |
| 1993 | attachments: [], |
| 1994 | modelSelection: runSelection, |
| 1995 | dispatchMode: { type: "queue_after_active" }, |
| 1996 | delegatedCompletion: { |
| 1997 | parentRunId: origin.runId, |
| 1998 | generation: firstDelivery.generation, |
| 1999 | taskIds: firstDelivery.taskIds, |
| 2000 | }, |
| 2001 | createdBy: "agent", |
| 2002 | creationSource: "server", |
| 2003 | }); |
| 2004 | const wakeOrdinal = userRunBeforeWake ? 3 : 2; |
| 2005 | const wakeRunning = yield* watchTurnRunning(wakeOrdinal); |
| 2006 | if (userRunBeforeWake) yield* finishTurn(harness.started[1]!); |
| 2007 | yield* orchestrator.resumeQueuedRuns; |
| 2008 | yield* worker.drain(); |
| 2009 | yield* Fiber.join(wakeRunning); |
| 2010 | const wake = harness.started[wakeOrdinal - 1]!; |
| 2011 | assert.equal(wake.message.messageId, firstDelivery.messageId); |
| 2012 | if (userRunBeforeWake) { |
| 2013 | // The wake's run holds a compaction, so the origin run's records are older than the window. |
| 2014 | const compactedAt = yield* DateTime.now; |
| 2015 | yield* sink.write({ |
| 2016 | events: [ |
| 2017 | { |
| 2018 | id: EventId.make("event:wake-compaction"), |
| 2019 | type: "turn-item.updated" as const, |
| 2020 | threadId, |
| 2021 | runId: wake.runId, |
| 2022 | nodeId: wake.rootNodeId, |
| 2023 | occurredAt: compactedAt, |
| 2024 | payload: { |
| 2025 | id: TurnItemId.make("item:wake-compaction"), |
| 2026 | threadId, |
| 2027 | runId: wake.runId, |
| 2028 | nodeId: wake.rootNodeId, |
| 2029 | providerThreadId: null, |
| 2030 | providerTurnId: null, |
| 2031 | nativeItemRef: null, |
| 2032 | parentItemId: null, |
| 2033 | ordinal: 1_000, |
| 2034 | status: "completed" as const, |
| 2035 | title: null, |
| 2036 | startedAt: compactedAt, |
| 2037 | completedAt: compactedAt, |
| 2038 | updatedAt: compactedAt, |
| 2039 | type: "compaction" as const, |
| 2040 | driver, |
| 2041 | }, |
| 2042 | }, |
| 2043 | ], |
| 2044 | }); |
| 2045 | } |
| 2046 | |
| 2047 | if (siblingFinishesFirst) { |
| 2048 | yield* completeTask(siblingTaskId); |
| 2049 | } |
| 2050 | |
| 2051 | // The provider cannot steer natively, so the steer restarts the wake run. |
| 2052 | const steeredRunning = yield* watchTurnRunning(wakeOrdinal + 1); |
| 2053 | yield* orchestrator.dispatch({ |
| 2054 | type: "message.dispatch", |
| 2055 | commandId: CommandId.make("command:steer-wake"), |
| 2056 | threadId, |
| 2057 | messageId: MessageId.make("message:steer-wake"), |
| 2058 | text: "also check the logs", |
| 2059 | attachments: [], |
| 2060 | modelSelection: runSelection, |
| 2061 | ...(intentNamesTarget |
| 2062 | ? { dispatchMode: { type: "start_immediately" as const }, deliveryIntent: "restart" as const } |
| 2063 | : { dispatchMode: { type: "steer_active" as const, targetRunId: wake.runId } }), |
| 2064 | createdBy: "user", |
| 2065 | creationSource: "web", |
| 2066 | }); |
| 2067 | yield* worker.drain(); |
| 2068 | yield* Fiber.join(steeredRunning); |
| 2069 | const steered = harness.started[wakeOrdinal]!; |
| 2070 | assert.equal(steered.runId, wake.runId); |
| 2071 | assert.equal( |
| 2072 | (yield* orchestrator.getThreadProjection(threadId)).attempts.find( |
| 2073 | (attempt) => attempt.id === steered.attemptId, |
| 2074 | )?.reason, |
| 2075 | "steering_restart", |
| 2076 | ); |
| 2077 | return { |
| 2078 | threadId, |
| 2079 | origin, |
| 2080 | wake, |
| 2081 | steered, |
| 2082 | firstTaskId, |
| 2083 | siblingTaskId, |
| 2084 | watchRun, |
| 2085 | finishTurn, |
| 2086 | completeTask, |
| 2087 | }; |
| 2088 | }); |
| 2089 | |
| 2090 | it.effect("wakes the parent for a sibling result after a steer restarts the completion wake", () => |
| 2091 | Effect.gen(function* () { |
| 2092 | const harness = yield* nextTurnSelectionHarness("steered-completion-wake", true, false); |
| 2093 | yield* Effect.gen(function* () { |
| 2094 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 2095 | const { |
| 2096 | threadId, |
| 2097 | origin, |
| 2098 | wake, |
| 2099 | steered, |
| 2100 | firstTaskId, |
| 2101 | siblingTaskId, |
| 2102 | watchRun, |
| 2103 | finishTurn, |
| 2104 | completeTask, |
| 2105 | } = yield* restartCompletionWake(harness, false); |
| 2106 | |
| 2107 | // A follow-up queued behind the wake starts once the terminal-run listener has settled it. |
| 2108 | const followUp = MessageId.make("message:follow-up"); |
| 2109 | yield* orchestrator.dispatch({ |
| 2110 | type: "message.dispatch", |
| 2111 | commandId: CommandId.make("command:follow-up"), |
| 2112 | threadId, |
| 2113 | messageId: followUp, |
| 2114 | text: "follow up", |
| 2115 | attachments: [], |
| 2116 | modelSelection: runSelection, |
| 2117 | dispatchMode: { type: "queue_after_active" }, |
| 2118 | createdBy: "user", |
| 2119 | creationSource: "web", |
| 2120 | }); |
| 2121 | const followUpRun = (yield* orchestrator.getThreadProjection(threadId)).runs.find( |
| 2122 | (run) => run.userMessageId === followUp, |
| 2123 | )!; |
| 2124 | const followUpStarted = yield* watchRun(followUpRun.id, "starting"); |
| 2125 | yield* finishTurn(steered); |
| 2126 | yield* Fiber.join(followUpStarted); |
| 2127 | assert.equal( |
| 2128 | (yield* orchestrator.getThreadProjection(threadId)).runs.find( |
| 2129 | (run) => run.id === wake.runId, |
| 2130 | )?.status, |
| 2131 | "completed", |
| 2132 | ); |
| 2133 | |
| 2134 | yield* completeTask(siblingTaskId); |
| 2135 | const final = yield* orchestrator.getThreadProjection(threadId); |
| 2136 | const tasks = new Map( |
| 2137 | final.subagents.map((task) => [task.id, task.completionDelivery?.state]), |
| 2138 | ); |
| 2139 | assert.deepInclude( |
| 2140 | final.runs.find((run) => run.id === origin.runId)?.delegatedCompletion?.delivery ?? {}, |
| 2141 | { generation: 2, taskIds: [siblingTaskId] }, |
| 2142 | "the sibling finished after the steered wake but no new completion wake was reserved", |
| 2143 | ); |
| 2144 | assert.equal(tasks.get(siblingTaskId), "claimed"); |
| 2145 | assert.equal( |
| 2146 | tasks.get(firstTaskId), |
| 2147 | "delivered", |
| 2148 | "the steered wake run's own task stays claimed after that run completed", |
| 2149 | ); |
| 2150 | }).pipe(Effect.provide(harness.layer)); |
| 2151 | }), |
| 2152 | ); |
| 2153 | |
| 2154 | it.effect.each([false, true])( |
| 2155 | "settles a steered completion wake whose parent run is before the last compaction (restart by intent: %s)", |
| 2156 | (intentNamesTarget) => |
| 2157 | Effect.gen(function* () { |
| 2158 | const harness = yield* nextTurnSelectionHarness( |
| 2159 | `steered-wake-after-compaction-${intentNamesTarget}`, |
| 2160 | true, |
| 2161 | false, |
| 2162 | ); |
| 2163 | yield* Effect.gen(function* () { |
| 2164 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 2165 | const { threadId, steered, firstTaskId, finishTurn } = yield* restartCompletionWake( |
| 2166 | harness, |
| 2167 | false, |
| 2168 | true, |
| 2169 | intentNamesTarget, |
| 2170 | ); |
| 2171 | yield* finishTurn(steered); |
| 2172 | const tasks = (yield* orchestrator.getThreadProjection(threadId)).subagents; |
| 2173 | assert.equal( |
| 2174 | tasks.find((task) => task.id === firstTaskId)?.completionDelivery?.state, |
| 2175 | "delivered", |
| 2176 | ); |
| 2177 | }).pipe(Effect.provide(harness.layer)); |
| 2178 | }), |
| 2179 | ); |
| 2180 | |
| 2181 | it.effect( |
| 2182 | "offers the next completion wake when a steer restarts a wake a sibling finished behind", |
| 2183 | () => |
| 2184 | Effect.gen(function* () { |
| 2185 | const harness = yield* nextTurnSelectionHarness( |
| 2186 | "steered-completion-wake-behind", |
| 2187 | true, |
| 2188 | false, |
| 2189 | ); |
| 2190 | const offers: ProviderContinuationRequests.ProviderContinuationRequest[] = []; |
| 2191 | const requests = Layer.succeed(ProviderContinuationRequests.ProviderContinuationRequests, { |
| 2192 | offer: (request) => Effect.sync(() => void offers.push(request)), |
| 2193 | take: Effect.never, |
| 2194 | }); |
| 2195 | yield* Effect.gen(function* () { |
| 2196 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 2197 | const { threadId, origin, firstTaskId, siblingTaskId } = yield* restartCompletionWake( |
| 2198 | harness, |
| 2199 | true, |
| 2200 | ); |
| 2201 | const projection = yield* orchestrator.getThreadProjection(threadId); |
| 2202 | const delivery = projection.runs.find((run) => run.id === origin.runId)?.delegatedCompletion |
| 2203 | ?.delivery; |
| 2204 | assert.deepInclude(delivery ?? {}, { generation: 2, taskIds: [siblingTaskId] }); |
| 2205 | const tasks = new Map( |
| 2206 | projection.subagents.map((task) => [task.id, task.completionDelivery?.state]), |
| 2207 | ); |
| 2208 | assert.equal(tasks.get(firstTaskId), "delivered"); |
| 2209 | assert.equal(tasks.get(siblingTaskId), "claimed"); |
| 2210 | assert.deepEqual(offers.at(-1)?.delegatedCompletion, { |
| 2211 | parentRunId: origin.runId, |
| 2212 | generation: 2, |
| 2213 | messageId: delivery!.messageId, |
| 2214 | }); |
| 2215 | }).pipe(Effect.provide(harness.layer.pipe(Layer.provide(requests)))); |
| 2216 | }), |
| 2217 | ); |
| 2218 | |
| 2219 | // Codex flushes held drafts after the final answer; reserve their pre-steer position before turn end. |
| 2220 | it.effect("keeps a held commentary draft before the steer when turn end flushes it", () => |
| 2221 | Effect.gen(function* () { |
| 2222 | const { events, started, layer, startFirstTurn } = |
| 2223 | yield* nextTurnSelectionHarness("steering-held-draft"); |
| 2224 | yield* Effect.gen(function* () { |
| 2225 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 2226 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 2227 | const threadId = yield* startFirstTurn; |
| 2228 | const first = started[0]!; |
| 2229 | const turn = (yield* orchestrator.getThreadProjection(threadId)).providerTurns[0]!; |
| 2230 | const now = yield* DateTime.now; |
| 2231 | const assistantItem = (id: string, text: string, streaming: boolean) => |
| 2232 | ({ |
| 2233 | type: "turn_item.updated", |
| 2234 | driver, |
| 2235 | turnItem: { |
| 2236 | id: TurnItemId.make(id), |
| 2237 | threadId, |
| 2238 | runId: first.runId, |
| 2239 | nodeId: first.rootNodeId, |
| 2240 | providerThreadId: turn.providerThreadId, |
| 2241 | providerTurnId: turn.id, |
| 2242 | nativeItemRef: null, |
| 2243 | parentItemId: null, |
| 2244 | ordinal: 0, |
| 2245 | status: streaming ? "running" : "completed", |
| 2246 | title: null, |
| 2247 | startedAt: now, |
| 2248 | completedAt: streaming ? null : now, |
| 2249 | updatedAt: now, |
| 2250 | type: "assistant_message", |
| 2251 | messageId: MessageId.make(`message:${id}`), |
| 2252 | text, |
| 2253 | streaming, |
| 2254 | }, |
| 2255 | }) satisfies ProviderAdapter.ProviderAdapterV2Event; |
| 2256 | const watch = (predicate: (event: OrchestrationV2DomainEvent) => boolean) => |
| 2257 | orchestrator.streamDomainEvents.pipe( |
| 2258 | Stream.filter(predicate), |
| 2259 | Stream.take(1), |
| 2260 | Stream.runDrain, |
| 2261 | Effect.forkScoped, |
| 2262 | ); |
| 2263 | |
| 2264 | yield* Queue.offer(events, assistantItem("draft", "Let me look at the", true)); |
| 2265 | const commentaryWritten = yield* watch( |
| 2266 | (event) => event.type === "turn-item.updated" && event.payload.id === "commentary", |
| 2267 | ); |
| 2268 | yield* Queue.offer(events, assistantItem("commentary", "Checking the popover.", false)); |
| 2269 | yield* Fiber.join(commentaryWritten); |
| 2270 | |
| 2271 | yield* orchestrator.dispatch({ |
| 2272 | type: "message.dispatch", |
| 2273 | commandId: CommandId.make("steer"), |
| 2274 | threadId, |
| 2275 | messageId: MessageId.make("message:steer"), |
| 2276 | text: "fix the popover", |
| 2277 | attachments: [], |
| 2278 | dispatchMode: { type: "steer_active", targetRunId: first.runId }, |
| 2279 | createdBy: "user", |
| 2280 | creationSource: "web", |
| 2281 | }); |
| 2282 | yield* worker.drain(); |
| 2283 | |
| 2284 | const settled = yield* watch( |
| 2285 | (event) => |
| 2286 | event.type === "run.updated" && |
| 2287 | event.payload.id === first.runId && |
| 2288 | event.payload.status === "waiting", |
| 2289 | ); |
| 2290 | yield* Queue.offer(events, assistantItem("answer", "All fixed.", false)); |
| 2291 | yield* Queue.offer(events, assistantItem("draft", "Let me look at the", false)); |
| 2292 | yield* Queue.offer(events, { |
| 2293 | type: "provider_turn.updated", |
| 2294 | driver, |
| 2295 | providerTurn: { ...turn, status: "completed", completedAt: yield* DateTime.now }, |
| 2296 | }); |
| 2297 | yield* Queue.offer(events, { |
| 2298 | type: "turn.terminal", |
| 2299 | driver, |
| 2300 | providerThreadId: turn.providerThreadId, |
| 2301 | providerTurnId: turn.id, |
| 2302 | runOrdinal: first.runOrdinal, |
| 2303 | status: "completed", |
| 2304 | failure: null, |
| 2305 | threadDisposition: "reusable", |
| 2306 | }); |
| 2307 | yield* Fiber.join(settled); |
| 2308 | yield* worker.drain(); |
| 2309 | |
| 2310 | const items = (yield* orchestrator.getThreadProjection(threadId)).turnItems |
| 2311 | .toSorted((left, right) => left.ordinal - right.ordinal) |
| 2312 | .flatMap((item) => |
| 2313 | item.type === "user_message" |
| 2314 | ? [String(item.messageId)] |
| 2315 | : item.type === "assistant_message" |
| 2316 | ? [String(item.id)] |
| 2317 | : [], |
| 2318 | ); |
| 2319 | assert.deepEqual(items, ["message:first", "draft", "commentary", "message:steer", "answer"]); |
| 2320 | }).pipe(Effect.provide(layer)); |
| 2321 | }), |
| 2322 | ); |
| 2323 | |
| 2324 | it.effect.each(["auto", "deferred", "promote-edited"] as const)( |
| 2325 | "rejects expanded steering input before commit: %s", |
| 2326 | (delivery) => |
| 2327 | Effect.scoped( |
| 2328 | Effect.gen(function* () { |
| 2329 | const { started, steered, interrupted, layer, startFirstTurn } = |
| 2330 | yield* nextTurnSelectionHarness(`oversized-steer-${delivery}`, true); |
| 2331 | yield* Effect.gen(function* () { |
| 2332 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 2333 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 2334 | const outbox = yield* EffectOutbox.EffectOutboxV2; |
| 2335 | const sink = yield* EventSink.EventSinkV2; |
| 2336 | const threadId = yield* startFirstTurn; |
| 2337 | const first = started[0]!; |
| 2338 | const citation = serializeAssistantCitation({ |
| 2339 | version: 1, |
| 2340 | environmentId: EnvironmentId.make("env"), |
| 2341 | threadId, |
| 2342 | messageId: MessageId.make("assistant"), |
| 2343 | text: "\u0001".repeat(8_000), |
| 2344 | start: 0, |
| 2345 | end: 8_000, |
| 2346 | prefix: "", |
| 2347 | suffix: "", |
| 2348 | }); |
| 2349 | const text = `${"x".repeat(80_000)}${citation}`; |
| 2350 | // Context excerpts are not part of the limit, matching the web composer's check. |
| 2351 | const largeContextText = |
| 2352 | "Read [one](t3-context://v1/terminal/one) and [two](t3-context://v1/terminal/two)"; |
| 2353 | const context = { |
| 2354 | version: 1 as const, |
| 2355 | records: (["one", "two"] as const).map((id) => ({ |
| 2356 | version: 1 as const, |
| 2357 | kind: "terminal" as const, |
| 2358 | contextId: ComposerContextId.make(id), |
| 2359 | label: id, |
| 2360 | terminalId: "terminal", |
| 2361 | terminalLabel: "Terminal", |
| 2362 | lineStart: 1, |
| 2363 | lineEnd: 1, |
| 2364 | text: "y".repeat(COMPOSER_CONTEXT_TERMINAL_TEXT_MAX_CHARS), |
| 2365 | })), |
| 2366 | }; |
| 2367 | assert.isBelow(text.length, PROVIDER_SEND_TURN_MAX_INPUT_CHARS); |
| 2368 | assert.isAbove( |
| 2369 | expandAssistantCitationsForProvider(text).length, |
| 2370 | PROVIDER_SEND_TURN_MAX_INPUT_CHARS, |
| 2371 | ); |
| 2372 | assert.isAbove( |
| 2373 | projectComposerContextForProvider({ |
| 2374 | text: largeContextText, |
| 2375 | records: context.records, |
| 2376 | }).length, |
| 2377 | PROVIDER_SEND_TURN_MAX_INPUT_CHARS, |
| 2378 | ); |
| 2379 | if (delivery === "deferred") { |
| 2380 | const turn = (yield* orchestrator.getThreadProjection(threadId)).providerTurns[0]!; |
| 2381 | yield* sink.write({ |
| 2382 | events: [ |
| 2383 | { |
| 2384 | id: EventId.make("turn-not-ready"), |
| 2385 | type: "provider-turn.updated", |
| 2386 | threadId, |
| 2387 | occurredAt: yield* DateTime.now, |
| 2388 | payload: { ...turn, status: "pending" }, |
| 2389 | }, |
| 2390 | ], |
| 2391 | }); |
| 2392 | } |
| 2393 | const commandId = CommandId.make("oversized-steer"); |
| 2394 | let command: Parameters<typeof orchestrator.dispatch>[0] = { |
| 2395 | type: "message.dispatch", |
| 2396 | commandId, |
| 2397 | threadId, |
| 2398 | messageId: MessageId.make("oversized"), |
| 2399 | text, |
| 2400 | attachments: [], |
| 2401 | dispatchMode: { type: "start_immediately" }, |
| 2402 | deliveryIntent: delivery === "deferred" ? "steer" : "auto", |
| 2403 | createdBy: "user", |
| 2404 | creationSource: "mobile", |
| 2405 | }; |
| 2406 | if (delivery === "promote-edited") { |
| 2407 | yield* orchestrator.dispatch({ |
| 2408 | ...command, |
| 2409 | commandId: CommandId.make("queue"), |
| 2410 | text: "Initially valid queued message", |
| 2411 | dispatchMode: { type: "queue_after_active" }, |
| 2412 | deliveryIntent: undefined, |
| 2413 | }); |
| 2414 | const queued = (yield* orchestrator.getThreadProjection(threadId)).runs.find( |
| 2415 | (run) => run.status === "queued", |
| 2416 | )!; |
| 2417 | yield* orchestrator.dispatch({ |
| 2418 | type: "queued-run.edit", |
| 2419 | commandId: CommandId.make("edit"), |
| 2420 | threadId, |
| 2421 | runId: queued.id, |
| 2422 | text, |
| 2423 | }); |
| 2424 | command = { |
| 2425 | type: "queued-message.promote-to-steer", |
| 2426 | commandId, |
| 2427 | threadId, |
| 2428 | queuedRunId: queued.id, |
| 2429 | targetRunId: first.runId, |
| 2430 | }; |
| 2431 | } |
| 2432 | const before = yield* orchestrator.getThreadProjection(threadId); |
| 2433 | const sequence = yield* orchestrator.getThreadEventSequence(threadId); |
| 2434 | const error = yield* orchestrator.dispatch(command).pipe(Effect.flip); |
| 2435 | assert.instanceOf(error, Orchestrator.OrchestratorCommandRejectedError); |
| 2436 | assert.equal( |
| 2437 | userFacingDispatchErrorMessage(error), |
| 2438 | "Message exceeds 120,000 characters after expanding quotes. Shorten it and try again.", |
| 2439 | ); |
| 2440 | assert.deepEqual(yield* outbox.listByCommandId(commandId), []); |
| 2441 | assert.deepEqual(yield* sink.readByCommandId({ commandId }).pipe(Stream.runCollect), []); |
| 2442 | yield* worker.drain(); |
| 2443 | assert.equal(yield* orchestrator.getThreadEventSequence(threadId), sequence); |
| 2444 | assert.deepEqual(yield* orchestrator.getThreadProjection(threadId), before); |
| 2445 | assert.deepEqual(steered, []); |
| 2446 | assert.deepEqual(interrupted, []); |
| 2447 | assert.lengthOf(started, 1); |
| 2448 | if (delivery === "auto") { |
| 2449 | yield* orchestrator.dispatch({ |
| 2450 | type: "message.dispatch", |
| 2451 | commandId: CommandId.make("large-context-steer"), |
| 2452 | threadId, |
| 2453 | messageId: MessageId.make("large-context"), |
| 2454 | text: largeContextText, |
| 2455 | context, |
| 2456 | attachments: [], |
| 2457 | dispatchMode: { type: "start_immediately" }, |
| 2458 | deliveryIntent: "auto", |
| 2459 | createdBy: "user", |
| 2460 | creationSource: "mobile", |
| 2461 | }); |
| 2462 | yield* worker.drain(); |
| 2463 | assert.lengthOf(steered, 1); |
| 2464 | } |
| 2465 | }).pipe(Effect.provide(layer)); |
| 2466 | }), |
| 2467 | ), |
| 2468 | ); |
| 2469 | |
| 2470 | it.effect("steers a promoted queued message with the selection it was queued with", () => |
| 2471 | Effect.gen(function* () { |
| 2472 | const { started, steered, layer, startFirstTurn } = yield* nextTurnSelectionHarness( |
| 2473 | "promote-queued-selection", |
| 2474 | ); |
| 2475 | yield* Effect.gen(function* () { |
| 2476 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 2477 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 2478 | const threadId = yield* startFirstTurn; |
| 2479 | yield* orchestrator.dispatch({ |
| 2480 | type: "message.dispatch", |
| 2481 | commandId: CommandId.make("queued"), |
| 2482 | threadId, |
| 2483 | messageId: MessageId.make("message:queued"), |
| 2484 | text: "queued", |
| 2485 | attachments: [], |
| 2486 | modelSelection: composerSelection, |
| 2487 | dispatchMode: { type: "queue_after_active" }, |
| 2488 | createdBy: "user", |
| 2489 | creationSource: "web", |
| 2490 | }); |
| 2491 | const queuedRun = (yield* orchestrator.getThreadProjection(threadId)).runs.find( |
| 2492 | (run) => run.status === "queued", |
| 2493 | )!; |
| 2494 | yield* orchestrator.dispatch({ |
| 2495 | type: "queued-message.promote-to-steer", |
| 2496 | commandId: CommandId.make("promote"), |
| 2497 | threadId, |
| 2498 | queuedRunId: queuedRun.id, |
| 2499 | targetRunId: started[0]!.runId, |
| 2500 | }); |
| 2501 | yield* worker.drain(); |
| 2502 | |
| 2503 | const projection = yield* orchestrator.getThreadProjection(threadId); |
| 2504 | assert.deepEqual(steered, ["queued"]); |
| 2505 | assert.deepEqual(projection.thread.modelSelection, composerSelection); |
| 2506 | }).pipe(Effect.provide(layer)); |
| 2507 | }), |
| 2508 | ); |
| 2509 | |
| 2510 | it.effect("steers a promoted scheduled delivery with the thread's current selection", () => |
| 2511 | Effect.scoped( |
| 2512 | Effect.gen(function* () { |
| 2513 | const { started, steered, layer, startFirstTurn } = yield* nextTurnSelectionHarness( |
| 2514 | "promote-scheduled-selection", |
| 2515 | ); |
| 2516 | yield* Effect.gen(function* () { |
| 2517 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 2518 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 2519 | const threadId = yield* startFirstTurn; |
| 2520 | // Bound scheduled tasks send like ScheduledTaskService: no model of their own. |
| 2521 | yield* orchestrator.dispatch({ |
| 2522 | type: "message.dispatch", |
| 2523 | commandId: CommandId.make("scheduled"), |
| 2524 | threadId, |
| 2525 | messageId: MessageId.make("message:scheduled"), |
| 2526 | scheduledTaskId: ScheduledTaskId.make("task:scheduled"), |
| 2527 | text: "scheduled", |
| 2528 | attachments: [], |
| 2529 | dispatchMode: { type: "queue_after_active" }, |
| 2530 | createdBy: "user", |
| 2531 | creationSource: "web", |
| 2532 | }); |
| 2533 | const queuedRun = (yield* orchestrator.getThreadProjection(threadId)).runs.find( |
| 2534 | (run) => run.status === "queued", |
| 2535 | )!; |
| 2536 | assert.deepEqual(queuedRun.modelSelection, runSelection); |
| 2537 | yield* orchestrator.dispatch({ |
| 2538 | type: "thread.model-selection.set", |
| 2539 | commandId: CommandId.make("select-composer"), |
| 2540 | threadId, |
| 2541 | modelSelection: composerSelection, |
| 2542 | }); |
| 2543 | yield* orchestrator.dispatch({ |
| 2544 | type: "queued-message.promote-to-steer", |
| 2545 | commandId: CommandId.make("promote"), |
| 2546 | threadId, |
| 2547 | queuedRunId: queuedRun.id, |
| 2548 | targetRunId: started[0]!.runId, |
| 2549 | }); |
| 2550 | yield* worker.drain(); |
| 2551 | |
| 2552 | const projection = yield* orchestrator.getThreadProjection(threadId); |
| 2553 | assert.deepEqual(steered, [ |
| 2554 | '[T3 Code: scheduled task "task:scheduled" sent this prompt, not the user typing in this chat.]\n\nscheduled', |
| 2555 | ]); |
| 2556 | assert.deepEqual(projection.thread.modelSelection, composerSelection); |
| 2557 | }).pipe(Effect.provide(layer)); |
| 2558 | }), |
| 2559 | ), |
| 2560 | ); |
| 2561 | |
| 2562 | const claudeSelection = { |
| 2563 | instanceId: ProviderInstanceId.make("claudeAgent"), |
| 2564 | model: "claude-test", |
| 2565 | }; |
| 2566 | const claudeAdapter: ProviderAdapter.ProviderAdapterV2["Service"] = { |
| 2567 | instanceId: claudeSelection.instanceId, |
| 2568 | driver: ProviderDriverKind.make("claudeAgent"), |
| 2569 | getCapabilities: () => Effect.succeed(CodexProviderCapabilitiesV2), |
| 2570 | planSelectionTransition: () => Effect.succeed({ type: "apply_on_next_turn" }), |
| 2571 | openSession: () => Effect.die("the Claude instance never starts a turn here"), |
| 2572 | }; |
| 2573 | |
| 2574 | // Queues a scheduled delivery behind the Codex turn, stops that turn (Stop holds the queue), then |
| 2575 | // switches the thread to Claude: a thread changes instance only while no run is active. |
| 2576 | const scheduledDeliveryAfterClaudeSwitch = (name: string, text: string) => |
| 2577 | Effect.gen(function* () { |
| 2578 | const harness = yield* nextTurnSelectionHarness( |
| 2579 | name, |
| 2580 | false, |
| 2581 | true, |
| 2582 | Effect.void, |
| 2583 | true, |
| 2584 | undefined, |
| 2585 | undefined, |
| 2586 | [claudeAdapter], |
| 2587 | ); |
| 2588 | const queueAndSwitch = Effect.gen(function* () { |
| 2589 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 2590 | const threadId = yield* harness.startFirstTurn; |
| 2591 | yield* orchestrator.dispatch({ |
| 2592 | type: "message.dispatch", |
| 2593 | commandId: CommandId.make("scheduled"), |
| 2594 | threadId, |
| 2595 | messageId: MessageId.make("message:scheduled"), |
| 2596 | scheduledTaskId: ScheduledTaskId.make("task:scheduled"), |
| 2597 | text, |
| 2598 | attachments: [], |
| 2599 | dispatchMode: { type: "queue_after_active" }, |
| 2600 | createdBy: "user", |
| 2601 | creationSource: "web", |
| 2602 | }); |
| 2603 | const queuedRun = (yield* orchestrator.getThreadProjection(threadId)).runs.find( |
| 2604 | (run) => run.status === "queued", |
| 2605 | )!; |
| 2606 | assert.equal(queuedRun.providerInstanceId, instanceId); |
| 2607 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 2608 | const first = harness.started[0]!; |
| 2609 | yield* orchestrator.dispatch({ |
| 2610 | type: "run.interrupt", |
| 2611 | commandId: CommandId.make("stop"), |
| 2612 | threadId, |
| 2613 | runId: first.runId, |
| 2614 | holdQueue: true, |
| 2615 | }); |
| 2616 | yield* worker.drain(); |
| 2617 | const stopped = (yield* orchestrator.getThreadProjection(threadId)).runs; |
| 2618 | assert.equal(stopped.find((run) => run.id === first.runId)?.status, "interrupted"); |
| 2619 | const held = stopped.find((run) => run.id === queuedRun.id); |
| 2620 | assert.equal(held?.status, "queued"); |
| 2621 | assert.isTrue(held?.queueHeld); |
| 2622 | yield* orchestrator.dispatch({ |
| 2623 | type: "thread.model-selection.set", |
| 2624 | commandId: CommandId.make("select-claude"), |
| 2625 | threadId, |
| 2626 | modelSelection: claudeSelection, |
| 2627 | }); |
| 2628 | return { threadId, queuedRun }; |
| 2629 | }); |
| 2630 | return { ...harness, queueAndSwitch }; |
| 2631 | }); |
| 2632 | |
| 2633 | it.effect("checks an edited scheduled /goal against the thread's current provider", () => |
| 2634 | Effect.scoped( |
| 2635 | Effect.gen(function* () { |
| 2636 | const { layer, queueAndSwitch } = yield* scheduledDeliveryAfterClaudeSwitch( |
| 2637 | "edit-scheduled-goal", |
| 2638 | "scheduled", |
| 2639 | ); |
| 2640 | yield* Effect.gen(function* () { |
| 2641 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 2642 | const { threadId, queuedRun } = yield* queueAndSwitch; |
| 2643 | const editError = yield* orchestrator |
| 2644 | .dispatch({ |
| 2645 | type: "queued-run.edit", |
| 2646 | commandId: CommandId.make("edit"), |
| 2647 | threadId, |
| 2648 | runId: queuedRun.id, |
| 2649 | text: "/goal pause", |
| 2650 | }) |
| 2651 | .pipe(Effect.flip); |
| 2652 | |
| 2653 | assert.match(String(editError.cause), /Claude goals cannot be paused or resumed/); |
| 2654 | assert.equal( |
| 2655 | (yield* orchestrator.getThreadProjection(threadId)).messages.find( |
| 2656 | (message) => message.id === queuedRun.userMessageId, |
| 2657 | )?.text, |
| 2658 | "scheduled", |
| 2659 | ); |
| 2660 | }).pipe(Effect.provide(layer)); |
| 2661 | }), |
| 2662 | ), |
| 2663 | ); |
| 2664 | |
| 2665 | it.effect("checks a queued scheduled /goal against the thread's current provider at start", () => |
| 2666 | Effect.scoped( |
| 2667 | Effect.gen(function* () { |
| 2668 | const { started, layer, queueAndSwitch } = yield* scheduledDeliveryAfterClaudeSwitch( |
| 2669 | "start-scheduled-goal", |
| 2670 | "/goal pause", |
| 2671 | ); |
| 2672 | yield* Effect.gen(function* () { |
| 2673 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 2674 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 2675 | const { threadId, queuedRun } = yield* queueAndSwitch; |
| 2676 | yield* orchestrator.dispatch({ |
| 2677 | type: "queue.resume", |
| 2678 | commandId: CommandId.make("resume"), |
| 2679 | threadId, |
| 2680 | }); |
| 2681 | yield* orchestrator.resumeQueuedRuns; |
| 2682 | yield* worker.drain(); |
| 2683 | |
| 2684 | const projection = yield* orchestrator.getThreadProjection(threadId); |
| 2685 | const failure = projection.turnItems.find( |
| 2686 | (item) => item.type === "error" && item.runId === queuedRun.id, |
| 2687 | ); |
| 2688 | assert.equal(started.length, 1); |
| 2689 | assert.equal(projection.runs.find((run) => run.id === queuedRun.id)?.status, "failed"); |
| 2690 | assert.equal( |
| 2691 | failure?.type === "error" ? failure.failure.code : null, |
| 2692 | "queued_start_failed", |
| 2693 | ); |
| 2694 | }).pipe(Effect.provide(layer)); |
| 2695 | }), |
| 2696 | ), |
| 2697 | ); |
| 2698 | |
| 2699 | it.effect("marks the model switch on a promoted message that restarts the turn", () => |
| 2700 | Effect.gen(function* () { |
| 2701 | const { started, layer, startFirstTurn } = yield* nextTurnSelectionHarness( |
| 2702 | "promote-restart-switch", |
| 2703 | true, |
| 2704 | false, |
| 2705 | ); |
| 2706 | const otherSelection = { ...runSelection, model: "other-model" }; |
| 2707 | yield* Effect.gen(function* () { |
| 2708 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 2709 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 2710 | const threadId = yield* startFirstTurn; |
| 2711 | yield* orchestrator.dispatch({ |
| 2712 | type: "message.dispatch", |
| 2713 | commandId: CommandId.make("queued"), |
| 2714 | threadId, |
| 2715 | messageId: MessageId.make("message:queued"), |
| 2716 | text: "queued", |
| 2717 | attachments: [], |
| 2718 | modelSelection: otherSelection, |
| 2719 | dispatchMode: { type: "queue_after_active" }, |
| 2720 | createdBy: "user", |
| 2721 | creationSource: "web", |
| 2722 | }); |
| 2723 | const queuedRun = (yield* orchestrator.getThreadProjection(threadId)).runs.find( |
| 2724 | (run) => run.status === "queued", |
| 2725 | )!; |
| 2726 | yield* orchestrator.dispatch({ |
| 2727 | type: "queued-message.promote-to-steer", |
| 2728 | commandId: CommandId.make("promote"), |
| 2729 | threadId, |
| 2730 | queuedRunId: queuedRun.id, |
| 2731 | targetRunId: started[0]!.runId, |
| 2732 | }); |
| 2733 | yield* worker.drain(); |
| 2734 | |
| 2735 | const promoted = (yield* orchestrator.getThreadProjection(threadId)).turnItems.find( |
| 2736 | (item) => item.type === "user_message" && item.messageId === "message:queued", |
| 2737 | ); |
| 2738 | assert.deepEqual(promoted?.type === "user_message" ? promoted.modelSwitch : undefined, { |
| 2739 | from: runSelection, |
| 2740 | to: otherSelection, |
| 2741 | }); |
| 2742 | }).pipe(Effect.provide(layer)); |
| 2743 | }), |
| 2744 | ); |
| 2745 | |
| 2746 | it.effect("keeps a queued message queued when promoted into a run that Stop reached", () => |
| 2747 | Effect.gen(function* () { |
| 2748 | const { started, steered, layer, startFirstTurn } = |
| 2749 | yield* nextTurnSelectionHarness("steering-after-stop"); |
| 2750 | yield* Effect.gen(function* () { |
| 2751 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 2752 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 2753 | const threadId = yield* startFirstTurn; |
| 2754 | const first = started[0]!; |
| 2755 | yield* orchestrator.dispatch({ |
| 2756 | type: "message.dispatch", |
| 2757 | commandId: CommandId.make("queued"), |
| 2758 | threadId, |
| 2759 | messageId: MessageId.make("message:queued"), |
| 2760 | text: "queued", |
| 2761 | attachments: [], |
| 2762 | modelSelection: runSelection, |
| 2763 | dispatchMode: { type: "queue_after_active" }, |
| 2764 | createdBy: "user", |
| 2765 | creationSource: "web", |
| 2766 | }); |
| 2767 | const queuedRun = (yield* orchestrator.getThreadProjection(threadId)).runs.find( |
| 2768 | (run) => run.status === "queued", |
| 2769 | )!; |
| 2770 | // The worker has not delivered the interrupt yet, so the run still reads as running. |
| 2771 | yield* orchestrator.dispatch({ |
| 2772 | type: "run.interrupt", |
| 2773 | commandId: CommandId.make("stop"), |
| 2774 | threadId, |
| 2775 | runId: first.runId, |
| 2776 | holdQueue: true, |
| 2777 | }); |
| 2778 | |
| 2779 | const promoteError = yield* orchestrator |
| 2780 | .dispatch({ |
| 2781 | type: "queued-message.promote-to-steer", |
| 2782 | commandId: CommandId.make("promote"), |
| 2783 | threadId, |
| 2784 | queuedRunId: queuedRun.id, |
| 2785 | targetRunId: first.runId, |
| 2786 | }) |
| 2787 | .pipe(Effect.flip); |
| 2788 | yield* worker.drain(); |
| 2789 | |
| 2790 | const projection = yield* orchestrator.getThreadProjection(threadId); |
| 2791 | assert.equal(promoteError._tag, "OrchestratorDispatchError"); |
| 2792 | assert.deepEqual(steered, []); |
| 2793 | const queued = projection.runs.find((run) => run.id === queuedRun.id); |
| 2794 | assert.equal(queued?.status, "queued"); |
| 2795 | assert.isTrue(queued?.queueHeld); |
| 2796 | }).pipe(Effect.provide(layer)); |
| 2797 | }), |
| 2798 | ); |
| 2799 | |
| 2800 | // regression: #32891 a restart that Stop already reached is lost |
| 2801 | it.effect.each([false, true])( |
| 2802 | "refuses a steer after Stop (provider turn ready: %s)", |
| 2803 | (providerTurnReady) => |
| 2804 | Effect.gen(function* () { |
| 2805 | const { started, steered, layer, startFirstTurn } = |
| 2806 | yield* nextTurnSelectionHarness("restart-after-stop"); |
| 2807 | yield* Effect.gen(function* () { |
| 2808 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 2809 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 2810 | const threadId = yield* startFirstTurn; |
| 2811 | const first = started[0]!; |
| 2812 | // The worker has not delivered the interrupt yet, so the run still reads as running. |
| 2813 | yield* orchestrator.dispatch({ |
| 2814 | type: "run.interrupt", |
| 2815 | commandId: CommandId.make("stop"), |
| 2816 | threadId, |
| 2817 | runId: first.runId, |
| 2818 | }); |
| 2819 | const turn = (yield* orchestrator.getThreadProjection(threadId)).providerTurns[0]!; |
| 2820 | if (!providerTurnReady) { |
| 2821 | yield* (yield* EventSink.EventSinkV2).write({ |
| 2822 | events: [ |
| 2823 | { |
| 2824 | id: EventId.make("turn-not-ready"), |
| 2825 | type: "provider-turn.updated", |
| 2826 | threadId, |
| 2827 | occurredAt: yield* DateTime.now, |
| 2828 | payload: { ...turn, status: "pending" }, |
| 2829 | }, |
| 2830 | ], |
| 2831 | }); |
| 2832 | } |
| 2833 | const steerError = yield* orchestrator |
| 2834 | .dispatch({ |
| 2835 | type: "message.dispatch", |
| 2836 | commandId: CommandId.make("steer"), |
| 2837 | threadId, |
| 2838 | messageId: MessageId.make("message:steer"), |
| 2839 | text: "steer", |
| 2840 | attachments: [], |
| 2841 | modelSelection: runSelection, |
| 2842 | dispatchMode: { type: "steer_active", targetRunId: first.runId }, |
| 2843 | createdBy: "user", |
| 2844 | creationSource: "web", |
| 2845 | }) |
| 2846 | .pipe(Effect.flip); |
| 2847 | assert.equal(steerError._tag, "OrchestratorDispatchError"); |
| 2848 | assert.include(String(steerError.cause), "is stopping and cannot be steered"); |
| 2849 | if (!providerTurnReady) { |
| 2850 | yield* (yield* EventSink.EventSinkV2).write({ |
| 2851 | events: [ |
| 2852 | { |
| 2853 | id: EventId.make("restore-ready-turn"), |
| 2854 | type: "provider-turn.updated", |
| 2855 | threadId, |
| 2856 | occurredAt: yield* DateTime.now, |
| 2857 | payload: turn, |
| 2858 | }, |
| 2859 | ], |
| 2860 | }); |
| 2861 | } |
| 2862 | yield* orchestrator.dispatch({ |
| 2863 | type: "message.dispatch", |
| 2864 | commandId: CommandId.make("restart"), |
| 2865 | threadId, |
| 2866 | messageId: MessageId.make("message:restart"), |
| 2867 | text: "restart", |
| 2868 | attachments: [], |
| 2869 | modelSelection: runSelection, |
| 2870 | dispatchMode: { type: "restart_active", targetRunId: first.runId }, |
| 2871 | createdBy: "user", |
| 2872 | creationSource: "web", |
| 2873 | }); |
| 2874 | yield* worker.drain(); |
| 2875 | |
| 2876 | assert.deepEqual(steered, []); |
| 2877 | assert.deepEqual( |
| 2878 | started.map((turn) => turn.message.text), |
| 2879 | ["first", "restart"], |
| 2880 | ); |
| 2881 | }).pipe(Effect.provide(layer)); |
| 2882 | }), |
| 2883 | ); |
| 2884 | |
| 2885 | // regression: #32891 a restart that arrives after the turn ended is lost |
| 2886 | it.effect.each([ |
| 2887 | { status: "completed", settled: true }, |
| 2888 | { status: "completed", settled: false }, |
| 2889 | { status: "interrupted", settled: true }, |
| 2890 | { status: "interrupted", settled: false }, |
| 2891 | { status: "cancelled", settled: false }, |
| 2892 | ] as const)( |
| 2893 | "starts a restart that missed the turn ($status, run settled: $settled) as a new turn", |
| 2894 | ({ status, settled: runSettled }) => |
| 2895 | Effect.gen(function* () { |
| 2896 | const { events, started, steered, layer, startFirstTurn } = yield* nextTurnSelectionHarness( |
| 2897 | `restart-after-${status}-${runSettled ? "settled" : "unsettled"}`, |
| 2898 | ); |
| 2899 | yield* Effect.gen(function* () { |
| 2900 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 2901 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 2902 | const threadId = yield* startFirstTurn; |
| 2903 | const first = started[0]!; |
| 2904 | const turn = (yield* orchestrator.getThreadProjection(threadId)).providerTurns[0]!; |
| 2905 | const turnEnded = yield* orchestrator.streamDomainEvents.pipe( |
| 2906 | Stream.filter( |
| 2907 | (event) => |
| 2908 | event.type === "provider-turn.updated" && |
| 2909 | event.payload.id === turn.id && |
| 2910 | event.payload.status === status, |
| 2911 | ), |
| 2912 | Stream.take(1), |
| 2913 | Stream.runDrain, |
| 2914 | Effect.forkScoped, |
| 2915 | ); |
| 2916 | yield* Queue.offer(events, { |
| 2917 | type: "provider_turn.updated", |
| 2918 | driver, |
| 2919 | providerTurn: { ...turn, status, completedAt: yield* DateTime.now }, |
| 2920 | }); |
| 2921 | yield* Fiber.join(turnEnded); |
| 2922 | const settleRun = Effect.gen(function* () { |
| 2923 | const settled = yield* orchestrator.streamDomainEvents.pipe( |
| 2924 | Stream.filter( |
| 2925 | (event) => |
| 2926 | event.type === "run.updated" && |
| 2927 | event.payload.id === first.runId && |
| 2928 | event.payload.status !== "running", |
| 2929 | ), |
| 2930 | Stream.take(1), |
| 2931 | Stream.runDrain, |
| 2932 | Effect.forkScoped, |
| 2933 | ); |
| 2934 | yield* Queue.offer(events, { |
| 2935 | type: "turn.terminal", |
| 2936 | driver, |
| 2937 | providerThreadId: turn.providerThreadId, |
| 2938 | providerTurnId: turn.id, |
| 2939 | runOrdinal: first.runOrdinal, |
| 2940 | status, |
| 2941 | failure: null, |
| 2942 | threadDisposition: "reusable", |
| 2943 | }); |
| 2944 | yield* Fiber.join(settled); |
| 2945 | yield* worker.drain(); |
| 2946 | }); |
| 2947 | if (runSettled) yield* settleRun; |
| 2948 | |
| 2949 | yield* orchestrator.dispatch({ |
| 2950 | type: "message.dispatch", |
| 2951 | commandId: CommandId.make("restart"), |
| 2952 | threadId, |
| 2953 | messageId: MessageId.make("message:restart"), |
| 2954 | text: "restart", |
| 2955 | attachments: [], |
| 2956 | modelSelection: runSelection, |
| 2957 | dispatchMode: { type: "restart_active", targetRunId: first.runId }, |
| 2958 | createdBy: "user", |
| 2959 | creationSource: "web", |
| 2960 | }); |
| 2961 | yield* worker.drain(); |
| 2962 | if (!runSettled) { |
| 2963 | const restartLeftQueue = yield* orchestrator.streamDomainEvents.pipe( |
| 2964 | Stream.filter( |
| 2965 | (event) => |
| 2966 | event.type === "run.updated" && |
| 2967 | event.payload.userMessageId === "message:restart" && |
| 2968 | event.payload.status !== "queued", |
| 2969 | ), |
| 2970 | Stream.take(1), |
| 2971 | Stream.runDrain, |
| 2972 | Effect.forkScoped, |
| 2973 | ); |
| 2974 | yield* settleRun; |
| 2975 | yield* Fiber.join(restartLeftQueue); |
| 2976 | yield* worker.drain(); |
| 2977 | } |
| 2978 | |
| 2979 | assert.deepEqual(steered, []); |
| 2980 | assert.deepEqual( |
| 2981 | started.map((turn) => turn.message.text), |
| 2982 | ["first", "restart"], |
| 2983 | ); |
| 2984 | }).pipe(Effect.provide(layer)); |
| 2985 | }), |
| 2986 | ); |
| 2987 | |
| 2988 | // regression: #32891 a restart into a failed provider turn is lost |
| 2989 | it.effect("queues a restart sent into a provider turn that failed before its run settled", () => |
| 2990 | Effect.gen(function* () { |
| 2991 | const { events, started, steered, layer, startFirstTurn } = yield* nextTurnSelectionHarness( |
| 2992 | "restart-after-failed-unsettled", |
| 2993 | ); |
| 2994 | yield* Effect.gen(function* () { |
| 2995 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 2996 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 2997 | const threadId = yield* startFirstTurn; |
| 2998 | const first = started[0]!; |
| 2999 | const turn = (yield* orchestrator.getThreadProjection(threadId)).providerTurns[0]!; |
| 3000 | const turnFailed = yield* orchestrator.streamDomainEvents.pipe( |
| 3001 | Stream.filter( |
| 3002 | (event) => |
| 3003 | event.type === "provider-turn.updated" && |
| 3004 | event.payload.id === turn.id && |
| 3005 | event.payload.status === "failed", |
| 3006 | ), |
| 3007 | Stream.take(1), |
| 3008 | Stream.runDrain, |
| 3009 | Effect.forkScoped, |
| 3010 | ); |
| 3011 | yield* Queue.offer(events, { |
| 3012 | type: "provider_turn.updated", |
| 3013 | driver, |
| 3014 | providerTurn: { ...turn, status: "failed", completedAt: yield* DateTime.now }, |
| 3015 | }); |
| 3016 | yield* Fiber.join(turnFailed); |
| 3017 | |
| 3018 | yield* orchestrator.dispatch({ |
| 3019 | type: "message.dispatch", |
| 3020 | commandId: CommandId.make("restart"), |
| 3021 | threadId, |
| 3022 | messageId: MessageId.make("message:restart"), |
| 3023 | text: "restart", |
| 3024 | attachments: [], |
| 3025 | modelSelection: runSelection, |
| 3026 | dispatchMode: { type: "restart_active", targetRunId: first.runId }, |
| 3027 | createdBy: "user", |
| 3028 | creationSource: "web", |
| 3029 | }); |
| 3030 | yield* worker.drain(); |
| 3031 | |
| 3032 | assert.deepEqual(steered, []); |
| 3033 | const projection = yield* orchestrator.getThreadProjection(threadId); |
| 3034 | assert.equal( |
| 3035 | projection.runs.find((run) => run.userMessageId === "message:restart")?.status, |
| 3036 | "queued", |
| 3037 | ); |
| 3038 | }).pipe(Effect.provide(layer)); |
| 3039 | }), |
| 3040 | ); |
| 3041 | |
| 3042 | it.effect("queues a plain send behind the stopped queue and releases its hold", () => |
| 3043 | Effect.gen(function* () { |
| 3044 | const interruptReached = yield* Deferred.make<void>(); |
| 3045 | const releaseInterrupt = yield* Deferred.make<void>(); |
| 3046 | const { started, steered, layer, startFirstTurn } = yield* nextTurnSelectionHarness( |
| 3047 | "plain-send-after-stop", |
| 3048 | false, |
| 3049 | true, |
| 3050 | Deferred.succeed(interruptReached, undefined).pipe( |
| 3051 | Effect.andThen(Deferred.await(releaseInterrupt)), |
| 3052 | ), |
| 3053 | ); |
| 3054 | yield* Effect.gen(function* () { |
| 3055 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 3056 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 3057 | const threadId = yield* startFirstTurn; |
| 3058 | const send = (id: string, delivery: "queue" | "auto") => |
| 3059 | orchestrator.dispatch({ |
| 3060 | type: "message.dispatch", |
| 3061 | commandId: CommandId.make(id), |
| 3062 | threadId, |
| 3063 | messageId: MessageId.make(`message:${id}`), |
| 3064 | text: id, |
| 3065 | attachments: [], |
| 3066 | modelSelection: runSelection, |
| 3067 | ...(delivery === "queue" |
| 3068 | ? { dispatchMode: { type: "queue_after_active" } } |
| 3069 | : { dispatchMode: { type: "start_immediately" }, deliveryIntent: "auto" }), |
| 3070 | createdBy: "user", |
| 3071 | creationSource: "web", |
| 3072 | }); |
| 3073 | yield* send("queued", "queue"); |
| 3074 | yield* orchestrator.dispatch({ |
| 3075 | type: "run.interrupt", |
| 3076 | commandId: CommandId.make("stop"), |
| 3077 | threadId, |
| 3078 | runId: started[0]!.runId, |
| 3079 | holdQueue: true, |
| 3080 | }); |
| 3081 | // The provider has not stopped yet, so a plain send still resolves to a steer. |
| 3082 | yield* send("sent", "auto"); |
| 3083 | // Once the stop settles, the run-update reactor promotes the queue on |
| 3084 | // its own fiber, so read the queue while the provider is still stopping. |
| 3085 | const draining = yield* worker.drain().pipe(Effect.forkScoped); |
| 3086 | yield* Deferred.await(interruptReached); |
| 3087 | |
| 3088 | const projection = yield* orchestrator.getThreadProjection(threadId); |
| 3089 | assert.equal(started.length, 1); |
| 3090 | const queued = projection.runs |
| 3091 | .filter((run) => run.status === "queued") |
| 3092 | .sort((left, right) => (left.queuePosition ?? 0) - (right.queuePosition ?? 0)); |
| 3093 | assert.deepEqual( |
| 3094 | queued.map( |
| 3095 | (run) => projection.messages.find((message) => message.id === run.userMessageId)?.text, |
| 3096 | ), |
| 3097 | ["queued", "sent"], |
| 3098 | ); |
| 3099 | // The user sent it, so the queue they stopped runs again once the provider stops. |
| 3100 | assert.isFalse(queued.some((run) => run.queueHeld === true)); |
| 3101 | yield* Deferred.succeed(releaseInterrupt, undefined); |
| 3102 | yield* Fiber.join(draining); |
| 3103 | assert.deepEqual(steered, []); |
| 3104 | }).pipe(Effect.provide(layer)); |
| 3105 | }), |
| 3106 | ); |
| 3107 | |
| 3108 | const goalHarness = Effect.fn("goalHarness")(function* (name: string, nativeGoals?: boolean) { |
| 3109 | const harness = yield* nextTurnSelectionHarness( |
| 3110 | name, |
| 3111 | false, |
| 3112 | true, |
| 3113 | Effect.void, |
| 3114 | true, |
| 3115 | nativeGoals, |
| 3116 | ); |
| 3117 | const send = (id: string, text: string, delivery: "queue" | "auto", freshContext = false) => |
| 3118 | Effect.gen(function* () { |
| 3119 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 3120 | yield* orchestrator.dispatch({ |
| 3121 | type: "message.dispatch", |
| 3122 | commandId: CommandId.make(id), |
| 3123 | threadId: ThreadId.make(`thread:${name}`), |
| 3124 | messageId: MessageId.make(`message:${id}`), |
| 3125 | text, |
| 3126 | attachments: [], |
| 3127 | modelSelection: runSelection, |
| 3128 | ...(freshContext ? { freshContext } : {}), |
| 3129 | ...(delivery === "queue" |
| 3130 | ? { dispatchMode: { type: "queue_after_active" } } |
| 3131 | : { dispatchMode: { type: "start_immediately" }, deliveryIntent: "auto" }), |
| 3132 | createdBy: "user", |
| 3133 | creationSource: "web", |
| 3134 | }); |
| 3135 | }); |
| 3136 | const read = (threadId: ThreadId) => |
| 3137 | Effect.gen(function* () { |
| 3138 | const projection = yield* (yield* Orchestrator.OrchestratorV2).getThreadProjection(threadId); |
| 3139 | const textOf = (run: (typeof projection.runs)[number]) => |
| 3140 | projection.messages.find((message) => message.id === run.userMessageId)?.text; |
| 3141 | return { |
| 3142 | queue: projection.runs |
| 3143 | .filter((run) => run.status === "queued") |
| 3144 | .toSorted((left, right) => (left.queuePosition ?? 0) - (right.queuePosition ?? 0)) |
| 3145 | .map(textOf), |
| 3146 | started: harness.started.map((turn) => turn.message.text), |
| 3147 | stopRequested: projection.turnItems.some( |
| 3148 | (item) => |
| 3149 | item.type === "run_interrupt_request" && item.runId === harness.started[0]?.runId, |
| 3150 | ), |
| 3151 | }; |
| 3152 | }); |
| 3153 | return { ...harness, send, read }; |
| 3154 | }); |
| 3155 | |
| 3156 | it.effect.each([undefined, false, true])( |
| 3157 | "goal dispatch stops the active turn only with native support: %s", |
| 3158 | (nativeGoals) => |
| 3159 | Effect.gen(function* () { |
| 3160 | const { steered, layer, startFirstTurn, send, read } = yield* goalHarness( |
| 3161 | "goal-steer", |
| 3162 | nativeGoals, |
| 3163 | ); |
| 3164 | yield* Effect.gen(function* () { |
| 3165 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 3166 | const threadId = yield* startFirstTurn; |
| 3167 | yield* send("queued", "queued", "queue"); |
| 3168 | const sending = send("goal", "/goal all tests pass", "auto"); |
| 3169 | if (nativeGoals === false) { |
| 3170 | yield* Effect.flip(sending); |
| 3171 | } else yield* sending; |
| 3172 | yield* worker.drain(); |
| 3173 | |
| 3174 | assert.deepEqual(steered, []); |
| 3175 | assert.deepEqual(yield* read(threadId), { |
| 3176 | queue: ["queued"], |
| 3177 | started: nativeGoals === false ? ["first"] : ["first", "/goal all tests pass"], |
| 3178 | stopRequested: nativeGoals !== false, |
| 3179 | }); |
| 3180 | }).pipe(Effect.provide(layer)); |
| 3181 | }), |
| 3182 | ); |
| 3183 | |
| 3184 | it.effect("starts a goal that names no model on the thread's model ahead of a queued pick", () => |
| 3185 | Effect.gen(function* () { |
| 3186 | const { started, layer, startFirstTurn, read } = yield* goalHarness("goal-ahead-of-pick"); |
| 3187 | yield* Effect.gen(function* () { |
| 3188 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 3189 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 3190 | const threadId = yield* startFirstTurn; |
| 3191 | for (const [id, text, modelSelection] of [ |
| 3192 | ["picked", "picked", composerSelection], |
| 3193 | ["goal", "/goal all tests pass", undefined], |
| 3194 | ] as const) { |
| 3195 | yield* orchestrator.dispatch({ |
| 3196 | type: "message.dispatch", |
| 3197 | commandId: CommandId.make(id), |
| 3198 | threadId, |
| 3199 | messageId: MessageId.make(`message:${id}`), |
| 3200 | text, |
| 3201 | attachments: [], |
| 3202 | ...(modelSelection === undefined ? {} : { modelSelection }), |
| 3203 | dispatchMode: |
| 3204 | id === "picked" ? { type: "queue_after_active" } : { type: "start_immediately" }, |
| 3205 | ...(id === "picked" ? {} : { deliveryIntent: "auto" as const }), |
| 3206 | createdBy: "user", |
| 3207 | creationSource: "web", |
| 3208 | }); |
| 3209 | } |
| 3210 | yield* worker.drain(); |
| 3211 | |
| 3212 | assert.deepEqual(yield* read(threadId), { |
| 3213 | queue: ["picked"], |
| 3214 | started: ["first", "/goal all tests pass"], |
| 3215 | stopRequested: true, |
| 3216 | }); |
| 3217 | assert.deepEqual(started[1]?.modelSelection, runSelection); |
| 3218 | }).pipe(Effect.provide(layer)); |
| 3219 | }), |
| 3220 | ); |
| 3221 | |
| 3222 | it.effect("restarts by stopping the running turn when the provider cannot interrupt-restart", () => |
| 3223 | Effect.gen(function* () { |
| 3224 | const { started, steered, layer, startFirstTurn, send, read } = yield* goalHarness( |
| 3225 | "restart-without-interrupt-restart", |
| 3226 | ); |
| 3227 | yield* Effect.gen(function* () { |
| 3228 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 3229 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 3230 | const threadId = yield* startFirstTurn; |
| 3231 | yield* send("queued", "queued", "queue"); |
| 3232 | yield* orchestrator.dispatch({ |
| 3233 | type: "message.dispatch", |
| 3234 | commandId: CommandId.make("restart"), |
| 3235 | threadId, |
| 3236 | messageId: MessageId.make("message:restart"), |
| 3237 | text: "do this instead", |
| 3238 | attachments: [], |
| 3239 | modelSelection: runSelection, |
| 3240 | dispatchMode: { type: "restart_active", targetRunId: started[0]!.runId }, |
| 3241 | createdBy: "agent", |
| 3242 | creationSource: "server", |
| 3243 | }); |
| 3244 | yield* worker.drain(); |
| 3245 | |
| 3246 | assert.deepEqual(steered, []); |
| 3247 | assert.deepEqual(yield* read(threadId), { |
| 3248 | queue: ["queued"], |
| 3249 | started: ["first", "do this instead"], |
| 3250 | stopRequested: true, |
| 3251 | }); |
| 3252 | }).pipe(Effect.provide(layer)); |
| 3253 | }), |
| 3254 | ); |
| 3255 | |
| 3256 | it.effect("starts a restart that stops the running turn ahead of an older held queue", () => |
| 3257 | Effect.gen(function* () { |
| 3258 | const { started, layer, startFirstTurn, send, read } = |
| 3259 | yield* goalHarness("restart-past-held-queue"); |
| 3260 | yield* Effect.gen(function* () { |
| 3261 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 3262 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 3263 | const threadId = yield* startFirstTurn; |
| 3264 | yield* send("queued", "queued", "queue"); |
| 3265 | yield* orchestrator.dispatch({ |
| 3266 | type: "thread.stop", |
| 3267 | commandId: CommandId.make("stop"), |
| 3268 | threadId, |
| 3269 | }); |
| 3270 | yield* worker.drain(); |
| 3271 | const secondRunning = yield* orchestrator.streamDomainEvents.pipe( |
| 3272 | Stream.filter( |
| 3273 | (event) => event.type === "provider-turn.updated" && event.payload.status === "running", |
| 3274 | ), |
| 3275 | Stream.take(1), |
| 3276 | Stream.runDrain, |
| 3277 | Effect.forkScoped, |
| 3278 | ); |
| 3279 | yield* orchestrator.dispatch({ |
| 3280 | type: "message.dispatch", |
| 3281 | commandId: CommandId.make("second"), |
| 3282 | threadId, |
| 3283 | messageId: MessageId.make("message:second"), |
| 3284 | text: "second", |
| 3285 | attachments: [], |
| 3286 | dispatchMode: { type: "start_immediately" }, |
| 3287 | createdBy: "agent", |
| 3288 | creationSource: "mcp", |
| 3289 | }); |
| 3290 | yield* worker.drain(); |
| 3291 | yield* Fiber.join(secondRunning); |
| 3292 | yield* orchestrator.dispatch({ |
| 3293 | type: "message.dispatch", |
| 3294 | commandId: CommandId.make("restart"), |
| 3295 | threadId, |
| 3296 | messageId: MessageId.make("message:restart"), |
| 3297 | text: "do this instead", |
| 3298 | attachments: [], |
| 3299 | dispatchMode: { type: "restart_active", targetRunId: started[1]!.runId }, |
| 3300 | createdBy: "agent", |
| 3301 | creationSource: "mcp", |
| 3302 | }); |
| 3303 | yield* worker.drain(); |
| 3304 | |
| 3305 | const { queue, started: startedTexts } = yield* read(threadId); |
| 3306 | assert.deepEqual( |
| 3307 | { queue, started: startedTexts }, |
| 3308 | { |
| 3309 | queue: ["queued"], |
| 3310 | started: [ |
| 3311 | "first", |
| 3312 | ...["second", "do this instead"].map( |
| 3313 | (text) => |
| 3314 | `[T3 Code: this message came through T3 Code's tools, not from the user typing in this chat.]\n\n${text}`, |
| 3315 | ), |
| 3316 | ], |
| 3317 | }, |
| 3318 | ); |
| 3319 | }).pipe(Effect.provide(layer)); |
| 3320 | }), |
| 3321 | ); |
| 3322 | |
| 3323 | it.effect("reports a restart that stops the running turn as restarted", () => |
| 3324 | Effect.gen(function* () { |
| 3325 | const { started, layer, startFirstTurn } = yield* goalHarness("restart-send-delivery"); |
| 3326 | yield* Effect.gen(function* () { |
| 3327 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 3328 | const threadManagement = yield* ThreadManagementService.ThreadManagementService; |
| 3329 | const threadId = yield* startFirstTurn; |
| 3330 | const input = { |
| 3331 | projectId: ProjectId.make("project:restart-send-delivery"), |
| 3332 | commandId: CommandId.make("restart"), |
| 3333 | threadId, |
| 3334 | messageId: MessageId.make("message:restart"), |
| 3335 | text: "do this instead", |
| 3336 | attachments: [], |
| 3337 | mode: "restart", |
| 3338 | createdBy: "agent", |
| 3339 | creationSource: "mcp", |
| 3340 | } as const; |
| 3341 | const sent = yield* threadManagement.sendToThread(input); |
| 3342 | // Stop holds the replacement before it starts; a replay must still report the restart. |
| 3343 | yield* orchestrator.dispatch({ |
| 3344 | type: "thread.stop", |
| 3345 | commandId: CommandId.make("stop-after-restart"), |
| 3346 | threadId, |
| 3347 | }); |
| 3348 | const replayed = yield* threadManagement.sendToThread(input); |
| 3349 | |
| 3350 | assert.equal(started.length, 1); |
| 3351 | assert.deepEqual([sent.delivery, replayed.delivery], ["restarted", "restarted"]); |
| 3352 | }).pipe( |
| 3353 | Effect.provide( |
| 3354 | ThreadManagementService.layer.pipe( |
| 3355 | Layer.provideMerge(layer), |
| 3356 | Layer.provide(ServerSettings.layerTest()), |
| 3357 | Layer.provide( |
| 3358 | CommandReceiptStore.layer.pipe(Layer.provide(SqlitePersistence.layerMemory)), |
| 3359 | ), |
| 3360 | ), |
| 3361 | ), |
| 3362 | ); |
| 3363 | }), |
| 3364 | ); |
| 3365 | |
| 3366 | it.effect("replays an accepted steer after its target turn completes", () => |
| 3367 | Effect.gen(function* () { |
| 3368 | const name = "steer-retry-after-completion"; |
| 3369 | const { events, started, steered, layer, startFirstTurn } = |
| 3370 | yield* nextTurnSelectionHarness(name); |
| 3371 | yield* Effect.gen(function* () { |
| 3372 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 3373 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 3374 | const threadManagement = yield* ThreadManagementService.ThreadManagementService; |
| 3375 | const threadId = yield* startFirstTurn; |
| 3376 | const input = { |
| 3377 | projectId: ProjectId.make(`project:${name}`), |
| 3378 | commandId: CommandId.make("steer"), |
| 3379 | threadId, |
| 3380 | messageId: MessageId.make("message:steer"), |
| 3381 | text: "Please report the test result", |
| 3382 | attachments: [], |
| 3383 | mode: "steer", |
| 3384 | createdBy: "agent", |
| 3385 | creationSource: "mcp", |
| 3386 | } as const; |
| 3387 | const accepted = yield* threadManagement.sendToThread(input); |
| 3388 | assert.equal(accepted.delivery, "steered"); |
| 3389 | yield* worker.drain(); |
| 3390 | |
| 3391 | const waiting = yield* orchestrator.streamDomainEvents.pipe( |
| 3392 | Stream.filter( |
| 3393 | (event) => |
| 3394 | event.type === "run.updated" && |
| 3395 | event.payload.id === accepted.run.id && |
| 3396 | event.payload.status === "waiting", |
| 3397 | ), |
| 3398 | Stream.take(1), |
| 3399 | Stream.runDrain, |
| 3400 | Effect.forkScoped({ startImmediately: true }), |
| 3401 | ); |
| 3402 | const turn = (yield* orchestrator.getThreadProjection(threadId)).providerTurns[0]!; |
| 3403 | yield* Queue.offer(events, { |
| 3404 | type: "provider_turn.updated", |
| 3405 | driver, |
| 3406 | providerTurn: { ...turn, status: "completed", completedAt: yield* DateTime.now }, |
| 3407 | }); |
| 3408 | yield* Queue.offer(events, { |
| 3409 | type: "turn.terminal", |
| 3410 | driver, |
| 3411 | providerThreadId: turn.providerThreadId, |
| 3412 | providerTurnId: turn.id, |
| 3413 | runOrdinal: accepted.run.ordinal, |
| 3414 | status: "completed", |
| 3415 | failure: null, |
| 3416 | threadDisposition: "reusable", |
| 3417 | }); |
| 3418 | yield* Fiber.join(waiting); |
| 3419 | yield* worker.drain(); |
| 3420 | |
| 3421 | const replayed = yield* threadManagement.sendToThread(input); |
| 3422 | assert.equal(replayed.dispatch.replayed, true); |
| 3423 | assert.equal(replayed.delivery, accepted.delivery); |
| 3424 | assert.equal(replayed.message.id, accepted.message.id); |
| 3425 | assert.equal(replayed.run.id, accepted.run.id); |
| 3426 | assert.equal(replayed.run.status, "completed"); |
| 3427 | |
| 3428 | const fresh = yield* threadManagement |
| 3429 | .sendToThread({ |
| 3430 | ...input, |
| 3431 | commandId: CommandId.make("fresh-steer"), |
| 3432 | messageId: MessageId.make("message:fresh-steer"), |
| 3433 | }) |
| 3434 | .pipe(Effect.flip); |
| 3435 | assert.equal(fresh._tag, "ThreadManagementNoSteerableRunError"); |
| 3436 | yield* worker.drain(); |
| 3437 | assert.deepEqual(steered, [ |
| 3438 | `[T3 Code: this message came through T3 Code's tools, not from the user typing in this chat.]\n\n${input.text}`, |
| 3439 | ]); |
| 3440 | assert.lengthOf(started, 1); |
| 3441 | }).pipe( |
| 3442 | Effect.provide( |
| 3443 | ThreadManagementService.layer.pipe( |
| 3444 | Layer.provideMerge(layer), |
| 3445 | Layer.provide(ServerSettings.layerTest()), |
| 3446 | Layer.provide( |
| 3447 | CommandReceiptStore.layer.pipe(Layer.provide(SqlitePersistence.layerMemory)), |
| 3448 | ), |
| 3449 | ), |
| 3450 | ), |
| 3451 | ); |
| 3452 | }), |
| 3453 | ); |
| 3454 | |
| 3455 | it.effect("queues a goal clear without stopping work when no goal drives it", () => |
| 3456 | Effect.gen(function* () { |
| 3457 | const { steered, layer, startFirstTurn, send, read } = yield* goalHarness("goal-clear"); |
| 3458 | yield* Effect.gen(function* () { |
| 3459 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 3460 | const threadId = yield* startFirstTurn; |
| 3461 | yield* send("queued", "queued", "queue"); |
| 3462 | yield* send("clear", "/goal clear", "auto"); |
| 3463 | yield* worker.drain(); |
| 3464 | |
| 3465 | assert.deepEqual(steered, []); |
| 3466 | assert.deepEqual(yield* read(threadId), { |
| 3467 | queue: ["queued", "/goal clear"], |
| 3468 | started: ["first"], |
| 3469 | stopRequested: false, |
| 3470 | }); |
| 3471 | }).pipe(Effect.provide(layer)); |
| 3472 | }), |
| 3473 | ); |
| 3474 | |
| 3475 | it.effect("promotes a queued goal by stopping the running turn", () => |
| 3476 | Effect.gen(function* () { |
| 3477 | const { started, steered, layer, startFirstTurn, send, read } = |
| 3478 | yield* goalHarness("goal-promote"); |
| 3479 | yield* Effect.gen(function* () { |
| 3480 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 3481 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 3482 | const threadId = yield* startFirstTurn; |
| 3483 | yield* send("queued", "queued", "queue"); |
| 3484 | yield* send("goal", "/goal ship it", "queue"); |
| 3485 | const goalRun = (yield* orchestrator.getThreadProjection(threadId)).runs.find( |
| 3486 | (run) => run.userMessageId === MessageId.make("message:goal"), |
| 3487 | )!; |
| 3488 | yield* orchestrator.dispatch({ |
| 3489 | type: "queued-message.promote-to-steer", |
| 3490 | commandId: CommandId.make("promote"), |
| 3491 | threadId, |
| 3492 | queuedRunId: goalRun.id, |
| 3493 | targetRunId: started[0]!.runId, |
| 3494 | }); |
| 3495 | yield* worker.drain(); |
| 3496 | |
| 3497 | assert.deepEqual(steered, []); |
| 3498 | assert.deepEqual(yield* read(threadId), { |
| 3499 | queue: ["queued"], |
| 3500 | started: ["first", "/goal ship it"], |
| 3501 | stopRequested: true, |
| 3502 | }); |
| 3503 | }).pipe(Effect.provide(layer)); |
| 3504 | }), |
| 3505 | ); |
| 3506 | |
| 3507 | // A fresh message skips compaction as its own turn, which steering a running turn would not. |
| 3508 | it.effect.each(["next", "/goal ship it", "fresh next"])( |
| 3509 | "promoting %s into a compaction stops it and sends it next", |
| 3510 | (promoted) => |
| 3511 | Effect.gen(function* () { |
| 3512 | const { events, layer, send, read, started, steered, startFirstTurn } = yield* goalHarness( |
| 3513 | `compaction-promote-${promoted.length}`, |
| 3514 | ); |
| 3515 | yield* Effect.gen(function* () { |
| 3516 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 3517 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 3518 | const threadId = yield* startFirstTurn; |
| 3519 | yield* send("compact", "/compact", "queue"); |
| 3520 | yield* send("queued", "queued", "queue"); |
| 3521 | yield* send("next", promoted, "queue", promoted.startsWith("fresh")); |
| 3522 | const settled = yield* orchestrator.streamDomainEvents.pipe( |
| 3523 | Stream.filter( |
| 3524 | (event) => |
| 3525 | event.type === "run.updated" && |
| 3526 | event.payload.id === started[0]!.runId && |
| 3527 | event.payload.status === "waiting", |
| 3528 | ), |
| 3529 | Stream.take(1), |
| 3530 | Stream.runDrain, |
| 3531 | Effect.forkScoped, |
| 3532 | ); |
| 3533 | const compacting = yield* orchestrator.streamDomainEvents.pipe( |
| 3534 | Stream.filter( |
| 3535 | (event) => |
| 3536 | event.type === "provider-turn.updated" && |
| 3537 | event.payload.status === "running" && |
| 3538 | event.payload.runAttemptId !== started[0]!.attemptId, |
| 3539 | ), |
| 3540 | Stream.take(1), |
| 3541 | Stream.runDrain, |
| 3542 | Effect.forkScoped, |
| 3543 | ); |
| 3544 | const turn = (yield* orchestrator.getThreadProjection(threadId)).providerTurns[0]!; |
| 3545 | yield* Queue.offer(events, { |
| 3546 | type: "provider_turn.updated", |
| 3547 | driver, |
| 3548 | providerTurn: { ...turn, status: "completed", completedAt: yield* DateTime.now }, |
| 3549 | }); |
| 3550 | yield* Queue.offer(events, { |
| 3551 | type: "turn.terminal", |
| 3552 | driver, |
| 3553 | providerThreadId: turn.providerThreadId, |
| 3554 | providerTurnId: turn.id, |
| 3555 | runOrdinal: started[0]!.runOrdinal, |
| 3556 | status: "completed", |
| 3557 | failure: null, |
| 3558 | threadDisposition: "reusable", |
| 3559 | }); |
| 3560 | yield* Fiber.join(settled); |
| 3561 | yield* worker.drain(); |
| 3562 | yield* orchestrator.resumeQueuedRuns; |
| 3563 | yield* worker.drain(); |
| 3564 | yield* Fiber.join(compacting); |
| 3565 | const compactRun = (yield* orchestrator.getThreadProjection(threadId)).runs.find( |
| 3566 | (run) => run.userMessageId === MessageId.make("message:compact"), |
| 3567 | )!; |
| 3568 | const nextRun = (yield* orchestrator.getThreadProjection(threadId)).runs.find( |
| 3569 | (run) => run.userMessageId === MessageId.make("message:next"), |
| 3570 | )!; |
| 3571 | yield* orchestrator.dispatch({ |
| 3572 | type: "queued-message.promote-to-steer", |
| 3573 | commandId: CommandId.make("promote"), |
| 3574 | threadId, |
| 3575 | queuedRunId: nextRun.id, |
| 3576 | targetRunId: compactRun.id, |
| 3577 | }); |
| 3578 | yield* worker.drain(); |
| 3579 | |
| 3580 | const projection = yield* orchestrator.getThreadProjection(threadId); |
| 3581 | assert.deepEqual(steered, []); |
| 3582 | assert.isTrue( |
| 3583 | projection.turnItems.some( |
| 3584 | (item) => item.type === "run_interrupt_request" && item.runId === compactRun.id, |
| 3585 | ), |
| 3586 | ); |
| 3587 | assert.deepEqual((yield* read(threadId)).queue, ["queued"]); |
| 3588 | assert.deepEqual( |
| 3589 | started.map((turn) => turn.message.text), |
| 3590 | ["first", "/compact", promoted], |
| 3591 | ); |
| 3592 | }).pipe(Effect.provide(layer)); |
| 3593 | }), |
| 3594 | ); |
| 3595 | |
| 3596 | it.effect("stops a turn that is still starting when a goal is sent into it", () => |
| 3597 | Effect.gen(function* () { |
| 3598 | const { cwd, layer, send, started } = yield* goalHarness("goal-during-start"); |
| 3599 | yield* Effect.gen(function* () { |
| 3600 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 3601 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 3602 | const threadId = ThreadId.make("thread:goal-during-start"); |
| 3603 | yield* orchestrator.dispatch({ |
| 3604 | type: "thread.create", |
| 3605 | commandId: CommandId.make("create"), |
| 3606 | threadId, |
| 3607 | projectId: ProjectId.make("project:goal-during-start"), |
| 3608 | title: "Goal during start", |
| 3609 | modelSelection: runSelection, |
| 3610 | runtimeMode: "full-access", |
| 3611 | interactionMode: "default", |
| 3612 | branch: null, |
| 3613 | worktreePath: cwd, |
| 3614 | createdBy: "user", |
| 3615 | creationSource: "web", |
| 3616 | }); |
| 3617 | // The stopped run's end starts the queue, which then needs one more drain. |
| 3618 | const goalStarting = yield* orchestrator.streamDomainEvents.pipe( |
| 3619 | Stream.filter( |
| 3620 | (event) => event.type === "run.updated" && event.payload.status === "starting", |
| 3621 | ), |
| 3622 | Stream.take(1), |
| 3623 | Stream.runDrain, |
| 3624 | Effect.forkScoped({ startImmediately: true }), |
| 3625 | ); |
| 3626 | yield* send("first", "first", "auto"); |
| 3627 | yield* send("goal", "/goal all tests pass", "auto"); |
| 3628 | yield* worker.drain(); |
| 3629 | yield* Fiber.join(goalStarting); |
| 3630 | yield* worker.drain(); |
| 3631 | |
| 3632 | const projection = yield* orchestrator.getThreadProjection(threadId); |
| 3633 | const statusOf = (text: string) => |
| 3634 | projection.runs.find( |
| 3635 | (run) => |
| 3636 | projection.messages.find((message) => message.id === run.userMessageId)?.text === text, |
| 3637 | )?.status; |
| 3638 | assert.equal(statusOf("first"), "interrupted"); |
| 3639 | assert.deepEqual( |
| 3640 | started.map((turn) => turn.message.text), |
| 3641 | ["/goal all tests pass"], |
| 3642 | ); |
| 3643 | }).pipe(Effect.provide(layer)); |
| 3644 | }), |
| 3645 | ); |
| 3646 | |
| 3647 | it.effect("stops the running turn when a goal clear ends the goal driving it", () => |
| 3648 | Effect.gen(function* () { |
| 3649 | const { events, layer, startFirstTurn, send, read, steered } = |
| 3650 | yield* goalHarness("goal-clear-active"); |
| 3651 | yield* Effect.gen(function* () { |
| 3652 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 3653 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 3654 | const threadId = yield* startFirstTurn; |
| 3655 | const providerThread = (yield* orchestrator.getThreadProjection(threadId)) |
| 3656 | .providerThreads[0]!; |
| 3657 | const goalSeen = yield* orchestrator.streamDomainEvents.pipe( |
| 3658 | Stream.filter( |
| 3659 | (event) => |
| 3660 | event.type === "provider-thread.updated" && event.payload.goal?.status === "active", |
| 3661 | ), |
| 3662 | Stream.take(1), |
| 3663 | Stream.runDrain, |
| 3664 | Effect.forkScoped({ startImmediately: true }), |
| 3665 | ); |
| 3666 | yield* Queue.offer(events, { |
| 3667 | type: "provider_thread.updated", |
| 3668 | driver, |
| 3669 | providerThread: { |
| 3670 | ...providerThread, |
| 3671 | goal: { objective: "all tests pass", status: "active" }, |
| 3672 | }, |
| 3673 | }); |
| 3674 | yield* Fiber.join(goalSeen); |
| 3675 | yield* send("clear", "/goal clear", "auto"); |
| 3676 | yield* worker.drain(); |
| 3677 | |
| 3678 | assert.deepEqual(steered, []); |
| 3679 | assert.deepEqual(yield* read(threadId), { |
| 3680 | queue: [], |
| 3681 | started: ["first", "/goal clear"], |
| 3682 | stopRequested: true, |
| 3683 | }); |
| 3684 | }).pipe(Effect.provide(layer)); |
| 3685 | }), |
| 3686 | ); |
| 3687 | |
| 3688 | it.effect("a side chat that opens with /goal gets its context on the next question", () => |
| 3689 | Effect.gen(function* () { |
| 3690 | const { events, layer, started, startFirstTurn } = yield* goalHarness("side-chat-goal"); |
| 3691 | yield* Effect.gen(function* () { |
| 3692 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 3693 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 3694 | const parentId = yield* startFirstTurn; |
| 3695 | const sideChatId = ThreadId.make("thread:side-chat-goal-child"); |
| 3696 | yield* orchestrator.dispatch({ |
| 3697 | type: "thread.fork", |
| 3698 | commandId: CommandId.make("side-chat"), |
| 3699 | sourceThreadId: parentId, |
| 3700 | targetThreadId: sideChatId, |
| 3701 | sourcePoint: { type: "latest_stable" }, |
| 3702 | sideChat: true, |
| 3703 | createdBy: "user", |
| 3704 | creationSource: "web", |
| 3705 | }); |
| 3706 | const ask = (id: string, text: string) => |
| 3707 | orchestrator.dispatch({ |
| 3708 | type: "message.dispatch", |
| 3709 | commandId: CommandId.make(id), |
| 3710 | threadId: sideChatId, |
| 3711 | messageId: MessageId.make(`message:${id}`), |
| 3712 | text, |
| 3713 | attachments: [], |
| 3714 | modelSelection: runSelection, |
| 3715 | dispatchMode: { type: "start_immediately" }, |
| 3716 | createdBy: "user", |
| 3717 | creationSource: "web", |
| 3718 | }); |
| 3719 | const sideChatEvent = (matches: (event: OrchestrationV2DomainEvent) => boolean) => |
| 3720 | orchestrator.streamDomainEvents.pipe( |
| 3721 | Stream.filter((event) => event.threadId === sideChatId && matches(event)), |
| 3722 | Stream.take(1), |
| 3723 | Stream.runDrain, |
| 3724 | Effect.forkScoped({ startImmediately: true }), |
| 3725 | ); |
| 3726 | const running = yield* sideChatEvent( |
| 3727 | (event) => event.type === "provider-turn.updated" && event.payload.status === "running", |
| 3728 | ); |
| 3729 | const settled = yield* sideChatEvent( |
| 3730 | (event) => event.type === "run.updated" && event.payload.status === "waiting", |
| 3731 | ); |
| 3732 | yield* ask("goal", "/goal clear"); |
| 3733 | yield* worker.drain(); |
| 3734 | yield* Fiber.join(running); |
| 3735 | const turn = (yield* orchestrator.getThreadProjection(sideChatId)).providerTurns[0]!; |
| 3736 | yield* Queue.offer(events, { |
| 3737 | type: "provider_turn.updated", |
| 3738 | driver, |
| 3739 | providerTurn: { ...turn, status: "completed", completedAt: yield* DateTime.now }, |
| 3740 | }); |
| 3741 | yield* Queue.offer(events, { |
| 3742 | type: "turn.terminal", |
| 3743 | driver, |
| 3744 | providerThreadId: turn.providerThreadId, |
| 3745 | providerTurnId: turn.id, |
| 3746 | runOrdinal: started[1]!.runOrdinal, |
| 3747 | status: "completed", |
| 3748 | failure: null, |
| 3749 | threadDisposition: "reusable", |
| 3750 | }); |
| 3751 | yield* Fiber.join(settled); |
| 3752 | yield* worker.drain(); |
| 3753 | yield* ask("question", "What is the main thread doing?"); |
| 3754 | yield* worker.drain(); |
| 3755 | |
| 3756 | const [first, goal, question] = started.map((turn) => turn.message.text); |
| 3757 | assert.deepEqual([first, goal], ["first", "/goal clear"]); |
| 3758 | assert.include(question, "You are in a side chat beside the main thread."); |
| 3759 | assert.include(question, "- User: first"); |
| 3760 | assert.isTrue(question?.endsWith("What is the main thread doing?")); |
| 3761 | }).pipe(Effect.provide(layer)); |
| 3762 | }), |
| 3763 | ); |