Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions pm-stack/opencode-claimant/claimant.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import argparse
import base64
import fcntl
import json
import os
import subprocess
Expand Down Expand Up @@ -1403,10 +1404,43 @@ def launch_step(
return launched, skips


def acquire_fire_lock():
"""Take the single-instance fire lock, or return None if held (#8039).

Two overlapping fires (the scheduled task plus an ad-hoc run) each
computed cap_room from the same active-root count and could spend
the same free slots or race one candidate. An exclusive,
non-blocking flock on STATE_DIR/claimant.lock makes a second fire
exit at once. The lock is released when the returned file is closed,
or by the kernel when the process exits, so a crashed fire never
leaves a stale lock.
"""
ensure_state_dir()
handle = (STATE_DIR / "claimant.lock").open("a")
try:
fcntl.flock(handle.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
except BlockingIOError:
handle.close()
return None
return handle


def main():
"""Main entry point for the claimant/executor."""
mode = read_mode()
lock = acquire_fire_lock()
if lock is None:
print("Another claimant fire is running. Exiting.")
log_fire(mode, None, [], None, [{"reason": "already_running"}])
return 0
try:
return _fire(mode)
finally:
lock.close()


def _fire(mode: str):
"""One claimant fire, run under the single-instance lock."""
print(f"OpenCode claimant starting in {mode} mode")

# Step 1: Supervision (always runs in live mode). Tasks it
Expand Down
7 changes: 4 additions & 3 deletions pm-stack/opencode-claimant/prompt.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ The deterministic implementation of these steps is
(`python3 pm-stack/opencode-claimant/claimant.py`, run from the
primary clone) and reports from its fire-log line; it does not
improvise the steps by hand. Inside a fire, claimant.py launches
distinct candidates in parallel under a bounded pool and a
single-instance lock; exactly-once stays anchored on the atomic DB
bind. (History: the cap was 12 until 2026-11-05, when the
distinct candidates one at a time, under a single-instance lock
(an exclusive flock on ~/.local/state/opencode-claimant/claimant.lock;
a second fire exits with fire-log reason `already_running`);
exactly-once stays anchored on the atomic DB bind. (History: the cap was 12 until 2026-11-05, when the
binding-rate fix raised it first to 20, then to 36 the same day (at 20, scheduled lane-runner waves of ~39 roots at load ~4/16 cores still left the claimant cap_room 0; 36 sits above the observed wave size while keeping total roots within ~2x the census-proven level);
claimant.py itself was committed the same day - before that it
existed only as an untracked file in the laptop primary checkout,
Expand Down
58 changes: 58 additions & 0 deletions pm-stack/opencode-claimant/test_claimant.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,64 @@ def poll_stub():
)


class TestSingleInstanceLock(unittest.TestCase):
"""#8039: a second fire while one holds the lock exits early and
logs already_running, touching nothing else."""

def test_second_fire_exits_with_already_running(self):
with tempfile.TemporaryDirectory() as tmp:
state_dir = Path(tmp)
fires = state_dir / "fires.jsonl"
with (
patch.object(claimant, "STATE_DIR", state_dir),
patch.object(claimant, "FIRES_FILE", fires),
patch.object(claimant, "read_mode", return_value="live"),
patch.object(claimant, "_fire") as fire,
):
held = claimant.acquire_fire_lock()
self.assertIsNotNone(held)
try:
self.assertEqual(claimant.main(), 0)
finally:
held.close()
fire.assert_not_called()
entries = [json.loads(l) for l in fires.read_text().splitlines() if l]
self.assertEqual(len(entries), 1)
self.assertEqual(entries[0]["skips"], [{"reason": "already_running"}])
self.assertIsNone(entries[0]["candidates_seen"])
self.assertIsNone(entries[0]["cap_room"])

def test_lock_is_released_after_a_fire(self):
with tempfile.TemporaryDirectory() as tmp:
state_dir = Path(tmp)
with (
patch.object(claimant, "STATE_DIR", state_dir),
patch.object(claimant, "FIRES_FILE", state_dir / "fires.jsonl"),
patch.object(claimant, "read_mode", return_value="live"),
patch.object(claimant, "_fire", return_value=0) as fire,
):
self.assertEqual(claimant.main(), 0)
self.assertEqual(claimant.main(), 0)
self.assertEqual(fire.call_count, 2)
again = claimant.acquire_fire_lock()
self.assertIsNotNone(again, "the lock is free once fires finish")
again.close()

def test_lock_is_released_when_a_fire_raises(self):
with tempfile.TemporaryDirectory() as tmp:
state_dir = Path(tmp)
with (
patch.object(claimant, "STATE_DIR", state_dir),
patch.object(claimant, "read_mode", return_value="live"),
patch.object(claimant, "_fire", side_effect=RuntimeError("boom")),
):
with self.assertRaises(RuntimeError):
claimant.main()
again = claimant.acquire_fire_lock()
self.assertIsNotNone(again)
again.close()


class TestCapUnknownFireLog(unittest.TestCase):
def test_cap_unknown_logs_cap_room_null_not_sentinel(self):
# #7770 Phase 4 audit: cap_step's -1 is an internal unknown
Expand Down