@anhlp/alp-daemon 0.0.0-stage → 0.0.1-beta.10
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 +14 -2
- package/dist/THIRD-PARTY-NOTICES.txt +456 -0
- package/dist/alp-templates.d.ts +5 -0
- package/dist/alpd.js +64050 -0
- package/dist/attachments.d.ts +187 -0
- package/dist/attachments.js +856 -0
- package/dist/build-manifest.json +150 -0
- package/dist/conversation.d.ts +119 -0
- package/dist/conversation.js +688 -0
- package/dist/disk.d.ts +67 -0
- package/dist/disk.js +152 -0
- package/dist/files.d.ts +262 -0
- package/dist/files.js +674 -0
- package/dist/git.d.ts +98 -0
- package/dist/git.js +1246 -0
- package/dist/gitignore.d.ts +62 -0
- package/dist/gitignore.js +254 -0
- package/dist/history.d.ts +24 -0
- package/dist/history.js +149 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +28 -0
- package/dist/lifecycle.d.ts +101 -0
- package/dist/lifecycle.js +239 -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 +363 -0
- package/dist/model-catalog.d.ts +2 -0
- package/dist/model-catalog.js +44 -0
- package/dist/native-entry.d.ts +2 -0
- package/dist/native-entry.js +3 -0
- package/dist/native-register.d.ts +1 -0
- package/dist/native-register.js +13 -0
- package/dist/native-terminal.d.ts +45 -0
- package/dist/native-terminal.js +201 -0
- package/dist/operations.d.ts +40 -0
- package/dist/operations.js +313 -0
- package/dist/registry.d.ts +89 -0
- package/dist/registry.js +343 -0
- package/dist/rpc-error.d.ts +6 -0
- package/dist/rpc-error.js +11 -0
- package/dist/scheduler.d.ts +96 -0
- package/dist/scheduler.js +298 -0
- package/dist/schedules.d.ts +70 -0
- package/dist/schedules.js +246 -0
- package/dist/server.d.ts +60 -0
- package/dist/server.js +1958 -0
- package/dist/store.d.ts +178 -0
- package/dist/store.js +386 -0
- package/dist/terminal-native/README.md +201 -0
- package/dist/terminal-native/prebuilt/darwin-arm64/manifest.json +11 -0
- package/dist/terminal-native/prebuilt/darwin-arm64/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/darwin-x64/manifest.json +11 -0
- package/dist/terminal-native/prebuilt/darwin-x64/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-arm64-glibc/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-arm64-glibc/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-arm64-musl/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-arm64-musl/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-x64-glibc/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-x64-glibc/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-x64-musl/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-x64-musl/terminal-owner +0 -0
- package/dist/terminal-native/terminal-owner.c +389 -0
- package/dist/terminal-screen.d.ts +24 -0
- package/dist/terminal-screen.js +99 -0
- package/dist/terminals.d.ts +203 -0
- package/dist/terminals.js +570 -0
- package/dist/windows-terminal.d.ts +32 -0
- package/dist/windows-terminal.js +79 -0
- package/dist/worktrees.d.ts +65 -0
- package/dist/worktrees.js +318 -0
- package/native/README.md +201 -0
- package/native/prebuilt/darwin-arm64/manifest.json +11 -0
- package/native/prebuilt/darwin-arm64/terminal-owner +0 -0
- package/native/prebuilt/darwin-x64/manifest.json +11 -0
- package/native/prebuilt/darwin-x64/terminal-owner +0 -0
- package/native/prebuilt/linux-arm64-glibc/manifest.json +10 -0
- package/native/prebuilt/linux-arm64-glibc/terminal-owner +0 -0
- package/native/prebuilt/linux-arm64-musl/manifest.json +10 -0
- package/native/prebuilt/linux-arm64-musl/terminal-owner +0 -0
- package/native/prebuilt/linux-x64-glibc/manifest.json +10 -0
- package/native/prebuilt/linux-x64-glibc/terminal-owner +0 -0
- package/native/prebuilt/linux-x64-musl/manifest.json +10 -0
- package/native/prebuilt/linux-x64-musl/terminal-owner +0 -0
- package/native/terminal-owner.c +389 -0
- package/package.json +163 -4
- package/src/alp-templates.d.ts +5 -0
- package/src/attachments.ts +908 -0
- package/src/conversation.ts +857 -0
- package/src/disk.ts +175 -0
- package/src/files.ts +884 -0
- package/src/git.ts +1220 -0
- package/src/gitignore.ts +277 -0
- package/src/history.ts +118 -0
- package/src/index.ts +12 -0
- package/src/lifecycle.ts +283 -0
- package/src/lock.ts +53 -0
- package/src/main.ts +381 -0
- package/src/model-catalog.ts +36 -0
- package/src/native-entry.ts +5 -0
- package/src/native-register.ts +21 -0
- package/src/native-terminal.ts +127 -0
- package/src/operations.ts +329 -0
- package/src/registry.ts +238 -0
- package/src/rpc-error.ts +4 -0
- package/src/scheduler.ts +438 -0
- package/src/schedules.ts +297 -0
- package/src/server.ts +2238 -0
- package/src/store.ts +449 -0
- package/src/terminal-screen.ts +74 -0
- package/src/terminals.ts +653 -0
- package/src/windows-terminal.ts +74 -0
- package/src/worktrees.ts +382 -0
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which paths of a workspace `.gitignore` hides, for workspace.files.list and workspace.files.read
|
|
3
|
+
* (docs/protocol/alp-json-rpc-v1.md §12.4), as Git itself says: `git check-ignore`, so its rules
|
|
4
|
+
* are Git's exactly (every `.gitignore` on the way, `.git/info/exclude`, `core.excludesFile`).
|
|
5
|
+
*
|
|
6
|
+
* The workspace is not trusted, so Git runs boxed in:
|
|
7
|
+
* - the Git found on the absolute entries of PATH, by its real path: never a `git` the workspace
|
|
8
|
+
* holds (a relative or empty PATH entry, or Windows searching the working directory first, would
|
|
9
|
+
* run one), whatever the spelling of the entry or a link on the way (see resolveGit);
|
|
10
|
+
* - spawned with an argument list, never a shell, in the workspace's real path;
|
|
11
|
+
* - `--no-index`: the index is not read (no fsmonitor or index extension runs; a tracked file a
|
|
12
|
+
* rule matches is hidden too), and `core.fsmonitor` is turned off besides;
|
|
13
|
+
* - an environment without any GIT_* variable of alpd's own, no prompts, no optional locks;
|
|
14
|
+
* - paths on stdin separated by NUL (`-z`), each as `./<path>` so that Git reads none as pathspec
|
|
15
|
+
* magic (`:(glob)…`, `:!…`) and none is ever an argument;
|
|
16
|
+
* - at most MAX_CONCURRENT at once in alpd, a bounded queue, a hard timeout that kills its whole
|
|
17
|
+
* process group, and a cap on what it may print.
|
|
18
|
+
* A timeout, a crash, a refusal, a full queue or an answer that is not plainly a list of the paths
|
|
19
|
+
* asked fails closed: the caller treats the paths as ignored. Git missing, or a workspace in no
|
|
20
|
+
* repository (no `.git` in it or above it): no rules apply, and only the block list hides anything.
|
|
21
|
+
*/
|
|
22
|
+
export declare const GIT_CHECK: Readonly<{
|
|
23
|
+
/** Git gets this long per request, waiting for a turn included, then is killed. */
|
|
24
|
+
timeoutMs: 2000;
|
|
25
|
+
/** Git processes alpd runs at once, and requests that may wait for one. */
|
|
26
|
+
maxConcurrent: 4;
|
|
27
|
+
maxQueued: 64;
|
|
28
|
+
maxErrorBytes: number;
|
|
29
|
+
}>;
|
|
30
|
+
/** Git's answer could not be had: whether the paths are ignored is not known, so they count as ignored. */
|
|
31
|
+
export declare class IgnoreUnknown extends Error {
|
|
32
|
+
constructor(why: string);
|
|
33
|
+
}
|
|
34
|
+
export type GitCheckOptions = {
|
|
35
|
+
binary?: string;
|
|
36
|
+
timeoutMs?: number;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* The absolute path of `name` on the absolute entries of PATH (Windows: with .exe or .com), or
|
|
40
|
+
* undefined. An absolute `name` (tests) is taken when it is an executable file. Relative and empty
|
|
41
|
+
* PATH entries are skipped: they name the working directory, which is the workspace.
|
|
42
|
+
*
|
|
43
|
+
* With `root`, nothing in the workspace is ever the answer (alpd started by npm or npx has
|
|
44
|
+
* `<project>/node_modules/.bin` on PATH): a PATH entry inside `root` is skipped, and so is a
|
|
45
|
+
* binary that is, or links to, a file inside it; both by insideRoot, which no other spelling of
|
|
46
|
+
* the path fools. The answer is then the binary's real path, so a link swapped later changes
|
|
47
|
+
* nothing. A workspace that cannot be examined finds no Git at all (IgnoreUnknown).
|
|
48
|
+
*/
|
|
49
|
+
export declare function resolveGit(name?: string, searchPath?: string, root?: string): string | undefined;
|
|
50
|
+
/**
|
|
51
|
+
* The absolute entries of PATH outside `root` (insideRoot), for the environment of a Git alpd runs
|
|
52
|
+
* in a workspace: whatever Git itself starts is then never found in the workspace either.
|
|
53
|
+
*/
|
|
54
|
+
export declare function searchPathOutside(root: string, searchPath?: string): string;
|
|
55
|
+
/** Git processes running now, for tests. */
|
|
56
|
+
export declare const gitChecksRunning: () => number;
|
|
57
|
+
/**
|
|
58
|
+
* The paths among `paths` (relative to `root`, '/'-separated, already checked) that Git ignores;
|
|
59
|
+
* null when no rules apply (Git is not installed, or `root` is in no repository). Throws
|
|
60
|
+
* IgnoreUnknown on anything else.
|
|
61
|
+
*/
|
|
62
|
+
export declare function gitIgnored(root: string, paths: readonly string[], { binary, timeoutMs }?: GitCheckOptions): Promise<Set<string> | null>;
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
// packages/daemon/src/gitignore.ts
|
|
2
|
+
import { spawn } from "node:child_process";
|
|
3
|
+
import { accessSync, constants, lstatSync, realpathSync, statSync } from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
var GIT_CHECK = Object.freeze({
|
|
6
|
+
/** Git gets this long per request, waiting for a turn included, then is killed. */
|
|
7
|
+
timeoutMs: 2e3,
|
|
8
|
+
/** Git processes alpd runs at once, and requests that may wait for one. */
|
|
9
|
+
maxConcurrent: 4,
|
|
10
|
+
maxQueued: 64,
|
|
11
|
+
maxErrorBytes: 64 * 1024
|
|
12
|
+
});
|
|
13
|
+
var IgnoreUnknown = class extends Error {
|
|
14
|
+
constructor(why) {
|
|
15
|
+
super(`git check-ignore failed: ${why}`);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
function gitEnvironment() {
|
|
19
|
+
const env = {};
|
|
20
|
+
for (const [key, value] of Object.entries(process.env)) if (!/^GIT_/i.test(key)) env[key] = value;
|
|
21
|
+
return { ...env, GIT_OPTIONAL_LOCKS: "0", GIT_TERMINAL_PROMPT: "0", LC_ALL: "C" };
|
|
22
|
+
}
|
|
23
|
+
var executable = (file) => {
|
|
24
|
+
try {
|
|
25
|
+
if (!statSync(file).isFile()) return false;
|
|
26
|
+
if (process.platform !== "win32") accessSync(file, constants.X_OK);
|
|
27
|
+
return true;
|
|
28
|
+
} catch {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
var identity = (file) => {
|
|
33
|
+
const { dev, ino } = statSync(file, { bigint: true });
|
|
34
|
+
return { dev, ino };
|
|
35
|
+
};
|
|
36
|
+
var foldCase = process.platform === "darwin" || process.platform === "win32";
|
|
37
|
+
var comparable = (file) => foldCase ? file.toLowerCase() : file;
|
|
38
|
+
function insideRoot(file, root) {
|
|
39
|
+
let directory;
|
|
40
|
+
try {
|
|
41
|
+
directory = realpathSync.native(file);
|
|
42
|
+
} catch {
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
const rootName = comparable(root.path.endsWith(path.sep) && root.path.length > 1 ? root.path.slice(0, -1) : root.path);
|
|
46
|
+
for (; ; ) {
|
|
47
|
+
const name = comparable(directory);
|
|
48
|
+
if (name === rootName || name.startsWith(rootName.endsWith(path.sep) ? rootName : `${rootName}${path.sep}`)) return true;
|
|
49
|
+
let id;
|
|
50
|
+
try {
|
|
51
|
+
id = identity(directory);
|
|
52
|
+
} catch {
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
if (root.id.ino !== 0n && id.dev === root.id.dev && id.ino === root.id.ino) return true;
|
|
56
|
+
const parent = path.dirname(directory);
|
|
57
|
+
if (parent === directory) return false;
|
|
58
|
+
directory = parent;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function rootOf(root) {
|
|
62
|
+
try {
|
|
63
|
+
const real = realpathSync.native(root);
|
|
64
|
+
return { path: real, id: identity(real) };
|
|
65
|
+
} catch {
|
|
66
|
+
return void 0;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
function resolveGit(name = "git", searchPath = process.env.PATH ?? process.env.Path ?? "", root) {
|
|
70
|
+
const box = root === void 0 ? void 0 : rootOf(root);
|
|
71
|
+
if (root !== void 0 && !box) throw new IgnoreUnknown("the workspace cannot be examined");
|
|
72
|
+
const accept = (file) => {
|
|
73
|
+
if (!executable(file)) return void 0;
|
|
74
|
+
if (!box) return file;
|
|
75
|
+
let real;
|
|
76
|
+
try {
|
|
77
|
+
real = realpathSync.native(file);
|
|
78
|
+
} catch {
|
|
79
|
+
return void 0;
|
|
80
|
+
}
|
|
81
|
+
return insideRoot(path.dirname(real), box) || !executable(real) ? void 0 : real;
|
|
82
|
+
};
|
|
83
|
+
if (path.isAbsolute(name)) return accept(name);
|
|
84
|
+
if (name.includes("/") || name.includes("\\")) return void 0;
|
|
85
|
+
const names = process.platform === "win32" ? [`${name}.exe`, `${name}.com`] : [name];
|
|
86
|
+
for (const directory of searchPath.split(path.delimiter)) {
|
|
87
|
+
if (!directory || !path.isAbsolute(directory)) continue;
|
|
88
|
+
if (box && insideRoot(directory, box)) continue;
|
|
89
|
+
for (const candidate of names) {
|
|
90
|
+
const file = accept(path.join(directory, candidate));
|
|
91
|
+
if (file) return file;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return void 0;
|
|
95
|
+
}
|
|
96
|
+
function searchPathOutside(root, searchPath = process.env.PATH ?? process.env.Path ?? "") {
|
|
97
|
+
const box = rootOf(root);
|
|
98
|
+
if (!box) throw new IgnoreUnknown("the workspace cannot be examined");
|
|
99
|
+
return searchPath.split(path.delimiter).filter((directory) => directory && path.isAbsolute(directory) && !insideRoot(directory, box)).join(path.delimiter);
|
|
100
|
+
}
|
|
101
|
+
function hasGitEntry(root) {
|
|
102
|
+
for (let directory = root; ; directory = path.dirname(directory)) {
|
|
103
|
+
try {
|
|
104
|
+
lstatSync(path.join(directory, ".git"));
|
|
105
|
+
return true;
|
|
106
|
+
} catch (error) {
|
|
107
|
+
if (error.code !== "ENOENT") throw new IgnoreUnknown("cannot examine .git");
|
|
108
|
+
}
|
|
109
|
+
if (path.dirname(directory) === directory) return false;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
var running = 0;
|
|
113
|
+
var waiting = [];
|
|
114
|
+
function acquire(deadline) {
|
|
115
|
+
const release = () => {
|
|
116
|
+
running--;
|
|
117
|
+
waiting.shift()?.();
|
|
118
|
+
};
|
|
119
|
+
if (running < GIT_CHECK.maxConcurrent) {
|
|
120
|
+
running++;
|
|
121
|
+
return Promise.resolve(release);
|
|
122
|
+
}
|
|
123
|
+
if (waiting.length >= GIT_CHECK.maxQueued) return Promise.reject(new IgnoreUnknown("too many waiting"));
|
|
124
|
+
return new Promise((resolve, reject) => {
|
|
125
|
+
const turn = () => {
|
|
126
|
+
clearTimeout(timer);
|
|
127
|
+
running++;
|
|
128
|
+
resolve(release);
|
|
129
|
+
};
|
|
130
|
+
const timer = setTimeout(() => {
|
|
131
|
+
const index = waiting.indexOf(turn);
|
|
132
|
+
if (index >= 0) waiting.splice(index, 1);
|
|
133
|
+
reject(new IgnoreUnknown("timed out waiting for a turn"));
|
|
134
|
+
}, Math.max(0, deadline - Date.now()));
|
|
135
|
+
waiting.push(turn);
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
var gitChecksRunning = () => running;
|
|
139
|
+
async function gitIgnored(root, paths, { binary = "git", timeoutMs = GIT_CHECK.timeoutMs } = {}) {
|
|
140
|
+
if (!paths.length) return /* @__PURE__ */ new Set();
|
|
141
|
+
const git = resolveGit(binary, void 0, root);
|
|
142
|
+
if (!git) return null;
|
|
143
|
+
const deadline = Date.now() + timeoutMs;
|
|
144
|
+
const release = await acquire(deadline);
|
|
145
|
+
try {
|
|
146
|
+
return await check(git, root, paths, deadline, timeoutMs);
|
|
147
|
+
} finally {
|
|
148
|
+
release();
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
function check(git, root, paths, deadline, timeoutMs) {
|
|
152
|
+
const input = `${paths.map((entry) => `./${entry}`).join("\0")}\0`;
|
|
153
|
+
const maxOutputBytes = Buffer.byteLength(input) + 1024;
|
|
154
|
+
return new Promise((resolve, reject) => {
|
|
155
|
+
let settled = false;
|
|
156
|
+
let timer;
|
|
157
|
+
let child;
|
|
158
|
+
const group = process.platform !== "win32";
|
|
159
|
+
const kill = () => {
|
|
160
|
+
try {
|
|
161
|
+
if (group && child.pid) process.kill(-child.pid, "SIGKILL");
|
|
162
|
+
else child.kill("SIGKILL");
|
|
163
|
+
} catch {
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
const finish = (outcome) => {
|
|
167
|
+
if (settled) return;
|
|
168
|
+
settled = true;
|
|
169
|
+
if (timer) clearTimeout(timer);
|
|
170
|
+
outcome();
|
|
171
|
+
};
|
|
172
|
+
try {
|
|
173
|
+
child = spawn(git, ["-c", "core.fsmonitor=false", "check-ignore", "--no-index", "--stdin", "-z"], {
|
|
174
|
+
cwd: root,
|
|
175
|
+
env: gitEnvironment(),
|
|
176
|
+
shell: false,
|
|
177
|
+
windowsHide: true,
|
|
178
|
+
detached: group,
|
|
179
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
180
|
+
});
|
|
181
|
+
} catch (error) {
|
|
182
|
+
reject(new IgnoreUnknown(String(error.message)));
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
timer = setTimeout(() => finish(() => {
|
|
186
|
+
kill();
|
|
187
|
+
reject(new IgnoreUnknown("timed out"));
|
|
188
|
+
}), Math.max(1, Math.min(timeoutMs, deadline - Date.now())));
|
|
189
|
+
const out = [];
|
|
190
|
+
let outBytes = 0;
|
|
191
|
+
let err = "";
|
|
192
|
+
child.stdout.on("data", (chunk) => {
|
|
193
|
+
outBytes += chunk.length;
|
|
194
|
+
if (outBytes > maxOutputBytes) finish(() => {
|
|
195
|
+
kill();
|
|
196
|
+
reject(new IgnoreUnknown("more output than input"));
|
|
197
|
+
});
|
|
198
|
+
else out.push(chunk);
|
|
199
|
+
});
|
|
200
|
+
child.stderr.on("data", (chunk) => {
|
|
201
|
+
if (err.length < GIT_CHECK.maxErrorBytes) err += chunk.toString("utf8");
|
|
202
|
+
});
|
|
203
|
+
child.on("error", (error) => finish(() => {
|
|
204
|
+
reject(new IgnoreUnknown(error.message));
|
|
205
|
+
}));
|
|
206
|
+
child.on("close", (code) => finish(() => {
|
|
207
|
+
if (code === 128 && /not a git repository/i.test(err)) {
|
|
208
|
+
try {
|
|
209
|
+
if (hasGitEntry(root)) reject(new IgnoreUnknown("a .git Git cannot use"));
|
|
210
|
+
else resolve(null);
|
|
211
|
+
} catch (error) {
|
|
212
|
+
reject(error);
|
|
213
|
+
}
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
if (code !== 0 && code !== 1) {
|
|
217
|
+
reject(new IgnoreUnknown(`exit ${code}`));
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
if (err.length || code === 1 && outBytes || code === 0 && !outBytes) {
|
|
221
|
+
reject(new IgnoreUnknown("inconsistent exit status or diagnostics"));
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
const output = Buffer.concat(out).toString("utf8");
|
|
225
|
+
if (outBytes && !output.endsWith("\0")) {
|
|
226
|
+
reject(new IgnoreUnknown("unterminated answer"));
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
const asked = new Set(paths);
|
|
230
|
+
const ignored = /* @__PURE__ */ new Set();
|
|
231
|
+
for (const entry of output.split("\0")) {
|
|
232
|
+
if (!entry) continue;
|
|
233
|
+
const relative = entry.startsWith("./") ? entry.slice(2) : void 0;
|
|
234
|
+
if (relative === void 0 || !asked.has(relative)) {
|
|
235
|
+
reject(new IgnoreUnknown("an answer for a path not asked"));
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
ignored.add(relative);
|
|
239
|
+
}
|
|
240
|
+
resolve(ignored);
|
|
241
|
+
}));
|
|
242
|
+
child.stdin.on("error", () => {
|
|
243
|
+
});
|
|
244
|
+
child.stdin.end(input);
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
export {
|
|
248
|
+
GIT_CHECK,
|
|
249
|
+
IgnoreUnknown,
|
|
250
|
+
gitChecksRunning,
|
|
251
|
+
gitIgnored,
|
|
252
|
+
resolveGit,
|
|
253
|
+
searchPathOutside
|
|
254
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Envelope } from '@anhlp/alp-protocol/events';
|
|
2
|
+
export type HistoryItem = Envelope | {
|
|
3
|
+
cursor: number;
|
|
4
|
+
oversized: {
|
|
5
|
+
bytes: number;
|
|
6
|
+
};
|
|
7
|
+
};
|
|
8
|
+
export declare class HistoryError extends Error {
|
|
9
|
+
reason: 'invalid_cursor' | 'stale_cursor';
|
|
10
|
+
constructor(reason: 'invalid_cursor' | 'stale_cursor', message: string);
|
|
11
|
+
}
|
|
12
|
+
export declare function createHistoryIndex(file: string): {
|
|
13
|
+
page(sessionId: string, options?: {
|
|
14
|
+
before?: string;
|
|
15
|
+
limit?: number;
|
|
16
|
+
maxBytes?: number;
|
|
17
|
+
}): Promise<{
|
|
18
|
+
tipCursor: number;
|
|
19
|
+
olderCursor?: string | undefined;
|
|
20
|
+
generation: string;
|
|
21
|
+
items: HistoryItem[];
|
|
22
|
+
hasOlder: boolean;
|
|
23
|
+
}>;
|
|
24
|
+
};
|
package/dist/history.js
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
// packages/daemon/src/history.ts
|
|
2
|
+
import { open, readFile, writeFile, rename, stat } from "node:fs/promises";
|
|
3
|
+
import { createReadStream } from "node:fs";
|
|
4
|
+
import { randomBytes, createHmac, timingSafeEqual } from "node:crypto";
|
|
5
|
+
var WIDTH = 24;
|
|
6
|
+
var HistoryError = class extends Error {
|
|
7
|
+
constructor(reason, message) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.reason = reason;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
function createHistoryIndex(file) {
|
|
13
|
+
let pending = Promise.resolve();
|
|
14
|
+
const index = `${file}.idx`;
|
|
15
|
+
const metadata = `${file}.history.json`;
|
|
16
|
+
async function sync() {
|
|
17
|
+
const info = await stat(file).catch((error) => {
|
|
18
|
+
if (error.code === "ENOENT") return void 0;
|
|
19
|
+
throw error;
|
|
20
|
+
});
|
|
21
|
+
let meta;
|
|
22
|
+
try {
|
|
23
|
+
meta = JSON.parse(await readFile(metadata, "utf8"));
|
|
24
|
+
} catch (error) {
|
|
25
|
+
if (error.code !== "ENOENT" && !(error instanceof SyntaxError)) throw error;
|
|
26
|
+
}
|
|
27
|
+
if (meta && (!/^[0-9a-f]{32}$/.test(meta.generation) || !/^[0-9a-f]{64}$/.test(meta.secret) || ![meta.bytes, meta.count, meta.tip, meta.ino].every((value) => Number.isSafeInteger(value) && value >= 0))) meta = void 0;
|
|
28
|
+
const indexInfo = await stat(index).catch((error) => {
|
|
29
|
+
if (error.code === "ENOENT") return void 0;
|
|
30
|
+
throw error;
|
|
31
|
+
});
|
|
32
|
+
if (!meta || !indexInfo || indexInfo.size < meta.count * WIDTH || meta.ino !== 0 && meta.ino !== (info?.ino ?? 0) || meta.bytes > (info?.size ?? 0)) {
|
|
33
|
+
meta = { generation: randomBytes(16).toString("hex"), secret: randomBytes(32).toString("hex"), ino: info?.ino ?? 0, bytes: 0, count: 0, tip: 0 };
|
|
34
|
+
await writeFile(index, "", { mode: 384 });
|
|
35
|
+
}
|
|
36
|
+
if (meta.ino === 0 && info) meta.ino = info.ino;
|
|
37
|
+
const handle = await open(index, "a+", 384);
|
|
38
|
+
try {
|
|
39
|
+
await handle.truncate(meta.count * WIDTH);
|
|
40
|
+
if (info && info.size > meta.bytes) {
|
|
41
|
+
let offset = meta.bytes;
|
|
42
|
+
let start = offset;
|
|
43
|
+
let fragments = [];
|
|
44
|
+
for await (const raw of createReadStream(file, { start: offset, end: info.size - 1, highWaterMark: 65536 })) {
|
|
45
|
+
const chunk = raw;
|
|
46
|
+
let from = 0;
|
|
47
|
+
for (let at = 0; at < chunk.length; at++) if (chunk[at] === 10) {
|
|
48
|
+
const part = chunk.subarray(from, at);
|
|
49
|
+
fragments.push(part);
|
|
50
|
+
const length = offset + at + 1 - start;
|
|
51
|
+
let cursor = meta.tip + 1;
|
|
52
|
+
const body = Buffer.concat(fragments).toString("utf8");
|
|
53
|
+
try {
|
|
54
|
+
const value = JSON.parse(body);
|
|
55
|
+
if (Number.isSafeInteger(value.cursor) && value.cursor > meta.tip) cursor = value.cursor;
|
|
56
|
+
} catch {
|
|
57
|
+
throw new Error("Unreadable history line");
|
|
58
|
+
}
|
|
59
|
+
const entry = Buffer.alloc(WIDTH);
|
|
60
|
+
[start, length, cursor].forEach((value, i) => entry.writeBigUInt64LE(BigInt(value), i * 8));
|
|
61
|
+
await handle.write(entry);
|
|
62
|
+
meta.count++;
|
|
63
|
+
meta.tip = cursor;
|
|
64
|
+
meta.bytes = offset + at + 1;
|
|
65
|
+
start = meta.bytes;
|
|
66
|
+
fragments = [];
|
|
67
|
+
from = at + 1;
|
|
68
|
+
}
|
|
69
|
+
const rest = chunk.subarray(from);
|
|
70
|
+
fragments.push(rest);
|
|
71
|
+
offset += chunk.length;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const temporary = `${metadata}.tmp`;
|
|
75
|
+
await writeFile(temporary, JSON.stringify(meta), { mode: 384 });
|
|
76
|
+
await rename(temporary, metadata);
|
|
77
|
+
return meta;
|
|
78
|
+
} finally {
|
|
79
|
+
await handle.close();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
page(sessionId, options = {}) {
|
|
84
|
+
const run = pending.then(async () => {
|
|
85
|
+
const { before, limit = 200 } = options;
|
|
86
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > 500) throw new Error("limit must be between 1 and 500");
|
|
87
|
+
const maxBytes = Math.min(512 * 1024, options.maxBytes ?? 512 * 1024);
|
|
88
|
+
if (maxBytes < 2048) throw new Error("history frame budget too small");
|
|
89
|
+
const meta = await sync();
|
|
90
|
+
const sign = (body2) => createHmac("sha256", meta.secret).update(body2).digest("base64url");
|
|
91
|
+
let position = meta.count, tip = meta.tip;
|
|
92
|
+
if (before !== void 0) {
|
|
93
|
+
if (typeof before !== "string" || before.length > 4096) throw new HistoryError("invalid_cursor", "Invalid history cursor");
|
|
94
|
+
try {
|
|
95
|
+
const [body2, signature, extra] = before.split(".");
|
|
96
|
+
const value = JSON.parse(Buffer.from(body2, "base64url").toString("utf8"));
|
|
97
|
+
if (value.sessionId !== sessionId) throw new Error();
|
|
98
|
+
if (value.generation !== meta.generation) throw new HistoryError("stale_cursor", "History generation changed");
|
|
99
|
+
const expected = Buffer.from(sign(body2));
|
|
100
|
+
const received = Buffer.from(signature ?? "");
|
|
101
|
+
if (extra || expected.length !== received.length || !timingSafeEqual(expected, received)) throw new Error();
|
|
102
|
+
if (!Number.isSafeInteger(value.position) || value.position < 0 || value.position > meta.count || !Number.isSafeInteger(value.tip) || value.tip < 0 || value.tip > meta.tip) throw new Error();
|
|
103
|
+
position = value.position;
|
|
104
|
+
tip = value.tip;
|
|
105
|
+
} catch (error) {
|
|
106
|
+
if (error instanceof HistoryError) throw error;
|
|
107
|
+
throw new HistoryError("invalid_cursor", "Invalid history cursor");
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
const items = [];
|
|
111
|
+
let bytes = 1024;
|
|
112
|
+
const idx = await open(index, "r");
|
|
113
|
+
const timeline = meta.count ? await open(file, "r") : void 0;
|
|
114
|
+
try {
|
|
115
|
+
while (position > 0 && items.length < limit) {
|
|
116
|
+
const entry = Buffer.alloc(WIDTH);
|
|
117
|
+
if ((await idx.read(entry, 0, WIDTH, (position - 1) * WIDTH)).bytesRead !== WIDTH) throw new Error("Incomplete history index");
|
|
118
|
+
const [offset, length, cursor] = [0, 8, 16].map((at) => Number(entry.readBigUInt64LE(at)));
|
|
119
|
+
let item;
|
|
120
|
+
if (length > maxBytes - 1024) item = { cursor, oversized: { bytes: length } };
|
|
121
|
+
else {
|
|
122
|
+
if (bytes + length > maxBytes) break;
|
|
123
|
+
const data = Buffer.alloc(length);
|
|
124
|
+
if ((await timeline.read(data, 0, length, offset)).bytesRead !== length) throw new Error("History changed during read");
|
|
125
|
+
item = { ...JSON.parse(data.toString("utf8")), cursor };
|
|
126
|
+
}
|
|
127
|
+
const size = Buffer.byteLength(JSON.stringify(item)) + 1;
|
|
128
|
+
if (bytes + size > maxBytes) break;
|
|
129
|
+
items.unshift(item);
|
|
130
|
+
bytes += size;
|
|
131
|
+
position--;
|
|
132
|
+
}
|
|
133
|
+
} finally {
|
|
134
|
+
await idx.close();
|
|
135
|
+
await timeline?.close();
|
|
136
|
+
}
|
|
137
|
+
const body = Buffer.from(JSON.stringify({ sessionId, generation: meta.generation, position, tip })).toString("base64url");
|
|
138
|
+
return { generation: meta.generation, items, hasOlder: position > 0, ...position > 0 ? { olderCursor: `${body}.${sign(body)}` } : {}, tipCursor: tip };
|
|
139
|
+
});
|
|
140
|
+
pending = run.catch(() => {
|
|
141
|
+
});
|
|
142
|
+
return run;
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
export {
|
|
147
|
+
HistoryError,
|
|
148
|
+
createHistoryIndex
|
|
149
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { createDaemonServer, ERROR } from './server.js';
|
|
2
|
+
export type { DaemonConnection, DaemonServer, SessionSummary } from './server.js';
|
|
3
|
+
export { createStore } from './store.js';
|
|
4
|
+
export type { SessionRecord, SessionStatus, Store } from './store.js';
|
|
5
|
+
export { createScheduleStore, PROMPT_CHARS, SCHEDULE_ID, SCHEDULE_VERSION } from './schedules.js';
|
|
6
|
+
export type { ScheduleRecord, ScheduleStore } from './schedules.js';
|
|
7
|
+
export { createScheduler, duration, renderPrompt, SCHEDULE_GRACE_MS, SCHEDULE_TICK_MS } from './scheduler.js';
|
|
8
|
+
export type { ScheduleDelivery, Scheduler } from './scheduler.js';
|
|
9
|
+
export { attachmentLimits, attachmentName, createAttachmentStore, readAttachmentConfig, sniffMimeType } from './attachments.js';
|
|
10
|
+
export type { AttachmentReader, AttachmentRuntime, AttachmentStore, Principal } from './attachments.js';
|
|
11
|
+
export { createDiskGuard, isDiskError } from './disk.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// packages/daemon/src/index.ts
|
|
2
|
+
import { createDaemonServer, ERROR } from "./server.js";
|
|
3
|
+
import { createStore } from "./store.js";
|
|
4
|
+
import { createScheduleStore, PROMPT_CHARS, SCHEDULE_ID, SCHEDULE_VERSION } from "./schedules.js";
|
|
5
|
+
import { createScheduler, duration, renderPrompt, SCHEDULE_GRACE_MS, SCHEDULE_TICK_MS } from "./scheduler.js";
|
|
6
|
+
import { attachmentLimits, attachmentName, createAttachmentStore, readAttachmentConfig, sniffMimeType } from "./attachments.js";
|
|
7
|
+
import { createDiskGuard, isDiskError } from "./disk.js";
|
|
8
|
+
export {
|
|
9
|
+
ERROR,
|
|
10
|
+
PROMPT_CHARS,
|
|
11
|
+
SCHEDULE_GRACE_MS,
|
|
12
|
+
SCHEDULE_ID,
|
|
13
|
+
SCHEDULE_TICK_MS,
|
|
14
|
+
SCHEDULE_VERSION,
|
|
15
|
+
attachmentLimits,
|
|
16
|
+
attachmentName,
|
|
17
|
+
createAttachmentStore,
|
|
18
|
+
createDaemonServer,
|
|
19
|
+
createDiskGuard,
|
|
20
|
+
createScheduleStore,
|
|
21
|
+
createScheduler,
|
|
22
|
+
createStore,
|
|
23
|
+
duration,
|
|
24
|
+
isDiskError,
|
|
25
|
+
readAttachmentConfig,
|
|
26
|
+
renderPrompt,
|
|
27
|
+
sniffMimeType
|
|
28
|
+
};
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { RpcError } from './rpc-error.js';
|
|
2
|
+
/**
|
|
3
|
+
* The lifecycle journal (docs/protocol/alp-json-rpc-v1.md §12.7): one record per mutation of a session's
|
|
4
|
+
* native conversation (fork, rewind; later reload and live configure), keyed by resource, operation and
|
|
5
|
+
* the client's requestId. A mutation crosses the native runtime and alpd's own files, which no rename
|
|
6
|
+
* makes atomic, so it is journaled before the native step and replayed from the journal:
|
|
7
|
+
*
|
|
8
|
+
* prepared → written before the native step; nothing native happened yet, or it is in flight.
|
|
9
|
+
* applied → the native step answered; `native` holds what it made. alpd's own commit may follow.
|
|
10
|
+
* committed → alpd's commit is on disk; `result` answers every repeat of the request.
|
|
11
|
+
* failed → the native runtime refused it for certain; `error` answers every repeat.
|
|
12
|
+
* unknown → the native runtime may or may not have acted (a crash or a lost answer after prepared).
|
|
13
|
+
*
|
|
14
|
+
* A start of alpd turns `prepared` into `unknown` (never sends the native step again) and rolls `applied`
|
|
15
|
+
* forward with the operation's commit, which must therefore be idempotent. Refusals before `prepared`
|
|
16
|
+
* (validation, busy, stale) record nothing, so the same requestId may be tried again. As the rest of
|
|
17
|
+
* alpd's store, a resolved write survives a crash of alpd, not a power loss (no fsync).
|
|
18
|
+
*/
|
|
19
|
+
export type LifecycleState = 'prepared' | 'applied' | 'committed' | 'failed' | 'unknown';
|
|
20
|
+
export type LifecycleEntry<Plan = any, Native = any, Result = any> = {
|
|
21
|
+
version: 1;
|
|
22
|
+
key: string;
|
|
23
|
+
/** The resource the operation changes or reads: a root session id. */
|
|
24
|
+
resource: string;
|
|
25
|
+
operation: string;
|
|
26
|
+
requestId: string;
|
|
27
|
+
/** A digest of the request; another payload under the same requestId is a conflict. */
|
|
28
|
+
fingerprint: string;
|
|
29
|
+
/** Unique attempt, so a retained completion witness cannot match a reused journal key. */
|
|
30
|
+
attemptId?: string;
|
|
31
|
+
state: LifecycleState;
|
|
32
|
+
/** Startup has observed this unknown outcome. It remains unknown for retries and audit. */
|
|
33
|
+
recoverySeenAt?: string;
|
|
34
|
+
/** What prepare decided; everything commit needs, so a restart can roll the operation forward. */
|
|
35
|
+
plan: Plan;
|
|
36
|
+
native?: Native;
|
|
37
|
+
result?: Result;
|
|
38
|
+
error?: {
|
|
39
|
+
code: number;
|
|
40
|
+
message: string;
|
|
41
|
+
data?: unknown;
|
|
42
|
+
};
|
|
43
|
+
createdAt: string;
|
|
44
|
+
updatedAt: string;
|
|
45
|
+
};
|
|
46
|
+
/** Terminal entries are kept this long after their last write, as prompt receipts are (§10). */
|
|
47
|
+
export declare const LIFECYCLE_RETENTION_MS: number;
|
|
48
|
+
export type LifecycleJournal = ReturnType<typeof createLifecycleJournal>;
|
|
49
|
+
export declare function createLifecycleJournal(directory: string): {
|
|
50
|
+
keyOf: (resource: string, operation: string, requestId: string) => string;
|
|
51
|
+
get(resource: string, operation: string, requestId: string): Promise<LifecycleEntry | undefined>;
|
|
52
|
+
/** Writes an entry atomically; writes of one key never land out of order. */
|
|
53
|
+
put(entry: LifecycleEntry): Promise<void>;
|
|
54
|
+
/** Every readable entry; an unreadable one is skipped (and named), never deleted. */
|
|
55
|
+
list(onUnreadable?: (name: string, error: unknown) => void): Promise<LifecycleEntry[]>;
|
|
56
|
+
/** Removes terminal entries not written for `maxAgeMs`, and temporary files a crash left (only at start). */
|
|
57
|
+
prune(maxAgeMs?: number, { temporaries }?: {
|
|
58
|
+
temporaries?: boolean | undefined;
|
|
59
|
+
}): Promise<void>;
|
|
60
|
+
flush(): Promise<void>;
|
|
61
|
+
};
|
|
62
|
+
export declare const outcomeUnknown: (operation: string, requestId: string) => RpcError;
|
|
63
|
+
export type LifecycleSteps<Plan, Native, Result> = {
|
|
64
|
+
/** Validates and decides; may refuse (nothing is recorded). Runs inside the resource's queue. */
|
|
65
|
+
prepare(): Promise<Plan>;
|
|
66
|
+
/** The native step; a thrown error with `outcomeUnknown: true` records unknown, any other records failed. */
|
|
67
|
+
apply(plan: Plan): Promise<Native>;
|
|
68
|
+
/** alpd's own, idempotent commit from the entry alone (a restart replays it). */
|
|
69
|
+
commit(entry: LifecycleEntry<Plan, Native, Result>): Promise<Result>;
|
|
70
|
+
/** How a repeat answers from a committed entry (e.g. with a fresh session summary). */
|
|
71
|
+
repeat?(entry: LifecycleEntry<Plan, Native, Result>): Result | Promise<Result>;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Runs journaled operations: one at a time per resource, at most once per (resource, operation,
|
|
75
|
+
* requestId). The seam later lifecycle operations (live configure, reload) reuse with their own steps.
|
|
76
|
+
*/
|
|
77
|
+
export declare function createLifecycle(journal: LifecycleJournal, { crashPoint, completed, }?: {
|
|
78
|
+
crashPoint?: (name: string) => void;
|
|
79
|
+
completed?: (entry: LifecycleEntry) => Promise<{
|
|
80
|
+
result: any;
|
|
81
|
+
} | undefined>;
|
|
82
|
+
}): {
|
|
83
|
+
run: <Plan, Native, Result>(request: {
|
|
84
|
+
resource: string;
|
|
85
|
+
operation: string;
|
|
86
|
+
requestId: string;
|
|
87
|
+
fingerprint: string;
|
|
88
|
+
}, steps: LifecycleSteps<Plan, Native, Result>) => Promise<{
|
|
89
|
+
result: Result;
|
|
90
|
+
duplicate: boolean;
|
|
91
|
+
entry: LifecycleEntry<Plan, Native, Result>;
|
|
92
|
+
}>;
|
|
93
|
+
serial: <T>(resource: string, work: () => Promise<T>) => Promise<T>;
|
|
94
|
+
/**
|
|
95
|
+
* At start, before any request: `prepared` becomes `unknown` (the native step is never sent again),
|
|
96
|
+
* and unknown outcomes are durably observed once, without claiming success or replaying a commit.
|
|
97
|
+
* `applied` rolls forward through the operation's commit (left `applied` when that fails, to be
|
|
98
|
+
* finished by a repeat or the next start; such entries are answered).
|
|
99
|
+
*/
|
|
100
|
+
recover(commits: Record<string, (entry: LifecycleEntry) => Promise<unknown>>, log?: (message: string, fields: Record<string, unknown>) => void): Promise<LifecycleEntry<any, any, any>[]>;
|
|
101
|
+
};
|