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
14 changes: 11 additions & 3 deletions src/session-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,15 +283,23 @@ export function dshPersonaFingerprintApplies(headers: Record<string, string | st
}

/** Persona text for the openai chat wire: the concatenation of every
* system-role message's text (string content or text parts). Empty string
* when the request carries no system message — subagentNamespace treats
* system/developer-role message's text (string content or text parts).
* #6303: pi-ai emits the system prompt as `role: "developer"` (string
* content) when the model declares reasoning and the upstream supports the
* developer role (openai-completions instructionRole), so the carrier MUST
* match openaiToCore (kernel/src/wire/openai.ts), which hoists BOTH — a
* system-only read left dsh's developer-role lanes riding the verbatim raw
* key and clobbering the main lane's fold state. Empty string when the
* request carries no system/developer message — subagentNamespace treats
* that as non-anchoring (verbatim key, no anchor claim), so system-less
* auxiliary calls keep riding the main session. */
export function openaiSystemTextForPersona(body: { messages?: unknown }): string {
const msgs = Array.isArray(body.messages) ? body.messages : [];
const texts: string[] = [];
for (const m of msgs) {
if (m === null || typeof m !== "object" || (m as { role?: unknown }).role !== "system") continue;
if (m === null || typeof m !== "object") continue;
const role = (m as { role?: unknown }).role;
if (role !== "system" && role !== "developer") continue;
const content = (m as { content?: unknown }).content;
if (typeof content === "string") {
if (content.length > 0) texts.push(content);
Expand Down
94 changes: 94 additions & 0 deletions tests/dsh-persona-fingerprint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,33 @@ test("openaiSystemTextForPersona: joins every system message's text", () => {
);
});

// #6303: pi-ai emits the system prompt as role:"developer" (string content)
// when the model declares reasoning + the upstream supports the developer
// role (openai-completions instructionRole) — the carrier MUST match
// openaiToCore (kernel/src/wire/openai.ts), which hoists system OR developer.
test("openaiSystemTextForPersona: developer-role system is the same carrier as system (#6303)", () => {
assert.equal(
openaiSystemTextForPersona({ messages: [{ role: "developer", content: "A" }, { role: "user", content: "u" }, { role: "developer", content: "B" }] }),
"A\n\nB",
"developer-role string content is read",
);
assert.equal(
openaiSystemTextForPersona({ messages: [{ role: "developer", content: [{ type: "text", text: "part-1" }, { type: "text", text: "part-2" }] }] }),
"part-1\n\npart-2",
"developer-role text parts are joined",
);
assert.equal(
openaiSystemTextForPersona({ messages: [{ role: "developer", content: "" }] }),
"",
"empty developer content contributes nothing",
);
assert.equal(
openaiSystemTextForPersona({ messages: [{ role: "user", content: "hi" }] }),
"",
"no system/developer message still yields empty (non-anchoring)",
);
});

type Rig = { proxyPort: number; upstreamPort: number; proxy: http.Server; upstream: http.Server };

type UpstreamHandler = (req: http.IncomingMessage, res: http.ServerResponse) => void;
Expand Down Expand Up @@ -225,6 +252,73 @@ test("e2e openai lane #4438: review persona rides the stateless side passthrough
}
});

// #6303: dsh's pi-ai lane emits BOTH the main agent and the auto-reviewer
// system as role:"developer" (string content) when the model declares
// reasoning + the upstream supports the developer role. Pre-fix, the persona
// extractor read only role:"system", so personaSystemText was "" for BOTH
// lanes, the gate at handle.ts:1410 was false, and both silently rode the
// verbatim raw key — the reviewer's divergent all-user history clobbered the
// main lane's fold state (30 blocks coverage-lost) and, after a client abort
// mid-compression, `compress` resolved against the side lane and died on
// gate:cannot-anchor (refs DEAD, 0 active blocks). Post-fix the extractor
// matches openaiToCore's carrier, the gate fires, and the reviewer forks onto
// its own |sub:<fp> lane.
test("e2e openai lane #6303: developer-role systems — main lane keeps the raw key, the no-tools reviewer lane forks to |sub:", async () => {
const rig = await startRig();
const CONV = "dshp-developer";
try {
const url = `http://127.0.0.1:${rig.proxyPort}/bili/http://127.0.0.1:${rig.upstreamPort}/v1/chat/completions`;
const headers: Record<string, string> = { "content-type": "application/json", "x-bili-plugin": "dsh", "x-bili-plugin-conversation": CONV };
const tools = [{ type: "function", function: { name: "bash", description: "run", parameters: { type: "object", properties: {} } } }];
const DEV_MAIN = "You are DeepSeek Harness, the main coding agent.\nWorkspace: /tmp.";
const DEV_REVIEW = "You are the final authorization reviewer for exactly one pending tool call.\nAnswer with one word.";

// Phase 1 — main lane: developer-role system + tools + assistant turns
// claims the raw key (anchor).
const mainMsgsDev = [
{ role: "user", content: "main-1-" + "z".repeat(6000) },
{ role: "assistant", content: "a-1-" + "z".repeat(6000) },
{ role: "user", content: "main-2-" + "z".repeat(6000) },
{ role: "assistant", content: "a-2-" + "z".repeat(6000) },
{ role: "user", content: "main-3-" + "z".repeat(6000) },
];
const r1 = await fetch(url, {
method: "POST",
headers,
body: JSON.stringify({ model: MODEL, max_tokens: 64_000, tools, messages: [{ role: "developer", content: DEV_MAIN }, ...mainMsgsDev] }),
});
assert.equal(r1.status, 200);
await r1.text();
const main = getSession(CONV);
assert.ok(main, "main session exists under the raw conversation key");
assert.ok(Object.keys(main.state.messageRefs.byRaw).length >= 5, "refs assigned to the main history");
const mainRefs = Object.keys(main.state.messageRefs.byRaw).length;

// Phase 2 — reviewer lane: developer-role system (DIFFERENT), no tools,
// all-user text msgs, no assistant turns, under the SAME conversation id.
// Must fork onto |sub:<fp> — NOT clobber the raw key.
const reviewMsgsDev = [
{ role: "user", content: "ENVIRONMENT {\"cwd\": \"/tmp\"}" },
{ role: "user", content: "user: build it\nassistant: ok" },
{ role: "user", content: "user: build it again\nassistant: done" },
{ role: "user", content: "Decide: risky?" },
];
const r2 = await fetch(url, {
method: "POST",
headers,
body: JSON.stringify({ model: MODEL, max_tokens: 384_000, messages: [{ role: "developer", content: DEV_REVIEW }, ...reviewMsgsDev] }),
});
assert.equal(r2.status, 200);
await r2.text();
const fork = forkKey(CONV, DEV_REVIEW);
assert.ok(peekSession(fork), "reviewer lane (developer-role system) forked onto |sub:<fp>");
assert.equal(getSession(CONV)?.stats.requests, 1, "main session request count unchanged by the reviewer");
assert.equal(Object.keys(getSession(CONV).state.messageRefs.byRaw).length, mainRefs, "main refs intact — the reviewer did NOT clobber the raw key");
} finally {
await closeRig(rig);
}
});

test("e2e anthropic lane #4438: the same stateless passthrough applies to dsh-over-anthropic", async () => {
const rig = await startRig();
const CONV = "dshp-anthropic";
Expand Down
Loading