pi-tool-duration 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.2.3] - 2026-09-18
4
+
5
+ ### Fixed
6
+
7
+ - Preserve timing in native compaction input when long tool output is truncated, including split-turn summaries. Ordinary requests still append one duration marker, and recorded tool content and details remain unchanged.
8
+
9
+ ## [0.2.2] - 2026-09-18
10
+
11
+ ### Fixed
12
+
13
+ - Keep duration markers out of terminal tool output after reload, resume, and branch navigation by storing timing in hidden session entries and annotating only model-request copies.
14
+ - Preserve model-visible timings across restoration, compaction summaries, and retained compaction history without changing tool content or details.
15
+
16
+ ### Changed
17
+
18
+ - Verify recorded output, reload, restored timing, branch isolation, and repeated model requests in regression tests.
19
+ - Run clean-install checks on Node 22.19 and Node 24 in GitHub Actions.
20
+
3
21
  ## [0.2.1] - 2026-08-09
4
22
 
5
23
  ### Fixed
package/README.md CHANGED
@@ -13,13 +13,17 @@ Pi already shows tool timing in the TUI (`Took Xs`), but that timing is UI-only.
13
13
 
14
14
  ## How it works
15
15
 
16
- The extension measures from Pi's `tool_execution_start` through `tool_execution_end`. When elapsed time is at or above the configured threshold, or Pi marks the result as failed, it appends one text block to the finalized model-visible tool message:
16
+ The extension measures from Pi's `tool_execution_start` through `tool_execution_end`. When elapsed time is at or above the configured threshold, or Pi marks the result as failed, it saves the timing in a hidden session entry. Before each model request, it appends one text block to that request's copy of the tool result:
17
17
 
18
18
  ```text
19
19
  [duration: 5.0s]
20
20
  ```
21
21
 
22
- This leaves Pi's TUI output unchanged, so built-in timing such as `Took 5.0s` is not duplicated. Scope: Pi tools that emit tool execution events, including built-ins and extension tools. Direct `!` / `!!` shell commands and RPC `bash` command messages are not tool results and are not annotated.
22
+ Compaction input copies put timing before the tool output so it survives Pi's truncation of long results. Recorded tool output stays unchanged, including after reload, resume, and branch navigation. Timings remain available to the model across those transitions without duplicating Pi's native TUI timing such as `Took 5.0s`.
23
+
24
+ Markers already saved as tool text by versions through 0.2.1 remain unchanged. They cannot be safely distinguished from genuine tool output with the same text.
25
+
26
+ Scope: Pi tools that emit tool execution events, including built-ins and extension tools. Direct `!` / `!!` shell commands and RPC `bash` command messages are not tool results and are not annotated.
23
27
 
24
28
  Pi starts these timers during sequential tool-call preflight. In a parallel batch, a call's elapsed time can therefore include time spent preparing later siblings. This mirrors Pi's TUI timing.
25
29
 
@@ -30,7 +34,7 @@ Requires Pi 0.84.0 or later.
30
34
  ```bash
31
35
  pi install . # local, global settings
32
36
  pi install -l --approve . # local, project settings
33
- pi install npm:pi-tool-duration # after npm publish
37
+ pi install npm:pi-tool-duration # published package
34
38
  ```
35
39
 
36
40
  ## Try without installing
@@ -5,9 +5,12 @@
5
5
  * how long a call actually took. pi already measures this for the TUI
6
6
  * ("Took Xs") but the model does not see that timing.
7
7
  */
8
- import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
8
+ import type { ContextEvent, ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
9
9
 
10
10
  const DEFAULT_THRESHOLD_MS = 1000;
11
+ const TIMING_ENTRY = "pi-tool-duration";
12
+
13
+ type SavedTiming = { toolCallId: string; timestamp: number; duration: string };
11
14
 
12
15
  function parseThreshold(value: unknown): number | undefined {
13
16
  if (typeof value !== "string" && typeof value !== "number") return undefined;
@@ -24,6 +27,35 @@ function thresholdMs(pi: ExtensionAPI): number {
24
27
  );
25
28
  }
26
29
 
30
+ function withDurations(
31
+ messages: ContextEvent["messages"],
32
+ ctx: ExtensionContext,
33
+ position: "append" | "prepend" = "append",
34
+ ) {
35
+ const saved = new Map<string, string>();
36
+ for (const entry of ctx.sessionManager.getBranch()) {
37
+ if (entry.type !== "custom" || entry.customType !== TIMING_ENTRY) continue;
38
+ const timing = entry.data as SavedTiming | undefined;
39
+ if (
40
+ typeof timing?.toolCallId !== "string" ||
41
+ typeof timing.timestamp !== "number" ||
42
+ typeof timing.duration !== "string"
43
+ ) continue;
44
+ saved.set(`${timing.timestamp}:${timing.toolCallId}`, timing.duration);
45
+ }
46
+
47
+ return messages.map((message) => {
48
+ if (message.role !== "toolResult") return message;
49
+ const duration = saved.get(`${message.timestamp}:${message.toolCallId}`);
50
+ if (!duration) return message;
51
+ const marker = { type: "text" as const, text: duration };
52
+ return {
53
+ ...message,
54
+ content: position === "prepend" ? [marker, ...message.content] : [...message.content, marker],
55
+ };
56
+ });
57
+ }
58
+
27
59
  export default function (pi: ExtensionAPI) {
28
60
  const starts = new Map<string, number>();
29
61
  const durations = new Map<string, string>();
@@ -54,12 +86,21 @@ export default function (pi: ExtensionAPI) {
54
86
  durations.delete(event.message.toolCallId);
55
87
  if (!duration) return;
56
88
 
57
- return {
58
- message: {
59
- ...event.message,
60
- content: [...event.message.content, { type: "text" as const, text: duration }],
61
- },
62
- };
89
+ // Keep timing out of recorded tool content: Pi also uses it to rebuild the TUI.
90
+ pi.appendEntry<SavedTiming>(TIMING_ENTRY, {
91
+ toolCallId: event.message.toolCallId,
92
+ timestamp: event.message.timestamp,
93
+ duration,
94
+ });
95
+ });
96
+
97
+ pi.on("context", (event, ctx) => ({ messages: withDurations(event.messages, ctx) }));
98
+
99
+ pi.on("session_before_compact", ({ preparation }, ctx) => {
100
+ // Native summarization bypasses context hooks and truncates tool text from the end.
101
+ // Put timing first in its input copies, never in saved messages or ordinary requests.
102
+ preparation.messagesToSummarize = withDurations(preparation.messagesToSummarize, ctx, "prepend");
103
+ preparation.turnPrefixMessages = withDurations(preparation.turnPrefixMessages, ctx, "prepend");
63
104
  });
64
105
 
65
106
  const clearTimings = () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-tool-duration",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "Append model-visible durations to slow Pi tool results",
5
5
  "type": "module",
6
6
  "author": "Mitch Fultz (https://github.com/fitchmultz)",