Class: Harnex::Waiter

Inherits:
Object
  • Object
show all
Defined in:
lib/harnex/commands/wait.rb

Constant Summary collapse

POLL_INTERVAL =
0.5
EVENT_PREDICATES =
%w[task_complete].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Waiter

Returns a new instance of Waiter.



40
41
42
43
44
45
46
47
48
49
# File 'lib/harnex/commands/wait.rb', line 40

def initialize(argv)
  @argv = argv.dup
  @options = {
    id: nil,
    until_state: nil,
    repo_path: Dir.pwd,
    timeout: nil,
    help: false
  }
end

Class Method Details

.usage(program_name = "harnex wait") ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/harnex/commands/wait.rb', line 12

def self.usage(program_name = "harnex wait")
  <<~TEXT
    Usage: #{program_name} [options]

    Options:
      --id ID         Session ID to wait for (required)
      --until STATE   Wait until session reaches STATE. Supported:
                        task_complete   (events JSONL — fires on
                                         turn/completed; adapter-agnostic)
                        <other>         (agent_state HTTP poll, e.g.
                                         "prompt", "busy")
                      Without --until, waits for session exit (default).
      --repo PATH     Resolve session using PATH's repo root (default: current repo)
      --timeout SECS  Maximum time to wait in seconds (default: unlimited)
      -h, --help      Show this help

    Common patterns:
      #{program_name} --id cx-i-42 --until task_complete --timeout 900
      #{program_name} --id cx-i-42 --until prompt --timeout 120
      #{program_name} --id cx-i-42

    Gotchas:
      task_complete is an event predicate; prompt/busy are live state polls.
      Prompt state alone does not prove work acceptance. Verify artifacts/tests.
      Without --timeout, wait can block indefinitely.
  TEXT
end

Instance Method Details

#runObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/harnex/commands/wait.rb', line 51

def run
  parser.parse!(@argv)
  if @options[:help]
    puts self.class.usage
    return 0
  end

  raise "--id is required for harnex wait" unless @options[:id]

  if @options[:until_state]
    if EVENT_PREDICATES.include?(@options[:until_state])
      wait_until_event(@options[:until_state])
    else
      wait_until_state
    end
  else
    wait_until_exit
  end
end