@timmyjl12/pi-subagents 0.20.0 → 0.21.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 +862 -827
- package/README.md +1125 -1080
- package/dist/active-agents-prompt.d.ts +31 -9
- package/dist/active-agents-prompt.js +73 -9
- package/dist/agent-color.js +9 -1
- package/dist/agent-file-toggle.d.ts +1 -1
- package/dist/agent-manager.d.ts +405 -18
- package/dist/agent-manager.js +1661 -131
- package/dist/agent-runner.d.ts +45 -6
- package/dist/agent-runner.js +109 -18
- package/dist/agent-types.d.ts +30 -7
- package/dist/agent-types.js +38 -9
- package/dist/awaited-result.d.ts +37 -0
- package/dist/awaited-result.js +150 -0
- package/dist/child-session-shutdown.d.ts +19 -1
- package/dist/child-session-shutdown.js +60 -11
- package/dist/cleanup-plans.d.ts +10 -0
- package/dist/cleanup-plans.js +112 -0
- package/dist/cross-extension-rpc.d.ts +8 -1
- package/dist/cross-extension-rpc.js +59 -5
- package/dist/custom-agents.d.ts +34 -9
- package/dist/custom-agents.js +308 -111
- package/dist/default-agents.d.ts +1 -1
- package/dist/env.d.ts +1 -1
- package/dist/error-log.d.ts +6 -0
- package/dist/error-log.js +8 -0
- package/dist/force-main-model.d.ts +21 -0
- package/dist/force-main-model.js +28 -0
- package/dist/goal-bridge.d.ts +155 -0
- package/dist/goal-bridge.js +305 -0
- package/dist/group-join.d.ts +5 -1
- package/dist/group-join.js +52 -7
- package/dist/index.d.ts +5 -5
- package/dist/index.js +2827 -333
- package/dist/invocation-config.d.ts +30 -3
- package/dist/invocation-config.js +30 -1
- package/dist/memory.d.ts +1 -1
- package/dist/mention-clone.d.ts +1 -1
- package/dist/model-scope.d.ts +1 -1
- package/dist/nested-tools.d.ts +22 -1
- package/dist/nested-tools.js +120 -26
- package/dist/output-file.d.ts +15 -4
- package/dist/output-file.js +146 -43
- package/dist/prompts.d.ts +1 -1
- package/dist/recovery.d.ts +1 -1
- package/dist/report-cleanup.d.ts +204 -0
- package/dist/report-cleanup.js +1632 -0
- package/dist/report-store.d.ts +738 -0
- package/dist/report-store.js +3612 -0
- package/dist/schedule-store.d.ts +14 -6
- package/dist/schedule-store.js +88 -58
- package/dist/schedule.d.ts +20 -8
- package/dist/schedule.js +135 -74
- package/dist/settings.d.ts +39 -11
- package/dist/settings.js +22 -5
- package/dist/status-note.d.ts +1 -1
- package/dist/structured-output.d.ts +1 -1
- package/dist/types.d.ts +32 -3
- package/dist/ui/agent-mention.d.ts +2 -2
- package/dist/ui/agent-widget.d.ts +82 -13
- package/dist/ui/agent-widget.js +155 -37
- package/dist/ui/cleanup-preview.d.ts +59 -0
- package/dist/ui/cleanup-preview.js +466 -0
- package/dist/ui/conversation-viewer.d.ts +4 -4
- package/dist/ui/conversation-viewer.js +10 -6
- package/dist/ui/fleet-list.d.ts +3 -3
- package/dist/ui/fleet-list.js +3 -3
- package/dist/ui/fleet-overlay.d.ts +37 -6
- package/dist/ui/fleet-overlay.js +214 -18
- package/dist/ui/hub-feed.d.ts +52 -0
- package/dist/ui/hub-feed.js +148 -0
- package/dist/ui/native-transcript.js +5 -1
- package/dist/ui/schedule-menu.d.ts +1 -1
- package/dist/ui/schedule-menu.js +1 -1
- package/dist/ui/workflow-card.d.ts +4 -4
- package/dist/ui/workflow-dialog.d.ts +4 -4
- package/dist/ui/workflow-dialog.js +8 -3
- package/dist/ui/workflow-menu.d.ts +2 -2
- package/dist/usage.d.ts +12 -0
- package/dist/usage.js +28 -1
- package/dist/workflow/entry.d.ts +3 -3
- package/dist/workflow/host.d.ts +7 -4
- package/dist/workflow/host.js +162 -48
- package/dist/workflow/journal.d.ts +23 -7
- package/dist/workflow/journal.js +80 -23
- package/dist/workflow/progress.d.ts +1 -1
- package/dist/workflow/runtime.d.ts +45 -6
- package/dist/workflow/runtime.js +105 -16
- package/dist/workflow/task.d.ts +13 -4
- package/dist/workflow/task.js +7 -1
- package/dist/workflow/tool-description.d.ts +7 -2
- package/dist/workflow/tool-description.js +10 -5
- package/docs/{FORK.md → DEVELOPMENT.md} +4 -4
- package/docs/artifacts.md +93 -0
- package/docs/rpc.md +252 -189
- package/docs/runtime-performance.md +53 -0
- package/docs/workflows.md +48 -18
- package/examples/agent-tool-description.md +2 -2
- package/examples/workflows/review-panel.js +6 -3
- package/package.json +5 -5
- package/src/active-agents-prompt.ts +115 -39
- package/src/agent-color.ts +10 -2
- package/src/agent-file-toggle.ts +2 -2
- package/src/agent-manager.ts +3312 -1601
- package/src/agent-runner.ts +164 -36
- package/src/agent-types.ts +56 -11
- package/src/awaited-result.ts +187 -0
- package/src/child-session-shutdown.ts +114 -46
- package/src/cleanup-plans.ts +112 -0
- package/src/cross-extension-rpc.ts +68 -9
- package/src/custom-agents.ts +384 -112
- package/src/default-agents.ts +1 -1
- package/src/enabled-models.ts +1 -1
- package/src/env.ts +1 -1
- package/src/error-log.ts +10 -1
- package/src/force-main-model.ts +29 -0
- package/src/goal-bridge.ts +468 -0
- package/src/group-join.ts +56 -9
- package/src/index.ts +2987 -349
- package/src/invocation-config.ts +50 -4
- package/src/memory.ts +1 -1
- package/src/mention-clone.ts +3 -3
- package/src/model-scope.ts +1 -1
- package/src/nested-tools.ts +164 -33
- package/src/output-file.ts +168 -46
- package/src/prompts.ts +1 -1
- package/src/recovery.ts +2 -2
- package/src/report-cleanup.ts +2044 -0
- package/src/report-store.ts +4130 -0
- package/src/schedule-store.ts +244 -201
- package/src/schedule.ts +144 -80
- package/src/settings.ts +61 -16
- package/src/skill-loader.ts +1 -1
- package/src/standalone-fallback.ts +2 -2
- package/src/status-note.ts +1 -1
- package/src/structured-output.ts +1 -1
- package/src/types.ts +33 -3
- package/src/ui/agent-mention.ts +3 -3
- package/src/ui/agent-widget.ts +179 -45
- package/src/ui/cleanup-preview.ts +518 -0
- package/src/ui/conversation-viewer.ts +19 -15
- package/src/ui/fleet-list.ts +7 -7
- package/src/ui/fleet-overlay.ts +1048 -847
- package/src/ui/hub-feed.ts +179 -0
- package/src/ui/native-transcript.ts +7 -3
- package/src/ui/schedule-menu.ts +4 -4
- package/src/ui/workflow-card.ts +4 -4
- package/src/ui/workflow-dialog.ts +10 -6
- package/src/ui/workflow-menu.ts +4 -4
- package/src/usage.ts +53 -3
- package/src/workflow/collisions.ts +1 -1
- package/src/workflow/entry.ts +3 -3
- package/src/workflow/host.ts +174 -54
- package/src/workflow/journal.ts +80 -23
- package/src/workflow/progress.ts +1 -1
- package/src/workflow/runtime.ts +146 -23
- package/src/workflow/saved.ts +3 -3
- package/src/workflow/task.ts +18 -7
- package/src/workflow/tool-description.ts +10 -5
package/src/schedule-store.ts
CHANGED
|
@@ -1,201 +1,244 @@
|
|
|
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 { randomBytes } from "node:crypto";
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
import
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const
|
|
21
|
-
/**
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
* A
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
const
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
}
|
|
201
|
-
|
|
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 { randomBytes } from "node:crypto";
|
|
14
|
+
import { readFileSync } from "node:fs";
|
|
15
|
+
import { mkdir, readFile, rename, stat, unlink, writeFile } from "node:fs/promises";
|
|
16
|
+
import { dirname, join } from "node:path";
|
|
17
|
+
import { setTimeout as sleep } from "node:timers/promises";
|
|
18
|
+
import type { ScheduledSubagent, ScheduleStoreData } from "./types.ts";
|
|
19
|
+
|
|
20
|
+
const LOCK_RETRY_MS = 50;
|
|
21
|
+
/** Total asynchronous wait for the lock. */
|
|
22
|
+
const LOCK_MAX_WAIT_MS = 1000;
|
|
23
|
+
/**
|
|
24
|
+
* A lock older than this is stale whatever its PID says: Windows reuses PIDs
|
|
25
|
+
* aggressively, so "that process is alive" does not mean it holds the lock.
|
|
26
|
+
* A mutation holds the lock for a few milliseconds.
|
|
27
|
+
*/
|
|
28
|
+
export const LOCK_STALE_MS = 30_000;
|
|
29
|
+
const RENAME_RETRIES = 5;
|
|
30
|
+
const RENAME_RETRY_MS = 20;
|
|
31
|
+
|
|
32
|
+
function isProcessRunning(pid: number): boolean {
|
|
33
|
+
try {
|
|
34
|
+
process.kill(pid, 0);
|
|
35
|
+
return true;
|
|
36
|
+
} catch {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function errorCode(e: unknown): string | undefined {
|
|
42
|
+
const code = (e as { code?: unknown } | null)?.code;
|
|
43
|
+
return typeof code === "string" ? code : undefined;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** True when the existing lock file can be broken: too old, or its owner is gone. */
|
|
47
|
+
async function isLockStale(lockPath: string): Promise<boolean> {
|
|
48
|
+
try {
|
|
49
|
+
if (Date.now() - (await stat(lockPath)).mtimeMs > LOCK_STALE_MS) return true;
|
|
50
|
+
const pid = parseInt(await readFile(lockPath, "utf-8"), 10);
|
|
51
|
+
return Number.isInteger(pid) && pid > 0 && !isProcessRunning(pid);
|
|
52
|
+
} catch (e) {
|
|
53
|
+
// Vanished between our write attempt and this check: retry the write.
|
|
54
|
+
return errorCode(e) === "ENOENT";
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function acquireLock(lockPath: string): Promise<void> {
|
|
59
|
+
const deadline = Date.now() + LOCK_MAX_WAIT_MS;
|
|
60
|
+
for (;;) {
|
|
61
|
+
try {
|
|
62
|
+
await writeFile(lockPath, `${process.pid}`, { flag: "wx" });
|
|
63
|
+
return;
|
|
64
|
+
} catch (e) {
|
|
65
|
+
if (errorCode(e) !== "EEXIST") throw e;
|
|
66
|
+
}
|
|
67
|
+
if (await isLockStale(lockPath)) {
|
|
68
|
+
try {
|
|
69
|
+
await unlink(lockPath);
|
|
70
|
+
} catch (e) {
|
|
71
|
+
if (errorCode(e) !== "ENOENT") throw e;
|
|
72
|
+
}
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (Date.now() >= deadline) break;
|
|
76
|
+
await sleep(LOCK_RETRY_MS);
|
|
77
|
+
}
|
|
78
|
+
throw new Error(`Failed to acquire schedule lock: ${lockPath}`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** rename with a bounded retry: Windows reports EPERM/EBUSY while AV or an indexer holds the target. */
|
|
82
|
+
async function renameWithRetry(from: string, to: string): Promise<void> {
|
|
83
|
+
for (let attempt = 0; ; attempt++) {
|
|
84
|
+
try {
|
|
85
|
+
await rename(from, to);
|
|
86
|
+
return;
|
|
87
|
+
} catch (e) {
|
|
88
|
+
const code = errorCode(e);
|
|
89
|
+
const transient = code === "EPERM" || code === "EBUSY" || code === "EACCES";
|
|
90
|
+
if (!transient || attempt >= RENAME_RETRIES) {
|
|
91
|
+
// Best effort: the rename error below is the one that matters.
|
|
92
|
+
await unlink(from).catch(() => undefined);
|
|
93
|
+
throw e;
|
|
94
|
+
}
|
|
95
|
+
await sleep(RENAME_RETRY_MS * (attempt + 1));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function releaseLock(lockPath: string): Promise<void> {
|
|
101
|
+
await unlink(lockPath).catch(() => undefined);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Resolve the storage path for a session-scoped store. */
|
|
105
|
+
export function resolveStorePath(cwd: string, sessionId: string): string {
|
|
106
|
+
return join(cwd, ".pi", "subagent-schedules", `${sessionId}.json`);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function parseStoreData(serialized: string): ScheduleStoreData | undefined {
|
|
110
|
+
try {
|
|
111
|
+
return JSON.parse(serialized) as ScheduleStoreData;
|
|
112
|
+
} catch {
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export class ScheduleStore {
|
|
118
|
+
private filePath: string;
|
|
119
|
+
private lockPath: string;
|
|
120
|
+
private jobs = new Map<string, ScheduledSubagent>();
|
|
121
|
+
|
|
122
|
+
constructor(filePath: string) {
|
|
123
|
+
this.filePath = filePath;
|
|
124
|
+
this.lockPath = filePath + ".lock";
|
|
125
|
+
this.loadInitial();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Create the backing directory lazily — only when we're about to persist. */
|
|
129
|
+
private async ensureDir(): Promise<void> {
|
|
130
|
+
await mkdir(dirname(this.filePath), { recursive: true });
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* One cold synchronous read preserves the synchronous constructor and the
|
|
135
|
+
* list/get snapshot API. Every reload on a mutation path uses async I/O.
|
|
136
|
+
*/
|
|
137
|
+
private loadInitial(): void {
|
|
138
|
+
let serialized: string;
|
|
139
|
+
try {
|
|
140
|
+
serialized = readFileSync(this.filePath, "utf-8");
|
|
141
|
+
} catch {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
const data = parseStoreData(serialized);
|
|
145
|
+
if (data) this.replaceJobs(data);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Reload from disk into the in-memory cache. Silent on parse errors. */
|
|
149
|
+
private async load(): Promise<void> {
|
|
150
|
+
let serialized: string;
|
|
151
|
+
try {
|
|
152
|
+
serialized = await readFile(this.filePath, "utf-8");
|
|
153
|
+
} catch (e) {
|
|
154
|
+
if (errorCode(e) === "ENOENT") return;
|
|
155
|
+
throw e;
|
|
156
|
+
}
|
|
157
|
+
const data = parseStoreData(serialized);
|
|
158
|
+
if (data) this.replaceJobs(data);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
private replaceJobs(data: ScheduleStoreData): void {
|
|
162
|
+
this.jobs.clear();
|
|
163
|
+
for (const j of data.jobs ?? []) this.jobs.set(j.id, j);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Atomic write via temp file + rename (POSIX-atomic). */
|
|
167
|
+
private async save(): Promise<void> {
|
|
168
|
+
const data: ScheduleStoreData = { version: 1, jobs: [...this.jobs.values()] };
|
|
169
|
+
// Unique per write, so two writers can never clobber each other's temp file.
|
|
170
|
+
const tmp = `${this.filePath}.${process.pid}.${randomBytes(4).toString("hex")}.tmp`;
|
|
171
|
+
await writeFile(tmp, JSON.stringify(data, null, 2));
|
|
172
|
+
await renameWithRetry(tmp, this.filePath);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Acquire lock → reload → mutate → save → release. */
|
|
176
|
+
private async withLock<T>(fn: () => T): Promise<T> {
|
|
177
|
+
await this.ensureDir();
|
|
178
|
+
await acquireLock(this.lockPath);
|
|
179
|
+
try {
|
|
180
|
+
await this.load();
|
|
181
|
+
const result = fn();
|
|
182
|
+
await this.save();
|
|
183
|
+
return result;
|
|
184
|
+
} finally {
|
|
185
|
+
await releaseLock(this.lockPath);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** Read-only — returns a snapshot of the in-memory cache. */
|
|
190
|
+
list(): ScheduledSubagent[] {
|
|
191
|
+
return [...this.jobs.values()];
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Read-only check — uses the cache. */
|
|
195
|
+
hasName(name: string, exceptId?: string): boolean {
|
|
196
|
+
for (const j of this.jobs.values()) {
|
|
197
|
+
if (j.id !== exceptId && j.name === name) return true;
|
|
198
|
+
}
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
get(id: string): ScheduledSubagent | undefined {
|
|
203
|
+
return this.jobs.get(id);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
async add(job: ScheduledSubagent): Promise<void> {
|
|
207
|
+
await this.withLock(() => {
|
|
208
|
+
this.jobs.set(job.id, job);
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** Atomically re-check the latest committed names and insert only if unique. */
|
|
213
|
+
async addUnlessNameExists(job: ScheduledSubagent): Promise<boolean> {
|
|
214
|
+
return this.withLock(() => {
|
|
215
|
+
if (this.hasName(job.name)) return false;
|
|
216
|
+
this.jobs.set(job.id, job);
|
|
217
|
+
return true;
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
async update(id: string, patch: Partial<ScheduledSubagent>): Promise<ScheduledSubagent | undefined> {
|
|
222
|
+
// No-op fast path — an unknown id changes nothing, so don't lock or touch
|
|
223
|
+
// disk (which would otherwise lazily create the backing directory).
|
|
224
|
+
if (!this.jobs.has(id)) return undefined;
|
|
225
|
+
return this.withLock(() => {
|
|
226
|
+
const existing = this.jobs.get(id);
|
|
227
|
+
if (!existing) return undefined;
|
|
228
|
+
const updated = { ...existing, ...patch };
|
|
229
|
+
this.jobs.set(id, updated);
|
|
230
|
+
return updated;
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
async remove(id: string): Promise<boolean> {
|
|
235
|
+
// No-op fast path — see update().
|
|
236
|
+
if (!this.jobs.has(id)) return false;
|
|
237
|
+
return this.withLock(() => this.jobs.delete(id));
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** Delete the backing file (used when no jobs remain, optional cleanup). */
|
|
241
|
+
async deleteFileIfEmpty(): Promise<void> {
|
|
242
|
+
if (this.jobs.size === 0) await unlink(this.filePath).catch(() => undefined);
|
|
243
|
+
}
|
|
244
|
+
}
|