diff --git a/apps/server/src/orchestration-v2/PullRequestSyncReactor.test.ts b/apps/server/src/orchestration-v2/PullRequestSyncReactor.test.ts index bb0b3380c3..cfe60e9fa3 100644 --- a/apps/server/src/orchestration-v2/PullRequestSyncReactor.test.ts +++ b/apps/server/src/orchestration-v2/PullRequestSyncReactor.test.ts @@ -186,6 +186,7 @@ interface HarnessOptions { input: PullRequestRef, ) => Effect.Effect; readonly onSync?: (command: SyncCommand) => Effect.Effect; + readonly onLink?: (command: LinkCommand) => Effect.Effect; } const makeHarness = Effect.fn("makePullRequestSyncHarness")(function* (options: HarnessOptions) { @@ -227,6 +228,7 @@ const makeHarness = Effect.fn("makePullRequestSyncHarness")(function* (options: } if (command.type === "thread.pull-request.link") { return Ref.update(linkCommands, (recorded) => [...recorded, command]).pipe( + Effect.andThen(options.onLink?.(command) ?? Effect.void), Effect.as({ sequence: 1, storedEvents: [] }), ); } @@ -521,6 +523,54 @@ const mergingSnapshot = () => makeSnapshot([makeThread(MERGING_THREAD, { pullRequests: [makeLink(7, { state: "open" })] })]); describe("PullRequestSyncReactor", () => { + // proof-only: a busy thread's lock held up every other thread's created pull request link + it.effect("links an idle thread's created pull request while another thread's lock is busy", () => + Effect.gen(function* () { + const busyEntered = yield* Deferred.make(); + const linked = yield* Queue.unbounded(); + const fixture = yield* makeHarness({ + snapshot: makeSnapshot([]), + // Stands in for a long send holding the busy thread's lock. + onLink: (command) => + Effect.gen(function* () { + if (command.threadId === "busy") { + yield* Deferred.succeed(busyEntered, undefined); + yield* Effect.sleep("10 minutes"); + } + yield* Queue.offer(linked, [command.threadId, yield* Clock.currentTimeMillis]); + }), + }); + yield* Effect.gen(function* () { + const reactor = yield* PullRequestSyncReactor.PullRequestSyncReactor; + yield* reactor.start(); + yield* Deferred.succeed(fixture.activation, undefined); + yield* Queue.offerAll(fixture.domainEvents, [ + commandRan( + ThreadId.make("busy"), + "gh pr create --fill", + "https://github.com/owner/repository/pull/1\n", + ), + commandRan( + ThreadId.make("idle"), + "gh pr create --fill", + "https://github.com/owner/repository/pull/2\n", + ), + ]); + yield* Deferred.await(busyEntered); + yield* TestClock.adjust("10 minutes"); + const first = yield* Queue.take(linked); + const second = yield* Queue.take(linked); + assert.deepEqual( + new Map([first, second]), + new Map([ + ["idle", 0], + ["busy", 10 * 60 * 1000], + ]), + ); + }).pipe(Effect.provide(fixture.layer)); + }), + ); + it.effect("lets a finished sweep's links go while a cache keeps the fiber that read them", () => Effect.gen(function* () { class SweptLink { diff --git a/apps/server/src/orchestration-v2/WorktreeBranchSync.test.ts b/apps/server/src/orchestration-v2/WorktreeBranchSync.test.ts index 14c5b5d275..60ac71eaf4 100644 --- a/apps/server/src/orchestration-v2/WorktreeBranchSync.test.ts +++ b/apps/server/src/orchestration-v2/WorktreeBranchSync.test.ts @@ -4,7 +4,10 @@ import { type OrchestrationV2ThreadShell, type OrchestrationV2ServerCommand, } from "@t3tools/contracts"; +import * as Clock from "effect/Clock"; import * as Deferred from "effect/Deferred"; +import * as Queue from "effect/Queue"; +import { TestClock } from "effect/testing"; import * as Stream from "effect/Stream"; import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; @@ -108,3 +111,72 @@ it.effect.each([false, true])( ); }, ); + +// proof-only: a busy thread's lock held up every other worktree's branch update +it.effect("updates an idle worktree's branch while another thread's lock is busy", () => { + const thread = (id: string, worktreePath: string) => + ({ id: ThreadId.make(id), branch: "old", worktreePath }) as OrchestrationV2ThreadShell; + const threads = [thread("busy", "/busy"), thread("idle", "/idle")]; + return Effect.gen(function* () { + const busyEntered = yield* Deferred.make(); + const dispatched = yield* Queue.unbounded(); + yield* Effect.gen(function* () { + yield* Layer.build(WorktreeBranchSync.layer); + yield* Deferred.await(busyEntered); + yield* TestClock.adjust("10 minutes"); + const first = yield* Queue.take(dispatched); + const second = yield* Queue.take(dispatched); + assert.deepEqual( + new Map([first, second]), + new Map([ + ["idle", 0], + ["busy", 10 * 60 * 1000], + ]), + ); + }).pipe( + Effect.provide( + Layer.mergeAll( + FileSystem.layerNoop({ realPath: (path) => Effect.succeed(path) }), + Layer.mock(ProjectionStore.ProjectionStoreV2)({ + getThreadWorktreePaths: () => + Effect.succeed( + threads.map((thread) => ({ + threadId: thread.id, + worktreePath: thread.worktreePath!, + })), + ), + }), + Layer.mock(VcsStatusBroadcaster.VcsStatusBroadcaster)({ + streamBranchChanges: Stream.fromIterable([ + { cwd: "/busy", refName: "next" }, + { cwd: "/idle", refName: "next" }, + ]), + }), + Layer.mock(GitWorkflowService.GitWorkflowService)({ + currentBranch: () => Effect.succeed("next"), + }), + Layer.mock(ThreadManagement.ThreadManagementService)({ + getShellSnapshot: ({ threadIds }) => + Effect.succeed({ + schemaVersion: 2, + snapshotSequence: 0, + threads: threads.filter((thread) => threadIds.includes(thread.id)), + archivedThreads: [], + }), + dispatch: (command) => + Effect.gen(function* () { + const threadId = "threadId" in command ? command.threadId : ""; + // Stands in for a long send holding the busy thread's lock. + if (threadId === "busy") { + yield* Deferred.succeed(busyEntered, undefined); + yield* Effect.sleep("10 minutes"); + } + yield* Queue.offer(dispatched, [threadId, yield* Clock.currentTimeMillis]); + return { sequence: 1, storedEvents: [] }; + }), + }), + ), + ), + ); + }); +});