SKILL
Rename a Python symbol/module with preflight and verification. Every source write is journaled by rename_snapshot.py: a post-edit failure restores all-or-nothing — each file the run changed gets its original bytes and a recorded git mv is undone with git mv back, but a file changed outside the run's recorded edits is a conflict, and any conflict keeps every file as it is and is reported; Step 7 lists restored files, conflicts and remaining work. Coverage:
- Definition site (def/class line)
__all__re-exports in__init__.pyfiles- Import call sites across all callers (fn-rdeps + symbol line-range narrowing)
- Non-call references — imports, base classes,
isinstancetargets, callback values, ancestor__init__.pyre-exports — in every file that imports the symbol's module or a re-exporting ancestor package (rdeps) - Sphinx docstring cross-refs across
.pyand.rst - Optional pyDeprecate
@deprecatedalias (--deprecate; needspyDeprecate) - Optional hard-delete when exhaustive=true + zero callers
Subcommands:
symbol <old_qname> <new_qname>— function/class/method. qname bare (MyClass) or qualified (MyClass.method); matches symbol-localqualified_name. Module-qualified form (module::symbol) is not accepted byfind-symbol.module <old_module_path> <new_module_path>— dotted path (mypackage.old_name). Renames file + all import lines.
Flags:
--dry-run— show change sites; no edits--deprecate[=<decorator>]— symbol only: preserve old name as pyDeprecate@deprecatedwrapper → new; needspyDeprecate. Bare derives decorator from symbol type; value pins it, single-quoted (--deprecate='@deprecated_class(...)').--no-deprecatedeclines the wrapper.--since <ver>/--removed-in <ver>— deprecation decorator values; default"?"--remove-if-no-callers— symbol only: confirm then delete definition when exhaustive=true + zero callers
Hard limits (static-analysis boundary):
getattr(obj, "old_name")— statically unbound string; Step 7 emits grep advisory- Cross-repo callers — out of scope; public APIs need
--deprecate+ semver bump - Importers beyond one re-export hop, string references, and same-name symbols of another origin — never edited automatically; Step 6 lists residual whole-word
OLD_NAMEin Python files for symbols (methods scoped to reference-site files), or literal dottedOLD_REFin Python, stub, documentation and config files for modules. Step 7 reports the count and unresolved references
IDE/LSP coverage preview → --dry-run. Only 1:1 renames.
NOT for: index builds (/codemap-py:scan-codebase); query-only work (/codemap-py:query-code); non-Python; ABC/Protocol symbols with subclass overrides. Static imports don't track overrides: manually review fn-rdeps; rename overrides explicitly. No --index <path>: always default project index. Monorepo --root <pkg> index from /codemap-py:scan-codebase isn't auto-resolved — resolve_proj_index.py derives PROJ only from git-root basename. Before rename, confirm path via resolve_index_env.py or keep --root consistent with default scans.
Step 0: Parse arguments¶
Question delivery: every AskUserQuestion uses at most 6 plain lines (target 600 characters, hard cap 1,500), no table, pipe layout, or code fence. Include the decision headline, counts and named content file path when applicable. Options carry labels and one-line descriptions, no previews; preserve existing answer mapping and keep the recommended option second. For new candidate/blast artifacts below, use $CSID for <session-id> and probe the path before Writing; if taken, append -2, then -3, … until free. Keep that resolved path for the matching question.
Parse $ARGUMENTS in one Bash block; parse_rename_args.py tokenizes it once (shell quoting rules), validates it and writes every value to a project-qualified sentinel. Later steps read sentinels because shell vars die at each Bash() boundary.
The arguments are pasted as text into the double-quoted word handed to parse_rename_args.py below, so a ", ` or $ in them ends or expands that word (--deprecate="@deprecated_class(...)" is a bash syntax error before the helper runs). If the arguments contain any of them, do not run the block: print ! Quote --deprecate values with single quotes (--deprecate='@deprecated_class(...)') and pass versions via --since/--removed-in; a double quote, backtick or $ breaks argument parsing. Re-invoke. and stop.
# timeout: 5000
PLUGIN_ROOT=$(python "${CLAUDE_PLUGIN_ROOT}/bin/resolve_plugin_root.py" --plugin-name codemap-py) || exit $?; export PLUGIN_ROOT
export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}"
_CM_PROJ=$(git rev-parse --show-toplevel 2>/dev/null | xargs basename 2>/dev/null || echo "cm")
# one shlex parse — awk/grep/sed re-splits fired on flags inside values; refs reach git mv, so validated there
python "$PLUGIN_ROOT/bin/parse_rename_args.py" "$ARGUMENTS" --output-prefix "codemap-${_CM_PROJ}" || exit 1
Non-zero exit → print the helper's stderr unchanged; stop. It rejects: missing/unknown subcommand (Usage line); missing old/new ref; flag-shaped ref (looks like a flag, not a target — check argument order); a ref that is not a dotted Python name, incl. module::symbol (own message); symbol-only flags under module; --since/--removed-in without a value; unquoted text left over; --deprecate with --remove-if-no-callers (conflicting flags). Unknown -- tokens print ! Unknown flag(s): --<token>. Supported flags: --dry-run, --deprecate[=<decorator>], --no-deprecate, --since, --removed-in, --remove-if-no-callers. Re-invoke with corrected flags. Never AskUserQuestion here: fail-fast keeps the worst-case AskUserQuestion path at 4 (post-query stale index, multiple-matches, apply/dry-run, hard-delete confirmation).
Step 1: Validate project¶
# timeout: 5000
PLUGIN_ROOT=$(python "${CLAUDE_PLUGIN_ROOT}/bin/resolve_plugin_root.py" --plugin-name codemap-py) || exit $?; export PLUGIN_ROOT
export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}"
_CM_PROJ=$(git rev-parse --show-toplevel 2>/dev/null | xargs basename 2>/dev/null || echo "cm")
# Python-only: zero .py/.pyi → stop before index checks; scan-codebase would only write an empty index. node_modules pruned — ships node-gyp .py
find "$(git rev-parse --show-toplevel 2>/dev/null || echo .)" \( -name .git -o -name .venv -o -name venv -o -name node_modules \) -prune -o \( -name '*.py' -o -name '*.pyi' \) -print -quit | grep -q . || { echo "! UNSUPPORTED — no .py/.pyi files: /codemap-py:rename-refs is Python-only. Stop: do not rename by hand."; exit 1; }
python "$PLUGIN_ROOT/bin/resolve_index_env.py" \
--output-prefix "codemap-${_CM_PROJ}" 2>/dev/null # timeout: 5000
IFS= read -r PROJ < "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-resolve-proj-${CSID}" 2>/dev/null || PROJ=""
[ -n "$PROJ" ] || { printf "! resolve_index_env.py failed — check Python availability and CLAUDE_PLUGIN_ROOT\n"; exit 1; }
printf 'false\n' > "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-rename-PROCEED_STALE-${CSID}"
printf '' > "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-edited-files-${CSID}"
printf '' > "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-processed-import-files-${CSID}"
# rollback journal: set aside an abandoned run's journal, open this run's — its preflight point for late files
python "$PLUGIN_ROOT/bin/rename_snapshot.py" discard --retain || exit 1
python "$PLUGIN_ROOT/bin/rename_snapshot.py" before || exit 1
! rename journal leftover(withRETAINED_JOURNAL <path>): a previous rename left unfinished edits; the block stopped before opening this run's journal. Stop before any index query or edit. Print the retained path and tell the user a previous rename left unfinished edits.AskUserQuestion: (a) Keep as is — leave the files and the retained journal; (b) Restore — undo that rename all-or-nothing. Restore → run the block below withRETAINEDsubstituted, then print itsRESTORED/REMOVED/MOVED_BACKlines, or on exit 4 itsNOTHING_RESTOREDandCONFLICTlines (journal kept at that path). Either answer ends the run; the user re-invokes the rename.
# timeout: 120000
PLUGIN_ROOT=$(python "${CLAUDE_PLUGIN_ROOT}/bin/resolve_plugin_root.py" --plugin-name codemap-py) || exit $?; export PLUGIN_ROOT
RETAINED="<RETAINED_JOURNAL path printed by Step 1>"
case "$RETAINED" in *'<'*'>'*|"") echo "! BLOCKED — substitute RETAINED with the printed RETAINED_JOURNAL path"; exit 1 ;; esac
python "$PLUGIN_ROOT/bin/rename_snapshot.py" restore --journal "$RETAINED"
- Block exits 1 (
! UNSUPPORTED,! resolve_index_env.py failed,! rename journal refused) → stop the skill. Every!line routes by edit state, never by step number; in no case finish the rename by hand (grep,Edit,git mv), in any language: - Before the first source edit (
Editorgit mv) — Steps 0–3, incl. Step 3's--remove-if-no-callersstops, Step 4 before its first write, Step 5's pre-git mvchecks — every!stop ends the run. Never read a stop as a gate to answer. ! BLOCKED — substitute …(Step 1, Step 2, Step 4c) is a placeholder self-check, not a stop: substitute the named value and re-run that block.- After the first source edit, a
!line never ends the run before the summary, and every post-edit failure restores. Any Step 4 or Step 5!after a file was written (e.g.! BLOCKED — sym-path sentinel missing,! gen_deprecation_wrapper failed), any journal block exiting non-zero (! rename journal refused) and any Step 6!(e.g.! BLOCKED — OLD_NAME sentinel missing) → stop further rename edits, skip the remaining Step 4–6 work, then go to Step 7: its journal restore block runs automatically, without a question, and the summary prints⚠ Rename incomplete, naming the restored files and every conflict. The re-scan! codemap-py index --incremental failedwarning does not skip other Step 6 checks; verification remains unavailable until a successful rescan, so Step 7 must report⚠ Rename incompletewith that warning and restore.
Let Step 2's first query create a missing index or refresh a stale one within the engine's heal cap. Inspect each answer's index.query_complete and index.stale before using it, including each batch item's ok and result.index. A failed query/preparation stops before edits. Only if a query still reports stale: true after bounded preparation (or preparation is explicitly disabled) ask: (a) Proceed anyway (callers may be incomplete); (b) Abort (explicitly refresh the index, then re-invoke). Abort stops. Missing/unknown freshness or query_complete: false → report the metadata and exact coverage gap in the blast report and Step 7; never treat it as complete or allow hard-delete. The caller preflight must still resolve every known site.
After the stale-index answer, record the user's decision with this block. Substitute true only for an explicit Proceed anyway answer; otherwise use false. Repeat when a later query first triggers that question; never infer consent from elapsed time or incomplete coverage.
# timeout: 3000
export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}"
_CM_PROJ=$(git rev-parse --show-toplevel 2>/dev/null | xargs basename 2>/dev/null || echo "cm")
PROCEED_STALE="<true for Proceed anyway; false otherwise>"
case "$PROCEED_STALE" in true|false) ;; *) echo "! BLOCKED — substitute PROCEED_STALE with the user's decision"; exit 1 ;; esac
printf '%s\n' "$PROCEED_STALE" > "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-rename-PROCEED_STALE-${CSID}"
Step 2: Resolve targets¶
All structural queries/rebuilds use codemap-py dispatcher, never scan-query/scan-index. Both routes use same in-engine shared query/exclusive scan leases, preventing mid-rename scan races. Dispatcher also owns interpreter probe; aliases skip it (127 when no eligible CPython, including invalid CODEMAP_PYTHON), deprecated shims removed no earlier than 1.0.0. Every block uses PATH-literal codemap-py query … / codemap-py index …; expansion-bearing path misses bare-name allow prefix, prompts each call. If bare command unavailable, use installed plugin's absolute bin/codemap-py launcher interactively.
Symbol subcommand:
# timeout: 25000
PLUGIN_ROOT=$(python "${CLAUDE_PLUGIN_ROOT}/bin/resolve_plugin_root.py" --plugin-name codemap-py) || exit $?; export PLUGIN_ROOT
export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}"
_CM_PROJ=$(git rev-parse --show-toplevel 2>/dev/null | xargs basename 2>/dev/null || echo "cm")
IFS= read -r OLD_REF < "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-rename-OLD_REF-${CSID}" 2>/dev/null || OLD_REF=""
_FS_SENTINEL="${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-find-symbol-json-${CSID}"
codemap-py query --timeout 20 find-symbol "$OLD_REF" --limit 0 > "$_FS_SENTINEL" || { printf '! find-symbol failed (exit %d) — stop before any source edit\n' "$?"; exit 1; }
python - "$_FS_SENTINEL" <<'PY'
import json
import sys
from pathlib import Path
try:
coverage = json.loads(Path(sys.argv[1]).read_text(encoding="utf-8")).get("index", {})
print("STALE=" + json.dumps(coverage.get("stale", "unknown")))
print("QUERY_COMPLETE=" + json.dumps(coverage.get("query_complete", "unknown")))
except (OSError, ValueError, AttributeError) as exc:
sys.exit(f"! query coverage unavailable: {exc}")
PY
[ "$?" -eq 0 ] || exit 1
# find-symbol is an unanchored pattern — keep exact qualified_name matches when any exist, so a unique exact target never asks
python "$PLUGIN_ROOT/bin/cm_json.py" narrow --input "$_FS_SENTINEL" --qualified-name "$OLD_REF"
find-symbol returns matches, each {name, qualified_name, type, module, path, start_line, end_line, source} (same as query-code symbol). Edits use path, start_line, end_line; exact filter uses qualified_name (applied above when any match is exact); Step 4e reads .matches[0].type.
- 0 matches → print
! Symbol '$OLD_REF' not found. Verify with: codemap-py query find-symbol <pattern>; stop. - Multiple → Write the full candidate list (name/type/module/path) with each match's 0-based index to
.temp/rename-refs-candidates-<session-id>.mdfirst; print it once as the last reply text beforeAskUserQuestion. The question text carries a plain candidate-count summary and→ <candidate list path>; options identify the candidate with a one-line description, no previews. At most 4 options: more than 4 candidates → offer the first 3 plusAbort — keep code unchanged. In the question text, say the Other free-text answer accepts any 0-based index from the saved candidate list. Accept any in-range index, including candidates 4 and later; an abort answer stops without edits. BindCHOSEN_IDXto that exact saved list, never to a new query or a module-qualified re-invocation. After choice, narrow sentinel so every downstream.matches[0]is correct:# timeout: 5000 PLUGIN_ROOT=$(python "${CLAUDE_PLUGIN_ROOT}/bin/resolve_plugin_root.py" --plugin-name codemap-py) || exit $?; export PLUGIN_ROOT export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}" _CM_PROJ=$(git rev-parse --show-toplevel 2>/dev/null | xargs basename 2>/dev/null || echo "cm") CHOSEN_IDX="<0-based index of the match selected via AskUserQuestion>" case "$CHOSEN_IDX" in *[!0-9]*|"") echo "! BLOCKED — substitute CHOSEN_IDX with the chosen 0-based index"; exit 1 ;; esac # out of range → exit 4, answer left as is — never fall through to an unnarrowed matches[0] python "$PLUGIN_ROOT/bin/cm_json.py" narrow --input "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-find-symbol-json-${CSID}" --index "$CHOSEN_IDX" || exit 1
Full qname: <module>::<qualified_name> (e.g. mypackage.auth::validate_token).
# timeout: 25000
PLUGIN_ROOT=$(python "${CLAUDE_PLUGIN_ROOT}/bin/resolve_plugin_root.py" --plugin-name codemap-py) || exit $?; export PLUGIN_ROOT
export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}"
_CM_PROJ=$(git rev-parse --show-toplevel 2>/dev/null | xargs basename 2>/dev/null || echo "cm")
# one parse of the narrowed answer; 4b/4e read sym-path/sym-type back
python "$PLUGIN_ROOT/bin/cm_json.py" first-match --input "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-find-symbol-json-${CSID}" --output-prefix "codemap-${_CM_PROJ}" --field module=sym-module --field qualified_name=sym-qname --field path=sym-path --field type=sym-type
IFS= read -r SYM_MODULE < "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-sym-module-${CSID}" 2>/dev/null || SYM_MODULE=""
IFS= read -r SYM_QNAME < "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-sym-qname-${CSID}" 2>/dev/null || SYM_QNAME=""
[ -n "$SYM_MODULE" ] && [ -n "$SYM_QNAME" ] || { printf "! could not extract module/qualified_name from find-symbol result\n" >&2; exit 1; }
# wire OLD_MODULE_PATH here for 4c — OLD_REF can't carry module for symbol rename, don't re-derive
printf '%s\n' "$SYM_MODULE" > "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-rename-OLD_MODULE_PATH-${CSID}"
_RDEPS_FILE="${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-rdeps-json-${CSID}"
# error → rc≠0 or {"error":…}; summarised as 0 callers it would let the rename skip every caller
codemap-py query --timeout 20 fn-rdeps "${SYM_MODULE}::${SYM_QNAME}" > "$_RDEPS_FILE" || { printf '! fn-rdeps failed (exit %d) — callers unknown; stop before any source edit\n' "$?"; exit 1; }
grep -q '^{"error"' "$_RDEPS_FILE" && { printf '! fn-rdeps returned an error — callers unknown; stop before any source edit\n'; head -c 400 "$_RDEPS_FILE"; exit 1; }
# forward-first, fail-closed — query_complete wins even when false (a `//` fallback let legacy true arm the delete gate on an incomplete graph); zero-caller method → `hint` blocks delete (obj.method()/dynamic dispatch unresolved statically). Rules: cm_json.py rdeps_values
python "$PLUGIN_ROOT/bin/cm_json.py" rdeps-summary --kind fn --input "$_RDEPS_FILE" --output-prefix "codemap-${_CM_PROJ}"
IFS= read -r RDEP_COUNT < "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-rdep-count-${CSID}" 2>/dev/null || RDEP_COUNT="0"
IFS= read -r EXHAUSTIVE < "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-rename-EXHAUSTIVE-${CSID}" 2>/dev/null || EXHAUSTIVE="false"
IFS= read -r RDEP_HINT < "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-rename-RDEP_HINT-${CSID}" 2>/dev/null || RDEP_HINT=""
IFS= read -r INDEX_HINT < "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-rename-INDEX_HINT-${CSID}" 2>/dev/null || INDEX_HINT=""
IFS= read -r REMOVE_IF_ZERO < "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-rename-REMOVE_IF_ZERO-${CSID}" 2>/dev/null || REMOVE_IF_ZERO="false"
IFS= read -r DRY_RUN < "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-rename-DRY_RUN-${CSID}" 2>/dev/null || DRY_RUN="false"
# emit — Step 3 guards decide on a printed value, not an invisible sentinel read
printf 'REMOVE_IF_ZERO=%s DRY_RUN=%s RDEP_COUNT=%s EXHAUSTIVE=%s\nRDEP_HINT=%s\nINDEX_HINT=%s\n' "$REMOVE_IF_ZERO" "$DRY_RUN" "$RDEP_COUNT" "$EXHAUSTIVE" "$RDEP_HINT" "$INDEX_HINT"
fn-rdeps returns {qname, called_by:[{caller, module, path, inferred?}], count, index:{query_complete, hint, ...}}, plus a top-level hint for a method or constructor with zero static callers. inferred: true marks a caller reached only through an inferred receiver type: confirm it in source before rewriting it, and report any unconfirmed one as skipped.
called_byhas no line numbers;callerismodule::qualified_name, whichsymbolnever matches. Step 3 splits it at::and saves ranges fromcodemap-py query symbols <module>filtered by exactqualified_name; Step 4c reads that preflight sentinel.EXHAUSTIVEfirst readsresult["index"]["query_complete"]; only if absent, use legacyresult["index"]["exhaustive"]; neither →false. Incomplete → note in blast report.RDEP_HINTnon-empty → zero static callers is not zero callers: instance calls on a receiver whose class the source does not state (obj.method()), overrides, subclass constructors, and implicit protocol (dunder) calls such aslen(x)orx == yare never resolved statically. Print it in the blast report.INDEX_HINT(index.hint) names the search for references the static AST never sees, such as hook-registered callers. Print it in the blast report; it never blocks the rename on its own.
called_by holds call edges only: a subclass base, isinstance target, callback value, plain import or ancestor __init__.py re-export never appears in it. List every whole-word OLD_NAME in the files that call the symbol, define it, import its module, or import an ancestor package that may re-export it (one batch of rdeps, module names mapped to paths by its list item); Step 4c edits from this list:
# timeout: 25000
PLUGIN_ROOT=$(python "${CLAUDE_PLUGIN_ROOT}/bin/resolve_plugin_root.py" --plugin-name codemap-py) || exit $?; export PLUGIN_ROOT
export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}"
_CM_PROJ=$(git rev-parse --show-toplevel 2>/dev/null | xargs basename 2>/dev/null || echo "cm")
IFS= read -r SYM_MODULE < "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-sym-module-${CSID}" 2>/dev/null || SYM_MODULE=""
IFS= read -r OLD_NAME < "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-rename-OLD_NAME-${CSID}" 2>/dev/null || OLD_NAME=""
[ -n "$SYM_MODULE" ] && [ -n "$OLD_NAME" ] || { echo "! BLOCKED — sym-module or OLD_NAME sentinel missing; stop, then re-invoke rename-refs"; exit 1; }
_T="${TMPDIR:-/tmp}/codemap-${_CM_PROJ}"
_CM_ROOT=$(git rev-parse --show-toplevel 2>/dev/null || pwd)
# rdeps of the module + every ancestor package: a re-export lives in an ancestor __init__.py, its users import the package
_ITEMS='{"cmd":"list","args":["--limit","0"]}'; _M="$SYM_MODULE"
while :; do _ITEMS="$_ITEMS,{\"cmd\":\"rdeps\",\"args\":[\"$_M\"]}"; case "$_M" in *.*) _M="${_M%.*}" ;; *) break ;; esac; done
printf '[%s]\n' "$_ITEMS" > "${_T}-ref-batch-in-${CSID}"
codemap-py query --timeout 20 batch "${_T}-ref-batch-in-${CSID}" > "${_T}-ref-batch-json-${CSID}" || { printf '! importer batch failed (exit %d) — reference sites unknown; stop before any source edit\n' "$?"; exit 1; }
# exit 1 = unreadable answer or unmappable batch; a partial site list would silently skip importers
(cd "$_CM_ROOT" && python "$PLUGIN_ROOT/bin/find_module_refs.py" --kind name --name "$OLD_NAME" --files-from-json "${_T}-rdeps-json-${CSID}" --files-from-json "${_T}-find-symbol-json-${CSID}" --files-from-json "${_T}-ref-batch-json-${CSID}") > "${_T}-ref-sites-${CSID}" || exit 1
printf 'REF_SITES='; wc -l < "${_T}-ref-sites-${CSID}" | tr -d ' '
cat "${_T}-ref-sites-${CSID}"
Each printed path:line:text is a candidate reference site. Hits in comments, docstrings and strings are included; hits naming a same-name symbol imported from another module are not this symbol and stay unedited. For a method, OLD_NAME is the bare method name: only an attribute access on an instance of the renamed method's class is a reference.
Module subcommand:
# timeout: 25000
PLUGIN_ROOT=$(python "${CLAUDE_PLUGIN_ROOT}/bin/resolve_plugin_root.py" --plugin-name codemap-py) || exit $?; export PLUGIN_ROOT
export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}"
_CM_PROJ=$(git rev-parse --show-toplevel 2>/dev/null | xargs basename 2>/dev/null || echo "cm")
IFS= read -r OLD_REF < "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-rename-OLD_REF-${CSID}" 2>/dev/null || OLD_REF=""
OLD_MODULE_PATH="$OLD_REF"
printf '%s\n' "$OLD_MODULE_PATH" > "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-rename-OLD_MODULE_PATH-${CSID}"
_RDEPS_FILE="${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-rdeps-json-${CSID}"
codemap-py query --timeout 20 rdeps "$OLD_REF" > "$_RDEPS_FILE" || { printf '! rdeps failed (exit %d) — importers unknown; stop before any source edit\n' "$?"; exit 1; }
grep -q '^{"error"' "$_RDEPS_FILE" && { printf '! rdeps returned an error — importers unknown; stop before any source edit\n'; head -c 400 "$_RDEPS_FILE"; exit 1; }
python - "$_RDEPS_FILE" <<'PY'
import json
import sys
from pathlib import Path
try:
coverage = json.loads(Path(sys.argv[1]).read_text(encoding="utf-8")).get("index", {})
print("STALE=" + json.dumps(coverage.get("stale", "unknown")))
print("QUERY_COMPLETE=" + json.dumps(coverage.get("query_complete", "unknown")))
except (OSError, ValueError, AttributeError) as exc:
sys.exit(f"! query coverage unavailable: {exc}")
PY
[ "$?" -eq 0 ] || exit 1
# same forward-first, fail-closed read as the symbol branch; rdeps carries no method hint, so --kind module overwrites it empty (an earlier symbol run's hint never reaches this gate)
python "$PLUGIN_ROOT/bin/cm_json.py" rdeps-summary --kind module --input "$_RDEPS_FILE" --output-prefix "codemap-${_CM_PROJ}"
IFS= read -r RDEP_COUNT < "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-rdep-count-${CSID}" 2>/dev/null || RDEP_COUNT="0"
IFS= read -r EXHAUSTIVE < "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-rename-EXHAUSTIVE-${CSID}" 2>/dev/null || EXHAUSTIVE="false"
# module never deletes — parse_rename_args.py rejects --remove-if-no-callers for module
REMOVE_IF_ZERO="false"
IFS= read -r DRY_RUN < "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-rename-DRY_RUN-${CSID}" 2>/dev/null || DRY_RUN="false"
IFS= read -r INDEX_HINT < "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-rename-INDEX_HINT-${CSID}" 2>/dev/null || INDEX_HINT=""
# emit — Step 3 guards decide on a printed value, not an invisible sentinel read
printf 'REMOVE_IF_ZERO=%s DRY_RUN=%s RDEP_COUNT=%s EXHAUSTIVE=%s\nRDEP_HINT=%s\nINDEX_HINT=%s\n' "$REMOVE_IF_ZERO" "$DRY_RUN" "$RDEP_COUNT" "$EXHAUSTIVE" "" "$INDEX_HINT"
Returns {imported_by:[...], dynamic_imported_by:[...], config_refs:[...], index:{query_complete,...}}; extract RDEP_COUNT, EXHAUSTIVE. Count and list dynamic_imported_by and config_refs separately in the blast report; include every listed location in preflight and Step 6 verification. String imports and mock/config targets need explicit source review; an unresolved old target leaves the migration incomplete.
Apply Step 1's stale decision to the printed STALE= and QUERY_COMPLETE= values immediately after the first answer, before candidate selection or further queries. Inspect later query coverage as Step 1 requires.
Step 3: Blast-radius report + confirmation gate¶
Write the full report below to a free .temp/rename-refs-blast-<session-id>.md path first. Before each confirmation call below, print the relevant saved report once as the last reply text before the question; question text still carries the rename/deletion headline, caller/site counts and → <report path>. Do not put the report or caller list in option previews.
Build this report (print it at the confirmation gate, after guards and output-path selection):
Rename: <OLD_REF> → <NEW_REF>
Type: symbol | module
[symbol] Definition: <path>:<start_line>-<end_line>
Static callers: N (across N files)
- src/foo/bar.py (caller: module::fn)
- src/foo/baz.py (caller: module::other_fn)
- tests/test_foo.py
[symbol] Reference sites (imports, bases, isinstance, callbacks, re-exports): N lines across N files
- pkg/c.py:4 class Child(OldName):
[module] Dynamic importers: D (list every module/location from dynamic_imported_by)
[module] Config references: C (list every location from config_refs)
Docstring refs: (will grep :func:/:class:/:meth:/:mod:/:attr: in Step 4d)
[if DEPRECATE]
Deprecation wrapper: <OLD_REF> kept as @deprecated alias → <NEW_REF>
⚠ Not covered (hard limits — grep advisories in Step 7):
- getattr("old_name") dynamic dispatch
- Cross-repo consumers
[if not EXHAUSTIVE]
⚠ Index non-exhaustive — some callers may not appear above
[if PROCEED_STALE=true or caller preflight reports incomplete coverage]
⚠ User chose Proceed anyway — source-verified known callers only; coverage remains stale or incomplete
[if RDEP_HINT]
⚠ <RDEP_HINT>
[if INDEX_HINT]
ℹ Outside the static AST: <INDEX_HINT>
Caller count alone never blocks a complete migration. Enumerate and preflight every known caller; include the complete caller inventory and resolved locations in the same .temp/rename-refs-blast-<session-id>.md report, regardless of its length. Never overwrite an existing report: probe before Writing and append -2, -3, … until free. The inventory supports confirmation and complete execution; it never permits a partial migration.
Before apply confirmation, resolve every known caller before any source edit: for symbols, query every caller and verify its unique source range and expected imports/calls; for modules, inspect every dependent file and matching import. For a missing, ambiguous or source-mismatched site, stop before any source edit with source unchanged. With the recorded Proceed anyway decision, stale or incomplete metadata alone does not reject a uniquely resolved, source-verified caller; carry that caveat into the blast report and Step 7. Without that decision, unavailable caller coverage stops as before. Retain these locations for apply; recheck them before mutation. The complete preflight applies to both subcommands regardless of caller count.
For symbols, run this preflight once before the confirmation gate. It queries symbols <module> once per caller module and publishes the caller-ranges sentinel only after every caller resolves. Read its saved source alongside the expected imports/calls before confirmation; Step 4c consumes these ranges without re-querying after edits.
# timeout: 400000
export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}"
_CM_ROOT=$(git rev-parse --show-toplevel 2>/dev/null || pwd)
_CM_PROJ=$(git rev-parse --show-toplevel 2>/dev/null | xargs basename 2>/dev/null || echo "cm")
_T="${TMPDIR:-/tmp}/codemap-${_CM_PROJ}"
IFS= read -r SUBCOMMAND < "${_T}-rename-SUBCOMMAND-${CSID}" || exit 1
[ "$SUBCOMMAND" = "symbol" ] || exit 0
python - "$_T" "$CSID" "$_CM_ROOT" <<'PY'
import ast
import json
import shutil
import subprocess
import sys
import textwrap
from pathlib import Path
prefix, session, root = sys.argv[1:]
output = Path(f"{prefix}-caller-ranges-{session}")
output.unlink(missing_ok=True)
launcher = shutil.which("codemap-py")
if launcher is None:
sys.exit("! caller preflight unavailable: codemap-py launcher not found")
decision = Path(f"{prefix}-rename-PROCEED_STALE-{session}")
def matches_caller(source, whole, qualified, start, end):
"""Verify that the current range still belongs to the indexed caller."""
definitions = ast.parse(textwrap.dedent(source)).body
kinds = (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)
if len(definitions) != 1 or not isinstance(definitions[0], kinds):
return False
if definitions[0].name != qualified.rsplit(".", 1)[-1]:
return False
try:
pending = [(ast.parse(whole), "")]
except SyntaxError:
return True
while pending:
node, prefix = pending.pop()
if isinstance(node, kinds):
prefix = f"{prefix}.{node.name}" if prefix else node.name
if node.lineno == start and node.end_lineno == end:
return prefix == qualified
pending.extend((child, prefix) for child in ast.iter_child_nodes(node))
return False
def has_expected_call(source, whole, old_ref):
"""Check the saved range for the old call, including imported aliases and constructors."""
names = {old_ref.rsplit(".", 1)[-1]}
if old_ref.endswith(".__init__"):
names.add(old_ref.split(".")[-2])
try:
imports = ast.walk(ast.parse(whole))
except SyntaxError:
imports = ()
aliases = {alias.asname for node in imports if isinstance(node, ast.ImportFrom)
for alias in node.names if alias.name in names and alias.asname}
names |= aliases
return any(isinstance(node, ast.Call) and
((isinstance(node.func, ast.Name) and node.func.id in names) or
(isinstance(node.func, ast.Attribute) and node.func.attr in names))
for node in ast.walk(ast.parse(textwrap.dedent(source))))
modules = {}
resolved = []
identity = "saved inventory"
try:
proceed = decision.is_file() and decision.read_text().strip() == "true"
callers = json.loads(Path(f"{prefix}-rdeps-json-{session}").read_text(encoding="utf-8"))["called_by"]
for caller in callers:
identity = caller["caller"]
module, qualified = identity.split("::", 1)
if module not in modules:
answer = subprocess.run(
[launcher, "query", "--timeout", "20", "symbols", module],
cwd=root, capture_output=True, text=True, timeout=25, check=True,
)
modules[module] = json.loads(answer.stdout)
data = modules[module]
if "error" in data:
raise ValueError(data["error"])
coverage = data.get("index", {})
incomplete = coverage.get("query_complete") is not True or coverage.get("stale") is not False
if incomplete and not proceed:
sys.exit(f"! caller preflight unavailable: {identity} — {coverage}")
matches = [item for item in data["symbols"] if item["qualified_name"] == qualified]
if len(matches) != 1 or data["path"] != caller["path"]:
sys.exit(f"! caller preflight missing/ambiguous: {identity}")
item = matches[0]
start, end = item["start_line"], item["end_line"]
whole = (Path(root) / data["path"]).read_text(encoding="utf-8")
lines = whole.splitlines()
if not 1 <= start <= end <= len(lines):
sys.exit(f"! caller preflight invalid range: {identity}")
source = "\n".join(lines[start - 1:end])
if incomplete:
old_ref = Path(f"{prefix}-rename-OLD_REF-{session}").read_text().strip()
if not matches_caller(source, whole, qualified, start, end) or not has_expected_call(source, whole, old_ref):
sys.exit(f"! caller preflight source mismatch: {identity}")
print(f"⚠ caller preflight used Proceed anyway: {identity} — {coverage}")
resolved.append({**caller, "start_line": start, "end_line": end, "source": source})
output.write_text(json.dumps(resolved) + "\n", encoding="utf-8")
except (OSError, subprocess.SubprocessError, ValueError, KeyError, TypeError, AttributeError, SyntaxError) as exc:
diagnostic = getattr(exc, "stderr", None) or str(exc)
if isinstance(diagnostic, bytes):
diagnostic = diagnostic.decode("utf-8", errors="replace")
sys.exit(f"! caller preflight unavailable: {identity} — {diagnostic[:400]}")
print(f"PREFLIGHT_CALLERS={len(resolved)}\n{output}")
PY
--remove-if-no-callers guards — evaluated before any rename edits, on the REMOVE_IF_ZERO=… DRY_RUN=… RDEP_COUNT=… EXHAUSTIVE=… and RDEP_HINT=… lines printed by Step 2's last block (symbol: fn-rdeps block; module: rdeps block):
REMOVE_IF_ZERO=trueANDRDEP_COUNT > 0→ print! --remove-if-no-callers: N callers found. Remove all callers first or omit flag.; stop entire rename operation.REMOVE_IF_ZERO=trueANDEXHAUSTIVE=false→ print! --remove-if-no-callers requires exhaustive=true. Run /codemap-py:scan-codebase to ensure full coverage.; stop entire rename operation.REMOVE_IF_ZERO=trueANDRDEP_HINTnon-empty → print! --remove-if-no-callers: 0 static callers is not proof for a method or constructor — <RDEP_HINT>; stop entire rename operation. Hint names a search → appendRun that search; delete manually only if it finds no reference.Protocol-method hint (a dunder such as__len__/__eq__, no search) → appendPython calls it implicitly; never delete it on zero callers.— an empty search is expected there, never a licence to delete. Zero static callers of a method is the hint's case, not evidence of none; never delete on it.REMOVE_IF_ZERO=trueANDRDEP_COUNT == 0ANDEXHAUSTIVE=trueANDRDEP_HINTempty → first run the non-call reference check below (bothDRY_RUNvalues).fn-rdepscounts call edges only; imports, base classes,isinstance, callback values and re-exports never appear in it. Any printed hit other than the definition's owndef/classline → print! --remove-if-no-callers: <N> non-call reference(s) to <OLD_NAME> — delete would leave ImportError/NameError; stop entire rename operation.# timeout: 15000 export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}" _CM_PROJ=$(git rev-parse --show-toplevel 2>/dev/null | xargs basename 2>/dev/null || echo "cm") IFS= read -r OLD_NAME < "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-rename-OLD_NAME-${CSID}" 2>/dev/null || OLD_NAME="" [ -n "$OLD_NAME" ] || { echo "! BLOCKED — OLD_NAME sentinel missing; stop, then re-invoke rename-refs"; exit 1; } _CM_ROOT=$(git rev-parse --show-toplevel 2>/dev/null || pwd) grep -rnw --include='*.py' --include='*.pyi' --exclude-dir=.git --exclude-dir=.venv --exclude-dir=node_modules -- "$OLD_NAME" "$_CM_ROOT" | head -50REMOVE_IF_ZERO=trueANDRDEP_COUNT == 0ANDEXHAUSTIVE=trueANDRDEP_HINTempty AND no non-call hit ANDDRY_RUN=false→AskUserQuestion: (a) Delete$OLD_REF— confirmed no callers; (b) Abort — keep file. Abort: stop. Delete: find-symbol line range, thenReadblock bounds. Verifystart_linecontains expected bareOLD_NAMEor qualifiedOLD_REF; mismatch → print! Symbol name mismatch at line <start_line>: expected <OLD_NAME>, index may be stale — run /codemap-py:scan-codebase first; abort without delete. Only then run the Step 4 journalbeforeblock for the definition path,Edit: remove definition fromdef/classthrough final body, including immediately preceding@decoratorlines, then run the Step 4 record and journalafterblocks for that path. Skip Steps 4a–4d. Printℹ Symbol had no callers — removed $OLD_REF without rename; go Step 6.REMOVE_IF_ZERO=trueANDRDEP_COUNT == 0ANDEXHAUSTIVE=trueANDRDEP_HINTempty AND no non-call hit ANDDRY_RUN=true→ record "would delete$OLD_REF— no callers, exhaustive" as a line in the dry-run report below; fall through to the--dry-runblock. NoAskUserQuestion, noEdit— dry-run means no edits, including the delete.- Otherwise (
REMOVE_IF_ZERO=false): proceed with normal rename flow.
--dry-run: print a free branch-scoped output path first ($DRY_OUT below), then write the report:
# timeout: 3000
PLUGIN_ROOT=$(python "${CLAUDE_PLUGIN_ROOT}/bin/resolve_plugin_root.py" --plugin-name codemap-py) || exit $?; export PLUGIN_ROOT
python "$PLUGIN_ROOT/bin/anti_overwrite_path.py" .temp output-rename-refs-dry --branch-date
Write report to the printed $DRY_OUT path via Write, beginning with YAML header:
---
Title: rename-refs dry-run — <OLD_REF> → <NEW_REF>
Date: <YYYY-MM-DD>
Scope: <project name>
Focus: dry-run: sites that would be changed
Agents: codemap-py:rename-refs
Outcome: DRY_RUN — no edits applied; <N callers, M import sites, K docstring refs>
Confidence: <exhaustive|partial>
Next steps: re-invoke without --dry-run to apply; or abort
Path: → <the resolved $DRY_OUT path>
---
[if reached via the zero-callers delete path above] Title → rename-refs dry-run — would delete <OLD_REF>; Outcome → DRY_RUN — no edits applied; would delete <OLD_REF> — zero callers, exhaustive; body records only that one line, no caller/import/docstring counts.
AskUserQuestion after printing the saved $DRY_OUT report once as the last reply text before the call; question text carries the dry-run headline, caller/import/docstring counts (or only the would-delete line for the delete path), and → <resolved $DRY_OUT>, no previews: (a) Apply for real — re-invoke without --dry-run; (b) Done — keep the report without edits. Stop.
Otherwise AskUserQuestion: print the saved blast-radius report once as the last reply text before the call; question text carries the rename headline, caller count, any cap/incomplete-index warning, and → <resolved blast report path>, no previews. (a) Apply edits — rename the listed sites; (b) Abort — keep the current code. Abort → stop.
Step 4: Apply edits — symbol rename¶
For either subcommand, run this journal before block immediately before each source write: every Edit (definition, re-export, non-call, documentation and config edits, the Step 3 hard-delete) and Step 5a's git mv, for its source path. A refused before (! rename journal refused) writes nothing; before the run's first source write it ends the run like any pre-edit stop, after one it is a post-edit failure (Step 1 routing).
# timeout: 5000
PLUGIN_ROOT=$(python "${CLAUDE_PLUGIN_ROOT}/bin/resolve_plugin_root.py" --plugin-name codemap-py) || exit $?; export PLUGIN_ROOT
export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}"
JOURNAL_PATH="<exact path about to be edited, created or moved>"
case "$JOURNAL_PATH" in *'<'*'>'*|"") echo "! BLOCKED — substitute JOURNAL_PATH with the path about to be written"; exit 1 ;; esac
python "$PLUGIN_ROOT/bin/rename_snapshot.py" before --file "$JOURNAL_PATH"
Immediately after each successful source Edit, record its exact path with this block, including definition, re-export, non-call, documentation and config edits. After a successful Step 5 git mv, run it for both the source and destination paths from that operation before any further edit. A failed write records no new path; retain all earlier entries for the partial-state summary.
# timeout: 3000
export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}"
_CM_PROJ=$(git rev-parse --show-toplevel 2>/dev/null | xargs basename 2>/dev/null || echo "cm")
EDITED_PATH="<exact path just edited or moved>"
case "$EDITED_PATH" in *'<'*'>'*|"") echo "! BLOCKED — substitute EDITED_PATH with the actual edited path"; exit 1 ;; esac
printf '%s\n' "$EDITED_PATH" >> "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-edited-files-${CSID}"
Then record the write in the journal before any further edit: after an Edit, run this journal after block for the same path; after a git mv, run the journal move block below with both paths instead. A non-zero exit is a post-edit failure.
# timeout: 5000
PLUGIN_ROOT=$(python "${CLAUDE_PLUGIN_ROOT}/bin/resolve_plugin_root.py" --plugin-name codemap-py) || exit $?; export PLUGIN_ROOT
export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}"
JOURNAL_PATH="<exact path just edited>"
case "$JOURNAL_PATH" in *'<'*'>'*|"") echo "! BLOCKED — substitute JOURNAL_PATH with the path just edited"; exit 1 ;; esac
python "$PLUGIN_ROOT/bin/rename_snapshot.py" after --file "$JOURNAL_PATH"
# timeout: 5000
PLUGIN_ROOT=$(python "${CLAUDE_PLUGIN_ROOT}/bin/resolve_plugin_root.py" --plugin-name codemap-py) || exit $?; export PLUGIN_ROOT
export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}"
MOVED_FROM="<source path of the git mv>"
MOVED_TO="<destination path of the git mv>"
case "$MOVED_FROM" in *'<'*'>'*|"") echo "! BLOCKED — substitute MOVED_FROM with the git mv source path"; exit 1 ;; esac
case "$MOVED_TO" in *'<'*'>'*|"") echo "! BLOCKED — substitute MOVED_TO with the git mv destination path"; exit 1 ;; esac
python "$PLUGIN_ROOT/bin/rename_snapshot.py" after --move "$MOVED_FROM" "$MOVED_TO"
Skip to Step 5 if SUBCOMMAND=module.
loads: symbol.md
Steps 4a–4e (definition site, __all__ re-exports, import call sites, Sphinx cross-refs, deprecation wrapper) live in modes/symbol.md; load it and follow it in order:
# timeout: 5000
PLUGIN_ROOT=$(python "${CLAUDE_PLUGIN_ROOT}/bin/resolve_plugin_root.py" --plugin-name codemap-py) || exit $?; export PLUGIN_ROOT
export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}"
_CM_PROJ=$(git rev-parse --show-toplevel 2>/dev/null | xargs basename 2>/dev/null || echo "cm")
IFS= read -r SUBCOMMAND < "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-rename-SUBCOMMAND-${CSID}" 2>/dev/null || SUBCOMMAND=""
[ "$SUBCOMMAND" = "symbol" ] || { printf 'ℹ SUBCOMMAND=%s — skip Step 4\n' "$SUBCOMMAND"; exit 0; }
python "$PLUGIN_ROOT/bin/render_plugin_root.py" "$PLUGIN_ROOT/claude-skills/rename-refs/modes/symbol.md"
Step 5: Apply edits — module rename¶
Skip to Step 6 if SUBCOMMAND=symbol.
loads: module.md
Stop conditions carried by 5a: non-git source, untracked source, or unmerged/conflicted path → stop before edits; preserve uncommitted user edits on a tracked source — dirty status alone is never a stop.
Journal the move: after 5a's first block (it saves old-file-path) and before its second block (the one running git mv), run this block; after a successful git mv, run the Step 4 record block for both paths and the journal move block before any further edit.
# timeout: 5000
PLUGIN_ROOT=$(python "${CLAUDE_PLUGIN_ROOT}/bin/resolve_plugin_root.py" --plugin-name codemap-py) || exit $?; export PLUGIN_ROOT
export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}"
_CM_PROJ=$(git rev-parse --show-toplevel 2>/dev/null | xargs basename 2>/dev/null || echo "cm")
IFS= read -r old_file_path < "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-old-file-path-${CSID}" 2>/dev/null || old_file_path=""
[ -n "$old_file_path" ] || { echo "! BLOCKED — old-file-path sentinel missing; stop before git mv"; exit 1; }
python "$PLUGIN_ROOT/bin/rename_snapshot.py" before --file "$old_file_path"
Steps 5a–5f (file rename, direct imports, from-imports, __init__.py relative re-exports, pyproject.toml/setup.cfg, Sphinx :mod: refs) live in modes/module.md; load it and follow it in order:
# timeout: 5000
PLUGIN_ROOT=$(python "${CLAUDE_PLUGIN_ROOT}/bin/resolve_plugin_root.py" --plugin-name codemap-py) || exit $?; export PLUGIN_ROOT
export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}"
_CM_PROJ=$(git rev-parse --show-toplevel 2>/dev/null | xargs basename 2>/dev/null || echo "cm")
IFS= read -r SUBCOMMAND < "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-rename-SUBCOMMAND-${CSID}" 2>/dev/null || SUBCOMMAND=""
[ "$SUBCOMMAND" = "module" ] || { printf 'ℹ SUBCOMMAND=%s — skip Step 5\n' "$SUBCOMMAND"; exit 0; }
python "$PLUGIN_ROOT/bin/render_plugin_root.py" "$PLUGIN_ROOT/claude-skills/rename-refs/modes/module.md"
Step 6: Re-scan + verify¶
# timeout: 400000
export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}"
_CM_PROJ=$(git rev-parse --show-toplevel 2>/dev/null | xargs basename 2>/dev/null || echo "cm")
# dispatcher not scan-index alias (rationale: Step 2). --incremental re-parses changed files only.
codemap-py index --incremental --timeout 360
_scan_rc=$?
if [ "$_scan_rc" -ne 0 ]; then
printf "! codemap-py index --incremental failed (exit %d) — verification may be incomplete; run /codemap-py:scan-codebase for full rebuild\n" "$_scan_rc"
# don't skip — gather the remaining evidence even when the rescan failed
fi
IFS= read -r OLD_REF < "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-rename-OLD_REF-${CSID}" 2>/dev/null || OLD_REF=""
codemap-py query --timeout 20 find-symbol "$OLD_REF" --limit 0 # timeout: 25000
For module:
# timeout: 25000
export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}"
_CM_PROJ=$(git rev-parse --show-toplevel 2>/dev/null | xargs basename 2>/dev/null || echo "cm")
IFS= read -r OLD_REF < "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-rename-OLD_REF-${CSID}" 2>/dev/null || OLD_REF=""
codemap-py query --timeout 20 rdeps "$OLD_REF"
Interpret the module query with its actual exit status: exit 3 plus structured error: "module not indexed" for OLD_REF is clean absence only after a successful rescan. Exit 0 with any imported_by, dynamic_imported_by or config_refs entry is a residual; exit 0 with empty lists still means the old module is indexed, so inspect its source rather than calling it absent. Any other error, malformed result or failed rescan means verification unavailable and Step 7 must report an incomplete rename.
For module, also search literal dotted OLD_REF, including string imports, mock targets and config/documentation references. This scan is required even when rdeps reports clean absence:
# timeout: 30000
export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}"
_CM_ROOT=$(git rev-parse --show-toplevel 2>/dev/null || pwd)
_CM_PROJ=$(git rev-parse --show-toplevel 2>/dev/null | xargs basename 2>/dev/null || echo "cm")
_T="${TMPDIR:-/tmp}/codemap-${_CM_PROJ}"
IFS= read -r SUBCOMMAND < "${_T}-rename-SUBCOMMAND-${CSID}" || exit 1
[ "$SUBCOMMAND" = "module" ] || exit 0
IFS= read -r OLD_REF < "${_T}-rename-OLD_REF-${CSID}" || exit 1
[ -n "$OLD_REF" ] || { echo "! module residual verification unavailable — OLD_REF missing"; exit 1; }
IFS= read -r NEW_REF < "${_T}-rename-NEW_REF-${CSID}" 2>/dev/null || NEW_REF=""
grep -rnE --include='*.py' --include='*.pyi' --include='*.rst' --include='*.md' --include='*.toml' --include='*.cfg' --include='*.ini' --include='*.json' --include='*.yaml' --include='*.yml' --exclude-dir=.git --exclude-dir=.venv --exclude-dir=venv --exclude-dir=node_modules --exclude-dir=__pycache__ --exclude-dir=.temp --exclude-dir=.reports --exclude-dir=.plans --exclude-dir=.cache --exclude-dir=.tox --exclude-dir=build --exclude-dir=dist -- "(^|[^A-Za-z0-9_])${OLD_REF//./\\.}([^A-Za-z0-9_]|$)" "$_CM_ROOT" > "${_T}-module-residual-raw-${CSID}"
_residual_rc=$?
[ "$_residual_rc" -le 1 ] || { echo "! module residual verification unavailable — search failed"; exit 1; }
python - "$_T" "$CSID" "$OLD_REF" "$NEW_REF" <<'PY'
import re
import sys
from pathlib import Path
prefix, session, old, new = sys.argv[1:]
try:
pattern = re.compile(r"(?<![A-Za-z0-9_])" + re.escape(old) + r"(?![A-Za-z0-9_])")
hits, history = [], []
for line in Path(f"{prefix}-module-residual-raw-{session}").read_text(encoding="utf-8").splitlines():
path, text = re.split(r":\d+:", line, maxsplit=1)
if not pattern.search(text.replace(new, "") if new else text):
continue
filename = Path(path)
historical = filename.suffix.lower() in {".md", ".rst"} and filename.stem.lower() in {
"changelog", "history", "changes"
}
(history if historical else hits).append(line)
Path(f"{prefix}-module-residual-{session}").write_text("".join(line + "\n" for line in hits), encoding="utf-8")
print(f"MODULE_RESIDUAL_COUNT={len(hits)}")
print("\n".join(hits))
print(f"MODULE_HISTORY_ADVISORY={len(history)}")
print("\n".join(history))
except (OSError, ValueError) as exc:
sys.exit(f"! module residual verification unavailable — {exc}")
PY
Classify every module residual hit against its origin and the Step 3 inventory. An old module target in a string import, mock.patch, config or current documentation is unresolved and prevents Renamed; report the exact location and required edit. Changelog/history hits are advisory and never unresolved; do not rewrite history. Same-text references to another origin may remain, with their classification recorded.
Expected: old name absent, except deprecated alias with --deprecate. find-symbol is an unanchored pattern, so count only matches whose qualified_name equals OLD_REF; a new name containing the old one (old_fn → old_fn_v2) is not a residual.
For symbol, the index check above never sees a reference — only definitions. Count every whole-word OLD_NAME still in *.py/*.pyi files (a function or class: the whole tree, VCS, virtualenv and cache directories skipped; a method: Step 2's reference-site files, since a bare method name such as run recurs across unrelated classes). With --deprecate, the alias stub's own def/class line is not counted:
# timeout: 30000
PLUGIN_ROOT=$(python "${CLAUDE_PLUGIN_ROOT}/bin/resolve_plugin_root.py" --plugin-name codemap-py) || exit $?; export PLUGIN_ROOT
export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}"
_CM_PROJ=$(git rev-parse --show-toplevel 2>/dev/null | xargs basename 2>/dev/null || echo "cm")
IFS= read -r SUBCOMMAND < "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-rename-SUBCOMMAND-${CSID}" 2>/dev/null || SUBCOMMAND=""
[ "$SUBCOMMAND" = "symbol" ] || { printf 'ℹ SUBCOMMAND=%s — no symbol residual check\n' "$SUBCOMMAND"; exit 0; }
IFS= read -r OLD_NAME < "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-rename-OLD_NAME-${CSID}" 2>/dev/null || OLD_NAME=""
IFS= read -r SYM_PATH < "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-sym-path-${CSID}" 2>/dev/null || SYM_PATH=""
IFS= read -r SYM_TYPE < "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-sym-type-${CSID}" 2>/dev/null || SYM_TYPE=""
IFS= read -r DEPRECATE < "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-rename-DEPRECATE-${CSID}" 2>/dev/null || DEPRECATE="false"
[ -n "$OLD_NAME" ] || { echo "! BLOCKED — OLD_NAME sentinel missing; source already edited — go to Step 7: restore, ⚠ Rename incomplete"; exit 1; }
_T="${TMPDIR:-/tmp}/codemap-${_CM_PROJ}"
_CM_ROOT=$(git rev-parse --show-toplevel 2>/dev/null || pwd)
_ALIAS=""; [ "$DEPRECATE" = "true" ] && _ALIAS="$SYM_PATH"
if [ "$SYM_TYPE" = "method" ]; then
(cd "$_CM_ROOT" && python "$PLUGIN_ROOT/bin/find_module_refs.py" --kind name --name "$OLD_NAME" ${_ALIAS:+--alias-in "$_ALIAS"} --files-from-json "${_T}-rdeps-json-${CSID}" --files-from-json "${_T}-find-symbol-json-${CSID}" --files-from-json "${_T}-ref-batch-json-${CSID}") > "${_T}-residual-${CSID}" || exit 1
else
python "$PLUGIN_ROOT/bin/find_module_refs.py" --kind name --name "$OLD_NAME" ${_ALIAS:+--alias-in "$_CM_ROOT/$_ALIAS"} --scope "$_CM_ROOT" > "${_T}-residual-${CSID}" || exit 1
fi
printf 'RESIDUAL_COUNT='; wc -l < "${_T}-residual-${CSID}" | tr -d ' '
cat "${_T}-residual-${CSID}"
Classify each residual line: an unedited reference to the renamed symbol (an import from its module or a re-exporting package, a base, a call, an isinstance target, a callback value, a comment or docstring naming it) or a same-name symbol of another origin. Any unedited reference makes the migration incomplete (Step 7). Hard-limit cases: dynamic refs, template strings, out-of-scope config strings.
Before claiming completion, record these verification results in the saved blast report:
- Require a successful rescan and the old-definition/importer and residual checks above. Record all residual classifications and exact unresolved locations.
- Run the identity comparison block below. For symbols it saves
fn-rdepsfor the new qualified name and compares every (caller,path) againstcaller-ranges, including equal totalcount; only the renamed provider's own qualified prefix and nested members transform. An equal count alone cannot substitute for the identity comparison. A hard-delete has no new target: require the old definition and all references absent instead. - For modules the same block saves
rdeps <NEW_REF>and compares static importers, dynamic importer locations and config locations with the pre-rename answer. Verify each saved source location names the new module, including relative imports; any absent or still-old consumer fails verification. Review extra identities as newly discovered consumers. Require complete, fresh post-query metadata only when pre-renameEXHAUSTIVE=true; otherwise compare known identities and carry the same non-exhaustive caveat into Step 7 without downgrading a fully matched rename. - Run the bounded import-smoke block below using the project's Python environment. It imports the provider and every inventoried Python consumer module in separate processes, with a 20-second limit per module, and prints the
test-impactpytest command as advisory only. Record each module, command and exit status in the same report. No covering test is advisory and never blocks✓ Renamed; no test suite runs automatically.
# timeout: 400000
export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}"
_CM_ROOT=$(git rev-parse --show-toplevel 2>/dev/null || pwd)
_CM_PROJ=$(git rev-parse --show-toplevel 2>/dev/null | xargs basename 2>/dev/null || echo "cm")
_T="${TMPDIR:-/tmp}/codemap-${_CM_PROJ}"
python - "$_T" "$CSID" "$_CM_ROOT" <<'PY'
import json
import shutil
import subprocess
import sys
from pathlib import Path
prefix, session, root = sys.argv[1:]
def saved(name):
"""Read a required session value."""
return Path(f"{prefix}-{name}-{session}").read_text(encoding="utf-8").strip()
def module_identities(data, old, new):
"""Compare consumer origins and locations without target literals or context text."""
def moved_name(name):
"""Map the moved provider's own module identity."""
return new if name == old else name
def moved_path(path):
"""Preserve the import root while mapping the provider's file path."""
suffix = old.replace(".", "/") + ".py"
return path[:-len(suffix)] + new.replace(".", "/") + ".py" if path.endswith(suffix) else path
identities = {("static", moved_name(name)) for name in data["imported_by"]}
identities.update(("dynamic", moved_name(row["importer"]), moved_path(row["path"]), row["line"])
for row in data["dynamic_imported_by"])
identities.update(("config", row["file"], row["line"]) for row in data["config_refs"])
return identities
output = Path(f"{prefix}-new-rdeps-json-{session}")
output.unlink(missing_ok=True)
try:
mode, old, new = saved("rename-SUBCOMMAND"), saved("rename-OLD_REF"), saved("rename-NEW_REF")
if mode == "symbol" and saved("rename-REMOVE_IF_ZERO") == "true":
print("NO_NEW_TARGET=hard-delete; verify old definition and all references absent")
sys.exit(0)
launcher = shutil.which("codemap-py")
if launcher is None:
raise ValueError("codemap-py launcher not found")
before = json.loads(saved("rdeps-json"))
if mode == "symbol":
module, qualified = saved("sym-module"), saved("sym-qname")
if "." not in new and "." in qualified:
new = qualified.rsplit(".", 1)[0] + "." + new
old_prefix, new_prefix = f"{module}::{qualified}", f"{module}::{new}"
rows = json.loads(saved("caller-ranges"))
expected = {(new_prefix + row["caller"][len(old_prefix):]
if row["caller"] == old_prefix or row["caller"].startswith(old_prefix + ".")
else row["caller"], row["path"]) for row in rows}
command, target = "fn-rdeps", new_prefix
else:
expected = module_identities(before, old, new)
command, target = "rdeps", new
answer = subprocess.run([launcher, "query", "--timeout", "20", command, target],
cwd=root, capture_output=True, text=True, timeout=25, check=True)
after = json.loads(answer.stdout)
if "error" in after:
raise ValueError(after["error"])
output.write_text(json.dumps(after) + "\n", encoding="utf-8")
coverage = after.get("index", {})
if saved("rename-EXHAUSTIVE") == "true":
if coverage.get("query_complete") is not True or coverage.get("stale") is not False:
sys.exit(f"! new-target coverage unavailable: {coverage}")
else:
print(f"⚠ Index non-exhaustive — comparing known consumers only; post-query coverage: {coverage}")
actual = ({(row["caller"], row["path"]) for row in after["called_by"]} if mode == "symbol"
else module_identities(after, new, new))
print("MATCHED_IDENTITIES=" + json.dumps(sorted(expected & actual)))
print("MISSING_IDENTITIES=" + json.dumps(sorted(expected - actual)))
print("EXTRA_IDENTITIES=" + json.dumps(sorted(actual - expected)))
if mode == "symbol" and not (after["count"] == len(after["called_by"]) == len(rows) == before["count"]):
sys.exit("! caller identity count mismatch")
if expected - actual or (mode == "symbol" and actual - expected):
sys.exit("! consumer identity mismatch — go to Step 7: restore, ⚠ Rename incomplete")
except (OSError, subprocess.SubprocessError, ValueError, KeyError, TypeError, AttributeError) as exc:
diagnostic = getattr(exc, "stderr", None) or str(exc)
sys.exit(f"! new-target verification unavailable: {str(diagnostic)[:400]}")
PY
# timeout: 400000
export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}"
_CM_ROOT=$(git rev-parse --show-toplevel 2>/dev/null || pwd)
_CM_PROJ=$(git rev-parse --show-toplevel 2>/dev/null | xargs basename 2>/dev/null || echo "cm")
_T="${TMPDIR:-/tmp}/codemap-${_CM_PROJ}"
python - "$_T" "$CSID" "$_CM_ROOT" <<'PY'
import json
import shutil
import subprocess
import sys
from pathlib import Path
prefix, session, root = sys.argv[1:]
try:
mode = Path(f"{prefix}-rename-SUBCOMMAND-{session}").read_text().strip()
new = Path(f"{prefix}-rename-NEW_REF-{session}").read_text().strip()
before = json.loads(Path(f"{prefix}-rdeps-json-{session}").read_text(encoding="utf-8"))
if mode == "symbol":
provider = Path(f"{prefix}-sym-module-{session}").read_text().strip()
modules = {provider, *(row["module"] for row in before["called_by"])}
batch = json.loads(Path(f"{prefix}-ref-batch-json-{session}").read_text(encoding="utf-8"))
modules.update(name for item in batch["batch"] if item.get("ok") and item["cmd"] == "rdeps"
for name in item["result"]["imported_by"])
qualified = Path(f"{prefix}-sym-qname-{session}").read_text().strip()
if "." not in new and "." in qualified:
new = qualified.rsplit(".", 1)[0] + "." + new
target = f"{provider}::{new}"
else:
modules = {new, *before["imported_by"], *(row["importer"] for row in before["dynamic_imported_by"])}
old = Path(f"{prefix}-rename-OLD_REF-{session}").read_text().strip()
modules = {new if module == old else module for module in modules}
target = new
except (OSError, ValueError, KeyError, TypeError) as exc:
sys.exit(f"! import smoke inventory unavailable: {exc}")
failed = []
for module in sorted(modules):
command = [sys.executable, "-c", "import importlib, sys; importlib.import_module(sys.argv[1])", module]
try:
result = subprocess.run(command, cwd=root, capture_output=True, text=True, timeout=20)
print(f"IMPORT_SMOKE={module} EXIT={result.returncode} COMMAND={json.dumps(command)}")
if result.returncode:
failed.append(module)
print(result.stderr[:400])
except (OSError, subprocess.TimeoutExpired) as exc:
failed.append(module)
print(f"IMPORT_SMOKE={module} unavailable: {exc}")
try:
launcher = shutil.which("codemap-py")
if launcher is None:
raise ValueError("codemap-py launcher not found")
result = subprocess.run([launcher, "query", "--timeout", "20", "test-impact", target],
cwd=root, capture_output=True, text=True, timeout=25, check=True)
impact = json.loads(result.stdout)
print("ADVISORY pytest command: " + (impact.get("pytest_cmd") or "no covering test"))
except (OSError, subprocess.SubprocessError, ValueError, TypeError, AttributeError) as exc:
print(f"ADVISORY test-impact unavailable: {str(exc)[:400]}")
if failed:
sys.exit("! import smoke failed: " + ", ".join(failed))
PY
N is the preflight inventory's distinct caller count (module mode: distinct static/dynamic/config consumers). M counts consumers passing the identity/source comparison and, for Python consumers, their module's import smoke check. Config consumers require their source-location check. An unresolved known caller, failed smoke check or unavailable required verification leaves the migration incomplete; list the exact remaining consumer and next action. A non-exhaustive index or missing covering test remains a caveat, not an additional failure. Import smoke verifies import-time behavior; runtime call behavior remains outside this bounded check.
Step 7: Summary¶
Run this block even after a post-edit failure. It combines the edit journal with processed-import-files and the definition path; the journal includes both paths of each successful git mv. Print its Files edited: list in either summary outcome.
# timeout: 5000
export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}"
_CM_PROJ=$(git rev-parse --show-toplevel 2>/dev/null | xargs basename 2>/dev/null || echo "cm")
_T="${TMPDIR:-/tmp}/codemap-${_CM_PROJ}"
python - "$_T" "$CSID" <<'PY'
import sys
from pathlib import Path
prefix, session = sys.argv[1:]
paths = set()
mode = Path(f"{prefix}-rename-SUBCOMMAND-{session}")
names = ["edited-files", "processed-import-files"]
if mode.is_file() and mode.read_text().strip() == "symbol":
names.append("sym-path")
for name in names:
source = Path(f"{prefix}-{name}-{session}")
if source.is_file():
paths.update(line for line in source.read_text(encoding="utf-8").splitlines() if line)
print("Files edited:")
print("\n".join(" " + path for path in sorted(paths)) or " (none recorded)")
PY
Then settle the journal with exactly one block. ⚠ Rename incomplete (a post-edit failure, an unresolved known consumer, an unedited reference, failed or unavailable verification) → the restore block, which runs automatically, without a question. ✓ Renamed → the discard block. A run that stopped before its first source write runs neither.
# timeout: 120000
PLUGIN_ROOT=$(python "${CLAUDE_PLUGIN_ROOT}/bin/resolve_plugin_root.py" --plugin-name codemap-py) || exit $?; export PLUGIN_ROOT
export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}"
python "$PLUGIN_ROOT/bin/rename_snapshot.py" restore
Restore is all-or-nothing: it checks every journaled write first, then puts back every file still holding the run's last recorded edit, deletes a file the run created only while it holds the created bytes, and undoes a git mv with git mv back. Exit 4 = a conflict: with NOTHING_RESTORED, nothing was restored and every file keeps its current bytes; without it, a file changed during the restore, which stopped after its printed lines. Print each CONFLICT line and the RETAINED_JOURNAL path, where the original bytes stay (restore --journal on that path retries once the conflicts are resolved). Exit 3 = restore unavailable: print its ! line and state that the edits remain in place.
# timeout: 5000
PLUGIN_ROOT=$(python "${CLAUDE_PLUGIN_ROOT}/bin/resolve_plugin_root.py" --plugin-name codemap-py) || exit $?; export PLUGIN_ROOT
export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}"
python "$PLUGIN_ROOT/bin/rename_snapshot.py" discard
Print:
[if any known consumer remains unresolved, any Step 6 residual is an unedited reference, a post-edit `!` stop fired, or verification failed/unavailable]
⚠ Rename incomplete — M of N callers verified; R unedited reference(s) to <OLD_NAME>:
<file>:<line>
<remaining consumers, failed checks and next actions>
Files edited: <the exact list printed above>
Restore: <RESTORED/REMOVED/MOVED_BACK lines, or nothing restored on a conflict>; conflicts: <each CONFLICT line, or none>; journal: <RETAINED_JOURNAL path when conflicts>
[else]
✓ Renamed: <OLD_REF> → <NEW_REF> — residual references: RESIDUAL_COUNT (symbol) / MODULE_RESIDUAL_COUNT (module); each remaining hit classified as another origin, 0 when none
Files changed: N
Files edited: <the exact list printed above>
Call sites updated: M
Docstring refs updated: K
[if DEPRECATE] Deprecation alias added at: <path>:<line>
Advisory — check manually (outside static analysis coverage):
[if PROCEED_STALE=true] User chose Proceed anyway — coverage remains stale or incomplete; only known source-verified consumers checked
[if not EXHAUSTIVE] Index non-exhaustive — unknown consumers remain outside this rename's verification
- <the printed test-impact pytest command, or no covering test; advisory only>
[if MODULE_HISTORY_ADVISORY > 0] Historical module references: <locations; advisory, left unchanged>
- getattr("<old_name>") dynamic dispatch: grep -rn '"<old_name>"' "$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
[if cross-repo public API and DEPRECATE not used]
- External consumers: update CHANGELOG; use --deprecate alias until next major release
[if residual hits from Step 6 re-scan]
- Residual same-name references (another origin — left unedited):
<file>:<line>