Repository navigation
fix(agent-core-v2): record measured tool durations in tool message attributes - #5266
Conversation
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
🦋 Changeset detectedLatest commit: 74c2119 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
commit: |
There was a problem hiding this comment.
💡 Codex Review
kimi-code/packages/core/src/tool/tool-executor.ts
Lines 289 to 290 in 429abf3
When an afterExecute hook returns a replacement without durationMs, reconcile replaces the timed result and silently drops its duration. This occurs in production paths such as legacyRejectedResult used by dynamic-tool selection and desktop browser guidance, which construct fresh untimed ToolResult objects. The journal remains enriched from the adapter snapshot, so those tool results again render differently live versus during compaction/resume; retain the previous duration when the replacement does not supply one.
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| const finalized = await this.finalize(context.call.id, coerceLegacyResult(result, this.name)); | ||
| prepared.result = finalized; | ||
| return legacyResult(finalized, this.name); | ||
| return { ...legacyResult(finalized, this.name), durationMs: prepared.durationMs }; |
There was a problem hiding this comment.
Propagate durations when legacy tool execution throws
When execution.execute(...) or the legacy result finalizer throws, this return statement is bypassed: the adapter records prepared.durationMs in finally, but the core executor converts the exception to an untimed failure result. The live tool message therefore has no wall-time header, while turnProjection.ts later enriches the journal from legacyToolDurationMs(snapshot), so compaction and resumed requests still differ from the live request for failed tools—the cache-prefix inconsistency this change is intended to eliminate. Ensure the measured duration is also attached to exception results.
Useful? React with 👍 / 👎.
429abf3 to
517a403
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 517a40311f
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| type: 'synthetic', | ||
| prepared: { kind: 'synthetic', result, durationMs: 0 }, | ||
| result: legacyResult(result, this.name), | ||
| result: { ...legacyResult(result, this.name), durationMs: 0 }, |
There was a problem hiding this comment.
Attach a duration when legacy preparation rejects
When resolveExecution(...) rejects, execution never reaches this timed synthetic return and the core executor creates an untimed failure message. However, turnProjection.ts later persists legacyToolDurationMs(snapshot) as 0, so the live follow-up omits the wall-time header while compaction or resume renders Wall time: 0.000 seconds, recreating the cache-prefix mismatch for tools whose execution resolution fails. Ensure preparation failures also produce a result carrying duration 0.
Useful? React with 👍 / 👎.
517a403 to
a5ea08f
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a5ea08fb13
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| isError: failed ? true : undefined, | ||
| stopTurn: 'stopTurn' in result && result.stopTurn === true ? true : undefined, | ||
| detached, | ||
| durationMs: 'durationMs' in result ? result.durationMs : undefined, |
There was a problem hiding this comment.
Cover pre-execution failures with a zero duration
When a tool call fails before adapter execution—for example because its JSON is malformed, the tool is unknown or invalid, or a starting/permitting hook vetoes it—the core executor produces a ToolResult without durationMs, so this line omits the timing header from the live follow-up. AgentTurnProjection nevertheless persists legacyToolDurationMs(snapshot), which is 0 when no preparation duration exists, so compaction or resume later renders Wall time: 0.000 seconds and changes the cache prefix. Ensure these pre-execution failure results carry the same zero duration and cover these additional trigger paths.
AGENTS.md reference: AGENTS.md:L60-L60
Useful? React with 👍 / 👎.
a5ea08f to
74c2119
Compare
Record MoonshotAI#5247 ported and MoonshotAI#5258, MoonshotAI#5259 and MoonshotAI#5266 equivalent to the fork's own sidebar, composer and tool-duration implementations, each with an executable probe.
Requirement or Bug
Tool results lost their measured duration on live turn requests. Since the agent loop runtime port (#4630), live tool messages are created by the core runtime, whose tool result contract had no duration field — so the request-time rendering pipeline saw
attributes.durationMson compaction and resumed requests (enriched from the journal) but never on the live path. The same tool result produced byte-different request content depending on the path, which broke prompt-cache prefix sharing between turn and compaction requests (observed as an 18.86% cache hit rate on a manual compaction).Bug Reproduction Steps
N/A (internal fix; root cause below was confirmed by diffing turn vs compaction request payloads byte-for-byte on a real session)
Root Cause
The executor-measured duration (
prepared.durationMs, the same monotonic value the journal records ontool.ended) had no channel into the core tool result contract:ToolReturn/ToolResultcarried no duration, sotoolOutcomeMessagecould not writeattributes.durationMsfor live messages. This is a fundamental fix: it connects the missing channel instead of re-deriving the value downstream.Code Changes
@moonshot-ai/corecontract: optionaldurationMsonToolReturn,ToolResult, andToolMessageAttributes;ToolStoppingContext.abandongains an optional second parameter so abandoning without a result can still report the measured duration.tool-executor.ts):returned()andabandon()thread the duration through every result shape; the two post-execution failure replacements (output-close failure, afterExecute hook throw) carry the discarded result's duration inline; journal/fold/projection are untouched.toolOutcomeMessagewritesattributes.durationMs(detach acknowledgements carry none, matching the journal).LegacyExecutableToolAdapterfills the duration on all paths: execute (measured), synthetic (0), and the three abandon paths — the value is frozen at abandon time so the live message and the journal-enriched value are the same number.result.durationMs === legacyToolDurationMs(snapshot)(live value equals the journal source); a three-path consistency test asserting turn, compaction, and resumed requests render one identical tool result (measured and 0ms forms).Behavior Changes and Affected Users
ToolStoppingContext.abandon(result?)(result?, durationMs?)— additive, existing callers unaffectedCoverage:
packages/core387/387; on this base,packages/agent-core-v2focused suites —fullCompaction.test.ts119/119 (including the new three-path consistency case),toolExecutor+plan90/90;tests/scenarios/conversation.test.ts24/25, the single failure being the manual-mode approval case (item.endeddouble-fire), which also reproduces on a cleanmaincheckout locally and is unrelated to this change. Typecheck forpackages/coreandpackages/agent-core-v2clean;oxlint --type-aware0/0. The product code is byte-identical to the previous CI-green head of this PR, whose full-suite run (7717 passed, 10 failures all attributed to pre-existing/environmental causes) is in the PR history.Checklist
/approve). (internal PR, N/A)gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.