diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 8341668ae2..746914f777 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -389,6 +389,13 @@ For each request, the proxy resolves the settings by longest-URL-prefix match (t - **Status:** ACTIVE - **Description:** With `stripImages: true`, how many trailing messages keep their images verbatim. Ignored unless `stripImages` is enabled. +#### `visibilityMarkers` + +- **Type:** `boolean` +- **Default:** `true` +- **Status:** ACTIVE +- **Description:** Controls the 📦/❌ ACP visibility markers emitted after the proxy executes a proxy tool call (`compress` / `decompress` / `search_context` / `acp_status`). When on, each execution appends a marker line to the response stream and/or re-injects a marker message into that round's rebuilt history so the model can see what happened on later turns. Set `false` to suppress both artifacts — for deployments where models imitate or narrate around the markers (outputting their own confirmation lines or commentary; see issue #862). The tool executions themselves are unaffected: calls still run, and paired tool-call/tool-result messages are still recorded as usual; only the marker line/message is omitted. Same three-level merge as every other field. The #717 anti-forgery stripping of model-emitted fake markers is independent of this flag and stays active. + ### Injection toggles (global only) These two toggles are honoured only at the **global** level. Setting them inside a per-provider or per-model `compress` block has no effect. diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index 29ca04a910..904e5238b4 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -387,6 +387,13 @@ - **状态:** ACTIVE - **说明:** 在 `stripImages: true` 时,末尾多少条消息保留其图像逐字转发。仅在启用 `stripImages` 时生效。 +#### `visibilityMarkers` + +- **类型:** `boolean` +- **默认值:** `true` +- **状态:** ACTIVE +- **说明:** 控制代理执行 proxy 工具调用(`compress` / `decompress` / `search_context` / `acp_status`)后输出的 📦/❌ ACP 可见标记。开启时,每次执行会在响应流中追加一行标记,并/或把标记消息重新注入该轮重建后的历史,让模型在后续回合看到发生了什么。设为 `false` 可完全抑制这两类产物 —— 适用于模型会模仿或围绕标记进行旁白的部署场景(自行输出确认行或中文旁白;见 issue #862)。工具执行本身不受影响:调用照常执行、成对的 tool-call/tool-result 消息照常记录,只是省略标记行/标记消息。与其他字段相同的三级合并。#717 对模型伪造标记的防伪造剥离逻辑独立于本开关,始终生效。 + ### 注入开关(仅全局生效) 这两个开关只在**全局**层级生效。在按 provider 或按模型的 `compress` 块中设置它们无效。 diff --git a/src/compress-loop-responses.ts b/src/compress-loop-responses.ts index 0f66fc9f83..7295dc3961 100644 --- a/src/compress-loop-responses.ts +++ b/src/compress-loop-responses.ts @@ -63,6 +63,9 @@ interface CompressLoopResponsesCtx { /** Resolved upstream proxy URL (http://host:port) or undefined for direct. */ proxyUrl?: string; textProtocol?: boolean; + /** #862: when `false`, suppress 📦/❌ visibility markers (same contract as + * LoopCtx.visibilityMarkers). Default (undefined) keeps markers on. */ + visibilityMarkers?: boolean; } interface RequestOptions { @@ -160,7 +163,7 @@ function surfaceReadonlyJson( result = `\u274c [ACP] ${call.name} FAILED: ${String(e)}`; ctx.log(`[acp-proxy: responses JSON ${call.name} (read-only) FAILED: ${String(e)}]`); } - markers.push(buildVisibilityMarker(call.name, result)); + if (ctx.visibilityMarkers !== false) markers.push(buildVisibilityMarker(call.name, result)); } if (markers.length === 0) return current; const out = Array.isArray(current.output) ? [...(current.output as unknown[])] : []; @@ -212,7 +215,7 @@ export async function compressLoopResponsesJson( } const result = executeProxyTool(call.name, args, ctx); ctx.log(`[acp-proxy: responses JSON ${call.name} → ${result.slice(0, 120).replace(/\n/g, " ")}]`); - inputItems.push({ type: "message", role: "developer", content: buildVisibilityMarker(call.name, result) }); + if (ctx.visibilityMarkers !== false) inputItems.push({ type: "message", role: "developer", content: buildVisibilityMarker(call.name, result) }); } requestBody.input = hoistTrappedToolItems(inputItems as ToolPairItem[]); const result = await fetchWithRetry(requestOptions.url, { diff --git a/src/compress-settings.ts b/src/compress-settings.ts index 13c3d2078a..7a88f29aa6 100644 --- a/src/compress-settings.ts +++ b/src/compress-settings.ts @@ -71,6 +71,7 @@ export function mergeCompress( stripImages: pick("stripImages"), stripImagesKeepRecent: pick("stripImagesKeepRecent"), + visibilityMarkers: pick("visibilityMarkers"), // `reasoning` is a third nested-object field merged sub-field-wise // exactly like `absorb`/`prompts`: a model-level `threshold` must not // discard a provider-level `drop: false`. diff --git a/src/config.ts b/src/config.ts index 3236a86ad3..a9725c9e67 100644 --- a/src/config.ts +++ b/src/config.ts @@ -140,6 +140,12 @@ export type CompressSettings = { minCompressRange?: number; /** Enable multi-tier (T2/T3) distillation (kernel `tiers.enabled`). */ tiers?: boolean; + /** Emit 📦/❌ ACP visibility markers after proxy tool executions + * (compress / decompress / search_context / acp_status) — both the marker + * line streamed to the client and the marker message re-injected into + * rebuilt history. `false` suppresses them entirely, for deployments where + * models imitate or narrate around the markers (#862). Default `true`. */ + visibilityMarkers?: boolean; /** Override the kernel's compression prompt text (compressPhilosophy / * howToCompressRules / tier2DistillRules / tier3CondenseRules). All four * fields are LOAD-BEARING: the kernel rules were tuned in production and @@ -709,6 +715,10 @@ export function parseCompressSettings(v: unknown): (CompressSettings & { injectT if (typeof obj.stripImages !== "boolean") ok = false; else out.stripImages = obj.stripImages; } + if ("visibilityMarkers" in obj) { + if (typeof obj.visibilityMarkers !== "boolean") ok = false; + else out.visibilityMarkers = obj.visibilityMarkers; + } // Injection toggles are file-level fields (FileConfig.compress) honored by // loadOptions via `=== false`; the web UI shows them from the raw file // block, so they must round-trip here. Dropping them would silently diff --git a/src/loop/core.ts b/src/loop/core.ts index f5aa35db5a..0910df5878 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -92,6 +92,12 @@ export interface LoopCtx { // compute the true context total correctly (Anthropic reports // input_tokens as NEW-only; OpenAI/Responses report the TOTAL). protocol?: WireProtocol; + /** #862: when `false`, suppress the 📦/❌ ACP visibility markers emitted + * after proxy tool executions — both the marker line streamed to the + * client (`emitMarker`) and the orphan marker message re-injected into + * rebuilt history. Default (undefined) keeps markers on. Paired + * tool-call/tool-result messages are unaffected. */ + visibilityMarkers?: boolean; } export interface RequestOptions { @@ -569,7 +575,7 @@ export async function* runCompressLoop( } const result = executeProxyTool(call.name, parsedArgs, ctx, call.callId); proxyResults.push({ name: call.name, callId: call.callId, result, arguments: call.arguments }); - yield adapter.emitMarker(call.name, result); + if (ctx.visibilityMarkers !== false) yield adapter.emitMarker(call.name, result); } else { realToolCalls.push(call); realCalls += 1; @@ -643,7 +649,7 @@ export async function* runCompressLoop( toolCallId: pr.callId, text: pr.result, }); - } else { + } else if (ctx.visibilityMarkers !== false) { coreMessages.push({ id: `acp_loop_r${round}_marker_${pr.callId}`, role: "system", diff --git a/src/server.ts b/src/server.ts index f1ebcea582..c16577c247 100644 --- a/src/server.ts +++ b/src/server.ts @@ -3958,9 +3958,10 @@ async function forward( const records = current.filter((m) => typeof m.id === "string" && m.id.startsWith("acp_loop_")); return repairResponsesAssistantOrdering(stripKernelSummaries([...viewed, ...records] as BiliMessage[], turn.state), prepared.originalMessages); }; + const visibilityMarkers = resolveCompress(opts.routes, route?.rewrittenUrl, (parsedReq as { model?: string }).model, opts.compress).visibilityMarkers ?? true; const loop = runCompressLoop( streamToRead, - { core, config, messages: prepared.processedMessages.length > 0 ? prepared.processedMessages : prepared.originalMessages, compressMessages: prepared.originalMessages, session: prepared.session, log: ctx.log, proxyUrl, protocol: prepared.protocol, textProtocol, debug: opts.debug, refreshFolded }, + { core, config, messages: prepared.processedMessages.length > 0 ? prepared.processedMessages : prepared.originalMessages, compressMessages: prepared.originalMessages, session: prepared.session, log: ctx.log, proxyUrl, protocol: prepared.protocol, textProtocol, debug: opts.debug, refreshFolded, visibilityMarkers }, parsedReq, { url: upstreamUrl, headers: reqHeaders, wireTransform }, adapter, @@ -4013,9 +4014,10 @@ async function forward( if (prepared.protocol === "responses" && prepared.responsesTextProtocol) { const requestBody = JSON.parse(typeof body === "string" ? body : body.toString("utf8")) as Record; const requestHeaders = buildForwardHeaders(headers); + const visibilityMarkers = resolveCompress(opts.routes, route?.rewrittenUrl, (requestBody as { model?: string }).model, opts.compress).visibilityMarkers ?? true; json = await compressLoopResponsesJson( json, - { core, config, messages: prepared.originalMessages, session: prepared.session, log: ctx.log, proxyUrl, textProtocol: true }, + { core, config, messages: prepared.originalMessages, session: prepared.session, log: ctx.log, proxyUrl, textProtocol: true, visibilityMarkers }, requestBody, { url: upstreamUrl, headers: requestHeaders, wireTransform }, ); diff --git a/src/web/page.ts b/src/web/page.ts index 7e012049d1..67d5a5d0a4 100644 --- a/src/web/page.ts +++ b/src/web/page.ts @@ -29,5 +29,5 @@ export function renderPage(origin: string, version: string): string {

ZCode(编程套餐)

MITM
方式
MITM 透明代理
设置
Settings → Network → HTTP Proxy = ${origin}
CA 证书
${caPathEsc}${caReady ? "" : "(尚未生成,启动代理后自动创建)"}

CA 路径是本机 bili 实际生成的绝对路径,点"复制 CA 路径"后直接粘贴进 ZCode。⚠️ Windows 上 ZCode 不会展开 ~,请勿手填 ~/… 形式的路径。

上游网络

控制 bili 如何访问真实 Provider,不会改变客户端的本地路由地址。

全局代理

当前来源
直连
有效代理
direct
系统 PAC
—
状态
尚未测试

按 URL 覆盖

为特定 Provider 单独设置上游代理,覆盖全局设置。留空 = 继承全局。

加载中

会话

ACP 压缩状态与上游用量。

标题协议标识请求上下文窗口最后活动
-

高级设置

直接编辑 providers JSON(添加新 Provider、配置模型上下文窗口、按 URL 代理等);保存后立即热更新。

压缩参数(全局)

全局 compress 配置(modelContextLimit / maxContextLimit / emergencyThresholdPercent / nudgeGrowthTokens / preserveRecentMessages / preserveRecentTokens / minCompressRangeChars(单位:字符,旧名 minCompressRange 兼容) / stripImages / stripImagesKeepRecent(历史图像剥离,默认关) / tiers / injectTool / injectNudge)。保存后热更新,无需重启。置空 = 恢复默认。

`; +

高级设置

直接编辑 providers JSON(添加新 Provider、配置模型上下文窗口、按 URL 代理等);保存后立即热更新。

压缩参数(全局)

全局 compress 配置(modelContextLimit / maxContextLimit / emergencyThresholdPercent / nudgeGrowthTokens / preserveRecentMessages / preserveRecentTokens / minCompressRangeChars(单位:字符,旧名 minCompressRange 兼容) / stripImages / stripImagesKeepRecent(历史图像剥离,默认关) / visibilityMarkers(📦/❌ 可见标记,默认开) / tiers / injectTool / injectNudge)。保存后热更新,无需重启。置空 = 恢复默认。

`; } diff --git a/tests/visibility-markers.test.ts b/tests/visibility-markers.test.ts new file mode 100644 index 0000000000..95af3fac87 --- /dev/null +++ b/tests/visibility-markers.test.ts @@ -0,0 +1,157 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import type { Config, CoreMessage } from "acp-kernel"; +import { createCore, createInitialState, defaultConfig } from "acp-kernel"; +import type { Session } from "../src/session.ts"; +import { getSession } from "../src/session.ts"; +import { parseCompressSettings } from "../src/config.ts"; +import { mergeCompress } from "../src/compress-settings.ts"; +import { runCompressLoop, createResponsesAdapter } from "../src/loop/index.ts"; +import { buildCompressSystemPrompt } from "../src/compress-tool.ts"; +import { compressLoopResponsesJson } from "../src/compress-loop-responses.ts"; + +test("parseCompressSettings: visibilityMarkers boolean round-trips", () => { + const off = parseCompressSettings({ tiers: true, visibilityMarkers: false }); + assert.deepEqual(off, { tiers: true, visibilityMarkers: false }); + const on = parseCompressSettings({ visibilityMarkers: true }); + assert.deepEqual(on, { visibilityMarkers: true }); +}); + +test("parseCompressSettings: non-boolean visibilityMarkers poisons the block", () => { + assert.equal(parseCompressSettings({ visibilityMarkers: "false" }), undefined); + assert.equal(parseCompressSettings({ visibilityMarkers: 0 }), undefined); +}); + +test("mergeCompress: visibilityMarkers merges per-level, deepest defined wins", () => { + assert.equal(mergeCompress({ visibilityMarkers: false }, { visibilityMarkers: true }).visibilityMarkers, true); + assert.equal(mergeCompress({ visibilityMarkers: true }, { visibilityMarkers: false }).visibilityMarkers, false); + assert.equal(mergeCompress(undefined, undefined, { visibilityMarkers: false }).visibilityMarkers, false); + assert.equal(mergeCompress({ tiers: true }).visibilityMarkers, undefined); +}); + +function makeCtx(): { + core: ReturnType; + config: Config; + messages: CoreMessage[]; + session: Session; + log: (m: string) => void; +} { + return { + core: createCore(), + config: defaultConfig(200000), + messages: [], + session: { + id: "vm-marker-test", + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, contextTokens: 0 }, + metadata: {}, + state: createInitialState(), + createdAt: Date.now(), + lastSeen: Date.now(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + }, + log: () => {}, + }; +} + +function sse(type: string, data: unknown): string { + return `event: ${type}\ndata: ${JSON.stringify(data)}\n\n`; +} + +const FC_COMPRESS = [ + sse("response.output_item.added", { item: { type: "function_call", id: "fc_c", call_id: "call_c", name: "compress" }, output_index: 0 }), + sse("response.function_call_arguments.delta", { item_id: "fc_c", delta: JSON.stringify({ content: [{ startId: "m00001", endId: "m00002", summary: "s" }] }) }), + sse("response.output_item.done", { item: { type: "function_call", id: "fc_c", call_id: "call_c", name: "compress", arguments: "{}" }, output_index: 0 }), +].join(""); + +const COMPLETED = sse("response.completed", { response: { id: "resp_done", status: "completed", output: [] } }); + +async function drainRound(ctx: Parameters[1]): Promise<{ out: string; fetchCalls: number }> { + let n = 0; + const previousFetch = globalThis.fetch; + globalThis.fetch = (async () => { + n++; + return new Response(COMPLETED, { status: 200, headers: { "content-type": "text/event-stream" } }); + }) as typeof fetch; + try { + const chunks: Buffer[] = []; + const stream = new Response(FC_COMPRESS + COMPLETED, { status: 200 }).body!; + for await (const chunk of runCompressLoop(stream, ctx, { model: "gpt-4o", input: [], stream: true }, { url: "http://mock", headers: {} }, createResponsesAdapter(), buildCompressSystemPrompt())) { + chunks.push(chunk); + } + return { out: Buffer.concat(chunks).toString("utf8"), fetchCalls: n }; + } finally { + globalThis.fetch = previousFetch; + } +} + +test("loop: explicit visibilityMarkers=true keeps the marker (default parity)", async () => { + const { out, fetchCalls } = await drainRound({ ...makeCtx(), visibilityMarkers: true }); + assert.ok(out.includes("[ACP]"), "marker shown when explicitly enabled"); + assert.ok(fetchCalls >= 1, "re-request still fires"); +}); + +test("loop: visibilityMarkers=false suppresses the marker but executes + re-requests", async () => { + const { out, fetchCalls } = await drainRound({ ...makeCtx(), visibilityMarkers: false }); + assert.ok(!out.includes("[ACP]"), "no marker line reaches the client when disabled"); + assert.ok(!out.includes("\u{1F4E6}"), "no 📦 icon in stream when disabled"); + assert.ok(fetchCalls >= 1, "the tool still executed and the continuation re-request fired"); + assert.ok(/event: response\.completed/.test(out), "graceful completion"); +}); + +function jsonCtx(log: (m: string) => void, visibilityMarkers?: boolean): Parameters[1] { + return { + core: createCore(), + config: { modelContextLimit: 200000 } as Config, + messages: [] as CoreMessage[], + session: getSession("vm-json-test"), + log, + ...(visibilityMarkers !== undefined ? { visibilityMarkers } : {}), + }; +} + +test("json: read-only acp_status marker suppressed when disabled", async () => { + const previousFetch = globalThis.fetch; + globalThis.fetch = (async () => new Response(JSON.stringify({ id: "r", status: "completed", output: [] }), { status: 200 })) as typeof fetch; + try { + const initial = { + id: "resp_vm_ro", + status: "completed", + output: [{ type: "function_call", id: "fc_st", call_id: "call_st", name: "acp_status", arguments: "{}" }], + }; + const on = await compressLoopResponsesJson(structuredClone(initial), jsonCtx(() => {}), { model: "gpt-4o", input: [{ type: "message", role: "user", content: "status" }] }, { url: "https://unused.example/responses", headers: { "content-type": "application/json" } }); + assert.ok(JSON.stringify(on.output).includes("[ACP]"), "default: read-only marker surfaced"); + const off = await compressLoopResponsesJson(structuredClone(initial), jsonCtx(() => {}, false), { model: "gpt-4o", input: [{ type: "message", role: "user", content: "status" }] }, { url: "https://unused.example/responses", headers: { "content-type": "application/json" } }); + assert.ok(!JSON.stringify(off.output).includes("[ACP]"), "disabled: no read-only marker in output"); + } finally { + globalThis.fetch = previousFetch; + } +}); + +test("json: mutating compress re-request body carries the developer marker only when enabled", async () => { + const bodies: Record[] = []; + const previousFetch = globalThis.fetch; + globalThis.fetch = (async (_u: unknown, init?: RequestInit) => { + bodies.push(JSON.parse(String(init?.body)) as Record); + return new Response(JSON.stringify({ id: "r2", status: "completed", output: [] }), { status: 200 }); + }) as typeof fetch; + try { + const initial = { + id: "resp_vm_mut", + status: "completed", + output: [{ type: "function_call", id: "fc_c", call_id: "call_c", name: "compress", arguments: JSON.stringify({ content: [{ startId: "m00001", endId: "m00002", summary: "s" }] }) }], + }; + const request = { model: "gpt-4o", input: [{ type: "message", role: "user", content: "go" }] }; + await compressLoopResponsesJson(structuredClone(initial), jsonCtx(() => {}), structuredClone(request), { url: "https://unused.example/responses", headers: { "content-type": "application/json" } }); + assert.ok(bodies.length === 1, "mutating tool triggers exactly one re-request"); + assert.ok(JSON.stringify(bodies[0]).includes("[ACP]"), "default: developer marker rides the re-request"); + bodies.length = 0; + await compressLoopResponsesJson(structuredClone(initial), jsonCtx(() => {}, false), structuredClone(request), { url: "https://unused.example/responses", headers: { "content-type": "application/json" } }); + assert.ok(bodies.length === 1, "disabled: re-request still fires"); + assert.ok(!JSON.stringify(bodies[0]).includes("[ACP]"), "disabled: no developer marker in re-request body"); + } finally { + globalThis.fetch = previousFetch; + } +});