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
9 changes: 7 additions & 2 deletions .github/workflows/ci-e2e-dsh-native.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ jobs:
# bundle API surface must not drift under this suite. Bump deliberately
# after re-running the suite locally on the new version.
- name: Install dsh CLI
run: npm i -g @deepseek-ai/dsh@0.1.5-rc.3
# 0.2.0-rc.2: the persona lane needs `--session-id` (session adoption
# across runs — the #2241 model-switch shape). The base lane below was
# verified against this version too.
run: npm i -g @deepseek-ai/dsh@0.2.0-rc.2

- name: Print dsh version
run: dsh --version
Expand All @@ -39,7 +42,9 @@ jobs:
- name: Run real-dsh native-lane compression E2E
env:
E2E_DSH_BIN: dsh
run: ACP_TEST_E2E_DSH_NATIVE=1 node --import tsx --test tests/e2e/e2e-native-dsh.test.ts
run: |
ACP_TEST_E2E_DSH_NATIVE=1 node --import tsx --test tests/e2e/e2e-native-dsh.test.ts
ACP_TEST_E2E_DSH_NATIVE=1 node --import tsx --test tests/e2e/e2e-native-dsh-persona.test.ts

- name: Collect E2E artifacts
if: failure()
Expand Down
104 changes: 104 additions & 0 deletions src/persona-anchor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// #2241: continuity-aware persona anchor for id-sharing dsh traffic.
//
// The original dsh persona fingerprint (db21309d, #1916/#1307/#1314) keyed
// dsh traffic by id + system-text hash with kernel subagentNamespace
// first-seen anchor semantics: the FIRST system seen under the id keeps the
// raw key, any different system forks onto `<id>|sub:<fp>`. That was built
// on the assumption "dsh composes its main system once per session, so main
// turns never fork mid-conversation" — validated for the volatile
// time-context (which dsh puts in a USER message) but broken by `/model`:
// dsh rebuilds the main system per model, so every model switch forked the
// MAIN lane off the raw key. The host only ever stamps the bare id, so
// compression tool calls kept hitting the frozen husk on the raw key while
// the live conversation rode a `|sub:` session with zero compression state
// (#2241: 266,966 pendingT1 tokens, 0 folds, 38 minutes).
//
// This module keeps the review isolation intact but changes the fork
// TRIGGER from "system text changed" to "system text changed AND the
// incoming history does not continue the raw key's chain":
//
// - model switch (same conversation, new system) — the request replays
// the full history plus new turns, so it BYTE-EXACTLY CONTINUES the
// raw key's prefix-affinity chain (#1106/#1148 identity semantics:
// content is identity, system drift is evolution). The anchor
// MIGRATES to the new fingerprint and the conversation keeps its
// compression blocks, refs, and ledger on the raw key.
// - auto-review classifyRisk (fresh single-message blob under a fixed
// REVIEW_POLICY system) — its message list does not continue any
// tracked chain, so it still forks onto `<id>|sub:<fp>` exactly as
// before, sharing ONE forked session across successive reviews.
//
// The fork id format `<rawId>|sub:<sha256(system)[0..16]>` is unchanged, so
// downstream consumers (the #2244 personaForkTarget routing, the #2170
// designNamespaced canary) keep working. Empty system text stays
// non-anchoring (kernel semantics): system-less auxiliary calls keep riding
// the main session verbatim.
//
// Anchors are process memory, like the kernel anchor map they replace — no
// persistence contract changes. After a proxy restart the first arrival
// claims the raw anchor; if a review claims it, the next main turn's
// continuity check runs against the raw key's PERSISTED chain (#1724
// permanent chains) and migrates the anchor back, so the restart race
// self-heals on the very next main turn.

import { createHash } from "node:crypto";
import { prefixAffinity } from "./prefix-affinity.js";

const anchors = new Map<string, string>();

function personaFingerprint(systemText: string): string {
return createHash("sha256").update(systemText, "utf8").digest("hex").slice(0, 16);
}

export type PersonaAnchorAction = "claim" | "match" | "migrate" | "fork" | "passthrough";

export interface PersonaKeyResolution {
key: string;
action: PersonaAnchorAction;
}

/**
* Resolve the session key for a dsh persona-fingerprinted request.
* `messages` is the protocol's conversation message list (the same list
* prefix-affinity hashes — top-level system fields are not part of it).
*/
export function resolvePersonaKey(rawId: string, systemText: string, messages: unknown[]): PersonaKeyResolution {
if (!systemText) return { key: rawId, action: "passthrough" };
const fp = personaFingerprint(systemText);
const anchored = anchors.get(rawId);
if (anchored === undefined) {
anchors.set(rawId, fp);
return { key: rawId, action: "claim" };
}
if (anchored === fp) return { key: rawId, action: "match" };
if (prefixAffinity.chainContinues(messages, rawId)) {
anchors.set(rawId, fp);
return { key: rawId, action: "migrate" };
}
return { key: `${rawId}|sub:${fp}`, action: "fork" };
}

/** server.ts wrapper: resolves the key and surfaces anchor migrations in
* the log (forks stay silent — they are the designed review path). */
export function dshPersonaNamespace(
rawId: string,
systemText: string,
messages: unknown,
log: (level: "info" | "warn" | "error", message: string) => void,
): string {
const list = Array.isArray(messages) ? messages : [];
const res = resolvePersonaKey(rawId, systemText, list);
if (res.action === "migrate") {
log("info", `[persona] system text changed mid-conversation but the history continues ${rawId}'s chain — anchor migrated, compression state stays on the raw key (#2241)`);
}
return res.key;
}

/** Current anchor fingerprint for a raw id (test/diagnostic surface). */
export function personaAnchorOf(rawId: string): string | undefined {
return anchors.get(rawId);
}

export function resetPersonaAnchorsForTest(): void {
anchors.clear();
}
19 changes: 19 additions & 0 deletions src/prefix-affinity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,25 @@ export class PrefixAffinityResolver {
return best ? { sessionId: best.sessionId, sharedDepth: best.sharedDepth } : null;
}

/** #2241: does `messages` continue the tracked chain of `sessionId`?
* True when the session's stored chain is a byte-exact head-anchored
* prefix of the incoming list — the progressive hash at the stored
* depth equals the stored tail hash (equal depth = byte-exact replay,
* which also counts). The building block for the dsh persona anchor
* migration: a system-text change whose history continues the raw
* key's chain is the SAME conversation (model switch), not a new
* persona. Cryptographic equality of the whole prefix is the
* discriminator — no depth floor beyond chainHashes' MIN_CANONICAL_BYTES. */
chainContinues(messages: unknown[], sessionId: string): boolean {
const entry = this.trackedChains.get(sessionId);
if (!entry) return false;
const msgs = normalizeAffinityMessages(messages);
if (!hasUserMessage(msgs)) return false;
const hashes = chainHashes(msgs);
if (hashes.length === 0 || msgs.length < entry.depth) return false;
return hashes[entry.depth - 1] === entry.tailHash;
}

/** Drop tracking for a removed session. */
forget(sessionId: string): void {
this.trackedChains.delete(sessionId);
Expand Down
20 changes: 18 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ import { rewriteResponsesJsonResponse } from "./stream-responses.js";
import { observeResponsesTerminalState } from "./stream-terminal.js";
import { emitPreflightError, emitStreamError } from "./stream-error.js";
import { affinityToken, claudeSubagentAgentId, claudeSubagentSplit, clientConversationHeader, codexTurnIdentity, conversationHeaderSource, dshPersonaFingerprintApplies, instructionsFingerprintApplies, openaiSystemTextForPersona, preferPromptCacheKeyIdentity, shouldStampRelayAffinityPck, type ConversationIdentity } from "./session-id.js";
import { dshPersonaNamespace } from "./persona-anchor.js";
import { prefixAffinity, type AnonymousAffinity } from "./prefix-affinity.js";
import { maybeAdoptForkBlocks, maybeAdoptResume } from "./fork-adoption.js";
import { publicForkInputMatches } from "./plugin.js";
Expand Down Expand Up @@ -2269,11 +2270,26 @@ async function handle(
(claudeSub !== undefined && opts.subagentSplit !== false
? claudeSubagentSplit(anthropicIdentity?.value ?? anthropicSignal, req.headers, systemTextsForSplit)
: dshPersona && !sideRequestLike
? subagentNamespace(anthropicIdentity?.value ?? anthropicSignal, personaSystemText)
// #2241: continuity-aware anchor — a system change whose
// history continues the raw key's prefix-affinity chain
// (model switch) MIGRATES the anchor instead of forking
// the main lane off the raw key; only history-discontinuous
// requests (review blobs) still fork onto `|sub:<fp>`.
? dshPersonaNamespace(
anthropicIdentity?.value ?? anthropicSignal,
personaSystemText,
(parsed as AnthropicRequestBody).messages,
log,
)
: anthropicIdentity?.value ?? anthropicSignal)
: protocol === "openai"
? (dshPersona && !sideRequestLike
? subagentNamespace(openaiIdentity?.value ?? openaiSignal, personaSystemText)
? dshPersonaNamespace(
openaiIdentity?.value ?? openaiSignal,
personaSystemText,
(parsed as OpenAIRequestBody).messages,
log,
)
: openaiIdentity?.value ?? openaiSignal)
: codexTurn
// Trusted Codex turn id enters the verbatim session chain
Expand Down
Loading
Loading