Class: Phronomy::Agent::Context::Capability::Base

Inherits:
RubyLLM::Tool
  • Object
show all
Defined in:
lib/phronomy/agent/context/capability/base.rb

Overview

Base class extending RubyLLM::Tool with Phronomy-specific DSL.

Additional DSL over RubyLLM::Tool:

  • tool_name : explicit function name exposed to the LLM (overrides auto-conversion)
  • scope : access-scope metadata (:read_only, :write, etc.)
  • on_error : error-handling policy (:raise or :return_empty)
  • on_schema_error : behavior when LLM passes schema-violating arguments :return_error (default), :raise, or :coerce
  • requires_approval : require human approval before execution
  • param :name, enum: [...] : restrict allowed values in the JSON Schema

Examples:

class SearchKnowledgeBase < Phronomy::Agent::Context::Capability::Base
  tool_name "search_kb"               # explicit name shown to the LLM
  description "Search the internal knowledge base"
  param :query,  type: :string, desc: "Search query"
  param :lang,   type: :string, desc: "Language", required: false, enum: %w[en ja fr]
  scope :read_only
  on_error :return_empty

  def execute(query:, lang: "en")
    KnowledgeBase.search(query, lang: lang)
  end
end

Direct Known Subclasses

Tools::Agent, Tools::Mcp, Tools::VectorSearch

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

._sleep_proc#call

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Injectable sleep callable for testing. Defaults to Kernel#sleep. mutant:disable - neutral failure: unparser round-trip produces different source

Returns:



250
251
252
# File 'lib/phronomy/agent/context/capability/base.rb', line 250

def _sleep_proc
  @_sleep_proc || method(:sleep)
end

Class Method Details

.execution_mode(value = nil) ⇒ Symbol

Sets or reads the execution mode for this tool.

Execution mode is the concurrency contract declaration for the tool. In Phronomy's non-preemptive, cooperative concurrency model it controls which runtime resource is used to dispatch the tool:

Mode Dispatcher Constraint
+:cooperative+ +Runtime.instance.spawn+ (scheduler task) Must not block the scheduler thread; use only for in-memory computation
+:blocking_io+ Concurrency::BlockingAdapterPool (bounded thread pool) Default. Safe for all blocking I/O (HTTP, DB, file)
+:cpu_bound+ Falls back to +:blocking_io+ + emits a warning No dedicated process pool yet; use +:blocking_io+ explicitly to suppress the warning
+:external_process+ Falls back to +:blocking_io+ No process manager yet

Tools that perform network calls, file I/O, or database queries should use +:blocking_io+ (the default). Tools that only perform in-memory computation may declare +:cooperative+ for lower overhead.

mutant:disable

Parameters:

  • value (Symbol, nil) (defaults to: nil)

    when nil, returns the current value

Returns:

  • (Symbol)

    the current execution mode (default :blocking_io)



126
127
128
129
130
131
132
133
134
135
# File 'lib/phronomy/agent/context/capability/base.rb', line 126

def execution_mode(value = nil)
  return @execution_mode || :blocking_io if value.nil?

  valid = %i[cooperative blocking_io cpu_bound external_process]
  unless valid.include?(value)
    raise ArgumentError, "execution_mode must be one of #{valid.inspect}, got #{value.inspect}"
  end

  @execution_mode = value
end

.max_result_size(value = :__unset__) ⇒ Object

Sets a per-tool maximum result size (in characters). Overrides the global +Phronomy.configuration.tool_result_max_size+ when set. Set to +nil+ to inherit the global limit.

Parameters:

  • value (Integer, nil) (defaults to: :__unset__)


209
210
211
212
213
# File 'lib/phronomy/agent/context/capability/base.rb', line 209

def max_result_size(value = :__unset__)
  return @max_result_size if value == :__unset__

  @max_result_size = value
end

.on_error(behavior = nil) ⇒ Object

Configures error-handling behavior when +execute+ raises an unexpected error.

Parameters:

  • behavior (Symbol) (defaults to: nil)

    :raise (default) — re-raise as Phronomy::ToolError, stopping the agent. :suppress — suppress the error and return a descriptive string so the LLM can recover on the next turn. :return_empty — deprecated alias for +:suppress+; will be removed in a future major release.



146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/phronomy/agent/context/capability/base.rb', line 146

def on_error(behavior = nil)
  return @on_error || :raise if behavior.nil?

  if behavior == :return_empty
    msg = "[Phronomy] on_error :return_empty is deprecated; use :suppress instead"
    if Phronomy.configuration.logger
      Phronomy.configuration.logger.warn(msg)
    else
      warn msg
    end
  end
  @on_error = behavior
end

.on_schema_error(behavior = nil) ⇒ Object

Configures how this tool responds when the LLM passes arguments that violate the declared parameter types or enum constraints.

mutant:disable - neutral failure: unparser round-trip produces different source

Parameters:

  • behavior (Symbol) (defaults to: nil)

    :return_error (default) — return a descriptive error string as the tool result so the LLM can self-correct on the next turn. :raise — raise Phronomy::ToolError, stopping the agent loop. :coerce — attempt type coercion (e.g. "42" → 42 for :integer); falls back to :return_error when coercion is not possible.



171
172
173
174
175
# File 'lib/phronomy/agent/context/capability/base.rb', line 171

def on_schema_error(behavior = nil)
  return @on_schema_error || :return_error if behavior.nil?

  @on_schema_error = behavior
end

.param(name, enum: nil, properties: nil, **options) ⇒ Object

Extends RubyLLM::Tool.param with optional +enum:+ and +properties:+ keywords.

  • +enum:+ restricts allowed values; injected into the JSON Schema.
  • +properties:+ declares nested fields for :object type params. Each entry is a Hash mapping field name (Symbol) to a spec Hash with keys: :type (Symbol, default :string), :required (Boolean, default false), and optionally :properties (for further nesting).

Parameters:

  • name (Symbol)

    parameter name

  • enum (Array, nil) (defaults to: nil)

    allowed values

  • properties (Hash, nil) (defaults to: nil)

    nested schema for :object params

  • options (Hash)

    forwarded to RubyLLM::Tool.param



57
58
59
60
61
# File 'lib/phronomy/agent/context/capability/base.rb', line 57

def param(name, enum: nil, properties: nil, **options)
  super(name, **options)
  param_enums[name] = enum if enum
  param_schemas[name] = normalize_nested_schema(properties) if properties
end

.param_enumsHash{Symbol => Array}

Returns the enum constraints registered via .param.

Returns:

  • (Hash{Symbol => Array})


66
67
68
# File 'lib/phronomy/agent/context/capability/base.rb', line 66

def param_enums
  @param_enums ||= {}
end

.param_schemasHash{Symbol => Hash}

Returns nested schema definitions registered via .param(properties: ...). mutant:disable - neutral failure: unparser round-trip produces different source

Returns:

  • (Hash{Symbol => Hash})


74
75
76
# File 'lib/phronomy/agent/context/capability/base.rb', line 74

def param_schemas
  @param_schemas ||= {}
end

.redact_params(*names) ⇒ Array<Symbol>

Marks one or more parameter names as sensitive so their values are replaced with +"[REDACTED]"+ in log and trace output.

mutant:disable

Parameters:

  • names (Array<Symbol>)

    parameter names to redact

Returns:

  • (Array<Symbol>)

    the full list of redacted param names



194
195
196
197
198
199
200
201
# File 'lib/phronomy/agent/context/capability/base.rb', line 194

def redact_params(*names)
  if names.empty?
    parent = superclass.respond_to?(:redact_params) ? superclass.redact_params : []
    ((@redacted_params || []) + parent).uniq
  else
    @redacted_params = ((@redacted_params || []) + names.map(&:to_sym)).uniq
  end
end

.requires_approval(value = nil) ⇒ Object

Configures whether human approval is required before executing this tool. mutant:disable - neutral failure: unparser round-trip produces different source

Parameters:

  • value (Boolean) (defaults to: nil)


181
182
183
184
185
# File 'lib/phronomy/agent/context/capability/base.rb', line 181

def requires_approval(value = nil)
  return @requires_approval || false if value.nil?

  @requires_approval = value
end

.retry_on(*exception_classes, times: 1, wait: 0, base: 1.0) ⇒ Object

Registers a retry policy for one or more exception classes.

When the tool raises one of the listed exception classes, it will be retried up to +times+ times with the specified wait strategy. Multiple policies can be registered and are evaluated in order.

GuardrailError is never retried regardless of this configuration.

Examples:

retry_on Phronomy::ToolError, times: 3, wait: :exponential, base: 1.0
retry_on Net::ReadTimeout, times: 2, wait: 0.5

Parameters:

  • exception_classes (Array<Class>)

    exception classes to retry on

  • times (Integer) (defaults to: 1)

    maximum retry attempts (default: 1)

  • wait (Symbol, Numeric) (defaults to: 0)

    :exponential, :linear, or a fixed Float

  • base (Float) (defaults to: 1.0)

    base wait time in seconds (default: 1.0)



232
233
234
235
# File 'lib/phronomy/agent/context/capability/base.rb', line 232

def retry_on(*exception_classes, times: 1, wait: 0, base: 1.0)
  @retry_policies ||= []
  @retry_policies << {exceptions: exception_classes, times: times, wait: wait, base: base}
end

.retry_policiesArray<Hash>

Returns all retry policies registered on this tool class. mutant:disable - neutral failure: unparser round-trip produces different source

Returns:

  • (Array<Hash>)


241
242
243
# File 'lib/phronomy/agent/context/capability/base.rb', line 241

def retry_policies
  @retry_policies || []
end

.scope(value = nil) ⇒ Object

Sets the access scope for this tool (metadata; enforcement is the responsibility of the Workflow/Guardrail layer). mutant:disable - neutral failure: unparser round-trip produces different source

Parameters:

  • value (Symbol) (defaults to: nil)

    e.g. :read_only, :write, :admin



99
100
101
102
103
# File 'lib/phronomy/agent/context/capability/base.rb', line 99

def scope(value = nil)
  return @scope if value.nil?

  @scope = value
end

.tool_name(value = nil) ⇒ Object

Sets an explicit function name to expose to the LLM, bypassing RubyLLM's automatic CamelCase-to-snake_case conversion. When omitted, RubyLLM's default conversion applies (e.g. WeatherTool → "weather").

Parameters:

  • value (String, nil) (defaults to: nil)

    the exact function name the LLM will see



39
40
41
42
43
# File 'lib/phronomy/agent/context/capability/base.rb', line 39

def tool_name(value = nil)
  return @tool_name if value.nil?

  @tool_name = value.to_s
end

Instance Method Details

#call(args, cancellation_token: nil) ⇒ Object

Overrides RubyLLM::Tool#call to apply schema validation, the retry policy, the on_error policy, and wrap errors as ToolError.

Execution order:

  1. Early cancellation check (kwarg token takes precedence over thread-local).
  2. Schema validation (type + enum checks).
  3. Inject +cancellation_token:+ into args when +execute+ opts in.
  4. Call super(validated_args) inside a retry loop.
  5. On persistent failure, apply on_error policy.

mutant:disable

Parameters:



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/phronomy/agent/context/capability/base.rb', line 326

def call(args, cancellation_token: nil)
  ct = cancellation_token
  ct&.raise_if_cancelled!
  validated_args, schema_error = validate_and_coerce(args)
  if schema_error
    case self.class.on_schema_error
    when :raise
      raise Phronomy::ToolError, "#{self.class.name} schema error: #{schema_error}"
    else
      # :return_error (default) and coerce fallback
      return "Schema validation failed: #{schema_error}"
    end
  end
  validated_args = validated_args.merge(cancellation_token: ct) if ct && execute_accepts_cancellation_token?
  result = with_tool_retry { super(validated_args) }
  truncate_result_if_needed(result)
rescue Phronomy::ToolError
  raise
rescue Phronomy::CancellationError
  raise
rescue => e
  case self.class.on_error
  when :return_empty, :suppress
    msg = "[Phronomy] Tool #{self.class.name} suppressed error: #{e.class}: #{e.message}"
    if Phronomy.configuration.logger
      Phronomy.configuration.logger.warn(msg)
    else
      warn msg
    end
    "Tool error suppressed: #{e.message}"
  else
    raise Phronomy::ToolError, "#{self.class.name} execution failed: #{e.message}"
  end
end

#call_async(args, cancellation_token: nil) ⇒ #await

Invokes this tool asynchronously and returns a Task.

Routing is governed by the class-level execution_mode setting. Delegates to ToolExecutor.call_async which is the single place in the framework that applies the execution-mode routing rules.

mutant:disable

Parameters:

Returns:

  • (#await)


372
373
374
375
376
377
378
# File 'lib/phronomy/agent/context/capability/base.rb', line 372

def call_async(args, cancellation_token: nil)
  Phronomy::Agent::ToolExecutor.call_async(
    tool: self,
    args: args,
    cancellation_token: cancellation_token
  )
end

#execute(**_args) ⇒ String

This method is abstract.

Subclasses must implement this method.

Override this method to implement the tool's logic.

The method receives the declared param fields as keyword arguments. The return value is passed back to the LLM as the tool result.

Examples:

class WeatherTool < Phronomy::Agent::Context::Capability::Base
  description "Get current weather"
  param :location, type: :string, desc: "City name"

  def execute(location:)
    WeatherService.fetch(location).to_s
  end
end

Returns:

  • (String)

    result string returned to the LLM

Raises:

  • (NotImplementedError)


410
411
412
# File 'lib/phronomy/agent/context/capability/base.rb', line 410

def execute(**_args)
  raise NotImplementedError, "#{self.class}#execute is not implemented"
end

#nameObject

Returns the function name exposed to the LLM. Uses the class-level tool_name if set; otherwise falls back to RubyLLM's automatic conversion (CamelCase → snake_case, strips trailing "_tool"). mutant:disable - neutral failure: unparser round-trip produces different source



263
264
265
# File 'lib/phronomy/agent/context/capability/base.rb', line 263

def name
  self.class.tool_name || super
end

#params_schemaObject

Returns the JSON Schema for this tool's parameters. Injects "enum" entries for any param declared with enum: [...]. mutant:disable - genuine equivalent mutations:

  1. || schema.dig(:properties): dead code because RubyLLM::Tool always returns a string-keyed hash; schema.dig(:properties) is always nil in practice.
  2. return schema unless properties guard: dead code when schema is non-nil because RubyLLM::Tool always includes a "properties" key when parameters are declared.


274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/phronomy/agent/context/capability/base.rb', line 274

def params_schema
  schema = super
  return schema if schema.nil?

  properties = schema.dig("properties") || schema.dig(:properties)
  return schema unless properties

  # Inject enum values for params declared with enum: [...].
  unless self.class.param_enums.empty?
    enums = self.class.param_enums
    enums.each do |param_name, values|
      key = properties.key?(param_name.to_s) ? param_name.to_s : param_name.to_sym
      next unless properties[key]

      param_type = properties[key]["type"]
      properties[key]["enum"] = values.map do |v|
        case param_type
        when "integer" then v.is_a?(Integer) ? v : Integer(v.to_s)
        when "number" then v.is_a?(Numeric) ? v : Float(v.to_s)
        else v.to_s
        end
      end
    end
  end

  # Inject nested properties for :object params (issue #162).
  # Without this the LLM sees only { "type": "object" } with no field
  # definitions, making it unable to populate nested object params.
  self.class.param_schemas.each do |param_name, nested|
    key = properties.key?(param_name.to_s) ? param_name.to_s : param_name.to_sym
    next unless properties[key]

    properties[key]["properties"] = nested_schema_to_json_schema(nested)
  end

  schema
end

#requires_approvalObject

Instance method accessor — delegates to the class-level flag.



381
382
383
# File 'lib/phronomy/agent/context/capability/base.rb', line 381

def requires_approval
  self.class.requires_approval
end

#requires_approval?Boolean

Instance method for requires_approval? (convenience accessor). mutant:disable - genuine equivalent: self.requires_approval delegates to self.class.requires_approval via the instance method defined above, so both expressions produce the same value.

Returns:

  • (Boolean)


389
390
391
# File 'lib/phronomy/agent/context/capability/base.rb', line 389

def requires_approval?
  self.class.requires_approval
end