Skip to content

session_dir_slug does not truncate at 200 chars + hash, so deep project paths silently never save #157

Closed

Description

@fdaviddpt

Description

session_dir_slug() never truncates. Claude Code does: past 200 characters it keeps the first 200 and appends a hash of the path. So for any project whose slugged path exceeds 200 characters, the computed slug does not match the directory Claude Code created, SESSION_DIR points at nothing, the hook finds no transcript, and memory silently never saves.

This is the same failure mode as #144, reached by a different route — long path instead of non-ASCII path — and it is not fixed by #156.

Reproduction

Confirmed on a real session, not inferred. A project path 262 characters long:

$ cd /very/deep/…/gggggggggggggggggggg     # 262 chars
$ claude -p 'reply with the single word ok'
ok

$ ls -t ~/.claude/projects/ | head -1 | awk '{print length, $0}'
207 -private-tmp-…-dddddddddddddddddddd--on930v

Claude Code wrote a 207-character directory. session_dir_slug returns the full 262-character slug. No overlap, so nothing is ever found.

The algorithm

From the shipped CLI bundle:

var jXA = 200;

function JXA(A) {
  let q = A.replace(/[^a-zA-Z0-9]/g, "-");
  if (q.length <= jXA) return q;
  let K = 0;
  for (let _ = 0; _ < A.length; _++) K = (K << 5) - K + A.charCodeAt(_), K |= 0;
  return `${q.slice(0, jXA)}-${Math.abs(K).toString(36)}`;
}

So: djb2-ish rolling hash over the original path (not the slug), charCodeAt so UTF-16 code units, forced to signed 32-bit by |0 each round, then Math.abs(...).toString(36).

I reimplemented that and it reproduces the observed directory name exactly (…dddddddddddddddddddd--on930v), so the algorithm above is verified rather than read-and-hoped.

Suggested fix

Extend session_dir_slug in scripts/detect-tools.sh: if the slug exceeds 200 characters, emit ${slug:0:200}-${hash}. The hash needs 32-bit signed wraparound and base36, which is awkward in pure bash — $PYTHON is already a hard dependency of every hook, and this branch is only reached for long paths, so paying a subprocess there costs nothing on the common path.

Worth pinning with a test that builds a >200-character path, since this class of bug is invisible until someone's checkout happens to be deep.

Notes

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