Skip to content

Read cwd and transcript_path from hook stdin instead of deriving them #407

Closed

Description

@fdaviddpt

Problem

Remember derives two things it is handed for free.

scripts/resolve-paths.sh resolves the plugin root from CLAUDE_PLUGIN_ROOT (line 105) and the project root from CLAUDE_PROJECT_DIR (line 121). pipeline/extract.py:_session_dir() then reconstructs the transcript location as <config-dir>/projects/<slug>/<session_id>.jsonl, which is why this repo carries the Windows drive-letter normalisation at resolve-paths.sh:131-181 and the four path shapes behind #263: the slug has to be rebuilt byte-for-byte or the glob silently returns nothing.

The host already tells us both. Every SessionStart and SessionEnd payload carries cwd and transcript_path on stdin, and session-start-hook.sh already reads that stdin — HOOK_STDIN at line 124, _stdin_json_string() at line 144, pulling session_id (#206) and source (#339) out of it. We consume two fields of a payload that contains four we want.

Why this is not Codex prep

Filed out of #406, but it stands on its own and should be reviewed on its own.

Reconstructing a path we were given is strictly worse than reading it, on the host we support today. Every failure mode in the slug logic — #263's four drive shapes, the twelve days of empty backups, the case-divergence library — exists only because the derivation can disagree with reality. A field handed over by the host cannot.

The portability argument is a bonus, and it is a real one. Three CLIs now ship hook systems, and they agree on this payload and on very little else:

Claude Code Codex Gemini CLI
stdin session_id, cwd, transcript_path yes yes yes
tool event names PreToolUse/PostToolUse same BeforeTool/AfterTool
plugin-root env var CLAUDE_PLUGIN_ROOT PLUGIN_ROOT (+ CLAUDE_* alias) none documented

The env-var route is the parochial one. Codex's CLAUDE_PLUGIN_ROOT alias is a courtesy that can be withdrawn in any release, and Gemini documents no such variable at all. The stdin contract is the part three vendors arrived at independently.

Proposed change

  1. Extend the _stdin_json_string() calls in session-start-hook.sh (and the equivalent in session-end-hook.sh) to also extract cwd and transcript_path, exporting them for the sourced resolver.
  2. resolve-paths.sh: insert stdin-cwd as a PROJECT_DIR source between the CLAUDE_PROJECT_DIR branch (121) and the .claude/remember layout branch (123). Read plugin root as ${PLUGIN_ROOT:-$CLAUDE_PLUGIN_ROOT} at 105 so the vendor-neutral name wins when present.
  3. pipeline/extract.py: accept an explicit transcript path and use it when supplied; keep _session_dir() reconstruction as the fallback for every path that has no payload (consolidation, doctor, manual invocation).

Nothing is removed. Each derivation stays as the fallback it already is.

Tests

  • resolve-paths.sh:107 — the derive-from-script-location plugin-root branch is currently reached only by local installs. If the env var is ever absent it becomes the only route, and it has no dedicated test. Pin it.
  • Precedence, explicitly: env var beats stdin beats derivation, and a stdin cwd disagreeing with CLAUDE_PROJECT_DIR must not silently win.
  • A supplied transcript_path must be used verbatim, including a path the slug logic would have mangled — the Windows drive-letter case is the positive control here.
  • Absent, malformed and empty stdin must behave exactly as today. _stdin_json_string() is documented as a heuristic and validated after extraction; two more keys must not weaken that.

Out of scope

The summarizer. pipeline/haiku.py shells claude -p, and that is the one genuinely Claude-coupled component. It is not touched here, and it should not be abstracted on the strength of a single implementation.

Activity

  1. fdaviddpt commented on Aug 28, 2026

    @fdaviddpt
    ContributorAuthor

    Narrowing this before starting, because the plan above does not survive contact with the hook ordering.

    What blocks step 2

    session-start-hook.sh:75 sources resolve-paths.sh. HOOK_STDIN is not captured until line 124. The resolver therefore runs before stdin has been read, and cannot consult a field that has not arrived yet.

    Neither escape is free:

    Scope now

    Doing here — both pay on the host we support, neither touches ordering:

    Deferred to #406, deliberately:

    • stdin cwd → PROJECT_DIR, together with the capture hoist and its nested-summarizer guard. It is unverifiable here: a test can unset CLAUDE_PROJECT_DIR to force the branch, but the thing that justifies the ordering change is a host that genuinely omits it, and no such host is installed yet. It gets done against a real Codex install, where the failing test is a real failure rather than a simulated one.

    The test list above stands, minus the cwd precedence case, which moves with the branch it covers.

  2. fdaviddpt commented on Aug 28, 2026

    @fdaviddpt
    ContributorAuthor

    Design decision, recorded before the implementation lane picks this up: the transcript path is read through a host adapter with a registry, not by an inline environment read.

    I argued against abstraction earlier in this thread and want to be explicit about why that changed. It was not a change of mind about speculative generality. It is that the seam is now measured rather than guessed: three shipped agent CLIs, and the disagreement between them is documented, narrow and stable in shape.

    Claude Code Codex Gemini CLI
    hook stdin session_id, cwd, transcript_path yes yes yes
    tool event names PreToolUse/PostToolUse same BeforeTool/AfterTool
    plugin-root env var CLAUDE_PLUGIN_ROOT PLUGIN_ROOT (+ CLAUDE_* alias) none documented

    What differs is the names, and that is data. pipeline/host.py (pushed on feat/407-transcript-path) holds it as a Host record per CLI plus a detect_host() factory, so a fourth host is a table entry rather than a search through the tree.

    Two details in it that are decisions rather than mechanics:

    • The native name wins over the alias. PLUGIN_ROOT is read before CLAUDE_PLUGIN_ROOT, so nothing depends on a compatibility alias outliving the release that shipped it.
    • CLAUDE_PLUGIN_ROOT is not a detection signature. Codex sets it too. Detection uses CLAUDE_CODE_*, which Claude Code sets and nothing else does. Getting this backwards would identify every Codex session as Claude Code.
    • Unknown is a normal result, not an error. Gemini exposes no variables at all, so it cannot be detected and does not need to be: everything Remember wants from it arrives on stdin.

    Three things are deliberately excluded, and the module docstring says so at the point someone would otherwise add them:

    • Event names — bindings live in each host's own manifest, which has to name them anyway.
    • The summarizer — pipeline/haiku.py shells claude -p. Still one implementation; an interface derived from it would encode that CLI's auth model and output shape as though they were neutral. Extracted from two real providers or not at all.
    • Shell path resolution — resolve-paths.sh runs before starting Python is worth it, so it mirrors the variable list by hand, exactly as lib-slug.sh mirrors pipeline/slug.py. That mirror needs a parity test or it will drift.

    State on the branch

    feat/407-transcript-path, commit f9852fa. Eleven tests in tests/test_transcript_path_407.py, four currently red — the four asserting a supplied path is used. The other seven pass today and are the positive controls: without them, the red four could be satisfied by a reader that simply stops reading.

    One hazard the tests pin, which is easy to miss: extract_session derives actual_id from the transcript's basename. Codex names rollout files rollout-<date>-<uuid>.jsonl, so a basename-derived id would key get_last_save_line on something that never matches, and every save would re-summarize the whole transcript from line 0 — the duplicate #140 exists to prevent. When a session id is known it must win over the filename.

    Nothing consumes host.py yet. Remaining: wire it into find_session/extract_session, export REMEMBER_TRANSCRIPT_PATH from the hooks that already consume the payload, add the ${PLUGIN_ROOT:-$CLAUDE_PLUGIN_ROOT} read plus its parity test, and document it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions