pi-tool-duration 0.2.0 → 0.2.1

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,18 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.2.1] - 2026-08-09
4
+
5
+ ### Fixed
6
+
7
+ - Use Pi's error status instead of guessing from arbitrary result text or metadata, eliminating false duration markers on successful tools.
8
+ - Append duration markers to finalized model-visible messages so the TUI keeps its single native timer, missing-content tools stay safe, blocked preflight failures are covered, and marker-like tool output is still measured.
9
+ - Keep the long threshold flag readable in `pi --help` and declare the documented Pi 0.84.0 host floor in package metadata.
10
+ - Keep the integration-test dependency lockfile installable from the public npm registry.
11
+
12
+ ### Changed
13
+
14
+ - Added real Pi CLI integration coverage over a local HTTP model transport for timing, flags, failures, parallel calls, lifecycle cleanup, and result preservation.
15
+
3
16
  ## [0.2.0] - 2026-08-06
4
17
 
5
18
  ### Changed
package/README.md CHANGED
@@ -13,13 +13,15 @@ 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 matches Pi `tool_execution_start` and `tool_result` events by `toolCallId`. When elapsed time is at or above the configured threshold, or a result reports a non-zero exit code, it appends one text block:
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:
17
17
 
18
18
  ```text
19
19
  [duration: 5.0s]
20
20
  ```
21
21
 
22
- Scope: Pi tools that emit `tool_result` events, including built-ins and extension tools. Direct `!` / `!!` shell commands and RPC `bash` command messages are not tool results and are not annotated.
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.
23
+
24
+ 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.
23
25
 
24
26
  ## Install
25
27
 
@@ -36,21 +38,23 @@ pi install npm:pi-tool-duration # after npm publish
36
38
  From this repo:
37
39
 
38
40
  ```bash
39
- pi -e .
41
+ pi --no-extensions -e .
40
42
  # or
41
- pi -e ./extensions/tool-duration/index.ts
43
+ pi --no-extensions -e ./extensions/tool-duration/index.ts
42
44
  ```
43
45
 
46
+ `--no-extensions` prevents a duplicate flag conflict when another copy is already installed.
47
+
44
48
  ## Configure
45
49
 
46
50
  Default threshold: `1000` ms.
47
51
 
48
52
  ```bash
49
- PI_TOOL_DURATION_THRESHOLD_MS=0 pi -e . # annotate every tool result
50
- pi -e . --tool-duration-threshold-ms 500 # annotate tools taking >= 500ms
53
+ PI_TOOL_DURATION_THRESHOLD_MS=0 pi --no-extensions -e . # annotate every tool result
54
+ pi --no-extensions -e . --tool-duration-threshold-ms 500 # annotate tools taking >= 500ms
51
55
  ```
52
56
 
53
- Invalid threshold values fall back to the default.
57
+ Invalid values are ignored. An invalid CLI value falls through to the environment value; an invalid environment value falls back to the default.
54
58
 
55
59
  ## Verify
56
60
 
@@ -67,7 +71,7 @@ hi
67
71
  [duration: 5.0s]
68
72
  ```
69
73
 
70
- A fast successful command below the threshold stays unchanged. A non-zero exit code is always annotated, even below the threshold.
74
+ A fast successful tool below the threshold stays unchanged. A failed tool result delivered to the model is always annotated, even below the threshold.
71
75
 
72
76
  ## License
73
77
 
@@ -1,14 +1,13 @@
1
1
  /**
2
2
  * pi-tool-duration
3
3
  *
4
- * Appends `[duration: Xs]` to slow tool results so the model sees how long a
5
- * call actually took. pi already measures this for the TUI ("Took Xs") but the
6
- * model does not see that timing.
4
+ * Appends `[duration: Xs]` to slow or failed tool messages so the model sees
5
+ * how long a call actually took. pi already measures this for the TUI
6
+ * ("Took Xs") but the model does not see that timing.
7
7
  */
8
8
  import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
9
9
 
10
10
  const DEFAULT_THRESHOLD_MS = 1000;
11
- const DURATION_RE = /^\[duration: \d+(?:\.\d+)?s\]$/;
12
11
 
13
12
  function parseThreshold(value: unknown): number | undefined {
14
13
  if (typeof value !== "string" && typeof value !== "number") return undefined;
@@ -25,56 +24,50 @@ function thresholdMs(pi: ExtensionAPI): number {
25
24
  );
26
25
  }
27
26
 
28
- function alreadyAnnotated(content: Array<{ type: string; text?: string }>): boolean {
29
- const last = content.at(-1);
30
- return last?.type === "text" && typeof last.text === "string" && DURATION_RE.test(last.text);
31
- }
32
-
33
- function nonZeroExitCode(event: { content: Array<{ type: string; text?: string }>; details?: unknown }): boolean {
34
- const details = event.details as Record<string, unknown> | null;
35
- const code = details && Number(details.exitCode ?? details.code ?? details.status);
36
- if (code !== null && Number.isFinite(code)) return code !== 0;
37
-
38
- return event.content.some(
39
- (item) =>
40
- item.type === "text" &&
41
- typeof item.text === "string" &&
42
- /(?:exited with code|exit code)\s+(-?[1-9]\d*)\b/i.test(item.text),
43
- );
44
- }
45
-
46
27
  export default function (pi: ExtensionAPI) {
47
28
  const starts = new Map<string, number>();
29
+ const durations = new Map<string, string>();
48
30
 
49
31
  pi.registerFlag("tool-duration-threshold-ms", {
50
- description: "Minimum tool duration in milliseconds before appending [duration: Xs] to the model-visible result",
32
+ // Pi's help formatter adds no separator once this long flag exceeds its 30-column width.
33
+ description: " Minimum elapsed milliseconds before appending a model-visible duration",
51
34
  type: "string",
52
35
  });
53
36
 
54
- pi.on("tool_execution_start", async (event) => {
37
+ pi.on("tool_execution_start", (event) => {
55
38
  starts.set(event.toolCallId, performance.now());
56
39
  });
57
40
 
58
- pi.on("tool_result", async (event) => {
41
+ pi.on("tool_execution_end", (event) => {
59
42
  const startedAt = starts.get(event.toolCallId);
60
43
  starts.delete(event.toolCallId);
61
44
  if (startedAt === undefined) return;
62
45
 
63
46
  const ms = performance.now() - startedAt;
64
- if (!nonZeroExitCode(event) && ms < thresholdMs(pi)) return;
65
- if (alreadyAnnotated(event.content)) return;
47
+ if (!event.isError && ms < thresholdMs(pi)) return;
48
+ durations.set(event.toolCallId, `[duration: ${(ms / 1000).toFixed(1)}s]`);
49
+ });
50
+
51
+ pi.on("message_end", (event) => {
52
+ if (event.message.role !== "toolResult") return;
53
+ const duration = durations.get(event.message.toolCallId);
54
+ durations.delete(event.message.toolCallId);
55
+ if (!duration) return;
66
56
 
67
57
  return {
68
- content: [
69
- ...event.content,
70
- { type: "text" as const, text: `[duration: ${(ms / 1000).toFixed(1)}s]` },
71
- ],
58
+ message: {
59
+ ...event.message,
60
+ content: [...event.message.content, { type: "text" as const, text: duration }],
61
+ },
72
62
  };
73
63
  });
74
64
 
75
- const clearStarts = () => starts.clear();
76
- pi.on("session_start", clearStarts);
77
- pi.on("session_shutdown", clearStarts);
78
- pi.on("agent_end", clearStarts);
79
- pi.on("agent_settled", clearStarts);
65
+ const clearTimings = () => {
66
+ starts.clear();
67
+ durations.clear();
68
+ };
69
+ pi.on("session_start", clearTimings);
70
+ pi.on("session_shutdown", clearTimings);
71
+ pi.on("agent_end", clearTimings);
72
+ pi.on("agent_settled", clearTimings);
80
73
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-tool-duration",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
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)",
@@ -41,10 +41,11 @@
41
41
  },
42
42
  "devDependencies": {
43
43
  "@earendil-works/pi-coding-agent": "0.84.0",
44
+ "typebox": "1.3.7",
44
45
  "typescript": "^5.9.3"
45
46
  },
46
47
  "peerDependencies": {
47
- "@earendil-works/pi-coding-agent": "*"
48
+ "@earendil-works/pi-coding-agent": ">=0.84.0"
48
49
  },
49
50
  "peerDependenciesMeta": {
50
51
  "@earendil-works/pi-coding-agent": {