diff --git a/src/compress-loop-responses.ts b/src/compress-loop-responses.ts index b5ba621446..e97b6f8c13 100644 --- a/src/compress-loop-responses.ts +++ b/src/compress-loop-responses.ts @@ -1,6 +1,6 @@ import { buildStatusReport, - estimateTokensFast, + defaultCountTokens, type CompressionCore, type Config, type CoreMessage, @@ -102,7 +102,7 @@ function executeProxyTool( return `Found ${blocks.length} block(s) for "${query}":\n\n${lines.join("\n\n")}`; } if (toolName === "acp_status") { - return buildStatusReport(ctx.session.state, ctx.messages, estimateTokensFast); + return buildStatusReport(ctx.session.state, ctx.messages, defaultCountTokens); } return `[Unknown proxy tool: ${toolName}]`; } diff --git a/src/loop/core.ts b/src/loop/core.ts index 1c3178c150..2815fbb670 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -1,6 +1,6 @@ import { buildStatusReport, - estimateTokensFast, + defaultCountTokens, formatRanges, hideConsumedCompressCalls, type CompressionCore, @@ -145,7 +145,7 @@ function handleAcpStatus(args: Record, ctx: LoopCtx): string { const tool = typeof args.tool === "string" ? args.tool : undefined; const sort = typeof args.sort === "string" ? (args.sort as "size" | "time" | "tool" | "age") : undefined; const limit = typeof args.limit === "number" ? args.limit : undefined; - const base = buildStatusReport(ctx.session.state, ctx.messages, estimateTokensFast, { scope, view, tool, sort, limit }); + const base = buildStatusReport(ctx.session.state, ctx.messages, defaultCountTokens, { scope, view, tool, sort, limit }); if (scope) return base; const nudge = ctx.nudge; const ranges = nudge?.compressibleRanges ?? []; diff --git a/src/server.ts b/src/server.ts index 8457c1351b..497b25d359 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,7 +1,7 @@ import http from "node:http"; import fs from "node:fs"; import { randomUUID } from "node:crypto"; -import { createCore, type CompressionCore, type CompressionState, type Config, type CoreMessage, type NudgeDecision, type Prompts, defaultPrompts, defaultCountTokens, estimateTokensFast, renderNudgeText, deactivateBlock, viableRanges } from "acp-kernel"; +import { createCore, type CompressionCore, type CompressionState, type Config, type CoreMessage, type NudgeDecision, type Prompts, defaultPrompts, defaultCountTokens, renderNudgeText, deactivateBlock, viableRanges } from "acp-kernel"; import { resolveCompress, resolveCompressPrompts, resolveRequestConfig } from "./compress-settings.js"; import type { ProxyOptions } from "./config.js"; import { loadOptions, loadRoutes } from "./config.js"; diff --git a/src/stream.ts b/src/stream.ts index 00a430d201..bee3e4a4a7 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -1,4 +1,4 @@ -import { buildStatusReport, collectBlockContent, estimateTokensFast, type CompressionCore, type Config, type CoreMessage, type CompressionState } from "acp-kernel"; +import { buildStatusReport, collectBlockContent, defaultCountTokens, type CompressionCore, type Config, type CoreMessage, type CompressionState } from "acp-kernel"; import { type Session, cacheBlockContent } from "./session.js"; import { COMPRESS_TOOL_NAME, parseCompressInput, PROXY_TOOL_NAMES } from "./compress-tool.js"; import { resolveDecompress } from "./decompress-shared.js"; @@ -43,7 +43,7 @@ function executeAnthropicProxyTool(toolName: string, args: Record { + // 4000 CJK chars = 4000 tokens on defaultCountTokens (the nudge's scale), + // 1000 on estimateTokensFast β€” the old acp_status scale (issue #390). + const messages: CoreMessage[] = [ + { id: "m1", role: "user", contentType: "text", text: "δΈ­".repeat(4_000) }, + ]; + const ctx = makeCtx(messages, "responses"); + ctx.session.state.messageRefs = assignRefs(messages, { + existing: ctx.session.state.messageRefs, + nextIndex: 1, + }).map; + const round1 = [ + sse("response.created", { response: { id: "resp_1", status: "in_progress" } }), + fcEvents(0, "call_status", "acp_status", "{}"), + COMPLETED, + ].join(""); + let captured = ""; + const orig = globalThis.fetch; + globalThis.fetch = (async (_url: unknown, init?: RequestInit) => { + captured = typeof init?.body === "string" ? init.body : ""; + return new Response(COMPLETED, { status: 200 }); + }) as typeof fetch; + try { + await drain( + new Response(round1, { status: 200 }).body!, + ctx, + { model: "gpt-4o", input: [], stream: true }, + { url: "http://mock", headers: {} }, + ); + assert.ok(captured, "re-request after acp_status carries the report"); + assert.match(captured, /4\.0K text \(100%\)/, "CJK counted 1:1, not chars/4"); + assert.doesNotMatch(captured, /1\.0K text \(100%\)/, "chars/4 scale must not appear"); + } finally { + globalThis.fetch = orig; + } +}); + test("loop #2: search_context-only round β†’ marker surfaced + re-request", async () => { const round1 = [ sse("response.created", { response: { id: "resp_1", status: "in_progress" } }),