Session Memory
Manage session memory artifacts in repos that have opted in via a docs/agent-sessions/ directory.
Two modes: start creates the session directory with a template memory.md; finalize checks for
completeness and stages everything.
Session directories are the index — each directory name encodes the date, session name, and scope. No shared index file is maintained, so parallel sessions never conflict.
Prerequisites
- •You must be working inside a repo that has a
docs/agent-sessions/directory. - •You must be on a feature branch (not
main/master).
Detecting Context
Before either mode, determine:
- •Session name: Extract from
$PWD. If the path contains.claude/worktrees/<name>/, use<name>. Otherwise, use the branch slug (branch name after the last/, e.g.,user/add-oauth→add-oauth). - •Repo root: Run
git rev-parse --show-toplevel. - •Branch: Run
git branch --show-current. - •Scope: Derive from branch name or PR number:
- •If a PR exists for the current branch (
gh pr view --json number --jq .number), usepr-{N}. - •Otherwise, take the branch slug after the
/(e.g.,bkonkle/add-oauth→add-oauth).
- •If a PR exists for the current branch (
- •Date: Today's date as
YYYY-MM-DD. - •Session directory:
docs/agent-sessions/YYYY-MM-DD-{session-name}-{scope}/. - •Session ID: Parse from the Claude Code JSONL file path. The session ID is the conversation
UUID — look for the most recent
.jsonlfile under~/.claude/projects/whose encoded path matches the current repo. The filename (without.jsonl) is the session ID. If detection fails, leave as(unknown)and note the user can fill it in manually.
If docs/agent-sessions/ does not exist in the repo root, stop and tell the user the repo has not
opted in. They can opt in by creating docs/agent-sessions/README.md.
Mode: start
Parse $ARGUMENTS — if it equals start (or is empty), run this mode.
Steps
- •
Check for existing session directory. If
docs/agent-sessions/YYYY-MM-DD-{session-name}-{scope}/already exists, print its path and note that it's ready for use. Do not overwrite. - •
Create the session directory:
shmkdir -p docs/agent-sessions/YYYY-MM-DD-{session-name}-{scope} - •
Create
memory.mdwith this template (fill in known fields, leave placeholders for others):markdown# Memory: <title — describe the work> | Field | Value | | ---------- | ------------------ | | Session | {session-name} | | Date | YYYY-MM-DD | | Session ID | {sessionId} | | PR | (pending) | | Branch | {branch} | | Issue(s) | (none yet) | ## Goal <!-- What this session sets out to accomplish --> ## Key Decisions <!-- 1. **Decision** — Rationale. Alternatives considered. --> ## Approach <!-- Implementation strategy, files changed, patterns followed --> ## Problems Encountered <!-- - **Problem** — Root cause and fix --> ## Outcome <!-- End state: merged, open for review, follow-up needed --> ## Follow-ups <!-- - [ ] Unresolved items for future sessions --> - •
Stage the new files:
shgit add docs/agent-sessions/YYYY-MM-DD-{session-name}-{scope}/ - •
Print summary — show the session directory path and the memory file path. Remind to update the memory file incrementally throughout the session.
Mode: finalize
Parse $ARGUMENTS — if it equals finalize, run this mode.
Steps
- •
Find the session directory. Look for a directory in
docs/agent-sessions/matching the current session name and branch scope for today's date. If multiple exist, use the most recent. If none exist, tell the user to run/session-memory startfirst. - •
Check for completeness. Read
memory.md. Look for HTML comment placeholders (<!-- ... -->). List sections that still have only placeholder content and prompt the user to fill them in before finalizing. - •
Update PR number. If
memory.mdstill shows(pending)for the PR field and a PR exists for the current branch, update it:shgh pr view --json number --jq .number
- •
Update session ID. If
memory.mdstill shows(unknown)for the Session ID field, attempt to detect it again from the JSONL file path. - •
Validate metadata completeness. Check that the metadata table in
memory.mdhas been filled in — Session, Date, Branch, and PR fields should not be placeholders. If any are still placeholders, warn the user. - •
Stage everything:
shgit add docs/agent-sessions/
- •
Print summary — confirm the memory file is complete and staged. List any remaining placeholder sections as warnings.