Skip to content

Commit 145cf15

Browse files
fix(server): OpenCode 2 keeps checking a failed prompt that starts after a reconnect
A reconnect counted every marked run as started, even when the server still ran the session. A failed prompt whose execution had not started yet then had its own start taken as a later execution's, which cleared the mark, so the next turn skipped the `session.active` check. A reconnect now counts the run as started only once the session is idle. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent bfa132a commit 145cf15

2 files changed

Lines changed: 55 additions & 3 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,6 +1486,54 @@ describe("OpenCode2 adapter", () => {
14861486
}).pipe(Effect.scoped, Effect.provide(TestClock.layer())),
14871487
);
14881488

1489+
it.effect("still checks the server when a failed prompt starts after a reconnect", () =>
1490+
Effect.gen(function* () {
1491+
const offered = yield* Deferred.make<void>();
1492+
const gate = makeProviderReplayGate(["drop"]);
1493+
const { runtime, thread } = yield* resumed(
1494+
[
1495+
out("session.prompt", { sessionID: SESSION, text: "<any>" }),
1496+
reply("session.prompt", {
1497+
status: 502,
1498+
body: { _tag: "UnknownError", message: "bad gateway" },
1499+
}),
1500+
// The stream drops before the prompt's execution starts, and the
1501+
// reconnect finds the session active: the prompt's run is still to come.
1502+
event("session.usage.updated", { sessionID: SESSION }, "drop"),
1503+
{ type: "runtime_exit", status: "success" } as const,
1504+
out("event.subscribe"),
1505+
out("session.active"),
1506+
replyData("session.active", { [SESSION]: { type: "running" } }),
1507+
out("permission.list", { sessionID: SESSION }),
1508+
replyData("permission.list", []),
1509+
out("session.form.list", { sessionID: SESSION }),
1510+
replyData("session.form.list", []),
1511+
// Its own start: that run is still going, so the next turn stops it and fails.
1512+
event("session.execution.started", { sessionID: SESSION }),
1513+
out("session.active"),
1514+
reply("session.active", { data: { [SESSION]: { type: "running" } } }),
1515+
out("session.interrupt", { sessionID: SESSION }),
1516+
reply("session.interrupt", { interrupted: true }),
1517+
],
1518+
{ replayGate: gate },
1519+
).pipe(
1520+
Effect.provideService(ProviderContinuationRequests.ProviderContinuationRequests, {
1521+
offer: () => Deferred.succeed(offered, undefined).pipe(Effect.asVoid),
1522+
take: Effect.never,
1523+
}),
1524+
);
1525+
const ended = yield* terminals(runtime, 2);
1526+
yield* runtime.startTurn(turnInput(thread)).pipe(Effect.ignore);
1527+
gate.release("drop");
1528+
// The late start has been read.
1529+
yield* Deferred.await(offered);
1530+
yield* runtime.startTurn(secondTurn(thread));
1531+
const [first, second] = yield* Fiber.join(ended);
1532+
assert.equal(first?.status, "failed");
1533+
assert.equal(second?.failure?.message, OPENCODE_2_STILL_STOPPING);
1534+
}).pipe(Effect.scoped, Effect.provide(TestClock.layer())),
1535+
);
1536+
14891537
it.effect("prompts again without a check after the server refused a prompt", () =>
14901538
Effect.gen(function* () {
14911539
const { runtime, thread } = yield* resumed([

‎apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2779,9 +2779,13 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
27792779
const active = yield* client.session.active();
27802780
// An execution that ended while the stream was down never said so.
27812781
for (const sessionId of busy) if (!(sessionId in active)) busy.delete(sessionId);
2782-
// A run `unsettled` waits on is running now or over: any start from here
2783-
// is a new execution's, even if the run's own start was lost in the gap.
2784-
for (const state of threads.values()) if (state.unsettled) state.unsettledStarted = true;
2782+
// A run `unsettled` waits on is over once its session is idle: any start
2783+
// from here is a new execution's, even if the run's own start was lost in
2784+
// the gap. A session still active may not have started that run yet, so
2785+
// its next start may still be the run's own; its end clears the mark.
2786+
for (const state of threads.values()) {
2787+
if (state.unsettled && !(state.sessionId in active)) state.unsettledStarted = true;
2788+
}
27852789
for (const [sessionId, state] of running) {
27862790
const turn = state.active;
27872791
if (turn === undefined) continue;

0 commit comments

Comments
 (0)