Skip to content
Closed
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
31 changes: 31 additions & 0 deletions src/loop/tag-echo-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,37 @@ export function createTagEchoFilter(onDrop?: (snippet: string) => void, onResidu
if (swallowUntilClose) {
const combined = swallowed + buf;
const span = looseCloseSpan(combined);
// #4163: a NEW opening inside the live bare-open span is the
// cross-chunk form of the #1720 nesting evidence ("more tag
// structure follows in this same chunk"): the whole-buffer
// path would have dropped THIS opening alone and released
// whatever sat between it and the inner markup as prose.
// Without the check the inner opening accumulates into
// `swallowed`, and the close-resolution release branch or the
// EOF bare-release (#1881) hands it to the client verbatim —
// same logical output, different persisted bytes per chunking
// (#3198). Split only when the opening precedes any close
// terminator: a close first resolves the pair on the existing
// path and the remainder (opening included) loops normally.
// Gated to bare-open release sessions: an attrs-bearing tail
// is tag content end-to-end (#644 EOF rule), and a wrapped
// turn discards its payload wholesale — neither re-adjudicates
// its interior.
if (swallowBareOpen && swallowReleases) {
const o = LONE_OPEN.exec(combined);
if (o !== null) {
const d = degenCloseAfterRef(combined);
const term = span !== null ? span.start : d !== null ? d.start : -1;
if (term === -1 || o.index < term) {
out += stripDeadRefCloses(combined.slice(0, o.index), true);
dropSwallowSpan(o[0]);
swallowed = "";
swallowUntilClose = false;
buf = combined.slice(o.index);
continue;
}
}
}
if (span !== null) {
// Only a refs-only body is tag content (#1720/#2023): a
// prose body between paired tags is released and just the
Expand Down
155 changes: 155 additions & 0 deletions tests/issue4163-bare-open-boundary.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// #4163: two bare open render tags straddling a chunk boundary — the second
// was handed to the client as body content while the whole-buffer push
// stripped both. Root cause: the #1720 nesting evidence ("more tag structure
// follows in this same chunk") was judged on the CURRENT buffer only; across
// a boundary the later opening accumulated into the live bare-open swallow
// and the #1881 EOF bare-release (or the close-resolution release branch)
// passed it through verbatim — same logical output, different persisted
// bytes per chunking (#3198 cache-seam contract).
//
// Fix: while a bare-open release session is live, a NEW opening inside the
// accumulated span is exactly that nesting evidence arriving late — split the
// span (release the prefix as prose, count the outer opening as dropped
// alone) and re-adjudicate from the inner opening through the normal loop,
// which is the code path the whole-buffer run takes.
import test from "node:test";
import assert from "node:assert/strict";
import { createTagEchoFilter } from "../src/loop/tag-echo-filter.ts";

const O = "\x3cacp\x3e"; // bare open render tag
const C = "\x3c/acp\x3e"; // matching close

function runStream(input: string, splits: number[]): { out: string; dropCount: number } {
const f = createTagEchoFilter();
let out = "";
let pos = 0;
for (const len of splits) {
out += f.push(input.slice(pos, pos + len));
pos += len;
}
if (pos < input.length) out += f.push(input.slice(pos));
out += f.flush();
return { out, dropCount: f.stats().dropCount };
}

/** Whole push plus every single cut k = 0..len (endpoint cuts degenerate to whole). */
function allCuts(input: string): number[][] {
const cuts: number[][] = [[input.length]];
for (let k = 1; k < input.length; k++) cuts.push([k, input.length - k]);
return cuts;
}

/** Seeded random splittings, sizes 1..40 (same discipline as the #3198 fuzz). */
function mulberry32(seed: number): () => number {
let a = seed >>> 0;
return () => {
a |= 0;
a = (a + 0x6d2b79f5) | 0;
let t = Math.imul(a ^ (a >>> 15), 1 | a);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
function randomSplits(input: string, seed: number): number[] {
const rnd = mulberry32(seed);
const splits: number[] = [];
let left = input.length;
while (left > 0) {
const n = Math.min(left, 1 + Math.floor(rnd() * 40));
splits.push(n);
left -= n;
}
return splits;
}

// Each shape pins its settled bytes (whole-buffer AND whole-text mode agree);
// the assertions then require every chunking to reproduce them byte-for-byte.
const SHAPES: Array<{ name: string; text: string; expected: string }> = [
{ name: "two bare opens adjacent (issue repro)", text: `${O}${O}text`, expected: "text" },
{ name: "two bare opens, space between", text: `${O} ${O}text`, expected: " text" },
{ name: "prose between two bare opens", text: `${O}xyz${O}text`, expected: "xyztext" },
{ name: "refs run between two bare opens", text: `${O}m1234${O}text`, expected: "m1234text" },
{ name: "three bare opens adjacent", text: `${O}${O}${O}text`, expected: "text" },
{ name: "bare open wrapping a bare pair", text: `${O}${O}m1234${C}text`, expected: "text" },
{ name: "prose then nested bare pair", text: `${O}junk${O}m1234${C}x`, expected: "junkx" },
{ name: "closed pair then nested bare open", text: `${O}m1234${C}junk${O}`, expected: "junk" },
{ name: "prose head, two bare opens", text: `Answer.\n${O}${O}tail.`, expected: "Answer.\ntail." },
{ name: "case-drifted names, two bare opens #1731", text: "\x3cACP\x3e\x3cACP\x3etext", expected: "text" },
];

test("#4163: issue repro table — whole push and the between-tags cut pin the same bytes", () => {
for (const s of SHAPES) {
const whole = runStream(s.text, [s.text.length]);
assert.equal(whole.out, s.expected, `[${s.name}] whole-buffer output drifted from pinned expectation`);
// The reported flip point: boundary immediately after the first open.
const between = runStream(s.text, [s.text.indexOf(O) + O.length, s.text.length - s.text.indexOf(O) - O.length]);
assert.equal(between.out, s.expected, `[${s.name}] between-tags cut leaked or lost bytes`);
}
});

test("#4163: every single-cut splitting reproduces the settled bytes", () => {
for (const s of SHAPES) {
for (const cuts of allCuts(s.text)) {
const got = runStream(s.text, cuts);
assert.equal(
got.out,
s.expected,
`[${s.name}] cuts=${JSON.stringify(cuts)}: got ${JSON.stringify(got.out)}, want ${JSON.stringify(s.expected)}`,
);
}
}
});

test("#4163: seeded random splittings reproduce the settled bytes", () => {
for (const s of SHAPES) {
for (let seed = 0; seed < 20; seed++) {
const got = runStream(s.text, randomSplits(s.text, seed));
assert.equal(got.out, s.expected, `[${s.name}] random seed ${seed}: got ${JSON.stringify(got.out)}, want ${JSON.stringify(s.expected)}`);
}
}
});

test("#4163: logical-artifact count (#2405c) is chunk-invariant for the nested-bare-open family", () => {
for (const s of SHAPES) {
const ref = runStream(s.text, [s.text.length]).dropCount;
for (const cuts of allCuts(s.text)) {
const got = runStream(s.text, cuts).dropCount;
assert.equal(got, ref, `[${s.name}] cuts=${JSON.stringify(cuts)}: dropCount ${got} != whole-buffer ${ref}`);
}
}
});

test("#4163: single bare opens keep the #1881 prose-wearing semantics under every cut", () => {
// One bare open, prose body: the open dies, the prose lives — the fix must
// not turn a lone open into a split trigger (no second opening exists).
const texts = [
`see ${O} for details`,
`lead ${O}m00123 tail`, // bare open, refs-only body -> whole unit dies
];
for (const text of texts) {
const ref = runStream(text, [text.length]);
for (const cuts of allCuts(text)) {
const got = runStream(text, cuts);
assert.equal(got.out, ref.out, `[${JSON.stringify(text)}] cuts=${JSON.stringify(cuts)}: got ${JSON.stringify(got.out)}, want ${JSON.stringify(ref.out)}`);
assert.equal(got.dropCount, ref.dropCount, `[${JSON.stringify(text)}] cuts=${JSON.stringify(cuts)}: dropCount differs`);
}
}
});

test("#4163: attrs-bearing and wrapped sessions are untouched by the split gate", () => {
// The gate is bare-open-release-only: an attrs-bearing tail is tag content
// end-to-end (#644 EOF rule), a wrapped turn discards its payload wholesale.
// Both stay chunk-deterministic exactly as before the fix.
const texts = [
`A \x3cacp tokens="1"\x3em1234${C}B.`,
`A \x3cacp tokens="1" type="text"\x3e${O}m1234${C}B.`, // nested markup inside an attrs session (pre-existing uniform behavior)
`A \x3cacp tokens="1" x="y\x3ctool\x3e payload ${C} tail.`, // wrapped odd quotes
];
for (const text of texts) {
const ref = runStream(text, [text.length]);
for (const cuts of allCuts(text)) {
const got = runStream(text, cuts);
assert.equal(got.out, ref.out, `[${JSON.stringify(text)}] cuts=${JSON.stringify(cuts)}: got ${JSON.stringify(got.out)}, want ${JSON.stringify(ref.out)}`);
}
}
});
4 changes: 4 additions & 0 deletions tests/tag-echo-chunking-determinism.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ const CORPUS: Array<{ name: string; text: string }> = [
{ name: "trunc ref close #2348v4", text: "Answer.\nm00123\x3c/ap" },
{ name: "dead ref close #2862", text: "Answer.\n\x3cacp tokens=\"2\" type=\"text\"\x3em00123\x3c/acp\x3e then m00124\x3c/warn prose continues here." },
{ name: "two pairs adjacent", text: "A \x3cacp tokens=\"2\" type=\"text\"\x3em00123\x3c/acp\x3e B \x3cacp tokens=\"3\" type=\"text\"\x3em00124\x3c/acp\x3e C." },
{ name: "two bare opens adjacent #4163", text: "Answer.\n\x3cacp\x3e\x3cacp\x3etext tail." },
{ name: "bare open prose bare open #4163", text: "Answer.\n\x3cacp\x3emid prose\x3cacp\x3etext tail." },
{ name: "three bare opens #4163", text: "Answer.\n\x3cacp\x3e\x3cacp\x3e\x3cacp\x3etext tail." },
{ name: "bare open wrapping bare pair #4163", text: "Answer.\n\x3cacp\x3e\x3cacp\x3em00123\x3c/acp\x3etail." },
{ name: "pair split shape mid-tag", text: "Answer. \x3cacp to\nkens=\"2\" type=\"text\"\x3em00123\x3c/acp\x3e tail." },
{ name: "plain prose control", text: "the acpi.h include and caption and app stay untouched, a<b c>d." },
{ name: "empty", text: "" },
Expand Down
15 changes: 9 additions & 6 deletions tests/verif-tag-echo-filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ test("P1 chunk-split equivalence: streaming === whole-text", { skip: false }, ()
for (let iter = 0; iter < 120; iter++) {
const base = rnd() < 0.1 ? emissionField() : randomText();
const text = rnd() < 0.2 ? `${pick(PROSE)}${base}${pick(PROSE)}` : base;
if (containsRenderTagText(text) || containsEchoResidue(text)) continue; // #4107 family: split-dependence on tag-bearing texts (2 shapes pinned)
if (containsRenderTagText(text) || containsEchoResidue(text)) continue; // #4107 family: split-dependence on tag-bearing texts (shape 1 still pinned; shape 2 closed by #4170)
for (const [name, mk] of FACTORIES) {
const whole = runStream(mk(), [text]);
for (const k of splitPoints(text)) {
Expand Down Expand Up @@ -338,16 +338,19 @@ test("C#4107 pinned: chunk split leaks dead-ref residue today", () => {
assert.ok(split.includes("m1255"), "residue no longer leaks? — #4107 may be fixed; flip this pin");
});
// #4107 shape 2 (same family, different mechanism): a CLOSE fragment at
// the chunk boundary — whole-text strips the bare </apc fragment, the
// split path RELEASES it to the client.
test("C#4107-2 pinned: close fragment at chunk boundary leaks today", () => {
// the chunk boundary followed by a nested bare open behind it. CLOSED by
// #4170: the split path now re-adjudicates the late-arriving open instead
// of releasing the interior verbatim, so streaming === whole-text. Flipped
// per the original pin's instruction; kept as a permanent parity pin.
// (Shape 1, C#4107 dead-ref residue, is still broken and stays pinned.)
test("C#4107-2 closed by #4170: close fragment + nested bare open no longer leaks", () => {
const t = 'unicode \u2713 caf\u00e9 \u65e5\u672c <apc>unicode \u2713 caf\u00e9 \u65e5\u672c</apc <apc>quotes " and \' mixed</apc>';
const run = (chunks: string[]): string => {
const f = createTagEchoFilter();
return chunks.map((c) => f.push(c)).join("") + f.flush();
};
const whole = run([t]);
const split = run([t.slice(0, 49), t.slice(49)]);
assert.notEqual(whole, split, "#4107 fixed? — flip this pin and re-enable the full P1 domain");
assert.ok(split.includes("</apc "), "close fragment no longer leaks? — flip this pin");
assert.equal(whole, split, "#4107-2 regressed? — streaming diverged from whole-text again");
assert.ok(!split.includes("<apc>"), "nested bare open leaked into body text again");
});
2 changes: 1 addition & 1 deletion verif/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ VERIF_PYTHON=/path/to/python-with-cvc5 node --import tsx --test tests/verif-bug*
| `verif-view-latch-discipline` | **锁存纪律判官**(#3989) | 安静轮 latch 字节逐字回放(Q1 UNSAT)+ 新鲜消息直通不受污染(Q2 UNSAT,任意 pre/post/latch 文本)+ 真代码行为矩阵(quiet 回放 / 到达锁存 / 折叠重置 / 稳态零churn) |
| `verif-conflict-watch` / `verif-snapshot-render` / `verif-wire-anthropic-roundtrip` / `verif-wire-google-rebuild-stability` | 定向不变量 | conflict-watch 降落门限纪律(#2709/#3806 形状,边界 749/1000 vs 750/1000);snapshot-render 尾保真(发现 **#4052**:新消息与已压缩消息同文本被整条丢弃,钉为翻转断言);anthropic 同类对序+载荷保真;google 重建 append-only 稳定 |
| `verif-responses-ingress` | 定向不变量(#3837 家族) | 入站三件套(normalize→sanitize→drop,真实管线顺序)幂等/规范化/局部性全部 UNSAT + 各阶段可达性 SAT + 60 轮随机回放锚 |
| `verif-tag-echo-filter` | **回放性质判官(无求解器)** | tag-echo-filter 真实代码上的流式分块等价 / strip 幂等 / 单调不增 / 干净保全 / 残留对偶 + 逐 issue 控制(#644/#1720/#1760/#1881/#2023/#2862/#717/#2405)。**发现 #4106(strip 不幂等:marker 门控序)与 #4107(流式输出依赖分块,两种形状)——均钉为已坏控制,修复后翻转**。strip 链的 lookaround+\p{So} 正则与流式 re.exec 循环在翻译子语言之外,故此判官用真实代码回放性质而非求解器 |
| `verif-tag-echo-filter` | **回放性质判官(无求解器)** | tag-echo-filter 真实代码上的流式分块等价 / strip 幂等 / 单调不增 / 干净保全 / 残留对偶 + 逐 issue 控制(#644/#1720/#1760/#1881/#2023/#2862/#717/#2405)。**发现 #4106(strip 不幂等:marker 门控序)与 #4107(流式输出依赖分块,两种形状)——均钉为已坏控制,修复后翻转(#4107 形状 2 已由 #4170 关闭并翻转为恒等钉;形状 1 仍钉)**。strip 链的 lookaround+\p{So} 正则与流式 re.exec 循环在翻译子语言之外,故此判官用真实代码回放性质而非求解器 |

## 无作弊规则(写新 harness 前必读)

Expand Down
Loading