@tintinweb/pi-subagents 0.4.0 → 0.4.1

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,41 @@
1
+ /**
2
+ * agent-types.ts — Unified agent type registry.
3
+ *
4
+ * Merges embedded default agents with user-defined agents from .pi/agents/*.md.
5
+ * User agents override defaults with the same name. Disabled agents are kept but excluded from spawning.
6
+ */
7
+ import type { AgentTool } from "@mariozechner/pi-agent-core";
8
+ import type { AgentConfig } from "./types.js";
9
+ /** All known built-in tool names, derived from the factory registry. */
10
+ export declare const BUILTIN_TOOL_NAMES: string[];
11
+ /**
12
+ * Register agents into the unified registry.
13
+ * Starts with DEFAULT_AGENTS, then overlays user agents (overrides defaults with same name).
14
+ * Disabled agents (enabled === false) are kept in the registry but excluded from spawning.
15
+ */
16
+ export declare function registerAgents(userAgents: Map<string, AgentConfig>): void;
17
+ /** Resolve a type name case-insensitively. Returns the canonical key or undefined. */
18
+ export declare function resolveType(name: string): string | undefined;
19
+ /** Get the agent config for a type (case-insensitive). */
20
+ export declare function getAgentConfig(name: string): AgentConfig | undefined;
21
+ /** Get all enabled type names (for spawning and tool descriptions). */
22
+ export declare function getAvailableTypes(): string[];
23
+ /** Get all type names including disabled (for UI listing). */
24
+ export declare function getAllTypes(): string[];
25
+ /** Get names of default agents currently in the registry. */
26
+ export declare function getDefaultAgentNames(): string[];
27
+ /** Get names of user-defined agents (non-defaults) currently in the registry. */
28
+ export declare function getUserAgentNames(): string[];
29
+ /** Check if a type is valid and enabled (case-insensitive). */
30
+ export declare function isValidType(type: string): boolean;
31
+ /** Get built-in tools for a type (case-insensitive). */
32
+ export declare function getToolsForType(type: string, cwd: string): AgentTool<any>[];
33
+ /** Get config for a type (case-insensitive, returns a SubagentTypeConfig-compatible object). Falls back to general-purpose. */
34
+ export declare function getConfig(type: string): {
35
+ displayName: string;
36
+ description: string;
37
+ builtinToolNames: string[];
38
+ extensions: true | string[] | false;
39
+ skills: true | string[] | false;
40
+ promptMode: "replace" | "append";
41
+ };
@@ -0,0 +1,130 @@
1
+ /**
2
+ * agent-types.ts — Unified agent type registry.
3
+ *
4
+ * Merges embedded default agents with user-defined agents from .pi/agents/*.md.
5
+ * User agents override defaults with the same name. Disabled agents are kept but excluded from spawning.
6
+ */
7
+ import { createReadTool, createBashTool, createEditTool, createWriteTool, createGrepTool, createFindTool, createLsTool, } from "@mariozechner/pi-coding-agent";
8
+ import { DEFAULT_AGENTS } from "./default-agents.js";
9
+ const TOOL_FACTORIES = {
10
+ read: (cwd) => createReadTool(cwd),
11
+ bash: (cwd) => createBashTool(cwd),
12
+ edit: (cwd) => createEditTool(cwd),
13
+ write: (cwd) => createWriteTool(cwd),
14
+ grep: (cwd) => createGrepTool(cwd),
15
+ find: (cwd) => createFindTool(cwd),
16
+ ls: (cwd) => createLsTool(cwd),
17
+ };
18
+ /** All known built-in tool names, derived from the factory registry. */
19
+ export const BUILTIN_TOOL_NAMES = Object.keys(TOOL_FACTORIES);
20
+ /** Unified runtime registry of all agents (defaults + user-defined). */
21
+ const agents = new Map();
22
+ /**
23
+ * Register agents into the unified registry.
24
+ * Starts with DEFAULT_AGENTS, then overlays user agents (overrides defaults with same name).
25
+ * Disabled agents (enabled === false) are kept in the registry but excluded from spawning.
26
+ */
27
+ export function registerAgents(userAgents) {
28
+ agents.clear();
29
+ // Start with defaults
30
+ for (const [name, config] of DEFAULT_AGENTS) {
31
+ agents.set(name, config);
32
+ }
33
+ // Overlay user agents (overrides defaults with same name)
34
+ for (const [name, config] of userAgents) {
35
+ agents.set(name, config);
36
+ }
37
+ }
38
+ /** Case-insensitive key resolution. */
39
+ function resolveKey(name) {
40
+ if (agents.has(name))
41
+ return name;
42
+ const lower = name.toLowerCase();
43
+ for (const key of agents.keys()) {
44
+ if (key.toLowerCase() === lower)
45
+ return key;
46
+ }
47
+ return undefined;
48
+ }
49
+ /** Resolve a type name case-insensitively. Returns the canonical key or undefined. */
50
+ export function resolveType(name) {
51
+ return resolveKey(name);
52
+ }
53
+ /** Get the agent config for a type (case-insensitive). */
54
+ export function getAgentConfig(name) {
55
+ const key = resolveKey(name);
56
+ return key ? agents.get(key) : undefined;
57
+ }
58
+ /** Get all enabled type names (for spawning and tool descriptions). */
59
+ export function getAvailableTypes() {
60
+ return [...agents.entries()]
61
+ .filter(([_, config]) => config.enabled !== false)
62
+ .map(([name]) => name);
63
+ }
64
+ /** Get all type names including disabled (for UI listing). */
65
+ export function getAllTypes() {
66
+ return [...agents.keys()];
67
+ }
68
+ /** Get names of default agents currently in the registry. */
69
+ export function getDefaultAgentNames() {
70
+ return [...agents.entries()]
71
+ .filter(([_, config]) => config.isDefault === true)
72
+ .map(([name]) => name);
73
+ }
74
+ /** Get names of user-defined agents (non-defaults) currently in the registry. */
75
+ export function getUserAgentNames() {
76
+ return [...agents.entries()]
77
+ .filter(([_, config]) => config.isDefault !== true)
78
+ .map(([name]) => name);
79
+ }
80
+ /** Check if a type is valid and enabled (case-insensitive). */
81
+ export function isValidType(type) {
82
+ const key = resolveKey(type);
83
+ if (!key)
84
+ return false;
85
+ return agents.get(key)?.enabled !== false;
86
+ }
87
+ /** Get built-in tools for a type (case-insensitive). */
88
+ export function getToolsForType(type, cwd) {
89
+ const key = resolveKey(type);
90
+ const raw = key ? agents.get(key) : undefined;
91
+ const config = raw?.enabled !== false ? raw : undefined;
92
+ const toolNames = config?.builtinToolNames?.length ? config.builtinToolNames : BUILTIN_TOOL_NAMES;
93
+ return toolNames.filter((n) => n in TOOL_FACTORIES).map((n) => TOOL_FACTORIES[n](cwd));
94
+ }
95
+ /** Get config for a type (case-insensitive, returns a SubagentTypeConfig-compatible object). Falls back to general-purpose. */
96
+ export function getConfig(type) {
97
+ const key = resolveKey(type);
98
+ const config = key ? agents.get(key) : undefined;
99
+ if (config && config.enabled !== false) {
100
+ return {
101
+ displayName: config.displayName ?? config.name,
102
+ description: config.description,
103
+ builtinToolNames: config.builtinToolNames ?? BUILTIN_TOOL_NAMES,
104
+ extensions: config.extensions,
105
+ skills: config.skills,
106
+ promptMode: config.promptMode,
107
+ };
108
+ }
109
+ // Fallback for unknown/disabled types — general-purpose config
110
+ const gp = agents.get("general-purpose");
111
+ if (gp && gp.enabled !== false) {
112
+ return {
113
+ displayName: gp.displayName ?? gp.name,
114
+ description: gp.description,
115
+ builtinToolNames: gp.builtinToolNames ?? BUILTIN_TOOL_NAMES,
116
+ extensions: gp.extensions,
117
+ skills: gp.skills,
118
+ promptMode: gp.promptMode,
119
+ };
120
+ }
121
+ // Absolute fallback (should never happen)
122
+ return {
123
+ displayName: "Agent",
124
+ description: "General-purpose agent for complex, multi-step tasks",
125
+ builtinToolNames: BUILTIN_TOOL_NAMES,
126
+ extensions: true,
127
+ skills: true,
128
+ promptMode: "append",
129
+ };
130
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * context.ts — Extract parent conversation context for subagent inheritance.
3
+ */
4
+ import type { ExtensionContext } from "@mariozechner/pi-coding-agent";
5
+ /** Extract text from a message content block array. */
6
+ export declare function extractText(content: unknown[]): string;
7
+ /**
8
+ * Build a text representation of the parent conversation context.
9
+ * Used when inherit_context is true to give the subagent visibility
10
+ * into what has been discussed/done so far.
11
+ */
12
+ export declare function buildParentContext(ctx: ExtensionContext): string;
@@ -0,0 +1,56 @@
1
+ /**
2
+ * context.ts — Extract parent conversation context for subagent inheritance.
3
+ */
4
+ /** Extract text from a message content block array. */
5
+ export function extractText(content) {
6
+ return content
7
+ .filter((c) => c.type === "text")
8
+ .map((c) => c.text ?? "")
9
+ .join("\n");
10
+ }
11
+ /**
12
+ * Build a text representation of the parent conversation context.
13
+ * Used when inherit_context is true to give the subagent visibility
14
+ * into what has been discussed/done so far.
15
+ */
16
+ export function buildParentContext(ctx) {
17
+ const entries = ctx.sessionManager.getBranch();
18
+ if (!entries || entries.length === 0)
19
+ return "";
20
+ const parts = [];
21
+ for (const entry of entries) {
22
+ if (entry.type === "message") {
23
+ const msg = entry.message;
24
+ if (msg.role === "user") {
25
+ const text = typeof msg.content === "string"
26
+ ? msg.content
27
+ : extractText(msg.content);
28
+ if (text.trim())
29
+ parts.push(`[User]: ${text.trim()}`);
30
+ }
31
+ else if (msg.role === "assistant") {
32
+ const text = extractText(msg.content);
33
+ if (text.trim())
34
+ parts.push(`[Assistant]: ${text.trim()}`);
35
+ }
36
+ // Skip toolResult messages — too verbose for context
37
+ }
38
+ else if (entry.type === "compaction") {
39
+ // Include compaction summaries — they're already condensed
40
+ if (entry.summary) {
41
+ parts.push(`[Summary]: ${entry.summary}`);
42
+ }
43
+ }
44
+ }
45
+ if (parts.length === 0)
46
+ return "";
47
+ return `# Parent Conversation Context
48
+ The following is the conversation history from the parent session that spawned you.
49
+ Use this context to understand what has been discussed and decided so far.
50
+
51
+ ${parts.join("\n\n")}
52
+
53
+ ---
54
+ # Your Task (below)
55
+ `;
56
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * custom-agents.ts — Load user-defined agents from project (.pi/agents/) and global (~/.pi/agent/agents/) locations.
3
+ */
4
+ import type { AgentConfig } from "./types.js";
5
+ /**
6
+ * Scan for custom agent .md files from multiple locations.
7
+ * Discovery hierarchy (higher priority wins):
8
+ * 1. Project: <cwd>/.pi/agents/*.md
9
+ * 2. Global: ~/.pi/agent/agents/*.md
10
+ *
11
+ * Project-level agents override global ones with the same name.
12
+ * Any name is allowed — names matching defaults (e.g. "Explore") override them.
13
+ */
14
+ export declare function loadCustomAgents(cwd: string): Map<string, AgentConfig>;
@@ -0,0 +1,100 @@
1
+ /**
2
+ * custom-agents.ts — Load user-defined agents from project (.pi/agents/) and global (~/.pi/agent/agents/) locations.
3
+ */
4
+ import { parseFrontmatter } from "@mariozechner/pi-coding-agent";
5
+ import { readFileSync, readdirSync, existsSync } from "node:fs";
6
+ import { join, basename } from "node:path";
7
+ import { homedir } from "node:os";
8
+ import { BUILTIN_TOOL_NAMES } from "./agent-types.js";
9
+ /**
10
+ * Scan for custom agent .md files from multiple locations.
11
+ * Discovery hierarchy (higher priority wins):
12
+ * 1. Project: <cwd>/.pi/agents/*.md
13
+ * 2. Global: ~/.pi/agent/agents/*.md
14
+ *
15
+ * Project-level agents override global ones with the same name.
16
+ * Any name is allowed — names matching defaults (e.g. "Explore") override them.
17
+ */
18
+ export function loadCustomAgents(cwd) {
19
+ const globalDir = join(homedir(), ".pi", "agent", "agents");
20
+ const projectDir = join(cwd, ".pi", "agents");
21
+ const agents = new Map();
22
+ loadFromDir(globalDir, agents, "global"); // lower priority
23
+ loadFromDir(projectDir, agents, "project"); // higher priority (overwrites)
24
+ return agents;
25
+ }
26
+ /** Load agent configs from a directory into the map. */
27
+ function loadFromDir(dir, agents, source) {
28
+ if (!existsSync(dir))
29
+ return;
30
+ let files;
31
+ try {
32
+ files = readdirSync(dir).filter(f => f.endsWith(".md"));
33
+ }
34
+ catch {
35
+ return;
36
+ }
37
+ for (const file of files) {
38
+ const name = basename(file, ".md");
39
+ let content;
40
+ try {
41
+ content = readFileSync(join(dir, file), "utf-8");
42
+ }
43
+ catch {
44
+ continue;
45
+ }
46
+ const { frontmatter: fm, body } = parseFrontmatter(content);
47
+ agents.set(name, {
48
+ name,
49
+ displayName: str(fm.display_name),
50
+ description: str(fm.description) ?? name,
51
+ builtinToolNames: csvList(fm.tools, BUILTIN_TOOL_NAMES),
52
+ extensions: inheritField(fm.extensions ?? fm.inherit_extensions),
53
+ skills: inheritField(fm.skills ?? fm.inherit_skills),
54
+ model: str(fm.model),
55
+ thinking: str(fm.thinking),
56
+ maxTurns: positiveInt(fm.max_turns),
57
+ systemPrompt: body.trim(),
58
+ promptMode: fm.prompt_mode === "append" ? "append" : "replace",
59
+ inheritContext: fm.inherit_context === true,
60
+ runInBackground: fm.run_in_background === true,
61
+ isolated: fm.isolated === true,
62
+ enabled: fm.enabled !== false, // default true; explicitly false disables
63
+ source,
64
+ });
65
+ }
66
+ }
67
+ // ---- Field parsers ----
68
+ // All follow the same convention: omitted → default, "none"/empty → nothing, value → exact.
69
+ /** Extract a string or undefined. */
70
+ function str(val) {
71
+ return typeof val === "string" ? val : undefined;
72
+ }
73
+ /** Extract a positive integer or undefined. */
74
+ function positiveInt(val) {
75
+ return typeof val === "number" && val >= 1 ? val : undefined;
76
+ }
77
+ /**
78
+ * Parse a comma-separated list field.
79
+ * omitted → defaults; "none"/empty → []; csv → listed items.
80
+ */
81
+ function csvList(val, defaults) {
82
+ if (val === undefined || val === null)
83
+ return defaults;
84
+ const s = String(val).trim();
85
+ if (!s || s === "none")
86
+ return [];
87
+ return s.split(",").map(t => t.trim()).filter(Boolean);
88
+ }
89
+ /**
90
+ * Parse an inherit field (extensions, skills).
91
+ * omitted/true → true (inherit all); false/"none"/empty → false; csv → listed names.
92
+ */
93
+ function inheritField(val) {
94
+ if (val === undefined || val === null || val === true)
95
+ return true;
96
+ if (val === false || val === "none")
97
+ return false;
98
+ const items = csvList(val, []);
99
+ return items.length > 0 ? items : false;
100
+ }
@@ -0,0 +1,7 @@
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
+ import type { AgentConfig } from "./types.js";
7
+ export declare const DEFAULT_AGENTS: Map<string, AgentConfig>;
@@ -0,0 +1,126 @@
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
+ const READ_ONLY_TOOLS = ["read", "bash", "grep", "find", "ls"];
7
+ export const DEFAULT_AGENTS = new Map([
8
+ [
9
+ "general-purpose",
10
+ {
11
+ name: "general-purpose",
12
+ displayName: "Agent",
13
+ description: "General-purpose agent for complex, multi-step tasks",
14
+ // builtinToolNames omitted — means "all available tools" (resolved at lookup time)
15
+ extensions: true,
16
+ skills: true,
17
+ systemPrompt: "",
18
+ promptMode: "append",
19
+ inheritContext: false,
20
+ runInBackground: false,
21
+ isolated: false,
22
+ isDefault: true,
23
+ },
24
+ ],
25
+ [
26
+ "Explore",
27
+ {
28
+ name: "Explore",
29
+ displayName: "Explore",
30
+ description: "Fast codebase exploration agent (read-only)",
31
+ builtinToolNames: READ_ONLY_TOOLS,
32
+ extensions: true,
33
+ skills: true,
34
+ model: "anthropic/claude-haiku-4-5-20251001",
35
+ systemPrompt: `# CRITICAL: READ-ONLY MODE - NO FILE MODIFICATIONS
36
+ You are a file search specialist. You excel at thoroughly navigating and exploring codebases.
37
+ Your role is EXCLUSIVELY to search and analyze existing code. You do NOT have access to file editing tools.
38
+
39
+ You are STRICTLY PROHIBITED from:
40
+ - Creating new files
41
+ - Modifying existing files
42
+ - Deleting files
43
+ - Moving or copying files
44
+ - Creating temporary files anywhere, including /tmp
45
+ - Using redirect operators (>, >>, |) or heredocs to write to files
46
+ - Running ANY commands that change system state
47
+
48
+ Use Bash ONLY for read-only operations: ls, git status, git log, git diff, find, cat, head, tail.
49
+
50
+ # Tool Usage
51
+ - Use the find tool for file pattern matching (NOT the bash find command)
52
+ - Use the grep tool for content search (NOT bash grep/rg command)
53
+ - Use the read tool for reading files (NOT bash cat/head/tail)
54
+ - Use Bash ONLY for read-only operations
55
+ - Make independent tool calls in parallel for efficiency
56
+ - Adapt search approach based on thoroughness level specified
57
+
58
+ # Output
59
+ - Use absolute file paths in all references
60
+ - Report findings as regular messages
61
+ - Do not use emojis
62
+ - Be thorough and precise`,
63
+ promptMode: "replace",
64
+ inheritContext: false,
65
+ runInBackground: false,
66
+ isolated: false,
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
+ inheritContext: false,
121
+ runInBackground: false,
122
+ isolated: false,
123
+ isDefault: true,
124
+ },
125
+ ],
126
+ ]);
package/dist/env.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ /**
2
+ * env.ts — Detect environment info (git, platform) for subagent system prompts.
3
+ */
4
+ import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
5
+ import type { EnvInfo } from "./types.js";
6
+ export declare function detectEnv(pi: ExtensionAPI, cwd: string): Promise<EnvInfo>;
package/dist/env.js ADDED
@@ -0,0 +1,28 @@
1
+ /**
2
+ * env.ts — Detect environment info (git, platform) for subagent system prompts.
3
+ */
4
+ export async function detectEnv(pi, cwd) {
5
+ let isGitRepo = false;
6
+ let branch = "";
7
+ try {
8
+ const result = await pi.exec("git", ["rev-parse", "--is-inside-work-tree"], { cwd, timeout: 5000 });
9
+ isGitRepo = result.code === 0 && result.stdout.trim() === "true";
10
+ }
11
+ catch {
12
+ // Not a git repo or git not installed
13
+ }
14
+ if (isGitRepo) {
15
+ try {
16
+ const result = await pi.exec("git", ["branch", "--show-current"], { cwd, timeout: 5000 });
17
+ branch = result.code === 0 ? result.stdout.trim() : "unknown";
18
+ }
19
+ catch {
20
+ branch = "unknown";
21
+ }
22
+ }
23
+ return {
24
+ isGitRepo,
25
+ branch,
26
+ platform: process.platform,
27
+ };
28
+ }
@@ -0,0 +1,32 @@
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
+ import type { AgentRecord } from "./types.js";
9
+ export type DeliveryCallback = (records: AgentRecord[], partial: boolean) => void;
10
+ export declare class GroupJoinManager {
11
+ private deliverCb;
12
+ private groupTimeout;
13
+ private groups;
14
+ private agentToGroup;
15
+ constructor(deliverCb: DeliveryCallback, groupTimeout?: number);
16
+ /** Register a group of agent IDs that should be joined. */
17
+ registerGroup(groupId: string, agentIds: string[]): void;
18
+ /**
19
+ * Called when an agent completes.
20
+ * Returns:
21
+ * - 'pass' — agent is not grouped, caller should send individual nudge
22
+ * - 'held' — result held, waiting for group completion
23
+ * - 'delivered' — this completion triggered the group notification
24
+ */
25
+ onAgentComplete(record: AgentRecord): 'delivered' | 'held' | 'pass';
26
+ private onTimeout;
27
+ private deliver;
28
+ private cleanupGroup;
29
+ /** Check if an agent is in a group. */
30
+ isGrouped(agentId: string): boolean;
31
+ dispose(): void;
32
+ }
@@ -0,0 +1,116 @@
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
+ /** Default timeout: 30s after first completion in a group. */
9
+ const DEFAULT_TIMEOUT = 30_000;
10
+ /** Straggler re-batch timeout: 15s. */
11
+ const STRAGGLER_TIMEOUT = 15_000;
12
+ export class GroupJoinManager {
13
+ deliverCb;
14
+ groupTimeout;
15
+ groups = new Map();
16
+ agentToGroup = new Map();
17
+ constructor(deliverCb, groupTimeout = DEFAULT_TIMEOUT) {
18
+ this.deliverCb = deliverCb;
19
+ this.groupTimeout = groupTimeout;
20
+ }
21
+ /** Register a group of agent IDs that should be joined. */
22
+ registerGroup(groupId, agentIds) {
23
+ const group = {
24
+ groupId,
25
+ agentIds: new Set(agentIds),
26
+ completedRecords: new Map(),
27
+ delivered: false,
28
+ isStraggler: false,
29
+ };
30
+ this.groups.set(groupId, group);
31
+ for (const id of agentIds) {
32
+ this.agentToGroup.set(id, groupId);
33
+ }
34
+ }
35
+ /**
36
+ * Called when an agent completes.
37
+ * Returns:
38
+ * - 'pass' — agent is not grouped, caller should send individual nudge
39
+ * - 'held' — result held, waiting for group completion
40
+ * - 'delivered' — this completion triggered the group notification
41
+ */
42
+ onAgentComplete(record) {
43
+ const groupId = this.agentToGroup.get(record.id);
44
+ if (!groupId)
45
+ return 'pass';
46
+ const group = this.groups.get(groupId);
47
+ if (!group || group.delivered)
48
+ return 'pass';
49
+ group.completedRecords.set(record.id, record);
50
+ // All done — deliver immediately
51
+ if (group.completedRecords.size >= group.agentIds.size) {
52
+ this.deliver(group, false);
53
+ return 'delivered';
54
+ }
55
+ // First completion in this batch — start timeout
56
+ if (!group.timeoutHandle) {
57
+ const timeout = group.isStraggler ? STRAGGLER_TIMEOUT : this.groupTimeout;
58
+ group.timeoutHandle = setTimeout(() => {
59
+ this.onTimeout(group);
60
+ }, timeout);
61
+ }
62
+ return 'held';
63
+ }
64
+ onTimeout(group) {
65
+ if (group.delivered)
66
+ return;
67
+ group.timeoutHandle = undefined;
68
+ // Partial delivery — some agents still running
69
+ const remaining = new Set();
70
+ for (const id of group.agentIds) {
71
+ if (!group.completedRecords.has(id))
72
+ remaining.add(id);
73
+ }
74
+ // Clean up agentToGroup for delivered agents (they won't complete again)
75
+ for (const id of group.completedRecords.keys()) {
76
+ this.agentToGroup.delete(id);
77
+ }
78
+ // Deliver what we have
79
+ this.deliverCb([...group.completedRecords.values()], true);
80
+ // Set up straggler group for remaining agents
81
+ group.completedRecords.clear();
82
+ group.agentIds = remaining;
83
+ group.isStraggler = true;
84
+ // Timeout will be started when the next straggler completes
85
+ }
86
+ deliver(group, partial) {
87
+ if (group.timeoutHandle) {
88
+ clearTimeout(group.timeoutHandle);
89
+ group.timeoutHandle = undefined;
90
+ }
91
+ group.delivered = true;
92
+ this.deliverCb([...group.completedRecords.values()], partial);
93
+ this.cleanupGroup(group.groupId);
94
+ }
95
+ cleanupGroup(groupId) {
96
+ const group = this.groups.get(groupId);
97
+ if (!group)
98
+ return;
99
+ for (const id of group.agentIds) {
100
+ this.agentToGroup.delete(id);
101
+ }
102
+ this.groups.delete(groupId);
103
+ }
104
+ /** Check if an agent is in a group. */
105
+ isGrouped(agentId) {
106
+ return this.agentToGroup.has(agentId);
107
+ }
108
+ dispose() {
109
+ for (const group of this.groups.values()) {
110
+ if (group.timeoutHandle)
111
+ clearTimeout(group.timeoutHandle);
112
+ }
113
+ this.groups.clear();
114
+ this.agentToGroup.clear();
115
+ }
116
+ }