Skip to content

Commit fb1a995

Browse files
committed
fix(server): keep remote task waits open for pending input
1 parent 8a1d20f commit fb1a995

3 files changed

Lines changed: 72 additions & 33 deletions

File tree

‎apps/server/src/peer/RemoteDelegation.test.ts‎

Lines changed: 66 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ import {
1515
type Project,
1616
type RepositoryIdentity,
1717
RunId,
18+
RuntimeRequestId,
1819
ThreadId,
1920
TurnItemId,
2021
} from "@t3tools/contracts";
2122
import { expect, it } from "@effect/vitest";
23+
import * as Clock from "effect/Clock";
2224
import * as Context from "effect/Context";
2325
import * as DateTime from "effect/DateTime";
2426
import * as Deferred from "effect/Deferred";
27+
import * as Duration from "effect/Duration";
2528
import * as Effect from "effect/Effect";
2629
import * as Layer from "effect/Layer";
2730
import * as Option from "effect/Option";
@@ -102,6 +105,7 @@ interface BoxThread {
102105
readonly waitedFor: Ref.Ref<ReadonlyArray<RunId | undefined>>;
103106
/** The thread's run ends with a task of its own still running, whose result wakes it again. */
104107
readonly nested: Ref.Ref<boolean>;
108+
readonly nestedInput: Ref.Ref<"question" | "approval" | null>;
105109
/** Completed once a wait there blocks on that task. */
106110
readonly nestedWaiting: Deferred.Deferred<void>;
107111
/** That task settled and the run it woke answered, with `laterReply`. */
@@ -145,6 +149,7 @@ const makeBoxThread = Effect.gen(function* () {
145149
laterReply: yield* Ref.make<string | null>(null),
146150
waitedFor: yield* Ref.make<ReadonlyArray<RunId | undefined>>([]),
147151
nested: yield* Ref.make(false),
152+
nestedInput: yield* Ref.make<"question" | "approval" | null>(null),
148153
nestedWaiting: yield* Deferred.make<void>(),
149154
nestedSettled: yield* Deferred.make<void>(),
150155
gone: yield* Ref.make(false),
@@ -280,6 +285,19 @@ const boxToolkitLayer = (thread: BoxThread) => {
280285
input.untilDelegatedWorkSettled === true &&
281286
(yield* Ref.get(thread.nested))
282287
) {
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+
}
283301
yield* Deferred.succeed(thread.nestedWaiting, undefined);
284302
const settled = yield* Deferred.await(thread.nestedSettled).pipe(
285303
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"
11051123
).pipe(Effect.provide(NodeHttpServer.layerTest)),
11061124
);
11071125

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);
11211153

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]);
11321166

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)),
11421176
);
11431177

11441178
it.effect("the same key from another provider session is another task", () =>

‎apps/server/src/peer/RemoteDelegation.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,11 @@ const make = Effect.gen(function* () {
610610
// The next look reads the result and stops it there.
611611
if ("endedHere" in waited) return false;
612612
if (waited.timedOut) return false;
613+
if (waited.hasPendingQuestion || waited.pendingApproval !== null) {
614+
// Input-aware waits return immediately; keep following without a hot loop.
615+
yield* Effect.sleep(Duration.seconds(5)).pipe(Effect.raceFirst(endedHere));
616+
return false;
617+
}
613618
// A task recorded without its run waits on the thread's latest; a
614619
// thread there with none has nothing left to wait for.
615620
if (waited.runId === null) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"claudeSystemAppendBytes": 5135,
33
"codexAdditionalContextBytes": 6865,
4-
"mcpToolListJsonBytes": 46007
4+
"mcpToolListJsonBytes": 45990
55
}

0 commit comments

Comments
 (0)