haiku-method icon indicating copy to clipboard operation
haiku-method copied to clipboard

decompose phase loops on coverage-mapping discovery after repeated successful haiku_discovery_complete { ok: true }

Open pgarza98 opened this issue 6 months ago • 3 comments

Versions

  • MCP / plugin: 4.5.6 (both)
  • Studio / stage: software / product
  • Intent: location-timesheet-summary

Symptom

After a per-stage reset of the product stage and a fresh run-through, the workflow engine emits a decompose prompt whose body contains the Discovery Fan-Out section listing coverage-mapping as the one outstanding discovery artifact — even though haiku_discovery_complete has returned { ok: true } for that template on three separate attempts.

Each haiku_run_next tick re-emits the same Discovery Fan-Out prompt asking for coverage-mapping. The decomposition phase is never reached.

Reproduction

  1. Reset the product stage: haiku_stage_reset { intent: "location-timesheet-summary", stage: "product" }.
  2. Tick to elaborate; record elaboration; verify; pass.
  3. Tick to decompose. Engine fans out 4 discovery subagents (acceptance-criteria, behavioral-spec, coverage-mapping, data-contracts).
  4. 3 of 4 merge cleanly. coverage-mapping hits discovery_merge_conflict on product/DATA-CONTRACTS.md + stages/product/elaboration.md (sibling-owned files).
  5. Run haiku_run_next → engine emits a new decompose prompt asking only for coverage-mapping. Spawn the retry agent.
  6. Retry agent resolves the merge conflicts via --theirs on sibling files, commits, calls haiku_discovery_complete { template: \"coverage-mapping\" }. Receives { ok: true, message: \"merged haiku/<intent>/discovery-product-coverage-mapping → haiku/<intent>/product\" }.
  7. Run haiku_run_next → engine emits the same decompose prompt asking for coverage-mapping again.
  8. Spawn a 3rd attempt: working tree clean, artifact already committed, haiku_discovery_complete returns { ok: true } again.
  9. Run haiku_run_next → engine emits the same decompose prompt asking for coverage-mapping a 4th time.

Expected

After haiku_discovery_complete { ok: true } for all 4 templates (acceptance-criteria, behavioral-spec, coverage-mapping, data-contracts), the next haiku_run_next tick should advance past the Discovery Fan-Out section and emit the actual unit-decomposition instructions.

Evidence

All four discovery artifacts are physically present and committed on the stage branch haiku/location-timesheet-summary/product:

``` .haiku/intents/location-timesheet-summary/product/ACCEPTANCE-CRITERIA.md (53,892 bytes) .haiku/intents/location-timesheet-summary/product/DATA-CONTRACTS.md (44,496 bytes) .haiku/intents/location-timesheet-summary/knowledge/COVERAGE-MAPPING.md (42,638 bytes) .haiku/intents/location-timesheet-summary/features/*.feature (24 files) ```

Latest commit on the coverage-mapping isolation worktree: `3c027b4b4b9 haiku: merge discovery coverage-mapping into product`.

`haiku_stage_get { field: "discovery_complete" }` and `{ field: "discovery" }` both return `{ found: false, value: null }`, so there doesn't appear to be a `state.json` field tracking discovery completion.

Suspected cause

The engine's per-tick "needed discoveries" computation does not appear to credit a prior `haiku_discovery_complete { ok: true }` response — possibly because the completion marker is keyed off a state field (or git note / tag) that isn't being written on the merge path, or because the conflict-recovery path on the first attempt left a flag inconsistent with the final merged state.

Repro environment

  • macOS 25.3.0 (Darwin)
  • Claude Code (Opus 4.7, 1M context)
  • Project: `gigsmart/projects/monorepo` (mainline `dev`)
  • Branch at time of repro: `haiku/location-timesheet-summary/product`

Workaround used (so far)

None — looping with re-spawn each time. Considering `/haiku:repair` next.

pgarza98 avatar May 13 '26 17:05 pgarza98

Fix landed in PR #355, commit `4682f42fb`.

Root cause confirmed from the session logs: the coverage-mapping subagent wrote COVERAGE-MAPPING.md to knowledge/ (carrying over the design-stage convention) instead of the template's declared product/ location:

``` plugin/studios/software/stages/product/discovery/COVERAGE-MAPPING.md: location: .haiku/intents/{intent-slug}/product/COVERAGE-MAPPING.md ```

Actual on-disk state from the repro: ``` knowledge/COVERAGE-MAPPING.md ← where the subagent wrote it product/ ← where the cursor looked (ACCEPTANCE-CRITERIA.md, DATA-CONTRACTS.md only) ```

haiku_discovery_complete ff-merged the wrong-path commit and returned { ok: true }. The cursor's existence check on every subsequent tick then looked at product/COVERAGE-MAPPING.md, found it missing, and re-emitted the Discovery Fan-Out prompt asking for the same template. Three successive "fixes" all wrote to the same wrong directory and got the same lying success.

The fix is three coordinated changes:

  1. haiku_discovery_complete now verifies the artifact exists at the template's resolved location: inside the isolation worktree BEFORE merging. On miss it returns a new error code discovery_artifact_missing with expected_path, location, and a message naming the exact absolute path + git invocation + re-call to retry. The worktree is NOT reaped on this error so the subagent can fix the write location and retry without redispatch.

  2. The subagent's initial prompt now states the EXACT absolute artifact path under a dedicated "Required artifact path (EXACT)" section, computed from the template's location: resolved against the isolation worktree. Replaces the old vague guidance ("under the worktree's knowledge/ dir per the template's location:") that let this kind of drift happen.

  3. The error message itself echoes the same path so a parent agent reading just the failure response sees the same fix path as the subagent — initial prompt and failure prompt agree.

Test coverage in packages/haiku/test/haiku-discovery-complete.test.mjs pins the new gate (subagent calls with a wrong-path commit → tool returns discovery_artifact_missing with expected_path inside the worktree → worktree NOT reaped).

After this lands, the repro case will fail-loud on the first wrong-path attempt instead of looping silently. The agent will see the expected path and write to the right place on retry.

jwaldrip avatar May 13 '26 17:05 jwaldrip

Re-tested on 5.0.1 after the 4.0.0 → 5.x migration ran cleanly via #358's fix.

haiku_run_next advances past migrated → emits the same decompose prompt with the Discovery Fan-Out section listing coverage-mapping as the one outstanding artifact:

## Discovery Fan-Out (REQUIRED)
This stage produces 1 discovery artifact: `coverage-mapping`.

All four discovery artifacts remain physically present on the stage branch:

.haiku/intents/location-timesheet-summary/product/ACCEPTANCE-CRITERIA.md   (53,892 bytes)
.haiku/intents/location-timesheet-summary/product/DATA-CONTRACTS.md        (44,496 bytes)
.haiku/intents/location-timesheet-summary/knowledge/COVERAGE-MAPPING.md    (42,638 bytes)
.haiku/intents/location-timesheet-summary/features/*.feature               (24 files)

So the migration cleared #358, but the underlying state-tracking miss on coverage-mapping is intact in 5.0.1. Not spawning a 4th retry — we already established haiku_discovery_complete returns ok: true and the engine still re-asks on the next tick.

pgarza98 avatar May 13 '26 22:05 pgarza98

@pgarza98 — addressed in PR #360, commit `7732cd915`.

You're right that PR #355 only prevented the NEW occurrence (the engine no longer ff-merges a wrong-path subagent write). The file landed at `knowledge/COVERAGE-MAPPING.md` on the pre-fix engine, so the artifact-presence gate has nothing to gate on — the merge already happened.

The follow-up fix (in PR #360) self-heals that historical bad state. In `decompose.ts`, before the existence-check filter that drives re-emission, the engine now scans each artifact's declared `outputPath`. If the declared path is empty AND a same-name file exists at the legacy `knowledge/.md` location, it auto-relocates (renameSync) with three safety conditions:

  1. Source must exist at `knowledge/.md`.
  2. Destination must be empty (no overwrite).
  3. The legacy path must NOT equal the declared outputPath (no self-move for templates that legitimately declare `knowledge/.md`).

Console-logs both the success and any error so operators see what happened.

For your `location-timesheet-summary` intent, on the next `haiku_run_next` tick after PR #360 ships, the engine will move `.haiku/intents/location-timesheet-summary/knowledge/COVERAGE-MAPPING.md` → `.haiku/intents/location-timesheet-summary/product/COVERAGE-MAPPING.md` automatically. The existence check then passes and decompose advances.

If you want to unblock NOW without waiting for the release, the manual move is: ```bash mv .haiku/intents/location-timesheet-summary/knowledge/COVERAGE-MAPPING.md \ .haiku/intents/location-timesheet-summary/product/COVERAGE-MAPPING.md git add -A && git commit -m "relocate misplaced coverage-mapping artifact" ```

Test coverage in PR #360: `discovery-relocate-misplaced.test.mjs` (3 cases) pins the relocation contract, the skip-when-empty-source path, and the skip-when-destination-exists path.

jwaldrip avatar May 13 '26 22:05 jwaldrip