Skip to content

fix(bin): fold decision keys written after the status colon - #3

Merged
digbycampbell merged 2 commits into
mainfrom
fm/fm-decision-key-parse
Aug 12, 2026
Merged

digbycampbell merged 2 commits into
mainfrom
fm/fm-decision-key-parse

Conversation

@digbycampbell

Copy link
Copy Markdown
Owner

Intent

Fix a defect where worker decision markers were recorded under the wrong key, so answering an escalation with 'fm-send.sh --resolve-key ' was rejected.

OBSERVED DEFECT (repeatedly, 2026-08-11 and 2026-08-12): a crewmate appends a status line such as 'needs-decision: [key=palette-review-gate] no-mistakes review parked with 2 ask-user findings'. status_open_decisions in bin/fm-classify-lib.sh folded that record under the key 'default' rather than 'palette-review-gate'. The OPEN DECISIONS listing therefore printed a key that 'bin/fm-send.sh --resolve-key palette-review-gate' then refused with 'no open decision or blocker with that key'; only '--resolve-key default' worked. Firstmate had to guess before every keyed answer. It cost three failed sends on one escalation and recurred on at least four tasks across two days. Clearing state/..open-decisions-cursor did not help, confirming the parse rather than the cursor fold.

ROOT CAUSE, reproduced before changing anything: _fm_decision_key read only , the text BEFORE the first colon. A '[key=...]' token written after the state prefix lands in the note and is never seen, so the line keeps the 'default' key. Proven with a before/after run of the real status_open_decisions against the HEAD version of the library and the fixed one.

REQUIRED SHAPE OF THE FIX (from the task): the brief scaffold and the classify library are two halves of one contract, so whichever is wrong must be fixed so they match, and if the scaffold's documented shape is the odd one out, say so rather than quietly changing worker instructions.

DIAGNOSIS AND DELIBERATE DECISIONS:

  1. The scaffold IS the odd one out and this is stated explicitly in the commit message. bin/fm-classify-lib.sh documents the canonical position (token between the verb and the colon) and the charter-brief variant uses it, but the crewmate/ship brief's rule 6 showed a bare 'needs-decision: {summary of options}' and mentioned '[key=]' only in passing on the resolved line. It never stated where the token goes when OPENING a decision, so workers naturally wrote it after the colon. Rule 6 in both crewmate brief variants now states the canonical position explicitly.
  2. The parser is ALSO made tolerant, deliberately, rather than relying on the corrected instructions alone. This is a considered choice, not scope creep: firstmate's own coding guidelines require deterministic enforcement over agent memory for supervision infrastructure, the writer is a busy worker recalling a format under load, and the failure mode is a decision nothing can ever resolve. A '[key=]' token is now accepted either between the verb and the colon (canonical) or as the first token of the note, and both fold under the same key.
  3. status_line_note now strips a consumed note-position token so every consumer (fm-crew-state, fm-fleet-snapshot, fm-pending-reply-lib) reads the same note text regardless of the position the writer used, and the reserved-namespace vocabulary check still works for post-colon keys.
  4. Deliberate asymmetry, documented in the library header: a MALFORMED token before the colon still voids the line (it is an unambiguous failed attempt at a key), while after the colon the same bytes are indistinguishable from ordinary prose, so they are left in the note and the line keeps the 'default' key. A '[key=...]' appearing mid-note is not treated as a key.
  5. One-owner discipline: both positions route through a single _fm_decision_key_leading slug validator rather than duplicating the slug rules.

REGRESSION COVERAGE (required by the task, covering multi-key and already-resolved cases): tests/fm-send-resolve-key.test.sh gains test_note_position_key_folds_under_its_slug, which extends the existing file rather than adding a new runner and drives the REAL fm-send.sh and fm-wake-drain.sh executables over stubbed transports, never asserting implementation source bytes. It asserts that note-position records list under their own slugs, that a canonical-position record still works alongside them, that a note-position 'resolved: [key=...]' line closes its own decision, that the key token is not left duplicated inside the listed note, that --resolve-key is now ACCEPTED, that the historical wrong guess --resolve-key default is now refused before anything is sent, and that answering one key leaves the other records untouched.

VERIFICATION ALREADY DONE: 8 affected suites green with zero failures (fm-brief, fm-crew-state, fm-decision-hold-lifecycle, fm-fleet-snapshot-view, fm-pending-reply, both fm-wake-drain-open-decisions suites, fm-send-resolve-key). ShellCheck is not installed on this host so bin/fm-lint.sh could not run locally and CI is relied on for lint parity.

CONSTRAINTS: this is firstmate's own shared tracked material, so firstmate-coding-guidelines applies - one sentence per line in tracked Markdown, plain dashes not em dashes, shellcheck-clean shell, contracts stated once with cross-references rather than restatements, and no agent name as a commit co-author. Delivery is this repo's own no-mistakes pipeline and PR path. The PR must remain a DRAFT: the captain previews and merges queued PRs one at a time and firstmate owns the readiness queue, so it must not be marked ready for review.

What Changed

  • _fm_decision_key in bin/fm-classify-lib.sh now accepts a [key=<slug>] token either between the verb and the colon (canonical) or as the first token of the note, so a line like needs-decision: [key=palette-review-gate] ... folds under its own slug instead of default; both positions route through a new single _fm_decision_key_leading slug validator, and status_line_note strips a consumed note-position token so every consumer reads the same note text. A malformed token before the colon still voids the line, while after the colon it is left as prose - documented in the library header - and FM_OPEN_DECISIONS_FOLD_VERSION bumps to 3 so existing cursors refold.
  • Rule 6 in both crewmate brief variants in bin/fm-brief.sh now states the canonical key position explicitly (needs-decision [key=<slug>]: {summary} / blocked [key=<slug>]: {why}), which the scaffold previously only mentioned for the resolved line.
  • tests/fm-send-resolve-key.test.sh gains test_note_position_key_folds_under_its_slug, driving the real fm-send.sh and fm-wake-drain.sh over stubbed transports to assert note-position records list under their own slugs alongside a canonical-position record, that a note-position resolved: line closes its own decision, that the token is not duplicated in the listed note, that --resolve-key <slug> is accepted while --resolve-key default is refused, and that answering one key leaves the others untouched.

Risk Assessment

✅ Low: The parser change is small, backward-compatible (malformed and mid-note tokens still fall back to "default"), routes both positions through one slug validator, the reserved-namespace vocabulary check still holds under the new note stripping, the fold version is now bumped to 3 so persisted cursors refold, and the new test drives the real fm-send/fm-wake-drain executables rather than asserting source text.

Testing

Reproduced the reported defect end to end before touching anything: with the base library, the crewmate line needs-decision: [key=palette-review-gate] ... listed with the token stuck inside the note and --resolve-key palette-review-gate failed with "no open decision or blocker with that key" while --resolve-key default succeeded. On the target commit the same transcript flips: the listing advertises [key=palette-review-gate], the keyed answer is delivered and appends resolved [key=palette-review-gate]:, and --resolve-key default is now refused before any send. The new regression test in tests/fm-send-resolve-key.test.sh fails on the base library and passes on the fix, and all eight affected suites are green. This is a CLI/text surface with no rendered UI, so evidence is the CLI transcript plus the generated brief excerpt rather than a screenshot. Transient data/t-demo/ from the brief run was removed; the worktree is clean.

Evidence: Before/after CLI transcript: OPEN DECISIONS listing and fm-send.sh --resolve-key

BEFORE (base 566d737) listing: palette needs-decision: [key=palette-review-gate] no-mistakes review parked with 2 ask-user findings fm-send.sh palette --resolve-key palette-review-gate -> exit=1 "no open decision or blocker with that key ... nothing was sent" fm-send.sh palette --resolve-key default -> exit=0, appends "resolved [key=default]: answered: guessing" AFTER (target 68afa0d) listing: palette [key=palette-review-gate] needs-decision: no-mistakes review parked with 2 ask-user findings fm-send.sh palette --resolve-key palette-review-gate -> exit=0, typed to the worker: "fix both findings, then continue", appends "resolved [key=palette-review-gate]: answered: fix both findings, then continue", OPEN DECISIONS now empty fm-send.sh palette --resolve-key default -> exit=1, refused before anything is sent

===== BEFORE FIX (bin/fm-classify-lib.sh at base 566d737) =====
--- worker's status log (as the crewmate wrote it) ---
working: reviewing palette
needs-decision: [key=palette-review-gate] no-mistakes review parked with 2 ask-user findings

--- what firstmate sees: bin/fm-wake-drain.sh OPEN DECISIONS ---
OPEN DECISIONS (still open, folded from the durable status logs - not just the latest line):
palette needs-decision: [key=palette-review-gate] no-mistakes review parked with 2 ask-user findings
OPEN DECISIONS: close one by answering it: bin/fm-send.sh <task> --resolve-key <key> '<answer>'

--- answering with the advertised slug: bin/fm-send.sh palette --resolve-key palette-review-gate ---
WARNING: watcher still down (same stale episode; last beat: never, grace 300s) - full banner already printed this episode.
error: --resolve-key 'palette-review-gate': no open decision or blocker with that key in /tmp/tmp.bJwvBAsFbT/home/state/palette.status (already closed, mistyped, or transferred). Re-check the OPEN DECISIONS listing, then resend without that key or with the right one; nothing was sent.
exit=1
cat: /tmp/tmp.bJwvBAsFbT/send.log: No such file or directory
typed to the worker: 

--- the historical wrong guess: --resolve-key default ---
WARNING: watcher still down (same stale episode; last beat: never, grace 300s) - full banner already printed this episode.
exit=0

--- status log after the answer ---
working: reviewing palette
needs-decision: [key=palette-review-gate] no-mistakes review parked with 2 ask-user findings
resolved [key=default]: answered: guessing

--- OPEN DECISIONS after the answer ---

===== AFTER FIX (target 68afa0d) =====
--- worker's status log (as the crewmate wrote it) ---
working: reviewing palette
needs-decision: [key=palette-review-gate] no-mistakes review parked with 2 ask-user findings

--- what firstmate sees: bin/fm-wake-drain.sh OPEN DECISIONS ---
OPEN DECISIONS (still open, folded from the durable status logs - not just the latest line):
palette [key=palette-review-gate] needs-decision: no-mistakes review parked with 2 ask-user findings
OPEN DECISIONS: close one by answering it: bin/fm-send.sh <task> --resolve-key <key> '<answer>'

--- answering with the advertised slug: bin/fm-send.sh palette --resolve-key palette-review-gate ---
WARNING: watcher still down (same stale episode; last beat: never, grace 300s) - full banner already printed this episode.
exit=0
typed to the worker: fix both findings, then continue

--- the historical wrong guess: --resolve-key default ---
WARNING: watcher still down (same stale episode; last beat: never, grace 300s) - full banner already printed this episode.
error: --resolve-key 'default': no open decision or blocker with that key in /tmp/tmp.87IJb5IBCG/home/state/palette.status (already closed, mistyped, or transferred). Re-check the OPEN DECISIONS listing, then resend without that key or with the right one; nothing was sent.
exit=1

--- status log after the answer ---
working: reviewing palette
needs-decision: [key=palette-review-gate] no-mistakes review parked with 2 ask-user findings
resolved [key=palette-review-gate]: answered: fix both findings, then continue

--- OPEN DECISIONS after the answer ---
Evidence: Generated crewmate brief, rule 6 (bin/fm-brief.sh output)

41: When more than one decision or blocker can be open at once, name yours: write the key between the state and the colon, as needs-decision [key=&lt;slug&gt;]: {summary} or blocked [key=&lt;slug&gt;]: {why}.

39:6. If a decision belongs above the implementation worker (product choices, destructive actions, ask-user findings),
40-   append `needs-decision: {summary of options}` and stop. Firstmate will apply the configured authority and reply with the decision.
41-   When more than one decision or blocker can be open at once, name yours: write the key between the state and the colon, as `needs-decision [key=<slug>]: {summary}` or `blocked [key=<slug>]: {why}`.
42-   A decision or blocker you opened stays open until a `resolved` line carrying its exact key lands; a later `done:` or `working:` line never closes it, even when the answer is what started that work.
43-   Firstmate's reply normally writes that closing line at answer time; when a blocker or wait clears WITHOUT a firstmate reply, append `resolved: {how it cleared}` yourself (same `[key=<slug>]` if you opened it with one) as you resume.
44-7. Never stop, restart, or update the shared `no-mistakes` daemon - it is one instance serving

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

⚠️ **Review** - 2 infos
  • ⚠️ bin/fm-classify-lib.sh:418 - FM_OPEN_DECISIONS_FOLD_VERSION is still 2 even though this commit changes the fold semantics (_fm_decision_key / status_line_note now derive a different key and note for note-position lines). The persisted cursor at state/.<id>.open-decisions-cursor is only invalidated and refolded from offset 0 when the version differs, so every task that already has a cursor keeps its pre-fix records: 'needs-decision: [key=palette-review-gate] ...' stays folded under key 'default' with the token still embedded in the note, and fm-send.sh --resolve-key palette-review-gate keeps failing with 'no open decision or blocker with that key' until the worker appends a fresh line. That is exactly the observed defect for the in-flight escalations the intent cites. Bumping the constant to 3 makes the existing status history re-fold under the corrected parser.

🔧 Fix: bump open-decisions fold version so cursors refold
2 infos still open:

  • ℹ️ tests/fm-send-resolve-key.test.sh:170 - The assertion grep -F &#39;[key=default]&#39; &amp;&amp; fail &#34;a note-position key was folded as default&#34; can never fail: bin/fm-wake-drain.sh:56 only appends [key=$key] when the key is NOT default, so a record folded under default prints with no key token at all. The regression is caught by the positive [key=palette-review-gate] greps above it, but this line reads as a guard and provides none. To actually assert the old behavior is gone, match the listing line for the record (e.g. that the line carrying "review parked with 2 findings" contains a [key= token) rather than searching for the string [key=default].
  • ℹ️ bin/fm-classify-lib.sh:209 - status_line_note strips a well-formed leading [key=...] unconditionally, including when the key already came from the canonical pre-colon position. Verified: needs-decision [key=a]: [key=b] text yields key=a and note="text" - the [key=b] bytes are silently dropped from the note even though they were never used as a key, while the header documents that a [key=...] appearing mid-note is left as prose. Harmless in practice (no writer emits that shape) and the current behavior is arguably the friendlier one, but the documented rule and the code diverge for this one input.
✅ **Test** - passed

✅ No issues found.

  • bash tests/fm-send-resolve-key.test.sh (11 tests green, including the new test_note_position_key_folds_under_its_slug)
  • Regression proof: restored bin/fm-classify-lib.sh from base 566d737 and re-ran the same suite - the new test fails (the open listing should carry [key=palette-review-gate]), passes on 68afa0d
  • bash tests/fm-brief.test.sh, tests/fm-crew-state.test.sh, tests/fm-decision-hold-lifecycle.test.sh, tests/fm-fleet-snapshot-view.test.sh, tests/fm-pending-reply.test.sh, tests/fm-wake-drain-open-decisions.test.sh, tests/fm-wake-drain-open-decisions-cursor.test.sh - all green
  • Manual end-to-end CLI transcript with the exact reported status line, driving the real bin/fm-wake-drain.sh and bin/fm-send.sh over a stubbed tmux transport, run against the base library and the fixed one
  • bash bin/fm-brief.sh t-demo firstmate --mode no-mistakes and inspected rule 6 of the generated brief
✅ **Document** - passed

✅ No issues found.

⚠️ **Lint** - 1 warning
  • ⚠️ linter found issues (exit code 127)
✅ **Push** - passed

✅ No issues found.

A worker that appends "needs-decision: [key=slug] ..." had its record
folded under "default": the key extractor only ever read the text before
the first colon, so a token in the note position was never seen. The
OPEN DECISIONS listing still advertised the slug, and fm-send
--resolve-key <slug> then refused it, leaving the answerer to guess.

Accept the token in either position and fold both under the same key,
strip a consumed note-position token so every consumer reads the same
note text, and route both positions through one slug validator. A
malformed token before the colon still voids the line; after the colon
the same bytes are ordinary prose and keep the "default" key.

The brief scaffold was the half that drifted: its rule 6 showed a bare
"needs-decision: {summary}" and mentioned [key=<slug>] only in passing
on the resolved line, never stating where the token goes when opening
one. It now states the canonical position explicitly.

Regression coverage drives the real fm-send and fm-wake-drain
executables over note-position, canonical, multi-key, and
already-resolved records.
@digbycampbell
digbycampbell marked this pull request as draft August 12, 2026 14:05
@digbycampbell
digbycampbell marked this pull request as ready for review August 12, 2026 22:58
@digbycampbell
digbycampbell merged commit 29d056b into main Aug 12, 2026
13 checks passed
@digbycampbell
digbycampbell deleted the fm/fm-decision-key-parse branch August 23, 2026 04:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant