Class: Harnex::Sender
- Inherits:
-
Object
- Object
- Harnex::Sender
- Defined in:
- lib/harnex/commands/send.rb
Defined Under Namespace
Classes: TimeoutError
Constant Summary collapse
- DEFAULT_TIMEOUT =
120.0- MIN_HTTP_TIMEOUT =
0.1- POLL_INTERVAL =
0.5- IDLE_FENCE_TIMEOUT =
30.0
Class Method Summary collapse
- .build_parser(options, program_name = "harnex send") ⇒ Object
- .usage(program_name = "harnex send") ⇒ Object
Instance Method Summary collapse
-
#initialize(argv) ⇒ Sender
constructor
A new instance of Sender.
- #run ⇒ Object
Constructor Details
#initialize(argv) ⇒ Sender
Returns a new instance of Sender.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/harnex/commands/send.rb', line 56 def initialize(argv) @options = { repo_path: Dir.pwd, id: nil, cli: nil, message: nil, submit: true, submit_only: false, relay: nil, force: false, wait: true, wait_for_idle: false, port: nil, token: nil, host: DEFAULT_HOST, timeout: DEFAULT_TIMEOUT, verbose: false, help: false } @argv = argv.dup end |
Class Method Details
.build_parser(options, program_name = "harnex send") ⇒ Object
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/send.rb', line 14 def self.build_parser(, program_name = "harnex send") OptionParser.new do |opts| opts. = "Usage: #{program_name} --id ID [options] [text...]" opts.on("--id ID", "Target session ID") { |value| [:id] = Harnex.normalize_id(value) } opts.on("--repo PATH", "Resolve the target using PATH's repo root") { |value| [:repo_path] = value } opts.on("--cli CLI", "Filter by CLI type") { |value| [:cli] = value } opts.on("--message TEXT", "Message text to inject instead of positional args") { |value| [:message] = value } opts.on("--no-submit", "Inject text without pressing Enter") { [:submit] = false } opts.on("--submit-only", "Press Enter without injecting text") do [:submit_only] = true [:submit] = true end opts.on("--force", "Send even if the agent is not at a prompt") { [:force] = true } opts.on("--no-wait", "Return immediately after queueing (HTTP 202). Use for fire-and-forget or when polling delivery separately.") { [:wait] = false } opts.on("--wait-for-idle", "After a successful send, wait for the agent to return to prompt") { [:wait_for_idle] = true } opts.on("--relay", "Force relay header formatting") { [:relay] = true } opts.on("--no-relay", "Disable automatic relay headers") { [:relay] = false } opts.on("--port PORT", Integer, "Send directly to a specific port") { |value| [:port] = value } opts.on("--token TOKEN", "Auth token for --port mode") { |value| [:token] = value } opts.on("--host HOST", "Override the host when --port is used") { |value| [:host] = value } opts.on("--timeout SECS", Float, "How long to wait for lookup, delivery, or idle wait (default: #{DEFAULT_TIMEOUT.to_i})") { |value| [:timeout] = value } opts.on("--verbose", "Print lookup and delivery details to stderr") { [:verbose] = true } opts.on("-h", "--help", "Show help") { [:help] = true } end end |
.usage(program_name = "harnex send") ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/harnex/commands/send.rb', line 40 def self.usage(program_name = "harnex send") <<~TEXT #{build_parser({}, program_name)} Common patterns: #{program_name} --id cx-i-42 --message "Read /tmp/task-impl-42.md" --wait-for-idle --timeout 900 #{program_name} --id cx-i-42 --message "Continue with the current task." --force #{program_name} --id "$HARNEX_ID" --message "Worker finished; tests passed." Gotchas: --wait-for-idle is a turn fence, not proof that the whole task is done. Use --no-wait for fire-and-forget delivery only when another monitor owns completion. Long prompts are more reliable when written to a file and referenced by path. Messages between harnex sessions get relay headers unless --no-relay is used. TEXT end |
Instance Method Details
#run ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/harnex/commands/send.rb', line 78 def run parser.parse!(@argv) if @options[:help] puts parser return 0 end raise "--id is required for harnex send" unless @options[:id] validate_modes! repo_root = Harnex.resolve_repo_root(@options[:repo_path]) @command_started_at = monotonic_now deadline = @command_started_at + @options[:timeout] verbose("repo_root=#{repo_root}") verbose("id=#{@options[:id]}") verbose("cli=#{@options[:cli] || '(any)'}") registry = wait_for_registry(repo_root, deadline: deadline) case registry when :ambiguous raise (repo_root) when :timeout puts JSON.generate(ok: false, id: @options[:id], status: "timeout", error: "lookup timed out after #{@options[:timeout]}s") return 124 end registry = direct_registry.merge(registry || {}) verbose("target=http://#{registry.fetch('host')}:#{registry.fetch('port')}/send") text = resolve_text text = relay_text(text, registry) raise "text is required" if text.to_s.empty? && !@options[:submit_only] response = with_http_retry(deadline: deadline) do uri = URI("http://#{registry.fetch('host')}:#{registry.fetch('port')}/send") request = Net::HTTP::Post.new(uri) request["Authorization"] = "Bearer #{registry['token']}" if registry["token"] request["Content-Type"] = "application/json" request.body = JSON.generate( text: text, submit: @options[:submit], enter_only: @options[:submit_only], force: @options[:force] ) Net::HTTP.start( uri.host, uri.port, open_timeout: http_timeout(deadline), read_timeout: http_timeout(deadline) ) { |http| http.request(request) } end result = nil exit_code = nil if response.code == "202" && @options[:wait] parsed = parse_json_body(response.body) = parsed["message_id"] if verbose("queued message_id=#{}") result = poll_delivery(registry, , deadline: deadline) exit_code = case result["status"] when "timeout" then 124 when "delivered" then 0 else 1 end end end unless result result = parse_json_body(response.body) exit_code = response.is_a?(Net::HTTPSuccess) ? 0 : 1 end if exit_code == 0 && @options[:wait_for_idle] result = wait_for_idle_state(registry, deadline) exit_code = result["ok"] ? 0 : (result["state"] == "exited" ? 1 : 124) end puts JSON.generate(result) exit_code rescue TimeoutError => e puts JSON.generate(ok: false, id: @options[:id], status: "timeout", error: e.) 124 end |