@deathbeam/pi-delegate 0.0.37 → 0.0.39

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/child.ts CHANGED
@@ -5,12 +5,6 @@ import { limitOutput, resultPreview, toolCallDetail } from "./format.ts";
5
5
 
6
6
  type Child = ChildProcessWithoutNullStreams;
7
7
 
8
- function usageTokens(usage: any): number | undefined {
9
- if (!usage) return undefined;
10
- const tokens = usage.totalTokens || usage.input + usage.output + usage.cacheRead + usage.cacheWrite;
11
- return tokens > 0 ? tokens : undefined;
12
- }
13
-
14
8
  export type ChildActivity = {
15
9
  kind: "tool" | "result" | "assistant" | "user" | "status";
16
10
  text: string;
@@ -30,6 +24,12 @@ type ChildRun = {
30
24
  swapModel: (provider: string, modelId: string, level?: string, onModelSet?: () => void) => Promise<void>;
31
25
  };
32
26
 
27
+ function usageTokens(usage: any): number | undefined {
28
+ if (!usage) return undefined;
29
+ const tokens = usage.totalTokens || usage.input + usage.output + usage.cacheRead + usage.cacheWrite;
30
+ return tokens > 0 ? tokens : undefined;
31
+ }
32
+
33
33
  /** Run one RPC child, keeping its writer serialized so backpressure and EPIPE reach the caller. */
34
34
  export function runChild(
35
35
  child: Child,
@@ -157,14 +157,7 @@ export function runChild(
157
157
  case "message_end": {
158
158
  const role = event.message?.role;
159
159
  if (role !== "assistant" && role !== "user") return;
160
- const content = event.message.content;
161
- const text =
162
- typeof content === "string"
163
- ? content
164
- : (content
165
- ?.filter((part: any) => part.type === "text")
166
- .map((part: any) => part.text ?? "")
167
- .join("") ?? "");
160
+ const text = transcriptText(event.message.content).join("");
168
161
  if (role === "assistant") {
169
162
  childError = event.message.errorMessage;
170
163
  if (text) liveText = text;
@@ -304,7 +297,7 @@ export function runChild(
304
297
  };
305
298
  }
306
299
 
307
- /** Text content of a persisted transcript message, in part order. */
300
+ /** Text parts of a message's content, in order. */
308
301
  function transcriptText(content: unknown): string[] {
309
302
  if (typeof content === "string") return [content];
310
303
  if (!Array.isArray(content)) return [];
@@ -331,7 +324,7 @@ export function priorActivity(sessionDir: string, id: string): ChildActivity[] {
331
324
  return [];
332
325
  }
333
326
  if (!sessionFile) return [];
334
- let lines: string[] = [];
327
+ let lines: string[];
335
328
  try {
336
329
  lines = readFileSync(join(sessionDir, sessionFile), "utf8").split("\n");
337
330
  } catch {
package/format.ts CHANGED
@@ -32,6 +32,20 @@ export type DelegateReport = {
32
32
  cloneClosed?: boolean;
33
33
  };
34
34
 
35
+ /** The isolation state to show for a job: pending means its clone still holds unapplied work. */
36
+ export type IsolationMark = "isolated" | "pending";
37
+
38
+ type IsolationStateFields = {
39
+ isolation?: unknown;
40
+ cloneClosed?: boolean;
41
+ status?: string;
42
+ };
43
+
44
+ export type DelegateDetails = Pick<
45
+ DelegateJob,
46
+ "id" | "agent" | "description" | "task" | "model" | "tools" | "status" | "isolation" | "cloneClosed"
47
+ > & { background: boolean };
48
+
35
49
  const SPINNER_FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
36
50
  export const SPINNER_INTERVAL_MS = 100;
37
51
  const STATUS_ICONS = {
@@ -64,15 +78,6 @@ export function statusText(theme: Theme, status: JobStatus, now = Date.now()): s
64
78
  return theme.fg(STATUS_COLORS[status], statusIcon(status, now));
65
79
  }
66
80
 
67
- /** The isolation state to show for a job: pending means its clone still holds unapplied work. */
68
- export type IsolationMark = "isolated" | "pending";
69
-
70
- type IsolationStateFields = {
71
- isolation?: unknown;
72
- cloneClosed?: boolean;
73
- status?: string;
74
- };
75
-
76
81
  /** Derives the mark from a job: an isolation with no clone left, or a still-running one, is not pending. */
77
82
  export function isolationMarkOf(job: IsolationStateFields): IsolationMark | undefined {
78
83
  if (!job.isolation) return undefined;
@@ -276,7 +281,7 @@ export function reportText(report: DelegateReport): string {
276
281
  // The apply hint is for the model only: the transcript renders `output`, so users never see it.
277
282
  const hint =
278
283
  report.isolation && !report.cloneClosed
279
- ? `\n\nCall delegate_apply with job ${report.id} to land its changes, or delegate_discard to drop them.`
284
+ ? `\n\nCall delegate_apply with job ${report.id} to land its changes, or delegate_discard to drop them. Call one, not both: a successful apply removes the clone.`
280
285
  : "";
281
286
  return `${head} finished.${output ? `\n\n${output}` : ""}${hint}`;
282
287
  }
@@ -291,11 +296,6 @@ export function widgetJobs<T>(jobs: T[]): { shown: T[]; hidden: number } {
291
296
  return { shown, hidden: jobs.length - shown.length };
292
297
  }
293
298
 
294
- export type DelegateDetails = Pick<
295
- DelegateJob,
296
- "id" | "agent" | "description" | "task" | "model" | "tools" | "status" | "isolation" | "cloneClosed"
297
- > & { background: boolean };
298
-
299
299
  export function resultFallback(body: string, theme: Theme, isError: boolean): Text {
300
300
  const mark = statusText(theme, isError ? "failed" : "done");
301
301
  return new Text(`\n${mark} ${theme.fg(isError ? "error" : "toolOutput", body || "(no output)")}`, 0, 0);
package/index.ts CHANGED
@@ -96,6 +96,11 @@ function withIsolation(output: string, isolation: IsolationState | undefined): s
96
96
  return isolation ? [output, isolationSummary(isolation)].filter(Boolean).join("\n\n") : output;
97
97
  }
98
98
 
99
+ /** Status icon, identity, and the muted detail line: the row the widget and delegate_list both show. */
100
+ function jobRow(theme: Theme, job: InspectJob, now: number): string {
101
+ return `${statusText(theme, job.status, now)} ${jobIdentity(theme, job)} ${theme.fg("muted", jobLine(job, elapsedMs(job, now)))}`;
102
+ }
103
+
99
104
  export default function (pi: ExtensionAPI) {
100
105
  const store = createJobStore(pi.appendEntry);
101
106
  let ticker: ReturnType<typeof setInterval> | undefined;
@@ -112,7 +117,7 @@ export default function (pi: ExtensionAPI) {
112
117
  /** The clone is the unit: every job id that ran in it is only a handle, so state changes reach all of them. */
113
118
  const jobsInClone = (clone: string) => store.all().filter((job) => job.isolation?.clone === clone);
114
119
 
115
- /** A job that still has a clone; any job id that ran in a clone is a handle on it. */
120
+ /** A job that still has a clone; throws explaining why it has none. */
116
121
  const clonedJob = (id: string) => {
117
122
  const job = store.findOrThrow(id);
118
123
  const isolation = job.isolation;
@@ -153,14 +158,10 @@ export default function (pi: ExtensionAPI) {
153
158
  const theme = ctx.ui.theme;
154
159
  const now = Date.now();
155
160
  const { shown, hidden } = widgetJobs(jobs);
156
- const lines: string[] = [];
157
- const all = store.all();
158
- const counts = tally(jobs.length, all, now);
159
- lines.push(`${statusText(theme, "running", now)} ${theme.fg("muted", counts)}`);
161
+ const counts = tally(jobs.length, store.all(), now);
162
+ const lines = [`${statusText(theme, "running", now)} ${theme.fg("muted", counts)}`];
160
163
  for (const job of shown) {
161
- lines.push(
162
- `${statusText(theme, "running", now)} ${jobIdentity(theme, job)} ${theme.fg("muted", jobLine(job, elapsedMs(job, now)))}`,
163
- );
164
+ lines.push(jobRow(theme, job, now));
164
165
  const { tool, result } = lastToolCall(job);
165
166
  if (tool)
166
167
  lines.push(
@@ -181,7 +182,6 @@ export default function (pi: ExtensionAPI) {
181
182
  });
182
183
  };
183
184
 
184
- // The ticker animates spinners exactly while live jobs exist.
185
185
  const syncTicker = (ctx: ExtensionContext) => {
186
186
  if (store.live().length > 0) ticker ??= setInterval(() => refreshWidget(ctx), SPINNER_INTERVAL_MS);
187
187
  else if (ticker) {
@@ -427,8 +427,7 @@ export default function (pi: ExtensionAPI) {
427
427
 
428
428
  registerInspector(pi, store);
429
429
 
430
- const cwd = process.cwd();
431
- const agentList = agentsFor(cwd).map((agent) => {
430
+ const agentList = agentsFor(process.cwd()).map((agent) => {
432
431
  const qualifiers = [agent.model, agent.isolated ? "isolated" : undefined].filter(Boolean).join(", ");
433
432
  return `- ${agent.name}${qualifiers ? ` (${qualifiers})` : ""}: ${agent.description}`;
434
433
  });
@@ -671,7 +670,7 @@ export default function (pi: ExtensionAPI) {
671
670
  name: "delegate_apply",
672
671
  label: "Apply delegate",
673
672
  description:
674
- "Apply an isolated delegate's changes to the main working tree by job id. The delegate's commits and uncommitted changes are 3-way merged onto a fresh snapshot of the main tree, so nearby but non-overlapping edits apply cleanly; only a true conflict fails. On conflict the clone is kept and the error explains the recovery. No delegate may be running in its clone.",
673
+ "Apply an isolated delegate's changes to the main working tree by job id. The delegate's commits and uncommitted changes are 3-way merged onto a fresh snapshot of the main tree, so nearby but non-overlapping edits apply cleanly; only a true conflict fails. A successful apply removes the clone itself, so never call delegate_discard for the same clone, in the same turn or after. On conflict the clone is kept and the error explains the recovery. No delegate may be running in its clone.",
675
674
  promptSnippet: "Apply an isolated delegate's clone to the main tree by job id",
676
675
  parameters: Type.Object({
677
676
  id: Type.String({ description: "Job id of an isolated delegate; any job id that ran in the clone works." }),
@@ -733,7 +732,8 @@ export default function (pi: ExtensionAPI) {
733
732
  pi.registerTool({
734
733
  name: "delegate_discard",
735
734
  label: "Discard delegate clone",
736
- description: "Remove an isolated delegate's clone without applying its changes.",
735
+ description:
736
+ "Remove an isolated delegate's clone without applying its changes. Use it instead of delegate_apply, never alongside it: a successful apply already removes the clone, and a discard in the same turn deletes the clone that apply needs.",
737
737
  promptSnippet: "Discard an isolated delegate's clone by job id",
738
738
  parameters: Type.Object({
739
739
  id: Type.String({ description: "Job id of an isolated delegate; any job id that ran in the clone works." }),
@@ -797,13 +797,9 @@ export default function (pi: ExtensionAPI) {
797
797
  const jobs = (result.details as { jobs?: InspectJob[] } | undefined)?.jobs;
798
798
  if (!jobs?.length || context.isError) return resultFallback(resultText(result), theme, context.isError);
799
799
  const now = Date.now();
800
+ const rows = jobs.map((job) => jobRow(theme, job, now)).join("\n");
800
801
  return new Text(
801
- `\n${statusText(theme, "done")} ${theme.fg("muted", `${jobs.length} delegates`)}\n${jobs
802
- .map(
803
- (job) =>
804
- `${statusText(theme, job.status, now)} ${jobIdentity(theme, job)} ${theme.fg("muted", jobLine(job, elapsedMs(job, now)))}`,
805
- )
806
- .join("\n")}`,
802
+ `\n${statusText(theme, "done")} ${theme.fg("muted", `${jobs.length} delegates`)}\n${rows}`,
807
803
  0,
808
804
  0,
809
805
  );
package/inspector.ts CHANGED
@@ -154,7 +154,6 @@ export function registerInspector(pi: ExtensionAPI, store: JobStore) {
154
154
  return [
155
155
  title,
156
156
  theme.fg("muted", "No delegates in this session."),
157
- // Fixed rows: title, top rule, bottom rule, footer.
158
157
  ...Array(Math.max(0, tui.terminal.rows - 4)).fill(""),
159
158
  footer,
160
159
  ].slice(0, tui.terminal.rows);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deathbeam/pi-delegate",
3
- "version": "0.0.37",
3
+ "version": "0.0.39",
4
4
  "description": "Minimal extension for delegating tasks to subagents.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/store.ts CHANGED
@@ -21,13 +21,6 @@ export type InspectJob = {
21
21
  cloneClosed?: boolean;
22
22
  };
23
23
 
24
- /** The job's latest tool call and its result, once the result arrives. */
25
- export function lastToolCall(job: InspectJob): { tool?: ChildActivity; result?: ChildActivity } {
26
- const at = job.activity.findLastIndex((entry) => entry.kind === "tool");
27
- if (at < 0) return {};
28
- return { tool: job.activity[at], result: job.activity.slice(at + 1).find((entry) => entry.kind === "result") };
29
- }
30
-
31
24
  export type DelegateJob = InspectJob & {
32
25
  controller: AbortController;
33
26
  steer?: (message: string) => Promise<void>;
@@ -164,3 +157,10 @@ export function createJobStore(append: (customType: string, data: InspectJob) =>
164
157
  }
165
158
 
166
159
  export type JobStore = ReturnType<typeof createJobStore>;
160
+
161
+ /** The job's latest tool call and its result, once the result arrives. */
162
+ export function lastToolCall(job: InspectJob): { tool?: ChildActivity; result?: ChildActivity } {
163
+ const at = job.activity.findLastIndex((entry) => entry.kind === "tool");
164
+ if (at < 0) return {};
165
+ return { tool: job.activity[at], result: job.activity.slice(at + 1).find((entry) => entry.kind === "result") };
166
+ }