fix(state): keep native loop lastCompletedAt consistent with completedRuns#171
Open
andrei-hasna wants to merge 1 commit into
Open
Conversation
…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
force-pushed
the
openloops/open-codewith/5205b768-8f6a-4452-95ea-3324669a825e-216e4af7
branch
from
July 9, 2026 12:01
c72b47c to
eb0e542
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes BUG-LOOP-001: native loop stats reported
lastCompletedAtset whilecompletedRunswas 0.get_thread_schedule_stats()computedlast_completed_atasMAX(completed_at_ms)over all run rows. Butdefer_thread_schedule_run()andfail_thread_schedule_run()also writecompleted_at_mson their rows (statusdeferred/failed). A schedule whose only terminal rows were deferred/failed therefore surfaced alastCompletedAttimestamp whilecompletedRunsstayed 0 — the exact inconsistency observed in heartbeat run999d7020(deferredRuns=14, completedRuns=0, lastCompletedAtset).Change
MAX(CASE WHEN status = 'completed' THEN completed_at_ms END).last_started_at(MAX(started_at_ms)over all rows) andlast_errorsemantics are unchanged.Tests
thread_schedule_stats_last_completed_at_only_reflects_completed_runs: assertslast_completed_atstaysNonewhile 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 largercompleted_at_ms.last_completed_at: None).cargo test -p codex-state schedule→ 25 passed;cargo fmt --checkandcargo clippy -p codex-state --all-targetsclean.🤖 Generated with Claude Code