|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | import json |
| 6 | +import time |
6 | 7 | from dataclasses import asdict |
7 | 8 | from pathlib import Path |
8 | 9 |
|
@@ -239,3 +240,151 @@ def test_running_dispatch_takes_priority_over_failure(tmp_path): |
239 | 240 |
|
240 | 241 | assert result is not None |
241 | 242 | assert result.name == "task-run" |
| 243 | + |
| 244 | + |
| 245 | +# --- PENDING dispatch search (Pass 3) --- |
| 246 | + |
| 247 | + |
| 248 | +def test_pending_dispatch_stale_match(tmp_path): |
| 249 | + """PENDING dispatch with stale attempt_history is returned.""" |
| 250 | + sp = tmp_path / "campaign.json" |
| 251 | + d = DispatchRecord( |
| 252 | + name="d1", |
| 253 | + status=DispatchStatus.PENDING, |
| 254 | + issue_url=_ISSUE_URL, |
| 255 | + labels_cleaned=False, |
| 256 | + dispatched_session_id="", |
| 257 | + attempt_history=[{"ended_at": time.time() - 120, "status": "failure"}], |
| 258 | + ) |
| 259 | + _write_state(sp, [d]) |
| 260 | + |
| 261 | + result = find_dispatch_for_issue(_ISSUE_URL, [sp]) |
| 262 | + |
| 263 | + assert result is not None |
| 264 | + assert result.name == "d1" |
| 265 | + |
| 266 | + |
| 267 | +def test_pending_dispatch_too_fresh_skipped(tmp_path): |
| 268 | + """PENDING dispatch within quiet period is skipped.""" |
| 269 | + sp = tmp_path / "campaign.json" |
| 270 | + d = DispatchRecord( |
| 271 | + name="d1", |
| 272 | + status=DispatchStatus.PENDING, |
| 273 | + issue_url=_ISSUE_URL, |
| 274 | + labels_cleaned=False, |
| 275 | + dispatched_session_id="", |
| 276 | + attempt_history=[{"ended_at": time.time() - 5, "status": "failure"}], |
| 277 | + ) |
| 278 | + _write_state(sp, [d]) |
| 279 | + |
| 280 | + result = find_dispatch_for_issue(_ISSUE_URL, [sp]) |
| 281 | + |
| 282 | + assert result is None |
| 283 | + |
| 284 | + |
| 285 | +def test_pending_dispatch_no_history_skipped(tmp_path): |
| 286 | + """PENDING dispatch with empty attempt_history is skipped (never dispatched).""" |
| 287 | + sp = tmp_path / "campaign.json" |
| 288 | + d = DispatchRecord( |
| 289 | + name="d1", |
| 290 | + status=DispatchStatus.PENDING, |
| 291 | + issue_url=_ISSUE_URL, |
| 292 | + labels_cleaned=False, |
| 293 | + dispatched_session_id="", |
| 294 | + attempt_history=[], |
| 295 | + ) |
| 296 | + _write_state(sp, [d]) |
| 297 | + |
| 298 | + result = find_dispatch_for_issue(_ISSUE_URL, [sp]) |
| 299 | + |
| 300 | + assert result is None |
| 301 | + |
| 302 | + |
| 303 | +def test_pending_dispatch_with_session_id_skipped(tmp_path): |
| 304 | + """PENDING dispatch with active dispatched_session_id is skipped.""" |
| 305 | + sp = tmp_path / "campaign.json" |
| 306 | + d = DispatchRecord( |
| 307 | + name="d1", |
| 308 | + status=DispatchStatus.PENDING, |
| 309 | + issue_url=_ISSUE_URL, |
| 310 | + labels_cleaned=False, |
| 311 | + dispatched_session_id="abc", |
| 312 | + attempt_history=[{"ended_at": time.time() - 120, "status": "failure"}], |
| 313 | + ) |
| 314 | + _write_state(sp, [d]) |
| 315 | + |
| 316 | + result = find_dispatch_for_issue(_ISSUE_URL, [sp]) |
| 317 | + |
| 318 | + assert result is None |
| 319 | + |
| 320 | + |
| 321 | +def test_running_beats_pending(tmp_path): |
| 322 | + """RUNNING dispatch wins over stale PENDING dispatch.""" |
| 323 | + sidecar = tmp_path / "sidecar.jsonl" |
| 324 | + _write_sidecar(sidecar, [IssueSidecarEntry(issue_url=_ISSUE_URL, status="completed", ts=_TS)]) |
| 325 | + running = DispatchRecord( |
| 326 | + name="task-run", |
| 327 | + status=DispatchStatus.RUNNING, |
| 328 | + sidecar_path=str(sidecar), |
| 329 | + ) |
| 330 | + pending = DispatchRecord( |
| 331 | + name="task-pending", |
| 332 | + status=DispatchStatus.PENDING, |
| 333 | + issue_url=_ISSUE_URL, |
| 334 | + labels_cleaned=False, |
| 335 | + dispatched_session_id="", |
| 336 | + attempt_history=[{"ended_at": time.time() - 120, "status": "failure"}], |
| 337 | + ) |
| 338 | + sp = tmp_path / "campaign.json" |
| 339 | + _write_state(sp, [pending, running]) |
| 340 | + |
| 341 | + result = find_dispatch_for_issue(_ISSUE_URL, [sp]) |
| 342 | + |
| 343 | + assert result is not None |
| 344 | + assert result.name == "task-run" |
| 345 | + |
| 346 | + |
| 347 | +def test_terminal_beats_pending(tmp_path): |
| 348 | + """Terminal FAILURE wins over stale PENDING dispatch.""" |
| 349 | + sidecar = tmp_path / "sidecar.jsonl" |
| 350 | + _write_sidecar(sidecar, [IssueSidecarEntry(issue_url=_ISSUE_URL, status="failed", ts=_TS)]) |
| 351 | + failure = DispatchRecord( |
| 352 | + name="task-fail", |
| 353 | + status=DispatchStatus.FAILURE, |
| 354 | + sidecar_path=str(sidecar), |
| 355 | + labels_cleaned=False, |
| 356 | + ) |
| 357 | + pending = DispatchRecord( |
| 358 | + name="task-pending", |
| 359 | + status=DispatchStatus.PENDING, |
| 360 | + issue_url=_ISSUE_URL, |
| 361 | + labels_cleaned=False, |
| 362 | + dispatched_session_id="", |
| 363 | + attempt_history=[{"ended_at": time.time() - 120, "status": "failure"}], |
| 364 | + ) |
| 365 | + sp = tmp_path / "campaign.json" |
| 366 | + _write_state(sp, [pending, failure]) |
| 367 | + |
| 368 | + result = find_dispatch_for_issue(_ISSUE_URL, [sp]) |
| 369 | + |
| 370 | + assert result is not None |
| 371 | + assert result.name == "task-fail" |
| 372 | + |
| 373 | + |
| 374 | +def test_pending_dispatch_ended_at_zero_treated_as_stale(tmp_path): |
| 375 | + """PENDING dispatch with ended_at=0.0 (process crashed unrecorded) is treated as stale.""" |
| 376 | + sp = tmp_path / "campaign.json" |
| 377 | + d = DispatchRecord( |
| 378 | + name="d1", |
| 379 | + status=DispatchStatus.PENDING, |
| 380 | + issue_url=_ISSUE_URL, |
| 381 | + labels_cleaned=False, |
| 382 | + dispatched_session_id="", |
| 383 | + attempt_history=[{"ended_at": 0.0, "status": "interrupted"}], |
| 384 | + ) |
| 385 | + _write_state(sp, [d]) |
| 386 | + |
| 387 | + result = find_dispatch_for_issue(_ISSUE_URL, [sp]) |
| 388 | + |
| 389 | + assert result is not None |
| 390 | + assert result.name == "d1" |
0 commit comments