@d3ara1n/pi-subagent 0.10.0 → 0.10.2
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 +13 -10
- package/package.json +2 -2
- package/src/config.test.ts +118 -0
- package/src/config.ts +5 -5
- package/src/history.ts +56 -0
- package/src/index.ts +31 -455
- package/src/output.ts +116 -0
- package/src/render.ts +277 -0
- package/src/roles.ts +10 -6
- package/src/spawn.ts +45 -22
- package/src/types.ts +8 -11
- package/src/utils.test.ts +45 -10
- package/src/utils.ts +26 -8
package/README.md
CHANGED
|
@@ -30,12 +30,12 @@ This means:
|
|
|
30
30
|
|
|
31
31
|
## Built-in Roles
|
|
32
32
|
|
|
33
|
-
| Role | Model Role | Tools | Can Delegate To | Description |
|
|
34
|
-
|
|
35
|
-
| `explorer` | fast | read, find, grep
|
|
36
|
-
| `reviewer` | heavy | read, bash, grep,
|
|
37
|
-
| `worker` | default | read, bash, edit, write, grep,
|
|
38
|
-
| `researcher` | fast | web_search, fetch_content, read, bash, delegate | explorer | Web research + GitHub repo analysis |
|
|
33
|
+
| Role | Model Role | Timeout | Tools | Can Delegate To | Description |
|
|
34
|
+
|------|-----------|---------|-------|-----------------|-------------|
|
|
35
|
+
| `explorer` | fast | 900s | read, find, grep | — | Fast code search (read-only, no bash) |
|
|
36
|
+
| `reviewer` | heavy | 3600s | read, bash, grep, find | — | Deep code review (read-only, bash for git/log) |
|
|
37
|
+
| `worker` | default | 2400s | read, bash, edit, write, grep, find, delegate | explorer, researcher | Implementation — the only role that can modify files |
|
|
38
|
+
| `researcher` | fast | 2400s | web_search, fetch_content, read, bash, delegate | explorer | Web research + GitHub repo analysis |
|
|
39
39
|
|
|
40
40
|
**Nested delegation**: `worker` and `researcher` can spawn their own subagents. This keeps the main model's context clean — a worker can explore unfamiliar code via an `explorer` subagent without returning intermediate results to the main model.
|
|
41
41
|
|
|
@@ -76,7 +76,6 @@ Edit `~/.pi/agent/settings.json`:
|
|
|
76
76
|
```json
|
|
77
77
|
{
|
|
78
78
|
"subagent": {
|
|
79
|
-
"timeout": 1500,
|
|
80
79
|
"maxConcurrency": 4,
|
|
81
80
|
"maxDepth": 3,
|
|
82
81
|
"maxTurns": 0,
|
|
@@ -92,7 +91,11 @@ Edit `~/.pi/agent/settings.json`:
|
|
|
92
91
|
}
|
|
93
92
|
```
|
|
94
93
|
|
|
95
|
-
All fields are optional. Defaults: `
|
|
94
|
+
All fields are optional. Defaults: `maxConcurrency: 4`, `maxDepth: 3`, `maxTurns: 0` (unlimited), `maxCost: 0` (unlimited), `history.enabled: true`, `summary.role: "utility"`, `summary.enabled: true`.
|
|
95
|
+
|
|
96
|
+
Timeouts are defined per role. Built-in defaults are `explorer: 900`, `reviewer: 3600`, `worker: 2400`, and `researcher: 2400` seconds. The timeout is active time — the clock pauses while the child is inside a nested `delegate` call, so delegate-capable roles need no extra headroom.
|
|
97
|
+
|
|
98
|
+
All numeric limits accept `0` for unlimited: `maxConcurrency`, `maxDepth`, `maxTurns`, `maxCost`, and per-role `timeout`. Negative values are normalized to `0`; non-numeric or non-finite values fall back to their defaults. `maxConcurrency: 0` runs delegates without queuing, and `maxDepth: 0` permits unrestricted nesting.
|
|
96
99
|
|
|
97
100
|
### Agent Overrides
|
|
98
101
|
|
|
@@ -129,7 +132,7 @@ Override, disable, or add subagent roles via `agentOverrides`. Built-in and cust
|
|
|
129
132
|
|
|
130
133
|
**Required fields for custom roles:** `role`, `description`, `examples`, `decisionTrigger`, `tools`, `systemPrompt`.
|
|
131
134
|
|
|
132
|
-
**Optional fields:** `subagentRoles` (roles this role can spawn via delegate), `timeout` (per-role timeout
|
|
135
|
+
**Optional fields:** `subagentRoles` (roles this role can spawn via delegate), `timeout` (per-role active-time timeout in seconds; unset or `0` is unlimited, negative values normalize to `0`), `maxTurns` / `maxCost` (per-role budget overrides; unset uses the top-level `maxTurns` / `maxCost` setting, `0` is unlimited, negative values normalize to `0`), `fallbackRole` (backup pi-model-roles role on provider errors).
|
|
133
136
|
|
|
134
137
|
Invalid custom roles (missing required fields) are silently skipped with an error notification at session start.
|
|
135
138
|
|
|
@@ -194,7 +197,7 @@ Each path is injected as an independent `@file` attachment the subagent reads di
|
|
|
194
197
|
|
|
195
198
|
### Budget enforcement
|
|
196
199
|
|
|
197
|
-
`maxTurns` / `maxCost` cap a run. When exceeded, the child is killed and the last completed output is returned with `stopReason: "budget_exceeded"` (shown in the expanded TUI). Defaults are unlimited (0); set global defaults in config or per-role
|
|
200
|
+
`maxTurns` / `maxCost` cap a run. When exceeded, the child is killed and the last completed output is returned with `stopReason: "budget_exceeded"` (shown in the expanded TUI). Defaults are unlimited (`0`); set global defaults in config or per-role overrides in `agentOverrides`. Negative values are normalized to `0`.
|
|
198
201
|
|
|
199
202
|
### Oversized outputs
|
|
200
203
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@d3ara1n/pi-subagent",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Role-based subagent orchestration for pi — delegates tasks to specialized pi child processes with configurable model roles",
|
|
6
6
|
"main": "src/index.ts",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
}
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@d3ara1n/pi-model-roles": "^0.
|
|
30
|
+
"@d3ara1n/pi-model-roles": "^0.7.0"
|
|
31
31
|
},
|
|
32
32
|
"pi": {
|
|
33
33
|
"extensions": [
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration loading regression tests.
|
|
3
|
+
*
|
|
4
|
+
* Uses isolated global/project settings roots via PI_AGENT_DIR.
|
|
5
|
+
* node --test packages/pi-subagent/src/config.test.ts
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { afterEach, describe, test } from "node:test";
|
|
9
|
+
import assert from "node:assert/strict";
|
|
10
|
+
import * as fs from "node:fs";
|
|
11
|
+
import * as os from "node:os";
|
|
12
|
+
import * as path from "node:path";
|
|
13
|
+
import { loadSubagentConfig } from "./config.ts";
|
|
14
|
+
import { DEFAULT_CONFIG } from "./types.ts";
|
|
15
|
+
|
|
16
|
+
const originalAgentDir = process.env.PI_AGENT_DIR;
|
|
17
|
+
const tempRoots: string[] = [];
|
|
18
|
+
|
|
19
|
+
function makeRoot(): { agentDir: string; projectDir: string } {
|
|
20
|
+
const root = fs.mkdtempSync(path.join(os.tmpdir(), "pi-subagent-config-test-"));
|
|
21
|
+
tempRoots.push(root);
|
|
22
|
+
const agentDir = path.join(root, "agent");
|
|
23
|
+
const projectDir = path.join(root, "project");
|
|
24
|
+
fs.mkdirSync(agentDir, { recursive: true });
|
|
25
|
+
fs.mkdirSync(projectDir, { recursive: true });
|
|
26
|
+
process.env.PI_AGENT_DIR = agentDir;
|
|
27
|
+
return { agentDir, projectDir };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function writeSettings(dir: string, settings: unknown): void {
|
|
31
|
+
writeSettingsText(dir, JSON.stringify(settings));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function writeSettingsText(dir: string, content: string): void {
|
|
35
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
36
|
+
fs.writeFileSync(path.join(dir, "settings.json"), content);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
afterEach(() => {
|
|
40
|
+
if (originalAgentDir === undefined) delete process.env.PI_AGENT_DIR;
|
|
41
|
+
else process.env.PI_AGENT_DIR = originalAgentDir;
|
|
42
|
+
for (const root of tempRoots.splice(0)) fs.rmSync(root, { recursive: true, force: true });
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe("loadSubagentConfig", () => {
|
|
46
|
+
test("preserves zero limits and clamps negative numeric limits to unlimited", () => {
|
|
47
|
+
const { agentDir } = makeRoot();
|
|
48
|
+
writeSettings(agentDir, {
|
|
49
|
+
subagent: {
|
|
50
|
+
maxConcurrency: -1,
|
|
51
|
+
maxDepth: -2,
|
|
52
|
+
maxTurns: 0,
|
|
53
|
+
maxCost: -0.5,
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const config = loadSubagentConfig();
|
|
58
|
+
assert.equal(config.maxConcurrency, 0);
|
|
59
|
+
assert.equal(config.maxDepth, 0);
|
|
60
|
+
assert.equal(config.maxTurns, 0);
|
|
61
|
+
assert.equal(config.maxCost, 0);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test("uses defaults for non-finite or non-numeric numeric limits", () => {
|
|
65
|
+
const { agentDir } = makeRoot();
|
|
66
|
+
// JSON.parse accepts numeric overflow as Infinity, even though JSON.stringify
|
|
67
|
+
// would serialize it as null. Exercise both non-finite and wrong-type inputs.
|
|
68
|
+
writeSettingsText(
|
|
69
|
+
agentDir,
|
|
70
|
+
'{"subagent":{"maxConcurrency":-1e999,"maxDepth":{},"maxTurns":false,"maxCost":"NaN"}}',
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
const config = loadSubagentConfig();
|
|
74
|
+
assert.equal(config.maxConcurrency, DEFAULT_CONFIG.maxConcurrency);
|
|
75
|
+
assert.equal(config.maxDepth, DEFAULT_CONFIG.maxDepth);
|
|
76
|
+
assert.equal(config.maxTurns, DEFAULT_CONFIG.maxTurns);
|
|
77
|
+
assert.equal(config.maxCost, DEFAULT_CONFIG.maxCost);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test("floors finite fractional count limits", () => {
|
|
81
|
+
const { agentDir } = makeRoot();
|
|
82
|
+
writeSettings(agentDir, {
|
|
83
|
+
subagent: { maxConcurrency: 2.9, maxDepth: 3.1, maxTurns: 4.8 },
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
const config = loadSubagentConfig();
|
|
87
|
+
assert.equal(config.maxConcurrency, 2);
|
|
88
|
+
assert.equal(config.maxDepth, 3);
|
|
89
|
+
assert.equal(config.maxTurns, 4);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test("project subagent block replaces global wholesale and defaults omitted fields", () => {
|
|
93
|
+
const { agentDir, projectDir } = makeRoot();
|
|
94
|
+
writeSettings(agentDir, {
|
|
95
|
+
subagent: {
|
|
96
|
+
maxConcurrency: 8,
|
|
97
|
+
maxDepth: 7,
|
|
98
|
+
maxTurns: 6,
|
|
99
|
+
maxCost: 5,
|
|
100
|
+
history: { enabled: false },
|
|
101
|
+
summary: { enabled: false, role: "global-summary" },
|
|
102
|
+
agentOverrides: { global: { disabled: true } },
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
writeSettings(path.join(projectDir, ".pi"), {
|
|
106
|
+
subagent: { summary: { role: "project-summary" } },
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
const config = loadSubagentConfig(projectDir);
|
|
110
|
+
assert.equal(config.maxConcurrency, DEFAULT_CONFIG.maxConcurrency);
|
|
111
|
+
assert.equal(config.maxDepth, DEFAULT_CONFIG.maxDepth);
|
|
112
|
+
assert.equal(config.maxTurns, DEFAULT_CONFIG.maxTurns);
|
|
113
|
+
assert.equal(config.maxCost, DEFAULT_CONFIG.maxCost);
|
|
114
|
+
assert.deepEqual(config.history, DEFAULT_CONFIG.history);
|
|
115
|
+
assert.deepEqual(config.summary, { enabled: DEFAULT_CONFIG.summary.enabled, role: "project-summary" });
|
|
116
|
+
assert.deepEqual(config.agentOverrides, {});
|
|
117
|
+
});
|
|
118
|
+
});
|
package/src/config.ts
CHANGED
|
@@ -10,6 +10,7 @@ import * as os from "node:os";
|
|
|
10
10
|
import * as path from "node:path";
|
|
11
11
|
import type { SubagentConfig } from "./types.ts";
|
|
12
12
|
import { DEFAULT_CONFIG } from "./types.ts";
|
|
13
|
+
import { normalizeNonNegativeInteger, normalizeNonNegativeNumber } from "./utils.ts";
|
|
13
14
|
|
|
14
15
|
function getAgentDir(): string {
|
|
15
16
|
const envDir = process.env.PI_AGENT_DIR;
|
|
@@ -45,11 +46,10 @@ export function loadSubagentConfig(cwd?: string): SubagentConfig {
|
|
|
45
46
|
const rawSummary = raw?.summary;
|
|
46
47
|
const rawHistory = raw?.history;
|
|
47
48
|
return {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
maxCost: raw.maxCost ?? DEFAULT_CONFIG.maxCost,
|
|
49
|
+
maxConcurrency: normalizeNonNegativeInteger(raw.maxConcurrency, DEFAULT_CONFIG.maxConcurrency),
|
|
50
|
+
maxDepth: normalizeNonNegativeInteger(raw.maxDepth, DEFAULT_CONFIG.maxDepth),
|
|
51
|
+
maxTurns: normalizeNonNegativeInteger(raw.maxTurns, DEFAULT_CONFIG.maxTurns),
|
|
52
|
+
maxCost: normalizeNonNegativeNumber(raw.maxCost, DEFAULT_CONFIG.maxCost),
|
|
53
53
|
history: {
|
|
54
54
|
enabled: rawHistory?.enabled ?? DEFAULT_CONFIG.history.enabled,
|
|
55
55
|
},
|
package/src/history.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* History persistence for pi-subagent delegate runs.
|
|
3
|
+
*
|
|
4
|
+
* Best-effort audit log: writes one JSON record per delegate run under
|
|
5
|
+
* ~/.pi/subagent/history/{sessionId}/{toolCallId}.json. Never throws — persistence
|
|
6
|
+
* must not fail the delegation. Privacy parity with pi's own session files.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import * as os from "node:os";
|
|
10
|
+
import * as fs from "node:fs";
|
|
11
|
+
import * as path from "node:path";
|
|
12
|
+
import type { SubagentResult } from "./types.ts";
|
|
13
|
+
import { sanitizeFilename } from "./utils.ts";
|
|
14
|
+
|
|
15
|
+
export function persistSubagentHistory(
|
|
16
|
+
sessionId: string | undefined,
|
|
17
|
+
toolCallId: string,
|
|
18
|
+
role: string,
|
|
19
|
+
task: string,
|
|
20
|
+
r: SubagentResult,
|
|
21
|
+
rawOutput?: string,
|
|
22
|
+
): void {
|
|
23
|
+
try {
|
|
24
|
+
const dir = path.join(
|
|
25
|
+
os.homedir(),
|
|
26
|
+
".pi",
|
|
27
|
+
"subagent",
|
|
28
|
+
"history",
|
|
29
|
+
sanitizeFilename(sessionId ?? "unknown"),
|
|
30
|
+
);
|
|
31
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
32
|
+
const payload = {
|
|
33
|
+
id: toolCallId,
|
|
34
|
+
role,
|
|
35
|
+
task,
|
|
36
|
+
timestamp: Date.now(),
|
|
37
|
+
exitCode: r.exitCode,
|
|
38
|
+
stopReason: r.stopReason,
|
|
39
|
+
model: r.model,
|
|
40
|
+
summary: r.summary,
|
|
41
|
+
// Keep the full original output for auditing even if LLM/TUI saw a compressed/truncated version.
|
|
42
|
+
output: rawOutput ?? r.output,
|
|
43
|
+
outputMethod: r.outputMethod,
|
|
44
|
+
errorMessage: r.errorMessage,
|
|
45
|
+
usage: r.usage,
|
|
46
|
+
activityLog: r.activityLog,
|
|
47
|
+
};
|
|
48
|
+
fs.writeFileSync(
|
|
49
|
+
path.join(dir, `${sanitizeFilename(toolCallId)}.json`),
|
|
50
|
+
JSON.stringify(payload, null, 2),
|
|
51
|
+
{ mode: 0o600 },
|
|
52
|
+
);
|
|
53
|
+
} catch {
|
|
54
|
+
/* best-effort — never fail the delegation */
|
|
55
|
+
}
|
|
56
|
+
}
|