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

推荐订阅源

月光博客
月光博客
Stack Overflow Blog
Stack Overflow Blog
L
LangChain Blog
Jina AI
Jina AI
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
雷峰网
雷峰网
T
Tailwind CSS Blog
MongoDB | Blog
MongoDB | Blog
博客园 - 【当耐特】
博客园 - 聂微东
V
Visual Studio Blog
博客园_首页
Engineering at Meta
Engineering at Meta
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The Cloudflare Blog
人人都是产品经理
人人都是产品经理
Apple Machine Learning Research
Apple Machine Learning Research
阮一峰的网络日志
阮一峰的网络日志
Microsoft Security Blog
Microsoft Security Blog
GbyAI
GbyAI
F
Fortinet All Blogs
C
Check Point Blog
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More

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(compaction): count user-message image blocks in cut-p...
yetval · 2026-06-22 · via Recent Commits to openclaw:main
1+

import { describe, expect, it } from "vitest";

2+

import type { ImageContent } from "../../llm.js";

3+

import type { AgentMessage } from "../../types.js";

4+

import type { SessionTreeEntry } from "../types.js";

5+

import { estimateTokens, findCutPoint } from "./compaction.js";

6+7+

const IMAGE_PAYLOAD = "a".repeat(1_500_000);

8+9+

function imageBlock(): ImageContent {

10+

return { type: "image", data: IMAGE_PAYLOAD, mimeType: "image/png" };

11+

}

12+13+

function userImage(timestamp: number): AgentMessage {

14+

return { role: "user", content: [imageBlock()], timestamp };

15+

}

16+17+

function userText(text: string, timestamp: number): AgentMessage {

18+

return { role: "user", content: [{ type: "text", text }], timestamp };

19+

}

20+21+

function toolResultImage(timestamp: number): AgentMessage {

22+

return {

23+

role: "toolResult",

24+

toolCallId: "call-1",

25+

toolName: "screenshot",

26+

content: [imageBlock()],

27+

isError: false,

28+

timestamp,

29+

};

30+

}

31+32+

function assistantText(text: string, timestamp: number): AgentMessage {

33+

return {

34+

role: "assistant",

35+

content: [{ type: "text", text }],

36+

api: "anthropic-messages",

37+

provider: "anthropic",

38+

model: "claude-fable-5",

39+

usage: {

40+

input: 0,

41+

output: 0,

42+

cacheRead: 0,

43+

cacheWrite: 0,

44+

totalTokens: 0,

45+

cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },

46+

},

47+

stopReason: "stop",

48+

timestamp,

49+

};

50+

}

51+52+

function messageEntry(message: AgentMessage, index: number): SessionTreeEntry {

53+

return {

54+

type: "message",

55+

id: `entry-${index}`,

56+

parentId: index === 0 ? null : `entry-${index - 1}`,

57+

timestamp: new Date(message.timestamp).toISOString(),

58+

message,

59+

};

60+

}

61+62+

function buildTranscript(recentUserTurns: AgentMessage[]): SessionTreeEntry[] {

63+

const messages: AgentMessage[] = [userText("start of the conversation", 1)];

64+

let timestamp = 2;

65+

for (const turn of recentUserTurns) {

66+

messages.push(assistantText("ok", timestamp++));

67+

messages.push(turn);

68+

}

69+

return messages.map((message, index) => messageEntry(message, index));

70+

}

71+72+

describe("estimateTokens image accounting", () => {

73+

it("charges a user-message image block the same as a tool-result image block", () => {

74+

const userTokens = estimateTokens(userImage(1));

75+

const toolTokens = estimateTokens(toolResultImage(1));

76+77+

expect(userTokens).toBe(toolTokens);

78+

expect(userTokens).toBeGreaterThanOrEqual(1200);

79+

});

80+

});

81+82+

describe("findCutPoint with image-heavy recent turns", () => {

83+

it("trims image-dominated user turns instead of keeping the whole transcript", () => {

84+

const entries = buildTranscript([userImage(10), userImage(20), userImage(30)]);

85+86+

const result = findCutPoint(entries, 0, entries.length, 1500);

87+88+

expect(result.firstKeptEntryIndex).toBeGreaterThan(0);

89+

});

90+91+

it("matches the cut point of an equivalent text-cost control", () => {

92+

const equivalentText = "x".repeat(4800);

93+

const imageEntries = buildTranscript([userImage(10), userImage(20), userImage(30)]);

94+

const textEntries = buildTranscript([

95+

userText(equivalentText, 10),

96+

userText(equivalentText, 20),

97+

userText(equivalentText, 30),

98+

]);

99+100+

const imageResult = findCutPoint(imageEntries, 0, imageEntries.length, 1500);

101+

const textResult = findCutPoint(textEntries, 0, textEntries.length, 1500);

102+103+

expect(textResult.firstKeptEntryIndex).toBeGreaterThan(0);

104+

expect(imageResult.firstKeptEntryIndex).toBe(textResult.firstKeptEntryIndex);

105+

});

106+

});