Skip to content

fix: key memory to the main checkout for git worktree sessions (#56) - #127

Merged
fdaviddpt merged 1 commit into
Digital-Process-Tools:mainfrom
jeongph:fix/worktree-memory-root
Jul 15, 2026
Merged

fdaviddpt merged 1 commit into
Digital-Process-Tools:mainfrom
jeongph:fix/worktree-memory-root

Conversation

@jeongph

@jeongph jeongph commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #56 — memory built up during a git worktree session was thrown away instead of being kept with the repository.

Claude Code sets CLAUDE_PROJECT_DIR to the worktree path for worktree sessions, and the plugin derives REMEMBER_DIR from it. So:

  • Legacy (default) mode — memory landed in <worktree>/.remember, physically inside the worktree. Because the plugin gitignores it with *, a plain git worktree remove (no --force, no warning) deletes every now.md / today-*.md / remember.md from that session, and none of it is migrated to the main checkout.
  • External mode — the {slug} was computed from the worktree path, producing an orphaned ~/.remember/<slug-of-worktree> subtree the main checkout's sessions never load.

This matches the observation and fix direction in #56 (thanks @KrzysztofKasprowicz for the report and @dewet22 for the diagnosis).

Fix

REMEMBER_DIR resolution now routes through git's common dir. When PROJECT_DIR is a linked worktree, memory is keyed to the repo's main checkout, so it:

  • survives git worktree remove, and
  • is shared across all worktrees of the same repo (one continuous memory, not a throwaway per worktree).

CLAUDE_PROJECT_DIR / PROJECT_DIR is left untouched — session recovery still resolves transcripts under the worktree slug Claude Code computed, exactly as @dewet22 flagged.

Fail-safe by design

The redirect only fires when a linked worktree is positively identified (git's common-dir ≠ git-dir) and the computed main checkout is a real work tree. These all fall through to the unchanged PROJECT_DIR:

  • ordinary (non-worktree) checkouts
  • non-git directories
  • bare-repo worktrees (parent of the common dir is not a work tree)
  • git without --path-format (< 2.31)

So non-worktree and non-git users see byte-for-byte identical behavior.

Changes

  • scripts/lib-memory-dir.sh — new _resolve_memory_project_dir(); REMEMBER_DIR is resolved against it and it is exported as MEMORY_PROJECT_DIR.
  • scripts/bootstrap-dirs.sh — the one-shot legacy→external migration and the .gitignore write are keyed to MEMORY_PROJECT_DIR so they stay consistent with where REMEMBER_DIR now resolves.
  • tests/test_worktree_memory.py — new: legacy + external resolution in a real worktree, regression guards for non-worktree / non-git, the shared-dir invariant, and that session-start emits === HANDOFF === pointing at the main checkout (the existing REMEMBER_ROOT != PROJECT_DIR guard already covers this once REMEMBER_DIR is redirected).
  • CHANGELOG.md, README.md — document the behavior (new "Git worktrees" section).

Handoff interaction (no extra code needed)

In legacy mode the === HANDOFF === hint is normally suppressed (#118) because the /remember skill's fallback {project}/.remember/remember.md already matches. Inside a worktree that fallback would be wrong — but session-start's existing REMEMBER_ROOT != PROJECT_DIR guard now fires (since REMEMBER_ROOT is the main checkout ≠ the worktree PROJECT_DIR), so the hint is emitted and the skill writes the handoff to the main checkout. A test locks this in.

Testing

  • Full suite (CI-equivalent, pytest with coverage): 500 passed, 43 skipped, 98.24% coverage.
  • New tests/test_worktree_memory.py uses real git worktree layouts.
  • Manual end-to-end: accumulated memory during a worktree session now survives git worktree remove and lands in the main checkout's .remember/.

Windows is skipped for the new tests (POSIX worktree layout + slug assertions), consistent with the existing worktree/path tests.

🤖 Generated with Claude Code

…al-Process-Tools#56)

Claude Code sets CLAUDE_PROJECT_DIR to the worktree path for worktree
sessions, so REMEMBER_DIR resolved inside the worktree. In the default
layout memory landed in <worktree>/.remember — gitignored with `*`, so a
plain `git worktree remove` deleted it silently (no --force, no warning)
and never migrated it to the main checkout. In external mode the {slug}
was computed from the worktree path, producing an orphaned
~/.remember/<slug-of-worktree> the main checkout's sessions never load.

Resolve REMEMBER_DIR through git's common dir: when PROJECT_DIR is a
linked worktree, memory is keyed to the repo's main checkout, so it
survives `worktree remove` and is shared across worktrees. PROJECT_DIR
is left as the worktree path so session recovery still resolves
transcripts under the worktree slug. Fail-safe: only redirects on a
positively-identified linked worktree whose main checkout is a real work
tree — ordinary checkouts, non-git dirs, bare-repo worktrees, and git
without --path-format all behave exactly as before.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@fdaviddpt

Copy link
Copy Markdown
Contributor

Thanks for this, @jeongph — really clean fix. 🙏

Reviewed and verified the core logic rather than just reading it:

  • Reproduced _resolve_memory_project_dir() under set -euo pipefail (bash 5 / git 2.46) for non-git, ordinary checkout, and linked worktree — all resolve correctly, and the { read; read; }<<EOF block doesn't trip set -e on EOF.
  • Non-worktree checkouts return PROJECT_DIR verbatim — behavior is byte-identical as claimed.
  • Migration + gitignore keying stays consistent with the redirect; no stray $PROJECT_DIR/.remember derivations elsewhere.

The fail-safe design (common-dir ≠ git-dir + main-checkout-is-worktree guard) is exactly right, and the tests using real git worktree layouts are appreciated.

One minor, non-blocking follow-up (in session-start-hook.sh, not touched by this PR): keying REMEMBER_DIR to the main checkout makes REMEMBER_ROOT = dirname(REMEMBER_DIR) differ from PROJECT_DIR in a legacy-mode worktree. That's what correctly fires the handoff hint (line ~105) — but it also flips the identity-tier branch at line ~88, and the comment at line ~84 ("In legacy mode REMEMBER_ROOT == PROJECT_DIR") is no longer true for worktrees. Real impact is small (needs a root-level identity.md), so it's fine to land this and clean that up separately.

LGTM. 👍

@fdaviddpt
fdaviddpt merged commit 7db7866 into Digital-Process-Tools:main Jul 15, 2026
12 checks passed
fdaviddpt pushed a commit that referenced this pull request Jul 25, 2026
After #127 keyed memory to the main checkout, a session running in a linked
worktree keeps PROJECT_DIR on the worktree while REMEMBER_DIR points into the
main checkout. The legacy guard in 50-git-backup.sh compares those two paths, so
it stopped matching: the hook mistook the project repo for a dedicated backup
repo, deleted the protective .remember/.gitignore (`*`), committed the whole
memory tree onto whatever branch the main checkout had out, and pushed it to the
project's origin — private session notes on a shared remote, on an unrelated
feature branch.

Compare git *common dirs* instead of paths. Every worktree of a repo shares one
common dir, so the plain, worktree and subdirectory layouts are all covered at
once, while a repo genuinely dedicated to memory backup keeps a different common
dir and still activates. Old git without --path-format falls back to resolving
the relative form; if even that fails, previous behaviour is untouched.

Co-Authored-By: Max <noreply>
fdaviddpt pushed a commit that referenced this pull request Jul 25, 2026
…pdate (#133)

Claude Code's plugin updater compares .claude-plugin/plugin.json versions, not
source SHAs. The 0.8.4 release shipped without bumping the manifest, so every
marketplace install kept answering "already at the latest version (0.8.3)" and
silently never received #122/#123 (a silent, self-reinforcing consolidation
stall) or the #127 worktree fix. Users had no signal they were stale — the
updater actively told them they were current.

Bump the manifest to 0.8.5 (carrying #56/#127, #125 and #138), and add
tests/test_version_manifest.py so CI fails whenever the manifest drifts from the
newest released CHANGELOG heading. The bump can no longer be forgotten silently.

Co-Authored-By: Max <noreply>
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.

[Question] plugin with worktress

2 participants