@arhen/pi-core-subagent 1.3.25 → 1.3.26

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/README.md CHANGED
@@ -50,7 +50,7 @@ flowchart LR
50
50
  - **A bad graph fails before it spawns.** Unknown ids, self-edges and cycles are rejected at call time — never halfway through a run with three children already burning tokens.
51
51
  - **Proof is an exit code, never a self-report.** Tasks are asked for a runnable `Verify:` command; the leader checks `git diff --stat`. Agents auditing their own work score ~0. ([why](#why-9-is-a-verification-command-not-a-self-report))
52
52
  - **No ceremony without edges.** Six independent reviewers stay six independent reviewers — no waves, no gates, no graph vocabulary imposed on flat work.
53
- - **No agent files, no discovery.** The leader defines every subagent inline per callname, system prompt, toolset. Nothing is read from or written to disk.
53
+ - **Agent files respected.** An `agent` name matching `.agents/agents/<name>.md`, `.claude/agents/<name>.md`, or `.pi/agents/<name>.md` loads that file body = system prompt, frontmatter `model`/`tools` apply, `model` is validated against the pi model registry. Inline params override the file. Project dirs win over home (`~/.agents` single source → `~/.claude` → `~/.pi`).
54
54
  - **Two toolsets only.** Read-only (`read, grep, find, ls` — default) or write (`read, grep, find, ls, bash, edit, write` — `write: true`). No per-agent tool config surface.
55
55
  - **In-process** — children are `AgentSession`s in the same runtime. No process spawn, no context bleed.
56
56
  - **Zero parent-context injection.** No catalog, no context hook. 6 slim tools total.
@@ -83,7 +83,7 @@ The dotted arrows are the whole point: a child may burn 200k tokens reading file
83
83
 
84
84
  ## Usage — the leader invents the agents
85
85
 
86
- Define agents inline per call never creates or reads agent files. Model resolution: explicit `provider/model-id` (or bare id) via the pi model registry → agent-file `model` → the parent's current model → settings default.
86
+ Define agents inline per call, or reference a named agent file (see [Agent files](#agent-files)). Model resolution: explicit `model` → agent-file `model` (validated against the pi model registry) → the parent's current model → settings default.
87
87
 
88
88
  ```json
89
89
  {
@@ -116,6 +116,27 @@ Chain — `{previous}` is replaced with the prior agent's output:
116
116
  }
117
117
  ```
118
118
 
119
+ ## Agent files
120
+
121
+ An `agent` name that matches `<name>.md` in an agents directory loads that file — no inline `prompt` needed. The file body becomes the system prompt; `model` and `tools` frontmatter apply.
122
+
123
+ ```md
124
+ ---
125
+ name: api-reviewer
126
+ description: Strict API reviewer — auth, rate limiting, error handling
127
+ model: claude-opus-4-6
128
+ tools: read, grep, find, ls
129
+ ---
130
+ You are a strict API reviewer. Check auth, rate limiting, and error handling. Cite file:line.
131
+ ```
132
+
133
+ **Lookup order** (first match wins):
134
+
135
+ 1. `.agents/agents/` then `.claude/agents/` then `.pi/agents/` in each directory from the task `cwd` up to the filesystem root (nearest ancestor wins).
136
+ 2. Home: `~/.agents/agents/` (single source) → `~/.claude/agents/` → `~/.pi/agents/`.
137
+
138
+ **Precedence** — inline params always win over the file: `prompt` overrides the body, `model` overrides frontmatter `model`, `tools`/`write` override frontmatter `tools`. A file `model` is validated against the pi model registry (unknown model fails the task with a catalog message). Files without frontmatter work too — the whole file is the system prompt.
139
+
119
140
  ## Graph mode — `needs`
120
141
 
121
142
  `parallel` runs everything at once; `chain` runs everything one at a time. Most real work is neither. Give a task an `id` and list the ids it `needs`:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arhen/pi-core-subagent",
3
- "version": "1.3.25",
3
+ "version": "1.3.26",
4
4
  "type": "module",
5
5
  "description": "pi extension: fast in-process subagents with a dependency-graph scheduler (needs edges gate tasks and carry upstream output into dependent prompts), plus background runs, intercom and agent-to-agent mailbox. Leader defines agents inline.",
6
6
  "license": "MIT",
@@ -0,0 +1,61 @@
1
+ /** Named agent-file resolution (.agents/.claude/.pi agents dirs). */
2
+ import { existsSync, readFileSync } from "node:fs";
3
+ import { dirname, join } from "node:path";
4
+ import { parseFrontmatter } from "@earendil-works/pi-coding-agent";
5
+
6
+ export interface AgentFileInfo {
7
+ body: string;
8
+ model?: string;
9
+ tools?: string[];
10
+ }
11
+
12
+ const AGENT_DIRS = [".agents/agents", ".claude/agents", ".pi/agents"] as const;
13
+ /** Agent names become file paths — refuse anything that could traverse. */
14
+ const SAFE_NAME = /^[\w.-]+$/;
15
+
16
+ function readAgentFile(dir: string, name: string): AgentFileInfo | undefined {
17
+ if (!existsSync(dir)) return undefined;
18
+ const path = join(dir, `${name}.md`);
19
+ if (!existsSync(path)) return undefined;
20
+ const { frontmatter, body } = parseFrontmatter(readFileSync(path, "utf8"));
21
+ const tools =
22
+ typeof frontmatter.tools === "string"
23
+ ? frontmatter.tools
24
+ .split(",")
25
+ .map((t) => t.trim())
26
+ .filter(Boolean)
27
+ : Array.isArray(frontmatter.tools)
28
+ ? frontmatter.tools.map(String)
29
+ : undefined;
30
+ return {
31
+ body,
32
+ model: typeof frontmatter.model === "string" ? frontmatter.model : undefined,
33
+ tools: tools?.length ? tools : undefined,
34
+ };
35
+ }
36
+
37
+ /**
38
+ * Look up `<name>.md` in agent dirs. Order: for each ancestor of `cwd` (nearest
39
+ * first): `.agents/agents` (single source per the ~/.agents spec) → `.claude/agents`
40
+ * → `.pi/agents`; then home: `~/.agents/agents` → `~/.claude/agents` → `~/.pi/agents`.
41
+ * First match wins. `agentDir` is the pi agent dir (`~/.pi/agent`); home is derived from it.
42
+ */
43
+ export function resolveAgentFile(name: string, cwd: string, agentDir: string): AgentFileInfo | undefined {
44
+ if (!SAFE_NAME.test(name)) return undefined;
45
+ let dir = cwd;
46
+ while (true) {
47
+ for (const sub of AGENT_DIRS) {
48
+ const hit = readAgentFile(join(dir, sub), name);
49
+ if (hit) return hit;
50
+ }
51
+ const parent = dirname(dir);
52
+ if (parent === dir) break;
53
+ dir = parent;
54
+ }
55
+ const home = dirname(dirname(agentDir)); // ~/.pi/agent → ~
56
+ for (const sub of AGENT_DIRS) {
57
+ const hit = readAgentFile(join(home, sub), name);
58
+ if (hit) return hit;
59
+ }
60
+ return undefined;
61
+ }
package/src/index.ts CHANGED
@@ -127,7 +127,7 @@ export default function (pi: ExtensionAPI) {
127
127
  // ponytail: this string is billed on every request. No example block — an example
128
128
  // biases the model toward one shape; guidelines + JSON schema describe all of them.
129
129
  description:
130
- "Run isolated subagents (own context, own session). You invent each agent: name, optional system prompt, toolset (read-only default, write:true to edit). Use `agent`+`task` for one, `tasks` for many. `needs` declares dependency edges: a task waits for its needs and receives their outputs prepended to its prompt. Every run is background: the call returns a runId immediately and completion notifies you. Set autoAwait:true when you need the result before your next step — the call parks until the run finishes and returns runId + final result in one response. allowIntercom:true lets children talk to you and each other.",
130
+ "Run isolated subagents (own context, own session). You invent each agent: name, optional system prompt, toolset (read-only default, write:true to edit). Use `agent`+`task` for one, `tasks` for many. `needs` declares dependency edges: a task waits for its needs and receives their outputs prepended to its prompt. An `agent` name matching a file in `.agents/agents`, `.claude/agents`, or `.pi/agents` (project dirs, then home) loads it: body = system prompt, frontmatter `model`/`tools` apply; inline prompt/model/tools/write override the file. Every run is background: the call returns a runId immediately and completion notifies you. Set autoAwait:true when you need the result before your next step — the call parks until the run finishes and returns runId + final result in one response. allowIntercom:true lets children talk to you and each other.",
131
131
  promptSnippet: "Define and delegate work to specialized subagents.",
132
132
  promptGuidelines: [
133
133
  "Use subagent when independent review, testing, research, or parallel analysis improves quality.",
@@ -135,7 +135,7 @@ export default function (pi: ExtensionAPI) {
135
135
  "Order comes from `needs`, not from separate calls: give tasks an `id`, list the ids each depends on. Tasks with no unmet needs run in parallel; dependents receive their upstream outputs automatically — do not restate them.",
136
136
  "Prefer flat `tasks` (plain parallel) unless a real dependency exists — only add `needs` edges when ordering genuinely matters.",
137
137
  "End each task with a runnable check, e.g. 'Verify: npx tsc --noEmit && bun test'. A subagent's claim of success is not evidence.",
138
- "Define each agent yourself: invented name, focused system prompt, and read-only (default) or write:true. Prefer read-only.",
138
+ "Define each agent yourself: invented name, focused system prompt, and read-only (default) or write:true. Prefer read-only. An `agent` name that matches an existing file in `.agents/agents`, `.claude/agents`, or `.pi/agents` (project first, then home) loads that agent — body = system prompt, frontmatter `model`/`tools` apply, file `model` is validated against the model registry; inline params override the file. Files without frontmatter work too.",
139
139
  "When you need a run's result before your next step, spawn with autoAwait:true — the call returns runId + final result in one response. Otherwise spawn background and settle results (await_subagent / subagent_result) before continuing dependent work.",
140
140
  "For long multi-task runs, don't autoAwait the whole run: spawn background, then loop await_subagent with short timeoutMs slices (e.g. 20s), processing whichever tasks completed in each slice while the rest keep running. You get incremental results instead of one big wait.",
141
141
  "allowIntercom:true only when a child may need to ask you something.",
package/src/manager.ts CHANGED
@@ -16,6 +16,7 @@ import {
16
16
  type ToolDefinition,
17
17
  } from "@earendil-works/pi-coding-agent";
18
18
  import type { TUI } from "@earendil-works/pi-tui";
19
+ import { resolveAgentFile } from "./agentfile.ts";
19
20
  import { CHILD_TALK_TOOLS, type ChildHandlers, createChildTools, createWatchdog, type Watchdog } from "./child.ts";
20
21
  import {
21
22
  activitySnippet,
@@ -631,18 +632,19 @@ export class SubagentManager {
631
632
  ): Promise<void> {
632
633
  if (TERMINAL.includes(task.status)) return; // canceled while queued
633
634
 
634
- // Inline params win; otherwise fall back to an existing agent file
635
- // (~/.agents, .pi/agents, user dir). Never creates files.
636
- const prompt = input.prompt?.trim();
635
+ // Named agent file (`.agents/agents` etc.): body = system prompt, frontmatter
636
+ // model/tools fill gaps. Inline params always win over the file.
637
+ const file = resolveAgentFile(input.agent, task.cwd, getAgentDir());
638
+ const prompt = input.prompt?.trim() ?? file?.body;
637
639
  const thinking = input.thinking;
638
- const baseTools = input.tools ?? (input.write ? WRITE_TOOLS : READONLY_TOOLS);
640
+ const baseTools = input.tools ?? (input.write ? WRITE_TOOLS : (file?.tools ?? READONLY_TOOLS));
639
641
  const tools = [...baseTools, ...(run.allowIntercom ? CHILD_TALK_TOOLS : [])];
640
642
 
641
643
  // Model + thinking resolve against the pi model registry; a bad request
642
644
  // fails the TASK with a helpful message, not the whole run.
643
645
  let model: Model<Api> | undefined;
644
646
  try {
645
- model = resolveChildModel(ctx, input.model);
647
+ model = resolveChildModel(ctx, input.model ?? file?.model);
646
648
  validateThinking(model, thinking);
647
649
  } catch (err) {
648
650
  this.updateTask(