@anhlp/alp-client 0.0.1-beta.3 → 0.0.1-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -1
- package/dist/antigravity-probe.d.ts +1 -2
- package/dist/antigravity-probe.js +11 -5
- package/dist/command.js +3 -1
- package/dist/doctor.d.ts +1 -0
- package/dist/doctor.js +105 -11
- package/dist/index.d.ts +92 -6
- package/dist/index.js +206 -37
- package/dist/integration-lock.d.ts +37 -0
- package/dist/integration-lock.js +199 -0
- package/dist/mcp-probe.js +1 -1
- package/dist/portable.d.ts +3 -1
- package/dist/portable.js +11 -2
- package/dist/rpc/handshake.d.ts +6 -1
- package/dist/rpc/handshake.js +4 -2
- package/dist/rpc/registry.d.ts +218 -0
- package/dist/rpc/registry.js +218 -0
- package/dist/rpc/terminal-directory.d.ts +24 -0
- package/dist/rpc/terminal-directory.js +70 -0
- package/dist/service.d.ts +1 -1
- package/dist/service.js +3 -1
- package/dist/setup.js +6 -5
- package/dist/supervise.d.ts +3 -1
- package/dist/supervise.js +14 -10
- package/package.json +11 -3
- package/src/antigravity-probe.js +12 -5
- package/src/command.js +3 -1
- package/src/doctor.d.ts +1 -0
- package/src/doctor.js +106 -1
- package/src/index.js +285 -39
- package/src/integration-lock.js +203 -0
- package/src/mcp-probe.js +1 -1
- package/src/portable.js +4 -1
- package/src/rpc/handshake.js +7 -2
- package/src/rpc/registry.js +224 -0
- package/src/rpc/terminal-directory.js +61 -0
- package/src/service.js +5 -2
- package/src/setup.js +7 -5
- package/src/supervise.d.ts +3 -1
- package/src/supervise.js +18 -11
package/dist/service.js
CHANGED
|
@@ -4,6 +4,7 @@ import { mkdir, readFile, unlink, writeFile } from "node:fs/promises";
|
|
|
4
4
|
import os from "node:os";
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
import { runCommand } from "./command.js";
|
|
7
|
+
import { entryCommand } from "@anhlp/alp-core/packaged";
|
|
7
8
|
var CARRIED = ["ALP_RUN_LOG_DIR", "ALP_CODEX_BIN", "ALP_CLAUDE_BIN", "ALP_ANTIGRAVITY_BIN", "ALP_GH_BIN"];
|
|
8
9
|
var escapeXml = (value) => value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
9
10
|
var quoteUnit = (value) => `"${value.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
|
|
@@ -44,7 +45,8 @@ function serviceFor({ home, env = process.env, platform = process.platform, uid
|
|
|
44
45
|
function serviceDefinition(service, { home, entry, execPath = process.execPath, env = process.env }) {
|
|
45
46
|
const variables = { ALP_HOME: path.resolve(home), PATH: env.PATH ?? "" };
|
|
46
47
|
for (const name of CARRIED) if (env[name]) variables[name] = env[name];
|
|
47
|
-
const
|
|
48
|
+
const launch = entryCommand(entry, "daemon", ["--service"], execPath);
|
|
49
|
+
const program = [launch.command, ...launch.args];
|
|
48
50
|
const fatal = path.join(path.resolve(home), "logs", "alpd.service.log");
|
|
49
51
|
if (service.kind === "launchd") {
|
|
50
52
|
return `<?xml version="1.0" encoding="UTF-8"?>
|
package/dist/setup.js
CHANGED
|
@@ -3,7 +3,7 @@ import { copyFile, readFile, writeFile } from "node:fs/promises";
|
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { parseArgs } from "node:util";
|
|
5
5
|
import { initProject } from "@anhlp/alp-core/init";
|
|
6
|
-
import {
|
|
6
|
+
import { catalogModels } from "@anhlp/alp-core/models";
|
|
7
7
|
import { fitEffort, modelEfforts } from "@anhlp/alp-core/profiles";
|
|
8
8
|
import { resolveTeam } from "@anhlp/alp-core/teams";
|
|
9
9
|
import { validateSettings } from "@anhlp/alp-core/validation";
|
|
@@ -83,7 +83,7 @@ async function setup(args, home) {
|
|
|
83
83
|
] });
|
|
84
84
|
if (next2 === "cancel") throw new Cancelled();
|
|
85
85
|
if (next2 === "save") break;
|
|
86
|
-
await change(seats);
|
|
86
|
+
await change(seats, home);
|
|
87
87
|
show();
|
|
88
88
|
}
|
|
89
89
|
}
|
|
@@ -102,16 +102,17 @@ async function setup(args, home) {
|
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
104
|
var describe = (choice) => `${choice.provider}:${choice.model}${choice.effort ? `:${choice.effort}` : ""}`;
|
|
105
|
-
async function change(seats) {
|
|
105
|
+
async function change(seats, home) {
|
|
106
|
+
const catalog = catalogModels(home);
|
|
106
107
|
const [seat, at] = await select({ message: "Which model?", choices: Object.entries(seats).flatMap(([seat2, choices]) => choices.map((choice, at2) => ({ label: `${SEAT_LABEL[seat2]} \xB7 ${PROVIDER_LABEL[choice.provider]}`, value: [seat2, at2], hint: describe(choice) }))) });
|
|
107
108
|
const current = seats[seat][at];
|
|
108
|
-
const offered =
|
|
109
|
+
const offered = catalog.filter((model2) => model2.runtime === current.provider);
|
|
109
110
|
const picked = await select({ message: `${SEAT_LABEL[seat]} on ${PROVIDER_LABEL[current.provider]}`, initial: current.model, choices: [
|
|
110
111
|
...offered.map((model2) => ({ label: model2.label, value: model2.id, hint: `${model2.id} \xB7 ${model2.description}` })),
|
|
111
112
|
{ label: "Other\u2026", value: void 0, hint: "type a model ID" }
|
|
112
113
|
] });
|
|
113
114
|
const model = picked ?? nativeModel(await text(`${PROVIDER_LABEL[current.provider]} model ID:`), current.provider, "The model ID");
|
|
114
|
-
const efforts = modelEfforts(`${current.provider}:${model}`) ?? ANY_EFFORT;
|
|
115
|
+
const efforts = catalog.find((entry) => entry.runtime === current.provider && entry.id === model)?.efforts ?? modelEfforts(`${current.provider}:${model}`) ?? ANY_EFFORT;
|
|
115
116
|
const effort = efforts.length > 1 ? await select({
|
|
116
117
|
message: "Effort",
|
|
117
118
|
initial: efforts.includes(current.effort) ? current.effort : DEFAULTS[seat][current.provider][1],
|
package/dist/supervise.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export function holdDaemon(home: string, by?: string): Promise<void>;
|
|
2
2
|
export function releaseHold(home: string): Promise<void>;
|
|
3
3
|
export function daemonHeld(home: string): Promise<boolean>;
|
|
4
|
-
export function startDaemon(options: { home: string; entry?: string; env?: NodeJS.ProcessEnv; timeoutMs?: number }): Promise<string>;
|
|
4
|
+
export function startDaemon(options: { home: string; entry?: string; env?: NodeJS.ProcessEnv; timeoutMs?: number; signal?: AbortSignal }): Promise<string>;
|
|
5
5
|
export function superviseDaemon(options: {
|
|
6
6
|
home: string;
|
|
7
7
|
entry?: string | (() => string | undefined);
|
|
@@ -9,4 +9,6 @@ export function superviseDaemon(options: {
|
|
|
9
9
|
intervalMs?: number;
|
|
10
10
|
misses?: number;
|
|
11
11
|
log?: (message: string) => void;
|
|
12
|
+
/** Who keeps alpd up, for the log: "alpd started with <by>". */
|
|
13
|
+
by?: string;
|
|
12
14
|
}): (() => Promise<void>) & { started: Promise<void> };
|
package/dist/supervise.js
CHANGED
|
@@ -15,37 +15,41 @@ async function releaseHold(home) {
|
|
|
15
15
|
async function daemonHeld(home) {
|
|
16
16
|
return stat(holdFile(home)).then(() => true, () => false);
|
|
17
17
|
}
|
|
18
|
-
async function whenReady(home, timeoutMs) {
|
|
18
|
+
async function whenReady(home, timeoutMs, signal) {
|
|
19
19
|
const deadline = Date.now() + timeoutMs;
|
|
20
20
|
while (Date.now() < deadline) {
|
|
21
|
+
signal?.throwIfAborted();
|
|
21
22
|
const lock = await readLock(home);
|
|
22
23
|
if (lockAlive(lock) && lock.ready) return lock.socket;
|
|
23
24
|
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
24
25
|
}
|
|
25
26
|
throw new Error(`alpd did not become ready within ${timeoutMs / 1e3} s; see ${path.join(home, "logs")}`);
|
|
26
27
|
}
|
|
27
|
-
async function startDaemon({ home, entry, env = process.env, timeoutMs = 15e3, automatic = false }) {
|
|
28
|
+
async function startDaemon({ home, entry, env = process.env, timeoutMs = 15e3, automatic = false, signal }) {
|
|
28
29
|
return withDaemonLifecycle(home, "start", async () => {
|
|
30
|
+
signal?.throwIfAborted();
|
|
29
31
|
if (automatic) {
|
|
30
32
|
if (await daemonHeld(home)) return;
|
|
31
33
|
} else await releaseHold(home);
|
|
32
34
|
const lock = await readLock(home);
|
|
33
|
-
|
|
35
|
+
signal?.throwIfAborted();
|
|
36
|
+
if (lockAlive(lock)) return lock.ready ? lock.socket : whenReady(home, timeoutMs, signal);
|
|
34
37
|
const service = serviceFor({ home, env });
|
|
35
38
|
if (service && await installedProgram(service)) {
|
|
39
|
+
signal?.throwIfAborted();
|
|
36
40
|
await startService(service);
|
|
37
|
-
return whenReady(home, timeoutMs);
|
|
41
|
+
return whenReady(home, timeoutMs, signal);
|
|
38
42
|
}
|
|
39
|
-
return ensureDaemon({ home, entry, env, timeoutMs, lifecycleLocked: true });
|
|
40
|
-
});
|
|
43
|
+
return ensureDaemon({ home, entry, env, timeoutMs, lifecycleLocked: true, signal });
|
|
44
|
+
}, { signal });
|
|
41
45
|
}
|
|
42
46
|
function superviseDaemon({ home, entry, env = process.env, intervalMs = 5e3, misses = 2, log = () => {
|
|
43
|
-
} }) {
|
|
47
|
+
}, by }) {
|
|
44
48
|
let stopped = false;
|
|
45
49
|
let missed = 0;
|
|
46
50
|
let starting;
|
|
47
|
-
const start = (reason) => {
|
|
48
|
-
starting ??= startDaemon({ home, entry: typeof entry === "function" ? entry() : entry, env, automatic
|
|
51
|
+
const start = (reason, automatic = true) => {
|
|
52
|
+
starting ??= startDaemon({ home, entry: typeof entry === "function" ? entry() : entry, env, automatic }).then((socket) => {
|
|
49
53
|
if (socket) log(`alpd ${reason}`);
|
|
50
54
|
}, (error) => log(`could not start alpd: ${error?.message ?? error}`)).finally(() => {
|
|
51
55
|
starting = void 0;
|
|
@@ -65,7 +69,7 @@ function superviseDaemon({ home, entry, env = process.env, intervalMs = 5e3, mis
|
|
|
65
69
|
}
|
|
66
70
|
if (++missed >= misses && !stopped) await start("was down; started it again");
|
|
67
71
|
};
|
|
68
|
-
const initial = start(
|
|
72
|
+
const initial = start(by ? `started with ${by}` : "started", false);
|
|
69
73
|
const timer = setInterval(() => {
|
|
70
74
|
void check();
|
|
71
75
|
}, intervalMs);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anhlp/alp-client",
|
|
3
|
-
"version": "0.0.1-beta.
|
|
3
|
+
"version": "0.0.1-beta.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "ALP client: portable JSON-RPC SDK, WebSocket and Node IPC transports, daemon discovery and setup",
|
|
6
6
|
"files": [
|
|
@@ -57,6 +57,14 @@
|
|
|
57
57
|
"types": "./dist/index.d.ts",
|
|
58
58
|
"default": "./src/index.js"
|
|
59
59
|
},
|
|
60
|
+
"./integration-lock": {
|
|
61
|
+
"alp-source": {
|
|
62
|
+
"types": "./src/integration-lock.js",
|
|
63
|
+
"default": "./src/integration-lock.js"
|
|
64
|
+
},
|
|
65
|
+
"types": "./dist/integration-lock.d.ts",
|
|
66
|
+
"default": "./src/integration-lock.js"
|
|
67
|
+
},
|
|
60
68
|
"./ipc": {
|
|
61
69
|
"alp-source": {
|
|
62
70
|
"types": "./src/ipc.js",
|
|
@@ -142,8 +150,8 @@
|
|
|
142
150
|
"node": ">=22.12"
|
|
143
151
|
},
|
|
144
152
|
"dependencies": {
|
|
145
|
-
"@anhlp/alp-core": "0.0.1-beta.
|
|
146
|
-
"@anhlp/alp-protocol": "0.0.1-beta.
|
|
153
|
+
"@anhlp/alp-core": "0.0.1-beta.5",
|
|
154
|
+
"@anhlp/alp-protocol": "0.0.1-beta.5"
|
|
147
155
|
},
|
|
148
156
|
"scripts": {
|
|
149
157
|
"build": "node ../../scripts/build-workspace.mjs client",
|
package/src/antigravity-probe.js
CHANGED
|
@@ -12,6 +12,16 @@ const DEFAULT_ANTIGRAVITY_MODELS = [
|
|
|
12
12
|
];
|
|
13
13
|
|
|
14
14
|
const ANTIGRAVITY_MODES = [
|
|
15
|
+
{
|
|
16
|
+
id: 'read-only',
|
|
17
|
+
label: 'Read only',
|
|
18
|
+
description: 'agy reads and searches; it denies file edits and terminal commands (--sandbox, request-review).',
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
id: 'workspace-write',
|
|
22
|
+
label: 'Workspace write',
|
|
23
|
+
description: 'agy edits files and runs commands in its terminal sandbox inside the workspace (--mode accept-edits --sandbox, proceed-in-sandbox).',
|
|
24
|
+
},
|
|
15
25
|
{
|
|
16
26
|
id: 'full-access',
|
|
17
27
|
label: 'Full access',
|
|
@@ -54,7 +64,7 @@ function runCommand(command, args, { env = process.env, cwd, timeoutMs = 20_000
|
|
|
54
64
|
|
|
55
65
|
/**
|
|
56
66
|
* Probes Antigravity CLI for version, status and available models.
|
|
57
|
-
* @param {{ command?: string, args?: string[], env?: Record<string, string>, label?: string, id?: string
|
|
67
|
+
* @param {{ command?: string, args?: string[], env?: Record<string, string>, label?: string, id?: string}} [provider] readOnlyMode, if set, is a legacy setting with no effect: agy enforces read-only itself.
|
|
58
68
|
* @param {{ cwd?: string, timeoutMs?: number }} [options]
|
|
59
69
|
*/
|
|
60
70
|
export async function probeAntigravity(provider = {}, { cwd, timeoutMs = 20_000 } = {}) {
|
|
@@ -97,10 +107,7 @@ export async function probeAntigravity(provider = {}, { cwd, timeoutMs = 20_000
|
|
|
97
107
|
agent: { name: 'antigravity', title: 'Antigravity', version },
|
|
98
108
|
models,
|
|
99
109
|
currentModel: models[0]?.id,
|
|
100
|
-
modes:
|
|
101
|
-
id: 'read-only', label: 'Read only (instructions)',
|
|
102
|
-
description: 'For new sessions: prompt-only read-only behavior. agy still runs with full access; no OS sandbox enforces this mode.',
|
|
103
|
-
}] : ANTIGRAVITY_MODES,
|
|
110
|
+
modes: ANTIGRAVITY_MODES,
|
|
104
111
|
currentMode: 'full-access',
|
|
105
112
|
};
|
|
106
113
|
}
|
package/src/command.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process';
|
|
2
|
+
import { scriptCommand } from '@anhlp/alp-core/packaged';
|
|
2
3
|
|
|
3
4
|
/** Runs a command; resolves with its exit code and output, never rejects. */
|
|
4
5
|
export function runCommand(command, args, { cwd, env = process.env, timeoutMs = 15_000 } = {}) {
|
|
@@ -6,7 +7,8 @@ export function runCommand(command, args, { cwd, env = process.env, timeoutMs =
|
|
|
6
7
|
let child;
|
|
7
8
|
// Windows cannot exec a script by its shebang; a JavaScript command runs under this Node, as the Codex transport does.
|
|
8
9
|
const script = process.platform === 'win32' && /\.[cm]?js$/i.test(command);
|
|
9
|
-
|
|
10
|
+
const run = script ? scriptCommand(command, args) : { command, args };
|
|
11
|
+
try { child = spawn(run.command, run.args, { cwd, env: run.env ? { ...env, ...run.env } : env, stdio: ['ignore', 'pipe', 'pipe'] }); }
|
|
10
12
|
catch (error) { resolve({ code: -1, stdout: '', stderr: error.message }); return; }
|
|
11
13
|
let stdout = '', stderr = '';
|
|
12
14
|
child.stdout.on('data', chunk => { stdout += chunk; });
|
package/src/doctor.d.ts
CHANGED
|
@@ -10,4 +10,5 @@ export type CommandResult = { code: number; stdout: string; stderr: string };
|
|
|
10
10
|
export type CommandRunner = (command: string, args: string[], options?: { cwd?: string; env?: NodeJS.ProcessEnv; timeoutMs?: number }) => Promise<CommandResult>;
|
|
11
11
|
export const runCommand: CommandRunner;
|
|
12
12
|
export function diagnose(options?: { home?: string; project?: string; env?: NodeJS.ProcessEnv; run?: CommandRunner; sandbox?: boolean; daemonEntry?: string; timeoutMs?: number }): Promise<DoctorCheck[]>;
|
|
13
|
+
export function diskCheck(home: string, options?: { run?: CommandRunner; env?: NodeJS.ProcessEnv; free?: number; tempDirs?: string[] }): Promise<DoctorCheck>;
|
|
13
14
|
export function repair(checks: DoctorCheck[]): Promise<Array<{ id: string; result: string }>>;
|
package/src/doctor.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { realpathSync } from 'node:fs';
|
|
2
|
+
import { access, chmod, constants, lstat, readdir, readFile, statfs, unlink, writeFile } from 'node:fs/promises';
|
|
3
|
+
import os from 'node:os';
|
|
2
4
|
import path from 'node:path';
|
|
3
5
|
import { runCommand } from './command.js';
|
|
4
6
|
import { assertPrivateSocket, connect, daemonPaths, lockAlive, readLock } from './index.js';
|
|
@@ -519,12 +521,115 @@ async function schemaChecks(home) {
|
|
|
519
521
|
return checks;
|
|
520
522
|
}
|
|
521
523
|
|
|
524
|
+
/** Checks whether directory `target` is inside directory `container`. */
|
|
525
|
+
function isInside(target, container) {
|
|
526
|
+
const resolve = directory => {
|
|
527
|
+
let current = path.resolve(directory);
|
|
528
|
+
let suffix = '';
|
|
529
|
+
for (;;) {
|
|
530
|
+
try { return path.join(realpathSync(current), suffix); }
|
|
531
|
+
catch {
|
|
532
|
+
const parent = path.dirname(current);
|
|
533
|
+
if (parent === current) return path.join(current, suffix);
|
|
534
|
+
suffix = path.join(path.basename(current), suffix);
|
|
535
|
+
current = parent;
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
};
|
|
539
|
+
const resolvedTarget = resolve(target);
|
|
540
|
+
const resolvedContainer = resolve(container);
|
|
541
|
+
const relative = path.relative(resolvedContainer, resolvedTarget);
|
|
542
|
+
return relative === '' || (relative !== '..' && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative));
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* Checks where ALP_HOME sits: staging and state live there (§12.6). Sandboxed sessions
|
|
547
|
+
* can write the temp directory and their workspace, so staged copies inside either could
|
|
548
|
+
* be modified by a sandboxed tool before alpd reads them (ALP_ATTACHMENT_STAGE_EXPOSED).
|
|
549
|
+
*/
|
|
550
|
+
async function homeLocationCheck(home, project) {
|
|
551
|
+
if (process.platform === 'win32' || !home) return { id: 'home', status: 'ok', summary: home ?? '' };
|
|
552
|
+
if (project && isInside(home, project)) {
|
|
553
|
+
return {
|
|
554
|
+
id: 'home', status: 'warn',
|
|
555
|
+
summary: `ALP_HOME (${home}) is inside this project's workspace, which sandboxed sessions may write: a tool could change staged attachment copies before they are read; keep ALP_HOME outside workspaces (default ~/.alp)`,
|
|
556
|
+
};
|
|
557
|
+
}
|
|
558
|
+
const inTmp = isInside(home, os.tmpdir()) || isInside(home, '/tmp');
|
|
559
|
+
if (inTmp) {
|
|
560
|
+
return {
|
|
561
|
+
id: 'home', status: 'warn',
|
|
562
|
+
summary: `ALP_HOME (${home}) is inside the temp directory, which sandboxed sessions may write: a tool could change staged attachment copies before they are read; keep ALP_HOME outside it (default ~/.alp)`,
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
return { id: 'home', status: 'ok', summary: home };
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
const GIB = 1024 ** 3;
|
|
569
|
+
const formatSize = bytes => bytes >= GIB ? `${(bytes / GIB).toFixed(1)} GB` : `${Math.round(bytes / 1024 ** 2)} MB`;
|
|
570
|
+
|
|
571
|
+
/** Entries of `directory` whose names start with `prefix`, as full paths; none when it cannot be read. */
|
|
572
|
+
async function entries(directory, prefix = '') {
|
|
573
|
+
const names = await readdir(directory).catch(() => []);
|
|
574
|
+
return names.filter(name => name.startsWith(prefix)).map(name => path.join(directory, name));
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
/**
|
|
578
|
+
* Free space where ALP_HOME lives (t-2831), and what earlier runs left that takes room:
|
|
579
|
+
* worktrees under ALP_HOME/worktrees and alp-* directories in the temp directory. When
|
|
580
|
+
* the disk fills, alpd pauses work instead of stopping; this says so before it happens.
|
|
581
|
+
* Nothing is deleted, by --fix either: a leftover may hold someone's uncommitted work.
|
|
582
|
+
*/
|
|
583
|
+
export async function diskCheck(home, { run = runCommand, env = process.env, free, tempDirs = [...new Set([os.tmpdir(), ...(process.platform === 'win32' ? [] : ['/tmp'])])] } = {}) {
|
|
584
|
+
let bytes = free;
|
|
585
|
+
if (bytes === undefined) {
|
|
586
|
+
try { const info = await statfs(home); bytes = Number(info.bavail) * Number(info.bsize); } catch {}
|
|
587
|
+
}
|
|
588
|
+
const worktrees = await entries(path.join(home, 'worktrees'));
|
|
589
|
+
const temp = [];
|
|
590
|
+
for (const directory of tempDirs) {
|
|
591
|
+
for (const entry of await entries(directory, 'alp-')) {
|
|
592
|
+
const info = await lstat(entry).catch(() => undefined);
|
|
593
|
+
if (info?.isDirectory() && !temp.includes(realpathOr(entry))) temp.push(realpathOr(entry));
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
/** Kilobytes du counts under the paths; undefined when du is missing, slow or fails. */
|
|
597
|
+
async function size(paths) {
|
|
598
|
+
if (!paths.length) return 0;
|
|
599
|
+
if (process.platform === 'win32') return undefined;
|
|
600
|
+
const result = await run('du', ['-sk', ...paths], { env, timeoutMs: 20_000 });
|
|
601
|
+
if (result.code !== 0 && !result.stdout) return undefined;
|
|
602
|
+
const total = result.stdout.split('\n').reduce((sum, line) => sum + (Number(line.split(/\s+/)[0]) || 0), 0);
|
|
603
|
+
return total * 1024;
|
|
604
|
+
}
|
|
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)}`}`);
|
|
610
|
+
}
|
|
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;
|
|
612
|
+
const where = bytes === undefined ? 'free space unknown' : `${formatSize(bytes)} free`;
|
|
613
|
+
// Leftovers are listed, not judged: some may be live.
|
|
614
|
+
const status = bytes !== undefined && bytes < 2 * GIB ? 'warn' : 'ok';
|
|
615
|
+
const summary = bytes !== undefined && bytes < 2 * GIB
|
|
616
|
+
? `${where} where ALP_HOME lives; when the disk is full, alpd pauses all work until space returns`
|
|
617
|
+
: `${where} where ALP_HOME lives`;
|
|
618
|
+
return { id: 'disk', status, summary, ...(details.length ? { details } : {}), ...(hint ? { hint } : {}) };
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
function realpathOr(file) {
|
|
622
|
+
try { return realpathSync(file); } catch { return file; }
|
|
623
|
+
}
|
|
624
|
+
|
|
522
625
|
export async function diagnose({ home, project, env = process.env, run = runCommand, sandbox, daemonEntry, timeoutMs = 3000 } = {}) {
|
|
523
626
|
const checks = [];
|
|
524
627
|
if (daemonEntry) {
|
|
525
628
|
checks.push(await lstat(daemonEntry).then(() => ({ id: 'build', status: 'ok', summary: daemonEntry }), () => ({ id: 'build', status: 'fail', summary: `alpd is not built (${daemonEntry}); run npm run build` })));
|
|
526
629
|
}
|
|
527
630
|
checks.push(...await runtimeChecks({ env, run }));
|
|
631
|
+
checks.push(await homeLocationCheck(home, project));
|
|
632
|
+
if (home) checks.push(await diskCheck(home, { run, env }));
|
|
528
633
|
const daemon = await daemonCheck(home);
|
|
529
634
|
checks.push(daemon);
|
|
530
635
|
const service = await serviceCheck(home, env);
|