@anhlp/alp-daemon 0.0.0-stage → 0.0.1-beta.3
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/LICENSE +202 -0
- package/NOTICE +7 -0
- package/README.md +10 -2
- package/dist/THIRD-PARTY-NOTICES.txt +431 -0
- package/dist/alp-templates.d.ts +5 -0
- package/dist/alpd.js +42558 -0
- package/dist/build-manifest.json +111 -0
- package/dist/files.d.ts +110 -0
- package/dist/files.js +350 -0
- package/dist/gitignore.d.ts +57 -0
- package/dist/gitignore.js +248 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +8 -0
- package/dist/lock.d.ts +21 -0
- package/dist/lock.js +42 -0
- package/dist/main.d.ts +1 -0
- package/dist/main.js +271 -0
- package/dist/operations.d.ts +23 -0
- package/dist/operations.js +275 -0
- package/dist/rpc-error.d.ts +6 -0
- package/dist/rpc-error.js +11 -0
- package/dist/server.d.ts +48 -0
- package/dist/server.js +1158 -0
- package/dist/store.d.ts +108 -0
- package/dist/store.js +279 -0
- package/package.json +100 -4
- package/src/alp-templates.d.ts +5 -0
- package/src/files.ts +438 -0
- package/src/gitignore.ts +267 -0
- package/src/index.ts +5 -0
- package/src/lock.ts +53 -0
- package/src/main.ts +286 -0
- package/src/operations.ts +300 -0
- package/src/rpc-error.ts +4 -0
- package/src/server.ts +1332 -0
- package/src/store.ts +336 -0
package/src/gitignore.ts
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { accessSync, constants, lstatSync, realpathSync, statSync, type BigIntStats } from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Which paths of a workspace `.gitignore` hides, for workspace.files.list and workspace.files.read
|
|
7
|
+
* (docs/protocol/alp-json-rpc-v1.md §12.4), as Git itself says: `git check-ignore`, so its rules
|
|
8
|
+
* are Git's exactly (every `.gitignore` on the way, `.git/info/exclude`, `core.excludesFile`).
|
|
9
|
+
*
|
|
10
|
+
* The workspace is not trusted, so Git runs boxed in:
|
|
11
|
+
* - the Git found on the absolute entries of PATH, by its real path: never a `git` the workspace
|
|
12
|
+
* holds (a relative or empty PATH entry, or Windows searching the working directory first, would
|
|
13
|
+
* run one), whatever the spelling of the entry or a link on the way (see resolveGit);
|
|
14
|
+
* - spawned with an argument list, never a shell, in the workspace's real path;
|
|
15
|
+
* - `--no-index`: the index is not read (no fsmonitor or index extension runs; a tracked file a
|
|
16
|
+
* rule matches is hidden too), and `core.fsmonitor` is turned off besides;
|
|
17
|
+
* - an environment without any GIT_* variable of alpd's own, no prompts, no optional locks;
|
|
18
|
+
* - paths on stdin separated by NUL (`-z`), each as `./<path>` so that Git reads none as pathspec
|
|
19
|
+
* magic (`:(glob)…`, `:!…`) and none is ever an argument;
|
|
20
|
+
* - at most MAX_CONCURRENT at once in alpd, a bounded queue, a hard timeout that kills its whole
|
|
21
|
+
* process group, and a cap on what it may print.
|
|
22
|
+
* A timeout, a crash, a refusal, a full queue or an answer that is not plainly a list of the paths
|
|
23
|
+
* asked fails closed: the caller treats the paths as ignored. Git missing, or a workspace in no
|
|
24
|
+
* repository (no `.git` in it or above it): no rules apply, and only the block list hides anything.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
export const GIT_CHECK = Object.freeze({
|
|
28
|
+
/** Git gets this long per request, waiting for a turn included, then is killed. */
|
|
29
|
+
timeoutMs: 2_000,
|
|
30
|
+
/** Git processes alpd runs at once, and requests that may wait for one. */
|
|
31
|
+
maxConcurrent: 4,
|
|
32
|
+
maxQueued: 64,
|
|
33
|
+
maxErrorBytes: 64 * 1024,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
/** Git's answer could not be had: whether the paths are ignored is not known, so they count as ignored. */
|
|
37
|
+
export class IgnoreUnknown extends Error {
|
|
38
|
+
constructor(why: string) { super(`git check-ignore failed: ${why}`); }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type GitCheckOptions = { binary?: string; timeoutMs?: number };
|
|
42
|
+
|
|
43
|
+
/** alpd's environment without Git's variables (GIT_DIR, GIT_WORK_TREE, GIT_CONFIG_*…), which would point Git elsewhere. */
|
|
44
|
+
function gitEnvironment(): NodeJS.ProcessEnv {
|
|
45
|
+
const env: NodeJS.ProcessEnv = {};
|
|
46
|
+
for (const [key, value] of Object.entries(process.env)) if (!/^GIT_/i.test(key)) env[key] = value;
|
|
47
|
+
return { ...env, GIT_OPTIONAL_LOCKS: '0', GIT_TERMINAL_PROMPT: '0', LC_ALL: 'C' };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const executable = (file: string) => {
|
|
51
|
+
try {
|
|
52
|
+
if (!statSync(file).isFile()) return false;
|
|
53
|
+
if (process.platform !== 'win32') accessSync(file, constants.X_OK);
|
|
54
|
+
return true;
|
|
55
|
+
} catch {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/** The file system's identity of a directory: one per directory, whatever path names it. */
|
|
61
|
+
type Identity = Pick<BigIntStats, 'dev' | 'ino'>;
|
|
62
|
+
const identity = (file: string): Identity => { const { dev, ino } = statSync(file, { bigint: true }); return { dev, ino }; };
|
|
63
|
+
// macOS and Windows file systems compare names without case, mostly: a name compare there folds it.
|
|
64
|
+
const foldCase = process.platform === 'darwin' || process.platform === 'win32';
|
|
65
|
+
const comparable = (file: string) => (foldCase ? file.toLowerCase() : file);
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Whether `file` is `root` or inside it, however either is spelled: a case-insensitive file
|
|
69
|
+
* system's other case, a Windows drive letter's case or 8.3 short name, a link, a bind mount. Both
|
|
70
|
+
* are made real paths by the system (realpath(3), which spells names as they are stored), and then
|
|
71
|
+
* each directory from `file` up to the file system's root is compared with `root` by device and
|
|
72
|
+
* inode, and by its name (case-folded where names are, for a file system without inode numbers).
|
|
73
|
+
* Anything that cannot be resolved or examined counts as inside: the caller skips it.
|
|
74
|
+
*/
|
|
75
|
+
function insideRoot(file: string, root: { path: string; id: Identity }): boolean {
|
|
76
|
+
let directory;
|
|
77
|
+
try { directory = realpathSync.native(file); } catch { return true; }
|
|
78
|
+
const rootName = comparable(root.path.endsWith(path.sep) && root.path.length > 1 ? root.path.slice(0, -1) : root.path);
|
|
79
|
+
for (;;) {
|
|
80
|
+
const name = comparable(directory);
|
|
81
|
+
if (name === rootName || name.startsWith(rootName.endsWith(path.sep) ? rootName : `${rootName}${path.sep}`)) return true;
|
|
82
|
+
let id;
|
|
83
|
+
try { id = identity(directory); } catch { return true; }
|
|
84
|
+
if (root.id.ino !== 0n && id.dev === root.id.dev && id.ino === root.id.ino) return true;
|
|
85
|
+
const parent = path.dirname(directory);
|
|
86
|
+
if (parent === directory) return false;
|
|
87
|
+
directory = parent;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** The workspace as insideRoot compares it, or undefined if it cannot be examined. */
|
|
92
|
+
function rootOf(root: string): { path: string; id: Identity } | undefined {
|
|
93
|
+
try {
|
|
94
|
+
const real = realpathSync.native(root);
|
|
95
|
+
return { path: real, id: identity(real) };
|
|
96
|
+
} catch {
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* The absolute path of `name` on the absolute entries of PATH (Windows: with .exe or .com), or
|
|
103
|
+
* undefined. An absolute `name` (tests) is taken when it is an executable file. Relative and empty
|
|
104
|
+
* PATH entries are skipped: they name the working directory, which is the workspace.
|
|
105
|
+
*
|
|
106
|
+
* With `root`, nothing in the workspace is ever the answer (alpd started by npm or npx has
|
|
107
|
+
* `<project>/node_modules/.bin` on PATH): a PATH entry inside `root` is skipped, and so is a
|
|
108
|
+
* binary that is, or links to, a file inside it; both by insideRoot, which no other spelling of
|
|
109
|
+
* the path fools. The answer is then the binary's real path, so a link swapped later changes
|
|
110
|
+
* nothing. A workspace that cannot be examined finds no Git at all (IgnoreUnknown).
|
|
111
|
+
*/
|
|
112
|
+
export function resolveGit(name = 'git', searchPath = process.env.PATH ?? process.env.Path ?? '', root?: string): string | undefined {
|
|
113
|
+
const box = root === undefined ? undefined : rootOf(root);
|
|
114
|
+
if (root !== undefined && !box) throw new IgnoreUnknown('the workspace cannot be examined');
|
|
115
|
+
/** `file` as the answer, or undefined: not executable, or in the workspace. */
|
|
116
|
+
const accept = (file: string) => {
|
|
117
|
+
if (!executable(file)) return undefined;
|
|
118
|
+
if (!box) return file;
|
|
119
|
+
let real;
|
|
120
|
+
try { real = realpathSync.native(file); } catch { return undefined; }
|
|
121
|
+
return insideRoot(path.dirname(real), box) || !executable(real) ? undefined : real;
|
|
122
|
+
};
|
|
123
|
+
if (path.isAbsolute(name)) return accept(name);
|
|
124
|
+
if (name.includes('/') || name.includes('\\')) return undefined;
|
|
125
|
+
const names = process.platform === 'win32' ? [`${name}.exe`, `${name}.com`] : [name];
|
|
126
|
+
for (const directory of searchPath.split(path.delimiter)) {
|
|
127
|
+
if (!directory || !path.isAbsolute(directory)) continue;
|
|
128
|
+
if (box && insideRoot(directory, box)) continue;
|
|
129
|
+
for (const candidate of names) {
|
|
130
|
+
const file = accept(path.join(directory, candidate));
|
|
131
|
+
if (file) return file;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return undefined;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Whether `root` or a directory above it holds a `.git` entry: Git saying "not a git repository" there is a broken repository, not none. */
|
|
138
|
+
function hasGitEntry(root: string): boolean {
|
|
139
|
+
for (let directory = root; ; directory = path.dirname(directory)) {
|
|
140
|
+
try {
|
|
141
|
+
lstatSync(path.join(directory, '.git'));
|
|
142
|
+
return true;
|
|
143
|
+
} catch (error) {
|
|
144
|
+
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw new IgnoreUnknown('cannot examine .git');
|
|
145
|
+
}
|
|
146
|
+
if (path.dirname(directory) === directory) return false;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// The slots: at most maxConcurrent Git processes; others wait in a bounded queue.
|
|
151
|
+
let running = 0;
|
|
152
|
+
const waiting: Array<() => void> = [];
|
|
153
|
+
function acquire(deadline: number): Promise<() => void> {
|
|
154
|
+
const release = () => { running--; waiting.shift()?.(); };
|
|
155
|
+
if (running < GIT_CHECK.maxConcurrent) { running++; return Promise.resolve(release); }
|
|
156
|
+
if (waiting.length >= GIT_CHECK.maxQueued) return Promise.reject(new IgnoreUnknown('too many waiting'));
|
|
157
|
+
return new Promise((resolve, reject) => {
|
|
158
|
+
const turn = () => { clearTimeout(timer); running++; resolve(release); };
|
|
159
|
+
const timer = setTimeout(() => {
|
|
160
|
+
const index = waiting.indexOf(turn);
|
|
161
|
+
if (index >= 0) waiting.splice(index, 1);
|
|
162
|
+
reject(new IgnoreUnknown('timed out waiting for a turn'));
|
|
163
|
+
}, Math.max(0, deadline - Date.now()));
|
|
164
|
+
waiting.push(turn);
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** Git processes running now, for tests. */
|
|
169
|
+
export const gitChecksRunning = () => running;
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* The paths among `paths` (relative to `root`, '/'-separated, already checked) that Git ignores;
|
|
173
|
+
* null when no rules apply (Git is not installed, or `root` is in no repository). Throws
|
|
174
|
+
* IgnoreUnknown on anything else.
|
|
175
|
+
*/
|
|
176
|
+
export async function gitIgnored(root: string, paths: readonly string[], { binary = 'git', timeoutMs = GIT_CHECK.timeoutMs }: GitCheckOptions = {}): Promise<Set<string> | null> {
|
|
177
|
+
if (!paths.length) return new Set();
|
|
178
|
+
const git = resolveGit(binary, undefined, root);
|
|
179
|
+
if (!git) return null;
|
|
180
|
+
const deadline = Date.now() + timeoutMs;
|
|
181
|
+
const release = await acquire(deadline);
|
|
182
|
+
try {
|
|
183
|
+
return await check(git, root, paths, deadline, timeoutMs);
|
|
184
|
+
} finally {
|
|
185
|
+
release();
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function check(git: string, root: string, paths: readonly string[], deadline: number, timeoutMs: number): Promise<Set<string> | null> {
|
|
190
|
+
const input = `${paths.map(entry => `./${entry}`).join('\0')}\0`;
|
|
191
|
+
// Git echoes only what it was asked, so what it prints never exceeds what it was sent.
|
|
192
|
+
const maxOutputBytes = Buffer.byteLength(input) + 1024;
|
|
193
|
+
return new Promise((resolve, reject) => {
|
|
194
|
+
let settled = false;
|
|
195
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
196
|
+
let child: ReturnType<typeof spawn>;
|
|
197
|
+
// POSIX: its own process group, so a timeout kills whatever it started too.
|
|
198
|
+
const group = process.platform !== 'win32';
|
|
199
|
+
const kill = () => {
|
|
200
|
+
try {
|
|
201
|
+
if (group && child.pid) process.kill(-child.pid, 'SIGKILL');
|
|
202
|
+
else child.kill('SIGKILL');
|
|
203
|
+
} catch {
|
|
204
|
+
// already gone
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
const finish = (outcome: () => void) => {
|
|
208
|
+
if (settled) return;
|
|
209
|
+
settled = true;
|
|
210
|
+
if (timer) clearTimeout(timer);
|
|
211
|
+
outcome();
|
|
212
|
+
};
|
|
213
|
+
try {
|
|
214
|
+
child = spawn(git, ['-c', 'core.fsmonitor=false', 'check-ignore', '--no-index', '--stdin', '-z'], {
|
|
215
|
+
cwd: root, env: gitEnvironment(), shell: false, windowsHide: true, detached: group, stdio: ['pipe', 'pipe', 'pipe'],
|
|
216
|
+
});
|
|
217
|
+
} catch (error) {
|
|
218
|
+
reject(new IgnoreUnknown(String((error as Error).message)));
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
timer = setTimeout(() => finish(() => { kill(); reject(new IgnoreUnknown('timed out')); }), Math.max(1, Math.min(timeoutMs, deadline - Date.now())));
|
|
222
|
+
const out: Buffer[] = [];
|
|
223
|
+
let outBytes = 0;
|
|
224
|
+
let err = '';
|
|
225
|
+
child.stdout!.on('data', (chunk: Buffer) => {
|
|
226
|
+
outBytes += chunk.length;
|
|
227
|
+
if (outBytes > maxOutputBytes) finish(() => { kill(); reject(new IgnoreUnknown('more output than input')); });
|
|
228
|
+
else out.push(chunk);
|
|
229
|
+
});
|
|
230
|
+
child.stderr!.on('data', (chunk: Buffer) => { if (err.length < GIT_CHECK.maxErrorBytes) err += chunk.toString('utf8'); });
|
|
231
|
+
child.on('error', error => finish(() => {
|
|
232
|
+
// Git was resolved already. ENOENT can also mean a missing cwd or interpreter;
|
|
233
|
+
// it does not establish that no ignore rules apply.
|
|
234
|
+
reject(new IgnoreUnknown(error.message));
|
|
235
|
+
}));
|
|
236
|
+
child.on('close', code => finish(() => {
|
|
237
|
+
// No repository: no rules. A `.git` that Git cannot use (a worktree whose repository moved)
|
|
238
|
+
// is not "no repository": fail closed.
|
|
239
|
+
if (code === 128 && /not a git repository/i.test(err)) {
|
|
240
|
+
try {
|
|
241
|
+
if (hasGitEntry(root)) reject(new IgnoreUnknown('a .git Git cannot use'));
|
|
242
|
+
else resolve(null);
|
|
243
|
+
} catch (error) { reject(error); }
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
// 0: some paths are ignored; 1: none is.
|
|
247
|
+
if (code !== 0 && code !== 1) { reject(new IgnoreUnknown(`exit ${code}`)); return; }
|
|
248
|
+
if (err.length || (code === 1 && outBytes) || (code === 0 && !outBytes)) {
|
|
249
|
+
reject(new IgnoreUnknown('inconsistent exit status or diagnostics')); return;
|
|
250
|
+
}
|
|
251
|
+
const output = Buffer.concat(out).toString('utf8');
|
|
252
|
+
if (outBytes && !output.endsWith('\0')) { reject(new IgnoreUnknown('unterminated answer')); return; }
|
|
253
|
+
const asked = new Set(paths);
|
|
254
|
+
const ignored = new Set<string>();
|
|
255
|
+
for (const entry of output.split('\0')) {
|
|
256
|
+
if (!entry) continue;
|
|
257
|
+
const relative = entry.startsWith('./') ? entry.slice(2) : undefined;
|
|
258
|
+
if (relative === undefined || !asked.has(relative)) { reject(new IgnoreUnknown('an answer for a path not asked')); return; }
|
|
259
|
+
ignored.add(relative);
|
|
260
|
+
}
|
|
261
|
+
resolve(ignored);
|
|
262
|
+
}));
|
|
263
|
+
// A Git that exits before reading everything closes the pipe: its exit decides.
|
|
264
|
+
child.stdin!.on('error', () => {});
|
|
265
|
+
child.stdin!.end(input);
|
|
266
|
+
});
|
|
267
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// alpd's building blocks, for embedding and tests; src/daemon/main.ts is the daemon itself.
|
|
2
|
+
export { createDaemonServer, ERROR } from './server.js';
|
|
3
|
+
export type { DaemonConnection, DaemonServer, SessionSummary } from './server.js';
|
|
4
|
+
export { createStore } from './store.js';
|
|
5
|
+
export type { SessionRecord, SessionStatus, Store } from './store.js';
|
package/src/lock.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { unlink, utimes, writeFile, rename } from 'node:fs/promises';
|
|
2
|
+
import { acquireDaemonLock, daemonPaths, readLock } from '@anhlp/alp-client/index';
|
|
3
|
+
|
|
4
|
+
export type LockContents = {
|
|
5
|
+
pid: number;
|
|
6
|
+
startedAt: string;
|
|
7
|
+
bootTime: number;
|
|
8
|
+
uid: number;
|
|
9
|
+
version: string;
|
|
10
|
+
protocolVersion: number;
|
|
11
|
+
socket: string;
|
|
12
|
+
ready: boolean;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/** Single-instance lock (Paseo acquirePidLock, simplified): exclusive create, stale when its pid or boot is gone. */
|
|
16
|
+
export async function acquireLock(home: string, contents: Omit<LockContents, 'pid' | 'startedAt' | 'bootTime' | 'uid' | 'ready'>): Promise<LockContents> {
|
|
17
|
+
return acquireDaemonLock(home, contents) as Promise<LockContents>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Moves `temporary` over `file`. On Windows the move fails with EPERM, EACCES or EBUSY while
|
|
22
|
+
* another process has `file` open, such as a client reading the lock; it is tried again then.
|
|
23
|
+
*/
|
|
24
|
+
export async function replaceFile(temporary: string, file: string) {
|
|
25
|
+
for (let attempt = 0; ; attempt++) {
|
|
26
|
+
try { return await rename(temporary, file); }
|
|
27
|
+
catch (error: any) {
|
|
28
|
+
if (process.platform !== 'win32' || attempt >= 20 || !['EPERM', 'EACCES', 'EBUSY'].includes(error?.code)) throw error;
|
|
29
|
+
await new Promise(resolve => setTimeout(resolve, 10 + attempt * 10));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export async function updateLock(home: string, lock: LockContents) {
|
|
35
|
+
const file = daemonPaths(home).lock;
|
|
36
|
+
const temporary = `${file}.${process.pid}.tmp`;
|
|
37
|
+
await writeFile(temporary, JSON.stringify(lock), { mode: 0o600 });
|
|
38
|
+
await replaceFile(temporary, file);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** True while this process still owns the lock; refreshes its mtime as a heartbeat. */
|
|
42
|
+
export async function heartbeat(home: string) {
|
|
43
|
+
const current = await readLock(home);
|
|
44
|
+
if (current?.pid !== process.pid) return false;
|
|
45
|
+
const now = new Date();
|
|
46
|
+
await utimes(daemonPaths(home).lock, now, now).catch(() => {});
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export async function releaseLock(home: string) {
|
|
51
|
+
const current = await readLock(home);
|
|
52
|
+
if (current?.pid === process.pid) await unlink(daemonPaths(home).lock).catch(() => {});
|
|
53
|
+
}
|
package/src/main.ts
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { openSync, writeSync } from 'node:fs';
|
|
3
|
+
import { mkdir, open, readFile, rename, stat, unlink, utimes, writeFile } from 'node:fs/promises';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import templates from 'alp:templates';
|
|
7
|
+
import { alpHome, daemonPaths, assertPrivateDirectory, PROTOCOL_VERSION, withDaemonLifecycle } from '@anhlp/alp-client/index';
|
|
8
|
+
import { createAlpRuntime, reclaimCopies, reclaimWorktrees } from '@anhlp/alp-runtime/index';
|
|
9
|
+
import { createDaemonServer } from './server.js';
|
|
10
|
+
import { acquireLock, heartbeat, releaseLock, updateLock } from './lock.js';
|
|
11
|
+
import { createStore } from './store.js';
|
|
12
|
+
import { validateUserSettings } from '@anhlp/alp-core/validation';
|
|
13
|
+
import { clearRestoredMarker, readRestoredMarker, recoverInterruptedRestore } from '@anhlp/alp-core/backup';
|
|
14
|
+
import { migrateState } from '@anhlp/alp-core/migrations';
|
|
15
|
+
import { createLogger } from '@anhlp/alp-core/log';
|
|
16
|
+
|
|
17
|
+
declare const __ALP_VERSION__: string;
|
|
18
|
+
const VERSION = typeof __ALP_VERSION__ === 'string' ? __ALP_VERSION__ : '0.0.0-dev';
|
|
19
|
+
const LOG_BYTES = 10 * 1024 * 1024;
|
|
20
|
+
const LOG_FILES = 3;
|
|
21
|
+
const log = createLogger({ component: 'alpd' });
|
|
22
|
+
|
|
23
|
+
async function rotate(log: string) {
|
|
24
|
+
const size = await stat(log).then(info => info.size, () => 0);
|
|
25
|
+
if (size < LOG_BYTES) return;
|
|
26
|
+
for (let index = LOG_FILES - 1; index >= 1; index--) {
|
|
27
|
+
await rename(index === 1 ? log : `${log}.${index - 1}`, `${log}.${index}`).catch(() => {});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The launcher owns its detached child until that child acknowledges readiness or exits.
|
|
32
|
+
* A ready lock belonging to some other process cannot settle this launch.
|
|
33
|
+
*/
|
|
34
|
+
async function detach(home: string) {
|
|
35
|
+
await assertPrivateDirectory(home, { create: true });
|
|
36
|
+
const { log } = daemonPaths(home);
|
|
37
|
+
await assertPrivateDirectory(path.dirname(log), { create: true });
|
|
38
|
+
await rotate(log);
|
|
39
|
+
const output = await open(log, 'a', 0o600);
|
|
40
|
+
const child = spawn(process.execPath, [fileURLToPath(import.meta.url)], {
|
|
41
|
+
detached: true,
|
|
42
|
+
stdio: ['ignore', output.fd, output.fd, 'ipc'],
|
|
43
|
+
env: process.env,
|
|
44
|
+
windowsHide: true,
|
|
45
|
+
});
|
|
46
|
+
await output.close();
|
|
47
|
+
await new Promise<void>((resolve, reject) => {
|
|
48
|
+
const requested = Number(process.argv.find(arg => arg.startsWith('--startup-timeout='))?.split('=')[1]);
|
|
49
|
+
const timeoutMs = Number.isFinite(requested) && requested > 0 ? requested : 15_000;
|
|
50
|
+
let timedOut = false;
|
|
51
|
+
let force: ReturnType<typeof setTimeout> | undefined;
|
|
52
|
+
const timer = setTimeout(() => {
|
|
53
|
+
timedOut = true;
|
|
54
|
+
child.kill();
|
|
55
|
+
force = setTimeout(() => child.kill('SIGKILL'), 1_000);
|
|
56
|
+
}, timeoutMs);
|
|
57
|
+
child.once('error', error => { clearTimeout(timer); clearTimeout(force); reject(error); });
|
|
58
|
+
child.once('exit', (code, signal) => {
|
|
59
|
+
clearTimeout(timer);
|
|
60
|
+
clearTimeout(force);
|
|
61
|
+
reject(new Error(timedOut ? `alpd startup timed out after ${timeoutMs} ms` : `alpd exited during startup (${code ?? signal})`));
|
|
62
|
+
});
|
|
63
|
+
child.once('message', message => {
|
|
64
|
+
if (message !== 'alpd.ready' || timedOut) return;
|
|
65
|
+
clearTimeout(timer);
|
|
66
|
+
child.disconnect();
|
|
67
|
+
child.unref();
|
|
68
|
+
resolve();
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Under a service manager (ALPD §37), which holds alpd's stdout open: alpd rotates its
|
|
75
|
+
* log at start as the detached launcher does, then writes its output there itself. The
|
|
76
|
+
* manager's own file keeps only what Node prints when it dies.
|
|
77
|
+
*/
|
|
78
|
+
async function serviceLog(home: string) {
|
|
79
|
+
await assertPrivateDirectory(home, { create: true });
|
|
80
|
+
const { log } = daemonPaths(home);
|
|
81
|
+
await assertPrivateDirectory(path.dirname(log), { create: true });
|
|
82
|
+
await rotate(log);
|
|
83
|
+
const fd = openSync(log, 'a', 0o600);
|
|
84
|
+
for (const stream of [process.stdout, process.stderr]) {
|
|
85
|
+
stream.write = ((chunk: string | Uint8Array, encoding?: unknown, callback?: unknown) => {
|
|
86
|
+
try { if (typeof chunk === 'string') writeSync(fd, chunk); else writeSync(fd, chunk); } catch {}
|
|
87
|
+
const done = typeof encoding === 'function' ? encoding : callback;
|
|
88
|
+
if (typeof done === 'function') done();
|
|
89
|
+
return true;
|
|
90
|
+
}) as typeof stream.write;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The user's alpd settings in $ALP_HOME/settings.json; none when it is missing.
|
|
96
|
+
* Invalid settings are logged and alpd starts with the defaults, so a typo never
|
|
97
|
+
* keeps it from running; alp doctor reports them too.
|
|
98
|
+
*/
|
|
99
|
+
async function userSettings(home: string): Promise<any> {
|
|
100
|
+
const file = path.join(home, 'settings.json');
|
|
101
|
+
let text: string;
|
|
102
|
+
try { text = await readFile(file, 'utf8'); } catch { return {}; }
|
|
103
|
+
try { return validateUserSettings(JSON.parse(text), file); }
|
|
104
|
+
catch (error: any) {
|
|
105
|
+
log.warn(`ignoring ${file}: ${error?.message ?? error}`, { code: 'ALP_SETTINGS_INVALID' });
|
|
106
|
+
return {};
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* How the previous alpd ended (ALPD §31). A running alpd keeps a marker file and
|
|
112
|
+
* touches it with its heartbeat; a clean stop removes it last. A marker found at
|
|
113
|
+
* start means the previous alpd crashed, around the marker's last touch.
|
|
114
|
+
*/
|
|
115
|
+
async function previousExit(marker: string): Promise<{ kind: 'clean' | 'crash'; at?: string }> {
|
|
116
|
+
const info = await stat(marker).catch(() => undefined);
|
|
117
|
+
return info ? { kind: 'crash', at: info.mtime.toISOString() } : { kind: 'clean' };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Whether a process group `id` exists; on POSIX a pid is never reused while its group lives. */
|
|
121
|
+
function groupAlive(id: number) {
|
|
122
|
+
try { process.kill(-id, 0); return true; } catch (error: any) { return error?.code === 'EPERM'; }
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** alpd leads its own process group when started detached or by a service manager. */
|
|
126
|
+
const leadsGroup = () => process.platform !== 'win32' && groupAlive(process.pid);
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Stops what a crashed alpd left running (ALPD §66): its provider processes normally exit
|
|
130
|
+
* when their stdin closes, but a stuck one, or a command an agent started, can outlive it.
|
|
131
|
+
* They stay in the crashed alpd's process group, so alpd ends that group when the crashed
|
|
132
|
+
* one led its group and this one leads its own (so it is not a member). Not on Windows.
|
|
133
|
+
*/
|
|
134
|
+
async function stopLeftovers(previous: { pid?: number; groupLeader?: boolean } | undefined) {
|
|
135
|
+
const pid = previous?.pid;
|
|
136
|
+
if (!previous?.groupLeader || !Number.isSafeInteger(pid) || pid === process.pid || !leadsGroup() || !groupAlive(pid!)) return;
|
|
137
|
+
log.warn(`stopping processes the previous alpd (pid ${pid}) left running`, { code: 'ALP_LEFTOVER_PROCESSES', previousPid: pid });
|
|
138
|
+
const signal = (name: NodeJS.Signals) => { try { process.kill(-pid!, name); } catch {} };
|
|
139
|
+
signal('SIGTERM');
|
|
140
|
+
for (let waited = 0; waited < 3000 && groupAlive(pid!); waited += 50) await new Promise(resolve => setTimeout(resolve, 50));
|
|
141
|
+
if (groupAlive(pid!)) signal('SIGKILL');
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Where start-up stands, so a failure says which step it stopped at. */
|
|
145
|
+
type Phase = 'home' | 'lock' | 'state' | 'recovery' | 'listen' | 'ready';
|
|
146
|
+
let phase: Phase = 'home';
|
|
147
|
+
const STARTUP_CODES = { home: 'ALP_HOME_UNUSABLE', lock: 'ALP_LOCK_FAILED', state: 'ALP_STATE_UNUSABLE', recovery: 'ALP_STARTUP_FAILED', listen: 'ALP_IPC_LISTEN_FAILED', ready: 'ALP_STARTUP_FAILED' } as const;
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Before anything reads or writes the state (ALPD §68): a restore cut short is settled, a
|
|
151
|
+
* state of a newer schema is refused untouched, an older one is migrated forward, and a
|
|
152
|
+
* restore's marker says to continue no work by itself this time; it is cleared once alpd is
|
|
153
|
+
* ready, so a start that fails keeps it for the next.
|
|
154
|
+
*/
|
|
155
|
+
async function openState(home: string) {
|
|
156
|
+
const settled = await recoverInterruptedRestore(home);
|
|
157
|
+
if (settled) log.warn(`a restore from ${settled.file} was cut short and has been ${settled.outcome}`, { code: 'ALP_RESTORE_SETTLED', outcome: settled.outcome });
|
|
158
|
+
const migrated = await migrateState(home, { by: `alpd ${VERSION}`, log });
|
|
159
|
+
for (const step of migrated.steps) log.info(`state migrated from schema ${step.from} to ${step.to} (${step.name}); the state before it is in ${step.backup}`, { code: 'ALP_STATE_MIGRATED', from: step.from, to: step.to, migration: step.name });
|
|
160
|
+
const restored = await readRestoredMarker(home);
|
|
161
|
+
if (restored) log.info(`state restored${restored.from ? ` from ${restored.from}` : ''}; sessions that were working wait for the user`, { code: 'ALP_STATE_RESTORED' });
|
|
162
|
+
return { restored: !!restored };
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async function run(home: string) {
|
|
166
|
+
await assertPrivateDirectory(home, { create: true });
|
|
167
|
+
phase = 'lock';
|
|
168
|
+
const { socket } = daemonPaths(home);
|
|
169
|
+
let lock;
|
|
170
|
+
try {
|
|
171
|
+
lock = await acquireLock(home, { version: VERSION, protocolVersion: PROTOCOL_VERSION, socket });
|
|
172
|
+
} catch (error: any) {
|
|
173
|
+
// Under a service manager, another alpd serving this home is no failure to retry.
|
|
174
|
+
if (process.argv.includes('--service') && /already running|ALP is busy/.test(error?.message ?? '')) {
|
|
175
|
+
log.info(`${error.message}; this one exits`, { code: 'ALP_LOCK_HELD' });
|
|
176
|
+
process.exit(0);
|
|
177
|
+
}
|
|
178
|
+
throw error;
|
|
179
|
+
}
|
|
180
|
+
phase = 'state';
|
|
181
|
+
let opened;
|
|
182
|
+
try {
|
|
183
|
+
opened = await openState(home);
|
|
184
|
+
} catch (error) {
|
|
185
|
+
await releaseLock(home);
|
|
186
|
+
throw error;
|
|
187
|
+
}
|
|
188
|
+
phase = 'recovery';
|
|
189
|
+
// First: nothing a crashed alpd left running may still write while this one recovers.
|
|
190
|
+
const marker = path.join(home, 'state', 'alpd.running');
|
|
191
|
+
const exit = await previousExit(marker);
|
|
192
|
+
if (exit.kind === 'crash') {
|
|
193
|
+
log.warn(`the previous alpd stopped unexpectedly around ${exit.at}`, { code: 'ALP_PREVIOUS_CRASH', at: exit.at });
|
|
194
|
+
await stopLeftovers(await readFile(marker, 'utf8').then(JSON.parse).catch(() => undefined));
|
|
195
|
+
}
|
|
196
|
+
await mkdir(path.dirname(marker), { recursive: true, mode: 0o700 });
|
|
197
|
+
await writeFile(marker, JSON.stringify({ pid: process.pid, startedAt: new Date().toISOString(), groupLeader: leadsGroup() }), { mode: 0o600 });
|
|
198
|
+
const runLogDir = process.env.ALP_RUN_LOG_DIR || path.join(home, 'runs');
|
|
199
|
+
const worktreeDir = path.join(home, 'worktrees');
|
|
200
|
+
// Worktrees of assignments a crash interrupted: their work goes to their branches.
|
|
201
|
+
for (const branch of await reclaimWorktrees(worktreeDir).catch(() => [] as string[])) log.info(`kept interrupted work on branch ${branch}`, { event: 'worktree.kept', branch });
|
|
202
|
+
// Review copies of interrupted assignments hold nothing to keep.
|
|
203
|
+
const copyDir = path.join(home, 'copies');
|
|
204
|
+
await reclaimCopies(copyDir).catch(() => 0);
|
|
205
|
+
// Requested starts lift the existing hold before launching. Never erase a stop
|
|
206
|
+
// published while this daemon was starting.
|
|
207
|
+
const settings = await userSettings(home);
|
|
208
|
+
const runtime = createAlpRuntime({
|
|
209
|
+
templates, runLogDir, worktreeDir, copyDir, boardDir: path.join(home, 'boards'), libraryDir: home,
|
|
210
|
+
recallFile: path.join(home, 'state', 'recall.json'), pauseFile: path.join(home, 'state', 'pause.json'), liveFile: path.join(home, 'state', 'live.json'),
|
|
211
|
+
autoResume: settings?.limits?.autoResume !== false, recoveryResume: settings?.recovery?.autoResume !== false && !opened.restored, previousExit: exit,
|
|
212
|
+
});
|
|
213
|
+
const store = createStore(path.join(home, 'state'));
|
|
214
|
+
let stopping: Promise<void> | undefined;
|
|
215
|
+
const shutdown = (code = 0) => {
|
|
216
|
+
stopping ??= (async () => {
|
|
217
|
+
const force = setTimeout(() => process.exit(code), 10_000);
|
|
218
|
+
force.unref();
|
|
219
|
+
clearInterval(beat);
|
|
220
|
+
await server.close().catch(() => {});
|
|
221
|
+
await runtime.shutdown().catch(() => {});
|
|
222
|
+
await store.flush();
|
|
223
|
+
// Last: what remains is a clean stop.
|
|
224
|
+
await unlink(marker).catch(() => {});
|
|
225
|
+
await releaseLock(home);
|
|
226
|
+
log.info('alpd stopped', { event: 'stopped', exitCode: code });
|
|
227
|
+
process.exit(code);
|
|
228
|
+
})();
|
|
229
|
+
return stopping;
|
|
230
|
+
};
|
|
231
|
+
const server = createDaemonServer({ runtime, socketPath: socket, version: VERSION, store, runLogDir, previousExit: exit, home, templates, onShutdown: () => void shutdown() });
|
|
232
|
+
const beat = setInterval(() => {
|
|
233
|
+
void heartbeat(home).then(owned => { if (!owned) void shutdown(1); });
|
|
234
|
+
const now = new Date();
|
|
235
|
+
void utimes(marker, now, now).catch(() => {});
|
|
236
|
+
}, 30_000);
|
|
237
|
+
beat.unref();
|
|
238
|
+
for (const signal of ['SIGINT', 'SIGTERM'] as const) process.on(signal, () => void shutdown());
|
|
239
|
+
process.on('uncaughtException', error => { log.error('uncaught exception; alpd stops', { code: 'ALP_FATAL', error }); void shutdown(1); });
|
|
240
|
+
process.on('unhandledRejection', error => { log.error('unhandled rejection; alpd stops', { code: 'ALP_FATAL', error }); void shutdown(1); });
|
|
241
|
+
phase = 'listen';
|
|
242
|
+
try {
|
|
243
|
+
await server.listen();
|
|
244
|
+
} catch (error) {
|
|
245
|
+
await releaseLock(home);
|
|
246
|
+
throw error;
|
|
247
|
+
}
|
|
248
|
+
phase = 'ready';
|
|
249
|
+
// Web app and relay are owned by the optional Paseo integration process.
|
|
250
|
+
await updateLock(home, { ...lock, ready: true });
|
|
251
|
+
// Clients that cannot locate alpd themselves (the Paseo plugin) start it from here next time.
|
|
252
|
+
const { install } = daemonPaths(home);
|
|
253
|
+
await writeFile(`${install}.tmp`, JSON.stringify({ entry: fileURLToPath(import.meta.url), version: VERSION }), { mode: 0o600 })
|
|
254
|
+
.then(() => rename(`${install}.tmp`, install))
|
|
255
|
+
.catch(error => log.warn('could not record alpd\'s location', { code: 'ALP_INSTALL_RECORD_FAILED', error }));
|
|
256
|
+
if (opened.restored) await clearRestoredMarker(home).catch(error => log.warn('could not clear the restore marker', { code: 'ALP_STATE_RESTORED', error }));
|
|
257
|
+
if (process.connected) process.send?.('alpd.ready');
|
|
258
|
+
log.info(`alpd ${VERSION} ready on ${socket} (pid ${process.pid})`, { event: 'ready', version: VERSION, pid: process.pid });
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const home = alpHome();
|
|
262
|
+
try {
|
|
263
|
+
if (process.argv.includes('--detach')) await detach(home);
|
|
264
|
+
else {
|
|
265
|
+
// A direct foreground/login invocation is an explicit start. Do this before
|
|
266
|
+
// admission registration, so a stop published during startup is preserved.
|
|
267
|
+
if (!process.connected) await unlink(path.join(home, 'state', 'alpd.held')).catch(() => {});
|
|
268
|
+
if (process.argv.includes('--service')) await serviceLog(home);
|
|
269
|
+
if (process.connected) await run(home);
|
|
270
|
+
else {
|
|
271
|
+
// Retain admission through readiness even if the requesting service client
|
|
272
|
+
// times out. An invocation without a requester (including login) is explicit.
|
|
273
|
+
await withDaemonLifecycle(home, 'start', async () => {
|
|
274
|
+
await run(home);
|
|
275
|
+
}, { joinStart: true });
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
} catch (error: any) {
|
|
279
|
+
// The log says where start-up stopped; alp doctor reads it from there.
|
|
280
|
+
const stoppedIn = phase as Phase;
|
|
281
|
+
// Opening the state fails with the state's own code (a newer schema, a failed migration).
|
|
282
|
+
const code = /already running|ALP is busy/.test(error?.message ?? '') ? 'ALP_LOCK_HELD'
|
|
283
|
+
: stoppedIn === 'state' && typeof error?.code === 'string' && /^[A-Z_]+$/.test(error.code) ? `ALP_${error.code}` : STARTUP_CODES[stoppedIn];
|
|
284
|
+
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 });
|
|
285
|
+
process.exit(1);
|
|
286
|
+
}
|