Skip to content

feat(tier3): split measured agent time into LLM and tool seconds - #344

Merged
rng1995 merged 10 commits into
mainfrom
naren/port-tier3-model-latency
Nov 6, 2026
Merged

rng1995 merged 10 commits into
mainfrom
naren/port-tier3-model-latency

Conversation

@rng1995

@rng1995 rng1995 commented Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • New module skillevaluator/tier3/harbor/trajectory_timing.py. trajectory_timing(trajectory) returns {"llm_seconds", "tool_seconds", "source"} for one Harbor ATIF trajectory, or None. It uses one of two sources and never mixes them within a trajectory:
    • atif (native timing extras): LLM time is the sum of step.extra.llm_duration_ms, only when every agent step has it. Tool time is the union of tool_calls[].extra started_at/ended_at intervals, so overlapping calls count once, plus duration_ms for calls without an interval, only when every call is timed. When any native extra is present, nothing is inferred.
    • step_timestamps (inferred): each gap between consecutive step timestamps is counted once. Tool time for a step is the wall time of its calls, and parallel calls count their longest. The rest of the gap is LLM time, which then also includes harness and queue time. Call wall time comes from the Wall time: N seconds line that Codex writes in the header of its shell tool output (exec_command, write_stdin, wait, wait_command, shell, shell_command) and apply_patch output, or from the duration_seconds that Harbor keeps in the structured tool metadata of those calls.
  • The header is read only before the first Output: line, so command output cannot supply a duration. A header with two Wall time lines, a wall time over 24 hours, a duplicate call id, tool time longer than the gap after its step, missing or naive (no UTC offset) or backwards timestamps, or a trailing tool step all leave the trial unmeasured. A call to any other tool (update_plan, web search, MCP tools, Claude Code Bash ...) has no measured duration, so the whole trial stays unmeasured. A trajectory with no tool calls measures 0 tool seconds.
  • A merged multi-step trajectory is split at each change of extra.harbor_step_name and each Harbor step is timed on its own, so verifier and setup time between steps is never counted.
  • Steps marked is_copied_context repeat earlier work, so each Harbor step's copied prefix is dropped before the timing source is chosen, as the collector already skips copied agent steps. A copied step after new steps, or a Harbor step with only copied steps, leaves the trial unmeasured. Copied timestamps do not show when the first new response started, so inferred timing also leaves the trial unmeasured when the new steps after a copied prefix start with an agent step. A new user or system step after the copied steps starts the timing as usual. Native llm_duration_ms timing does not depend on step gaps, so it is not affected.
  • load_agent_data computes the timing from the trial trajectory.json it already loads for token counts, and _normalize_trials records it as trial["timing"] for single-row trials.
  • resource_comparison adds llm_seconds, tool_seconds and llm_ms_per_token (1000 × LLM seconds / (prompt + completion tokens), from complete arm totals with a positive token count) next to tokens and agent_seconds. It also adds timing_sources (the sources each arm used), caveats (mixed_timing_sources when an agent's trials mix native and inferred timing) and a timing_definition string. Totals are rounded to milliseconds.
  • The HTML Measured Agent Resources table gets rows for LLM seconds, tool seconds and LLM ms per token, plus one line per agent naming the timing source of each arm and the mixed-source caveat. Payloads without the new fields render as before.
  • docs/reports.mdx documents the measures, sources, coverage rules and sign convention under "Measured agent resources". docs/tier3-live-evaluation.mdx points to it. CHANGELOG entry under Unreleased > Added.

Why

The resources table shows whether a skill changes total tokens and agent execution seconds, but not whether the time went to the model or to tools. No Harbor 0.24 agent converter writes native ATIF timing extras. The Harbor 0.24 Codex converter does keep an ISO timestamp on every step and pairs each tool output with its call by source_call_id, and Codex writes each call's wall time in its own output header. Inferring from those gives the default Codex agent real numbers. Native extras still take priority for agents that write them.

Coverage is partial by design. A Codex trial that also calls update_plan, web search or an MCP tool has no duration for that call, so it shows N/A rather than a guess. Claude Code trajectories with tool calls stay N/A. Timing is diagnostic only: scores, verdicts and gates do not change.

Kept from public

  • resource_comparison's gating applies to the new measures unchanged: deltas only for complete arms with identical entry_id counts, JSON-safe finite non-negative values, and measured/total trial counts per arm. Missing timing stays null (N/A), never zero.
  • agent_seconds still comes from Harbor's result.json agent_execution phase (timezone-aware, setup and verifier excluded) and remains the whole-agent wall time. The new timestamp parsing also rejects naive timestamps.
  • Timing is read from the same bounded _load_bounded_json load of the redacted trial trajectory.json. Redaction keeps step timestamps and the Wall time header (tested). Trials whose trajectory was skipped (too large, or redaction or validation failed) have no timing, as the docs say.
  • Multi-row trials stay out of per-trial resources.
  • The private _traj entry stays out of raw_trial_rewards, and report-budget compaction of the embedded payload is unchanged. The per-trial timing object is three small fields.
  • One Measured Agent Resources section and one resources JSON object; no second latency section. CLI, Markdown and BENCHMARK.md output are unchanged.

Tests

  • New tests/tier3/test_trajectory_timing.py (60 tests):
    • Codex-like inference from step timestamps; Wall time in the command output not counted; two Wall time lines in one header rejected
    • unbounded wall time rejected (400-digit value and just over 24 hours)
    • a Claude-like Bash call without a header, and a non-Codex tool with a Codex-style header, not inferred
    • update_plan, web search, an MCP-style tool, exec and view_image leave the trial unmeasured
    • apply_patch with a header counted; a bare apply_patch result unmeasured
    • namespaced shell tools (functions.shell_command) and wait_command measured
    • consecutive tool steps count each gap once; parallel calls use the longest wall time; a measured zero is kept; no tool calls measure 0 tool seconds
    • missing, earlier, too-short, naive and unparsable timestamps, and a trailing tool step, give no timing; duplicate call ids rejected
    • write_stdin with and without a Codex exec header
    • single-call tool_metadata duration_seconds used; a non-finite value rejected
    • native extras win over inference; partial native LLM or tool timing gives no subtotal; overlapping native intervals counted once; invalid native intervals and durations rejected
    • a fixture produced by Harbor 0.24's own Codex converter from a synthetic Codex rollout (gpt-5-mini), covering bundled steps with source_call_id, parallel calls, shell metadata and a free-form apply_patch call; timing is the same after _redacted_trajectory_data, and removing one call's metadata leaves the trial unmeasured
    • a two-step trial merged by _merged_step_trajectory, timed per Harbor step; a continuation step does not count the gap before it; every Harbor step must be timed
    • copied-context steps: a copied tool step an hour earlier gives 4.0 LLM and 1.0 tool seconds, not 3601.0 and 2.0; copied native llm_duration_ms and tool durations are not counted twice; native extras only on copied steps do not block inference; each Harbor step of a merged trial drops its own copied prefix; a copied step after new work, or only copied steps, give no timing
    • new work that starts with an agent step after copied context gives no timing, alone and as the second Harbor step of a merged trial (it would otherwise report 4.0 LLM seconds and drop the 10 s before the first tool call); a new user or system step after the copied steps gives the full 14.0 LLM and 1.0 tool seconds; native timing after copied context needs no new user step
    • malformed trajectories give no timing
  • tests/tier3/test_resource_usage.py (+22 test cases): timing validation and source tags; arm pairing and ms per token; millisecond rounding; tool totals require every trial; timing deltas without token counters; no deltas for partial or mismatched arms; missing timing is not zero; a measured zero is kept; mixed sources across and within arms flagged; consistent sources not flagged; invalid values (True, -1, "1.0", NaN, infinity, 10**1000, None) stay unavailable.
  • tests/reporting/test_resource_comparison.py (+3 tests): real trajectory.json files flow through agent_eval_result_from_directory to trial["timing"], resources, JSON and HTML (rows, deltas with percentages, timing-source line); an unmeasured arm shows N/A with no delta; mixed sources show the caveat.
  • Fail-before: with origin/main src, test_resource_comparison.py has 3 failed, 2 passed (KeyError: 'timing' and KeyError: 'llm_seconds'), and test_trajectory_timing.py and test_resource_usage.py fail at import (trajectory_timing module and normalized_trial_timing missing). Before the copied-context fix, the 6 copied-context tests failed (for example 3601.0 LLM and 2.0 tool seconds instead of 4.0 and 1.0). Before the agent-first fix, test_new_work_that_starts_with_an_agent_step_after_copied_context_is_unmeasured failed (4.0 LLM and 1.0 tool seconds instead of no timing; 13.0 and 2.0 for the merged trial) and the other 59 passed.
  • On the branch, merged with current main: the three changed test files pass (105 passed). The 31 test files that mention resources, load_agent_data, _normalize_trials, is_copied_context or the new timing code give 2335 passed, 4 skipped and 2 failed (-n 8); the 2 failures are test_sidecar_retains_* in tests/test_harbor_secure_docker_environment.py, which fail the same way on main in this local environment.
  • ruff check and ruff format --check are clean on the changed Python files.
  • scripts/check_oss_boundary.py passed.

🤖 Generated with Claude Code

rng1995 and others added 3 commits November 5, 2026 01:09
Add trajectory_timing(), which splits one ATIF trajectory's time into
LLM seconds and tool seconds and tags the result with its source.
Native ATIF timing extras win when present (source atif). Otherwise the
split is inferred from step timestamps plus the Wall time header that
Codex writes for its shell tools and apply_patch, or the
duration_seconds Harbor keeps in their tool metadata (source
step_timestamps). Each timestamp gap counts once, parallel calls count
their longest call, and each Harbor step of a merged multi-step
trajectory is timed on its own. Unmeasured tools, untrusted or
inconsistent evidence leave the trial unmeasured instead of zero.

Signed-off-by: Narendran Raghavan <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Record each single-row trial's trajectory timing and extend
resource_comparison with llm_seconds, tool_seconds and
llm_ms_per_token, under the existing complete-arm and matching-case
rules. The resources object also lists the timing sources per arm and
flags mixed_timing_sources. The HTML Measured Agent Resources table
gets the new rows and a timing-source line per agent. Scores, verdicts
and gates do not change.

Signed-off-by: Narendran Raghavan <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Narendran Raghavan <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>

@rng1995 rng1995 left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Automated review: 3 findings (0 P1, 0 P2, 3 P3); 2 posted inline.

Findings outside the diff or about the description:

  • /Users/nraghavan/work/se-port-2026-10-28/prs/W64.md:50: P3: The PR text still has an unfilled template placeholder. Line 50 reads '- On the branch: TEST_SUMMARY', so the ## Tests section of public PR #344 has no exact count for the branch run, which the W47 style requires. The other count claims are correct: 50 tests in test_trajectory_timing.py, +22 in test_resource_usage.py (40 vs 18 on main), +3 in test_resource_comparison.py, and the fail-before results reproduce.

Suggested fix: Replace the placeholder with the measured result, for example '- On the branch: the three changed test files pass (95 passed), and 29 test files that touch resources, load_agent_data or _normalize_trials pass.' Then update the #344 body from this file.

Comment thread CHANGELOG.md Resolved
rng1995 and others added 2 commits November 5, 2026 10:09
Steps marked is_copied_context repeat work from an earlier trajectory.
Timing them counted their tool time again, counted the gap after a copied
step as LLM time, and summed copied native llm_duration_ms twice. Drop
each Harbor step's copied prefix before choosing a timing source, and
leave the trial unmeasured when a copied step follows new work or a
Harbor step has only copied steps.

Signed-off-by: Narendran Raghavan <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolve the CHANGELOG.md conflict by keeping the new main entries and
this branch's entry in the existing Unreleased > Added subsection.

Signed-off-by: Narendran Raghavan <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@rng1995
rng1995 marked this pull request as ready for review November 5, 2026 18:29
rng1995 and others added 3 commits November 5, 2026 10:44
…t starts with an agent step

After a Harbor step's is_copied_context prefix is dropped, its first new
step starts the timing. When that step is an agent response, no new
timestamp shows when the model started it, so the model time of the
first response was silently left out while the trial was still reported
as measured. Inferred timing now leaves the trial unmeasured in that
case. A new user or system step after the copied steps still starts the
timing, and native llm_duration_ms timing is unaffected because it does
not depend on step gaps.

Signed-off-by: Narendran Raghavan <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolve the docs/reports.mdx conflict: keep this PR's LLM and tool timing
table and notes at the end of "Measured agent resources", followed by
main's new "Failure reasons and per-trial rows" section (#305).

Signed-off-by: Narendran Raghavan <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Narendran Raghavan <[email protected]>
@rng1995
rng1995 enabled auto-merge (squash) November 5, 2026 22:25
rng1995 added a commit that referenced this pull request Nov 6, 2026
The bridge_services fixture probes /healthz until the bridge answers. The
probe's server worker can still be finishing after urllib has read the
response, so under CI load a test could start with one worker already
active. test_slow_drip_request_line_and_headers_obey_an_absolute_deadline
then counted that worker next to its own connection and failed with
'assert 2 == 1' (seen on PRs #306 and #344). Wait for the bridge's workers
to drain before yielding the fixture.

Signed-off-by: Narendran Raghavan <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
@rng1995
rng1995 merged commit 55d2d04 into main Nov 6, 2026
22 checks passed
@rng1995
rng1995 deleted the naren/port-tier3-model-latency branch November 6, 2026 03:49
@rng1995

rng1995 commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator Author

Post-merge review: 2 finding(s) (P3). Follow-up PR: #365.

🤖 Generated with Claude Code

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.

1 participant