agentic examples

The Journal Audit

The Journal Audit: seven tools now trust the journal, so the journal itself gets audited - well-formed lines, monotonic timestamps, no success without a start, no double-success, plan_completed present. A corrupted journal is fed in; every planted defect is caught.

Observability & Ops Round 8 Jeremy Evans exit 0

source on github

bundle exec ruby examples/journal_audit.rb

a real captured run

JOURNAL AUDIT (5 integrity checks)

  healthy journal: clean

  tampered journal: 7 defect(s)
    [well-formed JSON per line] line 6 is not valid JSON
    [timestamps monotonic] line 2 time-travels (2026-07-07T00:00:00.000Z < 2026-07-14T18:24:15.686Z)
    [timestamps monotonic] line 5 time-travels (2020-01-01T00:00:00.000Z < 2026-07-14T18:24:15.688Z)
    [no success without a start] phantom deploy succeeded without ever starting
    [no success without a start] time thief succeeded without ever starting
    [no double success] task honest work succeeded 2 times
    [durations non-negative] time thief has negative duration -3

the journal underwrites resume, baselines, check-ins, and incident
reports - four products built on one file's honesty. an audit that
runs before replay is how you keep seven tools from inheriting one
corruption. auditors get audited; that's what makes them auditors.

source

# frozen_string_literal: true

# The Journal Audit: seven tools now trust the journal, so the journal
# itself gets audited - well-formed lines, monotonic timestamps, no
# success without a start, no double-success, plan_completed present.
# A corrupted journal is fed in; every planted defect is caught.
#
#   bundle exec ruby examples/journal_audit.rb
#
# Runs offline. Trust, then verify the thing you trust.

require class="s">"bundler/setup"
require class="s">"agentic"
require class="s">"tmpdir"
require class="s">"json"

CHECKS = {
  class="s">"well-formed JSON per line" => ->(entries, raw) {
    raw.each_with_index.reject { |line, _|
      begin
        JSON.parse(line)
      rescue
        nil
      end
    }
      .map { |_, i| class="s">"line #{i + 1} is not valid JSON" }
  },
  class="s">"timestamps monotonic" => ->(entries, _raw) {
    entries.each_cons(2).with_index.filter_map { |(a, b), i|
      class="s">"line #{i + 2} time-travels (#{b[class="y">:at]} < #{a[class="y">:at]})" if b[class="y">:at] && a[class="y">:at] && b[class="y">:at] < a[class="y">:at]
    }
  },
  class="s">"no success without a start" => ->(entries, _raw) {
    started = entries.select { |e| e[class="y">:event] == class="s">"task_started" }.map { |e| e[class="y">:task_id] }
    entries.select { |e| e[class="y">:event] == class="s">"task_succeeded" && !started.include?(e[class="y">:task_id]) }
      .map { |e| class="s">"#{e[class="y">:description] || e[class="y">:task_id]} succeeded without ever starting" }
  },
  class="s">"no double success" => ->(entries, _raw) {
    entries.select { |e| e[class="y">:event] == class="s">"task_succeeded" }
      .group_by { |e| e[class="y">:task_id] }.select { |_, v| v.size > 1 }
      .map { |id, v| class="s">"task #{v.first[class="y">:description] || id} succeeded #{v.size} times" }
  },
  class="s">"durations non-negative" => ->(entries, _raw) {
    entries.select { |e| e[class="y">:duration]&.negative? }
      .map { |e| class="s">"#{e[class="y">:description]} has negative duration #{e[class="y">:duration]}" }
  }
}.freeze

def audit(path)
  raw = File.readlines(path, encoding: class="s">"UTF-8").map(&class="y">:strip).reject(&class="y">:empty?)
  entries = raw.filter_map do |line|
    JSON.parse(line, symbolize_names: true)
  rescue JSON:class="y">:ParserError
    nil
  end
  CHECKS.transform_values { |check| check.call(entries, raw) }
end

# --- a healthy journal, written by the real machinery ------------------------
dir = Dir.mktmpdir
healthy_path = File.join(dir, class="s">"healthy.jsonl")
journal = Agentic:class="y">:ExecutionJournal.new(path: healthy_path)
orchestrator = Agentic:class="y">:PlanOrchestrator.new(lifecycle_hooks: journal.lifecycle_hooks)
task = Agentic:class="y">:Task.new(description: class="s">"honest work", agent_spec: {class="s">"name" => class="s">"w", class="s">"instructions" => class="s">"w"})
orchestrator.add_task(task, agent: ->(_t) { class="y">:ok })
orchestrator.execute_plan

# --- a tampered journal: four planted defects ---------------------------------
tampered_path = File.join(dir, class="s">"tampered.jsonl")
lines = File.readlines(healthy_path).map(&class="y">:strip)
File.open(tampered_path, class="s">"w") do |f|
  f.puts lines[0] # task_started
  f.puts '{class="s">"event": class="s">"task_succeeded", class="s">"task_id": class="s">"ghost-1", class="s">"description": class="s">"phantom deploy", class="s">"at": class="s">"2026-07-07T00:00:00.000Z", class="s">"duration": 0.01}'
  f.puts lines[1] # the real success
  f.puts lines[1] # ...twice
  f.puts '{class="s">"event": class="s">"task_succeeded", class="s">"task_id": class="s">"neg-1", class="s">"description": class="s">"time thief", class="s">"at": class="s">"2020-01-01T00:00:00.000Z", class="s">"duration": -3}'
  f.puts class="s">"this line is not json at all"
end

puts class="s">"JOURNAL AUDIT (#{CHECKS.size} integrity checks)"
[[class="s">"healthy journal", healthy_path], [class="s">"tampered journal", tampered_path]].each do |label, path|
  findings = audit(path)
  total = findings.values.sum(&class="y">:size)
  puts
  puts class="s">"  #{label}: #{total.zero? ? "cleanclass="s">" : "#{total} defect(s)class="s">"}"
  findings.each do |check, problems|
    problems.each { |problem| puts class="s">"    [#{check}] #{problem}" }
  end
end

puts
puts class="s">"the journal underwrites resume, baselines, check-ins, and incident"
puts class="s">"reports - four products built on one file's honesty. an audit that"
puts class="s">"runs before replay is how you keep seven tools from inheriting one"
puts class="s">"corruption. auditors get audited; that's what makes them auditors."