|
| 1 | +import { assert, it } from "@effect/vitest"; |
| 2 | +import { |
| 3 | + CommandId, |
| 4 | + EventId, |
| 5 | + MessageId, |
| 6 | + NodeId, |
| 7 | + ProjectId, |
| 8 | + ProviderDriverKind, |
| 9 | + ProviderInstanceId, |
| 10 | + ProviderThreadId, |
| 11 | + ProviderTurnId, |
| 12 | + RunAttemptId, |
| 13 | + RunId, |
| 14 | + ThreadId, |
| 15 | + TurnItemId, |
| 16 | + type OrchestrationV2DomainEvent, |
| 17 | +} from "@t3tools/contracts"; |
| 18 | +import * as DateTime from "effect/DateTime"; |
| 19 | +import * as Effect from "effect/Effect"; |
| 20 | +import * as Fiber from "effect/Fiber"; |
| 21 | +import * as Queue from "effect/Queue"; |
| 22 | +import * as Stream from "effect/Stream"; |
| 23 | +import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts"; |
| 24 | +import * as EffectWorker from "./EffectWorker.ts"; |
| 25 | +import * as EventSink from "./EventSink.ts"; |
| 26 | +import * as Orchestrator from "./Orchestrator.ts"; |
| 27 | +import type { |
| 28 | + ProviderAdapterV2Event, |
| 29 | + ProviderAdapterV2InterruptInput, |
| 30 | + ProviderAdapterV2Shape, |
| 31 | + ProviderAdapterV2TurnInput, |
| 32 | +} from "./ProviderAdapter.ts"; |
| 33 | +import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts"; |
| 34 | +import { makeOrchestratorV2ReplayLayerWithRegistry } from "./testkit/ProviderReplayHarness.ts"; |
| 35 | +import { checkpointWorkspace } from "./testkit/ReplayFixtureWorkspace.ts"; |
| 36 | + |
| 37 | +const driver = ProviderDriverKind.make("codex"); |
| 38 | +const instanceId = ProviderInstanceId.make("codex"); |
| 39 | +const modelSelection = { instanceId, model: "test-model" }; |
| 40 | + |
| 41 | +// A Codex turn leaves a dev server running, then the thread moves to another |
| 42 | +// provider thread (a provider switch). Stop on the newer, settled run must |
| 43 | +// still reach the Codex session that owns the dev server. |
| 44 | +it.effect("Stop reaches background work an earlier provider thread still runs", () => |
| 45 | + Effect.scoped( |
| 46 | + Effect.gen(function* () { |
| 47 | + const cwd = yield* checkpointWorkspace("background-work-stop"); |
| 48 | + const events = yield* Queue.unbounded<ProviderAdapterV2Event>(); |
| 49 | + const started: ProviderAdapterV2TurnInput[] = []; |
| 50 | + const interrupts: ProviderAdapterV2InterruptInput[] = []; |
| 51 | + const adapter: ProviderAdapterV2Shape = { |
| 52 | + instanceId, |
| 53 | + driver, |
| 54 | + getCapabilities: () => Effect.succeed(CodexProviderCapabilitiesV2), |
| 55 | + planSelectionTransition: () => Effect.succeed({ type: "apply_on_next_turn" }), |
| 56 | + openSession: (input) => |
| 57 | + Effect.gen(function* () { |
| 58 | + const now = yield* DateTime.now; |
| 59 | + return { |
| 60 | + instanceId, |
| 61 | + driver, |
| 62 | + providerSessionId: input.providerSessionId, |
| 63 | + providerSession: { |
| 64 | + id: input.providerSessionId, |
| 65 | + driver, |
| 66 | + providerInstanceId: instanceId, |
| 67 | + status: "ready", |
| 68 | + cwd, |
| 69 | + model: modelSelection.model, |
| 70 | + capabilities: CodexProviderCapabilitiesV2, |
| 71 | + createdAt: now, |
| 72 | + updatedAt: now, |
| 73 | + lastError: null, |
| 74 | + }, |
| 75 | + events: Stream.fromQueue(events), |
| 76 | + ensureThread: ({ threadId }) => |
| 77 | + Effect.succeed({ |
| 78 | + id: ProviderThreadId.make(`provider-thread:codex:${threadId}`), |
| 79 | + driver, |
| 80 | + providerInstanceId: instanceId, |
| 81 | + providerSessionId: input.providerSessionId, |
| 82 | + appThreadId: threadId, |
| 83 | + ownerNodeId: null, |
| 84 | + nativeThreadRef: { driver, nativeId: "native-thread", strength: "strong" }, |
| 85 | + nativeConversationHeadRef: null, |
| 86 | + status: "idle", |
| 87 | + firstRunOrdinal: null, |
| 88 | + lastRunOrdinal: null, |
| 89 | + handoffIds: [], |
| 90 | + forkedFrom: null, |
| 91 | + createdAt: now, |
| 92 | + updatedAt: now, |
| 93 | + }), |
| 94 | + resumeThread: ({ providerThread }) => Effect.succeed(providerThread), |
| 95 | + startTurn: (turn) => |
| 96 | + Effect.gen(function* () { |
| 97 | + started.push(turn); |
| 98 | + yield* Queue.offer(events, { |
| 99 | + type: "provider_turn.updated", |
| 100 | + driver, |
| 101 | + providerTurn: { |
| 102 | + id: ProviderTurnId.make(`provider-turn:${turn.attemptId}`), |
| 103 | + providerThreadId: turn.providerThread.id, |
| 104 | + nodeId: turn.rootNodeId, |
| 105 | + runAttemptId: turn.attemptId, |
| 106 | + nativeTurnRef: { |
| 107 | + driver, |
| 108 | + nativeId: `native:${turn.attemptId}`, |
| 109 | + strength: "strong", |
| 110 | + }, |
| 111 | + ordinal: turn.providerTurnOrdinal, |
| 112 | + status: "running", |
| 113 | + startedAt: now, |
| 114 | + completedAt: null, |
| 115 | + }, |
| 116 | + }); |
| 117 | + }), |
| 118 | + steerTurn: () => Effect.die("unused"), |
| 119 | + interruptTurn: (interrupt) => |
| 120 | + Effect.sync(() => { |
| 121 | + interrupts.push(interrupt); |
| 122 | + }), |
| 123 | + respondToRuntimeRequest: () => Effect.die("unused"), |
| 124 | + readThreadSnapshot: () => Effect.die("unused"), |
| 125 | + rollbackThread: () => Effect.die("unused"), |
| 126 | + forkThread: () => Effect.die("unused"), |
| 127 | + }; |
| 128 | + }), |
| 129 | + }; |
| 130 | + yield* Effect.gen(function* () { |
| 131 | + const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 132 | + const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 133 | + const sink = yield* EventSink.EventSinkV2; |
| 134 | + const threadId = ThreadId.make("thread:background-work-stop"); |
| 135 | + const watch = (predicate: (event: OrchestrationV2DomainEvent) => boolean) => |
| 136 | + orchestrator.streamDomainEvents.pipe( |
| 137 | + Stream.filter(predicate), |
| 138 | + Stream.take(1), |
| 139 | + Stream.runDrain, |
| 140 | + Effect.forkScoped, |
| 141 | + ); |
| 142 | + yield* orchestrator.dispatch({ |
| 143 | + type: "thread.create", |
| 144 | + commandId: CommandId.make("create"), |
| 145 | + threadId, |
| 146 | + projectId: ProjectId.make("project:background-work-stop"), |
| 147 | + title: "Background work stop", |
| 148 | + modelSelection, |
| 149 | + runtimeMode: "full-access", |
| 150 | + interactionMode: "default", |
| 151 | + branch: null, |
| 152 | + worktreePath: cwd, |
| 153 | + createdBy: "user", |
| 154 | + creationSource: "web", |
| 155 | + }); |
| 156 | + const running = yield* watch( |
| 157 | + (event) => event.type === "provider-turn.updated" && event.payload.status === "running", |
| 158 | + ); |
| 159 | + yield* orchestrator.dispatch({ |
| 160 | + type: "message.dispatch", |
| 161 | + commandId: CommandId.make("start-dev-server"), |
| 162 | + threadId, |
| 163 | + messageId: MessageId.make("message:start-dev-server"), |
| 164 | + text: "Start the dev server", |
| 165 | + attachments: [], |
| 166 | + dispatchMode: { type: "start_immediately" }, |
| 167 | + createdBy: "user", |
| 168 | + creationSource: "web", |
| 169 | + }); |
| 170 | + yield* worker.drain(); |
| 171 | + yield* Fiber.join(running); |
| 172 | + const first = started[0]!; |
| 173 | + const codexTurn = (yield* orchestrator.getThreadProjection(threadId)).providerTurns[0]!; |
| 174 | + const devServerId = TurnItemId.make("turn-item:dev-server"); |
| 175 | + const now = yield* DateTime.now; |
| 176 | + yield* sink.write({ |
| 177 | + events: [ |
| 178 | + { |
| 179 | + id: EventId.make("dev-server"), |
| 180 | + type: "turn-item.updated", |
| 181 | + threadId, |
| 182 | + runId: first.runId, |
| 183 | + occurredAt: now, |
| 184 | + payload: { |
| 185 | + id: devServerId, |
| 186 | + threadId, |
| 187 | + runId: first.runId, |
| 188 | + nodeId: first.rootNodeId, |
| 189 | + providerThreadId: codexTurn.providerThreadId, |
| 190 | + providerTurnId: codexTurn.id, |
| 191 | + nativeItemRef: null, |
| 192 | + parentItemId: null, |
| 193 | + ordinal: 100, |
| 194 | + status: "running", |
| 195 | + title: null, |
| 196 | + startedAt: now, |
| 197 | + completedAt: null, |
| 198 | + updatedAt: now, |
| 199 | + type: "command_execution", |
| 200 | + input: "vp run dev --share", |
| 201 | + }, |
| 202 | + }, |
| 203 | + ], |
| 204 | + }); |
| 205 | + const settled = yield* watch( |
| 206 | + (event) => |
| 207 | + event.type === "run.updated" && |
| 208 | + event.payload.id === first.runId && |
| 209 | + event.payload.status === "waiting", |
| 210 | + ); |
| 211 | + yield* Queue.offer(events, { |
| 212 | + type: "provider_turn.updated", |
| 213 | + driver, |
| 214 | + providerTurn: { ...codexTurn, status: "completed", completedAt: now }, |
| 215 | + }); |
| 216 | + yield* Queue.offer(events, { |
| 217 | + type: "turn.terminal", |
| 218 | + driver, |
| 219 | + providerThreadId: codexTurn.providerThreadId, |
| 220 | + providerTurnId: codexTurn.id, |
| 221 | + runOrdinal: first.runOrdinal, |
| 222 | + status: "completed", |
| 223 | + failure: null, |
| 224 | + threadDisposition: "reusable", |
| 225 | + }); |
| 226 | + yield* Fiber.join(settled); |
| 227 | + yield* worker.drain(); |
| 228 | + |
| 229 | + // The thread moved on: a later run settled on another provider thread |
| 230 | + // whose session is gone. |
| 231 | + const otherProviderThreadId = ProviderThreadId.make("provider-thread:other"); |
| 232 | + const runId = RunId.make("run:after-switch"); |
| 233 | + const attemptId = RunAttemptId.make("attempt:after-switch"); |
| 234 | + const nodeId = NodeId.make("node:after-switch"); |
| 235 | + yield* sink.write({ |
| 236 | + events: [ |
| 237 | + { |
| 238 | + id: EventId.make("switch:provider-thread"), |
| 239 | + type: "provider-thread.updated", |
| 240 | + threadId, |
| 241 | + occurredAt: now, |
| 242 | + payload: { |
| 243 | + id: otherProviderThreadId, |
| 244 | + driver, |
| 245 | + providerInstanceId: instanceId, |
| 246 | + providerSessionId: null, |
| 247 | + appThreadId: threadId, |
| 248 | + ownerNodeId: null, |
| 249 | + nativeThreadRef: null, |
| 250 | + nativeConversationHeadRef: null, |
| 251 | + status: "idle", |
| 252 | + firstRunOrdinal: 2, |
| 253 | + lastRunOrdinal: 2, |
| 254 | + handoffIds: [], |
| 255 | + forkedFrom: null, |
| 256 | + createdAt: now, |
| 257 | + updatedAt: now, |
| 258 | + }, |
| 259 | + }, |
| 260 | + { |
| 261 | + id: EventId.make("switch:run"), |
| 262 | + type: "run.created", |
| 263 | + threadId, |
| 264 | + occurredAt: now, |
| 265 | + payload: { |
| 266 | + id: runId, |
| 267 | + threadId, |
| 268 | + ordinal: 2, |
| 269 | + providerInstanceId: instanceId, |
| 270 | + modelSelection, |
| 271 | + providerThreadId: otherProviderThreadId, |
| 272 | + userMessageId: MessageId.make("message:after-switch"), |
| 273 | + rootNodeId: nodeId, |
| 274 | + activeAttemptId: attemptId, |
| 275 | + status: "completed", |
| 276 | + requestedAt: now, |
| 277 | + startedAt: now, |
| 278 | + completedAt: now, |
| 279 | + checkpointId: null, |
| 280 | + contextHandoffId: null, |
| 281 | + }, |
| 282 | + }, |
| 283 | + { |
| 284 | + id: EventId.make("switch:node"), |
| 285 | + type: "node.updated", |
| 286 | + threadId, |
| 287 | + runId, |
| 288 | + occurredAt: now, |
| 289 | + payload: { |
| 290 | + id: nodeId, |
| 291 | + threadId, |
| 292 | + runId, |
| 293 | + parentNodeId: null, |
| 294 | + rootNodeId: nodeId, |
| 295 | + kind: "root_turn", |
| 296 | + status: "completed", |
| 297 | + countsForRun: true, |
| 298 | + providerThreadId: otherProviderThreadId, |
| 299 | + providerTurnId: null, |
| 300 | + nativeItemRef: null, |
| 301 | + runtimeRequestId: null, |
| 302 | + checkpointScopeId: null, |
| 303 | + startedAt: now, |
| 304 | + completedAt: now, |
| 305 | + }, |
| 306 | + }, |
| 307 | + { |
| 308 | + id: EventId.make("switch:attempt"), |
| 309 | + type: "run-attempt.created", |
| 310 | + threadId, |
| 311 | + occurredAt: now, |
| 312 | + payload: { |
| 313 | + id: attemptId, |
| 314 | + runId, |
| 315 | + attemptOrdinal: 1, |
| 316 | + rootNodeId: nodeId, |
| 317 | + providerInstanceId: instanceId, |
| 318 | + providerThreadId: otherProviderThreadId, |
| 319 | + providerTurnId: ProviderTurnId.make("provider-turn:after-switch"), |
| 320 | + reason: "initial", |
| 321 | + status: "completed", |
| 322 | + startedAt: now, |
| 323 | + completedAt: now, |
| 324 | + }, |
| 325 | + }, |
| 326 | + { |
| 327 | + id: EventId.make("switch:turn"), |
| 328 | + type: "provider-turn.updated", |
| 329 | + threadId, |
| 330 | + occurredAt: now, |
| 331 | + payload: { |
| 332 | + id: ProviderTurnId.make("provider-turn:after-switch"), |
| 333 | + providerThreadId: otherProviderThreadId, |
| 334 | + nodeId, |
| 335 | + runAttemptId: attemptId, |
| 336 | + nativeTurnRef: null, |
| 337 | + ordinal: 1, |
| 338 | + status: "completed", |
| 339 | + startedAt: now, |
| 340 | + completedAt: now, |
| 341 | + }, |
| 342 | + }, |
| 343 | + ], |
| 344 | + }); |
| 345 | + |
| 346 | + yield* orchestrator.dispatch({ |
| 347 | + type: "run.interrupt", |
| 348 | + commandId: CommandId.make("stop-background-work"), |
| 349 | + threadId, |
| 350 | + runId, |
| 351 | + }); |
| 352 | + yield* worker.drain(); |
| 353 | + |
| 354 | + assert.deepEqual( |
| 355 | + interrupts.map((interrupt) => [interrupt.providerThread.id, interrupt.providerTurnId]), |
| 356 | + [[codexTurn.providerThreadId, codexTurn.id]], |
| 357 | + ); |
| 358 | + const after = yield* orchestrator.getThreadProjection(threadId); |
| 359 | + assert.equal( |
| 360 | + after.turnItems.find((item) => item.id === devServerId)?.status, |
| 361 | + "interrupted", |
| 362 | + ); |
| 363 | + }).pipe( |
| 364 | + Effect.provide( |
| 365 | + makeOrchestratorV2ReplayLayerWithRegistry( |
| 366 | + { name: "background-work-stop" }, |
| 367 | + ProviderAdapterRegistry.makeSingleLayer(adapter), |
| 368 | + { runEffectWorker: false }, |
| 369 | + ), |
| 370 | + ), |
| 371 | + ); |
| 372 | + }), |
| 373 | + ), |
| 374 | +); |
0 commit comments