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
|
@@ -7,68 +7,77 @@ module Agent
|
|
|
7
7
|
module Lock
|
|
8
8
|
module CLI
|
|
9
9
|
module Commands
|
|
10
|
-
#
|
|
11
|
-
# than copy it, or a configuration that names it
|
|
10
|
+
# `alock skills path`: where the bundled skill is, for a harness that
|
|
11
|
+
# would rather link it than copy it, or a configuration that names it
|
|
12
|
+
# by path.
|
|
12
13
|
class SkillsPath < Base
|
|
13
|
-
desc "Print the
|
|
14
|
+
desc "Print where the bundled agent-lock skill lives"
|
|
14
15
|
|
|
16
|
+
# @return [void]
|
|
15
17
|
def call(**)
|
|
16
18
|
say(Skill.source)
|
|
17
19
|
end
|
|
18
20
|
end
|
|
19
21
|
|
|
20
|
-
#
|
|
21
|
-
# how to claim files before it first
|
|
22
|
+
# `alock skills install`: copy the bundled skill into a skills
|
|
23
|
+
# directory, so an agent learns how to claim files before it first
|
|
24
|
+
# needs to.
|
|
22
25
|
class SkillsInstall < Base
|
|
23
|
-
desc "Copy the
|
|
26
|
+
desc "Copy the bundled skill (#{Skill.bundled.join(", ")}) into a skills directory"
|
|
24
27
|
|
|
25
|
-
option :into, type: :string, default: nil,
|
|
26
|
-
desc: "
|
|
28
|
+
option :into, type: :string, default: nil,
|
|
29
|
+
desc: "Skills directory to install into (default: ~/.agents/skills)"
|
|
27
30
|
option :for, type: :string, default: nil, aliases: ["-a"],
|
|
28
|
-
desc: "
|
|
29
|
-
option :
|
|
30
|
-
|
|
31
|
+
desc: "claude installs into ~/.claude/skills, any other agent ~/.agents/skills"
|
|
32
|
+
option :overwrite, type: :boolean, default: false,
|
|
33
|
+
desc: "Replace an installed copy that differs from the bundled one"
|
|
31
34
|
|
|
32
35
|
# An empty example crashes dry-cli-help's renderer, so the default
|
|
33
36
|
# install is spelled out as an option-free line with a comment.
|
|
34
37
|
example [
|
|
35
38
|
" # into ~/.agents/skills",
|
|
36
39
|
"--for claude # into ~/.claude/skills",
|
|
37
|
-
"--into ~/some/skills --
|
|
40
|
+
"--into ~/some/skills --overwrite # replace a copy that differs"
|
|
38
41
|
]
|
|
39
42
|
|
|
40
|
-
# @param options [Hash]
|
|
43
|
+
# @param options [Hash] `:into`, `:for`, `:overwrite`
|
|
44
|
+
# @return [Skill::Result]
|
|
41
45
|
def call(**options)
|
|
42
46
|
into = destination_for(options)
|
|
43
47
|
skill = into ? Skill.new(into: into) : Skill.new
|
|
44
48
|
|
|
45
|
-
result = skill.install(
|
|
49
|
+
result = skill.install(overwrite: options[:overwrite])
|
|
46
50
|
report(result)
|
|
47
51
|
finish(result)
|
|
48
52
|
end
|
|
49
53
|
|
|
50
54
|
private
|
|
51
55
|
|
|
56
|
+
# `--into` wins over `--for`, with a warning, since it is the more
|
|
57
|
+
# specific of the two.
|
|
58
|
+
#
|
|
52
59
|
# @param options [Hash]
|
|
53
60
|
# @return [String, nil] where to install, or nil for Skill's own default
|
|
54
61
|
def destination_for(options)
|
|
55
62
|
return options[:into] unless options[:for]
|
|
56
63
|
|
|
57
|
-
|
|
58
|
-
|
|
64
|
+
if options[:into]
|
|
65
|
+
warn_("Both --for and --into were given; --for is ignored")
|
|
66
|
+
return options[:into]
|
|
67
|
+
end
|
|
59
68
|
|
|
60
|
-
|
|
61
|
-
File.join(Dir.home, agent_dir, "skills")
|
|
69
|
+
Skill.default_into(options[:for])
|
|
62
70
|
end
|
|
63
71
|
|
|
64
72
|
# @param result [Skill::Result]
|
|
73
|
+
# @return [void]
|
|
65
74
|
def report(result)
|
|
66
75
|
case result.status
|
|
67
76
|
when :installed then say("INSTALLED #{result.path}")
|
|
68
77
|
when :current then say("UP TO DATE #{result.path}")
|
|
69
78
|
when :differs
|
|
70
79
|
warn_("REFUSED: #{result.path} differs from the copy this gem ships")
|
|
71
|
-
warn_(" #{program} skills install --
|
|
80
|
+
warn_(" #{program} skills install --overwrite # replace it")
|
|
72
81
|
when :linked
|
|
73
82
|
warn_("REFUSED: #{result.path} is a symlink, so something else installs it; left alone")
|
|
74
83
|
end
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "base"
|
|
4
|
+
require_relative "../../facade/strategies"
|
|
5
|
+
|
|
6
|
+
module Agent
|
|
7
|
+
module Lock
|
|
8
|
+
module CLI
|
|
9
|
+
module Commands
|
|
10
|
+
# Move a checkout's locks to the other backend.
|
|
11
|
+
#
|
|
12
|
+
# The backend marker binds a checkout to the store it used first, so
|
|
13
|
+
# that two agents beside each other never use two stores. Releasing
|
|
14
|
+
# every lock did not move it, so the one way out used to be deleting
|
|
15
|
+
# the marker by hand. This is that, done safely.
|
|
16
|
+
#
|
|
17
|
+
# A {Base}, not a {TreeCommand}: the backend is the argument, so a
|
|
18
|
+
# `--locking-strategy` beside it would be a second answer to the same
|
|
19
|
+
# question.
|
|
20
|
+
class SwitchBackend < Base
|
|
21
|
+
desc "Bind this checkout to the redis or filesystem store; exit 1 while the old one holds locks"
|
|
22
|
+
|
|
23
|
+
argument :backend, required: true, desc: "redis or filesystem (or file)"
|
|
24
|
+
|
|
25
|
+
option :dir, type: :string, default: ".", desc: "Act on the checkout that contains this directory"
|
|
26
|
+
option :redis_url, type: :string, aliases: ["-u"],
|
|
27
|
+
desc: "The one Redis to switch to, with no fallback (env: AGENT_LOCK_REDIS_URL)"
|
|
28
|
+
option :discard_locks, type: :boolean, default: false,
|
|
29
|
+
desc: "Delete every lock in the old store instead of refusing"
|
|
30
|
+
|
|
31
|
+
example ["redis", "redis -u redis://127.0.0.1:6379/0", "filesystem --discard-locks"]
|
|
32
|
+
|
|
33
|
+
# @param backend [String] as typed
|
|
34
|
+
# @param options [Hash] `:dir`, `:redis_url`, `:discard_locks`
|
|
35
|
+
# @return [Manager::Result, nil] nil when the tree was bound to
|
|
36
|
+
# nothing yet, or to a Redis that does not answer
|
|
37
|
+
# @raise [Store::Unreachable] when the tree is bound to a Redis that
|
|
38
|
+
# does not answer, and `:discard_locks` is not set
|
|
39
|
+
def call(backend:, **options)
|
|
40
|
+
tree = Tree.for(options[:dir] || ".")
|
|
41
|
+
target = target_store(tree, backend, options[:redis_url])
|
|
42
|
+
current, unread = current_store(tree)
|
|
43
|
+
return leave_unread(tree, target, unread, options) if unread
|
|
44
|
+
return bind_virgin(tree, target) if current.nil?
|
|
45
|
+
|
|
46
|
+
result = Manager.new(tree: tree, store: current).switch_backend(target, discard: options[:discard_locks])
|
|
47
|
+
report(result, from: current.backend, to: target.backend, url: options[:redis_url])
|
|
48
|
+
finish(result)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
private
|
|
52
|
+
|
|
53
|
+
# Only a `-u` typed beside `filesystem` contradicts it. The
|
|
54
|
+
# environment's URL is the Redis to switch to when the target is
|
|
55
|
+
# redis, and otherwise names the store being left: a dispatcher
|
|
56
|
+
# exports it for every agent it starts, and checked against the
|
|
57
|
+
# file store here it kept all of them from ever running this.
|
|
58
|
+
#
|
|
59
|
+
# @param tree [Tree]
|
|
60
|
+
# @param backend [String] as typed
|
|
61
|
+
# @param url [String, nil] `--redis-url` as typed
|
|
62
|
+
# @return [Store::FileSystemStore, Store::RedisStore] connected
|
|
63
|
+
# @raise [Dry::CLI::Error] for a backend that is not one, or an empty URL
|
|
64
|
+
# @raise [Store::Mismatch] for a Redis URL typed beside the file store
|
|
65
|
+
# @raise [Store::Unreachable] when the Redis named does not answer
|
|
66
|
+
def target_store(tree, backend, url)
|
|
67
|
+
url = url&.strip
|
|
68
|
+
# `-u "$UNSET"` must not quietly become "whichever answers first".
|
|
69
|
+
raise Dry::CLI::Error, "--redis-url is empty" if url && url.empty?
|
|
70
|
+
|
|
71
|
+
name = canonical(backend)
|
|
72
|
+
Store.requested_backend(name, url, url)
|
|
73
|
+
Store.build(name, tree, redis_url: url || Store.named_redis_url)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# The store being left, or the reason it cannot be read. Raised
|
|
77
|
+
# from here, a Redis that was gone for good kept the checkout bound
|
|
78
|
+
# to it, whatever the caller said about its locks.
|
|
79
|
+
#
|
|
80
|
+
# @param tree [Tree]
|
|
81
|
+
# @return [Array(Store::FileSystemStore, nil), Array(Store::RedisStore, nil), Array(nil, nil),
|
|
82
|
+
# Array(nil, Store::Unreachable)] the store and nil, two nils for
|
|
83
|
+
# a tree bound to nothing, or nil and why its Redis did not answer
|
|
84
|
+
def current_store(tree)
|
|
85
|
+
[Manager::Switch.current(tree), nil]
|
|
86
|
+
rescue Store::Unreachable => e
|
|
87
|
+
[nil, e]
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# The tree is bound to a Redis that does not answer, so there is no
|
|
91
|
+
# store to empty and no mutex to take.
|
|
92
|
+
#
|
|
93
|
+
# @param tree [Tree]
|
|
94
|
+
# @param target [Store::FileSystemStore, Store::RedisStore]
|
|
95
|
+
# @param error [Store::Unreachable] its message names the Redis, password masked
|
|
96
|
+
# @param options [Hash] `:redis_url`, `:discard_locks`
|
|
97
|
+
# @return [nil]
|
|
98
|
+
# @raise [Store::Unreachable] without `:discard_locks`, naming it as the way out
|
|
99
|
+
def leave_unread(tree, target, error, options)
|
|
100
|
+
left = Store.recorded(tree)
|
|
101
|
+
to = target.backend
|
|
102
|
+
# Nothing is left behind by staying, so there is nothing to discard.
|
|
103
|
+
left == to ? report_unchanged(to, options[:redis_url]) : abandon(tree, left, to, error, options)
|
|
104
|
+
nil
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# Whether the old store holds locks is not known, which is refused
|
|
108
|
+
# like locks that are known: `--discard-locks` is what says they do
|
|
109
|
+
# not matter. Then the Redis is named, since nothing in it was
|
|
110
|
+
# deleted and nothing its locks froze was thawed.
|
|
111
|
+
#
|
|
112
|
+
# @param tree [Tree]
|
|
113
|
+
# @param left [String] the backend the marker names now
|
|
114
|
+
# @param to [String] "file" or "redis", from a store that was built
|
|
115
|
+
# @param error [Store::Unreachable]
|
|
116
|
+
# @param options [Hash] `:discard_locks`
|
|
117
|
+
# @return [void]
|
|
118
|
+
# @raise [Store::Unreachable] without `:discard_locks`
|
|
119
|
+
def abandon(tree, left, to, error, options)
|
|
120
|
+
why = Store::RedisStore.scrub(error.message)
|
|
121
|
+
unless options[:discard_locks]
|
|
122
|
+
raise Store::Unreachable.new("#{why}, so this checkout's locks there cannot be checked; " \
|
|
123
|
+
"--discard-locks leaves them behind unread",
|
|
124
|
+
command: "switch-backend #{to} --discard-locks")
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
warn_("note: #{why}, so this checkout's locks there were not deleted, and nothing they froze was thawed")
|
|
128
|
+
say("SWITCHED #{left} -> #{Store.rebind(tree, to)}")
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# @param backend [String]
|
|
132
|
+
# @return [String] "redis" or "file"
|
|
133
|
+
# @raise [Dry::CLI::Error]
|
|
134
|
+
def canonical(backend)
|
|
135
|
+
Facade::Strategies.canonical(backend.strip.downcase)
|
|
136
|
+
rescue ArgumentError
|
|
137
|
+
raise Dry::CLI::Error, "unknown backend #{backend.inspect}, expected redis or filesystem"
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# A tree that never took a lock has no store to empty. Nor has one
|
|
141
|
+
# whose marker names no backend, but that is said: the name may be
|
|
142
|
+
# a typing error for a store that was never looked at.
|
|
143
|
+
#
|
|
144
|
+
# @param tree [Tree]
|
|
145
|
+
# @param target [Store::FileSystemStore, Store::RedisStore]
|
|
146
|
+
# @return [nil]
|
|
147
|
+
def bind_virgin(tree, target)
|
|
148
|
+
left = Store.recorded(tree)
|
|
149
|
+
unless left.nil? || Store::BACKENDS.include?(left)
|
|
150
|
+
warn_("note: the marker named \"#{printable(left)}\", which is not a backend, " \
|
|
151
|
+
"so no store was checked for locks")
|
|
152
|
+
end
|
|
153
|
+
say("SWITCHED to #{Store.rebind(tree, target.backend)}")
|
|
154
|
+
nil
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
# @param result [Manager::Result]
|
|
158
|
+
# @param from [String]
|
|
159
|
+
# @param to [String]
|
|
160
|
+
# @param url [String, nil] `--redis-url` as typed
|
|
161
|
+
# @return [void]
|
|
162
|
+
def report(result, from:, to:, url: nil)
|
|
163
|
+
case result.status
|
|
164
|
+
when :unchanged then report_unchanged(to, url)
|
|
165
|
+
when :switched
|
|
166
|
+
result.records.each do |record|
|
|
167
|
+
warn_("DISCARDED #{scope_of(record)}, held by #{holder_of(record)}")
|
|
168
|
+
end
|
|
169
|
+
say("SWITCHED #{from} -> #{to}")
|
|
170
|
+
when :occupied then report_occupied(result.records, from:, to:)
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
# The marker records the backend and no URL, so a tree on Redis is
|
|
175
|
+
# not moved to another Redis from here. Saying only ALREADY would
|
|
176
|
+
# let `-u` pass for something that happened.
|
|
177
|
+
#
|
|
178
|
+
# @param to [String]
|
|
179
|
+
# @param url [String, nil] `--redis-url` as typed
|
|
180
|
+
# @return [void]
|
|
181
|
+
def report_unchanged(to, url)
|
|
182
|
+
say("ALREADY #{to}")
|
|
183
|
+
return unless url
|
|
184
|
+
|
|
185
|
+
warn_("--redis-url was not used: the checkout records its backend, not a URL. " \
|
|
186
|
+
"Each command reads its Redis from --redis-url or #{Store::REDIS_URL_ENV}")
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
# @param records [Array<Record>]
|
|
190
|
+
# @param from [String]
|
|
191
|
+
# @param to [String]
|
|
192
|
+
# @return [void]
|
|
193
|
+
def report_occupied(records, from:, to:)
|
|
194
|
+
warn_("REFUSED: #{records.size} lock(s) still live in #{from}, and nobody would read them after the switch")
|
|
195
|
+
records.each do |record|
|
|
196
|
+
warn_(" #{scope_of(record)}\t#{holder_of(record)}\t#{printable(record.status)}")
|
|
197
|
+
end
|
|
198
|
+
warn_("HINT: run '#{program} release-all' or '#{program} break <scope>' for each, " \
|
|
199
|
+
"or '#{program} switch-backend #{to} --discard-locks' to delete them all")
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
end
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
end
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "base"
|
|
4
|
+
require_relative "../../facade/strategies"
|
|
5
|
+
|
|
6
|
+
module Agent
|
|
7
|
+
module Lock
|
|
8
|
+
module CLI
|
|
9
|
+
module Commands
|
|
10
|
+
# A command that works on the lock store of one checkout, and so
|
|
11
|
+
# takes `--dir` to name which checkout that is, and
|
|
12
|
+
# `--locking-strategy` and `--redis-url` to name which store.
|
|
13
|
+
#
|
|
14
|
+
# They are added in `inherited` rather than restated in every
|
|
15
|
+
# subclass, so a new lock verb gets them by subclassing and cannot
|
|
16
|
+
# forget one. Every verb takes the store flags, not `acquire` alone:
|
|
17
|
+
# a lock taken on a named Redis is released on that same Redis.
|
|
18
|
+
#
|
|
19
|
+
# @abstract Subclass and implement `#call(**options)`.
|
|
20
|
+
class TreeCommand < Base
|
|
21
|
+
# @param subclass [Class<TreeCommand>]
|
|
22
|
+
# @return [void]
|
|
23
|
+
def self.inherited(subclass)
|
|
24
|
+
super
|
|
25
|
+
subclass.option :dir, type: :string, default: ".",
|
|
26
|
+
desc: "Act on the checkout that contains this directory"
|
|
27
|
+
subclass.option :locking_strategy, type: :string, aliases: ["-s"],
|
|
28
|
+
desc: "redis or filesystem: where the locks live, instead of " \
|
|
29
|
+
"AGENT_LOCK_BACKEND or whichever answers first"
|
|
30
|
+
subclass.option :redis_url, type: :string, aliases: ["-u"],
|
|
31
|
+
desc: "The one Redis to use, with no fallback; implies " \
|
|
32
|
+
"--locking-strategy redis (env: AGENT_LOCK_REDIS_URL)"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
# @param options [Hash] `:dir`, `:locking_strategy`, `:redis_url`
|
|
38
|
+
# @return [Manager] bound to the tree that contains `--dir`, and to
|
|
39
|
+
# the store the flags name, when they name one
|
|
40
|
+
# @raise [Dry::CLI::Error] for a strategy that is not one, or an empty URL
|
|
41
|
+
# @raise [Store::Mismatch] for a Redis URL beside the file store
|
|
42
|
+
def manager(options)
|
|
43
|
+
tree = Tree.for(options[:dir] || ".")
|
|
44
|
+
Manager.new(tree: tree, store: Store.for(tree, **store_options(options)))
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# @param options [Hash]
|
|
48
|
+
# @return [Hash] `backend:` and `redis_url:` for {Store.for}
|
|
49
|
+
# @raise [Dry::CLI::Error]
|
|
50
|
+
def store_options(options)
|
|
51
|
+
url = options[:redis_url]&.strip
|
|
52
|
+
# `-u "$UNSET"` must not quietly become "whichever answers first".
|
|
53
|
+
raise Dry::CLI::Error, "--redis-url is empty" if url && url.empty?
|
|
54
|
+
|
|
55
|
+
{ backend: backend_named(options[:locking_strategy]), redis_url: url }
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# @param strategy [String, nil] as typed
|
|
59
|
+
# @return [String, nil] "redis" or "file"
|
|
60
|
+
# @raise [Dry::CLI::Error] for anything else
|
|
61
|
+
def backend_named(strategy)
|
|
62
|
+
return if strategy.nil?
|
|
63
|
+
|
|
64
|
+
Facade::Strategies.canonical(strategy.strip.downcase)
|
|
65
|
+
rescue ArgumentError
|
|
66
|
+
raise Dry::CLI::Error, "unknown --locking-strategy #{strategy.inspect}, expected redis or filesystem"
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
@@ -29,17 +29,19 @@ module Agent
|
|
|
29
29
|
|
|
30
30
|
# Where #parent_id came from, likewise.
|
|
31
31
|
PARENT_SOURCES = {
|
|
32
|
+
none: "AGENT_PARENT_ID is set and empty",
|
|
32
33
|
explicit: "from AGENT_PARENT_ID",
|
|
33
34
|
inferred: "inferred: the session this runs in, since AGENT_ID names somebody else"
|
|
34
35
|
}.freeze
|
|
35
36
|
|
|
36
|
-
desc "
|
|
37
|
+
desc "Print the id your locks would carry, its parent, and their source"
|
|
37
38
|
|
|
38
|
-
option :json, type: :boolean, default: false, desc: "
|
|
39
|
+
option :json, type: :boolean, default: false, desc: "Print id, source, parent_id and parent_source as JSON"
|
|
39
40
|
|
|
40
41
|
example ["", "--json"]
|
|
41
42
|
|
|
42
|
-
# @param options [Hash]
|
|
43
|
+
# @param options [Hash] `:json`
|
|
44
|
+
# @return [void]
|
|
43
45
|
def call(**options)
|
|
44
46
|
identity = Identity.current
|
|
45
47
|
warn_shared(identity) unless identity.source == :explicit
|
|
@@ -54,6 +56,7 @@ module Agent
|
|
|
54
56
|
# @param identity [Identity]
|
|
55
57
|
# @return [String]
|
|
56
58
|
def parent_line(identity)
|
|
59
|
+
return "parent: none (#{PARENT_SOURCES.fetch(:none)})" if identity.parent_source == :none
|
|
57
60
|
return "parent: none" if identity.parent_id.nil?
|
|
58
61
|
|
|
59
62
|
"parent: #{identity.parent_id} (#{PARENT_SOURCES.fetch(identity.parent_source)})"
|
|
@@ -65,6 +68,7 @@ module Agent
|
|
|
65
68
|
# them can ever block another.
|
|
66
69
|
#
|
|
67
70
|
# @param identity [Identity]
|
|
71
|
+
# @return [void]
|
|
68
72
|
def warn_shared(identity)
|
|
69
73
|
warn_("note: every sub-agent of this session answers to #{identity.id} too, so none can block another.")
|
|
70
74
|
warn_(" Give each one its own: AGENT_ID=<name> #{program} acquire ...")
|
data/lib/agent/lock/cli.rb
CHANGED
|
@@ -17,29 +17,63 @@ module Agent
|
|
|
17
17
|
# line, so guessing it wrong is not a cosmetic mistake.
|
|
18
18
|
PROGRAM_NAME = "alock"
|
|
19
19
|
|
|
20
|
-
#
|
|
21
|
-
#
|
|
22
|
-
|
|
20
|
+
# Colours the help text. Pastel honours NO_COLOR and a non-terminal
|
|
21
|
+
# stdout, so piped help and the specs read plain text.
|
|
22
|
+
COLORS = Pastel.new
|
|
23
|
+
|
|
24
|
+
# The name in the left cell of the help's title box.
|
|
25
|
+
TITLE = " Agent/Lock — Distributed Advisory Locker"
|
|
26
|
+
|
|
27
|
+
# Inner widths of the title box's two cells. The version is padded to
|
|
28
|
+
# the right one rather than spaced by hand, so 4.10.0 still closes the box.
|
|
29
|
+
TITLE_CELLS = [45, 33].freeze
|
|
30
|
+
|
|
31
|
+
# The three lines `alock -h` opens with: the gem's name and its version, boxed.
|
|
32
|
+
#
|
|
33
|
+
# @param version [String]
|
|
34
|
+
# @return [String]
|
|
35
|
+
def self.title_box(version = Agent::Lock::VERSION)
|
|
36
|
+
left, right = TITLE_CELLS
|
|
37
|
+
rule = ->(open, middle, close) { "#{open}#{"━" * left}#{middle}#{"━" * right}#{close}" }
|
|
38
|
+
name = COLORS.yellow(TITLE.ljust(left))
|
|
39
|
+
release = COLORS.green.bold(" Version #{version}".ljust(right))
|
|
40
|
+
|
|
41
|
+
[rule.call("┏", "┳", "┓"), "┃#{name}┃#{release}┃", rule.call("┗", "┻", "┛")].join("\n")
|
|
42
|
+
end
|
|
43
|
+
# The column dry-cli-help wraps at, rather than the terminal's width,
|
|
44
|
+
# so help reads the same in a wide terminal, a narrow one, and a log.
|
|
23
45
|
HELP_WIDTH = 85
|
|
24
46
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
47
|
+
# What `alock -h` says before it lists the commands.
|
|
48
|
+
DESCRIPTION = <<~TEXT.freeze
|
|
49
|
+
Advisory locks for AI coding agents that share a checkout, or a branch
|
|
50
|
+
across worktrees and optionally across machines. Claim the file, directory
|
|
51
|
+
or glob you are about to write to with `acquire`; any other agent that claims
|
|
52
|
+
or checks an overlapping scope is refused and told who holds it, since when,
|
|
53
|
+
and why. Locks are advisory only: #{COLORS.blue("they protect agents from working on the same files as other agents that also check for locks")}.
|
|
54
|
+
|
|
55
|
+
Sub-agents of one session share its process, so each must name itself on
|
|
56
|
+
every call: #{COLORS.blue("AGENT_ID=<name> alock acquire ...")} Run `alock whoami` to see the
|
|
57
|
+
name your locks would carry.
|
|
58
|
+
|
|
59
|
+
Backends: Redis when one answers on env REDIS_URL (shared across machines),
|
|
60
|
+
or 127.0.0.1:6379 (local only). Otherwise a file store inside .git (also
|
|
61
|
+
local only). The first process in a checkout records the choice. To choose it yourself,
|
|
62
|
+
pass #{COLORS.blue("--locking-strategy redis|filesystem")} (#{COLORS.blue("--redis-url URL")} names the one Redis
|
|
63
|
+
to use), or set #{COLORS.blue("AGENT_LOCK_BACKEND=redis|file")}.
|
|
64
|
+
|
|
65
|
+
Exit codes: 0 done, 1 refused or held by someone else, 2 could not run.
|
|
66
|
+
|
|
67
|
+
If you are using the gem from a Ruby application, you should default to the Ruby
|
|
68
|
+
API. Every CLI command is available via Ruby API, as described in the README:
|
|
69
|
+
#{COLORS.cyan.bold.underline("https://github.com/kigster/agent-lock#ruby-api")}
|
|
37
70
|
TEXT
|
|
38
71
|
|
|
39
72
|
Dry::CLI::Help.configure do
|
|
40
|
-
|
|
73
|
+
styles { heading :bold, :magenta }
|
|
74
|
+
title "\n#{CLI.title_box}"
|
|
41
75
|
description DESCRIPTION
|
|
42
|
-
epilogue "Documentation: https://github.com/kigster/agent-lock"
|
|
76
|
+
epilogue "Documentation: #{COLORS.cyan.bold.underline("https://github.com/kigster/agent-lock")}"
|
|
43
77
|
width HELP_WIDTH
|
|
44
78
|
end
|
|
45
79
|
|
|
@@ -64,22 +98,25 @@ module Agent
|
|
|
64
98
|
registry
|
|
65
99
|
end
|
|
66
100
|
|
|
67
|
-
# The whole command line
|
|
68
|
-
#
|
|
101
|
+
# The whole command line is the COMMANDS table below, filled in once
|
|
102
|
+
# the command classes are loaded, so adding a verb is one line.
|
|
69
103
|
end
|
|
70
104
|
end
|
|
71
105
|
end
|
|
72
106
|
|
|
73
107
|
require_relative "cli/commands/base"
|
|
108
|
+
require_relative "cli/commands/tree_command"
|
|
74
109
|
require_relative "cli/commands/acquire"
|
|
75
110
|
require_relative "cli/commands/release"
|
|
76
111
|
require_relative "cli/commands/check"
|
|
77
112
|
require_relative "cli/commands/list"
|
|
113
|
+
require_relative "cli/commands/show"
|
|
78
114
|
require_relative "cli/commands/mine"
|
|
79
115
|
require_relative "cli/commands/release_all"
|
|
80
116
|
require_relative "cli/commands/break"
|
|
81
117
|
require_relative "cli/commands/note"
|
|
82
118
|
require_relative "cli/commands/resume"
|
|
119
|
+
require_relative "cli/commands/switch_backend"
|
|
83
120
|
require_relative "cli/commands/whoami"
|
|
84
121
|
require_relative "cli/commands/skills"
|
|
85
122
|
require_relative "cli/commands/version"
|
|
@@ -88,16 +125,23 @@ require_relative "cli/commands/completion"
|
|
|
88
125
|
module Agent
|
|
89
126
|
module Lock
|
|
90
127
|
module CLI
|
|
128
|
+
# Every command but `completion`, which needs the finished registry.
|
|
129
|
+
# Each entry maps the name typed to its class and its aliases.
|
|
130
|
+
#
|
|
131
|
+
# @return [Hash{String => Array(Class, Array<String>)}]
|
|
91
132
|
COMMANDS = {
|
|
92
133
|
"acquire" => [Commands::Acquire, []],
|
|
93
134
|
"release" => [Commands::Release, []],
|
|
94
135
|
"check" => [Commands::Check, []],
|
|
95
136
|
"list" => [Commands::List, ["ls"]],
|
|
137
|
+
"show repos" => [Commands::ShowRepos, []],
|
|
138
|
+
"show repo" => [Commands::ShowRepo, []],
|
|
96
139
|
"mine" => [Commands::Mine, []],
|
|
97
140
|
"release-all" => [Commands::ReleaseAll, []],
|
|
98
141
|
"break" => [Commands::Break, []],
|
|
99
142
|
"note" => [Commands::Note, []],
|
|
100
143
|
"resume" => [Commands::Resume, []],
|
|
144
|
+
"switch-backend" => [Commands::SwitchBackend, []],
|
|
101
145
|
"whoami" => [Commands::Whoami, []],
|
|
102
146
|
"skills path" => [Commands::SkillsPath, []],
|
|
103
147
|
"skills install" => [Commands::SkillsInstall, []],
|
data/lib/agent/lock/error.rb
CHANGED
|
@@ -5,6 +5,57 @@ module Agent
|
|
|
5
5
|
# The base of everything this gem raises. In a file of its own so that any
|
|
6
6
|
# part of the library can require it without reaching for the entry point,
|
|
7
7
|
# which would require the part doing the asking.
|
|
8
|
-
|
|
8
|
+
#
|
|
9
|
+
# @example
|
|
10
|
+
# raise Agent::Lock::Error.new("this tree's locks live in file, not redis", command: "switch-backend redis")
|
|
11
|
+
class Error < StandardError
|
|
12
|
+
# The `alock` command that gets past this error, without the program
|
|
13
|
+
# name, which only the launcher knows: `alock` or `agent-lock`.
|
|
14
|
+
#
|
|
15
|
+
# @return [String, nil] e.g. `switch-backend redis`
|
|
16
|
+
attr_reader :command
|
|
17
|
+
|
|
18
|
+
# @param message [String, nil]
|
|
19
|
+
# @param command [String, nil] see {#command}
|
|
20
|
+
def initialize(message = nil, command: nil)
|
|
21
|
+
super(message)
|
|
22
|
+
@command = command
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# A lock operation the CLI would have explained on stderr and refused.
|
|
27
|
+
#
|
|
28
|
+
# The message is that text. `result` is the {Manager::Result} or
|
|
29
|
+
# {Skill::Result} behind it, so the records are available without parsing
|
|
30
|
+
# the message.
|
|
31
|
+
#
|
|
32
|
+
# @example
|
|
33
|
+
# begin
|
|
34
|
+
# facade.acquire("lib/**")
|
|
35
|
+
# rescue Agent::Lock::Refused => e
|
|
36
|
+
# e.result.record.agent_id
|
|
37
|
+
# end
|
|
38
|
+
class Refused < Error
|
|
39
|
+
# @return [Manager::Result, Skill::Result, nil] the operation that was refused
|
|
40
|
+
attr_reader :result
|
|
41
|
+
|
|
42
|
+
# @param message [String] the text the CLI would have written to stderr
|
|
43
|
+
# @param result [Manager::Result, Skill::Result, nil]
|
|
44
|
+
def initialize(message, result: nil)
|
|
45
|
+
super(message)
|
|
46
|
+
@result = result
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Redis was required and did not answer.
|
|
51
|
+
#
|
|
52
|
+
# A {RuntimeError}, so a caller can rescue the connection failure apart
|
|
53
|
+
# from a {Refused} lock. Raised while a {Facade} is constructed, before
|
|
54
|
+
# any lock is taken, when every strategy has been tried and none was the
|
|
55
|
+
# file store.
|
|
56
|
+
#
|
|
57
|
+
# @example
|
|
58
|
+
# Agent::Lock::Facade.new(strategies: %w[redis], redis_url: "redis://127.0.0.1:1")
|
|
59
|
+
class RedisUnavailableError < RuntimeError; end
|
|
9
60
|
end
|
|
10
61
|
end
|