Skip to content

feat(graph): provision tree-sitter into embed-deps so the auto-build hook works#323

Open
efenocchi wants to merge 1 commit into
mainfrom
fix/graph-deps-in-embed-deps
Open

feat(graph): provision tree-sitter into embed-deps so the auto-build hook works#323
efenocchi wants to merge 1 commit into
mainfrom
fix/graph-deps-in-embed-deps

Conversation

@efenocchi

@efenocchi efenocchi commented Jul 18, 2026

Copy link
Copy Markdown
Collaborator

Problem

The code-graph auto-build hook (graph-on-stop, on codex/cursor/hermes) resolves the native tree-sitter module at runtime from the shared ~/.hivemind/embed-deps/node_modules that each agent's plugin symlinks to. But ensureSharedDeps only installs @huggingface/transformers there — tree-sitter is never provisioned into embed-deps, so on a real install the graph never auto-builds. The hook just degrades to a graceful skip (thanks to the crash fix in #321).

Note: the CLI hivemind graph build / graph init does work in prod (verified end-to-end via a clean npm pack install) because it resolves tree-sitter from its own package. This PR fixes the hook path.

Fix

  • ensureGraphDeps() (src/cli/embeddings.ts) — provisions the tree-sitter parsers into embed-deps where the hook looks. tree-sitter is a native module (needs its node-gyp-build / node-addon-api siblings resolved), so a scoped npm install lays down the full tree rather than symlinking a single dir.
    • Idempotent: a .graph-deps specs-hash marker + a per-package presence check skip the re-download and catch partial installs / version bumps.
    • Best-effort: the whole body is wrapped — any failure is logged and swallowed, never aborting the caller.
    • Two-phase native provisioning: npm install --ignore-scripts (so a platform without a prebuild — arm64 / Node 24 — doesn't fail the download), then scripts/ensure-tree-sitter.mjs always runs to validate bindings + compile from source where needed (fast no-op on healthy prebuilds; also repairs an interrupted / ABI-mismatched install).
    • No drift: versions read from the package's own optionalDependencies.
    • Decoupled from the ~600 MB embeddings download — installs only the parsers (tens of MB).
  • ensureSharedDeps now MERGES the shared package.json instead of overwriting it, so a prior graph-deps declaration survives a first-time embeddings install.
  • Wired into installEmbeddings() and hivemind graph init (best-effort).

Verification

End-to-end in an isolated sandbox (HOME override):

  • hivemind graph initensureGraphDeps installs 12 packages into the sandbox embed-deps; all 10 parsers present + marker written.
  • A second graph initalready present (idempotent, no reinstall).
  • The codex graph-on-stop hook, with embed-deps now populated, builds the graph: Nodes: 5, Edges: 2, snapshot written, exit 0.
  • Full suite: 5546 passed.
  • Codex review: both prior HIGH findings (native recovery never running; idempotency accepting broken/stale installs) resolved; approach confirmed correct.

Known limitation (pre-existing, unchanged)

npm is invoked as bare npm, which does not resolve on Windows — identical to the existing ensureSharedDeps invocation. A proper cross-platform npm exec is out of scope here (it affects both paths).

Session Context

Session transcript

Summary by CodeRabbit

  • New Features

    • Graph initialization now automatically provisions required code-graph parser dependencies.
    • Embeddings and graph features share dependency setup, preserving previously installed parsers.
    • Dependency setup can repair native parser bindings automatically.
  • Bug Fixes

    • Missing or incomplete graph dependencies are restored during setup.
    • Dependency provisioning failures no longer prevent embeddings installation or graph initialization.

…hook works

The code-graph auto-build hook (graph-on-stop, on codex/cursor/hermes)
resolves the native tree-sitter module at runtime from the shared
~/.hivemind/embed-deps/node_modules that each agent's plugin symlinks to. But
`ensureSharedDeps` only installs @huggingface/transformers there, so on a real
install tree-sitter is absent and the graph never auto-builds — the hook just
degrades to a graceful skip (the CLI `hivemind graph build` works because it
resolves tree-sitter from its own package). This provisions the parsers where
the hook actually looks.

tree-sitter is a native module: symlinking the package dir alone is not enough
(it needs its own node-gyp-build / node-addon-api siblings resolved), so we run
a scoped npm install and let npm lay down the full tree.

- src/cli/embeddings.ts: new `ensureGraphDeps()` — idempotent (specs-hash
  marker + per-package presence check catch partial installs and version bumps)
  and best-effort (any failure is logged and swallowed, never aborts the
  caller). Two-phase native provisioning: `npm install --ignore-scripts` so a
  platform without a prebuild (arm64 / Node 24) doesn't fail the download, then
  scripts/ensure-tree-sitter.mjs ALWAYS runs to validate bindings + compile
  from source where needed (fast no-op on healthy prebuilds; also repairs an
  interrupted / ABI-mismatched install). Versions are read from the package's
  own optionalDependencies (`treeSitterSpecs`) so embed-deps never drifts from
  what the bundles were built against. Decoupled from the ~600 MB embeddings
  download — installs only the parsers (tens of MB).
- src/cli/embeddings.ts: `ensureSharedDeps` now MERGES the shared package.json
  instead of overwriting it, so a prior graph-deps declaration survives a
  first-time embeddings install.
- Wired into `installEmbeddings()` and `hivemind graph init` (best-effort).
- tests: treeSitterSpecs (version derivation, no drift, excludes transformers)
  + isGraphDepsInstalled (all-parsers-present / partial-install).

Verified e2e: `graph init` provisions tree-sitter into a sandbox embed-deps
(12 pkgs), a second run skips the reinstall (idempotent), and the codex
graph-on-stop hook then builds the graph (Nodes: 5, Edges: 2). Full suite 5546
passed. Codex review: both prior HIGH findings resolved.

Known limitation (pre-existing, unchanged): npm is invoked as bare `npm`,
which does not resolve on Windows — same as the existing ensureSharedDeps
invocation; a proper cross-platform npm exec is out of scope here.
@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The CLI now provisions tree-sitter parser dependencies in shared embedding dependencies, heals native bindings, preserves merged dependency declarations, and invokes provisioning during embeddings installation and graph initialization. New tests cover parser specification extraction and installation detection.

Changes

Graph dependency provisioning

Layer / File(s) Summary
Tree-sitter specification and provisioning
src/cli/embeddings.ts
Adds helpers to derive sorted parser specs, detect installed parsers, install missing packages using a specs marker, and heal native bindings.
CLI and shared dependency integration
src/cli/embeddings.ts, src/cli/index.ts
Merges shared dependencies and invokes best-effort graph provisioning during embeddings installation and graph init.
Graph dependency helper tests
tests/shared/graph-deps.test.ts
Tests parser filtering, sorting, missing metadata, empty specs, partial installs, and complete installation detection.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant CLI
  participant ensureGraphDeps
  participant SharedDependencies
  participant npm
  participant NativeHealing
  CLI->>ensureGraphDeps: provision graph dependencies
  ensureGraphDeps->>SharedDependencies: read and update shared package metadata
  ensureGraphDeps->>npm: install tree-sitter specs with --ignore-scripts
  npm-->>SharedDependencies: write parser packages
  ensureGraphDeps->>NativeHealing: heal native bindings
  NativeHealing-->>CLI: complete best-effort provisioning
Loading

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly states the graph/tree-sitter embed-deps provisioning change and its purpose.
Description check ✅ Passed The description covers the problem, fix, verification, and limitation, though it doesn't use the template's exact headings.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/graph-deps-in-embed-deps

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Comment thread src/cli/embeddings.ts
cwd: SHARED_DIR,
stdio: "inherit",
});
writeFileSync(marker, wantKey);
@github-actions

Copy link
Copy Markdown
Contributor

Coverage Report

Scope: files changed in this PR. Enforced threshold: 90% per metric (per file via vitest.config.ts).

Status Category Percentage Covered / Total
🔴 Lines 67.46% (🎯 90%) 284 / 421
🔴 Statements 69.34% (🎯 90%) 346 / 499
🔴 Functions 79.59% (🎯 90%) 39 / 49
🔴 Branches 72.35% (🎯 90%) 212 / 293
File Coverage — 2 files changed
File Stmts Branches Functions Lines
src/cli/embeddings.ts 🔴 45.5% 🔴 46.7% 🔴 77.3% 🔴 44.0%
src/cli/index.ts 🔴 83.9% 🔴 83.7% 🔴 81.5% 🔴 82.7%

Generated for commit f50603a.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/cli/embeddings.ts`:
- Around line 215-225: The shared-dependency installation in the embeddings
install flow must disable package lifecycle scripts. Update the npm install
invocation associated with the package.json written after `ensureGraphDeps` to
include the `--ignore-scripts` option, preserving the existing dependency merge
and installation behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: cfcbd462-e011-4233-b06e-6e5f4421c4ab

📥 Commits

Reviewing files that changed from the base of the PR and between 2611cd6 and 52ec8eb.

📒 Files selected for processing (3)
  • src/cli/embeddings.ts
  • src/cli/index.ts
  • tests/shared/graph-deps.test.ts

Comment thread src/cli/embeddings.ts
Comment on lines +215 to +225
// Merge, don't clobber: ensureGraphDeps may have already declared the
// tree-sitter parsers here. Overwriting with transformers-only would drop
// that declaration and let a subsequent reconcile prune the parsers.
const pkgPath = join(SHARED_DIR, "package.json");
let existing: { dependencies?: Record<string, string> } = {};
try { existing = JSON.parse(readFileSync(pkgPath, "utf8")); } catch { /* none yet */ }
writeJson(pkgPath, {
name: "hivemind-embed-deps",
version: "1.0.0",
private: true,
dependencies: { [TRANSFORMERS_PKG]: TRANSFORMERS_RANGE },
dependencies: { ...(existing.dependencies ?? {}), [TRANSFORMERS_PKG]: TRANSFORMERS_RANGE },

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

git ls-files src/cli/embeddings.ts
printf '\n--- outline ---\n'
ast-grep outline src/cli/embeddings.ts --view expanded
printf '\n--- relevant search ---\n'
rg -n --no-heading "ensureGraphDeps|ensureSharedDeps|installEmbeddings|ignore-scripts|npm install|writeJson|package.json" src/cli/embeddings.ts
printf '\n--- file slice 1 ---\n'
sed -n '1,320p' src/cli/embeddings.ts

Repository: activeloopai/hivemind

Length of output: 18495


🏁 Script executed:

#!/bin/bash
set -euo pipefail

sed -n '180,270p' src/cli/embeddings.ts
printf '\n--- search related install steps ---\n'
rg -n --no-heading "ensureGraphDeps|ensureSharedDeps|installEmbeddings|ignore-scripts|tree-sitter|parser" src/cli/embeddings.ts src/cli -g '!**/dist/**'

Repository: activeloopai/hivemind

Length of output: 11864


Keep lifecycle scripts disabled for the shared-dependency install.

If shared/package.json already includes tree-sitter parsers, this npm install can run their native lifecycle scripts before ensureGraphDeps() reaches the heal step, so embeddings install can still fail on the platforms this flow is meant to handle. Add --ignore-scripts here.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/cli/embeddings.ts` around lines 215 - 225, The shared-dependency
installation in the embeddings install flow must disable package lifecycle
scripts. Update the npm install invocation associated with the package.json
written after `ensureGraphDeps` to include the `--ignore-scripts` option,
preserving the existing dependency merge and installation behavior.

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.

2 participants