Skip to content

Commit b86909e

Browse files
luoxuanzaoQoder-AI
andauthored
chore: upgrade qoder-agent-sdk to 1.0.23 and consume the new context-usage type (#28)
* fix: read the CLI 1.1.21 context-usage response shape qodercli 1.1.21 changed the get_context_usage control response from flat token counts (totalTokens/maxTokens/rawMaxTokens/percentage) to a percentage-based shape (contextWindow.usedPercentage, categories, tokenCountsAvailable). The turn tracker kept reading the old fields, so every lookup came back undefined and the context usage meter was stuck at its "appears after the first response" placeholder. Parse the new shape: percentage drives the meter directly, absolute token counts are used when the CLI reports them (tokenCountsAvailable), and window size falls back to the previous turn or the model catalog. Router tests now mock the new wire shape. Co-authored-by: QoderAI (Qwen 3.8 Max) <qoder_ai@qoder.com> * fix: keep the chosen context-window tier in the usage meter The post-response context-usage refresh fell back to the model catalog default window whenever the CLI omitted maxTokens, so a tier chosen in the per-model editor (such as 400K) reverted to the default (200K) after the first message. Route the effective per-model context window into the turn tracker and prefer it over the catalog fallback when the CLI reports no absolute window. Co-authored-by: QoderAI (Qwen 3.8 Max) <qoder_ai@qoder.com> * chore: upgrade @qoder-ai/qoder-agent-sdk to 1.0.23 The new SDK ships the updated get_context_usage response type (contextWindow.usedPercentage plus percentage-only categories), so the turn tracker now consumes the SDK type directly instead of a local stand-in: drop the cast, the dead token-count branches, and the CLI-reported window path that the API no longer provides. Token counts shown in the meter tooltip are derived from the reported percentage against the effective context window (configured tier first). Smoke-tested against a real CLI; the one affected router test now asserts the percentage-derived shape. Co-authored-by: QoderAI (Qwen 3.8 Max) <qoder_ai@qoder.com> * fix: keep the context meter stable while a response streams Mid-turn usage chunks were built against the model catalog window and could carry zeroed token counts, so the meter flashed to the catalog default (200K) or to its 0% placeholder during a response and only recovered when the post-response refresh ran. Transform options now carry the effective per-model context window, the router drops zeroed usage snapshots while a real reading is buffered, and the buffered reading survives across turns (it is the fallback chain's previous-turn source) instead of being cleared at turn boundaries. Co-authored-by: QoderAI (Qwen 3.8 Max) <qoder_ai@qoder.com> --------- Co-authored-by: QoderAI (Qwen 3.8 Max) <qoder_ai@qoder.com>
1 parent bcda153 commit b86909e

7 files changed

Lines changed: 108 additions & 65 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ version with its date and start a fresh empty `[Unreleased]` above it.
7070
per-model editor after a response; previously the post-response
7171
refresh silently fell back to the model catalog default (such as
7272
200K), so a 400K selection reverted to 200K once a message was sent.
73+
- The context usage meter no longer flickers to 0% or to the catalog
74+
default window while a response is streaming: mid-turn usage
75+
snapshots now carry the configured context-window tier, and zeroed
76+
snapshots can no longer overwrite an existing reading.
7377

7478
## [1.0.4] - 2026-08-12
7579

‎package-lock.json‎

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"@codemirror/state": "^6.5.0",
5555
"@codemirror/view": "^6.38.6",
5656
"@modelcontextprotocol/sdk": "~1.30.0",
57-
"@qoder-ai/qoder-agent-sdk": "1.0.16",
57+
"@qoder-ai/qoder-agent-sdk": "^1.0.23",
5858
"tslib": "^2.8.1"
5959
},
6060
"overrides": {

‎src/qoder/runtime/qoder-response-router.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export class QoderResponseRouter {
6565
const autoTurnBufferStartLength = this.autoTurnBuffer.length;
6666
const transformOptions = this.deps.turnTracker.getTransformOptions(
6767
this.deps.getConfiguredModel(),
68+
this.deps.getConfiguredContextWindow(),
6869
);
6970

7071
for (const event of transformSDKMessage(message, transformOptions)) {
@@ -92,6 +93,15 @@ export class QoderResponseRouter {
9293

9394
if (!isStreamChunk(event)) continue;
9495

96+
// Streaming can emit zeroed usage snapshots (the CLI masks counts
97+
// mid-turn); dropping them keeps the meter on its last real reading
98+
// instead of flashing back to the placeholder.
99+
if (event.type === 'usage'
100+
&& event.usage.contextTokens <= 0
101+
&& this.deps.turnTracker.hasBufferedUsage()) {
102+
continue;
103+
}
104+
95105
if (
96106
message.type === 'assistant'
97107
&& event.type === 'text'

‎src/qoder/runtime/qoder-turn-tracker.ts‎

Lines changed: 19 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,6 @@ interface ContextUsageRequest {
2121
sessionId: string | null;
2222
}
2323

24-
/**
25-
* Wire shape returned by the qodercli `get_context_usage` control API
26-
* (1.1.21+). Percentages are reported in percent units (5.4 === 5.4%);
27-
* absolute token counts only appear when the CLI can provide them.
28-
* The SDK's declared response type still mirrors an older shape, so the
29-
* tracker reads the payload through this interface.
30-
*/
31-
interface CliContextUsagePayload {
32-
model?: string;
33-
tokenCountsAvailable?: boolean;
34-
contextWindow?: {
35-
usedPercentage?: number;
36-
usedTokens?: number;
37-
maxTokens?: number;
38-
};
39-
categories?: Array<{ type?: string; tokens?: number; percentage?: number }>;
40-
}
41-
4224
/** Owns metadata and usage state that lives for exactly one Qoder turn. */
4325
export class QoderTurnTracker {
4426
private metadata: ChatTurnMetadata = {};
@@ -49,13 +31,11 @@ export class QoderTurnTracker {
4931
consumeMetadata(): ChatTurnMetadata {
5032
const metadata = { ...this.metadata };
5133
this.metadata = {};
52-
this.bufferedUsageChunk = null;
5334
return metadata;
5435
}
5536

5637
reset(): void {
5738
this.metadata = {};
58-
this.bufferedUsageChunk = null;
5939
this.clearTransformState();
6040
}
6141

@@ -73,6 +53,15 @@ export class QoderTurnTracker {
7353
return chunk;
7454
}
7555

56+
/**
57+
* Whether a non-zero usage reading is buffered. The reading survives
58+
* across turns so mid-turn zeroed snapshots cannot flash the meter
59+
* back to its placeholder; only a fresh runtime starts empty.
60+
*/
61+
hasBufferedUsage(): boolean {
62+
return (this.bufferedUsageChunk?.usage.contextTokens ?? 0) > 0;
63+
}
64+
7665
updateContextWindow(contextWindow: number): UsageChunk | null {
7766
if (!this.bufferedUsageChunk || contextWindow <= 0) {
7867
return null;
@@ -96,9 +85,10 @@ export class QoderTurnTracker {
9685
return nextChunk;
9786
}
9887

99-
getTransformOptions(model: string) {
88+
getTransformOptions(model: string, contextWindow?: number) {
10089
return {
10190
intendedModel: toQoderRuntimeModelId(model),
91+
contextWindow,
10292
streamState: this.streamState,
10393
usageState: this.usageState,
10494
};
@@ -112,7 +102,9 @@ export class QoderTurnTracker {
112102
}
113103

114104
try {
115-
const payload = await activeQuery.getContextUsage() as unknown as CliContextUsagePayload;
105+
// The CLI reports occupancy as a percentage only; absolute token
106+
// counts are derived against the effective context window below.
107+
const payload = await activeQuery.getContextUsage();
116108
if (!request.isCurrentQuery(activeQuery)) {
117109
return null;
118110
}
@@ -121,23 +113,16 @@ export class QoderTurnTracker {
121113
const model = toQoderRuntimeModelId(
122114
payload.model || previousUsage?.model || request.configuredModel,
123115
);
124-
const rawMaxTokens = payload.contextWindow?.maxTokens;
125-
const reportedMaxTokens = typeof rawMaxTokens === 'number'
126-
&& Number.isFinite(rawMaxTokens) && rawMaxTokens > 0
127-
? rawMaxTokens
128-
: undefined;
129-
const hasReportedWindow = reportedMaxTokens !== undefined;
116+
// The CLI reports occupancy only; the window comes from the
117+
// configured tier, the previous turn, or the model catalog.
130118
const previousContextWindow = previousUsage?.model === model && previousUsage.contextWindow > 0
131119
? previousUsage.contextWindow
132120
: undefined;
133-
// Without a CLI-reported window the configured tier is the source of
134-
// truth; buffered chunks only carry catalog fallbacks.
135121
const configuredContextWindow = Number.isFinite(request.configuredContextWindow)
136122
&& (request.configuredContextWindow as number) > 0
137123
? request.configuredContextWindow
138124
: undefined;
139-
const contextWindow = reportedMaxTokens
140-
?? configuredContextWindow
125+
const contextWindow = configuredContextWindow
141126
?? previousContextWindow
142127
?? getContextWindowSize(model);
143128

@@ -146,27 +131,10 @@ export class QoderTurnTracker {
146131
&& Number.isFinite(rawUsedPercentage);
147132
const ratio = hasReportedRatio ? Math.min(1, Math.max(0, rawUsedPercentage / 100)) : 0;
148133

149-
// Absolute counts are only meaningful when the CLI can provide them.
150-
const categoryTokens = payload.tokenCountsAvailable === true
151-
? (payload.categories ?? []).reduce(
152-
(sum, category) => sum + (typeof category.tokens === 'number'
153-
&& Number.isFinite(category.tokens) && category.tokens > 0
154-
? category.tokens
155-
: 0),
156-
0,
157-
)
158-
: 0;
159-
const rawUsedTokens = payload.contextWindow?.usedTokens;
160-
const usedTokens = typeof rawUsedTokens === 'number'
161-
&& Number.isFinite(rawUsedTokens) && rawUsedTokens > 0
162-
? rawUsedTokens
163-
: 0;
164-
const reportedTotalTokens = [usedTokens, categoryTokens].find(value => value > 0) ?? 0;
165134
const estimatedContextTokens = ratio > 0
166135
? Math.max(1, Math.round(contextWindow * ratio))
167136
: 0;
168-
const contextTokens = reportedTotalTokens
169-
|| estimatedContextTokens
137+
const contextTokens = estimatedContextTokens
170138
|| previousUsage?.contextTokens
171139
|| 0;
172140

@@ -178,7 +146,7 @@ export class QoderTurnTracker {
178146
cacheCreationInputTokens: previousUsage?.cacheCreationInputTokens || 0,
179147
cacheReadInputTokens: previousUsage?.cacheReadInputTokens || 0,
180148
contextWindow,
181-
contextWindowIsAuthoritative: hasReportedWindow,
149+
contextWindowIsAuthoritative: false,
182150
contextTokens,
183151
percentage: hasReportedRatio
184152
? Math.round(ratio * 100)

‎src/qoder/stream/transform-qoder-message.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ function transformTaskNotification(message: SDKMessage): StreamChunk | null {
8080
export interface TransformOptions {
8181
/** The intended model from settings/query (used for context window size). */
8282
intendedModel?: string;
83+
/** Effective context window from the per-model editor override, if any. */
84+
contextWindow?: number;
8385
/** Tracks active streamed tool blocks so input_json_delta can be normalized. */
8486
streamState?: TransformStreamState;
8587
/** Tracks prompt-token usage across SDK-compatible stream events. */
@@ -322,7 +324,9 @@ function samePromptUsage(a: PromptUsageSnapshot, b: PromptUsageSnapshot): boolea
322324

323325
function buildUsageInfo(promptUsage: PromptUsageSnapshot, options?: TransformOptions): UsageInfo {
324326
const model = options?.intendedModel ?? 'sonnet';
325-
const contextWindow = getContextWindowSize(model);
327+
const contextWindow = typeof options?.contextWindow === 'number' && options.contextWindow > 0
328+
? options.contextWindow
329+
: getContextWindowSize(model);
326330
const percentage = Math.min(100, Math.max(0, Math.round((promptUsage.contextTokens / contextWindow) * 100)));
327331

328332
return {

‎tests/unit/qoder/runtime/qoder-chat-runtime.test.ts‎

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,16 +1381,24 @@ describe('QoderChatRuntime', () => {
13811381
});
13821382
});
13831383

1384-
it('should prefer public context token counts when Qoder CLI returns them', async () => {
1384+
it('should derive token counts from the percentage against the catalog window', async () => {
13851385
(service as any).persistentQuery = {
13861386
getContextUsage: jest.fn().mockResolvedValue({
13871387
model: 'ultimate',
1388-
tokenCountsAvailable: true,
1389-
contextWindow: { usedPercentage: 4, usedTokens: 12_000, maxTokens: 300_000 },
1388+
contextWindow: { usedPercentage: 4 },
13901389
categories: [
1391-
{ type: 'system_prompt', tokens: 2_000, percentage: 0.7 },
1392-
{ type: 'messages', tokens: 10_000, percentage: 3.3 },
1390+
{ type: 'system_prompt', percentage: 0.7 },
1391+
{ type: 'messages', percentage: 3.3 },
13931392
],
1393+
autoCompact: { enabled: true, thresholdPercentage: 92 },
1394+
skills: { count: 0, percentageOfContext: 0, items: [] },
1395+
duplicateFileReads: [],
1396+
session: {
1397+
messageCount: 2,
1398+
promptCount: 1,
1399+
toolCalls: { total: 0, succeeded: 0, failed: 0 },
1400+
linesChanged: { added: 0, removed: 0 },
1401+
},
13941402
}),
13951403
};
13961404

@@ -1407,15 +1415,64 @@ describe('QoderChatRuntime', () => {
14071415
inputTokens: 0,
14081416
cacheCreationInputTokens: 0,
14091417
cacheReadInputTokens: 0,
1410-
contextWindow: 300_000,
1411-
contextWindowIsAuthoritative: true,
1412-
contextTokens: 12_000,
1418+
contextWindow: 200_000,
1419+
contextWindowIsAuthoritative: false,
1420+
contextTokens: 8_000,
14131421
percentage: 4,
14141422
},
14151423
sessionId: null,
14161424
});
14171425
});
14181426

1427+
it('should size streamed usage chunks against the configured context-window tier', async () => {
1428+
(mockPlugin as any).settings.model = 'performance';
1429+
(mockPlugin as any).settings.qoder = {
1430+
discoveredModels: [{
1431+
value: 'performance',
1432+
contextTiers: [
1433+
{ label: '200K', tokenCount: 200_000, isDefault: true },
1434+
{ label: '400K', tokenCount: 400_000, isDefault: false },
1435+
],
1436+
}],
1437+
modelOverrides: { performance: { contextWindow: 400_000 } },
1438+
};
1439+
1440+
await (service as any).responseRouter.route({
1441+
type: 'assistant',
1442+
message: {
1443+
content: [{ type: 'text', text: 'ok' }],
1444+
usage: { input_tokens: 20_000 },
1445+
},
1446+
});
1447+
1448+
expect(onChunk).toHaveBeenCalledWith(expect.objectContaining({
1449+
type: 'usage',
1450+
usage: expect.objectContaining({
1451+
contextWindow: 400_000,
1452+
contextTokens: 20_000,
1453+
percentage: 5,
1454+
}),
1455+
}));
1456+
});
1457+
1458+
it('should not flash the meter to zero when streaming emits a zeroed usage snapshot', async () => {
1459+
await (service as any).responseRouter.route({
1460+
type: 'assistant',
1461+
message: { content: [], usage: { input_tokens: 10_000 } },
1462+
});
1463+
onChunk.mockClear();
1464+
1465+
await (service as any).responseRouter.route({
1466+
type: 'assistant',
1467+
message: { content: [], usage: { input_tokens: 0 } },
1468+
});
1469+
1470+
const zeroChunks = onChunk.mock.calls.filter(
1471+
([chunk]: any) => chunk.type === 'usage' && chunk.usage.contextTokens <= 0,
1472+
);
1473+
expect(zeroChunks).toHaveLength(0);
1474+
});
1475+
14191476
it('should still finish the turn when getContextUsage is unavailable', async () => {
14201477
(service as any).persistentQuery = {
14211478
getContextUsage: jest.fn().mockRejectedValue(new Error('unsupported')),

0 commit comments

Comments
 (0)