Skip to content

feat(witan): add witan lock run, a host-local named lock for commands - #744

Open
blarghmatey wants to merge 5 commits into
mainfrom
witan-lock-run
Open

blarghmatey wants to merge 5 commits into
mainfrom
witan-lock-run

Conversation

@blarghmatey

@blarghmatey blarghmatey commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

What are the relevant tickets?

N/A

Description (What does it do?)

Two agent workers on one host running a repo's tests against the same Docker setup collide with each other. That contention never crosses hosts, so the lock belongs on the machine rather than in the graph. This adds witan lock run <name> -- <cmd>, which runs the command while holding an exclusive host-local lock named <name> (e.g. witan lock run tests:<repo-url> --timeout 600 -- just test).

Implementation details
  • The lock is an flock (via witan_core._filelock) on a file under ${XDG_STATE_HOME:-~/.local/state}/witan/locks. The file name is a sanitized stem of the lock name plus a digest of the full name, so names with : and / work and two names that sanitize to the same stem don't share a lock.
  • The kernel drops the lock when the last descriptor closes, so a holder killed by any means (SIGKILL included) can't leave it held.
  • The command's exit code passes through (128 + N if a signal killed it). --timeout gives up with exit code 75 (EX_TEMPFAIL) without running the command. A command that exits 75 itself passes through unchanged, so the exit code alone does not prove a timeout; only the timeout message on stderr does.
  • SIGINT, SIGTERM and SIGHUP are forwarded to the command, and witan keeps waiting so the lock covers the command's shutdown too. A Ctrl-C the terminal already delivered to the command's process group is not forwarded a second time, because a second interrupt cuts short the teardown the first one started. A signal witan was started ignoring (nohup) stays ignored for the command.
  • The command inherits the lock descriptor, as flock(1) does without --close, so killing only witan leaves the lock held until the orphaned command exits rather than letting the next worker in while the resource is still in use.

How can this be tested?

just test-witan-council runs the new mcp/servers/witan/tests/test_cli_lock.py, which covers two runs of one name serializing, different names not waiting on each other, exit code passthrough, the timeout, an interrupted wait, forwarded SIGTERM/SIGINT freeing the lock, an ignored SIGHUP staying ignored, a SIGKILLed holder releasing the lock, and the lock staying with a command that outlives a killed witan.

To try it by hand, run witan lock run demo -- sleep 30 in one terminal and witan lock run demo --timeout 2 -- echo hi; echo $? in another. The second gives up after two seconds of waiting and prints 75.

https://claude.ai/code/session_01FGZebERkUorXxXDu66F91m

Two workers on one host running a repo's tests against the same Docker
setup collide. The contention never crosses hosts, so the lock belongs on
the machine, not in the graph.

witan lock run <name> -- <cmd> takes an exclusive flock (witan_core's
_filelock) on a file under ${XDG_STATE_HOME:-~/.local/state}/witan/locks,
named by a safe stem of <name> plus a digest so `tests:<repo-url>` works
and names that sanitize alike stay apart. The command's exit code passes
through, SIGINT/SIGTERM/SIGHUP are forwarded, and --timeout gives up with
exit code 75 without running the command.

The command inherits the lock descriptor, as flock(1) does by default, so
killing only witan leaves the lock with a command still using the
resource rather than letting the next worker in. A Ctrl-C the terminal
already delivered to the command is not forwarded a second time, because
a second interrupt cuts short the teardown the first one started, and a
signal witan was started ignoring (nohup) stays ignored for the command.

Claude-Session: https://claude.ai/code/session_01FGZebERkUorXxXDu66F91m
Copilot AI balanced review requested due to automatic review settings November 25, 2026 13:59

@agent-optibot agent-optibot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

Adds host-local named locks for serializing commands that share machine resources.

  • Adds: host-local flock execution through witan lock run <name> -- <cmd>.
  • Adds: sanitized lock paths with digests under the XDG witan state directory.
  • Adds: timeout handling, signal forwarding, and command exit-code passthrough for lock runs.
Details for reviewers

Core files: mcp/servers/witan/witan/cli/lock.py, mcp/servers/witan/witan/cli/__init__.py

More changes

  • Changes: the CLI reference to document witan lock run and its options. (cli.md)
  • Adds: release notes describing host-local locks and timeout behavior. (20261125_064110_blarghmatey_witan_lock_run.md)
  • Adds: process-level tests for serialization, signals, timeouts, and orphaned command locks. (test_cli_lock.py)

Unchanged

  • Commands keep their own exit codes unless a signal or lock timeout determines the result.

Summary of 47bbefa · React 👍 or 👎 on my comments to tune what I raise · Commands · Review config

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@agent-optibot agent-optibot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Status: Needs Changes 🔧

Blocking Issues:

  • Do not explicitly unlock a POSIX descriptor that a surviving subprocess still holds.
  • Forward PID-directed SIGINT even when witan owns the foreground terminal.
  • Do not offer Windows lock runs without preserving mutual exclusion after wrapper termination.
  • Enforce the lock deadline before accepting a retry.
  • Distinguish lock timeouts from commands that themselves exit 75.

📌 Reviewed up to the latest commit · ⏱️ Review completed in 8 minutes

  • 🔄 re-review this PR

Comment thread mcp/servers/witan/witan/cli/lock.py Resolved
Comment thread mcp/servers/witan/witan/cli/lock.py Resolved
Comment thread mcp/servers/witan/witan/cli/lock.py Resolved
Comment thread mcp/servers/witan/witan/cli/lock.py Resolved
Comment thread mcp/servers/witan/witan/cli/lock.py Resolved
Comment thread mcp/servers/witan/witan/cli/lock.py Resolved
flock(LOCK_UN) on any duplicate of the descriptor frees the lock for every
holder, so witan's explicit unlock after the command exited freed it under a
background child that still held the inherited copy. On POSIX witan now only
closes its own descriptor, as flock(1) does, and the kernel frees the lock
after the last holder closes.

Claude-Session: https://claude.ai/code/session_01FGZebERkUorXxXDu66F91m
The wait slept a full poll interval past the deadline before its last
attempt, so --timeout could start the command up to 0.1s late. The sleep is
now capped at the time remaining.

Claude-Session: https://claude.ai/code/session_01FGZebERkUorXxXDu66F91m
A command that exits 75 itself is indistinguishable by exit code from a lock
timeout, and the changelog said the kernel frees the lock when witan exits,
which the inherited descriptor deliberately prevents while the command or a
process it started still runs.

Claude-Session: https://claude.ai/code/session_01FGZebERkUorXxXDu66F91m
A SIGINT that arrives while witan is in the terminal's foreground process
group is taken as a Ctrl-C the command already got and is not forwarded, so a
runner has to stop it with SIGTERM. On Windows the command cannot inherit the
lock, so killing only witan frees it while the command may still run.

Claude-Session: https://claude.ai/code/session_01FGZebERkUorXxXDu66F91m

This branch has not been deployed

No deployments
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