Skip to content

Commit e5daf04

Browse files
authored
Use last-turn token usage for ACP context updates (#107)
Report ACP context usage from last-turn token usage instead of cumulative totals, and align the status context window output with the same source.
1 parent 596b824 commit e5daf04

5 files changed

Lines changed: 7 additions & 7 deletions

File tree

src/CodexCommands.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ export class CodexCommands {
206206
const accountText = this.formatAccountInfo(sessionState.account);
207207
const tokenUsageText = this.formatTokenUsage(sessionState.totalTokenUsage);
208208
const contextWindowText = this.formatContextWindow(
209-
sessionState.totalTokenUsage,
209+
sessionState.lastTokenUsage,
210210
sessionState.modelContextWindow
211211
);
212212

src/CodexEventHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ export class CodexEventHandler {
401401
private createUsageUpdate(params: ThreadTokenUsageUpdatedNotification): UpdateSessionEvent | null {
402402
this.handleTokenUsageUpdated(params);
403403

404-
const used = this.sessionState.totalTokenUsage?.totalTokens;
404+
const used = this.sessionState.lastTokenUsage?.totalTokens;
405405
const size = this.sessionState.modelContextWindow;
406406
if (used == null || size == null || size <= 0) {
407407
return null;

src/__tests__/CodexACPAgent/data/token-usage-session-update-multiple.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"sessionId": "test-session-id",
2020
"update": {
2121
"sessionUpdate": "usage_update",
22-
"used": 2000,
22+
"used": 1000,
2323
"size": 128000
2424
}
2525
}
@@ -32,7 +32,7 @@
3232
"sessionId": "test-session-id",
3333
"update": {
3434
"sessionUpdate": "usage_update",
35-
"used": 3500,
35+
"used": 1500,
3636
"size": 128000
3737
}
3838
}

src/__tests__/CodexACPAgent/data/token-usage-session-update.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"sessionId": "test-session-id",
66
"update": {
77
"sessionUpdate": "usage_update",
8-
"used": 5000,
8+
"used": 2500,
99
"size": 128000
1010
}
1111
}

src/__tests__/CodexACPAgent/token-usage-events.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ describe('Token Usage Events', () => {
190190
};
191191
}
192192

193-
it('should emit usage_update with cumulative context usage', async () => {
193+
it('should emit usage_update with latest turn usage as a context proxy', async () => {
194194
const events = await setupPromptAndReturnEvents([
195195
createTokenUsageNotification(sessionId, {
196196
total: {
@@ -214,7 +214,7 @@ describe('Token Usage Events', () => {
214214
await expect(`${JSON.stringify(events[0], null, 2)}\n`).toMatchFileSnapshot('data/token-usage-session-update.json');
215215
});
216216

217-
it('should emit latest cumulative usage from multiple updates', async () => {
217+
it('should emit latest turn usage from multiple updates', async () => {
218218
const events = await setupPromptAndReturnEvents([
219219
createTokenUsageNotification(sessionId, {
220220
total: { totalTokens: 1000, inputTokens: 800, cachedInputTokens: 0, outputTokens: 200, reasoningOutputTokens: 0 },

0 commit comments

Comments
 (0)