pi-subagents 0.17.4 → 0.18.0

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/run-status.ts ADDED
@@ -0,0 +1,134 @@
1
+ import * as fs from "node:fs";
2
+ import * as path from "node:path";
3
+ import type { AgentToolResult } from "@mariozechner/pi-agent-core";
4
+ import { formatAsyncRunList, listAsyncRuns } from "./async-status.ts";
5
+ import { ASYNC_DIR, RESULTS_DIR, type Details } from "./types.ts";
6
+ import { findByPrefix, readStatus } from "./utils.ts";
7
+
8
+ export interface RunStatusParams {
9
+ action?: "status";
10
+ id?: string;
11
+ runId?: string;
12
+ dir?: string;
13
+ }
14
+
15
+ function activityText(activityState: unknown, lastActivityAt: unknown): string | undefined {
16
+ if (typeof lastActivityAt !== "number") return undefined;
17
+ const seconds = Math.floor(Math.max(0, Date.now() - lastActivityAt) / 1000);
18
+ return activityState === "needs_attention" ? `no activity for ${seconds}s` : `active ${seconds}s ago`;
19
+ }
20
+
21
+ export function inspectSubagentStatus(params: RunStatusParams): AgentToolResult<Details> {
22
+ if (!params.id && !params.runId && !params.dir) {
23
+ try {
24
+ const runs = listAsyncRuns(ASYNC_DIR, { states: ["queued", "running"] });
25
+ return {
26
+ content: [{ type: "text", text: formatAsyncRunList(runs) }],
27
+ details: { mode: "single", results: [] },
28
+ };
29
+ } catch (error) {
30
+ const message = error instanceof Error ? error.message : String(error);
31
+ return {
32
+ content: [{ type: "text", text: message }],
33
+ isError: true,
34
+ details: { mode: "single", results: [] },
35
+ };
36
+ }
37
+ }
38
+
39
+ let asyncDir: string | null = null;
40
+ let resolvedId = params.id ?? params.runId;
41
+
42
+ if (params.dir) {
43
+ asyncDir = path.resolve(params.dir);
44
+ } else if (resolvedId) {
45
+ const direct = path.join(ASYNC_DIR, resolvedId);
46
+ if (fs.existsSync(direct)) {
47
+ asyncDir = direct;
48
+ } else {
49
+ const match = findByPrefix(ASYNC_DIR, resolvedId);
50
+ if (match) {
51
+ asyncDir = match;
52
+ resolvedId = path.basename(match);
53
+ }
54
+ }
55
+ }
56
+
57
+ const resultPath = resolvedId && !asyncDir ? findByPrefix(RESULTS_DIR, resolvedId, ".json") : null;
58
+
59
+ if (!asyncDir && !resultPath) {
60
+ return {
61
+ content: [{ type: "text", text: "Async run not found. Provide id or dir." }],
62
+ isError: true,
63
+ details: { mode: "single", results: [] },
64
+ };
65
+ }
66
+
67
+ if (asyncDir) {
68
+ let status;
69
+ try {
70
+ status = readStatus(asyncDir);
71
+ } catch (error) {
72
+ const message = error instanceof Error ? error.message : String(error);
73
+ return {
74
+ content: [{ type: "text", text: message }],
75
+ isError: true,
76
+ details: { mode: "single", results: [] },
77
+ };
78
+ }
79
+ const logPath = path.join(asyncDir, `subagent-log-${resolvedId ?? "unknown"}.md`);
80
+ const eventsPath = path.join(asyncDir, "events.jsonl");
81
+ if (status) {
82
+ const stepsTotal = status.steps?.length ?? 1;
83
+ const current = status.currentStep !== undefined ? status.currentStep + 1 : undefined;
84
+ const stepLine = current !== undefined ? `Step: ${current}/${stepsTotal}` : `Steps: ${stepsTotal}`;
85
+ const started = new Date(status.startedAt).toISOString();
86
+ const updated = status.lastUpdate ? new Date(status.lastUpdate).toISOString() : "n/a";
87
+ const statusActivityText = status.state === "running" ? activityText(status.activityState, status.lastActivityAt) : undefined;
88
+
89
+ const lines = [
90
+ `Run: ${status.runId}`,
91
+ `State: ${status.state}`,
92
+ statusActivityText ? `Activity: ${statusActivityText}` : undefined,
93
+ `Mode: ${status.mode}`,
94
+ stepLine,
95
+ `Started: ${started}`,
96
+ `Updated: ${updated}`,
97
+ `Dir: ${asyncDir}`,
98
+ ].filter((line): line is string => Boolean(line));
99
+ for (const [index, step] of (status.steps ?? []).entries()) {
100
+ const stepActivityText = step.status === "running" ? activityText(step.activityState, step.lastActivityAt) : undefined;
101
+ lines.push(`Step ${index + 1}: ${step.agent} ${step.status}${stepActivityText ? `, ${stepActivityText}` : ""}`);
102
+ }
103
+ if (status.sessionFile) lines.push(`Session: ${status.sessionFile}`);
104
+ if (fs.existsSync(logPath)) lines.push(`Log: ${logPath}`);
105
+ if (fs.existsSync(eventsPath)) lines.push(`Events: ${eventsPath}`);
106
+
107
+ return { content: [{ type: "text", text: lines.join("\n") }], details: { mode: "single", results: [] } };
108
+ }
109
+ }
110
+
111
+ if (resultPath) {
112
+ try {
113
+ const raw = fs.readFileSync(resultPath, "utf-8");
114
+ const data = JSON.parse(raw) as { id?: string; success?: boolean; summary?: string; exitCode?: number; state?: string };
115
+ const status = data.success ? "complete" : data.state === "paused" || data.exitCode === 0 ? "paused" : "failed";
116
+ const lines = [`Run: ${data.id ?? resolvedId}`, `State: ${status}`, `Result: ${resultPath}`];
117
+ if (data.summary) lines.push("", data.summary);
118
+ return { content: [{ type: "text", text: lines.join("\n") }], details: { mode: "single", results: [] } };
119
+ } catch (error) {
120
+ const message = error instanceof Error ? error.message : String(error);
121
+ return {
122
+ content: [{ type: "text", text: `Failed to read async result file: ${message}` }],
123
+ isError: true,
124
+ details: { mode: "single", results: [] },
125
+ };
126
+ }
127
+ }
128
+
129
+ return {
130
+ content: [{ type: "text", text: "Status file not found." }],
131
+ isError: true,
132
+ details: { mode: "single", results: [] },
133
+ };
134
+ }
package/schemas.ts CHANGED
@@ -57,12 +57,32 @@ export const ParallelStepSchema = Type.Object({
57
57
  // Note: Using Type.Any() for Google API compatibility (doesn't support anyOf)
58
58
  export const ChainItem = Type.Any({ description: "Chain step: either {agent, task?, ...} for sequential or {parallel: [...]} for concurrent execution" });
59
59
 
60
+ export const ControlOverrides = Type.Object({
61
+ enabled: Type.Optional(Type.Boolean({ description: "Enable/disable subagent control attention tracking for this run" })),
62
+ needsAttentionAfterMs: Type.Optional(Type.Integer({ minimum: 1, description: "No-observed-activity window before a run needs attention" })),
63
+ notifyOn: Type.Optional(Type.Array(Type.String({ enum: ["needs_attention"] }), {
64
+ description: "Control event types that should notify the parent/orchestrator. Defaults to needs_attention.",
65
+ })),
66
+ notifyChannels: Type.Optional(Type.Array(Type.String({ enum: ["event", "async", "intercom"] }), {
67
+ description: "Notification channels to use when available. Defaults to event, async, and intercom.",
68
+ })),
69
+ });
70
+
60
71
  export const SubagentParams = Type.Object({
61
72
  agent: Type.Optional(Type.String({ description: "Agent name (SINGLE mode) or target for management get/update/delete" })),
62
73
  task: Type.Optional(Type.String({ description: "Task (SINGLE mode)" })),
63
74
  // Management action (when present, tool operates in management mode)
64
75
  action: Type.Optional(Type.String({
65
- description: "Management action: 'list' (discover agents/chains), 'get' (full detail), 'create', 'update', 'delete'. Omit for execution mode."
76
+ description: "Action: management ('list','get','create','update','delete') or control ('status','interrupt'). Omit for execution mode."
77
+ })),
78
+ id: Type.Optional(Type.String({
79
+ description: "Run id or prefix for action='status' or action='interrupt'."
80
+ })),
81
+ runId: Type.Optional(Type.String({
82
+ description: "Target run ID for action='interrupt'. Defaults to the most recently active controllable run in this session. Prefer id for new calls."
83
+ })),
84
+ dir: Type.Optional(Type.String({
85
+ description: "Async run directory for action='status'."
66
86
  })),
67
87
  // Chain identifier for management (can't reuse 'chain' — that's the execution array)
68
88
  chainName: Type.Optional(Type.String({
@@ -96,14 +116,9 @@ export const SubagentParams = Type.Object({
96
116
  ),
97
117
  // Clarification TUI
98
118
  clarify: Type.Optional(Type.Boolean({ description: "Show TUI to preview/edit before execution (default: true for chains, false for single/parallel). Implies sync mode." })),
119
+ control: Type.Optional(ControlOverrides),
99
120
  // Solo agent overrides
100
121
  output: Type.Optional(Type.Any({ description: "Output file for single agent (string), or false to disable. Relative paths resolve against cwd." })),
101
122
  skill: Type.Optional(SkillOverride),
102
123
  model: Type.Optional(Type.String({ description: "Override model for single agent (e.g. 'anthropic/claude-sonnet-4')" })),
103
124
  });
104
-
105
- export const StatusParams = Type.Object({
106
- action: Type.Optional(Type.String({ description: "Action: 'list' to show active async runs, or omit to inspect one run by id/dir" })),
107
- id: Type.Optional(Type.String({ description: "Async run id or prefix" })),
108
- dir: Type.Optional(Type.String({ description: "Async run directory (overrides id search)" })),
109
- });
@@ -20,11 +20,12 @@ agents into a workflow, or create/edit agents and chains on demand.
20
20
  - **Recon and planning**: use `scout` or `context-builder`, then `planner`
21
21
  - **Parallel exploration**: run multiple non-conflicting tasks concurrently
22
22
  - **Long-running work**: launch async/background runs and inspect them later
23
+ - **Subagent control**: watch needs-attention signals and soft-interrupt only when a delegated run is genuinely blocked
23
24
  - **Agent authoring**: create, update, or override agents and chains for a project
24
25
 
25
26
  ## Tool vs Slash Commands
26
27
 
27
- Agents can use the `subagent(...)` and `subagent_status(...)` tools directly.
28
+ Agents can use the `subagent(...)` tool directly for execution, management, status, and control.
28
29
  Humans often use the slash-command layer instead:
29
30
 
30
31
  - `/run` — launch a single agent
@@ -44,14 +45,14 @@ and user/project agents override builtins with the same name.
44
45
  | Agent | Purpose | Model | Typical output / role |
45
46
  |-------|---------|-------|------------------------|
46
47
  | `scout` | Fast codebase recon | `openai-codex/gpt-5.4-mini` | Writes `context.md` handoff material |
47
- | `planner` | Creates implementation plans | `openai-codex/gpt-5.4` | Writes `plan.md` |
48
- | `worker` | General implementation | `openai-codex/gpt-5.4` | Edits code directly |
49
- | `reviewer` | Review-and-fix specialist | `openai-codex/gpt-5.3-codex:high` | Can edit/fix reviewed code |
50
- | `context-builder` | Requirements/codebase handoff builder | `openai-codex/gpt-5.4` | Writes structured context files |
51
- | `researcher` | Web research brief generator | `openai-codex/gpt-5.4` | Writes `research.md` |
48
+ | `planner` | Creates implementation plans | `openai-codex/gpt-5.5` | Writes `plan.md` |
49
+ | `worker` | General implementation | `openai-codex/gpt-5.5` | Edits code directly |
50
+ | `reviewer` | Review-and-fix specialist | `openai-codex/gpt-5.5` | Can edit/fix reviewed code |
51
+ | `context-builder` | Requirements/codebase handoff builder | `openai-codex/gpt-5.5` | Writes structured context files |
52
+ | `researcher` | Web research brief generator | `openai-codex/gpt-5.5` | Writes `research.md` |
52
53
  | `delegate` | Lightweight generic delegate | inherits parent model | No fixed output; generic delegated work |
53
- | `oracle` | Decision-consistency advisory review | `openai-codex/gpt-5.4:high` | Advisory review, intercom coordination |
54
- | `oracle-executor` | Implementation after approval | `openai-codex/gpt-5.3-codex:high` | Single-writer implementation after approval |
54
+ | `oracle` | Decision-consistency advisory review | `openai-codex/gpt-5.5` | Advisory review, intercom coordination |
55
+ | `oracle-executor` | Implementation after approval | `openai-codex/gpt-5.5` | Single-writer implementation after approval |
55
56
 
56
57
  Override builtin defaults via settings before copying full agent files when a
57
58
  small tweak is enough.
@@ -144,8 +145,42 @@ subagent({
144
145
  })
145
146
  ```
146
147
 
147
- Inspect async runs with the `subagent_status(...)` tool or the
148
- `/subagents-status` slash command.
148
+ Inspect async runs with `subagent({ action: "status", id: "..." })`, `subagent({ action: "status" })` for active runs, or the `/subagents-status` slash command.
149
+
150
+ ### Subagent control
151
+
152
+ Subagent control is the runtime visibility and intervention layer for delegated runs. It is separate from lifecycle status. Lifecycle status says whether a child is `queued`, `running`, `paused`, `complete`, or `failed`. Activity reporting is factual: it tracks the last observed activity time and the current tool when known. It does not pretend to know that a child is truly stuck.
153
+
154
+ Default behavior is intentionally conservative. When no activity has been observed past the configured threshold, the run emits a `needs_attention` control event. Foreground runs can push this as a `subagent:control-event` event, and async runs persist it to `events.jsonl` so the parent tracker can surface it without constant manual polling. Notification-worthy control events are also inserted into the visible transcript so both the user and the parent agent can see them, with a proactive hint plus concrete `nudge`, `status`, and `interrupt` options. Visible notifications fire once per child run and attention state.
155
+
156
+ Use soft interrupt when a child is clearly blocked or drifting and the parent needs to regain control:
157
+
158
+ ```typescript
159
+ subagent({ action: "interrupt" })
160
+ ```
161
+
162
+ Pass `id` when targeting a specific controllable run:
163
+
164
+ ```typescript
165
+ subagent({ action: "interrupt", id: "abc123" })
166
+ ```
167
+
168
+ A soft interrupt cancels the current child turn and leaves the run paused. It does not mean the delegated task succeeded or failed. After an interrupt, decide the next explicit action: resume with clearer instructions, replace the task, ask the user, or stop the workflow.
169
+
170
+ Per-run control thresholds can be overridden when a task legitimately runs without observable output for longer than usual:
171
+
172
+ ```typescript
173
+ subagent({
174
+ agent: "worker",
175
+ task: "Run the slow migration test suite",
176
+ control: {
177
+ needsAttentionAfterMs: 300000,
178
+ notifyOn: ["needs_attention"]
179
+ }
180
+ })
181
+ ```
182
+
183
+ If the run already has an active intercom bridge target, needs-attention notifications can also prepare a compact intercom ping for the orchestrator. When a child route is available, the ping tells the orchestrator which agent needs attention and includes the exact `intercom({ action: "send", to: "..." })` target for a nudge. Do not invent a target or ask the child to self-report when no bridge exists.
149
184
 
150
185
  ## Clarify TUI
151
186
 
@@ -334,6 +369,7 @@ particular agent or with forked context.
334
369
  filtered contexts.
335
370
  - **Default subagent nesting depth is 2.** Deeper recursive delegation is blocked
336
371
  unless configured otherwise.
372
+ - **Attention signals are not lifecycle state.** `needs_attention` means no activity has been observed past the configured threshold. `paused` means the child turn was intentionally interrupted or is awaiting direction; it is not the same as `failed`.
337
373
  - **Intercom asks are blocking.** A session can only maintain one pending outbound
338
374
  ask wait state at a time.
339
375
  - **Keep conversational authority clear.** Advisory subagents should not silently
@@ -362,6 +398,10 @@ Give subagents specific tasks rather than vague mandates.
362
398
  If a subagent encounters an unapproved product, architecture, or scope choice,
363
399
  it should coordinate back via `intercom` instead of deciding alone.
364
400
 
401
+ ### Intervene only on clear control signals
402
+
403
+ Use subagent control proactively when a delegated run emits `needs_attention`, or when a human asks you to regain control. Do not interrupt just because a child has briefly produced no output. Silence can be normal during long tool calls, test runs, or model reasoning.
404
+
365
405
  ### Name sessions meaningfully
366
406
 
367
407
  Use `/name` so intercom targeting stays stable.
@@ -0,0 +1,148 @@
1
+ import {
2
+ type ActivityState,
3
+ type ControlConfig,
4
+ type ControlEvent,
5
+ type ControlEventType,
6
+ type ControlNotificationChannel,
7
+ type ResolvedControlConfig,
8
+ } from "./types.ts";
9
+
10
+ const CONTROL_EVENT_TYPES: ControlEventType[] = ["needs_attention"];
11
+ const CONTROL_NOTIFICATION_CHANNELS: ControlNotificationChannel[] = ["event", "async", "intercom"];
12
+ const DEFAULT_NOTIFY_ON: ControlEventType[] = ["needs_attention"];
13
+
14
+ export const DEFAULT_CONTROL_CONFIG: ResolvedControlConfig = {
15
+ enabled: true,
16
+ needsAttentionAfterMs: 60_000,
17
+ notifyOn: DEFAULT_NOTIFY_ON,
18
+ notifyChannels: CONTROL_NOTIFICATION_CHANNELS,
19
+ };
20
+
21
+ function parsePositiveInt(value: unknown): number | undefined {
22
+ if (typeof value !== "number") return undefined;
23
+ if (!Number.isFinite(value) || !Number.isInteger(value) || value < 1) return undefined;
24
+ return value;
25
+ }
26
+
27
+ function parseControlList<T extends string>(value: unknown, allowed: readonly T[]): T[] | undefined {
28
+ if (!Array.isArray(value)) return undefined;
29
+ if (value.length === 0) return [];
30
+ const allowedSet = new Set(allowed);
31
+ const parsed = value.filter((entry): entry is T => typeof entry === "string" && allowedSet.has(entry as T));
32
+ return parsed.length > 0 ? Array.from(new Set(parsed)) : undefined;
33
+ }
34
+
35
+ export function resolveControlConfig(
36
+ globalConfig?: ControlConfig,
37
+ override?: ControlConfig,
38
+ ): ResolvedControlConfig {
39
+ const enabled = override?.enabled ?? globalConfig?.enabled ?? DEFAULT_CONTROL_CONFIG.enabled;
40
+ const needsAttentionAfterMs = parsePositiveInt(override?.needsAttentionAfterMs)
41
+ ?? parsePositiveInt(globalConfig?.needsAttentionAfterMs)
42
+ ?? DEFAULT_CONTROL_CONFIG.needsAttentionAfterMs;
43
+ const notifyOn = parseControlList(override?.notifyOn, CONTROL_EVENT_TYPES)
44
+ ?? parseControlList(globalConfig?.notifyOn, CONTROL_EVENT_TYPES)
45
+ ?? DEFAULT_CONTROL_CONFIG.notifyOn;
46
+ const notifyChannels = parseControlList(override?.notifyChannels, CONTROL_NOTIFICATION_CHANNELS)
47
+ ?? parseControlList(globalConfig?.notifyChannels, CONTROL_NOTIFICATION_CHANNELS)
48
+ ?? DEFAULT_CONTROL_CONFIG.notifyChannels;
49
+ return {
50
+ enabled,
51
+ needsAttentionAfterMs,
52
+ notifyOn: [...notifyOn],
53
+ notifyChannels: [...notifyChannels],
54
+ };
55
+ }
56
+
57
+ export function deriveActivityState(input: {
58
+ config: ResolvedControlConfig;
59
+ startedAt: number;
60
+ lastActivityAt?: number;
61
+ now?: number;
62
+ }): ActivityState | undefined {
63
+ if (!input.config.enabled) return undefined;
64
+ const now = input.now ?? Date.now();
65
+ const lastActivity = input.lastActivityAt ?? input.startedAt;
66
+ const ageMs = Math.max(0, now - lastActivity);
67
+ return ageMs > input.config.needsAttentionAfterMs ? "needs_attention" : undefined;
68
+ }
69
+
70
+ export function shouldEmitControlEvent(
71
+ config: ResolvedControlConfig,
72
+ from: ActivityState | undefined,
73
+ to: ActivityState | undefined,
74
+ ): boolean {
75
+ return config.enabled && from !== to && to === "needs_attention";
76
+ }
77
+
78
+ export function buildControlEvent(input: {
79
+ from?: ActivityState;
80
+ to: ActivityState;
81
+ runId: string;
82
+ agent: string;
83
+ index?: number;
84
+ ts?: number;
85
+ lastActivityAt?: number;
86
+ }): ControlEvent {
87
+ const ts = input.ts ?? Date.now();
88
+ const elapsedMs = input.lastActivityAt ? Math.max(0, ts - input.lastActivityAt) : undefined;
89
+ const elapsedSeconds = elapsedMs !== undefined ? Math.floor(elapsedMs / 1000) : undefined;
90
+ const message = elapsedSeconds !== undefined
91
+ ? `${input.agent} needs attention (no observed activity for ${elapsedSeconds}s)`
92
+ : `${input.agent} needs attention`;
93
+ return {
94
+ type: "needs_attention",
95
+ from: input.from,
96
+ to: input.to,
97
+ ts,
98
+ runId: input.runId,
99
+ agent: input.agent,
100
+ index: input.index,
101
+ message,
102
+ };
103
+ }
104
+
105
+ export function shouldNotifyControlEvent(config: ResolvedControlConfig, event: ControlEvent): boolean {
106
+ return config.enabled && config.notifyOn.includes(event.type);
107
+ }
108
+
109
+ export function controlNotificationKey(event: ControlEvent, childIntercomTarget?: string): string {
110
+ const childKey = childIntercomTarget ?? (event.index !== undefined ? `${event.runId}:${event.index}` : event.runId);
111
+ return `${childKey}:${event.type}`;
112
+ }
113
+
114
+ export function claimControlNotification(config: ResolvedControlConfig, event: ControlEvent, seenKeys: Set<string>, childIntercomTarget?: string): boolean {
115
+ if (!shouldNotifyControlEvent(config, event)) return false;
116
+ const key = controlNotificationKey(event, childIntercomTarget);
117
+ if (seenKeys.has(key)) return false;
118
+ seenKeys.add(key);
119
+ return true;
120
+ }
121
+
122
+ export function formatControlNoticeMessage(event: ControlEvent, childIntercomTarget?: string): string {
123
+ const runTarget = event.runId;
124
+ const nudgeCommand = childIntercomTarget
125
+ ? `intercom({ action: "send", to: "${childIntercomTarget}", message: "What are you blocked on? Reply with the smallest next step or ask for a decision." })`
126
+ : undefined;
127
+ return [
128
+ `Subagent needs attention: ${event.agent}`,
129
+ `Run: ${runTarget}${event.index !== undefined ? ` step ${event.index + 1}` : ""}`,
130
+ `Signal: ${event.message}`,
131
+ "Hint: Inspect status first unless the run is clearly blocked.",
132
+ childIntercomTarget
133
+ ? `Nudge: ${nudgeCommand}`
134
+ : "Nudge: no child message route registered",
135
+ `Status: subagent({ action: "status", id: "${runTarget}" })`,
136
+ `Interrupt: subagent({ action: "interrupt", id: "${runTarget}" })`,
137
+ ].join("\n");
138
+ }
139
+
140
+ export function formatControlIntercomMessage(event: ControlEvent, childIntercomTarget?: string): string {
141
+ return [
142
+ "subagent needs attention",
143
+ "",
144
+ `${event.agent} needs attention in run ${event.runId}.`,
145
+ "",
146
+ formatControlNoticeMessage(event, childIntercomTarget),
147
+ ].join("\n");
148
+ }