Skip to content

Commit 7c6f7d4

Browse files
fix(server): stop releasing devices once a settled thread re-engages
AI-assisted; verified with the tests and tools listed above.
1 parent 2a6434c commit 7c6f7d4

2 files changed

Lines changed: 59 additions & 13 deletions

File tree

‎apps/server/src/orchestration-v2/ThreadSettlementService.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ interface HarnessOptions {
564564
input: ProjectSetupScriptRunner.ProjectSetupScriptRunnerInput,
565565
) => Effect.Effect<void>;
566566
readonly onCloseIdle?: () => Effect.Effect<void>;
567+
readonly onDeviceClose?: (hostId: string | undefined) => Effect.Effect<void>;
567568
readonly deviceCloseError?: DeviceHostUnavailableError;
568569
readonly deviceSessions?: ReadonlyArray<{ readonly hostId: string; readonly deviceId: string }>;
569570
readonly currentThreads?: ReadonlyArray<OrchestrationV2AppThread>;
@@ -706,6 +707,7 @@ const makeHarness = Effect.fn("makeThreadSettlementHarness")(function* (options:
706707
),
707708
close: (input) =>
708709
Ref.update(deviceCloses, (closes) => [...closes, input]).pipe(
710+
Effect.andThen(options.onDeviceClose?.(input.hostId) ?? Effect.void),
709711
Effect.andThen(
710712
options.deviceCloseError && options.deviceCloseError.hostId === input.hostId
711713
? Effect.fail(options.deviceCloseError)
@@ -1507,6 +1509,48 @@ describe("ThreadSettlementServiceV2 terminals", () => {
15071509
),
15081510
);
15091511

1512+
it.effect("keeps the remaining devices of a thread re-engaged during an earlier release", () =>
1513+
Effect.scoped(
1514+
Effect.gen(function* () {
1515+
const firstStarted = yield* Deferred.make<void>();
1516+
const firstFinished = yield* Deferred.make<void>();
1517+
const settled = appThread("multi-device-thread", "settled");
1518+
const marker = appThread("marker-thread", "settled");
1519+
const currentThreads = [settled, marker];
1520+
const fixture = yield* makeHarness({
1521+
snapshot: makeSnapshot([]),
1522+
currentThreads,
1523+
deviceSessions: [
1524+
{ hostId: "slow-ssh", deviceId: "pixel" },
1525+
{ hostId: "local", deviceId: "emulator-5554" },
1526+
],
1527+
onDeviceClose: (hostId) =>
1528+
hostId === "slow-ssh"
1529+
? Deferred.succeed(firstStarted, undefined).pipe(
1530+
Effect.andThen(Deferred.await(firstFinished)),
1531+
)
1532+
: Effect.void,
1533+
});
1534+
1535+
yield* Effect.gen(function* () {
1536+
const service = yield* ThreadSettlementService.ThreadSettlementServiceV2;
1537+
yield* startHarness(service, fixture.activation, fixture.snapshotReads);
1538+
yield* fixture.publishEvent(settledEvent(settled));
1539+
yield* Deferred.await(firstStarted);
1540+
currentThreads[0] = { ...settled, settledOverride: "active" };
1541+
yield* Deferred.succeed(firstFinished, undefined);
1542+
yield* fixture.publishEvent(settledEvent(marker));
1543+
assert.deepStrictEqual(yield* Queue.take(fixture.closedIdle), { threadId: settled.id });
1544+
assert.deepStrictEqual(yield* Queue.take(fixture.closedIdle), { threadId: marker.id });
1545+
assert.deepStrictEqual(
1546+
(yield* Ref.get(fixture.deviceCloses)).filter((close) => close.threadId === settled.id),
1547+
[{ threadId: settled.id, hostId: "slow-ssh", deviceId: "pixel" }],
1548+
);
1549+
}).pipe(Effect.provide(fixture.layer));
1550+
}),
1551+
),
1552+
);
1553+
15101554
it.effect("releases a settled thread's devices without shutting them down", () =>
15111555
Effect.scoped(
15121556
Effect.gen(function* () {

‎apps/server/src/orchestration-v2/ThreadSettlementService.ts‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -604,22 +604,24 @@ export const make = Effect.gen(function* () {
604604
const settled = yield* projections.getThread(threadId);
605605
if (settled.settledOverride !== "settled") return;
606606
yield* terminals.closeOnSettle({ threadId });
607-
if ((yield* projections.getThread(threadId)).settledOverride !== "settled") return;
608-
// Per device: a thread without devices skips the lock a starting host holds, and one
609-
// unreachable host leaves no other device claimed.
607+
// Per device: a thread without devices skips the lock a starting host holds, one unreachable
608+
// host leaves no other device claimed, and a thread re-engaged meanwhile keeps the rest.
610609
yield* Effect.forEach(
611610
yield* devices.sessionsForThread(threadId),
612611
({ hostId, deviceId }) =>
613-
devices.close({ threadId, hostId, deviceId }).pipe(
614-
Effect.catch((error) =>
615-
Effect.logWarning("releasing a settled thread's device failed", {
616-
threadId,
617-
hostId,
618-
deviceId,
619-
error,
620-
}),
621-
),
622-
),
612+
Effect.gen(function* () {
613+
if ((yield* projections.getThread(threadId)).settledOverride !== "settled") return;
614+
yield* devices.close({ threadId, hostId, deviceId }).pipe(
615+
Effect.catch((error) =>
616+
Effect.logWarning("releasing a settled thread's device failed", {
617+
threadId,
618+
hostId,
619+
deviceId,
620+
error,
621+
}),
622+
),
623+
);
624+
}),
623625
{ discard: true },
624626
);
625627
const worktreePath = settled.worktreePath;

0 commit comments

Comments
 (0)