@anhlp/alp-daemon 0.0.1-beta.3 → 0.0.1-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -0
- package/dist/THIRD-PARTY-NOTICES.txt +26 -1
- package/dist/alpd.js +29928 -14898
- package/dist/attachments.d.ts +187 -0
- package/dist/attachments.js +856 -0
- package/dist/build-manifest.json +34 -1
- package/dist/conversation.d.ts +119 -0
- package/dist/conversation.js +688 -0
- package/dist/disk.d.ts +67 -0
- package/dist/disk.js +152 -0
- package/dist/files.d.ts +154 -2
- package/dist/files.js +392 -68
- package/dist/git.d.ts +93 -0
- package/dist/git.js +1192 -0
- package/dist/gitignore.d.ts +5 -0
- package/dist/gitignore.js +7 -1
- package/dist/history.d.ts +24 -0
- package/dist/history.js +149 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +21 -1
- package/dist/lifecycle.d.ts +101 -0
- package/dist/lifecycle.js +239 -0
- package/dist/main.js +43 -11
- package/dist/native-terminal.d.ts +45 -0
- package/dist/native-terminal.js +198 -0
- package/dist/operations.d.ts +8 -2
- package/dist/operations.js +20 -10
- package/dist/registry.d.ts +89 -0
- package/dist/registry.js +343 -0
- package/dist/scheduler.d.ts +96 -0
- package/dist/scheduler.js +296 -0
- package/dist/schedules.d.ts +70 -0
- package/dist/schedules.js +246 -0
- package/dist/server.d.ts +14 -2
- package/dist/server.js +836 -90
- package/dist/store.d.ts +72 -2
- package/dist/store.js +111 -4
- package/dist/terminal-native/README.md +201 -0
- package/dist/terminal-native/prebuilt/darwin-arm64/manifest.json +11 -0
- package/dist/terminal-native/prebuilt/darwin-arm64/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/darwin-x64/manifest.json +11 -0
- package/dist/terminal-native/prebuilt/darwin-x64/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-arm64-glibc/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-arm64-glibc/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-arm64-musl/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-arm64-musl/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-x64-glibc/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-x64-glibc/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-x64-musl/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-x64-musl/terminal-owner +0 -0
- package/dist/terminal-native/terminal-owner.c +389 -0
- package/dist/terminal-screen.d.ts +24 -0
- package/dist/terminal-screen.js +99 -0
- package/dist/terminals.d.ts +193 -0
- package/dist/terminals.js +570 -0
- package/dist/windows-terminal.d.ts +29 -0
- package/dist/windows-terminal.js +122 -0
- package/dist/worktrees.d.ts +65 -0
- package/dist/worktrees.js +318 -0
- package/native/README.md +201 -0
- package/native/prebuilt/darwin-arm64/manifest.json +11 -0
- package/native/prebuilt/darwin-arm64/terminal-owner +0 -0
- package/native/prebuilt/darwin-x64/manifest.json +11 -0
- package/native/prebuilt/darwin-x64/terminal-owner +0 -0
- package/native/prebuilt/linux-arm64-glibc/manifest.json +10 -0
- package/native/prebuilt/linux-arm64-glibc/terminal-owner +0 -0
- package/native/prebuilt/linux-arm64-musl/manifest.json +10 -0
- package/native/prebuilt/linux-arm64-musl/terminal-owner +0 -0
- package/native/prebuilt/linux-x64-glibc/manifest.json +10 -0
- package/native/prebuilt/linux-x64-glibc/terminal-owner +0 -0
- package/native/prebuilt/linux-x64-musl/manifest.json +10 -0
- package/native/prebuilt/linux-x64-musl/terminal-owner +0 -0
- package/native/terminal-owner.c +389 -0
- package/package.json +70 -7
- package/src/attachments.ts +908 -0
- package/src/conversation.ts +857 -0
- package/src/disk.ts +175 -0
- package/src/files.ts +528 -82
- package/src/git.ts +1140 -0
- package/src/gitignore.ts +10 -0
- package/src/history.ts +118 -0
- package/src/index.ts +7 -0
- package/src/lifecycle.ts +283 -0
- package/src/main.ts +37 -12
- package/src/native-terminal.ts +122 -0
- package/src/operations.ts +27 -11
- package/src/registry.ts +238 -0
- package/src/scheduler.ts +432 -0
- package/src/schedules.ts +297 -0
- package/src/server.ts +948 -102
- package/src/store.ts +117 -4
- package/src/terminal-screen.ts +74 -0
- package/src/terminals.ts +641 -0
- package/src/windows-terminal.ts +123 -0
- package/src/worktrees.ts +382 -0
package/src/scheduler.ts
ADDED
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
import { createLogger } from '@anhlp/alp-core/log';
|
|
2
|
+
import type { AlpRuntime } from '@anhlp/alp-runtime/index';
|
|
3
|
+
import type { PromptContent } from '@anhlp/alp-protocol/session';
|
|
4
|
+
import type { ScheduleJob } from '@anhlp/alp-protocol/index';
|
|
5
|
+
import type { ScheduleStore } from './schedules.js';
|
|
6
|
+
import { projectKey } from './schedules.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The scheduler of alpd: a tick that finds the jobs whose time has come
|
|
10
|
+
* and opens a turn on the session each of them names, without a user message.
|
|
11
|
+
*
|
|
12
|
+
* A job is delivered at most once. The daemon's prompt receipt is the guard: the
|
|
13
|
+
* delivery of a job carries a clientMessageId of its own, so the receipt of that
|
|
14
|
+
* message is what a retry after a crash reads. A job whose delivery the runtime
|
|
15
|
+
* refused is failed with the reason; a job whose target session is gone is failed
|
|
16
|
+
* too, never silently dropped.
|
|
17
|
+
*
|
|
18
|
+
* A job that comes due while its session is working waits for the turn to end
|
|
19
|
+
* rather than steering it: the delivery is 'auto', so it queues behind the running
|
|
20
|
+
* turn. Waiting is bounded; past the grace period the job fails and says why.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
const log = createLogger({ component: 'alpd.schedule' });
|
|
24
|
+
|
|
25
|
+
/** How long a job waits for a busy session before it fails; an hour by default. */
|
|
26
|
+
export const SCHEDULE_GRACE_MS = 60 * 60 * 1000;
|
|
27
|
+
/** How often alpd looks for jobs whose time has come. */
|
|
28
|
+
export const SCHEDULE_TICK_MS = 10_000;
|
|
29
|
+
/** How long a delivery may take; a session that never answers does not hold the tick. */
|
|
30
|
+
export const DELIVERY_TIMEOUT_MS = 2 * 60 * 1000;
|
|
31
|
+
|
|
32
|
+
/** What the daemon's prompt path answered. */
|
|
33
|
+
export type ScheduleDelivery = { outcome: 'accepted' | 'failed' | 'unknown'; error?: { message: string; code?: string } };
|
|
34
|
+
|
|
35
|
+
export type Scheduler = ReturnType<typeof createScheduler>;
|
|
36
|
+
|
|
37
|
+
/** A delay in words: "45 seconds", "7 minutes", "1 hour 3 minutes". */
|
|
38
|
+
export function duration(ms: number): string {
|
|
39
|
+
const seconds = Math.max(0, Math.round(ms / 1000));
|
|
40
|
+
if (seconds < 60) return `${seconds} second${seconds === 1 ? '' : 's'}`;
|
|
41
|
+
const minutes = Math.floor(seconds / 60);
|
|
42
|
+
if (minutes < 60) return `${minutes} minute${minutes === 1 ? '' : 's'}`;
|
|
43
|
+
const hours = Math.floor(minutes / 60);
|
|
44
|
+
const rest = minutes % 60;
|
|
45
|
+
const hour = `${hours} hour${hours === 1 ? '' : 's'}`;
|
|
46
|
+
return rest ? `${hour} ${rest} minute${rest === 1 ? '' : 's'}` : hour;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The text a job delivers: the continuation, and what the session needs to know
|
|
51
|
+
* about it — that it was asked for, and how late it is. The text is a function of
|
|
52
|
+
* the job alone, so a retry after a crash sends the very same words.
|
|
53
|
+
*/
|
|
54
|
+
export function renderPrompt(job: ScheduleJob): string {
|
|
55
|
+
const due = new Date(job.dueAt).toISOString();
|
|
56
|
+
const late = job.lateByMs && job.lateByMs > 0 ? ` and running ${duration(job.lateByMs)} late` : '';
|
|
57
|
+
return `This is a scheduled continuation (job ${job.id}), due ${due}${late}.\nBefore delegating a quota-reset continuation, recheck actual provider availability using the fresh runtime usage snapshot. The scheduled time does not guarantee quota; unavailable or missing usage data is not proof of availability.\n\n${job.prompt}`;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The id of the message a job delivers under. It is the job's own, so the receipt
|
|
62
|
+
* of that message is what keeps the job from running twice: a retry after a crash
|
|
63
|
+
* reads the receipt of the first delivery instead of sending again.
|
|
64
|
+
*/
|
|
65
|
+
export function clientMessageId(job: ScheduleJob): string {
|
|
66
|
+
return `alp-job-${job.id}`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function createScheduler({ store, runtime, deliver, ready, session, settled, stopping, now = () => Date.now(), graceMs = SCHEDULE_GRACE_MS, deliveryTimeoutMs = DELIVERY_TIMEOUT_MS }: {
|
|
70
|
+
/** The job files, in `$ALP_HOME/schedules`. */
|
|
71
|
+
store: ScheduleStore;
|
|
72
|
+
/** The runtime that holds the sessions a job continues. */
|
|
73
|
+
runtime: AlpRuntime;
|
|
74
|
+
/** The daemon's prompt path: a delivery of this job, by its own clientMessageId. */
|
|
75
|
+
deliver: (job: ScheduleJob, content: PromptContent, clientMessageId: string, send?: AlpRuntime['prompt']) => Promise<ScheduleDelivery>;
|
|
76
|
+
/** The daemon's own start-up; a job waits for it, so a reopened session is ready. */
|
|
77
|
+
ready?: () => Promise<unknown>;
|
|
78
|
+
/**
|
|
79
|
+
* What the daemon knows about the session of a job that the runtime does not show:
|
|
80
|
+
* that it is being opened again, or that it could not be opened. A session that is
|
|
81
|
+
* being opened is not closed: the job waits for it.
|
|
82
|
+
*/
|
|
83
|
+
session?: (sessionId: string) => { opening?: boolean; error?: { message: string; code?: string } } | undefined;
|
|
84
|
+
/**
|
|
85
|
+
* Called when a job of that session waits for nothing any more. The daemon may keep
|
|
86
|
+
* something for that job alone (the reason its session could not be opened again);
|
|
87
|
+
* it is told the job is through, so it can drop it.
|
|
88
|
+
*/
|
|
89
|
+
settled?: (sessionId: string) => void;
|
|
90
|
+
/** Whether the daemon is stopping; a job is not failed because alpd is going away. */
|
|
91
|
+
stopping?: () => boolean;
|
|
92
|
+
now?: () => number;
|
|
93
|
+
graceMs?: number;
|
|
94
|
+
/** How long a delivery may take before the job is failed for it. */
|
|
95
|
+
deliveryTimeoutMs?: number;
|
|
96
|
+
}) {
|
|
97
|
+
/** The jobs that wait, by the session they continue. */
|
|
98
|
+
const waiting = new Map<string, Set<string>>();
|
|
99
|
+
/** Every job that waits, with the moment its wait began. */
|
|
100
|
+
const held = new Map<string, { sessionId: string; at: number }>();
|
|
101
|
+
/** The jobs that wait for a session to be opened again, with the moment the wait began. */
|
|
102
|
+
const opening = new Map<string, number>();
|
|
103
|
+
/** The jobs that wait for a busy turn to end, with the moment the wait began. */
|
|
104
|
+
const busy = new Map<string, number>();
|
|
105
|
+
/** The sessions a job is being created for: they are held before the job is written. */
|
|
106
|
+
const heldSessions = new Map<string, number>();
|
|
107
|
+
/** The jobs being delivered right now; a cancel that arrives is refused. */
|
|
108
|
+
const delivering = new Set<string>();
|
|
109
|
+
/** Jobs whose final state could not be written; they are not tried again in this process. */
|
|
110
|
+
const unwritten = new Set<string>();
|
|
111
|
+
let sequence = 0;
|
|
112
|
+
let ticking = false;
|
|
113
|
+
/** The tick that runs now, so a stop can wait for it. */
|
|
114
|
+
let running: ReturnType<typeof runTick> | undefined;
|
|
115
|
+
let timer: NodeJS.Timeout | undefined;
|
|
116
|
+
|
|
117
|
+
const hold = (job: ScheduleJob) => {
|
|
118
|
+
if (!held.has(job.id)) held.set(job.id, { sessionId: job.sessionId, at: ++sequence });
|
|
119
|
+
// A job that does not keep its session open is tracked, but no session waits for it.
|
|
120
|
+
if (job.keepsSession === false) return;
|
|
121
|
+
const ids = waiting.get(job.sessionId) ?? new Set<string>();
|
|
122
|
+
ids.add(job.id);
|
|
123
|
+
waiting.set(job.sessionId, ids);
|
|
124
|
+
};
|
|
125
|
+
const releaseId = (id: string) => {
|
|
126
|
+
const entry = held.get(id);
|
|
127
|
+
opening.delete(id);
|
|
128
|
+
busy.delete(id);
|
|
129
|
+
if (!entry) return;
|
|
130
|
+
held.delete(id);
|
|
131
|
+
const ids = waiting.get(entry.sessionId);
|
|
132
|
+
if (!ids) return;
|
|
133
|
+
ids.delete(id);
|
|
134
|
+
if (!ids.size) waiting.delete(entry.sessionId);
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* A job that waits for nothing any more. The daemon is told the job is through, so
|
|
138
|
+
* what it kept for that job alone (the reason its session could not be opened again)
|
|
139
|
+
* can be dropped.
|
|
140
|
+
*/
|
|
141
|
+
const release = (job: ScheduleJob) => {
|
|
142
|
+
releaseId(job.id);
|
|
143
|
+
settled?.(job.sessionId);
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Reads the job files again, so the waits are what the files say. A wait that
|
|
148
|
+
* began before the files were read and whose job no longer waits is released; a
|
|
149
|
+
* wait that began while they were read (a job just created) is kept.
|
|
150
|
+
*/
|
|
151
|
+
async function refresh(): Promise<void> {
|
|
152
|
+
const at = sequence;
|
|
153
|
+
const jobs = await store.all({ status: 'pending' });
|
|
154
|
+
const pending = new Set(jobs.filter(job => !unwritten.has(job.id)).map(job => job.id));
|
|
155
|
+
for (const job of jobs) if (!unwritten.has(job.id)) hold(job);
|
|
156
|
+
for (const [id, entry] of [...held]) {
|
|
157
|
+
if (entry.at > at || pending.has(id)) continue;
|
|
158
|
+
releaseId(id);
|
|
159
|
+
// The job is gone: nothing waits for that session any more.
|
|
160
|
+
settled?.(entry.sessionId);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** A job that failed: the reason is kept, so nobody waits for a job that never ran. */
|
|
165
|
+
async function fail(job: ScheduleJob, message: string, code?: string): Promise<'failed'> {
|
|
166
|
+
const at = new Date(now()).toISOString();
|
|
167
|
+
const reason = { message, ...(code ? { code } : {}) };
|
|
168
|
+
// The final state is written under the store's own lock, and only onto a job that
|
|
169
|
+
// still waits: a cancel that landed meanwhile is not overwritten.
|
|
170
|
+
const saved = await store.change(job.id, current => current.status === 'pending' ? { status: 'failed' as const, failedAt: at, lastError: reason } : undefined)
|
|
171
|
+
.catch(error => {
|
|
172
|
+
// The file still says pending; the job is not tried again in this process.
|
|
173
|
+
unwritten.add(job.id);
|
|
174
|
+
log.warn(`could not record the failure of job ${job.id}; it stays pending on disk and is not tried again until the next alpd`, { code: 'ALP_SCHEDULE_WRITE_FAILED', jobId: job.id, error });
|
|
175
|
+
return undefined;
|
|
176
|
+
});
|
|
177
|
+
if (!saved) {
|
|
178
|
+
// The file still says pending, so the job waits for nothing in this process: it
|
|
179
|
+
// is not tried again here, and the next alpd reads the file and finds it.
|
|
180
|
+
release(job);
|
|
181
|
+
log.warn(`job ${job.id} failed: ${message} (the job file was not changed)`, { code: 'ALP_SCHEDULE_FAILED', jobId: job.id, sessionId: job.sessionId, reason: code });
|
|
182
|
+
return 'failed';
|
|
183
|
+
}
|
|
184
|
+
release(saved);
|
|
185
|
+
log.warn(`job ${job.id} failed: ${message}`, { code: 'ALP_SCHEDULE_FAILED', jobId: job.id, sessionId: job.sessionId, reason: code });
|
|
186
|
+
return 'failed';
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** A job that was delivered: the time it was delivered and how late it was. */
|
|
190
|
+
async function done(job: ScheduleJob): Promise<'delivered'> {
|
|
191
|
+
const at = new Date(now()).toISOString();
|
|
192
|
+
// The final state is written under the store's own lock, and only onto a job that
|
|
193
|
+
// still waits: a job file that is gone is not written again.
|
|
194
|
+
const saved = await store.change(job.id, current => current.status === 'pending' ? { status: 'delivered' as const, deliveredAt: at, lastError: undefined } : undefined)
|
|
195
|
+
.catch(error => {
|
|
196
|
+
unwritten.add(job.id);
|
|
197
|
+
log.warn(`could not record the delivery of job ${job.id}; it stays pending on disk and is not tried again until the next alpd`, { code: 'ALP_SCHEDULE_WRITE_FAILED', jobId: job.id, error });
|
|
198
|
+
return undefined;
|
|
199
|
+
});
|
|
200
|
+
if (!saved) {
|
|
201
|
+
// The file still says pending, so the job waits for nothing in this process: it
|
|
202
|
+
// is not tried again here, and the next alpd reads the file and finds it.
|
|
203
|
+
release(job);
|
|
204
|
+
log.warn(`job ${job.id} was delivered, but its file was not changed`, { code: 'ALP_SCHEDULE_DELIVERED', jobId: job.id, sessionId: job.sessionId });
|
|
205
|
+
return 'delivered';
|
|
206
|
+
}
|
|
207
|
+
release(saved);
|
|
208
|
+
log.info(`job ${job.id} delivered to ${job.sessionId}${job.lateByMs ? ` (${duration(job.lateByMs)} late)` : ''}`, { code: 'ALP_SCHEDULE_DELIVERED', jobId: job.id, sessionId: job.sessionId });
|
|
209
|
+
return 'delivered';
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Delivers one job. The target must be the live root session of the job's
|
|
214
|
+
* project; a session that is gone, or is not a root, fails the job with the
|
|
215
|
+
* reason rather than waking something else.
|
|
216
|
+
*/
|
|
217
|
+
async function fire(job: ScheduleJob): Promise<'delivered' | 'failed' | 'waiting' | 'skipped'> {
|
|
218
|
+
const current = await store.get(job.id);
|
|
219
|
+
// A job that is not pending any more will not run: it waits for nothing.
|
|
220
|
+
if (!current || current.status !== 'pending') { release(job); return 'skipped'; }
|
|
221
|
+
// A daemon that is going away does not fail a job: the job waits for the next alpd.
|
|
222
|
+
if (stopping?.()) return 'skipped';
|
|
223
|
+
// A job whose final state could not be written is not tried again in this process,
|
|
224
|
+
// and its session is not held for it either.
|
|
225
|
+
if (unwritten.has(current.id)) { release(current); return 'skipped'; }
|
|
226
|
+
/** The attempt as it was written, once it was: what a failure must keep. */
|
|
227
|
+
let attempt: ScheduleJob | undefined;
|
|
228
|
+
const snapshot = runtime.snapshot(current.sessionId);
|
|
229
|
+
const known = session?.(current.sessionId);
|
|
230
|
+
// Native opening publishes a snapshot before its thread is ready. Recovery
|
|
231
|
+
// still owns this session: it is neither closed nor busy with a turn yet.
|
|
232
|
+
if (known?.opening) {
|
|
233
|
+
// The wait is bounded from the moment it began: how late the job is says
|
|
234
|
+
// nothing about how long the session takes to open again.
|
|
235
|
+
const since = opening.get(current.id) ?? now();
|
|
236
|
+
opening.set(current.id, since);
|
|
237
|
+
const waited = now() - since;
|
|
238
|
+
if (waited > graceMs) return fail(current, `The session was not open again within ${duration(waited)}`, 'session_not_open');
|
|
239
|
+
const note = { message: 'The session is being opened again; the job waits for it', code: 'session_opening' };
|
|
240
|
+
await store.change(current.id, job => job.status === 'pending' && job.lastError?.code !== note.code ? { lastError: note } : undefined)
|
|
241
|
+
.catch(error => log.warn(`could not record the wait of job ${current.id}`, { code: 'ALP_SCHEDULE_WRITE_FAILED', jobId: current.id, error }));
|
|
242
|
+
return 'waiting';
|
|
243
|
+
}
|
|
244
|
+
if (!snapshot) {
|
|
245
|
+
// A session that could not be opened again says why; the job fails with that reason.
|
|
246
|
+
if (known?.error) return fail(current, known.error.message, known.error.code ?? 'session_open_failed');
|
|
247
|
+
return fail(current, 'The session this job continues is closed', 'session_closed');
|
|
248
|
+
}
|
|
249
|
+
// The session is open: the wait for an opening is over, and the next opening gets
|
|
250
|
+
// its own window.
|
|
251
|
+
opening.delete(current.id);
|
|
252
|
+
if (snapshot.parentId) return fail(current, 'The session this job continues is not a root session', 'not_a_root');
|
|
253
|
+
if (projectKey(snapshot.projectRoot) !== projectKey(current.projectRoot)) return fail(current, 'The session this job continues belongs to another project', 'other_project');
|
|
254
|
+
const waitBusy = async (): Promise<'waiting' | 'failed'> => {
|
|
255
|
+
// The session is working. The wait is bounded from the moment it began: how
|
|
256
|
+
// late the job is says nothing about how long a running turn takes to end.
|
|
257
|
+
const since = busy.get(current.id) ?? now();
|
|
258
|
+
busy.set(current.id, since);
|
|
259
|
+
const waited = now() - since;
|
|
260
|
+
if (waited > graceMs) return fail(current, `The session was still busy after waiting ${duration(waited)}`, 'session_busy');
|
|
261
|
+
// Only the note is written, and only when it says something new: a cancel that
|
|
262
|
+
// happened meanwhile is not overwritten, and the file is not rewritten every tick.
|
|
263
|
+
const note = { message: 'The session is busy; the job waits for its turn to end', code: 'session_busy' };
|
|
264
|
+
await store.change(current.id, job => job.status === 'pending' && job.lastError?.code !== note.code ? { lastError: note } : undefined)
|
|
265
|
+
.catch(error => log.warn(`could not record the wait of job ${current.id}`, { code: 'ALP_SCHEDULE_WRITE_FAILED', jobId: current.id, error }));
|
|
266
|
+
return 'waiting';
|
|
267
|
+
};
|
|
268
|
+
if (snapshot.activeTurnId || runtime.starting?.(current.sessionId)) return waitBusy();
|
|
269
|
+
// The session is idle: the wait for a busy turn is over.
|
|
270
|
+
busy.delete(current.id);
|
|
271
|
+
const dispatch = async (send?: AlpRuntime['prompt']): Promise<'delivered' | 'failed' | 'waiting' | 'skipped'> => {
|
|
272
|
+
// From here the job is being delivered: a cancel that arrives now is refused, so
|
|
273
|
+
// the job and the record cannot disagree about what happened.
|
|
274
|
+
delivering.add(current.id);
|
|
275
|
+
try {
|
|
276
|
+
// The first attempt fixes when the job was tried and how late it is, and both are
|
|
277
|
+
// written before the delivery: a retry after a crash sends the very same words.
|
|
278
|
+
const attemptedAtMs = current.attemptedAt ? Date.parse(current.attemptedAt) : now();
|
|
279
|
+
const lateByMs = current.lateByMs ?? Math.max(0, attemptedAtMs - Date.parse(current.dueAt));
|
|
280
|
+
const fields = { attemptedAt: new Date(attemptedAtMs).toISOString(), lateByMs, attempts: (current.attempts ?? 0) + 1, lastError: undefined };
|
|
281
|
+
// The attempt is written under the store's own lock: a cancel that landed while
|
|
282
|
+
// the job was being prepared wins, and the job does not run.
|
|
283
|
+
const persisted = await store.change(current.id, job => job.status === 'pending' ? fields : undefined);
|
|
284
|
+
if (!persisted || persisted.status !== 'pending') {
|
|
285
|
+
release(persisted ?? current);
|
|
286
|
+
return 'skipped';
|
|
287
|
+
}
|
|
288
|
+
attempt = persisted;
|
|
289
|
+
const written = attempt;
|
|
290
|
+
const content: PromptContent = [{ type: 'text', text: renderPrompt(written) }];
|
|
291
|
+
// A session that never answers does not hold the tick: the job fails for it.
|
|
292
|
+
const delivery = deliver(written, content, clientMessageId(written), send);
|
|
293
|
+
// A delivery that answers after the timeout is logged, not left unhandled.
|
|
294
|
+
void delivery.catch(error => log.warn(`the delivery of job ${written.id} failed after it timed out`, { code: 'ALP_SCHEDULE_DELIVERY_FAILED', jobId: written.id, error }));
|
|
295
|
+
let timer: NodeJS.Timeout | undefined;
|
|
296
|
+
const answer = await Promise.race([
|
|
297
|
+
delivery,
|
|
298
|
+
new Promise<ScheduleDelivery>(resolve => {
|
|
299
|
+
timer = setTimeout(() => resolve({ outcome: 'failed', error: { message: `The session did not acknowledge the prompt within ${duration(deliveryTimeoutMs)}; execution outcome is unknown. The outstanding request was not cancelled and may still start a turn. Do not recreate automatically`, code: 'delivery_timeout' } }), deliveryTimeoutMs);
|
|
300
|
+
timer.unref?.();
|
|
301
|
+
}),
|
|
302
|
+
]).finally(() => { if (timer) clearTimeout(timer); });
|
|
303
|
+
if (answer.error?.code === 'delivery_timeout') {
|
|
304
|
+
const result = await fail(attempt, answer.error.message, 'delivery_timeout');
|
|
305
|
+
// The timeout bounds waiting, not provider execution. Keep the terminal job
|
|
306
|
+
// (never resend it), but make a late acknowledgement observable on inspection.
|
|
307
|
+
void delivery.then(async late => {
|
|
308
|
+
const message = late.outcome === 'accepted'
|
|
309
|
+
? 'The provider acknowledged the prompt after the delivery timeout; a turn may have executed. No retry was sent.'
|
|
310
|
+
: `The delivery answered after the timeout: ${late.error?.message ?? late.outcome}. No retry was sent.`;
|
|
311
|
+
log.warn(`job ${written.id}: ${message}`, { code: 'ALP_SCHEDULE_LATE_ACK', jobId: written.id });
|
|
312
|
+
await store.change(written.id, job => job.status === 'failed' && job.lastError?.code === 'delivery_timeout'
|
|
313
|
+
? { lastError: { code: late.outcome === 'accepted' ? 'delivery_accepted_late' : 'delivery_timeout', message } } : undefined);
|
|
314
|
+
}).catch(error => log.warn(`could not record late acknowledgement for job ${written.id}`, { code: 'ALP_SCHEDULE_WRITE_FAILED', jobId: written.id, error }));
|
|
315
|
+
return result;
|
|
316
|
+
}
|
|
317
|
+
if (answer.outcome === 'accepted') return done(attempt);
|
|
318
|
+
if (answer.outcome === 'unknown') return fail(attempt, answer.error?.message ?? 'alpd cannot tell whether the prompt reached the agent', answer.error?.code ?? 'outcome_unknown');
|
|
319
|
+
return fail(attempt, answer.error?.message ?? 'The session refused the prompt', answer.error?.code ?? 'refused');
|
|
320
|
+
} catch (error: any) {
|
|
321
|
+
// The attempt, when it was written, is what is failed: the times it fixed are kept.
|
|
322
|
+
return fail(attempt ?? current, error?.message ?? String(error), error?.data?.reason ?? 'delivery_failed');
|
|
323
|
+
} finally {
|
|
324
|
+
delivering.delete(current.id);
|
|
325
|
+
}
|
|
326
|
+
};
|
|
327
|
+
// Historical attempts only reconcile receipts; they must never send a fresh prompt.
|
|
328
|
+
if (current.attempts || !runtime.withIdlePrompt) return dispatch();
|
|
329
|
+
const admission = await runtime.withIdlePrompt(current.sessionId, send => dispatch((_id, input) => send(input)));
|
|
330
|
+
return admission.busy ? waitBusy() : admission.value;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* One pass: every job whose time has come is delivered, in the order the jobs
|
|
335
|
+
* are due. Two ticks never run at once.
|
|
336
|
+
*/
|
|
337
|
+
async function runTick(): Promise<{ delivered: number; failed: number; waiting: number }> {
|
|
338
|
+
if (ticking) return { delivered: 0, failed: 0, waiting: 0 };
|
|
339
|
+
if (stopping?.()) return { delivered: 0, failed: 0, waiting: 0 };
|
|
340
|
+
ticking = true;
|
|
341
|
+
const outcome = { delivered: 0, failed: 0, waiting: 0 };
|
|
342
|
+
try {
|
|
343
|
+
await ready?.();
|
|
344
|
+
for (const job of await store.due(now())) {
|
|
345
|
+
// One broken job must not starve the others: each is delivered on its own.
|
|
346
|
+
let result: 'delivered' | 'failed' | 'waiting' | 'skipped' = 'skipped';
|
|
347
|
+
try { result = await fire(job); }
|
|
348
|
+
catch (error) { log.error(`job ${job.id} could not be delivered`, { code: 'ALP_SCHEDULE_DELIVERY_FAILED', jobId: job.id, error }); }
|
|
349
|
+
if (result === 'delivered') outcome.delivered++;
|
|
350
|
+
else if (result === 'failed') outcome.failed++;
|
|
351
|
+
else if (result === 'waiting') outcome.waiting++;
|
|
352
|
+
}
|
|
353
|
+
await refresh();
|
|
354
|
+
} catch (error) {
|
|
355
|
+
log.error('the scheduler could not deliver its jobs', { code: 'ALP_SCHEDULE_TICK_FAILED', error });
|
|
356
|
+
} finally {
|
|
357
|
+
ticking = false;
|
|
358
|
+
}
|
|
359
|
+
return outcome;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/** One pass of the tick; the pass that runs is remembered, so a stop can wait for it. */
|
|
363
|
+
async function tick(): Promise<{ delivered: number; failed: number; waiting: number }> {
|
|
364
|
+
// An interval firing during delivery joins the actual pass; it must not
|
|
365
|
+
// replace the promise stop() needs with the no-op overlap guard.
|
|
366
|
+
if (running) return running;
|
|
367
|
+
const pass = runTick();
|
|
368
|
+
running = pass;
|
|
369
|
+
try { return await pass; }
|
|
370
|
+
finally { if (running === pass) running = undefined; }
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
return {
|
|
374
|
+
/** Whether a job waits for this session; such a session is kept open. */
|
|
375
|
+
holds(sessionId: string): boolean {
|
|
376
|
+
return (heldSessions.get(sessionId) ?? 0) > 0 || (waiting.get(sessionId)?.size ?? 0) > 0;
|
|
377
|
+
},
|
|
378
|
+
|
|
379
|
+
/** A job that is just being created waits for its session, before it is written. */
|
|
380
|
+
holdSession(sessionId: string) {
|
|
381
|
+
heldSessions.set(sessionId, (heldSessions.get(sessionId) ?? 0) + 1);
|
|
382
|
+
},
|
|
383
|
+
|
|
384
|
+
/** The job is written; its own hold takes over. */
|
|
385
|
+
releaseSession(sessionId: string) {
|
|
386
|
+
// Two jobs of one session may be created at once: the hold is counted, so the
|
|
387
|
+
// first one that is through does not let go of the session the second one needs.
|
|
388
|
+
const count = (heldSessions.get(sessionId) ?? 0) - 1;
|
|
389
|
+
if (count > 0) heldSessions.set(sessionId, count);
|
|
390
|
+
else heldSessions.delete(sessionId);
|
|
391
|
+
},
|
|
392
|
+
|
|
393
|
+
/** Whether a job is being delivered right now; a cancel of it is refused. */
|
|
394
|
+
delivering(jobId: string): boolean {
|
|
395
|
+
return delivering.has(jobId);
|
|
396
|
+
},
|
|
397
|
+
|
|
398
|
+
/** A job that was just created waits for its session. */
|
|
399
|
+
hold,
|
|
400
|
+
|
|
401
|
+
/** A job that will not run any more waits for nothing. */
|
|
402
|
+
release,
|
|
403
|
+
|
|
404
|
+
/** Reads the job files again. */
|
|
405
|
+
refresh,
|
|
406
|
+
|
|
407
|
+
/** One pass of the tick. */
|
|
408
|
+
tick,
|
|
409
|
+
|
|
410
|
+
/** The tick, every `everyMs`; the first pass waits for the daemon's start-up. */
|
|
411
|
+
start(everyMs = SCHEDULE_TICK_MS) {
|
|
412
|
+
if (timer) return;
|
|
413
|
+
timer = setInterval(() => { void tick(); }, everyMs);
|
|
414
|
+
timer.unref();
|
|
415
|
+
void refresh().catch(error => log.warn('could not read the job files', { code: 'ALP_SCHEDULE_READ_FAILED', error }));
|
|
416
|
+
},
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* The tick stops, and the pass that runs now is waited for, up to a grace: a
|
|
420
|
+
* delivery that outlives the grace is left to the next alpd, whose receipt of
|
|
421
|
+
* the message says whether it went through.
|
|
422
|
+
*/
|
|
423
|
+
async stop() {
|
|
424
|
+
if (timer) clearInterval(timer);
|
|
425
|
+
timer = undefined;
|
|
426
|
+
if (!running) return;
|
|
427
|
+
let grace: NodeJS.Timeout | undefined;
|
|
428
|
+
try { await Promise.race([running, new Promise(resolve => { grace = setTimeout(resolve, 5_000); })]); }
|
|
429
|
+
finally { if (grace) clearTimeout(grace); }
|
|
430
|
+
},
|
|
431
|
+
};
|
|
432
|
+
}
|