Team mode's second half. A board-made move already commits itself (18); now that commit publishes, every board pulls on a beat, and a card two boards move at once resolves the way git resolves everything else — the push race is the concurrency control. - core/sync.py: push is event-driven (a new state.COMMIT_HOOKS registry fires it from taskfiles, so taskfiles stays left of everything that reacts to it); pull is a beat that fast-forwards, or replays this board's own commits on top when the two diverged. A replay that conflicts on a task file drops the local move — origin is the linearizer — and toasts who took the card. - The piggyback guard stands in front of every push and every replay: each local-ahead commit on main must be `board: `-prefixed, so a human's unpushed work is never published as a side effect of a card moving. Uncommitted changes, a checkout off main and an unreachable origin all stall or degrade rather than risk anything, each narrated once instead of once per beat. - watch.py names the commit author instead of "disk" for moves a pull brought, via the arrivals sync files for it; its narration moved out of the loop into narrate() so it can be tested directly. - The board grows a sync chip that appears only when sync stops converging, and the SSE stream grows a toast type so the server can say something to the person, not just to the ticker. Gate off (the default) means no fetch, no push, no thread, no change. Verified with tests/test_boards_sync.py: two real clones of a real bare upstream race each other through every case above.
74 lines
2.5 KiB
Python
74 lines
2.5 KiB
Python
"""Disk watcher: the directories are the source of truth, so poll and narrate.
|
|
|
|
Catches moves the HTTP API never saw — a file dragged by hand, an agent,
|
|
another tool, or a pull from origin/main — and attributes them via the
|
|
expectations registered in state and the arrivals registered by sync.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import time
|
|
|
|
import config
|
|
import github
|
|
import state
|
|
import sync
|
|
|
|
|
|
def _board_sig() -> dict[str, set[str]]:
|
|
sig = {}
|
|
for slug in config.STAGE_DIRS:
|
|
directory = config.TASKS / slug
|
|
sig[slug] = {p.name for p in directory.glob("*.md")} if directory.is_dir() else set()
|
|
return sig
|
|
|
|
|
|
def _actor(filename: str, stage: str) -> str:
|
|
"""Who did this. A move this board made is claimed from the
|
|
expectations; one a pull brought carries its commit author's name; a
|
|
plain mv on this disk is nobody in particular."""
|
|
actor = state.claim_expected(filename, stage)
|
|
if actor == "disk":
|
|
return sync.arrived_actor(filename) or "disk"
|
|
return actor
|
|
|
|
|
|
def narrate(prev: dict[str, set[str]], cur: dict[str, set[str]]) -> None:
|
|
"""Two board signatures → the events between them."""
|
|
prev_loc = {f: s for s, files in prev.items() for f in files}
|
|
cur_loc = {f: s for s, files in cur.items() for f in files}
|
|
for f, stage in sorted(cur_loc.items()):
|
|
if f in prev_loc and prev_loc[f] != stage:
|
|
actor = _actor(f, stage)
|
|
state.record_board_event({
|
|
"kind": "move", "file": f, "from": prev_loc[f], "to": stage,
|
|
"actor": actor,
|
|
"summary": f"{f} moved {prev_loc[f]} → {stage} ({actor})",
|
|
})
|
|
if stage == "review":
|
|
# a card entering review with a work branch gets a PR
|
|
github.open_pr_async(f)
|
|
elif f not in prev_loc:
|
|
actor = _actor(f, stage)
|
|
state.record_board_event({
|
|
"kind": "new", "file": f, "to": stage, "actor": actor,
|
|
"summary": f"{f} appeared in {stage}/"
|
|
+ (f" ({actor})" if actor != "disk" else ""),
|
|
})
|
|
|
|
|
|
def watcher(interval: float | None = None) -> None:
|
|
interval = config.WATCH_INTERVAL if interval is None else interval
|
|
prev = _board_sig()
|
|
while True:
|
|
time.sleep(interval)
|
|
try:
|
|
cur = _board_sig()
|
|
except OSError:
|
|
continue
|
|
if cur == prev:
|
|
continue
|
|
narrate(prev, cur)
|
|
prev = cur
|
|
state.broadcast({"type": "board"})
|