Skip to content

Commit 93ccc7e

Browse files
fix(server): guard handoff continuations and keep siblings streaming during detach
A worktree handoff's continuation now passes the same disabled-provider check as a queued message, and a planned detach interrupts native turns before taking the event permit so sibling threads keep streaming. AI-assisted; verified with the tests and tools listed above. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 4ff90a9 commit 93ccc7e

4 files changed

Lines changed: 117 additions & 6 deletions

File tree

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

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1910,6 +1910,94 @@ it.effect.each(["pending", "running", "completed"] as const)(
19101910
}),
19111911
);
19121912

1913+
it.effect(
1914+
"a planned detach keeps sibling streams flowing while its native interrupt is pending",
1915+
() =>
1916+
Effect.gen(function* () {
1917+
const state = yield* Ref.make(emptyState);
1918+
let onInterrupt: Effect.Effect<void> = Effect.void;
1919+
yield* Effect.gen(function* () {
1920+
const manager = yield* ProviderSessionManager.ProviderSessionManagerV2;
1921+
const idAllocator = yield* IdAllocator.IdAllocatorV2;
1922+
const sink = yield* EventSink.EventSinkV2;
1923+
const threadId = ThreadId.make("thread:detach-sibling-moving");
1924+
const providerSessionId = yield* idAllocator.allocate.providerSession({
1925+
providerInstanceId: modelSelection.instanceId,
1926+
threadId,
1927+
});
1928+
const now = yield* DateTime.now;
1929+
const providerThread = makeProviderThread({
1930+
idAllocator,
1931+
threadId,
1932+
providerSessionId,
1933+
now,
1934+
});
1935+
yield* sink.write({
1936+
events: [
1937+
yield* makeThreadCreatedEvent({ idAllocator, threadId, now }),
1938+
{
1939+
id: yield* idAllocator.allocate.event({ threadId }),
1940+
type: "provider-thread.updated",
1941+
threadId,
1942+
driver: CODEX_DRIVER,
1943+
occurredAt: now,
1944+
payload: providerThread,
1945+
},
1946+
],
1947+
});
1948+
const runtime = yield* manager.open({
1949+
threadId,
1950+
providerSessionId,
1951+
modelSelection,
1952+
runtimePolicy,
1953+
});
1954+
yield* runtime.subscribeEvents!(threadId);
1955+
const sibling = yield* runtime.subscribeEvents!(
1956+
ThreadId.make("thread:detach-sibling-other"),
1957+
);
1958+
const siblingEvents = yield* Queue.unbounded<ProviderAdapter.ProviderAdapterV2Event>();
1959+
yield* sibling.events.pipe(
1960+
Stream.runForEach((event) => Queue.offer(siblingEvents, event)),
1961+
Effect.forkScoped,
1962+
);
1963+
const turn = (nativeTurnId: string, status: "running" | "completed") =>
1964+
({
1965+
type: "provider_turn.updated",
1966+
driver: CODEX_DRIVER,
1967+
providerTurn: {
1968+
id: idAllocator.derive.providerTurn({ driver: CODEX_DRIVER, nativeTurnId }),
1969+
providerThreadId: providerThread.id,
1970+
nodeId: idAllocator.derive.rootNode({ runId: RunId.make(`run:${nativeTurnId}`) }),
1971+
runAttemptId: null,
1972+
nativeTurnRef: null,
1973+
ordinal: 1,
1974+
status,
1975+
startedAt: now,
1976+
completedAt: null,
1977+
},
1978+
}) satisfies ProviderAdapter.ProviderAdapterV2Event;
1979+
const adapterQueue = (yield* Ref.get(state)).eventQueues.get(String(providerSessionId))!;
1980+
yield* Queue.offer(adapterQueue, turn("moving", "running"));
1981+
yield* Queue.take(siblingEvents);
1982+
const fence = turn("sibling-fence", "completed");
1983+
onInterrupt = Queue.offer(adapterQueue, fence).pipe(
1984+
Effect.andThen(Queue.take(siblingEvents)),
1985+
Effect.map((event) => assert.deepEqual(event, fence)),
1986+
);
1987+
yield* manager.detach({ providerSessionId, threadId, reason: "workspace_changed" });
1988+
assert.equal((yield* Ref.get(state)).interruptCount, 1);
1989+
}).pipe(
1990+
Effect.provide(
1991+
layerTest({
1992+
state,
1993+
idleTimeoutMs: 60_000,
1994+
interruptTurn: Effect.suspend(() => onInterrupt),
1995+
}),
1996+
),
1997+
);
1998+
}),
1999+
);
2000+
19132001
it.effect(
19142002
"ProviderSessionManagerV2 projects idle extension statuses and rejects a retired runtime's updates",
19152003
() =>

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -636,9 +636,7 @@ export const layerWithOptions = (
636636
entry: LiveSessionEntry,
637637
error: ProviderAdapter.ProviderAdapterV2Error,
638638
threadId?: ThreadId,
639-
beforeFailure: Effect.Effect<void> = Effect.void,
640639
) {
641-
yield* beforeFailure;
642640
const subscribers = yield* Ref.modify(entry.eventSubscribers, (current) => {
643641
const failed: Array<ProviderSessionEventSubscriber> = [];
644642
const remaining = new Map(current);
@@ -3140,6 +3138,8 @@ export const layerWithOptions = (
31403138
if (Option.isNone(detached)) {
31413139
return;
31423140
}
3141+
// Interrupt outside the event permit so sibling threads keep streaming meanwhile.
3142+
yield* interruptActiveTurns;
31433143
if (input.reason !== undefined) {
31443144
yield* failSubscribers(
31453145
detached.value,
@@ -3150,10 +3150,7 @@ export const layerWithOptions = (
31503150
reason: input.reason,
31513151
}),
31523152
input.threadId,
3153-
interruptActiveTurns,
31543153
);
3155-
} else {
3156-
yield* interruptActiveTurns;
31573154
}
31583155
if (
31593156
detached.value.attachedThreadIds.size === 0 &&

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ const layerDispatchChokepointTest =
111111
);
112112

113113
it.effect(
114-
"the dispatch chokepoint refuses a continuation message.dispatch on a project-disabled instance before reaching the orchestrator",
114+
"the dispatch chokepoint refuses continuation prompts on a project-disabled instance before reaching the orchestrator",
115115
() => {
116116
const projectId = ProjectId.make("project:thread-management:chokepoint");
117117
const threadId = ThreadId.make("thread:thread-management:chokepoint");
@@ -147,6 +147,20 @@ it.effect(
147147
ThreadManagementService.ThreadManagementProviderInstanceDisabledError,
148148
);
149149
expect(error).toMatchObject({ projectId, instanceId });
150+
const handoffError = yield* Effect.flip(
151+
threads.dispatch({
152+
type: "thread.metadata.update",
153+
commandId: CommandId.make("command:thread-management:chokepoint-handoff"),
154+
threadId,
155+
worktreePath: "/worktrees/handoff",
156+
expectedWorktreePath: null,
157+
worktreeContinuation: {
158+
messageId: MessageId.make("message:thread-management:chokepoint-handoff"),
159+
text: "Keep going in the worktree.",
160+
},
161+
}),
162+
);
163+
expect(handoffError).toMatchObject({ projectId, instanceId });
150164
expect(dispatched).toEqual([]);
151165
}).pipe(Effect.provide(layerTest));
152166
},

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,18 @@ const make = Effect.gen(function* () {
740740
}),
741741
),
742742
);
743+
case "thread.metadata.update":
744+
return command.worktreeContinuation === undefined
745+
? Effect.void
746+
: orchestrator.getThreadRecords(command.threadId, []).pipe(
747+
Effect.flatMap((projection) =>
748+
assertProviderInstanceEnabled({
749+
projectId: projection.thread.projectId,
750+
commandId: command.commandId,
751+
instanceId: projection.thread.modelSelection.instanceId,
752+
}),
753+
),
754+
);
743755
case "delegated_task.request":
744756
return orchestrator.getThreadRecords(command.parentThreadId, []).pipe(
745757
Effect.flatMap((parent) =>

0 commit comments

Comments
 (0)