Skip to content

feat(stow): add operational memory capture - #197

Merged
kunchenguid merged 3 commits into
mainfrom
fm/fm-stow-memory-s5
Jul 2, 2026
Merged

kunchenguid merged 3 commits into
mainfrom
fm/fm-stow-memory-s5

Conversation

@kunchenguid

Copy link
Copy Markdown
Owner

Intent

Ship the /stow operational-memory feature for firstmate, per a locked captain design (2026-07-02): (1) establish data/learnings.md as each firstmate home's operational-learnings file - fleet-local operational facts and gotchas, dated/evidence-backed/curated like data/captain.md, created lazily and never itself tracked in git since data/ is gitignored; (2) add a compact knowledge-routing table to AGENTS.md (captain prefs -> data/captain.md, project-intrinsic -> project's own AGENTS.md via crewmate delivery, fleet-local ops facts -> data/learnings.md, generalizable knowledge -> shared AGENTS.md via PR, task-scoped notes -> backlog item notes, investigation findings -> scout reports) plus a bootstrap read-if-present step for data/learnings.md right after the existing data/captain.md read, plus a Layout section line describing data/learnings.md; (3) create a new user-invocable /stow skill at .agents/skills/stow/SKILL.md (resolves via the existing .claude/skills -> .agents/skills symlink) that sweeps the session for uncaptured durable knowledge, routes each finding via the AGENTS.md table (referencing it rather than duplicating it), files undone next steps to the backlog, and reports what was stowed and whether the session is safe to reset. A firm, explicit scope exclusion (captain's decision): /stow must never store, create, or edit anything in skills - no 'graduate to a skill' move - because repo skills cannot yet distinguish fleet-local from all-firstmate-user scope; this exclusion is written into the skill text itself. Also added a terse one-row README table entry for /stow alongside the existing /afk and /updatefirstmate rows. These are locked design decisions to implement as specified, not to redesign.

What Changed

  • Added a /stow skill that sweeps for durable operational knowledge before reset, routes findings through the documented memory destinations, files undone next steps to backlog notes, and reports reset readiness.
  • Documented data/learnings.md as the lazy, fleet-local operational learnings file and added bootstrap handling to read it when present.
  • Updated user-facing docs with the /stow command entry and memory-routing guidance, including the explicit exclusion from writing repo skills.

Risk Assessment

✅ Low: Captain, this is a documentation and skill-instruction change with consistent routing and no executable behavior changes.

Testing

The baseline full test command was already reported passing; I additionally ran the targeted bootstrap suite and a reviewer-visible end-to-end transcript that proves /stow is listed in README, discoverable via .claude/skills -> .agents/skills, user-invocable in skill metadata, routed through AGENTS.md to gitignored data/learnings.md, and explicitly barred from storing findings in skills. The working tree was clean afterward.

Evidence: stow end-to-end evidence transcript
stow operational-memory end-to-end evidence
repo: /Users/kunchen/.no-mistakes/worktrees/016d88035d58/01KWJ9K4JDYQVSGRFJC3EX51TF
head: 23704f61e8ff

1. README exposes the user-invocable /stow command
PASS: README built-in skills table includes /stow
144:| `/stow`            | Sweep the session for uncaptured durable knowledge, route each finding to its disk home per AGENTS.md, file undone next steps to the backlog, and report what is now safe to reset |

2. Harness skill path resolves through .claude/skills -> .agents/skills
.claude/skills symlink: ../.agents/skills
.agents skill: /Users/kunchen/.no-mistakes/worktrees/016d88035d58/01KWJ9K4JDYQVSGRFJC3EX51TF/.agents/skills/stow/SKILL.md
.claude skill: /Users/kunchen/.no-mistakes/worktrees/016d88035d58/01KWJ9K4JDYQVSGRFJC3EX51TF/.agents/skills/stow/SKILL.md
PASS: /stow is discoverable through both skill roots

3. Skill metadata is user-invocable and describes /stow
---
name: stow
description: Sweep the current session for uncaptured durable knowledge and file it to disk before a context reset. Use when the captain invokes /stow (e.g. "/stow", "stow what you've learned"), before a session reset or context compaction, or periodically to keep operational memory current.
user-invocable: true
---
PASS: skill frontmatter name is stow
2:name: stow
PASS: skill is user-invocable
4:user-invocable: true
PASS: skill trigger mentions /stow
3:description: Sweep the current session for uncaptured durable knowledge and file it to disk before a context reset. Use when the captain invokes /stow (e.g. "/stow", "stow what you've learned"), before a session reset or context compaction, or periodically to keep operational memory current.

4. AGENTS.md records the operational-memory storage contract
PASS: layout documents data/learnings.md as local fleet memory
85:  learnings.md       fleet-local operational facts and gotchas; LOCAL, gitignored; dated, evidence-backed, curated - rewrite and prune rather than append forever, the same contract as captain.md; created lazily, absent until this home has a learning to store
PASS: bootstrap instructions read data/learnings.md after captain.md
167:Then read `data/learnings.md` if present, to load fleet-local operational facts and gotchas this home has captured.
PASS: knowledge routing sends fleet-local facts to data/learnings.md
378:| Fleet-local operational facts and gotchas | `data/learnings.md` |
PASS: knowledge routing covers scout reports
381:| Investigation findings | scout reports at `data/<id>/report.md` |

5. /stow preserves the no-skill-storage exclusion
PASS: skill forbids storing findings in skills
48:`/stow` must **never** store, create, or edit a skill as a destination for any finding.
PASS: skill forbids graduate-to-skill routing
49:There is no "graduate this to a skill" move in this skill's routing.

6. data/learnings.md would stay fleet-local and untracked
PASS: data directory is gitignored
3:data/
git check-ignore -v data/learnings.md => .gitignore:3:data/	data/learnings.md
PASS: data/learnings.md is not tracked by git

RESULT: /stow is documented, discoverable through the harness skill path, routes operational memory to data/learnings.md, and explicitly excludes skill storage.
Evidence: stow evidence verification script
#!/usr/bin/env bash
set -euo pipefail
repo=${1:-$(pwd)}
cd "$repo"

say() { printf '%s\n' "$*"; }
require_fixed() {
  local needle=$1 file=$2 label=$3
  if grep -F -- "$needle" "$file" >/dev/null; then
    say "PASS: $label"
    grep -Fn -- "$needle" "$file" | head -n 1
  else
    say "FAIL: $label"
    say "missing: $needle"
    exit 1
  fi
}

say "stow operational-memory end-to-end evidence"
say "repo: $repo"
say "head: $(git rev-parse --short=12 HEAD)"
say ""

say "1. README exposes the user-invocable /stow command"
require_fixed '| `/stow`' README.md 'README built-in skills table includes /stow'
say ""

say "2. Harness skill path resolves through .claude/skills -> .agents/skills"
say ".claude/skills symlink: $(readlink .claude/skills)"
agent_skill=$(realpath .agents/skills/stow/SKILL.md)
claude_skill=$(realpath .claude/skills/stow/SKILL.md)
say ".agents skill: $agent_skill"
say ".claude skill: $claude_skill"
[ "$agent_skill" = "$claude_skill" ] || { say 'FAIL: symlink does not resolve to the stow skill'; exit 1; }
say 'PASS: /stow is discoverable through both skill roots'
say ""

say "3. Skill metadata is user-invocable and describes /stow"
awk 'NR==1{inside=0} /^---$/{inside=!inside; print; next} NR>1 && inside{print}' .agents/skills/stow/SKILL.md
require_fixed 'name: stow' .agents/skills/stow/SKILL.md 'skill frontmatter name is stow'
require_fixed 'user-invocable: true' .agents/skills/stow/SKILL.md 'skill is user-invocable'
require_fixed 'Use when the captain invokes /stow' .agents/skills/stow/SKILL.md 'skill trigger mentions /stow'
say ""

say "4. AGENTS.md records the operational-memory storage contract"
require_fixed 'learnings.md       fleet-local operational facts and gotchas; LOCAL, gitignored' AGENTS.md 'layout documents data/learnings.md as local fleet memory'
require_fixed 'Then read `data/learnings.md` if present' AGENTS.md 'bootstrap instructions read data/learnings.md after captain.md'
require_fixed '| Fleet-local operational facts and gotchas | `data/learnings.md` |' AGENTS.md 'knowledge routing sends fleet-local facts to data/learnings.md'
require_fixed '| Investigation findings | scout reports at `data/<id>/report.md` |' AGENTS.md 'knowledge routing covers scout reports'
say ""

say "5. /stow preserves the no-skill-storage exclusion"
require_fixed '/stow` must **never** store, create, or edit a skill as a destination for any finding.' .agents/skills/stow/SKILL.md 'skill forbids storing findings in skills'
require_fixed 'There is no "graduate this to a skill" move' .agents/skills/stow/SKILL.md 'skill forbids graduate-to-skill routing'
say ""

say "6. data/learnings.md would stay fleet-local and untracked"
require_fixed 'data/' .gitignore 'data directory is gitignored'
check_ignore=$(git check-ignore -v data/learnings.md)
say "git check-ignore -v data/learnings.md => $check_ignore"
tracked=$(git ls-files data/learnings.md)
[ -z "$tracked" ] || { say "FAIL: data/learnings.md is tracked as: $tracked"; exit 1; }
say 'PASS: data/learnings.md is not tracked by git'
say ""

say 'RESULT: /stow is documented, discoverable through the harness skill path, routes operational memory to data/learnings.md, and explicitly excludes skill storage.'

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

🔧 **Review** - 1 issue found → auto-fixed ✅
  • ⚠️ .agents/skills/stow/SKILL.md:34 - The task-note append command is incomplete: section 10 documents the required form as tasks-axi update &lt;id&gt; --append &#34;&lt;note&gt;&#34;, but /stow tells agents to run tasks-axi update --append. Because this skill is meant to persist task-scoped notes during reset prep, following the text literally can fail to write the note; include the &lt;id&gt; and note argument here and in the AGENTS.md routing table copy.

🔧 Fix: Fix stow backlog note command
✅ Re-checked - no issues remain.

✅ **Test** - passed

✅ No issues found.

  • command -v tmux >/dev/null || { echo "tmux is required for e2e tests" >&2; exit 1; }; tmux -V; rc=0; for t in tests/*.test.sh; do echo "== $t =="; bash "$t" || rc=1; done; exit "$rc"
  • Baseline already provided as passing: command -v tmux &gt;/dev/null || { echo &#34;tmux is required for e2e tests&#34; &gt;&amp;2; exit 1; }; tmux -V; rc=0; for t in tests/*.test.sh; do echo &#34;== $t ==&#34;; bash &#34;$t&#34; || rc=1; done; exit &#34;$rc&#34;
  • Inspected target diff: git diff --name-status 30a89bc1102fbe6610f27aa62c44885ac7dbfdc2..23704f61e8fffe2d5c2c1d93b9d45c86eeb00acd and git diff 30a89bc1102fbe6610f27aa62c44885ac7dbfdc2..23704f61e8fffe2d5c2c1d93b9d45c86eeb00acd -- AGENTS.md README.md .agents/skills/stow/SKILL.md
  • Ran targeted existing regression coverage: bash tests/fm-bootstrap.test.sh
  • Created and ran evidence check: /var/folders/5x/4nqprlbx0518k3ybcb1sz6gr0000gn/T/no-mistakes-evidence/01KWJ9K4JDYQVSGRFJC3EX51TF/verify_stow_e2e.sh &#34;$PWD&#34; | tee /var/folders/5x/4nqprlbx0518k3ybcb1sz6gr0000gn/T/no-mistakes-evidence/01KWJ9K4JDYQVSGRFJC3EX51TF/stow_end_to_end_transcript.txt
  • Checked cleanup state: git status --short --untracked-files=all
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

Add data/learnings.md as the fleet-local operational-learnings home,
a knowledge-routing table in AGENTS.md, and a user-invocable /stow
skill that sweeps a session for uncaptured durable knowledge and
files it to the right disk home before a reset.
@kunchenguid
kunchenguid merged commit 30ee4ef into main Jul 2, 2026
4 checks passed
@kunchenguid
kunchenguid deleted the fm/fm-stow-memory-s5 branch July 2, 2026 21:16
korallis added a commit to korallis/firstmate that referenced this pull request Jul 3, 2026
…nd stow skill (#4)

* feat(stow): add operational memory capture (kunchenguid#197)

* feat(stow): add operational-memory learnings convention and /stow skill

Add data/learnings.md as the fleet-local operational-learnings home,
a knowledge-routing table in AGENTS.md, and a user-invocable /stow
skill that sweeps a session for uncaptured durable knowledge and
files it to the right disk home before a reset.

* no-mistakes(review): Fix stow backlog note command

* no-mistakes(document): Document stow memory routing

* fix(tests): protect herdr smoke cleanup from default sessions (kunchenguid#199)

* fix(tests): stop real-herdr smoke tests from ever killing the default session

Both fm-backend-herdr-smoke.test.sh and fm-backend-autodetect-smoke.test.sh
tore down their isolated throwaway HERDR_SESSION via a bare/inline-prefixed
`herdr server stop`, which is unscoped and resolves ambiently. On this herdr
client, that ambient resolution silently falls back to whatever server is
already running instead of the requested session - it killed the captain's
live default herdr server twice in production (2026-07-02), once from each
smoke test's cleanup trap.

Add tests/herdr-test-safety.sh with herdr_safe_stop_and_delete: it uses the
explicit-by-name `herdr session stop/delete <name>` form (never the ambient
`server stop`) and, before that, a read-only hard guard
(herdr_refuse_if_default) that re-queries `herdr session list --json` and
refuses outright if the target is literally "default", not found, or flagged
default:true. Fails closed on any ambiguity. Verified empirically against a
real isolated session: refuses on default/nonexistent/empty names without
ever calling stop, and correctly tears down a genuine isolated session while
leaving the default session's workspace state byte-identical before and
after.

* no-mistakes(review): guard herdr delete with fresh check

* no-mistakes(document): Document herdr smoke cleanup safety

* feat(bin): route herdr secondmates into per-home workspaces (kunchenguid#200)

* feat(bin): give each secondmate its own labeled herdr workspace

Give each secondmate its own labeled herdr workspace, and land crewmates
spawned from a secondmate home in that secondmate's own space, instead of
every firstmate home (primary and all secondmates) sharing one "firstmate"
workspace.

bin/backends/herdr.sh: replace the constant FM_BACKEND_HERDR_WORKSPACE_LABEL
with fm_backend_herdr_workspace_label(), resolved fresh from FM_HOME on every
call. The primary (no .fm-secondmate-home marker) still resolves to
"firstmate" - byte-identical to every pre-existing task's recorded label, no
forced migration. A secondmate home resolves to "firstmate-<secondmate-id>".
Every workspace-scoped path (find/ensure, tab create + duplicate check,
list-live recovery, pane-for-tab) uses this same resolution, so recovery and
duplicate checks stay scoped to each home's own space. Workspace and tab
create now pass --no-focus unconditionally (verified: neither focuses by
default once a workspace exists; --no-focus is defense in depth against the
one bootstrap edge case where the very first workspace in a session auto-
focuses).

Also fixes a session-targeting bug found while verifying this empirically:
HERDR_SESSION (env var, exported or inline-prefixed) is not reliably honored
by herdr 0.7.1 CLI subcommands once another herdr server is already running -
it silently falls back to whatever server IS running. fm_backend_herdr_cli
wraps every herdr invocation with both HERDR_SESSION and a trailing
--session <name> flag (verified to route correctly in every case tried),
fixing this for the whole adapter, not just the new label-scoped calls.

bin/fm-spawn.sh: a --secondmate spawn is launched BY the primary's own
process, whose FM_HOME still names the primary at that point. The herdr case
arm now shadows FM_HOME to the secondmate's own home (PROJ_ABS) for just the
two calls that resolve/create the workspace and tab, restored automatically
afterward (bash's temporary-assignment-before-a-command form works for shell
functions too). A crewmate/scout spawned FROM a secondmate's own fm-spawn.sh
process needs no such glue - its own FM_HOME already names it.

Tests: extended tests/fm-backend-herdr.test.sh (per-home label resolution,
--no-focus, --session flag, workspace-find/list-live scoping) and
tests/fm-backend-herdr-smoke.test.sh (a secondmate-shaped home's workspace
label, list-live scoping, restart stability in the multi-workspace shape).
Added tests/fm-backend-herdr-workspace-per-home-e2e.test.sh: the mandatory
isolated E2E, driving real bin/fm-spawn.sh/fm-teardown.sh - a primary-shaped
home into "firstmate", a --secondmate spawn into its own labeled space, a
crewmate spawned FROM that secondmate-shaped home landing in the same space
(this exact path had never run before), teardown closing only the right tab,
and list-live recovery seeing only each home's own tabs. All ten assertions
passed on the real binary; the default herdr session's own workspace state
was confirmed byte-identical before and after every real-herdr test run in
this change.

docs/herdr-backend.md: rewrote "Task container shape" for the workspace-per-
home design (label derivation, the --secondmate FM_HOME-shadow wrinkle, focus
behavior, label-collision/adopt-don't-duplicate semantics, no-forced-
migration), added "Session targeting: the --session flag, not HERDR_SESSION
alone", extended "ID stability" to the multi-workspace shape, and documented
the new E2E test.

* no-mistakes(review): Clarify herdr focus docs

* no-mistakes(review): Captain, clarify herdr server session docs

* no-mistakes(document): Document herdr per-home spaces

* fix(backends): rename Herdr secondmate workspace labels (kunchenguid#203)

* Rename herdr secondmate workspace prefix to 2ndmate-

The primary home keeps the firstmate label; secondmate homes now
resolve to 2ndmate-<id> so the herdr spaces sidebar is unambiguous.
Tests and docs updated; pre-rename workspaces can be aligned with
herdr workspace rename.

* no-mistakes(review): Clarify herdr workspace migration behavior

* no-mistakes(document): Herdr docs label alignment

* feat(bin): add unified session start digest (kunchenguid#201)

* feat(bin): collapse session start into one command

Add bin/fm-session-start.sh, composing fm-lock.sh, fm-bootstrap.sh, and
fm-wake-drain.sh into one ordered digest (lock, bootstrap diagnostics,
wake queue, context files, fleet state) instead of six-plus separate
turns. Lock now runs before bootstrap's mutating sweeps, closing a race
where a second concurrent session could mutate shared state before
discovering the lock was held. A lock refusal prints a loud read-only
banner, skips every mutating step via a new opt-in
FM_BOOTSTRAP_DETECT_ONLY flag on fm-bootstrap.sh, and still completes
the read-only-safe digest.

Add fm_backend_target_exists to fm-backend.sh as a shared, read-only,
never-side-effecting per-task endpoint-liveness primitive for both the
tmux and herdr backends.

Rewrite AGENTS.md sections 3 and 5 around the single command and add
tests/fm-session-start.test.sh.

* no-mistakes(review): Harden session-start read-only guidance

* no-mistakes(review): Suppress read-only tangle repair guidance

* no-mistakes(review): Include orphan status logs

* no-mistakes(review): Captain: make session-start test executable

* no-mistakes(review): Captain: clarify status tail guidance

* no-mistakes(document): Sync session-start docs

* no-mistakes(lint): Shell lint clean

* fix(tests): avoid shellcheck boolean chain

* fix(bin): corroborate herdr idle crew state (kunchenguid#207)

* fix(bin): corroborate herdr idle agent_status with the pane's own text

crew_pane_is_busy trusted a bare `idle` verdict from herdr's agent.get
outright, skipping the tail-regex corroboration unknown already gets.
agent.get reports generation state only (working while the model streams
a turn), so it reads idle for a crew blocked on its own long foreground
no-mistakes run - even though the pane still shows the busy banner the
whole time. Combined with the no-mistakes CLI's 10-run attribution cap,
this made a genuinely working herdr crew read as not provably working,
triggering an immediate stale wake instead of absorb-then-escalate.

* no-mistakes(document): Align herdr busy-state docs

* fix(backends): reuse the herdr firstmate workspace instead of leaking one per spawn (kunchenguid#202)

* fix(backends): reuse the herdr firstmate workspace instead of leaking one per spawn

Every herdr-backed crewmate left an orphaned `firstmate`-labelled workspace
behind, one per task, because `fm_backend_herdr_workspace_find` never matched
the existing workspace: its jq filter used `--arg label ... $label`, and
`label` is a reserved keyword in jq (label/break), so the filter was a compile
error. The error was swallowed by `2>/dev/null`, the find returned empty on
every call, and `workspace_ensure` took the create path each spawn, minting a
fresh workspace. The same collision silently disabled the create-task
duplicate-label check and the bare-selector tab lookup.

Rename the jq variable to `$want` in all three affected filters so reuse,
duplicate detection, and bare-selector lookup work. With reuse restored the
single `firstmate` workspace is persistent (like tmux's session) and teardown
correctly leaves it in place, closing only the task's pane/tab.

Also prune the default tab (label "1") herdr auto-creates inside a freshly
created workspace, best-effort, so the workspace holds only real task tabs.

Add stateful-fake-CLI tests that replay repeated spawn/teardown cycles and
assert one reused workspace, zero orphans, the default tab pruned, and
`workspace create` invoked exactly once. Verified against the real herdr binary
too: the pre-fix code failed the smoke idempotency check (minted wE then wF in
an isolated session); the fix passes it.

Document the workspace lifecycle, the jq-keyword pitfall, the default-tab
prune, and the project-labelled-workspace anomaly (not adapter-created) in
docs/herdr-backend.md.

* docs(herdr): trim workspace-lifecycle addition to current-state facts

The docs/herdr-backend.md convention documents current behavior, not
history - narrative belongs in the PR/commit message. Tightened the
workspace-leak and default-tab-prune write-up down to the operative
facts (the jq reserved-keyword guard, when pruning is safe, and the
persistence caveat), and corrected the CLI-facts table rows to match
the corrected prune timing.

* fix(backends): defer herdr default-tab prune until a real task tab exists

Closing a workspace's LAST tab deletes the whole workspace on real
herdr (verified). Pruning the auto-created default tab right after
workspace create closed the workspace's only tab at that point,
destroying the just-created workspace on every single spawn instead of
reusing it - the fake-CLI unit tests didn't model this real-herdr
behavior, so they passed while the real-herdr smoke test failed with
"container_ensure is not idempotent".

Move the prune into fm_backend_herdr_create_task, right after the
first real task tab is added to a freshly created workspace, when
closing the default tab alongside it is safe. Update the fake-CLI unit
test to match the corrected timing.

Also fix a smoke-test-only bug this surfaced: the test's second
create_task call reused a $CONTAINER captured before the first task
was killed, rather than re-running container_ensure like real
fm-spawn.sh always does immediately before every create_task call - so
once the workspace (correctly) disappeared after its last tab closed,
the stale reference no longer named a live workspace.

* test(backends): guard against jq --arg names colliding with jq keywords

Regression guard for the workspace-leak bug this PR fixes: a jq
--arg/--argjson variable named after a jq reserved keyword (e.g.
label) is a compile error on jq <= 1.6, and this adapter's
2>/dev/null silently turns that into an empty result instead of a
visible failure. Greps bin/ for the pattern so a future violation
fails loudly here instead of silently misbehaving on an older jq.

* docs(herdr): fix per-home staleness and drop contributor-specific example

The workspace-lifecycle write-up hardcoded "the firstmate workspace"
as if the label were always the fixed constant, stale against the
per-home labeling documented earlier in this file (primary: firstmate,
secondmate: 2ndmate-<id>). Rephrased per-home throughout, and pointed
the "workspace this adapter did not derive" case at the existing
Label-derivation section instead of a separate anomaly writeup.

Dropped the "Anomaly: a workspace labelled with a project name"
section - the python-teslemetry-stream example was a contributor's own
environment, not current adapter fact, and it repeated a now-incorrect
FM_BACKEND_HERDR_WORKSPACE_LABEL constant claim. Replaced with one
generic sentence already covered by the corrected wording above.

Also fixed the jq-reserved-keyword guard test's file reference, which
named tests/fm-backend.test.sh when the test actually lives in
tests/fm-backend-herdr.test.sh.

---------

Co-authored-by: kunchenguid <[email protected]>

* fix(backends): make Herdr default-tab pruning safe (kunchenguid#215)

* fix(backends): make the herdr default-tab prune provably safe

The default-tab prune could close a pane holding a LIVE agent: an
ADOPTED workspace (found pre-existing by label match) was pruned using
the same tab-count/label heuristic as a freshly created one, and herdr
derives a workspace's displayed label from its cwd basename when no
explicit --label is given. A captain launching herdr directly inside a
directory named "firstmate" produces a workspace that looks identical,
by label alone, to firstmate's own container - so the very next spawn
adopted the captain's own live workspace and closed their live pane
27ms after creating its task tab (2026-07-02 incident).

The fix is structural: fm_backend_herdr_workspace_ensure now captures
the seeded default tab's id straight from its own `workspace create`
response, only when it just created the workspace. That id threads
through fm_backend_herdr_container_ensure to fm_backend_herdr_create_task,
which is the only function allowed to prune it - an adopted workspace's
caller always passes an empty seeded-tab-id, so create_task never
re-derives "prunable" from a tab's label or count. Defense in depth:
the prune also refuses a tab whose pane reports a working agent.

Covered by new unit tests (adopted-never-prunes, created-prunes-exactly,
the exact label-collision incident shape) and a new isolated real-herdr
E2E test that reproduces the incident against the pre-fix code and shows
it fixed, plus the normal happy path.

* no-mistakes(document): Sync herdr prune docs

* no-mistakes: apply CI fixes

* no-mistakes(review): fix(backends): route fm_backend_target_exists herdr check through fm_backend_herdr_cli

* no-mistakes(document): No documentation changes needed; docs already fully synced with code

---------

Co-authored-by: Kun Chen <[email protected]>
Co-authored-by: Brett Adams <[email protected]>
Co-authored-by: kunchenguid <[email protected]>
leo1oel added a commit to leo1oel/nemo that referenced this pull request Jul 3, 2026
…23)

Port upstream's stow skill adapted to this fork's knowledge routing:
captain preferences to data/captain.md, fleet-local operational
learnings to a new curated data/learnings.md, project-intrinsic facts
through a crewmate + fm-ensure-agents-md.sh and the delivery pipeline,
generalizable knowledge to shared AGENTS.md via PR, task notes and
undone next steps through tasks-axi. Skills are explicitly excluded as
a storage destination. AGENTS.md layout and docs/configuration.md
document data/learnings.md.
shuv1337 added a commit to shuv1337/shuvmate that referenced this pull request Jul 3, 2026
* feat(bin): auto-detect runtime backend (kunchenguid#188)

* feat(bin): auto-detect runtime backend from HERDR_ENV/TMUX markers

fm_backend_name now falls through to runtime auto-detection between
config/backend and the hard tmux default: a firstmate running natively
inside herdr (HERDR_ENV=1) now spawns crewmates into herdr by default,
mirroring how harness detection already works in fm-harness.sh. Nesting
resolves innermost-first (tmux wins over a nested herdr pane). Explicit
--backend/FM_BACKEND/config/backend settings always win over detection.
Selecting herdr via auto-detect prints a loud stderr notice; auto-detecting
tmux stays silent so the unconfigured default path is unchanged.

* no-mistakes(review): Captain, pin tmux tests and backend docs

* no-mistakes(document): Sync backend autodetect docs

* no-mistakes: apply CI fixes

* feat(stow): add operational memory capture (kunchenguid#197)

* feat(stow): add operational-memory learnings convention and /stow skill

Add data/learnings.md as the fleet-local operational-learnings home,
a knowledge-routing table in AGENTS.md, and a user-invocable /stow
skill that sweeps a session for uncaptured durable knowledge and
files it to the right disk home before a reset.

* no-mistakes(review): Fix stow backlog note command

* no-mistakes(document): Document stow memory routing

* fix(tests): protect herdr smoke cleanup from default sessions (kunchenguid#199)

* fix(tests): stop real-herdr smoke tests from ever killing the default session

Both fm-backend-herdr-smoke.test.sh and fm-backend-autodetect-smoke.test.sh
tore down their isolated throwaway HERDR_SESSION via a bare/inline-prefixed
`herdr server stop`, which is unscoped and resolves ambiently. On this herdr
client, that ambient resolution silently falls back to whatever server is
already running instead of the requested session - it killed the captain's
live default herdr server twice in production (2026-07-02), once from each
smoke test's cleanup trap.

Add tests/herdr-test-safety.sh with herdr_safe_stop_and_delete: it uses the
explicit-by-name `herdr session stop/delete <name>` form (never the ambient
`server stop`) and, before that, a read-only hard guard
(herdr_refuse_if_default) that re-queries `herdr session list --json` and
refuses outright if the target is literally "default", not found, or flagged
default:true. Fails closed on any ambiguity. Verified empirically against a
real isolated session: refuses on default/nonexistent/empty names without
ever calling stop, and correctly tears down a genuine isolated session while
leaving the default session's workspace state byte-identical before and
after.

* no-mistakes(review): guard herdr delete with fresh check

* no-mistakes(document): Document herdr smoke cleanup safety

* feat(bin): route herdr secondmates into per-home workspaces (kunchenguid#200)

* feat(bin): give each secondmate its own labeled herdr workspace

Give each secondmate its own labeled herdr workspace, and land crewmates
spawned from a secondmate home in that secondmate's own space, instead of
every firstmate home (primary and all secondmates) sharing one "firstmate"
workspace.

bin/backends/herdr.sh: replace the constant FM_BACKEND_HERDR_WORKSPACE_LABEL
with fm_backend_herdr_workspace_label(), resolved fresh from FM_HOME on every
call. The primary (no .fm-secondmate-home marker) still resolves to
"firstmate" - byte-identical to every pre-existing task's recorded label, no
forced migration. A secondmate home resolves to "firstmate-<secondmate-id>".
Every workspace-scoped path (find/ensure, tab create + duplicate check,
list-live recovery, pane-for-tab) uses this same resolution, so recovery and
duplicate checks stay scoped to each home's own space. Workspace and tab
create now pass --no-focus unconditionally (verified: neither focuses by
default once a workspace exists; --no-focus is defense in depth against the
one bootstrap edge case where the very first workspace in a session auto-
focuses).

Also fixes a session-targeting bug found while verifying this empirically:
HERDR_SESSION (env var, exported or inline-prefixed) is not reliably honored
by herdr 0.7.1 CLI subcommands once another herdr server is already running -
it silently falls back to whatever server IS running. fm_backend_herdr_cli
wraps every herdr invocation with both HERDR_SESSION and a trailing
--session <name> flag (verified to route correctly in every case tried),
fixing this for the whole adapter, not just the new label-scoped calls.

bin/fm-spawn.sh: a --secondmate spawn is launched BY the primary's own
process, whose FM_HOME still names the primary at that point. The herdr case
arm now shadows FM_HOME to the secondmate's own home (PROJ_ABS) for just the
two calls that resolve/create the workspace and tab, restored automatically
afterward (bash's temporary-assignment-before-a-command form works for shell
functions too). A crewmate/scout spawned FROM a secondmate's own fm-spawn.sh
process needs no such glue - its own FM_HOME already names it.

Tests: extended tests/fm-backend-herdr.test.sh (per-home label resolution,
--no-focus, --session flag, workspace-find/list-live scoping) and
tests/fm-backend-herdr-smoke.test.sh (a secondmate-shaped home's workspace
label, list-live scoping, restart stability in the multi-workspace shape).
Added tests/fm-backend-herdr-workspace-per-home-e2e.test.sh: the mandatory
isolated E2E, driving real bin/fm-spawn.sh/fm-teardown.sh - a primary-shaped
home into "firstmate", a --secondmate spawn into its own labeled space, a
crewmate spawned FROM that secondmate-shaped home landing in the same space
(this exact path had never run before), teardown closing only the right tab,
and list-live recovery seeing only each home's own tabs. All ten assertions
passed on the real binary; the default herdr session's own workspace state
was confirmed byte-identical before and after every real-herdr test run in
this change.

docs/herdr-backend.md: rewrote "Task container shape" for the workspace-per-
home design (label derivation, the --secondmate FM_HOME-shadow wrinkle, focus
behavior, label-collision/adopt-don't-duplicate semantics, no-forced-
migration), added "Session targeting: the --session flag, not HERDR_SESSION
alone", extended "ID stability" to the multi-workspace shape, and documented
the new E2E test.

* no-mistakes(review): Clarify herdr focus docs

* no-mistakes(review): Captain, clarify herdr server session docs

* no-mistakes(document): Document herdr per-home spaces

* fix(backends): rename Herdr secondmate workspace labels (kunchenguid#203)

* Rename herdr secondmate workspace prefix to 2ndmate-

The primary home keeps the firstmate label; secondmate homes now
resolve to 2ndmate-<id> so the herdr spaces sidebar is unambiguous.
Tests and docs updated; pre-rename workspaces can be aligned with
herdr workspace rename.

* no-mistakes(review): Clarify herdr workspace migration behavior

* no-mistakes(document): Herdr docs label alignment

* feat(bin): add unified session start digest (kunchenguid#201)

* feat(bin): collapse session start into one command

Add bin/fm-session-start.sh, composing fm-lock.sh, fm-bootstrap.sh, and
fm-wake-drain.sh into one ordered digest (lock, bootstrap diagnostics,
wake queue, context files, fleet state) instead of six-plus separate
turns. Lock now runs before bootstrap's mutating sweeps, closing a race
where a second concurrent session could mutate shared state before
discovering the lock was held. A lock refusal prints a loud read-only
banner, skips every mutating step via a new opt-in
FM_BOOTSTRAP_DETECT_ONLY flag on fm-bootstrap.sh, and still completes
the read-only-safe digest.

Add fm_backend_target_exists to fm-backend.sh as a shared, read-only,
never-side-effecting per-task endpoint-liveness primitive for both the
tmux and herdr backends.

Rewrite AGENTS.md sections 3 and 5 around the single command and add
tests/fm-session-start.test.sh.

* no-mistakes(review): Harden session-start read-only guidance

* no-mistakes(review): Suppress read-only tangle repair guidance

* no-mistakes(review): Include orphan status logs

* no-mistakes(review): Captain: make session-start test executable

* no-mistakes(review): Captain: clarify status tail guidance

* no-mistakes(document): Sync session-start docs

* no-mistakes(lint): Shell lint clean

* fix(tests): avoid shellcheck boolean chain

* fix(bin): corroborate herdr idle crew state (kunchenguid#207)

* fix(bin): corroborate herdr idle agent_status with the pane's own text

crew_pane_is_busy trusted a bare `idle` verdict from herdr's agent.get
outright, skipping the tail-regex corroboration unknown already gets.
agent.get reports generation state only (working while the model streams
a turn), so it reads idle for a crew blocked on its own long foreground
no-mistakes run - even though the pane still shows the busy banner the
whole time. Combined with the no-mistakes CLI's 10-run attribution cap,
this made a genuinely working herdr crew read as not provably working,
triggering an immediate stale wake instead of absorb-then-escalate.

* no-mistakes(document): Align herdr busy-state docs

* fix(backends): reuse the herdr firstmate workspace instead of leaking one per spawn (kunchenguid#202)

* fix(backends): reuse the herdr firstmate workspace instead of leaking one per spawn

Every herdr-backed crewmate left an orphaned `firstmate`-labelled workspace
behind, one per task, because `fm_backend_herdr_workspace_find` never matched
the existing workspace: its jq filter used `--arg label ... $label`, and
`label` is a reserved keyword in jq (label/break), so the filter was a compile
error. The error was swallowed by `2>/dev/null`, the find returned empty on
every call, and `workspace_ensure` took the create path each spawn, minting a
fresh workspace. The same collision silently disabled the create-task
duplicate-label check and the bare-selector tab lookup.

Rename the jq variable to `$want` in all three affected filters so reuse,
duplicate detection, and bare-selector lookup work. With reuse restored the
single `firstmate` workspace is persistent (like tmux's session) and teardown
correctly leaves it in place, closing only the task's pane/tab.

Also prune the default tab (label "1") herdr auto-creates inside a freshly
created workspace, best-effort, so the workspace holds only real task tabs.

Add stateful-fake-CLI tests that replay repeated spawn/teardown cycles and
assert one reused workspace, zero orphans, the default tab pruned, and
`workspace create` invoked exactly once. Verified against the real herdr binary
too: the pre-fix code failed the smoke idempotency check (minted wE then wF in
an isolated session); the fix passes it.

Document the workspace lifecycle, the jq-keyword pitfall, the default-tab
prune, and the project-labelled-workspace anomaly (not adapter-created) in
docs/herdr-backend.md.

* docs(herdr): trim workspace-lifecycle addition to current-state facts

The docs/herdr-backend.md convention documents current behavior, not
history - narrative belongs in the PR/commit message. Tightened the
workspace-leak and default-tab-prune write-up down to the operative
facts (the jq reserved-keyword guard, when pruning is safe, and the
persistence caveat), and corrected the CLI-facts table rows to match
the corrected prune timing.

* fix(backends): defer herdr default-tab prune until a real task tab exists

Closing a workspace's LAST tab deletes the whole workspace on real
herdr (verified). Pruning the auto-created default tab right after
workspace create closed the workspace's only tab at that point,
destroying the just-created workspace on every single spawn instead of
reusing it - the fake-CLI unit tests didn't model this real-herdr
behavior, so they passed while the real-herdr smoke test failed with
"container_ensure is not idempotent".

Move the prune into fm_backend_herdr_create_task, right after the
first real task tab is added to a freshly created workspace, when
closing the default tab alongside it is safe. Update the fake-CLI unit
test to match the corrected timing.

Also fix a smoke-test-only bug this surfaced: the test's second
create_task call reused a $CONTAINER captured before the first task
was killed, rather than re-running container_ensure like real
fm-spawn.sh always does immediately before every create_task call - so
once the workspace (correctly) disappeared after its last tab closed,
the stale reference no longer named a live workspace.

* test(backends): guard against jq --arg names colliding with jq keywords

Regression guard for the workspace-leak bug this PR fixes: a jq
--arg/--argjson variable named after a jq reserved keyword (e.g.
label) is a compile error on jq <= 1.6, and this adapter's
2>/dev/null silently turns that into an empty result instead of a
visible failure. Greps bin/ for the pattern so a future violation
fails loudly here instead of silently misbehaving on an older jq.

* docs(herdr): fix per-home staleness and drop contributor-specific example

The workspace-lifecycle write-up hardcoded "the firstmate workspace"
as if the label were always the fixed constant, stale against the
per-home labeling documented earlier in this file (primary: firstmate,
secondmate: 2ndmate-<id>). Rephrased per-home throughout, and pointed
the "workspace this adapter did not derive" case at the existing
Label-derivation section instead of a separate anomaly writeup.

Dropped the "Anomaly: a workspace labelled with a project name"
section - the python-teslemetry-stream example was a contributor's own
environment, not current adapter fact, and it repeated a now-incorrect
FM_BACKEND_HERDR_WORKSPACE_LABEL constant claim. Replaced with one
generic sentence already covered by the corrected wording above.

Also fixed the jq-reserved-keyword guard test's file reference, which
named tests/fm-backend.test.sh when the test actually lives in
tests/fm-backend-herdr.test.sh.

---------

Co-authored-by: kunchenguid <[email protected]>

* no-mistakes(review): Captain: fix backend config handling

* no-mistakes(review): Captain: update contributor test checklist

* no-mistakes(review): Fix zellij all-tab liveness

* no-mistakes(document): Sync backend documentation

---------

Co-authored-by: Kun Chen <[email protected]>
Co-authored-by: Brett Adams <[email protected]>
Co-authored-by: kunchenguid <[email protected]>
mielyemitchell added a commit to mielyemitchell/firstmate that referenced this pull request Jul 6, 2026