惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
Stack Overflow Blog
Stack Overflow Blog
人人都是产品经理
人人都是产品经理
Y
Y Combinator Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
D
DataBreaches.Net
GbyAI
GbyAI
Microsoft Security Blog
Microsoft Security Blog
博客园_首页
大猫的无限游戏
大猫的无限游戏
Jina AI
Jina AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Engineering at Meta
Engineering at Meta
IT之家
IT之家
MongoDB | Blog
MongoDB | Blog
The GitHub Blog
The GitHub Blog
月光博客
月光博客
U
Unit 42
Hugging Face - Blog
Hugging Face - Blog
博客园 - 叶小钗
腾讯CDC
B
Blog RSS Feed
博客园 - Franky
爱范儿
爱范儿

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
fix: use prompt snapshots for live context diagnostics · ...
steipete · 2026-04-26 · via Recent Commits to openclaw:main

@@ -10,6 +10,11 @@ import {

1010

import * as sessionTypesModule from "../../config/sessions.js";

1111

import type { SessionEntry } from "../../config/sessions.js";

1212

import { loadSessionStore, saveSessionStore } from "../../config/sessions.js";

13+

import {

14+

onInternalDiagnosticEvent,

15+

resetDiagnosticEventsForTest,

16+

type DiagnosticEventPayload,

17+

} from "../../infra/diagnostic-events.js";

1318

import {

1419

clearMemoryPluginState,

1520

registerMemoryFlushPlanResolver,

@@ -138,6 +143,7 @@ type RunWithModelFallbackParams = {

138143

};

139144140145

beforeEach(() => {

146+

resetDiagnosticEventsForTest();

141147

embeddedRunTesting.resetActiveEmbeddedRuns();

142148

replyRunRegistryTesting.resetReplyRunRegistry();

143149

runEmbeddedPiAgentMock.mockClear();

@@ -169,6 +175,7 @@ beforeEach(() => {

169175

});

170176171177

afterEach(() => {

178+

resetDiagnosticEventsForTest();

172179

vi.useRealTimers();

173180

clearMemoryPluginState();

174181

replyRunRegistryTesting.resetReplyRunRegistry();

@@ -289,6 +296,167 @@ describe("runReplyAgent auto-compaction token update", () => {

289296

// totalTokens should use lastCallUsage (55k), not accumulated (75k)

290297

expect(stored[sessionKey].totalTokens).toBe(55_000);

291298

});

299+300+

it("reports live diagnostic context from promptTokens, not provider usage totals", async () => {

301+

const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-usage-diagnostic-"));

302+

const storePath = path.join(tmp, "sessions.json");

303+

const sessionKey = "main";

304+

const sessionEntry = {

305+

sessionId: "session",

306+

updatedAt: Date.now(),

307+

totalTokens: 50_000,

308+

};

309+310+

await seedSessionStore({ storePath, sessionKey, entry: sessionEntry });

311+312+

runEmbeddedPiAgentMock.mockResolvedValue({

313+

payloads: [{ text: "ok" }],

314+

meta: {

315+

agentMeta: {

316+

usage: { input: 75_000, output: 5_000, cacheRead: 25_000, total: 105_000 },

317+

lastCallUsage: { input: 55_000, output: 2_000, cacheRead: 25_000, total: 82_000 },

318+

promptTokens: 44_000,

319+

},

320+

},

321+

});

322+323+

const diagnostics: DiagnosticEventPayload[] = [];

324+

const unsubscribe = onInternalDiagnosticEvent((event) => {

325+

diagnostics.push(event);

326+

});

327+

const { typing, sessionCtx, resolvedQueue, followupRun } = createBaseRun({

328+

storePath,

329+

sessionEntry,

330+

});

331+332+

try {

333+

await runReplyAgent({

334+

commandBody: "hello",

335+

followupRun,

336+

queueKey: "main",

337+

resolvedQueue,

338+

shouldSteer: false,

339+

shouldFollowup: false,

340+

isActive: false,

341+

isStreaming: false,

342+

typing,

343+

sessionCtx,

344+

sessionEntry,

345+

sessionStore: { [sessionKey]: sessionEntry },

346+

sessionKey,

347+

storePath,

348+

defaultModel: "anthropic/claude-opus-4-6",

349+

agentCfgContextTokens: 200_000,

350+

resolvedVerboseLevel: "off",

351+

isNewSession: false,

352+

blockStreamingEnabled: false,

353+

resolvedBlockStreamingBreak: "message_end",

354+

shouldInjectGroupIntro: false,

355+

typingMode: "instant",

356+

});

357+

} finally {

358+

unsubscribe();

359+

}

360+361+

const usageEvent = diagnostics.find((event) => event.type === "model.usage");

362+

expect(usageEvent).toMatchObject({

363+

type: "model.usage",

364+

usage: {

365+

input: 75_000,

366+

output: 5_000,

367+

cacheRead: 25_000,

368+

promptTokens: 100_000,

369+

total: 105_000,

370+

},

371+

context: {

372+

limit: 200_000,

373+

used: 44_000,

374+

},

375+

});

376+

});

377+378+

it("falls back to last-call prompt usage for live diagnostic context", async () => {

379+

const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-usage-diagnostic-last-"));

380+

const storePath = path.join(tmp, "sessions.json");

381+

const sessionKey = "main";

382+

const sessionEntry = {

383+

sessionId: "session",

384+

updatedAt: Date.now(),

385+

totalTokens: 50_000,

386+

};

387+388+

await seedSessionStore({ storePath, sessionKey, entry: sessionEntry });

389+390+

runEmbeddedPiAgentMock.mockResolvedValue({

391+

payloads: [{ text: "ok" }],

392+

meta: {

393+

agentMeta: {

394+

usage: { input: 75_000, output: 5_000, cacheRead: 25_000, total: 105_000 },

395+

lastCallUsage: {

396+

input: 55_000,

397+

output: 2_000,

398+

cacheRead: 25_000,

399+

cacheWrite: 1_000,

400+

total: 83_000,

401+

},

402+

},

403+

},

404+

});

405+406+

const diagnostics: DiagnosticEventPayload[] = [];

407+

const unsubscribe = onInternalDiagnosticEvent((event) => {

408+

diagnostics.push(event);

409+

});

410+

const { typing, sessionCtx, resolvedQueue, followupRun } = createBaseRun({

411+

storePath,

412+

sessionEntry,

413+

});

414+415+

try {

416+

await runReplyAgent({

417+

commandBody: "hello",

418+

followupRun,

419+

queueKey: "main",

420+

resolvedQueue,

421+

shouldSteer: false,

422+

shouldFollowup: false,

423+

isActive: false,

424+

isStreaming: false,

425+

typing,

426+

sessionCtx,

427+

sessionEntry,

428+

sessionStore: { [sessionKey]: sessionEntry },

429+

sessionKey,

430+

storePath,

431+

defaultModel: "anthropic/claude-opus-4-6",

432+

agentCfgContextTokens: 200_000,

433+

resolvedVerboseLevel: "off",

434+

isNewSession: false,

435+

blockStreamingEnabled: false,

436+

resolvedBlockStreamingBreak: "message_end",

437+

shouldInjectGroupIntro: false,

438+

typingMode: "instant",

439+

});

440+

} finally {

441+

unsubscribe();

442+

}

443+444+

const usageEvent = diagnostics.find((event) => event.type === "model.usage");

445+

expect(usageEvent).toMatchObject({

446+

type: "model.usage",

447+

usage: {

448+

input: 75_000,

449+

output: 5_000,

450+

cacheRead: 25_000,

451+

promptTokens: 100_000,

452+

total: 105_000,

453+

},

454+

context: {

455+

limit: 200_000,

456+

used: 81_000,

457+

},

458+

});

459+

});

292460

});

293461294462

describe("runReplyAgent block streaming", () => {

@@ -913,6 +1081,7 @@ describe("runReplyAgent Active Memory inline debug", () => {

9131081

model: "claude",

9141082

usage: { input: 1200, output: 45, cacheRead: 800, cacheWrite: 200, total: 2245 },

9151083

lastCallUsage: { input: 1000, output: 45, cacheRead: 750, cacheWrite: 150, total: 1945 },

1084+

promptTokens: 1250,

9161085

compactionCount: 1,

9171086

},

9181087

},

@@ -987,6 +1156,7 @@ describe("runReplyAgent Active Memory inline debug", () => {

9871156

expect(traceText).toContain("🔎 Usage (Session Total):");

9881157

expect(traceText).toContain("🔎 Usage (Last Turn Total):");

9891158

expect(traceText).toContain("🔎 Context Window (Last Model Request):");

1159+

expect(traceText).toContain("used=1,250 tok (1.3k)");

9901160

expect(traceText).toContain("🔎 Execution Result:");

9911161

expect(traceText).toContain("winner=anthropic/claude");

9921162

expect(traceText).toContain("fallbackUsed=yes");

@@ -1025,7 +1195,7 @@ describe("runReplyAgent Active Memory inline debug", () => {

10251195

expect(traceText).toContain("🔎 Model Input (User Role):");

10261196

expect(traceText).toContain("🔎 Model Output (Assistant Role):");

10271197

expect(traceText).toContain(

1028-

"Summary: winner=claude 🧠 low fallback=yes attempts=2 stop=end_turn prompt=1.9k/200k ⬇️ 1.2k ⬆️ 45 ♻️ 800 🆕 200 🔢 2.2k tools=2 compactions=1",

1198+

"Summary: winner=claude 🧠 low fallback=yes attempts=2 stop=end_turn prompt=1.3k/200k ⬇️ 1.2k ⬆️ 45 ♻️ 800 🆕 200 🔢 2.2k tools=2 compactions=1",

10291199

);

10301200

expect(traceText.indexOf("🔎 Execution Result:")).toBeGreaterThan(

10311201

traceText.indexOf("🔎 Context Window (Last Model Request):"),