Skip to content

Commit b777bfe

Browse files
t3dotggclaude
andcommitted
fix(opencode): a turn that spans an event stream reconnect reports partial usage
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8d12cf1 commit b777bfe

3 files changed

Lines changed: 23 additions & 2 deletions

File tree

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2150,6 +2150,13 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
21502150
const { runtime, thread } = yield* resumed([
21512151
out("session.prompt", { sessionID: SESSION, id: PROMPT_ID, text: "<any>" }),
21522152
promptAccepted,
2153+
event("session.step.ended", {
2154+
sessionID: SESSION,
2155+
assistantMessageID: "msg_before_gap",
2156+
finish: "tool-calls",
2157+
cost: 0,
2158+
tokens: { input: 100, output: 10, reasoning: 0, cache: { read: 0, write: 0 } },
2159+
}),
21532160
...reconnected({}, "succeeded"),
21542161
]);
21552162
const events = yield* runtime.events.pipe(
@@ -2164,6 +2171,15 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
21642171
"command_execution:completed",
21652172
]);
21662173
assert.deepInclude(collected.at(-1), { type: "turn.terminal", status: "completed" });
2174+
// Steps that ended during the gap sent no tokens, so the measured step is only part of it.
2175+
const settled = collected.findLast(
2176+
(event) =>
2177+
event.type === "provider_turn.updated" && event.providerTurn.completedAt !== null,
2178+
);
2179+
assert.deepInclude(
2180+
settled?.type === "provider_turn.updated" ? settled.providerTurn.turnTokenUsage : undefined,
2181+
{ usageStatus: "partial", inputTokens: 100, outputTokens: 10 },
2182+
);
21672183
}).pipe(Effect.scoped),
21682184
);
21692185

‎docs/internals/product-analytics.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ list, so user-configured names stay out.
9595
Status and counters answer different questions. `usageReported: false` means
9696
the adapter attached no usage; a harness where every row says so is unsupported,
9797
not idle. `partial` keeps valid counts that cannot establish a whole-turn total,
98-
such as an OpenCode step that ended without a report or an interrupted segment.
98+
such as an OpenCode step that ended without a report, an OpenCode stream
99+
reconnect, or an interrupted segment.
99100
Explicit zeros are sent and unknown counts stay absent. `localCommand` marks a
100101
provider-local command such as Claude's `/compact`, whose result counters are not
101102
model work. Reasoning tokens are a subset of output.

‎packages/provider-opencode/src/server/v2/adapter.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ interface ActiveTurn {
211211
};
212212
// Steps OpenCode started; a step that ends without tokens leaves the total partial.
213213
startedSteps: number;
214+
// A reconnect dropped events, so steps may have ended unseen.
215+
missedEvents: boolean;
214216
steps: number;
215217
readonly models: Set<string>;
216218
lastStep: Tokens | undefined;
@@ -645,7 +647,7 @@ const turnTokenUsage = (turn: ActiveTurn, status: OrchestrationV2ProviderTurn["s
645647
...models,
646648
usageScope: "main_agent" as const,
647649
usageStatus:
648-
status === "completed" && turn.steps >= turn.startedSteps
650+
status === "completed" && !turn.missedEvents && turn.steps >= turn.startedSteps
649651
? ("complete" as const)
650652
: ("partial" as const),
651653
inputTokens: turn.usage.input,
@@ -1122,6 +1124,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
11221124
nextOrdinal: providerTurn.ordinal * 100 + 1,
11231125
usage: { input: 0, cached: 0, cacheWrite: 0, output: 0, reasoning: 0 },
11241126
startedSteps: 0,
1127+
missedEvents: false,
11251128
steps: 0,
11261129
models: new Set(),
11271130
lastStep: undefined,
@@ -2584,6 +2587,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
25842587
for (const [sessionId, state] of running) {
25852588
const turn = state.active;
25862589
if (turn === undefined) continue;
2590+
turn.missedEvents = true;
25872591
const ended = yield* backfill(sessionId, state);
25882592
if (sessionId in active || state.active !== turn) continue;
25892593
yield* finishTurn(

0 commit comments

Comments
 (0)