|
| 1 | +import { assert, it } from "@effect/vitest"; |
| 2 | +import { |
| 3 | + CommandId, |
| 4 | + MessageId, |
| 5 | + ProjectId, |
| 6 | + ProviderDriverKind, |
| 7 | + ProviderInstanceId, |
| 8 | + ThreadId, |
| 9 | +} from "@t3tools/contracts"; |
| 10 | +import * as Effect from "effect/Effect"; |
| 11 | + |
| 12 | +import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts"; |
| 13 | +import * as Orchestrator from "./Orchestrator.ts"; |
| 14 | +import type { ProviderAdapterV2Shape } from "./ProviderAdapter.ts"; |
| 15 | +import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts"; |
| 16 | +import { pullRequestWatchWakePrefix } from "./pullRequestWatch.ts"; |
| 17 | +import { makeOrchestratorV2ReplayLayerWithRegistry } from "./testkit/ProviderReplayHarness.ts"; |
| 18 | + |
| 19 | +// Fork: a pull request watch wake queued behind a busy turn is cancelled when its watch ends, |
| 20 | +// so the agent is not woken about a pull request it stopped watching or that merged meanwhile. |
| 21 | + |
| 22 | +const driver = ProviderDriverKind.make("codex"); |
| 23 | +const instanceId = ProviderInstanceId.make("codex"); |
| 24 | +const modelSelection = { instanceId, model: "test-model" }; |
| 25 | +const adapter: ProviderAdapterV2Shape = { |
| 26 | + instanceId, |
| 27 | + driver, |
| 28 | + getCapabilities: () => Effect.succeed(CodexProviderCapabilitiesV2), |
| 29 | + planSelectionTransition: () => Effect.succeed({ type: "apply_on_next_turn" }), |
| 30 | + openSession: () => Effect.die("watch wake tests start no session"), |
| 31 | +}; |
| 32 | +const layer = makeOrchestratorV2ReplayLayerWithRegistry( |
| 33 | + { name: "pull-request-watch-wakes-fork" }, |
| 34 | + ProviderAdapterRegistry.makeLayer([adapter]), |
| 35 | + { runEffectWorker: false }, |
| 36 | +); |
| 37 | + |
| 38 | +const threadId = ThreadId.make("pr-watch-wakes"); |
| 39 | +const repository = { host: "github.com", repository: "rsi-software/t3code-hyprws" }; |
| 40 | + |
| 41 | +it.layer(layer)("pull request watch wakes (fork)", (it) => { |
| 42 | + it.effect("cancels the queued wakes of a watch that ends, and keeps its last wake", () => |
| 43 | + Effect.gen(function* () { |
| 44 | + const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 45 | + yield* orchestrator.dispatch({ |
| 46 | + type: "thread.create", |
| 47 | + commandId: CommandId.make("pr-watch-wakes-create"), |
| 48 | + threadId, |
| 49 | + projectId: ProjectId.make("pr-watch-wakes-project"), |
| 50 | + title: "Watch wakes", |
| 51 | + modelSelection, |
| 52 | + runtimeMode: "full-access", |
| 53 | + interactionMode: "default", |
| 54 | + branch: null, |
| 55 | + worktreePath: null, |
| 56 | + createdBy: "user", |
| 57 | + creationSource: "web", |
| 58 | + }); |
| 59 | + for (const number of [7, 8, 9]) { |
| 60 | + yield* orchestrator.dispatch({ |
| 61 | + type: "thread.pull-request.watch", |
| 62 | + commandId: CommandId.make(`pr-watch-wakes-start-${number}`), |
| 63 | + threadId, |
| 64 | + ...repository, |
| 65 | + number, |
| 66 | + watching: true, |
| 67 | + link: { |
| 68 | + url: `https://github.com/rsi-software/t3code-hyprws/pull/${number}`, |
| 69 | + source: "agent", |
| 70 | + }, |
| 71 | + }); |
| 72 | + } |
| 73 | + // The agent is busy, so every wake below queues behind this turn. |
| 74 | + yield* orchestrator.dispatch({ |
| 75 | + type: "message.dispatch", |
| 76 | + commandId: CommandId.make("pr-watch-wakes-busy"), |
| 77 | + threadId, |
| 78 | + messageId: MessageId.make("pr-watch-wakes-busy"), |
| 79 | + text: "Busy", |
| 80 | + attachments: [], |
| 81 | + modelSelection, |
| 82 | + dispatchMode: { type: "start_immediately" }, |
| 83 | + createdBy: "user", |
| 84 | + creationSource: "web", |
| 85 | + }); |
| 86 | + |
| 87 | + const linkOf = (number: number) => |
| 88 | + Effect.map(orchestrator.getThreadShell(threadId), (thread) => |
| 89 | + thread?.pullRequests?.find((link) => link.number === number), |
| 90 | + ); |
| 91 | + const sync = (number: number, ending: boolean, wake: string | null) => |
| 92 | + Effect.gen(function* () { |
| 93 | + const link = yield* linkOf(number); |
| 94 | + if (link?.watch === undefined) return yield* Effect.die(`#${number} is not watched`); |
| 95 | + yield* orchestrator.dispatch({ |
| 96 | + type: "thread.pull-request-watch.sync", |
| 97 | + commandId: CommandId.make(`pr-watch-wakes-sync-${number}-${wake ?? "end"}`), |
| 98 | + threadId, |
| 99 | + ...repository, |
| 100 | + number, |
| 101 | + startedAt: link.watch.startedAt, |
| 102 | + watch: ending ? null : { ...link.watch, wakes: link.watch.wakes + 1 }, |
| 103 | + ...(wake === null |
| 104 | + ? {} |
| 105 | + : { |
| 106 | + wake: { |
| 107 | + messageId: MessageId.make(`${pullRequestWatchWakePrefix(link)}${wake}`), |
| 108 | + text: `Update on #${number}`, |
| 109 | + notification: { |
| 110 | + source: { kind: "monitor" }, |
| 111 | + outcome: "updated", |
| 112 | + summary: `#${number}: checks passed`, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }), |
| 116 | + }); |
| 117 | + }); |
| 118 | + const queued = Effect.map(orchestrator.getThreadProjection(threadId), (projection) => |
| 119 | + projection.runs |
| 120 | + .filter((run) => run.status === "queued") |
| 121 | + .map((run) => run.userMessageId.split(":").at(-1)) |
| 122 | + .toSorted(), |
| 123 | + ); |
| 124 | + |
| 125 | + yield* sync(7, false, "checks-7"); |
| 126 | + yield* sync(8, false, "checks-8"); |
| 127 | + yield* sync(9, false, "checks-9"); |
| 128 | + assert.deepEqual(yield* queued, ["checks-7", "checks-8", "checks-9"]); |
| 129 | + |
| 130 | + // #7 merged: the watch ends with no wake of its own. |
| 131 | + yield* sync(7, true, null); |
| 132 | + assert.deepEqual(yield* queued, ["checks-8", "checks-9"]); |
| 133 | + |
| 134 | + // The agent stops watching #8. |
| 135 | + yield* orchestrator.dispatch({ |
| 136 | + type: "thread.pull-request.watch", |
| 137 | + commandId: CommandId.make("pr-watch-wakes-stop-8"), |
| 138 | + threadId, |
| 139 | + ...repository, |
| 140 | + number: 8, |
| 141 | + watching: false, |
| 142 | + }); |
| 143 | + assert.deepEqual(yield* queued, ["checks-9"]); |
| 144 | + |
| 145 | + // #9 gives up with a last wake: that one stays, the stale one before it goes. |
| 146 | + yield* sync(9, true, "stopped-9"); |
| 147 | + assert.deepEqual(yield* queued, ["stopped-9"]); |
| 148 | + }), |
| 149 | + ); |
| 150 | +}); |
0 commit comments