Skip to content

session: key SSM sessions by client conversation id / first user turn (#209) - #257

Merged
AzeezIsh merged 1 commit into
Atlas-Inf:mainfrom
maplepaladin73:fix/209-session-key
Oct 30, 2026
Merged

AzeezIsh merged 1 commit into
Atlas-Inf:mainfrom
maplepaladin73:fix/209-session-key

Conversation

@maplepaladin73

@maplepaladin73 maplepaladin73 commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #209 (design agreed with Azeez in the issue thread).

Problem

compute_session_hash keyed SSM snapshot sessions on the first 1024 prompt tokens. Agent clients (Claude Code, OpenCode, OpenClaw) send a shared system prompt + tool block far longer than 1024 tokens, so every conversation from the same agent collapsed onto one session hash.

Design

Session key, highest priority first (new session_key.rs, computed on rank 0 in api/chat/inner.rs):

  1. Client conversation id: headers x-session-id, x-session-affinity, x-conversation-id (first non-blank); Responses conversation; OpenAI prompt_cache_key (chat + Responses); metadata.session_id / metadata.conversation_id; Anthropic metadata.user_id (Claude Code puts its session id there).
  2. OpenAI user is only used combined with the first user turn (on its own it would merge all of one end user's conversations).
  3. first_user_turn: hash of all messages through the end of the first user message (past the shared system+tools block), computed from the client messages before server-side tool-prompt injection (no second render/tokenize on 17k+ prompts). Content past 1 MiB ignored.
  4. prefix1024: the existing first-1024-token hash (no user message; /v1/completions is unchanged).

Ids are hashed with a domain tag through fixed-key DefaultHasher::new() (deterministic, pinned by a test); 0 is mapped to 1 because 0 means "untracked". previous_response_id is deliberately not used (changes every turn).

TP=2: the hash is computed on rank 0 only from request data; nothing new is broadcast and the worker path is unchanged (worker sequences keep session_hash = 0 as today).

One info log line per chat request: session key: source=<header|conversation|prompt_cache_key|metadata|user|first_user_turn|prefix1024> hash=0x….

Open point for review: single-shot prefill (prefill_a.rs:144) checks session_matches on every restore, not only tails. With distinct keys, cross-conversation reuse of a shared system-prompt snapshot on that path is no longer taken (the collision was what allowed it). Making that check tails-only, as suggested in the thread, is in spark-model and left out of this PR; happy to follow up.

Tests

  • cargo fmt --all -- --check, cargo check -p spark-server, cargo clippy -p spark-server --all-targets clean (ATLAS_NO_RDMA=1 ATLAS_SKIP_BUILD=1).
  • cargo test -p spark-server --bin spark session: 20 passed (same id → same hash; distinct ids with identical prompts → distinct; pinned hash value; header beats body; blank header ignored; same system+tools, different first user msg → distinct; turns appended → same hash; prefix1024 fallback; real 26 KB Claude Code prompt + 70 tools fixture). Full spark bin suite: 2797 passed, 0 failed.
  • All touched files ≤ 500 lines.

Pair check (DGX Spark ×2, TP=EP=2, Qwen3.8-Flash-Next FP4, production profile, image built from this branch)

  • Session keys: conversations A and B with identical system prompt + 3 tools → distinct hashes (0xd37abd1ee735222b vs 0x7f23855313d1ce88, source=first_user_turn); each kept its hash on turn 2. x-session-id agent-x vs agent-y with identical messages → distinct; agent-x kept its hash across turns. Window total: 54 first_user_turn + 3 header keys, 22 distinct.
  • Tool check: 24/24 well-formed, right tool 24, bad args 0, unwanted calls 0/8, request errors 0.
  • Concurrency (total tok/s): T1 43.2, T2 63.3, T4 68.6 with tools (main 26e0b20 baseline same day: 41.2 / 39.8 / 63.4).
  • Smoke OK; 0 panic / NCCL-error lines on both ranks.

@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

All contributors have signed the CLA. Thank you!
Posted by the CLA Assistant Lite bot.

@maplepaladin73

Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

…Atlas-Inf#209)

The first-1024-token session hash was one value for every request sharing a
tool set (the template renders the tool JSON first), so separate agent
conversations shared one snapshot "session" for eviction, the tail lease and
insert_tail's same-session tail replacement.

Chat requests now resolve the key in session_key.rs, in priority order:
- headers x-session-id / x-session-affinity / x-conversation-id;
- Responses `conversation` id;
- OpenAI prompt_cache_key, metadata.session_id / metadata.conversation_id
  (chat and Responses), Anthropic metadata.user_id (Claude Code's session);
- OpenAI `user`, folded with the first user turn (an end-user id alone
  would merge one user's conversations);
- the messages through the end of the first user turn (role + content,
  length-prefixed, capped at 1 MiB), hashed before tool-prompt injection;
- the old first-1024-token hash when there is no user message.
previous_response_id is deliberately not a key: it changes every turn.

Client ids hash with a domain tag through the fixed-key DefaultHasher::new(),
on the head only. session_hash is still not sent to the TP worker rank, whose
sequences keep session_hash = 0 as before. /v1/completions is unchanged.

Each chat request logs `session key: source=<src> hash=0x<hex>`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
AzeezIsh added a commit that referenced this pull request Oct 30, 2026
Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@AzeezIsh

Copy link
Copy Markdown
Contributor

Sparky GB10 gate in progress (job 50a, refs/pull/257/merge 47b767d, FP4-Local, default recipe profile with prefix caching):

  • session_key tests: 11/11 pass.
  • agentic-webserver 50: 50/50, 50/50, 11.83 s/turn, 337 turns (run-1793341506832517369).
  • BFCL ST-995: running.

The control differs from our older reference, but main has moved 219 commits since that reference was taken (#265–#272, incl. #271's Flash-Next [behavior] defaults), so it isn't attributable to this PR. A one-variable baseline, main at this PR's exact merge base with the same profile, is queued next (control, agentic 50, BFCL). This PR merges once that comparison is clean. Campaign log: #211.

@AzeezIsh

Copy link
Copy Markdown
Contributor

One-variable baseline (sparky job 50a2: main at this PR's merge base b0df501, same FP4-Local profile):

main base main + #257
4-prompt T=0 control reference byte-identical
agentic-webserver 50 50/50, 50/50, 11.82 s/turn, 338 turns (run-1793355763037290233) 50/50, 50/50, 11.83 s/turn, 337 turns (run-1793341506832517369)
BFCL ST-995 running 87.64 / 87.13 (run-1793351507833439068)

So the earlier control diff came from main's own changes, not this PR. Merging once the baseline BFCL lands and matches within the subset-level spread.

@AzeezIsh

Copy link
Copy Markdown
Contributor

Gate passed: merging. Sparky GB10, FP4-Local, default recipe profile with prefix caching. One-variable A/B: this PR merged onto b0df501 (job 50a) vs main at b0df501 (job 50a2):

main base main + #257
session_key tests — 11/11
4-prompt T=0 control reference byte-identical
agentic-webserver 50 50/50, 50/50, 11.82 s/turn, 338 turns (run-1793355763037290233) 50/50, 50/50, 11.83 s/turn, 337 turns (run-1793341506832517369)
BFCL ST-995 87.64 / 87.13 (run-1793365805020178746) 87.64 / 87.13, every subset identical (run-1793351507833439068)
session key: source=first_user_turn lines 0 1,341
serve ERROR / panic 0 0

Main has since gained #273/#274. In the files this PR touches they only change unrelated lines (loop-detect steering gate, a test module). Thanks @maplepaladin73. Fixes #209.

@AzeezIsh
AzeezIsh merged commit a7a1f35 into Atlas-Inf:main Oct 30, 2026
1 check passed
AzeezIsh added a commit that referenced this pull request Oct 30, 2026
Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

session_hash collides for every request that shares a tool set (first-1024-token hash); snapshot eviction cannot tell agent conversations apart

2 participants