Skip to content

ask without the repo-root positional silently wipes the graph: freshness check anchors on cwd and rebuilds 0 nodes (cards survive) #438

Description

@docxology

Version: @nanonets/graft 0.18.0 (npm global, /opt/homebrew), Node 24, macOS arm64 (darwin 25.6.0)

Summary

Running graft --dir <context-dir> ask "<query>" without the repo-root positional silently wipes the graph: ask anchors the repo root on the current working directory, the pre-query freshness check concludes every file changed, and it rebuilds an empty graph (0 nodes) — while the per-file card markdown survives, masking the damage. Exit code is 0 throughout.

Reproduction (self-contained, ~10 s)

# sources: a small git repo outside the "hub"
mkdir -p /tmp/gresrc/repo/src && cd /tmp/gresrc && git init -q repo
printf 'export function utilFn(n: number): number {\n  return n * 2;\n}\n' > repo/src/util.ts
printf 'import { utilFn } from "./util";\nexport function mainFn(): number {\n  return utilFn(21);\n}\n' > repo/src/main.ts

# "hub": a separate git repo holding only the context dir + a symlink to the source repo
mkdir -p /tmp/grepro/graft/demo && cd /tmp/grepro && git init -q .
ln -s /tmp/gresrc/repo myentry
graft build --dir graft/demo myentry          # ✓ wiring: 4 nodes, 4 edges, 2 cards — wiring.json 2440 bytes

# the buggy form: no repo-root positional
graft --dir graft/demo ask "who calls utilFn" # [graft] refreshed the graph (2 files changed) → empty answer, exit 0
wc -c graft/demo/.graph/wiring.json           # 232 bytes

Observed after the buggy invocation:

[graft] refreshed the graph (2 files changed) before answering
graft ask — "who calls utilFn"  (empty)
⚠ structural index: no entries for 'utilFn' — showing lexical matches; ...
no matching nodes — try different words, or `graft build` if graft/ is empty
  • graft/demo/.graph/wiring.json shrinks 2440 → 232 bytes: nodeCount: 0, edgeCount: 0, languages: []
  • graft/demo/src/util.md / main.md (the per-file cards) are untouched — a user who checks the context dir at a glance sees nothing wrong
  • exit code 0
  • the only tell before the answer degrades is one info line: "refreshed the graph (2 files changed)" — phrased as if the sources changed, when in fact the refresh just re-rooted the graph on the wrong directory

Expected vs actual

  • Expected: ask resolves the repo root from the graph's recorded source root (what the original graft build --dir graft/demo myentry was given), answers structurally, and touches nothing. If it cannot reconcile cwd with the recorded root, it should warn/fail, not rebuild.
  • Actual: the freshness check treats cwd as the repo root. In the repro (and in our real layout) the cwd's file set is unrelated to the graph, so the refresh re-roots and rebuilds empty, destroying a valid graph in under a second.

Why this is easy to hit

The hub-of-graphs layout that triggered it for us: one git repo that is a graph hub, graft/<entry>/ context dirs, and <entry> symlinks to sibling source repos — one graph per repo. The natural muscle memory is graft --dir graft/<entry> ask "<query>"; the required positional is easy to omit. We hit this on a 9-repo hub (graphs of up to 1,651 nodes) — one ask emptied a graph while the surviving cards made it look intact.

Recovery

Both of these restore the graph in place:

graft --dir graft/<entry> ask "<query>" <entry>   # corrected ask form rebuilds it before answering
graft build --no-gitignore --no-ignore --dir graft/<entry> <entry>

(The 232-byte wiring.json is the emptied signature.)

Suggested fix

Anchor the pre-query freshness check on the graph's recorded source root rather than the cwd — e.g. persist the root (and/or its identity) that the last build/ask used in .graph/wiring.json meta or a sibling file, and compare against that. If cwd ≠ recorded root, either warn and re-anchor, or refuse with a clear message like "graph was built for ; pass it as an argument" — never rebuild from an unrelated cwd into the graph directory.

Bonus safety net: a rebuild that would take a non-empty graph to 0 nodes could require confirmation or at least print a loud warning — "silently emptied" is the worst failure mode here because the surviving cards mask it.

Activity

  1. changed the title [-]ask` without the repo-root positional silently wipes the graph: freshness check anchors on cwd and rebuilds 0 nodes (cards survive)[/-] [+]ask without the repo-root positional silently wipes the graph: freshness check anchors on cwd and rebuilds 0 nodes (cards survive)[/+] on Sep 21, 2026
  2. hr1juldey commented on Nov 18, 2026

    @hr1juldey

    Fix up as a draft: the graph now records its source root, and the pre-query freshness check refuses to re-root it anywhere else — the issue's hub layout (symlinked entries) is handled by realpath, and old graphs keep the old behavior exactly. PR: #857.

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