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
data/Rakefile CHANGED
@@ -12,7 +12,7 @@ end
12
12
 
13
13
  desc "Clean all temporary files"
14
14
  task :clean do
15
- shell("rm -rf pkg/ tmp/ coverage/ doc/ ")
15
+ shell("rm -rf pkg/ tmp/ coverage/ doc/ .yardoc/")
16
16
  end
17
17
 
18
18
  desc "Build the gem locally into pkg/"
@@ -32,11 +32,18 @@ end
32
32
  desc "Build the gem and set the permissions"
33
33
  task build: :permissions
34
34
 
35
- desc "Generate yard documentation"
36
- YARD::Rake::YardocTask.new(:doc) do |t|
37
- t.files = %w[lib/**/*.rb exe/*.rb - README.md CHANGELOG.md AGENTS.md LICENSE.txt]
38
- t.options.unshift("--title", '"FlowEngine — DSL + AST for buildiong complex flows in Ruby."')
39
- t.after = -> { exec("open doc/index.html") } if RUBY_PLATFORM.match?(/darwin/)
35
+ # Files, title and markup come from .yardopts, so `yard doc`, `yard server`
36
+ # and this task all build the same site. The output, doc/, is git-ignored.
37
+ YARD::Rake::YardocTask.new(:yard)
38
+
39
+ desc "Generate the YARD documentation into doc/ and open doc/index.html"
40
+ task doc: :yard do
41
+ index = File.expand_path("doc/index.html", __dir__)
42
+ opener = RUBY_PLATFORM.include?("darwin") ? "open" : "xdg-open"
43
+
44
+ # An open that fails, on a headless box or in CI, is not a failed build:
45
+ # the docs are on disk either way.
46
+ system(opener, index) || warn("Documentation written to #{index}")
40
47
  end
41
48
 
42
49
  RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,21 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <svg xmlns="http://www.w3.org/2000/svg" width="99" height="20">
3
+ <linearGradient id="b" x2="0" y2="100%">
4
+ <stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
5
+ <stop offset="1" stop-opacity=".1"/>
6
+ </linearGradient>
7
+ <mask id="a">
8
+ <rect width="99" height="20" rx="3" fill="#fff"/>
9
+ </mask>
10
+ <g mask="url(#a)">
11
+ <path fill="#555" d="M0 0h63v20H0z"/>
12
+ <path fill="#4c1" d="M63 0h36v20H63z"/>
13
+ <path fill="url(#b)" d="M0 0h99v20H0z"/>
14
+ </g>
15
+ <g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
16
+ <text x="31.5" y="15" fill="#010101" fill-opacity=".3">coverage</text>
17
+ <text x="31.5" y="14">coverage</text>
18
+ <text x="80" y="15" fill="#010101" fill-opacity=".3">97%</text>
19
+ <text x="80" y="14">97%</text>
20
+ </g>
21
+ </svg>
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" width="76" height="20" role="img" aria-label="docs: YARD"><title>docs: YARD</title><filter id="blur"><feGaussianBlur stdDeviation="16"/></filter><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="r"><rect width="76" height="20" rx="3"/></clipPath><g clip-path="url(#r)"><rect width="35" height="20" fill="#555"/><rect x="35" width="41" height="20" fill="#007ec6"/><rect width="76" height="20" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><g transform="scale(.1)"><g aria-hidden="true" fill="#010101"><text x="185" y="150" fill-opacity=".8" filter="url(#blur)" textLength="250">docs</text><text x="185" y="150" fill-opacity=".3" textLength="250">docs</text></g><text x="185" y="140" textLength="250">docs</text></g><g transform="scale(.1)"><g aria-hidden="true" fill="#010101"><text x="545" y="150" fill-opacity=".8" filter="url(#blur)" textLength="310">YARD</text><text x="545" y="150" fill-opacity=".3" textLength="310">YARD</text></g><text x="545" y="140" textLength="310">YARD</text></g></g></svg>
Binary file
@@ -0,0 +1,29 @@
1
+ # Sequence Diagram of Agent Lock Locking
2
+
3
+ ```mermaid
4
+ ---
5
+ config:
6
+ layout: elk
7
+ look: handDrawn
8
+ fontFamily: Open Sans Variable, sans-serif
9
+ theme: redux-color
10
+ ---
11
+ sequenceDiagram
12
+ participant O as Orchestrator (alock)
13
+ participant A as Sub-agent billing-a
14
+ participant B as Sub-agent billing-b
15
+ participant S as Lock store (.git/agent-locks)
16
+ O->>S: acquire src/**
17
+ S-->>O: ACQUIRED
18
+ par fan out
19
+ A->>S: AGENT_ID=billing-a acquire src/billing/**
20
+ S-->>A: ACQUIRED, parent inferred as the orchestrator
21
+ and
22
+ B->>S: AGENT_ID=billing-b acquire src/billing/tax.rb
23
+ S-->>B: REFUSED, held by billing-a
24
+ end
25
+ B->>S: AGENT_ID=billing-b acquire src/shipping/**
26
+ S-->>B: ACQUIRED
27
+ A->>S: AGENT_ID=billing-a release-all
28
+ Note over O,S: the orchestrator's src/** survives its children's release-all
29
+ ```
Binary file
data/lefthook.yml ADDED
@@ -0,0 +1,39 @@
1
+ pre-commit:
2
+ parallel: true
3
+ jobs:
4
+ # One after the other: rubocop rewrites the staged Ruby files, and the
5
+ # specs should run against what is actually being committed.
6
+ - name: ruby
7
+ group:
8
+ piped: true
9
+ jobs:
10
+ - name: rubocop
11
+ glob: "**/*.{rb,rake,gemspec}"
12
+ # RuboCop honours AllCops/Exclude when it walks the tree itself,
13
+ # but ignores it for paths passed on the command line, which is
14
+ # what {staged_files} does. --force-exclusion restores it.
15
+ run: bundle exec rubocop -a --force-exclusion --format progress {staged_files}
16
+ stage_fixed: true
17
+
18
+ # Only the spec files in this commit; the whole suite belongs to
19
+ # `just test` and to CI. Git exports GIT_DIR and GIT_INDEX_FILE to
20
+ # its hooks, and a spec's `git init` in a temp directory then
21
+ # rewrote this repository's own .git/config (issue #51).
22
+ # spec_helper.rb drops them too; this is the second guard. The full
23
+ # path, because uv installs a ~/.local/bin/env shell script that
24
+ # shadows env(1), ignores its arguments and exits 0: the job then
25
+ # ran no specs at all and still reported success.
26
+ - name: rspec
27
+ glob: "spec/**/*_spec.rb"
28
+ run: /usr/bin/env -u GIT_DIR -u GIT_INDEX_FILE -u GIT_WORK_TREE -u GIT_COMMON_DIR -u GIT_PREFIX bundle exec rspec --format progress {staged_files}
29
+
30
+ - name: gitleaks
31
+ # Scans only staged changes; a non-zero exit blocks the commit. Fails
32
+ # closed when gitleaks is missing, because the scan is mandatory.
33
+ run: |
34
+ if ! command -v gitleaks >/dev/null 2>&1; then
35
+ echo "gitleaks is not installed; secret scanning is required before committing."
36
+ echo "Install it: brew install gitleaks"
37
+ exit 1
38
+ fi
39
+ gitleaks git --pre-commit --redact --staged --no-banner --
@@ -1,38 +1,38 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "base"
3
+ require_relative "tree_command"
4
4
 
5
5
  module Agent
6
6
  module Lock
7
7
  module CLI
8
8
  module Commands
9
9
  # Claim a scope, or find out who has it.
10
- class Acquire < Base
11
- desc "Claim a path or glob; exits 1 if somebody else holds it"
10
+ class Acquire < TreeCommand
11
+ desc "Claim a path or glob before writing; exit 1 if another agent has it"
12
12
 
13
- argument :scope, required: true, desc: "A path, a directory, or a glob such as 'workflow/**'"
13
+ argument :scope, required: true, desc: "A path, a directory, or a glob such as 'lib/**'"
14
14
  argument :intent, required: false, desc: "What you are about to do, for whoever reads the lock"
15
15
 
16
16
  option :enforce, type: :boolean, default: false,
17
- desc: "Also make the matched files unwritable (macOS)"
18
- option :force, type: :boolean, default: false,
19
- desc: "Allow --enforce on a very wide scope"
17
+ desc: "Also freeze the matched files with chflags uchg (macOS only)"
18
+ option :allow_wide, type: :boolean, default: false,
19
+ desc: "Let --enforce freeze more than #{Freeze::LIMIT} files"
20
20
 
21
21
  example [
22
- "workflow/** 'rewriting the installer'",
23
- "lib/agent/lock/cli.rb 'adding the json flag'",
24
- "'**' 'a migration that touches everything'"
22
+ "'lib/billing/**' 'rewriting the invoices'",
23
+ "lib/billing/tax.rb 'fixing the rounding'",
24
+ "'config/credentials/**' 'keys must not move' --enforce"
25
25
  ]
26
26
 
27
- # @param scope [String]
28
- # @param intent [String, nil]
29
- # @param options [Hash]
27
+ # @param scope [String] as typed: a path, a directory or a glob
28
+ # @param intent [String, nil] what the holder is about to do
29
+ # @param options [Hash] `:dir`, `:locking_strategy`, `:redis_url`, `:enforce`, `:allow_wide`
30
30
  # @return [Manager::Result]
31
31
  def call(scope:, intent: nil, **options)
32
- manager = manager(options[:dir])
32
+ manager = manager(options)
33
33
  result = manager.acquire(
34
34
  scope, intent: intent || "unspecified",
35
- enforce: options[:enforce], force: options[:force]
35
+ enforce: options[:enforce], wide: options[:allow_wide]
36
36
  )
37
37
 
38
38
  report(result, manager.stale_minutes)
@@ -43,12 +43,14 @@ module Agent
43
43
 
44
44
  # @param result [Manager::Result]
45
45
  # @param stale_minutes [Integer]
46
+ # @return [void]
46
47
  def report(result, stale_minutes)
47
48
  case result.status
48
49
  when :acquired then report_acquired(result)
49
- when :already_mine then say("ALREADY YOURS #{result.record.scope}")
50
+ when :already_mine then say("ALREADY YOURS #{scope_of(result.record)}")
50
51
  when :held
51
52
  warn_("REFUSED, do not write here")
53
+ warn_(result.message) if result.message
52
54
  report_held(result.records, stale_minutes: stale_minutes)
53
55
  when :interrupted then report_interrupted(result.record)
54
56
  when :parent_scope then report_parent_scope(result.record)
@@ -60,14 +62,17 @@ module Agent
60
62
  # the very one this child would have to write.
61
63
  #
62
64
  # @param record [Record] the parent's lock on this exact scope
65
+ # @return [void]
63
66
  def report_parent_scope(record)
64
- warn_("REFUSED: #{record.scope} is your parent's (#{record.agent_id}) whole claim; " \
67
+ warn_("REFUSED: #{scope_of(record)} is your parent's (#{holder_of(record)}) whole claim; " \
65
68
  "claim a narrower scope inside it")
66
69
  end
67
70
 
71
+ # @param result [Manager::Result] an :acquired result
72
+ # @return [void]
68
73
  def report_acquired(result)
69
74
  record = result.record
70
- say("ACQUIRED #{record.scope} (holder: #{record.agent_id})")
75
+ say("ACQUIRED #{scope_of(record)} (holder: #{holder_of(record)})")
71
76
  say("FROZEN #{record.frozen_paths.size} file(s)") if record.frozen_paths.any?
72
77
  warn_(result.message) if result.message
73
78
  end
@@ -76,8 +81,9 @@ module Agent
76
81
  # are spelled out rather than left to be looked up.
77
82
  #
78
83
  # @param record [Record] the orphaned lock on this exact scope
84
+ # @return [void]
79
85
  def report_interrupted(record)
80
- warn_("INTERRUPTED WORK on #{record.scope}, left by #{record.agent_id}")
86
+ warn_("INTERRUPTED WORK on #{scope_of(record)}, left by #{holder_of(record)}")
81
87
  advise_interrupted(record)
82
88
  end
83
89
  end
@@ -8,6 +8,7 @@ require "json"
8
8
  module Agent
9
9
  module Lock
10
10
  module CLI
11
+ # One class per `alock` verb, all built on {Base}.
11
12
  module Commands
12
13
  # What every command shares: the launcher it prints through, the
13
14
  # manager it delegates to, and the wording of a held lock.
@@ -15,15 +16,15 @@ module Agent
15
16
  # Nothing here calls `puts` or `exit`. Output goes to the launcher's
16
17
  # streams and the exit code is handed back to it, which is what lets
17
18
  # the whole CLI run inside the test process.
19
+ #
20
+ # Commands that read or write a lock store subclass {TreeCommand}
21
+ # instead, which adds `--dir`. The rest (`version`, `whoami`,
22
+ # `skills`) never look at a tree, so offering them `--dir` would be
23
+ # a flag that silently does nothing.
24
+ #
25
+ # @abstract Subclass and implement `#call(**options)`.
18
26
  class Base < Dry::CLI::Command
19
- # Inherited by every command, so `--dir` works everywhere without
20
- # each one restating it.
21
- def self.inherited(subclass)
22
- super
23
- subclass.option :dir, type: :string, default: ".",
24
- desc: "Work as if run from this directory"
25
- end
26
-
27
+ # @return [Launcher] the streams, program name and exit code holder
27
28
  attr_reader :launcher
28
29
 
29
30
  # @param launcher [Launcher]
@@ -34,8 +35,10 @@ module Agent
34
35
 
35
36
  private
36
37
 
38
+ # @return [IO] where the deliverable goes
37
39
  def stdout = launcher.stdout
38
40
 
41
+ # @return [IO] where refusals, hints and warnings go
39
42
  def stderr = launcher.stderr
40
43
 
41
44
  # What to call this CLI in a hint, so the command it suggests is
@@ -44,11 +47,8 @@ module Agent
44
47
  # @return [String]
45
48
  def program = launcher.program
46
49
 
47
- # @param dir [String]
48
- # @return [Manager]
49
- def manager(dir) = Manager.new(tree: Tree.for(dir || "."))
50
-
51
- # @param text [String]
50
+ # @param text [String] one line of the deliverable
51
+ # @return [void]
52
52
  def say(text) = stdout.puts(text)
53
53
 
54
54
  # STDOUT is flushed first. An agent harness reads both streams down
@@ -56,13 +56,37 @@ module Agent
56
56
  # without the flush every hint arrives ahead of the record it is
57
57
  # about.
58
58
  #
59
- # @param text [String]
59
+ # @param text [String] one line of diagnostics
60
+ # @return [void]
60
61
  def warn_(text)
61
62
  stdout.flush
62
63
  stderr.puts(text)
63
64
  end
64
65
 
65
- # @param result [Manager::Result]
66
+ # A record is whatever some host sharing the store wrote, so every
67
+ # field read from one is printed through this. See {Text}.
68
+ #
69
+ # @param text [#to_s]
70
+ # @param lines [Boolean] keep newlines, for an intent with notes
71
+ # @return [String] `text` with every control character shown as an escape
72
+ def printable(text, lines: false) = Text.printable(text, lines:)
73
+
74
+ # @param record [Record]
75
+ # @return [String] its scope, {#printable}
76
+ def scope_of(record) = printable(record.scope)
77
+
78
+ # @param record [Record]
79
+ # @return [String] its holder, {#printable}
80
+ def holder_of(record) = printable(record.agent_id)
81
+
82
+ # @param text [#to_s] a field that lands inside a suggested command
83
+ # @return [String] {#printable}, then quoted for a shell
84
+ def shell(text) = Text.shell(text)
85
+
86
+ # Hand the result's exit code to the launcher, which exits with it.
87
+ #
88
+ # @param result [Manager::Result, Skill::Result]
89
+ # @return [Manager::Result, Skill::Result] the same result
66
90
  def finish(result)
67
91
  launcher.exit_code = result.code
68
92
  result
@@ -76,28 +100,38 @@ module Agent
76
100
  #
77
101
  # @param records [Array<Record>]
78
102
  # @param stale_minutes [Integer] Manager#stale_minutes
103
+ # @return [void]
79
104
  def report_held(records, stale_minutes:)
80
105
  records.each do |record|
81
- warn_(["HELD #{record.scope} by #{record.agent_id} since #{record.created_at}",
82
- tag_for(record, stale_minutes)].compact.join(" "))
83
- warn_(" intent: #{record.intent}") unless record.intent.empty?
106
+ warn_(held_line(record, stale_minutes))
107
+ warn_(" intent: #{printable(record.intent, lines: true)}") unless record.intent.empty?
84
108
  warn_(" frozen: #{record.frozen_paths.size} file(s)") if record.frozen_paths.any?
85
109
  warn_stale(record, stale_minutes) if record.stale?(stale_minutes)
86
110
  end
87
111
  end
88
112
 
113
+ # @param record [Record]
114
+ # @param stale_minutes [Integer]
115
+ # @return [String] who holds it and since when, tagged STALE or INTERRUPTED
116
+ def held_line(record, stale_minutes)
117
+ ["HELD #{scope_of(record)} by #{holder_of(record)} since #{printable(record.created_at)}",
118
+ tag_for(record, stale_minutes)].compact.join(" ")
119
+ end
120
+
89
121
  # One line per record, tagged, so that a line lifted out by `grep`
90
122
  # still says whether it is a claim in good standing.
91
123
  #
92
124
  # @param records [Array<Record>]
93
125
  # @param json [Boolean]
94
126
  # @param stale_minutes [Integer] Manager#stale_minutes
127
+ # @return [void]
95
128
  def report_records(records, json:, stale_minutes:)
96
129
  return say(JSON.pretty_generate(records.map { |record| as_json(record, stale_minutes) })) if json
97
130
 
98
131
  records.each do |record|
99
- say([record.scope, record.agent_id, record.created_at, tag_for(record, stale_minutes)].compact.join("\t"))
100
- say(" #{record.intent}") unless record.intent.empty?
132
+ say([*[record.scope, record.agent_id, record.created_at].map { |field| printable(field) },
133
+ tag_for(record, stale_minutes)].compact.join("\t"))
134
+ say(" #{printable(record.intent, lines: true)}") unless record.intent.empty?
101
135
  end
102
136
  end
103
137
 
@@ -105,9 +139,10 @@ module Agent
105
139
  # left to be looked up, since each throws away something different.
106
140
  #
107
141
  # @param record [Record] an orphaned lock
142
+ # @return [void]
108
143
  def advise_interrupted(record)
109
- warn_(" #{program} resume #{record.scope} # take it back, notes and all")
110
- warn_(" #{program} break #{record.scope} # throw it away and start over")
144
+ warn_(" #{program} resume #{shell(record.scope)} # take it back, notes and all")
145
+ warn_(" #{program} break #{shell(record.scope)} # throw it away and start over")
111
146
  end
112
147
 
113
148
  # @param record [Record]
@@ -126,9 +161,10 @@ module Agent
126
161
  #
127
162
  # @param record [Record]
128
163
  # @param stale_minutes [Integer]
164
+ # @return [void]
129
165
  def warn_stale(record, stale_minutes)
130
166
  warn_(" stale: untouched for over #{stale_minutes} minutes; ask its holder, " \
131
- "or announce it before `#{program} break #{record.scope}`")
167
+ "or announce it before `#{program} break #{shell(record.scope)}`")
132
168
  end
133
169
 
134
170
  # `stale` is computed, not stored, so it is added here; `status`
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "base"
3
+ require_relative "tree_command"
4
4
 
5
5
  module Agent
6
6
  module Lock
@@ -11,19 +11,24 @@ module Agent
11
11
  # Deliberately blunt and deliberately loud: an expired lock is reaped
12
12
  # on its own, so reaching for this means overruling a holder that still
13
13
  # looks alive. Whoever runs it owns the consequence.
14
- class Break < Base
15
- desc "Steal a lock; announce it first"
14
+ class Break < TreeCommand
15
+ desc "Take a lock from its holder, even a live one; announce it first"
16
16
 
17
- argument :scope, required: true, desc: "The path or glob to take"
17
+ argument :scope, required: true, desc: "The scope exactly as it was claimed"
18
18
 
19
+ example ["'docs/**'"]
20
+
21
+ # @param scope [String]
22
+ # @param options [Hash] `:dir`, `:locking_strategy`, `:redis_url`
23
+ # @return [Manager::Result] :broken or :not_found
19
24
  def call(scope:, **options)
20
- result = manager(options[:dir]).break_lock(scope)
25
+ result = manager(options).break_lock(scope)
21
26
 
22
27
  if result.status == :not_found
23
28
  say("NOT LOCKED #{scope}")
24
29
  else
25
- warn_("BREAKING #{result.record.scope}, held by #{result.record.agent_id}")
26
- warn_(" intent: #{result.record.intent}") unless result.record.intent.empty?
30
+ warn_("BREAKING #{scope_of(result.record)}, held by #{holder_of(result.record)}")
31
+ warn_(" intent: #{printable(result.record.intent, lines: true)}") unless result.record.intent.empty?
27
32
  say("Broken. You are responsible for having announced this first.")
28
33
  end
29
34
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "base"
3
+ require_relative "tree_command"
4
4
 
5
5
  module Agent
6
6
  module Lock
@@ -8,20 +8,20 @@ module Agent
8
8
  module Commands
9
9
  # Ask before writing. Exits 1 when anything overlapping is held, so a
10
10
  # shell script can gate on it without parsing anything.
11
- class Check < Base
12
- desc "Say who holds a path; exits 1 if it is held"
11
+ class Check < TreeCommand
12
+ desc "Say who holds a path; exit 1 if another agent does"
13
13
 
14
14
  argument :scope, required: true, desc: "A path, a directory, or a glob"
15
15
 
16
- option :json, type: :boolean, default: false, desc: "Machine-readable output"
16
+ option :json, type: :boolean, default: false, desc: "Print the matching locks as a JSON array"
17
17
 
18
- example ["workflow/**", "lib/agent/lock/cli.rb --json"]
18
+ example ["lib/billing/tax.rb", "'lib/**' --json"]
19
19
 
20
20
  # @param scope [String]
21
- # @param options [Hash]
22
- # @return [Manager::Result]
21
+ # @param options [Hash] `:dir`, `:locking_strategy`, `:redis_url`, `:json`
22
+ # @return [Manager::Result] :free, :mine or :held
23
23
  def call(scope:, **options)
24
- manager = manager(options[:dir])
24
+ manager = manager(options)
25
25
  result = manager.check(scope)
26
26
  report(result, scope, json: options[:json], stale_minutes: manager.stale_minutes)
27
27
  finish(result)
@@ -33,6 +33,7 @@ module Agent
33
33
  # @param scope [String] as the user typed it
34
34
  # @param json [Boolean]
35
35
  # @param stale_minutes [Integer]
36
+ # @return [void]
36
37
  def report(result, scope, json:, stale_minutes:)
37
38
  case result.status
38
39
  when :free then say(json ? "[]" : "FREE #{scope}")
@@ -40,7 +41,7 @@ module Agent
40
41
  # Held, but by you: safe to write, and worth saying which of your
41
42
  # own locks covers it rather than a bare "free".
42
43
  report_records(result.records, json: json, stale_minutes: stale_minutes)
43
- say("YOURS #{result.record.scope}") unless json
44
+ say("YOURS #{scope_of(result.record)}") unless json
44
45
  when :held
45
46
  if json
46
47
  report_records(result.records, json: true, stale_minutes: stale_minutes)
@@ -6,43 +6,26 @@ module Agent
6
6
  module Lock
7
7
  module CLI
8
8
  module Commands
9
- # `alock completion bash|zsh`, from dry-cli-autocomplete, with the two
10
- # things that gem's command cannot do for us restated here.
9
+ # `alock completion bash|zsh`, from dry-cli-autocomplete.
11
10
  #
12
- # It writes to `$stdout` directly, which the in-process launcher can
13
- # neither capture nor redirect, and it binds a registry by way of
14
- # `Class.new`, which is exactly where dry-cli clears an inherited
15
- # `desc` and `example`. Both belong upstream; until then, here.
11
+ # Upstream binds a registry by subclassing this command, restores
12
+ # the description and examples dry-cli's `inherited` hook clears, and
13
+ # writes to the stream dry-cli hands it. All that is left to say here
14
+ # is the wording, and where a Homebrew bash keeps its completions.
16
15
  class Completion < Dry::CLI::Autocomplete::Command
17
- desc "Print a shell completion script for bash or zsh"
16
+ desc "Print a bash or zsh completion script"
18
17
 
19
- # Spelled out rather than taken from $PROGRAM_NAME, which under a
20
- # test runner names the runner, and which upstream reads at load
21
- # time anyway.
22
- example [
23
- "bash > \"$(brew --prefix)/etc/bash_completion.d/alock\"",
24
- "zsh > \"${fpath[1]}/_alo\""
25
- ]
26
-
27
- # Binding a registry subclasses this command, and dry-cli's
28
- # `inherited` hook starts every subclass with no description and no
29
- # examples. Arguments and options survive it; those two do not, so
30
- # they are copied across by hand.
18
+ # Upstream calls this from inside the subclass it builds, so
19
+ # overriding it here changes the examples `--help` prints.
31
20
  #
32
- # @param registry [Dry::CLI::Registry]
33
- # @return [Class]
34
- def self.[](registry, program_name: nil)
35
- super.tap do |bound|
36
- bound.desc(description)
37
- bound.example(*examples)
38
- end
21
+ # @param name [String] the program the script completes
22
+ # @return [Array<String>]
23
+ def self.examples_for(name)
24
+ [
25
+ "bash > \"$(brew --prefix)/etc/bash_completion.d/#{name}\"",
26
+ "zsh > \"${fpath[1]}/_#{name}\""
27
+ ]
39
28
  end
40
-
41
- private
42
-
43
- # dry-cli hands every command the stream it should be writing to.
44
- # The command upstream ignores it; this one does not.
45
- def out = @out || $stdout
46
29
  end
47
30
  end
48
31
  end