@anhlp/alp-daemon 0.0.0-stage → 0.0.1-beta.4
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/LICENSE +202 -0
- package/NOTICE +7 -0
- package/README.md +14 -2
- package/dist/THIRD-PARTY-NOTICES.txt +431 -0
- package/dist/alp-templates.d.ts +5 -0
- package/dist/alpd.js +49562 -0
- package/dist/attachments.d.ts +211 -0
- package/dist/attachments.js +819 -0
- package/dist/build-manifest.json +131 -0
- package/dist/conversation.d.ts +119 -0
- package/dist/conversation.js +672 -0
- package/dist/files.d.ts +123 -0
- package/dist/files.js +365 -0
- package/dist/git.d.ts +87 -0
- package/dist/git.js +1093 -0
- package/dist/gitignore.d.ts +62 -0
- package/dist/gitignore.js +254 -0
- package/dist/history.d.ts +24 -0
- package/dist/history.js +149 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +25 -0
- package/dist/lifecycle.d.ts +101 -0
- package/dist/lifecycle.js +239 -0
- package/dist/lock.d.ts +21 -0
- package/dist/lock.js +42 -0
- package/dist/main.d.ts +1 -0
- package/dist/main.js +277 -0
- package/dist/operations.d.ts +23 -0
- package/dist/operations.js +275 -0
- package/dist/registry.d.ts +89 -0
- package/dist/registry.js +343 -0
- package/dist/rpc-error.d.ts +6 -0
- package/dist/rpc-error.js +11 -0
- package/dist/scheduler.d.ts +96 -0
- package/dist/scheduler.js +290 -0
- package/dist/schedules.d.ts +69 -0
- package/dist/schedules.js +244 -0
- package/dist/server.d.ts +58 -0
- package/dist/server.js +1780 -0
- package/dist/store.d.ts +176 -0
- package/dist/store.js +385 -0
- package/dist/terminals.d.ts +157 -0
- package/dist/terminals.js +487 -0
- package/dist/worktrees.d.ts +41 -0
- package/dist/worktrees.js +246 -0
- package/package.json +159 -4
- package/src/alp-templates.d.ts +5 -0
- package/src/attachments.ts +870 -0
- package/src/conversation.ts +837 -0
- package/src/files.ts +459 -0
- package/src/git.ts +1045 -0
- package/src/gitignore.ts +277 -0
- package/src/history.ts +118 -0
- package/src/index.ts +11 -0
- package/src/lifecycle.ts +283 -0
- package/src/lock.ts +53 -0
- package/src/main.ts +292 -0
- package/src/operations.ts +300 -0
- package/src/registry.ts +238 -0
- package/src/rpc-error.ts +4 -0
- package/src/scheduler.ts +422 -0
- package/src/schedules.ts +294 -0
- package/src/server.ts +2056 -0
- package/src/store.ts +448 -0
- package/src/terminals.ts +591 -0
- package/src/worktrees.ts +287 -0
package/src/schedules.ts
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import { realpathSync } from 'node:fs';
|
|
3
|
+
import { mkdir, readdir, readFile, rm } from 'node:fs/promises';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { writeAtomic } from '@anhlp/alp-core/atomic';
|
|
6
|
+
import { createLogger } from '@anhlp/alp-core/log';
|
|
7
|
+
import type { ScheduleJob, ScheduleStatus } from '@anhlp/alp-protocol/index';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* alpd's scheduled continuations: one JSON file per job under
|
|
11
|
+
* `$ALP_HOME/schedules`, written atomically. The directory is its own, beside the
|
|
12
|
+
* state directory and not inside it: the state has a schema version of its own
|
|
13
|
+
* (packages/core/src/state-schema.js), and a job is not part of that state, so an
|
|
14
|
+
* ALP that does not know jobs keeps working beside one that does.
|
|
15
|
+
*
|
|
16
|
+
* A job is one-shot: it waits for its time, is delivered once, and then says what
|
|
17
|
+
* became of it. Nothing here delivers anything; the daemon's scheduler does that.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
const log = createLogger({ component: 'alpd.schedule' });
|
|
21
|
+
|
|
22
|
+
/** The version of a job file; a file of another version is not read. */
|
|
23
|
+
export const SCHEDULE_VERSION = 1;
|
|
24
|
+
/** The id of a job: `sch_` and sixteen hex digits. */
|
|
25
|
+
export const SCHEDULE_ID = /^sch_[0-9a-f]{16}$/;
|
|
26
|
+
/** The most a job's prompt may hold, in characters. */
|
|
27
|
+
export const PROMPT_CHARS = 8000;
|
|
28
|
+
|
|
29
|
+
const STATUSES: ScheduleStatus[] = ['pending', 'delivered', 'failed', 'cancelled'];
|
|
30
|
+
|
|
31
|
+
export type ScheduleStore = ReturnType<typeof createScheduleStore>;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The key a project is compared by: the directory it really names. A client that names
|
|
35
|
+
* the same directory in another spelling (a trailing slash, a doubled separator, a
|
|
36
|
+
* symlinked parent, a different case on a case-insensitive volume) must see the same
|
|
37
|
+
* jobs; the job of a project is the job of that directory.
|
|
38
|
+
*/
|
|
39
|
+
export function projectKey(root: string): string {
|
|
40
|
+
const resolved = path.resolve(root);
|
|
41
|
+
// A path that is not there is compared as it was named: there is nothing to resolve.
|
|
42
|
+
try { return realpathSync.native(resolved); }
|
|
43
|
+
catch { return resolved; }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The project a request names: an absolute path, or nothing. A relative path would be
|
|
48
|
+
* read against the daemon's own directory, which is another project, so it is refused
|
|
49
|
+
* here as well as in the daemon.
|
|
50
|
+
*/
|
|
51
|
+
function namedProject(root?: string): string | undefined {
|
|
52
|
+
if (root === undefined) return undefined;
|
|
53
|
+
if (!path.isAbsolute(root)) throw new ScheduleStoreError('INVALID_SCHEDULE', 'A project is named by its absolute path');
|
|
54
|
+
return root;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** A refusal of the job store: what it refused, and why. */
|
|
58
|
+
export class ScheduleStoreError extends Error {
|
|
59
|
+
constructor(public code: 'INVALID_SCHEDULE' | 'SCHEDULE_NOT_FOUND' | 'SCHEDULE_NOT_PENDING', message: string) {
|
|
60
|
+
super(message);
|
|
61
|
+
this.name = 'ScheduleStoreError';
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** A job as it is written and read back: the wire shape, and nothing else. */
|
|
66
|
+
export type ScheduleRecord = ScheduleJob;
|
|
67
|
+
|
|
68
|
+
const isStatus = (value: unknown): value is ScheduleStatus => typeof value === 'string' && (STATUSES as string[]).includes(value);
|
|
69
|
+
|
|
70
|
+
/** A job file that is not a job: it is skipped, and the reason is logged. */
|
|
71
|
+
function readJob(text: string, name: string): ScheduleRecord | undefined {
|
|
72
|
+
let record: any;
|
|
73
|
+
try { record = JSON.parse(text); } catch {
|
|
74
|
+
log.warn(`skipped unreadable job ${name}`, { code: 'ALP_SCHEDULE_UNREADABLE', file: name });
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
/** A time of the job, as the wire type writes it. */
|
|
78
|
+
const isTime = (value: unknown) => typeof value === 'string' && !Number.isNaN(Date.parse(value));
|
|
79
|
+
/** The time a job is due: the contract writes it as UTC, ISO 8601, and so does the store. */
|
|
80
|
+
const isDueAt = (value: unknown) => typeof value === 'string' && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,3})?(Z|[+-]\d{2}:\d{2})$/.test(value) && !Number.isNaN(Date.parse(value));
|
|
81
|
+
const isError = (value: unknown) => typeof value === 'object' && value !== null
|
|
82
|
+
&& typeof (value as any).message === 'string'
|
|
83
|
+
&& ((value as any).code === undefined || typeof (value as any).code === 'string');
|
|
84
|
+
const sound = record?.version === SCHEDULE_VERSION
|
|
85
|
+
&& typeof record.id === 'string' && SCHEDULE_ID.test(record.id)
|
|
86
|
+
&& name === `${record.id}.json`
|
|
87
|
+
&& isStatus(record.status)
|
|
88
|
+
&& typeof record.projectRoot === 'string' && record.projectRoot
|
|
89
|
+
&& typeof record.sessionId === 'string' && record.sessionId
|
|
90
|
+
&& typeof record.prompt === 'string'
|
|
91
|
+
&& isDueAt(record.dueAt)
|
|
92
|
+
&& isTime(record.createdAt) && isTime(record.updatedAt)
|
|
93
|
+
&& (record.attemptedAt === undefined || isTime(record.attemptedAt))
|
|
94
|
+
&& (record.deliveredAt === undefined || isTime(record.deliveredAt))
|
|
95
|
+
&& (record.failedAt === undefined || isTime(record.failedAt))
|
|
96
|
+
&& (record.cancelledAt === undefined || isTime(record.cancelledAt))
|
|
97
|
+
&& (record.lateByMs === undefined || (Number.isSafeInteger(record.lateByMs) && record.lateByMs >= 0))
|
|
98
|
+
&& (record.attempts === undefined || (Number.isSafeInteger(record.attempts) && record.attempts >= 0))
|
|
99
|
+
&& (record.lastError === undefined || isError(record.lastError));
|
|
100
|
+
if (!sound) {
|
|
101
|
+
log.warn(`skipped invalid job ${name}`, { code: 'ALP_SCHEDULE_UNREADABLE', file: name });
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
// Only the fields the wire type knows cross to it; a file with more is still a job.
|
|
105
|
+
return {
|
|
106
|
+
id: record.id,
|
|
107
|
+
projectRoot: record.projectRoot,
|
|
108
|
+
sessionId: record.sessionId,
|
|
109
|
+
prompt: record.prompt,
|
|
110
|
+
dueAt: record.dueAt,
|
|
111
|
+
status: record.status,
|
|
112
|
+
attempts: Number.isSafeInteger(record.attempts) ? record.attempts : 0,
|
|
113
|
+
createdAt: record.createdAt,
|
|
114
|
+
updatedAt: record.updatedAt,
|
|
115
|
+
...(record.attemptedAt === undefined ? {} : { attemptedAt: record.attemptedAt }),
|
|
116
|
+
...(record.deliveredAt === undefined ? {} : { deliveredAt: record.deliveredAt }),
|
|
117
|
+
...(record.failedAt === undefined ? {} : { failedAt: record.failedAt }),
|
|
118
|
+
...(record.cancelledAt === undefined ? {} : { cancelledAt: record.cancelledAt }),
|
|
119
|
+
...(record.lateByMs === undefined ? {} : { lateByMs: record.lateByMs }),
|
|
120
|
+
...(record.lastError === undefined ? {} : { lastError: { message: record.lastError.message, ...(record.lastError.code === undefined ? {} : { code: record.lastError.code }) } }),
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** A job without the empty fields a later step cleared. */
|
|
125
|
+
function without<T extends object>(value: T): T {
|
|
126
|
+
return Object.fromEntries(Object.entries(value).filter(([, entry]) => entry !== undefined)) as T;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function createScheduleStore(root: string, { now = () => Date.now() } = {}) {
|
|
130
|
+
const directory = root;
|
|
131
|
+
let ready: Promise<void> | undefined;
|
|
132
|
+
const prepare = () => ready ??= mkdir(directory, { recursive: true, mode: 0o700 }).then(() => {}, error => { ready = undefined; throw error; });
|
|
133
|
+
/** alpd's writes to the job files go one at a time, so a later write never lands before an earlier one. */
|
|
134
|
+
let writes: Promise<unknown> = Promise.resolve();
|
|
135
|
+
const serial = <T>(work: () => Promise<T>): Promise<T> => {
|
|
136
|
+
const run = writes.catch(() => {}).then(work);
|
|
137
|
+
writes = run;
|
|
138
|
+
return run;
|
|
139
|
+
};
|
|
140
|
+
const fileOf = (id: string) => path.join(directory, `${id}.json`);
|
|
141
|
+
/** The file of a job, when the id names one; nothing else is ever read or written. */
|
|
142
|
+
const fileOfSafe = (id: string) => (typeof id === 'string' && SCHEDULE_ID.test(id) ? fileOf(id) : undefined);
|
|
143
|
+
/** Writes a job file now; the caller holds the store's lock when it needs one. */
|
|
144
|
+
const writeNow = async (job: ScheduleRecord) => {
|
|
145
|
+
await prepare();
|
|
146
|
+
// The project is written the one way it is compared, whatever the caller did:
|
|
147
|
+
// every write of a job carries the canonical path, and a relative one is refused
|
|
148
|
+
// here as well, so no writer can slip a project past the rule.
|
|
149
|
+
if (!path.isAbsolute(job.projectRoot)) throw new ScheduleStoreError('INVALID_SCHEDULE', 'A project is named by its absolute path');
|
|
150
|
+
const canonical = { ...job, projectRoot: projectKey(job.projectRoot) };
|
|
151
|
+
await writeAtomic(fileOf(job.id), `${JSON.stringify({ version: SCHEDULE_VERSION, ...canonical })}\n`);
|
|
152
|
+
return canonical;
|
|
153
|
+
};
|
|
154
|
+
const write = (job: ScheduleRecord) => serial(() => writeNow(job));
|
|
155
|
+
const stamp = () => new Date(now()).toISOString();
|
|
156
|
+
|
|
157
|
+
return {
|
|
158
|
+
/** The directory the jobs live in. */
|
|
159
|
+
directory() { return directory; },
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* A new job waiting for its time. The id is drawn here, so two jobs never
|
|
163
|
+
* share one and a job's own id names its delivery.
|
|
164
|
+
*/
|
|
165
|
+
async create({ projectRoot, sessionId, prompt, dueAt }: { projectRoot: string; sessionId: string; prompt: string; dueAt: string }): Promise<ScheduleRecord> {
|
|
166
|
+
if (typeof projectRoot !== 'string' || !projectRoot) throw new ScheduleStoreError('INVALID_SCHEDULE', 'A job needs the project it continues');
|
|
167
|
+
// A project is named by its absolute path: a relative one would be read against
|
|
168
|
+
// the daemon's own directory, which is another project.
|
|
169
|
+
if (!path.isAbsolute(projectRoot)) throw new ScheduleStoreError('INVALID_SCHEDULE', 'A job needs the absolute path of the project it continues');
|
|
170
|
+
if (typeof sessionId !== 'string' || !sessionId) throw new ScheduleStoreError('INVALID_SCHEDULE', 'A job needs the session it continues');
|
|
171
|
+
if (typeof prompt !== 'string' || !prompt.trim()) throw new ScheduleStoreError('INVALID_SCHEDULE', 'A job needs a prompt');
|
|
172
|
+
// The count is in code points, as the v1 contract counts them (packages/protocol/src/validate.js).
|
|
173
|
+
if ([...prompt].length > PROMPT_CHARS) throw new ScheduleStoreError('INVALID_SCHEDULE', `A job's prompt is at most ${PROMPT_CHARS} characters`);
|
|
174
|
+
const due = new Date(dueAt);
|
|
175
|
+
if (Number.isNaN(due.getTime())) throw new ScheduleStoreError('INVALID_SCHEDULE', 'A job needs the time it is due, as a timestamp');
|
|
176
|
+
const id = `sch_${randomBytes(8).toString('hex')}`;
|
|
177
|
+
const at = stamp();
|
|
178
|
+
return write({
|
|
179
|
+
id,
|
|
180
|
+
projectRoot,
|
|
181
|
+
sessionId,
|
|
182
|
+
prompt,
|
|
183
|
+
dueAt: due.toISOString(),
|
|
184
|
+
status: 'pending',
|
|
185
|
+
attempts: 0,
|
|
186
|
+
createdAt: at,
|
|
187
|
+
updatedAt: at,
|
|
188
|
+
});
|
|
189
|
+
},
|
|
190
|
+
|
|
191
|
+
/** The job of that id, or undefined when there is none. */
|
|
192
|
+
async get(id: string, { projectRoot }: { projectRoot?: string } = {}): Promise<ScheduleRecord | undefined> {
|
|
193
|
+
const wanted = namedProject(projectRoot);
|
|
194
|
+
const file = fileOfSafe(id);
|
|
195
|
+
if (!file) return undefined;
|
|
196
|
+
await prepare();
|
|
197
|
+
let text: string;
|
|
198
|
+
try { text = await readFile(file, 'utf8'); }
|
|
199
|
+
catch (error: any) { if (error?.code === 'ENOENT') return undefined; throw error; }
|
|
200
|
+
const job = readJob(text, `${id}.json`);
|
|
201
|
+
// The project is the boundary, here as well as in the daemon: a job of another
|
|
202
|
+
// project is not there for this one.
|
|
203
|
+
if (job && wanted !== undefined && projectKey(job.projectRoot) !== projectKey(wanted)) return undefined;
|
|
204
|
+
return job;
|
|
205
|
+
},
|
|
206
|
+
|
|
207
|
+
/** The jobs of one project, oldest first by the time they are due. */
|
|
208
|
+
async list({ projectRoot, status }: { projectRoot: string; status?: ScheduleStatus }): Promise<ScheduleRecord[]> {
|
|
209
|
+
await prepare();
|
|
210
|
+
// The project is not optional here: the store keeps the boundary itself, and a
|
|
211
|
+
// caller that wants the jobs of every project says so with all().
|
|
212
|
+
const wanted = namedProject(projectRoot);
|
|
213
|
+
// The status is one of the four the protocol names: an unknown one is refused,
|
|
214
|
+
// not an empty answer.
|
|
215
|
+
if (status !== undefined && !isStatus(status)) throw new ScheduleStoreError('INVALID_SCHEDULE', `Unknown job status ${String(status)}`);
|
|
216
|
+
const key = projectKey(wanted!);
|
|
217
|
+
const jobs: ScheduleRecord[] = [];
|
|
218
|
+
for (const name of await readdir(directory).catch(error => { if (error?.code === 'ENOENT') return [] as string[]; throw error; })) {
|
|
219
|
+
if (!name.endsWith('.json')) continue;
|
|
220
|
+
let job: ScheduleRecord | undefined;
|
|
221
|
+
try { job = readJob(await readFile(path.join(directory, name), 'utf8'), name); } catch { continue; }
|
|
222
|
+
if (!job) continue;
|
|
223
|
+
if (projectKey(job.projectRoot) !== key) continue;
|
|
224
|
+
if (status !== undefined && job.status !== status) continue;
|
|
225
|
+
jobs.push(job);
|
|
226
|
+
}
|
|
227
|
+
return jobs.sort((a, b) => Date.parse(a.dueAt) - Date.parse(b.dueAt) || a.id.localeCompare(b.id));
|
|
228
|
+
},
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Every job of the host, of every project. Only the daemon's own tick reads them
|
|
232
|
+
* all; a client reads the jobs of its own project (list).
|
|
233
|
+
*/
|
|
234
|
+
async all({ status }: { status?: ScheduleStatus } = {}): Promise<ScheduleRecord[]> {
|
|
235
|
+
await prepare();
|
|
236
|
+
if (status !== undefined && !isStatus(status)) throw new ScheduleStoreError('INVALID_SCHEDULE', `Unknown job status ${String(status)}`);
|
|
237
|
+
const jobs: ScheduleRecord[] = [];
|
|
238
|
+
for (const name of await readdir(directory).catch(error => { if (error?.code === 'ENOENT') return [] as string[]; throw error; })) {
|
|
239
|
+
if (!name.endsWith('.json')) continue;
|
|
240
|
+
let job: ScheduleRecord | undefined;
|
|
241
|
+
try { job = readJob(await readFile(path.join(directory, name), 'utf8'), name); } catch { continue; }
|
|
242
|
+
if (!job) continue;
|
|
243
|
+
if (status !== undefined && job.status !== status) continue;
|
|
244
|
+
jobs.push(job);
|
|
245
|
+
}
|
|
246
|
+
return jobs.sort((a, b) => Date.parse(a.dueAt) - Date.parse(b.dueAt) || a.id.localeCompare(b.id));
|
|
247
|
+
},
|
|
248
|
+
|
|
249
|
+
/** The jobs that are due at that time and still wait. */
|
|
250
|
+
async due(at: number): Promise<ScheduleRecord[]> {
|
|
251
|
+
const jobs = await this.all({ status: 'pending' });
|
|
252
|
+
return jobs.filter(job => Date.parse(job.dueAt) <= at);
|
|
253
|
+
},
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Changes the fields of a job under the store's own lock: the job is read and
|
|
257
|
+
* written in one step, so a change made elsewhere in between (a cancel) is not
|
|
258
|
+
* overwritten. The change is skipped when it returns nothing, and nothing is
|
|
259
|
+
* written then; undefined says the change was not made.
|
|
260
|
+
*/
|
|
261
|
+
async change(id: string, change: (job: ScheduleRecord) => Partial<ScheduleRecord> | undefined): Promise<ScheduleRecord | undefined> {
|
|
262
|
+
return serial(async () => {
|
|
263
|
+
const job = await this.get(id);
|
|
264
|
+
if (!job) return undefined;
|
|
265
|
+
const patch = change(job);
|
|
266
|
+
if (!patch) return undefined;
|
|
267
|
+
const merged = { ...job, ...patch };
|
|
268
|
+
return writeNow({ ...merged, updatedAt: stamp() });
|
|
269
|
+
});
|
|
270
|
+
},
|
|
271
|
+
|
|
272
|
+
/** A job that will not run: cancelled before its time. */
|
|
273
|
+
async cancel(id: string, { projectRoot }: { projectRoot?: string } = {}): Promise<ScheduleRecord> {
|
|
274
|
+
return serial(async () => {
|
|
275
|
+
// The project is the boundary here as well: a job of another project is not
|
|
276
|
+
// cancelled by a client of this one.
|
|
277
|
+
const job = await this.get(id, { projectRoot });
|
|
278
|
+
if (!job) throw new ScheduleStoreError('SCHEDULE_NOT_FOUND', `No scheduled job ${id}`);
|
|
279
|
+
if (job.status !== 'pending') throw new ScheduleStoreError('SCHEDULE_NOT_PENDING', `Job ${id} is already ${job.status}`);
|
|
280
|
+
return writeNow(without({ ...job, status: 'cancelled' as ScheduleStatus, cancelledAt: stamp(), lastError: undefined }));
|
|
281
|
+
});
|
|
282
|
+
},
|
|
283
|
+
|
|
284
|
+
/** Removes a job file outright; a job that is not there is not an error. */
|
|
285
|
+
async remove(id: string): Promise<void> {
|
|
286
|
+
const file = fileOfSafe(id);
|
|
287
|
+
if (!file) return;
|
|
288
|
+
await serial(async () => {
|
|
289
|
+
await prepare();
|
|
290
|
+
await rm(file, { force: true });
|
|
291
|
+
});
|
|
292
|
+
},
|
|
293
|
+
};
|
|
294
|
+
}
|