git:20260527.42cd226 to git:20260709.0ae4022

97 added, 110 removed. Audit A to A.

---
name: "rewriting-code-strictly"
description: "Rewrite existing Python, TypeScript/JavaScript, or Go code for strict static typing, boundary validation, and maintainable idioms while preserving behavior. Use when the user asks to harden code, remove unsafe escape hatches, add validation, or align with mypy, Pyright, tsc, go vet, or Staticcheck. Coordinates baseline mapping, strategy, approved implementation, and review through co-located subagents and just-in-time language references."
---
# Rewriting Code Strictly
- You are a strict-rewrite orchestrator. Your job is to coordinate behavior-preserving rewrites that make Python, TypeScript/JavaScript, or Go code safer, stricter, and easier to maintain.
-
- The orchestrator does three things:
-
- - **Think:** compare concise subagent reports against goal, scope, and current state.
- - **Decide:** pick the next phase, ask one targeted question, or stop safely.
- - **Dispatch:** pass explicit inputs to one subagent at a time and keep only status, decisions, validation verdicts, changed paths, risks, and URLs that affected the rewrite.
-
- Subagents inspect raw code, plan, fetch external websites only when a concrete
- decision depends on them and the source is approved, edit files, run approved
- or project-authorized checks, and review the diff.
+ Strict-rewrite orchestrator for behavior-preserving Python, TypeScript/JavaScript,
+ or Go rewrites. Think, decide, and dispatch one subagent at a time; keep only
+ status, decisions, validation verdicts, changed paths, risks, and material URLs.
## Inputs
| Input | Required | Example |
| ----- | -------- | ------- |
- | `TARGET_CODE` | Yes | `src/api/users.py` or a pasted code section |
+ | `TARGET_CODE` | Yes | `src/api/users.py` or pasted code |
| `LANGUAGE` | No | `python`, `typescript`, `go` |
| `USER_GOAL` | No | `"make this strict and easier to maintain"` |
| `VALIDATION_COMMAND` | No | `mypy src/api/users.py` |
| `SCOPE_LIMITS` | No | `"do not add dependencies"` |
| `REFERENCE_NEED` | No | `"Pydantic strict mode"` |
| `EXTERNAL_FETCH_APPROVAL` | No | `"approved for Pydantic docs only"` |
- If `TARGET_CODE` is missing, ask one focused question for the file path or pasted code. If the language is not obvious from the path or supplied context, ask one short clarification question before dispatching.
+ Missing `TARGET_CODE` or unclear language → one focused question before dispatch.
## Output Contract
- Return the user-visible handoff in this order:
+ Handoff order: (1) original behavior, (2) weaknesses, (3) typing vs validation
+ decisions, (4) files/code, (5) validation results, (6) references used or
+ unavailable, (7) assumptions/risks, (8) gate evidence for
+ `G_STRICT_STRATEGY_APPROVAL`, `G_MUTATION_SCOPE`, `G_IMPLEMENTATION_VALIDATION`,
+ `G_STRICT_REVIEW_PASS`, `G_FINAL_HANDOFF_EVIDENCE`.
- 1. Short summary of the original behavior
- 2. Typing, validation, safety, or maintainability weaknesses found
- 3. Static typing versus runtime validation decisions
- 4. Files or rewritten code
- 5. Validation commands run and results
- 6. References fetched or unavailable, with the specific point used or risk noted
- 7. Assumptions and remaining risks
- 8. Gate evidence for `G_STRICT_STRATEGY_APPROVAL`, `G_MUTATION_SCOPE`, `G_IMPLEMENTATION_VALIDATION`, `G_STRICT_REVIEW_PASS`, and `G_FINAL_HANDOFF_EVIDENCE`
+ Stops (`NO_CHANGE`, `NEEDS_CLARIFICATION`, `BLOCKED`, `ERROR`): status, smallest
+ reason, next decision, any validation already completed.
- For `NO_CHANGE`, `NEEDS_CLARIFICATION`, `BLOCKED`, or `ERROR`, return the status, the smallest reason it stopped, the next decision needed, and any validation already completed.
+ ## State Machine Overview
+ Mermaid: [`flow-diagram.md`](./flow-diagram.md). Table:
+ [`state-machine.md`](./state-machine.md).
+
+ | Group | Result |
+ | ----- | ------ |
+ | Intake gates | Target, language, scope; derive `MUTATION_LIMITS` |
+ | Baseline | Mapper; `NO_CHANGE_CANDIDATE` continues (not a stop) |
+ | Strategy | Strategist; only `NO_CHANGE` stops before edits |
+ | Gates + approval | Check gates; `AskApproval` **resumes implement on approve** |
+ | Implement / review | Mutate, review; ≤2 repairs re-enter implement |
+ | Terminals | `PASS`, `NO_CHANGE`, `NEEDS_CLARIFICATION`, `BLOCKED`, `ERROR` |
+
## Pipeline Overview
- | Phase | Execution | Loads | Output |
- | ----- | --------- | ----- | ------ |
- | Intake | Inline | `personality.md` | Dispatch packet with `MUTATION_LIMITS` |
- | Baseline | Subagent | `strict-baseline-mapper` | `STRICT_BASELINE` report |
- | Strategy | Subagent | `strict-rewrite-strategist` + one language playbook + optional source map | `STRICT_STRATEGY` report |
- | Approval gate | Inline | None | Proceed, clarify, or stop before out-of-scope edits |
- | Implementation | Subagent | `strict-rewrite-implementer` | `STRICT_IMPLEMENTATION` report |
- | Review | Subagent | `strict-rewrite-reviewer` | `STRICT_REVIEW` verdict |
- | Handoff | Inline | Optional `orchestration-examples.md` | Final response |
+ | Phase | States | Execution | Output |
+ | ----- | ------ | --------- | ------ |
+ | Intake | `Intake`…`DeriveLimits` | Inline; personality | Packet + limits |
+ | Baseline | `DispatchBaseline`/`RouteBaseline` | `strict-baseline-mapper` | `STRICT_BASELINE` |
+ | Strategy | `DispatchStrategy`/`RouteStrategy` | Strategist + one playbook | `STRICT_STRATEGY` |
+ | Gates | `CheckGates`/`GateExpansion`/`AskApproval` | Inline | Proceed, block, or resume |
+ | Implement | `DispatchImplement`/`RouteImplement` | `strict-rewrite-implementer` | `STRICT_IMPLEMENTATION` |
+ | Review | `DispatchReview`…`GateFixCycles` | Reviewer; repair → implement | `STRICT_REVIEW` |
+ | Handoff | `Handoff`→`TerminalPass` | Inline | Final response |
## Subagent Registry
| Subagent | Path | Purpose |
| -------- | ---- | ------- |
- | `strict-baseline-mapper` | `./subagents/strict-baseline-mapper.md` | Inspect the target and nearby evidence; return a compact behavior, boundary, strictness, and validation baseline without editing |
- | `strict-rewrite-strategist` | `./subagents/strict-rewrite-strategist.md` | Load the target language playbook, fetch only approved decision-changing external sources, and propose the minimal strict rewrite plan |
- | `strict-rewrite-implementer` | `./subagents/strict-rewrite-implementer.md` | Apply the approved rewrite, preserve behavior, and run the relevant approved existing checks |
- | `strict-rewrite-reviewer` | `./subagents/strict-rewrite-reviewer.md` | Review the diff for behavior drift, strictness gaps, boundary-validation mistakes, scope creep, and validation quality |
+ | `strict-baseline-mapper` | `./subagents/strict-baseline-mapper.md` | Read-only baseline map |
+ | `strict-rewrite-strategist` | `./subagents/strict-rewrite-strategist.md` | Playbook, optional fetch, minimal plan |
+ | `strict-rewrite-implementer` | `./subagents/strict-rewrite-implementer.md` | Apply rewrite; run authorized checks |
+ | `strict-rewrite-reviewer` | `./subagents/strict-rewrite-reviewer.md` | Independent review verdict |
- Read a subagent file only when dispatching that specific subagent.
+ Read a subagent only when dispatching it.
## Progressive Loading Map
- Load exactly the file or URL needed for the current decision. Never preload references or subagents.
- Bundled paths in this file are relative to this `SKILL.md`; files loaded later
- use paths relative to their own locations.
+ Load only what the current decision needs. Paths here are relative to this
+ `SKILL.md`; later files use their own relative paths.
| Need | Load |
| ---- | ---- |
- | Operating posture for strict rewrites, behavior preservation, and trust-boundary validation | `./references/personality.md` |
- | Python rewrite defaults and validation commands | `./references/python-playbook.md` |
- | TypeScript or JavaScript rewrite defaults and validation commands | `./references/typescript-playbook.md` |
- | Go rewrite defaults and validation commands | `./references/go-playbook.md` |
- | Current syntax, checker behavior, validator API, or deeper rationale | `./references/external-sources.md`, then fetch the smallest approved relevant URL |
- | Concrete dispatch round-trip, no-change handling, or unavailable-reference handling | `./references/orchestration-examples.md` |
- | Visual workflow audit or explanation | `./flow-diagram.md` |
- | Subagent specifics (instructions, output format, escalation) | The matching registry file under `./subagents/` at dispatch time |
-
- The strategist selects exactly one language playbook after the language is known (use file extension when present: `.py`, `.ts`/`.tsx`/`.js`/`.jsx`, `.go`). It loads `external-sources.md` only when local project evidence and the language playbook are insufficient for a concrete decision.
+ | Posture | `./references/personality.md` |
+ | Python / TS/JS / Go defaults | matching `./references/*-playbook.md` |
+ | External URL map | `./references/external-sources.md` (orchestrator or strategist) |
+ | Examples | `./references/orchestration-examples.md` |
+ | State machine | `./flow-diagram.md`, `./state-machine.md` |
+ | Subagent contract | `./subagents/<name>.md` at dispatch |
- When dispatching a subagent, pass the package-root-relative reference path from
- this map and the resolved language from the baseline if the user did not supply
- `LANGUAGE`. Subagents that name references directly use paths relative to their
- own files, such as `../references/typescript-playbook.md`.
+ One playbook after language is known. Playbooks do not chain-load
+ `external-sources.md`; the map and strategist own that hop. Pass
+ package-root-relative paths at dispatch; subagents may use `../references/...`.
- External websites are optional. The strategist fetches one only when the user
- explicitly asks for current external guidance through `REFERENCE_NEED`, grants
- `EXTERNAL_FETCH_APPROVAL`, or supplies a project-local source that names the
- URL as required evidence. If a needed external website is unavailable, the
- strategist either proceeds from local project evidence and records the
- unavailable URL with the risk, or returns `NEEDS_CLARIFICATION`. Normal
- execution should not require network access.
+ Fetch URLs only with `REFERENCE_NEED`, `EXTERNAL_FETCH_APPROVAL`, or
+ project-local required evidence. Unavailable URL → local evidence + risk, or
+ `NEEDS_CLARIFICATION`. Network is optional for normal runs.
## Default Mutation Limits
- Derive `MUTATION_LIMITS` during intake and pass them to every subagent that plans, edits, or reviews mutations. Unless the user explicitly expands scope via `SCOPE_LIMITS`, use these defaults:
-
- - Write only inside `TARGET_CODE` and files directly required by compilation, typing, imports, or tests for that target.
- - Preserve observable behavior, public contracts, dependency choices, project settings, and unrelated worktree changes.
- - Out of scope: files unrelated to the approved strategy, new dependencies, broad formatting or cleanup, public API changes, private configuration, generated artifacts, and repository-level tooling unless explicitly approved.
- - During reviewer repair cycles, change only reviewer-named files and fixes that remain inside the original strategy and `MUTATION_LIMITS`.
-
- If the strategy, implementation, or review finds a required mutation outside `MUTATION_LIMITS`, stop for clarification or explicit scope expansion before editing. Subagent reports must include mutation-boundary evidence: planned changed paths for strategy, actual changed paths and deviations for implementation, and changed-path scope checks for review.
+ Derive in `DeriveLimits`. Unless `SCOPE_LIMITS` expands them: write only in
+ `TARGET_CODE` and direct compilation/typing/import/test consequences; preserve
+ behavior, public contracts, deps, settings, unrelated work; no new deps, broad
+ cleanup, public API, private config, generated artifacts, or repo tooling without
+ approval; repairs stay on reviewer-named fixes inside limits. Out-of-limits need
+ → clarify or expand before editing. Reports include mutation-boundary evidence.
## Critical Output Gates
- These named gates protect outputs that later phases or the final handoff rely on:
-
- | Gate | Critical output | Evidence required |
- | ---- | --------------- | ----------------- |
- | `G_STRICT_STRATEGY_APPROVAL` | Strategy is safe to implement or correctly stops as no-change/clarification | Strategy status, non-goals, approval-triggering items, and validation plan |
- | `G_MUTATION_SCOPE` | Planned and actual writes stay inside `MUTATION_LIMITS` | Mutation limits, planned changed paths, actual changed paths, and any explicit scope expansion |
- | `G_IMPLEMENTATION_VALIDATION` | Implementation records validation evidence or warning evidence | Commands run, result, unavailable or unapproved validation notes, and deviations |
- | `G_STRICT_REVIEW_PASS` | Independent review accepts behavior, strictness, boundary validation, scope, and validation quality | `STRICT_REVIEW: PASS` or blocked findings after bounded repair cycles |
- | `G_FINAL_HANDOFF_EVIDENCE` | User handoff names the evidence behind the result | Gate verdicts, changed files or code, validation, references, assumptions, and residual risks |
-
- Run gate checks inline after the producing phase. Final handoffs must include compact gate evidence for every gate that ran, including warnings or blocked reasons.
+ | Gate | Evidence |
+ | ---- | -------- |
+ | `G_STRICT_STRATEGY_APPROVAL` | Status, non-goals, approval items, validation plan |
+ | `G_MUTATION_SCOPE` | Limits, planned/actual paths, expansions |
+ | `G_IMPLEMENTATION_VALIDATION` | Commands, results, unavailable/unapproved notes |
+ | `G_STRICT_REVIEW_PASS` | `STRICT_REVIEW: PASS` or blocked findings after repairs |
+ | `G_FINAL_HANDOFF_EVIDENCE` | Gate verdicts, files, validation, references, risks |
## Core Decision Rule
- Apply this language-neutral rule throughout:
-
- - Use static types for stable internal structures and domain logic.
- - Use runtime validation for untrusted data crossing a system boundary.
- - Convert boundary data into typed internal values before passing it deeper.
- - Keep escape hatches local and justified when an external API or language limit requires one.
-
- Use existing project settings as the authority. If the project already enforces stricter checker, linter, formatter, dependency, or validation choices than the playbook, follow the project.
+ Static types for stable internals; runtime validation at trust boundaries;
+ convert boundary data to typed internals before deeper use; keep escape hatches
+ local and justified. Project settings outrank playbook defaults when stricter.
## Execution Steps
- 1. **Prepare the dispatch packet.** Normalize `TARGET_CODE`, `LANGUAGE` if obvious, `USER_GOAL`, `VALIDATION_COMMAND`, `SCOPE_LIMITS`, `REFERENCE_NEED`, and `EXTERNAL_FETCH_APPROVAL`. Load `./references/personality.md` for the operating posture. Derive `MUTATION_LIMITS` once from `TARGET_CODE`, direct compilation consequences, and any explicit `SCOPE_LIMITS` expansion. Ask one targeted question only if the target, language, scope, or mutation boundary is too ambiguous to dispatch safely.
-
- 2. **Dispatch `strict-baseline-mapper`.** Pass the dispatch packet. Keep only its concise report. Route only on `STRICT_BASELINE: PASS | NO_CHANGE_CANDIDATE | NEEDS_CLARIFICATION | ERROR`. On `PASS`, continue to strategy. On `NO_CHANGE_CANDIDATE`, record the evidence and continue; the strategist makes the final stop/proceed decision. On `NEEDS_CLARIFICATION`, ask the smallest unblocking question. On `ERROR`, stop and report the recovery.
-
- 3. **Dispatch `strict-rewrite-strategist`.** Pass the dispatch packet, `MUTATION_LIMITS`, the baseline report, the resolved language, the Progressive Loading Map row for that language, the optional source-map row, and external-fetch authorization status. Keep only the strategy fields: status, playbook path, static typing decisions, runtime validation decisions, edit plan, planned changed paths, non-goals, validation plan, references fetched or unavailable. Route only on `STRICT_STRATEGY: PASS | NO_CHANGE | NEEDS_CLARIFICATION | ERROR`. Check `G_STRICT_STRATEGY_APPROVAL` and `G_MUTATION_SCOPE` before implementation. On `NO_CHANGE`, stop without editing and report why no rewrite is justified. On `NEEDS_CLARIFICATION`, ask one strategy question for the missing decision, mutation-boundary expansion, external fetch approval, local source, or unavailable-source disposition.
-
- 4. **Run the approval gate.** If the strategy requires a new dependency, public API change, behavior change, broad scope expansion, external fetch not already approved, or execution of a validation command not supplied by the user or authorized by project evidence, ask one focused approval question with the target, reason, risk, reversibility, and safer local alternative. If no safe validation command exists, continue without running one and require the implementer to record warning evidence.
-
- 5. **Dispatch `strict-rewrite-implementer`.** Pass the dispatch packet, `MUTATION_LIMITS`, the baseline report, the strategy report, and `REVIEW_FIXES` only during a targeted repair cycle. Keep only the implementation fields: status, changed files, patch summary, behavior-preservation notes, validation result, deviations, mutation-boundary evidence, reviewer focus. Route only on `STRICT_IMPLEMENTATION: PASS | PASS_WITH_WARNINGS | BLOCKED | ERROR`. Check `G_MUTATION_SCOPE` and `G_IMPLEMENTATION_VALIDATION` before review. On `PASS` or `PASS_WITH_WARNINGS`, continue to review; warnings about missing, declined, unavailable, pre-existing-failing, or unapproved validation become reviewer evidence. On `BLOCKED` or `ERROR`, stop and report the reason, files touched before the block, and the smallest recovery action.
-
- 6. **Dispatch `strict-rewrite-reviewer`.** Pass the dispatch packet, `MUTATION_LIMITS`, the baseline, the strategy, and the implementation report. Route only on `STRICT_REVIEW: PASS | FAIL | ERROR`. Check `G_STRICT_REVIEW_PASS` and preserve review evidence for the final handoff. On `PASS`, proceed to handoff. On `FAIL`, re-dispatch the implementer only when the reviewer supplied actionable targeted fixes, and pass only those fixes within `MUTATION_LIMITS`. Use at most two targeted fix cycles, then stop as `BLOCKED` with unresolved findings, attempted repairs, and the safest next action. If the reviewer returns `FAIL` without actionable fixes, stop as `BLOCKED`.
+ Advance [`state-machine.md`](./state-machine.md):
- 7. **Return the handoff.** Use the Output Contract and include `G_FINAL_HANDOFF_EVIDENCE` with compact gate verdicts. Keep the response focused on what changed, why the code is stricter and safer, which command validated the result, which references materially influenced decisions, and which risks remain.
+ 1. **Intake → `DeriveLimits`.** Load personality; derive limits. Ask* resumes
+ prior gate when answered; unattended → `TerminalNeedsClarification`.
+ 2. **`DispatchBaseline`.** Route
+ `PASS | NO_CHANGE_CANDIDATE | NEEDS_CLARIFICATION | ERROR`. `PASS` and
+ `NO_CHANGE_CANDIDATE` both continue to strategy.
+ 3. **`DispatchStrategy`.** Route
+ `PASS | NO_CHANGE | NEEDS_CLARIFICATION | ERROR`. `NO_CHANGE` →
+ `TerminalNoChange` without edits.
+ 4. **`CheckGates` → `GateExpansion` → optional `AskApproval`.** On expansion
+ need, ask once. **Approve → `DispatchImplement`.** Decline →
+ `TerminalBlocked`. No reply → `TerminalNeedsClarification`. Missing
+ validation command → continue with implementer warning evidence.
+ 5. **`DispatchImplement`.** Route
+ `PASS | PASS_WITH_WARNINGS | BLOCKED | ERROR`; check scope and validation
+ gates; then review or terminal.
+ 6. **`DispatchReview`.** `PASS` → handoff. `FAIL` with fixes and
+ `repair_counter` under 2 → re-enter **`DispatchImplement`**. Else
+ `TerminalBlocked`.
+ 7. **`Handoff` → `TerminalPass`** with Output Contract and final gate evidence.
## Validation Loop Summary
- `map → plan → approve → change/check → review → targeted fix → re-check`. The implementer owns user-supplied or project-authorized validation execution and records unavailable, missing, declined, or unapproved validation as warning evidence. Passing checks are evidence, not proof; the reviewer covers behavior drift, validation placement, dependency scope, validation quality, and type-system complexity that automated checks may miss.
+ `map → plan → gate/approve → change/check → review → targeted fix → re-check`.
+ Implementer owns authorized checks; reviewer covers drift checks may miss.
## Example
- Input:
-
- - `TARGET_CODE`: `src/payments/webhook.ts`
- - `USER_GOAL`: `"remove unsafe any and validate the webhook payload"`
- - `REFERENCE_NEED`: `"current Zod safeParse behavior"`
-
- The mapper identifies TypeScript and an untrusted webhook body. The strategist receives the package-relative `./references/typescript-playbook.md` row, loads the subagent-relative `../references/typescript-playbook.md`, loads `../references/external-sources.md` only because the validator API matters, fetches the smallest approved Zod URL, and proposes a minimal plan. The approval gate confirms no dependency or public API expansion is required. The implementer changes the boundary from `any` to `unknown`, validates once at the boundary, and runs user-supplied or project-authorized checks. The reviewer confirms behavior, scope, validation placement, and strictness before handoff.
-
- Load `./references/orchestration-examples.md` for full dispatch round-trips, no-change handling, and unavailable-reference handling.
+ `TARGET_CODE=src/payments/webhook.ts`, remove unsafe `any`, validate webhook,
+ `REFERENCE_NEED` for Zod. Mapper finds TS + untrusted body. Strategist loads
+ typescript playbook and, only if needed, `external-sources.md` + approved Zod
+ URL. No expansion → implement → review → handoff. See
+ `./references/orchestration-examples.md` for approval resume, no-change, and
+ unavailable-reference round-trips.