From b21b3453779f5a5abd2c6f9d0498bddf8aaaaf01 Mon Sep 17 00:00:00 2001 From: memtomem <118508877+memtomem@users.noreply.github.com> Date: Sat, 14 Nov 2026 22:53:03 +0900 Subject: [PATCH 1/2] fix: skip a damaged board dependency and bound user-repo git and preflight output - next_runnable reads depends_on through _parse_deps (a JSON list of strings only). A damaged row is unsatisfied and skipped with a warning. Before, 5 of 8 measured damage shapes raised BoardError and blocked every automatic claim, and {} or "" made the damaged item's dependencies vacuously done. - The user-repository git calls in project.py (6) and cli.py (2) run through worktree._run_git: a wedged git is group-killed after 600s and reaches each site's existing failure idiom. git_root says the inspection timed out instead of "not a Git repository". - The toolgraph preflight runner keeps a 4000-character tail of each stream; syncmill never reads its stdout and shows a short stderr snippet. - memory_migration._atomic_json fsyncs the file before the rename and the directory after it. Co-Authored-By: Claude Opus 5.5 --- CLAUDE.md | 2 +- docs/architecture/board.md | 2 +- src/syncmill/board.py | 42 +++++++-- src/syncmill/cli.py | 5 +- src/syncmill/memory_migration.py | 11 +++ src/syncmill/preflight.py | 11 ++- src/syncmill/project.py | 16 ++-- tests/test_board.py | 95 ++++++++++++++++---- tests/test_git_timeouts.py | 147 +++++++++++++++++++++++++++++++ tests/test_memory_migration.py | 31 +++++++ tests/test_preflight.py | 58 ++++++++++++ tests/test_project.py | 10 +-- 12 files changed, 391 insertions(+), 39 deletions(-) create mode 100644 tests/test_git_timeouts.py diff --git a/CLAUDE.md b/CLAUDE.md index c6ce96ca..59b923fb 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -254,7 +254,7 @@ Rules that must hold: - **Agents never touch the board.** The item reaches them via `render_item_prompt`; `parse_board_update` reads an optional ` ```syncmill-board ` block, **never raises** (catches `RecursionError` too), sanitizes each field independently, drops a malformed `outputs` list whole, and agrees with `strip_board_block` on block bounds (a genuine close fence is required). - **Load-bearing, built lazily.** Disabled by default (`build_board` → `None`). Built on first `run_item`, never in `Supervisor.__init__`, so a corrupt board cannot break a plain `run`. `Board()` raises only `BoardError` (`sqlite3.Error`, `OSError`, and `RuntimeError` from `expanduser`, which runs inside the try). Dispatch and mutation (`board` commands, `dashboard`) convert it to `ClickException` — no fallback. Read-mostly consumer surfaces degrade instead: `serve` keeps the shared-memory tools up and its `board_*` tools report the board as unavailable (not "not enabled"); `tui` opens in limited mode. - **State machine `planned → running → (blocked | review) → done`.** `done` requires ≥1 passing `Evidence` or a human sign-off, counting evidence already on the item plus any supplied now; `board update ID --state done` with neither is rejected. An agent's advisory `done` counts only with passing verification from **this** run, else it is clamped to `review` (never raises). Humans cannot move an item into `running`. -- **Atomic claim + lease fencing.** Every claim path (`claim(id)` and `next_runnable`) is a `BEGIN IMMEDIATE` CAS `planned→running` (autocommit `isolation_level=None`, WAL, `busy_timeout`). Automatic selection (`next_runnable`) additionally requires: not `human_required`, every `depends_on` item `done` (a missing dependency is unsatisfied), FIFO `created_at, rowid`, optional exact `goal`. An explicit `claim(id)` checks only id + `planned` — it is the operator override. The lease is a fencing token: `apply_update(run_id=)` and `_block_on_crash` CAS on `state='running' AND lease_run_id=?` and drop a 0-row match. A same-state annotate keeps the lease; a no-`--state` annotate resolves its target inside the transaction. Every transactional method rolls back on **any** exception. `recover_stale_running()` runs at the top of every `run_item`. +- **Atomic claim + lease fencing.** Every claim path (`claim(id)` and `next_runnable`) is a `BEGIN IMMEDIATE` CAS `planned→running` (autocommit `isolation_level=None`, WAL, `busy_timeout`). Automatic selection (`next_runnable`) additionally requires: not `human_required`, every `depends_on` item `done` (a missing dependency is unsatisfied; an unreadable `depends_on` is unsatisfied and skipped with a warning, never blocking the rest), FIFO `created_at, rowid`, optional exact `goal`. An explicit `claim(id)` checks only id + `planned` — it is the operator override. The lease is a fencing token: `apply_update(run_id=)` and `_block_on_crash` CAS on `state='running' AND lease_run_id=?` and drop a 0-row match. A same-state annotate keeps the lease; a no-`--state` annotate resolves its target inside the transaction. Every transactional method rolls back on **any** exception. `recover_stale_running()` runs at the top of every `run_item`. - **Memory boundary.** Items run with `SyncmillMemory.scoped(run_id)`, collapsing shared and per-agent namespaces to `board-run:{run_id}`; scoped reads also union `shared` (D4), writes never leave the scope. Afterwards one best-effort sanitized summary goes to global `shared` via `share_board_result` (output with the board block stripped, never the enriched prompt) — **only if the fenced `apply_update` applied**; a run that lost its lease publishes nothing. Decisions (`active_decisions(goal=item.goal)`: active global decisions plus that exact goal's) and the handoff baton are injected deterministically, never via top-k recall. Scratch namespaces are GC'd via `collectible_board_run_ids`, which reads ownership and liveness in one SQLite snapshot (a claim commits its lease and ledger row atomically) and excludes active leases and reclaimed runs still within one lease timeout of grace; the sweep runs under a cross-process lock, tombstones only acknowledged deletions, and never touches `shared` or `agent-runtime:*`. - **Crash vs failure.** A crash (`BaseException`) moves the item to `blocked` + `human_required` and re-raises; a FAILED result is not a crash (`apply_update` records `blocked`). - **Audit compaction.** `board compact` and the opt-in `board.transitions_retain_seconds` (default `0` = off) share `compact_transitions`: fold stats aggregates, then delete, in one transaction, so `stats()` is unchanged. The automatic call runs only at `run_item`'s housekeeping points (after the outcome, or on an empty claim; never on the claim or crash path) and degrades to a warning. diff --git a/docs/architecture/board.md b/docs/architecture/board.md index df4ece1e..d466be63 100644 --- a/docs/architecture/board.md +++ b/docs/architecture/board.md @@ -10,7 +10,7 @@ The `board` command group turns syncmill from a single-run, stateless orchestrator into one with **durable, structured records of team work** — goal, owner, state, what's blocked/why, what's done **and verified**, dependencies, and a handoff baton. It is a **new flat top-level module** (gateway/monitor/session precedent — *don't pre-abstract*) backed by its **own SQLite store** at `~/.syncmill/board.db`: memtomem can't back it (markdown+embedding *knowledge* chunks have no structured status/dependency fields, no state machine, no atomic compare-and-swap). *State lives only on the board; knowledge lives only in memtomem's `shared`; the two cross-link by id.* Five locked design decisions (codex-reviewed pre-code over 5 rounds): -1. **Applied at the Supervisor run-lifecycle level, NOT threaded into `Strategy.execute`** — like `_matched_order`, the board has no topology-specific meaning. A new `Supervisor.run_item` *wraps* `run`; the **`Strategy` protocol is unchanged**; `run` gains exactly **one optional kwarg `memory=`** (`mem = memory or self._memory`, used at ALL five memory call sites — register/start_session/strategy/share/end — so plain `run` is behavior-identical). 2. **Agents stay pure** — orchestrated agents NEVER touch the board; the Supervisor mediates by rendering the item into the prompt (`render_item_prompt`, deterministic) and parsing an OPTIONAL ` ```syncmill-board ` fenced block back out of `result.output` (`parse_board_update` — its OWN parser, NOT a reuse: it degrades to `{}` and **NEVER raises** — incl. on pathologically-nested JSON, whose `json.loads` raises `RecursionError`, a `RuntimeError` not a `ValueError`, so it is caught explicitly. Recognized fields are sanitized **individually** (one malformed field can't discard a valid `state`); the all-or-nothing part is the `outputs` **list** (a mixed list is dropped whole, never salvaged to a subset — the `DecomposeStrategy._parse_subtasks` *philosophy*). `_extract_board_block` requires a genuine close fence — a 2nd `syncmill-board` OPEN fence is never mistaken for the first block's close, matching `strip_board_block` so the parser and the stripper agree on block bounds). 3. **The board store is LOAD-BEARING, built LAZILY** — it's the source of work + system of record, so unlike memory/breaker it does **not** degrade silently: a corrupt/unopenable db raises `BoardError` (caught at `Board.__init__` for `sqlite3.Error` — a corrupt file, `OSError` — an unwritable parent dir, *and* `RuntimeError` — an unresolvable `~` when no home dir is set; `expanduser()` runs **inside** that try so `Board()` raises **only** `BoardError`, the contract `serve` relies on to catch a bad board and still close memory in its `finally`) → the CLI converts to `click.ClickException` (the `session.SessionLaunchError` "no fallback — it IS the feature" contract). Disabled by default (`build_board`→`None`); the Supervisor builds it lazily on first `run_item` (stored on `self._board`, closed in `close()`), **NOT** in `__init__`, so an enabled-but-corrupt board can never break a plain `run` (which never calls `run_item`). 4. **State machine `planned → running → (blocked | review) → done`**, where **`review` vs `done` = "done AND verified"**, load-bearing in `Board.transition`: a move TO `done` requires ≥1 passing structured `Evidence` (a `GateReport` maps to one) OR a human sign-off (`Evidence{kind:"human"}`), else `BoardError` — a bare `board update ID --state done` is rejected (the human CLI path). An AGENT's advisory `state:done` is honored only when **THIS run** produced passing verification (a gate that passed *this* run) — stale evidence from an earlier, now-discarded run does NOT certify the current work (`apply_update` gates the advisory `done` on the run's own evidence, while still persisting the accumulated evidence that backs the human `review→done` promotion); otherwise it is CLAMPED to `review`+audit (**never raises** — a good run is never turned `blocked` by an invalid advisory field). The human path also rejects a manual move INTO `running` (it is owned by the claim lease, never set by hand — `board update` does not offer it). Dependencies gate dispatch; an **atomic claim** (`BEGIN IMMEDIATE` CAS `planned→running`, stamping `lease_run_id`+`claimed_at`) is the no-duplicate-work guard on **every** path (`claim` by-id and `next_runnable` alike — opened in autocommit `isolation_level=None` so writers drive `BEGIN IMMEDIATE`/`COMMIT` by hand; WAL + `busy_timeout`); `human_required` items are excluded from auto-dispatch; FIFO order is `created_at, rowid` (rowid breaks same-clock-tick ties so insertion order holds). Append-only `transitions` audit table. **Stale-claim recovery** `recover_stale_running()` reclaims items past `lease_timeout_seconds` from `running`→`planned` (the `cleanup_orphaned_worktrees`/`_sweep_stale_session_dirs` analogue), run at the top of every `run_item`. The lease is the **fencing token**, not just a dispatch guard (adversarial-review catch): `apply_update(run_id=)` is a **lease-fenced CAS** (`WHERE id=? AND state='running' AND lease_run_id=?`) so a slow run whose item was reclaimed + re-dispatched to another runner can't clobber the new owner or revert a verified `done` — its stale write is dropped; the **crash-block path is fenced the same way** (`Supervisor._block_on_crash` threads `run_id` into `transition(run_id=)`, an identical `WHERE state='running' AND lease_run_id=?` CAS that drops a 0-row match), so a stale *crashed* run can't clobber a re-dispatched owner or revert a verified state either — including a crash AFTER `apply_update` already resolved the item (e.g. Ctrl-C during the shared-memory write), since the item has then left `running`; and `transition` preserves the lease on a *same-state annotate of a running item* (only clearing it when actually leaving `running`) and resolves a no-`--state` annotate's target from its **in-transaction** read (a `to_state=None` sentinel, never a stale pre-read), so a `board update` annotation can't strand a live claim into stale-recovery. Every transactional method rolls back on **any** exception (not only `sqlite3.Error` — e.g. a corrupt-row `ValueError`), so a `BEGIN IMMEDIATE` write lock is never leaked. 5. **The board↔`shared` boundary is closed via a per-run memory scope** — `run_item` runs the item through the normal pipeline with a **board-scoped `SyncmillMemory.scoped(run_id)`** that collapses BOTH the shared namespace AND every per-agent (session/search) namespace to `board-run:{run_id}` (via a single `_agent_ns` seam). So the strategy-internal shares (pipeline/council/decompose) AND `start_task_session`'s prompt-log all write to the per-run scope — intra-run handoff still works — and **nothing board-enriched leaks into the persistent global `shared` or `agent-runtime:*`**. After the strategy returns, `run_item` writes ONE sanitized summary to GLOBAL `shared` via `SyncmillMemory.share_board_result(item_id, task, strip_board_block(output))` (built from the item task + the agent output with the `syncmill-board` block stripped — never the enriched prompt); the **cross-link is the SINGLE composite tag `board-result:{item_id}`** (memtomem `add()` returns no chunk id; `search(tag_filter=...)` is a `str`/comma-OR, NOT a list). Decisions + the handoff baton are injected DETERMINISTICALLY from the board into the prompt, never via top-k recall (**Phase 2 (D4)** additionally has board runs **union-read** global cross-run `shared` — the scoped view's *reads* widen to the real `shared` while its *writes* stay confined to `board-run:{run_id}`, so the leak guard is untouched). The per-run `board-run:{run_id}` scratch namespaces are **GC'd automatically** after a run outcome or an empty claim, using `Board.collectible_board_run_ids(limit=100)` to read ownership and liveness in one SQLite snapshot. Claims atomically write their lease and ledger row; active leases and reclaimed-run grace remain protected. The sweep excludes the just-finished run, takes a nonblocking board-specific cross-process GC lock, and tombstones only backend-acknowledged deletions. Failure backoff prevents a poisoned namespace from monopolizing later sweeps. The exiting run reactivates its ledger row in `finally`; a process killed between a late write and this finalizer can still leave residue. Collection is advisory and can remove scratch from a slow run after reclaimed-lease grace expires. `delete_board_runs` reconstructs `board-run:` from ledger IDs, preserving `shared` and `agent-runtime:*`. Optional atomic backend purge and its limits are described above; historical mixed shared day files require manual review. See [the stabilization roadmap](../improvement-roadmap-2026-09-08.ko.md) for evidence and recovery boundaries. +1. **Applied at the Supervisor run-lifecycle level, NOT threaded into `Strategy.execute`** — like `_matched_order`, the board has no topology-specific meaning. A new `Supervisor.run_item` *wraps* `run`; the **`Strategy` protocol is unchanged**; `run` gains exactly **one optional kwarg `memory=`** (`mem = memory or self._memory`, used at ALL five memory call sites — register/start_session/strategy/share/end — so plain `run` is behavior-identical). 2. **Agents stay pure** — orchestrated agents NEVER touch the board; the Supervisor mediates by rendering the item into the prompt (`render_item_prompt`, deterministic) and parsing an OPTIONAL ` ```syncmill-board ` fenced block back out of `result.output` (`parse_board_update` — its OWN parser, NOT a reuse: it degrades to `{}` and **NEVER raises** — incl. on pathologically-nested JSON, whose `json.loads` raises `RecursionError`, a `RuntimeError` not a `ValueError`, so it is caught explicitly. Recognized fields are sanitized **individually** (one malformed field can't discard a valid `state`); the all-or-nothing part is the `outputs` **list** (a mixed list is dropped whole, never salvaged to a subset — the `DecomposeStrategy._parse_subtasks` *philosophy*). `_extract_board_block` requires a genuine close fence — a 2nd `syncmill-board` OPEN fence is never mistaken for the first block's close, matching `strip_board_block` so the parser and the stripper agree on block bounds). 3. **The board store is LOAD-BEARING, built LAZILY** — it's the source of work + system of record, so unlike memory/breaker it does **not** degrade silently: a corrupt/unopenable db raises `BoardError` (caught at `Board.__init__` for `sqlite3.Error` — a corrupt file, `OSError` — an unwritable parent dir, *and* `RuntimeError` — an unresolvable `~` when no home dir is set; `expanduser()` runs **inside** that try so `Board()` raises **only** `BoardError`, the contract `serve` relies on to catch a bad board and still close memory in its `finally`) → the CLI converts to `click.ClickException` (the `session.SessionLaunchError` "no fallback — it IS the feature" contract). Disabled by default (`build_board`→`None`); the Supervisor builds it lazily on first `run_item` (stored on `self._board`, closed in `close()`), **NOT** in `__init__`, so an enabled-but-corrupt board can never break a plain `run` (which never calls `run_item`). 4. **State machine `planned → running → (blocked | review) → done`**, where **`review` vs `done` = "done AND verified"**, load-bearing in `Board.transition`: a move TO `done` requires ≥1 passing structured `Evidence` (a `GateReport` maps to one) OR a human sign-off (`Evidence{kind:"human"}`), else `BoardError` — a bare `board update ID --state done` is rejected (the human CLI path). An AGENT's advisory `state:done` is honored only when **THIS run** produced passing verification (a gate that passed *this* run) — stale evidence from an earlier, now-discarded run does NOT certify the current work (`apply_update` gates the advisory `done` on the run's own evidence, while still persisting the accumulated evidence that backs the human `review→done` promotion); otherwise it is CLAMPED to `review`+audit (**never raises** — a good run is never turned `blocked` by an invalid advisory field). The human path also rejects a manual move INTO `running` (it is owned by the claim lease, never set by hand — `board update` does not offer it). Dependencies gate dispatch (an unreadable `depends_on` — anything but a JSON list of strings — is unsatisfied: `next_runnable` skips that row with a warning, so it neither dispatches nor blocks the items behind it); an **atomic claim** (`BEGIN IMMEDIATE` CAS `planned→running`, stamping `lease_run_id`+`claimed_at`) is the no-duplicate-work guard on **every** path (`claim` by-id and `next_runnable` alike — opened in autocommit `isolation_level=None` so writers drive `BEGIN IMMEDIATE`/`COMMIT` by hand; WAL + `busy_timeout`); `human_required` items are excluded from auto-dispatch; FIFO order is `created_at, rowid` (rowid breaks same-clock-tick ties so insertion order holds). Append-only `transitions` audit table. **Stale-claim recovery** `recover_stale_running()` reclaims items past `lease_timeout_seconds` from `running`→`planned` (the `cleanup_orphaned_worktrees`/`_sweep_stale_session_dirs` analogue), run at the top of every `run_item`. The lease is the **fencing token**, not just a dispatch guard (adversarial-review catch): `apply_update(run_id=)` is a **lease-fenced CAS** (`WHERE id=? AND state='running' AND lease_run_id=?`) so a slow run whose item was reclaimed + re-dispatched to another runner can't clobber the new owner or revert a verified `done` — its stale write is dropped; the **crash-block path is fenced the same way** (`Supervisor._block_on_crash` threads `run_id` into `transition(run_id=)`, an identical `WHERE state='running' AND lease_run_id=?` CAS that drops a 0-row match), so a stale *crashed* run can't clobber a re-dispatched owner or revert a verified state either — including a crash AFTER `apply_update` already resolved the item (e.g. Ctrl-C during the shared-memory write), since the item has then left `running`; and `transition` preserves the lease on a *same-state annotate of a running item* (only clearing it when actually leaving `running`) and resolves a no-`--state` annotate's target from its **in-transaction** read (a `to_state=None` sentinel, never a stale pre-read), so a `board update` annotation can't strand a live claim into stale-recovery. Every transactional method rolls back on **any** exception (not only `sqlite3.Error` — e.g. a corrupt-row `ValueError`), so a `BEGIN IMMEDIATE` write lock is never leaked. 5. **The board↔`shared` boundary is closed via a per-run memory scope** — `run_item` runs the item through the normal pipeline with a **board-scoped `SyncmillMemory.scoped(run_id)`** that collapses BOTH the shared namespace AND every per-agent (session/search) namespace to `board-run:{run_id}` (via a single `_agent_ns` seam). So the strategy-internal shares (pipeline/council/decompose) AND `start_task_session`'s prompt-log all write to the per-run scope — intra-run handoff still works — and **nothing board-enriched leaks into the persistent global `shared` or `agent-runtime:*`**. After the strategy returns, `run_item` writes ONE sanitized summary to GLOBAL `shared` via `SyncmillMemory.share_board_result(item_id, task, strip_board_block(output))` (built from the item task + the agent output with the `syncmill-board` block stripped — never the enriched prompt); the **cross-link is the SINGLE composite tag `board-result:{item_id}`** (memtomem `add()` returns no chunk id; `search(tag_filter=...)` is a `str`/comma-OR, NOT a list). Decisions + the handoff baton are injected DETERMINISTICALLY from the board into the prompt, never via top-k recall (**Phase 2 (D4)** additionally has board runs **union-read** global cross-run `shared` — the scoped view's *reads* widen to the real `shared` while its *writes* stay confined to `board-run:{run_id}`, so the leak guard is untouched). The per-run `board-run:{run_id}` scratch namespaces are **GC'd automatically** after a run outcome or an empty claim, using `Board.collectible_board_run_ids(limit=100)` to read ownership and liveness in one SQLite snapshot. Claims atomically write their lease and ledger row; active leases and reclaimed-run grace remain protected. The sweep excludes the just-finished run, takes a nonblocking board-specific cross-process GC lock, and tombstones only backend-acknowledged deletions. Failure backoff prevents a poisoned namespace from monopolizing later sweeps. The exiting run reactivates its ledger row in `finally`; a process killed between a late write and this finalizer can still leave residue. Collection is advisory and can remove scratch from a slow run after reclaimed-lease grace expires. `delete_board_runs` reconstructs `board-run:` from ledger IDs, preserving `shared` and `agent-runtime:*`. Optional atomic backend purge and its limits are described above; historical mixed shared day files require manual review. See [the stabilization roadmap](../improvement-roadmap-2026-09-08.ko.md) for evidence and recovery boundaries. `run_item` flow: `_require_board` (lazy build; clear error if disabled) → `recover_stale_running()` → atomically claim (`claim(id)` or `next_runnable(run_id)`; `None` stops a `--loop` caller) → `Task(prompt=render_item_prompt(item))` → `run(memory=scoped)` → `parse_board_update(result.output)` → `board.apply_update(...)` [advisory] → `share_board_result(...)` to global shared. Wrapped in `try/except BaseException` → a genuine CRASH transitions the item `→ blocked` (`_block_on_crash`, best-effort, sets `human_required`) and re-raises; a FAILED result is **not** a crash (`apply_update` records it as `blocked`). **`--strategy` is NOT a `run_item` param** — the board CLI mutates `config.default_strategy` BEFORE constructing the `Supervisor` (matching how `run` selects a strategy in `__init__`). The CLI `board` group mirrors `session`: `_require_board` (mirror `_require_tmux`) hard-errors when disabled; `add TASK [--goal --depends-on(repeatable) --human-required]` (no `--owner` — `owner_agent` is RECORD-ONLY in v1, stamped from `result.agent_id`), `list [--state --goal]`, `show ID`, `run [--id --loop --strategy --agent --team --goal]` (Phase 4: `--goal ` scopes the atomic claim to that goal via `next_runnable(goal=)` — mutually exclusive with `--id` — and auto-applies the team declaring `g`; `--team` overlays a profile pre-`Supervisor` and beats the goal mapping), `update ID [--state --evidence --sign-off --note --clear-human-required]` (the human path; `done` REQUIRES `--evidence`/`--sign-off`). **Hermetic tests:** `tests/test_board.py` drives the REAL `Board` against `tmp_path/"board.db"` with an injected `now` (like `test_breaker.py`) — CRUD/audit, atomic dedup on BOTH paths (two `Board`s/one db → exactly one claims), dep/skip, stale recovery, done-gate, gate→Evidence, advisory-done-clamps, corrupt+unopenable db→`BoardError`, parse/strip tables; `conftest.py` adds a duck-typed `FakeBoard` + `FakeMemory.scoped`/`share_board_result` (the scoped child's shares stay off the parent's global `.shared` — the leak guard); `test_supervisor.py` covers `run_item` (board-scoped-memory leak guard, crash→blocked, lazy build never touched by a plain `run`, `close()` closes the board); `test_cli.py` exercises the `board` subcommands via `CliRunner` with a tmp db. **`Evidence`/`WorkItem`/`WorkState`/`Decision`/`BoardStats` live in `board.py`, not `models.py`** (they never flow through a strategy — the `breaker._AgentState` rule). diff --git a/src/syncmill/board.py b/src/syncmill/board.py index 6a05e8a9..6094a84d 100644 --- a/src/syncmill/board.py +++ b/src/syncmill/board.py @@ -708,7 +708,9 @@ def next_runnable(self, run_id: str, goal: str | None = None) -> WorkItem | None """Atomically claim the next dispatchable item for ``run_id``, or ``None``. Dispatchable = ``planned``, not ``human_required``, and every ``depends_on`` - item is ``done``. Items are considered in FIFO order (``created_at``). The + item is ``done``. An unreadable ``depends_on`` (:func:`_parse_deps`) is + unsatisfied: the row is skipped with a warning, so it neither dispatches + nor blocks the items behind it. Items are considered in FIFO order (``created_at``). The whole select-then-claim runs in one ``BEGIN IMMEDIATE`` transaction, so it is as atomic as :meth:`claim`. @@ -741,7 +743,19 @@ def next_runnable(self, run_id: str, goal: str | None = None) -> WorkItem | None done_ids: set = set() looked_up: set = set() - def _deps_done(deps: list) -> bool: + def _deps_done(row: sqlite3.Row) -> bool: + deps = _parse_deps(row["depends_on"]) + if deps is None: + # Unsatisfied, never dispatched: a prerequisite list that + # cannot be read cannot be proven done. Skipping the row + # rather than raising keeps one damaged item from + # blocking every other item's automatic claim. + logger.warning( + "board item %s has an unreadable depends_on; automatic " + "dispatch skips it (fix the item, or claim it by id)", + _p(str(row["id"])), + ) + return False unknown = [d for d in deps if d not in looked_up] for start in range(0, len(unknown), 500): chunk = unknown[start : start + 500] @@ -758,7 +772,7 @@ def _deps_done(deps: list) -> bool: return self._deps_satisfied(deps, done_ids) chosen = next( - (r for r in rows if _deps_done(json.loads(r["depends_on"]))), + (r for r in rows if _deps_done(r)), None, ) if chosen is None: @@ -768,8 +782,7 @@ def _deps_done(deps: list) -> bool: self._conn.execute("COMMIT") return item except Exception as exc: - # Broad on purpose: a non-sqlite error mid-transaction (e.g. a corrupt - # `depends_on` JSON failing `json.loads` above → ValueError) must still + # Broad on purpose: a non-sqlite error mid-transaction must still # roll back the open BEGIN IMMEDIATE — otherwise the RESERVED lock leaks # and the next op fails with "cannot start a transaction within a # transaction". Surfaces as BoardError, like get()/list() already do. @@ -1948,6 +1961,25 @@ def _row_to_decision(row: sqlite3.Row) -> Decision: ) +def _parse_deps(raw: object) -> list[str] | None: + """A stored ``depends_on`` as dispatch reads it, or ``None`` when damaged. + + Only a JSON list of strings is a dependency list. Anything else is + damaged, including shapes that iterate without error: an empty ``{}`` or + ``""`` would make every dependency vacuously done, and ``"abc"`` would be + read as the three ids ``a``, ``b`` and ``c``. + """ + if not isinstance(raw, str): + return None + try: + value = json.loads(raw) + except (ValueError, RecursionError): + return None + if not isinstance(value, list) or not all(isinstance(d, str) for d in value): + return None + return value + + def _load_list(raw: str) -> list[str]: try: v = json.loads(raw) diff --git a/src/syncmill/cli.py b/src/syncmill/cli.py index 797afa51..68f2b29c 100644 --- a/src/syncmill/cli.py +++ b/src/syncmill/cli.py @@ -193,6 +193,7 @@ ) from syncmill.worktree import ( WorkspaceError, + _run_git, capture_repository_snapshot, cleanup_orphaned_worktrees, discard_retained_workspace, @@ -853,7 +854,7 @@ def _gate_for_this_run(commands: list[str] | None): def _dirty_count(root: Path) -> int: - proc = subprocess.run( + proc = _run_git( ["git", "status", "--porcelain"], cwd=str(root), capture_output=True, text=True, env=isolated_git_env(), ) @@ -1315,7 +1316,7 @@ def subprocess_status_dirty(root: Path) -> bool: reported an unreadable or rejected repository ("dubious ownership") as having nothing to preserve. """ - proc = subprocess.run( + proc = _run_git( ["git", "status", "--porcelain"], cwd=str(root), capture_output=True, text=True, env=isolated_git_env(), ) diff --git a/src/syncmill/memory_migration.py b/src/syncmill/memory_migration.py index b8513118..d73f0b72 100644 --- a/src/syncmill/memory_migration.py +++ b/src/syncmill/memory_migration.py @@ -440,11 +440,22 @@ def _atomic_json(path: Path, value: Any, *, mode: int = 0o600) -> None: try: with os.fdopen(fd, "w", encoding="utf-8") as fh: fh.write(json.dumps(value, ensure_ascii=False, indent=2) + "\n") + # Durable before it is published: apply, verify and rollback + # decide from these files, and a rename that reaches the disk + # ahead of the data can leave an empty state file after a crash. + fh.flush() + os.fsync(fh.fileno()) os.chmod(tmp, mode) os.replace(tmp, path) except BaseException: tmp.unlink(missing_ok=True) raise + # And the rename itself, so a crash cannot bring back the previous state. + directory = os.open(path.parent, os.O_RDONLY) + try: + os.fsync(directory) + finally: + os.close(directory) def _artifact(migration_id: str) -> Path: diff --git a/src/syncmill/preflight.py b/src/syncmill/preflight.py index b0fdc137..356366ed 100644 --- a/src/syncmill/preflight.py +++ b/src/syncmill/preflight.py @@ -40,6 +40,12 @@ # recognise it. from syncmill.planning import unmapped_principal_id +#: What the preflight runner keeps of each stream. Syncmill never reads the +#: binary's stdout (the verdict is the ``--out`` artifact, which toolgraph also +#: echoes there) and shows only a short stderr snippet, so the rest is drained +#: and dropped rather than held in memory. +_OUTPUT_TAIL_CHARS = 4000 + logger = logging.getLogger(__name__) PREFLIGHT_FILE = "preflight.json" @@ -199,7 +205,7 @@ def _invoke_control_toolgraph( try: proc = _sync_run( # owned-group runner: a timeout kills the whole tree argv, capture_output=True, text=True, errors="replace", - timeout=cfg.timeout_seconds, + timeout=cfg.timeout_seconds, tail_chars=_OUTPUT_TAIL_CHARS, ) except FileNotFoundError: return ( @@ -859,7 +865,7 @@ def _invoke_toolgraph( try: proc = _sync_run( # owned-group runner: a timeout kills the whole tree argv, capture_output=True, text=True, errors="replace", - timeout=cfg.timeout_seconds, + timeout=cfg.timeout_seconds, tail_chars=_OUTPUT_TAIL_CHARS, ) except FileNotFoundError: return "error", f"toolgraph binary {cfg.command[0]!r} not found in PATH" @@ -978,6 +984,7 @@ def _run_preflight_inner( # strict mode blocks on regardless of `unavailable_policy`. errors="replace", timeout=cfg.timeout_seconds, + tail_chars=_OUTPUT_TAIL_CHARS, ) except FileNotFoundError: warnings.append(f"toolgraph binary {cfg.command[0]!r} not found in PATH") diff --git a/src/syncmill/project.py b/src/syncmill/project.py index 56582129..7677b558 100644 --- a/src/syncmill/project.py +++ b/src/syncmill/project.py @@ -20,6 +20,8 @@ from syncmill import teamfile from syncmill.config import SyncmillConfig from syncmill.git_env import isolated_git_env +from syncmill.artifacts import _git_timed_out +from syncmill.worktree import _run_git logger = logging.getLogger(__name__) @@ -51,12 +53,14 @@ def git_root(path: Path) -> Path: ``--repo`` path as a configuration problem. """ try: - proc = subprocess.run( + proc = _run_git( ["git", "rev-parse", "--show-toplevel"], cwd=str(path), env=isolated_git_env(), capture_output=True, text=True, ) except OSError as exc: raise ProjectError(f"cannot inspect as a Git repository: {path} ({exc})") from exc + if _git_timed_out(proc): + raise ProjectError(f"git timed out inspecting {path}") if proc.returncode != 0: raise ProjectError(f"not a Git repository: {path}") return Path(proc.stdout.strip()).resolve() @@ -249,7 +253,7 @@ def _index_supply(root: Path) -> str | None: argv = ["git", "ls-files", "-z", "--stage", "--", f":(icase){_MARKER_DIR}"] argv.extend(_alias_pathspecs(root, _MARKER_DIR)) try: - proc = subprocess.run( + proc = _run_git( argv, cwd=str(root), env=isolated_git_env(), capture_output=True, ) except OSError: @@ -369,7 +373,7 @@ def _head_entries(root: Path, treeish: str) -> list[tuple[str, str]] | None: def _git_query(root: Path, args: list[str]) -> subprocess.CompletedProcess | None: """Run a read-only git query, or ``None`` when it could not run at all.""" try: - return subprocess.run( + return _run_git( ["git", *args], cwd=str(root), env=isolated_git_env(), capture_output=True, ) except OSError: @@ -551,7 +555,7 @@ def _require_ignored_state_root( relative_text = relative.as_posix().rstrip("/") probes = (relative_text, f"{relative_text}/", f"{relative_text}/.syncmill-probe") for probe in probes: - ignored = subprocess.run( + ignored = _run_git( ["git", "check-ignore", "-q", "--no-index", "--", probe], cwd=str(repository), env=env, @@ -918,7 +922,7 @@ def _path_is_ignored(root: Path, relative: str) -> bool: would append one more identical line. """ try: - proc = subprocess.run( + proc = _run_git( ["git", "check-ignore", "-q", "--no-index", "--", relative], cwd=str(root), env=isolated_git_env(), capture_output=True, ) @@ -1052,7 +1056,7 @@ def _write_init_artifacts( def _extend_info_exclude(root: Path) -> None: """Append syncmill's rules to `.git/info/exclude` (machine-local).""" - exclude_probe = subprocess.run( + exclude_probe = _run_git( ["git", "rev-parse", "--git-path", "info/exclude"], cwd=str(root), env=isolated_git_env(), capture_output=True, text=True, ) diff --git a/tests/test_board.py b/tests/test_board.py index f0ad07cf..270bfe9b 100644 --- a/tests/test_board.py +++ b/tests/test_board.py @@ -1349,27 +1349,88 @@ def test_scratch_ledger_upgrade_backfills_history_and_is_idempotent(tmp_path, cl reopened.close() -def test_next_runnable_corrupt_depends_on_raises_board_error_no_dangling_txn(tmp_path): - # FIX: a corrupt depends_on JSON surfaces as BoardError (like get/list) AND rolls - # back the BEGIN IMMEDIATE — else the lock leaks and the next op fails with - # "cannot start a transaction within a transaction". - import sqlite3 +def test_next_runnable_error_mid_scan_raises_board_error_no_dangling_txn( + tmp_path, monkeypatch +): + # A non-sqlite error inside the claim transaction surfaces as BoardError + # AND rolls back the BEGIN IMMEDIATE, else the lock leaks and the next op + # fails with "cannot start a transaction within a transaction". + import syncmill.board as board_mod + + bd = Board(str(tmp_path / "b.db")) + try: + bd.add("x", depends_on=[str(uuid.uuid4())]) + + def boom(raw): + raise ValueError("injected") + + with monkeypatch.context() as patched: + patched.setattr(board_mod, "_parse_deps", boom) + with pytest.raises(BoardError, match="injected"): + bd.next_runnable("r1") + # rolled back cleanly → a fresh BEGIN IMMEDIATE op works (no dangling txn) + assert bd.recover_stale_running() == 0 + finally: + bd.close() + + +_DAMAGED_DEPENDS_ON = { + "invalid-json": "[oops", + "null": "null", + "int": "5", + "nested-list": '[["a"]]', + "deep": "[" * 100000 + "]" * 100000, + "empty-object": "{}", + "empty-string": '""', + "string": '"abc"', + "object": '{"x": 1}', + "list-of-int": "[1]", +} + + +@pytest.mark.parametrize("raw", list(_DAMAGED_DEPENDS_ON.values()), ids=list(_DAMAGED_DEPENDS_ON)) +def test_next_runnable_skips_an_unreadable_depends_on_and_claims_the_rest( + tmp_path, caplog, raw +): + # One damaged row neither blocks every other item's claim nor gets claimed + # itself, including the shapes that iterate without error: an empty {} or + # "" made every dependency vacuously done, and "abc" / {"x": 1} were read + # as ids a, b, c / x, which are all done here. db = str(tmp_path / "b.db") bd = Board(db) - it = bd.add("x") - bd.close() - raw = sqlite3.connect(db) - raw.execute("UPDATE work_items SET depends_on='not json' WHERE id=?", (it.id,)) - raw.commit() - raw.close() - bd2 = Board(db) try: - with pytest.raises(BoardError): - bd2.next_runnable("r1") - # rolled back cleanly → a fresh BEGIN IMMEDIATE op works (no dangling txn) - assert bd2.recover_stale_running() == 0 + head = bd.add("damaged head", goal="g") + tail = bd.add("healthy tail", goal="g") + for done_id in ("a", "b", "c", "x", "1"): + done = bd.add(f"done {done_id}") + bd._conn.execute( + "UPDATE work_items SET id=?, state='done' WHERE id=?", (done_id, done.id) + ) + bd._conn.execute("UPDATE work_items SET depends_on=? WHERE id=?", (raw, head.id)) + with caplog.at_level("WARNING", logger="syncmill.board"): + claimed = bd.next_runnable("r1") + assert claimed is not None and claimed.id == tail.id + assert bd.get(head.id).state == WorkState.PLANNED + warnings = [r for r in caplog.records if r.levelname == "WARNING"] + assert [head.id in r.getMessage() for r in warnings] == [True] + # The goal-scoped claim takes the same path. + assert bd.next_runnable("r2", goal="g") is None finally: - bd2.close() + bd.close() + + +def test_next_runnable_still_dispatches_a_valid_dependency_list(tmp_path, caplog): + bd = Board(str(tmp_path / "b.db")) + try: + dep = bd.add("dep") + item = bd.add("item", depends_on=[dep.id]) + bd._conn.execute("UPDATE work_items SET state='done' WHERE id=?", (dep.id,)) + with caplog.at_level("WARNING", logger="syncmill.board"): + claimed = bd.next_runnable("r1") + assert claimed is not None and claimed.id == item.id + assert not [r for r in caplog.records if r.levelname == "WARNING"] + finally: + bd.close() # ── __init__ closes the connection on open failure (W7) ────────────────────── diff --git a/tests/test_git_timeouts.py b/tests/test_git_timeouts.py new file mode 100644 index 00000000..7e60ae82 --- /dev/null +++ b/tests/test_git_timeouts.py @@ -0,0 +1,147 @@ +"""User-repository git calls in project.py and cli.py are bounded. + +They run through ``worktree._run_git``, which kills git's whole process group +on timeout and reports rc 124, so a wedged git (a hung filter, fsmonitor or +credential helper) cannot hang ``init``, ``doctor``, ``start`` or a project +lookup forever. +""" + +from __future__ import annotations + +import ast +import os +import stat +import time +from pathlib import Path + +import click +import pytest + +import syncmill.worktree as worktree +from syncmill import cli, project +from syncmill.project import ProjectError + +pytestmark = pytest.mark.skipif(os.name != "posix", reason="POSIX process groups") + +_SRC = Path(__file__).resolve().parents[1] / "src" / "syncmill" + + +def _git_argv_calls(path: Path) -> list[int]: + """Lines where ``subprocess.`` is called with a git argv.""" + tree = ast.parse(path.read_text(encoding="utf-8")) + assigned: dict[str, ast.AST] = {} + for node in ast.walk(tree): + if isinstance(node, ast.Assign) and len(node.targets) == 1: + target = node.targets[0] + if isinstance(target, ast.Name): + assigned[target.id] = node.value + hits = [] + for node in ast.walk(tree): + if not ( + isinstance(node, ast.Call) + and isinstance(node.func, ast.Attribute) + and isinstance(node.func.value, ast.Name) + and node.func.value.id == "subprocess" + and node.args + ): + continue + argv = node.args[0] + if isinstance(argv, ast.Name): + argv = assigned.get(argv.id, argv) + if ( + isinstance(argv, ast.List) + and argv.elts + and isinstance(argv.elts[0], ast.Constant) + and argv.elts[0].value == "git" + ): + hits.append(node.lineno) + return hits + + +@pytest.mark.parametrize("module", ["project.py", "cli.py"]) +def test_no_unbounded_git_subprocess(module): + assert _git_argv_calls(_SRC / module) == [] + + +def test_the_guard_sees_a_git_subprocess(tmp_path): + sample = tmp_path / "sample.py" + sample.write_text( + "import subprocess\n" + "subprocess.run(['git', 'status'])\n" + "argv = ['git', 'ls-files']\n" + "subprocess.run(argv)\n" + "subprocess.run(['codex', 'login'])\n" + ) + assert _git_argv_calls(sample) == [2, 4] + + +@pytest.fixture +def wedged_git(tmp_path, monkeypatch): + """A ``git`` on PATH that starts a child and both hang; the child records + its pid so the test can prove the whole group died.""" + bin_dir = tmp_path / "bin" + bin_dir.mkdir() + pid_file = tmp_path / "child.pid" + script = bin_dir / "git" + script.write_text( + "#!/bin/sh\n" + f"sh -c 'echo $$ > \"{pid_file}\"; exec sleep 300' &\n" + "exec sleep 300\n" + ) + script.chmod(script.stat().st_mode | stat.S_IXUSR) + monkeypatch.setenv("PATH", f"{bin_dir}{os.pathsep}{os.environ['PATH']}") + monkeypatch.setattr(worktree, "_GIT_TIMEOUT_SECONDS", 1.0) + return pid_file + + +def _assert_child_died(pid_file: Path) -> None: + deadline = time.monotonic() + 5 + while not pid_file.exists() and time.monotonic() < deadline: + time.sleep(0.05) + pid = int(pid_file.read_text().strip()) + while time.monotonic() < deadline: + try: + os.kill(pid, 0) + except ProcessLookupError: + return + time.sleep(0.05) + pytest.fail(f"git's child {pid} outlived the timeout") + + +def test_git_root_reports_a_timeout_not_a_missing_repository(tmp_path, wedged_git): + start = time.monotonic() + with pytest.raises(ProjectError, match="git timed out inspecting"): + project.git_root(tmp_path) + assert time.monotonic() - start < 30 + _assert_child_died(wedged_git) + + +def test_dirty_count_fails_on_a_timeout(tmp_path, wedged_git): + with pytest.raises(click.ClickException, match="timed out"): + cli._dirty_count(tmp_path) + _assert_child_died(wedged_git) + + +def test_status_dirty_fails_on_a_timeout(tmp_path, wedged_git): + with pytest.raises(Exception, match="git status failed"): + cli.subprocess_status_dirty(tmp_path) + _assert_child_died(wedged_git) + + +def test_project_id_falls_back_when_git_times_out(git_repo, wedged_git): + """The marker question goes unanswered, so the marker is distrusted and + the path hash is used: the existing fallback, reached in bounded time.""" + import hashlib + import json + + marker = git_repo / ".syncmill" / "project.json" + marker.parent.mkdir(exist_ok=True) + marker.write_text(json.dumps({"id": "local-machine-id"}) + "\n") + with pytest.MonkeyPatch.context() as healthy: + healthy.setattr(worktree, "_GIT_TIMEOUT_SECONDS", 600.0) + healthy.setenv("PATH", os.environ["PATH"].split(os.pathsep, 1)[1]) + assert project.project_id(git_repo) == "local-machine-id" # trusted when git answers + start = time.monotonic() + path_hash = hashlib.sha256(str(git_repo.resolve()).encode()).hexdigest()[:24] + assert project.project_id(git_repo) == path_hash + assert time.monotonic() - start < 30 diff --git a/tests/test_memory_migration.py b/tests/test_memory_migration.py index ba9d38e2..bbf7e0b9 100644 --- a/tests/test_memory_migration.py +++ b/tests/test_memory_migration.py @@ -995,3 +995,34 @@ def test_apply_closes_both_adapters_when_one_close_fails(migration_env, monkeypa with pytest.raises(RuntimeError, match="close failed"): run_async(mm.apply_migration(config, migration_id)) assert sorted(closed) == ["mem0", "memtomem"] + + +def test_atomic_json_is_durable_before_and_after_it_is_published(tmp_path, monkeypatch): + """Order matters, not just the count: the data reaches the disk before the + rename, and the rename reaches it before the writer returns.""" + import stat as stat_mod + + events: list[str] = [] + real_fsync, real_replace = os.fsync, os.replace + + def fsync(fd): + mode = os.fstat(fd).st_mode + kind = "file" if stat_mod.S_ISREG(mode) else "dir" if stat_mod.S_ISDIR(mode) else "?" + if kind == "file": + # The bytes are already in the file being synced. + events.append(f"fsync-file:{os.fstat(fd).st_size > 0}") + else: + events.append(f"fsync-{kind}") + real_fsync(fd) + + def replace(src, dst): + events.append("replace") + real_replace(src, dst) + + monkeypatch.setattr(mm.os, "fsync", fsync) + monkeypatch.setattr(mm.os, "replace", replace) + target = tmp_path / "state.json" + mm._atomic_json(target, {"status": "applied"}) + assert events == ["fsync-file:True", "replace", "fsync-dir"] + assert json.loads(target.read_text()) == {"status": "applied"} + assert stat_mod.S_IMODE(target.stat().st_mode) == 0o600 diff --git a/tests/test_preflight.py b/tests/test_preflight.py index 6d31d79a..ed4e8787 100644 --- a/tests/test_preflight.py +++ b/tests/test_preflight.py @@ -587,3 +587,61 @@ def test_control_non_utf8_stderr_is_reported_as_the_exit(tmp_path): control_plan={"nodes": []}) assert out.should_block is False assert out.warnings == ["control preflight exited 1: cannot open /tmp/caf�.json"] + + +# ── output bound ──────────────────────────────────────────────────────────── + + +def _loud_toolgraph(tmp_path: Path) -> str: + """A real binary that floods both streams and then fails.""" + script = tmp_path / "toolgraph" + script.write_text( + f"#!{sys.executable}\n" + "import os, sys\n" + "chunk = b'x' * 65536\n" + "for _ in range(64):\n" + " os.write(1, chunk)\n" + " os.write(2, chunk)\n" + "os.write(2, b'\\nfinal error\\n')\n" + "sys.exit(1)\n", + encoding="utf-8", + ) + script.chmod(0o755) + return str(script) + + +@pytest.mark.parametrize("path", ["advisory", "strict", "control"]) +def test_the_runner_keeps_a_bounded_tail_of_each_stream(tmp_path, monkeypatch, path): + """4 MiB on each stream comes back as at most the tail the snippet needs.""" + import syncmill.preflight as pf + + real = pf._sync_run + kept: list[tuple[int, int]] = [] + + def spy(*args, **kwargs): + proc = real(*args, **kwargs) + kept.append((len(proc.stdout), len(proc.stderr))) + return proc + + monkeypatch.setattr(pf, "_sync_run", spy) + tool = _loud_toolgraph(tmp_path) + out_dir = tmp_path / "run" + out_dir.mkdir() + if path == "advisory": + out = run_preflight(_cfg(command=[tool]), run_id="r1", agent_ids=["codex"], + out_dir=None) + prefix = "preflight exited 1: " + elif path == "strict": + cfg = _strict_cfg(command=[tool], agent_map={"codex": "codex"}, + unavailable_policy="warn") + out = run_preflight(cfg, run_id="r1", agent_ids=["codex"], out_dir=out_dir) + prefix = "preflight exited 1: " + else: + cfg = _cfg(command=[tool], control_plan_enabled=True, + candidate_tools=["alpha::read"], control_plan_command=[tool]) + out = run_preflight(cfg, run_id="r1", agent_ids=["codex"], out_dir=None, + control_plan={"nodes": []}) + prefix = "control preflight exited 1: " + assert kept and all(o <= pf._OUTPUT_TAIL_CHARS and e <= pf._OUTPUT_TAIL_CHARS + for o, e in kept), kept + assert any(w.startswith(prefix) for w in out.warnings), out.warnings diff --git a/tests/test_project.py b/tests/test_project.py index deda36bc..90c8e0f0 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -478,7 +478,7 @@ def test_marker_is_distrusted_when_git_cannot_answer(git_repo, monkeypatch): def boom(*args, **kwargs): raise OSError("no git") - monkeypatch.setattr("syncmill.project.subprocess.run", boom) + monkeypatch.setattr("syncmill.project._run_git", boom) assert project_id(git_repo) == _path_hash_id(git_repo) @@ -651,14 +651,14 @@ def test_trackedness_survives_an_undecodable_sibling_name(git_repo, monkeypatch) b"100644 bb 0\t.syncmill/project.json\x00" ) - real_run = sp.run # captured BEFORE patching: `sp` is the patched module + from syncmill.worktree import _run_git as real_run def fake_run(argv, **kwargs): if argv[:2] == ["git", "ls-files"]: return sp.CompletedProcess(argv, 0, stdout=listing, stderr=b"") return real_run(argv, **kwargs) - monkeypatch.setattr("syncmill.project.subprocess.run", fake_run) + monkeypatch.setattr("syncmill.project._run_git", fake_run) # The marker's own path is in the listing, so it is tracked and rejected — # the point is that the undecodable sibling did not crash the read. assert project_id(git_repo) == _path_hash_id(git_repo) @@ -787,14 +787,14 @@ def test_init_refuses_to_write_when_the_index_cannot_be_read(git_repo, monkeypat import subprocess as sp _write_marker(git_repo, "local-machine-id") - real_run = sp.run # captured BEFORE patching: `sp` is the patched module + from syncmill.worktree import _run_git as real_run def fake_run(argv, **kwargs): if argv[:2] == ["git", "ls-files"]: return sp.CompletedProcess(argv, 128, stdout=b"", stderr=b"broken index") return real_run(argv, **kwargs) - monkeypatch.setattr("syncmill.project.subprocess.run", fake_run) + monkeypatch.setattr("syncmill.project._run_git", fake_run) with pytest.raises(ProjectError, match="status could not be read"): _init(git_repo) From 65b54ef8b28a0f01b7698cbf8172f5f07e9f9703 Mon Sep 17 00:00:00 2001 From: memtomem <118508877+memtomem@users.noreply.github.com> Date: Sat, 14 Nov 2026 23:15:10 +0900 Subject: [PATCH 2/2] test(board): read the skipped item's state without get() On Python 3.14 json.loads accepts the 100000-deep depends_on case, and get() renders it through _load_list, whose repr of the nested list overflows the C stack. The dispatch path under test already handles it; read the state with SQL so the test checks only that path. Co-Authored-By: Claude Opus 5.5 --- tests/test_board.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/test_board.py b/tests/test_board.py index 270bfe9b..fa66798a 100644 --- a/tests/test_board.py +++ b/tests/test_board.py @@ -1410,7 +1410,12 @@ def test_next_runnable_skips_an_unreadable_depends_on_and_claims_the_rest( with caplog.at_level("WARNING", logger="syncmill.board"): claimed = bd.next_runnable("r1") assert claimed is not None and claimed.id == tail.id - assert bd.get(head.id).state == WorkState.PLANNED + # Read raw: get() renders depends_on through _load_list, whose repr of + # a deeply nested list overflows on 3.14 (json.loads accepts it there). + state = bd._conn.execute( + "SELECT state FROM work_items WHERE id=?", (head.id,) + ).fetchone()["state"] + assert state == WorkState.PLANNED.value warnings = [r for r in caplog.records if r.levelname == "WARNING"] assert [head.id in r.getMessage() for r in warnings] == [True] # The goal-scoped claim takes the same path.