Skip to content

fix(state): keep native loop lastCompletedAt consistent with completedRuns#171

Open
andrei-hasna wants to merge 1 commit into
mainfrom
openloops/open-codewith/5205b768-8f6a-4452-95ea-3324669a825e-216e4af7
Open

fix(state): keep native loop lastCompletedAt consistent with completedRuns#171
andrei-hasna wants to merge 1 commit into
mainfrom
openloops/open-codewith/5205b768-8f6a-4452-95ea-3324669a825e-216e4af7

Conversation

@andrei-hasna

Copy link
Copy Markdown
Contributor

Summary

Fixes BUG-LOOP-001: native loop stats reported lastCompletedAt set while completedRuns was 0.

get_thread_schedule_stats() computed last_completed_at as MAX(completed_at_ms) over all run rows. But defer_thread_schedule_run() and fail_thread_schedule_run() also write completed_at_ms on their rows (status deferred/failed). A schedule whose only terminal rows were deferred/failed therefore surfaced a lastCompletedAt timestamp while completedRuns stayed 0 — the exact inconsistency observed in heartbeat run 999d7020 (deferredRuns=14, completedRuns=0, lastCompletedAt set).

Change

  • Constrain the aggregate to completed rows only: MAX(CASE WHEN status = 'completed' THEN completed_at_ms END).
  • last_started_at (MAX(started_at_ms) over all rows) and last_error semantics are unchanged.
  • Read-side stats only — no migration, no write-path change, scheduler counters/behavior unchanged.

Tests

  • New regression thread_schedule_stats_last_completed_at_only_reflects_completed_runs: asserts last_completed_at stays None while a scheduled prompt runs with a prior deferred run, and after completion reflects the completed run's timestamp — even when a newer deferred run carries a larger completed_at_ms.
  • Updated two existing tests that codified the leaky behavior (deferred-only and failed-only runs now assert last_completed_at: None).
  • cargo test -p codex-state schedule → 25 passed; cargo fmt --check and cargo clippy -p codex-state --all-targets clean.

🤖 Generated with Claude Code

…dRuns

get_thread_schedule_stats() computed last_completed_at as MAX(completed_at_ms) over all run rows, but deferred and failed runs also write completed_at_ms. A schedule with only deferred/failed runs therefore reported lastCompletedAt set while completedRuns stayed 0 (BUG-LOOP-001), making heartbeat health reporting internally inconsistent.

Constrain the aggregate to status='completed' rows (MAX(CASE WHEN status='completed' THEN completed_at_ms END)) so last_completed_at only reflects an actual completed run. last_started_at and last_error semantics are unchanged; this is a read-side stats fix with no scheduler behavior change.

Add regression coverage asserting last_completed_at stays None for deferred-only and failed-only runs (even though those rows carry completed_at_ms) and reflects the latest completed run when one exists, including when a newer deferred run carries a larger completed_at_ms. Update the two existing tests that codified the leaky behavior.
@andrei-hasna
andrei-hasna force-pushed the openloops/open-codewith/5205b768-8f6a-4452-95ea-3324669a825e-216e4af7 branch from c72b47c to eb0e542 Compare July 9, 2026 12:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant