Skip to content

fix(logbook): resolve a card by re-upserting its full item - #1

Merged
SchokoShake merged 3 commits into
mainfrom
fm/logbook-resolve-fullitem-f3
Jul 9, 2026
Merged

SchokoShake merged 3 commits into
mainfrom
fm/logbook-resolve-fullitem-f3

Conversation

@SchokoShake

Copy link
Copy Markdown
Owner

Intent

Fix a real defect in firstmate's logbook client: bin/fm-logbook-resolve.sh could not clear a card against the live board. It cleared a card by POSTing a bare {id,status} to POST /api/items, but the logbook server runs full validateItem on every upsert (kind, title, project required), so the partial body is rejected with HTTP 400 ("invalid kind"). Every resolve failed - breaking firstmate's own board updates and, more seriously, the logbook-respond inbound answer-loop's final "resolve the card" step: because that loop deliberately leaves the inbox file in place until resolve succeeds, a failed resolve let the captain's board answer be re-drained and the action re-run on the next poll (e.g. a merge attempted more than once).

The fix is client-side ONLY: the tool under projects/logbook is a separate, client-agnostic repo and was deliberately NOT modified. fm-logbook-resolve.sh now fetches the card's current fields via a read-only GET /api/board by id, then re-POSTs the WHOLE item (project, kind, title, body, options, priority, source) with status set to the requested terminal value (resolved by default, or dismissed).

Deliberate decisions/tradeoffs a reviewer would not see from the diff alone:
(1) An id not present on the board is treated as a clean no-op success, not an error - this is exactly what keeps the answer-loop's crash-recovery re-drain safe (a card already gone must not error).
(2) A board-read (GET) failure exits non-zero so the answer-loop leaves the inbox file and retries, rather than posting a blind, possibly-invalid upsert.
(3) A jq parse failure on the board response is distinguished from "not found" and surfaces as an error.
(4) Added a shared logbook_get_json helper to fm-logbook-lib.sh mirroring logbook_post_json (bounded curl -m 10, bearer token via a 0600 header file never on argv, subshell-isolated EXIT trap). A GET has no side effects, so it has NO dry-run branch by design.
(5) LOGBOOK_DRY_RUN now composes the same full item from the read-only GET and records it to state/logbook-outbox, sending no write. This is a deliberate, documented change from the previous fully-offline {id,status} preview: a faithful preview inherently needs the card's real fields, which only the board has. Only the write (POST) is suppressed in dry-run; the read still happens.

Preserved exactly: the CLI contract fm-logbook-resolve.sh [resolved|dismissed] (default resolved), token security (0600 bearer header, never argv), the shared logbook_post_json/config helpers, loopback-only with no new external bind path, and no weakening of server validation.

Tests: enhanced the test's fake curl to serve GET /api/board (backward-compatible; existing tests never hit that path), rewrote the resolve dry-run test to assert a FULL valid item (kind/title/options present), and added three tests - live full-item upsert accepted by the tool, unknown-id clean no-op, and unreadable-board non-zero-with-no-blind-upsert. Verified end-to-end against a real running logbook server: resolving a submitted card and dismissing a pending card both drop it off GET /api/board; an unknown id is a no-op; and the old bare {id,status} body still 400s (regression guard). All 35 logbook and 5 bootstrap tests pass.

Because behavior changed, I also updated the two instruction-surface docs that previously claimed resolve's dry-run never touches the board: AGENTS.md section 15 and the .agents/skills/logbook-respond/SKILL.md dry-run note now state that resolve performs a read-only GET to compose its full-item body while still writing nothing. These doc edits are intentional and part of the fix, not scope creep.

What Changed

  • bin/fm-logbook-resolve.sh now clears a card by reading its current fields via a read-only GET /api/board and re-POSTing the whole item (project, kind, title, body, options, priority, source) with a terminal status, instead of posting a bare {id, status} body that the tool's full item validation rejected with HTTP 400 — a failure that previously broke board updates and let the inbound answer-loop re-drain and re-run an already-acted-on decision on every poll.
  • Added a shared logbook_get_json helper to bin/fm-logbook-lib.sh (bounded curl, bearer token via a 0600 header file, no dry-run branch since a GET has no side effects); an id absent from the board is a clean no-op, a board-read failure exits non-zero so the answer-loop retries rather than posting a blind upsert, and LOGBOOK_DRY_RUN now composes the full item from that GET and records it to state/logbook-outbox while suppressing only the write.
  • Extended tests/fm-logbook.test.sh (fake curl serves GET /api/board, rewritten dry-run assertion, plus live-upsert, unknown-id no-op, and unreadable-board cases) and updated the logbook-respond skill, AGENTS.md section 15, and the configuration/scripts/architecture docs to reflect that resolve reads the board to build its body. The Review step flagged one info-level note that the fixed jq projection couples resolve to the tool's current required-field set.

Risk Assessment

✅ Low: A well-bounded, client-only fix that mirrors an existing verified helper pattern, is covered by four new/rewritten targeted tests plus documented end-to-end verification, and carries no correctness or security regressions I could substantiate.

Testing

  • ⏭️ Test - skipped

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

⚠️ **Review** - 1 info
  • ℹ️ bin/fm-logbook-resolve.sh:70 - fm-logbook-resolve.sh reconstructs the upsert body from a fixed jq projection ({id, project, kind, title, body, options, priority, source} + status) rather than preserving the whole card (e.g. . + {status:$status}). This tightly couples resolve to the tool's current required-field set. The choice is deliberate (enumerating avoids echoing server-managed fields the tool might reject), and it is verified against today's schema, but it is a latent fragility: if the separately-versioned logbook tool ever adds a new required item field, GET /api/board will return it while this projection drops it, so the re-upsert would 400, resolve would fail, and the answer-loop would leave the inbox file and re-run the acted-on decision on every poll. No action needed now; just a coupling to keep in mind if the tool's item schema evolves.
⏭️ **Test** - skipped

Step was skipped.

✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

…tatus}

fm-logbook-resolve.sh cleared a card by POSTing a bare {id,status} to
POST /api/items, but the tool runs full validateItem on every upsert, so
that partial body 400s (missing kind). Every resolve failed - breaking
firstmate's own board updates and, worse, the logbook-respond answer-loop's
final card-clear step, which deliberately leaves the inbox file until resolve
lands, so the captain's action (e.g. a merge) could be re-run on each poll.

Fetch the card's current fields via a read-only GET /api/board and re-POST
the whole item (project, kind, title, body, options, priority, source) with
the terminal status. An id absent from the board is a clean no-op; a
board-read failure exits non-zero so the answer-loop retries rather than
posting a blind upsert. Add logbook_get_json to the shared lib (0600 bearer
header, never on argv). Dry-run composes the same full body from that GET.

Extend the resolve tests (full-item dry-run + live, unknown-id no-op,
unreadable board) and update AGENTS.md section 15 and the logbook-respond
skill. The tool stays untouched and client-agnostic.
@SchokoShake
SchokoShake merged commit e3d5788 into main Jul 9, 2026
SchokoShake added a commit that referenced this pull request Jul 13, 2026
* fix(logbook): resolve a card by re-upserting its full item, not {id,status}

fm-logbook-resolve.sh cleared a card by POSTing a bare {id,status} to
POST /api/items, but the tool runs full validateItem on every upsert, so
that partial body 400s (missing kind). Every resolve failed - breaking
firstmate's own board updates and, worse, the logbook-respond answer-loop's
final card-clear step, which deliberately leaves the inbox file until resolve
lands, so the captain's action (e.g. a merge) could be re-run on each poll.

Fetch the card's current fields via a read-only GET /api/board and re-POST
the whole item (project, kind, title, body, options, priority, source) with
the terminal status. An id absent from the board is a clean no-op; a
board-read failure exits non-zero so the answer-loop retries rather than
posting a blind upsert. Add logbook_get_json to the shared lib (0600 bearer
header, never on argv). Dry-run composes the same full body from that GET.

Extend the resolve tests (full-item dry-run + live, unknown-id no-op,
unreadable board) and update AGENTS.md section 15 and the logbook-respond
skill. The tool stays untouched and client-agnostic.

* no-mistakes(document): document logbook resolve rewrite and Phase 2 scripts

* no-mistakes(document): sync architecture.md logbook resolve flow with full-item re-upsert

---------

Co-authored-by: Kun Chen <[email protected]>
SchokoShake added a commit that referenced this pull request Jul 21, 2026
…awn reuse

Address the review on PR #7:

- treehouse_reservation replaces the boolean holds-check and captures the
  `treehouse status` exit code separately, so a treehouse outage reports
  could-not-ask instead of a false "no longer reserves" on every respawn (#1).
- Match the leased `(held by <holder>)` against this task's fm-<id>, so a
  worktree treehouse re-leased to a different task is caught, not silently
  adopted; an in-use row carries no holder, so the gate only strengthens (#3).
- Cover the lease-release trap for a turn-end hook-block failure between the
  lease and the meta write, the leak class new to this change (#2).
- Record the deliberate readiness-barrier trade at the cd site, marking the
  brief's isolation assertion load-bearing and forbidding a re-added pane probe (#4).
- Make the two treehouse human-text couplings a conscious v2.0.0 assumption;
  v2.0.0 has no --json/--porcelain and both fail loud-but-wrong (#5).

The test treehouse stub now records the real --lease-holder so status renders
the caller's fm-<id>, exercising the holder match faithfully.
SchokoShake added a commit that referenced this pull request Jul 22, 2026
…ane-cwd discovery (#7)

* fix(spawn): capture crew worktree via treehouse get --lease; guard against $FM_HOME

Every projects/* spawn recorded worktree=$FM_HOME (the primary checkout) in
state/<id>.meta instead of the crew's real ~/.treehouse/... worktree, forcing a
manual repair each time and risking teardown/review on the wrong path. The crew
itself was always fine; firstmate-self spawns were immune.

Root cause: worktree discovery sent `treehouse get` into the pane and polled
#{pane_current_path} until it differed from the project dir, taking that as the
worktree. Right after `tmux new-window -c <project>`, the pane process is still
the forked tmux-server child (the shell has not yet exec'd and chdir'd), so
#{pane_current_path} transiently reports the tmux SERVER's own cwd - $FM_HOME
(the server was started there) - before settling on the -c dir a moment later.
The poll's "any path != project dir" exit latched that transient $FM_HOME on the
first iteration of a projects/* spawn ($FM_HOME != $FM_HOME/projects/<repo>),
before `treehouse get` even ran; a firstmate-self spawn ($FM_HOME == project dir)
kept polling to the real worktree - the exact projects/*-only signature. kunchenguid#134's
stable-window-id targeting could not help: the correctly-targeted pane
transiently reports the server cwd.

Fix (bin/fm-spawn.sh):
- Capture authoritatively: lease from fm-spawn itself with `treehouse get
  --lease` (prints only the ~/.treehouse/... path to stdout, banners to stderr)
  and cd the pane into that exact path. No poll, no startup-transient race.
  worktree= and the turn-end hook use the leased path; fm-teardown's existing
  `treehouse return --force <worktree=>` releases it, same as before. Consistent
  with fm-home-seed.sh's existing lease pattern. Applies to every non-orca
  backend and to batch (which re-execs the single path per pair).
- Loud backstop in validate_spawn_worktree: assert the worktree is not the
  primary checkout ($FM_HOME/$FM_ROOT). Because firstmate is a git repo of
  itself, $FM_HOME is a valid git toplevel that cleared every prior clause
  silently; now a regression aborts loudly instead of corrupting meta.

Tests updated to the lease-based capture; new cases pin real-worktree capture
(never $FM_HOME) and the $FM_HOME abort. Docs: tmux-backend.md gains a
root-cause/verification section; architecture.md and herdr-backend.md corrected.
Root cause reproduced with tmux 3.6; fix verified end-to-end against real
treehouse (worktree= records a real .treehouse/... path, pane cd's into it).

Refs fm-spawn-wt-batch-x5.

* test(spawn): strengthen symlinked-prefix test with a tangle case guarding PROJ_ABS_REAL

Review (decision A, captain rule: fix the root cause, do not soften) found the
reframed test_spawn_symlinked_project_prefix_avoids_false_refusal no longer
exercised the PROJ_ABS_REAL canonicalization its comment claimed to guard: its
leased worktree was entirely distinct from the project, so validate_spawn_worktree's
only project-dependent clause (wt_real == proj_real) was never near-triggered and
the test would pass even if PROJ_ABS_REAL regressed to a logical PROJ_ABS.

Add a tangle case: the treehouse stub leases the PROJECT ITSELF reached back
through the symlinked prefix (physical path equal to the project), and the spawn
must be REFUSED with "did not yield an isolated worktree". That refusal holds
ONLY because validate_spawn_worktree resolves both sides with pwd -P; were
PROJ_ABS_REAL the logical PROJ_ABS the physical worktree path would compare
unequal and the tangle would be wrongly allowed. Test-only; bin/fm-spawn.sh
unchanged.

Refs fm-spawn-wt-batch-x5.

* no-mistakes(review): fix(spawn): release treehouse lease on abort; drop dead current_path ops

* no-mistakes(review): fix(spawn): reuse recorded worktree on respawn; refresh stale adapter docs

* no-mistakes(review): fix(spawn): gate respawn reuse on meta identity and lease reservation

* no-mistakes(review): extend respawn identity gate to recorded backend

* no-mistakes(review): scope respawn backend refusal to orca; derive --help range

* no-mistakes(document): sync backend docs with treehouse-lease worktree capture and respawn

* fix(spawn): distinguish treehouse-status failure and re-lease on respawn reuse

Address the review on PR #7:

- treehouse_reservation replaces the boolean holds-check and captures the
  `treehouse status` exit code separately, so a treehouse outage reports
  could-not-ask instead of a false "no longer reserves" on every respawn (#1).
- Match the leased `(held by <holder>)` against this task's fm-<id>, so a
  worktree treehouse re-leased to a different task is caught, not silently
  adopted; an in-use row carries no holder, so the gate only strengthens (#3).
- Cover the lease-release trap for a turn-end hook-block failure between the
  lease and the meta write, the leak class new to this change (#2).
- Record the deliberate readiness-barrier trade at the cd site, marking the
  brief's isolation assertion load-bearing and forbidding a re-added pane probe (#4).
- Make the two treehouse human-text couplings a conscious v2.0.0 assumption;
  v2.0.0 has no --json/--porcelain and both fail loud-but-wrong (#5).

The test treehouse stub now records the real --lease-holder so status renders
the caller's fm-<id>, exercising the holder match faithfully.

* no-mistakes(document): sync tmux worktree-capture doc with --lease-holder flag

---------

Co-authored-by: SchokoShake <[email protected]>
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.

2 participants