pi-ptc-meter 1.0.26 → 1.0.28

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 +6 -6
  2. package/index.ts +21 -4
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -56,9 +56,9 @@ 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
- - `agent_end`:每 run 落盘一行日志。
61
+ - `agent_end`:每 run 落盘一行日志;goal 自动续轮共享同一 run 时,`turn_end` 超阈值即提示一次(每 run 一次),避免回路整段失效。
62
62
  - `before_agent_start`:结算上一轮并以 `nextTurn` 尾部消息回灌——计数、耗时、偏差、基线中位、**缓存命中率**(仅当该轮有 usage 数据时出现;< 90% 标「低」)——(`display: false`)。
63
63
 
64
64
  **注入样例(示意,字段可能随版本增删)**
@@ -75,7 +75,7 @@ PTC 计量:上一轮 9 次工具调用 · 实际 34s · 命中 92% · 预计 8
75
75
  ```text
76
76
  本轮开工前 ─ 声明「预计 N 次 / 预计 Ns」
77
77
  ↓
78
- tool_call ─ 计数 +1
78
+ tool_call ─ 仅顶层调用计数 +1
79
79
  ↓
80
80
  turn_end ─ 记录实际次数与耗时 + 解析预计值
81
81
  ↓
@@ -163,9 +163,9 @@ Also: log `~/.pi/ptc-meter.jsonl` (1 MiB truncation, tail-preserving); no enviro
163
163
 
164
164
  ## What it does (source facts)
165
165
 
166
- - `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).
167
167
  - `turn_end`: records calls and duration (no disk write).
168
- - `agent_end`: writes one log line per run.
168
+ - `agent_end`: writes one log line per run.; when goal auto-continuation shares one run, `turn_end` warns once above threshold (once per run) so the loop never goes dark.
169
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`).
170
170
 
171
171
  **Sample injection (illustrative; fields may change between versions)**
@@ -182,7 +182,7 @@ PTC metering: last turn 9 tool calls · 34s actual · 92% cache hit · estimated
182
182
  ```text
183
183
  before the turn ─ declare: about N calls / Ns
184
184
  ↓
185
- tool_call ─ counter +1
185
+ tool_call ─ top-level call: counter +1
186
186
  ↓
187
187
  turn_end ─ record actual calls and duration + parse estimate
188
188
  ↓
package/index.ts CHANGED
@@ -66,7 +66,9 @@ function threshold(): number {
66
66
  // 轮内计数(反馈回路:随注入回到上下文,见 before_agent_start)
67
67
  let callsThisTurn = 0;
68
68
  let lastCompletedCalls = -1; // 已完成轮的调用数(agent_end 落盘后置位;before_agent_start 缺席时仍可回灌)
69
- let injectedThisTurn = false; // 本轮是否已由 before_agent_start 注入(缺席时由 agent_end 补发)
69
+ let injectedThisTurn = false; // 本轮是否已由 before_agent_start 注入(缺席时由 agent_end 补发)
70
+ // goal 长跑:agent_end 可能整 run 不触发(续轮不发 user message)→ 超阈值即时补发一次,避免回路整段失效
71
+ let warnedThisRun = false; // 安全阀是否已在本 run 内提示过
70
72
  let lastTurnCalls = -1;
71
73
  let lastTurnMs = -1;
72
74
  let lastTurnEst: number | null = null;
@@ -268,9 +270,13 @@ export default function ptcMeter(pi: ExtensionAPI) {
268
270
  turns = 0;
269
271
  turnStartedAt = Date.now();
270
272
  });
271
- // 计数:不读 event 字段、不返回任何值 —— 这是最安全的形态(该层无 try/catch)。
272
- pi.on("tool_call", async () => {
273
- try { callsThisTurn++; } catch { /* 静默:计量不该影响调用 */ }
273
+ // 计数:只计**顶层**调用 —— codemode 的内层 tools.X 也走本 hook(pi 的 _executeNestedToolCall
274
+ // 显式带 parentToolCallId),计进去会把一次往返记成 N 次(实测 21 次顶层被记成 53)。
275
+ pi.on("tool_call", async (event) => {
276
+ try {
277
+ if ((event as { parentToolCallId?: string } | undefined)?.parentToolCallId) return undefined;
278
+ callsThisTurn++;
279
+ } catch { /* 静默:计量不该影响调用 */ }
274
280
  return undefined;
275
281
  });
276
282
 
@@ -295,6 +301,16 @@ export default function ptcMeter(pi: ExtensionAPI) {
295
301
  const ec = em ? parseInt(em[1], 10) : 0;
296
302
  if (ec > 0 && lastTurnEstCalls === null) lastTurnEstCalls = ec; // 同上:首个声明
297
303
  } catch { lastTurnEstCalls = null; }
304
+ // 安全阀:goal 自动续轮共享同一 run,agent_end 可能迟迟不触发——超阈值即提示一次
305
+ if (!injectedThisTurn && !warnedThisRun && callsThisTurn >= threshold()) {
306
+ try {
307
+ (pi as unknown as { sendMessage?: (m: unknown, o?: unknown) => void }).sendMessage?.(
308
+ { customType: SECTION_KEY, content: "PTC:本轮已 " + callsThisTurn + " 次(阈值 " + threshold() + ")→ 先合并可并调用,再继续。", display: false },
309
+ { deliverAs: "nextTurn" },
310
+ );
311
+ warnedThisRun = true;
312
+ } catch { /* 静默 */ }
313
+ }
298
314
  } catch { /* 静默 */ }
299
315
  });
300
316
 
@@ -314,6 +330,7 @@ export default function ptcMeter(pi: ExtensionAPI) {
314
330
  lastCompletedCalls = calls;
315
331
  callsThisTurn = 0;
316
332
  turnStartedAt = Date.now();
333
+ warnedThisRun = false;
317
334
  // D16:自动续轮下 before_agent_start 可能缺席——本轮未注入过则补发一次尾部回灌,保证反馈不断链(2026-10-17 实证:本轮无任何回灌行)。
318
335
  if (!injectedThisTurn && loadConf().inject !== false) {
319
336
  lastTurnCalls = calls;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-ptc-meter",
3
- "version": "1.0.26",
3
+ "version": "1.0.28",
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",