Skip to content

bug(watchdog): a recovered turn records the claim time as completed_at — 5m 32s shown for a 64-s turn #3589

Closed

Description

@pavshulin

Summary

When the watchdog claims a turn from the agent's retained terminal (#2944/#2947: the backend restarted mid-turn, the agent finished and retained the result), the execution row's completed_at is the claim time, not the time the agent finished. duration_ms then includes the claim grace (180 s) plus up to one cleanup cycle (300 s). A 64-second turn is recorded as 5 m 33 s. The Execution Details page shows the contradiction on one screen: the Duration tile says 5m 32s and the transcript footer says 1m 4s.

Build: v1.0.0-rc2 (96f88f9) · Matrix row: B9 (abilityai/trinity-enterprise#847) · Env: from-source dev stack, single uvicorn worker, PostgreSQL

Steps

  1. Start a sync chat that takes ~60 s (run sleep 60 and print done).
  2. ~15 s in: docker restart trinity-backend.
  3. Wait for the agent to finish and for the watchdog to claim the result ([Watchdog] result_recovered: execution … retained 266s ago in the backend log).
  4. GET /api/agents/{name}/executions/{id} and open Execution Details in the UI.

Observed

  • Row: status = success, response prefixed "Recovered by the watchdog", duration_ms = 332721, completed_at = the watchdog claim time (09:56:18Z).
  • The retained record on the agent: retained_at_iso = 09:51:51Z, envelope.metadata.duration_ms = 64234; the envelope keys are exactly status, response, execution_log, metadata, session_id, terminal_reason — there is no execution_time_ms key. [fixed: values from the 2026-10-12 re-run; first run was 332800 / 64010]
  • UI: Duration tile 5m 32s, transcript footer 1m 4s, on the same page.
  • Reproduced on every watchdog-recovered turn (twice on 2026-10-11, once on 2026-10-12; +5 to +8 min each).

Expected

completed_at = the agent's retained_at (or the terminal timestamp in the record), duration_ms = the turn's real duration. The claim time can be kept as a separate field or in the activity details, but the displayed and billed duration must be the turn's.

Where (file:line at v1.0.0-rc2)

  • src/backend/services/cleanup_service.py:245 _claim_retained_result builds the ExecutionResultEnvelope (line 285) from the retained record's status/response/error/…/execution_time_ms keys and hands it to apply_result (line 297). The record's retained_at is read only for the grace check (_retained_age_seconds, line 233).
  • src/backend/services/task_execution_service.py:3120 apply_result → src/backend/db/schedules/executions.py:381 update_execution_status, which stamps completed_at with the current time (line 436, parse_iso_timestamp(utc_now_iso())) and always computes duration_ms = completed_at − started_at (line 445). [fixed: the filed body said "when the envelope carries no execution_time_ms" — the writer never reads execution_time_ms at all; the envelope's execution_time_ms reaches only the activity details blob (task_execution_service.py:3309) and the terminal event payload (:3329), as the ExecutionResultEnvelope comment in models.py:2318 states.]
  • The agent never sends execution_time_ms: the retained envelope is result_callback._success_envelope (docker/base-image/agent_server/services/result_callback.py:157), called from agent_server/routers/chat.py:142 (sync chat) and :328 (task), and it carries the duration only as metadata.duration_ms; agent_server/services/retained_results.py:120 wraps that envelope unchanged under retained_at. [fixed: the filed body cited routers/chat.py:184, which is the HTTP response dict, not the retained envelope.] So the claim path hands apply_result an envelope with execution_time_ms = None, and even a populated value would not change the row.

Suggested fix

update_execution_status needs an optional terminal timestamp (e.g. completed_at=) threaded through apply_result; _claim_retained_result passes the record's retained_at, so duration_ms = retained_at − started_at ≈ the turn (started_at is the dispatch time, retained_at the agent's finish time). Mapping metadata.duration_ms into execution_time_ms is not sufficient on its own — it only changes the activity details and the event payload. [fixed] Add a unit test that a claimed terminal's completed_at ≤ retained_at + 1 s and its duration_ms is within a few seconds of metadata.duration_ms.

Regression?

Not a regression: the watchdog claim path is new in this release. It is a fabricated duration on a recovery path, the class the release checklist for this row lists as blocking.

Activity

  1. added
    priority-p2
    type-bug
    theme-reliability
    on Oct 12, 2026
  2. pavshulin commented on Oct 12, 2026

    @pavshulin
    ContributorAuthor

    /claim

  3. added
    status-in-progress
    on Oct 12, 2026
  4. github-actions commented on Oct 12, 2026

    @github-actions

    Claimed by @pavshulin. Label status-in-progress added.

    Next steps:

    1. Create branch: feature/3589-<short-slug>
    2. Implement the feature
    3. Open a PR with Fixes #3589
  5. pavshulin commented on Oct 12, 2026

    @pavshulin
    ContributorAuthor

    PR: #3629 · agent-side follow-up: #3628

  6. added
    status-in-dev
    and removed
    status-in-progress
    on Oct 13, 2026
  7. pavshulin commented on Oct 13, 2026

    @pavshulin
    ContributorAuthor

    rechecked on rc3 15e3502c0 — pass.

    Env: pash (PG profile). Fixer≠verifier: code = vybe's approval on PR #3629 plus the independent validation of the follow-up floor (PR #3649, APPROVE, comment 6143704643); live leg on the deployed tag = mine.

    Repro (the issue's own): inter-agent hop, backend restarted 20 s into a ~120 s callee turn, callee finished orphaned → retained; watchdog claimed 211 s later.

    • duration_ms = 126576 — the real wall clock, NOT claim-time inflated (the F16 defect would have stamped ~337 s).
    • completed_at = the retained instant (13:55:25.626783Z; watchdog log "retained 211s ago" agrees).
    • Caller-side agent_collaboration closed with completed_at byte-identical to the row.
    • Execution Details shows the real duration on screen.
  8. removed
    status-in-dev
    on Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

priority-p2theme-reliabilitytype-bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions