agentic examples

The Performance Detective

The Performance Detective: one task per Ruby file in lib/, fanned out through the orchestrator, each dissecting a file for long methods. The victim is this very gem. The report names names.

Observability & Ops Round 2 Aaron Patterson exit 0

source on github

bundle exec ruby examples/performance_detective.rb

a real captured run

CASE FILE: 68 files, 562 methods, 12372 lines
(completed in 213ms at concurrency 16)

THE USUAL SUSPECTS (longest methods):
  110 lines  generate_optimized_sequence  (agentic/learning/strategy_optimizer.rb:483)
   87 lines  adjust_plan_via_llm  (agentic/cli.rb:698)
   84 lines  generate_optimized_parameters  (agentic/learning/strategy_optimizer.rb:398)
   82 lines  generate_optimized_composition_with_llm  (agentic/learning/capability_optimizer.rb:434)
   81 lines  register_structured_extraction  (agentic/capabilities/examples.rb:393)
   71 lines  self.replay  (agentic/execution_journal.rb:171)
   71 lines  execute_task_in_slot  (agentic/plan_orchestrator.rb:518)

DENSEST NEIGHBORHOODS (methods per file):
   36 methods  agentic/cli.rb
   32 methods  agentic/cli/execution_observer.rb
   27 methods  agentic/plan_orchestrator.rb
   21 methods  agentic/errors.rb
   19 methods  agentic/learning/pattern_recognizer.rb

source

# frozen_string_literal: true

# The Performance Detective: one task per Ruby file in lib/, fanned out
# through the orchestrator, each dissecting a file for long methods.
# The victim is this very gem. The report names names.
#
#   bundle exec ruby examples/performance_detective.rb [concurrency]
#
# Runs offline - the "agent" here is Prism, Ruby's own parser, because
# the best LLM for counting your method lengths is the actual grammar.

require class="s">"bundler/setup"
require class="s">"agentic"
require class="s">"prism"

LIB = File.join(Gem:class="y">:Specification.find_by_name(class="s">"agentic").gem_dir, class="s">"lib") # the installed gem, wherever bundler put it

# Walks a parsed tree collecting every def with its measured length
def collect_defs(node, found)
  return unless node

  if node.is_a?(Prism:class="y">:DefNode)
    found << {
      name: node.receiver ? class="s">"self.#{node.name}" : node.name.to_s,
      line: node.location.start_line,
      lines: node.location.end_line - node.location.start_line + 1
    }
  end
  node.child_nodes.each { |child| collect_defs(child, found) }
end

# A capability that dissects one file: every def, with its length
spec = Agentic:class="y">:CapabilitySpecification.new(
  name: class="s">"dissect_file",
  description: class="s">"Measure the methods in one Ruby file",
  version: class="s">"1.0.0",
  inputs: {path: {type: class="s">"string", required: true}},
  outputs: {methods: {type: class="s">"array", required: true}, lines: {type: class="s">"number", required: true}}
)
provider = Agentic:class="y">:CapabilityProvider.new(
  capability: spec,
  implementation: ->(inputs) {
    parsed = Prism.parse_file(inputs[class="y">:path])
    methods = []
    collect_defs(parsed.value, methods)

    {methods: methods, lines: parsed.source.source.count(class="s">"\n")}
  }
)
Agentic.register_capability(spec, provider)

detective = Agentic:class="y">:Agent.build { |a| a.name = class="s">"Detective" }
detective.add_capability(class="s">"dissect_file")

# Every file is a lead; every lead gets a task with the path as payload
concurrency = (ARGV.first || 16).to_i
files = Dir[File.join(LIB, class="s">"**", class="s">"*.rb")].sort
if files.empty?
  puts class="s">"  no ruby files under #{LIB} - an empty survey proves nothing"
  exit 1
end

orchestrator = Agentic:class="y">:PlanOrchestrator.new(concurrency_limit: concurrency)
tasks = files.map do |path|
  task = Agentic:class="y">:Task.new(
    description: File.basename(path),
    agent_spec: {class="s">"name" => class="s">"Detective", class="s">"instructions" => class="s">"Dissect the file"},
    payload: path
  )
  orchestrator.add_task(task, agent: ->(t) {
    detective.execute_capability(class="s">"dissect_file", {path: t.payload})
  })
  task
end

started = Process.clock_gettime(Process:class="y">:CLOCK_MONOTONIC)
result = orchestrator.execute_plan
elapsed_ms = ((Process.clock_gettime(Process:class="y">:CLOCK_MONOTONIC) - started) * 1000).round

evidence = tasks.to_h { |task| [task.payload, result.results[task.id].output] }
all_methods = evidence.flat_map do |path, report|
  report[class="y">:methods].map { |m| m.merge(file: path.delete_prefix(class="s">"#{LIB}/")) }
end

puts class="s">"CASE FILE: #{files.size} files, #{all_methods.size} methods, " \
  class="s">"#{evidence.values.sum { |r| r[class="y">:lines] }} lines"
puts class="s">"(#{result.status} in #{elapsed_ms}ms at concurrency #{concurrency})"
puts
puts class="s">"THE USUAL SUSPECTS (longest methods):"
all_methods.sort_by { |m| -m[class="y">:lines] }.first(7).each do |m|
  puts format(class="s">"  %3d lines  %s  (%s:%d)", m[class="y">:lines], m[class="y">:name], m[class="y">:file], m[class="y">:line])
end
puts
puts class="s">"DENSEST NEIGHBORHOODS (methods per file):"
evidence.sort_by { |_, r| -r[class="y">:methods].size }.first(5).each do |path, r|
  puts format(class="s">"  %3d methods  %s", r[class="y">:methods].size, path.delete_prefix(class="s">"#{LIB}/"))
end