Skip to content

Cache uses absolute paths in hash, making it non-portable across machines #311

Closed

Description

@miklebel-penelope

Cache uses absolute paths in hash, making it non-portable across machines

Problem

graphify.cache.file_hash() includes str(p.resolve()) (the absolute path) in the SHA256 hash that becomes the cache filename:

# graphify/cache.py, line 20-33
def file_hash(path: Path) -> str:
    p = Path(path)
    raw = p.read_bytes()
    content = _body_content(raw) if p.suffix.lower() == ".md" else raw
    h = hashlib.sha256()
    h.update(content)
    h.update(b"\x00")
    h.update(str(p.resolve()).encode())  # <-- absolute path
    return h.hexdigest()

This means the same file with identical content produces different cache keys on different machines (or even different checkout paths on the same machine):

  • Machine A: SHA256(content + /Users/alice/projects/myapp/src/foo.py) → a1b2c3...json
  • Machine B: SHA256(content + /home/bob/myapp/src/foo.py) → d4e5f6...json

Impact

  • Team workflows are broken — committing graphify-out/cache/ to share incremental extraction state across a team is useless because every teammate gets cache misses and does a full re-extraction anyway.
  • CI/CD can't benefit — a CI job that caches graphify-out/cache/ between runs will miss if the checkout path changes (common with ephemeral runners).
  • Same machine, different clones — even cloning the same repo to a different directory invalidates the entire cache.

The cache content (nodes, edges) correctly uses relative paths in source_file, so the extracted data is portable. Only the lookup key is not.

Suggested fix

Use the path relative to the project root instead of the resolved absolute path:

def file_hash(path: Path, root: Path = Path(".")) -> str:
    p = Path(path)
    raw = p.read_bytes()
    content = _body_content(raw) if p.suffix.lower() == ".md" else raw
    h = hashlib.sha256()
    h.update(content)
    h.update(b"\x00")
    try:
        rel = p.resolve().relative_to(root.resolve())
    except ValueError:
        rel = p.resolve()
    h.update(str(rel).encode())
    return h.hexdigest()

This would make cache entries portable across machines while still preventing collisions between files with identical content at different relative paths.

Note: this is a cache-breaking change — existing caches would need to be rebuilt once. A migration path could rehash existing entries, or just document that users should rm -rf graphify-out/cache/ after upgrading.

Related

Environment

  • graphifyy 0.4.10
  • Python 3.14
  • macOS

Activity

  1. safishamsi commented on Apr 13, 2026

    @safishamsi
    Member

    Fixed in v0.4.12. file_hash() now uses the path relative to the project root instead of the resolved absolute path — cache entries are portable across machines, CI runners, and different checkout directories.

    Note: this is a cache-breaking change. Existing cache entries will produce misses on first run after upgrading (they used absolute paths). Just delete graphify-out/cache/ once and re-run:

    pip install --upgrade graphifyy
    rm -rf graphify-out/cache/

    After that one re-extraction, all subsequent runs will hit the shared cache correctly across machines.

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