Repository navigation
Closed
Labels
enhancementNew feature or request
Description
Problem
The harness runs one session per process (KEDA ScaledJob model). At scale this means 1 pod per active session — 10,000 concurrent sessions = 10,000 pods. The harness spends 80-90% of wall-clock time idle, waiting for LLM responses and tool calls, yet each session exclusively owns a full pod.
Root cause
Pi was designed as a single-user CLI agent. runTurn is not concurrent-safe — five categories of global mutable state prevent multiple sessions from sharing a process:
| Problem | Location | Severity |
|---|---|---|
process.env.ANTHROPIC_API_KEY mutation per call |
harness/src/run-turn.ts |
Showstopper — concurrent sessions with different auth tokens corrupt each other |
stdoutTakeoverState — global process.stdout.write replacement |
pi-fork: coding-agent/src/core/output-guard.ts |
Showstopper — one session's capture swallows another's output |
sessionResourceCleanups — global Set, cleanupSessionResources() fires ALL |
pi-fork: ai/src/session-resources.ts |
Showstopper — cleanup for session A tears down session B's resources |
fileMutationQueues — global serialization queue |
pi-fork: coding-agent/src/core/tools/file-mutation-queue.ts |
Race — concurrent sessions serialize through same promise chain |
commandResultCache — shell results cached for process lifetime |
pi-fork: coding-agent/src/core/resolve-config-value.ts |
Stale data — session B reads session A's cached command output |
4 of 5 are in pi-fork, 1 is in the harness.
Proposed fix
Each fix is mechanical — replace module-level globals with a per-session context object threaded through the call chain:
process.envmutation → set credentials once at startup (or accept as function args, not env). This is the harness-side fix.stdoutTakeoverState→ remove entirely (server mode doesn't need stdout capture) or scope per session.sessionResourceCleanups→ key the Set by session ID.fileMutationQueues→ scope per session (each session gets its own queue).commandResultCache→ scope per session or add TTL.
Target architecture
┌─────────────────────┐
│ Harness Pod (×N) │
│ │
│ session-A ─┐ │
│ session-B ──┤ Pi │ N concurrent sessions
│ session-C ──┤ loop │ per warm process
│ ... ─┘ │
└──────────┬──────────┘
│
┌──────────┴──────────┐
│ Sandbox Pool │ shared, leased
│ (kubectl exec) │ unchanged
└─────────────────────┘
- A fixed/elastic pool of harness pods replaces KEDA ScaledJob (create/destroy per session).
- Each harness process serves ~50-100 concurrent sessions.
- Session state stays in Redis (already external).
- Sandbox pool continues unchanged — sessions lease sandboxes as today.
Impact
| Metric | Current (1:1) | Multiplexed (N:1) |
|---|---|---|
| Pods for 10K concurrent | 10,000 | 100-200 |
| Activation latency | 1.5-3.4s (cold start) | ~10-50ms (Redis session load) |
| Resource for 10K concurrent | ~20,000 CPU | ~400 CPU |
This is the single largest density lever available — roughly 100× improvement in pod count and resource footprint.
Scope
- Introduce a
SessionContexttype threading session ID + credentials + scoped state - Fix the 5 global state categories listed above
- Add a concurrent session smoke test (2+ sessions interleaved on one process)
- Validate no cross-session state leakage under load
- Update deployment model from KEDA ScaledJob to elastic pod pool
Related
- Epic: Two-tier FS-free harness + shared durable sandbox pool #49 — Two-tier architecture epic (harness=brain, sandbox=hands). Assumes 1 session per harness; this issue removes that constraint.
- Sync-path saturation: bounded wait + 503 Retry-After per spec 4.3 #55 — Sync-path saturation / 503 Retry-After. With multiplexing, overload handling shifts from pod-level to session-level.
Metadata
Metadata
Assignees
Labels
enhancementNew feature or request
Activity
pdettori commentedon Sep 7, 2026
Correction: the severity table above is wrong on four of five rows
Traced against
mainatf78081f. The table in the body was written from reading the coderather than tracing it, so please scope from this comment (or the spec) and not from the body's
table. Full detail with citations is in
PR #228 §2 of
docs/specs/2026-09-06-p5-session-isolation-design.md.ANTHROPIC_API_KEYrun-turn.ts:310-312is write-once-if-absent, so session A's token sticks process-wide and B..N silently authenticate as A. A cross-tenant identity leak, not mutual corruption.stdoutTakeoverStatetakeOverStdout()is called only frommain.ts:511(CLI) andrpc-mode.ts:54; the harness enters Pi viacreateAgentSession.sessionResourceCleanupscloseOpenAICodexWebSocketSessions(sessionId)closes only that session's socket (openai-codex-responses.ts:769-784), andagent-session.ts:733passesthis.sessionId.fileMutationQueuesrealpath(file-mutation-queue.ts:16-26), so different files already run in parallel. The only true global isregistrationQueue(:5), which serializes onerealpath()call — throughput, not correctness.commandResultCache!(resolve-config-value.ts:211-219). The harness supplies credentials as config/env, never as!cmd.Two consequences that change the shape of the work:
"4 of 5 are in pi-fork" is no longer the right frame — none of them needs a
pi-forkchange.Pi is already per-session on the credential path:
AgentSession._getRequiredRequestAuth(
agent-session.ts:357-381) resolves auth per request from a per-instanceModelRegistryandpasses an explicit
apiKeyinto stream options, andwithEnvApiKey(stream.ts:26) consults theenvironment only when no explicit key was given. The harness's env seed is what activates that
fallback beneath a mechanism that already worked. The other four globals are inert in server mode,
so the design pins them with reachability tests instead of refactoring them — refactoring would add
fork divergence to fix nothing.
The actual prerequisite is missing from the issue: no per-session credential enters the harness
at all. Every
buildConfig()call site readsprocess.env(server.ts:66-73,leaf-job.ts:13-18,cli.ts:13), so the pod's environment is the credential, one perdeployment.
TurnConfig.anthropicAuthTokenexists but no caller ever fills it from a request.This forces an ordering the "Proposed fix" list does not have: inflow → remove the ambient
fallback → scrub. Deleting the seed first would not fix a leak; it would leave every session
unauthenticated.
Also note the proposed fix #1 ("set credentials once at startup") is only correct for a
single-tenant pod. Under mixed tenancy it is the bug, and ADR-0011/0012/0026 forbid the harness
holding a provider credential at all — so the harness carries an inert placeholder plus a subject,
and AuthBridge's
static-injectswaps it on egress.Suggested split
This issue currently bundles three separable things, and its title promises density that the
correctness work alone does not deliver:
plan written, postponed by decision as of 2026-09-06.
ScaledJob→ elastic pod pool, the pod-count/activation numbers,and the Sync-path saturation: bounded wait + 503 Retry-After per spec 4.3 #55 overload shift from pod-level to session-level. Deserves its own issue; unblocked by 1.
Z5's deferred per-user / RFC 8693 half, living in
kagenti-extensions. Track against Z5. Theharness half is the strict prerequisite: the injector cannot key on a subject the harness never
sends.
Assisted-By: Claude (Anthropic AI) [email protected]
cwiklik commentedon Sep 8, 2026
Closing in favor of focused replacement issues. The severity table in the body is wrong on 4 of 5 rows — see PR #228's spec (P5,
docs/specs/2026-09-06-p5-session-isolation-design.md§2.1) for the corrected analysis with code citations.This issue bundled three separable concerns:
x-api-key: null→ Record<string, string>) #1.kagenti-extensions. Blocked by harness: tsc fails on cli.ts gateway-header cast (x-api-key: null→ Record<string, string>) #1.New focused issues to follow.
docs(spec): address PR rossoctl#228 review — leaf path, precedence, s…
docs(spec): P5 multi-session isolation design (rossoctl#220)
docs(spec): address PR rossoctl#228 review — leaf path, precedence, s…
Merge pull request #228 from pdettori/docs/220-session-isolation-spec
cwiklik commentedon Sep 8, 2026
Following up on the closing comment's "New focused issues to follow" — the three replacements are now filed:
pi-forkchanges.ScaledJob→ elastic pod pool. The actual density win. Blocked by #239.Two notes for anyone arriving here from the density claim:
This issue was closed as superseded by a split, not as delivered. Its
COMPLETEDstate reason reads as done, but #228 says plainly that nothing in it closes this issue (Refs #220, notFixes). What landed in #228 is the corrected design —docs/specs/2026-09-06-p5-session-isolation-design.mdand ADR-0032 — with no implementation.Do not scope work from this issue's severity table. It was written from reading the code rather than tracing it, and is wrong on four of five rows; the corrected tracing with citations is in the spec's §2.1. In particular: only one of the five process-globals matters, and it is not a concurrency bug but an identity leak —
ANTHROPIC_API_KEYis seeded write-once-if-absent atharness/src/run-turn.ts:310-312, so session A's token sticks process-wide and B..N authenticate as A. The other four are inert in server mode, and pi's credential path is already per-session, which is why nopi-forkrefactor is warranted.The spec, not this issue, is the source of truth for all three.
Assisted-By: Claude (Anthropic AI) [email protected]
docs(spec): amend P5 isolation design — server.ts:174 is on the /turn…