fix(#2662): add state.add-roadmap-evolution SDK handler; insert-phase uses it (#2683)

/gsd-insert-phase step 4 instructed the agent to directly Edit/Write
.planning/STATE.md to append a Roadmap Evolution entry. Projects that
ship a protect-files.sh PreToolUse hook (a recommended hardening
pattern) blocked the raw write, silently leaving STATE.md out of sync
with ROADMAP.md.

Adds a dedicated SDK handler state.add-roadmap-evolution (plus space
alias) that:

  - Reads STATE.md through the shared readModifyWriteStateMd lockfile
    path (matches sibling mutation handlers — atomic against
    concurrent writers).
  - Locates ### Roadmap Evolution under ## Accumulated Context, or
    creates both sections as needed.
  - Dedupes on exact-line match so idempotent retries are no-ops
    ({ added: false, reason: "duplicate" }).
  - Validates --phase / --action presence and action membership,
    throwing GSDError(Validation) for bad input (no silent
    { ok: false } swallow).

Workflow change (insert-phase.md step 4):

  - Replaces the raw Edit/Write instructions for STATE.md with
    gsd-sdk query state.patch (for the next-phase pointer) and
    gsd-sdk query state.add-roadmap-evolution (for the evolution
    log).
  - Updates success criteria to check handler responses.
  - Drops "Write" from commands/gsd/insert-phase.md allowed-tools
    (no step in the workflow needs it any more).

Tests (vitest, sdk/src/query/state-mutation.test.ts): subsection
creation when missing; append-preserving-order when present;
duplicate -> reason=duplicate; idempotence over two calls; three
validation cases covering missing --phase, missing --action, and
invalid action.

This is the first SDK handler dedicated to STATE.md Roadmap
Evolution mutations. Other workflows with similar raw STATE.md
edits (/gsd-pause-work, /gsd-resume-work, /gsd-new-project,
/gsd-complete-milestone, /gsd-add-phase) remain on raw Edit/Write
and will need follow-up issues to migrate — out of scope for this
fix.

Closes #2662
This commit is contained in:
Tom Boucher
2026-04-24 20:20:02 -04:00
committed by GitHub
parent 7b470f2625
commit 303fd26b45
6 changed files with 334 additions and 13 deletions

View File

@@ -63,19 +63,35 @@ Extract from result: `phase_number`, `after_phase`, `name`, `slug`, `directory`.
</step>
<step name="update_project_state">
Update STATE.md to reflect the inserted phase:
Update STATE.md to reflect the inserted phase via SDK handlers (never raw
`Edit`/`Write` — projects may ship a `protect-files.sh` PreToolUse hook that
blocks direct STATE.md writes):
1. Read `.planning/STATE.md`
2. Update STATE.md's next-phase pointers to the newly inserted phase `{decimal_phase}`:
- Update structured field(s) used by tooling (e.g. `current_phase:`) to `{decimal_phase}`.
- Update human-readable recommendation text (e.g. `## Current Phase`, `Next recommended run:`) to `{decimal_phase}`.
- If multiple pointer locations exist, update all of them in the same edit.
3. Under "## Accumulated Context" → "### Roadmap Evolution" add entry:
```
- Phase {decimal_phase} inserted after Phase {after_phase}: {description} (URGENT)
1. Update STATE.md's next-phase pointer(s) to the newly inserted phase
`{decimal_phase}`:
```bash
gsd-sdk query state.patch '{"Current Phase":"{decimal_phase}","Next recommended run":"/gsd:plan-phase {decimal_phase}"}'
```
If "Roadmap Evolution" section doesn't exist, create it.
(Adjust field names to whatever pointers STATE.md exposes — the handler
reports which fields it matched.)
2. Append a Roadmap Evolution entry via the dedicated handler. It creates the
`### Roadmap Evolution` subsection under `## Accumulated Context` if missing
and dedupes identical entries:
```bash
gsd-sdk query state.add-roadmap-evolution \
--phase {decimal_phase} \
--action inserted \
--after {after_phase} \
--note "{description}" \
--urgent
```
Expected response shape: `{ added: true, entry: "- Phase ... (URGENT)" }`
(or `{ added: false, reason: "duplicate", entry: ... }` on replay).
</step>
<step name="completion">
@@ -129,6 +145,7 @@ Phase insertion is complete when:
- [ ] `gsd-sdk query phase.insert` executed successfully
- [ ] Phase directory created
- [ ] Roadmap updated with new phase entry (includes "(INSERTED)" marker)
- [ ] STATE.md updated with roadmap evolution note
- [ ] `gsd-sdk query state.add-roadmap-evolution ...` returned `{ added: true }` or `{ added: false, reason: "duplicate" }`
- [ ] `gsd-sdk query state.patch` returned matched next-phase pointer field(s)
- [ ] User informed of next steps and dependency implications
</success_criteria>