fix(ack): bound worker polling and detect stale heartbeats

This commit is contained in:
2026-09-19 19:30:20 +08:00
parent 2917708b10
commit 94c66bc695
5 changed files with 132 additions and 39 deletions
+23 -3
View File
@@ -5,6 +5,7 @@ import io
import json
import sys
import unittest
from datetime import datetime, timezone
from pathlib import Path
from unittest import mock
@@ -62,12 +63,30 @@ class WorkerProbeClassifyTests(unittest.TestCase):
self.assertEqual(result["status"], "stall")
self.assertEqual(result["stallReason"], "usage-limit")
def test_heartbeat_means_progress_even_without_tail_markers(self) -> None:
def test_fresh_heartbeat_means_progress_even_without_tail_markers(self) -> None:
result = worker_probe.classify(
"some unclassified output", "2026-08-23T12:00:00Z"
"some unclassified output",
"2026-08-23T12:00:00Z",
now=datetime(2026, 8, 23, 12, 2, tzinfo=timezone.utc),
)
self.assertEqual(result["status"], "progress")
self.assertEqual(result["heartbeatAt"], "2026-08-23T12:00:00Z")
self.assertEqual(result["heartbeatAgeSeconds"], 120.0)
def test_stale_heartbeat_is_unknown_even_with_working_tail(self) -> None:
result = worker_probe.classify(
["• Working (18m)"],
"2026-08-23T12:00:00Z",
now=datetime(2026, 8, 23, 12, 16, tzinfo=timezone.utc),
)
self.assertEqual(result["status"], "unknown")
self.assertEqual(result["stallReason"], "stale-heartbeat")
self.assertEqual(result["heartbeatAgeSeconds"], 960.0)
def test_invalid_heartbeat_is_unknown(self) -> None:
result = worker_probe.classify("some output", "not-a-timestamp")
self.assertEqual(result["status"], "unknown")
self.assertEqual(result["stallReason"], "invalid-heartbeat")
def test_working_marker_means_running_without_heartbeat(self) -> None:
result = worker_probe.classify(["• Working (12s)"], None)
@@ -87,6 +106,7 @@ class WorkerProbeClassifyTests(unittest.TestCase):
class WorkerProbeMainTests(unittest.TestCase):
def test_main_emits_single_json_with_progress(self) -> None:
fresh_heartbeat = worker_probe.utc_now().isoformat().replace("+00:00", "Z")
with (
mock.patch.object(
worker_probe,
@@ -97,7 +117,7 @@ class WorkerProbeMainTests(unittest.TestCase):
worker_probe,
"run_json",
side_effect=[
dispatch_show(heartbeat="2026-08-23T12:00:00Z"),
dispatch_show(heartbeat=fresh_heartbeat),
terminal_read(["Working (5s)"]),
],
),