@clanker-code/pi-subagents 0.10.5

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.
Files changed (130) hide show
  1. package/.plans/PLAN-next-changes.md +183 -0
  2. package/.plans/README.md +14 -0
  3. package/AGENTS.md +31 -0
  4. package/CHANGELOG.md +583 -0
  5. package/CLAUDE.md +1 -0
  6. package/LICENSE +21 -0
  7. package/README.md +630 -0
  8. package/RELEASE.md +39 -0
  9. package/dist/abort-resend.d.ts +35 -0
  10. package/dist/abort-resend.js +71 -0
  11. package/dist/agent-details.d.ts +17 -0
  12. package/dist/agent-details.js +22 -0
  13. package/dist/agent-manager.d.ts +132 -0
  14. package/dist/agent-manager.js +493 -0
  15. package/dist/agent-runner.d.ts +165 -0
  16. package/dist/agent-runner.js +732 -0
  17. package/dist/agent-tool-description.d.ts +9 -0
  18. package/dist/agent-tool-description.js +147 -0
  19. package/dist/agent-types.d.ts +60 -0
  20. package/dist/agent-types.js +157 -0
  21. package/dist/context.d.ts +12 -0
  22. package/dist/context.js +56 -0
  23. package/dist/cross-extension-rpc.d.ts +46 -0
  24. package/dist/cross-extension-rpc.js +76 -0
  25. package/dist/custom-agents.d.ts +14 -0
  26. package/dist/custom-agents.js +149 -0
  27. package/dist/default-agents.d.ts +7 -0
  28. package/dist/default-agents.js +119 -0
  29. package/dist/enabled-models.d.ts +49 -0
  30. package/dist/enabled-models.js +145 -0
  31. package/dist/env.d.ts +6 -0
  32. package/dist/env.js +28 -0
  33. package/dist/group-join.d.ts +32 -0
  34. package/dist/group-join.js +116 -0
  35. package/dist/index.d.ts +36 -0
  36. package/dist/index.js +1918 -0
  37. package/dist/invocation-config.d.ts +25 -0
  38. package/dist/invocation-config.js +19 -0
  39. package/dist/memory.d.ts +49 -0
  40. package/dist/memory.js +151 -0
  41. package/dist/model-resolver.d.ts +19 -0
  42. package/dist/model-resolver.js +62 -0
  43. package/dist/notifications.d.ts +6 -0
  44. package/dist/notifications.js +107 -0
  45. package/dist/output-file.d.ts +24 -0
  46. package/dist/output-file.js +86 -0
  47. package/dist/peek.d.ts +37 -0
  48. package/dist/peek.js +121 -0
  49. package/dist/prompts.d.ts +40 -0
  50. package/dist/prompts.js +95 -0
  51. package/dist/schedule-store.d.ts +38 -0
  52. package/dist/schedule-store.js +155 -0
  53. package/dist/schedule.d.ts +109 -0
  54. package/dist/schedule.js +338 -0
  55. package/dist/settings.d.ts +135 -0
  56. package/dist/settings.js +168 -0
  57. package/dist/skill-loader.d.ts +24 -0
  58. package/dist/skill-loader.js +93 -0
  59. package/dist/status-note.d.ts +13 -0
  60. package/dist/status-note.js +24 -0
  61. package/dist/types.d.ts +184 -0
  62. package/dist/types.js +7 -0
  63. package/dist/ui/agent-tool-rendering.d.ts +34 -0
  64. package/dist/ui/agent-tool-rendering.js +154 -0
  65. package/dist/ui/agent-widget-tree.d.ts +33 -0
  66. package/dist/ui/agent-widget-tree.js +130 -0
  67. package/dist/ui/agent-widget.d.ts +156 -0
  68. package/dist/ui/agent-widget.js +408 -0
  69. package/dist/ui/conversation-viewer.d.ts +47 -0
  70. package/dist/ui/conversation-viewer.js +290 -0
  71. package/dist/ui/menu-select.d.ts +20 -0
  72. package/dist/ui/menu-select.js +46 -0
  73. package/dist/ui/schedule-menu.d.ts +16 -0
  74. package/dist/ui/schedule-menu.js +99 -0
  75. package/dist/ui/viewer-keys.d.ts +20 -0
  76. package/dist/ui/viewer-keys.js +17 -0
  77. package/dist/usage.d.ts +50 -0
  78. package/dist/usage.js +49 -0
  79. package/dist/wait.d.ts +10 -0
  80. package/dist/wait.js +37 -0
  81. package/dist/worktree.d.ts +45 -0
  82. package/dist/worktree.js +160 -0
  83. package/docs/design/default-extension-tool-exposure.md +56 -0
  84. package/docs/superpowers/plans/2026-06-19-recursive-subagent-widget.md +600 -0
  85. package/docs/superpowers/specs/2026-06-19-recursive-subagent-widget-design.md +189 -0
  86. package/examples/agent-tool-description.md +45 -0
  87. package/package.json +56 -0
  88. package/reviews/proposal-structured-output-schema.md +135 -0
  89. package/reviews/recursive-subagent-widget-preview-rev2.png +0 -0
  90. package/reviews/recursive-subagent-widget-preview.html +137 -0
  91. package/reviews/recursive-subagent-widget-preview.png +0 -0
  92. package/reviews/subagent-features-comparison.md +350 -0
  93. package/src/abort-resend.ts +75 -0
  94. package/src/agent-details.ts +31 -0
  95. package/src/agent-manager.ts +596 -0
  96. package/src/agent-runner.ts +872 -0
  97. package/src/agent-tool-description.ts +163 -0
  98. package/src/agent-types.ts +189 -0
  99. package/src/context.ts +58 -0
  100. package/src/cross-extension-rpc.ts +122 -0
  101. package/src/custom-agents.ts +160 -0
  102. package/src/default-agents.ts +123 -0
  103. package/src/enabled-models.ts +180 -0
  104. package/src/env.ts +33 -0
  105. package/src/group-join.ts +141 -0
  106. package/src/index.ts +2115 -0
  107. package/src/invocation-config.ts +42 -0
  108. package/src/memory.ts +165 -0
  109. package/src/model-resolver.ts +81 -0
  110. package/src/notifications.ts +120 -0
  111. package/src/output-file.ts +96 -0
  112. package/src/peek.ts +155 -0
  113. package/src/prompts.ts +129 -0
  114. package/src/schedule-store.ts +153 -0
  115. package/src/schedule.ts +365 -0
  116. package/src/settings.ts +289 -0
  117. package/src/skill-loader.ts +102 -0
  118. package/src/status-note.ts +25 -0
  119. package/src/types.ts +195 -0
  120. package/src/ui/agent-tool-rendering.ts +175 -0
  121. package/src/ui/agent-widget-tree.ts +169 -0
  122. package/src/ui/agent-widget.ts +497 -0
  123. package/src/ui/conversation-viewer.ts +297 -0
  124. package/src/ui/menu-select.ts +68 -0
  125. package/src/ui/schedule-menu.ts +105 -0
  126. package/src/ui/viewer-keys.ts +39 -0
  127. package/src/usage.ts +60 -0
  128. package/src/wait.ts +44 -0
  129. package/src/worktree.ts +191 -0
  130. package/vitest.config.ts +25 -0
package/dist/peek.js ADDED
@@ -0,0 +1,121 @@
1
+ /**
2
+ * peek.ts — Lightweight tail/filter view of an agent's result or streaming
3
+ * output file for `get_subagent_result`'s `peek` parameter.
4
+ *
5
+ * Design:
6
+ * - Source precedence: streaming output file (best for running agents) → record
7
+ * result (finished agents) → "no output yet".
8
+ * - The output file is JSONL (one entry per message). We extract human-readable
9
+ * text lines (assistant text + tool-result text) so a peek shows useful
10
+ * progress, not raw JSON.
11
+ * - Semantics: filter-then-tail. If `regex` is given, only matching source lines
12
+ * are kept; then `after` (return all lines past an index) or `lines` (last N)
13
+ * is applied. Line numbers always refer to the FULL source so callers can use
14
+ * `after` for incremental updates without missing anything.
15
+ */
16
+ import { existsSync, readFileSync } from "node:fs";
17
+ /** Default number of tail lines when neither `after` nor `lines` is given. */
18
+ const DEFAULT_LINES = 20;
19
+ /**
20
+ * Produce a peek view of an agent's output. Returns null when there is no
21
+ * source content at all (the caller renders a "no output yet" message).
22
+ */
23
+ export function peekAgentOutput(record, opts = {}) {
24
+ const lines = readSourceLines(record);
25
+ if (lines.length === 0)
26
+ return null;
27
+ const regex = opts.regex ? compileRegex(opts.regex) : undefined;
28
+ const after = typeof opts.after === "number" ? opts.after : -1;
29
+ const tail = typeof opts.lines === "number" && opts.lines >= 1 ? opts.lines : DEFAULT_LINES;
30
+ // Index each source line with its original position (1-based for display).
31
+ const indexed = lines.map((text, i) => ({ no: i + 1, text }));
32
+ // Filter-then-select.
33
+ const filtered = regex ? indexed.filter((l) => regex.test(l.text)) : indexed;
34
+ const selected = after >= 0
35
+ ? filtered.filter((l) => l.no > after)
36
+ : filtered.slice(-tail);
37
+ const totalLines = lines.length;
38
+ const isRunning = record.status === "running" || record.status === "queued";
39
+ const source = isRunning && record.outputFile && existsSync(record.outputFile) ? "outputFile" : "result";
40
+ const header = buildHeader(opts, selected.length, totalLines, source);
41
+ const body = selected.map((l) => `[${l.no}] ${l.text}`).join("\n");
42
+ return { text: `${header}\n\n${body}`, totalLines, source };
43
+ }
44
+ /** Read the most useful text lines from the agent's output. */
45
+ function readSourceLines(record) {
46
+ const isRunning = record.status === "running" || record.status === "queued";
47
+ const outputFileLines = record.outputFile && existsSync(record.outputFile) ? parseOutputFileLines(record.outputFile) : [];
48
+ // While running, the live output file is the only source of progress.
49
+ if (isRunning && outputFileLines.length > 0)
50
+ return outputFileLines;
51
+ // Finished (or no live file): prefer the clean result text.
52
+ if (record.result?.trim()) {
53
+ return record.result.split("\n");
54
+ }
55
+ // Last resort: the output file (e.g. agent errored before producing a result).
56
+ return outputFileLines;
57
+ }
58
+ /**
59
+ * Parse the JSONL output file and extract human-readable text lines.
60
+ * Each entry has `{ type, message: { role, content } }`. We pull assistant text
61
+ * and tool-result text so a peek reflects actual progress.
62
+ */
63
+ function parseOutputFileLines(path) {
64
+ let raw;
65
+ try {
66
+ raw = readFileSync(path, "utf-8");
67
+ }
68
+ catch {
69
+ return [];
70
+ }
71
+ const out = [];
72
+ for (const line of raw.split("\n")) {
73
+ const trimmed = line.trim();
74
+ if (!trimmed)
75
+ continue;
76
+ let entry;
77
+ try {
78
+ entry = JSON.parse(trimmed);
79
+ }
80
+ catch {
81
+ continue;
82
+ }
83
+ const content = entry?.message?.content;
84
+ if (!Array.isArray(content)) {
85
+ // Some entries may carry a plain string content.
86
+ if (typeof content === "string" && content.trim())
87
+ out.push(content.trim());
88
+ continue;
89
+ }
90
+ for (const block of content) {
91
+ if (block?.type === "text" && typeof block.text === "string" && block.text.trim()) {
92
+ out.push(block.text.trimEnd());
93
+ }
94
+ }
95
+ }
96
+ return out;
97
+ }
98
+ function compileRegex(pattern) {
99
+ // Anchor-free; case-sensitive by default. Invalid patterns fall back to a
100
+ // substring literal match so a bad regex never throws into the tool result.
101
+ try {
102
+ return new RegExp(pattern);
103
+ }
104
+ catch {
105
+ return new RegExp(pattern.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
106
+ }
107
+ }
108
+ function buildHeader(opts, shown, total, source) {
109
+ const parts = [];
110
+ if (typeof opts.after === "number") {
111
+ parts.push(`after line number ${opts.after}`);
112
+ }
113
+ else {
114
+ const n = typeof opts.lines === "number" && opts.lines >= 1 ? opts.lines : DEFAULT_LINES;
115
+ parts.push(`last ${n} lines`);
116
+ }
117
+ if (opts.regex)
118
+ parts.push(`filtered by regex /${opts.regex}/`);
119
+ parts.push(`of ${total} total (${source === "outputFile" ? "live output file" : "result"})`);
120
+ return `Showing ${shown} ${parts.join(", ")}. Line numbers index the full source.`;
121
+ }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * prompts.ts — System prompt builder for agents.
3
+ */
4
+ import type { AgentConfig, EnvInfo } from "./types.js";
5
+ /** Extra sections to inject into the system prompt (memory, skills, etc.). */
6
+ export interface PromptExtras {
7
+ /** Persistent memory content to inject (first 200 lines of MEMORY.md + instructions). */
8
+ memoryBlock?: string;
9
+ /** Preloaded skill contents to inject. */
10
+ skillBlocks?: {
11
+ name: string;
12
+ content: string;
13
+ }[];
14
+ /** Manager-assigned agent id, used for recursive subagent metadata. */
15
+ agentId?: string;
16
+ /** Parent subagent id when this agent was spawned recursively. */
17
+ parentAgentId?: string;
18
+ /** Recursive subagent depth. */
19
+ depth?: number;
20
+ /** Maximum recursive subagent depth. */
21
+ maxDepth?: number;
22
+ }
23
+ /**
24
+ * Build the system prompt for an agent from its config.
25
+ *
26
+ * - "replace" mode: env header + config.systemPrompt (full control, no parent identity)
27
+ * - "append" mode: parent system prompt + sub-agent context + env header + config.systemPrompt
28
+ * - "append" with empty systemPrompt: pure parent clone
29
+ *
30
+ * Both modes include an `<active_agent name="${config.name}"/>` tag so downstream
31
+ * extensions (e.g. permission/policy systems) can resolve per-agent policy
32
+ * inside the child session by parsing the system prompt. In replace mode the tag
33
+ * is prepended; in append mode it follows the shared inherited content so the
34
+ * parent prompt forms an identical, cacheable byte prefix with the parent
35
+ * session (the LLM's KV cache can then reuse those tokens across every spawn).
36
+ *
37
+ * @param parentSystemPrompt The parent agent's effective system prompt (for append mode).
38
+ * @param extras Optional extra sections to inject (memory, preloaded skills).
39
+ */
40
+ export declare function buildAgentPrompt(config: AgentConfig, cwd: string, env: EnvInfo, parentSystemPrompt?: string, extras?: PromptExtras): string;
@@ -0,0 +1,95 @@
1
+ /**
2
+ * prompts.ts — System prompt builder for agents.
3
+ */
4
+ function xmlAttr(value) {
5
+ if (value == null)
6
+ return undefined;
7
+ return String(value)
8
+ .replace(/&/g, "&amp;")
9
+ .replace(/"/g, "&quot;")
10
+ .replace(/</g, "&lt;")
11
+ .replace(/>/g, "&gt;");
12
+ }
13
+ /**
14
+ * Build the system prompt for an agent from its config.
15
+ *
16
+ * - "replace" mode: env header + config.systemPrompt (full control, no parent identity)
17
+ * - "append" mode: parent system prompt + sub-agent context + env header + config.systemPrompt
18
+ * - "append" with empty systemPrompt: pure parent clone
19
+ *
20
+ * Both modes include an `<active_agent name="${config.name}"/>` tag so downstream
21
+ * extensions (e.g. permission/policy systems) can resolve per-agent policy
22
+ * inside the child session by parsing the system prompt. In replace mode the tag
23
+ * is prepended; in append mode it follows the shared inherited content so the
24
+ * parent prompt forms an identical, cacheable byte prefix with the parent
25
+ * session (the LLM's KV cache can then reuse those tokens across every spawn).
26
+ *
27
+ * @param parentSystemPrompt The parent agent's effective system prompt (for append mode).
28
+ * @param extras Optional extra sections to inject (memory, preloaded skills).
29
+ */
30
+ export function buildAgentPrompt(config, cwd, env, parentSystemPrompt, extras) {
31
+ const activeAgentAttrs = [
32
+ ["name", config.name],
33
+ ["agent_id", extras?.agentId],
34
+ ["parent_agent_id", extras?.parentAgentId],
35
+ ["depth", extras?.depth],
36
+ ["max_depth", extras?.maxDepth],
37
+ ]
38
+ .map(([name, value]) => {
39
+ const escaped = xmlAttr(value);
40
+ return escaped == null ? undefined : `${name}="${escaped}"`;
41
+ })
42
+ .filter(Boolean)
43
+ .join(" ");
44
+ const activeAgentTag = `<active_agent ${activeAgentAttrs}/>\n\n`;
45
+ const envBlock = `# Environment
46
+ Working directory: ${cwd}
47
+ ${env.isGitRepo ? `Git repository: yes\nBranch: ${env.branch}` : "Not a git repository"}
48
+ Platform: ${env.platform}`;
49
+ // Build optional extras suffix
50
+ const extraSections = [];
51
+ if (extras?.memoryBlock) {
52
+ extraSections.push(extras.memoryBlock);
53
+ }
54
+ if (extras?.skillBlocks?.length) {
55
+ for (const skill of extras.skillBlocks) {
56
+ extraSections.push(`\n# Preloaded Skill: ${skill.name}\n${skill.content}`);
57
+ }
58
+ }
59
+ const extrasSuffix = extraSections.length > 0 ? "\n\n" + extraSections.join("\n") : "";
60
+ if (config.promptMode === "append") {
61
+ const identity = parentSystemPrompt || genericBase;
62
+ const bridge = `<sub_agent_context>
63
+ You are operating as a sub-agent invoked to handle a specific task.
64
+ - Use the read tool instead of cat/head/tail
65
+ - Use the edit tool instead of sed/awk
66
+ - Use the write tool instead of echo/heredoc
67
+ - Use the find tool instead of bash find/ls for file search
68
+ - Use the grep tool instead of bash grep/rg for content search
69
+ - Make independent tool calls in parallel
70
+ - Use absolute file paths
71
+ - Do not use emojis
72
+ - Be concise but complete
73
+ </sub_agent_context>`;
74
+ const customSection = config.systemPrompt?.trim()
75
+ ? `\n\n<agent_instructions>\n${config.systemPrompt}\n</agent_instructions>`
76
+ : "";
77
+ // Place shared/stable content first so the LLM's KV cache can reuse the
78
+ // inherited prefix across all subagent invocations. The parent prompt is
79
+ // placed verbatim (no wrapper tag) so it forms an identical byte prefix
80
+ // with the parent session, maximising KV cache hits. The <active_agent>
81
+ // tag and env block vary per call and are placed after the cached prefix.
82
+ return identity + "\n\n" + bridge + "\n\n" + activeAgentTag + envBlock + customSection + extrasSuffix;
83
+ }
84
+ // "replace" mode — env header + the config's full system prompt
85
+ const replaceHeader = `You are a pi coding agent sub-agent.
86
+ You have been invoked to handle a specific task autonomously.
87
+
88
+ ${envBlock}`;
89
+ return activeAgentTag + replaceHeader + "\n\n" + config.systemPrompt + extrasSuffix;
90
+ }
91
+ /** Fallback base prompt when parent system prompt is unavailable in append mode. */
92
+ const genericBase = `# Role
93
+ You are a general-purpose coding agent for complex, multi-step tasks.
94
+ You have full access to read, write, edit files, and execute commands.
95
+ Do what has been asked; nothing more, nothing less.`;
@@ -0,0 +1,38 @@
1
+ /**
2
+ * schedule-store.ts — File-backed store for scheduled subagents.
3
+ *
4
+ * Session-scoped: each pi session owns its own schedules at
5
+ * `<cwd>/.pi/subagent-schedules/<sessionId>.json`. `/new` starts a fresh
6
+ * empty store; `/resume` reloads.
7
+ *
8
+ * Concurrency model lifted from pi-chonky-tasks/src/task-store.ts: every
9
+ * mutation acquires a PID-based exclusion lock, re-reads the latest state
10
+ * from disk, applies the change, atomic-writes via temp+rename, releases.
11
+ */
12
+ import type { ScheduledSubagent } from "./types.js";
13
+ /** Resolve the storage path for a session-scoped store. */
14
+ export declare function resolveStorePath(cwd: string, sessionId: string): string;
15
+ export declare class ScheduleStore {
16
+ private filePath;
17
+ private lockPath;
18
+ private jobs;
19
+ constructor(filePath: string);
20
+ /** Create the backing directory lazily — only when we're about to persist. */
21
+ private ensureDir;
22
+ /** Load from disk into the in-memory cache. Silent on parse errors. */
23
+ private load;
24
+ /** Atomic write via temp file + rename (POSIX-atomic). */
25
+ private save;
26
+ /** Acquire lock → reload → mutate → save → release. */
27
+ private withLock;
28
+ /** Read-only — returns a snapshot of the in-memory cache. */
29
+ list(): ScheduledSubagent[];
30
+ /** Read-only check — uses the cache. */
31
+ hasName(name: string, exceptId?: string): boolean;
32
+ get(id: string): ScheduledSubagent | undefined;
33
+ add(job: ScheduledSubagent): void;
34
+ update(id: string, patch: Partial<ScheduledSubagent>): ScheduledSubagent | undefined;
35
+ remove(id: string): boolean;
36
+ /** Delete the backing file (used when no jobs remain, optional cleanup). */
37
+ deleteFileIfEmpty(): void;
38
+ }
@@ -0,0 +1,155 @@
1
+ /**
2
+ * schedule-store.ts — File-backed store for scheduled subagents.
3
+ *
4
+ * Session-scoped: each pi session owns its own schedules at
5
+ * `<cwd>/.pi/subagent-schedules/<sessionId>.json`. `/new` starts a fresh
6
+ * empty store; `/resume` reloads.
7
+ *
8
+ * Concurrency model lifted from pi-chonky-tasks/src/task-store.ts: every
9
+ * mutation acquires a PID-based exclusion lock, re-reads the latest state
10
+ * from disk, applies the change, atomic-writes via temp+rename, releases.
11
+ */
12
+ import { existsSync, mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
13
+ import { dirname, join } from "node:path";
14
+ const LOCK_RETRY_MS = 50;
15
+ const LOCK_MAX_RETRIES = 100;
16
+ function isProcessRunning(pid) {
17
+ try {
18
+ process.kill(pid, 0);
19
+ return true;
20
+ }
21
+ catch {
22
+ return false;
23
+ }
24
+ }
25
+ function acquireLock(lockPath) {
26
+ for (let i = 0; i < LOCK_MAX_RETRIES; i++) {
27
+ try {
28
+ writeFileSync(lockPath, `${process.pid}`, { flag: "wx" });
29
+ return;
30
+ }
31
+ catch (e) {
32
+ if (e.code === "EEXIST") {
33
+ try {
34
+ const pid = parseInt(readFileSync(lockPath, "utf-8"), 10);
35
+ if (pid && !isProcessRunning(pid)) {
36
+ unlinkSync(lockPath);
37
+ continue;
38
+ }
39
+ }
40
+ catch { /* ignore — try again */ }
41
+ const start = Date.now();
42
+ while (Date.now() - start < LOCK_RETRY_MS) { /* busy wait */ }
43
+ continue;
44
+ }
45
+ throw e;
46
+ }
47
+ }
48
+ throw new Error(`Failed to acquire schedule lock: ${lockPath}`);
49
+ }
50
+ function releaseLock(lockPath) {
51
+ try {
52
+ unlinkSync(lockPath);
53
+ }
54
+ catch { /* ignore */ }
55
+ }
56
+ /** Resolve the storage path for a session-scoped store. */
57
+ export function resolveStorePath(cwd, sessionId) {
58
+ return join(cwd, ".pi", "subagent-schedules", `${sessionId}.json`);
59
+ }
60
+ export class ScheduleStore {
61
+ filePath;
62
+ lockPath;
63
+ jobs = new Map();
64
+ constructor(filePath) {
65
+ this.filePath = filePath;
66
+ this.lockPath = filePath + ".lock";
67
+ this.load();
68
+ }
69
+ /** Create the backing directory lazily — only when we're about to persist. */
70
+ ensureDir() {
71
+ mkdirSync(dirname(this.filePath), { recursive: true });
72
+ }
73
+ /** Load from disk into the in-memory cache. Silent on parse errors. */
74
+ load() {
75
+ if (!existsSync(this.filePath))
76
+ return;
77
+ try {
78
+ const data = JSON.parse(readFileSync(this.filePath, "utf-8"));
79
+ this.jobs.clear();
80
+ for (const j of data.jobs ?? [])
81
+ this.jobs.set(j.id, j);
82
+ }
83
+ catch { /* corrupt — start fresh, next save rewrites */ }
84
+ }
85
+ /** Atomic write via temp file + rename (POSIX-atomic). */
86
+ save() {
87
+ const data = { version: 1, jobs: [...this.jobs.values()] };
88
+ const tmp = this.filePath + ".tmp";
89
+ writeFileSync(tmp, JSON.stringify(data, null, 2));
90
+ renameSync(tmp, this.filePath);
91
+ }
92
+ /** Acquire lock → reload → mutate → save → release. */
93
+ withLock(fn) {
94
+ this.ensureDir();
95
+ acquireLock(this.lockPath);
96
+ try {
97
+ this.load();
98
+ const result = fn();
99
+ this.save();
100
+ return result;
101
+ }
102
+ finally {
103
+ releaseLock(this.lockPath);
104
+ }
105
+ }
106
+ /** Read-only — returns a snapshot of the in-memory cache. */
107
+ list() {
108
+ return [...this.jobs.values()];
109
+ }
110
+ /** Read-only check — uses the cache. */
111
+ hasName(name, exceptId) {
112
+ for (const j of this.jobs.values()) {
113
+ if (j.id !== exceptId && j.name === name)
114
+ return true;
115
+ }
116
+ return false;
117
+ }
118
+ get(id) {
119
+ return this.jobs.get(id);
120
+ }
121
+ add(job) {
122
+ this.withLock(() => {
123
+ this.jobs.set(job.id, job);
124
+ });
125
+ }
126
+ update(id, patch) {
127
+ // No-op fast path — an unknown id changes nothing, so don't lock or touch
128
+ // disk (which would otherwise lazily create the backing directory).
129
+ if (!this.jobs.has(id))
130
+ return undefined;
131
+ return this.withLock(() => {
132
+ const existing = this.jobs.get(id);
133
+ if (!existing)
134
+ return undefined;
135
+ const updated = { ...existing, ...patch };
136
+ this.jobs.set(id, updated);
137
+ return updated;
138
+ });
139
+ }
140
+ remove(id) {
141
+ // No-op fast path — see update().
142
+ if (!this.jobs.has(id))
143
+ return false;
144
+ return this.withLock(() => this.jobs.delete(id));
145
+ }
146
+ /** Delete the backing file (used when no jobs remain, optional cleanup). */
147
+ deleteFileIfEmpty() {
148
+ if (this.jobs.size === 0 && existsSync(this.filePath)) {
149
+ try {
150
+ unlinkSync(this.filePath);
151
+ }
152
+ catch { /* ignore */ }
153
+ }
154
+ }
155
+ }
@@ -0,0 +1,109 @@
1
+ /**
2
+ * schedule.ts — `SubagentScheduler`: timer-driven dispatcher of scheduled subagents.
3
+ *
4
+ * Mirrors the engine shape of pi-cron-schedule/src/scheduler.ts:
5
+ * - two-Map split (jobs = croner Cron, intervals = setInterval/setTimeout)
6
+ * - addJob/removeJob/updateJob/scheduleJob/unscheduleJob/executeJob
7
+ * - static parsers for cron / "+10m" / "5m" / ISO formats
8
+ *
9
+ * Differences vs pi-cron-schedule:
10
+ * - Persistence is via ScheduleStore (PID-locked, session-scoped, atomic).
11
+ * - `executeJob` calls `manager.spawn(..., { bypassQueue: true })` instead
12
+ * of dispatching a user message — schedule fires bypass maxConcurrent so
13
+ * a 5-minute interval can't be deferred behind 4 long-running agents.
14
+ * - Result delivery is implicit: spawn → background completion → existing
15
+ * steering-style `subagent-notification` path. No new delivery code.
16
+ */
17
+ import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
18
+ import type { AgentManager } from "./agent-manager.js";
19
+ import type { ScheduleStore } from "./schedule-store.js";
20
+ import type { IsolationMode, ScheduledSubagent, SubagentType, ThinkingLevel } from "./types.js";
21
+ /** Event emitted on `pi.events` for cross-extension consumers. */
22
+ export type ScheduleChangeEvent = {
23
+ type: "added";
24
+ job: ScheduledSubagent;
25
+ } | {
26
+ type: "removed";
27
+ jobId: string;
28
+ } | {
29
+ type: "updated";
30
+ job: ScheduledSubagent;
31
+ } | {
32
+ type: "fired";
33
+ jobId: string;
34
+ agentId: string;
35
+ name: string;
36
+ } | {
37
+ type: "error";
38
+ jobId: string;
39
+ error: string;
40
+ };
41
+ /** Params accepted at job creation — ID, timestamps, and state are derived. */
42
+ export interface NewJobInput {
43
+ name: string;
44
+ description: string;
45
+ schedule: string;
46
+ subagent_type: SubagentType;
47
+ prompt: string;
48
+ model?: string;
49
+ thinking?: ThinkingLevel;
50
+ max_turns?: number;
51
+ isolated?: boolean;
52
+ isolation?: IsolationMode;
53
+ }
54
+ export declare class SubagentScheduler {
55
+ private jobs;
56
+ private intervals;
57
+ private store;
58
+ private pi;
59
+ private ctx;
60
+ private manager;
61
+ /** Start the scheduler: bind to a session's store and arm enabled jobs. */
62
+ start(pi: ExtensionAPI, ctx: ExtensionContext, manager: AgentManager, store: ScheduleStore): void;
63
+ /** Stop all timers; drop refs. Safe to call repeatedly. */
64
+ stop(): void;
65
+ /** True if start() has bound a store and the scheduler is active. */
66
+ isActive(): boolean;
67
+ list(): ScheduledSubagent[];
68
+ /**
69
+ * Build a `ScheduledSubagent` from user input. Validates the schedule
70
+ * format and tags `scheduleType`. Throws on invalid input.
71
+ */
72
+ buildJob(input: NewJobInput): ScheduledSubagent;
73
+ /** Add a job, persist, and arm if enabled. Returns the stored job. */
74
+ addJob(input: NewJobInput): ScheduledSubagent;
75
+ removeJob(id: string): boolean;
76
+ /** Toggle / mutate a job. Re-arms based on the new `enabled` state. */
77
+ updateJob(id: string, patch: Partial<ScheduledSubagent>): ScheduledSubagent | undefined;
78
+ /** Next-run time as ISO, or undefined if not currently armed. */
79
+ getNextRun(jobId: string): string | undefined;
80
+ private scheduleJob;
81
+ private unscheduleJob;
82
+ /**
83
+ * Fire a job: persist running state, spawn (bypassing the concurrency
84
+ * queue), persist completion. Fire-and-forget: the timer tick returns
85
+ * immediately so other jobs keep firing.
86
+ */
87
+ private executeJob;
88
+ private emit;
89
+ private requireStore;
90
+ /**
91
+ * Sniff a schedule string and tag its type. Throws on invalid input.
92
+ * Order matters: relative ("+10m") and interval ("5m") both match digit+unit;
93
+ * relative requires the leading "+" to disambiguate.
94
+ */
95
+ static detectSchedule(s: string): {
96
+ type: "cron" | "once" | "interval";
97
+ intervalMs?: number;
98
+ normalized: string;
99
+ };
100
+ /** 6-field cron — 'second minute hour dom month dow'. */
101
+ static validateCronExpression(expr: string): {
102
+ valid: boolean;
103
+ error?: string;
104
+ };
105
+ /** "+10s"/"+5m"/"+1h"/"+2d" → ISO timestamp. */
106
+ static parseRelativeTime(s: string): string | null;
107
+ /** "10s"/"5m"/"1h"/"2d" → milliseconds. */
108
+ static parseInterval(s: string): number | null;
109
+ }