@@ -15,13 +15,16 @@ import { |
15 | 15 | type Project, |
16 | 16 | type RepositoryIdentity, |
17 | 17 | RunId, |
| 18 | + RuntimeRequestId, |
18 | 19 | ThreadId, |
19 | 20 | TurnItemId, |
20 | 21 | } from "@t3tools/contracts"; |
21 | 22 | import { expect, it } from "@effect/vitest"; |
| 23 | +import * as Clock from "effect/Clock"; |
22 | 24 | import * as Context from "effect/Context"; |
23 | 25 | import * as DateTime from "effect/DateTime"; |
24 | 26 | import * as Deferred from "effect/Deferred"; |
| 27 | +import * as Duration from "effect/Duration"; |
25 | 28 | import * as Effect from "effect/Effect"; |
26 | 29 | import * as Layer from "effect/Layer"; |
27 | 30 | import * as Option from "effect/Option"; |
@@ -102,6 +105,7 @@ interface BoxThread { |
102 | 105 | readonly waitedFor: Ref.Ref<ReadonlyArray<RunId | undefined>>; |
103 | 106 | /** The thread's run ends with a task of its own still running, whose result wakes it again. */ |
104 | 107 | readonly nested: Ref.Ref<boolean>; |
| 108 | + readonly nestedInput: Ref.Ref<"question" | "approval" | null>; |
105 | 109 | /** Completed once a wait there blocks on that task. */ |
106 | 110 | readonly nestedWaiting: Deferred.Deferred<void>; |
107 | 111 | /** That task settled and the run it woke answered, with `laterReply`. */ |
@@ -145,6 +149,7 @@ const makeBoxThread = Effect.gen(function* () { |
145 | 149 | laterReply: yield* Ref.make<string | null>(null), |
146 | 150 | waitedFor: yield* Ref.make<ReadonlyArray<RunId | undefined>>([]), |
147 | 151 | nested: yield* Ref.make(false), |
| 152 | + nestedInput: yield* Ref.make<"question" | "approval" | null>(null), |
148 | 153 | nestedWaiting: yield* Deferred.make<void>(), |
149 | 154 | nestedSettled: yield* Deferred.make<void>(), |
150 | 155 | gone: yield* Ref.make(false), |
@@ -280,6 +285,19 @@ const boxToolkitLayer = (thread: BoxThread) => { |
280 | 285 | input.untilDelegatedWorkSettled === true && |
281 | 286 | (yield* Ref.get(thread.nested)) |
282 | 287 | ) { |
| 288 | + const pendingInput = yield* Ref.getAndSet(thread.nestedInput, null); |
| 289 | + if (pendingInput !== null) { |
| 290 | + return { |
| 291 | + threadId: input.threadId, |
| 292 | + run: { id: runId, status: "completed" } as OrchestrationV2Run, |
| 293 | + timedOut: false, |
| 294 | + hasPendingQuestion: pendingInput === "question", |
| 295 | + pendingApproval: |
| 296 | + pendingInput === "approval" |
| 297 | + ? { requestId: RuntimeRequestId.make("request:box"), kind: "file-change", runId } |
| 298 | + : null, |
| 299 | + }; |
| 300 | + } |
283 | 301 | yield* Deferred.succeed(thread.nestedWaiting, undefined); |
284 | 302 | const settled = yield* Deferred.await(thread.nestedSettled).pipe( |
285 | 303 | Effect.timeoutOption(input.timeoutMs), |
@@ -1105,40 +1123,56 @@ it.effect("a task's result is its own run's whole last reply, not a later run's" |
1105 | 1123 | ).pipe(Effect.provide(NodeHttpServer.layerTest)), |
1106 | 1124 | ); |
1107 | 1125 |
|
1108 | | -it.effect("a task whose thread there waits on its own task ends with the answer after it", () => |
1109 | | - Effect.scoped( |
1110 | | - Effect.gen(function* () { |
1111 | | - const thread = yield* makeBoxThread; |
1112 | | - const b = yield* serveBox(thread); |
1113 | | - const offers = yield* SubscriptionRef.make< |
1114 | | - ReadonlyArray<ProviderContinuationRequests.ProviderContinuationRequest> |
1115 | | - >([]); |
1116 | | - const database = yield* SqlitePersistence.layerMemory.pipe(Layer.build); |
1117 | | - const a = yield* makeLaptop(database, offers); |
1118 | | - yield* linkTo(a.links, b, "full-access"); |
1119 | | - yield* seedParent(a); |
1120 | | - const delegated = yield* delegateToBox(a); |
| 1126 | +it.effect.each([null, "question", "approval"] as const)( |
| 1127 | + "a task whose thread there waits on its own task ends with the answer after it (%s)", |
| 1128 | + (pendingInput) => |
| 1129 | + Effect.scoped( |
| 1130 | + Effect.gen(function* () { |
| 1131 | + const thread = yield* makeBoxThread; |
| 1132 | + const b = yield* serveBox(thread); |
| 1133 | + const offers = yield* SubscriptionRef.make< |
| 1134 | + ReadonlyArray<ProviderContinuationRequests.ProviderContinuationRequest> |
| 1135 | + >([]); |
| 1136 | + const database = yield* SqlitePersistence.layerMemory.pipe(Layer.build); |
| 1137 | + const clock = yield* Clock.Clock; |
| 1138 | + const sleeps: Array<number> = []; |
| 1139 | + const a = yield* makeLaptop(database, offers).pipe( |
| 1140 | + Effect.provideService(Clock.Clock, { |
| 1141 | + ...clock, |
| 1142 | + sleep: (duration) => |
| 1143 | + Duration.toMillis(duration) === 5_000 |
| 1144 | + ? Effect.sync(() => { |
| 1145 | + sleeps.push(5_000); |
| 1146 | + }) |
| 1147 | + : clock.sleep(duration), |
| 1148 | + }), |
| 1149 | + ); |
| 1150 | + yield* linkTo(a.links, b, "full-access"); |
| 1151 | + yield* seedParent(a); |
| 1152 | + const delegated = yield* delegateToBox(a); |
1121 | 1153 |
|
1122 | | - // The task's run there ends after delegating a review of its own. |
1123 | | - yield* Ref.set(thread.nested, true); |
1124 | | - yield* Ref.set(thread.reply, "Started a review; I will answer when it reports."); |
1125 | | - yield* Ref.set(thread.status, "completed"); |
1126 | | - yield* Deferred.succeed(thread.finished, undefined); |
1127 | | - const first = yield* Effect.raceFirst( |
1128 | | - Deferred.await(thread.nestedWaiting).pipe(Effect.as("waiting there" as const)), |
1129 | | - taskResult(a, delegated.taskId).pipe(Effect.as("completed here" as const)), |
1130 | | - ); |
1131 | | - expect(first).toBe("waiting there"); |
| 1154 | + // The task's run there ends after delegating a review of its own. |
| 1155 | + yield* Ref.set(thread.nested, true); |
| 1156 | + yield* Ref.set(thread.nestedInput, pendingInput); |
| 1157 | + yield* Ref.set(thread.reply, "Started a review; I will answer when it reports."); |
| 1158 | + yield* Ref.set(thread.status, "completed"); |
| 1159 | + yield* Deferred.succeed(thread.finished, undefined); |
| 1160 | + const first = yield* Effect.raceFirst( |
| 1161 | + Deferred.await(thread.nestedWaiting).pipe(Effect.as("waiting there" as const)), |
| 1162 | + taskResult(a, delegated.taskId).pipe(Effect.as("completed here" as const)), |
| 1163 | + ); |
| 1164 | + expect(first).toBe("waiting there"); |
| 1165 | + expect(sleeps).toEqual(pendingInput === null ? [] : [5_000]); |
1132 | 1166 |
|
1133 | | - // The review reports and wakes the thread there, whose next run answers. |
1134 | | - yield* Ref.set(thread.laterReply, "The review found two issues; both are fixed."); |
1135 | | - yield* Deferred.succeed(thread.nestedSettled, undefined); |
1136 | | - expect(yield* taskResult(a, delegated.taskId)).toMatchObject({ |
1137 | | - status: "completed", |
1138 | | - result: "The review found two issues; both are fixed.", |
1139 | | - }); |
1140 | | - }), |
1141 | | - ).pipe(Effect.provide(NodeHttpServer.layerTest)), |
| 1167 | + // The review reports and wakes the thread there, whose next run answers. |
| 1168 | + yield* Ref.set(thread.laterReply, "The review found two issues; both are fixed."); |
| 1169 | + yield* Deferred.succeed(thread.nestedSettled, undefined); |
| 1170 | + expect(yield* taskResult(a, delegated.taskId)).toMatchObject({ |
| 1171 | + status: "completed", |
| 1172 | + result: "The review found two issues; both are fixed.", |
| 1173 | + }); |
| 1174 | + }), |
| 1175 | + ).pipe(Effect.provide(NodeHttpServer.layerTest)), |
1142 | 1176 | ); |
1143 | 1177 |
|
1144 | 1178 | it.effect("the same key from another provider session is another task", () => |
|