harnex 0.7.6 → 0.7.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f29114f723ccfc61ade344c784cb6a11144c25e79392bf136f5b722473b47f0b
4
- data.tar.gz: 810686487788509887bb8bfb5f9a4c45a8a1b8bca645530b1bf178f1435cee40
3
+ metadata.gz: '09276571f581a98eb6c613e88e4035ed8420bcae710c43d57e307cc5eb4d468b'
4
+ data.tar.gz: 886e1555b2ee4d128d992e4d35aafc716c0bc2aa16318123b053e5e128a7337a
5
5
  SHA512:
6
- metadata.gz: 90c68832ab9218716fd2e6181f006a12b996a23c28b120a769411618aee35e507c38df43bcf9293a98fb3902923e70eb1be9e08a90795e9ec970cb5b51ca2b54
7
- data.tar.gz: 64ebab38e3cf716bfe69fd7f8d1b28c0d114ef72e81f8028cb87f0b83f6a75cb555e1036a7831a41b3dddd156c0b9f71e51fa9550271f5be529dcea1fd23c9e1
6
+ metadata.gz: 90f7f457f829f99424c1f2635706c3231c08bf1cea7329e47b6992fde3aa45e123c40c0d278bf2c9a5c8bc2ecd6d0f742a812320c659c5bbe68fd4ab94df6c9d
7
+ data.tar.gz: 65323fa96b6f5c92564b2be10cfe802f846fff1b76079f26cbaa362588dce50ae86a59c250b456058f3acad8ca31f03c9c49b2ad096c85a6aae5510797ff3abb
data/CHANGELOG.md CHANGED
@@ -2,6 +2,24 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [0.7.7] - 2026-06-12 | 10:48 AM | IST
6
+
7
+ ### Fixed
8
+
9
+ - Codex app-server failed turns now emit `task_failed` instead of being
10
+ misreported as successful `task_complete` work. `harnex wait --until done`
11
+ returns non-zero for failed-turn events, dispatch history records
12
+ `terminal_event=task_failed`, and auto-stop terminates structured sessions
13
+ without sending a stale `turn/interrupt` after the turn is already complete.
14
+ - Codex app-server nested error notifications now preserve the real Codex error
15
+ message (for example missing provider credentials) without counting them as
16
+ transport disconnects.
17
+
18
+ ### Changed
19
+
20
+ - Refreshed the pinned Codex app-server JSON Schema fixtures to
21
+ `codex-cli 0.139.0` and taught the test schema validator `minLength`.
22
+
5
23
  ## [0.7.6] - 2026-06-09 | 12:59 AM | IST
6
24
 
7
25
  ### Added
data/README.md CHANGED
@@ -183,14 +183,15 @@ and stops the session after the first task completion or PTY prompt return.
183
183
  Choose the wait predicate that matches how you launched the worker:
184
184
 
185
185
  - `harnex wait --id ID --until done --timeout SECS` is the safest unattended
186
- work fence. It returns when Harnex sees `task_complete` or a terminal exit,
187
- whichever comes first.
186
+ work fence. It returns when Harnex sees `task_complete`, `task_failed`, or a
187
+ terminal exit, whichever comes first; failed work returns non-zero.
188
188
  - `harnex wait --id ID` waits for the wrapped process to exit. This is right
189
189
  for already-exited sessions and terminal-summary recovery, but interactive
190
190
  agents can stay open after finishing a turn.
191
191
  - For structured Pi RPC and Codex app-server sessions, use
192
192
  `harnex wait --id ID --until task_complete --timeout SECS` when you need the
193
- exact turn-completion event instead of terminal-exit fallback.
193
+ exact successful-turn event instead of terminal-exit fallback. Use
194
+ `--until task_failed` to wait specifically for a failed structured turn.
194
195
  - `harnex send --wait-for-idle` is an atomic send fence for PTY-style
195
196
  interactions. It proves the turn returned to an idle/prompt state, not that
196
197
  your acceptance criteria passed.
@@ -125,8 +125,8 @@ Use the lightest primitive that gives the signal you need:
125
125
  | Continuous pane view | `harnex pane --id pi-i-NN --follow` |
126
126
  | Transcript tail | `harnex logs --id pi-i-NN --lines 80` |
127
127
  | Structured events | `harnex events --id pi-i-NN --snapshot` |
128
- | Work completion fence | `harnex wait --id pi-i-NN --until done` |
129
- | Native turn completion | `harnex wait --id pi-i-NN --until task_complete` |
128
+ | Work completion/failure fence | `harnex wait --id pi-i-NN --until done` |
129
+ | Native successful-turn completion | `harnex wait --id pi-i-NN --until task_complete` |
130
130
 
131
131
  For unattended policy-only stall recovery, use built-in watch mode:
132
132
 
@@ -18,10 +18,11 @@ Prefer signals in this order:
18
18
  | `harnex status` | Session liveness and coarse state |
19
19
 
20
20
  For unattended monitors, prefer `harnex wait --until done`: it returns on the
21
- work-level `task_complete` signal or terminal exit, whichever comes first. For
22
- structured sessions (Pi RPC and Codex app-server), `harnex wait --until
23
- task_complete` remains the exact turn-level fence. Neither knows your acceptance
24
- criteria; verify the expected artifact or tests afterward.
21
+ work-level `task_complete` or `task_failed` signal, or terminal exit, whichever
22
+ comes first. Failed work returns non-zero. For structured sessions (Pi RPC and
23
+ Codex app-server), `harnex wait --until task_complete` remains the exact
24
+ successful-turn fence. Neither knows your acceptance criteria; verify the
25
+ expected artifact or tests afterward.
25
26
 
26
27
  ## Completion Test
27
28
 
@@ -34,8 +35,9 @@ harnex wait --id pi-i-NN --until done --timeout 5400 &&
34
35
  test -z "$(git status --short)"
35
36
  ```
36
37
 
37
- `harnex wait --until done` succeeds from `task_complete` or durable terminal
38
- telemetry (`--summary-out` / `.harnex/dispatch.jsonl` / exit status), not from
38
+ `harnex wait --until done` succeeds from `task_complete` or durable successful
39
+ terminal telemetry (`--summary-out` / `.harnex/dispatch.jsonl` / exit status),
40
+ returns non-zero for `task_failed` / failed terminal telemetry, and does not use
39
41
  tmp done markers.
40
42
 
41
43
  Adjust the artifact path to the task. The point is to avoid declaring done while
@@ -339,7 +339,7 @@ module Harnex
339
339
  @current_turn_id = nil
340
340
  @state = :prompt
341
341
  when "error"
342
- @state = :disconnected
342
+ @state = :busy
343
343
  end
344
344
 
345
345
  @notification_handler&.call(message)
@@ -265,8 +265,7 @@ module Harnex
265
265
 
266
266
  if message["error"]
267
267
  err_msg = message.dig("error", "message") || "RPC error"
268
- pending.push(StandardError.new("codex_appserver RPC error: #{err_msg}"))
269
- signal_disconnect(message["error"])
268
+ pending.push(StandardError.new(err_msg))
270
269
  else
271
270
  pending.push(message["result"] || {})
272
271
  end
@@ -102,14 +102,17 @@ module Harnex
102
102
  end
103
103
 
104
104
  def normalize_live_status(session)
105
- task_complete = task_complete?(session)
105
+ task_failed = task_failed?(session)
106
+ task_complete = task_complete?(session) && !task_failed
107
+ work_state = task_failed ? "failed" : Harnex.work_state_for("running", task_complete: task_complete)
106
108
  session.merge(
107
109
  "state" => "running",
108
110
  "process_state" => "running",
109
111
  "terminal" => false,
110
112
  "task_complete" => task_complete,
113
+ "task_failed" => task_failed,
111
114
  "done" => Harnex.work_done_for("running", task_complete: task_complete),
112
- "work_state" => Harnex.work_state_for("running", task_complete: task_complete),
115
+ "work_state" => work_state,
113
116
  "exit" => nil,
114
117
  "exit_code" => nil,
115
118
  "summary_out" => nil,
@@ -123,6 +126,11 @@ module Harnex
123
126
  !session["last_completed_at"].to_s.empty?
124
127
  end
125
128
 
129
+ def task_failed?(session)
130
+ session["task_failed"] == true || session["task_failed"].to_s == "true" ||
131
+ !session["last_failed_at"].to_s.empty?
132
+ end
133
+
126
134
  def load_live_status(session)
127
135
  uri = URI("http://#{session.fetch('host')}:#{session.fetch('port')}/status")
128
136
  request = Net::HTTP::Get.new(uri)
@@ -11,8 +11,8 @@ module Harnex
11
11
  EXIT_STATUS_GRACE_POLL_INTERVAL = 0.05
12
12
  FINAL_EVENT_GRACE_SECONDS = 5.0
13
13
 
14
- EVENT_PREDICATES = %w[task_complete].freeze
15
- LEGACY_EVENT_TYPES = %w[agent_state exited task_complete].freeze
14
+ EVENT_PREDICATES = %w[task_complete task_failed].freeze
15
+ LEGACY_EVENT_TYPES = %w[agent_state exited task_complete task_failed].freeze
16
16
 
17
17
  def self.usage(program_name = "harnex wait")
18
18
  <<~TEXT
@@ -21,10 +21,13 @@ module Harnex
21
21
  Options:
22
22
  --id ID Session ID to wait for (required)
23
23
  --until STATE Wait until session reaches STATE. Supported:
24
- done (work fence — task_complete or
25
- terminal exit, whichever comes first)
24
+ done (work fence — task_complete,
25
+ task_failed, or terminal exit,
26
+ whichever comes first)
26
27
  task_complete (events JSONL — fires on
27
- turn/completed; adapter-agnostic)
28
+ successful turn completion)
29
+ task_failed (events JSONL — fires on
30
+ failed turn completion)
28
31
  <other> (agent_state HTTP poll, e.g.
29
32
  "prompt", "busy")
30
33
  Without --until, waits for session exit (default).
@@ -40,7 +43,7 @@ module Harnex
40
43
 
41
44
  Gotchas:
42
45
  done is the safest work-level fence for monitors.
43
- task_complete is an event predicate; prompt/busy are live state polls.
46
+ task_complete/task_failed are event predicates; prompt/busy are live state polls.
44
47
  Prompt state alone does not prove work acceptance. Verify artifacts/tests.
45
48
  Exit waits can resolve from terminal summary rows when live registry/
46
49
  exit-status files are already gone.
@@ -140,7 +143,7 @@ module Harnex
140
143
  event = parse_event(line)
141
144
  next unless event
142
145
 
143
- task_complete_seen = true if event_type(event) == "task_complete"
146
+ task_complete_seen = true if %w[task_complete task_failed].include?(event_type(event))
144
147
  if matches?(event, predicate, task_complete_seen)
145
148
  return [emit_event_match(event, start_time, predicate), f.pos, task_complete_seen]
146
149
  end
@@ -173,8 +176,12 @@ module Harnex
173
176
  def matches?(event, predicate, task_complete_seen)
174
177
  type = event_type(event)
175
178
  case predicate
176
- when "task_complete", "done"
179
+ when "task_complete"
177
180
  type == "task_complete"
181
+ when "task_failed"
182
+ type == "task_failed"
183
+ when "done"
184
+ %w[task_complete task_failed].include?(type)
178
185
  when "prompt"
179
186
  type == "task_complete" ||
180
187
  (task_complete_seen && type == "agent_state" && event["state"] == "prompt")
@@ -183,6 +190,13 @@ module Harnex
183
190
  end
184
191
  end
185
192
 
193
+ def done_event_failed?(event)
194
+ return true if event_type(event) == "task_failed"
195
+
196
+ status = event["status"].to_s
197
+ !status.empty? && !%w[completed success succeeded].include?(status)
198
+ end
199
+
186
200
  def emit_event_match(event, start_time, predicate)
187
201
  waited = (Time.now - start_time).round(1)
188
202
  payload = {
@@ -193,17 +207,22 @@ module Harnex
193
207
  waited_seconds: waited
194
208
  }
195
209
  if predicate == "done"
210
+ failed = done_event_failed?(event)
196
211
  payload.merge!(
197
- status: "done",
212
+ ok: !failed,
213
+ status: failed ? "failed" : "done",
198
214
  state: "running",
199
215
  process_state: "running",
200
216
  terminal: false,
201
- task_complete: true,
202
- done: true,
203
- work_state: "completed"
217
+ task_complete: !failed,
218
+ done: !failed,
219
+ work_state: failed ? "failed" : "completed"
204
220
  )
221
+ payload[:last_error] = event["message"] || event["error"] if failed
205
222
  end
206
223
  puts JSON.generate(payload)
224
+ return 1 if predicate == "done" && done_event_failed?(event)
225
+
207
226
  0
208
227
  end
209
228
 
@@ -431,7 +450,8 @@ module Harnex
431
450
  data = JSON.parse(File.read(exit_path))
432
451
  exit_code = data["exit_code"]
433
452
  task_complete = data["task_complete"] == true || data["task_complete"].to_s == "true"
434
- exit_success = exit_code.nil? || exit_code.to_i == 0
453
+ task_failed = data["task_failed"] == true || data["task_failed"].to_s == "true"
454
+ exit_success = !task_failed && (exit_code.nil? || exit_code.to_i == 0)
435
455
  state = exit_success ? "completed" : "failed"
436
456
  done = task_complete || exit_success
437
457
  payload = data.merge(
@@ -441,6 +461,7 @@ module Harnex
441
461
  "process_state" => "exited",
442
462
  "terminal" => true,
443
463
  "task_complete" => task_complete,
464
+ "task_failed" => task_failed,
444
465
  "done" => done,
445
466
  "work_state" => Harnex.work_state_for(state, task_complete: task_complete)
446
467
  )
@@ -486,6 +507,7 @@ module Harnex
486
507
 
487
508
  def terminal_payload(status)
488
509
  task_complete = !!status["task_complete"]
510
+ task_failed = !!status["task_failed"]
489
511
  work_state = status["work_state"] || Harnex.work_state_for(status["state"], task_complete: task_complete)
490
512
  done = status.key?("done") ? !!status["done"] : work_state == "completed"
491
513
  {
@@ -495,6 +517,7 @@ module Harnex
495
517
  process_state: status["process_state"] || Harnex.process_state_for(status["state"], terminal: true),
496
518
  terminal: status.key?("terminal") ? !!status["terminal"] : true,
497
519
  task_complete: task_complete,
520
+ task_failed: task_failed,
498
521
  done: done,
499
522
  work_state: work_state,
500
523
  exit: status["exit"],
@@ -85,6 +85,7 @@ module Harnex
85
85
  end
86
86
 
87
87
  def classify(session)
88
+ return ["failed", "task_failed"] if session.respond_to?(:task_failed?) && session.task_failed?
88
89
  return ["completed", "task_complete"] if session.task_complete?
89
90
  return ["timeout", "timeout"] if session.exit_code == 124
90
91
  return ["killed", "process_kill"] if session.term_signal
@@ -16,6 +16,7 @@ module Harnex
16
16
  agent_session_id cost_usd
17
17
  ].freeze
18
18
  BUDGET_META_FIELDS = %w[read_budget_lines output_ceiling_lines].freeze
19
+ SUCCESSFUL_TURN_STATUSES = %w[completed success succeeded].freeze
19
20
  class EventCounters
20
21
  def initialize
21
22
  @counts = {
@@ -103,6 +104,8 @@ module Harnex
103
104
  @session_finalized = false
104
105
  @turn_started_seen = false
105
106
  @last_completed_at = nil
107
+ @last_failed_at = nil
108
+ @last_failed_status = nil
106
109
  @pi_streamed_text_by_message = {}
107
110
  @auto_stop = !!auto_stop
108
111
  @auto_stop_fired = false
@@ -221,14 +224,19 @@ module Harnex
221
224
  end
222
225
 
223
226
  payload[:input_state] = adapter.input_state(screen_snapshot) if include_input_state
224
- task_complete = !!@last_completed_at
227
+ task_complete = task_complete?
228
+ task_failed = task_failed?
229
+ work_state = task_failed ? "failed" : Harnex.work_state_for("running", task_complete: task_complete)
225
230
  payload[:agent_state] = @state_machine.to_s
226
231
  payload[:process_state] = "running"
227
232
  payload[:inbox] = @inbox.stats
228
233
  payload[:last_completed_at] = @last_completed_at&.iso8601
234
+ payload[:last_failed_at] = @last_failed_at&.iso8601
229
235
  payload[:task_complete] = task_complete
236
+ payload[:task_failed] = task_failed
230
237
  payload[:done] = Harnex.work_done_for("running", task_complete: task_complete)
231
- payload[:work_state] = Harnex.work_state_for("running", task_complete: task_complete)
238
+ payload[:work_state] = work_state
239
+ payload[:last_error] = @last_error
232
240
  payload[:model] = summary_model
233
241
  payload[:effort] = meta_hash["effort"]
234
242
  payload[:auto_disconnects] = @event_counters.snapshot[:disconnections]
@@ -236,7 +244,11 @@ module Harnex
236
244
  end
237
245
 
238
246
  def task_complete?
239
- !!@last_completed_at
247
+ !!@last_completed_at && !task_failed?
248
+ end
249
+
250
+ def task_failed?
251
+ !!@last_failed_at
240
252
  end
241
253
 
242
254
  def git_start
@@ -257,7 +269,7 @@ module Harnex
257
269
  inject_sequence([{ text: text, newline: newline }])
258
270
  end
259
271
 
260
- def inject_stop(turn_id: nil)
272
+ def inject_stop(turn_id: nil, interrupt: true)
261
273
  unless structured_transport?
262
274
  raise "session is not running" unless pid && Harnex.alive_pid?(pid)
263
275
  end
@@ -274,15 +286,21 @@ module Harnex
274
286
  end
275
287
  end
276
288
  end
277
- @inject_mutex.synchronize do
278
- begin
279
- adapter.interrupt(turn_id: turn_id)
280
- rescue StandardError
281
- nil
289
+ if interrupt
290
+ @inject_mutex.synchronize do
291
+ begin
292
+ adapter.interrupt(turn_id: turn_id)
293
+ rescue StandardError
294
+ nil
295
+ end
296
+ @state_machine.force_busy!
282
297
  end
283
- @state_machine.force_busy!
298
+ return { ok: true, signal: "interrupt_sent" }
284
299
  end
285
- return { ok: true, signal: "interrupt_sent" }
300
+
301
+ @state_machine.force_busy!
302
+ signal_rpc_done! unless @pid
303
+ return { ok: true, signal: "terminate_sent" }
286
304
  end
287
305
 
288
306
  @inject_mutex.synchronize do
@@ -336,7 +354,12 @@ module Harnex
336
354
 
337
355
  turn_id = nil
338
356
  @inject_mutex.synchronize do
339
- turn_id = adapter.dispatch(**dispatch)
357
+ begin
358
+ turn_id = adapter.dispatch(**dispatch)
359
+ rescue StandardError => e
360
+ mark_task_failed(status: "dispatch_error", error: e.message)
361
+ raise
362
+ end
340
363
  @state_machine.force_busy!
341
364
  @injected_count += 1
342
365
  @last_injected_at = Time.now
@@ -460,14 +483,25 @@ module Harnex
460
483
  @state_machine.force_busy!
461
484
  emit_event("turn_started", turnId: params.dig("turn", "id"))
462
485
  when "turn/completed"
463
- @last_completed_at = Time.now
464
486
  @state_machine.force_prompt!
465
487
  turn = params["turn"] || {}
466
- payload = { turnId: turn["id"] }
467
- payload[:status] = turn["status"] if turn["status"]
468
- payload[:tokenUsage] = params["tokenUsage"] if params["tokenUsage"]
469
- emit_event("task_complete", **payload)
470
- schedule_auto_stop("task_complete", turn_id: payload[:turnId])
488
+ status = turn["status"]
489
+ turn_id = turn["id"] || params["turnId"]
490
+ payload = { turnId: turn_id }
491
+ payload[:status] = status if status
492
+ payload[:tokenUsage] = params["tokenUsage"] if params["tokenUsage"].is_a?(Hash)
493
+ if successful_turn_status?(status)
494
+ @last_completed_at = Time.now
495
+ emit_event("task_complete", **payload)
496
+ else
497
+ mark_task_failed(
498
+ turn_id: turn_id,
499
+ status: status,
500
+ error: extract_turn_error_message(turn),
501
+ codex_error_info: extract_turn_error_info(turn)
502
+ )
503
+ end
504
+ schedule_auto_stop("turn_completed", interrupt: false)
471
505
  when "item/completed"
472
506
  emit_event("item_completed", item: params["item"])
473
507
  @event_counters.record_item(params["item"])
@@ -487,15 +521,70 @@ module Harnex
487
521
  when "account/rateLimits/updated"
488
522
  @rate_limits = params
489
523
  when "error"
490
- @last_error = params["message"].to_s unless params["message"].to_s.empty?
524
+ message = extract_error_notification_message(params)
525
+ @last_error = message unless message.to_s.empty?
491
526
  @state_machine.force_busy!
492
- emit_event("disconnected", source: "error_notification", message: params["message"])
493
- signal_rpc_done!
527
+ emit_event(
528
+ "error",
529
+ source: "error_notification",
530
+ message: message,
531
+ codex_error_info: extract_error_notification_info(params),
532
+ will_retry: params["willRetry"],
533
+ threadId: params["threadId"],
534
+ turnId: params["turnId"]
535
+ )
536
+ signal_rpc_done! if params["turnId"].to_s.empty?
494
537
  end
495
538
  rescue StandardError => e
496
539
  warn("harnex: rpc notification handler error: #{e.message}")
497
540
  end
498
541
 
542
+ def successful_turn_status?(status)
543
+ text = status.to_s
544
+ return true if text.empty?
545
+
546
+ SUCCESSFUL_TURN_STATUSES.include?(text)
547
+ end
548
+
549
+ def mark_task_failed(turn_id: nil, status: nil, error: nil, codex_error_info: nil)
550
+ @last_failed_at = Time.now
551
+ @last_failed_status = status.to_s.empty? ? "failed" : status.to_s
552
+ @last_error = error.to_s unless error.to_s.empty?
553
+
554
+ payload = { status: @last_failed_status }
555
+ payload[:turnId] = turn_id if turn_id
556
+ payload[:message] = error unless error.to_s.empty?
557
+ payload[:codex_error_info] = codex_error_info if codex_error_info
558
+ emit_event("task_failed", **payload)
559
+ end
560
+
561
+ def extract_error_notification_message(params)
562
+ error = params["error"]
563
+ if error.is_a?(Hash)
564
+ error["message"] || error.dig("error", "message") || params["message"]
565
+ else
566
+ params["message"]
567
+ end
568
+ end
569
+
570
+ def extract_error_notification_info(params)
571
+ error = params["error"]
572
+ error.is_a?(Hash) ? error["codexErrorInfo"] : nil
573
+ end
574
+
575
+ def extract_turn_error_message(turn)
576
+ error = turn["error"]
577
+ return error["message"] if error.is_a?(Hash)
578
+ return error if error.is_a?(String)
579
+
580
+ nil
581
+ end
582
+
583
+ def extract_turn_error_info(turn)
584
+ error = turn["error"]
585
+ error.is_a?(Hash) ? error["codexErrorInfo"] : nil
586
+ end
587
+
499
588
  def handle_jsonl_notification(message)
500
589
  event_type = message["type"].to_s
501
590
 
@@ -509,7 +598,7 @@ module Harnex
509
598
  @state_machine.force_prompt!
510
599
  emit_event("task_complete")
511
600
  adapter.request_session_stats_async if adapter.respond_to?(:request_session_stats_async)
512
- schedule_auto_stop("task_complete")
601
+ schedule_auto_stop("task_complete", interrupt: false)
513
602
  when "message_start"
514
603
  @pi_streamed_text_by_message[pi_message_key(message["message"])] = false
515
604
  when "message_update"
@@ -578,12 +667,21 @@ module Harnex
578
667
 
579
668
  def handle_rpc_disconnect(error)
580
669
  msg = error.is_a?(Hash) ? error["message"] : error&.message
670
+ if normal_auto_stop_disconnect?(msg)
671
+ signal_rpc_done!
672
+ return
673
+ end
674
+
581
675
  @last_error = msg.to_s unless msg.to_s.empty?
582
676
  @state_machine.force_busy!
583
677
  emit_event("disconnected", source: "transport", message: msg) rescue nil
584
678
  signal_rpc_done!
585
679
  end
586
680
 
681
+ def normal_auto_stop_disconnect?(message)
682
+ message.to_s.empty? && @auto_stop_fired && (task_complete? || task_failed?)
683
+ end
684
+
587
685
  def dispatch_initial_prompt
588
686
  return unless adapter.respond_to?(:initial_prompt)
589
687
 
@@ -738,10 +836,11 @@ module Harnex
738
836
  return unless defined?(@exit_code) && !@exit_code.nil?
739
837
 
740
838
  exit_path = Harnex.exit_status_path(repo_root, id)
741
- task_complete = !!@last_completed_at
742
- state = @exit_code.to_i == 0 ? "completed" : "failed"
839
+ task_complete = task_complete?
840
+ task_failed = task_failed?
841
+ state = task_failed || @exit_code.to_i != 0 ? "failed" : "completed"
743
842
  payload = {
744
- ok: true,
843
+ ok: !task_failed && state == "completed",
745
844
  id: id,
746
845
  cli: adapter.key,
747
846
  session_id: session_id,
@@ -750,6 +849,7 @@ module Harnex
750
849
  state: state,
751
850
  process_state: "exited",
752
851
  task_complete: task_complete,
852
+ task_failed: task_failed,
753
853
  done: Harnex.work_done_for(state, task_complete: task_complete),
754
854
  work_state: Harnex.work_state_for(state, task_complete: task_complete),
755
855
  started_at: @started_at.iso8601,
@@ -955,7 +1055,7 @@ module Harnex
955
1055
  schedule_auto_stop("prompt_after_busy") if seen_busy && new_state == :prompt
956
1056
  end
957
1057
 
958
- def schedule_auto_stop(reason, turn_id: nil)
1058
+ def schedule_auto_stop(reason, turn_id: nil, interrupt: true)
959
1059
  return unless @auto_stop
960
1060
 
961
1061
  should_fire = @auto_stop_mutex.synchronize do
@@ -970,7 +1070,7 @@ module Harnex
970
1070
 
971
1071
  thread = Thread.new do
972
1072
  begin
973
- inject_stop(turn_id: turn_id)
1073
+ inject_stop(turn_id: turn_id, interrupt: interrupt)
974
1074
  rescue StandardError => e
975
1075
  warn("harnex: auto-stop failed after #{reason}: #{e.message}")
976
1076
  end
@@ -1016,17 +1116,26 @@ module Harnex
1016
1116
 
1017
1117
  def normalize_auto_stop_exit_code!
1018
1118
  return unless @auto_stop
1019
- return unless @last_completed_at
1020
1119
  return unless @auto_stop_fired
1021
1120
 
1121
+ if task_failed?
1122
+ @exit_code = 1 if @exit_code.nil? || @exit_code.zero? || @term_signal
1123
+ @term_signal = nil if @exit_code == 1
1124
+ return
1125
+ end
1126
+
1127
+ return unless task_complete?
1128
+
1022
1129
  @exit_code = 0
1023
1130
  @term_signal = nil
1024
1131
  end
1025
1132
 
1026
1133
  def classify_exit
1027
1134
  return "timeout" if @exit_code == 124
1028
- return "success" if @exit_code == 0 && session_summary_present?
1029
1135
  return "boot_failure" if boot_failure_exit?
1136
+ return "failure" if task_failed?
1137
+ return "success" if @exit_code == 0 && task_complete?
1138
+ return "success" if @exit_code == 0 && session_summary_present?
1030
1139
  return "failure" unless @exit_code == 0
1031
1140
 
1032
1141
  "disconnected"
@@ -1110,7 +1219,7 @@ module Harnex
1110
1219
  files_changed: @git_end[:files_changed],
1111
1220
  commits: @git_end[:commits],
1112
1221
  exit: @exit_reason,
1113
- task_complete: !!@last_completed_at,
1222
+ task_complete: task_complete?,
1114
1223
  signal: @term_signal,
1115
1224
  exit_code: @exit_code,
1116
1225
  last_error: @last_error,
@@ -1256,6 +1365,7 @@ module Harnex
1256
1365
  @event_counters.record(type)
1257
1366
  @events_mutex.synchronize do
1258
1367
  return unless @events_log
1368
+ return if @events_log.closed?
1259
1369
 
1260
1370
  @events_log_seq += 1
1261
1371
  event = {
@@ -45,6 +45,7 @@ module Harnex
45
45
  "process_state" => "unknown",
46
46
  "terminal" => false,
47
47
  "task_complete" => false,
48
+ "task_failed" => false,
48
49
  "done" => false,
49
50
  "work_state" => "unknown",
50
51
  "exit" => nil,
@@ -131,6 +132,7 @@ module Harnex
131
132
  actual = record["actual"] || {}
132
133
  state = classify_summary_state(actual)
133
134
  task_complete = !!actual["task_complete"]
135
+ task_failed = state == "failed" && !task_complete
134
136
  terminal = state != "unknown"
135
137
  {
136
138
  "id" => meta["id"].to_s,
@@ -139,6 +141,7 @@ module Harnex
139
141
  "process_state" => Harnex.process_state_for(state, terminal: terminal),
140
142
  "terminal" => terminal,
141
143
  "task_complete" => task_complete,
144
+ "task_failed" => task_failed,
142
145
  "done" => Harnex.work_done_for(state, task_complete: task_complete),
143
146
  "work_state" => Harnex.work_state_for(state, task_complete: task_complete),
144
147
  "exit" => blank_to_nil(actual["exit"]),
@@ -173,6 +176,7 @@ module Harnex
173
176
  "unknown"
174
177
  end
175
178
  task_complete = record["terminal_event"].to_s == "task_complete"
179
+ task_failed = record["terminal_event"].to_s == "task_failed" || (state == "failed" && !task_complete)
176
180
  terminal = state != "unknown"
177
181
  {
178
182
  "id" => record["id"].to_s,
@@ -181,6 +185,7 @@ module Harnex
181
185
  "process_state" => Harnex.process_state_for(state, terminal: terminal),
182
186
  "terminal" => terminal,
183
187
  "task_complete" => task_complete,
188
+ "task_failed" => task_failed,
184
189
  "done" => Harnex.work_done_for(state, task_complete: task_complete),
185
190
  "work_state" => Harnex.work_state_for(state, task_complete: task_complete),
186
191
  "exit" => history_exit(status),
@@ -1,4 +1,4 @@
1
1
  module Harnex
2
- VERSION = "0.7.6"
3
- RELEASE_DATE = "2026-06-09"
2
+ VERSION = "0.7.7"
3
+ RELEASE_DATE = "2026-06-12"
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: harnex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.6
4
+ version: 0.7.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jikku Jose
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-06-08 00:00:00.000000000 Z
11
+ date: 2026-06-12 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A local PTY harness that wraps terminal AI agents (Claude, Codex, Pi)
14
14
  and adds a control plane for discovery, messaging, and coordination.