@gotgenes/pi-subagents 1.0.1 → 2.0.0
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 +29 -0
- package/README.md +0 -35
- package/docs/architecture/architecture.md +4 -8
- package/docs/decisions/0001-deferred-patches.md +7 -4
- package/docs/plans/0051-update-adr-0001-hard-fork.md +74 -0
- package/docs/plans/0052-remove-scheduled-subagents.md +131 -0
- package/docs/retro/0051-update-adr-0001-hard-fork.md +33 -0
- package/package.json +1 -2
- package/src/agent-manager.ts +2 -2
- package/src/index.ts +2 -135
- package/src/settings.ts +0 -14
- package/src/types.ts +0 -43
- package/src/schedule-store.ts +0 -143
- package/src/schedule.ts +0 -365
- package/src/ui/schedule-menu.ts +0 -104
package/src/index.ts
CHANGED
|
@@ -24,8 +24,6 @@ import { GroupJoinManager } from "./group-join.js";
|
|
|
24
24
|
import { resolveAgentInvocationConfig, resolveJoinMode } from "./invocation-config.js";
|
|
25
25
|
import { type ModelRegistry, resolveModel } from "./model-resolver.js";
|
|
26
26
|
import { createOutputFilePath, streamToOutputFile, writeInitialEntry } from "./output-file.js";
|
|
27
|
-
import { SubagentScheduler } from "./schedule.js";
|
|
28
|
-
import { resolveStorePath, ScheduleStore } from "./schedule-store.js";
|
|
29
27
|
import { applyAndEmitLoaded, type SubagentsSettings, saveAndEmitChanged } from "./settings.js";
|
|
30
28
|
import { type AgentConfig, type AgentInvocation, type AgentRecord, type JoinMode, type NotificationDetails, type SubagentType } from "./types.js";
|
|
31
29
|
import {
|
|
@@ -43,7 +41,6 @@ import {
|
|
|
43
41
|
SPINNER,
|
|
44
42
|
type UICtx,
|
|
45
43
|
} from "./ui/agent-widget.js";
|
|
46
|
-
import { showSchedulesMenu } from "./ui/schedule-menu.js";
|
|
47
44
|
import { addUsage, getLifetimeTotal, getSessionContextPercent, type LifetimeUsage } from "./usage.js";
|
|
48
45
|
|
|
49
46
|
// ---- Shared helpers ----
|
|
@@ -442,37 +439,14 @@ export default function (pi: ExtensionAPI) {
|
|
|
442
439
|
// --- Cross-extension RPC via pi.events ---
|
|
443
440
|
let currentCtx: ExtensionContext | undefined;
|
|
444
441
|
|
|
445
|
-
//
|
|
446
|
-
// Session-scoped: store is constructed inside session_start once sessionId
|
|
447
|
-
// is available. Mirrors pi-chonky-tasks's session-scoped task store —
|
|
448
|
-
// schedules reset on /new, restore on /resume.
|
|
449
|
-
const scheduler = new SubagentScheduler();
|
|
450
|
-
|
|
451
|
-
function startScheduler(ctx: ExtensionContext) {
|
|
452
|
-
try {
|
|
453
|
-
const sessionId = ctx.sessionManager?.getSessionId?.();
|
|
454
|
-
if (!sessionId) return; // sessionId not yet available — try again on next event
|
|
455
|
-
const path = resolveStorePath(ctx.cwd, sessionId);
|
|
456
|
-
const store = new ScheduleStore(path);
|
|
457
|
-
scheduler.start(pi, ctx, manager, store);
|
|
458
|
-
pi.events.emit("subagents:scheduler_ready", { sessionId, jobCount: store.list().length });
|
|
459
|
-
} catch (err) {
|
|
460
|
-
// Scheduling is non-essential — log and move on so the rest of the
|
|
461
|
-
// extension keeps working if e.g. .pi/ is unwritable.
|
|
462
|
-
console.warn("[pi-subagents] Failed to start scheduler:", err);
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
// Capture ctx from session_start for RPC spawn handler + start the scheduler.
|
|
442
|
+
// Capture ctx from session_start for RPC spawn handler.
|
|
467
443
|
pi.on("session_start", async (_event, ctx) => {
|
|
468
444
|
currentCtx = ctx;
|
|
469
445
|
manager.clearCompleted();
|
|
470
|
-
if (isSchedulingEnabled() && !scheduler.isActive()) startScheduler(ctx);
|
|
471
446
|
});
|
|
472
447
|
|
|
473
448
|
pi.on("session_before_switch", () => {
|
|
474
449
|
manager.clearCompleted();
|
|
475
|
-
scheduler.stop();
|
|
476
450
|
});
|
|
477
451
|
|
|
478
452
|
const { unsubPing: unsubPingRpc, unsubSpawn: unsubSpawnRpc, unsubStop: unsubStopRpc } = registerRpcHandlers({
|
|
@@ -493,7 +467,6 @@ export default function (pi: ExtensionAPI) {
|
|
|
493
467
|
unsubPingRpc();
|
|
494
468
|
currentCtx = undefined;
|
|
495
469
|
delete (globalThis as any)[MANAGER_KEY];
|
|
496
|
-
scheduler.stop();
|
|
497
470
|
manager.abortAll();
|
|
498
471
|
for (const timer of pendingNudges.values()) clearTimeout(timer);
|
|
499
472
|
pendingNudges.clear();
|
|
@@ -508,15 +481,6 @@ export default function (pi: ExtensionAPI) {
|
|
|
508
481
|
function getDefaultJoinMode(): JoinMode { return defaultJoinMode; }
|
|
509
482
|
function setDefaultJoinMode(mode: JoinMode) { defaultJoinMode = mode; }
|
|
510
483
|
|
|
511
|
-
// Master switch for the schedule subagent feature. Defaults to enabled.
|
|
512
|
-
// Read once at extension init (before tool registration) so the Agent tool's
|
|
513
|
-
// param schema reflects the persisted setting. Runtime toggles via /agents
|
|
514
|
-
// → Settings short-circuit the menu entry + the execute-time addJob path
|
|
515
|
-
// immediately, but the schema-level removal only takes effect on next
|
|
516
|
-
// extension load (next pi session). Documented in CHANGELOG/README.
|
|
517
|
-
let schedulingEnabled = true;
|
|
518
|
-
function isSchedulingEnabled(): boolean { return schedulingEnabled; }
|
|
519
|
-
function setSchedulingEnabled(b: boolean) { schedulingEnabled = b; }
|
|
520
484
|
|
|
521
485
|
// ---- Batch tracking for smart join mode ----
|
|
522
486
|
// Collects background agent IDs spawned in the current turn for smart grouping.
|
|
@@ -611,35 +575,12 @@ export default function (pi: ExtensionAPI) {
|
|
|
611
575
|
setDefaultMaxTurns,
|
|
612
576
|
setGraceTurns,
|
|
613
577
|
setDefaultJoinMode,
|
|
614
|
-
setSchedulingEnabled,
|
|
615
578
|
},
|
|
616
579
|
(event, payload) => pi.events.emit(event, payload),
|
|
617
580
|
);
|
|
618
581
|
|
|
619
582
|
// ---- Agent tool ----
|
|
620
583
|
|
|
621
|
-
// Schedule param + its guideline are gated on `schedulingEnabled` (read once
|
|
622
|
-
// at registration; flipping the setting later requires next pi session for
|
|
623
|
-
// the schema to update). Defining the shape once and spreading it via Partial
|
|
624
|
-
// preserves Type.Object's inference when present and produces a
|
|
625
|
-
// `schedule`-free schema when absent — zero LLM-context cost in disabled mode.
|
|
626
|
-
const scheduleParamShape = {
|
|
627
|
-
schedule: Type.Optional(
|
|
628
|
-
Type.String({
|
|
629
|
-
description:
|
|
630
|
-
'Opt-in only — fire later instead of now. Omit to run immediately (the default, almost always correct). ' +
|
|
631
|
-
'Formats: 6-field cron ("0 0 9 * * 1" = 9am Mon), interval ("5m"/"1h"), one-shot ("+10m" or ISO). ' +
|
|
632
|
-
'Forces run_in_background; incompatible with inherit_context and resume. Returns job ID.',
|
|
633
|
-
}),
|
|
634
|
-
),
|
|
635
|
-
};
|
|
636
|
-
const scheduleParam: Partial<typeof scheduleParamShape> =
|
|
637
|
-
isSchedulingEnabled() ? scheduleParamShape : {};
|
|
638
|
-
|
|
639
|
-
const scheduleGuideline = isSchedulingEnabled()
|
|
640
|
-
? `\n- Use \`schedule\` only when the user explicitly asked for scheduled / recurring / delayed execution (e.g. "every Monday", "in an hour"). Don't auto-schedule from vague intent like "monitor X" — run once now or ask.`
|
|
641
|
-
: "";
|
|
642
|
-
|
|
643
584
|
pi.registerTool(defineTool({
|
|
644
585
|
name: "Agent",
|
|
645
586
|
label: "Agent",
|
|
@@ -663,7 +604,7 @@ Guidelines:
|
|
|
663
604
|
- Use model to specify a different model (as "provider/modelId", or fuzzy e.g. "haiku", "sonnet").
|
|
664
605
|
- Use thinking to control extended thinking level.
|
|
665
606
|
- Use inherit_context if the agent needs the parent conversation history.
|
|
666
|
-
- Use isolation: "worktree" to run the agent in an isolated git worktree (safe parallel file modifications)
|
|
607
|
+
- Use isolation: "worktree" to run the agent in an isolated git worktree (safe parallel file modifications).`,
|
|
667
608
|
parameters: Type.Object({
|
|
668
609
|
prompt: Type.String({
|
|
669
610
|
description: "The task for the agent to perform.",
|
|
@@ -716,7 +657,6 @@ Guidelines:
|
|
|
716
657
|
description: 'Set to "worktree" to run the agent in a temporary git worktree (isolated copy of the repo). Changes are saved to a branch on completion.',
|
|
717
658
|
}),
|
|
718
659
|
),
|
|
719
|
-
...scheduleParam,
|
|
720
660
|
}),
|
|
721
661
|
|
|
722
662
|
// ---- Custom rendering: Claude Code style ----
|
|
@@ -877,47 +817,6 @@ Guidelines:
|
|
|
877
817
|
tags: agentTags.length > 0 ? agentTags : undefined,
|
|
878
818
|
};
|
|
879
819
|
|
|
880
|
-
// ---- Schedule: register a job, don't spawn now ----
|
|
881
|
-
if (params.schedule) {
|
|
882
|
-
if (!isSchedulingEnabled()) {
|
|
883
|
-
return textResult("Scheduling is disabled in this project. Enable via /agents → Settings → Scheduling.");
|
|
884
|
-
}
|
|
885
|
-
if (params.resume) {
|
|
886
|
-
return textResult("Cannot combine `schedule` with `resume` — schedules create fresh agents.");
|
|
887
|
-
}
|
|
888
|
-
if (params.inherit_context) {
|
|
889
|
-
return textResult("Cannot combine `schedule` with `inherit_context` — there is no parent conversation at fire time.");
|
|
890
|
-
}
|
|
891
|
-
if (params.run_in_background === false) {
|
|
892
|
-
return textResult("Cannot combine `schedule` with `run_in_background: false` — scheduled jobs always run in background.");
|
|
893
|
-
}
|
|
894
|
-
if (!scheduler.isActive()) {
|
|
895
|
-
return textResult("Scheduler is not active in this session yet. Try again after the session has fully started.");
|
|
896
|
-
}
|
|
897
|
-
try {
|
|
898
|
-
const job = scheduler.addJob({
|
|
899
|
-
name: params.description as string,
|
|
900
|
-
description: params.description as string,
|
|
901
|
-
schedule: params.schedule as string,
|
|
902
|
-
subagent_type: subagentType,
|
|
903
|
-
prompt: params.prompt as string,
|
|
904
|
-
model: params.model as string | undefined,
|
|
905
|
-
thinking: thinking,
|
|
906
|
-
max_turns: effectiveMaxTurns,
|
|
907
|
-
isolated: isolated,
|
|
908
|
-
isolation: isolation,
|
|
909
|
-
});
|
|
910
|
-
const next = scheduler.getNextRun(job.id);
|
|
911
|
-
return textResult(
|
|
912
|
-
`Scheduled "${job.name}" (id: ${job.id}, type: ${job.scheduleType}). ` +
|
|
913
|
-
`Next run: ${next ?? "(unknown)"}. ` +
|
|
914
|
-
`Manage via /agents → Scheduled jobs.`,
|
|
915
|
-
);
|
|
916
|
-
} catch (err) {
|
|
917
|
-
return textResult(err instanceof Error ? err.message : String(err));
|
|
918
|
-
}
|
|
919
|
-
}
|
|
920
|
-
|
|
921
820
|
// Resume existing agent
|
|
922
821
|
if (params.resume) {
|
|
923
822
|
const existing = manager.getRecord(params.resume);
|
|
@@ -1294,12 +1193,6 @@ Guidelines:
|
|
|
1294
1193
|
options.push(`Agent types (${allNames.length})`);
|
|
1295
1194
|
}
|
|
1296
1195
|
|
|
1297
|
-
// Scheduled jobs entry (always present when scheduler is active)
|
|
1298
|
-
if (scheduler.isActive()) {
|
|
1299
|
-
const jobCount = scheduler.list().length;
|
|
1300
|
-
options.push(`Scheduled jobs (${jobCount})`);
|
|
1301
|
-
}
|
|
1302
|
-
|
|
1303
1196
|
// Actions
|
|
1304
1197
|
options.push("Create new agent");
|
|
1305
1198
|
options.push("Settings");
|
|
@@ -1323,9 +1216,6 @@ Guidelines:
|
|
|
1323
1216
|
} else if (choice.startsWith("Agent types (")) {
|
|
1324
1217
|
await showAllAgentsList(ctx);
|
|
1325
1218
|
await showAgentsMenu(ctx);
|
|
1326
|
-
} else if (choice.startsWith("Scheduled jobs (")) {
|
|
1327
|
-
await showSchedulesMenu(ctx, scheduler);
|
|
1328
|
-
await showAgentsMenu(ctx);
|
|
1329
1219
|
} else if (choice === "Create new agent") {
|
|
1330
1220
|
await showCreateWizard(ctx);
|
|
1331
1221
|
} else if (choice === "Settings") {
|
|
@@ -1789,7 +1679,6 @@ ${systemPrompt}
|
|
|
1789
1679
|
defaultMaxTurns: getDefaultMaxTurns() ?? 0,
|
|
1790
1680
|
graceTurns: getGraceTurns(),
|
|
1791
1681
|
defaultJoinMode: getDefaultJoinMode(),
|
|
1792
|
-
schedulingEnabled: isSchedulingEnabled(),
|
|
1793
1682
|
};
|
|
1794
1683
|
}
|
|
1795
1684
|
|
|
@@ -1799,7 +1688,6 @@ ${systemPrompt}
|
|
|
1799
1688
|
`Default max turns (current: ${getDefaultMaxTurns() ?? "unlimited"})`,
|
|
1800
1689
|
`Grace turns (current: ${getGraceTurns()})`,
|
|
1801
1690
|
`Join mode (current: ${getDefaultJoinMode()})`,
|
|
1802
|
-
`Scheduling (current: ${isSchedulingEnabled() ? "enabled" : "disabled"})`,
|
|
1803
1691
|
]);
|
|
1804
1692
|
if (!choice) return;
|
|
1805
1693
|
|
|
@@ -1850,27 +1738,6 @@ ${systemPrompt}
|
|
|
1850
1738
|
setDefaultJoinMode(mode);
|
|
1851
1739
|
notifyApplied(ctx, `Default join mode set to ${mode}`);
|
|
1852
1740
|
}
|
|
1853
|
-
} else if (choice.startsWith("Scheduling")) {
|
|
1854
|
-
const val = await ctx.ui.select(
|
|
1855
|
-
"Schedule subagent feature",
|
|
1856
|
-
[
|
|
1857
|
-
"enabled — Agent tool accepts a `schedule` param; /agents → Scheduled jobs visible",
|
|
1858
|
-
"disabled — `schedule` removed from Agent tool spec (no LLM-context cost); menu hidden",
|
|
1859
|
-
],
|
|
1860
|
-
);
|
|
1861
|
-
if (val) {
|
|
1862
|
-
const enabled = val.startsWith("enabled");
|
|
1863
|
-
if (enabled === isSchedulingEnabled()) {
|
|
1864
|
-
ctx.ui.notify(`Scheduling already ${enabled ? "enabled" : "disabled"}.`, "info");
|
|
1865
|
-
} else {
|
|
1866
|
-
setSchedulingEnabled(enabled);
|
|
1867
|
-
if (!enabled) scheduler.stop(); // immediate kill — outstanding fires stop ticking
|
|
1868
|
-
notifyApplied(
|
|
1869
|
-
ctx,
|
|
1870
|
-
`Scheduling ${enabled ? "enabled" : "disabled"}. Tool spec change takes effect on next pi session.`,
|
|
1871
|
-
);
|
|
1872
|
-
}
|
|
1873
|
-
}
|
|
1874
1741
|
}
|
|
1875
1742
|
}
|
|
1876
1743
|
|
package/src/settings.ts
CHANGED
|
@@ -17,15 +17,6 @@ export interface SubagentsSettings {
|
|
|
17
17
|
defaultMaxTurns?: number;
|
|
18
18
|
graceTurns?: number;
|
|
19
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
20
|
}
|
|
30
21
|
|
|
31
22
|
/** Setter hooks used by applySettings to wire persisted values into in-memory state. */
|
|
@@ -34,7 +25,6 @@ export interface SettingsAppliers {
|
|
|
34
25
|
setDefaultMaxTurns: (n: number) => void;
|
|
35
26
|
setGraceTurns: (n: number) => void;
|
|
36
27
|
setDefaultJoinMode: (mode: JoinMode) => void;
|
|
37
|
-
setSchedulingEnabled: (b: boolean) => void;
|
|
38
28
|
}
|
|
39
29
|
|
|
40
30
|
/** Emit callback — a subset of `pi.events.emit` to keep helpers testable. */
|
|
@@ -78,9 +68,6 @@ function sanitize(raw: unknown): SubagentsSettings {
|
|
|
78
68
|
if (typeof r.defaultJoinMode === "string" && VALID_JOIN_MODES.has(r.defaultJoinMode)) {
|
|
79
69
|
out.defaultJoinMode = r.defaultJoinMode as JoinMode;
|
|
80
70
|
}
|
|
81
|
-
if (typeof r.schedulingEnabled === "boolean") {
|
|
82
|
-
out.schedulingEnabled = r.schedulingEnabled;
|
|
83
|
-
}
|
|
84
71
|
return out;
|
|
85
72
|
}
|
|
86
73
|
|
|
@@ -135,7 +122,6 @@ export function applySettings(s: SubagentsSettings, appliers: SettingsAppliers):
|
|
|
135
122
|
if (typeof s.defaultMaxTurns === "number") appliers.setDefaultMaxTurns(s.defaultMaxTurns);
|
|
136
123
|
if (typeof s.graceTurns === "number") appliers.setGraceTurns(s.graceTurns);
|
|
137
124
|
if (s.defaultJoinMode) appliers.setDefaultJoinMode(s.defaultJoinMode);
|
|
138
|
-
if (typeof s.schedulingEnabled === "boolean") appliers.setSchedulingEnabled(s.schedulingEnabled);
|
|
139
125
|
}
|
|
140
126
|
|
|
141
127
|
/**
|
package/src/types.ts
CHANGED
|
@@ -131,46 +131,3 @@ export interface EnvInfo {
|
|
|
131
131
|
branch: string;
|
|
132
132
|
platform: string;
|
|
133
133
|
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* A subagent spawn registered to fire on a schedule.
|
|
137
|
-
*
|
|
138
|
-
* Stored at `<cwd>/.pi/subagent-schedules/<sessionId>.json`. Session-scoped:
|
|
139
|
-
* survives `/resume` but resets on `/new`, mirroring pi-chonky-tasks.
|
|
140
|
-
*/
|
|
141
|
-
export interface ScheduledSubagent {
|
|
142
|
-
id: string;
|
|
143
|
-
/** Unique within store. Defaults to `description`. */
|
|
144
|
-
name: string;
|
|
145
|
-
description: string;
|
|
146
|
-
/** Raw user input — cron expr | "+10m" | ISO | "5m". */
|
|
147
|
-
schedule: string;
|
|
148
|
-
scheduleType: "cron" | "once" | "interval";
|
|
149
|
-
/** Computed at create time for interval/once. */
|
|
150
|
-
intervalMs?: number;
|
|
151
|
-
|
|
152
|
-
// spawn params (subset of Agent tool params; no inherit_context, no resume)
|
|
153
|
-
subagent_type: SubagentType;
|
|
154
|
-
prompt: string;
|
|
155
|
-
model?: string;
|
|
156
|
-
thinking?: ThinkingLevel;
|
|
157
|
-
max_turns?: number;
|
|
158
|
-
isolated?: boolean;
|
|
159
|
-
isolation?: IsolationMode;
|
|
160
|
-
|
|
161
|
-
// state
|
|
162
|
-
enabled: boolean;
|
|
163
|
-
/** ISO timestamp. */
|
|
164
|
-
createdAt: string;
|
|
165
|
-
lastRun?: string;
|
|
166
|
-
lastStatus?: "success" | "error" | "running";
|
|
167
|
-
/** Refreshed on every fire and on store load. */
|
|
168
|
-
nextRun?: string;
|
|
169
|
-
runCount: number;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
export interface ScheduleStoreData {
|
|
173
|
-
/** For future migrations. */
|
|
174
|
-
version: 1;
|
|
175
|
-
jobs: ScheduledSubagent[];
|
|
176
|
-
}
|
package/src/schedule-store.ts
DELETED
|
@@ -1,143 +0,0 @@
|
|
|
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
|
-
mkdirSync(dirname(filePath), { recursive: true });
|
|
66
|
-
this.load();
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/** Load from disk into the in-memory cache. Silent on parse errors. */
|
|
70
|
-
private load(): void {
|
|
71
|
-
if (!existsSync(this.filePath)) return;
|
|
72
|
-
try {
|
|
73
|
-
const data: ScheduleStoreData = JSON.parse(readFileSync(this.filePath, "utf-8"));
|
|
74
|
-
this.jobs.clear();
|
|
75
|
-
for (const j of data.jobs ?? []) this.jobs.set(j.id, j);
|
|
76
|
-
} catch { /* corrupt — start fresh, next save rewrites */ }
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/** Atomic write via temp file + rename (POSIX-atomic). */
|
|
80
|
-
private save(): void {
|
|
81
|
-
const data: ScheduleStoreData = { version: 1, jobs: [...this.jobs.values()] };
|
|
82
|
-
const tmp = this.filePath + ".tmp";
|
|
83
|
-
writeFileSync(tmp, JSON.stringify(data, null, 2));
|
|
84
|
-
renameSync(tmp, this.filePath);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/** Acquire lock → reload → mutate → save → release. */
|
|
88
|
-
private withLock<T>(fn: () => T): T {
|
|
89
|
-
acquireLock(this.lockPath);
|
|
90
|
-
try {
|
|
91
|
-
this.load();
|
|
92
|
-
const result = fn();
|
|
93
|
-
this.save();
|
|
94
|
-
return result;
|
|
95
|
-
} finally {
|
|
96
|
-
releaseLock(this.lockPath);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/** Read-only — returns a snapshot of the in-memory cache. */
|
|
101
|
-
list(): ScheduledSubagent[] {
|
|
102
|
-
return [...this.jobs.values()];
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/** Read-only check — uses the cache. */
|
|
106
|
-
hasName(name: string, exceptId?: string): boolean {
|
|
107
|
-
for (const j of this.jobs.values()) {
|
|
108
|
-
if (j.id !== exceptId && j.name === name) return true;
|
|
109
|
-
}
|
|
110
|
-
return false;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
get(id: string): ScheduledSubagent | undefined {
|
|
114
|
-
return this.jobs.get(id);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
add(job: ScheduledSubagent): void {
|
|
118
|
-
this.withLock(() => {
|
|
119
|
-
this.jobs.set(job.id, job);
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
update(id: string, patch: Partial<ScheduledSubagent>): ScheduledSubagent | undefined {
|
|
124
|
-
return this.withLock(() => {
|
|
125
|
-
const existing = this.jobs.get(id);
|
|
126
|
-
if (!existing) return undefined;
|
|
127
|
-
const updated = { ...existing, ...patch };
|
|
128
|
-
this.jobs.set(id, updated);
|
|
129
|
-
return updated;
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
remove(id: string): boolean {
|
|
134
|
-
return this.withLock(() => this.jobs.delete(id));
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/** Delete the backing file (used when no jobs remain, optional cleanup). */
|
|
138
|
-
deleteFileIfEmpty(): void {
|
|
139
|
-
if (this.jobs.size === 0 && existsSync(this.filePath)) {
|
|
140
|
-
try { unlinkSync(this.filePath); } catch { /* ignore */ }
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
}
|