pi-async-jobs 0.1.3 → 0.1.4

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 +13 -2
  2. package/index.ts +45 -3
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -37,7 +37,9 @@ pi install npm:pi-async-jobs
37
37
  - 给模型一条 steer 消息,提示这次调用已经太久、以后这类活该走 `job_start`;
38
38
  - `watchdog.hardAbort: true` 时直接 `ctx.abort()` 中断当前回合。
39
39
 
40
- watchdog 默认 60s,只警告不杀。
40
+ watchdog 默认 60s,只警告不杀。**人机交互类工具一律放过**(`ask_user_question`、`contact_supervisor`、
41
+ `subagent*`、`computer_*`、`browser*` 等,见 `watchdog.exempt`):它们的耗时是用户在思考,
42
+ 催模型没用。
41
43
 
42
44
  ## 后台任务
43
45
 
@@ -89,11 +91,20 @@ Log: ~/.pi/agent/async-jobs/j1.log — read it with job_output({ id: "j1" }).
89
91
  ```json
90
92
  {
91
93
  "timeouts": { "bash": 30, "read": 20, "grep": 20, "find": 20, "ls": 10, "write": 0, "edit": 0 },
92
- "watchdog": { "default": 60, "hardAbort": false, "perTool": { "mcp__slow__query": 20 } },
94
+ "watchdog": {
95
+ "default": 60,
96
+ "hardAbort": false,
97
+ "perTool": { "mcp__slow__query": 20 },
98
+ "exempt": ["ask_user_question", "contact_supervisor", "subagent*", "computer_*", "browser*", "window*", "clipboard*", "recording*", "diagnostics*"]
99
+ },
93
100
  "jobs": { "notify": true }
94
101
  }
95
102
  ```
96
103
 
104
+ `watchdog.exempt` 是默认放过的交互式工具(通配符):这类工具的耗时是人造成的,
105
+ 催模型也没用,硬 abort 更糟糕。要监控某个被豁免的工具,在 `perTool` 里显式写一个值即可
106
+ (`perTool` 优先于 `exempt`);写 `0` 则彻底关掉该工具的监控。
107
+
97
108
  环境变量(临时覆盖,优先级最高):
98
109
 
99
110
  ```bash
package/index.ts CHANGED
@@ -43,7 +43,13 @@ type Config = {
43
43
  /** Foreground cap per built-in tool, in seconds. 0 disables the cap. */
44
44
  timeouts: Record<string, number>;
45
45
  /** Watchdog for tools this extension cannot wrap. */
46
- watchdog: { default: number; hardAbort: boolean; perTool: Record<string, number> };
46
+ watchdog: {
47
+ default: number;
48
+ hardAbort: boolean;
49
+ perTool: Record<string, number>;
50
+ /** Tool name patterns that wait on a human and must never be nagged or aborted. */
51
+ exempt: string[];
52
+ };
47
53
  jobs: { notify: boolean };
48
54
  };
49
55
 
@@ -51,7 +57,24 @@ const DEFAULT_CONFIG: Config = {
51
57
  // 30s for shells is the point of the extension. write/edit default to 0:
52
58
  // aborting them mid-write risks a half-written file.
53
59
  timeouts: { bash: 30, powershell: 30, read: 20, grep: 20, find: 20, ls: 10, write: 0, edit: 0 },
54
- watchdog: { default: 60, hardAbort: false, perTool: {} },
60
+ watchdog: {
61
+ default: 60,
62
+ hardAbort: false,
63
+ perTool: {},
64
+ // Human-in-the-loop tools are slow by definition: the user is the one holding
65
+ // the tool call open. Warning on them is noise, and aborting them is worse.
66
+ exempt: [
67
+ "ask_user_question",
68
+ "contact_supervisor",
69
+ "subagent*",
70
+ "computer_*",
71
+ "browser*",
72
+ "window*",
73
+ "clipboard*",
74
+ "recording*",
75
+ "diagnostics*",
76
+ ],
77
+ },
55
78
  jobs: { notify: true },
56
79
  };
57
80
 
@@ -79,6 +102,7 @@ function loadConfig(): Config {
79
102
  ...DEFAULT_CONFIG.watchdog,
80
103
  ...(raw?.watchdog ?? {}),
81
104
  perTool: { ...(raw?.watchdog?.perTool ?? {}) },
105
+ exempt: Array.isArray(raw?.watchdog?.exempt) ? raw.watchdog.exempt : DEFAULT_CONFIG.watchdog.exempt,
82
106
  },
83
107
  jobs: { ...DEFAULT_CONFIG.jobs, ...(raw?.jobs ?? {}) },
84
108
  };
@@ -144,6 +168,18 @@ function fmtDuration(ms: number): string {
144
168
 
145
169
  const TITLE_MAX = 48;
146
170
 
171
+ /** Glob match for tool names: `ask_user_question`, `subagent*`, `mcp__*`. */
172
+ function matchesAny(name: string, patterns: string[]): boolean {
173
+ return patterns.some((pattern) => {
174
+ if (!pattern.includes("*")) return pattern === name;
175
+ const body = pattern
176
+ .split("*")
177
+ .map((part) => part.replace(/[.+?^${}()|[\]\\]/g, "\\$&"))
178
+ .join(".*");
179
+ return new RegExp(`^${body}$`).test(name);
180
+ });
181
+ }
182
+
147
183
  /** Cut on a word boundary so labels never end mid-word. */
148
184
  function cutWord(text: string, max: number): string {
149
185
  if (text.length <= max) return text;
@@ -680,13 +716,19 @@ export default function piAsyncJobs(pi: ExtensionAPI) {
680
716
  pi.on("tool_call", (event, ctx) => {
681
717
  const name = event.toolName;
682
718
  if (wrapped.has(name) || name.startsWith("job_")) return;
719
+ // An explicit perTool entry overrides the exempt list; otherwise interactive
720
+ // tools (waiting for the user) are skipped entirely.
721
+ if (cfg.watchdog.perTool[name] === undefined && matchesAny(name, cfg.watchdog.exempt)) return;
683
722
  const limit = cfg.watchdog.perTool[name] ?? cfg.watchdog.default;
684
723
  if (!limit || limit <= 0) return;
685
724
  const callId = event.toolCallId;
686
725
  clearWatchdog(callId);
687
726
  const timer = setTimeout(() => {
688
727
  watchdogs.delete(callId);
689
- const msg = `${name} has been running for ${limit}s (tool call ${callId}). If it does not return soon, prefer job_start for work like this.`;
728
+ const advice = name.startsWith("mcp__")
729
+ ? "Job tools cannot run MCP calls, so narrow the request or check the server."
730
+ : "If it does not return soon, prefer job_start for work like this.";
731
+ const msg = `${name} has been running for ${limit}s (tool call ${callId}). ${advice}`;
690
732
  try {
691
733
  ctx?.ui?.notify?.(`⚠ ${name} 超过 ${limit}s 仍在运行`, "warning");
692
734
  } catch {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-async-jobs",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Foreground command timeouts plus background jobs for the pi coding agent: kill slow tool calls, then run long commands with job_start and get completion, failure, and timeout notifications.",
5
5
  "type": "module",
6
6
  "license": "MIT",