#7208: Capture tool error occurredAtMs at tool-result boundary
agents
Cluster:
Error Handling in Agent Tools
### Why
Review note: `occurredAtMs` should reflect the earliest possible timestamp at the boundary where the tool result event is received, not after sanitization/processing.
### What
- Capture `occurredAtMs = Date.now()` at the start of `handleToolExecutionEnd` and propagate it into `lastToolError` when a tool fails.
- Add a short comment clarifying this is handler receipt time (pi-agent-core events do not currently include timestamps).
### Scope
Small/low-risk; improves timestamp fidelity without changing any external tool result contracts.
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR captures a best-effort `occurredAtMs` timestamp at the top of `handleToolExecutionEnd` and stores it on `state.lastToolError` when a tool call fails. The intent is to make the error timestamp reflect handler receipt time (tool-result event boundary) rather than later processing/sanitization time, since `pi-agent-core` events currently lack native timestamps. The change is localized to the embedded Pi tool event handler and does not alter the emitted tool-result event contract (only internal error tracking gains the timestamp).
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk.
- The change is small, localized, and does not modify outward-facing tool event payloads; it only captures `Date.now()` earlier and threads it into an internal `lastToolError` record. No control flow changes beyond adding a timestamp field.
- No files require special attention
<!-- greptile_other_comments_section -->
<sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub>
**Context used:**
- Context from `dashboard` - CLAUDE.md ([source](https://app.greptile.com/review/custom-context?memory=fd949e91-5c3a-4ab5-90a1-cbe184fd6ce8))
- Context from `dashboard` - AGENTS.md ([source](https://app.greptile.com/review/custom-context?memory=0d0c8278-ef8e-4d6c-ab21-f5527e322f13))
<!-- /greptile_comment -->
Most Similar PRs
#3362: fix: auto-repair and retry on orphan tool_result errors
by samhotchkiss · 2026-01-28
74.4%
#8312: fix: add logging and markers for tool result repair
by ekson73 · 2026-02-03
73.9%
#8888: fix: increase tool result/error context limits
by joetomasone · 2026-02-04
73.8%
#16856: feat(agents): add tool execution duration tracking with configurabl...
by EunHyeokJung · 2026-02-15
73.7%
#7760: fix(agents): resolve message ordering conflict during tool execution
by aryan877 · 2026-02-03
73.4%
#10261: fix(agents): prevent exec tool errors from leaking to channels (#9651)
by nu-gui · 2026-02-06
73.0%
#11417: fix: add defense against consecutive tool misuse loops
by seojoonkim · 2026-02-07
73.0%
#13282: fix(agents): instruct agent not to retry lost tool results
by thebtf · 2026-02-10
72.7%
#12487: fix(agents): strip orphaned tool_result when tool_use is sanitized ...
by skylarkoo7 · 2026-02-09
72.7%
#3647: fix: sanitize tool arguments in session history
by nhangen · 2026-01-29
72.6%