Skip to content

Commit 0ae7779

Browse files
committed
fix(server): retain remote task IDs across deferred launches
1 parent 39278ad commit 0ae7779

3 files changed

Lines changed: 45 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ it.effect("renews a missing peer session once and reuses its replacement", () =>
135135
Effect.succeed({
136136
link: {
137137
environmentId: box.environmentId,
138+
launchIdentity: "fixture-grant",
138139
label: box.label,
139140
urls: ["https://peer.example"],
140141
access: "auto",

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,41 @@ const taskResult = (laptopEnv: Effect.Success<ReturnType<typeof makeLaptop>>, ta
810810
),
811811
);
812812

813+
it.effect("returns the recorded task when an accepted launch loses its response", () =>
814+
Effect.scoped(
815+
Effect.gen(function* () {
816+
const thread = yield* makeBoxThread;
817+
const b = yield* serveBox(thread);
818+
const offers = yield* SubscriptionRef.make<
819+
ReadonlyArray<ProviderContinuationRequests.ProviderContinuationRequest>
820+
>([]);
821+
const database = yield* SqlitePersistence.layerMemory.pipe(Layer.build);
822+
const a = yield* makeLaptop(database, offers);
823+
yield* linkTo(a.links, b, "full-access");
824+
yield* seedParent(a);
825+
yield* Ref.set(
826+
thread.afterLaunch,
827+
Ref.set(thread.afterLaunch, Effect.void).pipe(
828+
Effect.andThen(Effect.die("The accepted launch response was lost.")),
829+
),
830+
);
831+
const delegated = yield* delegateToBox(a);
832+
expect(delegated.taskId).toBeDefined();
833+
const recorded = yield* a.orchestrator.getThreadProjection(parentThreadId);
834+
expect(recorded.subagents).toHaveLength(1);
835+
expect(recorded.subagents[0]?.id).toBe(delegated.taskId);
836+
yield* Ref.set(thread.reply, "Recovered the accepted child.");
837+
yield* Ref.set(thread.status, "completed");
838+
yield* Deferred.succeed(thread.finished, undefined);
839+
expect(yield* taskResult(a, delegated.taskId)).toMatchObject({
840+
status: "completed",
841+
result: "Recovered the accepted child.",
842+
});
843+
expect(new Set(yield* Ref.get(thread.launchIds)).size).toBe(1);
844+
}),
845+
).pipe(Effect.provide(NodeHttpServer.layerTest)),
846+
);
847+
813848
it.effect("a task delegated to the box wakes the laptop's parent when it ends there", () =>
814849
Effect.scoped(
815850
Effect.gen(function* () {

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,14 @@ const make = Effect.gen(function* () {
360360
});
361361
}
362362
yield* launchPending(scope.thread.threadId, taskId).pipe(
363+
Effect.catchIf(
364+
(error) => !isFinal(error) && !isModeRefusal(error) && error.code !== "invalid_request",
365+
(error) =>
366+
Effect.logWarning("remote-delegation.launch-deferred", {
367+
taskId,
368+
reason: error.message,
369+
}),
370+
),
363371
Effect.ensuring(followOne(scope.thread.threadId, taskId).pipe(Effect.forkIn(followers))),
364372
);
365373
return { taskId };
@@ -856,7 +864,7 @@ const make = Effect.gen(function* () {
856864
),
857865
);
858866
}
859-
yield* follow(task.threadId, task.id).pipe(Effect.forkIn(followers));
867+
yield* followOne(task.threadId, task.id).pipe(Effect.forkIn(followers));
860868
return { taskId: task.id, status: "cancel_requested" } as const;
861869
}
862870
const attached = { ...remote, threadId: remote.threadId };

0 commit comments

Comments
 (0)