Skip to content

Latest commit

 

History

History
152 lines (121 loc) · 8.36 KB

codegraph_sync.md

File metadata and controls

152 lines (121 loc) · 8.36 KB

codegraph_sync.sh

Revision: 1 Last modified: 2026-12-04T17:48:16Z

Overview

The §11.4.80 CodeGraph sync automation for a consuming project: after codegraph_update.sh has made the tool current, this runs the canonical sync sequence and writes an evidence ledger.

Run it when: a consuming project's CodeGraph index needs refreshing and you want the outcome recorded in both the project's and the constitution's docs/codegraph/Status.md.

How this differs from its siblings — settled up front, because it is the usual confusion:

Script Scope What it changes
codegraph_update.sh the global npm package nothing in any project; no index
codegraph_sync.sh (this) one consuming project's index codegraph sync . in that project, then validates
codegraph_update_and_resync.sh one project, wider sequence pull + index + commit + push, through the single writer entry

Note this script calls codegraph / npx -y @colbymchenry/codegraph directly. That is the older path; codegraph_update_and_resync.sh is the one that goes through scripts/codegraph/codegraph_safe.sh, which §11.4.78(7) / §11.4.80(5) name as the single writer entry. See "Documentation defect" below.

Prerequisites

  • codegraph on PATH, or npx (the script falls back to npx -y @colbymchenry/codegraph). Neither → exit 2.
  • A .codegraph directory at or above the resolved project root. Absent → exit 2 with Run 'codegraph init' in the consuming project first.
  • Write access to <project>/docs/codegraph/Status.md (directory created with mkdir -p) and to the constitution's docs/codegraph/Status.md.
  • Optional but expected: <project>/scripts/codegraph_validate.sh, executable. Per §11.4.78 step 4 a project MUST ship an anti-bluff validator; when it is missing the script warns and records validate: MISSING rather than pretending the step passed.
  • sed, tr, uniq, tail, grep, mktemp, tee — standard text tools.
  • No env vars are read.

Usage examples

# From inside the consuming project (root found by parent walk)
bash <constitution>/scripts/codegraph_sync.sh

# Explicit project root
bash <constitution>/scripts/codegraph_sync.sh /path/to/project

What you should see: four numbered steps and a ledger entry.

Step 1/4: baseline codegraph status
<file/function/method/struct/interface/constant summary lines>
Step 2/4: codegraph sync .
sync completed in 42s
Step 3/4: post-sync codegraph status
<post-sync summary lines>
Step 4/4: project's scripts/codegraph_validate.sh
<validator output>

There is no --help; the optional argument is a project root, not a flag.

Exit codes

Code Meaning
0 Sync succeeded and validate was PASS (or MISSING with a warning)
1 codegraph sync failed, or validate returned FAIL
2 Environment problem: codegraph/npx absent, or no .codegraph directory found at or above the root

Edge cases

  • No .codegraph anywhere. The script walks up from the given/current directory looking for one; if none is found it errors and exits 2. It never runs init itself — initialising is the project's job (or codegraph_update_and_resync.sh's, which does init when the DB is absent).
  • codegraph not installed but npx present. It uses npx -y @colbymchenry/codegraph, which downloads on demand. Slower, and it needs the network.
  • A missing validator. validate: MISSING, a WARNING naming the requirement, and exit 0. Deliberate: a consumer that has not written its validator should not be blocked, but the gap is visible in the ledger.
  • A spinner that would corrupt the ledger. This is the script's most important hardening and it is documented in the source for a reason. A prior run captured a 198 MB spinner blob into Status.md: codegraph's "Scanning files…" spinner writes carriage-return-laden ANSI output to stderr, and because it uses \r with no \n, the whole blob was one logical line — so a line-bounded grep | tail -N let that single line grow to ~180 MB, and tee -a wrote it verbatim. That corrupted Status.md and broke the CM-DOCS-COMPOSITE-SYNC / CM-UNIVERSAL-MARKDOWN-EXPORT-SYNC gates. sanitize_tail now bounds every raw output tail before it reaches the ledger: strip ANSI, convert \r to newlines, drop blank lines, collapse repeated identical lines (spinner spam), then cap at 40 lines and 8192 bytes.
  • codegraph sync fails. The full log goes to stderr, a **FAIL** section with the sanitised tail is appended to the ledger, and the script exits 1.
  • The log temp file is mktemp'd and removed by an EXIT trap.
  • The root argument is a directory with no .codegraph → the upward walk may still find the enclosing project's .codegraph, which is usually what you want. If that surprises you, pass the project root explicitly.

Internal behaviour

  1. Resolve SCRIPT_DIR and CONST_ROOT; set CONST_STATUS to the constitution's docs/codegraph/Status.md.
  2. Resolve PROJECT_ROOT: $1, else $PWD; if it has no .codegraph, walk up to / looking for one. Still none → exit 2.
  3. mkdir -p both status directories; compute a UTC timestamp; mktemp the log with an EXIT trap.
  4. Define sanitize_tail (sed ANSI strip → tr '\r' '\n' → drop blanks → uniq → tail -n 40 → tail -c 8192) and append_log (writes the same entry to the project ledger and appends to the constitution ledger).
  5. Require codegraph or npx; pick CG="codegraph" or CG="npx -y @colbymchenry/codegraph"; cd to the project.
  6. Step 1/4 — status, keep the last 10 lines mentioning file|function|method|struct|interface|constant.
  7. Step 2/4 — sync ., timed; failure → **FAIL** ledger entry and exit 1.
  8. Step 3/4 — status again, same summary extraction.
  9. Step 4/4 — run <project>/scripts/codegraph_validate.sh when executable; record PASS / FAIL / MISSING.
  10. Append the ledger entry (duration, baseline status, post-sync status, validate verdict) and exit 1 on validate: FAIL, else 0.

Related scripts

  • scripts/codegraph_update.sh — brings the tool current first. docs/scripts/codegraph_update.md.
  • scripts/codegraph_update_and_resync.sh — the operator one-shot (pull, index via the writer entry, commit, push). docs/scripts/codegraph_update_and_resync.md.
  • scripts/codegraph/codegraph_safe.sh — the single writer entry (§11.4.78(7), §11.4.80(5)). This script does not use it; see the defect note.
  • scripts/codegraph/codegraph_guard.sh, scripts/codegraph/codegraph_scope_guard.py, scripts/index_watch.py, scripts/fk_cascade_probe.py — the index guards and probes.
  • docs/codegraph/Status.md — the ledger this script appends to.
  • §11.4.78 (CodeGraph indexing), §11.4.80 (regular update), §11.4.107 / §11.4.201 (evidence, not exit codes).

Documentation defect found (reported, not fixed — out of this pass's file scope)

codegraph_sync.sh invokes codegraph status/sync directly (or via npx -y @colbymchenry/codegraph), while §11.4.78(7) and §11.4.80(5) require every init/index/sync to go through the single writer entry scripts/codegraph/codegraph_safe.sh, and CM-CODEGRAPH-SAFE-INDEX-PATH forbids the stock-invocation shape. The sibling codegraph_update_and_resync.sh was explicitly fixed to use the writer entry (its header cites review 2026-11-04 F-CG1 and commit 00d4d50). codegraph_sync.sh still shows the older pattern and also writes its ledger without the writer entry's lock, so two concurrent runs can sync the same index. Its header pre-dates the fix. Worth an issue; the guide above documents what the script does, not what it should do.

Last verified

2026-12-04 — bash -n clean; the script was not executed in this pass, because it rewrites a project's CodeGraph index and appends to tracked ledgers:

$ bash -n scripts/codegraph_sync.sh && echo PARSE-OK
PARSE-OK

$ grep -n 'SANITIZE_MAX_BYTES\|SANITIZE_MAX_LINES' scripts/codegraph_sync.sh | head -3

A reader can verify the refusal path without touching an index, by running it in a directory whose roots contain no .codegraph:

cd /tmp && bash <constitution>/scripts/codegraph_sync.sh
# ERROR: no .codegraph directory found at or above /tmp.  → exit 2