agent-lock 3.1.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitleaks.toml +23 -0
- data/AGENTS.md +11 -17
- data/CHANGELOG.md +124 -3
- data/README.md +496 -100
- data/Rakefile +13 -6
- data/docs/badges/coverage_badge.svg +21 -0
- data/docs/badges/yardoc.svg +1 -0
- data/docs/img/alock-sequence.avif +0 -0
- data/docs/img/alock-sequence.md +29 -0
- data/docs/img/alock-sequence.png +0 -0
- data/lefthook.yml +39 -0
- data/lib/agent/lock/cli/commands/acquire.rb +25 -19
- data/lib/agent/lock/cli/commands/base.rb +59 -23
- data/lib/agent/lock/cli/commands/break.rb +12 -7
- data/lib/agent/lock/cli/commands/check.rb +10 -9
- data/lib/agent/lock/cli/commands/completion.rb +15 -32
- data/lib/agent/lock/cli/commands/list.rb +46 -37
- data/lib/agent/lock/cli/commands/listing.rb +170 -0
- data/lib/agent/lock/cli/commands/mine.rb +7 -5
- data/lib/agent/lock/cli/commands/note.rb +13 -9
- data/lib/agent/lock/cli/commands/release.rb +10 -7
- data/lib/agent/lock/cli/commands/release_all.rb +7 -5
- data/lib/agent/lock/cli/commands/resume.rb +13 -7
- data/lib/agent/lock/cli/commands/show.rb +172 -0
- data/lib/agent/lock/cli/commands/skills.rb +28 -19
- data/lib/agent/lock/cli/commands/switch_backend.rb +205 -0
- data/lib/agent/lock/cli/commands/tree_command.rb +72 -0
- data/lib/agent/lock/cli/commands/version.rb +2 -1
- data/lib/agent/lock/cli/commands/whoami.rb +7 -3
- data/lib/agent/lock/cli.rb +63 -19
- data/lib/agent/lock/error.rb +52 -1
- data/lib/agent/lock/facade/async.rb +83 -0
- data/lib/agent/lock/facade/backend.rb +133 -0
- data/lib/agent/lock/facade/memo.rb +30 -0
- data/lib/agent/lock/facade/refusal.rb +78 -0
- data/lib/agent/lock/facade/session.rb +142 -0
- data/lib/agent/lock/facade/strategies.rb +36 -0
- data/lib/agent/lock/facade.rb +369 -0
- data/lib/agent/lock/freeze.rb +59 -5
- data/lib/agent/lock/identity.rb +63 -5
- data/lib/agent/lock/launcher.rb +17 -1
- data/lib/agent/lock/manager/enforcement.rb +98 -0
- data/lib/agent/lock/manager/switch.rb +91 -0
- data/lib/agent/lock/manager/thaw.rb +68 -0
- data/lib/agent/lock/manager.rb +176 -79
- data/lib/agent/lock/process_info/legacy_start.rb +60 -0
- data/lib/agent/lock/process_info.rb +42 -11
- data/lib/agent/lock/record.rb +124 -10
- data/lib/agent/lock/scope.rb +49 -4
- data/lib/agent/lock/skill.rb +14 -10
- data/lib/agent/lock/store/backend_name.rb +62 -0
- data/lib/agent/lock/store/file_system_store.rb +20 -1
- data/lib/agent/lock/store/redis_index.rb +179 -0
- data/lib/agent/lock/store/redis_store.rb +191 -33
- data/lib/agent/lock/store.rb +180 -19
- data/lib/agent/lock/text.rb +62 -0
- data/lib/agent/lock/tree.rb +143 -16
- data/lib/agent/lock/version.rb +2 -1
- data/lib/agent/lock.rb +3 -0
- data/skills/agent-lock/SKILL.md +75 -10
- metadata +41 -22
- data/justfile +0 -127
- data/sig/agent/lock.rbs +0 -6
data/lib/agent/lock/launcher.rb
CHANGED
|
@@ -17,6 +17,7 @@ module Agent
|
|
|
17
17
|
# in-process launcher expects exactly this shape, which is what makes the
|
|
18
18
|
# end-to-end specs fast enough to run on every save.
|
|
19
19
|
class Launcher
|
|
20
|
+
# The program name hints use when the caller does not say.
|
|
20
21
|
DEFAULT_PROGRAM = "agent-lock"
|
|
21
22
|
|
|
22
23
|
extend Forwardable
|
|
@@ -49,6 +50,11 @@ module Agent
|
|
|
49
50
|
# under the test runner that says `rspec`.
|
|
50
51
|
#
|
|
51
52
|
# @param argv [Array<String>]
|
|
53
|
+
# @param stdin [IO]
|
|
54
|
+
# @param stdout [IO]
|
|
55
|
+
# @param stderr [IO]
|
|
56
|
+
# @param kernel [Module] receives the one `exit`
|
|
57
|
+
# @param pastel [Pastel] colours, on only when stdout is a terminal
|
|
52
58
|
# @param program [String] e.g. `File.basename($PROGRAM_NAME)`
|
|
53
59
|
# rubocop:disable-next Metrics/ParameterLists
|
|
54
60
|
def initialize(argv = ARGV,
|
|
@@ -77,7 +83,15 @@ module Agent
|
|
|
77
83
|
# keeps that from taking the whole test process down with it.
|
|
78
84
|
code = e.status
|
|
79
85
|
rescue Dry::CLI::Error, Error => e
|
|
80
|
-
stderr.puts(bold(red("ERROR: #{program}: #{e.message}")))
|
|
86
|
+
stderr.puts(bold(red("ERROR: #{program}: #{Store::RedisStore.scrub(e.message)}")))
|
|
87
|
+
# Plain, not red: it is the way out, not more of the failure.
|
|
88
|
+
stderr.puts("HINT: run '#{program} #{e.command}'") if e.is_a?(Error) && e.command
|
|
89
|
+
code = 2
|
|
90
|
+
rescue StandardError => e
|
|
91
|
+
# Anything else used to fall through to the `ensure` with `code`
|
|
92
|
+
# still 0: an `acquire` that died on a malformed Redis URL printed
|
|
93
|
+
# nothing and exited 0, which an agent reads as "I hold it".
|
|
94
|
+
stderr.puts(bold(red("ERROR: #{program}: #{e.class}: #{Store::RedisStore.scrub(e.message)}")))
|
|
81
95
|
code = 2
|
|
82
96
|
rescue Interrupt
|
|
83
97
|
stderr.puts(bold(yellow("WARNING: #{program}: interrupted")))
|
|
@@ -88,6 +102,8 @@ module Agent
|
|
|
88
102
|
|
|
89
103
|
private
|
|
90
104
|
|
|
105
|
+
# @param msg [String]
|
|
106
|
+
# @return [void]
|
|
91
107
|
def p(msg = "")
|
|
92
108
|
stdout.puts(msg)
|
|
93
109
|
end
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../freeze"
|
|
4
|
+
|
|
5
|
+
module Agent
|
|
6
|
+
module Lock
|
|
7
|
+
class Manager
|
|
8
|
+
# `acquire --enforce`: freezing the files of a lock already won, and
|
|
9
|
+
# writing down which ones.
|
|
10
|
+
#
|
|
11
|
+
# Freezing happens after the claim is won, never before. Flagging files
|
|
12
|
+
# first and then losing the race would leave a tree full of unwritable
|
|
13
|
+
# files that no lock admits to having frozen, which is the one failure
|
|
14
|
+
# this whole feature is supposed to prevent.
|
|
15
|
+
#
|
|
16
|
+
# The walk and `chflags` run outside the store's mutex, since every
|
|
17
|
+
# other session would be waiting on them. That leaves a window in which
|
|
18
|
+
# the lock can be noted, broken, or broken and taken by somebody else,
|
|
19
|
+
# and a record's id is only its tree and scope, so the newcomer's lock
|
|
20
|
+
# has the id of the one this call won. Writing the record captured
|
|
21
|
+
# before the walk back over it lost the note, brought a broken lock
|
|
22
|
+
# back, or replaced another agent's lock with this one; deleting it by
|
|
23
|
+
# id on a refused freeze deleted the other agent's. So both writes go
|
|
24
|
+
# back under the mutex and act only on the claim this call won, read
|
|
25
|
+
# again: {#standing}.
|
|
26
|
+
class Enforcement
|
|
27
|
+
# @param tree [Tree]
|
|
28
|
+
# @param store [Store::FileSystemStore, Store::RedisStore]
|
|
29
|
+
def initialize(tree, store)
|
|
30
|
+
@tree = tree
|
|
31
|
+
@store = store
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Not for use inside the store's mutex, which it takes itself.
|
|
35
|
+
#
|
|
36
|
+
# @param record [Record] the lock, already created
|
|
37
|
+
# @param scope [Scope]
|
|
38
|
+
# @param wide [Boolean] freeze even a scope wider than Freeze::LIMIT files
|
|
39
|
+
# @return [Array(Record, String)] the lock with its frozen paths, and a
|
|
40
|
+
# warning, or nil, when the filesystem refused some of them. The
|
|
41
|
+
# record is nil when the claim did not outlast the freeze: nothing
|
|
42
|
+
# is left frozen, and whatever stands in the store is untouched
|
|
43
|
+
# @raise [Freeze::TooBroad] when the scope is too wide and `wide` is
|
|
44
|
+
# false. Nothing was flagged, and the claim is dropped with the
|
|
45
|
+
# freeze, not left standing unenforced
|
|
46
|
+
def apply(record, scope, wide:)
|
|
47
|
+
wanted = Freeze.matches(scope, @tree)
|
|
48
|
+
frozen = Freeze.apply(wanted, tree: @tree, wide: wide)
|
|
49
|
+
[@store.synchronize { settle(record, scope, frozen) }, shortfall(wanted, frozen)]
|
|
50
|
+
rescue Freeze::TooBroad
|
|
51
|
+
@store.synchronize { standing(record, @store.find(scope))&.then { |current| @store.delete(current) } }
|
|
52
|
+
raise
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
private
|
|
56
|
+
|
|
57
|
+
# @param record [Record] the lock as it was won
|
|
58
|
+
# @param scope [Scope]
|
|
59
|
+
# @param frozen [Array<String>] the files `chflags` accepted
|
|
60
|
+
# @return [Record, nil] the standing lock with its frozen paths, or
|
|
61
|
+
# nil, with the files thawed, when the claim is gone
|
|
62
|
+
def settle(record, scope, frozen)
|
|
63
|
+
found = @store.find(scope)
|
|
64
|
+
current = standing(record, found)
|
|
65
|
+
return current.with(frozen_paths: frozen).tap { |updated| @store.update(updated) } if current
|
|
66
|
+
|
|
67
|
+
# Whoever took the scope in the meantime may have frozen the same
|
|
68
|
+
# files under a lock of their own, and those stay frozen.
|
|
69
|
+
Freeze.clear(frozen - Array(found&.frozen_paths), tree: @tree)
|
|
70
|
+
nil
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# The claim this call won, as the store holds it now: notes and all.
|
|
74
|
+
# The same id under another holder, or taken at another time, is
|
|
75
|
+
# somebody else's lock.
|
|
76
|
+
#
|
|
77
|
+
# @param record [Record] the lock as it was won
|
|
78
|
+
# @param found [Record, nil] what the store holds under that id now
|
|
79
|
+
# @return [Record, nil] nil when it was broken, released or replaced
|
|
80
|
+
def standing(record, found)
|
|
81
|
+
return nil unless found&.active?
|
|
82
|
+
|
|
83
|
+
found if [found.agent_id, found.created_at] == [record.agent_id, record.created_at]
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# @param wanted [Array<String>] the files the scope matched
|
|
87
|
+
# @param frozen [Array<String>] the ones `chflags` accepted
|
|
88
|
+
# @return [String, nil] said only when the filesystem refused some of it
|
|
89
|
+
def shortfall(wanted, frozen)
|
|
90
|
+
missed = wanted.size - frozen.size
|
|
91
|
+
return nil if missed.zero?
|
|
92
|
+
|
|
93
|
+
"could not freeze #{missed} of #{wanted.size} file(s); the lock still stands"
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Agent
|
|
4
|
+
module Lock
|
|
5
|
+
class Manager
|
|
6
|
+
# `switch-backend`: binding a tree to the other store.
|
|
7
|
+
#
|
|
8
|
+
# Every lock the tree's marker governs has to go first. Once the marker
|
|
9
|
+
# moves nobody reads the old store, so a lock left there would block
|
|
10
|
+
# nobody and still tell its holder it held something. Those locks are
|
|
11
|
+
# refused, unless the caller discards them. It all happens under the
|
|
12
|
+
# old store's mutex, so nobody claims there while it is being emptied.
|
|
13
|
+
class Switch
|
|
14
|
+
# The store a tree is bound to now, whatever the caller asked for:
|
|
15
|
+
# the old store is the one that has to be emptied.
|
|
16
|
+
#
|
|
17
|
+
# A marker that names no backend is bound to nothing either. An older
|
|
18
|
+
# `alock` wrote AGENT_LOCK_BACKEND into it as typed, every command
|
|
19
|
+
# since has refused the tree, so no lock was ever taken under that
|
|
20
|
+
# name, and building it here left the tree with no way out.
|
|
21
|
+
#
|
|
22
|
+
# @param tree [Tree]
|
|
23
|
+
# @return [Store::FileSystemStore, Store::RedisStore, nil] nil for a
|
|
24
|
+
# virgin tree, which is bound to nothing yet, and for a marker
|
|
25
|
+
# that names no backend
|
|
26
|
+
# @raise [Store::Unreachable] when the tree is bound to a Redis that does not answer
|
|
27
|
+
def self.current(tree)
|
|
28
|
+
established = Store.recorded(tree)
|
|
29
|
+
return unless Store::BACKENDS.include?(established)
|
|
30
|
+
|
|
31
|
+
Store.build(established, tree, redis_url: Store.named_redis_url)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# The store's mutex for every other verb, which refuses to run in a
|
|
35
|
+
# store the tree has left. {#call} moves the marker under this mutex,
|
|
36
|
+
# so the check and the block see one answer.
|
|
37
|
+
#
|
|
38
|
+
# @param tree [Tree]
|
|
39
|
+
# @param store [Store::FileSystemStore, Store::RedisStore] the store a manager was built on
|
|
40
|
+
# @yield the critical section
|
|
41
|
+
# @return [Object] whatever the block returns
|
|
42
|
+
# @raise [Store::Mismatch] when the tree has moved to another backend since
|
|
43
|
+
def self.holding(tree, store)
|
|
44
|
+
store.synchronize do
|
|
45
|
+
moved = Store.recorded(tree)
|
|
46
|
+
next yield if moved.nil? || moved == store.backend
|
|
47
|
+
|
|
48
|
+
raise Store::Mismatch, "this tree's locks moved from #{store.backend} to #{moved} " \
|
|
49
|
+
"while this command waited; run it again"
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# @param tree [Tree]
|
|
54
|
+
# @param store [Store::FileSystemStore, Store::RedisStore] the store the tree is bound to now
|
|
55
|
+
# @param sweep [#call] reaps the store; called inside its mutex
|
|
56
|
+
# @param drop [#call] deletes one record, thawing what it froze
|
|
57
|
+
def initialize(tree, store, sweep:, drop:)
|
|
58
|
+
@tree = tree
|
|
59
|
+
@store = store
|
|
60
|
+
@sweep = sweep
|
|
61
|
+
@drop = drop
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Not for use inside the store's mutex, which it takes itself.
|
|
65
|
+
#
|
|
66
|
+
# @param target [Store::FileSystemStore, Store::RedisStore] the store
|
|
67
|
+
# to move to, already connected, so a Redis that does not answer is
|
|
68
|
+
# found out before anything is deleted
|
|
69
|
+
# @param discard [Boolean] delete the old store's locks instead of refusing
|
|
70
|
+
# @return [Array(Symbol, Array<Record>)] :switched with the locks it
|
|
71
|
+
# discarded, :unchanged, or :occupied with the locks in the way
|
|
72
|
+
def call(target, discard:)
|
|
73
|
+
return [:unchanged, []] if target.backend == @store.backend
|
|
74
|
+
|
|
75
|
+
@store.synchronize do
|
|
76
|
+
# Another switch got here first, while this one waited.
|
|
77
|
+
next [:unchanged, []] if Store.recorded(@tree) == target.backend
|
|
78
|
+
|
|
79
|
+
@sweep.call
|
|
80
|
+
stranded = @store.bound
|
|
81
|
+
next [:occupied, stranded] if stranded.any? && !discard
|
|
82
|
+
|
|
83
|
+
stranded.each { |record| @drop.call(record) }
|
|
84
|
+
Store.rebind(@tree, target.backend)
|
|
85
|
+
[:switched, stranded]
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../freeze"
|
|
4
|
+
|
|
5
|
+
module Agent
|
|
6
|
+
module Lock
|
|
7
|
+
class Manager
|
|
8
|
+
# Thawing what a lock froze, in the checkout that froze it.
|
|
9
|
+
#
|
|
10
|
+
# A frozen path is relative to the root of the checkout the lock was
|
|
11
|
+
# taken in, which need not be the checkout of the command giving the
|
|
12
|
+
# lock up: a reap, a parent's `release-all`, a `resume` and
|
|
13
|
+
# `switch-backend --discard-locks` all reach locks taken in another
|
|
14
|
+
# worktree. Thawing only in the checkout at hand deleted such a lock and
|
|
15
|
+
# left its files `uchg`, with the list of them gone in the record, so
|
|
16
|
+
# that neither `release` nor `break` could find anything to thaw. The
|
|
17
|
+
# thaw goes to the root the record names instead.
|
|
18
|
+
#
|
|
19
|
+
# Only on this host. Another machine's files cannot be reached from
|
|
20
|
+
# here, and its list must not be applied to this one: the same relative
|
|
21
|
+
# path here is a different file, which may be frozen under a lock of
|
|
22
|
+
# its own.
|
|
23
|
+
module Thaw
|
|
24
|
+
# The root a record names, exactly as it was written, answering the
|
|
25
|
+
# one thing {Freeze} asks of a tree. Deliberately not a {Tree}, which
|
|
26
|
+
# asks git where its root is: that raises for a worktree removed
|
|
27
|
+
# since, runs a subprocess inside the store's mutex, and resolves a
|
|
28
|
+
# directory that is no longer a checkout to the repository around it,
|
|
29
|
+
# whose files of the same relative name are not the frozen ones.
|
|
30
|
+
#
|
|
31
|
+
# @!attribute [r] root
|
|
32
|
+
# @return [String] absolute
|
|
33
|
+
Checkout = Data.define(:root)
|
|
34
|
+
|
|
35
|
+
module_function
|
|
36
|
+
|
|
37
|
+
# Best effort, as {Freeze.clear} is. A worktree removed since has
|
|
38
|
+
# nothing left to thaw, `rm -rf` being unable to remove a frozen file,
|
|
39
|
+
# and so is no reason to keep its lock. One moved since is not found:
|
|
40
|
+
# the record names only where it was.
|
|
41
|
+
#
|
|
42
|
+
# @param record [Record]
|
|
43
|
+
# @return [Array<String>] the paths still frozen because their files
|
|
44
|
+
# are on another machine: all of them, or none
|
|
45
|
+
def call(record)
|
|
46
|
+
home = home_of(record)
|
|
47
|
+
return record.frozen_paths unless home
|
|
48
|
+
|
|
49
|
+
Freeze.clear(record.frozen_paths, tree: home)
|
|
50
|
+
[]
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Being local to the checkout it names is what a record on this host
|
|
54
|
+
# is, wherever the command asking runs.
|
|
55
|
+
#
|
|
56
|
+
# @param record [Record]
|
|
57
|
+
# @return [Checkout, nil] where its files are; nil when they are on
|
|
58
|
+
# another machine, or the record names no checkout
|
|
59
|
+
def home_of(record)
|
|
60
|
+
return nil if record.tree.nil?
|
|
61
|
+
|
|
62
|
+
home = Checkout.new(root: record.tree)
|
|
63
|
+
home if record.local_to?(home)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|