Skip to content

Commit bfa132a

Browse files
fix(server): OpenCode 2 keeps checking a failed prompt whose run starts late
A prompt whose request failed may still have reached OpenCode, so the next turn checks `session.active` first. The previous commit cleared that mark on any execution start, including the late start of that same run, so the next turn skipped the check and prompted into the run still going. The mark now records whether its run had started when it was marked. A run not yet started clears nothing with its own start; only a start after it (a new execution) clears the mark. A reconnect counts the marked run as started, since by then it is running or over and its start may have been lost in the gap. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 990c97f commit bfa132a

2 files changed

Lines changed: 68 additions & 14 deletions

File tree

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,6 +1452,40 @@ describe("OpenCode2 adapter", () => {
14521452
}).pipe(Effect.scoped, Effect.provide(TestClock.layer())),
14531453
);
14541454

1455+
it.effect("still checks the server when a failed prompt's execution starts late", () =>
1456+
Effect.gen(function* () {
1457+
const offered = yield* Deferred.make<void>();
1458+
const { runtime, thread } = yield* resumed([
1459+
out("session.prompt", { sessionID: SESSION, text: "<any>" }),
1460+
// The request failed, but the server took the prompt: its execution
1461+
// starts only after the turn already failed.
1462+
reply("session.prompt", {
1463+
status: 502,
1464+
body: { _tag: "UnknownError", message: "bad gateway" },
1465+
}),
1466+
event("session.execution.started", { sessionID: SESSION }),
1467+
// That run is still going, so the next turn stops it and fails.
1468+
out("session.active"),
1469+
reply("session.active", { data: { [SESSION]: { type: "running" } } }),
1470+
out("session.interrupt", { sessionID: SESSION }),
1471+
reply("session.interrupt", { interrupted: true }),
1472+
]).pipe(
1473+
Effect.provideService(ProviderContinuationRequests.ProviderContinuationRequests, {
1474+
offer: () => Deferred.succeed(offered, undefined).pipe(Effect.asVoid),
1475+
take: Effect.never,
1476+
}),
1477+
);
1478+
const ended = yield* terminals(runtime, 2);
1479+
yield* runtime.startTurn(turnInput(thread)).pipe(Effect.ignore);
1480+
// The late start has been read.
1481+
yield* Deferred.await(offered);
1482+
yield* runtime.startTurn(secondTurn(thread));
1483+
const [first, second] = yield* Fiber.join(ended);
1484+
assert.equal(first?.status, "failed");
1485+
assert.equal(second?.failure?.message, OPENCODE_2_STILL_STOPPING);
1486+
}).pipe(Effect.scoped, Effect.provide(TestClock.layer())),
1487+
);
1488+
14551489
it.effect("prompts again without a check after the server refused a prompt", () =>
14561490
Effect.gen(function* () {
14571491
const { runtime, thread } = yield* resumed([

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

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,12 @@ interface ThreadState {
338338
* so the next execution end belongs to that run; it clears this and ends no turn.
339339
*/
340340
unsettled: boolean;
341+
/**
342+
* Whether the run `unsettled` waits on had started when it was marked. A
343+
* prompt whose request failed may start its run late; that start is the
344+
* run's own, and only a start after it means its end was lost.
345+
*/
346+
unsettledStarted: boolean;
341347
/** The session's location, where its agents' path rules are read. */
342348
directory: string;
343349
/**
@@ -915,6 +921,11 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
915921
const emit = (event: ProviderAdapter.ProviderAdapterV2Event) =>
916922
Queue.offer(events, event).pipe(Effect.asVoid);
917923
const ownerOf = (sessionId: string) => childOwners.get(sessionId) ?? threads.get(sessionId);
924+
/** Marks a run T3 ended while OpenCode may still run it (see `unsettled`). */
925+
const markUnsettled = (state: ThreadState) => {
926+
state.unsettled = true;
927+
state.unsettledStarted = busy.has(state.sessionId);
928+
};
918929

919930
const newThreadState = (
920931
sessionId: string,
@@ -928,6 +939,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
928939
active: undefined,
929940
model: undefined,
930941
unsettled: false,
942+
unsettledStarted: false,
931943
directory,
932944
agent: "build",
933945
rules: undefined,
@@ -1818,7 +1830,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
18181830
reason,
18191831
});
18201832
if (state.active === undefined) return;
1821-
state.unsettled = true;
1833+
markUnsettled(state);
18221834
yield* finishTurn(state, {
18231835
status: "failed",
18241836
failure: makeProviderFailure({
@@ -1915,7 +1927,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
19151927
const [permissions, forms] = listed.value;
19161928
if (permissions.length === 0 && forms.length === 0) return;
19171929
// The stopped run's end is not the next turn's.
1918-
state.unsettled = true;
1930+
markUnsettled(state);
19191931
for (const request of permissions) {
19201932
yield* stopStaleRequest(state.sessionId, { type: "permission", id: request.id });
19211933
}
@@ -2100,7 +2112,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
21002112
// goes on. A subagent's session has no Stop end to skip.
21012113
const asker = threads.get(form.sessionID) ?? state;
21022114
if (!cancelled) return yield* abandonRequest(asker, "form cancel failed");
2103-
if (asker.subagent === undefined) asker.unsettled = true;
2115+
if (asker.subagent === undefined) markUnsettled(asker);
21042116
yield* finishTurn(asker, {
21052117
status: "failed",
21062118
failure: makeProviderFailure({
@@ -2332,7 +2344,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
23322344
delivered.every((entry) => state.stoppedChildren.has(entry.childId));
23332345
for (const entry of delivered) state.stoppedChildren.delete(entry.childId);
23342346
if (stopped) {
2335-
state.unsettled = true;
2347+
markUnsettled(state);
23362348
yield* client.session
23372349
.interrupt({ sessionID: Session.ID.make(state.sessionId) })
23382350
.pipe(Effect.timeout(INTERRUPT_TIMEOUT), Effect.ignore({ log: true }));
@@ -2459,13 +2471,17 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
24592471
event.type === "session.execution.started" ||
24602472
event.type === "unreadable.execution.started"
24612473
) {
2462-
busy.add(sessionId);
24632474
// A session runs one execution at a time, each opening with `started`
2464-
// on this ordered stream, so the run `unsettled` waits on is over: an
2465-
// end not seen by now was lost with a dropped stream, and the next
2466-
// end is this execution's own.
2475+
// on this ordered stream. Once the run `unsettled` waits on has
2476+
// started, a later start means that run is over: an end not seen by
2477+
// now was lost with a dropped stream, and the next end is this
2478+
// execution's own. A run not yet started when it was marked starts here.
24672479
const state = threads.get(sessionId);
2468-
if (state !== undefined) state.unsettled = false;
2480+
if (state?.unsettled === true) {
2481+
if (state.unsettledStarted) state.unsettled = false;
2482+
else state.unsettledStarted = true;
2483+
}
2484+
busy.add(sessionId);
24692485
} else if (event.type === "unreadable.execution.ended" || executionEnd(event.type)) {
24702486
busy.delete(sessionId);
24712487
}
@@ -2758,10 +2774,14 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
27582774
const background = [...threads.values()].filter(
27592775
(state) => state.subagent === undefined && hasBackground(state),
27602776
);
2761-
if (running.length === 0 && background.length === 0 && busy.size === 0) return;
2777+
const marked = [...threads.values()].some((state) => state.unsettled);
2778+
if (running.length === 0 && background.length === 0 && busy.size === 0 && !marked) return;
27622779
const active = yield* client.session.active();
27632780
// An execution that ended while the stream was down never said so.
27642781
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;
27652785
for (const [sessionId, state] of running) {
27662786
const turn = state.active;
27672787
if (turn === undefined) continue;
@@ -3483,7 +3503,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
34833503
const running = state.wakes.some((wake) => wake.running);
34843504
for (const wake of state.wakes.splice(0)) wake.dropped = true;
34853505
if (running) {
3486-
state.unsettled = true;
3506+
markUnsettled(state);
34873507
yield* client.session
34883508
.interrupt({ sessionID: Session.ID.make(state.sessionId) })
34893509
.pipe(Effect.timeout(INTERRUPT_TIMEOUT), Effect.ignore({ log: true }));
@@ -3845,7 +3865,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
38453865
? Effect.gen(function* () {
38463866
// Without a clear rejection the server may have taken the
38473867
// prompt, so the next turn checks before it prompts again.
3848-
if (!CLEAR_PROMPT_REJECTIONS.has(cause._tag)) state.unsettled = true;
3868+
if (!CLEAR_PROMPT_REJECTIONS.has(cause._tag)) markUnsettled(state);
38493869
yield* finishTurn(state, {
38503870
status: "failed",
38513871
failure: makeProviderFailure({ cause, class: "provider_error" }),
@@ -3953,13 +3973,13 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
39533973
),
39543974
);
39553975
if (reply._tag === "None") {
3956-
state.unsettled = true;
3976+
markUnsettled(state);
39573977
return yield* lock.withPermit(finishTurn(state, { status: "interrupted" }));
39583978
}
39593979
// Between the executions a late steer spans nothing runs yet. The
39603980
// steers are taken back, and a run they already started is skipped.
39613981
if (!reply.value.interrupted && state.active === turn && turn.heldEnd !== undefined) {
3962-
state.unsettled = true;
3982+
markUnsettled(state);
39633983
yield* lock.withPermit(finishTurn(state, { status: "interrupted" }));
39643984
return yield* cancelStrandedSteers(state);
39653985
}

0 commit comments

Comments
 (0)