Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
3be4ed0
perf: bound the initial context profile sample
Afloat16 Nov 14, 2026
8637f13
fix(affinity): preserve parallel tool rounds across wire formats
Afloat16 Nov 14, 2026
94ec3dd
perf(storage): build bounded boot file indexes incrementally
Afloat16 Nov 14, 2026
d672592
perf: deduplicate captured subagent sessions with a set
Afloat16 Nov 14, 2026
dadbcd3
fix: parse buffered SSE fields without trimming boundaries
Afloat16 Nov 14, 2026
99cf108
fix: filter long message histories without argument overflow
Afloat16 Nov 14, 2026
3deaff5
perf: count preflight covered message characters in linear time
Afloat16 Nov 14, 2026
d55be36
fix: retain recent tool-use routing witnesses across clock changes
Afloat16 Nov 14, 2026
5a116b3
perf: reuse the fork prefix index when inheriting ref state
Afloat16 Nov 14, 2026
8b72a0d
Index direct block IDs when preparing external summaries
Afloat16 Nov 14, 2026
0eb56ed
Keep cache size-bucket reports within the argument limit
Afloat16 Nov 14, 2026
89ad752
fix: locate root message arrays in cache-diff attribution
Afloat16 Nov 14, 2026
a2a68ed
fix: keep Markdown inline code literal in handoff rendering
Afloat16 Nov 14, 2026
0eb1b13
Keep literal greater-than entities out of blockquote parsing
Afloat16 Nov 14, 2026
18fe636
fix: recognize escaped JSON image markers
Afloat16 Nov 14, 2026
bf5592c
perf: rewrite fold coverage without repeated scans
Afloat16 Nov 14, 2026
d7e316a
fix: match equivalent IPv6 proxy bypass literals
Afloat16 Nov 14, 2026
5304be7
Merge Afloat16/2026-11-14_buffered-sse-field-semantics (PR #6654) int…
ranxianglei Nov 14, 2026
a68c2cb
Merge Afloat16/2026-11-14_bounded-context-profile (PR #6656) into bat…
ranxianglei Nov 14, 2026
d9f5f7b
Merge Afloat16/2026-11-14_parallel-tool-affinity (PR #6660) into batc…
ranxianglei Nov 14, 2026
76640c7
Merge Afloat16/2026-11-14_linear-boot-file-index (PR #6663) into batc…
ranxianglei Nov 14, 2026
33c2f61
Merge Afloat16/2026-11-14_linear-subagent-capture (PR #6668) into bat…
ranxianglei Nov 14, 2026
18d2df5
Merge Afloat16/2026-11-14_bounded-history-filter (PR #6670) into batc…
ranxianglei Nov 14, 2026
c2fbf10
Merge Afloat16/2026-11-14_linear-preflight-accounting (PR #6674) into…
ranxianglei Nov 14, 2026
4fdf392
Merge Afloat16/2026-11-14_tool-witness-recency (PR #6679) into batch6…
ranxianglei Nov 14, 2026
0484d98
Merge Afloat16/2026-11-14_linear-fork-ref-membership (PR #6683) into …
ranxianglei Nov 14, 2026
a2fae88
Merge Afloat16/2026-11-14_linear-external-summary-references (PR #668…
ranxianglei Nov 14, 2026
c781b66
Merge Afloat16/2026-11-14_unbounded-cache-size-buckets (PR #6689) int…
ranxianglei Nov 14, 2026
cb1edd0
Merge Afloat16/2026-11-14_cache-diff-root-array (PR #6691) into batch…
ranxianglei Nov 14, 2026
ac36b09
Merge Afloat16/2026-11-14_literal-inline-code (PR #6696) into batch6-…
ranxianglei Nov 14, 2026
d31c58c
Merge Afloat16/2026-11-14_literal-blockquote-entities (PR #6698) into…
ranxianglei Nov 14, 2026
20289ce
Merge Afloat16/2026-11-14_escaped-image-markers (PR #6701) into batch…
ranxianglei Nov 14, 2026
fa0ed27
Merge Afloat16/2026-11-14_linear-fold-rewrites (PR #6705) into batch6…
ranxianglei Nov 14, 2026
d64086b
Merge Afloat16/2026-11-14_ipv6-bypass-literals (PR #6708) into batch6…
ranxianglei 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
72 changes: 72 additions & 0 deletions scripts/bench-bounded-boot-index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Run with: node --max-old-space-size=512 --expose-gc --import tsx scripts/bench-bounded-boot-index.mjs [files-per-root]
import assert from "node:assert/strict";
import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import { performance } from "node:perf_hooks";

const count = Number(process.argv[2] ?? 10_000);
if (!Number.isSafeInteger(count) || count < 1) throw new Error("files-per-root must be a positive safe integer");
const root = mkdtempSync(path.join(tmpdir(), "bili-boot-index-bench-"));
const dir = path.join(root, "sessions");
let store;
try {
process.env.BILI_SESSIONS_DIR = dir;
process.env.BILI_STORAGE_UNIFIED = "full";
process.env.BILI_STORAGE_BACKEND = "sqlite";
process.env.BILI_PERSIST_ZSTD = "0";
delete process.env.BILI_CONFIG_FILE;
delete process.env.BILI_ENCRYPTION_KEY;
for (const kind of ["DATA", "STATE", "CONFIG", "CACHE"]) {
process.env[`XDG_${kind}_HOME`] = path.join(root, kind.toLowerCase());
mkdirSync(process.env[`XDG_${kind}_HOME`], { recursive: true });
}
const { SessionStore, relPathFor } = await import("../src/persist.ts");
for (const [kind, base] of [["hot", dir], ["cold", `${dir}-cold`]]) {
mkdirSync(path.join(base, "_unknown"), { recursive: true });
for (let i = 0; i < count; i++) {
const id = `bench-${kind}-${i}`;
const payload = {
version: 3, savedAt: 1000 + i, id, meta: { label: id },
state: { blocks: [], messageRefs: { byRaw: {}, byRef: {} } },
blockContents: { b1: { one: null, full: { text: `original-${id}`, count: 1 } } },
stats: { requests: i + 1 }, createdAt: 1000 + i,
};
writeFileSync(path.join(base, relPathFor(id)), JSON.stringify({ version: 3, savedAt: payload.savedAt, id, payload }));
}
writeFileSync(path.join(base, "_unknown", "ignored.content-store.json"), "{}");
}
let indexed = 0;
store = new SessionStore({ dir, enabled: true, debounceMs: 0, log: (_level, message) => {
const match = /^\[persist\] indexed (\d+) state files;/.exec(message);
if (match) indexed = Number(match[1]);
} });
assert.ok(store.replicasAvailable(), "benchmark requires the SQLite sidecar");
global.gc?.();
const start = performance.now();
const loaded = await store.boot({ maxSessions: 0, maxBytes: 64 * 1024 * 1024 });
const bootMs = performance.now() - start;
assert.equal(loaded.size, 0);
assert.equal(indexed, count * 2, "both roots must be indexed and content-store companions excluded");
await store.backgroundScanComplete();
for (const kind of ["hot", "cold"]) {
for (const i of new Set([0, count - 1])) {
const id = `bench-${kind}-${i}`;
const session = store.loadSync(id);
assert.equal(session?.stats.requests, i + 1);
assert.equal(session?.blockContents.get("b1")?.full?.text, `original-${id}`);
}
}
console.log(JSON.stringify({
files_per_root: count,
indexed_files: indexed,
warm_sessions: loaded.size,
boot_ms: bootMs,
max_rss_kib: process.resourceUsage().maxRSS,
node: process.version,
}));
} finally {
await store?.backgroundScanComplete();
store?.closeQuietly();
rmSync(root, { recursive: true, force: true });
}
76 changes: 76 additions & 0 deletions scripts/bench-fork-ref-membership.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Public snapshot/fork handlers with deterministic in-memory parent state; no upstream model call.
import assert from "node:assert/strict";
import { createHash } from "node:crypto";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { performance } from "node:perf_hooks";
import { handlePluginFork, handlePluginSnapshot, _resetPluginStateForTest } from "../src/plugin.ts";
import { getSession, _resetSessionsForTest } from "../src/session.ts";
import { SessionStore, _setStoreForTest } from "../src/persist.ts";

const count = Number(process.argv[2] ?? 12000);
assert(Number.isSafeInteger(count) && count >= 4);
const digest = (value) => createHash("sha256").update(JSON.stringify(value)).digest("hex");

async function response(run) {
let status;
let body;
await run({ writeHead(value) { status = value; }, end(value) { body = JSON.parse(value); } });
assert(status >= 200 && status < 300, JSON.stringify({ status, body }));
return body;
}

const timings = [];
const outputs = new Set();
for (let run = 0; run < 4; run++) {
const dir = mkdtempSync(join(tmpdir(), "bili-fork-ref-bench-"));
const store = new SessionStore({ dir, enabled: false });
_setStoreForTest(store);
_resetSessionsForTest();
_resetPluginStateForTest();
try {
const parent = getSession("bench-parent");
const refs = Array.from({ length: count }, (_, i) => `m${String(i + 1).padStart(5, "0")}`);
parent.pluginSnapshot = refs.map((ref, i) => {
const id = `source-${i}`;
parent.state.messageRefs.byRaw[id] = ref;
parent.state.messageRefs.byRef[ref] = id;
return { id, role: i % 2 ? "assistant" : "user", contentType: "text", text: `source text ${i}` };
});
parent.state.lastPassIds = parent.pluginSnapshot.map((message) => message.id);
parent.state.hiddenOrphanRefs = [...refs];
parent.state.emergencyTruncateLatch = refs.map((ref) => ({ ref, text: `clipped ${ref}` }));
parent.state.renderDeferredRefs = [...refs];
parent.state.deadRefs = [...refs];
const snapshot = await response((res) => handlePluginSnapshot(parent.id, res));
const orderedMessages = snapshot.orderedMessages.slice(0, Math.floor(count / 2));
const payload = JSON.stringify({
protocolVersion: 2, parentConversationId: parent.id, childConversationId: "bench-child",
parentRevision: snapshot.parentRevision,
branchPoint: { messageCount: orderedMessages.length, orderHash: digest(orderedMessages) },
orderedMessages, idempotencyKey: "bench-fork",
});
const started = performance.now();
await response((res) => handlePluginFork(payload, res));
const elapsed = performance.now() - started;
const child = getSession("bench-child");
const state = child.state;
outputs.add(digest({
hidden: state.hiddenOrphanRefs, clipped: state.emergencyTruncateLatch,
deferred: state.renderDeferredRefs, dead: state.deadRefs, messages: child.pluginSnapshot,
}));
assert.equal(state.hiddenOrphanRefs.length, orderedMessages.length);
assert.equal(state.deadRefs.length, count - orderedMessages.length);
if (run > 0) timings.push(elapsed);
} finally {
store.cancelAll();
store.closeQuietly();
_resetSessionsForTest();
_resetPluginStateForTest();
rmSync(dir, { recursive: true, force: true });
}
}
assert.equal(outputs.size, 1);
timings.sort((a, b) => a - b);
console.log(JSON.stringify({ count, prefix: Math.floor(count / 2), medianMs: timings[1], measuredMs: timings, sha256: [...outputs][0] }));
22 changes: 22 additions & 0 deletions scripts/bench-subagent-capture.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import assert from "node:assert/strict";
import { performance } from "node:perf_hooks";
import { captureSubagentSessionIds, subagentSessionsOf, syncSubagentSessions } from "../src/subagent-sessions.ts";

const count = Number(process.argv[2] ?? 20_000);
if (!Number.isSafeInteger(count) || count < 1 || count > 100_000) throw new Error("Expected 1..100000 unique subagent sessions");
const expected = Array.from({ length: count }, (_, i) => `ses_${String(i).padStart(8, "0")}`);
const messages = expected.flatMap((id, i) => [
{ id: `call-${i}`, contentType: "tool-call", toolName: "task", text: JSON.stringify({ sessionId: id }) },
{ id: `result-${i}`, contentType: "tool-result", toolName: "task", text: `<task id="${id}" state="completed">\nresult` },
]);
const changed = [{ blockId: "b1", effectiveMessageIds: messages.map(m => m.id), directBlockIds: [] }];
const elapsedMs = [];
for (let run = 0; run < 3; run++) {
const session = { metadata: { pluginAgent: "opencode" }, state: { blocks: [{ blockId: "b1", active: true }] } };
const started = performance.now();
syncSubagentSessions(session, changed, messages);
elapsedMs.push(performance.now() - started);
assert.deepEqual(subagentSessionsOf(session).b1, expected.slice(0, 100));
}
assert.deepEqual(captureSubagentSessionIds(messages, new Set(changed[0].effectiveMessageIds)), expected);
console.log(JSON.stringify({ node: process.version, uniqueSessions: count, messages: messages.length, retainedIds: Math.min(count, 100), elapsedMs }, null, 2));
37 changes: 27 additions & 10 deletions src/acp-cache-diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,31 @@ interface MsgArrayInfo {
items: unknown[];
}

function rootArrayStart(text: string, key: string): number {
let depth = 0;
let found = -1;
for (let i = 0; i < text.length; i++) {
const c = text[i]!;
if (c === '"') {
const start = i++;
while (i < text.length) {
if (text[i] === "\\") { i += 2; continue; }
if (text[i] === '"') break;
i++;
}
if (depth !== 1) continue;
let value = i + 1;
while (value < text.length && " \t\n\r".includes(text[value]!)) value++;
if (text[value] !== ":" || JSON.parse(text.slice(start, i + 1)) !== key) continue;
value++;
while (value < text.length && " \t\n\r".includes(text[value]!)) value++;
found = text[value] === "[" ? value : -1;
} else if (c === "{" || c === "[") depth++;
else if (c === "}" || c === "]") depth--;
}
return found;
}

function msgArrayInfo(text: string): MsgArrayInfo | null {
let parsed: unknown;
try { parsed = JSON.parse(text); } catch { return null; }
Expand All @@ -235,16 +260,8 @@ function msgArrayInfo(text: string): MsgArrayInfo | null {
for (const key of MSG_KEYS) {
const arr = obj[key];
if (!Array.isArray(arr)) continue;
const re = new RegExp(`"${key}"\\s*:\\s*\\[`, "g");
let firstStart = -1;
let m: RegExpExecArray | null;
while ((m = re.exec(text)) !== null) {
const start = m.index + m[0].length - 1;
if (firstStart < 0) firstStart = start;
const spans = elementSpans(text, start);
if (spans.length === arr.length) return { key, spans, items: arr };
}
if (firstStart >= 0) return { key, spans: elementSpans(text, firstStart), items: arr };
const start = rootArrayStart(text, key);
if (start >= 0) return { key, spans: elementSpans(text, start), items: arr };
}
return null;
}
Expand Down
22 changes: 9 additions & 13 deletions src/affinity-neutral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ export function neutralTurns(messages: unknown[]): NeutralTurn[] {
const role = m.role === "model" ? "assistant" : typeof m.role === "string" ? m.role : "user";
const texts: string[] = [];
const calls: string[] = [];
let toolBody: string | undefined;
const toolBodies: string[] = [];
for (const part of m.parts) {
if (!part || typeof part !== "object") continue;
const p = part as { text?: unknown; functionCall?: unknown; functionResponse?: unknown };
Expand All @@ -253,11 +253,11 @@ export function neutralTurns(messages: unknown[]): NeutralTurn[] {
// gemini clients wrap the tool body under an arbitrary key
// ({output:…}/{result:…}/{content:…}); a single-string-valued
// object IS the body — unwrap so it equals the openai render.
if (typeof resp === "string") toolBody = resp;
if (typeof resp === "string") toolBodies.push(resp);
else if (resp && typeof resp === "object" && !Array.isArray(resp)) {
const entries = Object.entries(resp as Record<string, unknown>);
toolBody = entries.length === 1 && typeof entries[0]![1] === "string" ? entries[0]![1]! : canon(resp);
} else toolBody = canon(resp ?? p.functionResponse);
toolBodies.push(entries.length === 1 && typeof entries[0]![1] === "string" ? entries[0]![1]! : canon(resp));
} else toolBodies.push(canon(resp ?? p.functionResponse));
}
}
// model turns MERGE into the pending assistant run — gemini splits
Expand All @@ -273,8 +273,8 @@ export function neutralTurns(messages: unknown[]): NeutralTurn[] {
continue;
}
flushAssistant();
if (toolBody !== undefined) turns.push({ kind: "tool", text: toolBody, frags: [fragmentHash("tool", toolBody, [])] });
if (calls.length > 0 && toolBody === undefined) turns.push({ kind: "assistant", calls, frags: [fragmentHash("assistant", "", calls)] });
for (const toolBody of toolBodies) turns.push({ kind: "tool", text: toolBody, frags: [fragmentHash("tool", toolBody, [])] });
if (calls.length > 0 && toolBodies.length === 0) turns.push({ kind: "assistant", calls, frags: [fragmentHash("assistant", "", calls)] });
else if (texts.length > 0) pushTextTurn(role, texts.join(""), [fragmentHash(role, texts.join(""), [])]);
continue;
}
Expand All @@ -285,13 +285,9 @@ export function neutralTurns(messages: unknown[]): NeutralTurn[] {
const call = `${m.name}\u0000${canonicalCallArgs(m.arguments)}`;
// codex splits assistant commentary and the tool call across items —
// merge into the pending run so it reduces like the openai render.
if (asst !== null) {
asst.calls = [...(asst.calls ?? []), call];
asstFrags.push(fragmentHash("assistant", "", [call]));
continue;
}
flushAssistant();
turns.push({ kind: "assistant", calls: [call], frags: [fragmentHash("assistant", "", [call])] });
asst = asst ?? { kind: "assistant", text: "", calls: [] };
asst.calls = [...(asst.calls ?? []), call];
asstFrags.push(fragmentHash("assistant", "", [call]));
continue;
}
if (typeof m.type === "string" && (m.type === "function_call_output" || m.type === "local_shell_call_output")) {
Expand Down
12 changes: 7 additions & 5 deletions src/agent/opencode-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,11 +567,13 @@ export function createOpencodeV2Setup(options: OpencodeV2SetupOptions = {}): (ct
if (ctx.session?.hook) {
for (const name of ["context", "compaction", "title", "generate"]) {
const reg = await ctx.session.hook(name, (event) => {
if (!event.messages) return;
const kept = event.messages.filter((message) => !(
message.role === "user" && BILI_REPORT_ID_RE.test(message.id ?? "")
));
event.messages.splice(0, event.messages.length, ...kept);
const messages = event.messages;
if (!messages) return;
let kept = 0;
messages.forEach((message) => {
if (message.role !== "user" || !BILI_REPORT_ID_RE.test(message.id ?? "")) messages[kept++] = message;
});
messages.length = kept;
});
if (reg) registrations.push(reg);
}
Expand Down
2 changes: 1 addition & 1 deletion src/cache-ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2276,7 +2276,7 @@ export function buildSessionCacheReport(session: Session): BiliCacheReport {
else byLo.set(lo, [l.hitPct]);
}
for (const [lo, hits] of [...byLo.entries()].sort((x, y) => x[0] - y[0]).slice(0, 24)) {
sizeBuckets.push({ lo, hi: lo + SIZE_BUCKET_TOK, n: hits.length, hitMedian: median(hits) ?? 0, hitMin: Math.min(...hits) });
sizeBuckets.push({ lo, hi: lo + SIZE_BUCKET_TOK, n: hits.length, hitMedian: median(hits) ?? 0, hitMin: hits.reduce((min, hit) => Math.min(min, hit), Infinity) });
}
}
const lowGaps: number[] = [];
Expand Down
14 changes: 11 additions & 3 deletions src/context-analysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,23 @@ function snapshotInit(messages: CoreMessage[]): InitProfile {
totalChars += chars;
const tok = countMessageTokens(m, defaultCountTokens);
estTokens += tok;
entries.push({ role: m.role, contentType: m.contentType, toolName: m.toolName, estTokens: tok });
if (entries.length === INIT_PER_MSG_CAP && tok <= entries[entries.length - 1]!.estTokens) continue;
let lo = 0;
let hi = entries.length;
while (lo < hi) {
const mid = (lo + hi) >>> 1;
if (tok > entries[mid]!.estTokens) hi = mid;
else lo = mid + 1;
}
entries.splice(lo, 0, { role: m.role, contentType: m.contentType, toolName: m.toolName, estTokens: tok });
if (entries.length > INIT_PER_MSG_CAP) entries.pop();
}
entries.sort((x, y) => y.estTokens - x.estTokens);
return {
ts: Date.now(),
messageCount: messages.length,
totalChars,
estTokens,
perMessage: entries.slice(0, INIT_PER_MSG_CAP),
perMessage: entries,
};
}

Expand Down
3 changes: 2 additions & 1 deletion src/external-summary-compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,12 @@ export async function applyConfiguredCompression(input: unknown, ctx: RewriteCtx
}).join("\n\n");
if (!content.trim()) continue;
const covered = new Set(blocks.flatMap((block) => block.effectiveMessageIds));
const directBlockIds = new Set(blocks.flatMap((block) => block.directBlockIds));
const reference = JSON.stringify({
submittedSummaryHint: range.summary,
currentContext: messages.filter((message, index) => !covered.has(message.id)
&& (message.role === "user" || index >= messages.length - 5)).map(({ role, text }) => ({ role, text })),
otherSummaries: state.blocks.filter((candidate) => candidate.active && !blocks.some((block) => block.directBlockIds.includes(candidate.blockId)))
otherSummaries: state.blocks.filter((candidate) => candidate.active && !directBlockIds.has(candidate.blockId))
.map(({ topic, summary }) => ({ topic, summary })),
});
valid.push(range);
Expand Down
Loading
Loading