Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 63 additions & 11 deletions src/cache-ledger.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/fold-reconcile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2542,7 +2542,7 @@ export function noteSystemPromptFingerprint(session: Session, system: unknown, o
const prev = session.metadata[METADATA_SYSTEM_FP] as { fp: string; size: number } | undefined;
if (prev !== undefined && prev.fp !== fp) {
if (opts.log !== undefined) {
opts.log("info", `${opts.sessionId === undefined ? "" : `[${opts.sessionId}] `}[fold-reconcile] system prompt changed (${prev.size} → ${text.length} chars); fold state unaffected (#1921)`);
opts.log("info", `${opts.sessionId === undefined ? "" : `[${opts.sessionId}] `}[fold-reconcile] system prompt changed (${prev.size} → ${text.length} chars of the INBOUND client system/developer bundle, pre-injection — not the wire system); fold state unaffected (#1921)`);
}
}
session.metadata[METADATA_SYSTEM_FP] = { fp, size: text.length };
Expand Down
10 changes: 8 additions & 2 deletions src/loop/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import { warnCacheCollapse } from "../cache-warn.js";
import { dumpRejectedBody } from "../error-dump.js";
import { shedEgressIfOverBudget } from "../egress-shed.js";
import { DUMP_PLAINTEXT_BANNER } from "../dump-banner.js";
import { bodyDumpEnabled as knobBodyDumpEnabled, imageRescaleMaxDim } from "../knobs.js";
import { bodyDumpEnabled as knobBodyDumpEnabled, imageRescaleMaxDim, renderNone as knobRenderNone } from "../knobs.js";
import { dumpsDir } from "../paths.js";
import { isStrictReasoningEcho, modelIdOf, normalizeStrictEchoBody } from "../strict-echo.js";
import { log as loggerLog } from "../logger.js";
Expand Down Expand Up @@ -285,6 +285,10 @@ export interface LoopCtx {
* host = upstream hostname, fp = `${billing}:${cap}` fingerprint. When
* present, each sent round captures its image facts alongside the body. */
imageLearn?: { host: string; fp: string };
/** #4397: the per-request render-tag strategy that produced this loop's outbound
* bodies (the global knob can diverge — compaction-trigger requests force "none").
* Stamps noteForwardedBody's rt flag; undefined falls back to the knob. */
renderTags?: "text-only" | "none";
}

export interface RequestOptions {
Expand Down Expand Up @@ -643,7 +647,9 @@ export async function* runCompressLoop(
// noteForwardedBody is a string-only forensics sink; an opaque egress
// envelope (Buffer) is lossy-converted — advisory pairing only, and the
// provenance judge below already skips when the body was rewritten.
noteForwardedBody(ctx.session, typeof wireBodyStr === "string" ? wireBodyStr : wireBodyStr.toString("utf8"), wireArr !== undefined ? wireArr.length : null, credentialFingerprint(requestOptions.headers), generationParamFp(wireObj));
// #4397: stamp whether bili's render tags were active on THIS body (the #2483 suspect flag)
// — per-request strategy via ctx (see LoopCtx.renderTags), knob fallback where absent.
noteForwardedBody(ctx.session, typeof wireBodyStr === "string" ? wireBodyStr : wireBodyStr.toString("utf8"), wireArr !== undefined ? wireArr.length : null, credentialFingerprint(requestOptions.headers), generationParamFp(wireObj), ctx.renderTags !== undefined ? ctx.renderTags !== "none" : !knobRenderNone());
// #564/#2627: provenance is resolved against the array that belongs to
// `wireBodyStr`. Shedding rewrites the body wholesale, so the pre-shed
// `wireArr` no longer describes it; rather than map stale indexes (which
Expand Down
11 changes: 8 additions & 3 deletions src/server/relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { currentFetchTransport } from "../fetch-transport.js";
import { refForItemBytes } from "../image-rescale.js";
import { countImagesInParsedBody, countImagesInRawBody, upstreamHost } from "../image-tokens.js";
import { log as loggerLog } from "../logger.js";
import { dumpReqAllowed as knobDumpReqAllowed, limitsMaxMessages, limitsWsOutboundBytes, rawDumpDir as knobRawDumpDir } from "../knobs.js";
import { dumpReqAllowed as knobDumpReqAllowed, limitsMaxMessages, limitsWsOutboundBytes, rawDumpDir as knobRawDumpDir, renderNone as knobRenderNone } from "../knobs.js";
import { gcDumpDirIfConfigured } from "../state-gc.js";
import { applyLaneCredential, laneCredential } from "../lane-credentials.js";
import { CREDENTIAL_HEADER_RE, maskHeaderForLog, maskHeadersForLog, maskHostPortForDisplay, maskUrlForLog } from "../log-mask.js";
Expand Down Expand Up @@ -1191,7 +1191,12 @@ const reply = prepared && !prepared.sidePassthrough ? createHandoffReply(prepare
// #3803: the generation-parameter fingerprint rides the SAME full parse as the message count —
// it is extracted from sentParsed (the complete object), never from the capped body head, so a
// trailing-field flip is seen even when the body exceeds SEAM_BODY_CAP.
noteForwardedBody(prepared.session, sentBody, Array.isArray(sentArr) ? (sentArr as unknown[]).length : null, credentialFingerprint(headers), generationParamFp(sentParsed));
// #4397: stamp whether bili's render tags were active on THIS body (the #2483 suspect flag).
// Per-request strategy, not the global knob: compaction-trigger requests force "none"
// even with the knob off (prepare-responses); lanes that carry no strategy (byte-faithful
// passthroughs) fall back to the knob — their bodies are client bytes, so tag presence ≈
// session tag mode.
noteForwardedBody(prepared.session, sentBody, Array.isArray(sentArr) ? (sentArr as unknown[]).length : null, credentialFingerprint(headers), generationParamFp(sentParsed), prepared.renderTags !== undefined ? prepared.renderTags !== "none" : !knobRenderNone());
// #3023: the client's own resent view — the protocol conversion of the
// inbound bytes BEFORE processTurn folded/replaced anything, captured
// with the exact same pairing semantics so detectSeam can attribute a
Expand Down Expand Up @@ -2816,7 +2821,7 @@ const reply = prepared && !prepared.sidePassthrough ? createHandoffReply(prepare
const loopBillingUpstream = route?.rewrittenUrl ?? (/^https?:\/\//i.test(req.url ?? "") ? req.url ?? undefined : opts.upstream);
const loop = runCompressLoop(
responseBody,
{ core, config, messages: prepared.processedMessages.length > 0 ? prepared.processedMessages : prepared.originalMessages, compressMessages: prepared.originalMessages, session: prepared.session, reply, log: ctx.log, proxyUrl, skippedProxy, upstreamOrigin: targetOrigin, protocol: prepared.protocol, textProtocol, debug: opts.debug, refreshFolded, separatorSlots: () => loopSeparatorSlots, visibilityMarkers, dumpSse: loopDumpDir ? (name, stream) => dumpStreamToFile(stream, loopDumpDir, name) : undefined, imageLearn: { host: upstreamHost(loopBillingUpstream), fp: `${imageBillingFor(opts, loopBillingUpstream)}:${imageTokenCapFor(opts, loopBillingUpstream)}` } },
{ core, config, messages: prepared.processedMessages.length > 0 ? prepared.processedMessages : prepared.originalMessages, compressMessages: prepared.originalMessages, session: prepared.session, reply, log: ctx.log, proxyUrl, skippedProxy, upstreamOrigin: targetOrigin, protocol: prepared.protocol, textProtocol, debug: opts.debug, renderTags: prepared.renderTags, refreshFolded, separatorSlots: () => loopSeparatorSlots, visibilityMarkers, dumpSse: loopDumpDir ? (name, stream) => dumpStreamToFile(stream, loopDumpDir, name) : undefined, imageLearn: { host: upstreamHost(loopBillingUpstream), fp: `${imageBillingFor(opts, loopBillingUpstream)}:${imageTokenCapFor(opts, loopBillingUpstream)}` } },
parsedReq,
// #3280: egress goes out enveloped via wireTransform, so every
// round-2+ response comes back enveloped too — unwrap at the loop's
Expand Down
40 changes: 40 additions & 0 deletions tests/codex-compact-e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { ProxyOptions } from "../src/config.ts";
import { SessionStore, _setStoreForTest } from "../src/persist.ts";
import { _setForTest as setRegistryForTest } from "../src/registry.ts";
import { listSessions, _resetSessionsForTest } from "../src/session.ts";
import { getCacheLedger } from "../src/cache-ledger.ts";
import { buildTriggerForgeBody } from "../src/codex-compact.ts";

// Issue #321 PR-E2: conditional interception + forgery of codex's native
Expand Down Expand Up @@ -284,6 +285,45 @@ test("e2e E2 (trigger form): kill-switch off (BILI_CODEX_COMPACT=pass) → forwa
});
});

test("e2e E2 (trigger form): tag-free prepared body carries no rt flag even with the renderNone knob off (#4397 rt precision)", async () => {
const prevEnv = process.env.ACP_RENDER_NONE;
try {
delete process.env.ACP_RENDER_NONE; // knob OFF — a global-knob stamp would have set rt=1 here
await withHarness({ firstTurnTokens: 1000 }, async (h) => {
const afterSetup = await setupCompressedSession(h);
const s = listSessions().find((x) => x.meta.label === SESSION)!;
const linesBefore = getCacheLedger(s).lines.length;
// Non-codex UA: no codex early-dispatch (handle.ts forwards those
// verbatim with prepared:null, never touching the seam slot), so
// bili prepares the trigger body itself — renderTags forced "none"
// by prepare-responses regardless of the knob. The ONLY shape where
// per-request strategy diverges from the global knob.
const r2 = await fetch(h.url, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: SESSION, instructions: "You are the test coding agent.", input: [...conversation(), { type: "compaction_trigger" }] }),
});
assert.equal(r2.status, 200);
await r2.text();
assert.equal(h.bodies.length, afterSetup + 1, "compact request forwarded to upstream");
// The stamp lives on the seam slot until settle consumes it into the
// ledger line; a trigger-form turn may end unsettled (stream-truncation
// frame), so accept EITHER carrier — neither may carry the #2483 flag.
const led = getCacheLedger(s);
const slotRt = s.transient?.sentBody?.rt;
// Attribute the line to THIS turn: an unsettled turn leaves the
// previous turn's (legitimately tagged) line as the last one.
const lineRt = led.lines.length > linesBefore ? led.lines[led.lines.length - 1]?.rt : undefined;
assert.ok(s.transient?.sentBody !== undefined || led.lines.length > linesBefore, "compact turn was seam-observed");
assert.notEqual(slotRt, 1, "seam slot carries no rt flag on a tag-free body");
assert.notEqual(lineRt, 1, "settled line carries no rt flag on a tag-free body");
});
} finally {
if (prevEnv === undefined) delete process.env.ACP_RENDER_NONE;
else process.env.ACP_RENDER_NONE = prevEnv;
}
});

test("e2e E2 (trigger form): intercept + ACP not keeping up (≥90%) → forwarded (native backstop)", async () => {
await withHarness({ mode: "intercept", firstTurnTokens: 15000 }, async (h) => {
const afterSetup = await setupCompressedSession(h);
Expand Down
6 changes: 3 additions & 3 deletions tests/golden/storage-render/anthropic.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@
"foldAt": 9,
"editedAt": 12,
"summaryFrom": 9,
"stateDigest": "1f528f4fee4f59026d3b005f99a0489cadc139e54698896b2e5a0dadbc95ee2b",
"stateDigestAligned": "a796f61ccd006f5fa1573e1ef33bf756dc24bf017154fab8bde4c0b369516537",
"stateDigest": "75a2e66e7bac21c5cebb67b2da3c0100e15636180340fec39c927574d6069487",
"stateDigestAligned": "faec77643fd5d0c65e64b3e6ec22f3586c210567643ee2a268633cd662655fea",
"sectionDigests": {
"state": "56e66f6b72c7f0dd",
"blockContents": "dc7d13d250c94bb4",
"stats": "e83198a55e6b2c29",
"metadata": "65894bb8839e9947"
"metadata": "2e41901e47fbc2f9"
},
"stateKeyDigests": {
"blocks": "3a454b9a16e4f0b9",
Expand Down
6 changes: 3 additions & 3 deletions tests/golden/storage-render/chat.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@
"foldAt": 9,
"editedAt": 12,
"summaryFrom": 9,
"stateDigest": "a7b5a67f54a2cbd7978e2a57ad78f522f7af8f94a4e2402898aee4d93479541a",
"stateDigestAligned": "76f701f5ab1631fe1615a3f1cd8fa0fcd0a3fa19c02658cc5d077ae565f4897a",
"stateDigest": "84aa5f49e6ba7bd9cc7dd7d495dade9e9981d678b98e7dc1093681d8e5eacfcb",
"stateDigestAligned": "4fced9f01ffc7194484a73caf007ae2eef99849cb47bec5518994a202c910d28",
"sectionDigests": {
"state": "90857fbf82001a2b",
"blockContents": "94a58f96d7c5cfc3",
"stats": "5c2f0fe203d710e8",
"metadata": "0c7d1819e0d4c0f6"
"metadata": "a97fd75dacfa237c"
},
"stateKeyDigests": {
"blocks": "d9770feee91dafc6",
Expand Down
6 changes: 3 additions & 3 deletions tests/golden/storage-render/google.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@
"foldAt": 9,
"editedAt": 12,
"summaryFrom": 9,
"stateDigest": "f8d07462fd8881896c5b52864e84ff9a1a79b0cdb73cd2ef87e16d361b5a07d3",
"stateDigestAligned": "219c42a0f17ed7c9061174627ca56e5b6733e7413d8af98953105bf5d60a40b7",
"stateDigest": "ba3777528ac0d87f32d1de0ea0232a3379aea5e82a359be04271a29315e3cdee",
"stateDigestAligned": "47cc6da83e5413dbc1880faad99a0fcc64b96dcca0f567ab1b041220cd66b877",
"sectionDigests": {
"state": "c5243ba17364d3ac",
"blockContents": "473264d9df8ba338",
"stats": "a3c84817f1d0c3dd",
"metadata": "615f125909ed135d"
"metadata": "d65143e223045acd"
},
"stateKeyDigests": {
"blocks": "0cfd0b9c0fc53073",
Expand Down
6 changes: 3 additions & 3 deletions tests/golden/storage-render/responses.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@
"foldAt": 9,
"editedAt": 12,
"summaryFrom": 9,
"stateDigest": "aa95de9ed8ddaff40f0b0e2deb1991c85582e33a17149c030687f46cfa34a9a3",
"stateDigestAligned": "5f40c3661d4c43338d855b5a5e79b2915da934affe33d6cb07680a1da39bb996",
"stateDigest": "e5d7a2740ce7772813bbcb981815d56f46dea8d27d24de2ce3a0661594f8f488",
"stateDigestAligned": "7be015bfd5f2889b6cf600c29c9712ecb62cbca1baef4f18660d9ae6e2d34eb5",
"sectionDigests": {
"state": "e686a14f31d69ce5",
"blockContents": "b6701268a0bdcd8a",
"stats": "099bde0a9cf64cad",
"metadata": "be12181116331419"
"metadata": "b9ced50a31ccde78"
},
"stateKeyDigests": {
"blocks": "34b0dc17f8c24595",
Expand Down
Loading
Loading