Revision: 1 Last modified: 2026-12-04T17:48:16Z
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.
codegraphonPATH, ornpx(the script falls back tonpx -y @colbymchenry/codegraph). Neither → exit2.- A
.codegraphdirectory at or above the resolved project root. Absent → exit2withRun 'codegraph init' in the consuming project first. - Write access to
<project>/docs/codegraph/Status.md(directory created withmkdir -p) and to the constitution'sdocs/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 recordsvalidate: MISSINGrather than pretending the step passed. sed,tr,uniq,tail,grep,mktemp,tee— standard text tools.- No env vars are read.
# 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/projectWhat 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.
| 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 |
- No
.codegraphanywhere. The script walks up from the given/current directory looking for one; if none is found it errors and exits2. It never runsinititself — initialising is the project's job (orcodegraph_update_and_resync.sh's, which doesinitwhen the DB is absent). codegraphnot installed butnpxpresent. It usesnpx -y @colbymchenry/codegraph, which downloads on demand. Slower, and it needs the network.- A missing validator.
validate: MISSING, aWARNINGnaming the requirement, and exit0. 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\rwith no\n, the whole blob was one logical line — so a line-boundedgrep | tail -Nlet that single line grow to ~180 MB, andtee -awrote it verbatim. That corruptedStatus.mdand broke theCM-DOCS-COMPOSITE-SYNC/CM-UNIVERSAL-MARKDOWN-EXPORT-SYNCgates.sanitize_tailnow bounds every raw output tail before it reaches the ledger: strip ANSI, convert\rto newlines, drop blank lines, collapse repeated identical lines (spinner spam), then cap at 40 lines and 8192 bytes. codegraph syncfails. The full log goes to stderr, a**FAIL**section with the sanitised tail is appended to the ledger, and the script exits1.- The log temp file is
mktemp'd and removed by anEXITtrap. - 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.
- Resolve
SCRIPT_DIRandCONST_ROOT; setCONST_STATUSto the constitution'sdocs/codegraph/Status.md. - Resolve
PROJECT_ROOT:$1, else$PWD; if it has no.codegraph, walk up to/looking for one. Still none → exit2. mkdir -pboth status directories; compute a UTC timestamp;mktempthe log with anEXITtrap.- Define
sanitize_tail(sedANSI strip →tr '\r' '\n'→ drop blanks →uniq→tail -n 40→tail -c 8192) andappend_log(writes the same entry to the project ledger and appends to the constitution ledger). - Require
codegraphornpx; pickCG="codegraph"orCG="npx -y @colbymchenry/codegraph";cdto the project. - Step 1/4 —
status, keep the last 10 lines mentioningfile|function|method|struct|interface|constant. - Step 2/4 —
sync ., timed; failure →**FAIL**ledger entry and exit1. - Step 3/4 —
statusagain, same summary extraction. - Step 4/4 — run
<project>/scripts/codegraph_validate.shwhen executable; recordPASS/FAIL/MISSING. - Append the ledger entry (duration, baseline status, post-sync status, validate verdict) and exit
1onvalidate: FAIL, else0.
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).
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.
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 -3A 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