Repository navigation
[perf] agent: record where a turn spends its time (#449) - #469
Conversation
|
全量 Python 套件在本机 worktree 的结果,附基线对照,避免把环境问题读成回归:
|
| connection.connect(self.gateway["OPENDIRECTOR_AGENT_SOCKET"]) | ||
| connection.sendall((json.dumps({ | ||
| "id": str(identity or uuid4()), "cliCommand": self.command, "projectId": str(self.project) if self.project else None, "name": name, "arguments": arguments, | ||
| "timing": {"startedAt": STARTED, "sentAt": time.monotonic()}, |
There was a problem hiding this comment.
job wait reuses the process start timestamp on every poll, so later polls count earlier waits as CLI startup. Please start timing before the CLI imports and count startup once per process.
There was a problem hiding this comment.
Fixed in 3b04a604. STARTED now sits above the CLI's imports, and each request carries timing.firstCall. _spawn() charges start-up only for the process's first request and reports spawn: 0.0 for the warm ones after it, so one job wait is billed once no matter how many windows it polls.
Checked end to end against a real CLI subprocess and a fake gateway: job wait --timeout 2.5 sent 4 polls with spawns [0.734, 0.0, 0.0, 0.0]. Before the change those read 0.7, 1.7, 2.7, 3.7 — the whole poll loop billed as start-up.
|
|
||
| def report(turns): | ||
| calls = [call for turn in turns for call in turn.get('calls', [])] | ||
| durations = [call['endedAt'] - call['startedAt'] for call in calls if 'endedAt' in call] |
There was a problem hiding this comment.
In a 10 s replay, an 8 s Bash call containing a 6 s CLI call reports 140% tool time and −4 s unaccounted. Please use the union of call intervals for wall-clock shares and include CLI startup in the call duration used to calculate its share.
There was a problem hiding this comment.
Fixed in 3b04a604. call_interval() stretches each call back by the start-up it paid for, and union_seconds() merges nested intervals, so wall-clock shares now count covered time once. callSeconds is the union; callSecondsSum and overlapSeconds keep the double count visible instead of hiding it inside one number.
Your replay now reads, in a 10 s turn: callSeconds 8.0 (was 14.0), callShare 0.8 (was 1.4), unaccountedSeconds 2.0 (was −4.0). Pinned in tests/test_bench_turn_timing.py::test_nested_calls_do_not_report_more_tool_time_than_the_turn and ::test_a_call_is_charged_the_start_up_it_paid_for.
| times is not the same problem as an expensive one repeated a few times. | ||
| """ | ||
| share = numbers['spawnShareOfTurn'] | ||
| if share is None or not numbers['turnSeconds']: |
There was a problem hiding this comment.
In a manual project show turn, the --help invocations bypassed the gateway, leaving their startup time unmeasured. Please report the transport recommendation as inconclusive when startup coverage is unknown.
There was a problem hiding this comment.
Fixed in 3b04a604. The report now prints startupCoverage (decomposed calls ÷ calls), and verdict() returns inconclusive while it is below 1: a call that never reached the transport still started a process, so the uncovered start-up could move the band in either direction.
A project show turn with one --help probe now reports inconclusive: launch timing covers 1 of 2 calls, so start-up cannot be ruled in or out; a call that never reaches the transport still starts a process, and its start-up went unmeasured instead of picking a transport. Turns where every call goes through the transport still land in the pre-registered bands, which is what the existing band tests pin. Both docs say this: docs/development/agent-timing{,.zh-CN}.md.
|
T5's first step: one turn reports where its time went, and the bands that turn the numbers into a decision are pre-registered rather than chosen after seeing results. I reviewed the code, not the description.
Two non-blocking items:
One precision note for the docs, not a defect: Ran: Not run: the Python suite or the bench script locally. The turn-timing report has only been exercised with replayed/fake turns here; no real-model baseline exists yet, as the body states. Coordinator: ping after this Reviewer PASS and green CI on f3d03bb. This skill does not merge. |
Review of ThinkFlowLab#469 found three ways the timing report disagreed with itself. - `job wait` polls in one process and sent the process start on every request, so poll two billed poll one's wait as start-up. The clock is now anchored before the CLI imports, and each request says whether it is the process's first; the transport charges start-up once and reports none for the already-warm requests that follow. - The bench added call durations up, so a Bash call containing a CLI call counted the inner seconds twice: 8 s + 6 s in a 10 s turn read as 140% tool time with -4 s unaccounted. Wall-clock shares now use the union of the calls' intervals, each stretched by the start-up it paid for, and the sum plus overlap stay visible as `callSecondsSum` and `overlapSeconds`. - A `--help` probe starts a process but never reaches the transport, so its start-up is unmeasured. The verdict now reports `startupCoverage` and calls the transport decision inconclusive instead of naming a band when it is below 1. Tests pin each case; the full Python suite passes (2897 passed, 118 skipped).
## summary
- `cli.py` anchors a monotonic clock at import and reports it as
`timing.startedAt` on every request, because interpreter start-up and
imports finish before any request reaches the host and cannot be
observed from there.
- `agent/cli_transport.py` converts that into `spawn` and `work` on the
tool events it already emits, and drops an implausible value instead of
poisoning the turn's totals.
- `agent/timing.py` folds tool events into one entry per call and writes
`agent-runs/timing/<turn>.json` at turn end. The ledger keeps per-call
numbers plus one aggregate; shares across turns stay offline, and a
failed write loses a file rather than a turn.
- `apps/api` keeps `timing` out of the activity list so the persisted
conversation keeps the shape it had: a turn with hundreds of tool events
already sits at the 64 KB the CLI reads back.
- `scripts/bench_turn_timing.py` reads the ledgers and reports the turn's
wall clock, call time and its share, start-up and its share of the turn
and of calls, the unaccounted remainder, and P50/P90 of one call.
- `docs/development/agent-timing{,.zh-CN}.md` record what is measured, how
to read it, and the pre-registered bands: start-up above 40% of the turn
replaces the CLI transport, 20-40% runs a native channel alongside it,
below 20% keeps the transport and cuts calls and probes instead. The
bands stay out of CI; timing assertions inherit machine and provider
jitter.
Measured at `dfe61c67`: `opendirector --help` costs 1.16 s of which 0.85 s
is imports, and the ThinkFlowLab#449 trace's 1,224 tool calls put the start-up floor at
23.7 minutes without saying what share of the turn that is. This change
turns the floor into a share before any transport work starts.
Review of ThinkFlowLab#469 found three ways the timing report disagreed with itself. - `job wait` polls in one process and sent the process start on every request, so poll two billed poll one's wait as start-up. The clock is now anchored before the CLI imports, and each request says whether it is the process's first; the transport charges start-up once and reports none for the already-warm requests that follow. - The bench added call durations up, so a Bash call containing a CLI call counted the inner seconds twice: 8 s + 6 s in a 10 s turn read as 140% tool time with -4 s unaccounted. Wall-clock shares now use the union of the calls' intervals, each stretched by the start-up it paid for, and the sum plus overlap stay visible as `callSecondsSum` and `overlapSeconds`. - A `--help` probe starts a process but never reaches the transport, so its start-up is unmeasured. The verdict now reports `startupCoverage` and calls the transport decision inconclusive instead of naming a band when it is below 1. Tests pin each case; the full Python suite passes (2897 passed, 118 skipped).
face643 to
3b04a60
Compare
|
Supersedes my PASS at f3d03bb: the tip moved to a rebase plus
Two follow-ups, both in the advisory report path only — nothing here changes turn behaviour, and I am not holding the merge for them:
Ran: Not run: the Python suite or the bench script locally; the numbers in the body come from replay, not a live provider, as the body states. Coordinator: this is the PASS for the current tip; merging is handled separately. |
Why
Issue #449 asks for TTFA/TTC measurement (T5) before any tool-overhead optimization. The existing trace carries the events but no timestamps: 609 JSON-RPC messages, 173 tool calls, 954
job waitpolls, and a total duration that nothing records. The only thing known today is a floor — 1,224 tool calls × 1.16 s of CLI start-up ≈ 23.7 minutes — and a floor is not a share, so it cannot say whether the transport, the model loop, or project synchronization should be optimized first.This PR makes a turn report where its time went, and pre-registers the bands that turn the numbers into a decision.
What changes
One call now yields three numbers instead of a guess:
spawnworkcli.pyanchors a monotonic clock at import and sendstiming.startedAtwith each request.agent/cli_transport.pyturns that intospawnandworkon the tool events it already emits, and drops an impossible value instead of poisoning the turn's totals.agent/timing.pyfolds tool events into one entry per call and writesagent-runs/timing/<turn>.jsonwhen the turn ends. Raw numbers only: percentiles and shares stay offline, so a broken measurement can at worst lose one file and never a turn.scripts/bench_turn_timing.pyreads those files and prints the turn's wall clock, call time and share, start-up and its share of the turn and of calls, the unaccounted remainder (model thinking plus orchestration), and P50/P90 of one call.docs/development/agent-timing.md(+zh-CN) records what is measured, how to read it, and the bands below.Decision bands
Shares use the turn's wall clock, not the call count: batching already lowered the count, and a cheap call repeated many times is a different problem from an expensive one repeated a few times.
When more than half the turn is outside tool calls, fewer turns and fewer discovery probes pay before any transport change does. A runtime that reports no start-up leaves the question open rather than answered: unknown is not zero, and the report says so instead of printing
0.The bands are advisory and stay out of CI. Timing assertions inherit the flakiness of network, machine load and provider latency; evidence belongs in a report, not in a gate. The repository already has this convention for
tests/browser/reference_drag_performance.mjs("Diagnostic CPU profiles to /tmp; no pass/fail thresholds").What does not change
conversation.jsonkeeps the shape it had:timingis stripped before it reaches the activity list, so the 64 KB the CLI reads back does not grow. A turn with ~950 tool events is already at that edge.Validation
tests/test_agent_timing.py— a running then finished call folds into one entry; a runtime without transport timing keeps call time but reports start-up as unknown; events without identity are ignored; the ledger lands beside the other runtime state; an unwritable ledger never raises; the transport reports start-up through a real unix socket and drops a clock disagreement.tests/test_bench_turn_timing.py— pins the three bands and the "most of the turn is outside tool calls" note to the turn's wall clock, and reads back what a turn actually wrote.tests/test_assistant_conversations.py::test_a_turn_writes_its_timing_beside_its_runtime_state— a real turn through the API writes the ledger, and the persisted turn contains no timing.Not in this PR
head()before and after each tool event, plus the visibility and decode anchors)./canvasrefreshes per task is currently invisible to the server.