@anhlp/alp-daemon 0.0.1-beta.7 → 0.0.1-beta.8
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/dist/THIRD-PARTY-NOTICES.txt +1 -1
- package/dist/alpd.js +10588 -4190
- package/dist/build-manifest.json +8 -2
- package/dist/git.d.ts +6 -1
- package/dist/git.js +86 -32
- package/dist/main.js +51 -10
- package/dist/model-catalog.d.ts +2 -0
- package/dist/model-catalog.js +44 -0
- package/dist/operations.d.ts +8 -1
- package/dist/operations.js +32 -5
- package/dist/scheduler.js +2 -0
- package/dist/server.js +47 -11
- package/dist/terminals.d.ts +12 -2
- package/dist/terminals.js +3 -3
- package/dist/windows-terminal.d.ts +6 -3
- package/dist/windows-terminal.js +28 -5
- package/package.json +6 -6
- package/src/git.ts +102 -22
- package/src/main.ts +56 -7
- package/src/model-catalog.ts +36 -0
- package/src/operations.ts +18 -5
- package/src/scheduler.ts +6 -0
- package/src/server.ts +51 -11
- package/src/terminals.ts +18 -6
- package/src/windows-terminal.ts +31 -6
package/src/git.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process';
|
|
2
2
|
import { createHash, createHmac, randomBytes, timingSafeEqual } from 'node:crypto';
|
|
3
|
+
import type { Stats } from 'node:fs';
|
|
3
4
|
import { lstat, realpath, stat } from 'node:fs/promises';
|
|
4
5
|
import os from 'node:os';
|
|
5
6
|
import path from 'node:path';
|
|
@@ -53,6 +54,8 @@ export const GIT_READ = Object.freeze({
|
|
|
53
54
|
maxErrorBytes: 16 * 1024,
|
|
54
55
|
/** Configuration, change list and untracked list output, at most. */
|
|
55
56
|
maxListBytes: 16 * 1024 * 1024,
|
|
57
|
+
/** Ancestors retained per listing; exceeding the budget fails the read closed. */
|
|
58
|
+
maxAncestors: 16_384,
|
|
56
59
|
/** One patch stream, at most (its files are at most a window's worth of content). */
|
|
57
60
|
maxPatchStreamBytes: 32 * 1024 * 1024,
|
|
58
61
|
/** One side of a file scanned for a private key; a larger side is `too_large`. */
|
|
@@ -134,7 +137,7 @@ type RunOptions = {
|
|
|
134
137
|
/** Collect at most this much stdout; more fails (GitReadFailed). */
|
|
135
138
|
maxBytes?: number;
|
|
136
139
|
/** Stream stdout instead: return true to stop Git (killed; the result says `stopped`). */
|
|
137
|
-
onData?: (chunk: Buffer) => boolean | void
|
|
140
|
+
onData?: (chunk: Buffer) => boolean | void | Promise<boolean | void>;
|
|
138
141
|
};
|
|
139
142
|
|
|
140
143
|
function runGit(box: Box, args: string[], { stdin, okCodes = [0], maxBytes = GIT_READ.maxListBytes, onData }: RunOptions = {}): Promise<{ code: number; stdout: Buffer; stopped: boolean }> {
|
|
@@ -150,13 +153,18 @@ function runGit(box: Box, args: string[], { stdin, okCodes = [0], maxBytes = GIT
|
|
|
150
153
|
} catch {
|
|
151
154
|
// already gone
|
|
152
155
|
}
|
|
156
|
+
child.stdout?.resume();
|
|
153
157
|
};
|
|
154
158
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
159
|
+
let consuming: Promise<void> = Promise.resolve();
|
|
155
160
|
const finish = (outcome: () => void) => {
|
|
156
161
|
if (settled) return;
|
|
157
162
|
settled = true;
|
|
158
163
|
if (timer) clearTimeout(timer);
|
|
159
|
-
|
|
164
|
+
// An issued lstat cannot be cancelled. Retain this read's slot until its
|
|
165
|
+
// consumer settles, even when the caller's deadline has already returned,
|
|
166
|
+
// so repeated timeouts cannot accumulate unlimited filesystem requests.
|
|
167
|
+
void consuming.then(release);
|
|
160
168
|
outcome();
|
|
161
169
|
};
|
|
162
170
|
try {
|
|
@@ -171,23 +179,35 @@ function runGit(box: Box, args: string[], { stdin, okCodes = [0], maxBytes = GIT
|
|
|
171
179
|
const out: Buffer[] = [];
|
|
172
180
|
let outBytes = 0;
|
|
173
181
|
let err = '';
|
|
182
|
+
// Chunks are consumed strictly one after another: each consumer is chained after
|
|
183
|
+
// the previous one, so close/finish/release wait for every pending check. pause()
|
|
184
|
+
// is backpressure only; Node resumes stdout itself when the child exits
|
|
185
|
+
// (flushStdio), so it cannot provide mutual exclusion.
|
|
174
186
|
child.stdout!.on('data', (chunk: Buffer) => {
|
|
175
187
|
if (settled || stopped) return;
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
return;
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
188
|
+
child.stdout!.pause();
|
|
189
|
+
consuming = consuming.then(async () => {
|
|
190
|
+
if (settled || stopped) return;
|
|
191
|
+
if (onData) {
|
|
192
|
+
if (await onData(chunk)) { stopped = true; kill(); }
|
|
193
|
+
} else {
|
|
194
|
+
outBytes += chunk.length;
|
|
195
|
+
if (outBytes > maxBytes) throw new GitReadFailed('output over its bound');
|
|
196
|
+
out.push(chunk);
|
|
197
|
+
}
|
|
198
|
+
}).then(() => {
|
|
199
|
+
if (!settled && !stopped) child.stdout!.resume();
|
|
200
|
+
}, error => finish(() => { kill(); reject(error); }));
|
|
183
201
|
});
|
|
184
202
|
child.stderr!.on('data', (chunk: Buffer) => { if (err.length < GIT_READ.maxErrorBytes) err += chunk.toString('utf8'); });
|
|
185
203
|
child.on('error', error => finish(() => reject(new GitReadFailed(`spawn: ${error.message}`))));
|
|
186
|
-
child.on('close', code =>
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
204
|
+
child.on('close', code => {
|
|
205
|
+
void consuming.then(() => finish(() => {
|
|
206
|
+
if (stopped) { resolve({ code: code ?? -1, stdout: Buffer.alloc(0), stopped: true }); return; }
|
|
207
|
+
if (code === null || !okCodes.includes(code)) { reject(new GitReadFailed(`${args[0]} exit ${code}: ${err.trim().slice(0, 1000)}`)); return; }
|
|
208
|
+
resolve({ code, stdout: Buffer.concat(out), stopped: false });
|
|
209
|
+
}));
|
|
210
|
+
});
|
|
191
211
|
if (stdin !== undefined) {
|
|
192
212
|
child.stdin!.on('error', () => {});
|
|
193
213
|
child.stdin!.end(stdin);
|
|
@@ -586,11 +606,55 @@ export type GitReaderOptions = {
|
|
|
586
606
|
log?: (message: string, fields: Record<string, unknown>) => void;
|
|
587
607
|
/** Tests only: called after a window's contents were scanned and before Git writes their patch, where a racing writer could change a file. */
|
|
588
608
|
probe?: (step: 'scanned') => Promise<void> | void;
|
|
609
|
+
/** Tests only: how an untracked path's ancestor directories are inspected (default async lstat), to inject its failures. */
|
|
610
|
+
inspect?: (file: string) => Stats | Promise<Stats>;
|
|
589
611
|
};
|
|
590
612
|
|
|
591
613
|
type DiffParams = { workspaceId: string; compare: GitCompare; path?: string; cursor?: string };
|
|
592
614
|
|
|
593
|
-
|
|
615
|
+
/** What one ancestor directory of an untracked path is, as lstat saw it once in a listing. */
|
|
616
|
+
type Ancestor = 'directory' | 'link' | 'uninspectable';
|
|
617
|
+
|
|
618
|
+
/**
|
|
619
|
+
* An untracked path Git listed, cut at its first linked directory: Git for Windows descends
|
|
620
|
+
* into an untracked junction (or directory symlink) as a folder and lists what lies behind it,
|
|
621
|
+
* even outside the work tree. As on POSIX, the link itself is the entry; nothing behind it is.
|
|
622
|
+
*
|
|
623
|
+
* Fails closed: an ancestor lstat cannot inspect, or that is neither a directory nor a link
|
|
624
|
+
* (changed since Git listed it), answers `undefined` and the caller omits the entry, as it
|
|
625
|
+
* omits everything else under that ancestor. Only a directory lstat saw is passed through.
|
|
626
|
+
*
|
|
627
|
+
* `ancestors` caches each ancestor's answer for one listing, so a directory replaced by a
|
|
628
|
+
* junction during that listing keeps its earlier answer. That race is inherent (Git itself
|
|
629
|
+
* may have listed before or after the swap), it only concerns the names Git listed in that
|
|
630
|
+
* window, and every content read still goes through readConfinedFile, which refuses a path
|
|
631
|
+
* whose directories are linked at read time.
|
|
632
|
+
*/
|
|
633
|
+
async function untrackedEntry(root: string, name: string, ancestors: Map<string, Ancestor>, inspect: (file: string) => Stats | Promise<Stats>, deadline: number): Promise<string | undefined> {
|
|
634
|
+
const segments = name.split('/');
|
|
635
|
+
for (let count = 1; count < segments.length; count++) {
|
|
636
|
+
if (Date.now() >= deadline) throw new GitReadFailed('timed out');
|
|
637
|
+
const prefix = segments.slice(0, count).join('/');
|
|
638
|
+
let ancestor = ancestors.get(prefix);
|
|
639
|
+
if (ancestor === undefined) {
|
|
640
|
+
// Keep earlier answers stable; do not evict and re-inspect a swapped link.
|
|
641
|
+
if (ancestors.size >= GIT_READ.maxAncestors) throw new GitReadFailed('ancestor cache over its bound');
|
|
642
|
+
try {
|
|
643
|
+
const info = await inspect(path.join(root, ...segments.slice(0, count)));
|
|
644
|
+
ancestor = info.isSymbolicLink() ? 'link' : info.isDirectory() ? 'directory' : 'uninspectable';
|
|
645
|
+
} catch {
|
|
646
|
+
ancestor = 'uninspectable';
|
|
647
|
+
}
|
|
648
|
+
if (Date.now() >= deadline) throw new GitReadFailed('timed out');
|
|
649
|
+
ancestors.set(prefix, ancestor);
|
|
650
|
+
}
|
|
651
|
+
if (ancestor === 'link') return prefix;
|
|
652
|
+
if (ancestor === 'uninspectable') return undefined;
|
|
653
|
+
}
|
|
654
|
+
return name;
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
export function createGitReader({ registry, binary = 'git', timeoutMs = GIT_READ.timeoutMs, log = () => {}, probe, inspect = lstat }: GitReaderOptions) {
|
|
594
658
|
const key = randomBytes(32);
|
|
595
659
|
const mac = (scope: string) => createHmac('sha256', key).update(scope).digest('base64url').slice(0, 32);
|
|
596
660
|
|
|
@@ -670,6 +734,8 @@ export function createGitReader({ registry, binary = 'git', timeoutMs = GIT_READ
|
|
|
670
734
|
const digest = createHash('sha256');
|
|
671
735
|
let pending = '';
|
|
672
736
|
let pendingRename: string[] | undefined;
|
|
737
|
+
const ancestors = new Map<string, Ancestor>();
|
|
738
|
+
const collapsed = new Set<string>();
|
|
673
739
|
const take = (row: GitStatusFile) => {
|
|
674
740
|
dirty = true;
|
|
675
741
|
if (blockedFilePath(row.path) || (row.oldPath && blockedFilePath(row.oldPath)) || [...row.path].length > 4096 || [...(row.oldPath ?? '')].length > 4096) { hiddenCount++; return false; }
|
|
@@ -677,7 +743,7 @@ export function createGitReader({ registry, binary = 'git', timeoutMs = GIT_READ
|
|
|
677
743
|
files.push(row);
|
|
678
744
|
return false;
|
|
679
745
|
};
|
|
680
|
-
const record = (field: string): boolean => {
|
|
746
|
+
const record = async (field: string): Promise<boolean> => {
|
|
681
747
|
if (pendingRename) {
|
|
682
748
|
const [kind, xy] = pendingRename;
|
|
683
749
|
pendingRename = undefined;
|
|
@@ -705,19 +771,27 @@ export function createGitReader({ registry, binary = 'git', timeoutMs = GIT_READ
|
|
|
705
771
|
const change: GitChange = x === 'A' ? 'added' : x === 'D' || y === 'D' ? 'deleted' : x === 'T' || y === 'T' ? 'typechange' : 'modified';
|
|
706
772
|
return take({ path: rowPath, change, staged: x !== '.', unstaged: y !== '.' });
|
|
707
773
|
}
|
|
708
|
-
if (kind === '?') {
|
|
774
|
+
if (kind === '?') {
|
|
775
|
+
const listed = field.slice(2).replace(/\/$/, '');
|
|
776
|
+
if (!listed) return false;
|
|
777
|
+
const rowPath = await untrackedEntry(repo.root, listed, ancestors, inspect, repo.deadline);
|
|
778
|
+
// Behind an ancestor that cannot be inspected: something is untracked, nothing is listed.
|
|
779
|
+
if (rowPath === undefined) { dirty = true; return false; }
|
|
780
|
+
if (rowPath !== listed) { if (collapsed.has(rowPath)) return false; collapsed.add(rowPath); }
|
|
781
|
+
return take({ path: rowPath, change: 'untracked', staged: false, unstaged: true });
|
|
782
|
+
}
|
|
709
783
|
if (kind === '!') return false;
|
|
710
784
|
failed('unexpected status');
|
|
711
785
|
return false;
|
|
712
786
|
};
|
|
713
787
|
let seen = 0;
|
|
714
788
|
const { stopped } = await runGit(repo, ['status', '--porcelain=v2', '-z', '--branch', '--ahead-behind', '--untracked-files=all', '--ignored=no', '--ignore-submodules=dirty', '--find-renames'], {
|
|
715
|
-
onData: chunk => {
|
|
789
|
+
onData: async chunk => {
|
|
716
790
|
seen += chunk.length;
|
|
717
791
|
digest.update(chunk);
|
|
718
792
|
const fields = (pending + chunk.toString('utf8')).split('\0');
|
|
719
793
|
pending = fields.pop()!;
|
|
720
|
-
for (const field of fields) if (field && record(field)) return true;
|
|
794
|
+
for (const field of fields) if (field && await record(field)) return true;
|
|
721
795
|
if (seen > GIT_READ.maxListBytes) { truncated = true; return true; }
|
|
722
796
|
return false;
|
|
723
797
|
},
|
|
@@ -826,13 +900,19 @@ export function createGitReader({ registry, binary = 'git', timeoutMs = GIT_READ
|
|
|
826
900
|
let hidden = 0;
|
|
827
901
|
let more = false;
|
|
828
902
|
let pending = '';
|
|
903
|
+
const ancestors = new Map<string, Ancestor>();
|
|
904
|
+
const collapsed = new Set<string>();
|
|
829
905
|
const { stopped } = await runGit(repo, ['ls-files', '-z', '--others', '--exclude-standard', '--', ...(relative ? [relative] : [])], {
|
|
830
|
-
onData: chunk => {
|
|
906
|
+
onData: async chunk => {
|
|
831
907
|
const fields = (pending + chunk.toString('utf8')).split('\0');
|
|
832
908
|
pending = fields.pop()!;
|
|
833
|
-
for (const
|
|
909
|
+
for (const listed of fields) {
|
|
834
910
|
// A nested repository is listed as its directory: not a file.
|
|
835
|
-
if (!
|
|
911
|
+
if (!listed || listed.endsWith('/')) continue;
|
|
912
|
+
const name = await untrackedEntry(repo.root, listed, ancestors, inspect, repo.deadline);
|
|
913
|
+
// Behind an ancestor that cannot be inspected: omitted, also when asked for by path.
|
|
914
|
+
if (name === undefined) continue;
|
|
915
|
+
if (name !== listed) { if (collapsed.has(name)) continue; collapsed.add(name); }
|
|
836
916
|
if (relative && name !== relative) continue;
|
|
837
917
|
if (blockedFilePath(name) || [...name].length > 4096) { hidden++; continue; }
|
|
838
918
|
if (found.length >= limit) { more = true; return true; }
|
package/src/main.ts
CHANGED
|
@@ -5,7 +5,7 @@ import path from 'node:path';
|
|
|
5
5
|
import { fileURLToPath } from 'node:url';
|
|
6
6
|
import templates from 'alp:templates';
|
|
7
7
|
import { alpHome, daemonPaths, assertPrivateDirectory, PROTOCOL_VERSION, withDaemonLifecycle } from '@anhlp/alp-client/index';
|
|
8
|
-
import { attachmentStageDir, createAlpRuntime,
|
|
8
|
+
import { attachmentStageDir, createAlpRuntime, reclaimCopies, reclaimWorktrees, setActiveCatalog } from '@anhlp/alp-runtime/index';
|
|
9
9
|
import type { LeftWorktree } from '@anhlp/alp-runtime/index';
|
|
10
10
|
import { createDaemonServer } from './server.js';
|
|
11
11
|
import { acquireLock, heartbeat, releaseLock, updateLock } from './lock.js';
|
|
@@ -16,6 +16,7 @@ import { validateUserSettings } from '@anhlp/alp-core/validation';
|
|
|
16
16
|
import { clearRestoredMarker, readRestoredMarker, recoverInterruptedRestore } from '@anhlp/alp-core/backup';
|
|
17
17
|
import { migrateState } from '@anhlp/alp-core/migrations';
|
|
18
18
|
import { createLogger } from '@anhlp/alp-core/log';
|
|
19
|
+
import { createModelCatalog } from './model-catalog.js';
|
|
19
20
|
import { selfCommand, selfEntry } from '@anhlp/alp-core/packaged';
|
|
20
21
|
|
|
21
22
|
declare const __ALP_VERSION__: string;
|
|
@@ -44,6 +45,16 @@ async function rotate(log: string) {
|
|
|
44
45
|
}
|
|
45
46
|
}
|
|
46
47
|
|
|
48
|
+
/** What a detached alpd that failed to start tells its launcher over IPC. */
|
|
49
|
+
type StartupFailure = { code: string; message: string };
|
|
50
|
+
|
|
51
|
+
/** alpd failed to start and said why; the launcher reports it with alpd's own code. */
|
|
52
|
+
class ChildStartupError extends Error {
|
|
53
|
+
constructor(readonly failure: StartupFailure, exit: number | string | null) {
|
|
54
|
+
super(`alpd exited during startup (${exit}): ${failure.message}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
47
58
|
/** The launcher owns its detached child until that child acknowledges readiness or exits.
|
|
48
59
|
* A ready lock belonging to some other process cannot settle this launch.
|
|
49
60
|
*/
|
|
@@ -72,12 +83,17 @@ async function detach(home: string) {
|
|
|
72
83
|
force = setTimeout(() => child.kill('SIGKILL'), 1_000);
|
|
73
84
|
}, timeoutMs);
|
|
74
85
|
child.once('error', error => { clearTimeout(timer); clearTimeout(force); reject(error); });
|
|
86
|
+
// Why alpd failed, from alpd itself: on a full disk its log line may never reach the log.
|
|
87
|
+
let failed: StartupFailure | undefined;
|
|
75
88
|
child.once('exit', (code, signal) => {
|
|
76
89
|
clearTimeout(timer);
|
|
77
90
|
clearTimeout(force);
|
|
78
|
-
reject(new Error(
|
|
91
|
+
reject(timedOut ? new Error(`alpd startup timed out after ${timeoutMs} ms`)
|
|
92
|
+
: failed ? new ChildStartupError(failed, code ?? signal)
|
|
93
|
+
: new Error(`alpd exited during startup (${code ?? signal})`));
|
|
79
94
|
});
|
|
80
|
-
child.
|
|
95
|
+
child.on('message', (message: any) => {
|
|
96
|
+
if (message?.type === 'alpd.failed' && typeof message.code === 'string' && typeof message.message === 'string') { failed = { code: message.code, message: message.message }; return; }
|
|
81
97
|
if (message !== 'alpd.ready' || timedOut) return;
|
|
82
98
|
clearTimeout(timer);
|
|
83
99
|
child.disconnect();
|
|
@@ -236,6 +252,8 @@ async function run(home: string) {
|
|
|
236
252
|
...(process.platform !== 'win32' ? { attachmentStageDir: attachmentStageDir(home) } : {}),
|
|
237
253
|
assignmentResultFile: path.join(home, 'state', 'assignment-results.json'), recallFile: path.join(home, 'state', 'recall.json'), pauseFile: path.join(home, 'state', 'pause.json'), liveFile: path.join(home, 'state', 'live.json'),
|
|
238
254
|
autoResume: settings?.limits?.autoResume !== false, recoveryResume: settings?.recovery?.autoResume !== false && !opened.restored, previousExit: exit,
|
|
255
|
+
// Pause, live, recall and result writes the disk refused hold work at once, not at the next check (t-e80a).
|
|
256
|
+
onWriteError: error => diskReport(error),
|
|
239
257
|
});
|
|
240
258
|
// A full disk pauses ALP's work instead of stopping alpd (t-2831).
|
|
241
259
|
const disk = createDiskGuard({ home, runtime, log, ...(Number(process.env.ALP_DISK_CHECK_MS) > 0 ? { intervalMs: Number(process.env.ALP_DISK_CHECK_MS) } : {}) });
|
|
@@ -248,10 +266,15 @@ async function run(home: string) {
|
|
|
248
266
|
force.unref();
|
|
249
267
|
clearInterval(beat);
|
|
250
268
|
disk.stop();
|
|
251
|
-
|
|
269
|
+
// Stops the timer and listeners now; its promise settles once the cache writes under way are done.
|
|
270
|
+
const catalogStopped = modelCatalog?.stop().catch(() => {});
|
|
252
271
|
await server.close().catch(() => {});
|
|
253
272
|
await runtime.shutdown().catch(() => {});
|
|
254
273
|
await store.flush();
|
|
274
|
+
// Before the lock goes, so the next alpd never writes the cache beside this one (t-5294.2). Bounded:
|
|
275
|
+
// stop() also waits for a provider probe under way. Past 2 s alpd stops anyway; the write is a
|
|
276
|
+
// temporary file and a rename, so the cache keeps its previous content and a stray .tmp may remain.
|
|
277
|
+
if (catalogStopped) await Promise.race([catalogStopped, new Promise(resolve => setTimeout(resolve, 2_000).unref())]);
|
|
255
278
|
// Last: what remains is a clean stop.
|
|
256
279
|
await unlink(marker).catch(() => {});
|
|
257
280
|
await releaseLock(home);
|
|
@@ -265,7 +288,9 @@ async function run(home: string) {
|
|
|
265
288
|
for (const problem of attachmentConfig.problems) log.warn(problem, { code: 'ALP_ATTACHMENT_CONFIG' });
|
|
266
289
|
// The live model catalog (t-e5ee): static rows at once, provider lists in the background, every 6 h.
|
|
267
290
|
// ALP_MODEL_CATALOG=static keeps the static rows only (tests, offline hosts).
|
|
268
|
-
|
|
291
|
+
// ACP providers (t-d0b0) are listed from providers/<id>.json without starting any; one is probed only on
|
|
292
|
+
// catalog.refresh or when the user enables it, and learns its models from its first session otherwise.
|
|
293
|
+
const modelCatalog = process.env.ALP_MODEL_CATALOG === 'static' ? undefined : createModelCatalog(home);
|
|
269
294
|
setActiveCatalog(modelCatalog);
|
|
270
295
|
void modelCatalog?.start().catch(() => {});
|
|
271
296
|
const server = createDaemonServer({ runtime, socketPath: socket, version: VERSION, store, runLogDir, previousExit: exit, home, templates, attachmentOptions: { limits: attachmentConfig.limits }, modelCatalog, onShutdown: () => void shutdown() });
|
|
@@ -300,6 +325,14 @@ async function run(home: string) {
|
|
|
300
325
|
log.info(`alpd ${VERSION} ready on ${socket} (pid ${process.pid})`, { event: 'ready', version: VERSION, pid: process.pid });
|
|
301
326
|
}
|
|
302
327
|
|
|
328
|
+
/** Why a start-up write failed on a disk that takes none, and what helps. */
|
|
329
|
+
function diskStartupReason(error: any, home: string) {
|
|
330
|
+
let current = error;
|
|
331
|
+
for (let depth = 0; current && depth < 4 && !['ENOSPC', 'EDQUOT', 'EROFS'].includes(current.code); depth++) current = current.cause;
|
|
332
|
+
const what = current?.code === 'EROFS' ? 'the disk is read-only' : current?.code === 'EDQUOT' ? 'the disk quota is used up' : 'the disk is full';
|
|
333
|
+
return `${what} (${current?.code}${current?.path ? ` writing ${current.path}` : ''}); free space on the disk that holds ${home}, then start alpd again`;
|
|
334
|
+
}
|
|
335
|
+
|
|
303
336
|
const home = alpHome();
|
|
304
337
|
/** Where a failed write to alpd's own output goes once the disk guard exists. */
|
|
305
338
|
let diskReport = (_error: unknown) => {};
|
|
@@ -322,11 +355,27 @@ try {
|
|
|
322
355
|
}
|
|
323
356
|
}
|
|
324
357
|
} catch (error: any) {
|
|
358
|
+
// The launcher of a detached alpd that failed: alpd's own reason and code, which its log may lack.
|
|
359
|
+
if (error instanceof ChildStartupError) {
|
|
360
|
+
log.error(error.message, { code: error.failure.code, phase: 'home' });
|
|
361
|
+
process.exit(1);
|
|
362
|
+
}
|
|
325
363
|
// The log says where start-up stopped; alp doctor reads it from there.
|
|
326
364
|
const stoppedIn = phase as Phase;
|
|
365
|
+
// A full disk is named as such (t-e80a): alpd cannot take its lock or write its marker, and
|
|
366
|
+
// nothing but freeing space helps. The lock stays exclusive; alpd only says why it stopped.
|
|
367
|
+
const disk = isDiskError(error);
|
|
327
368
|
// Opening the state fails with the state's own code (a newer schema, a failed migration).
|
|
328
|
-
const code =
|
|
369
|
+
const code = disk ? 'ALP_DISK_FULL'
|
|
370
|
+
: /already running|ALP is busy/.test(error?.message ?? '') ? 'ALP_LOCK_HELD'
|
|
329
371
|
: stoppedIn === 'state' && typeof error?.code === 'string' && /^[A-Z_]+$/.test(error.code) ? `ALP_${error.code}` : STARTUP_CODES[stoppedIn];
|
|
330
|
-
|
|
372
|
+
const step = { home: 'preparing its home', lock: 'taking its lock', state: 'opening its state', recovery: 'recovering earlier work', listen: 'opening its IPC endpoint', ready: 'finishing start-up' }[stoppedIn];
|
|
373
|
+
const message = `alpd failed to start while ${step}${disk ? `: ${diskStartupReason(error, home)}` : stoppedIn === 'state' ? `: ${error?.message ?? error}` : ''}`;
|
|
374
|
+
log.error(message, { code, phase: stoppedIn, error });
|
|
375
|
+
// A detached alpd tells its launcher too, which passes it to the client that started it.
|
|
376
|
+
if (process.connected) await new Promise<void>(resolve => {
|
|
377
|
+
const timer = setTimeout(resolve, 1_000);
|
|
378
|
+
try { process.send!({ type: 'alpd.failed', code, message }, () => { clearTimeout(timer); resolve(); }); } catch { clearTimeout(timer); resolve(); }
|
|
379
|
+
});
|
|
331
380
|
process.exit(1);
|
|
332
381
|
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/** Production catalog wiring, shared with daemon integration tests. No model prompts. */
|
|
2
|
+
import { mkdtemp, rm } from 'node:fs/promises';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { loadProvider, listProviders } from '@anhlp/alp-core/providers';
|
|
6
|
+
import { probeAcp } from '@anhlp/alp-client/acp-probe';
|
|
7
|
+
import { antigravityProbe, liveProbes, ModelCatalog } from '@anhlp/alp-runtime/index';
|
|
8
|
+
|
|
9
|
+
export function createModelCatalog(home: string, warn: (message: string) => void = console.error) {
|
|
10
|
+
let warnings = new Set<string>();
|
|
11
|
+
const probes = liveProbes();
|
|
12
|
+
probes.antigravity = async signal => {
|
|
13
|
+
const provider = await loadProvider(home, 'antigravity');
|
|
14
|
+
if (provider?.enabled === false) return [];
|
|
15
|
+
return antigravityProbe(provider?.command, { ...process.env, ...provider?.env }, provider?.args)(signal);
|
|
16
|
+
};
|
|
17
|
+
return new ModelCatalog({ home, probes,
|
|
18
|
+
acpProviders: async () => {
|
|
19
|
+
const errors: string[] = [];
|
|
20
|
+
const current = new Set<string>();
|
|
21
|
+
const providers = await listProviders(home, { onError: (error: unknown, name: string) => {
|
|
22
|
+
const message = `skipping provider ${name}: ${error instanceof Error ? error.message : String(error)}`;
|
|
23
|
+
errors.push(message); current.add(message);
|
|
24
|
+
if (!warnings.has(message)) warn(`alpd: ${message}`);
|
|
25
|
+
} });
|
|
26
|
+
warnings = current;
|
|
27
|
+
return { providers, ...(errors.length ? { error: errors.join('\n') } : {}) };
|
|
28
|
+
},
|
|
29
|
+
acpProbe: async (provider, signal) => {
|
|
30
|
+
signal.throwIfAborted();
|
|
31
|
+
const cwd = await mkdtemp(path.join(os.tmpdir(), 'alp-model-probe-'));
|
|
32
|
+
try { return (await probeAcp(provider as Parameters<typeof probeAcp>[0], { cwd, signal, discover: true, timeoutMs: 15_000 })).models ?? []; }
|
|
33
|
+
finally { await rm(cwd, { recursive: true, force: true }); }
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
}
|
package/src/operations.ts
CHANGED
|
@@ -29,6 +29,10 @@ export type OperationsOptions = {
|
|
|
29
29
|
knownProjects?: () => Iterable<string>;
|
|
30
30
|
/** A gate changed through task.gate.resolve: the runtime syncs the project's gate cards and mail. */
|
|
31
31
|
onGateChange?: (projectRoot: string) => Promise<void> | void;
|
|
32
|
+
/** A provider file was written or removed; `enabled` when this save turned it from off to on (t-d0b0). */
|
|
33
|
+
onProviderSaved?: (id: string, enabled: boolean) => void;
|
|
34
|
+
/** The models alpd's catalog learned or probed for an ACP provider (t-d0b0). */
|
|
35
|
+
learnedModels?: (id: string) => Array<{ id: string; label?: string }>;
|
|
32
36
|
};
|
|
33
37
|
|
|
34
38
|
/** `caller.agent`: the connection said in daemon.hello that an ALP agent started it (t-ca1f). */
|
|
@@ -124,7 +128,7 @@ function unredact(kind: string, content: Record<string, unknown>, previous: Reco
|
|
|
124
128
|
return next;
|
|
125
129
|
}
|
|
126
130
|
|
|
127
|
-
export function createOperations({ home, templates, knownProjects = () => [], onGateChange }: OperationsOptions = {}) {
|
|
131
|
+
export function createOperations({ home, templates, knownProjects = () => [], onGateChange, onProviderSaved, learnedModels }: OperationsOptions = {}) {
|
|
128
132
|
/** Revisions on the wire are keyed per alpd process, so they never carry a hash of secret content. */
|
|
129
133
|
const revisionKey = randomBytes(32);
|
|
130
134
|
const toWire = (revision: string | null | undefined) => revision == null ? null : `r${createHmac('sha256', revisionKey).update(revision).digest('base64url').slice(0, 22)}`;
|
|
@@ -256,6 +260,8 @@ export function createOperations({ home, templates, knownProjects = () => [], on
|
|
|
256
260
|
const next = unredact(kind, content, previous);
|
|
257
261
|
// Core checks the revision again as it writes, against writers outside alpd.
|
|
258
262
|
const saved = await saveEntry(kind, name, next, { ...options, scope, policy, ...(revision !== undefined ? { revision: current?.revision ?? null } : {}) });
|
|
263
|
+
// Enabled is the change from off to on, never a save of a provider that was on already (t-d0b0).
|
|
264
|
+
if (kind === 'providers') { try { onProviderSaved?.(name, (previous as any)?.provider?.enabled === false && (next as any)?.provider?.enabled !== false); } catch { /* the catalog is best effort */ } }
|
|
259
265
|
if (kind !== 'teams' || sync !== 'known-projects') return { revision: toWire(saved.revision) };
|
|
260
266
|
const roots = new Set<string>(options.root ? [options.root] : []);
|
|
261
267
|
if (scope === 'library') for (const root of knownProjects()) if (typeof root === 'string' && path.isAbsolute(root)) roots.add(path.resolve(root));
|
|
@@ -273,19 +279,25 @@ export function createOperations({ home, templates, knownProjects = () => [], on
|
|
|
273
279
|
const current = await layer(kind, name, options, scope);
|
|
274
280
|
if (!current) throw failure(ERROR.notFound, 'not_found', `No ${kind} entry '${name}' in the ${scope}`);
|
|
275
281
|
if (revision !== undefined && revision !== toWire(current.revision)) throw failure(ERROR.conflict, 'revision_conflict', `${kind} '${name}' changed in the ${scope} since you opened it`);
|
|
276
|
-
|
|
282
|
+
const deleted = await deleteEntry(kind, name, { ...options, scope, policy, ...(revision !== undefined ? { revision: current.revision } : {}) });
|
|
283
|
+
if (kind === 'providers') { try { onProviderSaved?.(name, false); } catch { /* best effort */ } }
|
|
284
|
+
return deleted;
|
|
277
285
|
})),
|
|
278
286
|
|
|
279
287
|
'library.duplicate': guard(({ kind, from, to, scope, projectRoot, policy }) => serial(async () => {
|
|
280
288
|
const options = await where(projectRoot, scope);
|
|
281
289
|
try { await getEntry(kind, from, options); }
|
|
282
290
|
catch (error) { throw fromCore(error, { notFound: 'not_found' }); }
|
|
283
|
-
|
|
291
|
+
const result = { revision: toWire((await duplicateEntry(kind, from, to, { ...options, scope, policy })).revision) };
|
|
292
|
+
if (kind === 'providers') { try { onProviderSaved?.(to, false); } catch { /* best effort */ } }
|
|
293
|
+
return result;
|
|
284
294
|
}), { notFound: 'invalid_entry' }),
|
|
285
295
|
|
|
286
296
|
'library.rename': guard(({ kind, from, to, scope, projectRoot, policy }) => serial(async () => {
|
|
287
297
|
const options = await where(projectRoot, scope);
|
|
288
|
-
|
|
298
|
+
const renamed = await renameEntry(kind, from, to, { ...options, scope, policy });
|
|
299
|
+
if (kind === 'providers') { try { onProviderSaved?.(to, false); } catch { /* best effort */ } }
|
|
300
|
+
return { revision: toWire(renamed.revision) };
|
|
289
301
|
})),
|
|
290
302
|
|
|
291
303
|
'library.skills.set': guard(({ agent, skills }) => serial(() => setGivenSkills(agent, skills, { library: home })), { notFound: 'invalid_entry' }),
|
|
@@ -295,7 +307,8 @@ export function createOperations({ home, templates, knownProjects = () => [], on
|
|
|
295
307
|
for (const provider of await listProviders(home)) {
|
|
296
308
|
if (provider.enabled === false || provider.kind === 'antigravity') continue;
|
|
297
309
|
models.push({ id: `acp:${provider.id}`, label: `${provider.label} · Provider default`, efforts: [] });
|
|
298
|
-
|
|
310
|
+
// The file's models replace what a session or probe taught alpd's catalog (t-d0b0); additionalModels add.
|
|
311
|
+
for (const model of [...(provider.models ?? learnedModels?.(provider.id) ?? []), ...(provider.additionalModels ?? [])]) models.push({ id: `acp:${provider.id}/${model.id}`, label: `${provider.label} · ${model.label ?? model.id}`, efforts: [] });
|
|
299
312
|
}
|
|
300
313
|
return { models: [...new Map(models.map(model => [model.id, model])).values()] };
|
|
301
314
|
}),
|
package/src/scheduler.ts
CHANGED
|
@@ -251,6 +251,12 @@ export function createScheduler({ store, runtime, deliver, ready, session, settl
|
|
|
251
251
|
opening.delete(current.id);
|
|
252
252
|
if (snapshot.parentId) return fail(current, 'The session this job continues is not a root session', 'not_a_root');
|
|
253
253
|
if (projectKey(snapshot.projectRoot) !== projectKey(current.projectRoot)) return fail(current, 'The session this job continues belongs to another project', 'other_project');
|
|
254
|
+
// t-5ac5: the delivered text starts with ALP's own preamble, so the runtime's check of the
|
|
255
|
+
// whole message never sees a native command the job's prompt opens with (Claude's /clear,
|
|
256
|
+
// /new, /reset). The job's own words are checked: such a job fails with the reason instead
|
|
257
|
+
// of being recorded as delivered while the command it asked for never runs.
|
|
258
|
+
const refused = runtime.refusesPrompt?.(current.sessionId, [{ type: 'text', text: current.prompt }]);
|
|
259
|
+
if (refused) return fail(current, refused, 'native_command_unsupported');
|
|
254
260
|
const waitBusy = async (): Promise<'waiting' | 'failed'> => {
|
|
255
261
|
// The session is working. The wait is bounded from the moment it began: how
|
|
256
262
|
// late the job is says nothing about how long a running turn takes to end.
|
package/src/server.ts
CHANGED
|
@@ -9,7 +9,7 @@ import os from 'node:os';
|
|
|
9
9
|
import { assertPrivateDirectory, assertPrivateSocket } from '@anhlp/alp-client/index';
|
|
10
10
|
import path from 'node:path';
|
|
11
11
|
import { createLogger } from '@anhlp/alp-core/log';
|
|
12
|
-
import { AlpRpcError, CAPABILITY, MAX_FRAME, PROMPT_IMAGES, PROTOCOL_VERSION, ERROR, RPC_ERROR, RPC_METHODS, base64Bytes, encodeFrame } from '@anhlp/alp-protocol';
|
|
12
|
+
import { AGENT_NAME, AlpRpcError, CAPABILITY, MAX_FRAME, PROMPT_IMAGES, PROTOCOL_VERSION, ERROR, RPC_ERROR, RPC_METHODS, base64Bytes, encodeFrame } from '@anhlp/alp-protocol';
|
|
13
13
|
import { validateParams } from '@anhlp/alp-protocol/contract';
|
|
14
14
|
export { ERROR } from '@anhlp/alp-protocol';
|
|
15
15
|
import { DEFAULT_MODEL, models, modes, type ModelCatalog, thinkingOptions, type AlpEvent, type AlpRuntime, type Envelope, type SessionSnapshot } from '@anhlp/alp-runtime/index';
|
|
@@ -119,6 +119,8 @@ type Connection = {
|
|
|
119
119
|
helloed: boolean;
|
|
120
120
|
/** It said in daemon.hello that an ALP agent started it (t-ca1f): refused what only the user may do. */
|
|
121
121
|
agent?: boolean;
|
|
122
|
+
/** The agent's name an agent connection gave in daemon.hello (`principal.agent`, t-1c59): who it is, for the logs; never grants anything. Sticky: the first name stays. */
|
|
123
|
+
agentName?: string;
|
|
122
124
|
/** The client name daemon.hello gave: a gate card answered from alp-paseo* is recorded as paseo, else web. */
|
|
123
125
|
client?: string;
|
|
124
126
|
roots: Set<string>;
|
|
@@ -240,8 +242,15 @@ export function createDaemonServer({ runtime, socketPath, version, onShutdown, s
|
|
|
240
242
|
const connections = new Set<Connection>();
|
|
241
243
|
const catalogResult = () => {
|
|
242
244
|
const state = modelCatalog?.state();
|
|
243
|
-
|
|
244
|
-
|
|
245
|
+
// ACP providers' rows (t-d0b0), from their files and what sessions or an explicit probe taught.
|
|
246
|
+
const acpRows = modelCatalog?.acpRows() ?? [];
|
|
247
|
+
const acpModels = acpRows.map(row => ({ id: `acp:${row.id}`, label: row.label, description: row.description, thinkingOptions: [], runtime: 'acp', provider: row.provider, source: row.source, enabled: row.enabled, status: row.status }));
|
|
248
|
+
const acpProviders = Object.fromEntries([...new Set(acpRows.map(row => row.provider))].map(id => {
|
|
249
|
+
const acp = state?.acp?.[id];
|
|
250
|
+
return [`acp:${id}`, { status: acp?.status ?? 'unprobed', ...(acp?.fetchedAt ? { fetchedAt: acp.fetchedAt } : {}), ...(acp?.error ? { error: acp.error } : {}) }];
|
|
251
|
+
}));
|
|
252
|
+
return { models: [...models, ...acpModels], modes, thinkingOptions, defaultModel: `codex:${DEFAULT_MODEL}`,
|
|
253
|
+
...(state ? { fetchedAt: state.fetchedAt, providers: { ...Object.fromEntries(Object.entries(state.providers).map(([runtime, provider]) => [runtime, { status: provider!.status, fetchedAt: provider!.fetchedAt, ...(provider!.error ? { error: provider!.error } : {}) }])), ...acpProviders, ...(modelCatalog?.acpLoadError() ? { acp: { status: 'failed', error: modelCatalog.acpLoadError() } } : {}) } } : {}) };
|
|
245
254
|
};
|
|
246
255
|
// Every helloed connection learns that the catalog changed, and re-reads it (catalog.updated).
|
|
247
256
|
const unsubscribeCatalog = modelCatalog?.onUpdated(snapshot => {
|
|
@@ -1117,12 +1126,17 @@ export function createDaemonServer({ runtime, socketPath, version, onShutdown, s
|
|
|
1117
1126
|
if (!gitReader) throw new RpcError(ERROR.unavailable, 'Registry needs ALP_HOME');
|
|
1118
1127
|
return run(gitReader);
|
|
1119
1128
|
};
|
|
1120
|
-
const operations = createOperations({ home, templates, knownProjects: () => new Set([...records.values()].flatMap(record => record.session?.projectRoot ? [record.session.projectRoot] : [])), onGateChange: root => runtime.syncGates?.(root)
|
|
1129
|
+
const operations = createOperations({ home, templates, knownProjects: () => new Set([...records.values()].flatMap(record => record.session?.projectRoot ? [record.session.projectRoot] : [])), onGateChange: root => runtime.syncGates?.(root),
|
|
1130
|
+
// A provider file saved: reread the ACP rows; one the user just enabled is probed for its models (t-d0b0).
|
|
1131
|
+
learnedModels: id => modelCatalog?.acpModels(id) ?? [],
|
|
1132
|
+
onProviderSaved: (id, enabled) => { if (!modelCatalog) return; void (enabled ? modelCatalog.refreshAcp([id]) : modelCatalog.loadAcp()).catch(() => {}); } });
|
|
1121
1133
|
const extensionOwners = new Map<string, Connection>();
|
|
1122
1134
|
/** workspace.files.find's snapshots, each for the connection that asked. */
|
|
1123
1135
|
const findPages = createFindPages();
|
|
1124
1136
|
const handlers: Record<string, (connection: Connection, params: any) => Promise<unknown> | unknown> = {
|
|
1125
1137
|
'extension.register': async (connection, params) => {
|
|
1138
|
+
// An agent could claim a user's method (web.pairing) while its owner is down (t-d08b).
|
|
1139
|
+
agentRefused(connection, 'extension.register');
|
|
1126
1140
|
const method = params?.method;
|
|
1127
1141
|
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');
|
|
1128
1142
|
if (method.length > 128 || (reservedNamespaces.has(method.split('.')[0]) && !originalMethods.has(method))) throw new RpcError(ERROR.protocol, 'extension.register requires an extension namespace and a name of at most 128 characters');
|
|
@@ -1347,7 +1361,9 @@ export function createDaemonServer({ runtime, socketPath, version, onShutdown, s
|
|
|
1347
1361
|
'catalog.get': () => catalogResult(),
|
|
1348
1362
|
'catalog.refresh': async () => {
|
|
1349
1363
|
if (!modelCatalog) throw new RpcError(ERROR.unavailable, 'The live model catalog is not available');
|
|
1350
|
-
|
|
1364
|
+
// The one place, with a provider's enabling, where ALP starts ACP providers to list their models.
|
|
1365
|
+
// An ACP failure must not hide the providers' refresh: each records its own error.
|
|
1366
|
+
await Promise.all([modelCatalog.refresh(), modelCatalog.refreshAcp().catch(() => {})]);
|
|
1351
1367
|
return catalogResult();
|
|
1352
1368
|
},
|
|
1353
1369
|
// attachment.* (§12.6): each call acts for its principal context; without the store (no home or
|
|
@@ -1533,6 +1549,9 @@ export function createDaemonServer({ runtime, socketPath, version, onShutdown, s
|
|
|
1533
1549
|
async 'session.prompt'(_connection, { sessionId, clientMessageId, delivery = 'auto', content, ...context }) {
|
|
1534
1550
|
if (typeof clientMessageId !== 'string' || !Array.isArray(content)) throw new RpcError(-32602, 'clientMessageId and content are required');
|
|
1535
1551
|
if (delivery !== 'auto' && delivery !== 'steer') throw new RpcError(-32602, 'delivery must be auto or steer');
|
|
1552
|
+
// t-e906: a native command ALP cannot follow (Claude's /clear) is refused here, never sent.
|
|
1553
|
+
const refused = runtime.refusesPrompt?.(sessionId, content);
|
|
1554
|
+
if (refused) throw new RpcError(-32602, refused, { reason: 'native_command_unsupported' });
|
|
1536
1555
|
// Images are inline only (the schema refuses a path or URL); here their number and decoded size.
|
|
1537
1556
|
const images = content.filter((part: { type?: string }) => part?.type === 'image');
|
|
1538
1557
|
if (images.length > PROMPT_IMAGES.maxCount) throw new RpcError(-32602, `A prompt takes at most ${PROMPT_IMAGES.maxCount} images`);
|
|
@@ -1575,29 +1594,38 @@ export function createDaemonServer({ runtime, socketPath, version, onShutdown, s
|
|
|
1575
1594
|
},
|
|
1576
1595
|
|
|
1577
1596
|
/** A new root from a root's conversation cut at a turn, or from a branch a rewind set aside (§12.7). */
|
|
1578
|
-
async 'session.fork'(
|
|
1597
|
+
async 'session.fork'(connection, params) {
|
|
1598
|
+
agentRefused(connection, 'session.fork');
|
|
1579
1599
|
if (!conversation?.enabled) throw new RpcError(RPC_ERROR.methodNotFound, 'This alpd does not fork conversations (capability session.fork is not listed)');
|
|
1580
1600
|
return conversation.fork({ ...params, principal: principalOf(params) });
|
|
1581
1601
|
},
|
|
1582
1602
|
|
|
1583
1603
|
/** The root's conversation from before a turn on, same id; the later turns stay a recoverable branch, files untouched (§12.7). */
|
|
1584
|
-
async 'session.rewind'(
|
|
1604
|
+
async 'session.rewind'(connection, params) {
|
|
1605
|
+
agentRefused(connection, 'session.rewind');
|
|
1585
1606
|
if (!conversation?.enabled) throw new RpcError(RPC_ERROR.methodNotFound, 'This alpd does not rewind conversations (capability session.rewind is not listed)');
|
|
1586
1607
|
return conversation.rewind(params);
|
|
1587
1608
|
},
|
|
1588
1609
|
|
|
1589
1610
|
/** Restarts a live root's native process on its own conversation at the idle barrier (§12.8). */
|
|
1590
|
-
async 'session.reload'(
|
|
1611
|
+
async 'session.reload'(connection, params) {
|
|
1612
|
+
agentRefused(connection, 'session.reload');
|
|
1591
1613
|
if (!conversation?.reloads) throw new RpcError(RPC_ERROR.methodNotFound, 'This alpd does not reload sessions (capability session.reload is not listed)');
|
|
1592
1614
|
return conversation.reload(params);
|
|
1593
1615
|
},
|
|
1594
1616
|
|
|
1595
1617
|
/** Compacts a live root's context now or at its next turn boundary (t-0e56). */
|
|
1596
|
-
async 'session.compact'(
|
|
1618
|
+
async 'session.compact'(connection, { sessionId }) {
|
|
1619
|
+
// Compaction rewrites the user's conversation context; an agent's connection may not (t-de1a review).
|
|
1620
|
+
agentRefused(connection, 'session.compact');
|
|
1597
1621
|
if (typeof sessionId !== 'string' || !sessionId) throw new RpcError(-32602, 'sessionId is required');
|
|
1598
1622
|
if (!runtime.compact) throw new RpcError(RPC_ERROR.methodNotFound, 'This alpd does not compact sessions');
|
|
1599
1623
|
try { return runtime.compact(sessionId); }
|
|
1600
|
-
catch (error) {
|
|
1624
|
+
catch (error) {
|
|
1625
|
+
// A refusal keeps its reason (session_busy, runtime_unsupported), so a client can word it (t-de1a).
|
|
1626
|
+
const reason = (error as { data?: { reason?: unknown } })?.data?.reason;
|
|
1627
|
+
throw new RpcError(ERROR.unavailable, error instanceof Error ? error.message : String(error), typeof reason === 'string' ? { reason } : undefined);
|
|
1628
|
+
}
|
|
1601
1629
|
},
|
|
1602
1630
|
|
|
1603
1631
|
/** The slash commands and skills a live session's composer offers after "/" (t-f628). */
|
|
@@ -1853,7 +1881,7 @@ export function createDaemonServer({ runtime, socketPath, version, onShutdown, s
|
|
|
1853
1881
|
async function invokeExtended(owner: Connection, caller: Connection, method: string, params: any) {
|
|
1854
1882
|
if (owner.pending.size >= 128) throw new RpcError(ERROR.conflict, 'too many pending extension calls');
|
|
1855
1883
|
const id = ++owner.nextId;
|
|
1856
|
-
const invocation = { jsonrpc: '2.0', id, method: 'extension.invoke', params: { method, params } };
|
|
1884
|
+
const invocation = { jsonrpc: '2.0', id, method: 'extension.invoke', params: { method, params, caller: { agent: caller.agent === true, ...(caller.agentName !== undefined ? { agentName: caller.agentName } : {}) } } };
|
|
1857
1885
|
if (Buffer.byteLength(encodeFrame(invocation), 'utf8') > MAX_FRAME) throw new RpcError(-32600, 'ALP daemon frame exceeds size limit');
|
|
1858
1886
|
return new Promise((resolve, reject) => {
|
|
1859
1887
|
const timer = setTimeout(() => {
|
|
@@ -1952,10 +1980,22 @@ export function createDaemonServer({ runtime, socketPath, version, onShutdown, s
|
|
|
1952
1980
|
if (params?.protocolVersion !== PROTOCOL_VERSION) throw new RpcError(ERROR.protocol, `ALP daemon speaks protocol ${PROTOCOL_VERSION}; restart it after upgrading (alp daemon restart)`, { reason: 'protocol_version', supported: [PROTOCOL_VERSION] });
|
|
1953
1981
|
const issues = validateParams(method, params)!;
|
|
1954
1982
|
if (issues.length) throw invalidParams(issues);
|
|
1983
|
+
// Checked before the connection changes, so a refused hello changes nothing. The gateway checks a credential's agent with the same AGENT_NAME.
|
|
1984
|
+
const named = params?.principal?.agent;
|
|
1985
|
+
if (named !== undefined && (typeof named !== 'string' || !AGENT_NAME.test(named))) throw new RpcError(RPC_ERROR.invalidParams, 'principal.agent must be an agent name', { reason: 'invalid_params' });
|
|
1955
1986
|
connection.helloed = true;
|
|
1956
1987
|
// Sticky: a later hello never sheds it.
|
|
1957
1988
|
connection.agent = connection.agent === true || (Array.isArray(params?.capabilities) && params.capabilities.includes(AGENT_CAPABILITY));
|
|
1958
1989
|
if (typeof params?.client?.name === 'string') connection.client = params.client.name;
|
|
1990
|
+
// A named principal (t-1c59: the gateway sends a named agent credential's name) is an agent's
|
|
1991
|
+
// too: naming one only ever takes away. The name is a label for the logs, not a credential.
|
|
1992
|
+
if (named !== undefined) {
|
|
1993
|
+
connection.agent = true;
|
|
1994
|
+
if (connection.agentName === undefined) {
|
|
1995
|
+
connection.agentName = named;
|
|
1996
|
+
log.info('an agent connection named itself', { code: 'ALP_AGENT_CONNECTION', agentName: named, client: connection.client });
|
|
1997
|
+
} else if (connection.agentName !== named) log.warn('an agent connection gave a second name; the first stays', { code: 'ALP_AGENT_CONNECTION_RENAME', agentName: connection.agentName, refused: named });
|
|
1998
|
+
}
|
|
1959
1999
|
// Terminals are listed only while the host's switch is on (and the PTY module loads).
|
|
1960
2000
|
const terminalLimits = await terminals?.advertised();
|
|
1961
2001
|
respond({ result: { protocolVersion: PROTOCOL_VERSION, daemonVersion: version, pid: process.pid, hostname: os.hostname(), capabilities: terminalLimits ? [...capabilities, CAPABILITY.terminal, CAPABILITY.terminalCapture, CAPABILITY.terminalWatch] : capabilities, ...(attachments ? { attachmentLimits: attachments.limits } : {}), ...(terminalLimits ? { terminalLimits } : {}) } });
|