@anhlp/alp-client 0.0.1-beta.6 → 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/src/doctor.js CHANGED
@@ -8,6 +8,7 @@ import { installedProgram, serviceFor } from './service.js';
8
8
  import { jsonObject, agentSources, resolveAgent } from '@anhlp/alp-core/resolver';
9
9
  import { loadHooks } from '@anhlp/alp-core/hooks';
10
10
  import { isTrusted } from '@anhlp/alp-core/trust';
11
+ import { validateSkill } from '@anhlp/alp-core/skill-validate';
11
12
  import { listProviders } from '@anhlp/alp-core/providers';
12
13
  import { settingsWarnings, validateSettings, validateUserSettings } from '@anhlp/alp-core/validation';
13
14
  import { readLogTail, scrubText } from '@anhlp/alp-core/log';
@@ -403,6 +404,33 @@ async function providersCheck(home, env) {
403
404
  };
404
405
  }
405
406
 
407
+ /**
408
+ * The library's skills against the skill format, in legacy mode: a format problem is a warning
409
+ * (the skill still loads), an unsafe reference or link an error. Nothing is rewritten.
410
+ */
411
+ async function skillFormatCheck(home) {
412
+ const directory = path.join(home, 'skills');
413
+ const entries = await readdir(directory, { withFileTypes: true }).catch(() => []);
414
+ const failed = [];
415
+ const warned = [];
416
+ let checked = 0;
417
+ for (const entry of entries) {
418
+ // The skills the resolver loads: a plain directory name holding SKILL.md (not .git or notes).
419
+ if ((!entry.isDirectory() && !entry.isSymbolicLink()) || !/^[\w.-]+$/.test(entry.name) || entry.name.startsWith('.')) continue;
420
+ if (!(await lstat(path.join(directory, entry.name, 'SKILL.md')).then(() => true, () => false))) continue;
421
+ checked++;
422
+ try {
423
+ const report = await validateSkill(path.join(directory, entry.name));
424
+ if (!report.ok) failed.push(`${entry.name}: ${report.errors.map(finding => finding.message).join('; ')}`);
425
+ else if (report.warnings.length) warned.push(`${entry.name}: ${report.warnings.map(finding => finding.code).join(', ')}`);
426
+ } catch (error) { failed.push(`${entry.name}: could not be checked: ${error.message}`); }
427
+ }
428
+ if (!checked) return undefined;
429
+ if (failed.length) return { id: 'skill-format', status: 'fail', summary: `${failed.length} library skill${failed.length > 1 ? 's have' : ' has'} an unsafe link or an unreadable file (alp skill check <name> explains)`, details: [...failed, ...warned] };
430
+ if (warned.length) return { id: 'skill-format', status: 'warn', summary: `${warned.length} library skill${warned.length > 1 ? 's do' : ' does'} not fully meet the skill format; they still load (alp skill check <name> explains)`, details: warned };
431
+ return { id: 'skill-format', status: 'ok', summary: `the library's skills meet the skill format (${directory})` };
432
+ }
433
+
406
434
  async function librarySkillsCheck(home) {
407
435
  const file = path.join(home, 'role-skills.json');
408
436
  let roles;
@@ -574,13 +602,204 @@ async function entries(directory, prefix = '') {
574
602
  return names.filter(name => name.startsWith(prefix)).map(name => path.join(directory, name));
575
603
  }
576
604
 
605
+ /**
606
+ * What a path in a suggested command may hold, on every platform: ASCII letters and digits,
607
+ * `. _ - / \\ :` and a space (common in Windows user folders; literal inside single quotes in sh
608
+ * and PowerShell alike). Nothing else: no quote of any kind (PowerShell ends a single-quoted string
609
+ * at ' and at four typographic quotes), no shell syntax, no control, format or non-ASCII character.
610
+ */
611
+ const QUOTABLE = /^[A-Za-z0-9._\-/\\: ]+$/;
612
+
613
+ /**
614
+ * A path for a suggested command (POSIX sh, or PowerShell on win32), in single quotes; undefined
615
+ * unless it is absolute (so it cannot read as an option) and every character is QUOTABLE, in which
616
+ * case nothing in it needs escaping. A path that is not gets no command at all.
617
+ */
618
+ export function shellQuote(text, platform = process.platform) {
619
+ const value = String(text);
620
+ const absolute = platform === 'win32' ? path.win32.isAbsolute(value) : path.posix.isAbsolute(value);
621
+ return absolute && QUOTABLE.test(value) ? `'${value}'` : undefined;
622
+ }
623
+
624
+ /**
625
+ * A path as alp doctor prints it: control, format and unusual space characters escaped as \u{hex},
626
+ * so it stays one line and reads as it is. A backslash is doubled, so the escaping is injective:
627
+ * a path that holds the text \u{a} prints as \\u{a}, never as a path with a newline would.
628
+ */
629
+ export const printable = text => String(text).replace(/\\|[\p{C}\p{Zl}\p{Zp}]|(?! )\p{Zs}/gu, char => char === '\\' ? '\\\\' : `\\u{${char.codePointAt(0).toString(16)}}`);
630
+
631
+ /** Why a clean and pushed leftover has no command. */
632
+ const UNQUOTABLE = 'no command: its path holds characters other than ASCII letters, digits, space and . _ - / \\ :, which a pasted command might not carry safely; check it and remove it by hand';
633
+
634
+ /** What each leftover state means, in the order alp doctor lists them. */
635
+ export const LEFTOVER_STATES = {
636
+ 'clean-pushed': 'clean and pushed (no changes, no untracked or ignored files, and every commit and, for a clone, every ref on a remote it last fetched; remove by hand with the command shown, given only for a plain ASCII path)',
637
+ dirty: 'dirty (uncommitted changes or untracked files, or a merge or rebase in progress; keep)',
638
+ 'local-only': 'local-only data (ignored files such as .env, local config, node_modules or build output, which exist only here; keep)',
639
+ unpushed: 'unpushed (commits, or for a clone tags, branches or other refs, that no remote-tracking ref mirrors; keep)',
640
+ 'in-use': 'in use (a running assignment, a retained result, this shell, or changed in the last day; keep)',
641
+ unknown: 'unknown (not a git checkout of its own, or git could not tell; check by hand)',
642
+ };
643
+
644
+ /** Paths ALP still owns: running assignments' worktrees and the retained results' checkouts. */
645
+ async function ownedPaths(home) {
646
+ const owned = new Set();
647
+ const read = async file => JSON.parse(await readFile(file, 'utf8'));
648
+ const live = await read(path.join(home, 'state', 'live.json')).catch(() => []);
649
+ for (const entry of Array.isArray(live) ? live : []) if (typeof entry?.worktree?.path === 'string') owned.add(realpathOr(entry.worktree.path));
650
+ const ledger = await read(path.join(home, 'state', 'assignment-results.json')).catch(() => undefined);
651
+ for (const entry of Array.isArray(ledger?.results) ? ledger.results : []) {
652
+ if (entry?.handled) continue;
653
+ for (const where of [entry?.worktree, entry?.checkout]) if (typeof where === 'string') owned.add(realpathOr(where));
654
+ }
655
+ return owned;
656
+ }
657
+
658
+ /** Why a leftover alp doctor had no time left for is unknown. */
659
+ const NOT_CHECKED = 'not checked: alp doctor ran out of time; run it again, or check by hand';
660
+
661
+ /** Thrown when the classifying deadline passed: the leftover is unknown, not checked. */
662
+ class OutOfTime extends Error {}
663
+
664
+ /**
665
+ * Classifies one leftover directory (t-e80a) without changing it: clean-pushed when it is a git
666
+ * checkout of its own with no changes, no untracked files, no ignored files at all, no
667
+ * hidden index state or submodules, no operation in progress, and a HEAD (for a standalone clone:
668
+ * every ref, including tags and a stash) that a remote-tracking branch already holds. "Pushed" is
669
+ * what this clone last fetched. Anything git cannot answer is unknown. Each git command ends by
670
+ * `deadline`; past it the leftover is unknown, not checked.
671
+ */
672
+ async function classifyLeftover(directory, { run, env, owned, here, recentMs, deadline, platform }) {
673
+ const real = realpathOr(directory);
674
+ const inside = (outer, inner) => inner === outer || inner.startsWith(outer.endsWith(path.sep) ? outer : outer + path.sep);
675
+ if ([...owned].some(owner => inside(real, owner) || inside(owner, real)) || inside(real, here)) return { path: directory, state: 'in-use' };
676
+ const git = async args => {
677
+ const left = deadline - Date.now();
678
+ if (left <= 0) throw new OutOfTime();
679
+ const result = await run('git', ['-C', directory, ...args], { env, timeoutMs: Math.min(10_000, left) });
680
+ // A command cut off by the deadline says nothing about the leftover.
681
+ if (Date.now() >= deadline) throw new OutOfTime();
682
+ return result;
683
+ };
684
+ const unknown = reason => ({ path: directory, state: 'unknown', reason });
685
+ // The top of every git checkout holds .git (a directory, or a file in a linked worktree): without it, no git is needed.
686
+ if (!await lstat(path.join(directory, '.git')).then(() => true, () => false)) return unknown('not a git checkout');
687
+ const where = await git(['rev-parse', '--show-toplevel', '--absolute-git-dir', '--git-common-dir']);
688
+ if (where.code !== 0) return unknown('not a git checkout');
689
+ const [top, gitDir, common] = where.stdout.trim().split('\n');
690
+ if (!top || !gitDir || !common || realpathOr(top) !== real) return unknown('not the top of a git checkout');
691
+ const commonDir = realpathOr(path.resolve(top, common));
692
+ // Untracked files are listed one by one whatever status.showUntrackedFiles says, and ignored ones
693
+ // too: git status alone does not show an ignored .env, which exists nowhere else. Any ignored
694
+ // file, node_modules and build output included, keeps the checkout: removing it loses them.
695
+ // git lists an ignored directory as one entry.
696
+ const status = await git(['-c', 'status.showUntrackedFiles=all', 'status', '--porcelain=v1', '-z', '--untracked-files=all', '--ignored=matching']);
697
+ if (status.code !== 0) return unknown('git status failed');
698
+ const rows = status.stdout.split('\0').filter(Boolean);
699
+ if (rows.some(row => !row.startsWith('!! '))) return { path: directory, state: 'dirty' };
700
+ const ignored = rows.map(row => row.slice(3));
701
+ if (ignored.length) return { path: directory, state: 'local-only', reason: `ignored files: ${ignored.slice(0, 3).join(', ')}${ignored.length > 3 ? `, … ${ignored.length - 3} more` : ''}` };
702
+ // git status cannot see a change to a file marked assume-unchanged or skip-worktree, nor inside a submodule.
703
+ const index = await git(['ls-files', '-z', '-v', '--stage']);
704
+ if (index.code !== 0) return unknown('git could not list its index');
705
+ const tracked = index.stdout.split('\0').filter(Boolean);
706
+ if (tracked.some(row => row[0] === 'S' || /[a-z]/.test(row[0]))) return unknown('some files are marked assume-unchanged or skip-worktree, which git status does not check');
707
+ if (tracked.some(row => row.slice(2).startsWith('160000 '))) return unknown('it has submodules, whose own changes and commits this check does not read');
708
+ for (const name of ['MERGE_HEAD', 'CHERRY_PICK_HEAD', 'REVERT_HEAD', 'rebase-merge', 'rebase-apply']) {
709
+ if (await lstat(path.join(gitDir, name)).then(() => true, () => false)) return { path: directory, state: 'dirty' };
710
+ }
711
+ // A worktree someone made by hand moments ago (a peer's, which ALP does not track) looks clean and pushed: recent work stays.
712
+ const touched = Math.max(...await Promise.all([directory, path.join(gitDir, 'index'), path.join(gitDir, 'HEAD')].map(file => lstat(file).then(info => info.mtimeMs, () => 0))));
713
+ if (Date.now() - touched < recentMs) return { path: directory, state: 'in-use', reason: 'changed in the last day' };
714
+ const linked = realpathOr(gitDir) !== commonDir;
715
+ // A linked worktree's branches live in its main repository and stay there when it goes; a clone
716
+ // holds its own refs: every branch, tag, note and stash (--all), all of which must be on a remote.
717
+ const ahead = await git(['rev-list', '-n', '1', ...(linked ? ['HEAD'] : ['HEAD', '--all']), '--not', '--remotes']);
718
+ if (ahead.code !== 0) return unknown('git could not compare HEAD with the remotes');
719
+ if (ahead.stdout.trim()) return { path: directory, state: 'unpushed' };
720
+ if (!linked) {
721
+ // A clone's refs go with it, and a tag (or note, or other ref) on a pushed commit is not seen
722
+ // above. Without a fetch no one can tell which tags a remote has, so every local ref must be
723
+ // mirrored by a remote-tracking ref: a branch by name (its commits are checked above), any
724
+ // other ref by name and object (refs/tags/v1 by refs/remotes/<remote>/tags/v1).
725
+ const refs = await git(['for-each-ref', '--format=%(objectname) %(refname)']);
726
+ if (refs.code !== 0) return unknown('git could not list its refs');
727
+ const all = refs.stdout.split('\n').filter(Boolean).map(line => { const at = line.indexOf(' '); return [line.slice(at + 1), line.slice(0, at)]; });
728
+ // Matched by full ref name, refs/remotes/<remote>/<name> for a configured remote, so a branch
729
+ // x is not taken for origin/feature/x (nor for a remote named feature's x unless feature is one).
730
+ const remotes = await git(['remote']);
731
+ if (remotes.code !== 0) return unknown('git could not list its remotes');
732
+ const remoteNames = remotes.stdout.split('\n').map(line => line.trim()).filter(Boolean);
733
+ const mirrors = new Map(all.filter(([name]) => name.startsWith('refs/remotes/')));
734
+ const mirrored = ([name, object]) => {
735
+ const branch = name.startsWith('refs/heads/');
736
+ const rest = branch ? name.slice('refs/heads/'.length) : name.slice('refs/'.length);
737
+ return remoteNames.some(remote => {
738
+ const target = mirrors.get(`refs/remotes/${remote}/${rest}`);
739
+ return target !== undefined && (branch || target === object);
740
+ });
741
+ };
742
+ const local = all.filter(entry => !entry[0].startsWith('refs/remotes/') && !mirrored(entry));
743
+ if (local.length) return { path: directory, state: 'unpushed', reason: `local refs no remote-tracking ref mirrors: ${local.slice(0, 3).map(([name]) => name).join(', ')}${local.length > 3 ? `, … ${local.length - 3} more` : ''}` };
744
+ }
745
+ const list = await git(['worktree', 'list', '--porcelain']);
746
+ if (list.code !== 0) return unknown('git could not list its worktrees');
747
+ if (!linked) {
748
+ // Removing a clone removes the repository of every worktree made from it.
749
+ if ((list.stdout.match(/^worktree /gm) ?? []).length > 1) return { path: directory, state: 'in-use', reason: 'other worktrees use its repository' };
750
+ const quoted = shellQuote(directory, platform);
751
+ if (quoted === undefined) return { path: directory, state: 'clean-pushed', reason: UNQUOTABLE };
752
+ return { path: directory, state: 'clean-pushed', command: platform === 'win32' ? `Remove-Item -LiteralPath ${quoted} -Recurse -Force` : `rm -rf ${quoted}` };
753
+ }
754
+ const main = list.stdout.match(/^worktree (.*)$/m)?.[1];
755
+ if (!main) return unknown('git could not name its main worktree');
756
+ const [quotedMain, quoted] = [shellQuote(main, platform), shellQuote(directory, platform)];
757
+ if (quotedMain === undefined || quoted === undefined) return { path: directory, state: 'clean-pushed', reason: UNQUOTABLE };
758
+ return { path: directory, state: 'clean-pushed', command: `git -C ${quotedMain} worktree remove ${quoted}` };
759
+ }
760
+
761
+ /**
762
+ * Classifies leftovers eight at a time, within `budgetMs` in all: a machine can hold thousands
763
+ * (old test homes in the temp directory), and doctor must still answer. The deadline is hard:
764
+ * each git command is cut off when it comes, and doctor stops waiting then. Those not finished
765
+ * by then are unknown.
766
+ */
767
+ async function classifyLeftovers(paths, { budgetMs, ...options }) {
768
+ const results = paths.map(directory => ({ path: directory, state: 'unknown', reason: NOT_CHECKED }));
769
+ const deadline = Date.now() + budgetMs;
770
+ let next = 0, done = false;
771
+ const worker = async () => {
772
+ while (!done && next < paths.length && Date.now() < deadline) {
773
+ const index = next++;
774
+ try {
775
+ const result = await classifyLeftover(paths[index], { ...options, deadline });
776
+ if (!done && Date.now() < deadline) results[index] = result;
777
+ } catch (error) { if (!(error instanceof OutOfTime)) results[index] = { path: paths[index], state: 'unknown', reason: `could not check: ${error?.message ?? error}` }; }
778
+ }
779
+ };
780
+ // A file system call that hangs (a stale network mount) is not cut off by a command timeout: stop waiting at the deadline.
781
+ let timer;
782
+ const expired = new Promise(resolve => { timer = setTimeout(resolve, Math.max(0, budgetMs)); });
783
+ await Promise.race([Promise.all(Array.from({ length: Math.min(8, paths.length) }, worker)), expired]);
784
+ clearTimeout(timer);
785
+ done = true;
786
+ return results;
787
+ }
788
+
789
+ /** How many suggested commands the text output shows; --json lists every one. */
790
+ const SHOWN_COMMANDS = 20;
791
+
577
792
  /**
578
793
  * Free space where ALP_HOME lives (t-2831), and what earlier runs left that takes room:
579
794
  * worktrees under ALP_HOME/worktrees and alp-* directories in the temp directory. When
580
795
  * the disk fills, alpd pauses work instead of stopping; this says so before it happens.
581
796
  * Nothing is deleted, by --fix either: a leftover may hold someone's uncommitted work.
797
+ * Each leftover is classified (t-e80a): clean and pushed, dirty, local-only data, unpushed, in use, or
798
+ * unknown; only a clean and pushed one gets a command, for the user to run by hand.
799
+ * Classifying stops after `classifyMs` (10 s); what it did not reach is unknown. A clean one
800
+ * changed within `recentMs` (a day) is in use: someone may have just started in it.
582
801
  */
583
- export async function diskCheck(home, { run = runCommand, env = process.env, free, tempDirs = [...new Set([os.tmpdir(), ...(process.platform === 'win32' ? [] : ['/tmp'])])] } = {}) {
802
+ export async function diskCheck(home, { run = runCommand, env = process.env, free, tempDirs = [...new Set([os.tmpdir(), ...(process.platform === 'win32' ? [] : ['/tmp'])])], cwd = process.cwd(), classifyMs = 10_000, recentMs = 24 * 60 * 60 * 1000, platform = process.platform } = {}) {
584
803
  let bytes = free;
585
804
  if (bytes === undefined) {
586
805
  try { const info = await statfs(home); bytes = Number(info.bavail) * Number(info.bsize); } catch {}
@@ -602,20 +821,35 @@ export async function diskCheck(home, { run = runCommand, env = process.env, fre
602
821
  const total = result.stdout.split('\n').reduce((sum, line) => sum + (Number(line.split(/\s+/)[0]) || 0), 0);
603
822
  return total * 1024;
604
823
  }
605
- const details = [];
606
- for (const [label, paths] of [[`${path.join(home, 'worktrees')}`, worktrees], [`${tempDirs.map(directory => path.join(directory, 'alp-*')).join(', ')}`, temp]]) {
607
- if (!paths.length) continue;
608
- const used = await size(paths);
609
- details.push(`${paths.length} left in ${label}${used === undefined ? '' : `, ${formatSize(used)}`}`);
824
+ // git must answer about each leftover itself, not about a repository the caller's environment names,
825
+ // and only read: GIT_OPTIONAL_LOCKS=0 keeps git status from refreshing the index of a worktree in use.
826
+ const gitEnv = { ...Object.fromEntries(Object.entries(env).filter(([name]) => !/^GIT_(DIR|WORK_TREE|INDEX_FILE|COMMON_DIR|OBJECT_DIRECTORY|NAMESPACE|CEILING_DIRECTORIES)$/.test(name))), GIT_OPTIONAL_LOCKS: '0' };
827
+ const groups = [[`${path.join(home, 'worktrees')}`, worktrees], [`${tempDirs.map(directory => path.join(directory, 'alp-*')).join(', ')}`, temp]].filter(([, paths]) => paths.length);
828
+ // Sizing and classifying run side by side: du alone can take many seconds over thousands of leftovers.
829
+ const [sizes, leftovers] = await Promise.all([
830
+ Promise.all(groups.map(([, paths]) => size(paths))),
831
+ ownedPaths(home).then(owned => classifyLeftovers([...new Set([...worktrees, ...temp])], { run, env: gitEnv, owned, here: realpathOr(cwd), budgetMs: classifyMs, recentMs, platform })),
832
+ ]);
833
+ const details = groups.map(([label, paths], index) => `${paths.length} left in ${label}${sizes[index] === undefined ? '' : `, ${formatSize(sizes[index])}`}`);
834
+ for (const [state, meaning] of Object.entries(LEFTOVER_STATES)) {
835
+ const count = leftovers.filter(entry => entry.state === state).length;
836
+ if (count) details.push(`${count} ${meaning}`);
610
837
  }
611
- const hint = details.length ? 'ALP deletes none of these: some may belong to running assignments or hold uncommitted work. Remove those you no longer need (git worktree remove, or rm -rf).' : undefined;
838
+ const unchecked = leftovers.filter(entry => entry.reason === NOT_CHECKED).length;
839
+ if (unchecked) details.push(` ${unchecked} of the unknown were not checked within ${classifyMs < 1000 ? `${classifyMs} ms` : `${Math.round(classifyMs / 1000)} s`}`);
840
+ // A clean and pushed leftover whose path no command can safely carry is listed by its path alone,
841
+ // as a shell comment: pasted with the rest, it runs nothing.
842
+ const commands = leftovers.filter(entry => entry.state === 'clean-pushed').map(entry => entry.command ?? `# remove by hand after checking (no safe command): ${printable(entry.path)}`);
843
+ for (const command of commands.slice(0, SHOWN_COMMANDS)) details.push(` ${command}`);
844
+ if (commands.length > SHOWN_COMMANDS) details.push(` … ${commands.length - SHOWN_COMMANDS} more; alp doctor --json lists every one`);
845
+ const hint = details.length ? 'ALP deletes none of these, and --fix does not either. Only a clean and pushed leftover gets a command, for you to run by hand, and only when its path is plain ASCII; inspect the others before removing anything.' : undefined;
612
846
  const where = bytes === undefined ? 'free space unknown' : `${formatSize(bytes)} free`;
613
- // Leftovers are listed, not judged: some may be live.
847
+ // Leftovers never change the status: some may be live.
614
848
  const status = bytes !== undefined && bytes < 2 * GIB ? 'warn' : 'ok';
615
849
  const summary = bytes !== undefined && bytes < 2 * GIB
616
850
  ? `${where} where ALP_HOME lives; when the disk is full, alpd pauses all work until space returns`
617
851
  : `${where} where ALP_HOME lives`;
618
- return { id: 'disk', status, summary, ...(details.length ? { details } : {}), ...(hint ? { hint } : {}) };
852
+ return { id: 'disk', status, summary, ...(details.length ? { details } : {}), ...(hint ? { hint } : {}), ...(leftovers.length ? { leftovers } : {}) };
619
853
  }
620
854
 
621
855
  function realpathOr(file) {
@@ -651,6 +885,8 @@ export async function diagnose({ home, project, env = process.env, run = runComm
651
885
  checks.push(await userSettingsCheck(home));
652
886
  const skills = await librarySkillsCheck(home);
653
887
  if (skills) checks.push(skills);
888
+ const skillFormat = await skillFormatCheck(home);
889
+ if (skillFormat) checks.push(skillFormat);
654
890
  const providers = await providersCheck(home, env);
655
891
  if (providers) checks.push(providers);
656
892
  const live = await liveCheck(home, running);
package/src/index.js CHANGED
@@ -1,14 +1,14 @@
1
1
  import { setTimeout as delay } from 'node:timers/promises';
2
2
  import { execFileSync, spawn } from 'node:child_process';
3
3
  import { createHash, randomUUID } from 'node:crypto';
4
- import { readFileSync } from 'node:fs';
5
- import { open, readFile, writeFile, realpath, stat, lstat, mkdir, readlink, unlink, readdir, rename, rmdir, link } from 'node:fs/promises';
4
+ import { fstatSync, readFileSync } from 'node:fs';
5
+ import { open, readFile, writeFile, realpath, stat, lstat, mkdir, readlink, unlink, readdir, chmod, rename, rmdir, link } from 'node:fs/promises';
6
6
  import net from 'node:net';
7
7
  import os from 'node:os';
8
8
  import path from 'node:path';
9
9
 
10
10
  import { scrubText } from '@anhlp/alp-core/log';
11
- import { EXECUTABLE_MARKER, entryCommand, executablePath, isExecutableName, isScript } from '@anhlp/alp-core/packaged';
11
+ import { entryCommand } from '@anhlp/alp-core/packaged';
12
12
  import { createSocketTransport } from './ipc.js';
13
13
  import { AlpRpcClient } from './rpc/client.js';
14
14
  import { hello } from './rpc/handshake.js';
@@ -70,11 +70,24 @@ async function assertSafeAncestors(directory) {
70
70
  export async function assertPrivateDirectory(directory, { create = false } = {}) {
71
71
  await assertSafeAncestors(directory);
72
72
  if (create) await mkdir(directory, { recursive: true, mode: 0o700 });
73
- const info = await lstat(directory);
73
+ let info = await lstat(directory);
74
74
  if (!info.isDirectory() || info.isSymbolicLink()) throw new Error('IPC requires a private directory, not a symlink');
75
- if (process.platform !== 'win32' && (info.uid !== process.getuid?.() || (info.mode & 0o077) !== 0)) {
76
- throw new Error('IPC directory must be owned by the current user with private permissions (0700)');
75
+ if (process.platform === 'win32') return;
76
+ const shown = JSON.stringify(directory);
77
+ const mine = (/** @type {import('node:fs').Stats} */ entry) => entry.isDirectory() && !entry.isSymbolicLink() && entry.uid === process.getuid?.();
78
+ if (info.uid !== process.getuid?.()) throw new Error(`IPC directory ${shown} must be owned by the current user (it belongs to uid ${info.uid}); use a directory of your own`);
79
+ if ((info.mode & 0o077) === 0) return;
80
+ // t-37f1: a directory the user made for ALP (mkdir, mktemp) and left at 0755 is still empty:
81
+ // ALP tightens it. One that holds anything is left alone, with the exact command to fix it.
82
+ if (create && (await readdir(directory)).length === 0) {
83
+ await chmod(directory, 0o700).catch(() => {});
77
84
  }
85
+ // Re-read: another ALP process may have tightened it meanwhile (and filled it).
86
+ info = await lstat(directory);
87
+ if (mine(info) && (info.mode & 0o077) === 0) return;
88
+ // Single quotes, so the command is safe to paste into a POSIX shell.
89
+ const quoted = `'${directory.replaceAll("'", "'\\''")}'`;
90
+ throw new Error(`IPC directory ${shown} must have private permissions (0700), not ${(info.mode & 0o777).toString(8).padStart(4, '0')}; fix it with: chmod 700 ${quoted}`);
78
91
  }
79
92
 
80
93
  export async function assertPrivateSocket(socketPath) {
@@ -89,37 +102,6 @@ export async function assertPrivateSocket(socketPath) {
89
102
 
90
103
  const isFile = file => stat(file).then(info => info.isFile(), () => false);
91
104
 
92
- /**
93
- * Whether `file` is the self-contained ALP executable (t-1e02) rather than a script or a shell
94
- * wrapper: a native image (Mach-O, ELF or PE) named alp that carries ALP's executable marker.
95
- * @param {string} file
96
- */
97
- async function isAlpExecutable(file) {
98
- if (isScript(file) || !isExecutableName(file) || !await isFile(file)) return false;
99
- const handle = await open(file, 'r').catch(() => undefined);
100
- if (!handle) return false;
101
- try {
102
- const { buffer, bytesRead } = await handle.read(Buffer.alloc(4), 0, 4, 0);
103
- if (bytesRead < 4) return false;
104
- const magic = buffer.readUInt32BE(0);
105
- const native = buffer.toString('latin1') === '\x7fELF' || buffer.subarray(0, 2).toString('latin1') === 'MZ'
106
- || [0xfeedface, 0xfeedfacf, 0xcefaedfe, 0xcffaedfe, 0xcafebabe].includes(magic);
107
- if (!native) return false;
108
- // Identity without running it: ALP's generated entry embeds EXECUTABLE_MARKER (an unrelated `alp` does not).
109
- const marker = Buffer.from(EXECUTABLE_MARKER, 'latin1');
110
- const chunk = Buffer.alloc(1 << 20);
111
- let tail = Buffer.alloc(0);
112
- for (let position = 0; ;) {
113
- const { bytesRead: read } = await handle.read(chunk, 0, chunk.length, position);
114
- if (!read) return false;
115
- const window = Buffer.concat([tail, chunk.subarray(0, read)]);
116
- if (window.includes(marker)) return true;
117
- tail = window.subarray(Math.max(0, window.length - marker.length + 1));
118
- position += read;
119
- }
120
- } finally { await handle.close(); }
121
- }
122
-
123
105
  /** Global bin directories that a GUI app's PATH may lack. */
124
106
  const BIN_DIRECTORIES = [path.join(os.homedir(), '.alp', 'bin'), path.join(os.homedir(), '.npm-global', 'bin'), path.join(os.homedir(), '.local', 'bin'), '/opt/homebrew/bin', '/usr/local/bin'];
125
107
 
@@ -127,48 +109,62 @@ const BIN_DIRECTORIES = [path.join(os.homedir(), '.alp', 'bin'), path.join(os.ho
127
109
  const CLI_PACKAGES = ['@anhlp/alp', '@anhlp/alp-cli'];
128
110
 
129
111
  /**
130
- * The ALP executable behind npm's `alp` when that is the @anhlp/alp launcher (t-cd7f): `bin/alp.js`
131
- * of a package named @anhlp/alp, which runs @anhlp/alp-<platform>-<arch>/bin/alp. npm puts that
132
- * package beside the launcher (a global install) or in its own node_modules.
133
- * @param {string} launcher the real path of `alp` on PATH
112
+ * alpd.js behind an `alp` found on PATH (its real path), or undefined: the ALP bundle's launcher
113
+ * (<bundle>/bin/alp or alp.cmd, t-1807), npm's @anhlp/alp-cli (dist/cli.js) or the @anhlp/alp wrapper (bin/alp.js),
114
+ * which depends on @anhlp/alp-cli (installed beside it, or in its own node_modules). On Windows an
115
+ * alp.cmd shim of npm or install.ps1 is followed to the script it runs (t-1f14).
116
+ * @param {string} cli @param {number} [depth]
134
117
  */
135
- async function npmWrapperExecutable(launcher, platform = process.platform, arch = process.arch) {
136
- if (path.basename(launcher) !== 'alp.js') return undefined;
137
- const root = path.resolve(path.dirname(launcher), '..');
138
- const name = await readFile(path.join(root, 'package.json'), 'utf8').then(text => JSON.parse(text)?.name).catch(() => undefined);
139
- if (name !== '@anhlp/alp') return undefined;
140
- const id = `alp-${platform}-${arch}`;
141
- const file = path.join('bin', platform === 'win32' ? 'alp.exe' : 'alp');
142
- for (const candidate of [path.join(root, 'node_modules', '@anhlp', id, file), path.join(root, '..', id, file)]) {
143
- if (await isAlpExecutable(candidate)) return candidate;
118
+ async function cliDaemonEntry(cli, depth = 0) {
119
+ const base = path.basename(cli).toLowerCase();
120
+ const root = path.resolve(path.dirname(cli), '..');
121
+ const alpd = directory => path.join(directory, 'dist', 'alpd.js');
122
+ const named = async directory => readFile(path.join(directory, 'package.json'), 'utf8').then(text => JSON.parse(text)?.name).catch(() => undefined);
123
+ let candidates = [];
124
+ if (base === 'alp.cmd' && depth < 1 && !await isFile(path.join(root, 'alp-bundle.json'))) {
125
+ // A Windows shim that is not the bundle's own: npm's ("%dp0%\node_modules\@anhlp\alp\bin\alp.js")
126
+ // or install.ps1's ("<root>\app\current\lib\node_modules\@anhlp\alp-cli\dist\cli.js"): follow the script it runs.
127
+ const text = await stat(cli).then(info => info.size <= 64 * 1024 ? readFile(cli, 'utf8') : '', () => '');
128
+ for (const [, quoted] of text.matchAll(/"([^"\r\n]+\.js)"/gi)) {
129
+ const script = quoted.replace(/^%~?dp0%?[\\/]?/i, `${path.dirname(cli)}${path.sep}`);
130
+ const entry = path.isAbsolute(script) && await isFile(script) ? await cliDaemonEntry(script, depth + 1) : undefined;
131
+ if (entry) return entry;
132
+ }
133
+ return undefined;
144
134
  }
135
+ if (base === 'alp' || base === 'alp.cmd') {
136
+ // A bundle: bin/ beside alp-bundle.json; name alpd through app/current when that is this bundle.
137
+ if (!await isFile(path.join(root, 'alp-bundle.json'))) return undefined;
138
+ const current = path.join(path.dirname(root), 'current');
139
+ const viaCurrent = await realpath(current).then(real => real === root, () => false);
140
+ candidates = [alpd(path.join(viaCurrent ? current : root, 'lib', 'node_modules', '@anhlp', 'alp-cli'))];
141
+ } else if (base === 'cli.js' && CLI_PACKAGES.includes(await named(root))) {
142
+ candidates = [alpd(root)];
143
+ } else if (base === 'alp.js' && await named(root) === '@anhlp/alp') {
144
+ candidates = [alpd(path.join(root, 'node_modules', '@anhlp', 'alp-cli')), alpd(path.join(root, '..', 'alp-cli'))];
145
+ }
146
+ for (const candidate of candidates) if (await isFile(candidate)) return candidate;
145
147
  return undefined;
146
148
  }
147
149
 
148
150
  /**
149
151
  * Finds alpd.js for a client that cannot locate it beside itself, such as the Paseo
150
152
  * plugin, which Paseo re-bundles: the first existing candidate, then where alpd last
151
- * recorded itself ($ALP_HOME/alpd.json), then the ALP CLI (`alp`) on PATH.
153
+ * recorded itself ($ALP_HOME/alpd.json), then the ALP CLI (`alp`) on PATH and in the usual install
154
+ * directories (`binDirectories`; a test passes [] so the caller's own install is not found).
152
155
  */
153
- export async function findDaemonEntry({ home = alpHome(), env = process.env, candidates = /** @type {Array<string | undefined>} */ ([]) } = {}) {
156
+ export async function findDaemonEntry({ home = alpHome(), env = process.env, candidates = /** @type {Array<string | undefined>} */ ([]), binDirectories = BIN_DIRECTORIES } = {}) {
154
157
  for (const candidate of candidates) if (candidate && await isFile(candidate)) return candidate;
155
- // The ALP executable carries alpd itself (`alp daemon run`).
156
- const own = executablePath();
157
- if (own) return own;
158
158
  try {
159
159
  const { entry } = JSON.parse(await readFile(daemonPaths(home).install, 'utf8'));
160
160
  if (typeof entry === 'string' && await isFile(entry)) return entry;
161
161
  } catch {}
162
- for (const directory of [...(env.PATH ?? '').split(path.delimiter).filter(Boolean), ...BIN_DIRECTORIES]) {
163
- const cli = await realpath(path.join(directory, process.platform === 'win32' ? 'alp.exe' : 'alp')).catch(() => undefined);
164
- if (cli && await isAlpExecutable(cli)) return cli;
165
- const npmExecutable = cli && await npmWrapperExecutable(cli);
166
- if (npmExecutable) return npmExecutable;
167
- if (!cli || path.basename(cli) !== 'cli.js') continue;
168
- const root = path.resolve(path.dirname(cli), '..');
169
- const name = await readFile(path.join(root, 'package.json'), 'utf8').then(text => JSON.parse(text)?.name).catch(() => undefined);
170
- const entry = path.join(root, 'dist', 'alpd.js');
171
- if (CLI_PACKAGES.includes(name) && await isFile(entry)) return entry;
162
+ for (const directory of [...(env.PATH ?? '').split(path.delimiter).filter(Boolean), ...binDirectories]) {
163
+ for (const name of process.platform === 'win32' ? ['alp.cmd', 'alp'] : ['alp']) {
164
+ const cli = await realpath(path.join(directory, name)).catch(() => undefined);
165
+ const entry = cli && await cliDaemonEntry(cli);
166
+ if (entry) return entry;
167
+ }
172
168
  }
173
169
  return undefined;
174
170
  }
@@ -302,16 +298,34 @@ export async function acquireDaemonLock(home, contents) {
302
298
  for (let attempt = 0; attempt < 2; attempt++) {
303
299
  try {
304
300
  const handle = await open(file, 'wx', 0o600);
305
- await handle.writeFile(JSON.stringify(lock));
301
+ // The identity of the file this call created, taken before anything can fail on it.
302
+ let ours;
303
+ try {
304
+ ours = await handle.stat();
305
+ await handle.writeFile(JSON.stringify(lock));
306
+ } catch (error) {
307
+ // Recover the descriptor identity if async fstat failed. Never infer ownership from
308
+ // the pathname: another process may already have replaced it. If both stats fail,
309
+ // leave the unreadable lock for explicit recovery rather than unlinking a successor.
310
+ if (!ours) { try { ours = fstatSync(handle.fd); } catch {} }
311
+ await handle.close().catch(() => {});
312
+ const there = await lstat(file).catch(() => undefined);
313
+ if (ours && there && ours.ino === there.ino && ours.dev === there.dev) await unlink(file).catch(() => {});
314
+ throw error;
315
+ }
306
316
  await handle.close();
307
317
  return lock;
308
318
  } catch (error) {
309
319
  if (error?.code !== 'EEXIST') throw error;
310
320
  const current = await readLock(home);
311
321
  if (lockAlive(current)) throw new Error(current.maintenance ? `ALP is busy: alp ${current.maintenance} runs (pid ${current.pid}); try again when it finishes` : `ALP daemon already running (pid ${current.pid})`);
312
- // Remove the stale lock only if nobody replaced it meanwhile.
322
+ // Remove the stale lock only if nobody replaced it meanwhile. A lock that cannot be read may
323
+ // be one another start is writing now: it stays, and this start says so.
313
324
  const again = await readFile(file, 'utf8').catch(() => undefined);
314
- if (again === undefined || JSON.stringify(current) === again || !lockAlive(JSON.parse(again))) await unlink(file).catch(() => {});
325
+ let parsed;
326
+ try { parsed = again === undefined ? undefined : JSON.parse(again); }
327
+ catch { throw new Error(`ALP daemon lock ${file} cannot be read: another start may be writing it. If no alpd or alp command is starting, remove it and try again`); }
328
+ if (again === undefined || JSON.stringify(current) === again || !lockAlive(parsed)) await unlink(file).catch(() => {});
315
329
  }
316
330
  }
317
331
  throw new Error('Could not acquire the ALP daemon lock');
@@ -490,12 +504,29 @@ export async function withDaemonLifecycle(home, action, run, { joinStart = false
490
504
  }
491
505
  }
492
506
 
507
+ /** How long a start waits for alpd to be ready by default: generous, as a loaded host can take a while. */
508
+ export const DAEMON_START_TIMEOUT_MS = 60_000;
509
+ /** The largest wait ALP_DAEMON_START_TIMEOUT_MS may ask for (one hour; setTimeout's own bound is about 24 days). */
510
+ const DAEMON_START_TIMEOUT_MAX_MS = 3_600_000;
511
+
512
+ /**
513
+ * The time a start of alpd waits for it to become ready, in ms: ALP_DAEMON_START_TIMEOUT_MS when it
514
+ * is a positive whole number of at most one hour, else DAEMON_START_TIMEOUT_MS.
515
+ * @param {NodeJS.ProcessEnv} [env]
516
+ */
517
+ export function daemonStartTimeoutMs(env = process.env) {
518
+ const raw = env.ALP_DAEMON_START_TIMEOUT_MS?.trim();
519
+ if (!raw || !/^[0-9]+$/.test(raw)) return DAEMON_START_TIMEOUT_MS;
520
+ const value = Number(raw);
521
+ return value > 0 && value <= DAEMON_START_TIMEOUT_MAX_MS ? value : DAEMON_START_TIMEOUT_MS;
522
+ }
523
+
493
524
  /**
494
525
  * Returns the socket of a ready daemon, starting one when none runs.
495
526
  * `entry` is the daemon script; it is started through its own detaching launcher so the
496
527
  * daemon outlives the client that started it.
497
528
  */
498
- export async function ensureDaemon({ home = alpHome(), entry = /** @type {string | undefined} */ (undefined), env = process.env, execPath = process.execPath, timeoutMs = 15_000, lifecycleLocked = false, signal = /** @type {AbortSignal | undefined} */ (undefined) } = {}) {
529
+ export async function ensureDaemon({ home = alpHome(), entry = /** @type {string | undefined} */ (undefined), env = process.env, execPath = process.execPath, timeoutMs = daemonStartTimeoutMs(env), lifecycleLocked = false, signal = /** @type {AbortSignal | undefined} */ (undefined) } = {}) {
499
530
  signal?.throwIfAborted();
500
531
  if (!lifecycleLocked) return withDaemonLifecycle(home, 'start', () => ensureDaemon({ home, entry, env, execPath, timeoutMs, lifecycleLocked: true, signal }), { signal });
501
532
  const ready = async () => {
@@ -606,17 +637,18 @@ async function distributionRoots() {
606
637
  * rename to `paseo`, which must never run under the web lock. `ALP_WEB_INTEGRATION` is read for
607
638
  * `paseo` only, after `ALP_PASEO_INTEGRATION`, for one release (like the alp-web-integration alias).
608
639
  * @param {string} [name]
609
- * @param {{ home?: string }} [options]
640
+ * @param {{ home?: string, env?: NodeJS.ProcessEnv, binDirectories?: string[], roots?: string[] }} [options]
610
641
  */
611
642
  export async function discoverIntegration(name = 'web', options = {}) {
612
643
  // The name becomes a descriptor path under <home>/integrations, so it must stay one safe segment.
613
644
  assertIntegrationName(name);
614
645
  if (name === 'web') return undefined;
646
+ const { env = process.env, binDirectories = BIN_DIRECTORIES, roots } = options;
615
647
  // Explicit environment override always wins: the integration's own, then the generic one.
616
648
  const envCandidates = [
617
- process.env[`ALP_${name.toUpperCase()}_INTEGRATION`],
618
- name === 'paseo' ? process.env.ALP_WEB_INTEGRATION : undefined,
619
- process.env.ALP_INTEGRATION_ENTRY,
649
+ env[`ALP_${name.toUpperCase()}_INTEGRATION`],
650
+ name === 'paseo' ? env.ALP_WEB_INTEGRATION : undefined,
651
+ env.ALP_INTEGRATION_ENTRY,
620
652
  ].filter(Boolean);
621
653
  for (const candidate of envCandidates) if (await isFile(candidate)) return candidate;
622
654
 
@@ -625,7 +657,7 @@ export async function discoverIntegration(name = 'web', options = {}) {
625
657
  if (fromHome) return fromHome;
626
658
 
627
659
  // A full distribution may describe a bundled integration relative to its own root.
628
- for (const root of await distributionRoots()) {
660
+ for (const root of roots ?? await distributionRoots()) {
629
661
  for (const descriptorPath of [path.join(root, 'integrations', `${name}.json`), path.join(root, 'integrations', name, 'integration.json')]) {
630
662
  const entry = await readIntegrationDescriptor(descriptorPath, [root]);
631
663
  if (entry) return entry;
@@ -634,7 +666,7 @@ export async function discoverIntegration(name = 'web', options = {}) {
634
666
 
635
667
  // Installed integration packages announce themselves on PATH.
636
668
  const binary = integrationBinaryName(name);
637
- for (const directory of [...(process.env.PATH ?? '').split(path.delimiter).filter(Boolean), ...BIN_DIRECTORIES]) {
669
+ for (const directory of [...(env.PATH ?? '').split(path.delimiter).filter(Boolean), ...binDirectories]) {
638
670
  const candidate = await realpath(path.join(directory, binary)).catch(() => undefined);
639
671
  if (candidate && await isFile(candidate)) return candidate;
640
672
  }
@@ -754,7 +786,7 @@ export async function startIntegration(nameOrOptions = 'web') {
754
786
  // The child is detached with its stdio ignored, so it cannot rely on the parent's console. `--service`
755
787
  // is the generic contract: the entry writes its own log (the path its error messages advertise) and
756
788
  // keeps the process alive as a service. `--register` still prints to stdout and is not passed here.
757
- // alp-web may be the ALP executable itself (`alp web serve`); other integrations are scripts.
789
+ // alp-web may be an `alp` command (`alp web serve`); other integrations are scripts.
758
790
  const launch = name === 'web' ? entryCommand(entry, 'web', ['--service', ...args]) : entryCommand(entry, 'web', ['--service', ...args], undefined, { scriptOnly: true });
759
791
  const child = spawn(launch.command, launch.args, { stdio: 'ignore', detached: true, env: { ...process.env, ALP_HOME: home } });
760
792
  let spawnError = null;
package/src/render.js CHANGED
@@ -94,6 +94,9 @@ export function renderLog(rootId, entries) {
94
94
  ? `✦ ${entry.agent} saved skill ${entry.name} for ${entry.roles.join(', ')}`
95
95
  : `✦ ${entry.agent} uses skill ${entry.skill}${entry.via === 'skill' ? ' (Skill tool)' : ''}`;
96
96
  break;
97
+ case 'skill-resource':
98
+ text = `✦ ${entry.agent} ${entry.action === 'run' ? 'runs' : 'reads'} ${entry.skill}/${entry.resource}`;
99
+ break;
97
100
  case 'compacted':
98
101
  text = entry.failed
99
102
  ? `✗ ${entry.agent}'s context could not be compacted${entry.error ? `: ${entry.error}` : ''}`
@@ -206,6 +206,11 @@ export function conversation(client) {
206
206
  * @param {Map['session.resumeCommand']['params']} params @returns {Promise<Map['session.resumeCommand']['result']>}
207
207
  */
208
208
  resumeCommand: params => client.request('session.resumeCommand', params),
209
+ /**
210
+ * The slash commands and skills the session's composer offers after "/" (capability `session.commands`, §12.12).
211
+ * @param {Map['session.commands']['params']} params @returns {Promise<Map['session.commands']['result']>}
212
+ */
213
+ commands: params => client.request('session.commands', params),
209
214
  /**
210
215
  * Changes a live root's model, effort or team (capability `session.configure.live`, §12.9) at the
211
216
  * idle barrier, through a restart of its native process on the same conversation; `configured` says