The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | CheckpointId, |
| 3 | CheckpointRef, |
| 4 | CheckpointScopeId, |
| 5 | CommandId, |
| 6 | EnvironmentId, |
| 7 | MessageId, |
| 8 | NodeId, |
| 9 | ORCHESTRATION_V2_WS_METHODS, |
| 10 | ProjectId, |
| 11 | ProviderInstanceId, |
| 12 | ProviderDriverKind, |
| 13 | RunId, |
| 14 | ThreadId, |
| 15 | TurnItemId, |
| 16 | WS_METHODS, |
| 17 | type OrchestrationV2Command, |
| 18 | type OrchestrationV2ThreadLaunchInput, |
| 19 | type OrchestrationV2ThreadProjection, |
| 20 | type ProjectMutation, |
| 21 | } from "@t3tools/contracts"; |
| 22 | import { describe, expect, it } from "@effect/vitest"; |
| 23 | import * as Cause from "effect/Cause"; |
| 24 | import * as SecureRandom from "effect/crypto/SecureRandom"; |
| 25 | import * as Deferred from "effect/Deferred"; |
| 26 | import * as Effect from "effect/Effect"; |
| 27 | import * as Exit from "effect/Exit"; |
| 28 | import * as Fiber from "effect/Fiber"; |
| 29 | import * as Layer from "effect/Layer"; |
| 30 | import * as Option from "effect/Option"; |
| 31 | import * as SubscriptionRef from "effect/SubscriptionRef"; |
| 32 | import * as TestClock from "effect/testing/TestClock"; |
| 33 | import { RpcClientError } from "effect/rpc"; |
| 34 | |
| 35 | import { |
| 36 | AVAILABLE_CONNECTION_STATE, |
| 37 | PrimaryConnectionTarget, |
| 38 | type PreparedConnection, |
| 39 | type SupervisorConnectionState, |
| 40 | } from "../connection/model.ts"; |
| 41 | import * as EnvironmentSupervisor from "../connection/supervisor.ts"; |
| 42 | import * as RpcSession from "../rpc/session.ts"; |
| 43 | import type { WsRpcProtocolClient } from "../rpc/protocol.ts"; |
| 44 | import { v2Now, v2Projection, v2ThreadId } from "../state/orchestrationV2TestFixtures.ts"; |
| 45 | import { |
| 46 | archiveThread, |
| 47 | interruptThreadTurn, |
| 48 | revertThreadCheckpoint, |
| 49 | resumeThreadQueue, |
| 50 | startThreadTurn, |
| 51 | } from "./commands.ts"; |
| 52 | |
| 53 | const layerTestRandom = Layer.succeed( |
| 54 | SecureRandom.SecureRandom, |
| 55 | SecureRandom.make((size) => new Uint8Array(size)), |
| 56 | ); |
| 57 | |
| 58 | const TARGET = new PrimaryConnectionTarget({ |
| 59 | environmentId: EnvironmentId.make("environment-1"), |
| 60 | label: "Test environment", |
| 61 | httpBaseUrl: "https://environment.example.test", |
| 62 | wsBaseUrl: "wss://environment.example.test", |
| 63 | }); |
| 64 | |
| 65 | const CONNECTED_STATE: SupervisorConnectionState = { |
| 66 | ...AVAILABLE_CONNECTION_STATE, |
| 67 | desired: true, |
| 68 | phase: "connected", |
| 69 | }; |
| 70 | |
| 71 | const makeSupervisor = Effect.fn("TestEnvironmentCommands.makeSupervisor")(function* (input: { |
| 72 | readonly commands: OrchestrationV2Command[]; |
| 73 | readonly projects: ProjectMutation[]; |
| 74 | readonly launches?: OrchestrationV2ThreadLaunchInput[]; |
| 75 | readonly projection?: OrchestrationV2ThreadProjection; |
| 76 | readonly projectionRequests?: ThreadId[]; |
| 77 | readonly advertiseThreadStop?: boolean; |
| 78 | }) { |
| 79 | const client = { |
| 80 | [ORCHESTRATION_V2_WS_METHODS.dispatchCommand]: (command: OrchestrationV2Command) => |
| 81 | Effect.sync(() => { |
| 82 | input.commands.push(command); |
| 83 | return { sequence: input.commands.length }; |
| 84 | }), |
| 85 | [ORCHESTRATION_V2_WS_METHODS.getThreadProjection]: (requestInput: { |
| 86 | readonly threadId: ThreadId; |
| 87 | }) => |
| 88 | Effect.sync(() => { |
| 89 | input.projectionRequests?.push(requestInput.threadId); |
| 90 | return input.projection ?? v2Projection; |
| 91 | }), |
| 92 | [ORCHESTRATION_V2_WS_METHODS.launchThread]: (launchInput: OrchestrationV2ThreadLaunchInput) => |
| 93 | Effect.sync(() => { |
| 94 | input.launches?.push(launchInput); |
| 95 | return { |
| 96 | threadId: launchInput.threadId ?? v2ThreadId, |
| 97 | projection: input.projection ?? v2Projection, |
| 98 | resumed: false, |
| 99 | }; |
| 100 | }), |
| 101 | [WS_METHODS.projectsMutate]: (mutation: ProjectMutation) => |
| 102 | Effect.sync(() => { |
| 103 | input.projects.push(mutation); |
| 104 | return { |
| 105 | id: mutation.projectId, |
| 106 | title: mutation.type === "project.create" ? mutation.title : "Project", |
| 107 | workspaceRoot: |
| 108 | mutation.type === "project.create" ? mutation.workspaceRoot : "/workspace/project", |
| 109 | repositoryIdentity: null, |
| 110 | faviconPath: null, |
| 111 | defaultModelSelection: null, |
| 112 | scripts: [], |
| 113 | createdAt: "2026-06-06T00:00:00.000Z", |
| 114 | updatedAt: "2026-06-06T00:00:00.000Z", |
| 115 | deletedAt: null, |
| 116 | }; |
| 117 | }), |
| 118 | } as unknown as WsRpcProtocolClient; |
| 119 | const session: RpcSession.RpcSession = { |
| 120 | client, |
| 121 | initialConfig: Effect.succeed({ |
| 122 | environment: { |
| 123 | capabilities: { |
| 124 | repositoryIdentity: true, |
| 125 | ...(input.advertiseThreadStop === true ? { threadStop: true } : {}), |
| 126 | }, |
| 127 | }, |
| 128 | } as never), |
| 129 | subscribeServerConfig: (input = {}) => client.subscribeServerConfig(input), |
| 130 | ready: Effect.void, |
| 131 | probe: Effect.void, |
| 132 | inboundFrames: Effect.succeed(0), |
| 133 | closed: Effect.never, |
| 134 | }; |
| 135 | return EnvironmentSupervisor.EnvironmentSupervisor.of({ |
| 136 | target: TARGET, |
| 137 | state: yield* SubscriptionRef.make(CONNECTED_STATE), |
| 138 | session: yield* SubscriptionRef.make(Option.some(session)), |
| 139 | prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()), |
| 140 | connect: Effect.void, |
| 141 | disconnect: Effect.void, |
| 142 | retryNow: Effect.void, |
| 143 | } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]); |
| 144 | }); |
| 145 | |
| 146 | describe("V2 environment commands", () => { |
| 147 | it.effect("distinguishes sending a selected queued message from resuming queue order", () => |
| 148 | Effect.gen(function* () { |
| 149 | const commands: OrchestrationV2Command[] = []; |
| 150 | const supervisor = yield* makeSupervisor({ commands, projects: [] }); |
| 151 | const threadId = ThreadId.make("thread-1"); |
| 152 | yield* resumeThreadQueue({ |
| 153 | commandId: CommandId.make("send-selected"), |
| 154 | threadId, |
| 155 | runId: RunId.make("selected"), |
| 156 | }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor)); |
| 157 | yield* resumeThreadQueue({ |
| 158 | commandId: CommandId.make("resume-queue"), |
| 159 | threadId, |
| 160 | }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor)); |
| 161 | |
| 162 | expect(commands).toEqual([ |
| 163 | { type: "queued-run.start", commandId: "send-selected", threadId, runId: "selected" }, |
| 164 | { type: "queue.resume", commandId: "resume-queue", threadId }, |
| 165 | ]); |
| 166 | }).pipe(Effect.provide(layerTestRandom)), |
| 167 | ); |
| 168 | |
| 169 | it.effect("preserves caller command ids for idempotent V2 commands", () => |
| 170 | Effect.gen(function* () { |
| 171 | const commands: OrchestrationV2Command[] = []; |
| 172 | const supervisor = yield* makeSupervisor({ commands, projects: [] }); |
| 173 | |
| 174 | yield* archiveThread({ |
| 175 | commandId: CommandId.make("queued-command"), |
| 176 | threadId: ThreadId.make("thread-1"), |
| 177 | }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor)); |
| 178 | |
| 179 | expect(commands).toEqual([ |
| 180 | { type: "thread.archive", commandId: "queued-command", threadId: "thread-1" }, |
| 181 | ]); |
| 182 | }).pipe(Effect.provide(layerTestRandom)), |
| 183 | ); |
| 184 | |
| 185 | it.effect("resolves run ordinal zero to the persisted thread-start checkpoint", () => |
| 186 | Effect.gen(function* () { |
| 187 | const scopeId = CheckpointScopeId.make("checkpoint-scope-root"); |
| 188 | const checkpointId = CheckpointId.make("checkpoint-thread-start"); |
| 189 | const projection: OrchestrationV2ThreadProjection = { |
| 190 | ...v2Projection, |
| 191 | checkpoints: [ |
| 192 | { |
| 193 | id: checkpointId, |
| 194 | threadId: v2ThreadId, |
| 195 | scopeId, |
| 196 | runId: null, |
| 197 | nodeId: NodeId.make("node-run-1"), |
| 198 | parentCheckpointId: null, |
| 199 | ordinalWithinScope: 0, |
| 200 | appRunOrdinal: null, |
| 201 | ref: CheckpointRef.make("refs/t3/thread-start"), |
| 202 | status: "ready", |
| 203 | files: [], |
| 204 | capturedAt: v2Now, |
| 205 | }, |
| 206 | ], |
| 207 | }; |
| 208 | const commands: OrchestrationV2Command[] = []; |
| 209 | const supervisor = yield* makeSupervisor({ commands, projects: [], projection }); |
| 210 | |
| 211 | yield* revertThreadCheckpoint({ |
| 212 | commandId: CommandId.make("rollback-thread-start"), |
| 213 | threadId: v2ThreadId, |
| 214 | turnCount: 0, |
| 215 | }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor)); |
| 216 | |
| 217 | expect(commands).toEqual([ |
| 218 | { |
| 219 | type: "checkpoint.rollback", |
| 220 | commandId: "rollback-thread-start", |
| 221 | threadId: v2ThreadId, |
| 222 | scopeId, |
| 223 | checkpointId, |
| 224 | }, |
| 225 | ]); |
| 226 | }).pipe(Effect.provide(layerTestRandom)), |
| 227 | ); |
| 228 | |
| 229 | it.effect("preserves an existing worktree and branch during first-message launch", () => |
| 230 | Effect.gen(function* () { |
| 231 | const launches: OrchestrationV2ThreadLaunchInput[] = []; |
| 232 | const supervisor = yield* makeSupervisor({ commands: [], projects: [], launches }); |
| 233 | |
| 234 | yield* startThreadTurn({ |
| 235 | commandId: CommandId.make("launch-existing-worktree"), |
| 236 | threadId: v2ThreadId, |
| 237 | message: { |
| 238 | messageId: MessageId.make("message-existing-worktree"), |
| 239 | role: "user", |
| 240 | text: "Continue here", |
| 241 | attachments: [], |
| 242 | }, |
| 243 | runtimeMode: "full-access", |
| 244 | interactionMode: "default", |
| 245 | titleSeed: "Continue here", |
| 246 | bootstrap: { |
| 247 | createThread: { |
| 248 | projectId: ProjectId.make("project-1"), |
| 249 | title: "Thread", |
| 250 | modelSelection: v2Projection.thread.modelSelection, |
| 251 | runtimeMode: "full-access", |
| 252 | interactionMode: "default", |
| 253 | branch: "feature", |
| 254 | worktreePath: "/workspace/project-worktrees/feature", |
| 255 | createdAt: "2026-06-20T00:00:00.000Z", |
| 256 | }, |
| 257 | }, |
| 258 | }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor)); |
| 259 | |
| 260 | expect(launches[0]).toMatchObject({ |
| 261 | threadId: v2ThreadId, |
| 262 | title: "Continue here", |
| 263 | generateTitle: true, |
| 264 | workspaceStrategy: { |
| 265 | type: "existing_worktree", |
| 266 | worktreePath: "/workspace/project-worktrees/feature", |
| 267 | branch: "feature", |
| 268 | }, |
| 269 | }); |
| 270 | }).pipe(Effect.provide(layerTestRandom)), |
| 271 | ); |
| 272 | |
| 273 | it.effect("uses server-resolved delivery intent without fetching the full projection", () => |
| 274 | Effect.gen(function* () { |
| 275 | const commands: OrchestrationV2Command[] = []; |
| 276 | const projectionRequests: ThreadId[] = []; |
| 277 | const supervisor = yield* makeSupervisor({ commands, projects: [], projectionRequests }); |
| 278 | |
| 279 | for (const [mode, expectedType] of [ |
| 280 | ["queue", "queue_after_active"], |
| 281 | ["auto", "start_immediately"], |
| 282 | ["steer", "start_immediately"], |
| 283 | ["restart", "start_immediately"], |
| 284 | ] as const) { |
| 285 | yield* startThreadTurn({ |
| 286 | commandId: CommandId.make(`command-${mode}`), |
| 287 | threadId: v2ThreadId, |
| 288 | message: { |
| 289 | messageId: MessageId.make(`message-${mode}`), |
| 290 | role: "user", |
| 291 | text: mode, |
| 292 | attachments: [], |
| 293 | }, |
| 294 | runtimeMode: "full-access", |
| 295 | interactionMode: "default", |
| 296 | dispatchMode: mode, |
| 297 | }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor)); |
| 298 | |
| 299 | expect(commands.at(-1)).toMatchObject({ |
| 300 | type: "message.dispatch", |
| 301 | dispatchMode: { type: expectedType }, |
| 302 | ...(mode === "queue" ? {} : { deliveryIntent: mode }), |
| 303 | }); |
| 304 | } |
| 305 | expect(projectionRequests).toEqual([]); |
| 306 | }).pipe(Effect.provide(layerTestRandom)), |
| 307 | ); |
| 308 | |
| 309 | it.effect( |
| 310 | "sends fallback text only on empty manual continuations for servers without native resume", |
| 311 | () => |
| 312 | Effect.gen(function* () { |
| 313 | const commands: OrchestrationV2Command[] = []; |
| 314 | const supervisor = yield* makeSupervisor({ commands, projects: [] }); |
| 315 | const resumedRunId = RunId.make("run-interrupted"); |
| 316 | |
| 317 | yield* startThreadTurn({ |
| 318 | commandId: CommandId.make("command-resume-empty"), |
| 319 | threadId: v2ThreadId, |
| 320 | manualContinuationOfRunId: resumedRunId, |
| 321 | message: { |
| 322 | messageId: MessageId.make("message-resume-empty"), |
| 323 | role: "user", |
| 324 | text: "", |
| 325 | attachments: [], |
| 326 | }, |
| 327 | runtimeMode: "full-access", |
| 328 | interactionMode: "default", |
| 329 | dispatchMode: "start", |
| 330 | }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor)); |
| 331 | |
| 332 | yield* startThreadTurn({ |
| 333 | commandId: CommandId.make("command-resume-text"), |
| 334 | threadId: v2ThreadId, |
| 335 | manualContinuationOfRunId: resumedRunId, |
| 336 | message: { |
| 337 | messageId: MessageId.make("message-resume-text"), |
| 338 | role: "user", |
| 339 | text: "Keep going", |
| 340 | attachments: [], |
| 341 | }, |
| 342 | runtimeMode: "full-access", |
| 343 | interactionMode: "default", |
| 344 | dispatchMode: "start", |
| 345 | }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor)); |
| 346 | |
| 347 | yield* startThreadTurn({ |
| 348 | commandId: CommandId.make("command-start-empty"), |
| 349 | threadId: v2ThreadId, |
| 350 | message: { |
| 351 | messageId: MessageId.make("message-start-empty"), |
| 352 | role: "user", |
| 353 | text: "", |
| 354 | attachments: [], |
| 355 | }, |
| 356 | runtimeMode: "full-access", |
| 357 | interactionMode: "default", |
| 358 | dispatchMode: "start", |
| 359 | }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor)); |
| 360 | |
| 361 | expect(commands).toEqual([ |
| 362 | expect.objectContaining({ |
| 363 | type: "message.dispatch", |
| 364 | commandId: "command-resume-empty", |
| 365 | threadId: v2ThreadId, |
| 366 | manualContinuationOfRunId: resumedRunId, |
| 367 | text: "Continue where you left off.", |
| 368 | dispatchMode: { type: "start_immediately" }, |
| 369 | }), |
| 370 | expect.objectContaining({ |
| 371 | type: "message.dispatch", |
| 372 | commandId: "command-resume-text", |
| 373 | threadId: v2ThreadId, |
| 374 | manualContinuationOfRunId: resumedRunId, |
| 375 | text: "Keep going", |
| 376 | dispatchMode: { type: "start_immediately" }, |
| 377 | }), |
| 378 | expect.objectContaining({ |
| 379 | type: "message.dispatch", |
| 380 | commandId: "command-start-empty", |
| 381 | threadId: v2ThreadId, |
| 382 | text: "", |
| 383 | dispatchMode: { type: "start_immediately" }, |
| 384 | }), |
| 385 | ]); |
| 386 | }).pipe(Effect.provide(layerTestRandom)), |
| 387 | ); |
| 388 | |
| 389 | // A dead socket reaches the caller as a protocol error, or as an interrupt when |
| 390 | // the supervisor closes the session before the protocol reports the error. |
| 391 | it.effect.each([ |
| 392 | [ |
| 393 | "protocol error", |
| 394 | new RpcClientError.RpcClientError({ |
| 395 | reason: new RpcClientError.RpcClientDefect({ |
| 396 | message: "socket closed", |
| 397 | cause: new Error("socket closed"), |
| 398 | }), |
| 399 | }), |
| 400 | ], |
| 401 | ["session close", Effect.failCause(Cause.interrupt())], |
| 402 | ] as const)("retries a message dispatch lost to a %s with the same command id", ([, loss]) => |
| 403 | Effect.gen(function* () { |
| 404 | const commands: OrchestrationV2Command[] = []; |
| 405 | const supervisor = yield* makeSupervisor({ commands, projects: [] }); |
| 406 | const healthySession = Option.getOrThrow(yield* SubscriptionRef.get(supervisor.session)); |
| 407 | const firstAttemptLost = yield* Deferred.make<void>(); |
| 408 | const lostClient = { |
| 409 | ...healthySession.client, |
| 410 | [ORCHESTRATION_V2_WS_METHODS.dispatchCommand]: (command: OrchestrationV2Command) => |
| 411 | Effect.gen(function* () { |
| 412 | commands.push(command); |
| 413 | yield* SubscriptionRef.set(supervisor.session, Option.none()); |
| 414 | yield* Deferred.succeed(firstAttemptLost, undefined); |
| 415 | return yield* loss; |
| 416 | }), |
| 417 | } as unknown as WsRpcProtocolClient; |
| 418 | yield* SubscriptionRef.set( |
| 419 | supervisor.session, |
| 420 | Option.some({ ...healthySession, client: lostClient }), |
| 421 | ); |
| 422 | |
| 423 | const sendFiber = yield* startThreadTurn({ |
| 424 | commandId: CommandId.make("command-lost-ack"), |
| 425 | threadId: v2ThreadId, |
| 426 | message: { |
| 427 | messageId: MessageId.make("message-lost-ack"), |
| 428 | role: "user", |
| 429 | text: "hello", |
| 430 | attachments: [], |
| 431 | }, |
| 432 | runtimeMode: "full-access", |
| 433 | interactionMode: "default", |
| 434 | }).pipe( |
| 435 | Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 436 | Effect.forkChild, |
| 437 | ); |
| 438 | yield* Deferred.await(firstAttemptLost); |
| 439 | yield* SubscriptionRef.set(supervisor.session, Option.some(healthySession)); |
| 440 | const exit = yield* Fiber.await(sendFiber); |
| 441 | |
| 442 | expect(commands.map((command) => [command.type, command.commandId])).toEqual([ |
| 443 | ["message.dispatch", "command-lost-ack"], |
| 444 | ["message.dispatch", "command-lost-ack"], |
| 445 | ]); |
| 446 | expect(Exit.isSuccess(exit)).toBe(true); |
| 447 | }).pipe(Effect.provide(layerTestRandom)), |
| 448 | ); |
| 449 | |
| 450 | it.effect("stops resending a message whose dispatch keeps losing the socket", () => |
| 451 | Effect.gen(function* () { |
| 452 | const commands: OrchestrationV2Command[] = []; |
| 453 | const supervisor = yield* makeSupervisor({ commands, projects: [] }); |
| 454 | const healthySession = Option.getOrThrow(yield* SubscriptionRef.get(supervisor.session)); |
| 455 | const dyingClient: WsRpcProtocolClient = { |
| 456 | ...healthySession.client, |
| 457 | [ORCHESTRATION_V2_WS_METHODS.dispatchCommand]: (command: OrchestrationV2Command) => |
| 458 | Effect.gen(function* () { |
| 459 | commands.push(command); |
| 460 | yield* SubscriptionRef.set( |
| 461 | supervisor.session, |
| 462 | Option.some({ ...healthySession, client: dyingClient }), |
| 463 | ); |
| 464 | return yield* new RpcClientError.RpcClientError({ |
| 465 | reason: new RpcClientError.RpcClientDefect({ |
| 466 | message: "socket closed", |
| 467 | cause: new Error("socket closed"), |
| 468 | }), |
| 469 | }); |
| 470 | }), |
| 471 | } as unknown as WsRpcProtocolClient; |
| 472 | yield* SubscriptionRef.set( |
| 473 | supervisor.session, |
| 474 | Option.some({ ...healthySession, client: dyingClient }), |
| 475 | ); |
| 476 | |
| 477 | const error = yield* startThreadTurn({ |
| 478 | commandId: CommandId.make("command-socket-killer"), |
| 479 | threadId: v2ThreadId, |
| 480 | message: { |
| 481 | messageId: MessageId.make("message-socket-killer"), |
| 482 | role: "user", |
| 483 | text: "hello", |
| 484 | attachments: [], |
| 485 | }, |
| 486 | runtimeMode: "full-access", |
| 487 | interactionMode: "default", |
| 488 | }).pipe( |
| 489 | Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 490 | Effect.flip, |
| 491 | ); |
| 492 | |
| 493 | expect(commands.map((command) => command.commandId)).toEqual([ |
| 494 | "command-socket-killer", |
| 495 | "command-socket-killer", |
| 496 | "command-socket-killer", |
| 497 | ]); |
| 498 | expect(error._tag).toBe("RpcClientError"); |
| 499 | }).pipe(Effect.provide(layerTestRandom)), |
| 500 | ); |
| 501 | |
| 502 | it.effect("fails a message dispatch the host never answers on a live socket", () => |
| 503 | Effect.gen(function* () { |
| 504 | const commands: OrchestrationV2Command[] = []; |
| 505 | const supervisor = yield* makeSupervisor({ commands, projects: [] }); |
| 506 | const healthySession = Option.getOrThrow(yield* SubscriptionRef.get(supervisor.session)); |
| 507 | const silentClient = { |
| 508 | ...healthySession.client, |
| 509 | [ORCHESTRATION_V2_WS_METHODS.dispatchCommand]: (command: OrchestrationV2Command) => |
| 510 | Effect.sync(() => commands.push(command)).pipe(Effect.andThen(Effect.never)), |
| 511 | } as unknown as WsRpcProtocolClient; |
| 512 | yield* SubscriptionRef.set( |
| 513 | supervisor.session, |
| 514 | Option.some({ ...healthySession, client: silentClient }), |
| 515 | ); |
| 516 | |
| 517 | const sendFiber = yield* startThreadTurn({ |
| 518 | commandId: CommandId.make("command-unanswered"), |
| 519 | threadId: v2ThreadId, |
| 520 | message: { |
| 521 | messageId: MessageId.make("message-unanswered"), |
| 522 | role: "user", |
| 523 | text: "hello", |
| 524 | attachments: [], |
| 525 | }, |
| 526 | runtimeMode: "full-access", |
| 527 | interactionMode: "default", |
| 528 | }).pipe( |
| 529 | Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 530 | Effect.flip, |
| 531 | Effect.forkChild, |
| 532 | ); |
| 533 | yield* TestClock.adjust("1 minute"); |
| 534 | |
| 535 | expect(sendFiber.pollUnsafe()).toBeDefined(); |
| 536 | expect((yield* Fiber.join(sendFiber))._tag).toBe("EnvironmentRpcUnavailableError"); |
| 537 | expect(commands.map((command) => command.commandId)).toEqual(["command-unanswered"]); |
| 538 | }).pipe(Effect.provide(layerTestRandom)), |
| 539 | ); |
| 540 | |
| 541 | it.effect("holds a lost message through reconnect backoff until disconnect", () => |
| 542 | Effect.gen(function* () { |
| 543 | const commands: OrchestrationV2Command[] = []; |
| 544 | const supervisor = yield* makeSupervisor({ commands, projects: [] }); |
| 545 | const healthySession = Option.getOrThrow(yield* SubscriptionRef.get(supervisor.session)); |
| 546 | const lostClient = { |
| 547 | ...healthySession.client, |
| 548 | [ORCHESTRATION_V2_WS_METHODS.dispatchCommand]: (command: OrchestrationV2Command) => |
| 549 | Effect.gen(function* () { |
| 550 | commands.push(command); |
| 551 | yield* SubscriptionRef.set(supervisor.session, Option.none()); |
| 552 | return yield* new RpcClientError.RpcClientError({ |
| 553 | reason: new RpcClientError.RpcClientDefect({ |
| 554 | message: "socket closed", |
| 555 | cause: new Error("socket closed"), |
| 556 | }), |
| 557 | }); |
| 558 | }), |
| 559 | } as unknown as WsRpcProtocolClient; |
| 560 | const send = (resendAfterSocketLoss: boolean) => |
| 561 | Effect.gen(function* () { |
| 562 | yield* SubscriptionRef.set( |
| 563 | supervisor.session, |
| 564 | Option.some({ ...healthySession, client: lostClient }), |
| 565 | ); |
| 566 | return yield* startThreadTurn({ |
| 567 | commandId: CommandId.make(`command-host-gone-${resendAfterSocketLoss}`), |
| 568 | threadId: v2ThreadId, |
| 569 | message: { |
| 570 | messageId: MessageId.make(`message-host-gone-${resendAfterSocketLoss}`), |
| 571 | role: "user", |
| 572 | text: "hello", |
| 573 | attachments: [], |
| 574 | }, |
| 575 | runtimeMode: "full-access", |
| 576 | interactionMode: "default", |
| 577 | resendAfterSocketLoss, |
| 578 | }).pipe( |
| 579 | Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 580 | Effect.flip, |
| 581 | Effect.forkChild, |
| 582 | ); |
| 583 | }); |
| 584 | |
| 585 | const optedOut = yield* send(false); |
| 586 | expect((yield* Fiber.join(optedOut))._tag).toBe("RpcClientError"); |
| 587 | |
| 588 | const waiting = yield* send(true); |
| 589 | yield* SubscriptionRef.set(supervisor.state, { |
| 590 | ...CONNECTED_STATE, |
| 591 | phase: "backoff" as const, |
| 592 | }); |
| 593 | yield* TestClock.adjust("2 minutes"); |
| 594 | expect(waiting.pollUnsafe()).toBeUndefined(); |
| 595 | yield* SubscriptionRef.set(supervisor.state, AVAILABLE_CONNECTION_STATE); |
| 596 | expect((yield* Fiber.join(waiting))._tag).toBe("RpcClientError"); |
| 597 | expect(commands.map((command) => command.commandId)).toEqual([ |
| 598 | "command-host-gone-false", |
| 599 | "command-host-gone-true", |
| 600 | ]); |
| 601 | }).pipe(Effect.provide(layerTestRandom)), |
| 602 | ); |
| 603 | |
| 604 | it.effect("Stop with no run left ends the thread's pull request watches", () => |
| 605 | Effect.gen(function* () { |
| 606 | const link = { |
| 607 | host: "github.com", |
| 608 | repository: "pingdotgg/t3code", |
| 609 | url: "https://github.com/pingdotgg/t3code/pull/7", |
| 610 | source: "agent" as const, |
| 611 | linkedAt: "2026-10-05T00:00:00.000Z", |
| 612 | snapshot: null, |
| 613 | stack: null, |
| 614 | }; |
| 615 | const watch = { |
| 616 | startedAt: "2026-10-05T00:00:00.000Z", |
| 617 | headSha: null, |
| 618 | failedChecks: [], |
| 619 | passed: false, |
| 620 | passedChecks: [], |
| 621 | remarksThrough: "2026-10-05T00:00:00.000Z", |
| 622 | remarkIds: [], |
| 623 | conflicting: false, |
| 624 | wakes: 0, |
| 625 | }; |
| 626 | const projection: OrchestrationV2ThreadProjection = { |
| 627 | ...v2Projection, |
| 628 | thread: { |
| 629 | ...v2Projection.thread, |
| 630 | pullRequests: [ |
| 631 | { ...link, number: 7, watch }, |
| 632 | { ...link, number: 8 }, |
| 633 | { ...link, number: 9, source: "stack-dismissed", watch }, |
| 634 | ], |
| 635 | }, |
| 636 | }; |
| 637 | const commands: OrchestrationV2Command[] = []; |
| 638 | const supervisor = yield* makeSupervisor({ commands, projects: [], projection }); |
| 639 | |
| 640 | yield* interruptThreadTurn({ threadId: v2ThreadId }).pipe( |
| 641 | Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 642 | ); |
| 643 | |
| 644 | expect(commands).toEqual([ |
| 645 | { |
| 646 | type: "thread.pull-request.watch", |
| 647 | commandId: expect.any(String), |
| 648 | threadId: v2ThreadId, |
| 649 | host: "github.com", |
| 650 | repository: "pingdotgg/t3code", |
| 651 | number: 7, |
| 652 | watching: false, |
| 653 | }, |
| 654 | ]); |
| 655 | }).pipe(Effect.provide(layerTestRandom)), |
| 656 | ); |
| 657 | |
| 658 | it.effect.each(["waiting", "completed", "rolled_back"] as const)( |
| 659 | "dispatches Stop for %s runs with background commands except rollback", |
| 660 | (status) => |
| 661 | Effect.gen(function* () { |
| 662 | const waitingRunId = RunId.make("run-waiting"); |
| 663 | const projection: OrchestrationV2ThreadProjection = { |
| 664 | ...v2Projection, |
| 665 | runs: [ |
| 666 | { |
| 667 | id: waitingRunId, |
| 668 | threadId: v2ThreadId, |
| 669 | ordinal: 1, |
| 670 | providerInstanceId: v2Projection.thread.providerInstanceId, |
| 671 | modelSelection: v2Projection.thread.modelSelection, |
| 672 | providerThreadId: null, |
| 673 | userMessageId: MessageId.make("message-waiting"), |
| 674 | rootNodeId: null, |
| 675 | activeAttemptId: null, |
| 676 | status, |
| 677 | requestedAt: v2Now, |
| 678 | startedAt: v2Now, |
| 679 | completedAt: null, |
| 680 | checkpointId: null, |
| 681 | contextHandoffId: null, |
| 682 | }, |
| 683 | ], |
| 684 | turnItems: [ |
| 685 | { |
| 686 | id: TurnItemId.make("background-command"), |
| 687 | threadId: v2ThreadId, |
| 688 | runId: waitingRunId, |
| 689 | nodeId: null, |
| 690 | providerThreadId: null, |
| 691 | providerTurnId: null, |
| 692 | nativeItemRef: null, |
| 693 | parentItemId: null, |
| 694 | ordinal: 1, |
| 695 | status: "running", |
| 696 | title: null, |
| 697 | startedAt: v2Now, |
| 698 | completedAt: null, |
| 699 | updatedAt: v2Now, |
| 700 | type: "command_execution", |
| 701 | input: "vp run dev", |
| 702 | }, |
| 703 | ], |
| 704 | }; |
| 705 | const commands: OrchestrationV2Command[] = []; |
| 706 | const supervisor = yield* makeSupervisor({ commands, projects: [], projection }); |
| 707 | |
| 708 | const result = yield* interruptThreadTurn({ threadId: v2ThreadId }).pipe( |
| 709 | Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 710 | ); |
| 711 | |
| 712 | expect(result).toEqual({ sequence: status === "rolled_back" ? 0 : 1 }); |
| 713 | expect(commands).toEqual( |
| 714 | status === "rolled_back" |
| 715 | ? [] |
| 716 | : [ |
| 717 | { |
| 718 | type: "run.interrupt", |
| 719 | commandId: expect.any(String), |
| 720 | threadId: v2ThreadId, |
| 721 | runId: waitingRunId, |
| 722 | holdQueue: true, |
| 723 | }, |
| 724 | ], |
| 725 | ); |
| 726 | }).pipe(Effect.provide(layerTestRandom)), |
| 727 | ); |
| 728 | |
| 729 | it.effect( |
| 730 | "a native subagent stop targets its owning run rather than holding the parent queue", |
| 731 | () => |
| 732 | Effect.gen(function* () { |
| 733 | const subagentId = NodeId.make("native-subagent"); |
| 734 | const runId = RunId.make("settled-owner-run"); |
| 735 | const commands: OrchestrationV2Command[] = []; |
| 736 | const supervisor = yield* makeSupervisor({ |
| 737 | commands, |
| 738 | projects: [], |
| 739 | projection: { |
| 740 | ...v2Projection, |
| 741 | subagents: [ |
| 742 | { |
| 743 | id: subagentId, |
| 744 | threadId: v2ThreadId, |
| 745 | runId, |
| 746 | parentNodeId: NodeId.make("owner-node"), |
| 747 | origin: "provider_native", |
| 748 | createdBy: "agent", |
| 749 | driver: ProviderDriverKind.make("claudeAgent"), |
| 750 | providerInstanceId: ProviderInstanceId.make("claudeAgent"), |
| 751 | providerThreadId: null, |
| 752 | childThreadId: ThreadId.make("native-child-thread"), |
| 753 | nativeTaskRef: null, |
| 754 | prompt: "Work", |
| 755 | title: null, |
| 756 | model: null, |
| 757 | result: null, |
| 758 | status: "running", |
| 759 | startedAt: v2Now, |
| 760 | completedAt: null, |
| 761 | updatedAt: v2Now, |
| 762 | }, |
| 763 | ], |
| 764 | }, |
| 765 | }); |
| 766 | yield* interruptThreadTurn({ |
| 767 | commandId: CommandId.make("native-stop"), |
| 768 | threadId: v2ThreadId, |
| 769 | subagentId, |
| 770 | }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor)); |
| 771 | expect(commands).toEqual([ |
| 772 | { |
| 773 | type: "subagent.stop", |
| 774 | commandId: "native-stop", |
| 775 | threadId: v2ThreadId, |
| 776 | runId, |
| 777 | subagentId, |
| 778 | }, |
| 779 | ]); |
| 780 | }).pipe(Effect.provide(layerTestRandom)), |
| 781 | ); |
| 782 | |
| 783 | it.effect( |
| 784 | "an app-owned subagent stop asks the parent to clear an orphan, then stops the child", |
| 785 | () => |
| 786 | Effect.gen(function* () { |
| 787 | const subagentId = NodeId.make("app-owned-subagent"); |
| 788 | const runId = RunId.make("parent-run"); |
| 789 | const childThreadId = ThreadId.make("app-owned-child-thread"); |
| 790 | const commands: OrchestrationV2Command[] = []; |
| 791 | const supervisor = yield* makeSupervisor({ |
| 792 | commands, |
| 793 | projects: [], |
| 794 | advertiseThreadStop: true, |
| 795 | projection: { |
| 796 | ...v2Projection, |
| 797 | subagents: [ |
| 798 | { |
| 799 | id: subagentId, |
| 800 | threadId: v2ThreadId, |
| 801 | runId, |
| 802 | parentNodeId: NodeId.make("owner-node"), |
| 803 | origin: "app_owned", |
| 804 | createdBy: "agent", |
| 805 | driver: ProviderDriverKind.make("codex"), |
| 806 | providerInstanceId: ProviderInstanceId.make("codex"), |
| 807 | providerThreadId: null, |
| 808 | childThreadId, |
| 809 | nativeTaskRef: null, |
| 810 | prompt: "Work", |
| 811 | title: null, |
| 812 | model: null, |
| 813 | result: null, |
| 814 | status: "running", |
| 815 | startedAt: v2Now, |
| 816 | completedAt: null, |
| 817 | updatedAt: v2Now, |
| 818 | }, |
| 819 | ], |
| 820 | }, |
| 821 | }); |
| 822 | yield* interruptThreadTurn({ |
| 823 | commandId: CommandId.make("lineage-stop"), |
| 824 | threadId: v2ThreadId, |
| 825 | subagentId, |
| 826 | // A parent run must not be interrupted on the child thread. |
| 827 | runId, |
| 828 | }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor)); |
| 829 | expect(commands).toEqual([ |
| 830 | { |
| 831 | type: "subagent.stop", |
| 832 | commandId: expect.any(String), |
| 833 | threadId: v2ThreadId, |
| 834 | runId, |
| 835 | subagentId, |
| 836 | }, |
| 837 | { type: "thread.stop", commandId: "lineage-stop", threadId: childThreadId }, |
| 838 | ]); |
| 839 | }).pipe(Effect.provide(layerTestRandom)), |
| 840 | ); |
| 841 | |
| 842 | it.effect("stops a thread without relying on background work in the history window", () => |
| 843 | Effect.gen(function* () { |
| 844 | const commands: OrchestrationV2Command[] = []; |
| 845 | const projectionRequests: ThreadId[] = []; |
| 846 | const supervisor = yield* makeSupervisor({ |
| 847 | commands, |
| 848 | projects: [], |
| 849 | projectionRequests, |
| 850 | advertiseThreadStop: true, |
| 851 | }); |
| 852 | |
| 853 | const result = yield* interruptThreadTurn({ |
| 854 | commandId: CommandId.make("stop-outside-history-window"), |
| 855 | threadId: v2ThreadId, |
| 856 | }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor)); |
| 857 | |
| 858 | expect(result).toEqual({ sequence: 1 }); |
| 859 | expect(projectionRequests).toEqual([]); |
| 860 | expect(commands).toEqual([ |
| 861 | { |
| 862 | type: "thread.stop", |
| 863 | commandId: "stop-outside-history-window", |
| 864 | threadId: v2ThreadId, |
| 865 | }, |
| 866 | ]); |
| 867 | }).pipe(Effect.provide(layerTestRandom)), |
| 868 | ); |
| 869 | }); |