agentic examples

The Journal Cinema

The Journal Cinema: an execution journal is a film negative. The run happened once, in real time, unwatched - then the negative sits in a JSONL can holding everything: who started, who failed, who came back, to the millisecond. This projector plays it back as a movie: same scenes, same order, same rhythm, compressed 4x for the theater. The referee checks the projection is FAITHFUL - every frame from the negative, in order, with the comeback arc intact. Films get edited; …

Observability & Ops Round 18 Ryan Tomayko exit 0

source on github

bundle exec ruby examples/journal_cinema.rb

a real captured run

JOURNAL CINEMA presents: THE DEPLOY (runtime 1342ms, projected at 4x)

    00000ms  [ ACTION ]  brew coffee
    00001ms  [ ACTION ]  compile assets
    00032ms  [  CUT   ]  brew coffee
    00045ms  [ DRAMA! ]  compile assets (sass compiler mood - but the negative says they came back)
    01265ms  [ ACTION ]  compile assets
    01306ms  [  CUT   ]  compile assets
    01306ms  [ ACTION ]  deploy
    01339ms  [  CUT   ]  deploy
    01340ms  [ ACTION ]  take a bow
    01342ms  [  CUT   ]  take a bow

    ~ fin ~   cast: brew coffee, compile assets, deploy, take a bow

  referee: 10/10 frames, order preserved, comeback arc intact,
           projected 3.9x faster than life (asked for 4x)

  the journal was already a movie; it just needed a projector.
  every run of every plan leaves this negative behind - who
  started, who failed, who came back, with millisecond timecodes -
  and playback is 30 lines: parse the timestamps, sleep the gaps
  scaled, print the scenes. incident review is watching last
  night's footage instead of interviewing witnesses. the compile
  failure isn't a log line, it's a SCENE, with a before and an
  after and a comeback - which is how the on-call human actually
  thinks about it. evidence with a playhead beats evidence with
  a grep prompt.

source

# frozen_string_literal: true

# The Journal Cinema: an execution journal is a film negative. The
# run happened once, in real time, unwatched - then the negative
# sits in a JSONL can holding everything: who started, who failed,
# who came back, to the millisecond. This projector plays it back
# as a movie: same scenes, same order, same rhythm, compressed 4x
# for the theater. The referee checks the projection is FAITHFUL -
# every frame from the negative, in order, with the comeback arc
# intact. Films get edited; evidence doesn't.
#
#   bundle exec ruby examples/journal_cinema.rb
#
# Runs offline; a small drama is shot, then screened.

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

Agentic.logger.level = class="y">:fatal

REEL = File.join(Dir.tmpdir, class="s">"agentic_cinema.jsonl")
File.delete(REEL) if File.exist?(REEL)

# --- act 1: the shoot (nobody watches the actual run; that's the point) ------------
journal = Agentic:class="y">:ExecutionJournal.new(path: REEL)
orchestrator = Agentic:class="y">:PlanOrchestrator.new(
  concurrency_limit: 2,
  lifecycle_hooks: journal.lifecycle_hooks,
  retry_policy: {max_retries: 1, backoff_base: 0.01, retryable_errors: [Agentic:class="y">:Errors:class="y">:LlmRateLimitError]}
)

compile_attempts = 0
coffee = Agentic:class="y">:Task.new(description: class="s">"brew coffee", agent_spec: {class="s">"name" => class="s">"c", class="s">"instructions" => class="s">"w"})
compile = Agentic:class="y">:Task.new(description: class="s">"compile assets", agent_spec: {class="s">"name" => class="s">"a", class="s">"instructions" => class="s">"w"})
deploy = Agentic:class="y">:Task.new(description: class="s">"deploy", agent_spec: {class="s">"name" => class="s">"d", class="s">"instructions" => class="s">"w"})
bow = Agentic:class="y">:Task.new(description: class="s">"take a bow", agent_spec: {class="s">"name" => class="s">"b", class="s">"instructions" => class="s">"w"})

orchestrator.add_task(coffee, agent: ->(_t) {
  sleep(0.03)
  class="s">"hot"
})
orchestrator.add_task(compile, agent: ->(_t) {
  compile_attempts += 1
  sleep(0.04)
  raise Agentic:class="y">:Errors:class="y">:LlmRateLimitError, class="s">"sass compiler mood" if compile_attempts == 1
  class="s">"compiled"
})
orchestrator.add_task(deploy, [compile], agent: ->(_t) {
  sleep(0.03)
  class="s">"shipped"
})
orchestrator.add_task(bow, [deploy, coffee], agent: ->(_t) { class="s">"applause" })
orchestrator.execute_plan

# --- act 2: the projection ----------------------------------------------------------
state = Agentic:class="y">:ExecutionJournal.replay(path: REEL)
frames = state.events.select { |e| e[class="y">:event].start_with?(class="s">"task_") }
times = frames.map { |e| Time.parse(e[class="y">:at]) }
negative_runtime = times.last - times.first
speed = 4.0

ICON = {class="s">"task_started" => class="s">"[ ACTION ]", class="s">"task_succeeded" => class="s">"[  CUT   ]", class="s">"task_failed" => class="s">"[ DRAMA! ]"}.freeze

puts class="s">"JOURNAL CINEMA presents: THE DEPLOY (runtime #{(negative_runtime * 1000).round}ms, projected at #{speed.to_i}x)"
puts
screened = []
projection_started = Process.clock_gettime(Process:class="y">:CLOCK_MONOTONIC)
frames.each_with_index do |frame, i|
  sleep((times[i] - times[i - 1]) / speed) if i.positive?
  timecode = format(class="s">"%05.0fms", (times[i] - times.first) * 1000)
  extra = (frame[class="y">:event] == class="s">"task_failed") ? class="s">" (#{frame[class="y">:error] || "retryableclass="s">"} - but the negative says they came back)" : class="s">""
  puts class="s">"    #{timecode}  #{ICON.fetch(frame[class="y">:event], "[ ????? ]class="s">")}  #{frame[class="y">:description]}#{extra}"
  screened << frame
end
projection_runtime = Process.clock_gettime(Process:class="y">:CLOCK_MONOTONIC) - projection_started

puts
puts class="s">"    ~ fin ~   cast: #{[coffee, compile, deploy, bow].map(&class="y">:description).join(", class="s">")}"
puts

# --- the referee: projection must be faithful to the negative -----------------------
failures = []
failures << class="s">"frames dropped or reordered" unless screened == frames
comeback = frames.each_index.any? do |i|
  frames[i][class="y">:event] == class="s">"task_failed" &&
    frames[(i + 1)..].any? { |f| f[class="y">:event] == class="s">"task_succeeded" && f[class="y">:description] == frames[i][class="y">:description] }
end
failures << class="s">"the comeback arc was cut" unless comeback
ratio = negative_runtime / projection_runtime
failures << class="s">"projection speed off (#{ratio.round(1)}x)" unless ratio.between?(speed * 0.5, speed * 2.0)

puts class="s">"  referee: #{screened.size}/#{frames.size} frames, order preserved, comeback arc intact,"
puts class="s">"           projected #{ratio.round(1)}x faster than life (asked for #{speed.to_i}x)"
puts
puts class="s">"  the journal was already a movie; it just needed a projector."
puts class="s">"  every run of every plan leaves this negative behind - who"
puts class="s">"  started, who failed, who came back, with millisecond timecodes -"
puts class="s">"  and playback is 30 lines: parse the timestamps, sleep the gaps"
puts class="s">"  scaled, print the scenes. incident review is watching last"
puts class="s">"  night's footage instead of interviewing witnesses. the compile"
puts class="s">"  failure isn't a log line, it's a SCENE, with a before and an"
puts class="s">"  after and a comeback - which is how the on-call human actually"
puts class="s">"  thinks about it. evidence with a playhead beats evidence with"
puts class="s">"  a grep prompt."
exit(failures.empty? ? 0 : 1)