@anhlp/alp-daemon 0.0.0-beta.1 → 0.10.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/README.md +0 -2
- package/dist/alpd.js +1176 -3617
- package/dist/build-manifest.json +1 -28
- package/dist/lock.d.ts +0 -5
- package/dist/lock.js +17 -11
- package/dist/main.js +34 -97
- package/dist/server.d.ts +1 -8
- package/dist/server.js +91 -393
- package/dist/store.d.ts +6 -67
- package/dist/store.js +29 -191
- package/package.json +9 -16
- package/src/lock.ts +20 -17
- package/src/main.ts +24 -102
- package/src/server.ts +75 -422
- package/src/store.ts +27 -207
- package/LICENSE +0 -202
- package/NOTICE +0 -7
- package/dist/THIRD-PARTY-NOTICES.txt +0 -431
- package/dist/alp-templates.d.ts +0 -5
- package/dist/operations.d.ts +0 -23
- package/dist/operations.js +0 -272
- package/dist/rpc-error.d.ts +0 -6
- package/dist/rpc-error.js +0 -11
- package/src/alp-templates.d.ts +0 -5
- package/src/operations.ts +0 -298
- package/src/rpc-error.ts +0 -4
package/src/server.ts
CHANGED
|
@@ -1,17 +1,12 @@
|
|
|
1
1
|
import { createHash, randomBytes } from 'node:crypto';
|
|
2
|
-
import { appendFile, chmod, mkdir, readFile, unlink
|
|
2
|
+
import { appendFile, chmod, mkdir, readFile, unlink } from 'node:fs/promises';
|
|
3
3
|
import net from 'node:net';
|
|
4
|
-
import { assertPrivateDirectory, assertPrivateSocket } from '@anhlp/alp-client/index';
|
|
5
4
|
import path from 'node:path';
|
|
6
|
-
import {
|
|
7
|
-
import { AlpRpcError, CAPABILITY, MAX_FRAME, PROTOCOL_VERSION, ERROR, RPC_ERROR, RPC_METHODS, encodeFrame } from '@anhlp/alp-protocol';
|
|
8
|
-
import { validateParams } from '@anhlp/alp-protocol/contract';
|
|
5
|
+
import { AlpRpcError, MAX_FRAME, PROTOCOL_VERSION, ERROR, encodeFrame } from '@anhlp/alp-protocol';
|
|
9
6
|
export { ERROR } from '@anhlp/alp-protocol';
|
|
10
7
|
import { DEFAULT_MODEL, models, modes, thinkingOptions, type AlpEvent, type AlpRuntime, type Envelope, type SessionSnapshot } from '@anhlp/alp-runtime/index';
|
|
11
|
-
import type { Receipt, SessionRecord, SessionStatus, Store
|
|
8
|
+
import type { Receipt, SessionRecord, SessionStatus, Store } from './store.js';
|
|
12
9
|
import type { SessionSummary } from '@anhlp/alp-protocol/session';
|
|
13
|
-
import { RpcError } from './rpc-error.js';
|
|
14
|
-
import { createOperations, type OperationsOptions } from './operations.js';
|
|
15
10
|
|
|
16
11
|
/**
|
|
17
12
|
* alpd's JSON-RPC surface over one shared runtime (plans/reference/ALPD.md §3, §6, §14).
|
|
@@ -23,37 +18,10 @@ import { createOperations, type OperationsOptions } from './operations.js';
|
|
|
23
18
|
|
|
24
19
|
|
|
25
20
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
/** A request id v1 accepts: a string of at most 128 characters or a safe integer (docs/protocol/alp-json-rpc-v1.md §3). */
|
|
31
|
-
function validId(id: unknown): id is string | number {
|
|
32
|
-
if (typeof id !== 'string') return Number.isSafeInteger(id);
|
|
33
|
-
let length = 0;
|
|
34
|
-
for (const _point of id) if (++length > 128) return false;
|
|
35
|
-
return true;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/** The bytes a frame takes on a newline-delimited stream, its newline included. */
|
|
39
|
-
const frameBytes = (message: unknown) => Buffer.byteLength(encodeFrame(message), 'utf8');
|
|
40
|
-
|
|
41
|
-
const invalidParams = (issues: Array<{ path: string; message: string }>) =>
|
|
42
|
-
new RpcError(RPC_ERROR.invalidParams, `Invalid params: ${issues.map(issue => `${issue.path || 'params'} ${issue.message}`).join('; ')}`, { reason: 'invalid_params', issues });
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Crash points for the crash-recovery tests (ALPD §66): with ALP_TEST_CRASH_AT naming one,
|
|
46
|
-
* alpd kills itself there as a crash would. Unset, they do nothing.
|
|
47
|
-
*/
|
|
48
|
-
function crashPoint(name: string) {
|
|
49
|
-
if (process.env.ALP_TEST_CRASH_AT !== name) return;
|
|
50
|
-
createLogger({ component: 'alpd' }).error(`ALP_TEST_CRASH_AT=${name}: killing alpd`, { code: 'ALP_TEST_CRASH' });
|
|
51
|
-
process.kill(process.pid, 'SIGKILL');
|
|
21
|
+
class RpcError extends Error {
|
|
22
|
+
constructor(public code: number, message: string, public data?: unknown) { super(message); }
|
|
52
23
|
}
|
|
53
24
|
|
|
54
|
-
const persistenceFailed = (what: string, error: unknown) =>
|
|
55
|
-
new RpcError(ERROR.failed, `alpd could not record ${what}: ${error instanceof Error ? error.message : String(error)}`, { reason: 'persistence_failed' });
|
|
56
|
-
|
|
57
25
|
/** A stable hash of JSON data, independent of key order (as Paseo's receipts). */
|
|
58
26
|
function digest(value: unknown) {
|
|
59
27
|
return createHash('sha256').update(JSON.stringify(value, (_key, candidate) =>
|
|
@@ -68,7 +36,7 @@ type Connection = {
|
|
|
68
36
|
roots: Set<string>;
|
|
69
37
|
send(message: unknown): void;
|
|
70
38
|
nextId: number;
|
|
71
|
-
pending: Map<number, { resolve(value: unknown): void; reject(error: Error): void; timer: NodeJS.Timeout
|
|
39
|
+
pending: Map<number, { resolve(value: unknown): void; reject(error: Error): void; timer: NodeJS.Timeout }>;
|
|
72
40
|
};
|
|
73
41
|
|
|
74
42
|
/** Retained events per live tree, for attach and replay. */
|
|
@@ -76,8 +44,6 @@ const TREE_LOG_LIMIT = 20_000;
|
|
|
76
44
|
const SESSION_ID = /^[\w.:-]{1,128}$/;
|
|
77
45
|
const RETENTION_MS = 30 * 24 * 60 * 60 * 1000;
|
|
78
46
|
const RECEIPT_RETENTION_MS = 7 * 24 * 60 * 60 * 1000;
|
|
79
|
-
/** How often a running alpd applies retention, besides at start (ALPD §68). */
|
|
80
|
-
const RETENTION_SWEEP_MS = 6 * 60 * 60 * 1000;
|
|
81
47
|
const LIVE: SessionStatus[] = ['initializing', 'idle', 'running'];
|
|
82
48
|
|
|
83
49
|
/** What clients need from a daemon connection; AlpClient implements it over the socket. */
|
|
@@ -102,7 +68,7 @@ export type DaemonServer = {
|
|
|
102
68
|
* shape so every viewer agrees; re-exported here to keep `@anhlp/alp-daemon/server`'s API. */
|
|
103
69
|
export type { SessionSummary };
|
|
104
70
|
|
|
105
|
-
export function createDaemonServer({ runtime, socketPath, version, onShutdown, store, runLogDir, previousExit
|
|
71
|
+
export function createDaemonServer({ runtime, socketPath, version, onShutdown, store, runLogDir, previousExit }: {
|
|
106
72
|
runtime: AlpRuntime;
|
|
107
73
|
socketPath: string;
|
|
108
74
|
version: string;
|
|
@@ -113,12 +79,6 @@ export function createDaemonServer({ runtime, socketPath, version, onShutdown, s
|
|
|
113
79
|
runLogDir?: string;
|
|
114
80
|
/** How the previous alpd ended, for daemon.status. */
|
|
115
81
|
previousExit?: { kind: 'clean' | 'crash'; at?: string };
|
|
116
|
-
/** How often retention runs while alpd runs (ALPD §68); tests shorten it. */
|
|
117
|
-
retentionSweepMs?: number;
|
|
118
|
-
/** The daemon user's ALP_HOME, for the library, team and settings methods; without it alpd does not serve them. */
|
|
119
|
-
home?: OperationsOptions['home'];
|
|
120
|
-
/** ALP's built-in agents and teams, for the library and team methods. */
|
|
121
|
-
templates?: OperationsOptions['templates'];
|
|
122
82
|
}): DaemonServer {
|
|
123
83
|
const connections = new Set<Connection>();
|
|
124
84
|
/** Root of every live session. */
|
|
@@ -128,39 +88,18 @@ export function createDaemonServer({ runtime, socketPath, version, onShutdown, s
|
|
|
128
88
|
const records = new Map<string, SessionRecord & { activeTurnId?: string }>();
|
|
129
89
|
/** Prompt deliveries in flight, so a repeated clientMessageId waits for the first. */
|
|
130
90
|
const delivering = new Map<string, Promise<object>>();
|
|
131
|
-
/** Completed deliveries whose receipt could not be written; this process still answers them. */
|
|
132
|
-
const unsaved = new Map<string, Receipt>();
|
|
133
|
-
/** The last timeline cursor of each tree (ALPD §66). */
|
|
134
|
-
const cursors = new Map<string, number>();
|
|
135
91
|
let closing = false;
|
|
136
|
-
let socketIdentity: { dev: number; ino: number } | undefined;
|
|
137
92
|
/** Roots alpd reopens after a restart; they stay open until their recovery has started. */
|
|
138
|
-
const recovering = new
|
|
93
|
+
const recovering = new Set<string>();
|
|
139
94
|
const startedAt = new Date().toISOString();
|
|
140
95
|
const restored = store ? restore() : Promise.resolve();
|
|
141
96
|
|
|
142
97
|
const deliver = (connection: Connection, envelope: Envelope) => connection.send({ jsonrpc: '2.0', method: 'event', params: envelope });
|
|
143
98
|
|
|
144
|
-
/** Records a change; the write is queued, and a failure is logged and counted in daemon.status. */
|
|
145
99
|
function save(record: SessionRecord & { activeTurnId?: string }, changes: Partial<SessionRecord & { activeTurnId?: string }> = {}) {
|
|
146
100
|
Object.assign(record, changes, { updatedAt: new Date().toISOString() });
|
|
147
101
|
records.set(record.id, record);
|
|
148
|
-
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/** Gives an envelope of a tree its next cursor and queues it for the tree's timeline. */
|
|
152
|
-
function keep(root: string, envelope: Envelope): Envelope {
|
|
153
|
-
const cursor = (cursors.get(root) ?? 0) + 1;
|
|
154
|
-
cursors.set(root, cursor);
|
|
155
|
-
const kept = { ...envelope, cursor };
|
|
156
|
-
void store?.append(root, kept).catch(() => {});
|
|
157
|
-
return kept;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/** Continues a tree's cursors after what its stored timeline already holds. */
|
|
161
|
-
function seen(root: string, log: Envelope[]) {
|
|
162
|
-
const last = log.at(-1)?.cursor ?? 0;
|
|
163
|
-
if (last > (cursors.get(root) ?? 0)) cursors.set(root, last);
|
|
102
|
+
void store?.put(record);
|
|
164
103
|
}
|
|
165
104
|
|
|
166
105
|
/** Lifecycle bookkeeping for one event; returns true when the record changed. */
|
|
@@ -184,10 +123,9 @@ export function createDaemonServer({ runtime, socketPath, version, onShutdown, s
|
|
|
184
123
|
}
|
|
185
124
|
}
|
|
186
125
|
|
|
187
|
-
runtime.onEvent(
|
|
126
|
+
runtime.onEvent(envelope => {
|
|
188
127
|
// While alpd stops, records keep what was running, so the next alpd continues it (ALPD §31).
|
|
189
128
|
if (closing) return;
|
|
190
|
-
let envelope = original;
|
|
191
129
|
const { sessionId, event } = envelope;
|
|
192
130
|
if (event.type === 'session.opened') {
|
|
193
131
|
const parent = event.session.parentId;
|
|
@@ -195,11 +133,11 @@ export function createDaemonServer({ runtime, socketPath, version, onShutdown, s
|
|
|
195
133
|
}
|
|
196
134
|
const root = rootOf.get(sessionId) ?? sessionId;
|
|
197
135
|
if (rootOf.has(sessionId)) {
|
|
198
|
-
envelope = keep(root, envelope);
|
|
199
136
|
const log = logs.get(root) ?? [];
|
|
200
137
|
log.push(envelope);
|
|
201
138
|
if (log.length > TREE_LOG_LIMIT) log.splice(0, log.length - TREE_LOG_LIMIT);
|
|
202
139
|
logs.set(root, log);
|
|
140
|
+
void store?.append(root, envelope);
|
|
203
141
|
track(sessionId, root, event);
|
|
204
142
|
}
|
|
205
143
|
for (const connection of attached.get(root) ?? []) deliver(connection, envelope);
|
|
@@ -216,27 +154,27 @@ export function createDaemonServer({ runtime, socketPath, version, onShutdown, s
|
|
|
216
154
|
* are reopened afterwards (ALPD §31).
|
|
217
155
|
*/
|
|
218
156
|
async function restore() {
|
|
219
|
-
await store!.pruneReceipts(RECEIPT_RETENTION_MS
|
|
157
|
+
await store!.pruneReceipts(RECEIPT_RETENTION_MS);
|
|
220
158
|
const loaded = await store!.list();
|
|
221
|
-
await reconcileReceipts(loaded);
|
|
222
159
|
const now = Date.now();
|
|
223
160
|
const ended = { code: 'daemon_restarted', message: 'alpd stopped while this session was working' };
|
|
224
161
|
const epoch = `restart-${startedAt}`;
|
|
225
162
|
const continuing = new Set(runtime.recoverable().map(entry => entry.assignmentId));
|
|
226
163
|
const reopen: Array<{ record: SessionRecord; working: boolean }> = [];
|
|
227
|
-
const byRoot =
|
|
228
|
-
const
|
|
164
|
+
const byRoot = new Map<string, SessionRecord[]>();
|
|
165
|
+
for (const record of loaded) byRoot.set(record.rootId, [...(byRoot.get(record.rootId) ?? []), record]);
|
|
229
166
|
for (const [root, tree] of byRoot) {
|
|
230
167
|
const head = tree.find(record => record.id === root);
|
|
231
|
-
if (
|
|
168
|
+
if (head && !LIVE.includes(head.status) && now - Date.parse(head.updatedAt) > RETENTION_MS) {
|
|
169
|
+
await store!.remove(root, tree.map(record => record.id));
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
232
172
|
if (head && resumable(head) && (head.status === 'running' || tree.some(record => continuing.has(record.id)))) reopen.push({ record: head, working: head.status === 'running' });
|
|
233
|
-
if (tree.some(record => LIVE.includes(record.status))) seen(root, await store!.timeline(root));
|
|
234
173
|
for (const record of tree as Array<SessionRecord & { activeTurnId?: string }>) {
|
|
235
174
|
records.set(record.id, record);
|
|
236
175
|
if (!LIVE.includes(record.status)) continue;
|
|
237
176
|
const envelope = (event: AlpEvent): Envelope => ({ sessionId: record.id, epoch, seq: 0, ts: new Date().toISOString(), event });
|
|
238
|
-
|
|
239
|
-
if (record.activeTurnId) keep(root, envelope({ type: 'turn.ended', turnId: record.activeTurnId, state: 'failed', error: { message: ended.message, code: ended.code } }));
|
|
177
|
+
if (record.activeTurnId) void store!.append(root, envelope({ type: 'turn.ended', turnId: record.activeTurnId, state: 'failed', error: { message: ended.message } }));
|
|
240
178
|
if (record.id === root) {
|
|
241
179
|
save(record, record.status === 'idle' ? { status: 'closed', activeTurnId: undefined } : { status: 'error', lastError: ended, activeTurnId: undefined });
|
|
242
180
|
} else if (continuing.has(record.id) && reopen.some(entry => entry.record.id === root)) {
|
|
@@ -244,7 +182,7 @@ export function createDaemonServer({ runtime, socketPath, version, onShutdown, s
|
|
|
244
182
|
save(record, { status: 'closed', activeTurnId: undefined });
|
|
245
183
|
} else {
|
|
246
184
|
// Children never survive their runtime; their requester learns the assignment failed.
|
|
247
|
-
|
|
185
|
+
void store!.append(root, envelope({ type: 'session.closed' }));
|
|
248
186
|
save(record, { status: 'closed', lastError: ended, activeTurnId: undefined });
|
|
249
187
|
if (runLogDir && !continuing.has(record.id)) {
|
|
250
188
|
const line = JSON.stringify({ ts: new Date().toISOString(), rootSessionId: root, event: 'assignment.finished', assignmentId: record.id, agent: record.session?.agent, sessionId: record.id, status: 'failed', error: ended.message, reconciled: true }) + '\n';
|
|
@@ -258,88 +196,8 @@ export function createDaemonServer({ runtime, socketPath, version, onShutdown, s
|
|
|
258
196
|
for (const rootId of new Set(runtime.recoverable().map(entry => entry.rootId))) {
|
|
259
197
|
if (!reopened.has(rootId)) await runtime.abandon(rootId, 'its root session cannot be reopened').catch(() => {});
|
|
260
198
|
}
|
|
261
|
-
for (const { record
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
function trees(list: Iterable<SessionRecord>) {
|
|
265
|
-
const byRoot = new Map<string, SessionRecord[]>();
|
|
266
|
-
for (const record of list) byRoot.set(record.rootId, [...(byRoot.get(record.rootId) ?? []), record]);
|
|
267
|
-
return byRoot;
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
/**
|
|
271
|
-
* Retention (ALPD §68): deletes trees nobody needs any more, with their records, timeline and
|
|
272
|
-
* run log. A tree stays while any of its sessions is live or was written in the last 30 days,
|
|
273
|
-
* while a receipt of one of its sessions is still honoured (7 days from its last write, so a
|
|
274
|
-
* retried prompt is answered from it), while the runtime can continue one of its assignments,
|
|
275
|
-
* and while it is open, attached or being recovered. Timelines are never trimmed while their
|
|
276
|
-
* tree stays, so every cursor a client holds for it replays.
|
|
277
|
-
*/
|
|
278
|
-
async function expire(byRoot: Map<string, SessionRecord[]>, now = Date.now()) {
|
|
279
|
-
const removed = new Set<string>();
|
|
280
|
-
const receiptWritten = new Map<string, number>();
|
|
281
|
-
// Receipts age as the store prunes them: by when their file was written, and by what they say.
|
|
282
|
-
for (const { receipt, writtenAt } of await store!.receipts()) {
|
|
283
|
-
const at = Date.parse(receipt.updatedAt);
|
|
284
|
-
receiptWritten.set(receipt.sessionId, Math.max(receiptWritten.get(receipt.sessionId) ?? 0, Number.isNaN(at) ? now : at, writtenAt));
|
|
285
|
-
}
|
|
286
|
-
const continuing = new Set(runtime.recoverable().map(entry => entry.rootId));
|
|
287
|
-
for (const [root, tree] of byRoot) {
|
|
288
|
-
if (!tree.some(record => record.id === root)) continue;
|
|
289
|
-
if (tree.some(record => LIVE.includes(record.status))) continue;
|
|
290
|
-
if (now - Math.max(...tree.map(record => Date.parse(record.updatedAt) || now)) <= RETENTION_MS) continue;
|
|
291
|
-
if (tree.some(record => now - (receiptWritten.get(record.id) ?? 0) <= RECEIPT_RETENTION_MS)) continue;
|
|
292
|
-
if (continuing.has(root) || rootOf.has(root) || attached.get(root)?.size || recovering.has(root)) continue;
|
|
293
|
-
const ids = tree.map(record => record.id);
|
|
294
|
-
// A tree restored from a backup counts from its restore: its files were written then.
|
|
295
|
-
if (now - await store!.writtenAt(ids) <= RETENTION_MS) continue;
|
|
296
|
-
if (rootOf.has(root) || attached.get(root)?.size || recovering.has(root)) continue;
|
|
297
|
-
// Forgotten before the files go, so a resume arriving meanwhile finds nothing to reopen.
|
|
298
|
-
for (const id of ids) records.delete(id);
|
|
299
|
-
cursors.delete(root);
|
|
300
|
-
await store!.remove(root, ids);
|
|
301
|
-
if (runLogDir) await unlink(path.join(runLogDir, `${root.replace(/[^\w.-]/g, '_')}.jsonl`)).catch(() => {});
|
|
302
|
-
removed.add(root);
|
|
303
|
-
}
|
|
304
|
-
if (removed.size) recoveryLog.info(`retention removed ${removed.size} session tree${removed.size > 1 ? 's' : ''} unused for ${RETENTION_MS / 86_400_000} days`, { code: 'ALP_RETENTION_REMOVED', trees: removed.size });
|
|
305
|
-
return removed;
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
/** Retention while alpd runs: receipts past their window, then expired trees; temporary files stay, as writes may be under way. */
|
|
309
|
-
async function sweep() {
|
|
310
|
-
if (closing || !store) return;
|
|
311
|
-
try {
|
|
312
|
-
await restored;
|
|
313
|
-
await store.pruneReceipts(RECEIPT_RETENTION_MS, { temporaries: false });
|
|
314
|
-
await expire(trees(records.values()));
|
|
315
|
-
} catch (error) {
|
|
316
|
-
recoveryLog.warn('retention sweep failed', { code: 'ALP_RETENTION_FAILED', error });
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
const sweeper = store ? setInterval(() => void sweep(), retentionSweepMs) : undefined;
|
|
320
|
-
sweeper?.unref();
|
|
321
|
-
|
|
322
|
-
/**
|
|
323
|
-
* Completes receipts a crash left pending when the tree's timeline shows how the runtime
|
|
324
|
-
* answered; the rest stay pending, so their prompt answers outcome_unknown and is never
|
|
325
|
-
* sent again by itself.
|
|
326
|
-
*/
|
|
327
|
-
async function reconcileReceipts(loaded: SessionRecord[]) {
|
|
328
|
-
const rootOfRecord = new Map(loaded.map(record => [record.id, record.rootId]));
|
|
329
|
-
const timelines = new Map<string, Envelope[]>();
|
|
330
|
-
for (const { key, receipt } of await store!.receipts()) {
|
|
331
|
-
if (receipt.state !== 'pending') continue;
|
|
332
|
-
const root = rootOfRecord.get(receipt.sessionId);
|
|
333
|
-
if (!root) continue;
|
|
334
|
-
if (!timelines.has(root)) timelines.set(root, await store!.timeline(root).catch(() => []));
|
|
335
|
-
const answer = timelines.get(root)!.find(({ sessionId, event }) => sessionId === receipt.sessionId
|
|
336
|
-
&& (event.type === 'prompt.accepted' || event.type === 'prompt.failed') && event.clientMessageId === receipt.clientMessageId);
|
|
337
|
-
if (!answer) continue;
|
|
338
|
-
const event = answer.event as Extract<AlpEvent, { type: 'prompt.accepted' | 'prompt.failed' }>;
|
|
339
|
-
const outcome = event.type === 'prompt.accepted' ? 'accepted' : event.error.code === 'outcome_unknown' ? 'unknown' : 'failed';
|
|
340
|
-
await store!.putReceipt(key, { ...receipt, state: 'completed', outcome, ...(event.type === 'prompt.failed' ? { error: event.error } : {}), reconciled: true, updatedAt: new Date().toISOString() })
|
|
341
|
-
.catch(() => {});
|
|
342
|
-
}
|
|
199
|
+
for (const { record } of reopen) recovering.add(record.id);
|
|
200
|
+
void Promise.all(reopen.map(({ record, working }) => recoverTree(record, working)));
|
|
343
201
|
}
|
|
344
202
|
|
|
345
203
|
function resumable(record: SessionRecord) {
|
|
@@ -350,18 +208,16 @@ export function createDaemonServer({ runtime, socketPath, version, onShutdown, s
|
|
|
350
208
|
async function recoverTree(record: SessionRecord, working: boolean) {
|
|
351
209
|
const session = record.session!;
|
|
352
210
|
try {
|
|
353
|
-
|
|
354
|
-
seen(record.id, stored);
|
|
355
|
-
logs.set(record.id, [...stored]);
|
|
211
|
+
logs.set(record.id, [...await treeLog(record.id)]);
|
|
356
212
|
await runtime.open(record.id, {
|
|
357
213
|
...record.spec!,
|
|
358
214
|
persist: true,
|
|
359
215
|
restore: { agent: session.agent, threadId: session.threadId, runtime: session.runtime, model: session.model, workflow: session.workflow },
|
|
360
216
|
}, { history: 'skip', delegation: record.delegation ?? true });
|
|
361
217
|
const outcomes = await runtime.recover(record.id, { continueRoot: working });
|
|
362
|
-
|
|
218
|
+
console.log(`${new Date().toISOString()} recovered ${record.id}: ${outcomes.map(outcome => `${outcome.agent} ${outcome.outcome}`).join(', ') || 'root'}`);
|
|
363
219
|
} catch (error: any) {
|
|
364
|
-
|
|
220
|
+
console.error(`${new Date().toISOString()} could not recover ${record.id}`, error);
|
|
365
221
|
await runtime.abandon(record.id, error?.message ?? String(error)).catch(() => {});
|
|
366
222
|
} finally {
|
|
367
223
|
recovering.delete(record.id);
|
|
@@ -370,64 +226,25 @@ export function createDaemonServer({ runtime, socketPath, version, onShutdown, s
|
|
|
370
226
|
}
|
|
371
227
|
|
|
372
228
|
/**
|
|
373
|
-
* Sends a prompt at most once per (session, clientMessageId), as Paseo's MessageReceipts
|
|
374
|
-
*
|
|
375
|
-
*
|
|
376
|
-
* the first delivery did, with other content a conflict; a receipt left pending by a
|
|
377
|
-
* crash, or a delivery the provider may or may not have received, is outcome_unknown.
|
|
229
|
+
* Sends a prompt at most once per (session, clientMessageId), as Paseo's MessageReceipts:
|
|
230
|
+
* a repeat with the same content is a no-op, with other content a conflict, and a
|
|
231
|
+
* receipt left pending by a crash means the outcome is unknown.
|
|
378
232
|
*/
|
|
379
233
|
function deliverOnce(sessionId: string, clientMessageId: string, fingerprint: string, send: () => Promise<void>) {
|
|
380
234
|
const key = digest(['prompt', sessionId, clientMessageId]);
|
|
381
|
-
const unknown = () => new RpcError(ERROR.conflict, `alpd cannot tell whether message ${clientMessageId} reached the agent; check the session, then send it again with a new id`, { reason: 'outcome_unknown' });
|
|
382
|
-
const answer = (receipt: Receipt, extra: object = {}) => ({ outcome: receipt.outcome ?? 'accepted', ...(receipt.error ? { error: receipt.error } : {}), ...extra });
|
|
383
235
|
const attempt = async () => {
|
|
384
|
-
|
|
385
|
-
if (!existing) {
|
|
386
|
-
try { existing = await store!.receipt(key); }
|
|
387
|
-
catch (error) { throw persistenceFailed(`message ${clientMessageId}`, error); }
|
|
388
|
-
}
|
|
236
|
+
const existing = await store!.receipt(key);
|
|
389
237
|
if (existing) {
|
|
390
|
-
if (existing.state === 'unreadable') throw unknown();
|
|
391
238
|
if (existing.fingerprint !== fingerprint) throw new RpcError(ERROR.conflict, `Message ${clientMessageId} was already sent with different content`, { reason: 'key_conflict' });
|
|
392
|
-
if (existing.state === '
|
|
393
|
-
|
|
239
|
+
if (existing.state === 'completed') return { duplicate: true };
|
|
240
|
+
throw new RpcError(ERROR.conflict, `alpd stopped while delivering message ${clientMessageId}; check the session, then send it again with a new id`, { reason: 'outcome_unknown' });
|
|
394
241
|
}
|
|
395
|
-
const receipt = (state: Receipt['state']
|
|
242
|
+
const receipt = (state: Receipt['state']): Receipt => ({ version: 1, sessionId, clientMessageId, fingerprint, state, updatedAt: new Date().toISOString() });
|
|
396
243
|
live(sessionId);
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
crashPoint('prompt.after-receipt');
|
|
402
|
-
// The runtime reports how the provider answered as an event, before prompt() resolves.
|
|
403
|
-
let reply: { outcome: 'accepted' | 'failed' | 'unknown'; error?: { message: string; code?: string } } = { outcome: 'accepted' };
|
|
404
|
-
const stop = runtime.onEvent(({ sessionId: from, event }) => {
|
|
405
|
-
if (from !== sessionId) return;
|
|
406
|
-
if (event.type === 'prompt.accepted' && event.clientMessageId === clientMessageId) reply = { outcome: 'accepted' };
|
|
407
|
-
if (event.type === 'prompt.failed' && event.clientMessageId === clientMessageId) reply = { outcome: event.error.code === 'outcome_unknown' ? 'unknown' : 'failed', error: event.error };
|
|
408
|
-
});
|
|
409
|
-
try {
|
|
410
|
-
await send();
|
|
411
|
-
} catch (error) {
|
|
412
|
-
// The runtime refused it before reaching the provider (the session closed meanwhile): not sent.
|
|
413
|
-
await store!.removeReceipt(key).catch(() => {});
|
|
414
|
-
throw error;
|
|
415
|
-
} finally {
|
|
416
|
-
stop();
|
|
417
|
-
}
|
|
418
|
-
const root = rootOf.get(sessionId) ?? sessionId;
|
|
419
|
-
const lost = await store!.drain({ records: [sessionId], timelines: [root] });
|
|
420
|
-
crashPoint('prompt.after-send');
|
|
421
|
-
const done = receipt('completed', reply);
|
|
422
|
-
let durable = lost.length === 0;
|
|
423
|
-
try { await store!.putReceipt(key, done); }
|
|
424
|
-
catch {
|
|
425
|
-
unsaved.set(key, done);
|
|
426
|
-
durable = false;
|
|
427
|
-
}
|
|
428
|
-
crashPoint('prompt.after-completed');
|
|
429
|
-
if (reply.outcome === 'unknown') throw unknown();
|
|
430
|
-
return answer(done, { durable });
|
|
244
|
+
await store!.putReceipt(key, receipt('pending'));
|
|
245
|
+
await send();
|
|
246
|
+
await store!.putReceipt(key, receipt('completed'));
|
|
247
|
+
return {};
|
|
431
248
|
};
|
|
432
249
|
const previous = delivering.get(key) ?? Promise.resolve();
|
|
433
250
|
const result = previous.catch(() => {}).then(attempt);
|
|
@@ -436,15 +253,6 @@ export function createDaemonServer({ runtime, socketPath, version, onShutdown, s
|
|
|
436
253
|
return result;
|
|
437
254
|
}
|
|
438
255
|
|
|
439
|
-
/**
|
|
440
|
-
* Waits for an opened root's record and timeline (its native thread) to be written, so a
|
|
441
|
-
* crash after the answer can still resume it (ALPD §66); `durable: false` when they were not.
|
|
442
|
-
*/
|
|
443
|
-
async function persisted(root: string) {
|
|
444
|
-
if (!store) return {};
|
|
445
|
-
return { durable: (await store.drain({ records: [root], timelines: [root] })).length === 0 };
|
|
446
|
-
}
|
|
447
|
-
|
|
448
256
|
function forget(root: string) {
|
|
449
257
|
if (runtime.snapshot(root)) return;
|
|
450
258
|
for (const [id, candidate] of rootOf) if (candidate === root) rootOf.delete(id);
|
|
@@ -560,7 +368,6 @@ export function createDaemonServer({ runtime, socketPath, version, onShutdown, s
|
|
|
560
368
|
const agent = spec.agent ?? spec.restore?.agent;
|
|
561
369
|
if (agent !== undefined && agent !== session.agent) throw new RpcError(ERROR.failed, 'Cannot resume a session as a different ALP agent');
|
|
562
370
|
const stored = await treeLog(record.id);
|
|
563
|
-
seen(record.id, stored);
|
|
564
371
|
logs.set(record.id, [...stored]);
|
|
565
372
|
attach(connection, record.id);
|
|
566
373
|
try {
|
|
@@ -579,23 +386,20 @@ export function createDaemonServer({ runtime, socketPath, version, onShutdown, s
|
|
|
579
386
|
}
|
|
580
387
|
}
|
|
581
388
|
|
|
582
|
-
/** Projects, tasks, library and settings (docs/protocol/alp-json-rpc-v1.md §15); a team save syncs the projects sessions ran in. */
|
|
583
|
-
const operations = createOperations({ home, templates, knownProjects: () => new Set([...records.values()].flatMap(record => record.session?.projectRoot ? [record.session.projectRoot] : [])) });
|
|
584
389
|
const extensionOwners = new Map<string, Connection>();
|
|
585
390
|
const handlers: Record<string, (connection: Connection, params: any) => Promise<unknown> | unknown> = {
|
|
586
391
|
'extension.register': async (connection, params) => {
|
|
587
392
|
const method = params?.method;
|
|
588
393
|
if (typeof method !== 'string' || !/^[a-zA-Z][a-zA-Z0-9._-]*$/.test(method) || method === '__proto__' || method === 'constructor' || method === 'toString') throw new RpcError(ERROR.protocol, 'extension.register requires a safe method name');
|
|
589
|
-
if (method
|
|
394
|
+
if (typeof method !== 'string' || !method.trim()) throw new RpcError(ERROR.protocol, 'extension.register requires a method name');
|
|
590
395
|
if (originalMethods.has(method) || extensionOwners.has(method) || Object.hasOwn(handlers, method)) throw new RpcError(ERROR.conflict, `${method} is already registered`);
|
|
591
|
-
if ((methodOwners.get(connection)?.size ?? 0) >= 128 || extensionOwners.size >= 1024) throw new RpcError(ERROR.conflict, 'too many registered extension methods');
|
|
592
396
|
extensionOwners.set(method, connection);
|
|
593
397
|
if (!methodOwners.has(connection)) methodOwners.set(connection, new Set());
|
|
594
398
|
methodOwners.get(connection).add(method);
|
|
595
399
|
handlers[method] = async (_connection, invokeParams) => {
|
|
596
400
|
const owner = extensionOwners.get(method);
|
|
597
401
|
if (!owner || !connections.has(owner)) { delete handlers[method]; extensionOwners.delete(method); throw new RpcError(ERROR.notFound, `${method} is not available`); }
|
|
598
|
-
return invokeExtended(owner,
|
|
402
|
+
return invokeExtended(owner, method, invokeParams ?? {});
|
|
599
403
|
};
|
|
600
404
|
return { registered: method };
|
|
601
405
|
},
|
|
@@ -611,7 +415,7 @@ export function createDaemonServer({ runtime, socketPath, version, onShutdown, s
|
|
|
611
415
|
owned?.delete(method);
|
|
612
416
|
return { unregistered: method };
|
|
613
417
|
},
|
|
614
|
-
'daemon.status':
|
|
418
|
+
'daemon.status': () => ({ pid: process.pid, startedAt, version, protocolVersion: PROTOCOL_VERSION, sessions: runtime.list().length, questions: runtime.questions().length, ...(previousExit ? { previousExit } : {}) }),
|
|
615
419
|
|
|
616
420
|
'session.status'(_connection, { sessionId }) {
|
|
617
421
|
if (typeof sessionId !== 'string') throw new RpcError(-32602, 'sessionId is required');
|
|
@@ -693,8 +497,6 @@ export function createDaemonServer({ runtime, socketPath, version, onShutdown, s
|
|
|
693
497
|
if (sessionId !== undefined && (typeof sessionId !== 'string' || !SESSION_ID.test(sessionId))) throw new RpcError(-32602, 'Invalid session id');
|
|
694
498
|
if (history !== 'replay' && history !== 'skip') throw new RpcError(-32602, 'history must be replay or skip');
|
|
695
499
|
const id = sessionId ?? `ses_${randomBytes(8).toString('hex')}`;
|
|
696
|
-
// A root alpd is reopening after a restart: attach once it is open, rather than open it twice.
|
|
697
|
-
await recovering.get(id);
|
|
698
500
|
const existing = runtime.snapshot(id);
|
|
699
501
|
if (existing) {
|
|
700
502
|
// Reopening a root that kept working after its viewer left: attach to it.
|
|
@@ -706,24 +508,13 @@ export function createDaemonServer({ runtime, socketPath, version, onShutdown, s
|
|
|
706
508
|
}
|
|
707
509
|
const record = records.get(id);
|
|
708
510
|
if (!record && resuming) throw new RpcError(ERROR.notFound, `ALP session ${id} no longer exists in alpd`);
|
|
709
|
-
if (record) {
|
|
710
|
-
const session = await resume(connection, record, spec, history, delegation);
|
|
711
|
-
return { session, attached: false, resumed: true, ...await persisted(id) };
|
|
712
|
-
}
|
|
511
|
+
if (record) return { session: await resume(connection, record, spec, history, delegation), attached: false, resumed: true };
|
|
713
512
|
const { restore: _restore, ...kept } = spec;
|
|
714
513
|
const now = new Date().toISOString();
|
|
715
|
-
|
|
716
|
-
records.set(id, created);
|
|
717
|
-
// A root is resumable only once its record is on disk; without it the create fails and nothing starts.
|
|
718
|
-
try { await store?.put(created); }
|
|
719
|
-
catch (error) {
|
|
720
|
-
if (records.get(id) === created) records.delete(id);
|
|
721
|
-
throw persistenceFailed(`session ${id}; it was not started`, error);
|
|
722
|
-
}
|
|
514
|
+
save({ version: 1, id, rootId: id, spec: kept, delegation, status: 'initializing', createdAt: now, updatedAt: now });
|
|
723
515
|
attach(connection, id);
|
|
724
516
|
try {
|
|
725
|
-
|
|
726
|
-
return { session, attached: false, ...await persisted(id) };
|
|
517
|
+
return { session: await runtime.open(id, spec, { history, delegation }), attached: false };
|
|
727
518
|
} catch (error) {
|
|
728
519
|
detach(connection, id);
|
|
729
520
|
records.delete(id);
|
|
@@ -738,37 +529,13 @@ export function createDaemonServer({ runtime, socketPath, version, onShutdown, s
|
|
|
738
529
|
return { preview: await runtime.preview(spec) };
|
|
739
530
|
},
|
|
740
531
|
|
|
741
|
-
|
|
742
|
-
* Watches a tree. With `after`, the cursor of the last event a client has, only later
|
|
743
|
-
* events are replayed (ALPD §66); a cursor this tree never reached answers `reset: true`
|
|
744
|
-
* with the whole timeline. The replay and the live events that follow are in cursor order.
|
|
745
|
-
*/
|
|
746
|
-
async 'session.attach'(connection, { sessionId, replay = true, after }) {
|
|
747
|
-
if (after !== undefined && (!Number.isSafeInteger(after) || after < 0)) throw new RpcError(-32602, 'after must be a cursor');
|
|
532
|
+
async 'session.attach'(connection, { sessionId, replay = true }) {
|
|
748
533
|
const found = known(sessionId);
|
|
749
534
|
const root = rootOf.get(sessionId) ?? records.get(sessionId)?.rootId ?? sessionId;
|
|
750
|
-
let stored: Envelope[] = [];
|
|
751
|
-
if (replay || (store && !cursors.has(root))) {
|
|
752
|
-
// The in-memory log keeps a live tree's latest events only; older ones come from the store.
|
|
753
|
-
const memory = logs.get(root);
|
|
754
|
-
const covered = !!memory?.length && (memory[0].cursor ?? 1) <= (after ?? 0) + 1;
|
|
755
|
-
if (!covered) stored = store ? await store.timeline(root) : memory ?? [];
|
|
756
|
-
}
|
|
757
|
-
// From here to the return nothing awaits, so no live event slips between replay and attach.
|
|
758
|
-
const live = logs.get(root) ?? [];
|
|
759
|
-
const tip = Math.max(cursors.get(root) ?? 0, stored.at(-1)?.cursor ?? 0, live.at(-1)?.cursor ?? 0);
|
|
760
|
-
// Without a store the in-memory log keeps only the latest events: a cursor before them cannot continue either.
|
|
761
|
-
const first = replay ? [...stored, ...live].find(envelope => envelope.cursor !== undefined)?.cursor : undefined;
|
|
762
|
-
const reset = after !== undefined && (after > tip || (replay && after < tip && (first === undefined || first > after + 1)));
|
|
763
|
-
const from = reset ? 0 : after ?? 0;
|
|
764
535
|
attach(connection, root);
|
|
765
|
-
if (replay)
|
|
766
|
-
let last = from;
|
|
767
|
-
for (const envelope of stored) if ((envelope.cursor ?? 0) > last) { if (current(envelope)) deliver(connection, envelope); last = envelope.cursor ?? last; }
|
|
768
|
-
for (const envelope of live) if ((envelope.cursor ?? 0) > last) { if (current(envelope)) deliver(connection, envelope); last = envelope.cursor ?? last; }
|
|
769
|
-
}
|
|
536
|
+
if (replay) for (const envelope of await treeLog(root)) if (current(envelope)) deliver(connection, envelope);
|
|
770
537
|
announceQuestions(connection, root);
|
|
771
|
-
return { session: root === found.id ? found : summary(root)
|
|
538
|
+
return { session: root === found.id ? found : summary(root) };
|
|
772
539
|
},
|
|
773
540
|
|
|
774
541
|
async 'session.release'(connection, { sessionId }) {
|
|
@@ -807,14 +574,12 @@ export function createDaemonServer({ runtime, socketPath, version, onShutdown, s
|
|
|
807
574
|
},
|
|
808
575
|
|
|
809
576
|
/** The user's name for a root, in place of its first prompt (ALPD §61). */
|
|
810
|
-
|
|
811
|
-
if (typeof title !== 'string' || !title.trim() ||
|
|
577
|
+
'session.rename'(_connection, { sessionId, title }) {
|
|
578
|
+
if (typeof title !== 'string' || !title.trim() || title.length > 200) throw new RpcError(-32602, 'title must be 1 to 200 characters');
|
|
812
579
|
const record = rootRecord(sessionId);
|
|
813
580
|
// A name is not activity: the record keeps its time.
|
|
814
|
-
const before = record.title;
|
|
815
581
|
Object.assign(record, { title: title.replace(/\s+/g, ' ').trim() });
|
|
816
|
-
|
|
817
|
-
catch (error) { record.title = before; throw persistenceFailed(`the name of ${record.id}`, error); }
|
|
582
|
+
void store?.put(record);
|
|
818
583
|
return { session: summary(record.id) };
|
|
819
584
|
},
|
|
820
585
|
|
|
@@ -822,10 +587,8 @@ export function createDaemonServer({ runtime, socketPath, version, onShutdown, s
|
|
|
822
587
|
async 'session.archive'(_connection, { sessionId, archived = true }) {
|
|
823
588
|
const record = rootRecord(sessionId);
|
|
824
589
|
if (archived && runtime.snapshot(record.id)) await runtime.close(record.id);
|
|
825
|
-
const before = record.archived;
|
|
826
590
|
Object.assign(record, { archived: archived === true ? true : undefined });
|
|
827
|
-
|
|
828
|
-
catch (error) { record.archived = before; throw persistenceFailed(`the archive state of ${record.id}`, error); }
|
|
591
|
+
void store?.put(record);
|
|
829
592
|
return { session: summary(record.id) };
|
|
830
593
|
},
|
|
831
594
|
|
|
@@ -844,70 +607,30 @@ export function createDaemonServer({ runtime, socketPath, version, onShutdown, s
|
|
|
844
607
|
return { sessions };
|
|
845
608
|
},
|
|
846
609
|
};
|
|
847
|
-
for (const [method, handler] of Object.entries(operations.handlers)) handlers[method] = (_connection, params) => handler(params);
|
|
848
610
|
Object.setPrototypeOf(handlers, null);
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
const reservedNamespaces = new Set([...originalMethods].map(method => method.split('.')[0]));
|
|
611
|
+
const originalMethods = new Set(Object.keys(handlers));
|
|
612
|
+
originalMethods.add('daemon.hello');
|
|
852
613
|
|
|
853
|
-
async function invokeExtended(owner: Connection,
|
|
614
|
+
async function invokeExtended(owner: Connection, method: string, params: any) {
|
|
854
615
|
if (owner.pending.size >= 128) throw new RpcError(ERROR.conflict, 'too many pending extension calls');
|
|
855
616
|
const id = ++owner.nextId;
|
|
856
|
-
const invocation = { jsonrpc: '2.0', id, method: 'extension.invoke', params: { method, params } };
|
|
857
|
-
if (Buffer.byteLength(encodeFrame(invocation), 'utf8') > MAX_FRAME) throw new RpcError(-32600, 'ALP daemon frame exceeds size limit');
|
|
858
617
|
return new Promise((resolve, reject) => {
|
|
859
618
|
const timer = setTimeout(() => {
|
|
860
619
|
owner.pending.delete(id);
|
|
861
620
|
reject(new RpcError(ERROR.failed, `${method} timed out`));
|
|
862
621
|
}, 30_000);
|
|
863
|
-
owner.pending.set(id, { resolve, reject, timer
|
|
864
|
-
|
|
865
|
-
catch (error) { clearTimeout(timer); owner.pending.delete(id); reject(error); }
|
|
622
|
+
owner.pending.set(id, { resolve, reject, timer });
|
|
623
|
+
owner.send({ jsonrpc: '2.0', id, method: 'extension.invoke', params: { method, params } });
|
|
866
624
|
});
|
|
867
625
|
}
|
|
868
626
|
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
* Not ready is a state to fix, not a reason to stop: the checks say what and the codes
|
|
873
|
-
* match alpd's log.
|
|
874
|
-
*/
|
|
875
|
-
async function health() {
|
|
876
|
-
const checks: Array<{ id: string; ok: boolean; code?: string; message?: string }> = [{ id: 'accepting', ok: !closing, ...(closing ? { code: 'ALP_SHUTTING_DOWN', message: 'alpd is shutting down' } : {}) }];
|
|
877
|
-
if (store) {
|
|
878
|
-
const probe = await store.probe();
|
|
879
|
-
checks.push({ id: 'state', ...probe });
|
|
880
|
-
const failing = store.failing();
|
|
881
|
-
checks.push({ id: 'persistence', ok: failing === 0, ...(failing ? { code: 'ALP_PERSISTENCE_FAILING', message: `${failing} write${failing > 1 ? 's' : ''} of sessions or timelines failed and have not been written since` } : {}) });
|
|
882
|
-
}
|
|
883
|
-
return { live: true, ready: checks.every(check => check.ok), checks };
|
|
884
|
-
}
|
|
885
|
-
|
|
886
|
-
/** What this daemon lists in daemon.hello: only features it serves now (docs/protocol/alp-json-rpc-v1.md §6). */
|
|
887
|
-
const capabilities = [CAPABILITY.attachAfter, CAPABILITY.extensionMethods, ...(store ? [CAPABILITY.promptReceipts] : []), ...operations.capabilities];
|
|
888
|
-
|
|
889
|
-
/** A response too large for the wire becomes an error the client can read. */
|
|
890
|
-
function fits(response: { jsonrpc: '2.0'; id: string | number | null; result?: unknown; error?: unknown }) {
|
|
891
|
-
if (frameBytes(response) <= MAX_FRAME) return response;
|
|
892
|
-
log.warn('a response exceeded the frame size limit', { code: 'ALP_IPC_RESPONSE_TOO_LARGE', requestId: String(response.id) });
|
|
893
|
-
return { jsonrpc: '2.0', id: response.id, error: { code: ERROR.failed, message: 'The response exceeds the ALP frame size limit', data: { reason: 'response_too_large' } } };
|
|
894
|
-
}
|
|
895
|
-
|
|
896
|
-
/**
|
|
897
|
-
* One incoming message, in the order docs/protocol/alp-json-rpc-v1.md §5 fixes: a message that is
|
|
898
|
-
* not a single JSON-RPC 2.0 object, a frame over the limit, a notification, an invalid id or params,
|
|
899
|
-
* the handshake, an unknown method, then the method's own params. Nothing runs before all of them pass.
|
|
900
|
-
*/
|
|
901
|
-
async function dispatch(connection: Connection, message: any, receivedBytes?: number) {
|
|
902
|
-
const single = !!message && typeof message === 'object' && !Array.isArray(message);
|
|
903
|
-
const { id, method, params } = single ? message : {} as any;
|
|
904
|
-
const respond = (body: object) => connection.send(fits({ jsonrpc: '2.0', id: validId(id) ? id : null, ...body }));
|
|
627
|
+
async function dispatch(connection: Connection, message: any) {
|
|
628
|
+
const { id, method, params } = message ?? {};
|
|
629
|
+
const respond = (body: object) => { if (id !== undefined && id !== null) connection.send({ jsonrpc: '2.0', id, ...body }); };
|
|
905
630
|
try {
|
|
906
|
-
if (Array.isArray(message)) throw new RpcError(RPC_ERROR.invalidRequest, 'Batch requests are not supported', { reason: 'batch_unsupported' });
|
|
907
631
|
// Validate the envelope before anything else: a frame that is not JSON-RPC 2.0 must never be
|
|
908
632
|
// correlated against (or consume) a pending extension call, however it is shaped.
|
|
909
|
-
if (!
|
|
910
|
-
const oversized = (receivedBytes ?? frameBytes(message)) > MAX_FRAME;
|
|
633
|
+
if (!message || typeof message !== 'object' || Array.isArray(message) || message.jsonrpc !== '2.0') throw new RpcError(-32600, 'Invalid request');
|
|
911
634
|
// A frame with no `method` property is a response to an outgoing extension.invoke. It must carry
|
|
912
635
|
// exactly one of result/error and an id; a malformed or unknown one is dropped, leaving any
|
|
913
636
|
// pending invocation intact (never mis-correlated, never leaked).
|
|
@@ -919,40 +642,25 @@ export function createDaemonServer({ runtime, socketPath, version, onShutdown, s
|
|
|
919
642
|
if (!waiter) return;
|
|
920
643
|
connection.pending.delete(id);
|
|
921
644
|
clearTimeout(waiter.timer);
|
|
922
|
-
if (oversized) { waiter.reject(new RpcError(RPC_ERROR.invalidRequest, 'ALP daemon frame exceeds size limit')); return; }
|
|
923
645
|
if (hasError) waiter.reject(new RpcError(message.error?.code ?? ERROR.failed, message.error?.message ?? 'extension call failed', message.error?.data));
|
|
924
646
|
else waiter.resolve(message.result);
|
|
925
647
|
return;
|
|
926
648
|
}
|
|
927
|
-
if (
|
|
928
|
-
if (typeof method !== 'string') throw new RpcError(RPC_ERROR.invalidRequest, 'Invalid request');
|
|
929
|
-
// v1 defines no notifications to the daemon: one is dropped unrun, and JSON-RPC forbids answering it.
|
|
930
|
-
if (id === undefined) { log.debug('ignored a notification', { code: 'ALP_IPC_NOTIFICATION_IGNORED', method }); return; }
|
|
931
|
-
if (!validId(id)) throw new RpcError(RPC_ERROR.invalidRequest, 'A request id must be a string of at most 128 characters or a safe integer', { reason: 'invalid_id' });
|
|
932
|
-
if (params !== undefined && (params === null || typeof params !== 'object' || Array.isArray(params))) throw new RpcError(RPC_ERROR.invalidParams, 'params must be an object', { reason: 'invalid_params' });
|
|
649
|
+
if (typeof method !== 'string') throw new RpcError(-32600, 'Invalid request');
|
|
933
650
|
if (method === 'daemon.hello') {
|
|
934
|
-
if (params?.protocolVersion !== PROTOCOL_VERSION) throw new RpcError(ERROR.protocol, `ALP daemon speaks protocol ${PROTOCOL_VERSION}; restart it after upgrading (alp daemon restart)
|
|
935
|
-
const issues = validateParams(method, params)!;
|
|
936
|
-
if (issues.length) throw invalidParams(issues);
|
|
651
|
+
if (params?.protocolVersion !== PROTOCOL_VERSION) throw new RpcError(ERROR.protocol, `ALP daemon speaks protocol ${PROTOCOL_VERSION}; restart it after upgrading (alp daemon restart)`);
|
|
937
652
|
connection.helloed = true;
|
|
938
|
-
respond({ result: { protocolVersion: PROTOCOL_VERSION, daemonVersion: version, pid: process.pid
|
|
653
|
+
respond({ result: { protocolVersion: PROTOCOL_VERSION, daemonVersion: version, pid: process.pid } });
|
|
939
654
|
return;
|
|
940
655
|
}
|
|
941
|
-
if (!connection.helloed) throw new RpcError(ERROR.protocol, 'daemon.hello is required first'
|
|
656
|
+
if (!connection.helloed) throw new RpcError(ERROR.protocol, 'daemon.hello is required first');
|
|
942
657
|
await restored;
|
|
943
|
-
if (!connections.has(connection)) return;
|
|
944
658
|
const handler = Object.hasOwn(handlers, method) ? handlers[method] : undefined;
|
|
945
|
-
if (!handler) throw new RpcError(
|
|
946
|
-
// Built-in methods are checked against the v1 schema; extension methods own their params.
|
|
947
|
-
const issues = validateParams(method, params);
|
|
948
|
-
if (issues?.length) throw invalidParams(issues);
|
|
659
|
+
if (!handler) throw new RpcError(-32601, `Unknown method '${method}'`);
|
|
949
660
|
respond({ result: await handler(connection, params ?? {}) });
|
|
950
661
|
} catch (error) {
|
|
951
662
|
const code = error instanceof RpcError ? error.code : /is not open/.test(String((error as Error)?.message)) ? ERROR.notFound : ERROR.failed;
|
|
952
663
|
const data = error instanceof RpcError && error.data !== undefined ? { data: error.data } : {};
|
|
953
|
-
// Plain errors are mostly refusals of bad input; logged so an unexpected one can be traced by its request.
|
|
954
|
-
if (!(error instanceof RpcError) && code === ERROR.failed) log.warn(`${method} failed`, { code: 'ALP_RPC_FAILED', requestId: id, sessionId: params?.sessionId, method, error });
|
|
955
|
-
else if (error instanceof RpcError && (error.data as { reason?: string } | undefined)?.reason === 'persistence_failed') log.warn(`${method} failed`, { code: 'ALP_PERSISTENCE_FAILED', requestId: id, sessionId: params?.sessionId, method, error });
|
|
956
664
|
respond({ error: { code, message: error instanceof Error ? error.message : String(error), ...data } });
|
|
957
665
|
}
|
|
958
666
|
}
|
|
@@ -964,12 +672,6 @@ export function createDaemonServer({ runtime, socketPath, version, onShutdown, s
|
|
|
964
672
|
methodOwners.delete(connection);
|
|
965
673
|
for (const [id, waiting] of connection.pending) { clearTimeout(waiting.timer); waiting.reject(new Error('ALP daemon connection closed')); }
|
|
966
674
|
connection.pending.clear();
|
|
967
|
-
for (const owner of connections) for (const [id, waiting] of owner.pending) {
|
|
968
|
-
if (waiting.caller !== connection) continue;
|
|
969
|
-
clearTimeout(waiting.timer);
|
|
970
|
-
owner.pending.delete(id);
|
|
971
|
-
waiting.reject(new Error('ALP daemon caller connection closed'));
|
|
972
|
-
}
|
|
973
675
|
connections.delete(connection);
|
|
974
676
|
for (const root of [...connection.roots]) {
|
|
975
677
|
detach(connection, root);
|
|
@@ -989,36 +691,20 @@ export function createDaemonServer({ runtime, socketPath, version, onShutdown, s
|
|
|
989
691
|
};
|
|
990
692
|
connections.add(connection);
|
|
991
693
|
let buffer = '';
|
|
992
|
-
/** UTF-8 bytes of `buffer`, kept as chunks arrive so a large frame costs linear time. */
|
|
993
|
-
let bytes = 0;
|
|
994
694
|
socket.setEncoding('utf8');
|
|
995
|
-
|
|
996
|
-
const tooLarge = () => { log.warn('closed a connection that sent a frame over the size limit', { code: 'ALP_IPC_FRAME_TOO_LARGE' }); buffer = ''; bytes = 0; socket.destroy(); };
|
|
997
|
-
socket.on('data', (chunk: string) => {
|
|
695
|
+
socket.on('data', chunk => {
|
|
998
696
|
buffer += chunk;
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
return;
|
|
1004
|
-
}
|
|
1005
|
-
let start = 0;
|
|
1006
|
-
for (let index = buffer.indexOf('\n', buffer.length - chunk.length); index >= 0; index = buffer.indexOf('\n', start)) {
|
|
1007
|
-
const line = buffer.slice(start, index);
|
|
1008
|
-
start = index + 1;
|
|
1009
|
-
if (Buffer.byteLength(line, 'utf8') + 1 > MAX_FRAME) { tooLarge(); return; }
|
|
697
|
+
if (buffer.length > MAX_FRAME) { socket.destroy(); return; }
|
|
698
|
+
for (let index = buffer.indexOf('\n'); index >= 0; index = buffer.indexOf('\n')) {
|
|
699
|
+
const line = buffer.slice(0, index);
|
|
700
|
+
buffer = buffer.slice(index + 1);
|
|
1010
701
|
if (!line.trim()) continue;
|
|
1011
702
|
let message;
|
|
1012
|
-
try { message = JSON.parse(line); } catch {
|
|
1013
|
-
void dispatch(connection, message
|
|
703
|
+
try { message = JSON.parse(line); } catch { connection.send({ jsonrpc: '2.0', id: null, error: { code: -32700, message: 'Parse error' } }); continue; }
|
|
704
|
+
void dispatch(connection, message);
|
|
1014
705
|
}
|
|
1015
|
-
// What is left has no newline yet; with one it would already be over the limit.
|
|
1016
|
-
buffer = buffer.slice(start);
|
|
1017
|
-
bytes = Buffer.byteLength(buffer, 'utf8');
|
|
1018
|
-
if (bytes >= MAX_FRAME) tooLarge();
|
|
1019
706
|
});
|
|
1020
|
-
|
|
1021
|
-
socket.on('error', (error: NodeJS.ErrnoException) => log[error.code === 'ECONNRESET' || error.code === 'EPIPE' ? 'debug' : 'warn']('a client connection failed', { code: 'ALP_IPC_SOCKET_ERROR', error }));
|
|
707
|
+
socket.on('error', () => {});
|
|
1022
708
|
socket.on('close', () => disconnect(connection));
|
|
1023
709
|
});
|
|
1024
710
|
|
|
@@ -1043,40 +729,12 @@ export function createDaemonServer({ runtime, socketPath, version, onShutdown, s
|
|
|
1043
729
|
},
|
|
1044
730
|
|
|
1045
731
|
async listen() {
|
|
1046
|
-
if (process.platform !== 'win32') {
|
|
1047
|
-
await assertPrivateDirectory(path.dirname(socketPath), { create: true });
|
|
1048
|
-
const existing = await lstat(socketPath).catch(error => { if (error.code === 'ENOENT') return undefined; throw error; });
|
|
1049
|
-
if (existing) {
|
|
1050
|
-
await assertPrivateSocket(socketPath);
|
|
1051
|
-
// Never unlink a live daemon; only ECONNREFUSED proves a stale Unix socket.
|
|
1052
|
-
const active = await new Promise<boolean>((resolve, reject) => {
|
|
1053
|
-
const probe = net.createConnection(socketPath);
|
|
1054
|
-
probe.once('connect', () => { probe.destroy(); resolve(true); });
|
|
1055
|
-
probe.once('error', error => { probe.destroy(); if ((error as NodeJS.ErrnoException).code === 'ECONNREFUSED') resolve(false); else reject(error); });
|
|
1056
|
-
probe.setTimeout(1000, () => { probe.destroy(); reject(new Error('IPC endpoint probe timed out')); });
|
|
1057
|
-
});
|
|
1058
|
-
if (active) throw new Error('IPC endpoint already in use by a running daemon');
|
|
1059
|
-
const current = await assertPrivateSocket(socketPath);
|
|
1060
|
-
if (!current || current.dev !== existing.dev || current.ino !== existing.ino) throw new Error('IPC endpoint changed during validation');
|
|
1061
|
-
await unlink(socketPath);
|
|
1062
|
-
}
|
|
1063
|
-
}
|
|
1064
|
-
// libuv unlinks its bind path on close without checking inode identity. Bind at an
|
|
1065
|
-
// unpublished sibling, then publish; cleanup of the public endpoint is ours alone.
|
|
1066
|
-
const bindPath = process.platform === 'win32' ? socketPath : path.join(path.dirname(socketPath), `.s${randomBytes(3).toString('hex')}`);
|
|
732
|
+
if (process.platform !== 'win32') await unlink(socketPath).catch(() => {});
|
|
1067
733
|
await new Promise<void>((resolve, reject) => {
|
|
1068
734
|
server.once('error', reject);
|
|
1069
|
-
server.listen(
|
|
735
|
+
server.listen(socketPath, () => { server.off('error', reject); resolve(); });
|
|
1070
736
|
});
|
|
1071
|
-
if (process.platform !== 'win32')
|
|
1072
|
-
try {
|
|
1073
|
-
await chmod(bindPath, 0o600);
|
|
1074
|
-
const info = await lstat(bindPath);
|
|
1075
|
-
await rename(bindPath, socketPath);
|
|
1076
|
-
socketIdentity = { dev: info.dev, ino: info.ino };
|
|
1077
|
-
}
|
|
1078
|
-
catch (error) { await new Promise<void>(resolve => server.close(() => resolve())); throw error; }
|
|
1079
|
-
}
|
|
737
|
+
if (process.platform !== 'win32') await chmod(socketPath, 0o600);
|
|
1080
738
|
},
|
|
1081
739
|
|
|
1082
740
|
local() {
|
|
@@ -1144,15 +802,10 @@ export function createDaemonServer({ runtime, socketPath, version, onShutdown, s
|
|
|
1144
802
|
|
|
1145
803
|
async close() {
|
|
1146
804
|
closing = true;
|
|
1147
|
-
if (sweeper) clearInterval(sweeper);
|
|
1148
805
|
const stopped = new Promise<void>(resolve => server.close(() => resolve()));
|
|
1149
806
|
for (const connection of connections) connection.socket?.destroy();
|
|
1150
807
|
await stopped;
|
|
1151
|
-
if (
|
|
1152
|
-
const current = await lstat(socketPath).catch(error => { if (error.code === 'ENOENT') return undefined; throw error; });
|
|
1153
|
-
if (current?.isSocket() && current.dev === socketIdentity.dev && current.ino === socketIdentity.ino) await unlink(socketPath);
|
|
1154
|
-
socketIdentity = undefined;
|
|
1155
|
-
}
|
|
808
|
+
if (process.platform !== 'win32') await unlink(socketPath).catch(() => {});
|
|
1156
809
|
await store?.flush();
|
|
1157
810
|
},
|
|
1158
811
|
};
|