@anhlp/alp-daemon 0.0.1-beta.7 → 0.0.1-beta.9

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/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
- release();
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
- if (onData) {
177
- if (onData(chunk)) { stopped = true; kill(); }
178
- return;
179
- }
180
- outBytes += chunk.length;
181
- if (outBytes > maxBytes) finish(() => { kill(); reject(new GitReadFailed('output over its bound')); });
182
- else out.push(chunk);
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 => finish(() => {
187
- if (stopped) { resolve({ code: code ?? -1, stdout: Buffer.alloc(0), stopped: true }); return; }
188
- if (code === null || !okCodes.includes(code)) { reject(new GitReadFailed(`${args[0]} exit ${code}: ${err.trim().slice(0, 1000)}`)); return; }
189
- resolve({ code, stdout: Buffer.concat(out), stopped: false });
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
- export function createGitReader({ registry, binary = 'git', timeoutMs = GIT_READ.timeoutMs, log = () => {}, probe }: GitReaderOptions) {
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 === '?') { const rowPath = field.slice(2).replace(/\/$/, ''); return rowPath ? take({ path: rowPath, change: 'untracked', staged: false, unstaged: true }) : false; }
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 name of fields) {
909
+ for (const listed of fields) {
834
910
  // A nested repository is listed as its directory: not a file.
835
- if (!name || name.endsWith('/')) continue;
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, liveProbes, ModelCatalog, reclaimCopies, reclaimWorktrees, setActiveCatalog } from '@anhlp/alp-runtime/index';
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(timedOut ? `alpd startup timed out after ${timeoutMs} ms` : `alpd exited during startup (${code ?? signal})`));
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.once('message', message => {
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
- modelCatalog?.stop();
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
- const modelCatalog = process.env.ALP_MODEL_CATALOG === 'static' ? undefined : new ModelCatalog({ home, probes: liveProbes() });
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 = /already running|ALP is busy/.test(error?.message ?? '') ? 'ALP_LOCK_HELD'
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
- log.error(`alpd failed to start while ${{ 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]}${stoppedIn === 'state' ? `: ${error?.message ?? error}` : ''}`, { code, phase: stoppedIn, error });
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
+ }
@@ -0,0 +1,5 @@
1
+ // Entry of the native alpd (t-81b0.9): `bun build --compile` of this file is `<bundle>/bin/alpd`
2
+ // (scripts/build-alpd-native.mjs). The registration must load before alpd: ES modules evaluate in
3
+ // import order.
4
+ import './native-register.js';
5
+ import './main.js';
@@ -0,0 +1,21 @@
1
+ // The native alpd (t-81b0.9, scripts/build-alpd-native.mjs): registers this process as the native
2
+ // alpd before alpd's own modules load (native-entry.ts imports this first). The Claude Agent SDK is
3
+ // imported here by its literal name so the build embeds it in the executable; claude-transport.ts
4
+ // reaches it through this registration instead of its computed import.
5
+ import { bundleDist, bundleInfo, registerNativeDaemon } from '@anhlp/alp-core/packaged';
6
+
7
+ declare const __ALP_VERSION__: string;
8
+ // `alpd --version` answers before alpd loads, so install.sh, alp update and the build can check that this
9
+ // executable runs on the machine without starting a daemon.
10
+ if (process.argv.length === 3 && process.argv[2] === '--version') {
11
+ process.stdout.write(`${typeof __ALP_VERSION__ === 'string' ? __ALP_VERSION__ : '0.0.0-dev'}\n`);
12
+ process.exit(0);
13
+ }
14
+ // The bundle is read once at start: a later reinstall of the same version moves this directory aside.
15
+ const bundle = bundleInfo();
16
+
17
+ registerNativeDaemon({
18
+ modules: { claudeAgentSdk: () => import('@anthropic-ai/claude-agent-sdk') },
19
+ // The terminal helper (native-terminal.ts) lies in the bundle's @anhlp/alp-cli dist, not beside alpd.
20
+ distDir: () => bundle ? bundleDist(bundle) : undefined,
21
+ });
@@ -12,8 +12,16 @@ const sha = (value: Buffer) => createHash('sha256').update(value).digest('hex');
12
12
  function binary() {
13
13
  const libc = process.platform === 'linux' ? (process.report?.getReport() as { header?: { glibcVersionRuntime?: string } })?.header?.glibcVersionRuntime ? 'glibc' : 'musl' : undefined;
14
14
  const slot = [process.platform, process.arch, libc].filter(Boolean).join('-');
15
- const adjacent = path.join(path.dirname(fileURLToPath(import.meta.url)), 'terminal-native');
16
- const dir = existsSync(adjacent) ? adjacent : path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../native');
15
+ // The native alpd (t-81b0.9) has no directory of its own (import.meta.url is inside its executable):
16
+ // it uses the helper in its bundle's @anhlp/alp-cli dist; outside a bundle it has none.
17
+ // This file stays self-contained (it is shipped and tested on its own), so it reads the native alpd's
18
+ // registration (@anhlp/alp-core/packaged, native-register.ts) from its global directly.
19
+ const native = (globalThis as { [key: symbol]: { distDir?: () => string | undefined } | undefined })[Symbol.for('alp.nativeDaemon')];
20
+ const bundled = native?.distDir?.();
21
+ if (native && !bundled) throw new Error('The native alpd runs outside an ALP bundle: no terminal helper');
22
+ const here = bundled ?? path.dirname(fileURLToPath(import.meta.url));
23
+ const adjacent = path.join(here, 'terminal-native');
24
+ const dir = existsSync(adjacent) || bundled ? adjacent : path.resolve(here, '../native');
17
25
  const file = path.join(dir, 'prebuilt', slot, 'terminal-owner');
18
26
  const manifest = JSON.parse(readFileSync(path.join(path.dirname(file), 'manifest.json'), 'utf8'));
19
27
  if (!statSync(file).isFile() || manifest.protocol !== 1 || manifest.slot !== slot || manifest.source !== sha(readFileSync(path.join(dir, 'terminal-owner.c'))) || manifest.sha256 !== sha(readFileSync(file))) throw new Error('Native helper manifest mismatch');
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
- return deleteEntry(kind, name, { ...options, scope, policy, ...(revision !== undefined ? { revision: current.revision } : {}) });
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
- return { revision: toWire((await duplicateEntry(kind, from, to, { ...options, scope, policy })).revision) };
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
- return { revision: toWire((await renameEntry(kind, from, to, { ...options, scope, policy })).revision) };
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
- for (const model of [...(provider.models ?? []), ...(provider.additionalModels ?? [])]) models.push({ id: `acp:${provider.id}/${model.id}`, label: `${provider.label} · ${model.label ?? model.id}`, efforts: [] });
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.