Repository navigation
fix(watchdog): a claimed turn records the agent's finish time, not the claim time (abilityai/trinity#3589) - #3629
Conversation
…e claim time (#3589) `update_execution_status` and `complete_activity` stamped `completed_at = now` unconditionally and measured `duration_ms` to it. The #2944 watchdog claim runs >= 180 s (claim grace) plus up to one 300 s cleanup cycle after the agent retained the terminal, so a 64 s turn was recorded as 5 m 33 s on the execution row and the dispatch activity while the transcript footer on the same page said 1 m 4 s -- a fabricated duration on a recovery path. The producer's terminal instant now rides the wire contract: `ExecutionResultEnvelope.completed_at` -> `terminal_from_callback_payload` -> `TerminalEnvelope.completed_at` -> `apply_result` -> `ExecutionResult.completed_at` / `complete_activity(completed_at=)`. Both writers resolve it through one helper, `utils.helpers.terminal_completed_at` (None -> now, naive -> UTC, never in the future), so `duration_ms` measures the turn. The claim fills it from the record's `retained_at` through the same parser the grace check uses (`_retained_at`); unreadable -> None -> stamp now, never a refused claim, and the final log line no longer `int(inf)`s. A record whose `execution_time_ms` is outside the int4 contract is dropped before the pydantic constructor instead of raising into an `error` verdict. The #1083 result callback (retried to the lease deadline, re-sent by the startup sweep) inherits the field once the agent stamps it; the agent-side stamp is a base-image follow-up. Co-Authored-By: Claude Fable 5.1 <[email protected]>
) Co-Authored-By: Claude Fable 5.1 <[email protected]>
…t_sys_modules Co-Authored-By: Claude Fable 5.1 <[email protected]>
Manual verification on the rc2 box (B9 stack, PostgreSQL, single worker) — 2026-10-12Branch checked out at
The watchdog claimed the turn ~5 min after the finish, as before ( Box returned to |
vybe
left a comment
There was a problem hiding this comment.
merge-train: validated (Phase 2) and merged individually; no integration train needed for this batch.
…tarted_at (trinity-enterprise#882) Abilityai#3629 made the producer's terminal instant authoritative; terminal_completed_at clamped only the future, so a past value reached the completed_at column verbatim. The helper gains a REQUIRED keyword-only floor: parameter — the fourth rule in the one place both writers resolve through — and update_execution_status / complete_activity pass the row's own server-written started_at. A floored write logs a warning and remains a successful claim; an in-window instant (the Abilityai#3589 behaviour) is recorded verbatim; the future-clamp is unchanged; a naive floor is normalized to UTC like the given instant. TDD: 10 of the 15 new tests in tests/unit/test_882_completed_at_floor.py were red before the fix — the column assertion failed with the backdated value written verbatim. test_3589_claim_completed_at.py stays green (37/37; four direct helper calls updated to the new signature, assertions unchanged). test_1482_write_params.py's persistence fixture moves to an in-window instant. Fixes abilityai/trinity-enterprise#882 Co-Authored-By: Claude Fable 5 <[email protected]>
Summary
completed_at = now: a 64 s turn was recorded as 5 m 33 s on the execution row and the dispatch activity, on the same page as a transcript footer saying 1 m 4 s.ExecutionResultEnvelope.completed_at→terminal_from_callback_payload→TerminalEnvelope.completed_at→apply_result→ExecutionResult.completed_at/complete_activity(completed_at=). Both writers resolve it through one helper,utils.helpers.terminal_completed_at(None→ now, naive → UTC, never in the future), soduration_msmeasures the turn. Every existing caller passes nothing and keeps today's behaviour.retained_atthrough the same parser the grace check uses (_retained_at); unreadable →None→ stamp now, never a refused claim, and the final log line no longerint(inf)s. A record whoseexecution_time_msis outside the int4 contract is dropped before the pydantic constructor instead of raising into anerrorverdict (which withheld a finished turn every cycle).Changes
src/backend/utils/helpers.py—terminal_completed_at(the one rule set for both writers)src/backend/db/write_params.py,db/schedules/executions.py—ExecutionResult.completed_at, writer uses itsrc/backend/db/activities.py,database.py,services/activity_service.py— kw-onlycompleted_aton the activity closesrc/backend/models.py,services/execution_envelope.py— contract field + mapper (naive → UTC)src/backend/services/task_execution_service.py—apply_resultthreads it to both writers on both branchessrc/backend/services/cleanup_service.py—_retained_at,_int4_or_none, claim passescompleted_attests/unit/test_3589_claim_completed_at.py(new, 37 cases),tests/unit/test_1482_write_params.py(field pin)docs/memory/architecture/execution.md,docs/memory/feature-flows/cleanup-service.md, learnings fragmentTest Plan
cd tests && pytest unit/test_3589_claim_completed_at.py -v(37 passed; SQLite viadb_harness, PG whenTEST_POSTGRES_URLis set)completed_at=Nonestamps now; in-contractexecution_time_mskept; nometadata.duration_msmapping)test_1083_apply_result,test_1083_callback_endpoint,test_2944_*,test_1804_*,test_1832_duration_clamp(AST guard),test_1332,test_1578,test_cancelled_not_overwritten,test_2434_execution_time_bound,test_679,test_terminal_write_cas_gate,test_2433,test_models_centralized,test_1482sleep 60,docker restart trinity-backend~15 s in, wait for[Watchdog] result_recovered) →GET /api/agents/{name}/executions/{id}showscompleted_at= the agent'sretained_atandduration_ms≈ 64 s; Execution Details Duration tile matches the transcript footer; thechat_startactivity shows the same durationFollow-up
result_callbackenvelopes carry no finish time, so a refactor: fire-and-forget dispatch — a hung turn holds zero backend resource #1083 callback delivered late (retried to the lease deadline / startup sweep) still records the delivery time. The contract field this PR adds makes that a base-image-only change.Review notes
task_execution_service.pyis code-health hotspot Feature/vector log retention #3 and refactor: collapse 9-path cleanup pyramid once agent is authoritative #429 (cleanup pyramid) is open; this PR adds 6 and ~30 lines there respectively, no paydown bundled.Fixes #3589
🤖 Generated with Claude Code