mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-03 23:50:25 +00:00
fix: context overflow compaction and subagent announce improvements (#11664) (thanks @tyler6204)
* initial commit * feat: implement deriveSessionTotalTokens function and update usage tests * Added deriveSessionTotalTokens function to calculate total tokens based on usage and context tokens. * Updated usage tests to include cases for derived session total tokens. * Refactored session usage calculations in multiple files to utilize the new function for improved accuracy. * fix: restore overflow truncation fallback + changelog/test hardening (#11551) (thanks @tyler6204)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { hasNonzeroUsage, normalizeUsage } from "./usage.js";
|
||||
import { deriveSessionTotalTokens, hasNonzeroUsage, normalizeUsage } from "./usage.js";
|
||||
|
||||
describe("normalizeUsage", () => {
|
||||
it("normalizes Anthropic-style snake_case usage", () => {
|
||||
@@ -46,4 +46,32 @@ describe("normalizeUsage", () => {
|
||||
expect(hasNonzeroUsage({ input: 1 })).toBe(true);
|
||||
expect(hasNonzeroUsage({ total: 1 })).toBe(true);
|
||||
});
|
||||
|
||||
it("caps derived session total tokens to the context window", () => {
|
||||
expect(
|
||||
deriveSessionTotalTokens({
|
||||
usage: {
|
||||
input: 27,
|
||||
cacheRead: 2_400_000,
|
||||
cacheWrite: 0,
|
||||
total: 2_402_300,
|
||||
},
|
||||
contextTokens: 200_000,
|
||||
}),
|
||||
).toBe(200_000);
|
||||
});
|
||||
|
||||
it("uses prompt tokens when within context window", () => {
|
||||
expect(
|
||||
deriveSessionTotalTokens({
|
||||
usage: {
|
||||
input: 1_200,
|
||||
cacheRead: 300,
|
||||
cacheWrite: 50,
|
||||
total: 2_000,
|
||||
},
|
||||
contextTokens: 200_000,
|
||||
}),
|
||||
).toBe(1_550);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user