Module: Agent::Lock::Facade::Refusal

Defined in:
lib/agent/lock/facade/refusal.rb

Overview

Raises Refused for a result the command line would have refused, and hands every other result back unchanged.

The message says what the CLI says on stderr, worded for a log line: who holds the scope and what to do instead.

Constant Summary collapse

MESSAGES =

One message per refusing status. scope is the scope as the caller typed it, for the one status (:not_found) that has no record.

{
  held: lambda { |result, _scope|
    ["REFUSED, do not write here", result.message,
     *result.records.map { |record| Refusal.held_line(record) }].compact.join("\n")
  },
  parent_scope: lambda { |result, _scope|
    record = result.record
    "REFUSED: #{Refusal.scope_of(record)} is your parent's (#{Refusal.holder_of(record)}) whole claim; " \
      "claim a narrower scope inside it"
  },
  # Manager#rebind can report :interrupted with no record left to name.
  interrupted: lambda { |result, scope|
    record = result.record
    owner = record ? ", left by #{Refusal.holder_of(record)}" : ""
    "INTERRUPTED WORK on #{Text.printable(record&.scope || scope)}#{owner}; resume it or break it"
  },
  refused: lambda { |result, _scope|
    "REFUSED: #{Refusal.scope_of(result.record)} is held by #{Refusal.holder_of(result.record)}, not you"
  },
  not_found: ->(_result, scope) { "NOT LOCKED #{scope}, take the lock before writing in it" },
  differs: lambda { |result, _scope|
    "REFUSED: #{result.path} differs from the copy this gem ships; pass overwrite: true to replace it"
  },
  linked: lambda { |result, _scope|
    "REFUSED: #{result.path} is a symlink, so something else installs it; left alone"
  }
}.freeze
REFUSING =

Refused by every verb that can return them. :not_found is not: releasing or breaking a free scope is a success.

%i[held parent_scope interrupted refused differs linked].freeze

Class Method Summary collapse

Class Method Details

.check!(result, scope: nil, also: []) ⇒ Manager::Result, Skill::Result

Returns the same result, when it is not a refusal.

Parameters:

  • result (Manager::Result, Skill::Result)
  • scope (String, nil) (defaults to: nil) —

    as the caller typed it

  • also (Array<Symbol>) (defaults to: []) —

    statuses this verb refuses on top of REFUSING

Returns:

Raises:



55
56
57
58
59
# File 'lib/agent/lock/facade/refusal.rb', line 55

def check!(result, scope: nil, also: [])
  return result unless REFUSING.include?(result.status) || also.include?(result.status)

  raise Refused.new(MESSAGES.fetch(result.status).call(result, scope), result: result)
end

.held_line(record) ⇒ String

Returns e.g. "HELD lib/** by luke since 2026-11-25T10:00:00Z intent: billing".

Parameters:

Returns:

  • (String) —

    e.g. "HELD lib/** by luke since 2026-11-25T10:00:00Z intent: billing"



71
72
73
74
# File 'lib/agent/lock/facade/refusal.rb', line 71

def held_line(record)
  line = "HELD  #{scope_of(record)}  by #{holder_of(record)}  since #{Text.printable(record.created_at)}"
  record.intent.empty? ? line : "#{line}  intent: #{Text.printable(record.intent, lines: true)}"
end

.holder_of(record) ⇒ String

Returns its holder, made safe to log (Text.printable).

Parameters:

Returns:



67
# File 'lib/agent/lock/facade/refusal.rb', line 67

def holder_of(record) = Text.printable(record.agent_id)

.scope_of(record) ⇒ String

Returns its scope, made safe to log (Text.printable).

Parameters:

Returns:



63
# File 'lib/agent/lock/facade/refusal.rb', line 63

def scope_of(record) = Text.printable(record.scope)