Fix: record started_at for background agent tasks so duration is reported#2493
Open
nankingjing wants to merge 2 commits into
Open
Fix: record started_at for background agent tasks so duration is reported#2493nankingjing wants to merge 2 commits into
nankingjing wants to merge 2 commits into
Conversation
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.
Problem
Background agent tasks never have
runtime.started_atset, so their runduration is silently lost. Background bash tasks set it correctly.
The only place
started_atis ever assigned is the bash worker startup(
background/worker.py):Agent tasks reach the running state through
BackgroundTaskManager._mark_task_running(called from
background/agent_runner.py), which set every startup fieldexcept
started_at:Since
TaskRuntime.started_atdefaults toNone(background/models.py), agenttasks keep
started_at is Nonefor their whole lifetime.Impact
Downstream consumers guard on
started_at, so for agent tasks they silentlydegrade:
publish_terminal_notificationscomputesduration_s = None, so the completionnotification omits the
Duration: …line that bash tasks show._mark_task_completed/_mark_task_failed/_mark_task_timed_out/_mark_task_killedall gate telemetry onif runtime.started_at and runtime.finished_at:, which is never true for agenttasks — so the
background_task_completedevent (withduration_s) is neveremitted for them.
Fix
Record the first transition into
runningas the start time in_mark_task_running, mirroring what the bash worker does. The assignment isguarded by
started_at is Nonebecause_mark_task_runningis also invokedagain after each approval is resolved; overwriting would otherwise shrink the
measured duration to the last approval gap.
This has no effect on bash tasks (they don't go through
_mark_task_running) andis a no-op for tasks that already reached a terminal state (the early return
guarding terminal status is untouched).
Test
Adds
test_mark_task_running_sets_started_at_once, asserting that the first_mark_task_runningsetsstarted_at, a subsequent re-mark (e.g. after anapproval) preserves it, and completion then yields
finished_at >= started_at.Verification
python -m py_compileon both changed files: passes.(runtime deps such as
kosong/kaoswere not installable here).