Skip to content
Merged
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
6 changes: 4 additions & 2 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1012,12 +1012,14 @@ function forkSnapshot(session: Session) {
const indexedHashes = new Set(Object.values(store.byRef).map((entry) => entry.hash));
if (Object.entries(store.byHash).some(([hash, text]) => !indexedHashes.has(hash) || typeof text !== "string" || createHash("sha256").update(text, "utf8").digest("hex") !== hash)) throw new Error("CCR original payload/index inconsistent");
if (Object.entries(store.byRef).some(([ref, entry]) => typeof store.byHash[entry.hash] !== "string" || session.state.messageRefs.byRaw[entry.rawId] !== ref || session.state.messageRefs.byRef[ref] !== entry.rawId)) throw new Error("CCR original alias inconsistent");
const orderedMessages = messages.map((m): ForkIdentity => {
const orderedMessages = messages.map((m, i): ForkIdentity => {
const ref = session.state.messageRefs.byRaw[m.id];
// #2620: protected tool calls/results map to the kernel BLOCKED sentinel by design
// (kernel assignRefs) — no numeric ref exists and byRef never holds it, so the
// bidirectional check applies only to numeric refs; genuine corruption still fails.
if (!ref || (ref !== BLOCKED_REF && session.state.messageRefs.byRef[ref] !== m.id)) throw new Error("raw/ref mapping inconsistent");
// #2676: name the offending message in the error — a bare invariant string gave no
// clue which of hundreds of snapshot entries broke the mapping.
if (!ref || (ref !== BLOCKED_REF && session.state.messageRefs.byRef[ref] !== m.id)) throw new Error(`raw/ref mapping inconsistent (msg ${i + 1}/${messages.length} role=${m.role} tool=${m.toolName ?? "-"} ref=${ref ?? "MISSING"} rawId=${m.id.slice(0, 16)})`);
const entry = store.byRef[ref];
const placeholder = m.text ? parseStoredPlaceholder(m.text) : null;
if ((entry && (entry.rawId !== m.id || typeof store.byHash[entry.hash] !== "string")) || (placeholder && (placeholder.ref !== ref || !entry))) throw new Error("CCR original unavailable or alias inconsistent");
Expand Down
36 changes: 34 additions & 2 deletions tests/plugin-fork.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ test("HTTP exposes a versioned snapshot and forks an exact ordered prefix", asyn
} finally { await h.close(); }
});

async function sendIntentModel(h: Awaited<ReturnType<typeof harness>>, conversationId: string, messages: unknown[], tools: unknown[], requestAgent?: string, maxTokens = 256, announcePlugin = true) {
async function sendIntentModel(h: Awaited<ReturnType<typeof harness>>, conversationId: string, messages: unknown[], tools: unknown[], requestAgent?: string, maxTokens = 256, announcePlugin = true, pluginName = "ekko-agent") {
const headers: Record<string, string> = { "content-type": "application/json", "x-acp-session": conversationId };
if (announcePlugin) {
headers["x-bili-plugin"] = "ekko-agent";
headers["x-bili-plugin"] = pluginName;
headers["x-bili-plugin-conversation"] = conversationId;
}
if (requestAgent !== undefined) headers["x-bili-plugin-agent"] = requestAgent;
Expand Down Expand Up @@ -281,6 +281,38 @@ test("HTTP protected tools keep plain-text snapshots, forks and revisions usable
} finally { await h.close(); }
});

test("HTTP dsh-lane durable guards keep plain-text snapshots, forks and revisions usable (#2676)", async () => {
const h = await harness(false, false);
try {
const history = [
{ role: "user", content: "Synthetic snapshot test" },
{ role: "user", content: `Instructions from: /repo/AGENTS.md\n# repo rules\nbehave` },
{ role: "assistant", content: "Starting" },
...Array.from({ length: 4 }, (_, i) => ({ role: i % 2 ? "assistant" : "user", content: `tail ${i}` })),
];
await sendIntentModel(h, "parent", history, [], "main", 256, true, "dsh");
const snapshot = await h.request("/__bili/plugin/snapshot?conversationId=parent");
assert.equal(snapshot.status, 200, JSON.stringify(snapshot.body));
const refs = snapshot.body.orderedMessages.map((m) => m.ref);
assert.equal(refs.length, history.length);
assert.ok(/^m\d{5,}$/.test(refs[0]), JSON.stringify(refs));
// dsh durable-guard lane (#2447) pins this carrier; the kernel BLOCKED
// sentinel addresses it internally and the wire renders null (#2620 final contract).
assert.equal(refs[1], null);
assert.ok(!refs.includes("BLOCKED"), JSON.stringify(refs));
assert.ok(refs.slice(2).every((ref) => /^m\d{5,}$/.test(ref)), JSON.stringify(refs));
const fork = await h.request("/__bili/plugin/fork", forkRequest(snapshot.body, "child", history.length));
assert.equal(fork.status, 201, JSON.stringify(fork.body));
const childSnap = (await h.request("/__bili/plugin/snapshot?conversationId=child")).body;
assert.deepEqual(childSnap.orderedMessages, snapshot.body.orderedMessages);
const childSession = resolveConversation("child").session!;
assert.equal(childSession.state.messageRefs.byRef["BLOCKED"], undefined);
assert.ok(Object.values(childSession.state.messageRefs.byRaw).includes("BLOCKED"));
await sendIntentModel(h, "child", [...history, { role: "assistant", content: "child continuation" }], [], "main", 256, true, "dsh");
assert.ok((await h.request("/__bili/plugin/status?conversationId=parent")).body.sessionRevision);
} finally { await h.close(); }
});

test("HTTP snapshot still rejects a genuinely inconsistent raw/ref mapping (#2620)", async () => {
const h = await harness();
try {
Expand Down
Loading