The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | import { assert, it } from "@effect/vitest"; |
| 3 | import { |
| 4 | CheckpointScopeId, |
| 5 | ProjectId, |
| 6 | RunId, |
| 7 | ThreadId, |
| 8 | type OrchestrationV2ThreadShell, |
| 9 | type Project, |
| 10 | type RepositoryIdentity, |
| 11 | } from "@t3tools/contracts"; |
| 12 | import * as Deferred from "effect/Deferred"; |
| 13 | import * as Effect from "effect/Effect"; |
| 14 | import * as Fiber from "effect/Fiber"; |
| 15 | import * as FileSystem from "effect/FileSystem"; |
| 16 | import * as Layer from "effect/Layer"; |
| 17 | import * as Option from "effect/Option"; |
| 18 | import * as PubSub from "effect/PubSub"; |
| 19 | import * as TestClock from "effect/testing/TestClock"; |
| 20 | |
| 21 | import * as ProcessRunner from "../processRunner.ts"; |
| 22 | import * as ProjectEnrichment from "../project/ProjectEnrichmentService.ts"; |
| 23 | import * as ProjectFaviconResolver from "../project/ProjectFaviconResolver.ts"; |
| 24 | import * as ProjectService from "../project/ProjectService.ts"; |
| 25 | import * as RepositoryIdentityResolver from "../project/RepositoryIdentityResolver.ts"; |
| 26 | import * as PullRequestService from "../pullRequest/PullRequestService.ts"; |
| 27 | import * as VcsStatusBroadcaster from "../vcs/VcsStatusBroadcaster.ts"; |
| 28 | import * as WorkspaceEntries from "../workspace/WorkspaceEntries.ts"; |
| 29 | import * as CheckpointCapture from "./CheckpointCaptureService.ts"; |
| 30 | import * as ProjectionStore from "./ProjectionStore.ts"; |
| 31 | import * as RunFinalization from "./RunFinalizationService.ts"; |
| 32 | |
| 33 | // Without a project the observer never touches enrichment, so its methods stay unimplemented. |
| 34 | const layerNoProject = Layer.mergeAll( |
| 35 | Layer.mock(ProjectService.ProjectService)({ getById: () => Effect.succeedNone }), |
| 36 | Layer.mock(ProjectEnrichment.ProjectEnrichmentService)({}), |
| 37 | ); |
| 38 | |
| 39 | const finalizationLayer = ( |
| 40 | scopeId: CheckpointScopeId, |
| 41 | runId: RunId, |
| 42 | steps: string[], |
| 43 | refresh: (typeof RunFinalization.RunFinalizationObserver)["Service"]["refresh"], |
| 44 | ) => |
| 45 | RunFinalization.layer.pipe( |
| 46 | Layer.provide( |
| 47 | Layer.mergeAll( |
| 48 | Layer.mock(CheckpointCapture.CheckpointCaptureServiceV2)({ |
| 49 | execute: () => |
| 50 | Effect.sync(() => { |
| 51 | steps.push("checkpoint"); |
| 52 | }), |
| 53 | }), |
| 54 | Layer.mock(ProjectionStore.ProjectionStoreV2)({ |
| 55 | getCheckpointContext: () => |
| 56 | Effect.succeed({ |
| 57 | runs: [], |
| 58 | checkpointScopes: [{ id: scopeId, runId, kind: "root_run" as const, cwd: "/repo" }], |
| 59 | checkpoints: [], |
| 60 | }), |
| 61 | }), |
| 62 | Layer.succeed(RunFinalization.RunFinalizationObserver, { |
| 63 | refresh, |
| 64 | refreshAfterTurn: () => Effect.void, |
| 65 | }), |
| 66 | ), |
| 67 | ), |
| 68 | ); |
| 69 | |
| 70 | it.effect("completes finalization when the post-checkpoint workspace refresh fails", () => { |
| 71 | const threadId = ThreadId.make("thread_refresh_failure"); |
| 72 | const runId = RunId.make("run_refresh_failure"); |
| 73 | const scopeId = CheckpointScopeId.make("scope_refresh_failure"); |
| 74 | const captured: string[] = []; |
| 75 | const layer = finalizationLayer(scopeId, runId, captured, () => |
| 76 | Effect.gen(function* () { |
| 77 | captured.push("refresh"); |
| 78 | return yield* new RunFinalization.RunFinalizationRefreshError({ |
| 79 | cwd: "/repo", |
| 80 | cause: new Error("Git command timed out"), |
| 81 | }); |
| 82 | }), |
| 83 | ); |
| 84 | return Effect.gen(function* () { |
| 85 | const service = yield* RunFinalization.RunFinalizationService; |
| 86 | yield* service.finalize({ threadId, runId, scopeId }); |
| 87 | assert.deepEqual(captured, ["checkpoint", "refresh"]); |
| 88 | }).pipe(Effect.provide(layer)); |
| 89 | }); |
| 90 | |
| 91 | it.effect("completes finalization without waiting for the display refresh", () => { |
| 92 | const threadId = ThreadId.make("thread_slow_refresh"); |
| 93 | const runId = RunId.make("run_slow_refresh"); |
| 94 | const scopeId = CheckpointScopeId.make("scope_slow_refresh"); |
| 95 | const steps: string[] = []; |
| 96 | const release = Deferred.makeUnsafe<void>(); |
| 97 | const refreshed = Deferred.makeUnsafe<void>(); |
| 98 | const layer = finalizationLayer(scopeId, runId, steps, () => |
| 99 | Deferred.await(release).pipe(Effect.andThen(Deferred.succeed(refreshed, undefined))), |
| 100 | ); |
| 101 | return Effect.gen(function* () { |
| 102 | const service = yield* RunFinalization.RunFinalizationService; |
| 103 | const finalize = yield* service |
| 104 | .finalize({ threadId, runId, scopeId }) |
| 105 | .pipe(Effect.timeoutOption("1 minute"), Effect.forkChild({ startImmediately: true })); |
| 106 | yield* TestClock.adjust("1 minute"); |
| 107 | assert.isTrue( |
| 108 | Option.isSome(yield* Fiber.join(finalize)), |
| 109 | "finalization waited on the display refresh, blocking the thread's next turn", |
| 110 | ); |
| 111 | yield* Deferred.succeed(release, undefined); |
| 112 | yield* Deferred.await(refreshed); |
| 113 | }).pipe(Effect.provide(layer)); |
| 114 | }); |
| 115 | |
| 116 | it.effect.each( |
| 117 | ( |
| 118 | [ |
| 119 | { |
| 120 | label: "discovers a new PR for the completed run's branch", |
| 121 | branch: "feature", |
| 122 | checkedOut: "feature", |
| 123 | activeRun: null, |
| 124 | expected: ["/repo"], |
| 125 | }, |
| 126 | { |
| 127 | label: "leaves the default branch's PR cache alone", |
| 128 | branch: "main", |
| 129 | checkedOut: "main", |
| 130 | activeRun: null, |
| 131 | expected: [], |
| 132 | }, |
| 133 | { |
| 134 | label: "does not refresh another thread's checkout", |
| 135 | branch: "feature", |
| 136 | checkedOut: "other", |
| 137 | activeRun: null, |
| 138 | expected: [], |
| 139 | }, |
| 140 | { |
| 141 | label: "does not refresh during a newer active run", |
| 142 | branch: "feature", |
| 143 | checkedOut: "feature", |
| 144 | activeRun: "newer-run", |
| 145 | expected: [], |
| 146 | }, |
| 147 | ] as const |
| 148 | ).map((scenario) => [scenario.label, scenario] as const), |
| 149 | )("%s", ([, scenario]) => { |
| 150 | const refreshed: string[] = []; |
| 151 | const lookedUp = Deferred.makeUnsafe<void>(); |
| 152 | const threadId = ThreadId.make("thread-pr-refresh"); |
| 153 | const runId = RunId.make("completed-run"); |
| 154 | const layer = RunFinalization.layerObserver.pipe( |
| 155 | Layer.provide( |
| 156 | Layer.mergeAll( |
| 157 | layerNoProject, |
| 158 | Layer.mock(WorkspaceEntries.WorkspaceEntries)({ refresh: () => Effect.void }), |
| 159 | Layer.mock(PullRequestService.PullRequestService)({ |
| 160 | refreshAfterTurn: () => Effect.void, |
| 161 | }), |
| 162 | Layer.mock(VcsStatusBroadcaster.VcsStatusBroadcaster)({ |
| 163 | refreshLocalStatus: () => |
| 164 | Effect.succeed({ |
| 165 | isRepo: true, |
| 166 | hasPrimaryRemote: true, |
| 167 | isDefaultRef: scenario.checkedOut === "main", |
| 168 | refName: scenario.checkedOut, |
| 169 | hasWorkingTreeChanges: false, |
| 170 | workingTree: { files: [], insertions: 0, deletions: 0 }, |
| 171 | }), |
| 172 | refreshStatus: () => |
| 173 | Effect.die("turn completion must preserve known PRs and lookup backoff"), |
| 174 | // Requested synchronously by refresh; the lookup itself runs in the background. |
| 175 | refreshPullRequestStatus: (cwd) => { |
| 176 | refreshed.push(cwd); |
| 177 | return Deferred.succeed(lookedUp, undefined).pipe(Effect.as(null)); |
| 178 | }, |
| 179 | }), |
| 180 | Layer.mock(ProjectionStore.ProjectionStoreV2)({ |
| 181 | getThreadShell: () => |
| 182 | Effect.succeed({ |
| 183 | id: threadId, |
| 184 | branch: scenario.branch, |
| 185 | activeRunId: scenario.activeRun === null ? null : RunId.make(scenario.activeRun), |
| 186 | } as OrchestrationV2ThreadShell), |
| 187 | }), |
| 188 | ), |
| 189 | ), |
| 190 | ); |
| 191 | return Effect.gen(function* () { |
| 192 | const observer = yield* RunFinalization.RunFinalizationObserver; |
| 193 | yield* observer.refresh({ cwd: "/repo", threadId, runId }); |
| 194 | if (scenario.expected.length > 0) yield* Deferred.await(lookedUp); |
| 195 | assert.deepEqual(refreshed, [...scenario.expected]); |
| 196 | }).pipe(Effect.provide(layer)); |
| 197 | }); |
| 198 | |
| 199 | it.effect("returns from the post-turn refresh without waiting for the PR lookup", () => { |
| 200 | const threadId = ThreadId.make("thread-slow-forge"); |
| 201 | const runId = RunId.make("completed-run"); |
| 202 | const layer = RunFinalization.layerObserver.pipe( |
| 203 | Layer.provide( |
| 204 | Layer.mergeAll( |
| 205 | layerNoProject, |
| 206 | Layer.mock(WorkspaceEntries.WorkspaceEntries)({ refresh: () => Effect.void }), |
| 207 | Layer.mock(PullRequestService.PullRequestService)({ |
| 208 | refreshAfterTurn: () => Effect.void, |
| 209 | }), |
| 210 | Layer.mock(VcsStatusBroadcaster.VcsStatusBroadcaster)({ |
| 211 | refreshLocalStatus: () => |
| 212 | Effect.succeed({ |
| 213 | isRepo: true, |
| 214 | hasPrimaryRemote: true, |
| 215 | isDefaultRef: false, |
| 216 | refName: "feature", |
| 217 | hasWorkingTreeChanges: false, |
| 218 | workingTree: { files: [], insertions: 0, deletions: 0 }, |
| 219 | }), |
| 220 | refreshPullRequestStatus: () => Effect.never, |
| 221 | }), |
| 222 | Layer.mock(ProjectionStore.ProjectionStoreV2)({ |
| 223 | getThreadShell: () => |
| 224 | Effect.succeed({ |
| 225 | id: threadId, |
| 226 | branch: "feature", |
| 227 | activeRunId: null, |
| 228 | } as OrchestrationV2ThreadShell), |
| 229 | }), |
| 230 | ), |
| 231 | ), |
| 232 | ); |
| 233 | return Effect.gen(function* () { |
| 234 | const observer = yield* RunFinalization.RunFinalizationObserver; |
| 235 | const refresh = yield* observer |
| 236 | .refresh({ cwd: "/repo", threadId, runId }) |
| 237 | .pipe(Effect.timeoutOption("1 minute"), Effect.forkChild({ startImmediately: true })); |
| 238 | yield* TestClock.adjust("1 minute"); |
| 239 | const result = yield* Fiber.join(refresh); |
| 240 | assert.isTrue( |
| 241 | Option.isSome(result), |
| 242 | "turn finalization waited on the forge PR lookup, blocking the thread's next turn", |
| 243 | ); |
| 244 | }).pipe(Effect.provide(layer)); |
| 245 | }); |
| 246 | |
| 247 | it.effect("replaces a still-running PR lookup when a newer turn finishes", () => { |
| 248 | const threadId = ThreadId.make("thread-back-to-back"); |
| 249 | const runId = RunId.make("completed-run"); |
| 250 | const firstInterrupted = Deferred.makeUnsafe<void>(); |
| 251 | const secondLookedUp = Deferred.makeUnsafe<void>(); |
| 252 | let lookups = 0; |
| 253 | const layer = RunFinalization.layerObserver.pipe( |
| 254 | Layer.provide( |
| 255 | Layer.mergeAll( |
| 256 | layerNoProject, |
| 257 | Layer.mock(WorkspaceEntries.WorkspaceEntries)({ refresh: () => Effect.void }), |
| 258 | Layer.mock(PullRequestService.PullRequestService)({ |
| 259 | refreshAfterTurn: () => Effect.void, |
| 260 | }), |
| 261 | Layer.mock(VcsStatusBroadcaster.VcsStatusBroadcaster)({ |
| 262 | refreshLocalStatus: () => |
| 263 | Effect.succeed({ |
| 264 | isRepo: true, |
| 265 | hasPrimaryRemote: true, |
| 266 | isDefaultRef: false, |
| 267 | refName: "feature", |
| 268 | hasWorkingTreeChanges: false, |
| 269 | workingTree: { files: [], insertions: 0, deletions: 0 }, |
| 270 | }), |
| 271 | refreshPullRequestStatus: () => |
| 272 | ++lookups === 1 |
| 273 | ? Effect.never.pipe( |
| 274 | Effect.onInterrupt(() => Deferred.succeed(firstInterrupted, undefined)), |
| 275 | ) |
| 276 | : Deferred.succeed(secondLookedUp, undefined).pipe(Effect.as(null)), |
| 277 | }), |
| 278 | Layer.mock(ProjectionStore.ProjectionStoreV2)({ |
| 279 | getThreadShell: () => |
| 280 | Effect.succeed({ |
| 281 | id: threadId, |
| 282 | branch: "feature", |
| 283 | activeRunId: null, |
| 284 | } as OrchestrationV2ThreadShell), |
| 285 | }), |
| 286 | ), |
| 287 | ), |
| 288 | ); |
| 289 | return Effect.gen(function* () { |
| 290 | const observer = yield* RunFinalization.RunFinalizationObserver; |
| 291 | yield* observer.refresh({ cwd: "/repo", threadId, runId }); |
| 292 | yield* observer.refresh({ cwd: "/repo", threadId, runId }); |
| 293 | yield* Deferred.await(firstInterrupted); |
| 294 | yield* Deferred.await(secondLookedUp); |
| 295 | assert.strictEqual(lookups, 2); |
| 296 | }).pipe(Effect.provide(layer)); |
| 297 | }); |
| 298 | |
| 299 | const identityProjectId = ProjectId.make("project-identity-refresh"); |
| 300 | const identityThreadId = ThreadId.make("thread-identity-refresh"); |
| 301 | const identityRunId = RunId.make("completed-run"); |
| 302 | |
| 303 | const layerIdentityRefresh = <R>( |
| 304 | workspaceRoot: string, |
| 305 | resolver: Layer.Layer<RepositoryIdentityResolver.RepositoryIdentityResolver, never, R>, |
| 306 | ) => |
| 307 | RunFinalization.layerObserver.pipe( |
| 308 | Layer.provideMerge( |
| 309 | Layer.mergeAll( |
| 310 | Layer.effect(ProjectEnrichment.ProjectEnrichmentService, ProjectEnrichment.make()).pipe( |
| 311 | Layer.provide( |
| 312 | Layer.mergeAll( |
| 313 | resolver, |
| 314 | Layer.succeed(ProjectFaviconResolver.ProjectFaviconResolver, { |
| 315 | invalidate: () => Effect.void, |
| 316 | resolvePath: () => Effect.succeed(null), |
| 317 | }), |
| 318 | ), |
| 319 | ), |
| 320 | ), |
| 321 | Layer.mock(WorkspaceEntries.WorkspaceEntries)({ refresh: () => Effect.void }), |
| 322 | Layer.mock(PullRequestService.PullRequestService)({ refreshAfterTurn: () => Effect.void }), |
| 323 | Layer.mock(VcsStatusBroadcaster.VcsStatusBroadcaster)({ |
| 324 | refreshLocalStatus: () => |
| 325 | Effect.succeed({ |
| 326 | isRepo: true, |
| 327 | hasPrimaryRemote: true, |
| 328 | isDefaultRef: true, |
| 329 | refName: "main", |
| 330 | hasWorkingTreeChanges: false, |
| 331 | workingTree: { files: [], insertions: 0, deletions: 0 }, |
| 332 | }), |
| 333 | }), |
| 334 | Layer.mock(ProjectionStore.ProjectionStoreV2)({ |
| 335 | getThreadShell: () => |
| 336 | Effect.succeed({ |
| 337 | id: identityThreadId, |
| 338 | projectId: identityProjectId, |
| 339 | branch: "main", |
| 340 | activeRunId: null, |
| 341 | } as OrchestrationV2ThreadShell), |
| 342 | }), |
| 343 | Layer.mock(ProjectService.ProjectService)({ |
| 344 | getById: () => Effect.succeedSome({ id: identityProjectId, workspaceRoot } as Project), |
| 345 | }), |
| 346 | ), |
| 347 | ), |
| 348 | ); |
| 349 | |
| 350 | it.effect("resolves the identity of a project whose remote was added during the turn", () => |
| 351 | Effect.gen(function* () { |
| 352 | const fileSystem = yield* FileSystem.FileSystem; |
| 353 | const processRunner = yield* ProcessRunner.ProcessRunner; |
| 354 | const workspaceRoot = yield* fileSystem.makeTempDirectoryScoped({ |
| 355 | prefix: "t3-run-finalization-identity-test-", |
| 356 | }); |
| 357 | const git = (args: ReadonlyArray<string>) => |
| 358 | processRunner.run({ command: "git", args: ["-C", workspaceRoot, ...args] }); |
| 359 | yield* git(["init"]); |
| 360 | |
| 361 | yield* Effect.gen(function* () { |
| 362 | const enrichment = yield* ProjectEnrichment.ProjectEnrichmentService; |
| 363 | // A shell read before the turn caches the folder as having no repository. |
| 364 | assert.isNull(yield* enrichment.readRepositoryIdentity(workspaceRoot)); |
| 365 | |
| 366 | yield* git(["remote", "add", "origin", "https://github.com/t3-triage/example-29929.git"]); |
| 367 | const changes = yield* enrichment.subscribeChanges; |
| 368 | const observer = yield* RunFinalization.RunFinalizationObserver; |
| 369 | yield* observer.refresh({ |
| 370 | cwd: workspaceRoot, |
| 371 | threadId: identityThreadId, |
| 372 | runId: identityRunId, |
| 373 | }); |
| 374 | |
| 375 | const change = yield* PubSub.take(changes); |
| 376 | assert.strictEqual( |
| 377 | change.enrichment.repositoryIdentity?.locator.remoteUrl, |
| 378 | "https://github.com/t3-triage/example-29929.git", |
| 379 | "the project's repository identity is still null after the turn added a remote", |
| 380 | ); |
| 381 | }).pipe( |
| 382 | Effect.scoped, |
| 383 | Effect.provide(layerIdentityRefresh(workspaceRoot, RepositoryIdentityResolver.layer)), |
| 384 | ); |
| 385 | }).pipe( |
| 386 | Effect.scoped, |
| 387 | Effect.provide(ProcessRunner.layer.pipe(Layer.provideMerge(NodeServices.layer))), |
| 388 | ), |
| 389 | ); |
| 390 | |
| 391 | it.effect("returns from the post-turn refresh while the identity resolver is still running", () => { |
| 392 | const workspaceRoot = "/repo-stubbed-identity"; |
| 393 | const identity: RepositoryIdentity = { |
| 394 | canonicalKey: "example.test/stubbed-identity", |
| 395 | locator: { |
| 396 | source: "git-remote", |
| 397 | remoteName: "origin", |
| 398 | remoteUrl: "https://example.test/stubbed-identity.git", |
| 399 | }, |
| 400 | }; |
| 401 | const releaseResolver = Deferred.makeUnsafe<void>(); |
| 402 | let remoteAdded = false; |
| 403 | const resolver = Layer.succeed(RepositoryIdentityResolver.RepositoryIdentityResolver, { |
| 404 | resolve: (_cwd, options) => |
| 405 | options?.refresh |
| 406 | ? Deferred.await(releaseResolver).pipe( |
| 407 | Effect.map(() => { |
| 408 | remoteAdded = true; |
| 409 | return identity; |
| 410 | }), |
| 411 | ) |
| 412 | : Effect.sync(() => (remoteAdded ? identity : null)), |
| 413 | }); |
| 414 | return Effect.gen(function* () { |
| 415 | const enrichment = yield* ProjectEnrichment.ProjectEnrichmentService; |
| 416 | assert.isNull(yield* enrichment.readRepositoryIdentity(workspaceRoot)); |
| 417 | const changes = yield* enrichment.subscribeChanges; |
| 418 | const observer = yield* RunFinalization.RunFinalizationObserver; |
| 419 | const refresh = yield* observer |
| 420 | .refresh({ cwd: workspaceRoot, threadId: identityThreadId, runId: identityRunId }) |
| 421 | .pipe(Effect.timeoutOption("1 minute"), Effect.forkChild({ startImmediately: true })); |
| 422 | yield* TestClock.adjust("1 minute"); |
| 423 | assert.isTrue( |
| 424 | Option.isSome(yield* Fiber.join(refresh)), |
| 425 | "turn finalization waited on the identity resolver, blocking the thread's next turn", |
| 426 | ); |
| 427 | |
| 428 | yield* Deferred.succeed(releaseResolver, undefined); |
| 429 | const change = yield* PubSub.take(changes); |
| 430 | assert.strictEqual(change.enrichment.repositoryIdentity?.canonicalKey, identity.canonicalKey); |
| 431 | }).pipe(Effect.scoped, Effect.provide(layerIdentityRefresh(workspaceRoot, resolver))); |
| 432 | }); |