Skip to content

Commit 99de889

Browse files
committed
fix(server): stamp interrupted context usage on the aborted turn
The suppress-path emit used activeTurnId, which is empty after an interrupt (turnless, breaking revert) and is already the next turn once awaitingBusyAfterInterruption is set (misattributing aborted counts to the new turn). Use interruptedTurnId / pendingIdle turn instead, and avoid stamping on the new turn when no aborted id is known.
1 parent 537322a commit 99de889

1 file changed

Lines changed: 30 additions & 8 deletions

File tree

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

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2165,19 +2165,41 @@ export function makeOpenCodeAdapter(
21652165
// for interrupted turns. Without this, an aborted assistant message
21662166
// that already carried the latest cumulative counts would be dropped
21672167
// and the meter would underreport until the next completed message.
2168+
// Use the interrupted turn's id, not the current `activeTurnId`:
2169+
// after an interrupt `activeTurnId` is empty (turnless) and once a
2170+
// follow-up prompt sets `awaitingBusyAfterInterruption` it is already
2171+
// the *next* turn, so stamping with `turnId` would misattribute the
2172+
// aborted counts or break revert.
21682173
if (
21692174
event.type === "message.updated" &&
21702175
event.properties.info.role === "assistant" &&
21712176
event.properties.info.tokens !== undefined
21722177
) {
2173-
yield* emitContextWindowUsage(
2174-
context,
2175-
event.properties.info.tokens,
2176-
event.properties.info.providerID,
2177-
event.properties.info.modelID,
2178-
turnId,
2179-
event,
2180-
);
2178+
const usageTurnId =
2179+
context.interruptedTurnId ??
2180+
context.pendingIdleReconciliation?.turnId ??
2181+
(context.awaitingBusyAfterInterruption ? undefined : turnId);
2182+
if (usageTurnId !== undefined) {
2183+
yield* emitContextWindowUsage(
2184+
context,
2185+
event.properties.info.tokens,
2186+
event.properties.info.providerID,
2187+
event.properties.info.modelID,
2188+
usageTurnId,
2189+
event,
2190+
);
2191+
} else if (!context.awaitingBusyAfterInterruption) {
2192+
// No interrupted id known and not on the next turn — emit
2193+
// turnless as a last resort rather than dropping the meter.
2194+
yield* emitContextWindowUsage(
2195+
context,
2196+
event.properties.info.tokens,
2197+
event.properties.info.providerID,
2198+
event.properties.info.modelID,
2199+
turnId,
2200+
event,
2201+
);
2202+
}
21812203
}
21822204
return;
21832205
}

0 commit comments

Comments
 (0)