Revision: 1 Last modified: 2026-12-04T00:00:00Z
Library: scripts/pr/lib/lock.sh (sourced by scripts/pr/lib/steps.sh; never executed on its own).
Engine guide: docs/scripts/pr_deliver.md. Manual: docs/manuals/pr_delivery_engine_manual.md.
One exclusive lock per (repository slug, task id), taken BEFORE the engine mutates anything. Two agents
delivering the same task at the same time would otherwise create two branches or two pull requests for one
task id — the duplicate-primitive mess the engine exists to prevent. The second run exits 7 LOCKED
instead of interleaving.
The lock is a DIRECTORY, <journal dir>/.locks/<repo>:<task>.lock/, holding two files: pid (the holder)
and key (the locked key). mkdir is the atomic acquisition primitive — it either wins or it does not,
with no read-then-write window.
bash4+,mkdir,kill,cat.- The journal directory must already resolve (see
journal.md); the lock lives under it. - Nothing else: no provider, no credential, no network.
. "$PR_SELF_DIR/lib/lock.sh"
pr_lock_acquire "$(pr_cfg_get pr_repository):$PR_TASK" || exit 7
trap 'pr_lock_release' EXIT # what the engine does: release on every exit pathWaiting instead of failing fast:
PR_LOCK_WAIT_S=30 bash scripts/pr/pr_deliver.sh create --task CON-641 --roadmap-item CON-641Inspecting who holds a task (the same path status implies):
$ cat "$HOME/.local/state/helix-pr-delivery/owner__repo/.locks/owner/repo:CON-641.lock/pid"
4711- A held lock with a live holder is never stolen.
pr_lock_holderreadspid;kill -0proves the process is alive; the caller then waits up toPR_LOCK_WAIT_S(default 0, fail fast) and exits 7. - A provably dead holder is reaped and the acquisition retried once. Liveness is proven with
kill -0, never inferred from the lock's age or mtime — the same discipline as §11.4.180. - A lock directory with no readable
pidis treated as HELD (conservative default). An unreadable lock is not evidence that nobody holds it. pr_lock_releaseonly removes a lock this process owns: it compares the recordedpidwith$$, so a reaped-and-retaken lock cannot be released by the process that lost it. Releasing a lock you do not hold is a no-op, which makes theEXITtrap safe on every path.- The key contains a slash (
owner/repo:TASK), so the lock path nests one directory deeper.mkdir -pon the parent makes that harmless. - Two different tasks never block each other — the key includes the task id.
pr_lock_path KEY=$(pr_journal_dir)/.locks/<key>.lock.pr_lock_acquire KEYloops:mkdir→ on success writepidandkey, setPR_LOCK_HELD, return 0; on failure read the holder → alive and budget exhausted ⇒ return 7; alive and budget left ⇒sleep 1and retry; dead ⇒rm -rfthe lock directory, print a reaping note to stderr, and retry; no readablepid⇒ return 7.pr_lock_release [KEY]defaults toPR_LOCK_HELD, removes the directory only whenpidequals$$, and clearsPR_LOCK_HELD.- The engine calls
pr_lock_acquireincreate,reviewandmerge, and installstrap … EXITin the same block — an interrupted run therefore does not leave the task permanently locked unless its process was killed without the trap firing, in which case the dead-holder reap path recovers it.
journal.md— supplies the directory the lock lives in.steps.md— the three subcommands that take the lock.pr_deliver.md— exit code 7 and thelock_held:<repo>:<task>reason.
2026-12-04 — read against scripts/pr/lib/lock.sh (78 lines). The PR_LOCK_WAIT_S default (0) and the
reap-then-retry-once behaviour are the code's, not an assumption.