pi-ptc-meter 1.0.25 → 1.0.27

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.
Files changed (3) hide show
  1. package/README.md +54 -32
  2. package/index.ts +7 -3
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -56,7 +56,7 @@ pi install npm:pi-ptc-meter
56
56
 
57
57
  ## 作用(源码事实)
58
58
 
59
- - `tool_call`:计数 +1(不拦截、不改调用)。
59
+ - `tool_call`:顶层调用计数 +1(codemode 内层调用不另计;不拦截、不改调用)。
60
60
  - `turn_end`:记录本轮次数与耗时(不落盘)。
61
61
  - `agent_end`:每 run 落盘一行日志。
62
62
  - `before_agent_start`:结算上一轮并以 `nextTurn` 尾部消息回灌——计数、耗时、偏差、基线中位、**缓存命中率**(仅当该轮有 usage 数据时出现;< 90% 标「低」)——(`display: false`)。
@@ -72,27 +72,38 @@ PTC 计量:上一轮 9 次工具调用 · 实际 34s · 命中 92% · 预计 8
72
72
 
73
73
  ## 原理(专业)
74
74
 
75
- ```mermaid
76
- flowchart TD
77
- A[本轮开工前<br/>声明 预计 N 次 / 预计 Ns] --> B[tool_call<br/>计数 +1]
78
- B --> C[turn_end<br/>记录实际次数与耗时 + 解析预计值]
79
- C --> D[agent_end<br/>落盘一行日志(含预计)]
80
- D --> E[before_agent_start<br/>结算上一轮]
81
- E --> F[对比:实际 vs 预计 → 偏差 %]
82
- F -->|nextTurn 尾部消息| A
83
- F -.->|system 只留静态骨架| S[ptc_meter section]
75
+ ```text
76
+ 本轮开工前 ─ 声明「预计 N 次 / 预计 Ns」
77
+ ↓
78
+ tool_call ─ 仅顶层调用计数 +1
79
+ ↓
80
+ turn_end ─ 记录实际次数与耗时 + 解析预计值
81
+ ↓
82
+ agent_end ─ 落盘一行日志(含预计)
83
+ ↓
84
+ before_agent_start ─ 结算上一轮
85
+ ↓
86
+ 对比:实际 vs 预计 → 偏差 %
87
+ ↓ nextTurn 尾部消息
88
+ (回到本轮开工前)
89
+
90
+ system 只留静态骨架(ptc_meter section)
84
91
  ```
85
92
 
86
93
  ## 原理(小白)
87
94
 
88
95
  **像记账对账**:开工前先说「这趟大概 5 步」,收工后对账「实际 7 步,比预计多 40%」,下次开工前把账报给你。
89
96
 
90
- ```mermaid
91
- flowchart TD
92
- A[开工前先估:预计几步] --> B[干活:实际跑几步]
93
- B --> C[收工对比:实际 vs 预计]
94
- C --> D[下次开工时报出偏差]
95
- D --> A
97
+ ```text
98
+ 开工前先估:预计几步
99
+ ↓
100
+ 干活:实际跑几步
101
+ ↓
102
+ 收工对比:实际 vs 预计
103
+ ↓
104
+ 下次开工时报出偏差
105
+ ↓
106
+ (回到开工前先估)
96
107
  ```
97
108
 
98
109
  ## License
@@ -152,7 +163,7 @@ Also: log `~/.pi/ptc-meter.jsonl` (1 MiB truncation, tail-preserving); no enviro
152
163
 
153
164
  ## What it does (source facts)
154
165
 
155
- - `tool_call`: increments a counter (never intercepts or rewrites).
166
+ - `tool_call`: increments the counter for top-level calls only (nested codemode calls are not counted separately; never intercepts or rewrites).
156
167
  - `turn_end`: records calls and duration (no disk write).
157
168
  - `agent_end`: writes one log line per run.
158
169
  - `before_agent_start`: settles the previous turn and feeds back counts, duration, deviation, the baseline median and the **cache hit rate** (only when that turn reported usage; flagged low below 90%) as a `nextTurn` trailing message (`display: false`).
@@ -168,27 +179,38 @@ PTC metering: last turn 9 tool calls · 34s actual · 92% cache hit · estimated
168
179
 
169
180
  ## How it works (technical)
170
181
 
171
- ```mermaid
172
- flowchart TD
173
- A[before the turn<br/>declare: about N calls / Ns] --> B[tool_call<br/>counter +1]
174
- B --> C[turn_end<br/>record actual calls and duration + parse estimate]
175
- C --> D[agent_end<br/>write one log line (includes estimate)]
176
- D --> E[before_agent_start<br/>settle the previous turn]
177
- E --> F[compare: actual vs estimate -> deviation %]
178
- F -->|nextTurn message| A
179
- F -.->|system keeps a static skeleton| S[ptc_meter section]
182
+ ```text
183
+ before the turn ─ declare: about N calls / Ns
184
+ ↓
185
+ tool_call ─ top-level call: counter +1
186
+ ↓
187
+ turn_end ─ record actual calls and duration + parse estimate
188
+ ↓
189
+ agent_end ─ write one log line (includes estimate)
190
+ ↓
191
+ before_agent_start ─ settle the previous turn
192
+ ↓
193
+ compare: actual vs estimate -> deviation %
194
+ ↓ nextTurn message
195
+ (back to before the turn)
196
+
197
+ the system prompt keeps only a static skeleton (ptc_meter section)
180
198
  ```
181
199
 
182
200
  ## How it works (plain words)
183
201
 
184
202
  **Like a ledger**: you estimate "about 5 steps" before starting, the ledger compares it with the actual 7 (40% over), and reports the gap before the next job.
185
203
 
186
- ```mermaid
187
- flowchart TD
188
- A[before starting: guess N steps] --> B[work: actually N steps]
189
- B --> C[at the end: actual vs guess]
190
- C --> D[report the gap next time]
191
- D --> A
204
+ ```text
205
+ before starting: guess N steps
206
+ ↓
207
+ work: actually N steps
208
+ ↓
209
+ at the end: actual vs guess
210
+ ↓
211
+ report the gap next time
212
+ ↓
213
+ (back to before starting)
192
214
  ```
193
215
  ## License
194
216
 
package/index.ts CHANGED
@@ -268,9 +268,13 @@ export default function ptcMeter(pi: ExtensionAPI) {
268
268
  turns = 0;
269
269
  turnStartedAt = Date.now();
270
270
  });
271
- // 计数:不读 event 字段、不返回任何值 —— 这是最安全的形态(该层无 try/catch)。
272
- pi.on("tool_call", async () => {
273
- try { callsThisTurn++; } catch { /* 静默:计量不该影响调用 */ }
271
+ // 计数:只计**顶层**调用 —— codemode 的内层 tools.X 也走本 hook(pi 的 _executeNestedToolCall
272
+ // 显式带 parentToolCallId),计进去会把一次往返记成 N 次(实测 21 次顶层被记成 53)。
273
+ pi.on("tool_call", async (event) => {
274
+ try {
275
+ if ((event as { parentToolCallId?: string } | undefined)?.parentToolCallId) return undefined;
276
+ callsThisTurn++;
277
+ } catch { /* 静默:计量不该影响调用 */ }
274
278
  return undefined;
275
279
  });
276
280
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-ptc-meter",
3
- "version": "1.0.25",
3
+ "version": "1.0.27",
4
4
  "description": "Measures tool-call batching per turn and feeds the numbers back to the model.",
5
5
  "type": "module",
6
6
  "license": "MIT",