Skip to content

fix(watchdog): a claimed turn records the agent's finish time, not the claim time (abilityai/trinity#3589) - #3629

Merged
vybe merged 3 commits into
devfrom
feature/3589-claim-completed-at
Oct 13, 2026
Merged

vybe merged 3 commits into
devfrom
feature/3589-claim-completed-at

Conversation

@pavshulin

@pavshulin pavshulin commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • The bug: watchdog fails a completed synchronous execution after a backend restart instead of recovering its result #2944 watchdog claim applies the agent's retained terminal ≥ 180 s (claim grace) + up to one 300 s cleanup cycle after the agent finished, and both terminal writers stamped 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.
  • 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. Every existing caller passes nothing and keeps today's behaviour.
  • 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 (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 it
  • src/backend/db/activities.py, database.py, services/activity_service.py — kw-only completed_at on the activity close
  • src/backend/models.py, services/execution_envelope.py — contract field + mapper (naive → UTC)
  • src/backend/services/task_execution_service.py — apply_result threads it to both writers on both branches
  • src/backend/services/cleanup_service.py — _retained_at, _int4_or_none, claim passes completed_at
  • tests/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 fragment

Test Plan

  • New tests pass: cd tests && pytest unit/test_3589_claim_completed_at.py -v (37 passed; SQLite via db_harness, PG when TEST_POSTGRES_URL is set)
  • Mutation: with the ten source files reverted to the merge-base in a scratch worktree, 34 of the new tests go red; the 3 that stay green pin unchanged behaviour (completed_at=None stamps now; in-contract execution_time_ms kept; no metadata.duration_ms mapping)
  • Neighbouring suites unaffected: 317 passed across 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_1482
  • Manual verification on the rc2 box: issue repro (sync chat sleep 60, docker restart trinity-backend ~15 s in, wait for [Watchdog] result_recovered) → GET /api/agents/{name}/executions/{id} shows completed_at = the agent's retained_at and duration_ms ≈ 64 s; Execution Details Duration tile matches the transcript footer; the chat_start activity shows the same duration

Follow-up

Review notes

  • Plan reviewed via /autoplan (strategy + engineering voices); both flagged the writer-side clamp and the activity row — adopted. /cso skipped (P2; no new endpoint, auth or credential surface — the only new input is an agent-authored timestamp, bounded in the writer).
  • Debt adjacency: task_execution_service.py is 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

 Pavlo and others added 2 commits October 12, 2026 13:46
…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]>
@pavshulin

pavshulin commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor Author

Manual verification on the rc2 box (B9 stack, PostgreSQL, single worker) — 2026-10-12

Branch checked out at ba48e6d31 on the from-source dev stack (uvicorn --reload), issue repro re-run (~/b9/v-3589.sh, a copy of session-7's s7-3589.sh with a fresh Idempotency-Key):

before (issue, 09:50Z run) after (this branch, 13:37Z run)
agent retained_at 09:51:51.458Z 13:38:13.424Z
row completed_at 09:56:18.218Z (the claim) 13:38:13.424Z (= retained_at)
row duration_ms 332721 66123 (dispatch 13:37:07.301 → finish 13:38:13.424)
metadata.duration_ms (CLI's own) 64234 64308
chat_start activity duration_ms 332723 66115

The watchdog claimed the turn ~5 min after the finish, as before (t+360s poll is the first success); the recorded instant and both durations are now the turn's. The ~1.8 s between metadata.duration_ms and duration_ms is dispatch overhead (started_at is the dispatch stamp), the same gap a live turn shows.

Box returned to v1.0.0-rc2 afterwards. Correction: the rc2 backend does not boot on the migrated schema (alembic: Can't locate revision 0098_proactive_consent_not_asked — the dev-based branches ran Alembic past rc2's head). Restoring the pre-session dump is the documented way back to rc2 state; left for the box owner.

@vybe vybe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge-train: validated (Phase 2) and merged individually; no integration train needed for this batch.

@vybe
vybe merged commit d427814 into dev Oct 13, 2026
27 checks passed
developerjillur pushed a commit to developerjillur/trinity that referenced this pull request Oct 13, 2026
…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]>
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.

2 participants