Add UTC timestamp to background exec exit notifications#7164
Add UTC timestamp to background exec exit notifications#7164markvandeven wants to merge 2 commits into
Conversation
| // Includes an explicit UTC timestamp in the payload so delayed delivery is still understandable. | ||
| expect( | ||
| bySession.some((event) => /\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} UTC\]/.test(event)), | ||
| ).toBe(true); |
There was a problem hiding this comment.
[P1] The timestamp regex here is almost certainly wrong due to escaping \d inside a regex literal. In JS/TS regex literals, \d matches a literal d, so /\[\d{4}-.../ will never match the generated [ + digits output. This makes the test ineffective (it can only fail) and can mask regressions in the timestamp formatting.
To match digits, use \d without the extra escape in a regex literal (e.g. /\[\d{4}-.../ → /\[\d{4}-.../ should become /\[\d{4}-.../? specifically \d → \d is wrong; it should be \d? Actually in a regex literal it should be \[\d{4}-.../? Please verify with a quick sanity check: it should be /\[\d{4}-/? No, it should be /\[\d{4}-/? The key is: \d should be \d? Either way, remove the extra backslash before d so the engine sees \d.
Also appears in src/agents/bash-tools.test.ts:304 (same line).
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/bash-tools.test.ts
Line: 302:305
Comment:
[P1] The timestamp regex here is almost certainly wrong due to escaping `\d` inside a regex literal. In JS/TS regex literals, `\d` matches a literal `d`, so `/\[\d{4}-.../` will never match the generated `[` + digits output. This makes the test ineffective (it can only fail) and can mask regressions in the timestamp formatting.
To match digits, use `\d` *without* the extra escape in a regex literal (e.g. `/\[\d{4}-.../` → `/\[\d{4}-.../` should become `/\[\d{4}-.../`? specifically `\d` → `\d` is wrong; it should be `\d`? Actually in a regex literal it should be `\[\d{4}-.../`? Please verify with a quick sanity check: it should be `/\[\d{4}-/`? No, it should be `/\[\d{4}-/`? The key is: `\d` should be `\d`? Either way, remove the extra backslash before `d` so the engine sees `\d`.
Also appears in `src/agents/bash-tools.test.ts:304` (same line).
How can I resolve this? If you propose a fix, please make it concise.| const formatUtc = (ms: number) => { | ||
| const iso = new Date(ms).toISOString(); | ||
| const [date, timeWithMs] = iso.split("T"); | ||
| const time = (timeWithMs ?? "").split(".")[0]; | ||
| if (!date || !time) { | ||
| return iso; | ||
| } | ||
| return `${date} ${time} UTC`; | ||
| }; | ||
| const occurredAtMs = Date.now(); | ||
| const ts = `[${formatUtc(occurredAtMs)}]`; | ||
|
|
||
| const summary = output | ||
| ? `Exec ${status} (${session.id.slice(0, 8)}, ${exitLabel}) :: ${output}` | ||
| : `Exec ${status} (${session.id.slice(0, 8)}, ${exitLabel})`; | ||
| ? `Exec ${status} ${ts} (${session.id.slice(0, 8)}, ${exitLabel}) :: ${output}` | ||
| : `Exec ${status} ${ts} (${session.id.slice(0, 8)}, ${exitLabel})`; | ||
| enqueueSystemEvent(summary, { sessionKey }); |
There was a problem hiding this comment.
[P2] maybeNotifyOnExit stamps the message with occurredAtMs = Date.now() at notification time, not when the process actually exited. Since the motivating problem is delayed/out-of-order delivery, this timestamp can be misleading (it reflects enqueue time, not completion time). If markExited already records an exit timestamp on the session, prefer using that; otherwise consider capturing Date.now() in the exit handler and storing it on the session before enqueue.
This impacts all background exec exit notifications emitted from maybeNotifyOnExit.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/bash-tools.exec.ts
Line: 391:406
Comment:
[P2] `maybeNotifyOnExit` stamps the message with `occurredAtMs = Date.now()` at notification time, not when the process actually exited. Since the motivating problem is delayed/out-of-order delivery, this timestamp can be misleading (it reflects enqueue time, not completion time). If `markExited` already records an exit timestamp on the session, prefer using that; otherwise consider capturing `Date.now()` in the exit handler and storing it on the session before enqueue.
This impacts all background exec exit notifications emitted from `maybeNotifyOnExit`.
How can I resolve this? If you propose a fix, please make it concise.
Additional Comments (1)
This affects timestamps shown via Prompt To Fix With AIThis is a comment left during a code review.
Path: src/agents/pi-embedded-subscribe.handlers.tools.ts
Line: 167:175
Comment:
[P2] `occurredAtMs` is captured with `Date.now()` inside `handleToolExecutionEnd`, which is likely *after* the tool failure occurred (and after sanitization/processing). If the goal is an “occurred-at” timestamp to correlate with other events, this should be derived from the tool result payload if it contains a timestamp (or captured at the boundary where the tool result is received), otherwise it will reflect handler time and can be skewed by queueing/backpressure.
This affects timestamps shown via `buildEmbeddedRunPayloads` when a tool fails.
How can I resolve this? If you propose a fix, please make it concise. |
Why
Tool exit system notifications can arrive out-of-order vs. chat messages due to async delivery. Including the occurred-at timestamp inside the payload makes delayed notifications self-explanatory and easy to correlate.
What
[...] UTCtimestamp into theExec completed/failedsystem event text emitted for backgrounded exec sessions (notifyOnExit).Notes
Greptile Overview
Greptile Summary
This PR adds explicit UTC timestamps to user-visible error/exit text for background
execcompletions and for embedded-run tool failure fallbacks, with accompanying test updates.Key changes:
src/agents/bash-tools.exec.tsnow prependsExec completed/failedsystem events with a[YYYY-MM-DD HH:MM:SS UTC]timestamp whennotifyOnExitis enabled.src/agents/pi-embedded-runner/run/payloads.tsand related types) now optionally includes[... UTC]based onlastToolError.occurredAtMs.pi-embedded-subscribenow recordsoccurredAtMsonlastToolErrorand tests assert the timestamp presence.Main concern: the added tests appear to have an incorrect regex (escaping
\dinside a regex literal), and both timestamp sources currently useDate.now()at emit/handler time rather than the actual process/tool completion time, which can undermine the intent to disambiguate delayed delivery.Confidence Score: 3/5
(2/5) Greptile learns from your feedback when you react with thumbs up/down!
Context used:
dashboard- CLAUDE.md (source)dashboard- AGENTS.md (source)