Skip to content

fix(remote-job): never start a duplicate Linux worker supervisor beside a live lock owner - #6217

Open
Diabl0570 wants to merge 6 commits into
kunchenguid:mainfrom
Diabl0570:fm/fm-remote-job-dup-workers-pr
Open

Diabl0570 wants to merge 6 commits into
kunchenguid:mainfrom
Diabl0570:fm/fm-remote-job-dup-workers-pr

Conversation

@Diabl0570

@Diabl0570 Diabl0570 commented Sep 30, 2026 •

Copy link
Copy Markdown

Intent

Follow up existing PR #6217 and synchronize with latest main through no-mistakes. User asked to clone repository and use sub-agents for necessary PR follow-up and main sync. Preserve the focused fix preventing duplicate Linux remote-job supervisors, including live lock-owner checks and inode-safe readiness heartbeat refresh, while reconciling newer main implementations. Preserve previous branch commits and resolved initializing-worker race feedback; fix necessary failures. Update only existing Diabl0570 fork branch and existing PR. Do not merge, post comments, alter accounts or shared fleets, or launch real vendor workers. Use focused executable tests and accurately report Linux-only evidence unavailable on this macOS host.

What Changed

  • fm_remote_job_worker_owned_alive in bin/fm-remote-job-lib.sh now treats a verified live lock owner (one whose recorded pid, start time and command still match) as running before it reads worker.pid, regardless of heartbeat age. fm_remote_job_start_linux_worker returns early for an owner that has not yet published its code identity, so it only stops the owner once that identity has been published and does not match. Before this change, the start path could launch more supervisors beside a slow or still-initializing owner.
  • bin/fm-remote-job-worker.sh changes how the heartbeat refreshes readiness:
    • It binds to the ready file through fd 3 (/proc/self/fd/3 where that path exists) and refreshes that file, not the pathname.
    • It recreates a missing ready file through a no-clobber ln, so it never overwrites readiness that a replacement owner published in the meantime.
    • On acquiring the lock, it deletes the previous owner's worker identity file along with its ready file, before publishing its own lock ownership.
  • Adds Linux regression cases to tests/fm-remote-job.test.sh: live owners with aged readiness, absent or stale public PID records, unpublished identity over an absent or stale predecessor identity, and readiness binding where fd paths exist. The touch stub in tests/fm-remote-job-launchagent.test.sh now matches the ready file by identity (-ef), not by path. docs/remote-secondmates.md points to the new owner-reuse contract and test coverage.

🤖 Generated with Claude Code

Risk Assessment

✅ Low: The focused changes address predecessor identity cleanup and preserve ownership checks and readiness gating without substantiated regressions.

Testing

I drove the real worker and start-path scripts under the Linux platform override on this macOS host, running the duplicate-worker section of the test file on its own. All five cases passed: - an owner with an absent public PID - an owner with a stale public PID - an owner with no published code identity, over absent predecessor state - the same, over stale predecessor state (the new R1 case) - an owner with an aged heartbeat Reverting only the R1 identity clearing made the new stale-identity case fail, first on its precondition and then, with that skipped, because the start path stopped or duplicated the initializing replacement. The launch-agent test file passed. Two runs of the whole fm-remote-job test file each failed early, in different timing-sensitive cases, while the host load average was about 85. Neither failure reached code this change touches. The fd-bound heartbeat check skips without /proc, so that Linux-only behaviour is untested here.

  • Live validation: ✅ go - 5 of 6 scenarios driven live against the product
Scenario Result Live Evidence
Starting a worker while the live, verified lock owner's heartbeat has aged (owner group frozen) keeps the existing owner and launches no second supervisor ✅ pass live dup-section-target.log: 'a live lock owner with an aged heartbeat is never duplicated'
Starting a worker while the lock owner has not yet published worker.pid (absent, or stale PID left by a predecessor) does not duplicate it ✅ pass live dup-section-target.log: absent and stale public PID cases ok
Starting a worker while the owner has not yet published its code identity (no predecessor identity) neither stops nor duplicates it ✅ pass live dup-section-target.log: 'over absent predecessor state' ok
R1 adversarial: a replacement starts over a crashed predecessor's stale worker.identity and a concurrent start runs before the replacement publishes its identity; the replacement survives ✅ pass live dup-section-target.log passes the 'over stale predecessor state' case; dup-section-without-identity-clear-no-precheck.log shows the same case failing with the fix reverted
Darwin launch-agent paths still handle a stale live owner when the heartbeat may refresh through a bound fd ✅ pass live launchagent.test.log ALL TESTS PASSED
Linux heartbeat refreshes only the ready file it opened or recreated (inode-safe refresh via /proc/self/fd) ⏸️ untested no Needs a Linux host with /proc. Run tests/fm-remote-job.test.sh on Linux CI to cover it.
Evidence: Duplicate-worker section at target (all pass)

Source: Duplicate-worker section at target (all pass)

ok - a live lock owner is never duplicated before absent public PID publication
ok - a live lock owner is never duplicated before stale public PID publication
ok - a live lock owner is neither stopped nor duplicated before it publishes its code identity over absent predecessor state
ok - a live lock owner is neither stopped nor duplicated before it publishes its code identity over stale predecessor state
ok - a live lock owner with an aged heartbeat is never duplicated
ALL TESTS PASSED
bash $S/dup.sh  0.94s user 2.39s system 19% cpu 17.027 total
Evidence: Same section with R1 identity clear reverted (fails on precondition)

Source: Same section with R1 identity clear reverted (fails on precondition)

ok - a live lock owner is never duplicated before absent public PID publication
ok - a live lock owner is never duplicated before stale public PID publication
ok - a live lock owner is neither stopped nor duplicated before it publishes its code identity over absent predecessor state
not ok - the initializing fixture already published its code identity
Evidence: R1 reverted, precondition skipped: replacement stopped or duplicated

Source: R1 reverted, precondition skipped: replacement stopped or duplicated

ok - a live lock owner is never duplicated before absent public PID publication
ok - a live lock owner is never duplicated before stale public PID publication
ok - a live lock owner is neither stopped nor duplicated before it publishes its code identity over absent predecessor state
not ok - the start path stopped or duplicated the owner before worker.identity publication
Evidence: Launch-agent test file run

Source: Launch-agent test file run

ok - a stale-code crash heartbeat cannot count as replacement readiness
ok - the live worker recreates missing readiness independently of a blocked sweep
ok - a slow sequence-claim sweep keeps readiness fresh and cannot trigger a LaunchAgent bootout
ok - concurrent Darwin ensures serialize one reload and adopt its fresh worker
ok - stale readiness logs the named condition without reloading a verified launchd-tracked live owner
ok - Darwin stops an untracked stale-code owner and starts the current worker through launchd
ok - Darwin replaces a current-code owner that the loaded agent does not track
ok - a fresh spawn survives when the caller that started it dies before publication
ok - a dead repair-lock holder is reclaimed by one caller at a time
ok - Darwin reload waits for asynchronous bootout cleanup before bootstrap (2s)
ALL TESTS PASSED
Evidence: Whole-file run 1 (early timing failure under load)

Source: Whole-file run 1 (early timing failure under load)

ok - default: result and command samples use 0.25 seconds; dispatcher uses four 0.05-second waits then one second
ok - legacy: result and command samples use 0.07 seconds; dispatcher uses four 0.07-second waits then one second
ok - active: result and command samples use 0.12 seconds; dispatcher uses four 0.07-second waits then one second
ok - default queue and execution bounds independently cover long polls
ok - lock and claim owner identity stays locale-invariant (fa_IR)
ok - operator PATH excludes a symlinked local bin
ok - operator PATH honors nvm defaults with a deterministic fallback
ok - operator PATH resolves the authorized Nix profile bin link
ok - operator PATH orders discovered tool installs deterministically
ok - the worker preserves bounded argv and stdin in an empty environment
ok - active jobs keep the worker ready for concurrent requests
ok - ensure replaces a live worker after its code changes
ok - worker identity binds the canonical configured code root
ok - stale ownership is reclaimed without signaling a reused pid
ok - the worker enforces the job timeout and publishes its result
ok - the worker expires queued jobs before they can mutate
ok - queued jobs receive a fresh bounded execution window
ok - a queued short command preempts a running long poll instead of waiting its window
ok - a poll re-armed after preemption reads the same cursor with nothing lost
ok - sibling polls never preempt each other into a re-arm churn loop
ok - worker shutdown terminates the active command tree before replacement
ok - Linux supervision recovers crashes and stops orphaned commands
ok - pre-execution validation obeys the job timeout
ok - the worker drains bounded output without changing command results
ok - the worker refuses symlinked job fields before command execution
ok - failed shutdown quarantines ownership against replacement workers
ok - quarantine recovery refuses unverifiable supervisors and ignores reused pids
ok - a repeatedly signalled shutdown still releases ownership for the next worker
ok - TERM after ownership loss stops the serving worker
not ok - the active command kept running after an unowned TERM
bash tests/fm-remote-job.test.sh  11.22s user 27.63s system 18% cpu 3:30.03 total
Evidence: Whole-file run 2 (different early timing failure under load)

Source: Whole-file run 2 (different early timing failure under load)

ok - default: result and command samples use 0.25 seconds; dispatcher uses four 0.05-second waits then one second
ok - legacy: result and command samples use 0.07 seconds; dispatcher uses four 0.07-second waits then one second
ok - active: result and command samples use 0.12 seconds; dispatcher uses four 0.07-second waits then one second
ok - default queue and execution bounds independently cover long polls
ok - lock and claim owner identity stays locale-invariant (fa_IR)
ok - operator PATH excludes a symlinked local bin
ok - operator PATH honors nvm defaults with a deterministic fallback
ok - operator PATH resolves the authorized Nix profile bin link
ok - operator PATH orders discovered tool installs deterministically
ok - the worker preserves bounded argv and stdin in an empty environment
ok - active jobs keep the worker ready for concurrent requests
ok - ensure replaces a live worker after its code changes
ok - worker identity binds the canonical configured code root
ok - stale ownership is reclaimed without signaling a reused pid
ok - the worker enforces the job timeout and publishes its result
ok - the worker expires queued jobs before they can mutate
not ok - queue time consumed the second job's execution timeout
- Outcome: ⚠️ 1 info across 1 run (10m11s)

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

🔧 **Rebase** - 3 issues found → auto-fixed ✅
  • ⚠️ bin/fm-remote-job-lib.sh - merge conflict rebasing onto origin/main
  • ⚠️ bin/fm-remote-job-worker.sh - merge conflict rebasing onto origin/main
  • ⚠️ tests/fm-remote-job.test.sh - merge conflict rebasing onto origin/main

🔧 Fix applied.
✅ Re-checked - no issues remain.

🔧 **Review** - 1 issue found → auto-fixed ✅
  • 🚨 bin/fm-remote-job-lib.sh:1463 - The required "Preserve previous branch commits and resolved initializing-worker race feedback" remains incomplete. After a crash and code update, worker.identity can retain the predecessor's stale hash. A replacement publishes ownership before replacing that identity at bin/fm-remote-job-worker.sh:246. A concurrent start now accepts this initializing owner at bin/fm-remote-job-lib.sh:1155, but the added if [ ! -e "$identity" ] && [ ! -L "$identity" ]; then return 0; fi guard misses the existing stale identity, so the mismatch terminates the replacement's entire process group. The initialization case at tests/fm-remote-job.test.sh:1670 covers only an absent identity. Clear the predecessor's identity under the newly acquired lock before publishing ownership, and exercise this sequence with a stale identity present.

🔧 Fix applied.
✅ Re-checked - no issues remain.

⚠️ **Test** - 1 info
  • ℹ️ tests/fm-remote-job.test.sh - Two runs of the whole tests/fm-remote-job.test.sh failed in different early timing-sensitive cases ('the active command kept running after an unowned TERM', then 'queue time consumed the second job's execution timeout'). The host load average was about 85 on 18 cores. Neither case exercises the code this change touches, and the duplicate-worker section passes on its own. Remote CI should confirm the whole file.
  • Live validation: ✅ go - 5 of 6 scenarios driven live against the product
Scenario Result Live Evidence
Starting a worker while the live, verified lock owner's heartbeat has aged (owner group frozen) keeps the existing owner and launches no second supervisor ✅ pass live dup-section-target.log: 'a live lock owner with an aged heartbeat is never duplicated'
Starting a worker while the lock owner has not yet published worker.pid (absent, or stale PID left by a predecessor) does not duplicate it ✅ pass live dup-section-target.log: absent and stale public PID cases ok
Starting a worker while the owner has not yet published its code identity (no predecessor identity) neither stops nor duplicates it ✅ pass live dup-section-target.log: 'over absent predecessor state' ok
R1 adversarial: a replacement starts over a crashed predecessor's stale worker.identity and a concurrent start runs before the replacement publishes its identity; the replacement survives ✅ pass live dup-section-target.log passes the 'over stale predecessor state' case; dup-section-without-identity-clear-no-precheck.log shows the same case failing with the fix reverted
Darwin launch-agent paths still handle a stale live owner when the heartbeat may refresh through a bound fd ✅ pass live launchagent.test.log ALL TESTS PASSED
Linux heartbeat refreshes only the ready file it opened or recreated (inode-safe refresh via /proc/self/fd) ⏸️ untested no Needs a Linux host with /proc. Run tests/fm-remote-job.test.sh on Linux CI to cover it.
  • bash tests/fm-remote-job.test.sh at 6bb01db (twice; run 1 failed at the unowned-TERM case, run 2 at the queue-timeout case, both under load average ~85)
  • Duplicate-worker section of tests/fm-remote-job.test.sh (fixture setup plus lines 1555-1733) run standalone against the 6bb01db worktree: all 5 cases pass
  • Same section against a temp copy of 6bb01db with only the R1 identity rm reverted in bin/fm-remote-job-worker.sh: the stale-identity case fails (precondition, and with the precondition skipped: 'the start path stopped or duplicated the owner before worker.identity publication')
  • bash tests/fm-remote-job-launchagent.test.sh at 6bb01db: all pass
✅ **Document** - passed

✅ No issues found.

⚠️ **Lint** - 1 warning
  • ⚠️ linter found issues (exit code 1)

🔧 No changes applied.
1 warning still open:

  • ⚠️ linter found issues (exit code 1)

🔧 No changes applied.
1 warning still open:

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

✅ No issues found.

@greptile-apps

greptile-apps Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

RetriggerConfidence Score: 5/5

[High risk] Changes remote worker startup and heartbeat logic to prevent duplicate supervisors.

The PR appears safe to merge based on the changes since the previous review.

Reviews (8) · Last reviewed commit: "no-mistakes(document): Document Linux wo..."

@kunchenguid

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate: first stamp vs tip 549e07f37fd73aa01d74cd126b1111c99175abed. Body links #2921 / complementary to open #5851 (owner-identity half); GitHub closing refs empty (no Fixes keyword) — intent verified as restore of single live remote-job owner. Tip fm_remote_job_worker_owned_alive still requires the heartbeat probe before accepting a verified lock owner; this head accepts the lock owner first and moves readiness refresh into a sibling heartbeat process.

HEAD aaa8802e5f648b00c7797448ce511d6562a604d2. Attestation: MATCH. NM SUCCESS. Greptile SUCCESS. MERGEABLE/UNSTABLE. Fork workflows were action_required; approved runs 36818657858 (CI, now re-running attempt 3), 36818657842, 36823148158, plus older-SHA 36701888886/36700944872/36700944853. Author Diabl0570 not blocked.

contract-class: restore — start path must not launch a second supervisor beside a live verified lock owner when heartbeat ages under load; existing single-owner invariant.

VISION (per-rule)

  • One captain / Authority: aligns — no new autonomy; ownership safety only.
  • Scripts own mechanics: aligns — lock-owner match before probe; heartbeat process is exact.
  • Restart is a non-event: aligns — readiness survives slow serving passes; losing supervisor leaves owner records.
  • Fleet / Scope: aligns — remote-job worker path; Linux/shared-host durability.

Waiting on CI (behavior/lint lanes in progress on run 36818657858). Will auto-merge on green if still restore + safe. No Firstmate flag.

@kunchenguid

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate: CI run 36818657858 (attempt 3, after fork workflow approval) finished. Attestation MATCH, NM SUCCESS, all Behavior portable + Herdr green, Greptile SUCCESS. Lint 1 FAILURE — ambient tip bin/fm-teardown.sh reason=memory (~8.4 GiB RSS under the 12 GiB address-space cap). This PR does not touch teardown; same envelope issue noted on #6264/#6102 and addressed by #6271's 16 GiB raise.

contract-class: restore unchanged. Not mergeable until Lint 1 is green. Not otherwise-ready → no Firstmate flag. Please rebase onto a tip that carries the lint envelope fix (or coordinate with #6271) and re-push no-mistakes if the head moves.

@Diabl0570 Diabl0570 changed the title fix(bin): never duplicate a live remote job worker owner fix: prevent duplicate remote job supervisors Oct 3, 2026
@kunchenguid

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate: restamp vs tip 9ea0c41a480c (newer activity; prior stamp waiting-author lint1 on aaa8802e).

VISION (per-rule): One captain/interface — aligns. Authority explicit — aligns. Scripts vs agents — aligns (ownership/heartbeat mechanics in scripts). Restart non-event / durability — aligns (live verified owner not duplicated under aged heartbeat). Delegation spine — aligns (remote secondmate supervision). Fleet outlives vendor — n/a. Scope — aligns.

contract-class: restore — concrete existing remote job supervisor start path wrongly launched a second supervisor beside a live verified lock owner when heartbeat aged under slow serving. Tip accepts verified live owner before heartbeat probe; separate heartbeat refresher. Complements open #5851 (owner-identity); body refs #2921.

Attestation MATCH. Greptile 5/5. Ahead/behind diverged — CONFLICTING/DIRTY; author must rebase onto current main (prior lint1 memory note may already be moot after tip advanced). Fork CI/NM re-approved this pass. Not otherwise auto-merge-ready → no conflict resolve here. No Firstmate flag.

@Diabl0570 Diabl0570 changed the title fix: prevent duplicate remote job supervisors fix: prevent duplicate remote worker supervisors Oct 4, 2026
@greptile-apps

This comment has been minimized.

Comment thread bin/fm-remote-job-lib.sh Resolved
@Diabl0570
Diabl0570 force-pushed the fm/fm-remote-job-dup-workers-pr branch from 3645190 to b377e59 Compare October 5, 2026 05:15
@Diabl0570 Diabl0570 changed the title fix: prevent duplicate remote worker supervisors fix: prevent duplicate Linux remote job supervisors Oct 5, 2026
…ied lock owner

The Linux start path probed the readiness heartbeat before checking the
worker lock. A live lock owner whose recorded pid, start time and command
still matched was therefore read as dead whenever its heartbeat aged past
the probe's ten-second bound on a loaded host, and another restart
supervisor was launched beside it. Accept such an owner before the
heartbeat probe.

Bind heartbeat refreshes to the readiness object: where a Linux fd path
reaches it, the heartbeat refreshes only the ready file it opened or
recreated, and it recreates a missing ready file through a no-clobber
link after its ownership check. A refresh delayed past a loss of
ownership can then neither refresh nor overwrite a replacement owner's
readiness. The LaunchAgent fault injector now matches the ready file
rather than its pathname, since refreshes may arrive through that fd path.

Refs kunchenguid#2921
…ker.pid

The Linux start path now checks for a live lock owner whose recorded pid,
start time and command match before it reads the published worker.pid, so
a start between lock publication and worker.pid publication counts the
owner as running instead of launching another supervisor. Identity
publication order and identity handling are unchanged. Regression tests
cover a start with an absent and with a stale worker.pid.
…s running

A worker publishes its lock owner records before its code identity. A
Linux start in that window found the verified live owner, then failed the
identity comparison against the not-yet-published identity file and
stopped the initializing worker's whole tree before launching a
replacement. Treat a verified live lock owner without a published identity
as running, as the LaunchAgent path already does; an identity mismatch
still stops the owner once the identity is published.

Refs kunchenguid#2921
@Diabl0570
Diabl0570 force-pushed the fm/fm-remote-job-dup-workers-pr branch from 1ee85f7 to 9b03374 Compare October 23, 2026 15:42
@Diabl0570 Diabl0570 changed the title fix: prevent duplicate Linux remote job supervisors fix(remote-job): never start a duplicate Linux worker supervisor beside a live lock owner Oct 23, 2026
@kunchenguid

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate: thanks @Diabl0570 for bringing this back up to date — the new head reads well.

I reviewed head 9b033744 against main myself:

  • fm_remote_job_worker_owned_alive now accepts a verified live lock owner (pid, start time and command still match) before reading worker.pid or probing the heartbeat, so a loaded host or a still-initializing owner no longer gets a second supervisor beside it.
  • fm_remote_job_start_linux_worker treats an owner whose code identity isn't published yet as running, and still stops it on a real identity mismatch once published.
  • The worker clears the predecessor's identity alongside its ready file before publishing lock ownership, binds heartbeat refreshes to the opened ready file where /proc/self/fd exists, and recreates a missing ready file through a no-clobber ln.
  • No workflow, credential or network changes, so I've approved the fork CI runs for this head (39980521767, 39980521703, 39980595793).

VISION check: it aligns. It keeps one live remote-job owner across restarts, the mechanics stay in scripts, and there's no new captain-facing surface. Contract class: restore. The Linux start path was already meant to reuse a live verified owner and was duplicating it.

The one scenario you couldn't drive live (the inode-bound heartbeat refresh, which needs /proc) is covered by the Linux CI run. This is now waiting on CI only, nothing from you right now. If it comes back green on this head, it's a merge candidate on the next pass.

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