A Semantic Refactor Packet Before Agents Rewrite Shared Code
In Short
A semantic refactor packet gives an agent a bounded change and gives reviewers the evidence needed to assess it. Assemble the target symbols, structural matches, rewrite rule, duplicate hotspots, and baseline tests before editing shared code. Then approve a small pilot diff before expanding the rewrite.
This workflow combines navigation and automation without treating either as proof of correctness. The packet should answer three questions: what can change, what behavior must remain, and what evidence would stop the work? The steps below are a suggested operating procedure, rather than a claim that any tool guarantees safe refactoring.
Who this is for
This is for maintainers asking coding agents to rename shared helpers, migrate repeated API calls, or consolidate duplicated code. It is especially useful when several packages depend on the same behavior and a locally correct edit could break an overlooked consumer.
For example, imagine replacing loadConfig(path) with readConfig({ path }) across a repository. Before changing calls, identify the exported definition, import aliases, wrappers, and tests covering missing files. Keep error behavior and return values explicit. A renamed function can still introduce an unintended contract change.
Starter workflow
| Stage | Review artifact | Stop condition |
|---|---|---|
| Symbol map | Definitions and known consumers | Unresolved references |
| Pattern scan | Matches and exclusions | Unexpected syntax shapes |
| Codemod diff | Pilot patch and rule fixtures | Unexplained file changes |
| Duplicate review | Hotspots with owner decisions | Different behavior hidden by similar code |
| Smoke tests | Baseline and candidate results | Regression or missing evidence |
- Freeze scope and baseline. Record the starting commit, allowed directories, excluded generated files, and a reviewer. Run the existing tests before editing and retain their output. Label pre-existing failures so they cannot disappear inside a later “tests failed” summary. Use a separate branch or worktree with a recoverable starting point.
- Map symbols and consumers. Use definitions and references to understand the shared entry point. Serena’s official repository describes symbol-based querying and editing through language-server or JetBrains backends. Check backend support and initialization for your project; record unresolved dynamic calls separately. A reference list is evidence with coverage limits.
- Inventory structural matches. ast-grep matches abstract syntax tree nodes and supports structural rewriting. Start with search only. Save matching file paths, representative calls, aliases, and exclusions. For the configuration example, include a direct call that should change and an unrelated object method with the same name that must remain untouched.
- Prepare and review a pilot. Encode the transformation and its fixtures before expanding it. Codemod’s official project supports multi-step transformation workflows. Pin the transformation revision and review its code and dependencies. Apply it to a suggested pilot of five files, inspect every changed hunk, and run it again to check that it produces no additional edits.
- Inspect duplication deliberately. Duplicate findings can identify neighboring implementations that deserve attention. jscpd’s official repository documents copy-and-paste detection. Compare hotspots against the baseline and ask owners whether similar blocks share a contract. Consolidation belongs in a separate change when it alters ownership, lifecycle, or error handling.
- Verify and hand off. Run focused unit tests, the project’s normal checks, and relevant smoke tests against the candidate. Bats-core provides a framework for testing Bash and UNIX programs. For a CLI wrapper, check exit status, expected output, and a failure path. Attach the commands, environment, results, remaining uncertainties, and rollback commit to the review.
Recommended ASE skills
Use these five skill routes in sequence. Each has a distinct output; adding them all does not replace deciding whether the proposed change is appropriate.
- Serena semantic navigation and refactors: build the symbol and reference map before proposing edits.
- ast-grep structural search and rewrite: inventory syntax patterns and define matching boundaries.
- Reviewable Codemod CLI workflows: package the transformation into a repeatable, inspectable procedure.
- Copy-pasted code hotspot detection: identify related duplication for an explicit reviewer decision.
- Shell and CLI smoke tests: capture observable command behavior before and after the change.
What to watch
Structural similarity does not establish identical meaning. Shadowed names, reflection, plugin registries, generated clients, and serialized function names need separate investigation. Tests can pass while an untested downstream consumer still breaks. Preserve compatibility where consumers cannot migrate together.
Set a suggested initial expansion limit of twenty files per review batch and pause if the changed-file inventory exceeds the approved list. These are starting limits for this workflow, not vendor requirements. Keep formatting churn, dependency updates, and unrelated cleanup out of the migration diff so reviewers can trace each edit to its rule.
FAQ
Does semantic navigation make a rewrite safe?
It improves the context available for a decision. Safety still depends on supported analysis, correct scope, representative tests, and review of the resulting diff. Record blind spots rather than assuming the symbol map is complete.
Should every duplicate be consolidated?
No. Similar code may implement different promises or belong to independently released components. Use the duplicate report to ask better questions; consolidate only when the shared behavior and ownership are agreed.
What should a reviewer receive?
Send the baseline commit, scope, symbol map, match inventory, rule fixtures, pilot diff, duplicate decisions, and test results. The next step is a bounded pilot review, followed by expansion only when the evidence supports it.
