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
Large file — syntax highlighting disabled.
| 1 | import { PullRequestProviderError } from "@t3tools/source-control-core/server/PullRequestProvider"; |
| 2 | import { SourceControlProviderKind } from "@t3tools/contracts"; |
| 3 | import { limitRecoveryCommand } from "./UsageLimitRecoveryWorker.ts"; |
| 4 | import { createOrchestrationV2TurnItemVisibility } from "@t3tools/shared/orchestrationV2Timeline"; |
| 5 | import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions"; |
| 6 | import * as SourceControlProviderRegistry from "../sourceControl/SourceControlProviderRegistry.ts"; |
| 7 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 8 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 9 | import { assert, it } from "@effect/vitest"; |
| 10 | import { vi } from "vite-plus/test"; |
| 11 | import { |
| 12 | type ApplicationStoredEvent, |
| 13 | CheckpointId, |
| 14 | CheckpointRef, |
| 15 | CommandId, |
| 16 | ContextTransferId, |
| 17 | EnvironmentId, |
| 18 | EventId, |
| 19 | IssueTrackerKind, |
| 20 | MessageId, |
| 21 | NodeId, |
| 22 | RuntimeRequestId, |
| 23 | TurnItemId, |
| 24 | type ModelSelection, |
| 25 | type OrchestrationV2DomainEvent, |
| 26 | type OrchestrationV2Run, |
| 27 | type OrchestrationV2ShellSnapshot, |
| 28 | type OrchestrationV2RuntimeRequest, |
| 29 | ProjectId, |
| 30 | type PullRequestCheck, |
| 31 | type PullRequestDetail, |
| 32 | type PullRequestComment, |
| 33 | PullRequestOperationError, |
| 34 | type PullRequestWatchUntil, |
| 35 | ProviderDriverKind, |
| 36 | ProviderInstanceId, |
| 37 | ProviderSessionId, |
| 38 | ProviderThreadId, |
| 39 | ProviderTurnId, |
| 40 | RunId, |
| 41 | ScheduledTaskId, |
| 42 | ThreadId, |
| 43 | } from "@t3tools/contracts"; |
| 44 | import * as Effect from "effect/Effect"; |
| 45 | import * as DateTime from "effect/DateTime"; |
| 46 | import * as Deferred from "effect/Deferred"; |
| 47 | import * as Fiber from "effect/Fiber"; |
| 48 | import * as FileSystem from "effect/FileSystem"; |
| 49 | import * as Layer from "effect/Layer"; |
| 50 | import * as Option from "effect/Option"; |
| 51 | import * as Path from "effect/Path"; |
| 52 | import * as PubSub from "effect/PubSub"; |
| 53 | import * as Queue from "effect/Queue"; |
| 54 | import * as Ref from "effect/Ref"; |
| 55 | import * as Stream from "effect/Stream"; |
| 56 | import * as TestClock from "effect/testing/TestClock"; |
| 57 | import * as SqlClient from "effect/sql/SqlClient"; |
| 58 | |
| 59 | import { createAttachmentId } from "../attachmentStore.ts"; |
| 60 | import * as CheckpointStore from "../checkpointing/CheckpointStore.ts"; |
| 61 | import * as GitWorkflow from "../git/GitWorkflowService.ts"; |
| 62 | import * as ServerConfig from "../config.ts"; |
| 63 | import * as SqlitePersistence from "../persistence/Sqlite.ts"; |
| 64 | import * as OrchestrationEventStore from "../persistence/OrchestrationEventStore.ts"; |
| 65 | import * as ProjectEnrichmentService from "../project/ProjectEnrichmentService.ts"; |
| 66 | import * as ProjectService from "../project/ProjectService.ts"; |
| 67 | import * as ServerSettings from "../serverSettings.ts"; |
| 68 | import * as McpSessionRegistryTestkit from "../mcp/McpSessionRegistry.testkit.ts"; |
| 69 | import * as ModelManifest from "../provider/ModelManifest.ts"; |
| 70 | import * as ProviderInstanceRegistry from "../provider/ProviderInstanceRegistry.ts"; |
| 71 | import type { ProviderInstance } from "@t3tools/provider-core/server/driver"; |
| 72 | import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts"; |
| 73 | import * as VcsProcess from "../vcs/VcsProcess.ts"; |
| 74 | import * as WorkspacePaths from "../workspace/WorkspacePaths.ts"; |
| 75 | import * as LegacyV1ThreadImporter from "./legacy/LegacyV1ThreadImporter.ts"; |
| 76 | import * as Orchestrator from "./Orchestrator.ts"; |
| 77 | import { ROLLBACK_FAILED_MESSAGE } from "./CheckpointRollbackService.ts"; |
| 78 | import * as EffectWorker from "./EffectWorker.ts"; |
| 79 | import * as EffectOutbox from "./EffectOutbox.ts"; |
| 80 | import * as EventSink from "./EventSink.ts"; |
| 81 | import * as ProviderRuntimeRecoveryService from "./ProviderRuntimeRecoveryService.ts"; |
| 82 | import * as EventStore from "./EventStore.ts"; |
| 83 | import * as ProjectionReplay from "./testkit/ProjectionReplay.ts"; |
| 84 | import * as ProjectionStore from "./ProjectionStore.ts"; |
| 85 | import * as PullRequestWatchReactor from "./PullRequestWatchReactor.ts"; |
| 86 | import { PULL_REQUEST_WATCH_WAKE_LIMIT } from "./pullRequestWatch.ts"; |
| 87 | import * as PullRequestService from "../pullRequest/PullRequestService.ts"; |
| 88 | import * as ProjectStore from "./ProjectStore.ts"; |
| 89 | import * as ProviderSessionManager from "./ProviderSessionManager.ts"; |
| 90 | import * as RuntimeLayer from "./runtimeLayer.ts"; |
| 91 | import { shellStreamItemFromThreadShell } from "./ShellStream.ts"; |
| 92 | import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts"; |
| 93 | import * as ThreadManagementService from "./ThreadManagementService.ts"; |
| 94 | import * as ManagedProjectFolders from "../project/ManagedProjectFolders.ts"; |
| 95 | import * as ScratchThreadPromotion from "../project/ScratchThreadPromotion.ts"; |
| 96 | import * as ScheduledTaskService from "../scheduledTasks/ScheduledTaskService.ts"; |
| 97 | import * as ManagedPolicy from "../policy/ManagedPolicy.ts"; |
| 98 | import * as Scheduler from "../scheduling/Scheduler.ts"; |
| 99 | import * as SecretRequests from "../secrets/SecretRequests.ts"; |
| 100 | import * as DeviceHost from "../device/DeviceHost.ts"; |
| 101 | import * as DeviceService from "../device/DeviceService.ts"; |
| 102 | import * as PreviewManager from "../preview/Manager.ts"; |
| 103 | import * as TerminalManager from "../terminal/Manager.ts"; |
| 104 | import * as ResourceCleanupService from "./ResourceCleanupService.ts"; |
| 105 | import * as ThreadLaunchService from "./ThreadLaunchService.ts"; |
| 106 | import * as ThreadCommandExecutor from "./ThreadCommandExecutor.ts"; |
| 107 | import * as DelegatedTaskQuestionReactor from "./DelegatedTaskQuestionReactor.ts"; |
| 108 | import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter"; |
| 109 | |
| 110 | const layerPlatformTest = Layer.merge( |
| 111 | Layer.mergeAll(NodeServices.layer, NodeCrypto.layer), |
| 112 | Layer.mock(SourceControlProviderRegistry.SourceControlProviderRegistry)({ |
| 113 | resolveLink: () => Effect.die("unused title link"), |
| 114 | }), |
| 115 | ); |
| 116 | |
| 117 | const layerServerConfig = ServerConfig.layerTest(process.cwd(), { |
| 118 | prefix: "t3-orchestration-v2-runtime-layer-", |
| 119 | }); |
| 120 | |
| 121 | const modelSelection = { |
| 122 | instanceId: ProviderInstanceId.make("codex"), |
| 123 | model: "gpt-5.4", |
| 124 | } satisfies ModelSelection; |
| 125 | const alternateInstanceId = ProviderInstanceId.make("codex_alternate"); |
| 126 | |
| 127 | const layerVcsDriverRegistryTest = VcsDriverRegistry.layer.pipe( |
| 128 | Layer.provide(VcsProcess.layer), |
| 129 | Layer.provide(layerServerConfig), |
| 130 | Layer.provide(layerPlatformTest), |
| 131 | ); |
| 132 | |
| 133 | const layerCheckpointStoreTest = CheckpointStore.layer.pipe( |
| 134 | Layer.provide(layerVcsDriverRegistryTest), |
| 135 | ); |
| 136 | const layerGitWorkflowTest = Layer.mock(GitWorkflow.GitWorkflowService)({ |
| 137 | pruneWorktrees: () => Effect.void, |
| 138 | createWorktree: () => Effect.succeed({} as never), |
| 139 | }); |
| 140 | const layerProjectServiceTest = Layer.mock(ProjectService.ProjectService)({ |
| 141 | getById: () => Effect.succeedNone, |
| 142 | }); |
| 143 | |
| 144 | const driver = ProviderDriverKind.make("codex"); |
| 145 | const orchestrationAdapter = { |
| 146 | instanceId: modelSelection.instanceId, |
| 147 | driver, |
| 148 | getCapabilities: () => Effect.succeed(CodexProviderCapabilitiesV2), |
| 149 | planSelectionTransition: () => Effect.succeed({ type: "apply_on_next_turn" }), |
| 150 | openSession: () => Effect.die("sessions are not used by lifecycle tests"), |
| 151 | } as ProviderAdapter.ProviderAdapterV2["Service"]; |
| 152 | const providerInstance = { |
| 153 | instanceId: modelSelection.instanceId, |
| 154 | driverKind: driver, |
| 155 | continuationIdentity: { |
| 156 | driverKind: driver, |
| 157 | continuationKey: "codex:test", |
| 158 | }, |
| 159 | displayName: "Codex test", |
| 160 | enabled: true, |
| 161 | snapshot: { getSnapshot: Effect.succeed({}) } as unknown as ProviderInstance["snapshot"], |
| 162 | orchestrationAdapter, |
| 163 | textGeneration: {} as ProviderInstance["textGeneration"], |
| 164 | } satisfies ProviderInstance; |
| 165 | const alternateProviderInstance = { |
| 166 | ...providerInstance, |
| 167 | instanceId: alternateInstanceId, |
| 168 | continuationIdentity: { |
| 169 | driverKind: driver, |
| 170 | continuationKey: "codex:test:alternate", |
| 171 | }, |
| 172 | displayName: "Codex alternate test", |
| 173 | orchestrationAdapter: { |
| 174 | ...orchestrationAdapter, |
| 175 | instanceId: alternateInstanceId, |
| 176 | }, |
| 177 | } satisfies ProviderInstance; |
| 178 | const claudeInstanceId = ProviderInstanceId.make("claude_test"); |
| 179 | const claudeDriver = ProviderDriverKind.make("claudeAgent"); |
| 180 | const claudeProviderInstance = { |
| 181 | ...providerInstance, |
| 182 | instanceId: claudeInstanceId, |
| 183 | driverKind: claudeDriver, |
| 184 | continuationIdentity: { driverKind: claudeDriver, continuationKey: "claude:test" }, |
| 185 | displayName: "Claude test", |
| 186 | orchestrationAdapter: { |
| 187 | ...orchestrationAdapter, |
| 188 | instanceId: claudeInstanceId, |
| 189 | driver: claudeDriver, |
| 190 | }, |
| 191 | } satisfies ProviderInstance; |
| 192 | |
| 193 | const antigravityInstanceId = ProviderInstanceId.make("antigravity_test"); |
| 194 | const antigravityDriver = ProviderDriverKind.make("antigravity"); |
| 195 | const antigravityProviderInstance = { |
| 196 | ...providerInstance, |
| 197 | instanceId: antigravityInstanceId, |
| 198 | driverKind: antigravityDriver, |
| 199 | continuationIdentity: { driverKind: antigravityDriver, continuationKey: "antigravity:test" }, |
| 200 | displayName: "Antigravity test", |
| 201 | orchestrationAdapter: { |
| 202 | ...orchestrationAdapter, |
| 203 | instanceId: antigravityInstanceId, |
| 204 | driver: antigravityDriver, |
| 205 | getCapabilities: () => |
| 206 | Effect.succeed({ |
| 207 | ...CodexProviderCapabilitiesV2, |
| 208 | turns: { ...CodexProviderCapabilitiesV2.turns, supportsNativeGoals: true }, |
| 209 | }), |
| 210 | }, |
| 211 | } satisfies ProviderInstance; |
| 212 | |
| 213 | const layerTestProviderInstanceRegistry = Layer.merge( |
| 214 | ModelManifest.layerTest, |
| 215 | Layer.succeed(ProviderInstanceRegistry.ProviderInstanceRegistry, { |
| 216 | getInstance: (instanceId) => |
| 217 | Effect.succeed( |
| 218 | [ |
| 219 | providerInstance, |
| 220 | alternateProviderInstance, |
| 221 | claudeProviderInstance, |
| 222 | antigravityProviderInstance, |
| 223 | ].find((instance) => instanceId === instance.instanceId), |
| 224 | ), |
| 225 | listInstances: Effect.succeed([ |
| 226 | providerInstance, |
| 227 | alternateProviderInstance, |
| 228 | claudeProviderInstance, |
| 229 | antigravityProviderInstance, |
| 230 | ]), |
| 231 | listUnavailable: Effect.succeed([]), |
| 232 | streamChanges: Stream.empty, |
| 233 | subscribeChanges: Effect.never, |
| 234 | }), |
| 235 | ); |
| 236 | |
| 237 | const watchedPullRequestDetail = (input: { |
| 238 | readonly projectId: ProjectId; |
| 239 | readonly number: number; |
| 240 | readonly at: string; |
| 241 | }): PullRequestDetail => ({ |
| 242 | provider: SourceControlProviderKind.make("github"), |
| 243 | capabilities: { |
| 244 | diff: true, |
| 245 | comment: true, |
| 246 | actions: [], |
| 247 | mergeMethods: [], |
| 248 | search: false, |
| 249 | checksAlwaysReport: true, |
| 250 | review: { inlineComment: false, reply: false, resolve: false, verdicts: [] }, |
| 251 | reviewers: { request: false, listCandidates: false }, |
| 252 | }, |
| 253 | viewerPermissions: { |
| 254 | actions: [], |
| 255 | comment: true, |
| 256 | resolve: true, |
| 257 | verdicts: [], |
| 258 | requestReviewers: false, |
| 259 | }, |
| 260 | projectId: input.projectId, |
| 261 | projectTitle: "Watch wake", |
| 262 | workspaceRoot: "/workspace/watch", |
| 263 | repository: "pingdotgg/t3code", |
| 264 | number: input.number, |
| 265 | title: "Watched pull request", |
| 266 | body: "", |
| 267 | url: `https://github.com/pingdotgg/t3code/pull/${input.number}`, |
| 268 | author: { login: "agent-user", name: null, avatarUrl: null }, |
| 269 | state: "open", |
| 270 | isDraft: false, |
| 271 | mergeability: "mergeable", |
| 272 | additions: 1, |
| 273 | deletions: 0, |
| 274 | changedFiles: 1, |
| 275 | headBranch: "feature", |
| 276 | headSha: "abc1234def", |
| 277 | baseBranch: "main", |
| 278 | createdAt: input.at, |
| 279 | updatedAt: input.at, |
| 280 | mergedAt: null, |
| 281 | closedAt: null, |
| 282 | reviewers: [], |
| 283 | labels: [], |
| 284 | checks: [{ name: "lint", status: "failure", description: null, url: null }], |
| 285 | mergeCapabilities: { merge: true, squash: true, rebase: true }, |
| 286 | viewer: "agent-user", |
| 287 | }); |
| 288 | |
| 289 | const seedProject = (input: { |
| 290 | readonly projectId: ProjectId; |
| 291 | readonly title: string; |
| 292 | readonly workspaceRoot: string; |
| 293 | readonly defaultModelSelection: ModelSelection | null; |
| 294 | readonly createdAt: string; |
| 295 | }) => |
| 296 | Effect.flatMap(ProjectStore.ProjectStoreV2, (projects) => |
| 297 | projects.apply({ |
| 298 | sequence: 0, |
| 299 | eventId: EventId.make(`seed:${input.projectId}`), |
| 300 | aggregateKind: "project", |
| 301 | aggregateId: input.projectId, |
| 302 | occurredAt: input.createdAt, |
| 303 | commandId: null, |
| 304 | causationEventId: null, |
| 305 | correlationId: null, |
| 306 | metadata: {}, |
| 307 | type: "project.created", |
| 308 | payload: { |
| 309 | projectId: input.projectId, |
| 310 | title: input.title, |
| 311 | workspaceRoot: input.workspaceRoot, |
| 312 | defaultModelSelection: input.defaultModelSelection, |
| 313 | scripts: [], |
| 314 | createdAt: input.createdAt, |
| 315 | updatedAt: input.createdAt, |
| 316 | }, |
| 317 | }), |
| 318 | ); |
| 319 | |
| 320 | const moveProject = (projectId: ProjectId, workspaceRoot: string, updatedAt: string) => |
| 321 | Effect.flatMap(ProjectStore.ProjectStoreV2, (projects) => |
| 322 | projects.apply({ |
| 323 | sequence: 0, |
| 324 | eventId: EventId.make(`move:${projectId}:${workspaceRoot}`), |
| 325 | aggregateKind: "project", |
| 326 | aggregateId: projectId, |
| 327 | occurredAt: updatedAt, |
| 328 | commandId: null, |
| 329 | causationEventId: null, |
| 330 | correlationId: null, |
| 331 | metadata: {}, |
| 332 | type: "project.meta-updated", |
| 333 | payload: { projectId, workspaceRoot, updatedAt }, |
| 334 | }), |
| 335 | ); |
| 336 | |
| 337 | // Read-only oracle: projections still equal a replay of these unpruned test logs. |
| 338 | const layerProjectionReplayTest = ProjectionReplay.layer.pipe( |
| 339 | Layer.provide( |
| 340 | Layer.merge( |
| 341 | EventStore.layerFromOrchestrationEventStore.pipe( |
| 342 | Layer.provide(RuntimeLayer.layerEventInfrastructure), |
| 343 | ), |
| 344 | ProjectionStore.layer, |
| 345 | ), |
| 346 | ), |
| 347 | ); |
| 348 | |
| 349 | const layerTest = Layer.mergeAll( |
| 350 | RuntimeLayer.layer, |
| 351 | layerProjectionReplayTest, |
| 352 | RuntimeLayer.layerEventSink, |
| 353 | ProjectStore.layer, |
| 354 | ProjectionStore.layer, |
| 355 | EffectOutbox.layer, |
| 356 | ThreadCommandExecutor.layer, |
| 357 | ).pipe( |
| 358 | Layer.provide(McpSessionRegistryTestkit.layer), |
| 359 | Layer.provide(McpProviderSessions.layer), |
| 360 | Layer.provide(SqlitePersistence.layerMemory), |
| 361 | Layer.provide(layerCheckpointStoreTest), |
| 362 | Layer.provide(layerServerConfig), |
| 363 | Layer.provide(ServerSettings.layerTest()), |
| 364 | Layer.provide(layerTestProviderInstanceRegistry), |
| 365 | Layer.provide(layerGitWorkflowTest), |
| 366 | Layer.provide(layerProjectServiceTest), |
| 367 | Layer.provide(layerPlatformTest), |
| 368 | ); |
| 369 | |
| 370 | const layerLegacyImportTest = Layer.merge(RuntimeLayer.layer, layerProjectionReplayTest).pipe( |
| 371 | Layer.provide(McpSessionRegistryTestkit.layer), |
| 372 | Layer.provide(McpProviderSessions.layer), |
| 373 | Layer.provideMerge(SqlitePersistence.layerMemory), |
| 374 | Layer.provide(layerCheckpointStoreTest), |
| 375 | Layer.provide(layerServerConfig), |
| 376 | Layer.provide(ServerSettings.layerTest()), |
| 377 | Layer.provide(layerTestProviderInstanceRegistry), |
| 378 | Layer.provide(layerGitWorkflowTest), |
| 379 | Layer.provide(layerProjectServiceTest), |
| 380 | Layer.provide(layerPlatformTest), |
| 381 | ); |
| 382 | |
| 383 | const layerScheduledTasksTest = ScheduledTaskService.layer.pipe( |
| 384 | Layer.provide( |
| 385 | Layer.mergeAll( |
| 386 | ManagedPolicy.layerTest(), |
| 387 | Layer.mock(ThreadLaunchService.ThreadLaunchService)({}), |
| 388 | // The orchestrator depends on this layer, so read the shell from its projection. |
| 389 | Layer.unwrap( |
| 390 | ProjectionStore.ProjectionStoreV2.pipe( |
| 391 | Effect.map(({ getThreadShell }) => |
| 392 | Layer.mock(ThreadManagementService.ThreadManagementService)({ |
| 393 | getThreadShell: (threadId) => getThreadShell(threadId).pipe(Effect.orDie), |
| 394 | }), |
| 395 | ), |
| 396 | ), |
| 397 | ).pipe(Layer.provide(ProjectionStore.layer)), |
| 398 | Layer.mock(SecretRequests.SecretRequests)({}), |
| 399 | Layer.mock(ManagedProjectFolders.ManagedProjectFolders)({ |
| 400 | namedProjectsRoot: "/projects", |
| 401 | isScratchProject: () => Effect.succeed(false), |
| 402 | }), |
| 403 | Scheduler.layer, |
| 404 | ), |
| 405 | ), |
| 406 | ); |
| 407 | const bootedSimulatorReady: DeviceHost.DeviceHostAgentReady = { |
| 408 | nodePath: process.execPath, |
| 409 | hub: { origin: "http://device.test" }, |
| 410 | agentDevice: null, |
| 411 | helpers: { serveSimAxSettings: null, serveSimCli: null }, |
| 412 | run: () => Effect.die("Thread cleanup must not run host commands"), |
| 413 | releaseAgentSession: () => Effect.void, |
| 414 | devices: { |
| 415 | list: Effect.succeed({ |
| 416 | devices: [ |
| 417 | { |
| 418 | id: "phone", |
| 419 | name: "iPhone", |
| 420 | platform: "ios", |
| 421 | version: "26", |
| 422 | booted: true, |
| 423 | physical: false, |
| 424 | }, |
| 425 | ], |
| 426 | detail: undefined, |
| 427 | }), |
| 428 | boot: () => Effect.die("The simulator is already booted"), |
| 429 | attach: () => Effect.void, |
| 430 | shutdown: () => Effect.die("Thread cleanup must keep the simulator booted"), |
| 431 | screenshot: () => Effect.die("unused screenshot"), |
| 432 | }, |
| 433 | }; |
| 434 | const bootedSimulatorHost = DeviceHost.DeviceHost.of({ |
| 435 | id: "local", |
| 436 | label: "Local", |
| 437 | summary: Effect.succeed({ |
| 438 | id: "local", |
| 439 | label: "Local", |
| 440 | kind: "local", |
| 441 | hubInstalled: true, |
| 442 | agentDeviceInstalled: true, |
| 443 | platforms: [{ platform: "ios", available: true }], |
| 444 | }), |
| 445 | platformAvailability: (platform) => Effect.succeed({ platform, available: true }), |
| 446 | ensureReady: () => Effect.succeed(bootedSimulatorReady), |
| 447 | ensureAgentReady: () => Effect.succeed(bootedSimulatorReady), |
| 448 | current: Effect.succeed(bootedSimulatorReady), |
| 449 | stopAgent: Effect.void, |
| 450 | stop: Effect.void, |
| 451 | }); |
| 452 | const layerResourceCleanupTest = ResourceCleanupService.layer.pipe( |
| 453 | Layer.provideMerge(layerScheduledTasksTest), |
| 454 | Layer.provideMerge( |
| 455 | Layer.effect( |
| 456 | DeviceService.DeviceService, |
| 457 | DeviceService.makeWithHosts(new Map([[bootedSimulatorHost.id, bootedSimulatorHost]])), |
| 458 | ), |
| 459 | ), |
| 460 | Layer.provide( |
| 461 | Layer.mergeAll( |
| 462 | Layer.mock(PreviewManager.PreviewManager)({ close: () => Effect.void }), |
| 463 | Layer.mock(TerminalManager.TerminalManager)({ close: () => Effect.void }), |
| 464 | ), |
| 465 | ), |
| 466 | ); |
| 467 | |
| 468 | const layerProjectDeletionTest = Layer.mergeAll( |
| 469 | RuntimeLayer.layer.pipe(Layer.provide(RuntimeLayer.layerProjectService)), |
| 470 | RuntimeLayer.layerProjectService, |
| 471 | RuntimeLayer.layerEventSink, |
| 472 | ProjectionStore.layer, |
| 473 | ThreadCommandExecutor.layer, |
| 474 | ).pipe( |
| 475 | Layer.provide( |
| 476 | Layer.mock(ProjectEnrichmentService.ProjectEnrichmentService)({ |
| 477 | peek: () => |
| 478 | Effect.succeed({ |
| 479 | repositoryIdentity: null, |
| 480 | faviconPath: null, |
| 481 | repositoryIdentityResolved: false, |
| 482 | }), |
| 483 | getAvailable: () => |
| 484 | Effect.succeed({ |
| 485 | repositoryIdentity: null, |
| 486 | faviconPath: null, |
| 487 | repositoryIdentityResolved: false, |
| 488 | }), |
| 489 | invalidate: () => Effect.void, |
| 490 | }), |
| 491 | ), |
| 492 | Layer.provide( |
| 493 | Layer.mock(WorkspacePaths.WorkspacePaths)({ |
| 494 | normalizeWorkspaceRoot: (workspaceRoot) => Effect.succeed(workspaceRoot), |
| 495 | }), |
| 496 | ), |
| 497 | Layer.provideMerge(layerResourceCleanupTest), |
| 498 | Layer.provide(McpSessionRegistryTestkit.layer), |
| 499 | Layer.provide(McpProviderSessions.layer), |
| 500 | Layer.provide(SqlitePersistence.layerMemory), |
| 501 | Layer.provide(layerCheckpointStoreTest), |
| 502 | // Exposed so tests can reach the attachments directory that resource cleanup removes from. |
| 503 | Layer.provideMerge(layerServerConfig), |
| 504 | Layer.provide(ServerSettings.layerTest()), |
| 505 | Layer.provide(layerTestProviderInstanceRegistry), |
| 506 | Layer.provide(layerGitWorkflowTest), |
| 507 | Layer.provideMerge(layerPlatformTest), |
| 508 | ); |
| 509 | |
| 510 | it.layer(layerProjectDeletionTest)("project deletion during thread commands", (it) => { |
| 511 | it.effect.each(["thread", "project"] as const)( |
| 512 | "deleting a %s removes its thread-bound automations through the effect worker", |
| 513 | (deletion) => |
| 514 | Effect.gen(function* () { |
| 515 | const projects = yield* ProjectService.ProjectService; |
| 516 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 517 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 518 | const tasks = yield* ScheduledTaskService.ScheduledTaskService; |
| 519 | const projectId = ProjectId.make(`automation-delete-project:${deletion}`); |
| 520 | const threadId = ThreadId.make(`automation-delete-thread:${deletion}`); |
| 521 | const otherThreadId = ThreadId.make(`automation-surviving-thread:${deletion}`); |
| 522 | const otherProjectId = ProjectId.make("automation-other-project"); |
| 523 | yield* projects.create({ |
| 524 | commandId: CommandId.make(`automation-create-project:${deletion}`), |
| 525 | projectId, |
| 526 | title: "Automation lifecycle", |
| 527 | workspaceRoot: `/work/automation-lifecycle/${deletion}`, |
| 528 | }); |
| 529 | for (const id of [threadId, otherThreadId]) { |
| 530 | yield* orchestrator.dispatch({ |
| 531 | type: "thread.create", |
| 532 | commandId: CommandId.make(`automation-create:${id}`), |
| 533 | createdBy: "user", |
| 534 | creationSource: "web", |
| 535 | threadId: id, |
| 536 | projectId: id === threadId ? projectId : otherProjectId, |
| 537 | title: "Automation target", |
| 538 | modelSelection, |
| 539 | runtimeMode: "full-access", |
| 540 | interactionMode: "default", |
| 541 | branch: null, |
| 542 | worktreePath: null, |
| 543 | }); |
| 544 | } |
| 545 | const save = (name: string, target: ThreadId | null, webhook = false) => |
| 546 | tasks.upsert({ |
| 547 | // As schedule_task sends it, so clearing the thread unbinds the task. |
| 548 | threadMode: target === null ? "new_thread" : "continue", |
| 549 | id: ScheduledTaskId.make(`automation:${deletion}:${name}`), |
| 550 | title: name, |
| 551 | prompt: "Check orchestration progress", |
| 552 | enabled: !webhook, |
| 553 | schedule: webhook ? { type: "webhook" } : { type: "interval", everyMs: 60_000 }, |
| 554 | projectId: target === otherThreadId ? otherProjectId : projectId, |
| 555 | threadId: target, |
| 556 | workspaceStrategy: { type: "root" }, |
| 557 | modelSelection, |
| 558 | runtimeMode: "full-access", |
| 559 | interactionMode: "default", |
| 560 | createdBy: "agent", |
| 561 | creationSource: "mcp", |
| 562 | }); |
| 563 | const { task: bound } = yield* save("bound", threadId); |
| 564 | const { task: webhook } = yield* save("webhook", threadId, true); |
| 565 | const { task: independent } = yield* save("independent", null); |
| 566 | yield* save("detached", threadId); |
| 567 | const { task: detached } = yield* save("detached", null); |
| 568 | yield* save("moved", threadId); |
| 569 | const { task: moved } = yield* save("moved", otherThreadId); |
| 570 | const { task: other } = yield* save("other", otherThreadId); |
| 571 | yield* tasks.triggerWebhook({ |
| 572 | hookId: webhook.id, |
| 573 | token: webhook.webhook!.path.split("/").at(-1)!, |
| 574 | method: "POST", |
| 575 | path: webhook.webhook!.path, |
| 576 | query: "", |
| 577 | headers: {}, |
| 578 | body: new Uint8Array(), |
| 579 | bodyText: "", |
| 580 | }); |
| 581 | assert.lengthOf((yield* tasks.listWebhookDeliveries({ id: webhook.id })).deliveries, 1); |
| 582 | |
| 583 | yield* orchestrator.dispatch({ |
| 584 | type: "thread.archive", |
| 585 | commandId: CommandId.make(`automation-archive:${deletion}`), |
| 586 | threadId, |
| 587 | }); |
| 588 | yield* worker.drain(); |
| 589 | assert.include( |
| 590 | (yield* tasks.list()).tasks.map((task) => task.id), |
| 591 | bound.id, |
| 592 | ); |
| 593 | |
| 594 | if (deletion === "thread") { |
| 595 | yield* orchestrator.dispatch({ |
| 596 | type: "thread.delete", |
| 597 | commandId: CommandId.make(`automation-delete:${deletion}`), |
| 598 | threadId, |
| 599 | }); |
| 600 | } else { |
| 601 | yield* projects.delete({ |
| 602 | commandId: CommandId.make(`automation-delete:${deletion}`), |
| 603 | projectId, |
| 604 | force: true, |
| 605 | }); |
| 606 | } |
| 607 | yield* worker.drain(); |
| 608 | const remainingIds = (yield* tasks.list()).tasks.map((task) => task.id); |
| 609 | assert.notInclude(remainingIds, bound.id); |
| 610 | assert.notInclude(remainingIds, webhook.id); |
| 611 | assert.includeMembers(remainingIds, [independent.id, detached.id, moved.id, other.id]); |
| 612 | assert.lengthOf((yield* tasks.listWebhookDeliveries({ id: webhook.id })).deliveries, 0); |
| 613 | const rejected = yield* tasks.runNow({ id: bound.id }).pipe(Effect.flip); |
| 614 | assert.equal(rejected.message, "Schedule task not found."); |
| 615 | assert.equal( |
| 616 | (yield* tasks.triggerWebhook({ |
| 617 | hookId: webhook.id, |
| 618 | token: webhook.webhook!.path.split("/").at(-1)!, |
| 619 | method: "POST", |
| 620 | path: webhook.webhook!.path, |
| 621 | query: "", |
| 622 | headers: {}, |
| 623 | body: new Uint8Array(), |
| 624 | bodyText: "", |
| 625 | }))._tag, |
| 626 | "not_found", |
| 627 | ); |
| 628 | // Retried cleanup is harmless and cannot affect unrelated tasks. |
| 629 | yield* tasks.deleteForThread(threadId); |
| 630 | assert.sameMembers( |
| 631 | (yield* tasks.list()).tasks.map((task) => task.id), |
| 632 | remainingIds, |
| 633 | ); |
| 634 | }), |
| 635 | ); |
| 636 | |
| 637 | it.effect.each(["archive", "thread delete", "project delete"] as const)( |
| 638 | "%s releases the thread's devices and keeps them booted", |
| 639 | (lifecycle) => |
| 640 | Effect.gen(function* () { |
| 641 | const projects = yield* ProjectService.ProjectService; |
| 642 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 643 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 644 | const devices = yield* DeviceService.DeviceService; |
| 645 | yield* devices.configure({ enabled: true, agentAccessEnabled: true }); |
| 646 | const projectId = ProjectId.make(`device-cleanup-project:${lifecycle}`); |
| 647 | const threadId = ThreadId.make(`device-cleanup-thread:${lifecycle}`); |
| 648 | const otherThreadId = ThreadId.make(`device-surviving-thread:${lifecycle}`); |
| 649 | yield* projects.create({ |
| 650 | commandId: CommandId.make(`device-cleanup-create-project:${lifecycle}`), |
| 651 | projectId, |
| 652 | title: "Device cleanup", |
| 653 | workspaceRoot: `/work/device-cleanup/${lifecycle}`, |
| 654 | }); |
| 655 | for (const id of [threadId, otherThreadId]) { |
| 656 | yield* orchestrator.dispatch({ |
| 657 | type: "thread.create", |
| 658 | commandId: CommandId.make(`device-cleanup-create:${id}`), |
| 659 | createdBy: "user", |
| 660 | creationSource: "web", |
| 661 | threadId: id, |
| 662 | projectId: id === threadId ? projectId : ProjectId.make("device-cleanup-other-project"), |
| 663 | title: "Device user", |
| 664 | modelSelection, |
| 665 | runtimeMode: "full-access", |
| 666 | interactionMode: "default", |
| 667 | branch: null, |
| 668 | worktreePath: null, |
| 669 | }); |
| 670 | yield* devices.open({ threadId: id, deviceId: "phone", platform: "ios" }); |
| 671 | } |
| 672 | const otherSessions = yield* devices.sessionsForThread(otherThreadId); |
| 673 | |
| 674 | const commandId = CommandId.make(`device-cleanup:${lifecycle}`); |
| 675 | if (lifecycle === "project delete") { |
| 676 | yield* projects.delete({ commandId, projectId, force: true }); |
| 677 | } else { |
| 678 | yield* orchestrator.dispatch({ |
| 679 | type: lifecycle === "archive" ? "thread.archive" : "thread.delete", |
| 680 | commandId, |
| 681 | threadId, |
| 682 | }); |
| 683 | } |
| 684 | yield* worker.drain(); |
| 685 | |
| 686 | assert.deepEqual(yield* devices.sessionsForThread(threadId), []); |
| 687 | assert.deepEqual(yield* devices.sessionsForThread(otherThreadId), otherSessions); |
| 688 | assert.isTrue((yield* devices.state).devices[0]?.booted); |
| 689 | }), |
| 690 | ); |
| 691 | |
| 692 | it.effect.each(["no fork", "a live fork"] as const)( |
| 693 | "a queued edit's cleanup keeps a file earlier history still shows, with %s", |
| 694 | (fork) => |
| 695 | Effect.gen(function* () { |
| 696 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 697 | const eventSink = yield* EventSink.EventSinkV2; |
| 698 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 699 | const config = yield* ServerConfig.ServerConfig; |
| 700 | const fileSystem = yield* FileSystem.FileSystem; |
| 701 | const path = yield* Path.Path; |
| 702 | const key = `queued-edit-shared-file-${fork.replaceAll(" ", "-")}`; |
| 703 | const threadId = ThreadId.make(key); |
| 704 | const storedFile = (name: string) => |
| 705 | ({ |
| 706 | type: "image", |
| 707 | id: createAttachmentId(threadId)!, |
| 708 | name: `${name}.png`, |
| 709 | mimeType: "image/png", |
| 710 | sizeBytes: 5, |
| 711 | }) as const; |
| 712 | const shared = storedFile("shared"); |
| 713 | const dropped = storedFile("dropped"); |
| 714 | const filePath = (file: typeof shared) => |
| 715 | path.join(config.attachmentsDir, `${file.id}.png`); |
| 716 | yield* fileSystem.writeFileString(filePath(shared), "bytes"); |
| 717 | yield* fileSystem.writeFileString(filePath(dropped), "bytes"); |
| 718 | yield* orchestrator.dispatch({ |
| 719 | type: "thread.create", |
| 720 | commandId: CommandId.make(`${key}:create`), |
| 721 | createdBy: "user", |
| 722 | creationSource: "web", |
| 723 | threadId, |
| 724 | projectId: ProjectId.make("queued-edit-shared-file-project"), |
| 725 | title: "Shared file", |
| 726 | modelSelection, |
| 727 | runtimeMode: "full-access", |
| 728 | interactionMode: "default", |
| 729 | branch: null, |
| 730 | worktreePath: null, |
| 731 | }); |
| 732 | const writeRun = Effect.fn("test.writeQueuedEditRun")(function* (input: { |
| 733 | readonly name: string; |
| 734 | readonly ordinal: number; |
| 735 | readonly status: "completed" | "queued"; |
| 736 | readonly attachments: ReadonlyArray<typeof shared>; |
| 737 | }) { |
| 738 | const now = yield* DateTime.now; |
| 739 | const runId = RunId.make(`${key}:${input.name}`); |
| 740 | const messageId = MessageId.make(`${key}:${input.name}`); |
| 741 | const completed = input.status === "completed"; |
| 742 | yield* eventSink.write({ |
| 743 | events: [ |
| 744 | { |
| 745 | id: EventId.make(`${key}:${input.name}:run`), |
| 746 | type: "run.created", |
| 747 | threadId, |
| 748 | runId, |
| 749 | occurredAt: now, |
| 750 | payload: { |
| 751 | id: runId, |
| 752 | threadId, |
| 753 | ordinal: input.ordinal, |
| 754 | providerInstanceId: modelSelection.instanceId, |
| 755 | modelSelection, |
| 756 | providerThreadId: null, |
| 757 | userMessageId: messageId, |
| 758 | rootNodeId: null, |
| 759 | activeAttemptId: null, |
| 760 | status: input.status, |
| 761 | requestedAt: now, |
| 762 | startedAt: completed ? now : null, |
| 763 | completedAt: completed ? now : null, |
| 764 | checkpointId: null, |
| 765 | contextHandoffId: null, |
| 766 | }, |
| 767 | }, |
| 768 | { |
| 769 | id: EventId.make(`${key}:${input.name}:message`), |
| 770 | type: "message.updated", |
| 771 | threadId, |
| 772 | runId, |
| 773 | occurredAt: now, |
| 774 | payload: { |
| 775 | id: messageId, |
| 776 | threadId, |
| 777 | runId, |
| 778 | nodeId: null, |
| 779 | role: "user", |
| 780 | text: `Send ${input.name} work`, |
| 781 | attachments: input.attachments, |
| 782 | streaming: false, |
| 783 | createdBy: "user", |
| 784 | creationSource: "web", |
| 785 | createdAt: now, |
| 786 | updatedAt: now, |
| 787 | }, |
| 788 | }, |
| 789 | ], |
| 790 | }); |
| 791 | return runId; |
| 792 | }); |
| 793 | |
| 794 | const sourceRunId = yield* writeRun({ |
| 795 | name: "source", |
| 796 | ordinal: 1, |
| 797 | status: "completed", |
| 798 | attachments: [shared], |
| 799 | }); |
| 800 | if (fork !== "no fork") { |
| 801 | const forkThreadId = ThreadId.make(`${key}:fork`); |
| 802 | yield* orchestrator.dispatch({ |
| 803 | type: "thread.fork", |
| 804 | commandId: CommandId.make(`${key}:fork`), |
| 805 | sourceThreadId: threadId, |
| 806 | targetThreadId: forkThreadId, |
| 807 | sourcePoint: { type: "run", runId: sourceRunId }, |
| 808 | createdBy: "user", |
| 809 | creationSource: "web", |
| 810 | }); |
| 811 | } |
| 812 | const queuedRunId = yield* writeRun({ |
| 813 | name: "queued", |
| 814 | ordinal: 2, |
| 815 | status: "queued", |
| 816 | attachments: [shared, dropped], |
| 817 | }); |
| 818 | yield* orchestrator.dispatch({ |
| 819 | type: "queued-run.edit", |
| 820 | commandId: CommandId.make(`${key}:edit`), |
| 821 | threadId, |
| 822 | runId: queuedRunId, |
| 823 | text: "Send queued work without files", |
| 824 | attachments: [], |
| 825 | }); |
| 826 | yield* worker.drain(); |
| 827 | |
| 828 | assert.isFalse(yield* fileSystem.exists(filePath(dropped))); |
| 829 | assert.equal(yield* fileSystem.readFileString(filePath(shared)), "bytes"); |
| 830 | |
| 831 | if (fork !== "no fork") { |
| 832 | const laterRunId = yield* writeRun({ |
| 833 | name: "later", |
| 834 | ordinal: 3, |
| 835 | status: "completed", |
| 836 | attachments: [shared], |
| 837 | }); |
| 838 | const laterForkId = ThreadId.make(`${key}:later-fork`); |
| 839 | yield* orchestrator.dispatch({ |
| 840 | type: "thread.fork", |
| 841 | commandId: CommandId.make(`${key}:later-fork`), |
| 842 | sourceThreadId: threadId, |
| 843 | targetThreadId: laterForkId, |
| 844 | sourcePoint: { type: "run", runId: laterRunId }, |
| 845 | createdBy: "user", |
| 846 | creationSource: "web", |
| 847 | }); |
| 848 | for (const id of [threadId, laterForkId]) { |
| 849 | yield* orchestrator.dispatch({ |
| 850 | type: "thread.delete", |
| 851 | commandId: CommandId.make(`${key}:delete:${id}`), |
| 852 | threadId: id, |
| 853 | }); |
| 854 | yield* worker.drain(); |
| 855 | assert.equal(yield* fileSystem.readFileString(filePath(shared)), "bytes"); |
| 856 | } |
| 857 | yield* orchestrator.dispatch({ |
| 858 | type: "thread.delete", |
| 859 | commandId: CommandId.make(`${key}:delete-first-fork`), |
| 860 | threadId: ThreadId.make(`${key}:fork`), |
| 861 | }); |
| 862 | yield* worker.drain(); |
| 863 | assert.isFalse(yield* fileSystem.exists(filePath(shared))); |
| 864 | } |
| 865 | }).pipe(Effect.provide(NodeServices.layer)), |
| 866 | ); |
| 867 | |
| 868 | it.effect("waits for an in-flight thread update before planning deletion", () => |
| 869 | Effect.gen(function* () { |
| 870 | const projects = yield* ProjectService.ProjectService; |
| 871 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 872 | const eventSink = yield* EventSink.EventSinkV2; |
| 873 | const executor = yield* ThreadCommandExecutor.ThreadCommandExecutor; |
| 874 | const projectId = ProjectId.make("runtime-project-delete-concurrent"); |
| 875 | const threadId = ThreadId.make("runtime-thread-delete-concurrent"); |
| 876 | const updateCommandId = CommandId.make("runtime-thread-delete-concurrent-update"); |
| 877 | yield* projects.create({ |
| 878 | commandId: CommandId.make("runtime-project-delete-concurrent-create"), |
| 879 | projectId, |
| 880 | title: "Concurrent deletion", |
| 881 | workspaceRoot: "/work/concurrent-deletion", |
| 882 | }); |
| 883 | yield* orchestrator.dispatch({ |
| 884 | type: "thread.create", |
| 885 | commandId: CommandId.make("runtime-thread-delete-concurrent-create"), |
| 886 | createdBy: "user", |
| 887 | creationSource: "web", |
| 888 | threadId, |
| 889 | projectId, |
| 890 | title: "Original title", |
| 891 | modelSelection, |
| 892 | runtimeMode: "full-access", |
| 893 | interactionMode: "default", |
| 894 | branch: null, |
| 895 | worktreePath: null, |
| 896 | }); |
| 897 | |
| 898 | const updateReady = yield* Deferred.make<void>(); |
| 899 | const releaseUpdate = yield* Deferred.make<void>(); |
| 900 | const deletionQueued = yield* Deferred.make<void>(); |
| 901 | const commitCommand = eventSink.commitCommand; |
| 902 | const withLock = executor.withLock; |
| 903 | let threadLockRequests = 0; |
| 904 | const commitSpy = vi |
| 905 | .spyOn(eventSink, "commitCommand") |
| 906 | .mockImplementation((input) => |
| 907 | input.commandId === updateCommandId |
| 908 | ? Deferred.succeed(updateReady, undefined).pipe( |
| 909 | Effect.andThen(Deferred.await(releaseUpdate)), |
| 910 | Effect.andThen(commitCommand(input)), |
| 911 | ) |
| 912 | : commitCommand(input), |
| 913 | ); |
| 914 | const observeLock: ThreadCommandExecutor.ThreadCommandExecutor["Service"]["withLock"] = ( |
| 915 | key, |
| 916 | effect, |
| 917 | ) => { |
| 918 | if (key !== threadId || ++threadLockRequests !== 2) return withLock(key, effect); |
| 919 | return Deferred.succeed(deletionQueued, undefined).pipe( |
| 920 | Effect.andThen(withLock(key, effect)), |
| 921 | ); |
| 922 | }; |
| 923 | const lockSpy = vi.spyOn(executor, "withLock").mockImplementation(observeLock); |
| 924 | yield* Effect.gen(function* () { |
| 925 | const updateFiber = yield* orchestrator |
| 926 | .dispatch({ |
| 927 | type: "thread.metadata.update", |
| 928 | commandId: updateCommandId, |
| 929 | threadId, |
| 930 | title: "Updated before deletion", |
| 931 | }) |
| 932 | .pipe(Effect.forkChild({ startImmediately: true })); |
| 933 | yield* Effect.raceFirst( |
| 934 | Deferred.await(updateReady), |
| 935 | Fiber.join(updateFiber).pipe( |
| 936 | Effect.andThen(Effect.die("The update completed before reaching its commit barrier.")), |
| 937 | ), |
| 938 | ); |
| 939 | const deleteFiber = yield* projects |
| 940 | .delete({ |
| 941 | commandId: CommandId.make("runtime-project-delete-concurrent-delete"), |
| 942 | projectId, |
| 943 | force: true, |
| 944 | }) |
| 945 | .pipe(Effect.forkChild({ startImmediately: true })); |
| 946 | yield* Effect.raceFirst( |
| 947 | Deferred.await(deletionQueued), |
| 948 | Fiber.join(deleteFiber).pipe( |
| 949 | Effect.andThen(Effect.die("Project deletion bypassed the in-flight thread command.")), |
| 950 | ), |
| 951 | ); |
| 952 | yield* Deferred.succeed(releaseUpdate, undefined); |
| 953 | yield* Fiber.join(updateFiber); |
| 954 | const deletedProject = yield* Fiber.join(deleteFiber); |
| 955 | const projection = yield* orchestrator.getThreadProjection(threadId); |
| 956 | assert.isNotNull(deletedProject.deletedAt); |
| 957 | assert.isNotNull(projection.thread.deletedAt); |
| 958 | assert.equal(projection.thread.title, "Updated before deletion"); |
| 959 | }).pipe( |
| 960 | Effect.ensuring( |
| 961 | Effect.sync(() => { |
| 962 | commitSpy.mockRestore(); |
| 963 | lockSpy.mockRestore(); |
| 964 | }), |
| 965 | ), |
| 966 | ); |
| 967 | }), |
| 968 | ); |
| 969 | |
| 970 | it.effect( |
| 971 | "deletes a side chat with its parent only after the side chat's in-flight command", |
| 972 | () => |
| 973 | Effect.gen(function* () { |
| 974 | const projects = yield* ProjectService.ProjectService; |
| 975 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 976 | const eventSink = yield* EventSink.EventSinkV2; |
| 977 | const executor = yield* ThreadCommandExecutor.ThreadCommandExecutor; |
| 978 | const projectId = ProjectId.make("runtime-side-chat-delete"); |
| 979 | const parentId = ThreadId.make("runtime-side-chat-delete-parent"); |
| 980 | const sideChatId = ThreadId.make("runtime-side-chat-delete-child"); |
| 981 | const updateCommandId = CommandId.make("runtime-side-chat-delete-update"); |
| 982 | yield* projects.create({ |
| 983 | commandId: CommandId.make("runtime-side-chat-delete-project"), |
| 984 | projectId, |
| 985 | title: "Side chat deletion", |
| 986 | workspaceRoot: "/work/side-chat-deletion", |
| 987 | }); |
| 988 | yield* orchestrator.dispatch({ |
| 989 | type: "thread.create", |
| 990 | commandId: CommandId.make("runtime-side-chat-delete-create"), |
| 991 | createdBy: "user", |
| 992 | creationSource: "web", |
| 993 | threadId: parentId, |
| 994 | projectId, |
| 995 | title: "Parent", |
| 996 | modelSelection, |
| 997 | runtimeMode: "full-access", |
| 998 | interactionMode: "default", |
| 999 | branch: null, |
| 1000 | worktreePath: null, |
| 1001 | }); |
| 1002 | yield* orchestrator.dispatch({ |
| 1003 | type: "thread.fork", |
| 1004 | commandId: CommandId.make("runtime-side-chat-delete-fork"), |
| 1005 | sourceThreadId: parentId, |
| 1006 | targetThreadId: sideChatId, |
| 1007 | sourcePoint: { type: "latest_stable" }, |
| 1008 | sideChat: true, |
| 1009 | createdBy: "user", |
| 1010 | creationSource: "web", |
| 1011 | }); |
| 1012 | |
| 1013 | const updateReady = yield* Deferred.make<void>(); |
| 1014 | const releaseUpdate = yield* Deferred.make<void>(); |
| 1015 | const deletionQueued = yield* Deferred.make<void>(); |
| 1016 | const commitCommand = eventSink.commitCommand; |
| 1017 | const withLock = executor.withLock; |
| 1018 | let sideChatLockRequests = 0; |
| 1019 | const commitSpy = vi |
| 1020 | .spyOn(eventSink, "commitCommand") |
| 1021 | .mockImplementation((input) => |
| 1022 | input.commandId === updateCommandId |
| 1023 | ? Deferred.succeed(updateReady, undefined).pipe( |
| 1024 | Effect.andThen(Deferred.await(releaseUpdate)), |
| 1025 | Effect.andThen(commitCommand(input)), |
| 1026 | ) |
| 1027 | : commitCommand(input), |
| 1028 | ); |
| 1029 | const observeLock: ThreadCommandExecutor.ThreadCommandExecutor["Service"]["withLock"] = ( |
| 1030 | key, |
| 1031 | effect, |
| 1032 | ) => { |
| 1033 | if (key !== sideChatId || ++sideChatLockRequests !== 2) return withLock(key, effect); |
| 1034 | return Deferred.succeed(deletionQueued, undefined).pipe( |
| 1035 | Effect.andThen(withLock(key, effect)), |
| 1036 | ); |
| 1037 | }; |
| 1038 | const lockSpy = vi.spyOn(executor, "withLock").mockImplementation(observeLock); |
| 1039 | yield* Effect.gen(function* () { |
| 1040 | const updateFiber = yield* orchestrator |
| 1041 | .dispatch({ |
| 1042 | type: "thread.metadata.update", |
| 1043 | commandId: updateCommandId, |
| 1044 | threadId: sideChatId, |
| 1045 | title: "Updated before deletion", |
| 1046 | }) |
| 1047 | .pipe(Effect.forkChild({ startImmediately: true })); |
| 1048 | yield* Effect.raceFirst( |
| 1049 | Deferred.await(updateReady), |
| 1050 | Fiber.join(updateFiber).pipe( |
| 1051 | Effect.andThen( |
| 1052 | Effect.die("The update completed before reaching its commit barrier."), |
| 1053 | ), |
| 1054 | ), |
| 1055 | ); |
| 1056 | const deleteFiber = yield* orchestrator |
| 1057 | .dispatch({ |
| 1058 | type: "thread.delete", |
| 1059 | commandId: CommandId.make("runtime-side-chat-delete-parent"), |
| 1060 | threadId: parentId, |
| 1061 | }) |
| 1062 | .pipe(Effect.forkChild({ startImmediately: true })); |
| 1063 | yield* Effect.raceFirst( |
| 1064 | Deferred.await(deletionQueued), |
| 1065 | Fiber.join(deleteFiber).pipe( |
| 1066 | Effect.andThen(Effect.die("Parent deletion bypassed the side chat's command.")), |
| 1067 | ), |
| 1068 | ); |
| 1069 | yield* Deferred.succeed(releaseUpdate, undefined); |
| 1070 | yield* Fiber.join(updateFiber); |
| 1071 | yield* Fiber.join(deleteFiber); |
| 1072 | const sideChat = yield* orchestrator.getThreadProjection(sideChatId); |
| 1073 | assert.isNotNull(sideChat.thread.deletedAt); |
| 1074 | assert.equal(sideChat.thread.title, "Updated before deletion"); |
| 1075 | }).pipe( |
| 1076 | Effect.ensuring( |
| 1077 | Effect.sync(() => { |
| 1078 | commitSpy.mockRestore(); |
| 1079 | lockSpy.mockRestore(); |
| 1080 | }), |
| 1081 | ), |
| 1082 | ); |
| 1083 | }), |
| 1084 | ); |
| 1085 | |
| 1086 | // A fork from a side chat onto its parent used to lock the side chat, then the parent: the |
| 1087 | // reverse of the parent delete's order. |
| 1088 | it.effect("a parent delete and a fork from its side chat onto the parent do not deadlock", () => |
| 1089 | Effect.gen(function* () { |
| 1090 | const projects = yield* ProjectService.ProjectService; |
| 1091 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 1092 | const executor = yield* ThreadCommandExecutor.ThreadCommandExecutor; |
| 1093 | const projectId = ProjectId.make("runtime-side-chat-lock-order"); |
| 1094 | const parentId = ThreadId.make("runtime-side-chat-lock-order-parent"); |
| 1095 | const sideChatId = ThreadId.make("runtime-side-chat-lock-order-child"); |
| 1096 | yield* projects.create({ |
| 1097 | commandId: CommandId.make("runtime-side-chat-lock-order-project"), |
| 1098 | projectId, |
| 1099 | title: "Side chat lock order", |
| 1100 | workspaceRoot: "/work/side-chat-lock-order", |
| 1101 | }); |
| 1102 | yield* orchestrator.dispatch({ |
| 1103 | type: "thread.create", |
| 1104 | commandId: CommandId.make("runtime-side-chat-lock-order-create"), |
| 1105 | createdBy: "user", |
| 1106 | creationSource: "web", |
| 1107 | threadId: parentId, |
| 1108 | projectId, |
| 1109 | title: "Parent", |
| 1110 | modelSelection, |
| 1111 | runtimeMode: "full-access", |
| 1112 | interactionMode: "default", |
| 1113 | branch: null, |
| 1114 | worktreePath: null, |
| 1115 | }); |
| 1116 | yield* orchestrator.dispatch({ |
| 1117 | type: "thread.fork", |
| 1118 | commandId: CommandId.make("runtime-side-chat-lock-order-fork"), |
| 1119 | sourceThreadId: parentId, |
| 1120 | targetThreadId: sideChatId, |
| 1121 | sourcePoint: { type: "latest_stable" }, |
| 1122 | sideChat: true, |
| 1123 | createdBy: "user", |
| 1124 | creationSource: "web", |
| 1125 | }); |
| 1126 | |
| 1127 | const deleteHoldsParent = yield* Deferred.make<void>(); |
| 1128 | const releaseDelete = yield* Deferred.make<void>(); |
| 1129 | const forkWaitsForParent = yield* Deferred.make<void>(); |
| 1130 | const withLock = executor.withLock; |
| 1131 | let parentLockRequests = 0; |
| 1132 | const observeLock: ThreadCommandExecutor.ThreadCommandExecutor["Service"]["withLock"] = ( |
| 1133 | key, |
| 1134 | effect, |
| 1135 | ) => { |
| 1136 | if (key !== parentId) return withLock(key, effect); |
| 1137 | // Dispatch builds locks it may not run, so count when one runs. |
| 1138 | return Effect.suspend(() => |
| 1139 | ++parentLockRequests === 1 |
| 1140 | ? withLock( |
| 1141 | key, |
| 1142 | Deferred.succeed(deleteHoldsParent, undefined).pipe( |
| 1143 | Effect.andThen(Deferred.await(releaseDelete)), |
| 1144 | Effect.andThen(effect), |
| 1145 | ), |
| 1146 | ) |
| 1147 | : Deferred.succeed(forkWaitsForParent, undefined).pipe( |
| 1148 | Effect.andThen(withLock(key, effect)), |
| 1149 | ), |
| 1150 | ); |
| 1151 | }; |
| 1152 | const lockSpy = vi.spyOn(executor, "withLock").mockImplementation(observeLock); |
| 1153 | yield* Effect.gen(function* () { |
| 1154 | const deleteFiber = yield* orchestrator |
| 1155 | .dispatch({ |
| 1156 | type: "thread.delete", |
| 1157 | commandId: CommandId.make("runtime-side-chat-lock-order-delete"), |
| 1158 | threadId: parentId, |
| 1159 | }) |
| 1160 | .pipe(Effect.forkChild({ startImmediately: true })); |
| 1161 | yield* Deferred.await(deleteHoldsParent); |
| 1162 | const forkFiber = yield* orchestrator |
| 1163 | .dispatch({ |
| 1164 | type: "thread.fork", |
| 1165 | commandId: CommandId.make("runtime-side-chat-lock-order-fork-back"), |
| 1166 | sourceThreadId: sideChatId, |
| 1167 | targetThreadId: parentId, |
| 1168 | sourcePoint: { type: "latest_stable" }, |
| 1169 | sideChat: true, |
| 1170 | createdBy: "user", |
| 1171 | creationSource: "web", |
| 1172 | }) |
| 1173 | .pipe(Effect.flip, Effect.forkChild({ startImmediately: true })); |
| 1174 | yield* Deferred.await(forkWaitsForParent); |
| 1175 | yield* Deferred.succeed(releaseDelete, undefined); |
| 1176 | yield* Fiber.join(deleteFiber); |
| 1177 | yield* Fiber.join(forkFiber); |
| 1178 | assert.isNotNull((yield* orchestrator.getThreadProjection(parentId)).thread.deletedAt); |
| 1179 | assert.isNotNull((yield* orchestrator.getThreadProjection(sideChatId)).thread.deletedAt); |
| 1180 | }).pipe(Effect.ensuring(Effect.sync(() => lockSpy.mockRestore()))); |
| 1181 | }), |
| 1182 | ); |
| 1183 | |
| 1184 | it.effect("deleted sources keep the html_render pages any live fork shows", () => |
| 1185 | Effect.gen(function* () { |
| 1186 | const projects = yield* ProjectService.ProjectService; |
| 1187 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 1188 | const projections = yield* ProjectionStore.ProjectionStoreV2; |
| 1189 | const executor = yield* ThreadCommandExecutor.ThreadCommandExecutor; |
| 1190 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 1191 | const fileSystem = yield* FileSystem.FileSystem; |
| 1192 | const projectId = ProjectId.make("html-fork-project"); |
| 1193 | const sourceThreadId = ThreadId.make("html-fork-source"); |
| 1194 | const forkThreadId = ThreadId.make("html-fork-target"); |
| 1195 | yield* projects.create({ |
| 1196 | commandId: CommandId.make("html-fork-project"), |
| 1197 | projectId, |
| 1198 | title: "Page family", |
| 1199 | workspaceRoot: "/work/html-fork", |
| 1200 | }); |
| 1201 | yield* orchestrator.dispatch({ |
| 1202 | type: "thread.create", |
| 1203 | commandId: CommandId.make("html-fork-create"), |
| 1204 | threadId: sourceThreadId, |
| 1205 | projectId, |
| 1206 | title: "Page source", |
| 1207 | modelSelection, |
| 1208 | runtimeMode: "full-access", |
| 1209 | interactionMode: "default", |
| 1210 | branch: null, |
| 1211 | worktreePath: null, |
| 1212 | createdBy: "user", |
| 1213 | creationSource: "web", |
| 1214 | }); |
| 1215 | const inherited = yield* writeHtmlRenderRun({ |
| 1216 | threadId: sourceThreadId, |
| 1217 | key: "inherited", |
| 1218 | ordinal: 1, |
| 1219 | }); |
| 1220 | const fork = (sourceThreadId: ThreadId, runId: RunId, targetThreadId: ThreadId) => |
| 1221 | orchestrator.dispatch({ |
| 1222 | type: "thread.fork", |
| 1223 | commandId: CommandId.make(`html-fork:${targetThreadId}`), |
| 1224 | sourceThreadId, |
| 1225 | targetThreadId, |
| 1226 | sourcePoint: { type: "run", runId }, |
| 1227 | createdBy: "user", |
| 1228 | creationSource: "web", |
| 1229 | }); |
| 1230 | yield* fork(sourceThreadId, inherited.runId, forkThreadId); |
| 1231 | const forkPage = yield* writeHtmlRenderRun({ |
| 1232 | threadId: forkThreadId, |
| 1233 | key: "fork", |
| 1234 | ordinal: 1, |
| 1235 | }); |
| 1236 | const [first, second] = [ThreadId.make("html-fork-first"), ThreadId.make("html-fork-second")]; |
| 1237 | yield* fork(forkThreadId, forkPage.runId, first); |
| 1238 | // Project deletion goes newest first, so it reaches `second` before `first`. |
| 1239 | yield* TestClock.adjust("1 second"); |
| 1240 | yield* fork(forkThreadId, forkPage.runId, second); |
| 1241 | const firstRun = yield* writeHtmlRenderRun({ threadId: first, key: "first", ordinal: 1 }); |
| 1242 | const later = yield* writeHtmlRenderRun({ |
| 1243 | threadId: sourceThreadId, |
| 1244 | key: "later", |
| 1245 | ordinal: 2, |
| 1246 | }); |
| 1247 | const deleteThread = (threadId: ThreadId) => |
| 1248 | orchestrator.dispatch({ |
| 1249 | type: "thread.delete", |
| 1250 | commandId: CommandId.make(`html-fork-delete:${threadId}`), |
| 1251 | threadId, |
| 1252 | }); |
| 1253 | const pagesOnDisk = Effect.gen(function* () { |
| 1254 | yield* worker.drain(); |
| 1255 | return yield* Effect.forEach([inherited, forkPage, later], (page) => |
| 1256 | fileSystem.exists(page.pagePath), |
| 1257 | ); |
| 1258 | }); |
| 1259 | yield* deleteThread(forkThreadId); |
| 1260 | yield* deleteThread(sourceThreadId); |
| 1261 | assert.deepEqual(yield* pagesOnDisk, [true, true, false]); |
| 1262 | |
| 1263 | // `first`'s delete pauses after reading what to free. A fork of it and the project delete, |
| 1264 | // which reaches `second`, must each wait on a lock it holds rather than decide on the same |
| 1265 | // state; `blocked` hears each request for one. |
| 1266 | const firstPlanned = yield* Deferred.make<void>(); |
| 1267 | const releaseFirst = yield* Deferred.make<void>(); |
| 1268 | const blocked = yield* Queue.unbounded<void>(); |
| 1269 | const readAttachmentIds = projections.getThreadAttachmentIds; |
| 1270 | const attachmentSpy = vi |
| 1271 | .spyOn(projections, "getThreadAttachmentIds") |
| 1272 | .mockImplementation((threadId) => |
| 1273 | threadId === first |
| 1274 | ? readAttachmentIds(threadId).pipe( |
| 1275 | Effect.tap(() => Deferred.succeed(firstPlanned, undefined)), |
| 1276 | Effect.tap(() => Deferred.await(releaseFirst)), |
| 1277 | ) |
| 1278 | : readAttachmentIds(threadId), |
| 1279 | ); |
| 1280 | const deletionHeld = yield* Ref.make(false); |
| 1281 | const requestsHeld = (held: Effect.Effect<boolean>) => |
| 1282 | Effect.all([Deferred.isDone(firstPlanned), held]).pipe( |
| 1283 | Effect.flatMap(([planned, isHeld]) => |
| 1284 | planned && isHeld ? Queue.offer(blocked, undefined) : Effect.void, |
| 1285 | ), |
| 1286 | ); |
| 1287 | const { withLock, withDeletionLock } = executor; |
| 1288 | const observeLock: ThreadCommandExecutor.ThreadCommandExecutor["Service"]["withLock"] = ( |
| 1289 | key, |
| 1290 | effect, |
| 1291 | ) => requestsHeld(Effect.succeed(key === first)).pipe(Effect.andThen(withLock(key, effect))); |
| 1292 | const observeDeletion: ThreadCommandExecutor.ThreadCommandExecutor["Service"]["withDeletionLock"] = |
| 1293 | (effect) => |
| 1294 | requestsHeld(Ref.get(deletionHeld)).pipe( |
| 1295 | Effect.andThen( |
| 1296 | withDeletionLock( |
| 1297 | Ref.set(deletionHeld, true).pipe( |
| 1298 | Effect.andThen(effect), |
| 1299 | Effect.ensuring(Ref.set(deletionHeld, false)), |
| 1300 | ), |
| 1301 | ), |
| 1302 | ), |
| 1303 | ); |
| 1304 | const lockSpy = vi.spyOn(executor, "withLock").mockImplementation(observeLock); |
| 1305 | const deletionSpy = vi |
| 1306 | .spyOn(executor, "withDeletionLock") |
| 1307 | .mockImplementation(observeDeletion); |
| 1308 | const forkOfDeleted = yield* Effect.gen(function* () { |
| 1309 | const firstFiber = yield* deleteThread(first).pipe( |
| 1310 | Effect.forkChild({ startImmediately: true }), |
| 1311 | ); |
| 1312 | yield* Deferred.await(firstPlanned); |
| 1313 | const forkFiber = yield* fork(first, firstRun.runId, ThreadId.make("html-fork-late")).pipe( |
| 1314 | Effect.match({ |
| 1315 | onSuccess: () => "created", |
| 1316 | onFailure: (error) => ("cause" in error ? String(error.cause) : error._tag), |
| 1317 | }), |
| 1318 | Effect.forkChild({ startImmediately: true }), |
| 1319 | ); |
| 1320 | yield* Effect.raceFirst(Queue.take(blocked), Fiber.await(forkFiber)); |
| 1321 | const projectFiber = yield* projects |
| 1322 | .delete({ commandId: CommandId.make("html-fork-project-delete"), projectId, force: true }) |
| 1323 | .pipe(Effect.forkChild({ startImmediately: true })); |
| 1324 | yield* Effect.raceFirst(Queue.take(blocked), Fiber.await(projectFiber)); |
| 1325 | yield* Deferred.succeed(releaseFirst, undefined); |
| 1326 | yield* Fiber.join(firstFiber); |
| 1327 | yield* Fiber.join(projectFiber); |
| 1328 | return yield* Fiber.join(forkFiber); |
| 1329 | }).pipe( |
| 1330 | Effect.ensuring( |
| 1331 | Effect.sync(() => { |
| 1332 | lockSpy.mockRestore(); |
| 1333 | deletionSpy.mockRestore(); |
| 1334 | attachmentSpy.mockRestore(); |
| 1335 | }), |
| 1336 | ), |
| 1337 | ); |
| 1338 | assert.deepEqual( |
| 1339 | { afterLastForks: yield* pagesOnDisk, forkOfDeleted }, |
| 1340 | { afterLastForks: [false, false, false], forkOfDeleted: `Thread ${first} was deleted.` }, |
| 1341 | ); |
| 1342 | }), |
| 1343 | ); |
| 1344 | }); |
| 1345 | |
| 1346 | const layerSharedApplicationDataPlaneTest = Layer.mergeAll( |
| 1347 | layerProjectionReplayTest, |
| 1348 | RuntimeLayer.layer.pipe(Layer.provide(RuntimeLayer.layerProjectService)), |
| 1349 | RuntimeLayer.layerProjectService, |
| 1350 | RuntimeLayer.layerEventSink, |
| 1351 | RuntimeLayer.layerEventInfrastructure, |
| 1352 | ).pipe( |
| 1353 | Layer.provide( |
| 1354 | Layer.mock(WorkspacePaths.WorkspacePaths)({ |
| 1355 | normalizeWorkspaceRoot: (workspaceRoot) => Effect.succeed(workspaceRoot), |
| 1356 | }), |
| 1357 | ), |
| 1358 | Layer.provide( |
| 1359 | Layer.succeed(ProjectEnrichmentService.ProjectEnrichmentService, { |
| 1360 | peek: () => |
| 1361 | Effect.succeed({ |
| 1362 | repositoryIdentity: null, |
| 1363 | faviconPath: null, |
| 1364 | repositoryIdentityResolved: false, |
| 1365 | }), |
| 1366 | request: () => Effect.void, |
| 1367 | readRepositoryIdentity: () => Effect.succeed(null), |
| 1368 | getAvailable: () => |
| 1369 | Effect.succeed({ |
| 1370 | repositoryIdentity: null, |
| 1371 | faviconPath: null, |
| 1372 | repositoryIdentityResolved: false, |
| 1373 | }), |
| 1374 | invalidate: () => Effect.void, |
| 1375 | refreshRepositoryIdentity: () => Effect.void, |
| 1376 | subscribeChanges: Effect.never, |
| 1377 | }), |
| 1378 | ), |
| 1379 | Layer.provide(McpSessionRegistryTestkit.layer), |
| 1380 | Layer.provide(McpProviderSessions.layer), |
| 1381 | Layer.provideMerge(SqlitePersistence.layerMemory), |
| 1382 | Layer.provide(layerCheckpointStoreTest), |
| 1383 | Layer.provide(layerServerConfig), |
| 1384 | Layer.provide(ServerSettings.layerTest()), |
| 1385 | Layer.provide(layerTestProviderInstanceRegistry), |
| 1386 | Layer.provide(layerGitWorkflowTest), |
| 1387 | Layer.provide(layerPlatformTest), |
| 1388 | ); |
| 1389 | |
| 1390 | it.layer(layerTest)("OrchestrationV2LayerLive", (it) => { |
| 1391 | const seedFailingRollbackThread = (name: string) => |
| 1392 | Effect.gen(function* () { |
| 1393 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 1394 | const eventSink = yield* EventSink.EventSinkV2; |
| 1395 | const outbox = yield* EffectOutbox.EffectOutboxV2; |
| 1396 | const threadId = ThreadId.make(name); |
| 1397 | yield* orchestrator.dispatch({ |
| 1398 | type: "thread.create", |
| 1399 | createdBy: "user", |
| 1400 | creationSource: "web", |
| 1401 | commandId: CommandId.make(`${name}-create`), |
| 1402 | threadId, |
| 1403 | projectId: ProjectId.make(`${name}-project`), |
| 1404 | title: "Rollback failure", |
| 1405 | modelSelection, |
| 1406 | runtimeMode: "full-access", |
| 1407 | interactionMode: "default", |
| 1408 | branch: null, |
| 1409 | worktreePath: `/tmp/t3-${name}`, |
| 1410 | }); |
| 1411 | yield* orchestrator.dispatch({ |
| 1412 | type: "message.dispatch", |
| 1413 | createdBy: "user", |
| 1414 | creationSource: "web", |
| 1415 | commandId: CommandId.make(`${name}-message`), |
| 1416 | threadId, |
| 1417 | messageId: MessageId.make(`${name}-message`), |
| 1418 | text: "Create the provider thread and checkpoint scope.", |
| 1419 | attachments: [], |
| 1420 | dispatchMode: { type: "start_immediately" }, |
| 1421 | }); |
| 1422 | const scope = (yield* orchestrator.getThreadProjection(threadId)).checkpointScopes[0]!; |
| 1423 | const now = yield* DateTime.now; |
| 1424 | const checkpointId = CheckpointId.make(`${name}-checkpoint`); |
| 1425 | yield* eventSink.write({ |
| 1426 | commandId: CommandId.make(`${name}-seed`), |
| 1427 | events: [ |
| 1428 | { |
| 1429 | id: EventId.make(`${name}-checkpoint-event`), |
| 1430 | type: "checkpoint.captured", |
| 1431 | threadId, |
| 1432 | occurredAt: now, |
| 1433 | payload: { |
| 1434 | id: checkpointId, |
| 1435 | threadId, |
| 1436 | scopeId: scope.id, |
| 1437 | runId: null, |
| 1438 | nodeId: scope.nodeId, |
| 1439 | parentCheckpointId: null, |
| 1440 | ordinalWithinScope: 0, |
| 1441 | appRunOrdinal: null, |
| 1442 | ref: CheckpointRef.make(`refs/t3/${name}`), |
| 1443 | status: "ready", |
| 1444 | files: [], |
| 1445 | capturedAt: now, |
| 1446 | }, |
| 1447 | }, |
| 1448 | ], |
| 1449 | }); |
| 1450 | yield* outbox.cancelUnsettled({ |
| 1451 | threadId, |
| 1452 | effectTypes: ["provider-turn.start"], |
| 1453 | reason: "not under test", |
| 1454 | }); |
| 1455 | return { |
| 1456 | threadId, |
| 1457 | rollback: (commandId: CommandId) => |
| 1458 | orchestrator.dispatch({ |
| 1459 | type: "checkpoint.rollback", |
| 1460 | commandId, |
| 1461 | threadId, |
| 1462 | checkpointId, |
| 1463 | scopeId: scope.id, |
| 1464 | restoreFiles: false, |
| 1465 | }), |
| 1466 | }; |
| 1467 | }); |
| 1468 | |
| 1469 | it.effect("projects a rollback that fails every attempt and clears it on the next one", () => |
| 1470 | Effect.gen(function* () { |
| 1471 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 1472 | const outbox = yield* EffectOutbox.EffectOutboxV2; |
| 1473 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 1474 | const { threadId, rollback } = yield* seedFailingRollbackThread("runtime-rollback-failure"); |
| 1475 | |
| 1476 | const rollbackCommandId = CommandId.make("runtime-rollback-failure-rollback"); |
| 1477 | yield* rollback(rollbackCommandId); |
| 1478 | for (let attempt = 0; attempt < 5; attempt++) { |
| 1479 | yield* worker.drain(); |
| 1480 | yield* TestClock.adjust("30 seconds"); |
| 1481 | } |
| 1482 | |
| 1483 | const [rollbackEffect] = yield* outbox.listByCommandId(rollbackCommandId); |
| 1484 | assert.equal(rollbackEffect?.status, "failed"); |
| 1485 | const failed = yield* orchestrator.getThreadProjection(threadId); |
| 1486 | assert.deepEqual(failed.thread.rollbackFailure, { |
| 1487 | requestId: rollbackCommandId, |
| 1488 | message: ROLLBACK_FAILED_MESSAGE, |
| 1489 | }); |
| 1490 | |
| 1491 | yield* rollback(CommandId.make("runtime-rollback-failure-retry")); |
| 1492 | const retried = yield* orchestrator.getThreadProjection(threadId); |
| 1493 | assert.isNull(retried.thread.rollbackFailure); |
| 1494 | }), |
| 1495 | ); |
| 1496 | |
| 1497 | it.effect("captures and repeatedly rewinds non-Git conversations without losing run links", () => |
| 1498 | Effect.gen(function* () { |
| 1499 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 1500 | const eventSink = yield* EventSink.EventSinkV2; |
| 1501 | const outbox = yield* EffectOutbox.EffectOutboxV2; |
| 1502 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 1503 | const sessions = yield* ProviderSessionManager.ProviderSessionManagerV2; |
| 1504 | const fileSystem = yield* FileSystem.FileSystem; |
| 1505 | const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-non-git-rewind-" }); |
| 1506 | const threadId = ThreadId.make("runtime-non-git-rewind"); |
| 1507 | const rollbackThread = vi.fn( |
| 1508 | ({ providerThread }: ProviderAdapter.ProviderAdapterV2RollbackThreadInput) => |
| 1509 | Effect.succeed({ |
| 1510 | providerThread: { |
| 1511 | ...providerThread, |
| 1512 | nativeThreadRef: { |
| 1513 | driver, |
| 1514 | nativeId: "rewound-native-thread", |
| 1515 | strength: "strong" as const, |
| 1516 | }, |
| 1517 | }, |
| 1518 | }), |
| 1519 | ); |
| 1520 | const sessionSpy = vi.spyOn(sessions, "open").mockReturnValue( |
| 1521 | Effect.succeed({ |
| 1522 | rollbackThread, |
| 1523 | } as unknown as ProviderAdapter.ProviderAdapterV2SessionRuntime), |
| 1524 | ); |
| 1525 | yield* Effect.addFinalizer(() => Effect.sync(() => sessionSpy.mockRestore())); |
| 1526 | yield* orchestrator.dispatch({ |
| 1527 | type: "thread.create", |
| 1528 | commandId: CommandId.make("non-git-create"), |
| 1529 | createdBy: "user", |
| 1530 | creationSource: "web", |
| 1531 | threadId, |
| 1532 | projectId: ProjectId.make("non-git-project"), |
| 1533 | title: "Non-Git rewind", |
| 1534 | modelSelection, |
| 1535 | runtimeMode: "full-access", |
| 1536 | interactionMode: "default", |
| 1537 | branch: null, |
| 1538 | worktreePath: cwd, |
| 1539 | }); |
| 1540 | |
| 1541 | const completeMessage = (name: string) => |
| 1542 | Effect.gen(function* () { |
| 1543 | yield* orchestrator.dispatch({ |
| 1544 | type: "message.dispatch", |
| 1545 | commandId: CommandId.make(name), |
| 1546 | createdBy: "user", |
| 1547 | creationSource: "web", |
| 1548 | threadId, |
| 1549 | messageId: MessageId.make(name), |
| 1550 | text: name, |
| 1551 | attachments: [], |
| 1552 | dispatchMode: { type: "start_immediately" }, |
| 1553 | }); |
| 1554 | const projection = yield* orchestrator.getThreadProjection(threadId); |
| 1555 | const run = projection.runs.find((candidate) => candidate.userMessageId === name)!; |
| 1556 | const node = projection.nodes.find((candidate) => candidate.id === run.rootNodeId)!; |
| 1557 | const providerThread = projection.providerThreads.find( |
| 1558 | (candidate) => candidate.id === run.providerThreadId, |
| 1559 | )!; |
| 1560 | const now = yield* DateTime.now; |
| 1561 | const commandId = CommandId.make(`${name}-complete`); |
| 1562 | // The provider completion is the fixture boundary; capture and rollback use real effects. |
| 1563 | yield* outbox.cancelUnsettled({ |
| 1564 | threadId, |
| 1565 | effectTypes: ["provider-turn.start"], |
| 1566 | reason: "provider completion supplied by fixture", |
| 1567 | }); |
| 1568 | yield* eventSink.writeWithEffects({ |
| 1569 | commandId, |
| 1570 | events: [ |
| 1571 | { |
| 1572 | id: EventId.make(`${name}-run`), |
| 1573 | type: "run.updated", |
| 1574 | threadId, |
| 1575 | runId: run.id, |
| 1576 | occurredAt: now, |
| 1577 | payload: { ...run, status: "waiting", startedAt: now }, |
| 1578 | }, |
| 1579 | { |
| 1580 | id: EventId.make(`${name}-node`), |
| 1581 | type: "node.updated", |
| 1582 | threadId, |
| 1583 | runId: run.id, |
| 1584 | nodeId: node.id, |
| 1585 | occurredAt: now, |
| 1586 | payload: { ...node, status: "completed", completedAt: now }, |
| 1587 | }, |
| 1588 | { |
| 1589 | id: EventId.make(`${name}-turn`), |
| 1590 | type: "provider-turn.updated", |
| 1591 | threadId, |
| 1592 | runId: run.id, |
| 1593 | occurredAt: now, |
| 1594 | payload: { |
| 1595 | id: ProviderTurnId.make(`${name}-turn`), |
| 1596 | providerThreadId: providerThread.id, |
| 1597 | nodeId: node.id, |
| 1598 | runAttemptId: run.activeAttemptId, |
| 1599 | nativeTurnRef: null, |
| 1600 | ordinal: run.ordinal, |
| 1601 | status: "completed", |
| 1602 | startedAt: now, |
| 1603 | completedAt: now, |
| 1604 | }, |
| 1605 | }, |
| 1606 | ], |
| 1607 | effects: [ |
| 1608 | { |
| 1609 | id: `${name}-capture`, |
| 1610 | commandId, |
| 1611 | threadId, |
| 1612 | request: { |
| 1613 | type: "checkpoint.capture", |
| 1614 | runId: run.id, |
| 1615 | scopeId: node.checkpointScopeId!, |
| 1616 | }, |
| 1617 | }, |
| 1618 | ], |
| 1619 | }); |
| 1620 | yield* worker.drain(); |
| 1621 | const captured = yield* orchestrator.getThreadProjection(threadId); |
| 1622 | assert.equal( |
| 1623 | captured.runs.find((candidate) => candidate.id === run.id)?.status, |
| 1624 | "completed", |
| 1625 | ); |
| 1626 | const checkpoint = captured.checkpoints.find((candidate) => candidate.runId === run.id)!; |
| 1627 | assert.equal(checkpoint.status, "missing"); |
| 1628 | return { run, checkpoint }; |
| 1629 | }); |
| 1630 | |
| 1631 | const first = yield* completeMessage("non-git-first"); |
| 1632 | const second = yield* completeMessage("non-git-second"); |
| 1633 | const before = yield* orchestrator.getThreadProjection(threadId); |
| 1634 | assert.equal( |
| 1635 | before.checkpoints.find((checkpoint) => checkpoint.id === first.checkpoint.id)?.runId, |
| 1636 | first.run.id, |
| 1637 | "capturing a later run must preserve the earlier checkpoint's run link", |
| 1638 | ); |
| 1639 | const baseline = before.checkpoints.find( |
| 1640 | (checkpoint) => checkpoint.ordinalWithinScope === 0, |
| 1641 | )!; |
| 1642 | assert.equal(baseline.status, "missing"); |
| 1643 | for (const checkpoint of [baseline, first.checkpoint]) { |
| 1644 | for (const restoreFiles of [undefined, true]) { |
| 1645 | const commandId = CommandId.make(`non-git-files-${checkpoint.id}-${restoreFiles}`); |
| 1646 | yield* orchestrator |
| 1647 | .dispatch({ |
| 1648 | type: "checkpoint.rollback", |
| 1649 | commandId, |
| 1650 | threadId, |
| 1651 | checkpointId: checkpoint.id, |
| 1652 | scopeId: checkpoint.scopeId, |
| 1653 | ...(restoreFiles === undefined ? {} : { restoreFiles }), |
| 1654 | }) |
| 1655 | .pipe(Effect.flip); |
| 1656 | assert.deepEqual(yield* outbox.listByCommandId(commandId), []); |
| 1657 | } |
| 1658 | } |
| 1659 | assert.equal(rollbackThread.mock.calls.length, 0); |
| 1660 | |
| 1661 | const rewind = (name: string) => |
| 1662 | Effect.gen(function* () { |
| 1663 | const commandId = CommandId.make(name); |
| 1664 | yield* orchestrator.dispatch({ |
| 1665 | type: "checkpoint.rollback", |
| 1666 | commandId, |
| 1667 | threadId, |
| 1668 | checkpointId: baseline.id, |
| 1669 | scopeId: baseline.scopeId, |
| 1670 | restoreFiles: false, |
| 1671 | }); |
| 1672 | yield* worker.drain(); |
| 1673 | assert.deepEqual( |
| 1674 | (yield* outbox.listByCommandId(commandId)).map((effect) => effect.status), |
| 1675 | ["succeeded"], |
| 1676 | ); |
| 1677 | const projection = yield* orchestrator.getThreadProjection(threadId); |
| 1678 | assert.ok(projection.runs.every((run) => run.status === "rolled_back")); |
| 1679 | assert.isNull(projection.providerThreads[0]!.lastRunOrdinal); |
| 1680 | assert.equal( |
| 1681 | projection.providerThreads[0]!.nativeThreadRef?.nativeId, |
| 1682 | "rewound-native-thread", |
| 1683 | ); |
| 1684 | assert.isNull(projection.thread.rollbackFailure); |
| 1685 | }); |
| 1686 | |
| 1687 | yield* rewind("non-git-rewind-first"); |
| 1688 | assert.equal(rollbackThread.mock.calls[0]![0].target.type, "thread_start"); |
| 1689 | assert.deepEqual( |
| 1690 | rollbackThread.mock.calls[0]![0].providerThreadTurns.map((turn) => turn.id), |
| 1691 | [ProviderTurnId.make("non-git-first-turn"), ProviderTurnId.make("non-git-second-turn")], |
| 1692 | ); |
| 1693 | const discardedCommandId = CommandId.make("non-git-discarded-target"); |
| 1694 | yield* orchestrator |
| 1695 | .dispatch({ |
| 1696 | type: "checkpoint.rollback", |
| 1697 | commandId: discardedCommandId, |
| 1698 | threadId, |
| 1699 | checkpointId: second.checkpoint.id, |
| 1700 | scopeId: second.checkpoint.scopeId, |
| 1701 | restoreFiles: false, |
| 1702 | }) |
| 1703 | .pipe(Effect.flip); |
| 1704 | assert.deepEqual(yield* outbox.listByCommandId(discardedCommandId), []); |
| 1705 | assert.equal(rollbackThread.mock.calls.length, 1); |
| 1706 | |
| 1707 | const resent = yield* completeMessage("non-git-resent"); |
| 1708 | assert.isAbove(resent.run.ordinal, second.run.ordinal); |
| 1709 | yield* rewind("non-git-rewind-again"); |
| 1710 | assert.equal(rollbackThread.mock.calls.length, 2); |
| 1711 | assert.equal(rollbackThread.mock.calls[1]![0].target.type, "thread_start"); |
| 1712 | assert.deepEqual( |
| 1713 | rollbackThread.mock.calls[1]![0].providerThreadTurns.map((turn) => turn.id), |
| 1714 | [ProviderTurnId.make("non-git-resent-turn")], |
| 1715 | ); |
| 1716 | }).pipe(Effect.provide(Layer.merge(Layer.fresh(layerTest), NodeServices.layer))), |
| 1717 | ); |
| 1718 | |
| 1719 | it.effect("rejects non-ready rollback targets before persisting events or effects", () => |
| 1720 | Effect.gen(function* () { |
| 1721 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 1722 | const eventSink = yield* EventSink.EventSinkV2; |
| 1723 | const outbox = yield* EffectOutbox.EffectOutboxV2; |
| 1724 | const threadId = ThreadId.make("runtime-rollback-readiness"); |
| 1725 | yield* orchestrator.dispatch({ |
| 1726 | type: "thread.create", |
| 1727 | createdBy: "user", |
| 1728 | creationSource: "web", |
| 1729 | commandId: CommandId.make("runtime-rollback-readiness-create"), |
| 1730 | threadId, |
| 1731 | projectId: ProjectId.make("runtime-rollback-readiness-project"), |
| 1732 | title: "Rollback readiness", |
| 1733 | modelSelection, |
| 1734 | runtimeMode: "full-access", |
| 1735 | interactionMode: "default", |
| 1736 | branch: null, |
| 1737 | worktreePath: process.cwd(), |
| 1738 | }); |
| 1739 | yield* orchestrator.dispatch({ |
| 1740 | type: "message.dispatch", |
| 1741 | createdBy: "user", |
| 1742 | creationSource: "web", |
| 1743 | commandId: CommandId.make("runtime-rollback-readiness-message"), |
| 1744 | threadId, |
| 1745 | messageId: MessageId.make("runtime-rollback-readiness-message"), |
| 1746 | text: "Create the provider thread and checkpoint scope.", |
| 1747 | attachments: [], |
| 1748 | dispatchMode: { type: "start_immediately" }, |
| 1749 | }); |
| 1750 | const projection = yield* orchestrator.getThreadProjection(threadId); |
| 1751 | const scope = projection.checkpointScopes[0]!; |
| 1752 | const now = yield* DateTime.now; |
| 1753 | |
| 1754 | for (const status of ["missing", "error", "stale", "ready"] as const) { |
| 1755 | const checkpointId = CheckpointId.make("runtime-rollback-checkpoint"); |
| 1756 | const commandId = CommandId.make(`runtime-rollback-${status}`); |
| 1757 | yield* eventSink.write({ |
| 1758 | commandId: CommandId.make(`runtime-rollback-${status}-seed`), |
| 1759 | events: [ |
| 1760 | { |
| 1761 | id: EventId.make(`runtime-rollback-${status}-event`), |
| 1762 | type: "checkpoint.captured", |
| 1763 | threadId, |
| 1764 | occurredAt: now, |
| 1765 | payload: { |
| 1766 | id: checkpointId, |
| 1767 | threadId, |
| 1768 | scopeId: scope.id, |
| 1769 | runId: null, |
| 1770 | nodeId: scope.nodeId, |
| 1771 | parentCheckpointId: null, |
| 1772 | ordinalWithinScope: 0, |
| 1773 | appRunOrdinal: null, |
| 1774 | ref: CheckpointRef.make(`refs/t3/runtime-rollback-${status}`), |
| 1775 | status, |
| 1776 | files: [], |
| 1777 | capturedAt: now, |
| 1778 | }, |
| 1779 | }, |
| 1780 | ], |
| 1781 | }); |
| 1782 | const previousSequence = yield* orchestrator.getThreadEventSequence(threadId); |
| 1783 | const rollback = orchestrator.dispatch({ |
| 1784 | type: "checkpoint.rollback", |
| 1785 | commandId, |
| 1786 | threadId, |
| 1787 | checkpointId, |
| 1788 | scopeId: scope.id, |
| 1789 | }); |
| 1790 | |
| 1791 | if (status === "ready") { |
| 1792 | const accepted = yield* rollback; |
| 1793 | assert.deepEqual( |
| 1794 | accepted.storedEvents.map((stored) => stored.event.type), |
| 1795 | ["thread.metadata-updated", "checkpoint.rollback-requested"], |
| 1796 | ); |
| 1797 | assert.deepEqual( |
| 1798 | (yield* outbox.listByCommandId(commandId)).map((effect) => effect.request.type), |
| 1799 | ["provider-thread.rollback"], |
| 1800 | ); |
| 1801 | const path = yield* Path.Path.pipe( |
| 1802 | Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), |
| 1803 | ); |
| 1804 | yield* orchestrator.dispatch({ |
| 1805 | type: "thread.create", |
| 1806 | createdBy: "user", |
| 1807 | creationSource: "web", |
| 1808 | commandId: CommandId.make("runtime-rollback-ancestor-create"), |
| 1809 | threadId: ThreadId.make("runtime-rollback-ancestor"), |
| 1810 | projectId: ProjectId.make("runtime-rollback-readiness-project"), |
| 1811 | title: "Ancestor workspace owner", |
| 1812 | modelSelection, |
| 1813 | runtimeMode: "full-access", |
| 1814 | interactionMode: "default", |
| 1815 | branch: null, |
| 1816 | worktreePath: path.dirname(process.cwd()), |
| 1817 | }); |
| 1818 | const overlapCommandId = CommandId.make("runtime-rollback-overlap"); |
| 1819 | const overlapSequence = yield* orchestrator.getThreadEventSequence(threadId); |
| 1820 | const overlap = yield* orchestrator |
| 1821 | .dispatch({ |
| 1822 | type: "checkpoint.rollback", |
| 1823 | commandId: overlapCommandId, |
| 1824 | threadId, |
| 1825 | checkpointId, |
| 1826 | scopeId: scope.id, |
| 1827 | }) |
| 1828 | .pipe(Effect.flip); |
| 1829 | assert.match(String(overlap.cause), /isolated worktree/); |
| 1830 | assert.equal(yield* orchestrator.getThreadEventSequence(threadId), overlapSequence); |
| 1831 | assert.deepEqual(yield* outbox.listByCommandId(overlapCommandId), []); |
| 1832 | yield* orchestrator.dispatch({ |
| 1833 | type: "thread.metadata.update", |
| 1834 | commandId: CommandId.make("runtime-rollback-share"), |
| 1835 | threadId, |
| 1836 | worktreePath: null, |
| 1837 | }); |
| 1838 | const sharedCommandId = CommandId.make("runtime-rollback-shared"); |
| 1839 | const sequence = yield* orchestrator.getThreadEventSequence(threadId); |
| 1840 | const shared = yield* orchestrator |
| 1841 | .dispatch({ |
| 1842 | type: "checkpoint.rollback", |
| 1843 | commandId: sharedCommandId, |
| 1844 | threadId, |
| 1845 | checkpointId, |
| 1846 | scopeId: scope.id, |
| 1847 | }) |
| 1848 | .pipe(Effect.flip); |
| 1849 | assert.match(String(shared.cause), /isolated worktree/); |
| 1850 | assert.equal(yield* orchestrator.getThreadEventSequence(threadId), sequence); |
| 1851 | assert.deepEqual(yield* outbox.listByCommandId(sharedCommandId), []); |
| 1852 | const conversationOnly = yield* orchestrator.dispatch({ |
| 1853 | type: "checkpoint.rollback", |
| 1854 | commandId: CommandId.make("runtime-rollback-conversation"), |
| 1855 | threadId, |
| 1856 | checkpointId, |
| 1857 | scopeId: scope.id, |
| 1858 | restoreFiles: false, |
| 1859 | }); |
| 1860 | assert.deepEqual( |
| 1861 | conversationOnly.storedEvents.map((stored) => stored.event.type), |
| 1862 | ["thread.metadata-updated", "checkpoint.rollback-requested"], |
| 1863 | ); |
| 1864 | } else { |
| 1865 | const error = yield* rollback.pipe(Effect.flip); |
| 1866 | assert.instanceOf(error, Orchestrator.OrchestratorDispatchError); |
| 1867 | assert.equal( |
| 1868 | error.cause, |
| 1869 | `Checkpoint ${checkpointId} is ${status} and cannot be restored.`, |
| 1870 | ); |
| 1871 | assert.equal(yield* orchestrator.getThreadEventSequence(threadId), previousSequence); |
| 1872 | assert.deepEqual( |
| 1873 | yield* eventSink.readByCommandId({ commandId }).pipe(Stream.runCollect), |
| 1874 | [], |
| 1875 | ); |
| 1876 | assert.deepEqual(yield* outbox.listByCommandId(commandId), []); |
| 1877 | } |
| 1878 | } |
| 1879 | }).pipe(Effect.provide(Layer.fresh(layerTest))), |
| 1880 | ); |
| 1881 | |
| 1882 | it.effect("resolves delivery intent against the active run and starts after it completes", () => |
| 1883 | Effect.gen(function* () { |
| 1884 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 1885 | const eventSink = yield* EventSink.EventSinkV2; |
| 1886 | const outbox = yield* EffectOutbox.EffectOutboxV2; |
| 1887 | const sessions = yield* ProviderSessionManager.ProviderSessionManagerV2; |
| 1888 | const threadId = ThreadId.make("runtime-delivery-intent"); |
| 1889 | yield* orchestrator.dispatch({ |
| 1890 | type: "thread.create", |
| 1891 | createdBy: "user", |
| 1892 | creationSource: "web", |
| 1893 | commandId: CommandId.make("runtime-delivery-intent-create"), |
| 1894 | threadId, |
| 1895 | projectId: ProjectId.make("runtime-delivery-intent-project"), |
| 1896 | title: "Delivery intent", |
| 1897 | modelSelection, |
| 1898 | runtimeMode: "full-access", |
| 1899 | interactionMode: "default", |
| 1900 | branch: null, |
| 1901 | worktreePath: process.cwd(), |
| 1902 | }); |
| 1903 | yield* orchestrator.dispatch({ |
| 1904 | type: "message.dispatch", |
| 1905 | createdBy: "user", |
| 1906 | creationSource: "web", |
| 1907 | commandId: CommandId.make("runtime-delivery-intent-first"), |
| 1908 | threadId, |
| 1909 | messageId: MessageId.make("runtime-delivery-intent-first"), |
| 1910 | text: "Start work.", |
| 1911 | attachments: [], |
| 1912 | dispatchMode: { type: "start_immediately" }, |
| 1913 | }); |
| 1914 | const initial = yield* orchestrator.getThreadProjection(threadId); |
| 1915 | const run = initial.runs[0]!; |
| 1916 | const providerThread = initial.providerThreads[0]!; |
| 1917 | const now = yield* DateTime.now; |
| 1918 | const providerSession = { |
| 1919 | id: providerThread.providerSessionId!, |
| 1920 | driver, |
| 1921 | providerInstanceId: modelSelection.instanceId, |
| 1922 | status: "running" as const, |
| 1923 | cwd: process.cwd(), |
| 1924 | model: modelSelection.model, |
| 1925 | capabilities: CodexProviderCapabilitiesV2, |
| 1926 | createdAt: now, |
| 1927 | updatedAt: now, |
| 1928 | lastError: null, |
| 1929 | }; |
| 1930 | const providerTurn = { |
| 1931 | id: ProviderTurnId.make("runtime-delivery-intent-turn"), |
| 1932 | providerThreadId: providerThread.id, |
| 1933 | nodeId: run.rootNodeId!, |
| 1934 | runAttemptId: run.activeAttemptId, |
| 1935 | nativeTurnRef: null, |
| 1936 | ordinal: 1, |
| 1937 | status: "running" as const, |
| 1938 | startedAt: now, |
| 1939 | completedAt: null, |
| 1940 | }; |
| 1941 | yield* eventSink.write({ |
| 1942 | commandId: CommandId.make("runtime-delivery-intent-running"), |
| 1943 | events: [ |
| 1944 | { |
| 1945 | id: EventId.make("runtime-delivery-intent-run-event"), |
| 1946 | type: "run.updated", |
| 1947 | threadId, |
| 1948 | runId: run.id, |
| 1949 | occurredAt: now, |
| 1950 | payload: { ...run, status: "running", startedAt: now }, |
| 1951 | }, |
| 1952 | { |
| 1953 | id: EventId.make("runtime-delivery-intent-session-event"), |
| 1954 | type: "provider-session.attached", |
| 1955 | threadId, |
| 1956 | occurredAt: now, |
| 1957 | payload: providerSession, |
| 1958 | }, |
| 1959 | { |
| 1960 | id: EventId.make("runtime-delivery-intent-turn-event"), |
| 1961 | type: "provider-turn.updated", |
| 1962 | threadId, |
| 1963 | runId: run.id, |
| 1964 | occurredAt: now, |
| 1965 | payload: providerTurn, |
| 1966 | }, |
| 1967 | ], |
| 1968 | }); |
| 1969 | const sessionSpy = vi.spyOn(sessions, "get").mockReturnValue( |
| 1970 | Effect.succeedSome({ |
| 1971 | providerSession, |
| 1972 | } as ProviderAdapter.ProviderAdapterV2SessionRuntime), |
| 1973 | ); |
| 1974 | yield* Effect.addFinalizer(() => Effect.sync(() => sessionSpy.mockRestore())); |
| 1975 | |
| 1976 | const steerCommandId = CommandId.make("runtime-delivery-intent-auto"); |
| 1977 | const steerMessageId = MessageId.make("runtime-delivery-intent-auto"); |
| 1978 | yield* orchestrator.dispatch({ |
| 1979 | type: "message.dispatch", |
| 1980 | createdBy: "user", |
| 1981 | creationSource: "web", |
| 1982 | commandId: steerCommandId, |
| 1983 | threadId, |
| 1984 | messageId: steerMessageId, |
| 1985 | text: "Include this in the active work.", |
| 1986 | attachments: [], |
| 1987 | dispatchMode: { type: "start_immediately" }, |
| 1988 | deliveryIntent: "auto", |
| 1989 | }); |
| 1990 | const steered = yield* orchestrator.getThreadProjection(threadId); |
| 1991 | assert.lengthOf(steered.runs, 1); |
| 1992 | assert.equal( |
| 1993 | steered.messages.find((message) => message.id === steerMessageId)?.runId, |
| 1994 | run.id, |
| 1995 | ); |
| 1996 | assert.deepEqual( |
| 1997 | (yield* outbox.listByCommandId(steerCommandId)).map((effect) => effect.request), |
| 1998 | [ |
| 1999 | { |
| 2000 | type: "provider-turn.steer", |
| 2001 | providerSessionId: providerSession.id, |
| 2002 | providerThreadId: providerThread.id, |
| 2003 | providerTurnId: providerTurn.id, |
| 2004 | messageId: steerMessageId, |
| 2005 | modelSelection: run.modelSelection, |
| 2006 | steerFollowUpAfterRunOrdinal: run.ordinal, |
| 2007 | }, |
| 2008 | ], |
| 2009 | ); |
| 2010 | |
| 2011 | yield* eventSink.write({ |
| 2012 | commandId: CommandId.make("runtime-delivery-intent-completed"), |
| 2013 | events: [ |
| 2014 | { |
| 2015 | id: EventId.make("runtime-delivery-intent-run-completed"), |
| 2016 | type: "run.updated", |
| 2017 | threadId, |
| 2018 | runId: run.id, |
| 2019 | occurredAt: now, |
| 2020 | payload: { ...run, status: "completed", startedAt: now, completedAt: now }, |
| 2021 | }, |
| 2022 | { |
| 2023 | id: EventId.make("runtime-delivery-intent-turn-completed"), |
| 2024 | type: "provider-turn.updated", |
| 2025 | threadId, |
| 2026 | runId: run.id, |
| 2027 | occurredAt: now, |
| 2028 | payload: { ...providerTurn, status: "completed", completedAt: now }, |
| 2029 | }, |
| 2030 | ], |
| 2031 | }); |
| 2032 | const nextCommandId = CommandId.make("runtime-delivery-intent-next"); |
| 2033 | yield* orchestrator.dispatch({ |
| 2034 | type: "message.dispatch", |
| 2035 | createdBy: "user", |
| 2036 | creationSource: "web", |
| 2037 | commandId: nextCommandId, |
| 2038 | threadId, |
| 2039 | messageId: MessageId.make("runtime-delivery-intent-next"), |
| 2040 | text: "The previous run finished before this arrived.", |
| 2041 | attachments: [], |
| 2042 | dispatchMode: { type: "start_immediately" }, |
| 2043 | deliveryIntent: "restart", |
| 2044 | }); |
| 2045 | const restarted = yield* orchestrator.getThreadProjection(threadId); |
| 2046 | assert.deepEqual( |
| 2047 | restarted.runs.map((candidate) => candidate.status), |
| 2048 | ["completed", "starting"], |
| 2049 | ); |
| 2050 | assert.deepEqual( |
| 2051 | (yield* outbox.listByCommandId(nextCommandId)).map((effect) => effect.request.type), |
| 2052 | ["provider-turn.start"], |
| 2053 | ); |
| 2054 | }), |
| 2055 | ); |
| 2056 | |
| 2057 | // regression: #37412 one thread's busy lock held up every other thread's run-end reaction |
| 2058 | it.effect("starts an idle thread's queued run while another thread's lock is busy", () => |
| 2059 | Effect.gen(function* () { |
| 2060 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 2061 | const eventSink = yield* EventSink.EventSinkV2; |
| 2062 | const executor = yield* ThreadCommandExecutor.ThreadCommandExecutor; |
| 2063 | const now = yield* DateTime.now; |
| 2064 | const startRun = Effect.fnUntraced(function* (threadId: ThreadId) { |
| 2065 | yield* orchestrator.dispatch({ |
| 2066 | type: "thread.create", |
| 2067 | createdBy: "user", |
| 2068 | creationSource: "web", |
| 2069 | commandId: CommandId.make(`${threadId}:create`), |
| 2070 | threadId, |
| 2071 | projectId: ProjectId.make("runtime-run-end-isolation-project"), |
| 2072 | title: threadId, |
| 2073 | modelSelection, |
| 2074 | runtimeMode: "full-access", |
| 2075 | interactionMode: "default", |
| 2076 | branch: null, |
| 2077 | worktreePath: process.cwd(), |
| 2078 | }); |
| 2079 | yield* orchestrator.dispatch({ |
| 2080 | type: "message.dispatch", |
| 2081 | createdBy: "user", |
| 2082 | creationSource: "web", |
| 2083 | commandId: CommandId.make(`${threadId}:first`), |
| 2084 | threadId, |
| 2085 | messageId: MessageId.make(`${threadId}:first`), |
| 2086 | text: "Start work.", |
| 2087 | attachments: [], |
| 2088 | dispatchMode: { type: "start_immediately" }, |
| 2089 | }); |
| 2090 | const run = (yield* orchestrator.getThreadProjection(threadId)).runs[0]!; |
| 2091 | yield* eventSink.write({ |
| 2092 | commandId: CommandId.make(`${threadId}:running`), |
| 2093 | events: [ |
| 2094 | { |
| 2095 | id: EventId.make(`${threadId}:running-event`), |
| 2096 | type: "run.updated", |
| 2097 | threadId, |
| 2098 | runId: run.id, |
| 2099 | occurredAt: now, |
| 2100 | payload: { ...run, status: "running", startedAt: now }, |
| 2101 | }, |
| 2102 | ], |
| 2103 | }); |
| 2104 | return run; |
| 2105 | }); |
| 2106 | const endRun = (run: OrchestrationV2Run) => |
| 2107 | eventSink.write({ |
| 2108 | commandId: CommandId.make(`${run.threadId}:completed`), |
| 2109 | events: [ |
| 2110 | { |
| 2111 | id: EventId.make(`${run.threadId}:completed-event`), |
| 2112 | type: "run.updated", |
| 2113 | threadId: run.threadId, |
| 2114 | runId: run.id, |
| 2115 | occurredAt: now, |
| 2116 | payload: { ...run, status: "completed", startedAt: now, completedAt: now }, |
| 2117 | }, |
| 2118 | ], |
| 2119 | }); |
| 2120 | const busyThreadId = ThreadId.make("runtime-run-end-isolation-busy"); |
| 2121 | const idleThreadId = ThreadId.make("runtime-run-end-isolation-idle"); |
| 2122 | const busyRun = yield* startRun(busyThreadId); |
| 2123 | const idleRun = yield* startRun(idleThreadId); |
| 2124 | yield* orchestrator.dispatch({ |
| 2125 | type: "message.dispatch", |
| 2126 | createdBy: "user", |
| 2127 | creationSource: "web", |
| 2128 | commandId: CommandId.make(`${idleThreadId}:queue`), |
| 2129 | threadId: idleThreadId, |
| 2130 | messageId: MessageId.make(`${idleThreadId}:queued`), |
| 2131 | text: "Run this after the current turn.", |
| 2132 | attachments: [], |
| 2133 | dispatchMode: { type: "queue_after_active" }, |
| 2134 | }); |
| 2135 | const queuedRun = (yield* orchestrator.getThreadProjection(idleThreadId)).runs.find( |
| 2136 | (run) => run.status === "queued", |
| 2137 | ); |
| 2138 | assert.isDefined(queuedRun); |
| 2139 | |
| 2140 | const order: string[] = []; |
| 2141 | const queuedRunStarted = yield* orchestrator.streamDomainEvents.pipe( |
| 2142 | Stream.filter( |
| 2143 | (event) => |
| 2144 | event.type === "run.updated" && |
| 2145 | event.payload.id === queuedRun.id && |
| 2146 | event.payload.status !== "queued", |
| 2147 | ), |
| 2148 | Stream.take(1), |
| 2149 | Stream.runDrain, |
| 2150 | Effect.andThen(Effect.sync(() => order.push("idle thread's queued run started"))), |
| 2151 | Effect.forkScoped, |
| 2152 | ); |
| 2153 | // Stands in for a long send or delegated bookkeeping holding the busy thread's lock. |
| 2154 | const lockHeld = yield* Deferred.make<void>(); |
| 2155 | const busyLock = yield* executor |
| 2156 | .withLock( |
| 2157 | busyThreadId, |
| 2158 | Deferred.succeed(lockHeld, undefined).pipe( |
| 2159 | Effect.andThen(Effect.sleep("1 minute")), |
| 2160 | Effect.andThen(Effect.sync(() => order.push("busy thread's lock released"))), |
| 2161 | ), |
| 2162 | ) |
| 2163 | .pipe(Effect.forkScoped); |
| 2164 | yield* Deferred.await(lockHeld); |
| 2165 | |
| 2166 | yield* endRun(busyRun); |
| 2167 | yield* endRun(idleRun); |
| 2168 | yield* TestClock.adjust("1 minute"); |
| 2169 | yield* Fiber.join(busyLock); |
| 2170 | yield* Fiber.join(queuedRunStarted); |
| 2171 | |
| 2172 | assert.deepEqual(order, ["idle thread's queued run started", "busy thread's lock released"]); |
| 2173 | }), |
| 2174 | ); |
| 2175 | |
| 2176 | // regression: one coordinator's busy lock held up every other coordinator's question wake |
| 2177 | it.effect("wakes an idle coordinator for its task's question while another's lock is busy", () => |
| 2178 | Effect.gen(function* () { |
| 2179 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 2180 | const eventSink = yield* EventSink.EventSinkV2; |
| 2181 | const executor = yield* ThreadCommandExecutor.ThreadCommandExecutor; |
| 2182 | const now = yield* DateTime.now; |
| 2183 | const delegate = Effect.fnUntraced(function* (parentThreadId: ThreadId) { |
| 2184 | yield* orchestrator.dispatch({ |
| 2185 | type: "thread.create", |
| 2186 | createdBy: "user", |
| 2187 | creationSource: "web", |
| 2188 | commandId: CommandId.make(`${parentThreadId}:create`), |
| 2189 | threadId: parentThreadId, |
| 2190 | projectId: ProjectId.make("runtime-question-wake-project"), |
| 2191 | title: parentThreadId, |
| 2192 | modelSelection, |
| 2193 | runtimeMode: "full-access", |
| 2194 | interactionMode: "default", |
| 2195 | branch: null, |
| 2196 | worktreePath: process.cwd(), |
| 2197 | }); |
| 2198 | yield* orchestrator.dispatch({ |
| 2199 | type: "message.dispatch", |
| 2200 | createdBy: "user", |
| 2201 | creationSource: "web", |
| 2202 | commandId: CommandId.make(`${parentThreadId}:first`), |
| 2203 | threadId: parentThreadId, |
| 2204 | messageId: MessageId.make(`${parentThreadId}:first`), |
| 2205 | text: "Delegate the research.", |
| 2206 | attachments: [], |
| 2207 | dispatchMode: { type: "start_immediately" }, |
| 2208 | }); |
| 2209 | const parentRun = (yield* orchestrator.getThreadProjection(parentThreadId)).runs[0]!; |
| 2210 | yield* orchestrator.dispatch({ |
| 2211 | type: "delegated_task.request", |
| 2212 | createdBy: "agent", |
| 2213 | creationSource: "mcp", |
| 2214 | commandId: CommandId.make(`${parentThreadId}:delegate`), |
| 2215 | parentThreadId, |
| 2216 | parentRunId: parentRun.id, |
| 2217 | parentNodeId: parentRun.rootNodeId!, |
| 2218 | task: "Research the sources.", |
| 2219 | modelSelection, |
| 2220 | runtimeMode: "full-access", |
| 2221 | interactionMode: "default", |
| 2222 | completionWake: "settled_only", |
| 2223 | }); |
| 2224 | const childThreadId = (yield* orchestrator.getThreadProjection(parentThreadId)) |
| 2225 | .subagents[0]!.childThreadId!; |
| 2226 | const childRun = (yield* orchestrator.getThreadProjection(childThreadId)).runs[0]!; |
| 2227 | const requestId = RuntimeRequestId.make(`${childThreadId}:question`); |
| 2228 | return { |
| 2229 | wakeMessageId: MessageId.make( |
| 2230 | `message:delegated-task-question:${parentThreadId}:${requestId}`, |
| 2231 | ), |
| 2232 | ask: eventSink.write({ |
| 2233 | events: [ |
| 2234 | { |
| 2235 | id: EventId.make(`${childThreadId}:question-event`), |
| 2236 | type: "runtime-request.updated", |
| 2237 | threadId: childThreadId, |
| 2238 | runId: childRun.id, |
| 2239 | occurredAt: now, |
| 2240 | payload: { |
| 2241 | id: requestId, |
| 2242 | nodeId: childRun.rootNodeId!, |
| 2243 | providerTurnId: null, |
| 2244 | nativeRequestRef: null, |
| 2245 | kind: "user_input", |
| 2246 | status: "pending", |
| 2247 | responseCapability: { type: "message" }, |
| 2248 | createdAt: now, |
| 2249 | resolvedAt: null, |
| 2250 | }, |
| 2251 | }, |
| 2252 | ], |
| 2253 | }), |
| 2254 | }; |
| 2255 | }); |
| 2256 | // Started as server.ts starts it, so its wakes come from its own event stream. |
| 2257 | yield* DelegatedTaskQuestionReactor.DelegatedTaskQuestionReactor.pipe( |
| 2258 | Effect.flatMap((reactor) => reactor.start()), |
| 2259 | Effect.provide(DelegatedTaskQuestionReactor.layer), |
| 2260 | ); |
| 2261 | const busyThreadId = ThreadId.make("runtime-question-wake-busy"); |
| 2262 | const busy = yield* delegate(busyThreadId); |
| 2263 | const idle = yield* delegate(ThreadId.make("runtime-question-wake-idle")); |
| 2264 | |
| 2265 | const order: string[] = []; |
| 2266 | const idleWoken = yield* orchestrator.streamDomainEvents.pipe( |
| 2267 | Stream.filter( |
| 2268 | (event) => event.type === "message.updated" && event.payload.id === idle.wakeMessageId, |
| 2269 | ), |
| 2270 | Stream.take(1), |
| 2271 | Stream.runDrain, |
| 2272 | Effect.andThen(Effect.sync(() => order.push("idle coordinator woken"))), |
| 2273 | Effect.forkScoped, |
| 2274 | ); |
| 2275 | // Stands in for a long send or delegated bookkeeping holding the busy coordinator's lock. |
| 2276 | const lockHeld = yield* Deferred.make<void>(); |
| 2277 | const busyLock = yield* executor |
| 2278 | .withLock( |
| 2279 | busyThreadId, |
| 2280 | Deferred.succeed(lockHeld, undefined).pipe( |
| 2281 | Effect.andThen(Effect.sleep("1 minute")), |
| 2282 | Effect.andThen(Effect.sync(() => order.push("busy coordinator's lock released"))), |
| 2283 | ), |
| 2284 | ) |
| 2285 | .pipe(Effect.forkScoped); |
| 2286 | yield* Deferred.await(lockHeld); |
| 2287 | |
| 2288 | yield* busy.ask; |
| 2289 | yield* idle.ask; |
| 2290 | yield* TestClock.adjust("1 minute"); |
| 2291 | yield* Fiber.join(busyLock); |
| 2292 | yield* Fiber.join(idleWoken); |
| 2293 | |
| 2294 | assert.deepEqual(order, ["idle coordinator woken", "busy coordinator's lock released"]); |
| 2295 | }), |
| 2296 | ); |
| 2297 | |
| 2298 | const seedPendingQuestion = Effect.fnUntraced(function* ( |
| 2299 | threadId: ThreadId, |
| 2300 | runId: RunId | null, |
| 2301 | responseCapability: OrchestrationV2RuntimeRequest["responseCapability"], |
| 2302 | ) { |
| 2303 | const eventSink = yield* EventSink.EventSinkV2; |
| 2304 | const now = yield* DateTime.now; |
| 2305 | const requestId = RuntimeRequestId.make(`${threadId}:request`); |
| 2306 | const nodeId = NodeId.make(`${threadId}:node`); |
| 2307 | const itemId = TurnItemId.make(`${threadId}:item`); |
| 2308 | yield* eventSink.write({ |
| 2309 | commandId: CommandId.make(`${threadId}:seed`), |
| 2310 | events: [ |
| 2311 | { |
| 2312 | id: EventId.make(`${threadId}:node-event`), |
| 2313 | type: "node.updated", |
| 2314 | threadId, |
| 2315 | nodeId, |
| 2316 | occurredAt: now, |
| 2317 | payload: { |
| 2318 | id: nodeId, |
| 2319 | threadId, |
| 2320 | runId, |
| 2321 | parentNodeId: null, |
| 2322 | rootNodeId: nodeId, |
| 2323 | kind: "user_input_request", |
| 2324 | status: "waiting", |
| 2325 | countsForRun: false, |
| 2326 | providerThreadId: null, |
| 2327 | providerTurnId: null, |
| 2328 | nativeItemRef: null, |
| 2329 | runtimeRequestId: requestId, |
| 2330 | checkpointScopeId: null, |
| 2331 | startedAt: now, |
| 2332 | completedAt: null, |
| 2333 | }, |
| 2334 | }, |
| 2335 | { |
| 2336 | id: EventId.make(`${threadId}:request-event`), |
| 2337 | type: "runtime-request.updated", |
| 2338 | threadId, |
| 2339 | nodeId, |
| 2340 | occurredAt: now, |
| 2341 | payload: { |
| 2342 | id: requestId, |
| 2343 | nodeId, |
| 2344 | providerTurnId: null, |
| 2345 | nativeRequestRef: null, |
| 2346 | kind: "user_input", |
| 2347 | status: "pending", |
| 2348 | responseCapability, |
| 2349 | createdAt: now, |
| 2350 | resolvedAt: null, |
| 2351 | }, |
| 2352 | }, |
| 2353 | { |
| 2354 | id: EventId.make(`${threadId}:item-event`), |
| 2355 | type: "turn-item.updated", |
| 2356 | threadId, |
| 2357 | nodeId, |
| 2358 | occurredAt: now, |
| 2359 | payload: { |
| 2360 | id: itemId, |
| 2361 | type: "user_input_request", |
| 2362 | threadId, |
| 2363 | runId, |
| 2364 | nodeId, |
| 2365 | providerThreadId: null, |
| 2366 | providerTurnId: null, |
| 2367 | nativeItemRef: null, |
| 2368 | parentItemId: null, |
| 2369 | ordinal: 0, |
| 2370 | status: "waiting", |
| 2371 | title: null, |
| 2372 | startedAt: now, |
| 2373 | completedAt: null, |
| 2374 | updatedAt: now, |
| 2375 | requestId, |
| 2376 | questions: [ |
| 2377 | { id: "color", header: "Color", question: "Which color?", options: [] }, |
| 2378 | { |
| 2379 | id: "sizes", |
| 2380 | header: "Sizes", |
| 2381 | question: "Which sizes?", |
| 2382 | options: [], |
| 2383 | multiSelect: true, |
| 2384 | }, |
| 2385 | { |
| 2386 | id: "note", |
| 2387 | header: "Note", |
| 2388 | question: "Anything else?", |
| 2389 | options: [], |
| 2390 | required: false, |
| 2391 | }, |
| 2392 | ], |
| 2393 | }, |
| 2394 | }, |
| 2395 | ], |
| 2396 | }); |
| 2397 | return { requestId, nodeId, itemId }; |
| 2398 | }); |
| 2399 | |
| 2400 | it.effect.each([ |
| 2401 | { trigger: "startup", scope: "run" }, |
| 2402 | { trigger: "shutdown", scope: "session" }, |
| 2403 | ] as const)( |
| 2404 | "recovers only reconstructable user questions and keeps their cards pending on $trigger ($scope)", |
| 2405 | ({ trigger, scope }) => |
| 2406 | Effect.gen(function* () { |
| 2407 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 2408 | const eventSink = yield* EventSink.EventSinkV2; |
| 2409 | const now = yield* DateTime.now; |
| 2410 | const threadId = ThreadId.make(`runtime-recovered-question-${trigger}-${scope}`); |
| 2411 | yield* orchestrator.dispatch({ |
| 2412 | type: "thread.create", |
| 2413 | commandId: CommandId.make(`runtime-recovered-question-create-${trigger}-${scope}`), |
| 2414 | createdBy: "user", |
| 2415 | creationSource: "web", |
| 2416 | threadId, |
| 2417 | projectId: ProjectId.make(`runtime-recovered-question-project-${trigger}-${scope}`), |
| 2418 | title: "Async question", |
| 2419 | modelSelection, |
| 2420 | runtimeMode: "full-access", |
| 2421 | interactionMode: "default", |
| 2422 | branch: null, |
| 2423 | worktreePath: process.cwd(), |
| 2424 | }); |
| 2425 | if (scope === "run") { |
| 2426 | yield* orchestrator.dispatch({ |
| 2427 | type: "message.dispatch", |
| 2428 | commandId: CommandId.make(`${threadId}:run`), |
| 2429 | threadId, |
| 2430 | messageId: MessageId.make(`${threadId}:message`), |
| 2431 | text: "Ask me a question", |
| 2432 | attachments: [], |
| 2433 | createdBy: "user", |
| 2434 | creationSource: "web", |
| 2435 | dispatchMode: { type: "start_immediately" }, |
| 2436 | }); |
| 2437 | } |
| 2438 | const runId = (yield* orchestrator.getThreadProjection(threadId)).runs[0]?.id ?? null; |
| 2439 | const { requestId, nodeId, itemId } = yield* seedPendingQuestion(threadId, runId, { |
| 2440 | type: "live", |
| 2441 | providerSessionId: ProviderSessionId.make(`lost-session-${trigger}-${scope}`), |
| 2442 | }); |
| 2443 | const recovery = yield* ProviderRuntimeRecoveryService.ProviderRuntimeRecoveryService; |
| 2444 | const original = yield* orchestrator.getThreadProjection(threadId); |
| 2445 | const request = original.runtimeRequests[0]!; |
| 2446 | const questionItem = original.turnItems.find((item) => item.type === "user_input_request")!; |
| 2447 | const nativeThreadId = ThreadId.make(`runtime-recovered-native-child-${trigger}-${scope}`); |
| 2448 | const nativeRequestId = RuntimeRequestId.make( |
| 2449 | `runtime-recovered-native-request-${trigger}-${scope}`, |
| 2450 | ); |
| 2451 | const approvalId = RuntimeRequestId.make(`runtime-recovered-approval-${trigger}-${scope}`); |
| 2452 | const missingTextId = RuntimeRequestId.make( |
| 2453 | `runtime-recovered-missing-text-${trigger}-${scope}`, |
| 2454 | ); |
| 2455 | yield* eventSink.write({ |
| 2456 | events: [ |
| 2457 | { |
| 2458 | id: EventId.make(`recovery-approval-${trigger}-${scope}`), |
| 2459 | type: "runtime-request.updated", |
| 2460 | threadId, |
| 2461 | occurredAt: now, |
| 2462 | payload: { ...request, id: approvalId, kind: "command" }, |
| 2463 | }, |
| 2464 | { |
| 2465 | id: EventId.make(`recovery-missing-text-${trigger}-${scope}`), |
| 2466 | type: "runtime-request.updated", |
| 2467 | threadId, |
| 2468 | occurredAt: now, |
| 2469 | payload: { ...request, id: missingTextId }, |
| 2470 | }, |
| 2471 | { |
| 2472 | id: EventId.make(`recovery-native-thread-${trigger}-${scope}`), |
| 2473 | type: "thread.created", |
| 2474 | threadId: nativeThreadId, |
| 2475 | occurredAt: now, |
| 2476 | payload: { |
| 2477 | ...original.thread, |
| 2478 | id: nativeThreadId, |
| 2479 | creationSource: "provider", |
| 2480 | lineage: { |
| 2481 | parentThreadId: threadId, |
| 2482 | rootThreadId: threadId, |
| 2483 | relationshipToParent: "subagent", |
| 2484 | }, |
| 2485 | }, |
| 2486 | }, |
| 2487 | { |
| 2488 | id: EventId.make(`recovery-native-request-${trigger}-${scope}`), |
| 2489 | type: "runtime-request.updated", |
| 2490 | threadId: nativeThreadId, |
| 2491 | occurredAt: now, |
| 2492 | payload: { ...request, id: nativeRequestId }, |
| 2493 | }, |
| 2494 | { |
| 2495 | id: EventId.make(`recovery-native-item-${trigger}-${scope}`), |
| 2496 | type: "turn-item.updated", |
| 2497 | threadId: nativeThreadId, |
| 2498 | occurredAt: now, |
| 2499 | payload: { |
| 2500 | ...questionItem, |
| 2501 | id: TurnItemId.make(`recovery-native-item-${trigger}-${scope}`), |
| 2502 | type: "user_input_request", |
| 2503 | threadId: nativeThreadId, |
| 2504 | runId: null, |
| 2505 | requestId: nativeRequestId, |
| 2506 | questions: [ |
| 2507 | { id: "color", header: "Color", question: "Which color?", options: [] }, |
| 2508 | ], |
| 2509 | }, |
| 2510 | }, |
| 2511 | ], |
| 2512 | }); |
| 2513 | yield* recovery.reconcile(trigger); |
| 2514 | const recovered = yield* orchestrator.getThreadProjection(threadId); |
| 2515 | if (scope === "run") { |
| 2516 | assert.equal(recovered.runs[0]?.status, "cancelled"); |
| 2517 | } else { |
| 2518 | assert.isEmpty(recovered.runs); |
| 2519 | } |
| 2520 | assert.deepEqual( |
| 2521 | recovered.runtimeRequests.find((row) => row.id === requestId), |
| 2522 | { |
| 2523 | ...request, |
| 2524 | responseCapability: { type: "message" }, |
| 2525 | }, |
| 2526 | ); |
| 2527 | assert.equal(recovered.nodes.find((row) => row.id === nodeId)?.status, "waiting"); |
| 2528 | assert.equal(recovered.turnItems.find((row) => row.id === itemId)?.status, "waiting"); |
| 2529 | for (const id of [approvalId, missingTextId]) { |
| 2530 | const closed = recovered.runtimeRequests.find((row) => row.id === id); |
| 2531 | assert.equal(closed?.status, trigger === "startup" ? "expired" : "cancelled"); |
| 2532 | assert.equal(closed?.responseCapability.type, "not_resumable"); |
| 2533 | } |
| 2534 | const native = yield* orchestrator.getThreadProjection(nativeThreadId); |
| 2535 | assert.equal( |
| 2536 | native.runtimeRequests[0]?.status, |
| 2537 | trigger === "startup" ? "expired" : "cancelled", |
| 2538 | ); |
| 2539 | assert.equal(native.runtimeRequests[0]?.responseCapability.type, "not_resumable"); |
| 2540 | yield* recovery.reconcile("shutdown"); |
| 2541 | yield* recovery.reconcile("startup"); |
| 2542 | const rerecovered = yield* orchestrator.getThreadProjection(threadId); |
| 2543 | assert.deepEqual(rerecovered.runtimeRequests, recovered.runtimeRequests); |
| 2544 | const runItems = rerecovered.turnItems.filter((item) => item.runId === runId); |
| 2545 | const notices = runItems.filter((item) => item.type === "system_notice"); |
| 2546 | assert.lengthOf(notices, scope === "run" ? 1 : 0); |
| 2547 | if (scope === "run") { |
| 2548 | assert.equal(notices[0]?.title, "Server restarted"); |
| 2549 | assert.equal(notices[0]?.nodeId, recovered.runs[0]?.rootNodeId); |
| 2550 | assert.equal(notices[0]?.ordinal, Math.max(...runItems.map((item) => item.ordinal))); |
| 2551 | } |
| 2552 | yield* orchestrator.dispatch({ |
| 2553 | type: "runtime-request.respond", |
| 2554 | commandId: CommandId.make(`recovery-answer-${trigger}-${scope}`), |
| 2555 | threadId, |
| 2556 | requestId, |
| 2557 | answers: { color: "Blue", sizes: ["Small", "Large"] }, |
| 2558 | }); |
| 2559 | const answered = yield* orchestrator.getThreadProjection(threadId); |
| 2560 | assert.equal( |
| 2561 | answered.runtimeRequests.find((row) => row.id === requestId)?.status, |
| 2562 | "resolved", |
| 2563 | ); |
| 2564 | assert.equal(answered.nodes.find((row) => row.id === nodeId)?.status, "completed"); |
| 2565 | assert.equal(answered.turnItems.find((row) => row.id === itemId)?.status, "completed"); |
| 2566 | assert.lengthOf(answered.runs, recovered.runs.length + 1); |
| 2567 | assert.equal(answered.messages.at(-1)?.runId, answered.runs.at(-1)?.id); |
| 2568 | assert.equal( |
| 2569 | answered.messages.at(-1)?.text, |
| 2570 | "Which color?\nBlue\n\nWhich sizes?\nSmall, Large", |
| 2571 | ); |
| 2572 | assert.deepEqual( |
| 2573 | (yield* (yield* EffectOutbox.EffectOutboxV2).listByCommandId( |
| 2574 | CommandId.make(`recovery-answer-${trigger}-${scope}`), |
| 2575 | )).map((effect) => effect.request.type), |
| 2576 | ["provider-turn.start"], |
| 2577 | ); |
| 2578 | }), |
| 2579 | ); |
| 2580 | |
| 2581 | it.effect("answers an async question after its provider exits and commits the answer once", () => |
| 2582 | Effect.gen(function* () { |
| 2583 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 2584 | const eventSink = yield* EventSink.EventSinkV2; |
| 2585 | const threadId = ThreadId.make("runtime-async-question"); |
| 2586 | yield* orchestrator.dispatch({ |
| 2587 | type: "thread.create", |
| 2588 | commandId: CommandId.make("runtime-async-question-create"), |
| 2589 | createdBy: "user", |
| 2590 | creationSource: "web", |
| 2591 | threadId, |
| 2592 | projectId: ProjectId.make("runtime-async-question-project"), |
| 2593 | title: "Async question", |
| 2594 | modelSelection, |
| 2595 | runtimeMode: "full-access", |
| 2596 | interactionMode: "default", |
| 2597 | branch: null, |
| 2598 | worktreePath: process.cwd(), |
| 2599 | }); |
| 2600 | const { requestId, nodeId, itemId } = yield* seedPendingQuestion(threadId, null, { |
| 2601 | type: "message", |
| 2602 | }); |
| 2603 | const outbox = yield* EffectOutbox.EffectOutboxV2; |
| 2604 | const now = yield* DateTime.now; |
| 2605 | const beforeSnooze = yield* orchestrator.getThreadProjection(threadId); |
| 2606 | const snooze = { |
| 2607 | type: "thread.snooze" as const, |
| 2608 | commandId: CommandId.make("runtime-async-question-snooze"), |
| 2609 | threadId, |
| 2610 | snoozedUntil: "2099-07-25T09:00:00.000Z", |
| 2611 | }; |
| 2612 | yield* orchestrator.dispatch(snooze); |
| 2613 | const snoozed = yield* orchestrator.getThreadProjection(threadId); |
| 2614 | assert.isNotNull(snoozed.thread.snoozedUntil); |
| 2615 | assert.deepEqual(snoozed.runtimeRequests, beforeSnooze.runtimeRequests); |
| 2616 | assert.deepEqual(snoozed.turnItems, beforeSnooze.turnItems); |
| 2617 | assert.deepEqual(snoozed.nodes, beforeSnooze.nodes); |
| 2618 | assert.deepEqual(snoozed.runs, beforeSnooze.runs); |
| 2619 | |
| 2620 | // A question arriving after an earlier snooze must be acknowledged even |
| 2621 | // when the user picks the same wake time again. |
| 2622 | const priorSnooze = DateTime.makeUnsafe(DateTime.toEpochMillis(now) - 1_000); |
| 2623 | yield* eventSink.write({ |
| 2624 | commandId: CommandId.make("runtime-async-question-earlier-snooze"), |
| 2625 | events: [ |
| 2626 | { |
| 2627 | id: EventId.make("runtime-async-question-earlier-snooze-event"), |
| 2628 | type: "thread.snoozed", |
| 2629 | threadId, |
| 2630 | occurredAt: priorSnooze, |
| 2631 | payload: { ...snoozed.thread, snoozedAt: priorSnooze }, |
| 2632 | }, |
| 2633 | ], |
| 2634 | }); |
| 2635 | yield* orchestrator.dispatch({ |
| 2636 | ...snooze, |
| 2637 | commandId: CommandId.make("runtime-async-question-resnooze"), |
| 2638 | }); |
| 2639 | assert.deepEqual((yield* orchestrator.getThreadProjection(threadId)).thread.snoozedAt, now); |
| 2640 | yield* orchestrator.dispatch({ |
| 2641 | type: "thread.unsnooze", |
| 2642 | commandId: CommandId.make("runtime-async-question-wake"), |
| 2643 | threadId, |
| 2644 | reason: "user", |
| 2645 | }); |
| 2646 | const awake = yield* orchestrator.getThreadProjection(threadId); |
| 2647 | assert.isNull(awake.thread.snoozedUntil); |
| 2648 | assert.deepEqual(awake.runtimeRequests, beforeSnooze.runtimeRequests); |
| 2649 | |
| 2650 | const beforeInvalid = yield* orchestrator.getThreadProjection(threadId); |
| 2651 | const beforeSequence = yield* eventSink.latestSequence(); |
| 2652 | for (const [index, answers] of [ |
| 2653 | { color: " ", sizes: ["Small"] }, |
| 2654 | { color: "Blue", sizes: [] }, |
| 2655 | { color: "Blue", sizes: ["Small", " "] }, |
| 2656 | { color: "Blue", sizes: ["Small"], extra: " " }, |
| 2657 | ].entries()) { |
| 2658 | const commandId = CommandId.make(`runtime-async-question-invalid:${index}`); |
| 2659 | const invalid = yield* orchestrator |
| 2660 | .dispatch({ |
| 2661 | type: "runtime-request.respond", |
| 2662 | commandId, |
| 2663 | threadId, |
| 2664 | requestId, |
| 2665 | answers, |
| 2666 | }) |
| 2667 | .pipe(Effect.result); |
| 2668 | assert.equal(invalid._tag, "Failure", JSON.stringify(answers)); |
| 2669 | assert.deepEqual(yield* orchestrator.getThreadProjection(threadId), beforeInvalid); |
| 2670 | assert.equal(yield* eventSink.latestSequence(), beforeSequence); |
| 2671 | assert.deepEqual(yield* outbox.listByCommandId(commandId), []); |
| 2672 | } |
| 2673 | |
| 2674 | const command = { |
| 2675 | type: "runtime-request.respond" as const, |
| 2676 | commandId: CommandId.make("runtime-async-question-answer"), |
| 2677 | threadId, |
| 2678 | requestId, |
| 2679 | answers: { color: " Blue ", sizes: ["Small", "Large"], note: "" }, |
| 2680 | }; |
| 2681 | const accepted = yield* orchestrator.dispatch(command); |
| 2682 | const repeated = yield* orchestrator.dispatch(command); |
| 2683 | assert.equal(repeated.sequence, accepted.sequence); |
| 2684 | const answered = yield* orchestrator.getThreadProjection(threadId); |
| 2685 | assert.equal(answered.runtimeRequests[0]?.status, "resolved"); |
| 2686 | assert.deepEqual(answered.runtimeRequests[0]?.answers, command.answers); |
| 2687 | assert.equal(answered.nodes.find((node) => node.id === nodeId)?.status, "completed"); |
| 2688 | assert.equal(answered.turnItems.find((item) => item.id === itemId)?.status, "completed"); |
| 2689 | const answeredItem = answered.turnItems.find((item) => item.id === itemId); |
| 2690 | assert.equal(answeredItem?.type, "user_input_request"); |
| 2691 | if (answeredItem?.type === "user_input_request") { |
| 2692 | assert.deepEqual(answeredItem.questionAnswer, { |
| 2693 | requestId, |
| 2694 | answers: command.answers, |
| 2695 | attachmentsByQuestionId: {}, |
| 2696 | questionTextById: { |
| 2697 | color: "Which color?", |
| 2698 | sizes: "Which sizes?", |
| 2699 | note: "Anything else?", |
| 2700 | }, |
| 2701 | }); |
| 2702 | } |
| 2703 | assert.equal(answered.messages.length, 1); |
| 2704 | assert.equal(answered.messages[0]?.text, "Which color?\nBlue\n\nWhich sizes?\nSmall, Large"); |
| 2705 | assert.equal(answered.messages[0]?.role, "user"); |
| 2706 | assert.equal(answered.runs.length, 1); |
| 2707 | assert.deepEqual( |
| 2708 | (yield* outbox.listByCommandId(command.commandId)).map((effect) => effect.request.type), |
| 2709 | ["provider-turn.start"], |
| 2710 | ); |
| 2711 | |
| 2712 | const duplicate = yield* orchestrator |
| 2713 | .dispatch({ |
| 2714 | ...command, |
| 2715 | commandId: CommandId.make("runtime-async-question-duplicate"), |
| 2716 | }) |
| 2717 | .pipe(Effect.result); |
| 2718 | assert.equal(duplicate._tag, "Failure"); |
| 2719 | assert.equal((yield* orchestrator.getThreadProjection(threadId)).messages.length, 1); |
| 2720 | }), |
| 2721 | ); |
| 2722 | |
| 2723 | it.effect("reports an answer that never reaches its provider without reopening it", () => |
| 2724 | Effect.gen(function* () { |
| 2725 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 2726 | const eventSink = yield* EventSink.EventSinkV2; |
| 2727 | const outbox = yield* EffectOutbox.EffectOutboxV2; |
| 2728 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 2729 | const now = yield* DateTime.now; |
| 2730 | const threadId = ThreadId.make("runtime-lost-answer"); |
| 2731 | // Projected as attached, but no live session holds it, so every delivery fails. |
| 2732 | const providerSessionId = ProviderSessionId.make("runtime-lost-answer-session"); |
| 2733 | const requestId = RuntimeRequestId.make("runtime-lost-answer-request"); |
| 2734 | const nodeId = NodeId.make("runtime-lost-answer-node"); |
| 2735 | yield* orchestrator.dispatch({ |
| 2736 | type: "thread.create", |
| 2737 | commandId: CommandId.make("runtime-lost-answer-create"), |
| 2738 | createdBy: "user", |
| 2739 | creationSource: "web", |
| 2740 | threadId, |
| 2741 | projectId: ProjectId.make("runtime-lost-answer-project"), |
| 2742 | title: "Lost answer", |
| 2743 | modelSelection, |
| 2744 | runtimeMode: "full-access", |
| 2745 | interactionMode: "default", |
| 2746 | branch: null, |
| 2747 | worktreePath: process.cwd(), |
| 2748 | }); |
| 2749 | yield* eventSink.write({ |
| 2750 | commandId: CommandId.make("runtime-lost-answer-seed"), |
| 2751 | events: [ |
| 2752 | { |
| 2753 | id: EventId.make("runtime-lost-answer-session-event"), |
| 2754 | type: "provider-session.attached", |
| 2755 | threadId, |
| 2756 | driver, |
| 2757 | occurredAt: now, |
| 2758 | payload: { |
| 2759 | id: providerSessionId, |
| 2760 | driver, |
| 2761 | providerInstanceId: modelSelection.instanceId, |
| 2762 | status: "running", |
| 2763 | cwd: process.cwd(), |
| 2764 | model: modelSelection.model, |
| 2765 | capabilities: CodexProviderCapabilitiesV2, |
| 2766 | createdAt: now, |
| 2767 | updatedAt: now, |
| 2768 | lastError: null, |
| 2769 | }, |
| 2770 | }, |
| 2771 | { |
| 2772 | id: EventId.make("runtime-lost-answer-node-event"), |
| 2773 | type: "node.updated", |
| 2774 | threadId, |
| 2775 | nodeId, |
| 2776 | occurredAt: now, |
| 2777 | payload: { |
| 2778 | id: nodeId, |
| 2779 | threadId, |
| 2780 | runId: null, |
| 2781 | parentNodeId: null, |
| 2782 | rootNodeId: nodeId, |
| 2783 | kind: "user_input_request", |
| 2784 | status: "waiting", |
| 2785 | countsForRun: false, |
| 2786 | providerThreadId: null, |
| 2787 | providerTurnId: null, |
| 2788 | nativeItemRef: null, |
| 2789 | runtimeRequestId: requestId, |
| 2790 | checkpointScopeId: null, |
| 2791 | startedAt: now, |
| 2792 | completedAt: null, |
| 2793 | }, |
| 2794 | }, |
| 2795 | { |
| 2796 | id: EventId.make("runtime-lost-answer-request-event"), |
| 2797 | type: "runtime-request.updated", |
| 2798 | threadId, |
| 2799 | nodeId, |
| 2800 | occurredAt: now, |
| 2801 | payload: { |
| 2802 | id: requestId, |
| 2803 | nodeId, |
| 2804 | providerTurnId: null, |
| 2805 | nativeRequestRef: null, |
| 2806 | kind: "user_input", |
| 2807 | status: "pending", |
| 2808 | responseCapability: { type: "live", providerSessionId }, |
| 2809 | createdAt: now, |
| 2810 | resolvedAt: null, |
| 2811 | }, |
| 2812 | }, |
| 2813 | ], |
| 2814 | }); |
| 2815 | const commandId = CommandId.make("runtime-lost-answer-answer"); |
| 2816 | const answers = { color: "Blue" }; |
| 2817 | yield* orchestrator.dispatch({ |
| 2818 | type: "runtime-request.respond", |
| 2819 | commandId, |
| 2820 | threadId, |
| 2821 | requestId, |
| 2822 | answers, |
| 2823 | }); |
| 2824 | const errors = orchestrator |
| 2825 | .getThreadProjection(threadId) |
| 2826 | .pipe( |
| 2827 | Effect.map((projection) => projection.turnItems.filter((item) => item.type === "error")), |
| 2828 | ); |
| 2829 | |
| 2830 | // A delivery that can still be retried reports nothing. |
| 2831 | yield* worker.drain(); |
| 2832 | assert.deepEqual(yield* errors, []); |
| 2833 | for (let attempt = 1; attempt < 5; attempt++) { |
| 2834 | yield* TestClock.adjust("30 seconds"); |
| 2835 | yield* worker.drain(); |
| 2836 | } |
| 2837 | |
| 2838 | const [deliveryEffect] = yield* outbox.listByCommandId(commandId); |
| 2839 | assert.equal(deliveryEffect?.request.type, "runtime-request.respond"); |
| 2840 | assert.equal(deliveryEffect?.status, "failed"); |
| 2841 | const failed = yield* orchestrator.getThreadProjection(threadId); |
| 2842 | const reported = failed.turnItems.filter((item) => item.type === "error"); |
| 2843 | assert.equal(reported.length, 1); |
| 2844 | assert.equal(reported[0]?.status, "failed"); |
| 2845 | assert.equal(reported[0]?.nodeId, nodeId); |
| 2846 | // The answer stays recorded; the user is not asked again. |
| 2847 | const request = failed.runtimeRequests.find((row) => row.id === requestId); |
| 2848 | assert.equal(request?.status, "resolved"); |
| 2849 | assert.deepEqual(request?.answers, answers); |
| 2850 | }), |
| 2851 | ); |
| 2852 | |
| 2853 | it.effect("dismisses message-capable questions directly and while settling", () => |
| 2854 | Effect.gen(function* () { |
| 2855 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 2856 | const eventSink = yield* EventSink.EventSinkV2; |
| 2857 | |
| 2858 | const seedQuestion = Effect.fn("runtimeLayerTest.seedQuestion")(function* (name: string) { |
| 2859 | const threadId = ThreadId.make(`${name}-thread`); |
| 2860 | const requestId = RuntimeRequestId.make(`${name}-request`); |
| 2861 | const nodeId = NodeId.make(`${name}-node`); |
| 2862 | const itemId = TurnItemId.make(`${name}-item`); |
| 2863 | const now = yield* DateTime.now; |
| 2864 | yield* orchestrator.dispatch({ |
| 2865 | type: "thread.create", |
| 2866 | commandId: CommandId.make(`${name}-create`), |
| 2867 | createdBy: "user", |
| 2868 | creationSource: "web", |
| 2869 | threadId, |
| 2870 | projectId: ProjectId.make(`${name}-project`), |
| 2871 | title: "Dismissible question", |
| 2872 | modelSelection, |
| 2873 | runtimeMode: "full-access", |
| 2874 | interactionMode: "default", |
| 2875 | branch: null, |
| 2876 | worktreePath: process.cwd(), |
| 2877 | }); |
| 2878 | yield* eventSink.write({ |
| 2879 | commandId: CommandId.make(`${name}-seed`), |
| 2880 | events: [ |
| 2881 | { |
| 2882 | id: EventId.make(`${name}-node-event`), |
| 2883 | type: "node.updated", |
| 2884 | threadId, |
| 2885 | nodeId, |
| 2886 | occurredAt: now, |
| 2887 | payload: { |
| 2888 | id: nodeId, |
| 2889 | threadId, |
| 2890 | runId: null, |
| 2891 | parentNodeId: null, |
| 2892 | rootNodeId: nodeId, |
| 2893 | kind: "user_input_request", |
| 2894 | status: "waiting", |
| 2895 | countsForRun: false, |
| 2896 | providerThreadId: null, |
| 2897 | providerTurnId: null, |
| 2898 | nativeItemRef: null, |
| 2899 | runtimeRequestId: requestId, |
| 2900 | checkpointScopeId: null, |
| 2901 | startedAt: now, |
| 2902 | completedAt: null, |
| 2903 | }, |
| 2904 | }, |
| 2905 | { |
| 2906 | id: EventId.make(`${name}-request-event`), |
| 2907 | type: "runtime-request.updated", |
| 2908 | threadId, |
| 2909 | nodeId, |
| 2910 | occurredAt: now, |
| 2911 | payload: { |
| 2912 | id: requestId, |
| 2913 | nodeId, |
| 2914 | providerTurnId: null, |
| 2915 | nativeRequestRef: null, |
| 2916 | kind: "user_input", |
| 2917 | status: "pending", |
| 2918 | responseCapability: { type: "message" }, |
| 2919 | createdAt: now, |
| 2920 | resolvedAt: null, |
| 2921 | }, |
| 2922 | }, |
| 2923 | { |
| 2924 | id: EventId.make(`${name}-item-event`), |
| 2925 | type: "turn-item.updated", |
| 2926 | threadId, |
| 2927 | nodeId, |
| 2928 | occurredAt: now, |
| 2929 | payload: { |
| 2930 | id: itemId, |
| 2931 | type: "user_input_request", |
| 2932 | threadId, |
| 2933 | runId: null, |
| 2934 | nodeId, |
| 2935 | providerThreadId: null, |
| 2936 | providerTurnId: null, |
| 2937 | nativeItemRef: null, |
| 2938 | parentItemId: null, |
| 2939 | ordinal: 0, |
| 2940 | status: "waiting", |
| 2941 | title: null, |
| 2942 | startedAt: now, |
| 2943 | completedAt: null, |
| 2944 | updatedAt: now, |
| 2945 | requestId, |
| 2946 | responseMode: "message", |
| 2947 | questions: [{ id: "choice", header: "Choice", question: "Continue?", options: [] }], |
| 2948 | }, |
| 2949 | }, |
| 2950 | ], |
| 2951 | }); |
| 2952 | return { threadId, requestId, nodeId, itemId }; |
| 2953 | }); |
| 2954 | |
| 2955 | const dismissed = yield* seedQuestion("runtime-dismiss-question"); |
| 2956 | yield* orchestrator.dispatch({ |
| 2957 | type: "thread.user-input.dismiss", |
| 2958 | commandId: CommandId.make("runtime-dismiss-question-command"), |
| 2959 | threadId: dismissed.threadId, |
| 2960 | requestId: dismissed.requestId, |
| 2961 | }); |
| 2962 | const dismissedProjection = yield* orchestrator.getThreadProjection(dismissed.threadId); |
| 2963 | assert.equal(dismissedProjection.runtimeRequests[0]?.status, "resolved"); |
| 2964 | assert.equal(dismissedProjection.runtimeRequests[0]?.decision, "cancel"); |
| 2965 | assert.equal( |
| 2966 | dismissedProjection.nodes.find((node) => node.id === dismissed.nodeId)?.status, |
| 2967 | "cancelled", |
| 2968 | ); |
| 2969 | assert.equal( |
| 2970 | dismissedProjection.turnItems.find((item) => item.id === dismissed.itemId)?.status, |
| 2971 | "cancelled", |
| 2972 | ); |
| 2973 | assert.lengthOf(dismissedProjection.messages, 0); |
| 2974 | |
| 2975 | const settled = yield* seedQuestion("runtime-settle-question"); |
| 2976 | yield* orchestrator.dispatch({ |
| 2977 | type: "thread.settle", |
| 2978 | commandId: CommandId.make("runtime-settle-question-command"), |
| 2979 | threadId: settled.threadId, |
| 2980 | }); |
| 2981 | const settledProjection = yield* orchestrator.getThreadProjection(settled.threadId); |
| 2982 | assert.equal(settledProjection.thread.settledOverride, "settled"); |
| 2983 | assert.equal(settledProjection.runtimeRequests[0]?.status, "resolved"); |
| 2984 | assert.equal(settledProjection.runtimeRequests[0]?.decision, "cancel"); |
| 2985 | assert.equal( |
| 2986 | settledProjection.nodes.find((node) => node.id === settled.nodeId)?.status, |
| 2987 | "cancelled", |
| 2988 | ); |
| 2989 | assert.equal( |
| 2990 | settledProjection.turnItems.find((item) => item.id === settled.itemId)?.status, |
| 2991 | "cancelled", |
| 2992 | ); |
| 2993 | }), |
| 2994 | ); |
| 2995 | |
| 2996 | it.effect("refuses busy merge-back targets without blocking the queue", () => |
| 2997 | Effect.gen(function* () { |
| 2998 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 2999 | const eventSink = yield* EventSink.EventSinkV2; |
| 3000 | const now = yield* DateTime.now; |
| 3001 | const projectId = ProjectId.make("runtime-layer-waiting-merge-project"); |
| 3002 | const targetThreadId = ThreadId.make("runtime-layer-waiting-merge-target"); |
| 3003 | const sourceThreadId = ThreadId.make("runtime-layer-waiting-merge-source"); |
| 3004 | const baseRunId = RunId.make("runtime-layer-waiting-merge-base-run"); |
| 3005 | const sourceRunId = RunId.make("runtime-layer-waiting-merge-source-run"); |
| 3006 | const sourceProviderThreadId = ProviderThreadId.make( |
| 3007 | "runtime-layer-waiting-merge-provider-thread", |
| 3008 | ); |
| 3009 | const forkTransferId = ContextTransferId.make("runtime-layer-waiting-merge-fork-transfer"); |
| 3010 | |
| 3011 | yield* seedProject({ |
| 3012 | projectId, |
| 3013 | title: "Merge-back project", |
| 3014 | workspaceRoot: process.cwd(), |
| 3015 | defaultModelSelection: modelSelection, |
| 3016 | createdAt: DateTime.formatIso(now), |
| 3017 | }); |
| 3018 | yield* orchestrator.dispatch({ |
| 3019 | type: "thread.create", |
| 3020 | createdBy: "user", |
| 3021 | creationSource: "web", |
| 3022 | commandId: CommandId.make("runtime-layer-waiting-merge-create-target"), |
| 3023 | threadId: targetThreadId, |
| 3024 | projectId, |
| 3025 | title: "Waiting merge target", |
| 3026 | modelSelection, |
| 3027 | runtimeMode: "full-access", |
| 3028 | interactionMode: "default", |
| 3029 | branch: null, |
| 3030 | worktreePath: null, |
| 3031 | }); |
| 3032 | const target = yield* orchestrator.getThreadProjection(targetThreadId); |
| 3033 | |
| 3034 | yield* eventSink.write({ |
| 3035 | commandId: CommandId.make("runtime-layer-waiting-merge-seed"), |
| 3036 | events: [ |
| 3037 | { |
| 3038 | id: EventId.make("runtime-layer-waiting-merge-source-thread-event"), |
| 3039 | type: "thread.created", |
| 3040 | threadId: sourceThreadId, |
| 3041 | providerInstanceId: modelSelection.instanceId, |
| 3042 | occurredAt: now, |
| 3043 | payload: { |
| 3044 | ...target.thread, |
| 3045 | id: sourceThreadId, |
| 3046 | title: "Waiting merge source", |
| 3047 | activeProviderThreadId: null, |
| 3048 | lineage: { |
| 3049 | parentThreadId: targetThreadId, |
| 3050 | relationshipToParent: "fork", |
| 3051 | rootThreadId: targetThreadId, |
| 3052 | }, |
| 3053 | forkedFrom: { |
| 3054 | type: "run", |
| 3055 | threadId: targetThreadId, |
| 3056 | runId: baseRunId, |
| 3057 | }, |
| 3058 | createdAt: now, |
| 3059 | updatedAt: now, |
| 3060 | }, |
| 3061 | }, |
| 3062 | { |
| 3063 | id: EventId.make("runtime-layer-waiting-merge-fork-transfer-event"), |
| 3064 | type: "context-transfer.created", |
| 3065 | threadId: sourceThreadId, |
| 3066 | providerInstanceId: modelSelection.instanceId, |
| 3067 | occurredAt: now, |
| 3068 | payload: { |
| 3069 | id: forkTransferId, |
| 3070 | type: "fork", |
| 3071 | sourceThreadId: targetThreadId, |
| 3072 | targetThreadId: sourceThreadId, |
| 3073 | sourcePoint: { threadId: targetThreadId, runId: baseRunId }, |
| 3074 | basePoint: null, |
| 3075 | sourceProviderInstanceId: modelSelection.instanceId, |
| 3076 | targetProviderInstanceId: modelSelection.instanceId, |
| 3077 | targetRunId: null, |
| 3078 | status: "consumed", |
| 3079 | resolution: null, |
| 3080 | createdBy: "user", |
| 3081 | error: null, |
| 3082 | createdAt: now, |
| 3083 | updatedAt: now, |
| 3084 | consumedAt: now, |
| 3085 | }, |
| 3086 | }, |
| 3087 | { |
| 3088 | id: EventId.make("runtime-layer-waiting-merge-provider-thread-event"), |
| 3089 | type: "provider-thread.updated", |
| 3090 | threadId: sourceThreadId, |
| 3091 | driver, |
| 3092 | providerInstanceId: modelSelection.instanceId, |
| 3093 | occurredAt: now, |
| 3094 | payload: { |
| 3095 | id: sourceProviderThreadId, |
| 3096 | driver, |
| 3097 | providerInstanceId: modelSelection.instanceId, |
| 3098 | providerSessionId: null, |
| 3099 | appThreadId: sourceThreadId, |
| 3100 | ownerNodeId: null, |
| 3101 | nativeThreadRef: { |
| 3102 | driver, |
| 3103 | nativeId: "native-waiting-merge-source", |
| 3104 | strength: "strong", |
| 3105 | }, |
| 3106 | nativeConversationHeadRef: null, |
| 3107 | status: "idle", |
| 3108 | firstRunOrdinal: 1, |
| 3109 | lastRunOrdinal: 1, |
| 3110 | handoffIds: [], |
| 3111 | forkedFrom: null, |
| 3112 | createdAt: now, |
| 3113 | updatedAt: now, |
| 3114 | }, |
| 3115 | }, |
| 3116 | { |
| 3117 | id: EventId.make("runtime-layer-waiting-merge-source-run-event"), |
| 3118 | type: "run.created", |
| 3119 | threadId: sourceThreadId, |
| 3120 | runId: sourceRunId, |
| 3121 | providerInstanceId: modelSelection.instanceId, |
| 3122 | occurredAt: now, |
| 3123 | payload: { |
| 3124 | id: sourceRunId, |
| 3125 | threadId: sourceThreadId, |
| 3126 | ordinal: 1, |
| 3127 | providerInstanceId: modelSelection.instanceId, |
| 3128 | modelSelection, |
| 3129 | providerThreadId: sourceProviderThreadId, |
| 3130 | userMessageId: MessageId.make("runtime-layer-waiting-merge-message"), |
| 3131 | rootNodeId: null, |
| 3132 | activeAttemptId: null, |
| 3133 | status: "waiting", |
| 3134 | queuePosition: null, |
| 3135 | requestedAt: now, |
| 3136 | startedAt: now, |
| 3137 | completedAt: null, |
| 3138 | checkpointId: null, |
| 3139 | contextHandoffId: null, |
| 3140 | }, |
| 3141 | }, |
| 3142 | ], |
| 3143 | }); |
| 3144 | |
| 3145 | yield* orchestrator.dispatch({ |
| 3146 | type: "message.dispatch", |
| 3147 | createdBy: "user", |
| 3148 | creationSource: "web", |
| 3149 | commandId: CommandId.make("runtime-layer-merge-target-start"), |
| 3150 | threadId: targetThreadId, |
| 3151 | messageId: MessageId.make("runtime-layer-merge-target-message"), |
| 3152 | text: "Keep working in the parent.", |
| 3153 | attachments: [], |
| 3154 | dispatchMode: { type: "start_immediately" }, |
| 3155 | }); |
| 3156 | const activeTarget = yield* orchestrator.getThreadProjection(targetThreadId); |
| 3157 | const targetRun = activeTarget.runs[0]!; |
| 3158 | for (const status of ["preparing", "starting", "waiting", "queued", "running"] as const) { |
| 3159 | yield* eventSink.write({ |
| 3160 | commandId: CommandId.make(`runtime-layer-merge-target-${status}`), |
| 3161 | events: [ |
| 3162 | { |
| 3163 | id: EventId.make(`runtime-layer-merge-target-${status}-event`), |
| 3164 | type: "run.updated", |
| 3165 | threadId: targetThreadId, |
| 3166 | runId: targetRun.id, |
| 3167 | occurredAt: now, |
| 3168 | payload: { ...targetRun, status }, |
| 3169 | }, |
| 3170 | ], |
| 3171 | }); |
| 3172 | const error = yield* orchestrator |
| 3173 | .dispatch({ |
| 3174 | type: "thread.merge_back", |
| 3175 | createdBy: "user", |
| 3176 | creationSource: "mobile", |
| 3177 | commandId: CommandId.make(`runtime-layer-merge-refused-${status}`), |
| 3178 | sourceThreadId, |
| 3179 | targetThreadId, |
| 3180 | sourcePoint: { type: "run", runId: sourceRunId }, |
| 3181 | }) |
| 3182 | .pipe(Effect.flip); |
| 3183 | assert.instanceOf(error, Orchestrator.OrchestratorDispatchError); |
| 3184 | assert.equal( |
| 3185 | error.cause, |
| 3186 | "Finish or stop the target thread's active work and clear its queue before merging back.", |
| 3187 | ); |
| 3188 | const refusedTarget = yield* orchestrator.getThreadProjection(targetThreadId); |
| 3189 | assert.deepEqual(refusedTarget.contextTransfers, activeTarget.contextTransfers); |
| 3190 | assert.equal(refusedTarget.runs[0]?.status, status); |
| 3191 | } |
| 3192 | yield* orchestrator.dispatch({ |
| 3193 | type: "message.dispatch", |
| 3194 | createdBy: "user", |
| 3195 | creationSource: "web", |
| 3196 | commandId: CommandId.make("runtime-layer-merge-target-queue"), |
| 3197 | threadId: targetThreadId, |
| 3198 | messageId: MessageId.make("runtime-layer-merge-target-queued-message"), |
| 3199 | text: "Run this after the current turn.", |
| 3200 | attachments: [], |
| 3201 | dispatchMode: { type: "queue_after_active" }, |
| 3202 | }); |
| 3203 | const queuedTarget = yield* orchestrator.getThreadProjection(targetThreadId); |
| 3204 | const queuedRun = queuedTarget.runs.find((run) => run.status === "queued"); |
| 3205 | assert.isDefined(queuedRun); |
| 3206 | yield* orchestrator.dispatch({ |
| 3207 | type: "queued-run.cancel", |
| 3208 | commandId: CommandId.make("runtime-layer-merge-target-cancel-queue"), |
| 3209 | threadId: targetThreadId, |
| 3210 | runId: queuedRun.id, |
| 3211 | }); |
| 3212 | yield* eventSink.write({ |
| 3213 | commandId: CommandId.make("runtime-layer-merge-target-complete"), |
| 3214 | events: [ |
| 3215 | { |
| 3216 | id: EventId.make("runtime-layer-merge-target-completed-event"), |
| 3217 | type: "run.updated", |
| 3218 | threadId: targetThreadId, |
| 3219 | runId: targetRun.id, |
| 3220 | occurredAt: now, |
| 3221 | payload: { ...targetRun, status: "completed", completedAt: now }, |
| 3222 | }, |
| 3223 | ], |
| 3224 | }); |
| 3225 | |
| 3226 | yield* orchestrator.dispatch({ |
| 3227 | type: "thread.merge_back", |
| 3228 | createdBy: "user", |
| 3229 | creationSource: "mobile", |
| 3230 | commandId: CommandId.make("runtime-layer-waiting-merge"), |
| 3231 | sourceThreadId, |
| 3232 | targetThreadId, |
| 3233 | sourcePoint: { type: "run", runId: sourceRunId }, |
| 3234 | createdAt: now, |
| 3235 | }); |
| 3236 | |
| 3237 | const mergedTarget = yield* orchestrator.getThreadProjection(targetThreadId); |
| 3238 | const transfer = mergedTarget.contextTransfers.find( |
| 3239 | (candidate) => candidate.type === "merge_back", |
| 3240 | ); |
| 3241 | assert.isDefined(transfer); |
| 3242 | assert.equal(transfer.status, "pending"); |
| 3243 | assert.equal(transfer.sourceThreadId, sourceThreadId); |
| 3244 | assert.equal(transfer.targetThreadId, targetThreadId); |
| 3245 | assert.equal(transfer.sourcePoint.runId, sourceRunId); |
| 3246 | assert.isUndefined(transfer.sourcePoint.checkpointId); |
| 3247 | assert.equal(transfer.sourcePoint.providerThreadRef?.nativeId, "native-waiting-merge-source"); |
| 3248 | assert.equal(transfer.basePoint?.runId, baseRunId); |
| 3249 | assert.isNull(transfer.error); |
| 3250 | }), |
| 3251 | ); |
| 3252 | }); |
| 3253 | |
| 3254 | it.layer(layerLegacyImportTest)("OrchestrationV2 legacy import", (it) => { |
| 3255 | it.effect("hydrates imported transcripts before commands and propagates hydration failures", () => |
| 3256 | Effect.gen(function* () { |
| 3257 | const sql = yield* SqlClient.SqlClient; |
| 3258 | const importer = yield* LegacyV1ThreadImporter.LegacyV1ThreadImporter; |
| 3259 | const maintenance = yield* ProjectionReplay.ProjectionReplay; |
| 3260 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 3261 | const threadManagement = yield* ThreadManagementService.ThreadManagementService; |
| 3262 | const metadataThreadId = ThreadId.make("runtime-layer-legacy-metadata-thread"); |
| 3263 | const failureThreadId = ThreadId.make("runtime-layer-legacy-failure-thread"); |
| 3264 | const projectId = ProjectId.make("runtime-layer-legacy-project"); |
| 3265 | |
| 3266 | yield* sql` |
| 3267 | INSERT INTO projection_projects ( |
| 3268 | project_id, |
| 3269 | title, |
| 3270 | workspace_root, |
| 3271 | default_model_selection_json, |
| 3272 | scripts_json, |
| 3273 | created_at, |
| 3274 | updated_at, |
| 3275 | deleted_at |
| 3276 | ) VALUES ( |
| 3277 | ${projectId}, |
| 3278 | 'Legacy project', |
| 3279 | '/tmp/runtime-layer-legacy-project', |
| 3280 | '{"instanceId":"codex","model":"gpt-5.4"}', |
| 3281 | '[]', |
| 3282 | '2026-01-01T00:00:00.000Z', |
| 3283 | '2026-01-04T00:00:00.000Z', |
| 3284 | NULL |
| 3285 | ) |
| 3286 | `; |
| 3287 | yield* sql` |
| 3288 | INSERT INTO projection_threads ( |
| 3289 | thread_id, |
| 3290 | project_id, |
| 3291 | title, |
| 3292 | model_selection_json, |
| 3293 | runtime_mode, |
| 3294 | interaction_mode, |
| 3295 | branch, |
| 3296 | worktree_path, |
| 3297 | latest_turn_id, |
| 3298 | created_at, |
| 3299 | updated_at, |
| 3300 | archived_at, |
| 3301 | settled_override, |
| 3302 | settled_at, |
| 3303 | deleted_at |
| 3304 | ) VALUES |
| 3305 | ( |
| 3306 | ${metadataThreadId}, |
| 3307 | ${projectId}, |
| 3308 | 'Legacy metadata title', |
| 3309 | '{"instanceId":"codex","model":"gpt-5.4"}', |
| 3310 | 'full-access', |
| 3311 | 'default', |
| 3312 | 'main', |
| 3313 | '/tmp/runtime-layer-legacy-project', |
| 3314 | NULL, |
| 3315 | '2026-01-01T00:00:00.000Z', |
| 3316 | '2026-01-04T00:00:00.000Z', |
| 3317 | NULL, |
| 3318 | NULL, |
| 3319 | NULL, |
| 3320 | NULL |
| 3321 | ), |
| 3322 | ( |
| 3323 | ${failureThreadId}, |
| 3324 | ${projectId}, |
| 3325 | 'Legacy failure title', |
| 3326 | '{"instanceId":"codex","model":"gpt-5.4"}', |
| 3327 | 'full-access', |
| 3328 | 'default', |
| 3329 | 'main', |
| 3330 | '/tmp/runtime-layer-legacy-project', |
| 3331 | NULL, |
| 3332 | '2026-01-01T00:00:00.000Z', |
| 3333 | '2026-01-04T00:00:00.000Z', |
| 3334 | NULL, |
| 3335 | NULL, |
| 3336 | NULL, |
| 3337 | NULL |
| 3338 | ) |
| 3339 | `; |
| 3340 | yield* sql` |
| 3341 | INSERT INTO projection_thread_messages ( |
| 3342 | message_id, |
| 3343 | thread_id, |
| 3344 | turn_id, |
| 3345 | role, |
| 3346 | text, |
| 3347 | attachments_json, |
| 3348 | is_streaming, |
| 3349 | created_at, |
| 3350 | updated_at |
| 3351 | ) VALUES |
| 3352 | ( |
| 3353 | 'message:runtime-layer-legacy:1', |
| 3354 | ${metadataThreadId}, |
| 3355 | NULL, |
| 3356 | 'user', |
| 3357 | 'First imported question', |
| 3358 | '[]', |
| 3359 | 0, |
| 3360 | '2026-01-01T01:00:00.000Z', |
| 3361 | '2026-01-01T01:00:00.000Z' |
| 3362 | ), |
| 3363 | ( |
| 3364 | 'message:runtime-layer-legacy:2', |
| 3365 | ${metadataThreadId}, |
| 3366 | NULL, |
| 3367 | 'assistant', |
| 3368 | 'First imported answer', |
| 3369 | '[]', |
| 3370 | 0, |
| 3371 | '2026-01-02T01:00:00.000Z', |
| 3372 | '2026-01-02T01:00:00.000Z' |
| 3373 | ), |
| 3374 | ( |
| 3375 | 'message:runtime-layer-legacy:3', |
| 3376 | ${metadataThreadId}, |
| 3377 | NULL, |
| 3378 | 'user', |
| 3379 | 'Latest imported question', |
| 3380 | '[]', |
| 3381 | 0, |
| 3382 | '2026-01-03T01:00:00.000Z', |
| 3383 | '2026-01-03T01:00:00.000Z' |
| 3384 | ), |
| 3385 | ( |
| 3386 | 'message:runtime-layer-legacy:failure', |
| 3387 | ${failureThreadId}, |
| 3388 | NULL, |
| 3389 | 'user', |
| 3390 | 'Imported context must load before archive', |
| 3391 | '[]', |
| 3392 | 0, |
| 3393 | '2026-01-03T01:00:00.000Z', |
| 3394 | '2026-01-03T01:00:00.000Z' |
| 3395 | ) |
| 3396 | `; |
| 3397 | |
| 3398 | yield* importer.reconcileShells; |
| 3399 | assert.isTrue((yield* maintenance.verify).valid); |
| 3400 | |
| 3401 | yield* threadManagement.dispatch({ |
| 3402 | type: "thread.metadata.update", |
| 3403 | commandId: CommandId.make("runtime-layer-legacy-metadata-update"), |
| 3404 | threadId: metadataThreadId, |
| 3405 | title: "Updated after import", |
| 3406 | }); |
| 3407 | const updatedProjection = yield* threadManagement.getThreadProjection(metadataThreadId); |
| 3408 | assert.equal(updatedProjection.thread.title, "Updated after import"); |
| 3409 | assert.deepEqual( |
| 3410 | updatedProjection.messages.map((message) => message.text), |
| 3411 | ["First imported question", "First imported answer", "Latest imported question"], |
| 3412 | ); |
| 3413 | |
| 3414 | yield* sql` |
| 3415 | ALTER TABLE projection_thread_messages |
| 3416 | RENAME TO projection_thread_messages_unavailable |
| 3417 | `; |
| 3418 | const { projectionFailure, hydrationFailure } = yield* Effect.all({ |
| 3419 | projectionFailure: threadManagement.getThreadProjection(failureThreadId).pipe(Effect.flip), |
| 3420 | hydrationFailure: threadManagement |
| 3421 | .dispatch({ |
| 3422 | type: "thread.archive", |
| 3423 | commandId: CommandId.make("runtime-layer-legacy-failed-archive"), |
| 3424 | threadId: failureThreadId, |
| 3425 | }) |
| 3426 | .pipe(Effect.flip), |
| 3427 | }).pipe( |
| 3428 | Effect.ensuring( |
| 3429 | sql` |
| 3430 | ALTER TABLE projection_thread_messages_unavailable |
| 3431 | RENAME TO projection_thread_messages |
| 3432 | `.pipe(Effect.orDie), |
| 3433 | ), |
| 3434 | ); |
| 3435 | assert.instanceOf(projectionFailure, Orchestrator.OrchestratorProjectionError); |
| 3436 | assert.instanceOf(projectionFailure.cause, LegacyV1ThreadImporter.LegacyV1ThreadImportError); |
| 3437 | assert.instanceOf(hydrationFailure, Orchestrator.OrchestratorDispatchError); |
| 3438 | assert.instanceOf(hydrationFailure.cause, LegacyV1ThreadImporter.LegacyV1ThreadImportError); |
| 3439 | |
| 3440 | const projectionAfterFailure = yield* orchestrator.getThreadProjection(failureThreadId); |
| 3441 | assert.isNull(projectionAfterFailure.thread.archivedAt); |
| 3442 | |
| 3443 | yield* threadManagement.dispatch({ |
| 3444 | type: "thread.archive", |
| 3445 | commandId: CommandId.make("runtime-layer-legacy-retried-archive"), |
| 3446 | threadId: failureThreadId, |
| 3447 | }); |
| 3448 | const projectionAfterRetry = yield* threadManagement.getThreadProjection(failureThreadId); |
| 3449 | assert.isNotNull(projectionAfterRetry.thread.archivedAt); |
| 3450 | }), |
| 3451 | ); |
| 3452 | }); |
| 3453 | |
| 3454 | const writeHtmlRenderRun = Effect.fn("test.writeHtmlRenderRun")(function* (input: { |
| 3455 | readonly threadId: ThreadId; |
| 3456 | readonly key: string; |
| 3457 | readonly ordinal: number; |
| 3458 | }) { |
| 3459 | const eventSink = yield* EventSink.EventSinkV2; |
| 3460 | const now = yield* DateTime.now; |
| 3461 | const runId = RunId.make(`html-fork-run:${input.key}`); |
| 3462 | const messageId = MessageId.make(`html-fork-message:${input.key}`); |
| 3463 | const attachmentId = createAttachmentId(input.threadId, "html")!; |
| 3464 | const fileSystem = yield* FileSystem.FileSystem; |
| 3465 | const { attachmentsDir } = yield* ServerConfig.ServerConfig; |
| 3466 | const pagePath = (yield* Path.Path).join(attachmentsDir, `${attachmentId}.html`); |
| 3467 | yield* fileSystem.makeDirectory(attachmentsDir, { recursive: true }); |
| 3468 | yield* fileSystem.writeFileString(pagePath, "<p>Page</p>"); |
| 3469 | yield* eventSink.write({ |
| 3470 | events: [ |
| 3471 | { |
| 3472 | id: EventId.make(`html-fork-run:${input.key}`), |
| 3473 | type: "run.created", |
| 3474 | threadId: input.threadId, |
| 3475 | runId, |
| 3476 | occurredAt: now, |
| 3477 | payload: { |
| 3478 | id: runId, |
| 3479 | threadId: input.threadId, |
| 3480 | ordinal: input.ordinal, |
| 3481 | providerInstanceId: modelSelection.instanceId, |
| 3482 | modelSelection, |
| 3483 | providerThreadId: null, |
| 3484 | userMessageId: messageId, |
| 3485 | rootNodeId: null, |
| 3486 | activeAttemptId: null, |
| 3487 | status: "completed", |
| 3488 | requestedAt: now, |
| 3489 | startedAt: now, |
| 3490 | completedAt: now, |
| 3491 | checkpointId: null, |
| 3492 | contextHandoffId: null, |
| 3493 | }, |
| 3494 | }, |
| 3495 | { |
| 3496 | id: EventId.make(`html-fork-message:${input.key}`), |
| 3497 | type: "message.updated", |
| 3498 | threadId: input.threadId, |
| 3499 | runId, |
| 3500 | occurredAt: now, |
| 3501 | payload: { |
| 3502 | id: messageId, |
| 3503 | threadId: input.threadId, |
| 3504 | runId, |
| 3505 | nodeId: null, |
| 3506 | role: "user", |
| 3507 | text: "Publish a page", |
| 3508 | attachments: [], |
| 3509 | streaming: false, |
| 3510 | createdBy: "user", |
| 3511 | creationSource: "web", |
| 3512 | createdAt: now, |
| 3513 | updatedAt: now, |
| 3514 | }, |
| 3515 | }, |
| 3516 | { |
| 3517 | id: EventId.make(`html-fork-item:${input.key}`), |
| 3518 | type: "turn-item.updated", |
| 3519 | threadId: input.threadId, |
| 3520 | runId, |
| 3521 | occurredAt: now, |
| 3522 | payload: { |
| 3523 | id: TurnItemId.make(`html-fork-item:${input.key}`), |
| 3524 | type: "dynamic_tool", |
| 3525 | threadId: input.threadId, |
| 3526 | runId, |
| 3527 | nodeId: null, |
| 3528 | providerThreadId: null, |
| 3529 | providerTurnId: null, |
| 3530 | nativeItemRef: null, |
| 3531 | parentItemId: null, |
| 3532 | ordinal: 0, |
| 3533 | status: "completed", |
| 3534 | title: null, |
| 3535 | startedAt: now, |
| 3536 | completedAt: now, |
| 3537 | updatedAt: now, |
| 3538 | toolName: "mcp__t3-code__html_render", |
| 3539 | input: {}, |
| 3540 | output: [ |
| 3541 | { |
| 3542 | type: "text", |
| 3543 | text: JSON.stringify({ htmlRender: { attachmentId, title: "Page", height: 300 } }), |
| 3544 | }, |
| 3545 | ], |
| 3546 | }, |
| 3547 | }, |
| 3548 | ], |
| 3549 | }); |
| 3550 | return { runId, pagePath }; |
| 3551 | }); |
| 3552 | |
| 3553 | it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => { |
| 3554 | it.effect("applies lifecycle commands idempotently and emits archive/removal shell deltas", () => |
| 3555 | Effect.gen(function* () { |
| 3556 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 3557 | const threadId = ThreadId.make("runtime-layer-lifecycle-thread"); |
| 3558 | const projectId = ProjectId.make("runtime-layer-lifecycle-project"); |
| 3559 | const project = { |
| 3560 | projectId, |
| 3561 | title: "Lifecycle project", |
| 3562 | workspaceRoot: "/workspace/project", |
| 3563 | defaultModelSelection: null, |
| 3564 | createdAt: "2026-09-07T00:00:00.000Z", |
| 3565 | } as const; |
| 3566 | yield* seedProject(project); |
| 3567 | const create = { |
| 3568 | type: "thread.create" as const, |
| 3569 | createdBy: "user" as const, |
| 3570 | creationSource: "web" as const, |
| 3571 | commandId: CommandId.make("runtime-layer-lifecycle-create"), |
| 3572 | threadId, |
| 3573 | projectId, |
| 3574 | title: "Lifecycle thread", |
| 3575 | modelSelection, |
| 3576 | runtimeMode: "full-access" as const, |
| 3577 | interactionMode: "default" as const, |
| 3578 | branch: null, |
| 3579 | worktreePath: null, |
| 3580 | }; |
| 3581 | |
| 3582 | const firstCreate = yield* orchestrator.dispatch(create); |
| 3583 | const retriedCreate = yield* orchestrator.dispatch(create); |
| 3584 | assert.equal(retriedCreate.sequence, firstCreate.sequence); |
| 3585 | assert.lengthOf(retriedCreate.storedEvents, 1); |
| 3586 | |
| 3587 | yield* orchestrator.dispatch({ |
| 3588 | type: "thread.metadata.update", |
| 3589 | commandId: CommandId.make("runtime-layer-lifecycle-metadata"), |
| 3590 | threadId, |
| 3591 | title: "Renamed lifecycle thread", |
| 3592 | branch: "feature/v2", |
| 3593 | worktreePath: "/tmp/t3-v2-worktree", |
| 3594 | }); |
| 3595 | const staleWorkspaceUpdate = yield* orchestrator |
| 3596 | .dispatch({ |
| 3597 | type: "thread.metadata.update", |
| 3598 | commandId: CommandId.make("runtime-layer-lifecycle-stale-workspace"), |
| 3599 | threadId, |
| 3600 | branch: "feature/stale", |
| 3601 | worktreePath: "/tmp/stale-worktree", |
| 3602 | expectedWorktreePath: null, |
| 3603 | }) |
| 3604 | .pipe(Effect.flip); |
| 3605 | assert.instanceOf(staleWorkspaceUpdate, Orchestrator.OrchestratorDispatchError); |
| 3606 | const projectionAfterStaleWorkspaceUpdate = yield* orchestrator.getThreadProjection(threadId); |
| 3607 | assert.equal(projectionAfterStaleWorkspaceUpdate.thread.branch, "feature/v2"); |
| 3608 | assert.equal(projectionAfterStaleWorkspaceUpdate.thread.worktreePath, "/tmp/t3-v2-worktree"); |
| 3609 | const pullRequestSnapshot = yield* orchestrator.getShellSnapshot(); |
| 3610 | const pullRequest = { |
| 3611 | projectId, |
| 3612 | repository: "owner/repository", |
| 3613 | number: 24, |
| 3614 | url: "https://github.com/owner/repository/pull/24", |
| 3615 | }; |
| 3616 | yield* moveProject(projectId, "/workspace/moved", "2026-09-07T00:01:00.000Z"); |
| 3617 | const staleProjectWorkspace = yield* orchestrator |
| 3618 | .dispatch({ |
| 3619 | type: "thread.pull-request.sync", |
| 3620 | commandId: CommandId.make("runtime-layer-lifecycle-pr-sync-stale-project-workspace"), |
| 3621 | threadId, |
| 3622 | projectId, |
| 3623 | snapshotSequence: pullRequestSnapshot.snapshotSequence, |
| 3624 | expected: { |
| 3625 | workspaceRoot: "/workspace/project", |
| 3626 | branch: "feature/v2", |
| 3627 | worktreePath: "/tmp/t3-v2-worktree", |
| 3628 | linkedPullRequest: null, |
| 3629 | branchPullRequest: null, |
| 3630 | }, |
| 3631 | branchPullRequest: pullRequest, |
| 3632 | }) |
| 3633 | .pipe(Effect.flip); |
| 3634 | assert.instanceOf(staleProjectWorkspace, Orchestrator.OrchestratorDispatchError); |
| 3635 | yield* moveProject(projectId, project.workspaceRoot, "2026-09-07T00:02:00.000Z"); |
| 3636 | // An unrelated thread event during discovery must not drop the branch pull request. |
| 3637 | yield* orchestrator.dispatch({ |
| 3638 | type: "thread.visit", |
| 3639 | commandId: CommandId.make("runtime-layer-lifecycle-visit-during-pr-sync"), |
| 3640 | threadId, |
| 3641 | visitedAt: "2026-09-07T00:02:30.000Z", |
| 3642 | }); |
| 3643 | yield* orchestrator.dispatch({ |
| 3644 | type: "thread.pull-request.sync", |
| 3645 | commandId: CommandId.make("runtime-layer-lifecycle-pr-sync"), |
| 3646 | threadId, |
| 3647 | projectId, |
| 3648 | snapshotSequence: pullRequestSnapshot.snapshotSequence, |
| 3649 | expected: { |
| 3650 | workspaceRoot: "/workspace/project", |
| 3651 | branch: "feature/v2", |
| 3652 | worktreePath: "/tmp/t3-v2-worktree", |
| 3653 | linkedPullRequest: null, |
| 3654 | branchPullRequest: null, |
| 3655 | }, |
| 3656 | branchPullRequest: pullRequest, |
| 3657 | }); |
| 3658 | assert.deepEqual( |
| 3659 | (yield* orchestrator.getThreadProjection(threadId)).thread.branchPullRequest, |
| 3660 | pullRequest, |
| 3661 | ); |
| 3662 | const stalePullRequestSync = yield* orchestrator |
| 3663 | .dispatch({ |
| 3664 | type: "thread.pull-request.sync", |
| 3665 | commandId: CommandId.make("runtime-layer-lifecycle-pr-sync-stale"), |
| 3666 | threadId, |
| 3667 | projectId, |
| 3668 | snapshotSequence: pullRequestSnapshot.snapshotSequence, |
| 3669 | expected: { |
| 3670 | workspaceRoot: "/workspace/project", |
| 3671 | branch: "feature/v2", |
| 3672 | worktreePath: "/tmp/t3-v2-worktree", |
| 3673 | linkedPullRequest: null, |
| 3674 | branchPullRequest: null, |
| 3675 | }, |
| 3676 | branchPullRequest: null, |
| 3677 | }) |
| 3678 | .pipe(Effect.flip); |
| 3679 | assert.instanceOf(stalePullRequestSync, Orchestrator.OrchestratorDispatchError); |
| 3680 | const staleLinkedPullRequestReplacement = yield* orchestrator |
| 3681 | .dispatch({ |
| 3682 | type: "thread.pull-request.sync", |
| 3683 | commandId: CommandId.make("runtime-layer-lifecycle-pr-sync-stale-replacement"), |
| 3684 | threadId, |
| 3685 | projectId, |
| 3686 | snapshotSequence: pullRequestSnapshot.snapshotSequence, |
| 3687 | expected: { |
| 3688 | workspaceRoot: "/workspace/project", |
| 3689 | branch: "feature/v2", |
| 3690 | worktreePath: "/tmp/t3-v2-worktree", |
| 3691 | linkedPullRequest: null, |
| 3692 | branchPullRequest: pullRequest, |
| 3693 | }, |
| 3694 | branchPullRequest: pullRequest, |
| 3695 | linkedPullRequest: pullRequest, |
| 3696 | }) |
| 3697 | .pipe(Effect.flip); |
| 3698 | assert.instanceOf(staleLinkedPullRequestReplacement, Orchestrator.OrchestratorDispatchError); |
| 3699 | yield* orchestrator.dispatch({ |
| 3700 | type: "thread.runtime-mode.set", |
| 3701 | commandId: CommandId.make("runtime-layer-lifecycle-runtime"), |
| 3702 | threadId, |
| 3703 | runtimeMode: "approval-required", |
| 3704 | }); |
| 3705 | yield* orchestrator.dispatch({ |
| 3706 | type: "thread.interaction-mode.set", |
| 3707 | commandId: CommandId.make("runtime-layer-lifecycle-interaction"), |
| 3708 | threadId, |
| 3709 | interactionMode: "plan", |
| 3710 | }); |
| 3711 | yield* orchestrator.dispatch({ |
| 3712 | type: "thread.model-selection.set", |
| 3713 | commandId: CommandId.make("runtime-layer-lifecycle-model"), |
| 3714 | threadId, |
| 3715 | modelSelection: { ...modelSelection, model: "gpt-5.5" }, |
| 3716 | }); |
| 3717 | yield* orchestrator.dispatch({ |
| 3718 | type: "thread.active.reorder", |
| 3719 | commandId: CommandId.make("runtime-layer-lifecycle-active-order"), |
| 3720 | threadId, |
| 3721 | orderKey: "a0", |
| 3722 | }); |
| 3723 | assert.equal((yield* orchestrator.getThreadProjection(threadId)).thread.activeOrderKey, "a0"); |
| 3724 | |
| 3725 | const shellOf = (shell: Pick<OrchestrationV2ShellSnapshot, "threads">) => |
| 3726 | shell.threads.find((thread) => thread.id === threadId)!; |
| 3727 | const untaggedShell = shellOf(yield* orchestrator.getShellSnapshot()); |
| 3728 | yield* TestClock.adjust("1 minute"); |
| 3729 | for (const [step, tag, tagged, expected] of [ |
| 3730 | ["add", "demo", true, ["demo"]], |
| 3731 | ["retry", "demo", true, ["demo"]], |
| 3732 | ["add-other", "private", true, ["demo", "private"]], |
| 3733 | ["remove", "demo", false, ["private"]], |
| 3734 | ["remove-last", "private", false, []], |
| 3735 | ] as const) { |
| 3736 | yield* orchestrator.dispatch({ |
| 3737 | type: "thread.tag.set", |
| 3738 | commandId: CommandId.make(`runtime-layer-lifecycle-tag-${step}`), |
| 3739 | threadId, |
| 3740 | tag, |
| 3741 | tagged, |
| 3742 | }); |
| 3743 | assert.deepEqual( |
| 3744 | shellOf(yield* orchestrator.getShellSnapshot()).tags ?? [], |
| 3745 | expected, |
| 3746 | step, |
| 3747 | ); |
| 3748 | } |
| 3749 | const taggedShell = shellOf(yield* orchestrator.getShellSnapshot()); |
| 3750 | // Tags are not activity, so the thread keeps its place in thread lists. |
| 3751 | assert.equal( |
| 3752 | DateTime.toEpochMillis(taggedShell.updatedAt), |
| 3753 | DateTime.toEpochMillis(untaggedShell.updatedAt), |
| 3754 | ); |
| 3755 | |
| 3756 | const preAutoProjection = yield* orchestrator.getThreadProjection(threadId); |
| 3757 | const staleAutoSettle = yield* orchestrator |
| 3758 | .dispatch({ |
| 3759 | type: "thread.auto-settle", |
| 3760 | commandId: CommandId.make("runtime-layer-lifecycle-auto-settle-stale"), |
| 3761 | threadId, |
| 3762 | snapshotAt: DateTime.makeUnsafe( |
| 3763 | DateTime.toEpochMillis(preAutoProjection.thread.updatedAt) - 1, |
| 3764 | ), |
| 3765 | }) |
| 3766 | .pipe(Effect.flip); |
| 3767 | assert.instanceOf(staleAutoSettle, Orchestrator.OrchestratorDispatchError); |
| 3768 | yield* orchestrator.dispatch({ |
| 3769 | type: "thread.auto-settle", |
| 3770 | commandId: CommandId.make("runtime-layer-lifecycle-auto-settle"), |
| 3771 | threadId, |
| 3772 | snapshotAt: preAutoProjection.thread.updatedAt, |
| 3773 | }); |
| 3774 | const autoSettledProjection = yield* orchestrator.getThreadProjection(threadId); |
| 3775 | assert.equal(autoSettledProjection.thread.settledOverride, "settled"); |
| 3776 | assert.isNull(autoSettledProjection.thread.activeOrderKey); |
| 3777 | yield* orchestrator.dispatch({ |
| 3778 | type: "thread.unsettle", |
| 3779 | commandId: CommandId.make("runtime-layer-lifecycle-auto-unsettle"), |
| 3780 | threadId, |
| 3781 | reason: "user", |
| 3782 | }); |
| 3783 | const postUnsettleProjection = yield* orchestrator.getThreadProjection(threadId); |
| 3784 | const overriddenAutoSettle = yield* orchestrator |
| 3785 | .dispatch({ |
| 3786 | type: "thread.auto-settle", |
| 3787 | commandId: CommandId.make("runtime-layer-lifecycle-auto-settle-overridden"), |
| 3788 | threadId, |
| 3789 | snapshotAt: postUnsettleProjection.thread.updatedAt, |
| 3790 | }) |
| 3791 | .pipe(Effect.flip); |
| 3792 | assert.instanceOf(overriddenAutoSettle, Orchestrator.OrchestratorDispatchError); |
| 3793 | |
| 3794 | yield* orchestrator.dispatch({ |
| 3795 | type: "thread.settle", |
| 3796 | commandId: CommandId.make("runtime-layer-lifecycle-settle"), |
| 3797 | threadId, |
| 3798 | }); |
| 3799 | const settledProjection = yield* orchestrator.getThreadProjection(threadId); |
| 3800 | assert.equal(settledProjection.thread.settledOverride, "settled"); |
| 3801 | assert.isNotNull(settledProjection.thread.settledAt); |
| 3802 | |
| 3803 | yield* orchestrator.dispatch({ |
| 3804 | type: "thread.unsettle", |
| 3805 | commandId: CommandId.make("runtime-layer-lifecycle-unsettle"), |
| 3806 | threadId, |
| 3807 | reason: "user", |
| 3808 | }); |
| 3809 | const activeProjection = yield* orchestrator.getThreadProjection(threadId); |
| 3810 | assert.equal(activeProjection.thread.settledOverride, "active"); |
| 3811 | assert.isNull(activeProjection.thread.settledAt); |
| 3812 | assert.isNotNull(activeProjection.thread.unsettledAt); |
| 3813 | const activeShell = (yield* orchestrator.getShellSnapshot()).threads.find( |
| 3814 | (thread) => thread.id === threadId, |
| 3815 | ); |
| 3816 | assert.deepEqual(activeShell?.unsettledAt, activeProjection.thread.unsettledAt); |
| 3817 | |
| 3818 | yield* orchestrator.dispatch({ |
| 3819 | type: "thread.settle", |
| 3820 | commandId: CommandId.make("runtime-layer-lifecycle-resettle"), |
| 3821 | threadId, |
| 3822 | }); |
| 3823 | yield* orchestrator.dispatch({ |
| 3824 | type: "thread.pin", |
| 3825 | commandId: CommandId.make("runtime-layer-lifecycle-pin-settled"), |
| 3826 | threadId, |
| 3827 | }); |
| 3828 | const pinnedProjection = yield* orchestrator.getThreadProjection(threadId); |
| 3829 | assert.equal(pinnedProjection.thread.settledOverride, "active"); |
| 3830 | assert.isNotNull(pinnedProjection.thread.unsettledAt); |
| 3831 | |
| 3832 | const archive = yield* orchestrator.dispatch({ |
| 3833 | type: "thread.archive", |
| 3834 | commandId: CommandId.make("runtime-layer-lifecycle-archive"), |
| 3835 | threadId, |
| 3836 | }); |
| 3837 | const archivedShell = yield* orchestrator.getShellSnapshot(); |
| 3838 | assert.notInclude( |
| 3839 | archivedShell.threads.map((thread) => thread.id), |
| 3840 | threadId, |
| 3841 | ); |
| 3842 | assert.include( |
| 3843 | archivedShell.archivedThreads.map((thread) => thread.id), |
| 3844 | threadId, |
| 3845 | ); |
| 3846 | const activeOnlyShell = yield* orchestrator.getShellSnapshot({ location: "active" }); |
| 3847 | assert.notInclude( |
| 3848 | activeOnlyShell.threads.map((thread) => thread.id), |
| 3849 | threadId, |
| 3850 | ); |
| 3851 | assert.lengthOf(activeOnlyShell.archivedThreads, 0); |
| 3852 | const archiveOnlyShell = yield* orchestrator.getShellSnapshot({ location: "archive" }); |
| 3853 | assert.lengthOf(archiveOnlyShell.threads, 0); |
| 3854 | assert.include( |
| 3855 | archiveOnlyShell.archivedThreads.map((thread) => thread.id), |
| 3856 | threadId, |
| 3857 | ); |
| 3858 | assert.deepEqual( |
| 3859 | shellStreamItemFromThreadShell({ |
| 3860 | stored: archive.storedEvents[0]!, |
| 3861 | shell: yield* orchestrator.getThreadShell(threadId), |
| 3862 | }), |
| 3863 | { |
| 3864 | kind: "thread.removed", |
| 3865 | sequence: archive.sequence, |
| 3866 | location: "active", |
| 3867 | threadId, |
| 3868 | }, |
| 3869 | ); |
| 3870 | |
| 3871 | const remove = yield* orchestrator.dispatch({ |
| 3872 | type: "thread.delete", |
| 3873 | commandId: CommandId.make("runtime-layer-lifecycle-delete"), |
| 3874 | threadId, |
| 3875 | }); |
| 3876 | const deletedShell = yield* orchestrator.getShellSnapshot(); |
| 3877 | assert.notInclude( |
| 3878 | deletedShell.threads.map((thread) => thread.id), |
| 3879 | threadId, |
| 3880 | ); |
| 3881 | assert.notInclude( |
| 3882 | deletedShell.archivedThreads.map((thread) => thread.id), |
| 3883 | threadId, |
| 3884 | ); |
| 3885 | assert.deepEqual( |
| 3886 | shellStreamItemFromThreadShell({ |
| 3887 | stored: remove.storedEvents[0]!, |
| 3888 | shell: yield* orchestrator.getThreadShell(threadId), |
| 3889 | }), |
| 3890 | { |
| 3891 | kind: "thread.removed", |
| 3892 | sequence: remove.sequence, |
| 3893 | location: "active", |
| 3894 | threadId, |
| 3895 | }, |
| 3896 | ); |
| 3897 | |
| 3898 | const projection = yield* orchestrator.getThreadProjection(threadId); |
| 3899 | assert.equal(projection.thread.title, "Renamed lifecycle thread"); |
| 3900 | assert.equal(projection.thread.branch, "feature/v2"); |
| 3901 | assert.equal(projection.thread.worktreePath, "/tmp/t3-v2-worktree"); |
| 3902 | assert.equal(projection.thread.runtimeMode, "approval-required"); |
| 3903 | assert.equal(projection.thread.interactionMode, "plan"); |
| 3904 | assert.equal(projection.thread.modelSelection.model, "gpt-5.5"); |
| 3905 | assert.isNotNull(projection.thread.archivedAt); |
| 3906 | assert.isNotNull(projection.thread.deletedAt); |
| 3907 | }), |
| 3908 | ); |
| 3909 | |
| 3910 | it.effect("keeps the branch pull request when linking another pull request", () => |
| 3911 | Effect.gen(function* () { |
| 3912 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 3913 | const maintenance = yield* ProjectionReplay.ProjectionReplay; |
| 3914 | const threadId = ThreadId.make("branch-pr-link"); |
| 3915 | const projectId = ProjectId.make("branch-pr-project"); |
| 3916 | yield* seedProject({ |
| 3917 | projectId, |
| 3918 | title: "PR links", |
| 3919 | workspaceRoot: "/workspace/pr-links", |
| 3920 | defaultModelSelection: null, |
| 3921 | createdAt: "2026-09-17T00:00:00.000Z", |
| 3922 | }); |
| 3923 | yield* orchestrator.dispatch({ |
| 3924 | type: "thread.create", |
| 3925 | createdBy: "user", |
| 3926 | creationSource: "web", |
| 3927 | commandId: CommandId.make("branch-pr-create"), |
| 3928 | threadId, |
| 3929 | projectId, |
| 3930 | title: "PR links", |
| 3931 | modelSelection, |
| 3932 | runtimeMode: "full-access", |
| 3933 | interactionMode: "default", |
| 3934 | branch: "feature/pr-links", |
| 3935 | worktreePath: null, |
| 3936 | }); |
| 3937 | const snapshot = yield* orchestrator.getShellSnapshot(); |
| 3938 | yield* orchestrator.dispatch({ |
| 3939 | type: "thread.pull-request.sync", |
| 3940 | commandId: CommandId.make("branch-pr-discover"), |
| 3941 | threadId, |
| 3942 | projectId, |
| 3943 | snapshotSequence: snapshot.snapshotSequence, |
| 3944 | expected: { |
| 3945 | workspaceRoot: "/workspace/pr-links", |
| 3946 | branch: "feature/pr-links", |
| 3947 | worktreePath: null, |
| 3948 | linkedPullRequest: null, |
| 3949 | branchPullRequest: null, |
| 3950 | }, |
| 3951 | branchPullRequest: { |
| 3952 | projectId, |
| 3953 | repository: "pingdotgg/t3code", |
| 3954 | number: 1, |
| 3955 | url: "https://github.com/pingdotgg/t3code/pull/1", |
| 3956 | }, |
| 3957 | }); |
| 3958 | for (const [index, number] of [2, 2, 1, 3].entries()) { |
| 3959 | yield* orchestrator.dispatch({ |
| 3960 | type: "thread.pull-request.link", |
| 3961 | commandId: CommandId.make(`branch-pr-link-${index}`), |
| 3962 | threadId, |
| 3963 | host: "GitHub.com", |
| 3964 | repository: "Pingdotgg/T3code", |
| 3965 | number, |
| 3966 | url: `https://github.com/pingdotgg/t3code/pull/${number}`, |
| 3967 | source: "manual", |
| 3968 | }); |
| 3969 | assert.deepEqual( |
| 3970 | (yield* orchestrator.getThreadShell(threadId))?.pullRequests?.map((link) => link.number), |
| 3971 | number === 3 ? [1, 2, 3] : [1, 2], |
| 3972 | ); |
| 3973 | } |
| 3974 | yield* orchestrator.dispatch({ |
| 3975 | type: "thread.pull-request.unlink", |
| 3976 | commandId: CommandId.make("branch-pr-unlink"), |
| 3977 | threadId, |
| 3978 | host: "github.com", |
| 3979 | repository: "pingdotgg/t3code", |
| 3980 | number: 1, |
| 3981 | }); |
| 3982 | yield* orchestrator.dispatch({ |
| 3983 | type: "thread.pull-request.link", |
| 3984 | commandId: CommandId.make("branch-pr-link-after-unlink"), |
| 3985 | threadId, |
| 3986 | host: "github.com", |
| 3987 | repository: "pingdotgg/t3code", |
| 3988 | number: 4, |
| 3989 | url: "https://github.com/pingdotgg/t3code/pull/4", |
| 3990 | source: "manual", |
| 3991 | }); |
| 3992 | assert.isTrue((yield* maintenance.rebuild).valid); |
| 3993 | assert.deepEqual( |
| 3994 | (yield* orchestrator.getThreadShell(threadId))?.pullRequests?.map((link) => link.number), |
| 3995 | [2, 3, 4], |
| 3996 | ); |
| 3997 | for (const [index, route] of ["pull", "pulls"].entries()) { |
| 3998 | yield* orchestrator.dispatch({ |
| 3999 | type: "thread.pull-request.link", |
| 4000 | commandId: CommandId.make(`gitcafe-pr-link-${index}`), |
| 4001 | threadId, |
| 4002 | host: "git.cafe", |
| 4003 | repository: "pingdotgg/t3code", |
| 4004 | number: 21353 + index, |
| 4005 | url: `https://git.cafe/pingdotgg/t3code/${route}/${21353 + index}`, |
| 4006 | source: "manual", |
| 4007 | }); |
| 4008 | } |
| 4009 | assert.isTrue((yield* maintenance.rebuild).valid); |
| 4010 | assert.deepEqual( |
| 4011 | (yield* orchestrator.getThreadShell(threadId))?.pullRequests |
| 4012 | ?.filter((link) => link.host === "git.cafe") |
| 4013 | .map((link) => link.url), |
| 4014 | [21353, 21354].map((number) => `https://git.cafe/pingdotgg/t3code/pulls/${number}`), |
| 4015 | ); |
| 4016 | }), |
| 4017 | ); |
| 4018 | |
| 4019 | it.effect("retains multiple pull requests and dismissed stack members through rebuilds", () => |
| 4020 | Effect.gen(function* () { |
| 4021 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 4022 | const maintenance = yield* ProjectionReplay.ProjectionReplay; |
| 4023 | const threadId = ThreadId.make("runtime-multiple-pull-requests"); |
| 4024 | yield* orchestrator.dispatch({ |
| 4025 | type: "thread.create", |
| 4026 | createdBy: "user", |
| 4027 | creationSource: "web", |
| 4028 | commandId: CommandId.make("multi-pr-create"), |
| 4029 | threadId, |
| 4030 | projectId: ProjectId.make("multi-pr-project"), |
| 4031 | title: "Stack", |
| 4032 | modelSelection, |
| 4033 | runtimeMode: "full-access", |
| 4034 | interactionMode: "default", |
| 4035 | branch: null, |
| 4036 | worktreePath: null, |
| 4037 | }); |
| 4038 | const key = { host: "GitHub.com", repository: "Pingdotgg/T3code" }; |
| 4039 | for (const number of [1, 2]) { |
| 4040 | yield* orchestrator.dispatch({ |
| 4041 | type: "thread.pull-request.link", |
| 4042 | commandId: CommandId.make(`multi-pr-link-${number}`), |
| 4043 | threadId, |
| 4044 | ...key, |
| 4045 | number, |
| 4046 | url: `https://github.com/pingdotgg/t3code/pull/${number}`, |
| 4047 | source: number === 1 ? "manual" : "stack", |
| 4048 | }); |
| 4049 | } |
| 4050 | const linked = yield* orchestrator.getThreadShell(threadId); |
| 4051 | assert.deepEqual( |
| 4052 | linked?.pullRequests?.map(({ host, repository, number }) => ({ host, repository, number })), |
| 4053 | [1, 2].map((number) => ({ host: "github.com", repository: "pingdotgg/t3code", number })), |
| 4054 | ); |
| 4055 | yield* orchestrator.dispatch({ |
| 4056 | type: "thread.pull-request.unlink", |
| 4057 | commandId: CommandId.make("multi-pr-dismiss"), |
| 4058 | threadId, |
| 4059 | ...key, |
| 4060 | number: 2, |
| 4061 | }); |
| 4062 | yield* orchestrator.dispatch({ |
| 4063 | type: "thread.pull-request.unlink", |
| 4064 | commandId: CommandId.make("multi-pr-unlink"), |
| 4065 | threadId, |
| 4066 | ...key, |
| 4067 | number: 1, |
| 4068 | }); |
| 4069 | assert.deepEqual( |
| 4070 | (yield* orchestrator.getThreadShell(threadId))?.pullRequests?.map(({ number, source }) => ({ |
| 4071 | number, |
| 4072 | source, |
| 4073 | })), |
| 4074 | [{ number: 2, source: "stack-dismissed" }], |
| 4075 | ); |
| 4076 | yield* orchestrator.dispatch({ |
| 4077 | type: "thread.pull-request.link", |
| 4078 | commandId: CommandId.make("multi-pr-rediscover"), |
| 4079 | threadId, |
| 4080 | ...key, |
| 4081 | number: 2, |
| 4082 | url: "https://github.com/pingdotgg/t3code/pull/2", |
| 4083 | source: "stack", |
| 4084 | }); |
| 4085 | assert.equal( |
| 4086 | (yield* orchestrator.getThreadShell(threadId))?.pullRequests?.[0]?.source, |
| 4087 | "stack-dismissed", |
| 4088 | ); |
| 4089 | assert.isTrue((yield* maintenance.rebuild).valid); |
| 4090 | assert.equal( |
| 4091 | (yield* orchestrator.getThreadShell(threadId))?.pullRequests?.[0]?.source, |
| 4092 | "stack-dismissed", |
| 4093 | ); |
| 4094 | yield* orchestrator.dispatch({ |
| 4095 | type: "thread.pull-request.link", |
| 4096 | commandId: CommandId.make("multi-pr-restore"), |
| 4097 | threadId, |
| 4098 | ...key, |
| 4099 | number: 2, |
| 4100 | url: "https://github.com/pingdotgg/t3code/pull/2", |
| 4101 | source: "manual", |
| 4102 | }); |
| 4103 | assert.equal( |
| 4104 | (yield* orchestrator.getThreadShell(threadId))?.pullRequests?.[0]?.source, |
| 4105 | "manual", |
| 4106 | ); |
| 4107 | }), |
| 4108 | ); |
| 4109 | |
| 4110 | it.effect("links an issue, syncs its snapshot, and unlinks it", () => |
| 4111 | Effect.gen(function* () { |
| 4112 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 4113 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 4114 | const threadId = ThreadId.make("runtime-issue-links"); |
| 4115 | const projectId = ProjectId.make("issue-link-project"); |
| 4116 | yield* orchestrator.dispatch({ |
| 4117 | type: "thread.create", |
| 4118 | createdBy: "user", |
| 4119 | creationSource: "web", |
| 4120 | commandId: CommandId.make("issue-link-create"), |
| 4121 | threadId, |
| 4122 | projectId, |
| 4123 | title: "Issue links", |
| 4124 | modelSelection, |
| 4125 | runtimeMode: "full-access", |
| 4126 | interactionMode: "default", |
| 4127 | branch: null, |
| 4128 | worktreePath: null, |
| 4129 | }); |
| 4130 | const key = { |
| 4131 | tracker: IssueTrackerKind.make("github"), |
| 4132 | host: "GitHub.com", |
| 4133 | scope: "Pingdotgg/T3code", |
| 4134 | key: "42", |
| 4135 | }; |
| 4136 | yield* orchestrator.dispatch({ |
| 4137 | type: "thread.issue.link", |
| 4138 | commandId: CommandId.make("issue-link-1"), |
| 4139 | threadId, |
| 4140 | ...key, |
| 4141 | projectId, |
| 4142 | url: "https://github.com/pingdotgg/t3code/issues/42", |
| 4143 | displayKey: "#42", |
| 4144 | title: "Issue links", |
| 4145 | source: "manual", |
| 4146 | }); |
| 4147 | // A second link of the same issue, spelled differently, is a no-op. |
| 4148 | yield* orchestrator.dispatch({ |
| 4149 | type: "thread.issue.link", |
| 4150 | commandId: CommandId.make("issue-link-2"), |
| 4151 | threadId, |
| 4152 | ...key, |
| 4153 | host: "github.com", |
| 4154 | scope: "pingdotgg/t3code", |
| 4155 | projectId, |
| 4156 | url: "https://github.com/pingdotgg/t3code/issues/42", |
| 4157 | displayKey: "#42", |
| 4158 | title: "Issue links, renamed", |
| 4159 | source: "agent", |
| 4160 | }); |
| 4161 | yield* worker.drain(); |
| 4162 | const linked = yield* orchestrator.getThreadShell(threadId); |
| 4163 | assert.deepEqual( |
| 4164 | linked?.issues?.map(({ host, scope, key, source, title }) => ({ |
| 4165 | host, |
| 4166 | scope, |
| 4167 | key, |
| 4168 | source, |
| 4169 | title, |
| 4170 | })), |
| 4171 | [ |
| 4172 | { |
| 4173 | host: "github.com", |
| 4174 | scope: "Pingdotgg/T3code", |
| 4175 | key: "42", |
| 4176 | source: "manual", |
| 4177 | title: "Issue links", |
| 4178 | }, |
| 4179 | ], |
| 4180 | ); |
| 4181 | yield* orchestrator.dispatch({ |
| 4182 | type: "thread.issue.unlink", |
| 4183 | commandId: CommandId.make("issue-unlink"), |
| 4184 | threadId, |
| 4185 | ...key, |
| 4186 | }); |
| 4187 | yield* worker.drain(); |
| 4188 | assert.deepEqual((yield* orchestrator.getThreadShell(threadId))?.issues, []); |
| 4189 | assert.deepEqual((yield* orchestrator.getThreadProjection(threadId)).thread.issues, []); |
| 4190 | }), |
| 4191 | ); |
| 4192 | |
| 4193 | it.effect("starts, records, and stops a pull request watch", () => |
| 4194 | Effect.gen(function* () { |
| 4195 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 4196 | const maintenance = yield* ProjectionReplay.ProjectionReplay; |
| 4197 | const threadId = ThreadId.make("runtime-pull-request-watch"); |
| 4198 | yield* orchestrator.dispatch({ |
| 4199 | type: "thread.create", |
| 4200 | createdBy: "user", |
| 4201 | creationSource: "web", |
| 4202 | commandId: CommandId.make("pr-watch-create"), |
| 4203 | threadId, |
| 4204 | projectId: ProjectId.make("pr-watch-project"), |
| 4205 | title: "Watch", |
| 4206 | modelSelection, |
| 4207 | runtimeMode: "full-access", |
| 4208 | interactionMode: "default", |
| 4209 | branch: null, |
| 4210 | worktreePath: null, |
| 4211 | }); |
| 4212 | const key = { host: "github.com", repository: "pingdotgg/t3code", number: 7 }; |
| 4213 | const url = "https://github.com/pingdotgg/t3code/pull/7"; |
| 4214 | const watchOf = Effect.map( |
| 4215 | orchestrator.getThreadShell(threadId), |
| 4216 | (thread) => thread?.pullRequests?.[0]?.watch, |
| 4217 | ); |
| 4218 | |
| 4219 | yield* orchestrator.dispatch({ |
| 4220 | type: "thread.pull-request.watch", |
| 4221 | commandId: CommandId.make("pr-watch-start"), |
| 4222 | threadId, |
| 4223 | ...key, |
| 4224 | watching: true, |
| 4225 | link: { url, source: "agent" }, |
| 4226 | }); |
| 4227 | assert.equal( |
| 4228 | (yield* orchestrator.getThreadShell(threadId))?.pullRequests?.[0]?.source, |
| 4229 | "agent", |
| 4230 | ); |
| 4231 | const started = yield* watchOf; |
| 4232 | assert.isDefined(started); |
| 4233 | if (started === undefined) return; |
| 4234 | |
| 4235 | yield* orchestrator.dispatch({ |
| 4236 | type: "thread.metadata.update", |
| 4237 | commandId: CommandId.make("pr-watch-legacy-relink"), |
| 4238 | threadId, |
| 4239 | linkedPullRequest: { projectId: ProjectId.make("pr-watch-project"), ...key, url }, |
| 4240 | }); |
| 4241 | assert.deepEqual(yield* watchOf, started); |
| 4242 | |
| 4243 | const recorded = { ...started, headSha: "abc123", failedChecks: ["lint"], wakes: 1 }; |
| 4244 | yield* orchestrator.dispatch({ |
| 4245 | type: "thread.pull-request-watch.sync", |
| 4246 | commandId: CommandId.make("pr-watch-record"), |
| 4247 | threadId, |
| 4248 | ...key, |
| 4249 | startedAt: started.startedAt, |
| 4250 | watch: recorded, |
| 4251 | }); |
| 4252 | assert.deepEqual(yield* watchOf, recorded); |
| 4253 | assert.isTrue((yield* maintenance.rebuild).valid); |
| 4254 | assert.deepEqual(yield* watchOf, recorded); |
| 4255 | |
| 4256 | yield* orchestrator.dispatch({ |
| 4257 | type: "thread.pull-request.watch", |
| 4258 | commandId: CommandId.make("pr-watch-stop"), |
| 4259 | threadId, |
| 4260 | ...key, |
| 4261 | watching: false, |
| 4262 | }); |
| 4263 | const late = yield* orchestrator |
| 4264 | .dispatch({ |
| 4265 | type: "thread.pull-request-watch.sync", |
| 4266 | commandId: CommandId.make("pr-watch-late-record"), |
| 4267 | threadId, |
| 4268 | ...key, |
| 4269 | startedAt: started.startedAt, |
| 4270 | watch: { ...recorded, wakes: 2 }, |
| 4271 | wake: { |
| 4272 | messageId: MessageId.make("pr-watch-late-wake"), |
| 4273 | text: "Update", |
| 4274 | notification: { source: { kind: "monitor" }, outcome: "updated", summary: "#7" }, |
| 4275 | }, |
| 4276 | }) |
| 4277 | .pipe(Effect.flip); |
| 4278 | assert.equal(late._tag, "OrchestratorDispatchError"); |
| 4279 | assert.isUndefined(yield* watchOf); |
| 4280 | const { messages } = yield* orchestrator.getThreadRecords(threadId, ["messages"]); |
| 4281 | assert.deepEqual(messages, []); |
| 4282 | |
| 4283 | // A client start sends no CI state, so the link's last sync seeds it. |
| 4284 | yield* orchestrator.dispatch({ |
| 4285 | type: "thread.pull-request-link.sync", |
| 4286 | commandId: CommandId.make("pr-watch-ci-running"), |
| 4287 | threadId, |
| 4288 | ...key, |
| 4289 | snapshot: { |
| 4290 | state: "open", |
| 4291 | title: "Watched pull request", |
| 4292 | headBranch: "feature", |
| 4293 | baseBranch: "main", |
| 4294 | isDraft: false, |
| 4295 | updatedAt: null, |
| 4296 | syncedAt: "2026-10-02T12:30:00.000Z", |
| 4297 | checksState: "pending", |
| 4298 | }, |
| 4299 | stack: null, |
| 4300 | }); |
| 4301 | yield* orchestrator.dispatch({ |
| 4302 | type: "thread.pull-request.watch", |
| 4303 | commandId: CommandId.make("pr-watch-client-start"), |
| 4304 | threadId, |
| 4305 | ...key, |
| 4306 | watching: true, |
| 4307 | }); |
| 4308 | assert.isTrue((yield* watchOf)?.checksPending); |
| 4309 | }), |
| 4310 | ); |
| 4311 | |
| 4312 | it.effect("keeps how long a watch holds across reads until the agent changes it", () => |
| 4313 | Effect.gen(function* () { |
| 4314 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 4315 | const maintenance = yield* ProjectionReplay.ProjectionReplay; |
| 4316 | const threadId = ThreadId.make("runtime-pull-request-watch-until-merged"); |
| 4317 | yield* orchestrator.dispatch({ |
| 4318 | type: "thread.create", |
| 4319 | createdBy: "user", |
| 4320 | creationSource: "web", |
| 4321 | commandId: CommandId.make("pr-watch-until-create"), |
| 4322 | threadId, |
| 4323 | projectId: ProjectId.make("pr-watch-project"), |
| 4324 | title: "Watch until merged", |
| 4325 | modelSelection, |
| 4326 | runtimeMode: "full-access", |
| 4327 | interactionMode: "default", |
| 4328 | branch: null, |
| 4329 | worktreePath: null, |
| 4330 | }); |
| 4331 | const key = { host: "github.com", repository: "pingdotgg/t3code", number: 7 }; |
| 4332 | const watchOf = Effect.map( |
| 4333 | orchestrator.getThreadShell(threadId), |
| 4334 | (thread) => thread?.pullRequests?.[0]?.watch, |
| 4335 | ); |
| 4336 | const watch = (id: string, until?: PullRequestWatchUntil) => |
| 4337 | orchestrator.dispatch({ |
| 4338 | type: "thread.pull-request.watch", |
| 4339 | commandId: CommandId.make(id), |
| 4340 | threadId, |
| 4341 | ...key, |
| 4342 | watching: true, |
| 4343 | link: { url: "https://github.com/pingdotgg/t3code/pull/7", source: "agent" }, |
| 4344 | ...(until !== undefined && { until }), |
| 4345 | }); |
| 4346 | |
| 4347 | yield* watch("pr-watch-until-start"); |
| 4348 | assert.isUndefined((yield* watchOf)?.until); |
| 4349 | |
| 4350 | yield* watch("pr-watch-until-on", "merged"); |
| 4351 | const started = yield* watchOf; |
| 4352 | assert.equal(started?.until, "merged"); |
| 4353 | if (started === undefined) return; |
| 4354 | |
| 4355 | // A read that began before the agent set it must not drop it. |
| 4356 | const { until: _read, ...readBefore } = started; |
| 4357 | yield* orchestrator.dispatch({ |
| 4358 | type: "thread.pull-request-watch.sync", |
| 4359 | commandId: CommandId.make("pr-watch-until-record"), |
| 4360 | threadId, |
| 4361 | ...key, |
| 4362 | startedAt: started.startedAt, |
| 4363 | watch: { ...readBefore, headSha: "abc123", checksPending: false }, |
| 4364 | }); |
| 4365 | assert.equal((yield* watchOf)?.until, "merged"); |
| 4366 | assert.equal((yield* watchOf)?.headSha, "abc123"); |
| 4367 | |
| 4368 | yield* watch("pr-watch-until-again"); |
| 4369 | assert.equal((yield* watchOf)?.until, "merged"); |
| 4370 | assert.isTrue((yield* maintenance.rebuild).valid); |
| 4371 | assert.equal((yield* watchOf)?.until, "merged"); |
| 4372 | |
| 4373 | yield* watch("pr-watch-until-off", "checks"); |
| 4374 | assert.isUndefined((yield* watchOf)?.until); |
| 4375 | assert.equal((yield* watchOf)?.headSha, "abc123"); |
| 4376 | }), |
| 4377 | ); |
| 4378 | |
| 4379 | it.effect.each(["manual", "automatic"])("settling ends every pull request watch: %s", (mode) => |
| 4380 | Effect.gen(function* () { |
| 4381 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 4382 | const maintenance = yield* ProjectionReplay.ProjectionReplay; |
| 4383 | const threadId = ThreadId.make(`pr-watch-settle-${mode}`); |
| 4384 | yield* orchestrator.dispatch({ |
| 4385 | type: "thread.create", |
| 4386 | createdBy: "user", |
| 4387 | creationSource: "web", |
| 4388 | commandId: CommandId.make(`pr-watch-settle-create-${mode}`), |
| 4389 | threadId, |
| 4390 | projectId: ProjectId.make("pr-watch-settle-project"), |
| 4391 | title: "Settle watched PRs", |
| 4392 | modelSelection, |
| 4393 | runtimeMode: "full-access", |
| 4394 | interactionMode: "default", |
| 4395 | branch: null, |
| 4396 | worktreePath: null, |
| 4397 | }); |
| 4398 | const key = { host: "github.com", repository: "pingdotgg/t3code" }; |
| 4399 | for (const number of [7, 8]) { |
| 4400 | yield* orchestrator.dispatch({ |
| 4401 | type: "thread.pull-request.watch", |
| 4402 | commandId: CommandId.make(`pr-watch-settle-start-${mode}-${number}`), |
| 4403 | threadId, |
| 4404 | ...key, |
| 4405 | number, |
| 4406 | watching: true, |
| 4407 | link: { url: `https://github.com/pingdotgg/t3code/pull/${number}`, source: "agent" }, |
| 4408 | }); |
| 4409 | } |
| 4410 | const before = yield* orchestrator.getThreadShell(threadId); |
| 4411 | const started = before?.pullRequests?.[0]?.watch; |
| 4412 | assert.isDefined(started); |
| 4413 | if (started === undefined || before === null) return; |
| 4414 | yield* orchestrator.dispatch({ |
| 4415 | ...(mode === "manual" |
| 4416 | ? { type: "thread.settle" as const } |
| 4417 | : { type: "thread.auto-settle" as const, snapshotAt: before.updatedAt }), |
| 4418 | commandId: CommandId.make(`pr-watch-settle-${mode}`), |
| 4419 | threadId, |
| 4420 | }); |
| 4421 | const expected = before.pullRequests?.map(({ watch, ...link }) => ({ |
| 4422 | ...link, |
| 4423 | endedWatch: watch, |
| 4424 | })); |
| 4425 | assert.deepEqual((yield* orchestrator.getThreadShell(threadId))?.pullRequests, expected); |
| 4426 | assert.isTrue((yield* maintenance.rebuild).valid); |
| 4427 | assert.deepEqual((yield* orchestrator.getThreadShell(threadId))?.pullRequests, expected); |
| 4428 | |
| 4429 | for (const wake of [ |
| 4430 | undefined, |
| 4431 | { |
| 4432 | messageId: MessageId.make(`pr-watch-settle-late-wake-${mode}`), |
| 4433 | text: "Update", |
| 4434 | notification: { |
| 4435 | source: { kind: "monitor" as const }, |
| 4436 | outcome: "updated" as const, |
| 4437 | summary: "#7", |
| 4438 | }, |
| 4439 | }, |
| 4440 | ]) { |
| 4441 | const late = yield* orchestrator |
| 4442 | .dispatch({ |
| 4443 | type: "thread.pull-request-watch.sync", |
| 4444 | commandId: CommandId.make(`pr-watch-settle-late-${mode}-${wake !== undefined}`), |
| 4445 | threadId, |
| 4446 | ...key, |
| 4447 | number: 7, |
| 4448 | startedAt: started.startedAt, |
| 4449 | watch: { ...started, headSha: "late", wakes: 1 }, |
| 4450 | ...(wake === undefined ? {} : { wake }), |
| 4451 | }) |
| 4452 | .pipe(Effect.flip); |
| 4453 | assert.equal(late._tag, "OrchestratorDispatchError"); |
| 4454 | } |
| 4455 | const restart = { |
| 4456 | type: "thread.pull-request.watch" as const, |
| 4457 | threadId, |
| 4458 | ...key, |
| 4459 | number: 7, |
| 4460 | watching: true, |
| 4461 | }; |
| 4462 | const blocked = yield* orchestrator |
| 4463 | .dispatch({ |
| 4464 | ...restart, |
| 4465 | commandId: CommandId.make(`pr-watch-settle-blocked-${mode}`), |
| 4466 | }) |
| 4467 | .pipe(Effect.flip); |
| 4468 | assert.equal(blocked._tag, "OrchestratorDispatchError"); |
| 4469 | assert.deepEqual((yield* orchestrator.getThreadRecords(threadId, ["messages"])).messages, []); |
| 4470 | yield* orchestrator.dispatch({ |
| 4471 | type: "thread.unsettle", |
| 4472 | commandId: CommandId.make(`pr-watch-settle-unsettle-${mode}`), |
| 4473 | threadId, |
| 4474 | reason: "user", |
| 4475 | }); |
| 4476 | yield* orchestrator.dispatch({ |
| 4477 | ...restart, |
| 4478 | commandId: CommandId.make(`pr-watch-settle-restart-${mode}`), |
| 4479 | }); |
| 4480 | assert.isDefined((yield* orchestrator.getThreadShell(threadId))?.pullRequests?.[0]?.watch); |
| 4481 | }), |
| 4482 | ); |
| 4483 | |
| 4484 | it.effect("archiving ends watches, and an archived thread cannot start one", () => |
| 4485 | Effect.gen(function* () { |
| 4486 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 4487 | const threadId = ThreadId.make("runtime-pull-request-watch-archive"); |
| 4488 | yield* orchestrator.dispatch({ |
| 4489 | type: "thread.create", |
| 4490 | createdBy: "user", |
| 4491 | creationSource: "web", |
| 4492 | commandId: CommandId.make("pr-watch-archive-create"), |
| 4493 | threadId, |
| 4494 | projectId: ProjectId.make("pr-watch-archive-project"), |
| 4495 | title: "Archive watched PR", |
| 4496 | modelSelection, |
| 4497 | runtimeMode: "full-access", |
| 4498 | interactionMode: "default", |
| 4499 | branch: null, |
| 4500 | worktreePath: null, |
| 4501 | }); |
| 4502 | const watch = { |
| 4503 | type: "thread.pull-request.watch" as const, |
| 4504 | threadId, |
| 4505 | host: "github.com", |
| 4506 | repository: "pingdotgg/t3code", |
| 4507 | number: 7, |
| 4508 | watching: true, |
| 4509 | link: { url: "https://github.com/pingdotgg/t3code/pull/7", source: "agent" as const }, |
| 4510 | }; |
| 4511 | const watched = Effect.map( |
| 4512 | orchestrator.getThreadShell(threadId), |
| 4513 | (thread) => thread?.pullRequests?.[0]?.watch !== undefined, |
| 4514 | ); |
| 4515 | yield* orchestrator.dispatch({ |
| 4516 | ...watch, |
| 4517 | commandId: CommandId.make("pr-watch-archive-start"), |
| 4518 | }); |
| 4519 | assert.isTrue(yield* watched); |
| 4520 | |
| 4521 | yield* orchestrator.dispatch({ |
| 4522 | type: "thread.archive", |
| 4523 | commandId: CommandId.make("pr-watch-archive"), |
| 4524 | threadId, |
| 4525 | }); |
| 4526 | assert.isFalse(yield* watched); |
| 4527 | const refused = yield* orchestrator |
| 4528 | .dispatch({ ...watch, commandId: CommandId.make("pr-watch-archive-restart") }) |
| 4529 | .pipe(Effect.flip); |
| 4530 | assert.equal(refused._tag, "OrchestratorDispatchError"); |
| 4531 | |
| 4532 | yield* orchestrator.dispatch({ |
| 4533 | type: "thread.unarchive", |
| 4534 | commandId: CommandId.make("pr-watch-unarchive"), |
| 4535 | threadId, |
| 4536 | }); |
| 4537 | assert.isFalse(yield* watched); |
| 4538 | }), |
| 4539 | ); |
| 4540 | |
| 4541 | it.effect.each([ |
| 4542 | { reason: "failed", activityFails: false }, |
| 4543 | { reason: "not-found", activityFails: false }, |
| 4544 | { reason: "failed", activityFails: true }, |
| 4545 | { reason: "not-found", activityFails: true }, |
| 4546 | { reason: "forbidden", activityFails: true }, |
| 4547 | ] as const)( |
| 4548 | "handles repeated host read failures: $reason (activity fails: $activityFails)", |
| 4549 | ({ reason, activityFails }) => |
| 4550 | Effect.gen(function* () { |
| 4551 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 4552 | const threadId = ThreadId.make( |
| 4553 | `runtime-pull-request-watch-unreadable-${reason}-${activityFails}`, |
| 4554 | ); |
| 4555 | const projectId = ProjectId.make(`pr-watch-unreadable-project-${reason}-${activityFails}`); |
| 4556 | yield* seedProject({ |
| 4557 | projectId, |
| 4558 | title: "Watch unreadable", |
| 4559 | workspaceRoot: "/workspace/watch-unreadable", |
| 4560 | defaultModelSelection: null, |
| 4561 | createdAt: "2026-10-01T00:00:00.000Z", |
| 4562 | }); |
| 4563 | yield* orchestrator.dispatch({ |
| 4564 | type: "thread.create", |
| 4565 | createdBy: "user", |
| 4566 | creationSource: "web", |
| 4567 | commandId: CommandId.make(`pr-watch-unreadable-create-${reason}-${activityFails}`), |
| 4568 | threadId, |
| 4569 | projectId, |
| 4570 | title: "Watch unreadable", |
| 4571 | modelSelection, |
| 4572 | runtimeMode: "full-access", |
| 4573 | interactionMode: "default", |
| 4574 | branch: null, |
| 4575 | worktreePath: null, |
| 4576 | }); |
| 4577 | yield* orchestrator.dispatch({ |
| 4578 | type: "thread.pull-request.watch", |
| 4579 | commandId: CommandId.make(`pr-watch-unreadable-start-${reason}-${activityFails}`), |
| 4580 | threadId, |
| 4581 | host: "github.com", |
| 4582 | repository: "pingdotgg/t3code", |
| 4583 | number: 8, |
| 4584 | watching: true, |
| 4585 | link: { url: "https://github.com/pingdotgg/t3code/pull/8", source: "agent" }, |
| 4586 | }); |
| 4587 | let disconnected = false; |
| 4588 | let passed = false; |
| 4589 | let genericFailure = false; |
| 4590 | const activityFailures = yield* Queue.unbounded<void>(); |
| 4591 | const readFailure = (operation: string, failure: "failed" | "not-found" | "forbidden") => |
| 4592 | new PullRequestOperationError({ |
| 4593 | operation, |
| 4594 | detail: "host read failed", |
| 4595 | cause: new PullRequestProviderError({ |
| 4596 | provider: SourceControlProviderKind.make("github"), |
| 4597 | operation, |
| 4598 | reason: failure, |
| 4599 | detail: "host read failed", |
| 4600 | }), |
| 4601 | }); |
| 4602 | const reactor = yield* PullRequestWatchReactor.make.pipe( |
| 4603 | Effect.provide( |
| 4604 | Layer.mergeAll( |
| 4605 | NodeServices.layer, |
| 4606 | NodeCrypto.layer, |
| 4607 | Layer.mock(PullRequestService.PullRequestService)({ |
| 4608 | watchFingerprint: () => Effect.succeed(null), |
| 4609 | detail: () => |
| 4610 | genericFailure |
| 4611 | ? Effect.die("host read failed") |
| 4612 | : disconnected |
| 4613 | ? (activityFails ? Queue.take(activityFailures) : Effect.void).pipe( |
| 4614 | Effect.andThen(Effect.fail(readFailure("detail", reason))), |
| 4615 | ) |
| 4616 | : Effect.succeed({ |
| 4617 | ...watchedPullRequestDetail({ |
| 4618 | projectId, |
| 4619 | number: 8, |
| 4620 | at: "2026-10-02T12:00:00.000Z", |
| 4621 | }), |
| 4622 | headSha: "abc1234def", |
| 4623 | checks: [ |
| 4624 | { |
| 4625 | name: "test", |
| 4626 | status: passed ? ("success" as const) : ("pending" as const), |
| 4627 | description: null, |
| 4628 | url: null, |
| 4629 | required: true, |
| 4630 | }, |
| 4631 | ], |
| 4632 | }), |
| 4633 | activity: () => |
| 4634 | disconnected && activityFails |
| 4635 | ? Queue.offer(activityFailures, undefined).pipe( |
| 4636 | Effect.andThen(Effect.fail(readFailure("activity", "failed"))), |
| 4637 | ) |
| 4638 | : Effect.succeed({ |
| 4639 | comments: [], |
| 4640 | commentCount: 0, |
| 4641 | commentsTruncated: false, |
| 4642 | reviewThreads: [], |
| 4643 | commits: [], |
| 4644 | }), |
| 4645 | }), |
| 4646 | ), |
| 4647 | ), |
| 4648 | ); |
| 4649 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 4650 | const sweep = reactor.sweep.pipe(Effect.andThen(worker.drain())); |
| 4651 | const summaries = Effect.map( |
| 4652 | orchestrator.getThreadRecords(threadId, ["messages"]), |
| 4653 | ({ messages }) => messages.flatMap((message) => message.notification?.summary ?? []), |
| 4654 | ); |
| 4655 | yield* sweep; |
| 4656 | assert.deepEqual(yield* summaries, []); |
| 4657 | disconnected = true; |
| 4658 | for (let pass = 0; pass < 15; pass += 1) { |
| 4659 | genericFailure = reason === "failed" && (pass === 0 || (pass >= 2 && pass <= 8)); |
| 4660 | yield* TestClock.adjust("2 minutes"); |
| 4661 | yield* sweep; |
| 4662 | } |
| 4663 | |
| 4664 | const thread = yield* orchestrator.getThreadShell(threadId); |
| 4665 | if (reason !== "failed") { |
| 4666 | assert.isUndefined(thread?.pullRequests?.[0]?.watch); |
| 4667 | assert.deepEqual(yield* summaries, ["#8: stopped watching, could not read it"]); |
| 4668 | return; |
| 4669 | } |
| 4670 | assert.isDefined(thread?.pullRequests?.[0]?.watch); |
| 4671 | assert.deepEqual(yield* summaries, []); |
| 4672 | disconnected = false; |
| 4673 | genericFailure = false; |
| 4674 | passed = true; |
| 4675 | yield* sweep; |
| 4676 | assert.deepEqual(yield* summaries, ["#8: checks passed"]); |
| 4677 | yield* TestClock.adjust("10 minutes"); |
| 4678 | yield* sweep; |
| 4679 | assert.deepEqual(yield* summaries, ["#8: checks passed"]); |
| 4680 | yield* orchestrator.dispatch({ |
| 4681 | type: "thread.pull-request.watch", |
| 4682 | commandId: CommandId.make(`pr-watch-unreadable-stop-${reason}-${activityFails}`), |
| 4683 | threadId, |
| 4684 | host: "github.com", |
| 4685 | repository: "pingdotgg/t3code", |
| 4686 | number: 8, |
| 4687 | watching: false, |
| 4688 | }); |
| 4689 | yield* worker.drain(); |
| 4690 | }), |
| 4691 | ); |
| 4692 | |
| 4693 | // regression: watched #26248 merged and its agent kept saying it was still watching |
| 4694 | it.effect("wakes once when a watched pull request merges, naming the merge commit", () => |
| 4695 | Effect.gen(function* () { |
| 4696 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 4697 | const threadId = ThreadId.make("runtime-pull-request-watch-merged"); |
| 4698 | const projectId = ProjectId.make("pr-watch-merged-project"); |
| 4699 | yield* seedProject({ |
| 4700 | projectId, |
| 4701 | title: "Watch merged", |
| 4702 | workspaceRoot: "/workspace/watch-merged", |
| 4703 | defaultModelSelection: null, |
| 4704 | createdAt: "2026-10-01T00:00:00.000Z", |
| 4705 | }); |
| 4706 | yield* orchestrator.dispatch({ |
| 4707 | type: "thread.create", |
| 4708 | createdBy: "user", |
| 4709 | creationSource: "web", |
| 4710 | commandId: CommandId.make("pr-watch-merged-create"), |
| 4711 | threadId, |
| 4712 | projectId, |
| 4713 | title: "Watch merged", |
| 4714 | modelSelection, |
| 4715 | runtimeMode: "full-access", |
| 4716 | interactionMode: "default", |
| 4717 | branch: null, |
| 4718 | worktreePath: null, |
| 4719 | }); |
| 4720 | yield* orchestrator.dispatch({ |
| 4721 | type: "thread.pull-request.watch", |
| 4722 | commandId: CommandId.make("pr-watch-merged-start"), |
| 4723 | threadId, |
| 4724 | host: "github.com", |
| 4725 | repository: "pingdotgg/t3code", |
| 4726 | number: 15, |
| 4727 | watching: true, |
| 4728 | link: { url: "https://github.com/pingdotgg/t3code/pull/15", source: "agent" }, |
| 4729 | }); |
| 4730 | const reactor = yield* PullRequestWatchReactor.make.pipe( |
| 4731 | Effect.provide( |
| 4732 | Layer.mergeAll( |
| 4733 | NodeServices.layer, |
| 4734 | NodeCrypto.layer, |
| 4735 | Layer.mock(PullRequestService.PullRequestService)({ |
| 4736 | detail: () => |
| 4737 | Effect.succeed({ |
| 4738 | ...watchedPullRequestDetail({ |
| 4739 | projectId, |
| 4740 | number: 15, |
| 4741 | at: "2026-10-02T12:00:00.000Z", |
| 4742 | }), |
| 4743 | state: "merged" as const, |
| 4744 | mergedAt: "2026-10-02T12:00:00.000Z", |
| 4745 | mergeCommitSha: "3b2f958603a51d8c42c4ede02eee7b8cb310b2d9", |
| 4746 | mergedBy: { login: "juliusmarminge", name: null, avatarUrl: null }, |
| 4747 | }), |
| 4748 | activity: () => |
| 4749 | Effect.succeed({ |
| 4750 | comments: [], |
| 4751 | commentCount: 0, |
| 4752 | commentsTruncated: false, |
| 4753 | reviewThreads: [], |
| 4754 | commits: [], |
| 4755 | }), |
| 4756 | }), |
| 4757 | ), |
| 4758 | ), |
| 4759 | ); |
| 4760 | yield* reactor.sweep; |
| 4761 | yield* TestClock.adjust("10 minutes"); |
| 4762 | yield* reactor.sweep; |
| 4763 | |
| 4764 | assert.isUndefined((yield* orchestrator.getThreadShell(threadId))?.pullRequests?.[0]?.watch); |
| 4765 | const wakes = (yield* orchestrator.getThreadRecords(threadId, ["messages"])).messages.filter( |
| 4766 | (message) => message.notification != null, |
| 4767 | ); |
| 4768 | assert.deepEqual( |
| 4769 | wakes.map((message) => message.notification?.summary), |
| 4770 | ["#15: merged, stopped watching"], |
| 4771 | ); |
| 4772 | assert.include(wakes[0]!.text, "merged into main by juliusmarminge as 3b2f958"); |
| 4773 | }), |
| 4774 | ); |
| 4775 | |
| 4776 | it.effect.each([ |
| 4777 | // The agent's merge of #19 needs no wake, but its command did not close #20. |
| 4778 | { name: "completed", status: "completed", wakes: ["#20: closed, stopped watching"] }, |
| 4779 | // A merge command that failed ended nothing itself. |
| 4780 | { |
| 4781 | name: "failed", |
| 4782 | status: "failed", |
| 4783 | wakes: ["#19: merged, stopped watching", "#20: closed, stopped watching"], |
| 4784 | }, |
| 4785 | { |
| 4786 | name: "exit-1", |
| 4787 | status: "completed", |
| 4788 | exitCode: 1, |
| 4789 | wakes: ["#19: merged, stopped watching", "#20: closed, stopped watching"], |
| 4790 | }, |
| 4791 | { |
| 4792 | name: "failure-output", |
| 4793 | status: "completed", |
| 4794 | output: "GraphQL: Pull request is not mergeable\n<exited with exit code 1>", |
| 4795 | wakes: ["#19: merged, stopped watching", "#20: closed, stopped watching"], |
| 4796 | }, |
| 4797 | ] as const)( |
| 4798 | "ends a watch without a wake when the thread's own agent merged it ($name)", |
| 4799 | ({ name: key, status: ended, wakes: expected, ...result }) => |
| 4800 | Effect.gen(function* () { |
| 4801 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 4802 | const threadId = ThreadId.make(`runtime-pull-request-watch-own-merge-${key}`); |
| 4803 | const projectId = ProjectId.make(`pr-watch-own-merge-${key}-project`); |
| 4804 | yield* seedProject({ |
| 4805 | projectId, |
| 4806 | title: "Watch own merge", |
| 4807 | workspaceRoot: `/workspace/watch-own-merge-${key}`, |
| 4808 | defaultModelSelection: null, |
| 4809 | createdAt: "2026-10-01T00:00:00.000Z", |
| 4810 | }); |
| 4811 | yield* orchestrator.dispatch({ |
| 4812 | type: "thread.create", |
| 4813 | createdBy: "user", |
| 4814 | creationSource: "web", |
| 4815 | commandId: CommandId.make(`pr-watch-own-merge-${key}-create`), |
| 4816 | threadId, |
| 4817 | projectId, |
| 4818 | title: "Watch own merge", |
| 4819 | modelSelection, |
| 4820 | runtimeMode: "full-access", |
| 4821 | interactionMode: "default", |
| 4822 | branch: null, |
| 4823 | worktreePath: null, |
| 4824 | }); |
| 4825 | for (const number of [19, 20]) { |
| 4826 | yield* orchestrator.dispatch({ |
| 4827 | type: "thread.pull-request.watch", |
| 4828 | commandId: CommandId.make(`pr-watch-own-merge-${key}-start-${number}`), |
| 4829 | threadId, |
| 4830 | host: "github.com", |
| 4831 | repository: "pingdotgg/t3code", |
| 4832 | number, |
| 4833 | watching: true, |
| 4834 | link: { url: `https://github.com/pingdotgg/t3code/pull/${number}`, source: "agent" }, |
| 4835 | }); |
| 4836 | } |
| 4837 | |
| 4838 | const states = new Map<number, PullRequestDetail["state"]>([ |
| 4839 | [19, "open"], |
| 4840 | [20, "open"], |
| 4841 | ]); |
| 4842 | const detailReads = yield* Queue.unbounded<number>(); |
| 4843 | const agentRead = yield* Deferred.make<void>(); |
| 4844 | // Each of the reactor's subscribers reads every event, as each event sink stream does. |
| 4845 | const agentEvents = yield* PubSub.unbounded<OrchestrationV2DomainEvent>({ replay: 16 }); |
| 4846 | const host = Layer.mergeAll( |
| 4847 | NodeServices.layer, |
| 4848 | NodeCrypto.layer, |
| 4849 | Layer.mock(PullRequestService.PullRequestService)({ |
| 4850 | detail: (input) => |
| 4851 | Effect.sync(() => states.get(input.number)!).pipe( |
| 4852 | Effect.tap(() => Queue.offer(detailReads, input.number)), |
| 4853 | // Reads of an open pull request are slow, so the first sweep outlasts the merge. |
| 4854 | Effect.tap((state) => (state === "open" ? Effect.sleep("1 second") : Effect.void)), |
| 4855 | Effect.map((state) => ({ |
| 4856 | ...watchedPullRequestDetail({ |
| 4857 | projectId, |
| 4858 | number: input.number, |
| 4859 | at: "2026-10-02T12:00:00.000Z", |
| 4860 | }), |
| 4861 | state, |
| 4862 | })), |
| 4863 | ), |
| 4864 | activity: () => |
| 4865 | Effect.succeed({ |
| 4866 | comments: [], |
| 4867 | commentCount: 0, |
| 4868 | commentsTruncated: false, |
| 4869 | reviewThreads: [], |
| 4870 | commits: [], |
| 4871 | }), |
| 4872 | // A fingerprint that never changes, so only a forced read sees an end. |
| 4873 | watchFingerprint: () => Effect.succeed({ status: "steady", remarks: "" }), |
| 4874 | invalidate: () => Effect.void, |
| 4875 | changes: () => Stream.never, |
| 4876 | }), |
| 4877 | Layer.succeed(Orchestrator.OrchestratorV2, { |
| 4878 | ...orchestrator, |
| 4879 | streamDomainEvents: Stream.fromPubSub(agentEvents), |
| 4880 | }), |
| 4881 | // Only the read at a command's end asks for one thread's watches. |
| 4882 | Layer.effect( |
| 4883 | ProjectionStore.ProjectionStoreV2, |
| 4884 | Effect.map(ProjectionStore.ProjectionStoreV2, (store) => ({ |
| 4885 | ...store, |
| 4886 | getThreadsWithPullRequests: (thread?: ThreadId) => |
| 4887 | store |
| 4888 | .getThreadsWithPullRequests(thread) |
| 4889 | .pipe( |
| 4890 | Effect.tap(() => |
| 4891 | thread === undefined ? Effect.void : Deferred.succeed(agentRead, undefined), |
| 4892 | ), |
| 4893 | ), |
| 4894 | })), |
| 4895 | ), |
| 4896 | ); |
| 4897 | const runId = RunId.make(`run:${threadId}:1`); |
| 4898 | const command = (status: "running" | typeof ended) => |
| 4899 | Effect.map(DateTime.now, (at): OrchestrationV2DomainEvent => ({ |
| 4900 | type: "turn-item.updated", |
| 4901 | id: EventId.make(`event:${threadId}:merge-${status}`), |
| 4902 | threadId, |
| 4903 | runId, |
| 4904 | occurredAt: at, |
| 4905 | payload: { |
| 4906 | id: TurnItemId.make(`item:${threadId}:merge`), |
| 4907 | threadId, |
| 4908 | runId, |
| 4909 | nodeId: null, |
| 4910 | providerThreadId: null, |
| 4911 | providerTurnId: null, |
| 4912 | nativeItemRef: null, |
| 4913 | parentItemId: null, |
| 4914 | ordinal: 1, |
| 4915 | status, |
| 4916 | title: null, |
| 4917 | startedAt: at, |
| 4918 | completedAt: status === "completed" ? at : null, |
| 4919 | updatedAt: at, |
| 4920 | type: "command_execution", |
| 4921 | input: "gh pr merge 19 --squash", |
| 4922 | ...(status === "running" ? {} : result), |
| 4923 | }, |
| 4924 | })); |
| 4925 | const reactor = yield* PullRequestWatchReactor.make.pipe(Effect.provide(host)); |
| 4926 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 4927 | yield* reactor.start(); |
| 4928 | // The first sweep has read both as open, and has not recorded that yet. |
| 4929 | yield* Queue.take(detailReads); |
| 4930 | yield* Queue.take(detailReads); |
| 4931 | |
| 4932 | // While the agent merges #19, someone else closes #20. |
| 4933 | yield* PubSub.publish(agentEvents, yield* command("running")); |
| 4934 | states.set(19, "merged"); |
| 4935 | states.set(20, "closed"); |
| 4936 | // The command's end reads the thread's watches in full, after that sweep. |
| 4937 | yield* PubSub.publish(agentEvents, yield* command(ended)); |
| 4938 | yield* Deferred.await(agentRead); |
| 4939 | yield* TestClock.adjust("1 second"); |
| 4940 | yield* Queue.take(detailReads); |
| 4941 | yield* Queue.take(detailReads); |
| 4942 | // A sweep waits for that read to finish. |
| 4943 | yield* reactor.sweep; |
| 4944 | yield* worker.drain(); |
| 4945 | const thread = yield* orchestrator.getThreadShell(threadId); |
| 4946 | assert.isTrue(thread?.pullRequests?.every((link) => link.watch === undefined)); |
| 4947 | const wakes = (yield* orchestrator.getThreadRecords(threadId, [ |
| 4948 | "messages", |
| 4949 | ])).messages.flatMap((message) => message.notification?.summary ?? []); |
| 4950 | assert.sameMembers(wakes, [...expected]); |
| 4951 | }), |
| 4952 | ); |
| 4953 | |
| 4954 | it.effect("a pull request closed, reopened, and then merged wakes once for each end", () => |
| 4955 | Effect.gen(function* () { |
| 4956 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 4957 | const threadId = ThreadId.make("runtime-pull-request-watch-reopened"); |
| 4958 | const projectId = ProjectId.make("pr-watch-reopened-project"); |
| 4959 | yield* seedProject({ |
| 4960 | projectId, |
| 4961 | title: "Watch reopened", |
| 4962 | workspaceRoot: "/workspace/watch-reopened", |
| 4963 | defaultModelSelection: null, |
| 4964 | createdAt: "2026-10-01T00:00:00.000Z", |
| 4965 | }); |
| 4966 | yield* orchestrator.dispatch({ |
| 4967 | type: "thread.create", |
| 4968 | createdBy: "user", |
| 4969 | creationSource: "web", |
| 4970 | commandId: CommandId.make("pr-watch-reopened-create"), |
| 4971 | threadId, |
| 4972 | projectId, |
| 4973 | title: "Watch reopened", |
| 4974 | modelSelection, |
| 4975 | runtimeMode: "full-access", |
| 4976 | interactionMode: "default", |
| 4977 | branch: null, |
| 4978 | worktreePath: null, |
| 4979 | }); |
| 4980 | const key = { host: "github.com", repository: "pingdotgg/t3code", number: 16 }; |
| 4981 | const watch = (id: string) => |
| 4982 | orchestrator.dispatch({ |
| 4983 | type: "thread.pull-request.watch", |
| 4984 | commandId: CommandId.make(`pr-watch-reopened-${id}`), |
| 4985 | threadId, |
| 4986 | ...key, |
| 4987 | watching: true, |
| 4988 | link: { url: "https://github.com/pingdotgg/t3code/pull/16", source: "agent" }, |
| 4989 | }); |
| 4990 | yield* watch("start"); |
| 4991 | |
| 4992 | let state: PullRequestDetail["state"] = "closed"; |
| 4993 | const host = Layer.mergeAll( |
| 4994 | NodeServices.layer, |
| 4995 | NodeCrypto.layer, |
| 4996 | Layer.mock(PullRequestService.PullRequestService)({ |
| 4997 | detail: () => |
| 4998 | Effect.sync(() => ({ |
| 4999 | ...watchedPullRequestDetail({ |
| 5000 | projectId, |
Showing first 5,000 of 14,011 lines.
View raw