Skip to content

feat(identity,sticky): purify session switches across slots and protocols - #2479

Open
dofastted wants to merge 2 commits into
feat/slot-machine-envfrom
feat/session-switch-purify
Open

dofastted wants to merge 2 commits into
feat/slot-machine-envfrom
feat/session-switch-purify

Conversation

@dofastted

@dofastted dofastted commented Dec 1, 2026 •

Copy link
Copy Markdown
Owner

1. 本次改动与原因

整理本地未提交工作树 eat/session-switch-purify 并与 #2472( eat/slot-machine-env)对齐去重:

2. 已执行验证与结果

  • 语法检查: ind src scripts -name '*.mjs' -print0 | xargs -0 -n1 node --check — PASS
  • 格式检查:
    px biome check(全部 15 个变更文件)— 0 fixes
  • CI 核心集:KIN_SCHEDULER_BIN=/tmp/kin-sched-target/release/kin-scheduler npm run test:ci — 13/13 PASS
  • 关联单测:
    ode --test test/unit/agent-summary-routing.test.mjs test/unit/go-worker-client.test.mjs test/unit/identity-rewrite.test.mjs test/unit/sticky-router.test.mjs test/unit/slot-machine-env.test.mjs — 202/202 PASS(0 fail,0 新建测试文件)

3. 破坏性验证(Broken-by-hand)

  • �gent-summary-routing.test.mjs 端到端断言 slot switch purifies outbound session headers, foreign thinking, tool IDs, and sticky reset across protocols 与 4-module single-slot pipeline ... isolates cross-slot failover,以及 sticky-router.test.mjs 断言 deleteByFilter purges SQLite & PG bindings and marks key for generation >= 2 purification(同时验证与 feat(identity,protocol): bind 3-OS slot machine profile and 4-module isolation #2472 slot-machine-env.test.mjs 无冲突回归)。

4. 未运行项

  • 未连接生产机 ph24;未跑全量 554 个 est:unit / E2E 套件与真实 PG18 实例(PG 路径由 mock 事务池与契约单测覆盖)。

5. 风险与回滚

  • 仅新增 1 个独立模块 session-switch-purify.mjs 并挂接 10 个路由/身份/粘性文件与 4 个已有测试文件;回滚只需 revert 本次提交即可完全恢复。

Note

High Risk
Touches core request routing, sticky session binding, and outbound identity/session IDs—errors could break continuity, tool replay, or leak cross-account state.

Overview
Adds session-switch purification so when a conversation hops to another VM/account, fails over, or has sticky bindings deleted, outbound state does not leak from the previous slot.

A new session-switch-purify module provides account-scoped Feistel tool ID encode/decode (inbound remap + outbound SSE/message restore), payload scrubbing (thread/diagnostics, foreign extension/_kinAccountId, # userEmail and internal paths), a bounded purify ledger for slot lineage, and foreign-tool ownership tracking.

Identity and worker path: resolveOutboundSessionId now requires same VM and account to reuse a bound session; slot switches mint fresh sessions (including under passthrough). CRAG caller headers get purified parent/session/prompt/request IDs via purifyCragCallerHeaders.

Sticky and admin: StickyRouter.deleteByFilter + StickyRepo.removeByFilter purge bindings and set reset tombstones so the next bind forces generation ≥ 2 and full purification. Scheduler/panel sticky DELETE also clears the purify ledger and returns purify_ledger_purged.

Request pipeline: handle-protocol detects slot/account changes, foreign tools, and post-reset generations; on switch it aggressively strips foreign thinking, runs purifySessionPayloadForSlot, and threads slotSwitched/sessionPurified through CRAG, CLI, and HTTP hops. failover-runner inspects/commits the ledger around each attempt.

Tests cover Feistel round-trips, sticky delete + ledger purge, header purification, and a multi-protocol end-to-end slot-switch scenario.

Reviewed by Cursor Bugbot for commit 5198ee3. Configure here.

…cols

WHY:
When a conversation switches upstream slots/accounts (failover, sticky reset,
or admin sticky delete), stale outbound session headers, foreign thinking
blocks, account-scoped tool IDs, and internal SHM/session paths can leak
across accounts. Reconciles session-switch purification cleanly atop #2472
without conflicting with src/lib/identity/tool-id-mapping.mjs.

CHANGES:
- src/lib/identity/session-switch-purify.mjs: HKDF-SHA256 Feistel account-isolated
  tool ID mapping, internal SHM/CRAG path sanitization, and outbound session
  switch purification helpers (distinct from #2472 tool-id-mapping.mjs)
- src/lib/identity/identity-rewrite.mjs, src/lib/pool/{sticky-router,failover-runner,thinking-owner}.mjs,
  src/lib/db/repos/sticky-repo.mjs, src/lib/protocol/{handle-protocol,issued-tool-replay}.mjs,
  src/lib/admin/scheduler-pools-routes.mjs, src/lib/scheduler/scheduler-runtime.mjs,
  src/lib/transport/go-worker-client.mjs: wire generation >= 2 purification on
  slot switch / sticky deleteByFilter across protocols
- test/unit/{agent-summary-routing,go-worker-client,identity-rewrite,sticky-router}.test.mjs:
  regression coverage (0 new test files)

VERIFIED:
- find src scripts -name '*.mjs' | xargs -n1 node --check: PASS
- npx biome check (15 files): 0 fixes
- KIN_SCHEDULER_BIN=... npm run test:ci: 13/13 PASS
- node --test agent-summary-routing go-worker-client identity-rewrite sticky-router slot-machine-env: 202/202 PASS
@cursor

cursor Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Bugbot couldn't run - usage limit reached

Bugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit.

A user or team admin can review and increase usage limits in the Cursor dashboard.

(requestId: 09df36ed-b506-4d13-b2f0-6562b07448dc)

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale comment

Agentic security review found one medium isolation gap: CRAG thread/diagnostics and prompt/request correlation IDs are stripped only on the first slotSwitched hop, not on later sessionPurified hops after sticky reset or failover.

Open in Web View Automation 

Sent by Cursor Security Agent: Security Reviewer

@dofastted

Copy link
Copy Markdown
Owner Author

1. 本次改动与原因(Head 5198ee387f30e743c03b65c91542bab763994c82)

2. 已执行验证与结果

  • npx biome check(4 个改动文件)— 0 fixes
  • find src scripts -name '*.mjs' -print0 | xargs -0 -n1 node --check — PASS
  • node scripts/run-isolated-tests.mjs(8 关联套件:scheduler-m3-cutover 9/9、scheduler-m1b-pools-api、agent-summary-routing 13/13、go-worker-client、identity-rewrite、sticky-router 52/52、slot-machine-env 30/30、issued-tool-replay)— 全部 PASS
  • KIN_SCHEDULER_BIN=/tmp/kin-sched-target/release/kin-scheduler npm run test:ci — 13/13 PASS

3. 破坏性验证(Broken-by-hand)

4. 未运行项

5. 风险与回滚

  • 仅同步基线并收敛冲突,单提交可回滚。

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agentic security review found two medium isolation gaps in session-switch purification: CRAG thread/diagnostics (and prompt/request correlators) are still stripped only on the first slotSwitched hop, and # userEmail replacement fails for typical dotted addresses when rewriting onto the target slot.

Open in Web View Automation 

Sent by Cursor Security Agent: Security Reviewer

Comment on lines +403 to +410
if (slotSwitched) {
if ('thread' in body && body.thread != null) {
delete ensureBodyCopy().thread
}
if ('diagnostics' in body && body.diagnostics != null) {
delete ensureBodyCopy().diagnostics
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔒 Agentic Security Review
Severity: MEDIUM

thread / diagnostics 只在 slotSwitched === true 时被删掉。粘性 reset / failover 的第一跳(freshSlot 或 ledger explicitReset)会打上 slotSwitched,但 commitSlotHop 后 generation >= 2 的后续跳只会走 sessionPurified。同一套路里 purifyCragCallerHeaders 也只在 slotSwitched 时重签 x-claude-code-prompt-id / x-client-request-id。

客户端在切槽后继续上送旧 thread.continue 时,前一槽的 CRAG 会话与 tool 状态可以被重新挂上,使本次会话切换净化失效。

Fix in Cursor Fix in Web

Reviewed by Cursor Security Reviewer for commit 5198ee3. Configure here.

Comment on lines +303 to +307
if (cleanTarget) {
return reminder.replace(
/(# userEmail\r?\nThe user's email address is )([^\s.\r\n]+@[^\s.\r\n]+)(\.?)/g,
(_, p1, _old, p3) => `${p1}${cleanTarget}${p3}`,
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔒 Agentic Security Review
Severity: MEDIUM

目标槽有 email 时,# userEmail 替换正则 [^\s.\r ]+@[^\s.\r ]+ 禁止 local-part 和 domain 出现 .。真实地址(含 user.name@gmail.com 这类)匹配不上,前一槽的 email 会原样留在 <system-reminder> 里转发给新账号。

生产路径会把 VM claude.email 作为 targetEmail 传入,走的正是这条替换分支;现有单测只覆盖空 targetEmail 的整段删除路径。

Fix in Cursor Fix in Web

Reviewed by Cursor Security Reviewer for commit 5198ee3. Configure here.

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.

1 participant