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.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/.gitleaks.toml +23 -0
  3. data/AGENTS.md +11 -17
  4. data/CHANGELOG.md +124 -3
  5. data/README.md +496 -100
  6. data/Rakefile +13 -6
  7. data/docs/badges/coverage_badge.svg +21 -0
  8. data/docs/badges/yardoc.svg +1 -0
  9. data/docs/img/alock-sequence.avif +0 -0
  10. data/docs/img/alock-sequence.md +29 -0
  11. data/docs/img/alock-sequence.png +0 -0
  12. data/lefthook.yml +39 -0
  13. data/lib/agent/lock/cli/commands/acquire.rb +25 -19
  14. data/lib/agent/lock/cli/commands/base.rb +59 -23
  15. data/lib/agent/lock/cli/commands/break.rb +12 -7
  16. data/lib/agent/lock/cli/commands/check.rb +10 -9
  17. data/lib/agent/lock/cli/commands/completion.rb +15 -32
  18. data/lib/agent/lock/cli/commands/list.rb +46 -37
  19. data/lib/agent/lock/cli/commands/listing.rb +170 -0
  20. data/lib/agent/lock/cli/commands/mine.rb +7 -5
  21. data/lib/agent/lock/cli/commands/note.rb +13 -9
  22. data/lib/agent/lock/cli/commands/release.rb +10 -7
  23. data/lib/agent/lock/cli/commands/release_all.rb +7 -5
  24. data/lib/agent/lock/cli/commands/resume.rb +13 -7
  25. data/lib/agent/lock/cli/commands/show.rb +172 -0
  26. data/lib/agent/lock/cli/commands/skills.rb +28 -19
  27. data/lib/agent/lock/cli/commands/switch_backend.rb +205 -0
  28. data/lib/agent/lock/cli/commands/tree_command.rb +72 -0
  29. data/lib/agent/lock/cli/commands/version.rb +2 -1
  30. data/lib/agent/lock/cli/commands/whoami.rb +7 -3
  31. data/lib/agent/lock/cli.rb +63 -19
  32. data/lib/agent/lock/error.rb +52 -1
  33. data/lib/agent/lock/facade/async.rb +83 -0
  34. data/lib/agent/lock/facade/backend.rb +133 -0
  35. data/lib/agent/lock/facade/memo.rb +30 -0
  36. data/lib/agent/lock/facade/refusal.rb +78 -0
  37. data/lib/agent/lock/facade/session.rb +142 -0
  38. data/lib/agent/lock/facade/strategies.rb +36 -0
  39. data/lib/agent/lock/facade.rb +369 -0
  40. data/lib/agent/lock/freeze.rb +59 -5
  41. data/lib/agent/lock/identity.rb +63 -5
  42. data/lib/agent/lock/launcher.rb +17 -1
  43. data/lib/agent/lock/manager/enforcement.rb +98 -0
  44. data/lib/agent/lock/manager/switch.rb +91 -0
  45. data/lib/agent/lock/manager/thaw.rb +68 -0
  46. data/lib/agent/lock/manager.rb +176 -79
  47. data/lib/agent/lock/process_info/legacy_start.rb +60 -0
  48. data/lib/agent/lock/process_info.rb +42 -11
  49. data/lib/agent/lock/record.rb +124 -10
  50. data/lib/agent/lock/scope.rb +49 -4
  51. data/lib/agent/lock/skill.rb +14 -10
  52. data/lib/agent/lock/store/backend_name.rb +62 -0
  53. data/lib/agent/lock/store/file_system_store.rb +20 -1
  54. data/lib/agent/lock/store/redis_index.rb +179 -0
  55. data/lib/agent/lock/store/redis_store.rb +191 -33
  56. data/lib/agent/lock/store.rb +180 -19
  57. data/lib/agent/lock/text.rb +62 -0
  58. data/lib/agent/lock/tree.rb +143 -16
  59. data/lib/agent/lock/version.rb +2 -1
  60. data/lib/agent/lock.rb +3 -0
  61. data/skills/agent-lock/SKILL.md +75 -10
  62. metadata +41 -22
  63. data/justfile +0 -127
  64. data/sig/agent/lock.rbs +0 -6
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Agent
4
+ module Lock
5
+ class Facade
6
+ # `locker.async`: every lock method of a {Facade}, run off the calling
7
+ # thread and answered with a `Concurrent::Promises::Future`.
8
+ #
9
+ # A proxy of our own rather than `Concurrent::Async`, whose
10
+ # `method_missing` forwards positional arguments only: there,
11
+ # `async.acquire("lib/**", agent_id: "other")` folded the keywords
12
+ # into `intent`, and the lock was taken, reported as acquired, and
13
+ # signed by the facade's default holder in whatever checkout the
14
+ # process happened to be in. Here keywords and blocks reach the facade
15
+ # exactly as they would on a direct call.
16
+ #
17
+ # One facade's calls run one at a time, in the order they were made.
18
+ # Handing each call to the I/O pool on its own ran them in parallel:
19
+ # an `async.release` could overtake the `async.acquire` issued before
20
+ # it, and fifty `async.note` calls on one scope, each reading the
21
+ # record, adding its note and writing it back, kept two notes while
22
+ # every future reported `:noted`.
23
+ #
24
+ # A future nobody waits on can be dropped when the process exits with
25
+ # the pool still busy. Call `value!` or `wait` on every future whose
26
+ # outcome matters, a `release` most of all.
27
+ class Async
28
+ # The facade methods offered asynchronously: the ones that touch a
29
+ # store. `version`, `skills_path` and the like answer at once.
30
+ METHODS = %i[acquire release check list mine release_all break_lock note resume whoami].freeze
31
+
32
+ # concurrent-ruby is not a dependency of this gem. Only an `async:`
33
+ # facade needs it, so only an `async:` facade asks for it.
34
+ #
35
+ # @return [void]
36
+ # @raise [LoadError] naming the gem to add, when it is not installed
37
+ def self.load!
38
+ require "concurrent"
39
+ rescue LoadError => e
40
+ raise LoadError, "Agent::Lock::Facade.new(async: true) needs the concurrent-ruby gem: " \
41
+ "add `gem \"concurrent-ruby\"` to your Gemfile (#{e.message})"
42
+ end
43
+
44
+ # @param facade [Facade] the locker every call is made on
45
+ def initialize(facade)
46
+ @facade = facade
47
+ @queue = Concurrent::SerializedExecutionDelegator.new(Concurrent.global_io_executor)
48
+ @running = :"agent_lock_async_#{object_id}"
49
+ freeze
50
+ end
51
+
52
+ METHODS.each do |name|
53
+ # @return [Concurrent::Promises::Future] fulfilled with what the
54
+ # facade method returns, or rejected with what it raised
55
+ define_method(name) do |*args, **kwargs, &block|
56
+ Concurrent::Promises.future_on(executor) do
57
+ queued { @facade.public_send(name, *args, **kwargs, &block) }
58
+ end
59
+ end
60
+ end
61
+
62
+ private
63
+
64
+ # A call made from inside one of this proxy's own calls (the block
65
+ # of an `async.acquire`) runs where it stands. Queued, it would wait
66
+ # behind the very call that is waiting for it.
67
+ #
68
+ # @return [Symbol, Concurrent::SerializedExecutionDelegator]
69
+ def executor = Thread.current[@running] ? :immediate : @queue
70
+
71
+ # @yield the facade call
72
+ # @return [Object] whatever the call returns
73
+ def queued
74
+ nested = Thread.current[@running]
75
+ Thread.current[@running] = true
76
+ yield
77
+ ensure
78
+ Thread.current[@running] = nested
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,133 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "redis"
4
+ require_relative "../error"
5
+ require_relative "../store"
6
+ require_relative "../store/file_system_store"
7
+ require_relative "../store/redis_store"
8
+ require_relative "strategies"
9
+
10
+ module Agent
11
+ module Lock
12
+ class Facade
13
+ # The one store a facade uses for its whole life, chosen once.
14
+ #
15
+ # Strategies are tried in order: `file` is always usable, `redis` only
16
+ # when its URL answers. A list that runs out without either raises
17
+ # {RedisUnavailableError}.
18
+ #
19
+ # Frozen once built. The `::Redis` client it keeps is shared by every
20
+ # thread calling the facade; redis-rb serializes its own commands, so
21
+ # sharing it is safe.
22
+ class Backend
23
+ # Seconds to wait for Redis to answer the probe, so a dead URL costs
24
+ # a fraction of a second instead of the client's default timeout.
25
+ PROBE_TIMEOUT = 0.2
26
+
27
+ # @return [String] "redis" or "file"
28
+ attr_reader :name
29
+
30
+ # @return [String] the Redis URL this backend was given
31
+ attr_reader :redis_url
32
+
33
+ # @param strategies [Array<String, Symbol>] see {Strategies.parse}
34
+ # @param redis_url [String]
35
+ # @raise [ArgumentError] when a strategy name is unknown, the list is
36
+ # empty, or `redis_url` is tried and is not a Redis URL
37
+ # @raise [RedisUnavailableError] when Redis was required and did not answer
38
+ def initialize(strategies:, redis_url:)
39
+ @redis_url = redis_url
40
+ @name, @client = resolve(Strategies.parse(strategies))
41
+ freeze
42
+ end
43
+
44
+ # Bound by the checkout's backend marker, exactly like `alock`: a
45
+ # virgin tree is claimed for this backend, so the CLI follows the
46
+ # facade there, and a tree already claimed for the other backend is
47
+ # refused. Silently using another store would let a facade and an
48
+ # `alock` in one checkout both "acquire" the same scope.
49
+ #
50
+ # @param tree [Tree]
51
+ # @return [Store::FileSystemStore, Store::RedisStore] a store over `tree`
52
+ # @raise [Store::Mismatch] when the tree's locks live in the other backend
53
+ def store_for(tree)
54
+ # Read first: a claimed tree, the usual case, costs no write.
55
+ established = Store.recorded(tree) || Store.claim(tree, name)
56
+ unless established == name
57
+ raise Store::Mismatch, "this tree's locks live in #{established}, but this facade uses #{name}; " \
58
+ "build it with strategies: %w[#{established}]"
59
+ end
60
+
61
+ return Store::FileSystemStore.new(tree) if name == "file"
62
+
63
+ Store::RedisStore.new(tree, client: @client, url: redis_url)
64
+ end
65
+
66
+ # The default would print `@redis_url` as given, password and all,
67
+ # into whatever log or console inspects a facade.
68
+ #
69
+ # @return [String]
70
+ def inspect = "#<#{self.class.name} #{name} #{masked_url}>"
71
+
72
+ private
73
+
74
+ # @param names [Array<String>]
75
+ # @return [Array(String, ::Redis), Array(String, nil)]
76
+ # @raise [RedisUnavailableError]
77
+ def resolve(names)
78
+ names.each do |name|
79
+ return ["file", nil] if name == "file"
80
+
81
+ client = connect
82
+ return ["redis", client] if client
83
+ end
84
+
85
+ raise RedisUnavailableError, "Redis did not answer on #{masked_url}"
86
+ end
87
+
88
+ # An exception message is logged by whoever rescues it, and a Redis
89
+ # URL may carry a password.
90
+ #
91
+ # @return [String] the URL with any userinfo replaced by `***`
92
+ def masked_url = Store::RedisStore.safe(redis_url)
93
+
94
+ # Probes with a short-timeout client, then hands back one with the
95
+ # gem's default timeouts: the probe's 0.2s would otherwise apply to
96
+ # every lock operation on a slow, remote Redis.
97
+ #
98
+ # @return [::Redis, nil] a working client once Redis answered, or nil
99
+ # @raise [ArgumentError] when the URL is not one redis-rb can read
100
+ def connect
101
+ probe = client(timeout: PROBE_TIMEOUT, connect_timeout: PROBE_TIMEOUT)
102
+ probe.ping
103
+ client
104
+ rescue ::Redis::BaseError, SystemCallError, SocketError
105
+ nil
106
+ ensure
107
+ close_quietly(probe)
108
+ end
109
+
110
+ # redis-rb and URI quote the whole URL, password included, when they
111
+ # cannot read it. That error is raised again without the URL, and
112
+ # without a `cause`, which would carry the original message into
113
+ # every backtrace and error reporter. Keywords go to `::Redis.new`.
114
+ #
115
+ # @return [::Redis]
116
+ # @raise [ArgumentError] when the URL is not one redis-rb can read
117
+ def client(**)
118
+ ::Redis.new(url: redis_url, **)
119
+ rescue ArgumentError, URI::InvalidURIError => e
120
+ raise ArgumentError, "not a Redis URL: #{masked_url} (#{e.class})", cause: nil
121
+ end
122
+
123
+ # @param client [::Redis, nil]
124
+ # @return [void]
125
+ def close_quietly(client)
126
+ client&.close
127
+ rescue StandardError
128
+ nil
129
+ end
130
+ end
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Agent
4
+ module Lock
5
+ class Facade
6
+ # One value, built on first use by exactly one thread and reused after.
7
+ #
8
+ # A build that raises is not kept, so the next call tries again: a
9
+ # Redis that was down for {Agent::Lock.distributed} is retried, not
10
+ # remembered as down. Each memo has its own mutex, so a slow build of
11
+ # one never blocks another.
12
+ class Memo
13
+ # @yieldreturn [Object] the value, built under the memo's mutex
14
+ def initialize(&build)
15
+ @build = build
16
+ @mutex = Mutex.new
17
+ @value = nil
18
+ end
19
+
20
+ # @return [Object] the value, building it if this is the first call
21
+ def value = @mutex.synchronize { @value ||= @build.call }
22
+
23
+ # Forget the value, so the next {#value} builds a new one.
24
+ #
25
+ # @return [void]
26
+ def reset = @mutex.synchronize { @value = nil }
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../error"
4
+
5
+ module Agent
6
+ module Lock
7
+ class Facade
8
+ # Raises {Refused} for a result the command line would have refused,
9
+ # and hands every other result back unchanged.
10
+ #
11
+ # The message says what the CLI says on stderr, worded for a log
12
+ # line: who holds the scope and what to do instead.
13
+ module Refusal
14
+ # One message per refusing status. `scope` is the scope as the caller
15
+ # typed it, for the one status (`:not_found`) that has no record.
16
+ MESSAGES = {
17
+ held: lambda { |result, _scope|
18
+ ["REFUSED, do not write here", result.message,
19
+ *result.records.map { |record| Refusal.held_line(record) }].compact.join("\n")
20
+ },
21
+ parent_scope: lambda { |result, _scope|
22
+ record = result.record
23
+ "REFUSED: #{Refusal.scope_of(record)} is your parent's (#{Refusal.holder_of(record)}) whole claim; " \
24
+ "claim a narrower scope inside it"
25
+ },
26
+ # Manager#rebind can report :interrupted with no record left to name.
27
+ interrupted: lambda { |result, scope|
28
+ record = result.record
29
+ owner = record ? ", left by #{Refusal.holder_of(record)}" : ""
30
+ "INTERRUPTED WORK on #{Text.printable(record&.scope || scope)}#{owner}; resume it or break it"
31
+ },
32
+ refused: lambda { |result, _scope|
33
+ "REFUSED: #{Refusal.scope_of(result.record)} is held by #{Refusal.holder_of(result.record)}, not you"
34
+ },
35
+ not_found: ->(_result, scope) { "NOT LOCKED #{scope}, take the lock before writing in it" },
36
+ differs: lambda { |result, _scope|
37
+ "REFUSED: #{result.path} differs from the copy this gem ships; pass overwrite: true to replace it"
38
+ },
39
+ linked: lambda { |result, _scope|
40
+ "REFUSED: #{result.path} is a symlink, so something else installs it; left alone"
41
+ }
42
+ }.freeze
43
+
44
+ # Refused by every verb that can return them. `:not_found` is not:
45
+ # releasing or breaking a free scope is a success.
46
+ REFUSING = %i[held parent_scope interrupted refused differs linked].freeze
47
+
48
+ module_function
49
+
50
+ # @param result [Manager::Result, Skill::Result]
51
+ # @param scope [String, nil] as the caller typed it
52
+ # @param also [Array<Symbol>] statuses this verb refuses on top of {REFUSING}
53
+ # @return [Manager::Result, Skill::Result] the same result, when it is not a refusal
54
+ # @raise [Refused] when it is
55
+ def check!(result, scope: nil, also: [])
56
+ return result unless REFUSING.include?(result.status) || also.include?(result.status)
57
+
58
+ raise Refused.new(MESSAGES.fetch(result.status).call(result, scope), result: result)
59
+ end
60
+
61
+ # @param record [Record]
62
+ # @return [String] its scope, made safe to log ({Text.printable})
63
+ def scope_of(record) = Text.printable(record.scope)
64
+
65
+ # @param record [Record]
66
+ # @return [String] its holder, made safe to log ({Text.printable})
67
+ def holder_of(record) = Text.printable(record.agent_id)
68
+
69
+ # @param record [Record]
70
+ # @return [String] e.g. "HELD lib/** by luke since 2026-11-25T10:00:00Z intent: billing"
71
+ def held_line(record)
72
+ line = "HELD #{scope_of(record)} by #{holder_of(record)} since #{Text.printable(record.created_at)}"
73
+ record.intent.empty? ? line : "#{line} intent: #{Text.printable(record.intent, lines: true)}"
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,142 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../identity"
4
+ require_relative "../manager"
5
+ require_relative "../tree"
6
+
7
+ module Agent
8
+ module Lock
9
+ class Facade
10
+ # Who is asking, against which store: a {Manager} per call.
11
+ #
12
+ # Holds the facade's defaults and nothing that changes, and builds a
13
+ # fresh {Manager}, {Tree} and store for every call. Two threads never
14
+ # share one of those, which is what keeps a facade thread-safe without
15
+ # a lock of its own; the store's own mutex serializes the writes.
16
+ class Session
17
+ # What a lock's holder may be tied to, for liveness: see {#initialize}.
18
+ HOLDERS = %i[process session].freeze
19
+ # What a session's name may be made of: see {.name!}.
20
+ NAME = %r{\A[A-Za-z0-9._:@/+-]+\z}
21
+
22
+ # @param backend [Backend]
23
+ # @param agent_id [String, nil] the holder name when a call does not pass one
24
+ # @param parent_id [String, false, nil] the parent when a call does
25
+ # not pass one; `false` says there is none, where nil infers it
26
+ # @param stale_minutes [Integer, nil] nil reads `AGENT_LOCK_STALE_MINUTES`
27
+ # @param holder [Symbol, nil] `:process`, the default, ties a lock to this Ruby process,
28
+ # so a host that crashes is reaped like a dead `alock` session;
29
+ # `:session` ties it to the first non-shell ancestor, as `alock`
30
+ # does, for a short script whose lock should outlive it
31
+ # @param session_name [String, nil] the session's name, in place of
32
+ # `CLAUDE_SESSION_ID` and the fingerprint: a plan id, a job id.
33
+ # Its locks are tied to no process, so they survive any restart of
34
+ # the host and lapse only when untouched for `stale_minutes`
35
+ # @raise [ArgumentError] for any other holder, for `holder:` given
36
+ # beside `session_name:`, which ties a lock to no process at all,
37
+ # and for a name that is not one
38
+ # rubocop:disable-next Metrics/ParameterLists
39
+ def initialize(backend, agent_id: nil, parent_id: nil, stale_minutes: nil, holder: nil, session_name: nil)
40
+ @holder = Session.holder!(holder, named: !session_name.nil?)
41
+ @backend = backend
42
+ @agent_id = agent_id
43
+ @parent_id = Session.parent!(parent_id)
44
+ @stale_minutes = stale_minutes
45
+ @session_name = Session.name!(session_name)
46
+ freeze
47
+ end
48
+
49
+ class << self
50
+ # @param holder [Symbol, nil] nil is `:process`
51
+ # @param named [Boolean] whether the facade names its session
52
+ # @return [Symbol]
53
+ # @raise [ArgumentError]
54
+ def holder!(holder, named:)
55
+ if holder && named
56
+ raise ArgumentError, "holder: does not go with session_name:, which ties a lock to no process"
57
+ end
58
+ return holder || :process if holder.nil? || HOLDERS.include?(holder)
59
+
60
+ raise ArgumentError, "holder must be one of #{HOLDERS.join(", ")}, got #{holder.inspect}"
61
+ end
62
+
63
+ # `true` is refused, not read as some parent: it names nobody. So
64
+ # is a blank String, which would otherwise reach the environment
65
+ # as "no parent": `parent_id: ENV["X"].to_s` is a mistake, and
66
+ # `false` is how none is said on purpose.
67
+ #
68
+ # @param parent [String, false, nil]
69
+ # @return [String, false, nil]
70
+ # @raise [ArgumentError] for anything else
71
+ def parent!(parent)
72
+ return parent if parent.nil? || parent == false
73
+ return parent if parent.is_a?(String) && !parent.strip.empty?
74
+
75
+ raise ArgumentError, "parent_id must be a name, or false for none, got #{parent.inspect}"
76
+ end
77
+
78
+ # A holder id is pasted back into a shell, so it holds nothing a
79
+ # shell would read as more than a word.
80
+ #
81
+ # @param name [String, nil]
82
+ # @return [String, nil]
83
+ # @raise [ArgumentError] for a name that is not {NAME}
84
+ def name!(name)
85
+ return name if name.nil? || (name.is_a?(String) && name.match?(NAME))
86
+
87
+ raise ArgumentError, "session_name must be letters, digits and any of . _ : @ / + -, got #{name.inspect}"
88
+ end
89
+ end
90
+
91
+ # @param dir [String, nil] a directory inside the checkout; nil is the current one
92
+ # @param agent_id [String, nil] overrides the session's default
93
+ # @param parent_id [String, false, nil] overrides the session's default
94
+ # @param stale_minutes [Integer, nil] overrides the session's default
95
+ # @param session_name [String, nil] overrides the session's default
96
+ # @return [Manager] bound to that checkout, this backend and that identity
97
+ def manager(dir: nil, agent_id: nil, parent_id: nil, stale_minutes: nil, session_name: nil)
98
+ tree = Tree.for(dir || ".")
99
+ Manager.new(tree: tree, store: @backend.store_for(tree),
100
+ identity: identity(agent_id:, parent_id:, session_name:),
101
+ stale_minutes: stale_minutes || @stale_minutes)
102
+ end
103
+
104
+ # Read fresh from the environment on every call, so it follows
105
+ # `AGENT_ID` as that changes; a name from the call or the session
106
+ # overrides it, and the rest of the environment is kept so the
107
+ # session id and fingerprint still resolve. The pid is read per call
108
+ # too, so a forked child holds its own locks, not its parent's.
109
+ #
110
+ # A named session is tied to no process; `holder:` is for a facade
111
+ # that names none.
112
+ #
113
+ # @param agent_id [String, nil]
114
+ # @param parent_id [String, false, nil] `false` for a holder with no
115
+ # parent; nil uses the session's default, and without one infers it
116
+ # @param session_name [String, nil] overrides the session's default
117
+ # @return [Identity]
118
+ # @raise [ArgumentError] for a name or a parent that is not one
119
+ def identity(agent_id: nil, parent_id: nil, session_name: nil)
120
+ name = Session.name!(session_name) || @session_name
121
+ liveness = ::Process.pid if @holder == :process && name.nil?
122
+ parent = Session.parent!(parent_id).nil? ? @parent_id : parent_id
123
+ Identity.new(env: env_for(agent_id || @agent_id, parent),
124
+ liveness_pid: liveness, session_name: name)
125
+ end
126
+
127
+ private
128
+
129
+ # @param id [String, nil]
130
+ # @param parent [String, false, nil]
131
+ # @return [Hash{String => String}] the environment, with those two
132
+ # named. No parent is AGENT_PARENT_ID set and empty, as on the CLI
133
+ def env_for(id, parent)
134
+ env = ENV.to_h
135
+ env["AGENT_ID"] = id unless id.nil?
136
+ env["AGENT_PARENT_ID"] = parent || "" unless parent.nil?
137
+ env
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Agent
4
+ module Lock
5
+ class Facade
6
+ # Turns the `strategies:` a caller passed into the backend names a
7
+ # {Backend} tries, in order.
8
+ module Strategies
9
+ # Names that mean the file store.
10
+ FILE_ALIASES = %w[file filesystem].freeze
11
+
12
+ module_function
13
+
14
+ # @param strategies [Array<String, Symbol>] e.g. `%w[redis filesystem]`
15
+ # @return [Array<String>] "redis" and "file", in the order given, without repeats
16
+ # @raise [ArgumentError] when the list is empty or names an unknown backend
17
+ def parse(strategies)
18
+ names = Array(strategies).map { |name| canonical(name.to_s.strip.downcase) }.uniq
19
+ raise ArgumentError, "strategies must name at least one of redis, file" if names.empty?
20
+
21
+ names
22
+ end
23
+
24
+ # @param name [String]
25
+ # @return [String] "redis" or "file"
26
+ # @raise [ArgumentError] for anything else
27
+ def canonical(name)
28
+ return "file" if FILE_ALIASES.include?(name)
29
+ return name if name == "redis"
30
+
31
+ raise ArgumentError, "unknown strategy #{name.inspect}, expected redis or file"
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end