feat(loop-worktree): add advisory path locking to prevent multi-loop collisions#274
Open
KhaiTrang1995 wants to merge 1 commit into
Open
feat(loop-worktree): add advisory path locking to prevent multi-loop collisions#274KhaiTrang1995 wants to merge 1 commit into
KhaiTrang1995 wants to merge 1 commit into
Conversation
…collisions Independently-scheduled loops can race on the same files with no code to stop them (see stories/multi-loop-collision.md and dependency-vs-ci-sweeper-collision.md). Adds `lock`/`unlock`/`locks` subcommands: an owner locks a set of path globs, another owner's overlapping lock request fails clearly instead of racing silently. Advisory only, not enforced by `create` -- paired by convention in the control script, same as loop-context/loop-worktree already are. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Contributor
|
Thanks @KhaiTrang1995 for contributing a production story — visible, reviewable PRs like this grow the reference for everyone. What happens next
More ways to help — loop-engineering maintainers |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Independently-scheduled loops (e.g. a
ci-sweeperloop every 15m and adependency-sweeperloop every 6h) can race on the same files with nothingin code to stop them -- see
stories/multi-loop-collision.mdandstories/dependency-vs-ci-sweeper-collision.md.docs/multi-loop.mdalready documents an
acting_onconvention for this, but it's prose-only,hand-checked by each loop's control script.
What
Adds three subcommands to
loop-worktree:lock --paths <glob1,glob2,...> --owner <name> [--ttl <duration>]--acquires an advisory lock; fails clearly if another (non-expired) owner
already holds an overlapping path.
unlock --owner <name>-- releases that owner's lock (no-op if it didn'thold one).
locks [--sweep] [--force] [--json]-- lists active locks;--sweepreports expired ones (report-only by default,
--forcedeletes them,mirroring
gc's existing convention).Deliberately advisory, not enforced by
createitself -- paired byconvention in the control script (
lockbeforecreate,unlockaftercleanup), the same wayloop-context --checkandloop-worktree mark --status escalatedare already paired without a runtime dependency betweenthe two tools.
Locking is keyed on path globs rather than run ids, so it also catches
collisions across different patterns (e.g. CI Sweeper and Dependency
Sweeper both touching
package.json), not just within one. Path overlap iscompared segment-by-segment (a wildcard segment matches anything at that
position) rather than as a raw string prefix, so
docs/apidoesn'tfalse-positive against
docs/apidocs.md.Also updates
docs/multi-loop.mdto cross-reference this as the mechanicalform of the existing
acting_onconvention.Why this shape
hard sandboxing -- a loop that skips the
lockcall isn't physicallyblocked.
--owneris restricted to a safe charset (letters/digits/./_/-)since it names the lock file directly.
exclusive-create mutex file so two racing
lockcalls can't both pass theoverlap check before either writes -- otherwise the feature would fail at
exactly the race it exists to prevent.
file, rather than crashing or silently ignoring a lock that might still be
real.
Tested
tools/loop-worktree: 28/28 passing after a fully clean rebuild (wipednode_modules/dist, reinstalled, rebuilt). This went through anindependent review pass (a separate agent given only the diff and task
description, not implementation rationale) before pushing, which caught and
led to fixes for: a TOCTOU race between the overlap check and the write
(now serialized via the mutex file), a corrupt-lock-file crash (now a clear
error), a path-segment-boundary bug in the original overlap heuristic (raw
string-prefix instead of segment-aware), and missing
--ownervalidation(path traversal in the lock filename). All four are covered by new
regression tests, including a concurrent-lock test that fires 3 racing
lockPathscalls on the same path and asserts exactly one succeeds.19 new tests total (11 unit + 8 CLI integration) plus the 8 pre-existing
loop-worktreetests, all passing unmodified. Version 1.0.0 -> 1.1.0.