Refactor — one structural experiment
Refactor changes structure while preserving observable behavior. It performs one
caller-selected transformation and reports the result.
Prompt
Refactor billing-service/internal/retry/backoff.go: extract the exponential backoff calculation out of RetryRequest into its own function, no other behavior change. Record a baseline, run go test ./internal/retry/... before and after, and report the diff summary, commands, results, and anything not checked.
It's working if
- The report names the preserved behavior and cites
go test ./internal/retry/... run both before and after.
git diff --stat touches only internal/retry/backoff.go, never an unrelated file.
- Golden-output hashes get captured and compared byte-for-byte whenever the changed surface produces output, e.g.
sha256sum before and after.
- The report's
behavior not checked list is present in the output even when empty, naming any surface the gates skipped.
Procedure
- Name the preserved behavior and the focused acceptance surface.
- Record an honest baseline, including any reproducible ambient failures.
- Apply one bounded transformation: extract, rename, inline, simplify,
encapsulate, move, or delete dead code.
- Run the focused check and the smallest package-level regression check justified
by the changed surface.
- Return the diff summary, commands, results, and behavior not checked.
Do not combine a newly discovered behavior fix with the structural change. A red
result is evidence for the caller; this skill does not revert, narrow, retry,
commit, validate, or route subsequent work automatically.
Seam experiments before commitment
When the transformation needs a seam — an extraction boundary, interface, or
module split — and more than one candidate seam exists, probe before you cut.
Run the probe in disposable isolation (a scratch branch, worktree, or copied
tree the caller's policy allows): rough in the seam, see what it forces —
signature churn, import cycles, test rewrites — then discard the probe and
keep only the knowledge. Stop condition: at most two probes; if the second
candidate seam also fights back, report both findings to the caller instead of
trying a third. Cutting the first imaginable seam directly into the working
tree is the premature seam failure mode: the wrong boundary calcifies
because reverting it now costs more than living with it.
Neutrality gates
"Behavior-preserving" is a claim to execute, not assert. Gate the
transformation on behavior-identical proof:
- The focused check and the package-level regression check pass both before
and after, with the same set of pre-existing failures — no new red, and no
quietly vanished red either (a test that stops running is a behavior change).
- For output-producing surfaces (generators, serializers, formatters, reports),
hash the outputs: capture golden-output hashes over identical inputs before
the change and compare byte-for-byte after. A hash mismatch is a behavior
diff to surface and explain, never to shrug at; the caller decides whether to
keep, narrow, or reverse the change.
- Observable error messages, exit codes, and public signatures on the changed
surface are part of behavior unless the caller excluded them.
A neutrality gate that was skipped or narrowed after the fact is the
post-hoc neutrality failure mode — the diff decides what got tested. Name
any surface the gates did not cover in the report's behavior-not-checked list.
References
- Behavior-preserving simplification
- Behavior scenarios
1---
2name: refactor
3description: 'Execute one behavior-preserving structural transformation and report evidence. Triggers: "refactor this", "simplify without changing behavior".'
4practices:
5- refactoring
6- legacy-code-seams
7- design-patterns
8hexagonal_role: supporting
9consumes:
10- repo-context
11produces:
12- code-changes
13context_rel: []
14skill_api_version: 1
15context:
16 window: fork
17 intent:
18 mode: task
19 sections:
20 exclude:
21 - HISTORY
22metadata:
23 capabilities: [refactor]
24 effects: [modify_source_files]
25 canonical_status: canonical
26 disposition: keep_specialist
27 tier: execution
28 dependencies: []
29output_contract: code changes with regression evidence
30---
31
32# Refactor — one structural experiment
33
34Refactor changes structure while preserving observable behavior. It performs one
35caller-selected transformation and reports the result.
36
37## Prompt
38
39```text
40Refactor billing-service/internal/retry/backoff.go: extract the exponential backoff calculation out of RetryRequest into its own function, no other behavior change. Record a baseline, run go test ./internal/retry/... before and after, and report the diff summary, commands, results, and anything not checked.
41```
42
43## It's working if
44
45- The report names the preserved behavior and cites `go test ./internal/retry/...` run both before and after.
46- `git diff --stat` touches only `internal/retry/backoff.go`, never an unrelated file.
47- Golden-output hashes get captured and compared byte-for-byte whenever the changed surface produces output, e.g. `sha256sum` before and after.
48- The report's `behavior not checked` list is present in the output even when empty, naming any surface the gates skipped.
49
50## Procedure
51
521. Name the preserved behavior and the focused acceptance surface.
532. Record an honest baseline, including any reproducible ambient failures.
543. Apply one bounded transformation: extract, rename, inline, simplify,
55 encapsulate, move, or delete dead code.
564. Run the focused check and the smallest package-level regression check justified
57 by the changed surface.
585. Return the diff summary, commands, results, and behavior not checked.
59
60Do not combine a newly discovered behavior fix with the structural change. A red
61result is evidence for the caller; this skill does not revert, narrow, retry,
62commit, validate, or route subsequent work automatically.
63
64## Seam experiments before commitment
65
66When the transformation needs a seam — an extraction boundary, interface, or
67module split — and more than one candidate seam exists, probe before you cut.
68Run the probe in disposable isolation (a scratch branch, worktree, or copied
69tree the caller's policy allows): rough in the seam, see what it forces —
70signature churn, import cycles, test rewrites — then discard the probe and
71keep only the knowledge. Stop condition: at most two probes; if the second
72candidate seam also fights back, report both findings to the caller instead of
73trying a third. Cutting the first imaginable seam directly into the working
74tree is the **premature seam** failure mode: the wrong boundary calcifies
75because reverting it now costs more than living with it.
76
77## Neutrality gates
78
79"Behavior-preserving" is a claim to execute, not assert. Gate the
80transformation on behavior-identical proof:
81
82- The focused check and the package-level regression check pass both before
83 and after, with the same set of pre-existing failures — no new red, and no
84 quietly vanished red either (a test that stops running is a behavior change).
85- For output-producing surfaces (generators, serializers, formatters, reports),
86 hash the outputs: capture golden-output hashes over identical inputs before
87 the change and compare byte-for-byte after. A hash mismatch is a behavior
88 diff to surface and explain, never to shrug at; the caller decides whether to
89 keep, narrow, or reverse the change.
90- Observable error messages, exit codes, and public signatures on the changed
91 surface are part of behavior unless the caller excluded them.
92
93A neutrality gate that was skipped or narrowed after the fact is the
94**post-hoc neutrality** failure mode — the diff decides what got tested. Name
95any surface the gates did not cover in the report's behavior-not-checked list.
96
97## References
98
99- [Behavior-preserving simplification](references/behavior-preserving-simplification.md)
100- [Behavior scenarios](references/refactor.feature)