pi-async-jobs 0.1.1 → 0.1.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.
Files changed (3) hide show
  1. package/README.md +13 -1
  2. package/index.ts +53 -5
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -44,10 +44,22 @@ watchdog 默认 60s,只警告不杀。
44
44
  | 工具 | 作用 |
45
45
  | --- | --- |
46
46
  | `job_start` | `{ command, title?, cwd?, timeout? }`,立即返回 job id 和日志路径 |
47
- | `job_list` | `{ include_finished? }`,状态 / 耗时 / 退出码 / 命令 |
47
+ | `job_list` | `{ include_finished? }`,状态 / 耗时 / 退出码 / 标题 |
48
48
  | `job_output` | `{ id, lines?, pattern? }`,读日志尾部,可用正则过滤 |
49
49
  | `job_kill` | `{ id }`,SIGTERM 进程组,5s 后 SIGKILL |
50
50
 
51
+ ### 任务标题
52
+
53
+ `title` 是给人看的标签,不是命令。扩展会强制约束:
54
+
55
+ - 单行、≤48 字符,超长按词边界截断加省略号
56
+ - 模型漏传、或把命令原文/命令开头塞进 `title` 时,自动改写成从命令派生的短标签
57
+ (跳过 `cd x &&`、`FOO=1`、`sudo` 前缀,取前 3 个词,去掉路径)
58
+ - `promptGuidelines` 里要求模型给 3–6 个词的具体名字,禁止 "job"/"command" 这类空标签
59
+
60
+ 效果:通知头变成 `[async-job] j1 “install deps” ✅ finished in 42.1s`,`job_list` 第一列是标题,
61
+ 命令只在后面附带,方便扫。
62
+
51
63
  任务跑完、失败、超时都会推一条消息(`customType: "async-job"`):
52
64
 
53
65
  ```text
package/index.ts CHANGED
@@ -142,6 +142,47 @@ function fmtDuration(ms: number): string {
142
142
  return `${m}m${Math.round(s - m * 60)}s`;
143
143
  }
144
144
 
145
+ const TITLE_MAX = 48;
146
+
147
+ /** Cut on a word boundary so labels never end mid-word. */
148
+ function cutWord(text: string, max: number): string {
149
+ if (text.length <= max) return text;
150
+ const slice = text.slice(0, max);
151
+ const space = slice.lastIndexOf(" ");
152
+ return `${(space > max * 0.6 ? slice.slice(0, space) : slice).trimEnd()}…`;
153
+ }
154
+
155
+ /** Short label derived from a command: skip `cd`/env prefixes, first 3 words, no paths. */
156
+ function deriveTitle(command: string): string {
157
+ let rest = command.trim();
158
+ for (let i = 0; i < 6; i++) {
159
+ const next = rest
160
+ .replace(/^(?:cd|pushd)\s+\S+\s*(?:&&\s*)?/, "")
161
+ .replace(/^(?:env|sudo|nohup|time|command)\s+/, "")
162
+ .replace(/^[A-Za-z_][A-Za-z0-9_]*=\S*\s+/, "");
163
+ if (next === rest) break;
164
+ rest = next;
165
+ }
166
+ const first = rest.split(/&&|\|\||[;|\n]/)[0].trim();
167
+ const tokens = first
168
+ .split(/\s+/)
169
+ .filter(Boolean)
170
+ .slice(0, 3)
171
+ .map((token) => token.split("/").filter(Boolean).pop() ?? token);
172
+ return tokens.join(" ") || "shell command";
173
+ }
174
+
175
+ /**
176
+ * Job titles are labels, not commands: single line, short, and never a copy of the
177
+ * command itself (that is what the notification's `$ ...` line is for).
178
+ */
179
+ function jobTitle(raw: string | undefined, command: string): string {
180
+ const cmd = truncate(command, 400);
181
+ let title = truncate(raw ?? "", TITLE_MAX).replace(/^[$>#]\s*/, "");
182
+ if (!title || cmd.startsWith(title) || title.startsWith(cmd.slice(0, 24))) title = deriveTitle(cmd);
183
+ return cutWord(title, TITLE_MAX);
184
+ }
185
+
145
186
  function killTree(child: ChildProcess, signal: NodeJS.Signals): void {
146
187
  const pid = child.pid;
147
188
  if (!pid) return;
@@ -260,7 +301,7 @@ export default function piAsyncJobs(pi: ExtensionAPI) {
260
301
  const elapsed = fmtDuration(job.endedAt - job.startedAt);
261
302
  const icon =
262
303
  job.status === "done" ? "✅ finished" : job.status === "timeout" ? "⏱ timed out" : job.status === "killed" ? "⛔ killed" : "❌ failed";
263
- const head = `[async-job] ${job.id} ${icon} in ${elapsed}${exitCode !== undefined ? ` · exit ${exitCode}` : ""}`;
304
+ const head = `[async-job] ${job.id} “${job.title}” ${icon} in ${elapsed}${exitCode !== undefined ? ` · exit ${exitCode}` : ""}`;
264
305
  const lines = [`${head}`, `$ ${truncate(job.command, 200)}`];
265
306
 
266
307
  if (job.status !== "done") {
@@ -291,7 +332,7 @@ export default function piAsyncJobs(pi: ExtensionAPI) {
291
332
  id,
292
333
  command,
293
334
  cwd,
294
- title: opts.title,
335
+ title: jobTitle(opts.title, command),
295
336
  startedAt: Date.now(),
296
337
  status: "running",
297
338
  pid: child.pid,
@@ -370,7 +411,7 @@ export default function piAsyncJobs(pi: ExtensionAPI) {
370
411
  : job.signal
371
412
  ? `sig ${job.signal}`
372
413
  : "-";
373
- return `${job.id.padEnd(4)} ${job.status.padEnd(8)} ${elapsed.padStart(7)} ${exit.padEnd(12)} ${truncate(job.title ? `${job.title}: ${job.command}` : job.command, 80)}`;
414
+ return `${job.id.padEnd(4)} ${job.status.padEnd(8)} ${elapsed.padStart(7)} ${exit.padEnd(12)} ${job.title}${job.title === job.command ? "" : ` · ${truncate(job.command, 60)}`}`;
374
415
  });
375
416
  return [rows.join("\n"), "", `Logs: ${JOBS_DIR}`].join("\n");
376
417
  }
@@ -385,11 +426,17 @@ export default function piAsyncJobs(pi: ExtensionAPI) {
385
426
  promptSnippet: "Run a long command in the background and get a job id immediately",
386
427
  promptGuidelines: [
387
428
  "Long-running commands (builds, installs, full test suites, servers, watchers) must use job_start; foreground bash is capped at 30 seconds by default.",
429
+ 'Give every job a specific title (3-6 words naming the task, e.g. "install deps", "build webpack", "pytest full suite"). Do not paste the command into the title, and do not use vague labels like "job" or "command".',
388
430
  "After job_start, do not poll in a loop. Wait for the completion message, or call job_list/job_output when you have other work done.",
389
431
  ],
390
432
  parameters: Type.Object({
391
433
  command: Type.String({ description: "Shell command to run" }),
392
- title: Type.Optional(Type.String({ description: "Short label for job_list" })),
434
+ title: Type.Optional(
435
+ Type.String({
436
+ description:
437
+ 'Short human-readable label, 3-6 words, in the user\'s language: e.g. "install deps", "build webpack", "pytest full suite". Never restate the command and never pass a bare command string; if omitted, a label is derived from the command.',
438
+ }),
439
+ ),
393
440
  cwd: Type.Optional(Type.String({ description: "Working directory (defaults to the session cwd)" })),
394
441
  timeout: Type.Optional(Type.Number({ description: "Kill the job after this many seconds (0 or omitted = no limit)" })),
395
442
  }),
@@ -409,7 +456,8 @@ export default function piAsyncJobs(pi: ExtensionAPI) {
409
456
  {
410
457
  type: "text",
411
458
  text: [
412
- `Started ${job.id}${job.pid ? ` (pid ${job.pid})` : ""} in ${job.cwd}`,
459
+ `Started ${job.id} “${job.title}”${job.pid ? ` (pid ${job.pid})` : ""} in ${job.cwd}`,
460
+ `Title shows up in job_list and in the completion message; pass a better one with title: "..." if this label is wrong.`,
413
461
  `$ ${truncate(command, 200)}`,
414
462
  `Log: ${job.logPath}`,
415
463
  timeout && timeout > 0 ? `Killed automatically after ${timeout}s.` : "",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-async-jobs",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
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",