Skip to content

feat(skill-lock): per-skill write lock for the batch skills that post - #1931

Merged
potiuk merged 9 commits into
mainfrom
batch-skill-write-lock
Nov 1, 2026
Merged

potiuk merged 9 commits into
mainfrom
batch-skill-write-lock

Conversation

@justinmclean

Copy link
Copy Markdown
Member

Summary

  • Two overlapping runs of one batch skill over one repository post the same triage comment, stale nudge or label twice, which is exactly what makes contributors distrust the bot. The read and classify phase is side-effect free and may overlap; the write phase must not.
  • New tools/skill-lock: a per-skill, per-repository write lock (acquire / release / renew / status / list, one JSON object per call; exit 0 acquired, 3 already_running, 2 error). Atomic mkdir plus an owner record with a lease, in the personal config layer (run/locks/), never in the working tree. Stale (expired lease, or dead owner pid on the same host) is taken over; a lock with no readable owner is respected.
  • A shared prose block write-lock is propagated by check-shared-blocks.py into the write step of the six batch skills that post, label or close: issue-triage, security-issue-triage, issue-stale-sweep, pr-stale-sweep, pr-management-triage, good-first-issue-sweep. Acquire before the first write; on already_running post nothing and show the holder (not a sweep failure); release after the recap and on the failed-post path. Read-only skills take no lock.

Type of change

  • Skill change (.claude/skills/<name>/) — prose only at the write step; no classification behaviour changed, so no eval fixture is added
  • Python package (tools/*/ with pyproject.toml)
  • Documentation (docs/, README.md, CONTRIBUTING.md)
  • Other: spec tools/spec-loop/specs/batch-write-lock.md

Test plan

  • uv run --directory tools/skill-lock --group dev pytest: 14 passed (acquire / contend / release, scope isolation, expired lease takeover, dead-pid takeover, live pid respected, owner-less lock respected, wrong token refused unless forced, renew, CLI exit codes, adopted vs unadopted lock location, non-directory lock path is an error)
  • ruff check, ruff format --check, mypy clean for tools/skill-lock
  • tools/setup-preflight/tests/test_layers.py passes with the new layers.py copy registered
  • python3 tools/dev/check-shared-blocks.py and skill-surface-hash.py report everything in sync
  • Apache RAT 0.18 run locally with the workflow's exact arguments: 0 unapproved
  • uv run sandbox-lint OK
  • prek run --files <all changed files> passes every hook except skill-token-count, which cannot download its tokenizer in the authoring sandbox; measured_tokens on the three touched SKILL.md files was restamped locally before push with uv run --project tools/skill-token-count skill-token-count --write
  • A live sweep against a repository with two concurrent runs (not possible in the authoring sandbox)

RFC-AI-0004 compliance

  • HITL — no new mutation; the lock only gates existing confirmed writes, and only the maintainer clears a held lock by force
  • Sandbox — no network, no credentials; the lock lives in the personal layer the sandbox already allows
  • Vendor neutrality — <framework> and <upstream> placeholders throughout; the lock is forge-agnostic
  • Write-access discipline — a failed post still stops and surfaces; the lock never retries a write

Linked issues

Related: the batch failure budget proposal (#1930) covers the per-item failure semantics this lock sits beside.

Notes for reviewers (optional)

  • Liveness is a lease (default two hours) rather than a pid, because acquire runs as a short uv run and the owning skill run is an agent session with no stable pid. --owner-pid adds the sharper check for shell callers.
  • The lock is advisory for a caller that bypasses the skill prose and posts with gh directly. A deterministic agent-guard check (refuse gh issue comment / gh pr comment from a batch skill without a held lock) would make it enforced; listed under Known gaps in the spec.
  • substrate:action-guard was chosen for the capability row: a deterministic pre-write guard against a concurrent run.
  • Generative-AI disclosure: authored with Claude Code (Fable 5.1) under maintainer direction; commits carry the Generated-by: trailer.

@github-actions github-actions Bot added family:pr-management pr-management-* skills family:security security-* skills family:tools tools/* family:docs Docs, MISSION.md, READMEs family:ci .github workflows, prek, validators family:issue issue-* skills capability:triage Sweep + classify + propose disposition capability:review Deep per-item code review or contributor mentoring family:mentoring mentoring skills substrate:framework-dev Tool substrate: build / validate / eval the framework itself substrate:setup Tool substrate: adopter-side setup state resolved at runtime (lock parsing, floors, fingerprints) substrate:sandbox Tool substrate: agent isolation, egress control, settings audit labels Oct 31, 2026

@potiuk potiuk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The lock is the right shape, small and stdlib-only, and kept out of the tree. But as written it does not yet stop the duplicate posts it exists for: two concurrent takeovers of a stale lock both succeed, and two runs whose write phases follow one another still post the same proposals.

the second exits 3 with status: already_running (tools/skill-lock/src/skill_lock/__init__.py:203)

skill-lock acquire twice for the same skill and repository: the second exits 3 with status: already_running and the first holder in the result; the lock is unchanged.

if reason is not None and took_over is None:
    took_over = reason
    _remove_lock(lock)
    continue

Two runs that hit the same stale lock both read the expired owner. A removes it, re-creates the directory and returns acquired; B then calls _remove_lock(lock) on the same path, which deletes A's live lock, and B acquires too. Both hold the lock and both post. Nothing re-checks that the directory still belongs to the stale owner before removing it.

Reproduced against this file by interleaving two acquire() calls over a planted expired owner: both return acquired, and only B's token is on disk.

Two sweeps queued behind a crashed run (a scheduled one and a manual one, after the lease expires) is exactly when this happens. Taking over by os.rename(lock, <lock>.stale-<token>) makes the takeover atomic: only one contender's rename succeeds, and the winner can check the tombstone's owner.json token against the stale one it read before deleting it. A test that interleaves two takeovers would pin it.

Suggested fix: Take over with an atomic rename to a unique tombstone, check the tombstone's owner token matches the stale owner read earlier, then delete it; add an interleaved-takeover test.

Flagged by both the primary and adversarial reviewers.

both post the same proposal comment on the same issue (tools/dev/blocks/write-lock.md:15)

When two runs of issue-triage over the same repository overlap, whether a maintainer started one while a scheduled one was still posting or two maintainers ran it at once, both post the same proposal comment on the same issue. The lock stops the second run at the door of its write step, tells the maintainer who holds it, and lets the first finish.

- Exit `0` (`status: acquired`): proceed. Keep the `token` from the JSON
  result for the release below.

The lock only stops a second run whose write step starts while the first is still writing. If run B classified before run A posted, and reaches its write step after A has released, B acquires a free lock and posts the same proposals. A slower run, or one waiting on the maintainer's confirmation, is enough. Nothing after acquire refreshes the items B classified against the state A just wrote. For example, security-issue-triage checks for unanswered proposals in Step 2 but does not repeat the check at Step 6.

The block could require it: once the lock is held, re-read each item just before writing to it, and skip any that already carry this skill's marker (a triage comment or label newer than B's read). The lock then guarantees that check runs on current state.

Suggested fix: After acquired, re-read each item immediately before its write and skip it when this skill's marker already landed since the classify read; say so in the recap.

Flagged by both the primary and adversarial reviewers.

Smaller observations

  • See inline comments on tools/skill-lock/src/skill_lock/layers.py:175, tools/dev/blocks/write-lock.md:12, tools/skill-lock/src/skill_lock/__init__.py:269.

This review was drafted by an AI-assisted tool and
confirmed by an Apache Magpie maintainer. After you've
addressed the points above and pushed an update, an Apache Magpie
maintainer — a real person — will take the next look
at the PR. The findings cite the project's review criteria;
if you think one of them is mis-applied, please reply on the
PR and a maintainer will weigh in.

More on how Apache Magpie handles maintainer review:
Contributing guide.

Comment thread tools/dev/blocks/write-lock.md Resolved
Comment thread tools/dev/blocks/write-lock.md Resolved
@justinmclean

Copy link
Copy Markdown
Member Author

Thanks Jarek. Fixed: the stale-lock takeover is now an atomic rename, renew takes --token, and the block re-reads each item just before writing. The spec now says the lock covers one clone, with other clones caught by the re-read. Keying locks on the git common dir would mean writing under .git/ in an adopted project, which the spec rules out, so I've added worktrees as a known gap. The permission rule touches the sandbox settings, the sandbox lint and setup, so I'd prefer a separate PR for it.

claude and others added 9 commits November 1, 2026 08:10
Two overlapping runs of one batch skill over one repository post the same
triage comment, stale nudge or label twice, which is exactly what makes
contributors distrust the bot. The read and classify phase is side-effect
free and may overlap with anything; the write phase must not.

- New tools/skill-lock: `skill-lock acquire | release | renew | status |
  list`, one JSON object per call, exit 0 acquired, 3 already_running
  (with the holder record and a --force release hint), 2 error. Scope is
  <skill>@<repo>, so different skills and different repositories never
  contend. Atomic mkdir plus an owner record in the personal config
  layer's run/locks/, never in the working tree. Liveness is a lease
  (default two hours; `renew` extends it) because the acquiring process is
  a short `uv run` and the owning skill run has no stable pid; a dead
  --owner-pid on the same host also counts as stale. A lock with no
  readable owner is a racing starter and is respected. Stdlib only; the
  layers.py copy is registered with the shared layers test.
- New shared block tools/dev/blocks/write-lock.md, propagated into the
  write step of issue-triage, security-issue-triage, issue-stale-sweep,
  pr-stale-sweep, pr-management-triage and good-first-issue-sweep:
  acquire before the first write; on already_running post nothing, show
  the holder and say it is not a sweep failure; release after the recap
  and on the failed-post path. Read-only skills take no lock.
- Spec tools/spec-loop/specs/batch-write-lock.md, capability row in
  docs/labels-and-capabilities.md (substrate:action-guard), workspace
  member, labeler and vendor-neutrality regenerated.

Generated-by: Claude Code (Fable 5.1)
On a marketplace install `<framework>/tools/` is materialised from the
magpie-security-tools plugin, which carried no copy of skill-lock, so the
write-lock block in security-issue-triage (and every other batch skill)
would have called a tool that was not there. Add it to SECURITY_TOOLS,
regenerate the plugin copies, and let the check-family-plugins hook
re-fire on skill-lock changes so the copies stay in sync.

Also add the doctoc table of contents and leading SPDX header the README
hooks require; their absence failed the prek job.

Generated-by: Claude Code (Opus 5.5)
The previous fix shipped skill-lock inside magpie-security-tools, so the
write-lock block in the issue, mentoring and pr-management families only
worked for adopters who also installed the security family; on any other
marketplace install the tool was missing.

Ship it in magpie-vetted-ops instead, which is on every install. Its
session hook now links ~/.claude/magpie/skill-lock beside
~/.claude/magpie/vetted-ops, each link independent of the other, and the
block runs `uvx --from ~/.claude/magpie/skill-lock skill-lock` because
the plugin cache is read-only. It stays sandboxed: sandboxed uv has a
writable cache and pypi.org on its allowlist, and the tool touches only
the lock directory.

A new test runs the hook against the real plugin and fails if any
~/.claude/magpie/<tool> path a skill names is not linked to a tool the
plugin carries.

Generated-by: Claude Code (Opus 5.5)
Review fixes:

- Two runs that found the same stale lock could both acquire it: A
  removed and re-created the lock, then B's _remove_lock deleted A's
  live lock. A takeover now renames the stale lock to a unique tombstone
  (only one rename succeeds) and deletes it only if it still holds the
  owner judged stale; otherwise the live lock is put back and the loser
  reports already_running.
- renew rewrote the lease for whoever held the lock, so a run whose lease
  had been taken over could keep the new holder's lock alive. renew now
  takes --token and refuses a non-matching holder, as release does.

Tests cover the interleaved takeover and a renew from a taken-over run.

Generated-by: Claude Code (Opus 5.5)
…to one clone

Review follow-ups:

- The lock only stopped a second run whose write step overlapped the
  first. A run that classified before another posted could reach its
  write step after that run released and post the same proposals. The
  write-lock block now has each skill re-read an item just before writing
  to it and skip one that already carries the skill's marker, listing it
  in the recap.
- The spec claimed to cover two maintainers running at once, but the lock
  is local. It now says the lock serialises one clone, and that other
  clones and machines are caught by the re-read. Linked worktrees with
  their own personal layer are a new Known gap.

Generated-by: Claude Code (Opus 5.5)
…overlap

Review fixups:

- The write-lock block and the batch-write-lock spec said runs from
  another clone or machine "are caught by" the re-read before each write.
  The re-read is itself check-then-post, so two runs that re-read at the
  same moment both post; it narrows the window, it does not close it.
  Reworded in the block (propagated to its six copies) and the spec.
- Record the three-starter window in the stale-lock takeover as a Known
  gap, with the populated-temp-dir acquire that would close it.

Generated-by: Claude Opus 5.5
@potiuk
potiuk force-pushed the batch-skill-write-lock branch from 91c9989 to fc32344 Compare November 1, 2026 07:12

@potiuk potiuk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks — the double-takeover and renew-after-takeover fixes hold up and are pinned by tests, and the re-read plus one-clone scoping answer the overlap concern. I rebased the branch onto main and pushed one small fixup commit, fc32344 (please pull before pushing again):

  • tools/dev/blocks/write-lock.md (and its six propagated copies) and tools/spec-loop/specs/batch-write-lock.md: "caught by the re-read" → "narrowed, not prevented, by the re-read". The re-read is itself check-then-post, so two runs re-reading at the same moment both post.
  • tools/spec-loop/specs/batch-write-lock.md Known gaps: recorded the three-starter window in the stale-lock takeover (a third run creating the lock path between the move-aside and the put-back makes _take_over delete the live lock), with the populated-temp-dir acquire that would close it.

Accepting the deferral of the skill-lock allow rule; please open a tracking issue for it, since each sweep pays two extra permission prompts until it lands.

Before skill-lock gets its sandbox exclusion (the tracking issue above), _remove_lock must refuse a symlinked lock dir and unlink only the known owner files — is_dir()/iterdir() follow a planted symlink and would delete the target's files.


This review was drafted by an AI-assisted tool and
confirmed by an Apache Magpie maintainer. The maintainer
approving this PR has read the findings and signed off. If
something feels off, please reply on the PR and a maintainer
will follow up.

More on how Apache Magpie handles maintainer review:
Contributing guide.

@potiuk
potiuk merged commit 5520cf7 into main Nov 1, 2026
20 checks passed
@potiuk
potiuk deleted the batch-skill-write-lock branch November 1, 2026 07:19
@github-actions github-actions Bot added the substrate:action-guard Tool substrate: deterministic pre-tool-use command guards label Nov 1, 2026
potiuk added a commit that referenced this pull request Nov 1, 2026
…ly real candidates

Review fixups, after moving the change onto main (#1930's 403 rule and
acceptance criterion 6 kept; the six skills restamped over the write-lock
block #1931 added):

- pr-stale-sweep's recap is built in code and ignored the new failed and
  unfinished fields, so a PR the sweep recorded as failed and carried on
  past disappeared from the final recap the skill prints as-is. The recap
  now leads with "N PR(s) failed and M not reached", lists each failed PR
  with its reason and each unreached one, and names the rerun selector.
- The sweep sized the budget on, and took unfinished from, every loaded PR,
  so drafts, bot PRs and PRs outside the label or the explicit numbers
  landed in the rerun selector. It now filters first and budgets only the
  PRs it would classify.
- The shared block pointed every skill at issue-tracker-config.md; it now
  names the file each family already reads (pr-management-config.md for
  the PR skills, project.md for security), which also drops the two config
  table rows that claimed pr-stale-sweep and security issue-triage read
  issue-tracker-config.md.
- The eval README lists the new item-failure-budget suite per skill, and
  the overview marks the spec experimental with step 1 shipped.

Generated-by: Claude Opus 5.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

capability:review Deep per-item code review or contributor mentoring capability:triage Sweep + classify + propose disposition family:ci .github workflows, prek, validators family:docs Docs, MISSION.md, READMEs family:issue issue-* skills family:mentoring mentoring skills family:pr-management pr-management-* skills family:security security-* skills family:tools tools/* substrate:action-guard Tool substrate: deterministic pre-tool-use command guards substrate:framework-dev Tool substrate: build / validate / eval the framework itself substrate:sandbox Tool substrate: agent isolation, egress control, settings audit substrate:setup Tool substrate: adopter-side setup state resolved at runtime (lock parsing, floors, fingerprints)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants