@agnishc/edb-subagents 0.8.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.
@@ -0,0 +1,121 @@
1
+ /**
2
+ * Cross-extension RPC handlers for the subagents extension.
3
+ *
4
+ * Exposes ping, spawn, and stop RPCs over the pi.events event bus,
5
+ * using per-request scoped reply channels.
6
+ *
7
+ * Reply envelope follows pi-mono convention:
8
+ * success → { success: true, data?: T }
9
+ * error → { success: false, error: string }
10
+ */
11
+
12
+ import { type ModelRegistry, resolveModel } from "./model-resolver.js";
13
+
14
+ /** Minimal event bus interface needed by the RPC handlers. */
15
+ export interface EventBus {
16
+ on(event: string, handler: (data: unknown) => void): () => void;
17
+ emit(event: string, data: unknown): void;
18
+ }
19
+
20
+ /** RPC reply envelope — matches pi-mono's RpcResponse shape. */
21
+ export type RpcReply<T = void> = { success: true; data?: T } | { success: false; error: string };
22
+
23
+ /** RPC protocol version — bumped when the envelope or method contracts change. */
24
+ export const PROTOCOL_VERSION = 2;
25
+
26
+ /** Minimal AgentManager interface needed by the spawn/stop RPCs. */
27
+ export interface SpawnCapable {
28
+ spawn(pi: unknown, ctx: unknown, type: string, prompt: string, options: any): string;
29
+ abort(id: string): boolean;
30
+ }
31
+
32
+ export interface RpcDeps {
33
+ events: EventBus;
34
+ pi: unknown; // passed through to manager.spawn
35
+ getCtx: () => unknown | undefined; // returns current ExtensionContext
36
+ manager: SpawnCapable;
37
+ }
38
+
39
+ export interface RpcHandle {
40
+ unsubPing: () => void;
41
+ unsubSpawn: () => void;
42
+ unsubStop: () => void;
43
+ }
44
+
45
+ /**
46
+ * Wire a single RPC handler: listen on `channel`, run `fn(params)`,
47
+ * emit the reply envelope on `channel:reply:${requestId}`.
48
+ */
49
+ function handleRpc<P extends { requestId: string }>(
50
+ events: EventBus,
51
+ channel: string,
52
+ fn: (params: P) => unknown | Promise<unknown>,
53
+ ): () => void {
54
+ return events.on(channel, async (raw: unknown) => {
55
+ const params = raw as P;
56
+ try {
57
+ const data = await fn(params);
58
+ const reply: { success: true; data?: unknown } = { success: true };
59
+ if (data !== undefined) reply.data = data;
60
+ events.emit(`${channel}:reply:${params.requestId}`, reply);
61
+ } catch (err: any) {
62
+ events.emit(`${channel}:reply:${params.requestId}`, {
63
+ success: false,
64
+ error: err?.message ?? String(err),
65
+ });
66
+ }
67
+ });
68
+ }
69
+
70
+ /**
71
+ * Register ping, spawn, and stop RPC handlers on the event bus.
72
+ * Returns unsub functions for cleanup.
73
+ */
74
+ export function registerRpcHandlers(deps: RpcDeps): RpcHandle {
75
+ const { events, pi, getCtx, manager } = deps;
76
+
77
+ const unsubPing = handleRpc(events, "subagents:rpc:ping", () => {
78
+ return { version: PROTOCOL_VERSION };
79
+ });
80
+
81
+ const unsubSpawn = handleRpc<{ requestId: string; type: string; prompt: string; options?: any }>(
82
+ events,
83
+ "subagents:rpc:spawn",
84
+ ({ type, prompt, options }) => {
85
+ const ctx = getCtx();
86
+ if (!ctx) throw new Error("No active session");
87
+
88
+ // Cross-extension RPC callers (e.g. pi-tasks TaskExecute) naturally
89
+ // forward serializable values, so options.model can be a string like
90
+ // "openai-codex/gpt-5.5". Resolve it to a real Model instance here
91
+ // — same pattern the scheduler path already uses — so the spawned
92
+ // agent's auth lookup doesn't crash with "No API key found for
93
+ // undefined".
94
+ let normalizedOptions = options ?? {};
95
+ if (typeof normalizedOptions.model === "string") {
96
+ const registry = (ctx as { modelRegistry?: ModelRegistry }).modelRegistry;
97
+ if (!registry) {
98
+ throw new Error(
99
+ `Model override "${normalizedOptions.model}" provided but ctx.modelRegistry is unavailable`,
100
+ );
101
+ }
102
+ const resolved = resolveModel(normalizedOptions.model, registry);
103
+ if (typeof resolved === "string") {
104
+ // resolveModel returns a human-readable error string when the
105
+ // input doesn't match any available model. Surface it instead of
106
+ // silently falling back so the caller sees the auth/typo issue.
107
+ throw new Error(resolved);
108
+ }
109
+ normalizedOptions = { ...normalizedOptions, model: resolved };
110
+ }
111
+
112
+ return { id: manager.spawn(pi, ctx, type, prompt, normalizedOptions) };
113
+ },
114
+ );
115
+
116
+ const unsubStop = handleRpc<{ requestId: string; agentId: string }>(events, "subagents:rpc:stop", ({ agentId }) => {
117
+ if (!manager.abort(agentId)) throw new Error("Agent not found");
118
+ });
119
+
120
+ return { unsubPing, unsubSpawn, unsubStop };
121
+ }
@@ -0,0 +1,140 @@
1
+ /**
2
+ * custom-agents.ts — Load user-defined agents from project (.pi/agents/) and global ($PI_CODING_AGENT_DIR/agents/, default ~/.pi/agent/agents/) locations.
3
+ */
4
+
5
+ import { existsSync, readdirSync, readFileSync } from "node:fs";
6
+ import { basename, join } from "node:path";
7
+ import { getAgentDir, parseFrontmatter } from "@earendil-works/pi-coding-agent";
8
+ import { BUILTIN_TOOL_NAMES } from "./agent-types.js";
9
+ import type { AgentConfig, MemoryScope, ThinkingLevel } from "./types.js";
10
+
11
+ /**
12
+ * Scan for custom agent .md files from multiple locations.
13
+ * Discovery hierarchy (higher priority wins):
14
+ * 1. Project: <cwd>/.pi/agents/*.md
15
+ * 2. Global: $PI_CODING_AGENT_DIR/agents/*.md (default: ~/.pi/agent/agents/*.md)
16
+ *
17
+ * Project-level agents override global ones with the same name.
18
+ * Any name is allowed — names matching defaults (e.g. "Explore") override them.
19
+ */
20
+ export function loadCustomAgents(cwd: string): Map<string, AgentConfig> {
21
+ const globalDir = join(getAgentDir(), "agents");
22
+ const projectDir = join(cwd, ".pi", "agents");
23
+
24
+ const agents = new Map<string, AgentConfig>();
25
+ loadFromDir(globalDir, agents, "global"); // lower priority
26
+ loadFromDir(projectDir, agents, "project"); // higher priority (overwrites)
27
+ return agents;
28
+ }
29
+
30
+ /** Load agent configs from a directory into the map. */
31
+ function loadFromDir(dir: string, agents: Map<string, AgentConfig>, source: "project" | "global"): void {
32
+ if (!existsSync(dir)) return;
33
+
34
+ let files: string[];
35
+ try {
36
+ files = readdirSync(dir).filter((f) => f.endsWith(".md"));
37
+ } catch {
38
+ return;
39
+ }
40
+
41
+ for (const file of files) {
42
+ const name = basename(file, ".md");
43
+
44
+ let content: string;
45
+ try {
46
+ content = readFileSync(join(dir, file), "utf-8");
47
+ } catch {
48
+ continue;
49
+ }
50
+
51
+ const { frontmatter: fm, body } = parseFrontmatter<Record<string, unknown>>(content);
52
+
53
+ agents.set(name, {
54
+ name,
55
+ displayName: str(fm.display_name),
56
+ description: str(fm.description) ?? name,
57
+ builtinToolNames: csvList(fm.tools, BUILTIN_TOOL_NAMES),
58
+ disallowedTools: csvListOptional(fm.disallowed_tools),
59
+ extensions: inheritField(fm.extensions ?? fm.inherit_extensions),
60
+ skills: inheritField(fm.skills ?? fm.inherit_skills),
61
+ model: str(fm.model),
62
+ fallbackModels: csvListOptional(fm.fallback_models),
63
+ thinking: str(fm.thinking) as ThinkingLevel | undefined,
64
+ maxTurns: nonNegativeInt(fm.max_turns),
65
+ systemPrompt: body.trim(),
66
+ promptMode: fm.prompt_mode === "append" ? "append" : "replace",
67
+ inheritContext: fm.inherit_context != null ? fm.inherit_context === true : undefined,
68
+ runInBackground: fm.run_in_background != null ? fm.run_in_background === true : undefined,
69
+ isolated: fm.isolated != null ? fm.isolated === true : undefined,
70
+ memory: parseMemory(fm.memory),
71
+ isolation: fm.isolation === "worktree" ? "worktree" : undefined,
72
+ enabled: fm.enabled !== false, // default true; explicitly false disables
73
+ source,
74
+ });
75
+ }
76
+ }
77
+
78
+ // ---- Field parsers ----
79
+ // All follow the same convention: omitted → default, "none"/empty → nothing, value → exact.
80
+
81
+ /** Extract a string or undefined. */
82
+ function str(val: unknown): string | undefined {
83
+ return typeof val === "string" ? val : undefined;
84
+ }
85
+
86
+ /** Extract a non-negative integer or undefined. 0 means unlimited for max_turns. */
87
+ function nonNegativeInt(val: unknown): number | undefined {
88
+ return typeof val === "number" && val >= 0 ? val : undefined;
89
+ }
90
+
91
+ /**
92
+ * Parse a raw CSV field value into items, or undefined if absent/empty/"none".
93
+ */
94
+ function parseCsvField(val: unknown): string[] | undefined {
95
+ if (val === undefined || val === null) return undefined;
96
+ const s = String(val).trim();
97
+ if (!s || s === "none") return undefined;
98
+ const items = s
99
+ .split(",")
100
+ .map((t) => t.trim())
101
+ .filter(Boolean);
102
+ return items.length > 0 ? items : undefined;
103
+ }
104
+
105
+ /**
106
+ * Parse a comma-separated list field with defaults.
107
+ * omitted → defaults; "none"/empty → []; csv → listed items.
108
+ */
109
+ function csvList(val: unknown, defaults: string[]): string[] {
110
+ if (val === undefined || val === null) return defaults;
111
+ return parseCsvField(val) ?? [];
112
+ }
113
+
114
+ /**
115
+ * Parse an optional comma-separated list field.
116
+ * omitted → undefined; "none"/empty → undefined; csv → listed items.
117
+ */
118
+ function csvListOptional(val: unknown): string[] | undefined {
119
+ return parseCsvField(val);
120
+ }
121
+
122
+ /**
123
+ * Parse a memory scope field.
124
+ * omitted → undefined; "user"/"project"/"local" → MemoryScope.
125
+ */
126
+ function parseMemory(val: unknown): MemoryScope | undefined {
127
+ if (val === "user" || val === "project" || val === "local") return val;
128
+ return undefined;
129
+ }
130
+
131
+ /**
132
+ * Parse an inherit field (extensions, skills).
133
+ * omitted/true → true (inherit all); false/"none"/empty → false; csv → listed names.
134
+ */
135
+ function inheritField(val: unknown): true | string[] | false {
136
+ if (val === undefined || val === null || val === true) return true;
137
+ if (val === false || val === "none") return false;
138
+ const items = csvList(val, []);
139
+ return items.length > 0 ? items : false;
140
+ }
@@ -0,0 +1,123 @@
1
+ /**
2
+ * default-agents.ts — Embedded default agent configurations.
3
+ *
4
+ * These are always available but can be overridden by user .md files with the same name.
5
+ */
6
+
7
+ import type { AgentConfig } from "./types.js";
8
+
9
+ const READ_ONLY_TOOLS = ["read", "bash", "grep", "find", "ls"];
10
+
11
+ export const DEFAULT_AGENTS: Map<string, AgentConfig> = new Map([
12
+ [
13
+ "general-purpose",
14
+ {
15
+ name: "general-purpose",
16
+ displayName: "Agent",
17
+ description: "General-purpose agent for complex, multi-step tasks",
18
+ // builtinToolNames omitted — means "all available tools" (resolved at lookup time)
19
+ // inheritContext / runInBackground / isolated omitted — strategy fields, callers decide per-call.
20
+ // Setting them to false would lock callsite intent (see resolveAgentInvocationConfig in invocation-config.ts).
21
+ extensions: true,
22
+ skills: true,
23
+ systemPrompt: "",
24
+ promptMode: "append",
25
+ isDefault: true,
26
+ },
27
+ ],
28
+ [
29
+ "Explore",
30
+ {
31
+ name: "Explore",
32
+ displayName: "Explore",
33
+ description: "Fast codebase exploration agent (read-only)",
34
+ builtinToolNames: READ_ONLY_TOOLS,
35
+ extensions: true,
36
+ skills: true,
37
+ model: "anthropic/claude-haiku-4-5-20251001",
38
+ systemPrompt: `# CRITICAL: READ-ONLY MODE - NO FILE MODIFICATIONS
39
+ You are a file search specialist. You excel at thoroughly navigating and exploring codebases.
40
+ Your role is EXCLUSIVELY to search and analyze existing code. You do NOT have access to file editing tools.
41
+
42
+ You are STRICTLY PROHIBITED from:
43
+ - Creating new files
44
+ - Modifying existing files
45
+ - Deleting files
46
+ - Moving or copying files
47
+ - Creating temporary files anywhere, including /tmp
48
+ - Using redirect operators (>, >>, |) or heredocs to write to files
49
+ - Running ANY commands that change system state
50
+
51
+ Use Bash ONLY for read-only operations: ls, git status, git log, git diff, find, cat, head, tail.
52
+
53
+ # Tool Usage
54
+ - Use the find tool for file pattern matching (NOT the bash find command)
55
+ - Use the grep tool for content search (NOT bash grep/rg command)
56
+ - Use the read tool for reading files (NOT bash cat/head/tail)
57
+ - Use Bash ONLY for read-only operations
58
+ - Make independent tool calls in parallel for efficiency
59
+ - Adapt search approach based on thoroughness level specified
60
+
61
+ # Output
62
+ - Use absolute file paths in all references
63
+ - Report findings as regular messages
64
+ - Do not use emojis
65
+ - Be thorough and precise`,
66
+ promptMode: "replace",
67
+ isDefault: true,
68
+ },
69
+ ],
70
+ [
71
+ "Plan",
72
+ {
73
+ name: "Plan",
74
+ displayName: "Plan",
75
+ description: "Software architect for implementation planning (read-only)",
76
+ builtinToolNames: READ_ONLY_TOOLS,
77
+ extensions: true,
78
+ skills: true,
79
+ systemPrompt: `# CRITICAL: READ-ONLY MODE - NO FILE MODIFICATIONS
80
+ You are a software architect and planning specialist.
81
+ Your role is EXCLUSIVELY to explore the codebase and design implementation plans.
82
+ You do NOT have access to file editing tools — attempting to edit files will fail.
83
+
84
+ You are STRICTLY PROHIBITED from:
85
+ - Creating new files
86
+ - Modifying existing files
87
+ - Deleting files
88
+ - Moving or copying files
89
+ - Creating temporary files anywhere, including /tmp
90
+ - Using redirect operators (>, >>, |) or heredocs to write to files
91
+ - Running ANY commands that change system state
92
+
93
+ # Planning Process
94
+ 1. Understand requirements
95
+ 2. Explore thoroughly (read files, find patterns, understand architecture)
96
+ 3. Design solution based on your assigned perspective
97
+ 4. Detail the plan with step-by-step implementation strategy
98
+
99
+ # Requirements
100
+ - Consider trade-offs and architectural decisions
101
+ - Identify dependencies and sequencing
102
+ - Anticipate potential challenges
103
+ - Follow existing patterns where appropriate
104
+
105
+ # Tool Usage
106
+ - Use the find tool for file pattern matching (NOT the bash find command)
107
+ - Use the grep tool for content search (NOT bash grep/rg command)
108
+ - Use the read tool for reading files (NOT bash cat/head/tail)
109
+ - Use Bash ONLY for read-only operations
110
+
111
+ # Output Format
112
+ - Use absolute file paths
113
+ - Do not use emojis
114
+ - End your response with:
115
+
116
+ ### Critical Files for Implementation
117
+ List 3-5 files most critical for implementing this plan:
118
+ - /absolute/path/to/file.ts - [Brief reason]`,
119
+ promptMode: "replace",
120
+ isDefault: true,
121
+ },
122
+ ],
123
+ ]);
package/src/env.ts ADDED
@@ -0,0 +1,33 @@
1
+ /**
2
+ * env.ts — Detect environment info (git, platform) for subagent system prompts.
3
+ */
4
+
5
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
6
+ import type { EnvInfo } from "./types.js";
7
+
8
+ export async function detectEnv(pi: ExtensionAPI, cwd: string): Promise<EnvInfo> {
9
+ let isGitRepo = false;
10
+ let branch = "";
11
+
12
+ try {
13
+ const result = await pi.exec("git", ["rev-parse", "--is-inside-work-tree"], { cwd, timeout: 5000 });
14
+ isGitRepo = result.code === 0 && result.stdout.trim() === "true";
15
+ } catch {
16
+ // Not a git repo or git not installed
17
+ }
18
+
19
+ if (isGitRepo) {
20
+ try {
21
+ const result = await pi.exec("git", ["branch", "--show-current"], { cwd, timeout: 5000 });
22
+ branch = result.code === 0 ? result.stdout.trim() : "unknown";
23
+ } catch {
24
+ branch = "unknown";
25
+ }
26
+ }
27
+
28
+ return {
29
+ isGitRepo,
30
+ branch,
31
+ platform: process.platform,
32
+ };
33
+ }
@@ -0,0 +1,141 @@
1
+ /**
2
+ * group-join.ts — Manages grouped background agent completion notifications.
3
+ *
4
+ * Instead of each agent individually nudging the main agent on completion,
5
+ * agents in a group are held until all complete (or a timeout fires),
6
+ * then a single consolidated notification is sent.
7
+ */
8
+
9
+ import type { AgentRecord } from "./types.js";
10
+
11
+ export type DeliveryCallback = (records: AgentRecord[], partial: boolean) => void;
12
+
13
+ interface AgentGroup {
14
+ groupId: string;
15
+ agentIds: Set<string>;
16
+ completedRecords: Map<string, AgentRecord>;
17
+ timeoutHandle?: ReturnType<typeof setTimeout>;
18
+ delivered: boolean;
19
+ /** Shorter timeout for stragglers after a partial delivery. */
20
+ isStraggler: boolean;
21
+ }
22
+
23
+ /** Default timeout: 30s after first completion in a group. */
24
+ const DEFAULT_TIMEOUT = 30_000;
25
+ /** Straggler re-batch timeout: 15s. */
26
+ const STRAGGLER_TIMEOUT = 15_000;
27
+
28
+ export class GroupJoinManager {
29
+ private groups = new Map<string, AgentGroup>();
30
+ private agentToGroup = new Map<string, string>();
31
+
32
+ constructor(
33
+ private deliverCb: DeliveryCallback,
34
+ private groupTimeout = DEFAULT_TIMEOUT,
35
+ ) {}
36
+
37
+ /** Register a group of agent IDs that should be joined. */
38
+ registerGroup(groupId: string, agentIds: string[]): void {
39
+ const group: AgentGroup = {
40
+ groupId,
41
+ agentIds: new Set(agentIds),
42
+ completedRecords: new Map(),
43
+ delivered: false,
44
+ isStraggler: false,
45
+ };
46
+ this.groups.set(groupId, group);
47
+ for (const id of agentIds) {
48
+ this.agentToGroup.set(id, groupId);
49
+ }
50
+ }
51
+
52
+ /**
53
+ * Called when an agent completes.
54
+ * Returns:
55
+ * - 'pass' — agent is not grouped, caller should send individual nudge
56
+ * - 'held' — result held, waiting for group completion
57
+ * - 'delivered' — this completion triggered the group notification
58
+ */
59
+ onAgentComplete(record: AgentRecord): "delivered" | "held" | "pass" {
60
+ const groupId = this.agentToGroup.get(record.id);
61
+ if (!groupId) return "pass";
62
+
63
+ const group = this.groups.get(groupId);
64
+ if (!group || group.delivered) return "pass";
65
+
66
+ group.completedRecords.set(record.id, record);
67
+
68
+ // All done — deliver immediately
69
+ if (group.completedRecords.size >= group.agentIds.size) {
70
+ this.deliver(group, false);
71
+ return "delivered";
72
+ }
73
+
74
+ // First completion in this batch — start timeout
75
+ if (!group.timeoutHandle) {
76
+ const timeout = group.isStraggler ? STRAGGLER_TIMEOUT : this.groupTimeout;
77
+ group.timeoutHandle = setTimeout(() => {
78
+ this.onTimeout(group);
79
+ }, timeout);
80
+ }
81
+
82
+ return "held";
83
+ }
84
+
85
+ private onTimeout(group: AgentGroup): void {
86
+ if (group.delivered) return;
87
+ group.timeoutHandle = undefined;
88
+
89
+ // Partial delivery — some agents still running
90
+ const remaining = new Set<string>();
91
+ for (const id of group.agentIds) {
92
+ if (!group.completedRecords.has(id)) remaining.add(id);
93
+ }
94
+
95
+ // Clean up agentToGroup for delivered agents (they won't complete again)
96
+ for (const id of group.completedRecords.keys()) {
97
+ this.agentToGroup.delete(id);
98
+ }
99
+
100
+ // Deliver what we have
101
+ this.deliverCb([...group.completedRecords.values()], true);
102
+
103
+ // Set up straggler group for remaining agents
104
+ group.completedRecords.clear();
105
+ group.agentIds = remaining;
106
+ group.isStraggler = true;
107
+ // Timeout will be started when the next straggler completes
108
+ }
109
+
110
+ private deliver(group: AgentGroup, partial: boolean): void {
111
+ if (group.timeoutHandle) {
112
+ clearTimeout(group.timeoutHandle);
113
+ group.timeoutHandle = undefined;
114
+ }
115
+ group.delivered = true;
116
+ this.deliverCb([...group.completedRecords.values()], partial);
117
+ this.cleanupGroup(group.groupId);
118
+ }
119
+
120
+ private cleanupGroup(groupId: string): void {
121
+ const group = this.groups.get(groupId);
122
+ if (!group) return;
123
+ for (const id of group.agentIds) {
124
+ this.agentToGroup.delete(id);
125
+ }
126
+ this.groups.delete(groupId);
127
+ }
128
+
129
+ /** Check if an agent is in a group. */
130
+ isGrouped(agentId: string): boolean {
131
+ return this.agentToGroup.has(agentId);
132
+ }
133
+
134
+ dispose(): void {
135
+ for (const group of this.groups.values()) {
136
+ if (group.timeoutHandle) clearTimeout(group.timeoutHandle);
137
+ }
138
+ this.groups.clear();
139
+ this.agentToGroup.clear();
140
+ }
141
+ }