Skip to content

Commit d3a0df1

Browse files
committed
fix(server): keep OpenCode usage worker alive across snapshot failures
Handle probe/emit failures per worker iteration instead of letting one bad snapshot end the runContextWindowUsageWorker loop. A dead worker left modelContextWindowQueue set with nothing reading it, silencing the meter for the rest of the session. Only interruption still ends the loop on session teardown.
1 parent 346be23 commit d3a0df1

2 files changed

Lines changed: 120 additions & 9 deletions

File tree

‎apps/server/src/provider/Layers/OpenCodeAdapter.test.ts‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7805,6 +7805,108 @@ it.layer(OpenCodeAdapterTestLayer)("OpenCodeAdapterLive", (it) => {
78057805
}),
78067806
);
78077807

7808+
it.effect("keeps emitting usage snapshots after a snapshot emission fails", () =>
7809+
Effect.gen(function* () {
7810+
const threadId = asThreadId("thread-opencode-token-usage-worker-survives-failure");
7811+
runtimeMock.state.configProviders = {
7812+
providers: [
7813+
{
7814+
id: "opencode",
7815+
models: {
7816+
"gpt-5.4": { limit: { context: 200_000, output: 16_384 } },
7817+
},
7818+
},
7819+
],
7820+
};
7821+
runtimeMock.state.subscribedEvents = [
7822+
{
7823+
type: "message.updated",
7824+
properties: {
7825+
sessionID: "http://127.0.0.1:9999/session",
7826+
info: {
7827+
id: "msg-worker-survives-a",
7828+
role: "assistant",
7829+
providerID: "opencode",
7830+
modelID: "gpt-5.4",
7831+
tokens: {
7832+
input: 1000,
7833+
output: 100,
7834+
reasoning: 10,
7835+
cache: { read: 200, write: 50 },
7836+
},
7837+
},
7838+
},
7839+
},
7840+
{
7841+
type: "message.updated",
7842+
properties: {
7843+
sessionID: "http://127.0.0.1:9999/session",
7844+
info: {
7845+
id: "msg-worker-survives-b",
7846+
role: "assistant",
7847+
providerID: "opencode",
7848+
modelID: "gpt-5.4",
7849+
tokens: {
7850+
input: 2000,
7851+
output: 100,
7852+
reasoning: 10,
7853+
cache: { read: 200, write: 50 },
7854+
},
7855+
},
7856+
},
7857+
},
7858+
];
7859+
7860+
// Fail the third event-id allocation: the first two belong to
7861+
// session.started/thread.started, so the first usage snapshot's
7862+
// buildEventBase dies while the second snapshot must still flow.
7863+
let uuidCalls = 0;
7864+
const nodeCrypto = yield* Crypto.Crypto;
7865+
const gatedCrypto = {
7866+
...nodeCrypto,
7867+
randomUUIDv4: Effect.suspend(() => {
7868+
uuidCalls += 1;
7869+
if (uuidCalls === 3) {
7870+
return Effect.die(new Error("worker-survival-test injected uuid failure"));
7871+
}
7872+
return nodeCrypto.randomUUIDv4;
7873+
}),
7874+
} satisfies Crypto.Crypto;
7875+
const adapter = yield* makeOpenCodeAdapter(openCodeAdapterTestSettings).pipe(
7876+
Effect.provideService(Crypto.Crypto, gatedCrypto),
7877+
);
7878+
7879+
const eventsFiber = yield* adapter.streamEvents.pipe(
7880+
Stream.filter((event) => event.threadId === threadId),
7881+
Stream.take(3),
7882+
Stream.runCollect,
7883+
Effect.forkChild,
7884+
);
7885+
7886+
yield* adapter.startSession({
7887+
provider: ProviderDriverKind.make("opencode"),
7888+
threadId,
7889+
runtimeMode: "full-access",
7890+
});
7891+
7892+
// Without per-iteration recovery the worker dies on the first failure
7893+
// and the second snapshot never arrives, so take(3) never completes.
7894+
const events = Array.from(yield* Fiber.join(eventsFiber).pipe(Effect.timeout("1 second")));
7895+
NodeAssert.deepEqual(
7896+
events.map((event) => event.type),
7897+
["session.started", "thread.started", "thread.token-usage.updated"],
7898+
);
7899+
const usageEvent = events[2];
7900+
NodeAssert.ok(usageEvent);
7901+
if (usageEvent.type === "thread.token-usage.updated") {
7902+
NodeAssert.equal(usageEvent.payload.usage.usedTokens, 2360);
7903+
NodeAssert.equal(usageEvent.payload.usage.maxTokens, 200_000);
7904+
} else {
7905+
NodeAssert.fail(`expected thread.token-usage.updated, got ${usageEvent.type}`);
7906+
}
7907+
}),
7908+
);
7909+
78087910
it.effect("skips metadata probes for zero-token assistant broadcasts", () =>
78097911
Effect.gen(function* () {
78107912
const adapter = yield* OpenCodeAdapter;

‎apps/server/src/provider/Layers/OpenCodeAdapter.ts‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,21 +1249,30 @@ export function makeOpenCodeAdapter(
12491249
// Ordered background worker for usage snapshots. Takes pending inputs
12501250
// FIFO so an older message can never overwrite newer usage: every
12511251
// snapshot is built from the metadata cache as of its own turn in line.
1252-
// Runs until the session scope closes.
1252+
// Failures are handled per iteration — one bad snapshot must not end the
1253+
// worker, or the queue would stay set with nothing reading it and the
1254+
// meter would go silent for the rest of the session. Only interruption
1255+
// escapes to end the loop when the session scope closes.
12531256
const runContextWindowUsageWorker = Effect.fn("runContextWindowUsageWorker")(function* (
12541257
context: OpenCodeSessionContext,
12551258
queue: Queue.Queue<PendingOpenCodeContextWindowUsage>,
12561259
) {
12571260
while (true) {
12581261
const pending = yield* Queue.take(queue);
1259-
yield* loadContextUsageMetadata(context).pipe(Effect.ignore);
1260-
yield* emitUsageSnapshot(
1261-
context,
1262-
pending.tokens,
1263-
pending.providerID,
1264-
pending.modelID,
1265-
pending.turnId,
1266-
pending.raw,
1262+
yield* Effect.gen(function* () {
1263+
yield* loadContextUsageMetadata(context).pipe(Effect.ignore);
1264+
yield* emitUsageSnapshot(
1265+
context,
1266+
pending.tokens,
1267+
pending.providerID,
1268+
pending.modelID,
1269+
pending.turnId,
1270+
pending.raw,
1271+
);
1272+
}).pipe(
1273+
Effect.catchCause((cause) =>
1274+
Cause.hasInterruptsOnly(cause) ? Effect.interrupt : Effect.void,
1275+
),
12671276
);
12681277
}
12691278
});

0 commit comments

Comments
 (0)