Personal_AI_Infrastructure icon indicating copy to clipboard operation
Personal_AI_Infrastructure copied to clipboard

Session-resolution guessing: AlgoPhase.ts writes phase to the wrong session's work.json row; Inference.ts --auto-state attaches the wrong ISA

Open jbmml opened this issue 4 months ago • 1 comments

Summary

Two tools resolve "which session am I acting on" non-deterministically and then act on the guess:

  1. LifeOS/install/LIFEOS/TOOLS/AlgoPhase.ts — when called without --slug/--uuid (and CLAUDE_SESSION_ID unset, which is always today — Claude Code does not export it to tool subprocesses), resolveSlug() falls back to the most-recent algorithm-mode row in MEMORY/STATE/work.json. It prints a WARN (the hazard is even called out in the code comment at the fallback site) but still writes the phase to that row. With two Algorithm sessions alive, a bare AlgoPhase think from one session flips the OTHER session's registry row — cross-session state corruption that propagates to the Pulse dashboard and anything else reading work.json.

  2. LifeOS/install/LIFEOS/TOOLS/Inference.ts (synthesizeAdvisorState(), used by --mode advisor --auto-state) — reads work.json expecting active / current / activeSession top-level keys. The registry's actual shape is { "sessions": { "<slug>": {...} } } and has no such keys, so resolution always falls through to "most recently modified directory in MEMORY/WORK/". That fallback is usually right by accident (the current session's ISA is often the newest), but it carries no session identity at all — under concurrency or right after another session completes, it attaches a completed, unrelated session's ISA as the advisor's state blob, so the reviewer confidently reviews the wrong work.

Repro (AlgoPhase)

With a work.json containing two live algorithm-mode rows (easy to hit with two concurrent Claude Code tabs):

{
  "sessions": {
    "20260101-000001_session-a": { "mode": "interactive", "currentMode": "algorithm", "phase": "build",
      "sessionUUID": "aaaaaaaa-0000-0000-0000-000000000001", "updatedAt": "2026-01-01T18:00:00.000Z" },
    "20260101-000002_session-b": { "mode": "interactive", "currentMode": "algorithm", "phase": "build",
      "sessionUUID": "aaaaaaaa-0000-0000-0000-000000000002", "updatedAt": "2026-01-01T18:30:00.000Z" }
  }
}
$ bun ~/.claude/LIFEOS/TOOLS/AlgoPhase.ts think        # called from session-a's context
WARN: AlgoPhase falling back to most-recent algorithm-mode row "20260101-000002_session-b". ...
OK: 20260101-000002_session-b build→think              # session-b's phase was just flipped by session-a

Expected: explicit --slug/--uuid wins; else CLAUDE_SESSION_ID when set; else, if the number of plausibly-live candidate rows is anything other than exactly one, fail closed — exit non-zero, list the candidates, write nothing. The single-live-session convenience path (bare call, exactly one candidate) should keep working. Note that "not complete" alone is a weak liveness signal: crashed sessions leave non-complete rows behind for days (the registry stale sweep prunes at ~7 days), so candidate selection also needs a freshness horizon or fail-closed would hard-fail bare calls indefinitely.

Actual: warns, then guesses and writes anyway.

Repro (Inference --auto-state)

With any work.json in the shipped registry shape and several dirs under MEMORY/WORK/:

$ bun ~/.claude/LIFEOS/TOOLS/Inference.ts --mode advisor --auto-state "task" "question"

synthesizeAdvisorState() attaches the ISA of whichever MEMORY/WORK/ directory has the newest mtime, because state?.active || state?.current || state?.activeSession is always undefined against the real registry shape. Whenever the newest-mtime directory is not the caller's (another session just completed, or wrote its ISA more recently), the advisor reviews the wrong session's state.

Expected: prefer the row matching CLAUDE_SESSION_ID when set; else resolve deterministically from the registry (a just-completed row within a short window is the completion-boundary advisor call the doctrine mandates; a single fresh live row is unambiguous); if ambiguous, proceed with NO auto-synthesized state and a loud warning — a reviewer with no state is strictly safer than a reviewer confidently reviewing the wrong session's ISA.

Proposed fix

Two PRs to follow (separable concerns, same principle):

  • PR 1: Inference.ts — parse the real {sessions} registry shape and resolve deterministically with the completion-boundary window; no CLI contract change.
  • PR 2: AlgoPhase.ts — deterministic priority chain, fail closed (no write) on ambiguity, freshness horizon on candidates; plus the ALGORITHM doc update recommending --slug at every phase emit.

Both keep the OK:/ERR: output format, exit codes, and the single-session happy path, and ship self-contained bun test suites.

jbmml avatar Jul 11 '26 21:07 jbmml

This is good stuff. We're about to push 7.x, which changes how all this stuff works massively, so you shouldn't have this problem anymore. Thanks.

danielmiessler avatar Jul 12 '26 05:07 danielmiessler