Skip to content

Latest commit

 

History

History
138 lines (105 loc) · 7 KB

codegraph_update_and_resync.md

File metadata and controls

138 lines (105 loc) · 7 KB

codegraph_update_and_resync.sh

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

Overview

The operator's one-shot: pull, refresh the CodeGraph index, commit, push.

Run it when: you want the whole local cycle in one command — bring the work tree up to date, re-index, commit what changed, and push.

The important design fact: the index refresh goes only through the single writer entry scripts/codegraph/codegraph_safe.sh (§11.4.78(7), §11.4.80(5)), always with --wait. Stock codegraph is never used as a fallback.

Why --wait matters (review 2026-11-04 F-CG1): a BULK run is detached. Without --wait, codegraph_safe.sh returns 0 as soon as the indexer starts, so this script would commit, push and exit 0 before the index exists — and would never see the indexer's failure. With --wait the exit status is the run's final result.

How this differs from its siblings:

Script Does
codegraph_update.sh updates the global npm package only; no index, no git
codegraph_sync.sh status → sync . → status → validate, for one project; calls stock codegraph; no git
codegraph_update_and_resync.sh (this) pull → index (via the writer entry, --wait) → commit → push

The original version of this cycle (commit 00d4d50) was a one-liner calling stock codegraph init/sync/index and masking every failure with || true — the pattern CM-CODEGRAPH-SAFE-INDEX-PATH forbids. A writer failure is now reported and becomes this script's exit status.

Prerequisites

  • bash, git, and a project root (git rev-parse --show-toplevel, falling back to pwd).
  • scripts/codegraph/codegraph_safe.sh present and executable — or CG_UPDATE_SAFE_BIN pointing at an equivalent. Missing → exit 127 with writer entry not found: <path> (stock codegraph is never used).
  • pull_all, commit and push_all on PATH or as shell functions in the calling environment. These are the operator's own helper commands; they are UNKNOWN to this repository and not defined by it. If they are absent the parenthesised (... || true) subshells fail silently and the script proceeds to the index step regardless — which is a real footgun, documented rather than papered over.
  • No env vars are required; CG_UPDATE_SAFE_BIN is the only one read.

Usage examples

# From the project you want to update (the helpers must be available)
bash scripts/codegraph_update_and_resync.sh

# Point at a different writer entry (the tests do this)
CG_UPDATE_SAFE_BIN=/path/to/codegraph_safe.sh bash scripts/codegraph_update_and_resync.sh

What you should see: the output of pull_all, then codegraph_safe.sh's output for init or sync, then the output of commit and push_all. On a writer failure:

codegraph_update_and_resync: codegraph_safe.sh sync FAILED (rc=1)

There is no --help and no flags.

Exit codes

Code Meaning
0 The index run finished and verified
(writer's status) Anything else is codegraph_safe.sh's own final exit status, passed through unchanged
127 The writer entry was not found at CG_UPDATE_SAFE_BIN / scripts/codegraph/codegraph_safe.sh

Note the asymmetry: pull_all / commit / push_all failures are swallowed (|| true, unchanged from the operator's helpers) and do not affect the exit code. Only the index run decides it. A 0 from this script therefore means "the index is current", not "the push succeeded" — check the helper output for that.

Edge cases

  • No .codegraph/codegraph.db → OP=init; otherwise OP=sync. The choice is made once, before the writer runs.
  • The writer entry is missing. Prints the path it looked for, states that stock codegraph is never used, and sets the exit status to 127. It does not fall back — that is the point.
  • A BULK run blocks until it ends, which can be hours. This is why --wait is mandatory here: the alternative is a silent early return.
  • pull_all fails → the failure is printed by the helper but || true swallows the status; the index step still runs and commit/push_all still run. If that sequencing is not what you want, run the steps yourself.
  • commit and push_all are chained with &&, so a failing commit skips push_all — but, again, neither affects the exit status.
  • Inherited git environment. The script first unsets the repository-locating GIT_* variables (git-env-hermetic), so git rev-parse --show-toplevel cannot be redirected at another repository by your environment.
  • Run from a subdirectory. git rev-parse --show-toplevel finds the project root, so the writer entry operates on the right project; pull_all/commit/push_all run from wherever you are.

Internal behaviour

  1. Unset the inherited repository-locating GIT_* variables.
  2. Resolve HERE; SAFE = CG_UPDATE_SAFE_BIN, else $HERE/codegraph/codegraph_safe.sh.
  3. (pull_all || true).
  4. PROJECT = git rev-parse --show-toplevel, else pwd.
  5. OP = sync when $PROJECT/.codegraph/codegraph.db exists, else init.
  6. If the writer entry exists → bash "$SAFE" --wait "$OP" and capture CG_RC. Else print the refusal and set CG_RC=127.
  7. On non-zero, print codegraph_update_and_resync: codegraph_safe.sh $OP FAILED (rc=$CG_RC) to stderr.
  8. (commit || true) && (push_all || true).
  9. exit "$CG_RC".

Related scripts

  • scripts/codegraph/codegraph_safe.sh — the single writer entry this script uses; it classifies the run BULK or INCREMENTAL by pending backlog and supports --wait. docs/scripts/codegraph_safe.md.
  • scripts/codegraph/codegraph_guard.sh — the companion guard (docs/scripts/codegraph_guard.md).
  • scripts/codegraph_update.sh — updates the npm package. docs/scripts/codegraph_update.md.
  • scripts/codegraph_sync.sh — the older per-project sync that still calls stock codegraph (docs/scripts/codegraph_sync.md — see the defect note there).
  • scripts/codegraph/tests/test_update_and_resync.sh, scripts/codegraph/tests/test_guard.sh (case G9).
  • §11.4.78(7), §11.4.80(5) (single writer entry), CM-CODEGRAPH-SAFE-INDEX-PATH.

Last verified

2026-12-04 — bash -n clean; the script was not executed in this pass, because a run pulls, commits, pushes and rewrites a CodeGraph index:

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

$ grep -n 'CG_UPDATE_SAFE_BIN\|--wait' scripts/codegraph_update_and_resync.sh

A reader can verify the refusal path safely: point CG_UPDATE_SAFE_BIN at a path that does not exist and inspect the 127 behaviour — the writer is never reached, so no index and no git state is touched:

CG_UPDATE_SAFE_BIN=/nonexistent bash scripts/codegraph_update_and_resync.sh; echo "RC=$?"
# codegraph_update_and_resync: writer entry not found: /nonexistent (stock codegraph is never used)
# RC=127

Beware: this still runs your pull_all / commit / push_all helpers, so only do it in a scratch clone.