Skip to content

fix: prevent watcher check starvation - #13

Merged
kunchenguid merged 4 commits into
mainfrom
fix/check-starvation
Jun 13, 2026
Merged

kunchenguid merged 4 commits into
mainfrom
fix/check-starvation

Conversation

@kunchenguid

Copy link
Copy Markdown
Owner

Intent

Fix merge-poll starvation in firstmate's watcher (bin/fm-watch.sh). The poll loop evaluated conditions in order signals -> stale -> check-sweep -> heartbeat, and wake() exits the cycle on the first hit. So when a chatty sibling crewmate kept producing status/turn-end signals, every cycle exited on the signal before reaching the check sweep, and the slow per-task polls (e.g. merged-PR detection) were starved indefinitely - observed live: a merged PR went unsurfaced while a sibling task was active. Fix: move the due-check sweep to run right after the liveness beacon, before the signal scan, so due checks run before signals can preempt them. Checks are due only every CHECK_INTERVAL (300s), so most cycles skip the block and fall straight through to the signal scan as before; signals are never lost (persisted .seen-* signatures catch any that land while the watcher is between cycles). Pure reordering of existing blocks plus an explanatory comment; no new behavior.

What Changed

  • Run due watcher check scripts before signal scanning so chatty crewmate status updates cannot indefinitely preempt merge-poll checks.
  • Add FM_CHECK_TIMEOUT and bounded check execution so slow or hung check scripts cannot block later watcher signals.
  • Document the new check ordering and timeout contract in AGENTS.md and README.md.

Risk Assessment

✅ Low: The change is narrowly scoped to watcher check scheduling and bounded check execution, with no material correctness issues found in the reviewed diff.

Testing

Exercised the actual watcher entry point in a controlled merge-poll-starvation scenario, captured the CLI transcript as reviewer-visible evidence, confirmed due checks preempt chatty signals while signals persist for the next cycle, and cleaned up transient state/ files afterward.

Evidence: Watcher check-before-signal transcript

Output 1: check: .../state/merge-poll.check.sh: merged-pr-detected Output 2: signal: .../state/chatty.status

Scenario: due merge-poll check and pending chatty status signal both exist before watcher starts.
Command 1: FM_POLL=1 FM_CHECK_INTERVAL=0 FM_CHECK_TIMEOUT=5 FM_SIGNAL_GRACE=0 FM_HEARTBEAT=999999 bin/fm-watch.sh
Output 1: check: /Users/kunchen/.no-mistakes/worktrees/016d88035d58/01KV1957JM706HH26XY4EFQ1YM/state/merge-poll.check.sh: merged-pr-detected
Command 2: FM_POLL=1 FM_CHECK_INTERVAL=300 FM_CHECK_TIMEOUT=5 FM_SIGNAL_GRACE=0 FM_HEARTBEAT=999999 bin/fm-watch.sh
Output 2: signal: /Users/kunchen/.no-mistakes/worktrees/016d88035d58/01KV1957JM706HH26XY4EFQ1YM/state/chatty.status
Interpretation: Output 1 proves due checks preempt signals; Output 2 proves the pending signal was not lost.

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

🔧 **Review** - 1 issue found → auto-fixed ✅
  • ⚠️ bin/fm-watch.sh:90 - Due checks now run before the signal scan, but each check script is executed synchronously with no timeout. A hung or very slow poll, such as a network-backed PR check, can now block pending crewmate status/turn-end signals from being surfaced until the command returns; add a bounded timeout around each check invocation so fixing check starvation does not create signal starvation.

🔧 Fix: Bound watcher check execution
✅ Re-checked - no issues remain.

✅ **Test** - passed

✅ No issues found.

  • Inspected the target diff with git diff a5ffb9f4b5d0ed57d5918fa6f508617007a4bb2f...289a229011f5e500129ca023ef479e7c4fc32cc1 -- bin/fm-watch.sh.
  • Ran an end-to-end watcher scenario: created state/merge-poll.check.sh and state/chatty.status, then ran FM_POLL=1 FM_CHECK_INTERVAL=0 FM_CHECK_TIMEOUT=5 FM_SIGNAL_GRACE=0 FM_HEARTBEAT=999999 bin/fm-watch.sh and observed a check: wake before any signal wake.
  • Ran the follow-up persisted-signal check after removing the check script: FM_POLL=1 FM_CHECK_INTERVAL=300 FM_CHECK_TIMEOUT=5 FM_SIGNAL_GRACE=0 FM_HEARTBEAT=999999 bin/fm-watch.sh and observed the pending signal: wake.
  • Verified transient working-tree test state was removed with git status --short.
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

@kunchenguid
kunchenguid force-pushed the fix/check-starvation branch from 169b7df to 732a1fc Compare June 13, 2026 20:14
@kunchenguid
kunchenguid merged commit 129a963 into main Jun 13, 2026
3 checks passed
@kunchenguid
kunchenguid deleted the fix/check-starvation branch June 13, 2026 20:15
vipentti pushed a commit to vipentti/firstmate that referenced this pull request Aug 5, 2026
* fix(watch): run due check sweep before signal scan to prevent starvation

* no-mistakes(review): Bound watcher check execution

* no-mistakes(document): Document watcher check scheduling

* no-mistakes(lint): Silence shellcheck Perl warning
@prajwal-395

Copy link
Copy Markdown

NOTE: This branch disarms the reaper first and re-arms it last. The first commit removes the --close invocation from bin/fm-bootstrap.sh so nothing runs the reaper automatically while the fix is being reviewed or applied. This is necessary because the chmod -x hand mitigation was already defeated when a secondmate interpreted the Permission denied error as a broken checkout and restored the exec bit, re-arming the hazard. Taking the hazard out of the source of truth entirely ensures no home accidentally closes its own window. The final commit re-arms the bootstrap call after the fixes and tests are in place.

NewAiCoder referenced this pull request in NewAiCoder/firstmate Sep 26, 2026
…rkers (#13)

* fix(bin): also approve external CLAUDE.md imports at the canonical project root

Claude Code's external-imports check (hasClaudeMdExternalIncludesApproved)
reads only the canonical git-root project entry in ~/.claude.json, which its
own worktree-to-primary-checkout canonicalization means is never the task
worktree fm-claude-trust.sh registered. The trust dialog kept working after
PR 10 only because its check has an ancestor-walk fallback that happens to
reach the worktree entry; the external-imports check has no such fallback.

Verified by disassembling the installed claude binary and reproducing in an
isolated three-way tmux launch: identical flags registered only at the
worktree key still showed the external-imports dialog, and registering them
at the primary checkout key suppressed both dialogs.

fm-claude-trust.sh now registers all three flags on both the worktree entry
and the primary-checkout entry in one atomic write, and refuses when the
<project> argument is not itself a primary checkout (its own write target
would then be wrong). Extends the harness-adapters Claude reference and the
trust test suite.

* no-mistakes(test): fix(bin): resolve linked-worktree project arg to primary checkout in fm-claude-trust

* no-mistakes(document): docs: describe fm-claude-trust.sh's added external-imports pre-approval

---------

Co-authored-by: NewAiCoder <170579485+NewAiCoder@users.noreply.github.com>
@zakna

zakna commented Sep 27, 2026

Copy link
Copy Markdown

Validation evidence for this sync:

  • The pipeline test step timed out at its 30-minute agent budget (a budget cut, not a code failure) and was approved on that basis.
  • Instead, these ran locally on the merged result and passed: bin/fm-lint.sh (ShellCheck 0.11.0, actionlint 1.7.12), and the 10 test files the 6 upstream commits touch: tests/fm-backend-herdr.test.sh, tests/fm-contributions.test.sh, tests/fm-devin-harness.test.sh, tests/fm-gate-refuse.test.sh, tests/fm-herdr-submit-confirm-live-e2e.test.sh, tests/fm-jev-mem-guard.test.sh, tests/fm-session-start.test.sh, tests/fm-spawn-dispatch-profile.test.sh, tests/fm-timeout-lib.test.sh, tests/fm-watch-triage.test.sh.
  • The merge of upstream/main had no conflicts, so there are no contradicting spots to name, and no extra fixes were needed.
  • origin/main (PR fix: wake pi crewmates after each turn #12, one test file) was merged in afterwards, also without conflicts.

Thoughts-One added a commit to Thoughts-One/firstmate-contrib that referenced this pull request Sep 29, 2026
…nguid#13)

bin/fm-herdr-session-retire.sh guardedly stops one explicitly named,
pre-existing, empty Herdr session that Firstmate did not provision. It
is stop-only: no delete, no server call, no restart. It rejects
default, fm-remote, and every fm-lab-* name, requires exactly one
matching, non-default, running target row with an empty workspace
list, snapshots every other session, re-verifies everything
immediately before the single herdr session stop call, and confirms
both the target and every other session's postconditions before
reporting success.

fm-herdr-lab.sh remains the sole owner of generated fm-lab-* sessions
and is unchanged; the new helper owns the disjoint case of a session
Firstmate did not create.

fm-brief.sh gains --herdr-retire-session <name>, mutually exclusive
with --herdr-lab, restricted to ship/scout briefs, and the unguarded
NOT ENABLED declaration now routes existing-session retirement to it.

Adds tests/fm-herdr-session-retire.test.sh (stateful fake Herdr
client, no live session touched) and extends tests/fm-brief.test.sh
for the new routing contract. Registers the new test's family in
fm-test-run.sh so it lands in the derived portable-serial lane.
Documents the new helper's safety contract in docs/herdr-backend.md
and adds it to docs/scripts.md.
This was referenced Oct 1, 2026
jroethel referenced this pull request in jroethel/firstmate Oct 9, 2026
A projected spawn holds the shared Herdr session lock from its
presentation mutation until its launch is sent, across worktree entry
and launch staging, so a hold routinely lasts several seconds. A
recovering spawn waited only 5 s for that lock and then refused, so two
homes resuming together after a restart could spuriously refuse a
legitimate queued resume. Recovery now waits behind a peer's whole
bounded hold; the fresh-create path keeps its short wait because it
degrades flat instead. The concurrent-recovery case now holds the lock
past the old wait so it proves the queueing deterministically.

The focus-flash Part C non-vacuity check needed its concurrent sampler
to land inside the short wrong-focus window between the explicit close
and the corrective tab focus. The test's CLI wrapper now holds the
operation after the close until the sampler records that window.

Also make the presentation test's cleanup remove the read-only
git-hooks strip directories it used to leave behind in /tmp.

Evidence, real Herdr 0.7.4 in an isolated sandbox:
- Presentation e2e, old code: 3/3 failed with "could not acquire its
  session lock; refusing a concurrent resume"; the winning recovery
  held the lock 12-16 s while the loser gave up after its 5 s wait.
- Presentation e2e, new test with old spawn code: 2/2 failed at the
  concurrent-recovery case; fixed code: 5/5 passed.
- Focus-flash with a sampler slowed past the window: old test 7/10
  failed with "observed no wrong-focus sample" although every run's
  production backstop issued the corrective tab focus; fixed test
  10/10 passed, and a deliberately blind sampler still fails 2/2.
- Focus-flash fixed test: 50/50 on 2 CPUs and 40/40 on 1 loaded CPU.
- Whole real-Herdr family as the CI step runs it, fixed code: all
  scripts passed except fm-afk-launch, which failed only because the
  sandbox PATH lacked node and passes with node present.

Test evidence: the no-mistakes test step is skipped for this change
because its agent would run these real-Herdr e2e scripts against the
host's live Herdr server; the sandbox before/after runs above (full
record in the task's evidence.md) and this PR's "Behavior tests
(Herdr)" CI check are the test evidence.

Co-authored-by: Jeremy Roethel <jjrdar@rit.edu>
@PemZhipengXie

Copy link
Copy Markdown

真站验证说明(需船长真站核一次)

  • 评估字段位置尚未实见:素材评估读的是千川视频库同一个 video-list 接口返回的那条素材,按原客户端的 is_carry_material / is_ecp_high_quality_material / is_first_publish_material 三个字段查找。真实站点把它们放在哪一层、是否在这个接口里返回,工人没有见过,需要船长按 docs/connected-real.md §V1b 在真站核对一次。
  • 读不到时显示「未出」,不报错:未登录、视频库未列出、读取超时、平台还没给评估,都显示「未出」并在小字写原因;没有回答搬运字段时也不会被当成「未判搬运」。应用不自动处理、不自动删除千川上的任何素材。
  • 证据记录 qianchuan.eval 带 fieldHints(评估相关字段名,不含取值和页面内容),可用来对应修正。
  • 对 docs/connected-real.md V1「尚未对真站验证」的补充:①②③ 会在 V1b 的真实上传 + 挂标签里同时得到验证;④(评估字段位置)、⑤(批量串行与限流)为本票新增核对项。
  • 验证:npm test 全绿(129);Electron 端到端(合成数据、本地桩网关、自己的 Electron 实例与调试端口 9477)在推送头 6e97e18 上通过:成功 / 被拒 / 断连 / 评估各状态 / 手动刷新 / 勾选搬运再去重 / 重试失败行。未登录、未连接任何真实平台或账号。
  • Mac DMG:~/Downloads/素材工厂-千川批量回读-6e97e18.dmg,sha256 f9a1e8a179f5afc605738936c7a225e6e169e44b3f13daa52d4e68c2608db908。

Self-decided findings

  • R5 — Fix:自由文本评估回退会把「未判搬运」按子串误判为搬运,已移除,只读明确的评估字段。
  • R6 — approve(已知取舍):保留上一次已得到的评估,避免一次读取失败(超时、网页未登录)把已出的「优质 / 搬运」冲成「未出」;界面小字注明沿用旧结果。
  • R7 — approve(已知取舍):保留「停止上传」;批量往真实账号传时需要能中途停下,正在传的一条传完后停止,不影响已传的行。
  • R9 — approve(已知取舍):评估解析保留对字段名写法与嵌套层级的宽松匹配,因为真站字段位置尚未实见;已在文档中写明,真站核对后可收紧。

fritznoff pushed a commit to fritznoff/starship that referenced this pull request Nov 2, 2026
…e session (kunchenguid#13)

* feat(bin): close leak guard gaps for keys, draft text and every Claude session

The leak guard now finds API keys under any name and in any case, and the
provider key prefixes it shares with the command guard from one module,
bin/fm_secret_shapes.py, which adds Stripe live and GitLab keys to both.
Draft text checks (--text) look for those key and token shapes too, with or
without a word list.

A private word or token is reported only by where it is (file, line and
column) with a count, never the word or value, so a command that names the
word list file no longer prints the list.

The PR text hook now also runs for a first mate or second mate Claude session
through the tracked .claude/settings.json, standing aside in spawned workers,
which keep their own copy.

* no-mistakes(review): Check token shapes only in text a command sends

* no-mistakes(review): Place listed words in commits, check tokens in piped files

* no-mistakes(review): Hide listed phrase words, count $(<file), skip quoted cat mentions

* no-mistakes(review): Hide overlapping listed phrases, blank heredocs inside substitutions

* no-mistakes(document): Count the PR text hook in Cursor stand-down set
@JackyXiao98

Copy link
Copy Markdown

Decisions and measurements

  • No new program-level wake filter (review finding R3F1, decided: accept). PR docs: require full PR URLs in captain reports #11 (merged 2026-11-11 07:05Z) already filters the main no-change shapes in the watcher and the Stop hook. Measured from the two newest main-session logs after 2026-11-11 07:30Z: 12 wake turns, 3 of them no-change, and each of the 3 is a different safety-relevant shape (a check: rearm-resurface recovery wake, a GPU-guard custom check, a rejected-unauthenticated-checks warning). Before docs: require full PR URLs in captain reports #11, the older session had 534 no-change wake turns (signal 291, stale 212, check 31). Adding more filtering would extend into wake-classification logic with small gain and a real risk of missed events, so this change merges repeated wakes only in the printed digest (measured: 809 rows in 582 distinct per-turn events in the older session) and leaves the watcher, queue, drain and acknowledgement unchanged.
  • No default compaction cap or rotation cadence in the shared template (review finding F3, decided: keep it optional). Each home enables the earlier compaction point in its own private settings; the primary home now has "autoCompactWindow": 300000 in .claude/settings.local.json. Estimate, not measured: average input per request about 170k tokens instead of the measured 463,643 and 552,789, with about 4 times as many compactions.
  • Estimates, not measured on a live session: drain output per wake turn about 35% to 60% smaller; model replies per wake turn about 2.2 to 2.5 instead of the measured 2.62.

ghazni101 pushed a commit to ghazni101/firstmate-dsh that referenced this pull request Nov 19, 2026
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.

5 participants