@by-lua/lspec-subagents 1.0.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.
- package/CHANGELOG.md +482 -0
- package/LICENSE +21 -0
- package/README.md +123 -0
- package/dist/agent-manager.d.ts +108 -0
- package/dist/agent-manager.js +391 -0
- package/dist/agent-runner.d.ts +95 -0
- package/dist/agent-runner.js +377 -0
- package/dist/agent-types.d.ts +58 -0
- package/dist/agent-types.js +157 -0
- package/dist/context.d.ts +12 -0
- package/dist/context.js +56 -0
- package/dist/cross-extension-rpc.d.ts +46 -0
- package/dist/cross-extension-rpc.js +76 -0
- package/dist/custom-agents.d.ts +14 -0
- package/dist/custom-agents.js +127 -0
- package/dist/default-agents.d.ts +12 -0
- package/dist/default-agents.js +489 -0
- package/dist/env.d.ts +6 -0
- package/dist/env.js +28 -0
- package/dist/group-join.d.ts +32 -0
- package/dist/group-join.js +116 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +1863 -0
- package/dist/invocation-config.d.ts +22 -0
- package/dist/invocation-config.js +15 -0
- package/dist/memory.d.ts +49 -0
- package/dist/memory.js +151 -0
- package/dist/model-config-loader.d.ts +58 -0
- package/dist/model-config-loader.js +157 -0
- package/dist/model-resolver.d.ts +19 -0
- package/dist/model-resolver.js +62 -0
- package/dist/output-file.d.ts +24 -0
- package/dist/output-file.js +86 -0
- package/dist/prompts.d.ts +29 -0
- package/dist/prompts.js +65 -0
- package/dist/schedule-store.d.ts +38 -0
- package/dist/schedule-store.js +155 -0
- package/dist/schedule.d.ts +109 -0
- package/dist/schedule.js +338 -0
- package/dist/settings.d.ts +66 -0
- package/dist/settings.js +130 -0
- package/dist/skill-loader.d.ts +24 -0
- package/dist/skill-loader.js +93 -0
- package/dist/types.d.ts +164 -0
- package/dist/types.js +8 -0
- package/dist/ui/agent-widget.d.ts +134 -0
- package/dist/ui/agent-widget.js +451 -0
- package/dist/ui/conversation-viewer.d.ts +35 -0
- package/dist/ui/conversation-viewer.js +252 -0
- package/dist/ui/schedule-menu.d.ts +16 -0
- package/dist/ui/schedule-menu.js +95 -0
- package/dist/usage.d.ts +50 -0
- package/dist/usage.js +49 -0
- package/dist/worktree.d.ts +36 -0
- package/dist/worktree.js +139 -0
- package/install.sh +77 -0
- package/lspec-model-config.example.json +17 -0
- package/package.json +50 -0
- package/src/agent-manager.ts +483 -0
- package/src/agent-runner.ts +486 -0
- package/src/agent-types.ts +188 -0
- package/src/context.ts +58 -0
- package/src/cross-extension-rpc.ts +122 -0
- package/src/custom-agents.ts +136 -0
- package/src/default-agents.ts +501 -0
- package/src/env.ts +33 -0
- package/src/group-join.ts +141 -0
- package/src/index.ts +2032 -0
- package/src/invocation-config.ts +40 -0
- package/src/memory.ts +165 -0
- package/src/model-config-loader.ts +193 -0
- package/src/model-resolver.ts +81 -0
- package/src/output-file.ts +96 -0
- package/src/prompts.ts +91 -0
- package/src/schedule-store.ts +153 -0
- package/src/schedule.ts +365 -0
- package/src/settings.ts +186 -0
- package/src/skill-loader.ts +102 -0
- package/src/types.ts +179 -0
- package/src/ui/agent-widget.ts +533 -0
- package/src/ui/conversation-viewer.ts +261 -0
- package/src/ui/schedule-menu.ts +104 -0
- package/src/usage.ts +60 -0
- package/src/worktree.ts +162 -0
- package/uninstall.sh +55 -0
- package/update.sh +64 -0
|
@@ -0,0 +1,153 @@
|
|
|
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
|
+
|
|
13
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
|
|
14
|
+
import { dirname, join } from "node:path";
|
|
15
|
+
import type { ScheduledSubagent, ScheduleStoreData } from "./types.js";
|
|
16
|
+
|
|
17
|
+
const LOCK_RETRY_MS = 50;
|
|
18
|
+
const LOCK_MAX_RETRIES = 100;
|
|
19
|
+
|
|
20
|
+
function isProcessRunning(pid: number): boolean {
|
|
21
|
+
try { process.kill(pid, 0); return true; } catch { return false; }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function acquireLock(lockPath: string): void {
|
|
25
|
+
for (let i = 0; i < LOCK_MAX_RETRIES; i++) {
|
|
26
|
+
try {
|
|
27
|
+
writeFileSync(lockPath, `${process.pid}`, { flag: "wx" });
|
|
28
|
+
return;
|
|
29
|
+
} catch (e: any) {
|
|
30
|
+
if (e.code === "EEXIST") {
|
|
31
|
+
try {
|
|
32
|
+
const pid = parseInt(readFileSync(lockPath, "utf-8"), 10);
|
|
33
|
+
if (pid && !isProcessRunning(pid)) {
|
|
34
|
+
unlinkSync(lockPath);
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
} catch { /* ignore — try again */ }
|
|
38
|
+
const start = Date.now();
|
|
39
|
+
while (Date.now() - start < LOCK_RETRY_MS) { /* busy wait */ }
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
throw e;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
throw new Error(`Failed to acquire schedule lock: ${lockPath}`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function releaseLock(lockPath: string): void {
|
|
49
|
+
try { unlinkSync(lockPath); } catch { /* ignore */ }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Resolve the storage path for a session-scoped store. */
|
|
53
|
+
export function resolveStorePath(cwd: string, sessionId: string): string {
|
|
54
|
+
return join(cwd, ".pi", "subagent-schedules", `${sessionId}.json`);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export class ScheduleStore {
|
|
58
|
+
private filePath: string;
|
|
59
|
+
private lockPath: string;
|
|
60
|
+
private jobs = new Map<string, ScheduledSubagent>();
|
|
61
|
+
|
|
62
|
+
constructor(filePath: string) {
|
|
63
|
+
this.filePath = filePath;
|
|
64
|
+
this.lockPath = filePath + ".lock";
|
|
65
|
+
this.load();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Create the backing directory lazily — only when we're about to persist. */
|
|
69
|
+
private ensureDir(): void {
|
|
70
|
+
mkdirSync(dirname(this.filePath), { recursive: true });
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Load from disk into the in-memory cache. Silent on parse errors. */
|
|
74
|
+
private load(): void {
|
|
75
|
+
if (!existsSync(this.filePath)) return;
|
|
76
|
+
try {
|
|
77
|
+
const data: ScheduleStoreData = JSON.parse(readFileSync(this.filePath, "utf-8"));
|
|
78
|
+
this.jobs.clear();
|
|
79
|
+
for (const j of data.jobs ?? []) this.jobs.set(j.id, j);
|
|
80
|
+
} catch { /* corrupt — start fresh, next save rewrites */ }
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Atomic write via temp file + rename (POSIX-atomic). */
|
|
84
|
+
private save(): void {
|
|
85
|
+
const data: ScheduleStoreData = { version: 1, jobs: [...this.jobs.values()] };
|
|
86
|
+
const tmp = this.filePath + ".tmp";
|
|
87
|
+
writeFileSync(tmp, JSON.stringify(data, null, 2));
|
|
88
|
+
renameSync(tmp, this.filePath);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Acquire lock → reload → mutate → save → release. */
|
|
92
|
+
private withLock<T>(fn: () => T): T {
|
|
93
|
+
this.ensureDir();
|
|
94
|
+
acquireLock(this.lockPath);
|
|
95
|
+
try {
|
|
96
|
+
this.load();
|
|
97
|
+
const result = fn();
|
|
98
|
+
this.save();
|
|
99
|
+
return result;
|
|
100
|
+
} finally {
|
|
101
|
+
releaseLock(this.lockPath);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Read-only — returns a snapshot of the in-memory cache. */
|
|
106
|
+
list(): ScheduledSubagent[] {
|
|
107
|
+
return [...this.jobs.values()];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Read-only check — uses the cache. */
|
|
111
|
+
hasName(name: string, exceptId?: string): boolean {
|
|
112
|
+
for (const j of this.jobs.values()) {
|
|
113
|
+
if (j.id !== exceptId && j.name === name) return true;
|
|
114
|
+
}
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
get(id: string): ScheduledSubagent | undefined {
|
|
119
|
+
return this.jobs.get(id);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
add(job: ScheduledSubagent): void {
|
|
123
|
+
this.withLock(() => {
|
|
124
|
+
this.jobs.set(job.id, job);
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
update(id: string, patch: Partial<ScheduledSubagent>): ScheduledSubagent | undefined {
|
|
129
|
+
// No-op fast path — an unknown id changes nothing, so don't lock or touch
|
|
130
|
+
// disk (which would otherwise lazily create the backing directory).
|
|
131
|
+
if (!this.jobs.has(id)) return undefined;
|
|
132
|
+
return this.withLock(() => {
|
|
133
|
+
const existing = this.jobs.get(id);
|
|
134
|
+
if (!existing) return undefined;
|
|
135
|
+
const updated = { ...existing, ...patch };
|
|
136
|
+
this.jobs.set(id, updated);
|
|
137
|
+
return updated;
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
remove(id: string): boolean {
|
|
142
|
+
// No-op fast path — see update().
|
|
143
|
+
if (!this.jobs.has(id)) return false;
|
|
144
|
+
return this.withLock(() => this.jobs.delete(id));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Delete the backing file (used when no jobs remain, optional cleanup). */
|
|
148
|
+
deleteFileIfEmpty(): void {
|
|
149
|
+
if (this.jobs.size === 0 && existsSync(this.filePath)) {
|
|
150
|
+
try { unlinkSync(this.filePath); } catch { /* ignore */ }
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
package/src/schedule.ts
ADDED
|
@@ -0,0 +1,365 @@
|
|
|
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
|
+
* `subagent-notification` followUp path. No new delivery code.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import type { ExtensionAPI, ExtensionContext } from "@mariozechner/pi-coding-agent";
|
|
19
|
+
import { Cron } from "croner";
|
|
20
|
+
import { nanoid } from "nanoid";
|
|
21
|
+
import type { AgentManager } from "./agent-manager.js";
|
|
22
|
+
import { resolveModel } from "./model-resolver.js";
|
|
23
|
+
import type { ScheduleStore } from "./schedule-store.js";
|
|
24
|
+
import type { IsolationMode, ScheduledSubagent, SubagentType, ThinkingLevel } from "./types.js";
|
|
25
|
+
|
|
26
|
+
/** Event emitted on `pi.events` for cross-extension consumers. */
|
|
27
|
+
export type ScheduleChangeEvent =
|
|
28
|
+
| { type: "added"; job: ScheduledSubagent }
|
|
29
|
+
| { type: "removed"; jobId: string }
|
|
30
|
+
| { type: "updated"; job: ScheduledSubagent }
|
|
31
|
+
| { type: "fired"; jobId: string; agentId: string; name: string }
|
|
32
|
+
| { type: "error"; jobId: string; error: string };
|
|
33
|
+
|
|
34
|
+
/** Params accepted at job creation — ID, timestamps, and state are derived. */
|
|
35
|
+
export interface NewJobInput {
|
|
36
|
+
name: string;
|
|
37
|
+
description: string;
|
|
38
|
+
schedule: string;
|
|
39
|
+
subagent_type: SubagentType;
|
|
40
|
+
prompt: string;
|
|
41
|
+
model?: string;
|
|
42
|
+
thinking?: ThinkingLevel;
|
|
43
|
+
max_turns?: number;
|
|
44
|
+
isolated?: boolean;
|
|
45
|
+
isolation?: IsolationMode;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export class SubagentScheduler {
|
|
49
|
+
private jobs = new Map<string, Cron>();
|
|
50
|
+
private intervals = new Map<string, NodeJS.Timeout>();
|
|
51
|
+
private store: ScheduleStore | undefined;
|
|
52
|
+
private pi: ExtensionAPI | undefined;
|
|
53
|
+
private ctx: ExtensionContext | undefined;
|
|
54
|
+
private manager: AgentManager | undefined;
|
|
55
|
+
|
|
56
|
+
/** Start the scheduler: bind to a session's store and arm enabled jobs. */
|
|
57
|
+
start(pi: ExtensionAPI, ctx: ExtensionContext, manager: AgentManager, store: ScheduleStore): void {
|
|
58
|
+
this.pi = pi;
|
|
59
|
+
this.ctx = ctx;
|
|
60
|
+
this.manager = manager;
|
|
61
|
+
this.store = store;
|
|
62
|
+
|
|
63
|
+
for (const job of store.list()) {
|
|
64
|
+
if (job.enabled) this.scheduleJob(job);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Stop all timers; drop refs. Safe to call repeatedly. */
|
|
69
|
+
stop(): void {
|
|
70
|
+
for (const cron of this.jobs.values()) cron.stop();
|
|
71
|
+
this.jobs.clear();
|
|
72
|
+
for (const t of this.intervals.values()) clearTimeout(t);
|
|
73
|
+
this.intervals.clear();
|
|
74
|
+
this.store = undefined;
|
|
75
|
+
this.pi = undefined;
|
|
76
|
+
this.ctx = undefined;
|
|
77
|
+
this.manager = undefined;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** True if start() has bound a store and the scheduler is active. */
|
|
81
|
+
isActive(): boolean {
|
|
82
|
+
return this.store !== undefined;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
list(): ScheduledSubagent[] {
|
|
86
|
+
return this.store?.list() ?? [];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Build a `ScheduledSubagent` from user input. Validates the schedule
|
|
91
|
+
* format and tags `scheduleType`. Throws on invalid input.
|
|
92
|
+
*/
|
|
93
|
+
buildJob(input: NewJobInput): ScheduledSubagent {
|
|
94
|
+
const detected = SubagentScheduler.detectSchedule(input.schedule);
|
|
95
|
+
return {
|
|
96
|
+
id: nanoid(10),
|
|
97
|
+
name: input.name,
|
|
98
|
+
description: input.description,
|
|
99
|
+
schedule: detected.normalized,
|
|
100
|
+
scheduleType: detected.type,
|
|
101
|
+
intervalMs: detected.intervalMs,
|
|
102
|
+
subagent_type: input.subagent_type,
|
|
103
|
+
prompt: input.prompt,
|
|
104
|
+
model: input.model,
|
|
105
|
+
thinking: input.thinking,
|
|
106
|
+
max_turns: input.max_turns,
|
|
107
|
+
isolated: input.isolated,
|
|
108
|
+
isolation: input.isolation,
|
|
109
|
+
enabled: true,
|
|
110
|
+
createdAt: new Date().toISOString(),
|
|
111
|
+
runCount: 0,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Add a job, persist, and arm if enabled. Returns the stored job. */
|
|
116
|
+
addJob(input: NewJobInput): ScheduledSubagent {
|
|
117
|
+
const store = this.requireStore();
|
|
118
|
+
if (store.hasName(input.name)) {
|
|
119
|
+
throw new Error(`A scheduled job named "${input.name}" already exists.`);
|
|
120
|
+
}
|
|
121
|
+
const job = this.buildJob(input);
|
|
122
|
+
store.add(job);
|
|
123
|
+
if (job.enabled) this.scheduleJob(job);
|
|
124
|
+
this.emit({ type: "added", job });
|
|
125
|
+
return job;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
removeJob(id: string): boolean {
|
|
129
|
+
const store = this.requireStore();
|
|
130
|
+
if (!store.get(id)) return false;
|
|
131
|
+
this.unscheduleJob(id);
|
|
132
|
+
const ok = store.remove(id);
|
|
133
|
+
if (ok) this.emit({ type: "removed", jobId: id });
|
|
134
|
+
return ok;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Toggle / mutate a job. Re-arms based on the new `enabled` state. */
|
|
138
|
+
updateJob(id: string, patch: Partial<ScheduledSubagent>): ScheduledSubagent | undefined {
|
|
139
|
+
const store = this.requireStore();
|
|
140
|
+
const updated = store.update(id, patch);
|
|
141
|
+
if (!updated) return undefined;
|
|
142
|
+
this.unscheduleJob(id);
|
|
143
|
+
if (updated.enabled) this.scheduleJob(updated);
|
|
144
|
+
this.emit({ type: "updated", job: updated });
|
|
145
|
+
return updated;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Next-run time as ISO, or undefined if not currently armed. */
|
|
149
|
+
getNextRun(jobId: string): string | undefined {
|
|
150
|
+
const cron = this.jobs.get(jobId);
|
|
151
|
+
if (cron) return cron.nextRun()?.toISOString();
|
|
152
|
+
const job = this.store?.get(jobId);
|
|
153
|
+
if (!job?.enabled) return undefined;
|
|
154
|
+
if (job.scheduleType === "once") return job.schedule;
|
|
155
|
+
if (job.scheduleType === "interval" && job.intervalMs) {
|
|
156
|
+
// Before the first fire there's no `lastRun`, so fall back to "now" —
|
|
157
|
+
// accurate at create time (setInterval was just armed) and within
|
|
158
|
+
// intervalMs of correct in any pre-first-fire view.
|
|
159
|
+
const base = job.lastRun ? new Date(job.lastRun).getTime() : Date.now();
|
|
160
|
+
return new Date(base + job.intervalMs).toISOString();
|
|
161
|
+
}
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// ── Scheduling primitives ────────────────────────────────────────────
|
|
166
|
+
|
|
167
|
+
private scheduleJob(job: ScheduledSubagent): void {
|
|
168
|
+
const store = this.store;
|
|
169
|
+
if (!store) return;
|
|
170
|
+
try {
|
|
171
|
+
if (job.scheduleType === "interval" && job.intervalMs) {
|
|
172
|
+
const t = setInterval(() => this.executeJob(job.id), job.intervalMs);
|
|
173
|
+
this.intervals.set(job.id, t);
|
|
174
|
+
} else if (job.scheduleType === "once") {
|
|
175
|
+
const target = new Date(job.schedule).getTime();
|
|
176
|
+
const delay = target - Date.now();
|
|
177
|
+
if (delay > 0) {
|
|
178
|
+
const t = setTimeout(() => {
|
|
179
|
+
this.executeJob(job.id);
|
|
180
|
+
// Auto-disable one-shots after they fire (mirrors pi-cron-schedule)
|
|
181
|
+
store.update(job.id, { enabled: false });
|
|
182
|
+
const updated = store.get(job.id);
|
|
183
|
+
if (updated) this.emit({ type: "updated", job: updated });
|
|
184
|
+
}, delay);
|
|
185
|
+
this.intervals.set(job.id, t);
|
|
186
|
+
} else {
|
|
187
|
+
// Past timestamp — disable, mark error, never fire
|
|
188
|
+
store.update(job.id, { enabled: false, lastStatus: "error" });
|
|
189
|
+
this.emit({ type: "error", jobId: job.id, error: `Scheduled time ${job.schedule} is in the past` });
|
|
190
|
+
}
|
|
191
|
+
} else {
|
|
192
|
+
const cron = new Cron(job.schedule, () => this.executeJob(job.id));
|
|
193
|
+
this.jobs.set(job.id, cron);
|
|
194
|
+
}
|
|
195
|
+
} catch (err) {
|
|
196
|
+
this.emit({ type: "error", jobId: job.id, error: err instanceof Error ? err.message : String(err) });
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
private unscheduleJob(id: string): void {
|
|
201
|
+
const cron = this.jobs.get(id);
|
|
202
|
+
if (cron) {
|
|
203
|
+
cron.stop();
|
|
204
|
+
this.jobs.delete(id);
|
|
205
|
+
}
|
|
206
|
+
const t = this.intervals.get(id);
|
|
207
|
+
if (t) {
|
|
208
|
+
clearTimeout(t);
|
|
209
|
+
clearInterval(t);
|
|
210
|
+
this.intervals.delete(id);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Fire a job: persist running state, spawn (bypassing the concurrency
|
|
216
|
+
* queue), persist completion. Fire-and-forget: the timer tick returns
|
|
217
|
+
* immediately so other jobs keep firing.
|
|
218
|
+
*/
|
|
219
|
+
private executeJob(id: string): void {
|
|
220
|
+
const store = this.store;
|
|
221
|
+
const pi = this.pi;
|
|
222
|
+
const ctx = this.ctx;
|
|
223
|
+
const manager = this.manager;
|
|
224
|
+
if (!store || !pi || !ctx || !manager) return;
|
|
225
|
+
const job = store.get(id);
|
|
226
|
+
if (!job?.enabled) return;
|
|
227
|
+
|
|
228
|
+
store.update(id, { lastStatus: "running" });
|
|
229
|
+
|
|
230
|
+
// Resolve model at fire time — registry contents may have changed since the
|
|
231
|
+
// job was created (auth added/removed). Fall back silently to spawn-default
|
|
232
|
+
// if resolution fails; the spawn path handles undefined model gracefully.
|
|
233
|
+
let resolvedModel: any | undefined;
|
|
234
|
+
if (job.model) {
|
|
235
|
+
const r = resolveModel(job.model, ctx.modelRegistry);
|
|
236
|
+
if (typeof r !== "string") resolvedModel = r;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
let agentId: string;
|
|
240
|
+
try {
|
|
241
|
+
agentId = manager.spawn(pi, ctx, job.subagent_type, job.prompt, {
|
|
242
|
+
description: job.description,
|
|
243
|
+
isBackground: true,
|
|
244
|
+
bypassQueue: true,
|
|
245
|
+
model: resolvedModel,
|
|
246
|
+
maxTurns: job.max_turns,
|
|
247
|
+
isolated: job.isolated,
|
|
248
|
+
thinkingLevel: job.thinking,
|
|
249
|
+
isolation: job.isolation,
|
|
250
|
+
});
|
|
251
|
+
} catch (err) {
|
|
252
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
253
|
+
store.update(id, { lastRun: new Date().toISOString(), lastStatus: "error" });
|
|
254
|
+
this.emit({ type: "error", jobId: id, error });
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
this.emit({ type: "fired", jobId: id, agentId, name: job.name });
|
|
259
|
+
|
|
260
|
+
const record = manager.getRecord(agentId);
|
|
261
|
+
const finalize = (status: "success" | "error") => {
|
|
262
|
+
const next = this.getNextRun(id);
|
|
263
|
+
const current = store.get(id);
|
|
264
|
+
store.update(id, {
|
|
265
|
+
lastRun: new Date().toISOString(),
|
|
266
|
+
lastStatus: status,
|
|
267
|
+
runCount: (current?.runCount ?? 0) + 1,
|
|
268
|
+
nextRun: next,
|
|
269
|
+
});
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
// AgentManager's promise resolves either way (its .catch returns ""), so we
|
|
273
|
+
// can't infer success/failure from the promise — read record.status instead.
|
|
274
|
+
// Terminal states: completed/steered = success; error/aborted/stopped = error.
|
|
275
|
+
if (record?.promise) {
|
|
276
|
+
record.promise
|
|
277
|
+
.then(() => {
|
|
278
|
+
const r = manager.getRecord(agentId);
|
|
279
|
+
const failed = r?.status === "error" || r?.status === "aborted" || r?.status === "stopped";
|
|
280
|
+
finalize(failed ? "error" : "success");
|
|
281
|
+
})
|
|
282
|
+
.catch(() => finalize("error"));
|
|
283
|
+
} else {
|
|
284
|
+
// Spawn returned without a promise (defensive — bypassQueue path always sets one).
|
|
285
|
+
finalize("success");
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
private emit(event: ScheduleChangeEvent): void {
|
|
290
|
+
if (this.pi) this.pi.events.emit("subagents:scheduled", event);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
private requireStore(): ScheduleStore {
|
|
294
|
+
if (!this.store) throw new Error("Scheduler not started — no active session.");
|
|
295
|
+
return this.store;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// ── Format detection / parsers (statics — pure) ──────────────────────
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Sniff a schedule string and tag its type. Throws on invalid input.
|
|
302
|
+
* Order matters: relative ("+10m") and interval ("5m") both match digit+unit;
|
|
303
|
+
* relative requires the leading "+" to disambiguate.
|
|
304
|
+
*/
|
|
305
|
+
static detectSchedule(s: string): { type: "cron" | "once" | "interval"; intervalMs?: number; normalized: string } {
|
|
306
|
+
const trimmed = s.trim();
|
|
307
|
+
// "+10m" — relative one-shot
|
|
308
|
+
const rel = SubagentScheduler.parseRelativeTime(trimmed);
|
|
309
|
+
if (rel !== null) return { type: "once", normalized: rel };
|
|
310
|
+
// "5m" — interval
|
|
311
|
+
const ivl = SubagentScheduler.parseInterval(trimmed);
|
|
312
|
+
if (ivl !== null) return { type: "interval", intervalMs: ivl, normalized: trimmed };
|
|
313
|
+
// ISO timestamp — one-shot. Reject past timestamps upfront so we never
|
|
314
|
+
// create a dead-on-arrival record (scheduleJob's safety net still catches
|
|
315
|
+
// micro-races from `+0s`-style relatives).
|
|
316
|
+
if (/^\d{4}-\d{2}-\d{2}T/.test(trimmed)) {
|
|
317
|
+
const d = new Date(trimmed);
|
|
318
|
+
if (!Number.isNaN(d.getTime())) {
|
|
319
|
+
if (d.getTime() <= Date.now()) {
|
|
320
|
+
throw new Error(`Scheduled time ${d.toISOString()} is in the past.`);
|
|
321
|
+
}
|
|
322
|
+
return { type: "once", normalized: d.toISOString() };
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
// Cron — 6-field
|
|
326
|
+
const cronCheck = SubagentScheduler.validateCronExpression(trimmed);
|
|
327
|
+
if (cronCheck.valid) return { type: "cron", normalized: trimmed };
|
|
328
|
+
throw new Error(
|
|
329
|
+
`Invalid schedule "${s}". Use 6-field cron (e.g. "0 0 9 * * 1" — 9am every Monday), interval ("5m"/"1h"), or one-shot ("+10m" / ISO).`
|
|
330
|
+
);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** 6-field cron — 'second minute hour dom month dow'. */
|
|
334
|
+
static validateCronExpression(expr: string): { valid: boolean; error?: string } {
|
|
335
|
+
const fields = expr.trim().split(/\s+/);
|
|
336
|
+
if (fields.length !== 6) {
|
|
337
|
+
return {
|
|
338
|
+
valid: false,
|
|
339
|
+
error: `Cron must have 6 fields (second minute hour dom month dow), got ${fields.length}. Example: "0 0 9 * * 1" for 9am every Monday.`,
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
try {
|
|
343
|
+
// Croner validates by construction.
|
|
344
|
+
new Cron(expr, () => {});
|
|
345
|
+
return { valid: true };
|
|
346
|
+
} catch (e) {
|
|
347
|
+
return { valid: false, error: e instanceof Error ? e.message : "Invalid cron expression" };
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/** "+10s"/"+5m"/"+1h"/"+2d" → ISO timestamp. */
|
|
352
|
+
static parseRelativeTime(s: string): string | null {
|
|
353
|
+
const m = s.match(/^\+(\d+)(s|m|h|d)$/);
|
|
354
|
+
if (!m) return null;
|
|
355
|
+
const ms = parseInt(m[1], 10) * { s: 1000, m: 60_000, h: 3_600_000, d: 86_400_000 }[m[2] as "s" | "m" | "h" | "d"];
|
|
356
|
+
return new Date(Date.now() + ms).toISOString();
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/** "10s"/"5m"/"1h"/"2d" → milliseconds. */
|
|
360
|
+
static parseInterval(s: string): number | null {
|
|
361
|
+
const m = s.match(/^(\d+)(s|m|h|d)$/);
|
|
362
|
+
if (!m) return null;
|
|
363
|
+
return parseInt(m[1], 10) * { s: 1000, m: 60_000, h: 3_600_000, d: 86_400_000 }[m[2] as "s" | "m" | "h" | "d"];
|
|
364
|
+
}
|
|
365
|
+
}
|
package/src/settings.ts
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
// Persistence for pi-subagents operational settings.
|
|
2
|
+
// - Global: ~/.pi/agent/subagents.json (via getAgentDir()) — manual defaults, never written here
|
|
3
|
+
// - Project: <cwd>/.pi/subagents.json — written by /agents → Settings; overrides global on load
|
|
4
|
+
|
|
5
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
6
|
+
import { dirname, join } from "node:path";
|
|
7
|
+
import { getAgentDir } from "@mariozechner/pi-coding-agent";
|
|
8
|
+
import type { JoinMode } from "./types.js";
|
|
9
|
+
|
|
10
|
+
export interface SubagentsSettings {
|
|
11
|
+
maxConcurrent?: number;
|
|
12
|
+
/**
|
|
13
|
+
* 0 = unlimited — the extension's single source of truth for that convention:
|
|
14
|
+
* `normalizeMaxTurns()` in agent-runner.ts treats 0 → `undefined`, and the
|
|
15
|
+
* `/agents` → Settings input prompt explicitly says "0 = unlimited".
|
|
16
|
+
*/
|
|
17
|
+
defaultMaxTurns?: number;
|
|
18
|
+
graceTurns?: number;
|
|
19
|
+
defaultJoinMode?: JoinMode;
|
|
20
|
+
/**
|
|
21
|
+
* Master switch for the schedule subagent feature. Defaults to `true`.
|
|
22
|
+
* When `false`: the `Agent` tool's `schedule` param + its guideline are
|
|
23
|
+
* stripped from the tool spec at registration (zero LLM-context cost), the
|
|
24
|
+
* scheduler doesn't bind to the session, and the `/agents → Scheduled jobs`
|
|
25
|
+
* menu entry is hidden. Schema-level removal applies at extension load
|
|
26
|
+
* (next pi session); runtime menu/runtime-fire short-circuit is immediate.
|
|
27
|
+
*/
|
|
28
|
+
schedulingEnabled?: boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Setter hooks used by applySettings to wire persisted values into in-memory state. */
|
|
32
|
+
export interface SettingsAppliers {
|
|
33
|
+
setMaxConcurrent: (n: number) => void;
|
|
34
|
+
setDefaultMaxTurns: (n: number) => void;
|
|
35
|
+
setGraceTurns: (n: number) => void;
|
|
36
|
+
setDefaultJoinMode: (mode: JoinMode) => void;
|
|
37
|
+
setSchedulingEnabled: (b: boolean) => void;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Emit callback — a subset of `pi.events.emit` to keep helpers testable. */
|
|
41
|
+
export type SettingsEmit = (event: string, payload: unknown) => void;
|
|
42
|
+
|
|
43
|
+
const VALID_JOIN_MODES: ReadonlySet<string> = new Set<JoinMode>(["async", "group", "smart"]);
|
|
44
|
+
|
|
45
|
+
// Sanity ceilings — prevent hand-edited configs from asking for values that
|
|
46
|
+
// make no operational sense (e.g. 1e6 concurrent subagents). Permissive enough
|
|
47
|
+
// that any realistic power-user setting passes through.
|
|
48
|
+
const MAX_CONCURRENT_CEILING = 1024;
|
|
49
|
+
const MAX_TURNS_CEILING = 10_000;
|
|
50
|
+
const GRACE_TURNS_CEILING = 1_000;
|
|
51
|
+
|
|
52
|
+
/** Drop fields that don't match the expected shape. Silent — garbage becomes absent. */
|
|
53
|
+
function sanitize(raw: unknown): SubagentsSettings {
|
|
54
|
+
if (!raw || typeof raw !== "object") return {};
|
|
55
|
+
const r = raw as Record<string, unknown>;
|
|
56
|
+
const out: SubagentsSettings = {};
|
|
57
|
+
if (
|
|
58
|
+
Number.isInteger(r.maxConcurrent) &&
|
|
59
|
+
(r.maxConcurrent as number) >= 1 &&
|
|
60
|
+
(r.maxConcurrent as number) <= MAX_CONCURRENT_CEILING
|
|
61
|
+
) {
|
|
62
|
+
out.maxConcurrent = r.maxConcurrent as number;
|
|
63
|
+
}
|
|
64
|
+
if (
|
|
65
|
+
Number.isInteger(r.defaultMaxTurns) &&
|
|
66
|
+
(r.defaultMaxTurns as number) >= 0 &&
|
|
67
|
+
(r.defaultMaxTurns as number) <= MAX_TURNS_CEILING
|
|
68
|
+
) {
|
|
69
|
+
out.defaultMaxTurns = r.defaultMaxTurns as number;
|
|
70
|
+
}
|
|
71
|
+
if (
|
|
72
|
+
Number.isInteger(r.graceTurns) &&
|
|
73
|
+
(r.graceTurns as number) >= 1 &&
|
|
74
|
+
(r.graceTurns as number) <= GRACE_TURNS_CEILING
|
|
75
|
+
) {
|
|
76
|
+
out.graceTurns = r.graceTurns as number;
|
|
77
|
+
}
|
|
78
|
+
if (typeof r.defaultJoinMode === "string" && VALID_JOIN_MODES.has(r.defaultJoinMode)) {
|
|
79
|
+
out.defaultJoinMode = r.defaultJoinMode as JoinMode;
|
|
80
|
+
}
|
|
81
|
+
if (typeof r.schedulingEnabled === "boolean") {
|
|
82
|
+
out.schedulingEnabled = r.schedulingEnabled;
|
|
83
|
+
}
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function globalPath(): string {
|
|
88
|
+
return join(getAgentDir(), "subagents.json");
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function projectPath(cwd: string): string {
|
|
92
|
+
return join(cwd, ".pi", "subagents.json");
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Read a settings file. Missing file is silent (returns `{}`). A file that
|
|
97
|
+
* exists but can't be parsed emits a warning to stderr so users aren't
|
|
98
|
+
* silently reverted to defaults — and still returns `{}` so startup proceeds.
|
|
99
|
+
*/
|
|
100
|
+
function readSettingsFile(path: string): SubagentsSettings {
|
|
101
|
+
if (!existsSync(path)) return {};
|
|
102
|
+
try {
|
|
103
|
+
return sanitize(JSON.parse(readFileSync(path, "utf-8")));
|
|
104
|
+
} catch (err) {
|
|
105
|
+
const reason = err instanceof Error ? err.message : String(err);
|
|
106
|
+
console.warn(`[pi-subagents] Ignoring malformed settings at ${path}: ${reason}`);
|
|
107
|
+
return {};
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Load merged settings: global provides defaults, project overrides. */
|
|
112
|
+
export function loadSettings(cwd: string = process.cwd()): SubagentsSettings {
|
|
113
|
+
return { ...readSettingsFile(globalPath()), ...readSettingsFile(projectPath(cwd)) };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Write project-local settings. Global is never touched from code.
|
|
118
|
+
* Returns `true` on success, `false` if the write (or mkdir) failed so the
|
|
119
|
+
* caller can surface a warning — persistence isn't fatal but isn't silent.
|
|
120
|
+
*/
|
|
121
|
+
export function saveSettings(s: SubagentsSettings, cwd: string = process.cwd()): boolean {
|
|
122
|
+
const path = projectPath(cwd);
|
|
123
|
+
try {
|
|
124
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
125
|
+
writeFileSync(path, JSON.stringify(s, null, 2), "utf-8");
|
|
126
|
+
return true;
|
|
127
|
+
} catch {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Apply persisted settings to the in-memory state via caller-supplied setters. */
|
|
133
|
+
export function applySettings(s: SubagentsSettings, appliers: SettingsAppliers): void {
|
|
134
|
+
if (typeof s.maxConcurrent === "number") appliers.setMaxConcurrent(s.maxConcurrent);
|
|
135
|
+
if (typeof s.defaultMaxTurns === "number") appliers.setDefaultMaxTurns(s.defaultMaxTurns);
|
|
136
|
+
if (typeof s.graceTurns === "number") appliers.setGraceTurns(s.graceTurns);
|
|
137
|
+
if (s.defaultJoinMode) appliers.setDefaultJoinMode(s.defaultJoinMode);
|
|
138
|
+
if (typeof s.schedulingEnabled === "boolean") appliers.setSchedulingEnabled(s.schedulingEnabled);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Format the user-facing toast for a settings mutation. Pure function —
|
|
143
|
+
* routes the success/failure of `saveSettings` into the right message + level
|
|
144
|
+
* so the UI layer (index.ts) stays a thin wire between input and notification.
|
|
145
|
+
*/
|
|
146
|
+
export function persistToastFor(
|
|
147
|
+
successMsg: string,
|
|
148
|
+
persisted: boolean,
|
|
149
|
+
): { message: string; level: "info" | "warning" } {
|
|
150
|
+
return persisted
|
|
151
|
+
? { message: successMsg, level: "info" }
|
|
152
|
+
: { message: `${successMsg} (session only; failed to persist)`, level: "warning" };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Load merged settings, apply them to in-memory state, and emit the
|
|
157
|
+
* `subagents:settings_loaded` lifecycle event. Returns the loaded settings so
|
|
158
|
+
* callers can log/inspect. Extension init wires this once.
|
|
159
|
+
*/
|
|
160
|
+
export function applyAndEmitLoaded(
|
|
161
|
+
appliers: SettingsAppliers,
|
|
162
|
+
emit: SettingsEmit,
|
|
163
|
+
cwd: string = process.cwd(),
|
|
164
|
+
): SubagentsSettings {
|
|
165
|
+
const settings = loadSettings(cwd);
|
|
166
|
+
applySettings(settings, appliers);
|
|
167
|
+
emit("subagents:settings_loaded", { settings });
|
|
168
|
+
return settings;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Persist a settings snapshot, emit the `subagents:settings_changed` event
|
|
173
|
+
* (regardless of persist outcome so listeners see the in-memory change), and
|
|
174
|
+
* return the toast the UI should display. Event payload carries the `persisted`
|
|
175
|
+
* flag so listeners can react to write failures.
|
|
176
|
+
*/
|
|
177
|
+
export function saveAndEmitChanged(
|
|
178
|
+
snapshot: SubagentsSettings,
|
|
179
|
+
successMsg: string,
|
|
180
|
+
emit: SettingsEmit,
|
|
181
|
+
cwd: string = process.cwd(),
|
|
182
|
+
): { message: string; level: "info" | "warning" } {
|
|
183
|
+
const persisted = saveSettings(snapshot, cwd);
|
|
184
|
+
emit("subagents:settings_changed", { settings: snapshot, persisted });
|
|
185
|
+
return persistToastFor(successMsg, persisted);
|
|
186
|
+
}
|