Skip to content

Commit 537322a

Browse files
committed
fix(server): handle fractional limits and interrupted context usage
- require integer context windows before emitting maxTokens (fractional limits violated PositiveInt and could persist) - keep emitting token usage even when display output is suppressed after an interrupt, otherwise the meter underreports until the next completed message
1 parent 09a9319 commit 537322a

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ export function buildOpenCodeContextWindowUsage(input: {
246246

247247
const modelContextWindow =
248248
typeof input.modelContextWindow === "number" &&
249+
Number.isInteger(input.modelContextWindow) &&
249250
Number.isFinite(input.modelContextWindow) &&
250251
input.modelContextWindow > 0
251252
? input.modelContextWindow
@@ -1118,6 +1119,7 @@ export function makeOpenCodeAdapter(
11181119
context.modelContextWindowCache.set(
11191120
`${provider.id}/${modelId}`,
11201121
typeof contextWindow === "number" &&
1122+
Number.isInteger(contextWindow) &&
11211123
Number.isFinite(contextWindow) &&
11221124
contextWindow > 0
11231125
? contextWindow
@@ -2159,6 +2161,24 @@ export function makeOpenCodeAdapter(
21592161
event.type === "message.part.updated" ||
21602162
(event.type === "message.updated" && event.properties.info.role === "assistant"));
21612163
if (suppressInterruptedParentOutput) {
2164+
// Token usage is not display output — keep the meter accurate even
2165+
// for interrupted turns. Without this, an aborted assistant message
2166+
// that already carried the latest cumulative counts would be dropped
2167+
// and the meter would underreport until the next completed message.
2168+
if (
2169+
event.type === "message.updated" &&
2170+
event.properties.info.role === "assistant" &&
2171+
event.properties.info.tokens !== undefined
2172+
) {
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+
);
2181+
}
21622182
return;
21632183
}
21642184

0 commit comments

Comments
 (0)