|
| 1 | +/** |
| 2 | + * T3O-76 — human-in-the-loop is the human's toggle and nothing else's. |
| 3 | + * |
| 4 | + * Two bugs, one rule. A human-in-the-loop step that reached |
| 5 | + * `board_complete_step` sat in its stage forever, because `advanceStage` |
| 6 | + * skipped every human-in-the-loop run; it now honours the stage's |
| 7 | + * `autoAdvance` exactly as an unattended run does. And a human messaging a |
| 8 | + * running board agent flipped the card to human-in-the-loop and paused it: |
| 9 | + * the queued message they steered in read as a Stop at the gateway, and a Stop |
| 10 | + * flipped the card. Both halves are covered here through the live reactor. |
| 11 | + */ |
| 12 | +import { |
| 13 | + BOARD_SEED_STAGE_IDS, |
| 14 | + BoardCardId, |
| 15 | + boardCardStepState, |
| 16 | + CommandId, |
| 17 | + EventId, |
| 18 | + MessageId, |
| 19 | + RunId, |
| 20 | + type BoardState, |
| 21 | + type OrchestrationV2StoredEvent, |
| 22 | + type ThreadId, |
| 23 | +} from "@t3tools/contracts"; |
| 24 | +import { assert, it } from "@effect/vitest"; |
| 25 | +import * as DateTime from "effect/DateTime"; |
| 26 | +import * as Effect from "effect/Effect"; |
| 27 | + |
| 28 | +import { makeSignalReader } from "./ThreadGatewayV2.ts"; |
| 29 | +import { |
| 30 | + buildingCard, |
| 31 | + cardMoved, |
| 32 | + cardStage, |
| 33 | + codexStep, |
| 34 | + makeBoardCard, |
| 35 | + movedToBuilding, |
| 36 | + settingsWith, |
| 37 | + stepCompleted, |
| 38 | + stepStatus, |
| 39 | + withGovernor, |
| 40 | +} from "./supervisorHarness.testkit.ts"; |
| 41 | + |
| 42 | +const cardHumanInLoop = (board: BoardState, id: BoardCardId) => |
| 43 | + board.cards.find((candidate) => candidate.id === id)?.humanInLoop; |
| 44 | + |
| 45 | +// ── Bug 1: a finished human-in-the-loop step advances ───────────────────── |
| 46 | + |
| 47 | +it.effect("a human-in-the-loop build that completes moves on to Code review", () => |
| 48 | + withGovernor( |
| 49 | + { |
| 50 | + board: { cards: [buildingCard("hitl", "m", true)], nextCardNumberByProject: {} }, |
| 51 | + settings: settingsWith({ building: [codexStep], globalMaxConcurrent: 3 }), |
| 52 | + }, |
| 53 | + ({ pumpDomain, board }) => |
| 54 | + Effect.gen(function* () { |
| 55 | + const id = BoardCardId.make("hitl"); |
| 56 | + yield* pumpDomain(movedToBuilding(buildingCard("hitl", "m", true), 1)); |
| 57 | + assert.isTrue(boardCardStepState(yield* board, id)?.humanInLoop); |
| 58 | + |
| 59 | + yield* pumpDomain(stepCompleted(id, "succeeded", 2)); |
| 60 | + |
| 61 | + assert.strictEqual(cardStage(yield* board, id), BOARD_SEED_STAGE_IDS.review); |
| 62 | + }), |
| 63 | + ), |
| 64 | +); |
| 65 | + |
| 66 | +it.effect("a human-in-the-loop build stays put when Building's auto-advance is off", () => |
| 67 | + withGovernor( |
| 68 | + { |
| 69 | + board: { cards: [buildingCard("held", "m", true)], nextCardNumberByProject: {} }, |
| 70 | + settings: settingsWith({ |
| 71 | + building: [codexStep], |
| 72 | + globalMaxConcurrent: 3, |
| 73 | + buildAutoAdvance: false, |
| 74 | + }), |
| 75 | + }, |
| 76 | + ({ pumpDomain, board }) => |
| 77 | + Effect.gen(function* () { |
| 78 | + const id = BoardCardId.make("held"); |
| 79 | + yield* pumpDomain(movedToBuilding(buildingCard("held", "m", true), 1)); |
| 80 | + yield* pumpDomain(stepCompleted(id, "succeeded", 2)); |
| 81 | + |
| 82 | + const after = yield* board; |
| 83 | + assert.strictEqual(stepStatus(after, id), "succeeded"); |
| 84 | + assert.strictEqual(cardStage(after, id), BOARD_SEED_STAGE_IDS.building); |
| 85 | + }), |
| 86 | + ), |
| 87 | +); |
| 88 | + |
| 89 | +// A drag-back re-entry conversation (D7) is always human-in-the-loop with no |
| 90 | +// prompt. It finishes the stage exactly as any other step does: a human who |
| 91 | +// talked the work through to `board_complete_step` wants the card moved on, and |
| 92 | +// a stage that should hold for them ships with `autoAdvance` off. |
| 93 | +it.effect("a drag-back re-entry conversation that completes moves on like any other step", () => { |
| 94 | + const card = buildingCard("again", "m"); |
| 95 | + const priorBuild = { |
| 96 | + cardId: card.id, |
| 97 | + stepId: String(BOARD_SEED_STAGE_IDS.building), |
| 98 | + outcome: "succeeded" as const, |
| 99 | + summary: "built it", |
| 100 | + payload: null, |
| 101 | + threadId: null, |
| 102 | + completedAt: "2026-01-01T00:00:00.000Z", |
| 103 | + }; |
| 104 | + return withGovernor( |
| 105 | + { |
| 106 | + board: { cards: [card], stepCompletions: [priorBuild], nextCardNumberByProject: {} }, |
| 107 | + settings: settingsWith({ building: [codexStep], globalMaxConcurrent: 3 }), |
| 108 | + }, |
| 109 | + ({ pumpDomain, board }) => |
| 110 | + Effect.gen(function* () { |
| 111 | + yield* pumpDomain(cardMoved(card, "review", "building", 1)); |
| 112 | + const reentry = boardCardStepState(yield* board, card.id); |
| 113 | + assert.isTrue(reentry?.humanInLoop); |
| 114 | + assert.strictEqual(reentry?.prompt, ""); |
| 115 | + assert.strictEqual(cardStage(yield* board, card.id), BOARD_SEED_STAGE_IDS.building); |
| 116 | + |
| 117 | + yield* pumpDomain(stepCompleted(card.id, "succeeded", 2)); |
| 118 | + assert.strictEqual(cardStage(yield* board, card.id), BOARD_SEED_STAGE_IDS.review); |
| 119 | + }), |
| 120 | + ); |
| 121 | +}); |
| 122 | + |
| 123 | +const planningCard = () => makeBoardCard({ id: "plan", stage: "planning", orderKey: "m" }); |
| 124 | + |
| 125 | +const completePlanning = (planningAutoAdvance: boolean, expectedStage: string) => |
| 126 | + withGovernor( |
| 127 | + { |
| 128 | + board: { cards: [planningCard()], nextCardNumberByProject: {} }, |
| 129 | + settings: settingsWith({ |
| 130 | + building: [codexStep], |
| 131 | + planning: codexStep, |
| 132 | + planningHumanInLoop: true, |
| 133 | + planningAutoAdvance, |
| 134 | + globalMaxConcurrent: 3, |
| 135 | + }), |
| 136 | + }, |
| 137 | + ({ pumpDomain, board }) => |
| 138 | + Effect.gen(function* () { |
| 139 | + const id = BoardCardId.make("plan"); |
| 140 | + yield* pumpDomain(cardMoved(planningCard(), "sprint", "planning", 1)); |
| 141 | + assert.isTrue(boardCardStepState(yield* board, id)?.humanInLoop); |
| 142 | + |
| 143 | + yield* pumpDomain(stepCompleted(id, "succeeded", 2, String(BOARD_SEED_STAGE_IDS.planning))); |
| 144 | + assert.strictEqual(cardStage(yield* board, id), expectedStage); |
| 145 | + }), |
| 146 | + ); |
| 147 | + |
| 148 | +it.effect("a human-in-the-loop planning step advances when Planning opts into it", () => |
| 149 | + completePlanning(true, BOARD_SEED_STAGE_IDS.ready), |
| 150 | +); |
| 151 | + |
| 152 | +it.effect("a human-in-the-loop planning step waits for a human by default", () => |
| 153 | + completePlanning(false, BOARD_SEED_STAGE_IDS.planning), |
| 154 | +); |
| 155 | + |
| 156 | +// ── Bug 2: messaging a running agent changes nothing on the card ────────── |
| 157 | + |
| 158 | +const CLIENT_COMMAND_ID = "8c7f0f0e-6d2b-4a5e-9a1d-0f2b3c4d5e6f"; |
| 159 | +const t0 = DateTime.makeUnsafe("2026-10-20T00:00:00.000Z"); |
| 160 | +let sequence = 0; |
| 161 | +const stored = ( |
| 162 | + threadId: ThreadId, |
| 163 | + type: string, |
| 164 | + payload: object, |
| 165 | + commandId: string | null, |
| 166 | +): OrchestrationV2StoredEvent => |
| 167 | + ({ |
| 168 | + sequence: (sequence += 1), |
| 169 | + commandId: commandId === null ? null : CommandId.make(commandId), |
| 170 | + event: { type, id: EventId.make(`evt-${sequence}`), threadId, occurredAt: t0, payload }, |
| 171 | + }) as unknown as OrchestrationV2StoredEvent; |
| 172 | + |
| 173 | +/** What v2 records when a human types into a busy thread and then promotes the |
| 174 | + queued message to a steer (or deletes it): the queued run and its message, |
| 175 | + then that run cancelled straight out of the queue under the human's command. */ |
| 176 | +const queuedThenSteered = (threadId: ThreadId) => { |
| 177 | + const messageId = MessageId.make("message-queued"); |
| 178 | + const run = (status: string, queuePosition: number | null) => ({ |
| 179 | + id: RunId.make("run-queued"), |
| 180 | + threadId, |
| 181 | + status, |
| 182 | + queuePosition, |
| 183 | + userMessageId: messageId, |
| 184 | + requestedAt: t0, |
| 185 | + startedAt: null, |
| 186 | + }); |
| 187 | + return [ |
| 188 | + stored(threadId, "run.created", run("queued", 1), CLIENT_COMMAND_ID), |
| 189 | + stored( |
| 190 | + threadId, |
| 191 | + "message.updated", |
| 192 | + { id: messageId, role: "user", createdBy: "user", creationSource: "web" }, |
| 193 | + CLIENT_COMMAND_ID, |
| 194 | + ), |
| 195 | + stored(threadId, "run.updated", run("cancelled", null), "a1b2c3d4-0000-4000-8000-000000000000"), |
| 196 | + ]; |
| 197 | +}; |
| 198 | + |
| 199 | +it.effect("a message steered into a running unattended build leaves the card as it was", () => |
| 200 | + withGovernor( |
| 201 | + { |
| 202 | + board: { cards: [buildingCard("steered", "m")], nextCardNumberByProject: {} }, |
| 203 | + settings: settingsWith({ building: [codexStep], globalMaxConcurrent: 3 }), |
| 204 | + }, |
| 205 | + ({ pumpDomain, board }) => |
| 206 | + Effect.gen(function* () { |
| 207 | + const id = BoardCardId.make("steered"); |
| 208 | + yield* pumpDomain(movedToBuilding(buildingCard("steered", "m"), 1)); |
| 209 | + const running = boardCardStepState(yield* board, id); |
| 210 | + assert.strictEqual(running?.status, "running"); |
| 211 | + assert.isFalse(running?.humanInLoop); |
| 212 | + |
| 213 | + // Through the real signal reader, so this covers the gateway's reading |
| 214 | + // of the cancelled queued run as well as the reactor's handling of it. |
| 215 | + const read = makeSignalReader(); |
| 216 | + for (const event of queuedThenSteered(running!.threadId!)) { |
| 217 | + for (const signal of read(event)) yield* pumpDomain(signal); |
| 218 | + } |
| 219 | + |
| 220 | + const after = yield* board; |
| 221 | + assert.strictEqual(stepStatus(after, id), "running"); |
| 222 | + assert.isNull(cardHumanInLoop(after, id)); |
| 223 | + assert.isFalse(boardCardStepState(after, id)?.humanInLoop); |
| 224 | + }), |
| 225 | + ), |
| 226 | +); |
0 commit comments