Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
a731d24
fix: retain reconciliation brackets for very long histories
Afloat16 Nov 12, 2026
2f2fe73
merge: integrate current master for PR 6492
Afloat16 Nov 13, 2026
b8545db
fix(incremental): keep canonical-pointer upkeep linear past the argum…
Nov 13, 2026
5e1972e
test: pin unified storage in the long-history regression
Afloat16 Nov 13, 2026
c6369fb
fix(tests): pin pure-sqlite selection in notice-registry suite
Nov 13, 2026
b1bc17b
fix(tests): pin BILI_STORAGE_JSON_WRITER=on in CCR cold-disk-restore …
Nov 14, 2026
75c358e
Merge remote-tracking branch 'origin/master' into w5p
ranxianglei Nov 14, 2026
cd8bc8a
Merge remote-tracking branch 'origin/master' into ci-fixes
Nov 14, 2026
7492c35
fix(tests): pin BILI_STORAGE_JSON_WRITER=on in native opencode e2e lane
Nov 14, 2026
45f93db
fix(tests): pin BILI_STORAGE_JSON_WRITER=on in native dsh e2e lane
Nov 14, 2026
d67cc43
fix(tests): pin BILI_STORAGE_JSON_WRITER=on in pi codex-responses WS …
Nov 14, 2026
c392237
fix(tests): pin BILI_STORAGE_JSON_WRITER=on in pi package-native e2e …
Nov 14, 2026
974cd4c
fix(tests): pin BILI_STORAGE_JSON_WRITER=on in fork-ccr-adoption suite
Nov 14, 2026
271abba
fix(tests): pin BILI_STORAGE_JSON_WRITER=on in subagent-sessions e2e …
Nov 14, 2026
5d47196
fix(tests): pin BILI_STORAGE_JSON_WRITER=on in dsh-persona e2e lane
Nov 14, 2026
1ca4087
fix(tests): pin BILI_STORAGE_JSON_WRITER=on in update-state e2e lane
Nov 14, 2026
8b278fc
Merge upstream master and ci-fixes to refresh PR validation
Afloat16 Nov 14, 2026
9304052
fix: align e2e-native-pi env block indentation to the 8-space style
Nov 14, 2026
6f7916f
Merge upstream master and converge CI test fixes
Afloat16 Nov 14, 2026
04f998b
test: preserve long-history pointer handler coverage
Afloat16 Nov 14, 2026
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
11 changes: 6 additions & 5 deletions src/fold-reconcile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,8 @@ export function planReconciliation(
// claimed. A gap cell is bracketed when verified claims exist on both
// sides — order ownership that the tiers below may corroborate.
const gapPairs: { oldIdx: number; oldId: string; newId: string }[] = [];
const claimedIdx: number[] = [];
let minClaimed = Infinity;
let maxClaimed = -Infinity;
const recordGap = (oldIdx: number, oldId: string, newId: string): void => {
if (oldId !== newId && missingSet.has(oldId) && !covered.has(newId) && !claimedCandidates.has(newId)) {
gapPairs.push({ oldIdx, oldId, newId });
Expand Down Expand Up @@ -1034,7 +1035,8 @@ export function planReconciliation(
plan.claims.set(oldId, [newId]);
claimedCandidates.add(newId);
plan.byPos++;
claimedIdx.push(i);
minClaimed = Math.min(minClaimed, i);
maxClaimed = Math.max(maxClaimed, i);
i++;
}
const tail = Math.min(oldOrder.length, newOrder.length) - prefix;
Expand Down Expand Up @@ -1064,7 +1066,8 @@ export function planReconciliation(
plan.claims.set(oldId, [newId]);
claimedCandidates.add(newId);
plan.byPos++;
claimedIdx.push(iOld);
minClaimed = Math.min(minClaimed, iOld);
maxClaimed = Math.max(maxClaimed, iOld);
j++;
}
}
Expand Down Expand Up @@ -1387,8 +1390,6 @@ export function planReconciliation(
// when something verified it.
if (gapPairs.length > 0) {
const fenced = plan.byPos > 0 || plan.resyncs > 0;
const minClaimed = claimedIdx.length > 0 ? Math.min(...claimedIdx) : -1;
const maxClaimed = claimedIdx.length > 0 ? Math.max(...claimedIdx) : -1;
for (const gap of gapPairs) {
if (plan.claims.has(gap.oldId) || claimedCandidates.has(gap.newId)) continue;
const anchor = anchors[gap.oldId];
Expand Down
9 changes: 6 additions & 3 deletions src/incremental-lane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,16 +421,19 @@ function stableMessageKey(m: Record<string, unknown>): string {
* handful of fresh rows instead of walking the whole table every turn.
* Any miss → leave the previous pointers untouched (never a partial list);
* with the debounced save this is the common case right after a turn — a
* later settled turn records them. */
function recordCanonicalPointers(sessionId: string, msgs: BiliMessage[], log: (l: string, m: string) => void): void {
* later settled turn records them. Exposed for unit tests (#6490). */
export function recordCanonicalPointers(sessionId: string, msgs: BiliMessage[], log: (l: string, m: string) => void): void {
try {
const store = getStore();
const prevPtrs = readCanonicalPointers(sessionId);
let ptrs: number[];
let rows: { seq: number; content: string; ts: number | null }[] | null;
if (prevPtrs !== null && prevPtrs.length <= msgs.length) {
ptrs = [...prevPtrs];
rows = store.unifiedRawMessages(sessionId, ptrs.length > 0 ? Math.max(...ptrs) : -1);
// reduce, not Math.max(...spread): a long session's pointer list
// outgrows V8's argument-spread limit (~65K-125K depending on
// build; measured 125K ok / 130K throws on Node 22 x64) → RangeError (#6490).
rows = store.unifiedRawMessages(sessionId, ptrs.length > 0 ? ptrs.reduce((m, p) => (p > m ? p : m), 0) : -1);
} else {
ptrs = [];
rows = store.unifiedRawMessages(sessionId);
Expand Down
5 changes: 5 additions & 0 deletions tests/e2e/e2e-update-state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,11 @@ test("self-update keeps live compression state: folds survive the OLD→NEW hop
envBase.BILI_UPDATE_POLICY_URL = "http://127.0.0.1:1/no-policy.json";
envBase.BILI_PERSIST_DEBOUNCE_MS = "200";
envBase.BILI_SESSIONS_DIR = sessionsDir;
// Pure-sqlite default (5a80e2b8e, #6480) turns the JSON envelope
// writer OFF, but this lane asserts on the on-disk .json session file
// (state survival across the update). Pin the writer ON so the
// envelope this test was written for is still produced.
envBase.BILI_STORAGE_JSON_WRITER = "on";

const port = await freePort();
proxy = spawnProxy(installDir, port, envBase);
Expand Down
58 changes: 58 additions & 0 deletions tests/fold-reconcile-large-history.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import assert from "node:assert/strict";
import { test } from "node:test";
import type { CoreMessage } from "acp-kernel";
import { planReconciliation, positionalFingerprint, type FoldAnchor } from "../src/fold-reconcile.ts";

function remintedHistory(count: number, gaps: number[]) {
const order = Array.from({ length: count }, (_, i) => `old_${i}`);
const messages: CoreMessage[] = order.map((_, i) => ({ id: `new_${i}`, role: "assistant", contentType: "text", text: `unchanged response ${i}` }));
const positions = { ids: order, canon: messages.map(positionalFingerprint) };
const anchors: Record<string, FoldAnchor> = {};
for (const gap of gaps) {
anchors[order[gap]] = { r: "assistant", c: "text", b: messages[gap].text!.length };
messages[gap] = { ...messages[gap], text: `reformatted response ${gap}` };
}
return { order, messages, positions, anchors, covered: new Set(order) };
}

test("reconciles a gap bracketed by 150,000 verified positional claims", () => {
const gap = 75_000;
const h = remintedHistory(150_001, [gap]);
const plan = planReconciliation(h.order, h.anchors, h.messages, h.covered, h.positions);
assert.equal(plan.byPos, 150_000);
assert.equal(plan.byOrder, 1);
assert.equal(plan.claims.size, h.order.length);
assert.deepEqual(plan.claims.get(h.order[gap]), [`new_${gap}`]);
assert.deepEqual(plan.claims.get(h.order[0]), ["new_0"]);
assert.deepEqual(plan.claims.get(h.order.at(-1)!), ["new_150000"]);
assert.deepEqual(plan.unmatched, []);
});

test("keeps leading and trailing gaps outside the verified bracket", () => {
const h = remintedHistory(20, [0, 10, 19]);
const plan = planReconciliation(h.order, h.anchors, h.messages, h.covered, h.positions);
assert.equal(plan.byPos, 17);
assert.equal(plan.byOrder, 1);
assert.deepEqual(plan.claims.get("old_10"), ["new_10"]);
assert.deepEqual(plan.unmatched, ["old_0", "old_19"]);
});

test("does not infer a bracket without any verified positional claims", () => {
const h = remintedHistory(5, [0, 1, 2, 3, 4]);
const plan = planReconciliation(h.order, h.anchors, h.messages, h.covered, h.positions);
assert.equal(plan.byPos, 0);
assert.equal(plan.byOrder, 0);
assert.equal(plan.claims.size, 0);
assert.deepEqual(plan.unmatched, h.order);
});

test("keeps the zero-drift fast path for a large unchanged history", () => {
const h = remintedHistory(150_001, []);
for (let i = 0; i < h.messages.length; i++) h.messages[i].id = h.order[i];
const plan = planReconciliation(h.order, h.anchors, h.messages, h.covered, h.positions);
assert.equal(plan.claims.size, 0);
assert.equal(plan.byPos, 0);
assert.equal(plan.byOrder, 0);
assert.deepEqual(plan.unmatched, []);
assert.deepEqual(plan.newOrder, h.order);
});
61 changes: 60 additions & 1 deletion tests/inc-canonical-pointers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import test from "node:test";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { Readable } from "node:stream";
import { defaultConfig } from "acp-kernel";
import { anthropicToCore } from "acp-kernel/wire";
import { startServer } from "../src/server.ts";
import type { ProxyOptions } from "../src/config.ts";
import { SessionStore, _setStoreForTest, getStore } from "../src/persist.ts";
import { _resetPluginStateForTest } from "../src/plugin.ts";
import { _resetSessionsForTest } from "../src/session.ts";
import { _setForTest } from "../src/registry.ts";
import { resetToolRingForTest } from "../src/tool-ring.ts";
import { __resetIncrementalLaneForTests, __incrementalPtrChainForTests } from "../src/incremental-lane.ts";
import { __resetIncrementalLaneForTests, __incrementalPtrChainForTests, handleIncrementalTurn } from "../src/incremental-lane.ts";

/**
* #3901 / #3798 — incremental canonical row pointers.
Expand Down Expand Up @@ -88,6 +90,63 @@ async function harness(): Promise<{

const core = (id: string, text: string): Record<string, unknown> => ({ id, role: "user", contentType: "text", content: text.repeat(200) });

test("pointer upkeep extends very long restored histories from their highest row", async (t) => {
_resetSessionsForTest();
_resetPluginStateForTest();
__resetIncrementalLaneForTests();
const dir = mkdtempSync(join(testRoot, "long-"));
const store = new SessionStore({ dir, enabled: false });
_setStoreForTest(store);
const count = 250_000;
const ptrs = Array.from({ length: count }, (_, i) => i);
[ptrs[0], ptrs[count - 1]] = [ptrs[count - 1]!, ptrs[0]!];
const rows = Array.from({ length: count }, (_, seq) => ({
seq, content: JSON.stringify({ id: `prefix-${seq}`, role: "user", contentType: "text", text: "x" }), ts: null,
}));
const delta = { role: "assistant" as const, content: "new tail" };
const tail = anthropicToCore({ messages: [delta] }).msgs[0]!;
const reads: number[] = [];
const written: unknown[] = [];
const warnings: string[] = [];
t.mock.method(store, "getSessionExt", (_id: string, key: string) => key === "inc:canonical-ptrs" ? ptrs : undefined);
t.mock.method(store, "setSessionExt", (_id: string, key: string, value: unknown) => {
if (key === "inc:canonical-ptrs") written.push(value);
});
t.mock.method(store, "unifiedRawMessages", (_id: string, after?: number) => {
if (after === undefined) return rows;
reads.push(after);
return [{ seq: count, content: JSON.stringify(tail), ts: null }];
});
t.mock.method(store, "afterNextFlush", async () => true);
try {
const req = Readable.from([]) as unknown as http.IncomingMessage;
Object.assign(req, {
method: "POST", url: "/v1/messages",
headers: { "x-bili-plugin": "dsh", "x-bili-plugin-conversation": "long-pointers" },
socket: { remoteAddress: "127.0.0.1", localPort: 43210 },
});
const res = {
writeHead: () => {}, setHeader: () => {}, end: () => {}, on: () => res,
} as unknown as http.ServerResponse;
let forwarded = false;
await handleIncrementalTurn(req, res, (level, message) => { if (level === "warn") warnings.push(message); }, async (_req, response) => {
forwarded = true;
response.writeHead(200);
response.end();
}, "/v1/messages", Buffer.from(JSON.stringify({
conversationId: "long-pointers", sinceSeq: count,
request: { model: "claude-test", max_tokens: 512 }, messages: [delta],
})));
assert.equal(forwarded, true);
assert.deepEqual(warnings, []);
assert.deepEqual(reads, [count - 1], "query only rows above the highest pointer, even when it is not last");
assert.deepEqual(written, [[...ptrs, count]], "the settled delta extends the persisted canonical");
} finally {
__resetIncrementalLaneForTests();
store.closeQuietly();
}
});

test("cold canonical rebuild: identical resync keeps the shared prefix; delta assembles the FULL history", async () => {
const h = await harness();
try {
Expand Down
62 changes: 62 additions & 0 deletions tests/issue6490-spread-argument-limit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// #6490: Math.min/Math.max argument spread over unbounded arrays trips V8's
// argument-spread limit (~65K args) → RangeError. The reconciliation site
// (planReconciliation) is pinned by fold-reconcile-large-history.test.ts;
// this file pins the second same-mechanism site in src/ — the incremental
// lane's canonical-pointer upkeep, which spreads the session's full pointer
// list (one entry per settled canonical message) into Math.max.
import assert from "node:assert/strict";
import test from "node:test";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import type { BiliMessage } from "acp-kernel/wire";
import { SessionStore, _setStoreForTest, _resetStoreForTest } from "../src/persist.ts";
import { recordCanonicalPointers } from "../src/incremental-lane.ts";

// Beyond V8's argument-spread limit (~125K–130K on Node 22/24 x64, lower on
// some builds): spreading this many arguments throws RangeError: Maximum call
// stack size exceeded. Same size as the fold-reconcile-large-history case.
const SPREAD_LIMIT = 150_000;

test("#6490: canonical-pointer upkeep survives a pointer list beyond the argument limit", () => {
const dir = mkdtempSync(join(tmpdir(), "bili-6490-"));
const previousMode = process.env.BILI_STORAGE_UNIFIED;
process.env.BILI_STORAGE_UNIFIED = "full";
try {
const store = new SessionStore({ dir, enabled: true });
_setStoreForTest(store);
const ptrs = Array.from({ length: SPREAD_LIMIT }, (_, i) => i);
store.setSessionExt("conv-6490", "inc:canonical-ptrs", ptrs);
const msgs: BiliMessage[] = Array.from({ length: SPREAD_LIMIT }, (_, i) => ({
id: `m${i}`,
role: "user",
contentType: "text",
text: `turn ${i}`,
}));
const warnings: string[] = [];
let seenFromSeq: number | undefined;
const orig = store.unifiedRawMessages.bind(store);
store.unifiedRawMessages = (sessionId: string, fromSeqExclusive?: number) => {
seenFromSeq = fromSeqExclusive;
return orig(sessionId, fromSeqExclusive);
};
recordCanonicalPointers("conv-6490", msgs, (level, m) => {
if (level === "warn") warnings.push(m);
});
assert.equal(
warnings.filter((w) => w.includes("Maximum call stack")).length,
0,
`no argument-limit RangeError: ${warnings.join(" | ")}`,
);
assert.equal(seenFromSeq, SPREAD_LIMIT - 1, "the upkeep reads rows after the highest pointer");
const after = store.getSessionExt("conv-6490", "inc:canonical-ptrs");
assert.ok(Array.isArray(after), "pointer list preserved");
assert.equal((after as number[]).length, SPREAD_LIMIT);
assert.equal((after as number[])[SPREAD_LIMIT - 1], SPREAD_LIMIT - 1);
} finally {
_resetStoreForTest();
if (previousMode === undefined) delete process.env.BILI_STORAGE_UNIFIED;
else process.env.BILI_STORAGE_UNIFIED = previousMode;
rmSync(dir, { recursive: true, force: true });
}
});
Loading