From 2bf71704cc8f830227ecaf865d0f20ddd5cf846e Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sun, 20 Sep 2026 16:50:20 +0800 Subject: [PATCH] feat: make the marker silence clause conditional on visibilityMarkers Fixes #913 --- CHANGELOG.md | 2 ++ CONFIGURATION.md | 2 +- src/compress-tool.ts | 16 +++++++++++----- src/server.ts | 36 +++++++++++++++++++++--------------- tests/marker-echo.test.ts | 13 +++++++++++++ 5 files changed, 48 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b23b262db0..b4fbed3102 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag ## [Unreleased] +- **Marker-integrity silence clause now follows `visibilityMarkers` (#913)**: `MARKER_INTEGRITY_NOTE` was one unconditional constant carrying two rules — the #717 anti-forgery segment (never write a marker-style line as your own text) and the #862 silence clause (execute compress-class calls silently, no preamble before / no summary after). With `compress.visibilityMarkers: false` the proxy stops emitting markers entirely, yet the silence clause kept riding every nudge and the injected compress prompt — an instruction about artifacts that deployment never shows. The constant is split (`MARKER_INTEGRITY_NOTE` base + `MARKER_SILENCE_CLAUSE`), and `withMarkerIntegrityNote(text, visibilityMarkers = true)` appends the clause only while markers are visible. The flag is resolved once per request at the prepare dispatch (`resolveCompress(...).visibilityMarkers ?? true`) and threaded through `prepareAnthropic` / `prepareOpenai` / `prepareGoogle` / `prepareResponses` / `injectSystem` plus the compress-loop systemPrompt build, so first request and loop re-requests agree (per-config byte-stable anchor preserved; default output byte-identical to before). The #717 anti-forgery segment stays unconditional in every configuration. Tests pin: clause dropped at `false`, base intact, pure-suffix byte-stability, default-arg legacy equality. + - **Failed-compress receipts now name the live compressible span (#1026, slimmed port of PR #1008 by asuan-dev)**: a model whose compress call fails kept anchoring on refs an earlier fold already consumed — it re-issued the same dead range, burned retries, and eventually gave up and let the nudge re-fire every turn (observed live 2026-09-18: three consecutive failed compresses, two full T1 checkpoints wasted). Both failure receipts (`no valid ranges parsed` and `blocksCreated === 0`) now append `compressibleSpanHint(state)` — the raw-ref span NOT covered by an ACTIVE block right now (`mXXXXX–mYYYYY`, computed as highest mapped ref minus the newest active-block boundary) — so one retry lands on a valid range without another acp_status round-trip. Boundary counts ACTIVE blocks only: after a decompress, the restored span shows as compressible again, which is exactly the recoverable truth. When no raw refs are compressible (everything folded), the receipt names the active blocks' real ids (`startId b7, endId b11`) and suggests folding them into a higher-tier block — a kernel-first-class operation (b-ranges expand to their `effectiveMessageIds` and merge tiers). Coordinates with the #849 repeat-failure guard: the guard forbids repeating the same dead ranges, the hint hands over the live ones. Port keeps only PR #1008's head commit (e4a4700); its bundled #1003/#1004/#1006 content stays out (superseded by #740 / closed in the PR-queue triage). Tests pin: span past the highest ACTIVE block, inactive blocks not holding the boundary (decompress releases it), exhausted fallback naming real active block ids, and both receipt paths carrying the span. - **Log upstream 2xx null-body responses instead of replying empty silently (#821, PR #823)**: when an upstream answers 2xx with a null body (e.g. a gateway replying 204/304, or some proxy implementations collapsing `Content-Length: 0` into no body at all), the forward path replied empty to the client with no log line — zero SSE chunks and nothing server-side to debug from, so hosts report "empty stream" errors with no trace. A 2xx with a non-null *empty* stream (0 chunks read) already flows through the logged #413 truncation path; only the null-body case fell through to a bare `res.end()`. A warn now names the session and upstream status before responding empty. No behavior change. diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 5015f19c8a..f57fffe411 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -406,7 +406,7 @@ For each request, the proxy resolves the settings by longest-URL-prefix match (t - **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. +- **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. Since #913, `false` also drops the #862 silence clause from the marker-integrity note appended to nudges and the injected compress prompt — an invisible deployment has no marker for the model to imitate or narrate around, so the clause is pure noise there. The #717 anti-forgery segment stays unconditional in every configuration. 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) diff --git a/src/compress-tool.ts b/src/compress-tool.ts index ff16eee872..535b8003ad 100644 --- a/src/compress-tool.ts +++ b/src/compress-tool.ts @@ -188,13 +188,19 @@ export function withStagedCompressGuidance(text: string): string { // confirmation lines plus preambles/summaries (incl. non-English commentary) // before and after each real compression. const MARKER_INTEGRITY_NOTE = - "\n\n[ACP marker integrity: lines shaped like '📦 [ACP] Compressed …' or '❌ [ACP] … FAILED' are CONFIRMATION MARKERS emitted by the bili proxy itself, right after it executes a compress/decompress/search_context/acp_status call. They are not something you write. NEVER emit such a line as your own text — writing one fakes a state change that did not happen, and the proxy strips it. To compress, call the compress tool. To verify a compression landed, call acp_status and confirm the block count increased — a confirmation line you wrote yourself proves nothing. Execute these calls silently: no announcement or preamble before the call, and no completion summary, status line, or marker-style line after it — when the tool returns, continue the task directly as if the call had not happened.]"; + "\n\n[ACP marker integrity: lines shaped like '📦 [ACP] Compressed …' or '❌ [ACP] … FAILED' are CONFIRMATION MARKERS emitted by the bili proxy itself, right after it executes a compress/decompress/search_context/acp_status call. They are not something you write. NEVER emit such a line as your own text — writing one fakes a state change that did not happen, and the proxy strips it. To compress, call the compress tool. To verify a compression landed, call acp_status and confirm the block count increased — a confirmation line you wrote yourself proves nothing."; +const MARKER_SILENCE_CLAUSE = + " Execute these calls silently: no announcement or preamble before the call, and no completion summary, status line, or marker-style line after it — when the tool returns, continue the task directly as if the call had not happened.]"; /** Append the marker-integrity rule to a nudge or system-prompt text. - * Unconditional (unlike withStagedCompressGuidance): the rule must hold in - * every configuration where a marker can be seen in history. */ -export function withMarkerIntegrityNote(text: string): string { - return text + MARKER_INTEGRITY_NOTE; + * The anti-forgery segment is unconditional (unlike withStagedCompressGuidance): + * the rule must hold in every configuration where a marker can be seen in + * history. The #862 silence clause rides along only while markers are + * visible (#913): with compress.visibilityMarkers=false the client never + * sees a marker, so there is nothing to imitate or narrate around, and the + * clause is dropped. */ +export function withMarkerIntegrityNote(text: string, visibilityMarkers = true): string { + return text + MARKER_INTEGRITY_NOTE + (visibilityMarkers ? MARKER_SILENCE_CLAUSE : ""); } // #760: per-call conversation_id for MCP tools. Hosts that share ONE MCP shim diff --git a/src/server.ts b/src/server.ts index 6413f972f8..70c0421f8a 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1653,6 +1653,7 @@ async function handle( async (): Promise<{ body: string | Buffer; prepared: Prepared | null } | null> => { const runPrepare = (): Prepared => { const cs = resolveCompress(opts.routes, route?.rewrittenUrl, requestModel, opts.compress); + const visibilityMarkers = cs.visibilityMarkers ?? true; const reasoningCfg = cs.reasoning; const keepRecent = cs.stripImagesKeepRecent ?? DEFAULT_STRIP_IMAGES_KEEP_RECENT; const stripped = cs.stripImages @@ -1671,19 +1672,19 @@ async function handle( // Both the model and the stream flag live in the URL path // for this wire (the body carries neither), so they are // derived here instead of read off `work`. - return prepareGoogle(work as GoogleRequestBody, opts, core, reqConfig, reqPrompts, reqSurface, log, session, pluginMode, nativeWindow, googleModel, googlePathKind(urlPath) === "stream-generate"); + return prepareGoogle(work as GoogleRequestBody, opts, core, reqConfig, reqPrompts, reqSurface, log, session, pluginMode, nativeWindow, googleModel, googlePathKind(urlPath) === "stream-generate", visibilityMarkers); } return protocol === "anthropic" - ? prepareAnthropic(work as AnthropicRequestBody, req, opts, core, reqConfig, reqPrompts, reqSurface, log, session, pluginMode, upstreamOrigin, reasoningCfg) + ? prepareAnthropic(work as AnthropicRequestBody, req, opts, core, reqConfig, reqPrompts, reqSurface, log, session, pluginMode, upstreamOrigin, reasoningCfg, visibilityMarkers) : protocol === "openai" - ? prepareOpenai(work as OpenAIRequestBody, req, opts, core, reqConfig, reqPrompts, reqSurface, log, session, pluginMode, upstreamOrigin, nativeWindow, reasoningCfg, route?.rewrittenUrl) + ? prepareOpenai(work as OpenAIRequestBody, req, opts, core, reqConfig, reqPrompts, reqSurface, log, session, pluginMode, upstreamOrigin, nativeWindow, reasoningCfg, visibilityMarkers, route?.rewrittenUrl) : responsesCompact // #618 review nit: when no bili compaction item is present, // prepareResponsesCompact falls back to the raw bodyBuffer — forward // the re-serialized post-strip work instead so dropped images don't // ride along. Unchanged bodies keep the original buffer byte-identical. ? prepareResponsesCompact(stripped.removed > 0 ? Buffer.from(JSON.stringify(work)) : bodyBuffer, work as ResponsesRequestBody, session, req, core, reqConfig, log) - : prepareResponses(work as ResponsesRequestBody, req, opts, core, reqConfig, reqPrompts, reqSurface, log, session, responsesIdentity!, pluginMode, upstreamOrigin, nativeWindow, reasoningCfg, route?.rewrittenUrl); + : prepareResponses(work as ResponsesRequestBody, req, opts, core, reqConfig, reqPrompts, reqSurface, log, session, responsesIdentity!, pluginMode, upstreamOrigin, nativeWindow, reasoningCfg, visibilityMarkers, route?.rewrittenUrl); }; // #332: codex's native remote-compaction request (trigger form) // is dispatched BEFORE prepare/preflight. When it is not @@ -2068,6 +2069,7 @@ function prepareAnthropic( pluginMode: boolean, upstreamOrigin: string, reasoning: CompressReasoningConfig | undefined, + visibilityMarkers: boolean, ): Prepared { const sessionId = session.id; const stream = parsed.stream === true; @@ -2166,7 +2168,7 @@ function prepareAnthropic( reapOrphanBlocks(session, msgs, deactivateBlock); rebuiltMessages = coreToAnthropic(processedMessages as BiliMessage[], cacheControls); - systemOut = injectSystem(parsed, opts, prompts, loopConfig, ensureCanonicalId(session), surface); + systemOut = injectSystem(parsed, opts, prompts, loopConfig, ensureCanonicalId(session), surface, visibilityMarkers); if (injectTools) { toolsOut = injectTool(parsed.tools, [...(absorbActive ? [ABSORB_TOOL] : []), ...(rulesActive ? [RULE_TOOL] : [])], surface?.toolPrompts); } @@ -2181,7 +2183,7 @@ function prepareAnthropic( try { const rendered = renderNudgeText(turn.nudge, prompts, surface?.nudgeSections); if (rendered.text) { - rebuiltMessages = [...rebuiltMessages, { role: "user", content: withMarkerIntegrityNote(withSummaryBudgetNote(withStagedCompressGuidance(rendered.text))) }]; + rebuiltMessages = [...rebuiltMessages, { role: "user", content: withMarkerIntegrityNote(withSummaryBudgetNote(withStagedCompressGuidance(rendered.text)), visibilityMarkers) }]; } } catch { } @@ -2230,6 +2232,7 @@ function prepareOpenai( upstreamOrigin: string, nativeWindow: number, reasoning: CompressReasoningConfig | undefined, + visibilityMarkers: boolean, billingUpstream?: string, ): Prepared { const sessionId = session.id; @@ -2330,7 +2333,7 @@ function prepareOpenai( // would invalidate the cache every turn. const sysParts: string[] = []; if (systemText) sysParts.push(systemText); - if (shouldInject) sysParts.push(withConversationIdNote(withMarkerIntegrityNote(withSummaryBudgetNote(buildCompressSystemPrompt(prompts, surface?.promptSections))), ensureCanonicalId(session))); + if (shouldInject) sysParts.push(withConversationIdNote(withMarkerIntegrityNote(withSummaryBudgetNote(buildCompressSystemPrompt(prompts, surface?.promptSections)), visibilityMarkers), ensureCanonicalId(session))); if (absorbActive) sysParts.push(buildAbsorbSystemPrompt(absorbToolName(config))); rebuiltMessages = injectOpenaiSystem(rebuiltMessages, sysParts); // #532: capture what bili injects outside the fold space (client system @@ -2351,7 +2354,7 @@ function prepareOpenai( try { const rendered = renderNudgeText(turn.nudge, prompts, surface?.nudgeSections); if (rendered.text) { - rebuiltMessages = [...rebuiltMessages, { role: "user", content: withMarkerIntegrityNote(withSummaryBudgetNote(withStagedCompressGuidance(rendered.text))) }]; + rebuiltMessages = [...rebuiltMessages, { role: "user", content: withMarkerIntegrityNote(withSummaryBudgetNote(withStagedCompressGuidance(rendered.text)), visibilityMarkers) }]; } } catch { } @@ -2443,6 +2446,7 @@ function prepareGoogle( nativeWindow: number, model: string | undefined, stream: boolean, + visibilityMarkers: boolean, ): Prepared { const sessionId = session.id; ++session.stats.requests; @@ -2506,7 +2510,7 @@ function prepareGoogle( // trailing user content below (see prepareOpenai for the rationale). const sysParts: string[] = []; if (systemText) sysParts.push(systemText); - if (shouldInject) sysParts.push(withMarkerIntegrityNote(buildCompressSystemPrompt(prompts, surface?.promptSections))); + if (shouldInject) sysParts.push(withMarkerIntegrityNote(buildCompressSystemPrompt(prompts, surface?.promptSections), visibilityMarkers)); if (absorbActive) sysParts.push(buildAbsorbSystemPrompt(absorbToolName(config))); googleOutboundSystem = sysParts.join("\n\n"); // Untouched when nothing was added beyond the client's own text: the @@ -2521,7 +2525,7 @@ function prepareGoogle( try { const rendered = renderNudgeText(turn.nudge, prompts, surface?.nudgeSections); if (rendered.text) { - rebuiltContents = appendGoogleNudge(rebuiltContents, withMarkerIntegrityNote(withStagedCompressGuidance(rendered.text))); + rebuiltContents = appendGoogleNudge(rebuiltContents, withMarkerIntegrityNote(withStagedCompressGuidance(rendered.text), visibilityMarkers)); } } catch { } @@ -2598,6 +2602,7 @@ function prepareResponses( upstreamOrigin: string, nativeWindow: number, reasoning: CompressReasoningConfig | undefined, + visibilityMarkers: boolean, billingUpstream?: string, ): Prepared { const sessionId = session.id; @@ -2721,7 +2726,7 @@ function prepareResponses( ? [] : (session.metadata.codexForgedSummaries as string[] | undefined) ?? []; if (shouldInject && !isCompactionTrigger && !process.env.ACP_NO_COMPRESS_PROMPT) { - const prompt = withConversationIdNote(withMarkerIntegrityNote(withSummaryBudgetNote(responsesTextProtocol ? buildCompressHybridSystemPrompt(prompts, surface?.promptSections) : buildCompressSystemPrompt(prompts, surface?.promptSections))), ensureCanonicalId(session)); + const prompt = withConversationIdNote(withMarkerIntegrityNote(withSummaryBudgetNote(responsesTextProtocol ? buildCompressHybridSystemPrompt(prompts, surface?.promptSections) : buildCompressSystemPrompt(prompts, surface?.promptSections)), visibilityMarkers), ensureCanonicalId(session)); const devParts = [...projection.systemParts, ...forgedSummaries, prompt]; if (absorbActive) devParts.push(buildAbsorbSystemPrompt(absorbToolName(config))); const devContent = devParts.join("\n\n---\n\n"); @@ -2752,7 +2757,7 @@ function prepareResponses( const inputItems: ResponseInputItem[] = typeof rebuiltInput === "string" ? [{ type: "message", role: "user", content: rebuiltInput }] : rebuiltInput; - inputItems.push({ type: "message", role: "user", content: withMarkerIntegrityNote(withSummaryBudgetNote(withStagedCompressGuidance(rendered.text))) }); + inputItems.push({ type: "message", role: "user", content: withMarkerIntegrityNote(withSummaryBudgetNote(withStagedCompressGuidance(rendered.text)), visibilityMarkers) }); rebuiltInput = inputItems; } } catch { @@ -3062,6 +3067,7 @@ function injectSystem( config: Config, noteId: string, surface?: PackSurface, + visibilityMarkers = true, ): string | AnthropicRequestBody["system"] { // ONLY the static compress prompt goes into the system block — it is the // prefix-cache anchor and must stay byte-stable across turns. The nudge @@ -3069,7 +3075,7 @@ function injectSystem( // the caller (prepareAnthropic), never merged into system. const baseText = extractSystem(parsed.system); const parts: string[] = []; - if (opts.compress.injectTool) parts.push(withConversationIdNote(withMarkerIntegrityNote(withSummaryBudgetNote(buildCompressSystemPrompt(prompts, surface?.promptSections))), noteId)); + if (opts.compress.injectTool) parts.push(withConversationIdNote(withMarkerIntegrityNote(withSummaryBudgetNote(buildCompressSystemPrompt(prompts, surface?.promptSections)), visibilityMarkers), noteId)); if (opts.compress.injectTool && absorbEnabled(config)) parts.push(buildAbsorbSystemPrompt(absorbToolName(config))); if (parts.length === 0) return parsed.system; const full = baseText ? `${baseText}\n\n---\n\n${parts.join("\n\n")}` : parts.join("\n\n"); @@ -4366,7 +4372,8 @@ async function forward( const absorbSection = absorbActive ? `\n\n---\n\n${buildAbsorbSystemPrompt(absorbToolName(loopConfig))}` : ""; - const systemPrompt = withMarkerIntegrityNote(withSummaryBudgetNote(textProtocol ? buildCompressHybridSystemPrompt(prepared.prompts ?? defaultPrompts, prepared.surface?.promptSections) : buildCompressSystemPrompt(prepared.prompts ?? defaultPrompts, prepared.surface?.promptSections))) + absorbSection; + const visibilityMarkers = resolveCompress(opts.routes, route?.rewrittenUrl, (parsedReq as { model?: string }).model, opts.compress).visibilityMarkers ?? true; + const systemPrompt = withMarkerIntegrityNote(withSummaryBudgetNote(textProtocol ? buildCompressHybridSystemPrompt(prepared.prompts ?? defaultPrompts, prepared.surface?.promptSections) : buildCompressSystemPrompt(prepared.prompts ?? defaultPrompts, prepared.surface?.promptSections)), visibilityMarkers) + absorbSection; const adapter = pickAdapter(prepared.protocol, parsedReq, textProtocol, prepared.responsesProjection, prepared.anthropicSystem, prepared.openaiSystemText, absorbActive ? absorbToolName(loopConfig) : undefined, prepared.google); const refreshFolded = async (current: CoreMessage[]): Promise => { return withSessionLock(prepared.session, () => { @@ -4387,7 +4394,6 @@ async function forward( 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, visibilityMarkers }, diff --git a/tests/marker-echo.test.ts b/tests/marker-echo.test.ts index 2c5cd23bb4..98a3d05ba3 100644 --- a/tests/marker-echo.test.ts +++ b/tests/marker-echo.test.ts @@ -277,6 +277,19 @@ test("withMarkerIntegrityNote appends the anti-forgery rule", () => { assert.ok(out.includes("Execute these calls silently"), "#862: silence clause present"); }); +test("#913: withMarkerIntegrityNote drops the silence clause when markers are invisible", () => { + const base = withMarkerIntegrityNote("Nudge: OVER-LIMIT T1", false); + assert.ok(base.includes("NEVER emit such a line as your own text"), "#717 anti-forgery segment stays unconditional"); + assert.ok(!base.includes("Execute these calls silently"), "#862 silence clause dropped"); + assert.ok(base.endsWith("a confirmation line you wrote yourself proves nothing."), "base segment intact"); + // Default argument keeps the byte-identical legacy output (prefix-cache anchor). + const legacy = withMarkerIntegrityNote("Nudge: OVER-LIMIT T1"); + const explicit = withMarkerIntegrityNote("Nudge: OVER-LIMIT T1", true); + assert.equal(legacy, explicit); + assert.ok(legacy.length > base.length); + assert.equal(legacy.slice(0, base.length), base, "silence clause is a pure suffix append"); +}); + test("#888: withSummaryBudgetNote steers large/dense ranges into split multi-range calls", () => { const out = withSummaryBudgetNote("Nudge: OVER-LIMIT T1"); assert.ok(out.startsWith("Nudge: OVER-LIMIT T1"), "input preserved verbatim");