@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
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "0.0.1-beta.3",
|
|
3
|
+
"entries": {
|
|
4
|
+
"packages/daemon/dist/alpd.js": {
|
|
5
|
+
"inputs": [
|
|
6
|
+
"packages/client/src/index.js",
|
|
7
|
+
"packages/client/src/ipc.js",
|
|
8
|
+
"packages/client/src/portable.js",
|
|
9
|
+
"packages/client/src/rpc/client.js",
|
|
10
|
+
"packages/client/src/rpc/errors.js",
|
|
11
|
+
"packages/client/src/rpc/framing.js",
|
|
12
|
+
"packages/client/src/rpc/handshake.js",
|
|
13
|
+
"packages/client/src/rpc/reconnect.js",
|
|
14
|
+
"packages/client/src/rpc/timers.js",
|
|
15
|
+
"packages/client/src/websocket.js",
|
|
16
|
+
"packages/core/src/adapter.js",
|
|
17
|
+
"packages/core/src/atomic.js",
|
|
18
|
+
"packages/core/src/backup.js",
|
|
19
|
+
"packages/core/src/delegation.js",
|
|
20
|
+
"packages/core/src/errors.js",
|
|
21
|
+
"packages/core/src/formulas.js",
|
|
22
|
+
"packages/core/src/hook-run.js",
|
|
23
|
+
"packages/core/src/hooks.js",
|
|
24
|
+
"packages/core/src/init.js",
|
|
25
|
+
"packages/core/src/library-edit.js",
|
|
26
|
+
"packages/core/src/library.js",
|
|
27
|
+
"packages/core/src/log.js",
|
|
28
|
+
"packages/core/src/migrations.js",
|
|
29
|
+
"packages/core/src/models.js",
|
|
30
|
+
"packages/core/src/permissions.js",
|
|
31
|
+
"packages/core/src/profiles.js",
|
|
32
|
+
"packages/core/src/promptsafe.js",
|
|
33
|
+
"packages/core/src/providers.js",
|
|
34
|
+
"packages/core/src/resolver.js",
|
|
35
|
+
"packages/core/src/safe-path.js",
|
|
36
|
+
"packages/core/src/shell.js",
|
|
37
|
+
"packages/core/src/state-schema.js",
|
|
38
|
+
"packages/core/src/task-rows.js",
|
|
39
|
+
"packages/core/src/tasks.js",
|
|
40
|
+
"packages/core/src/teams.js",
|
|
41
|
+
"packages/core/src/template-path.js",
|
|
42
|
+
"packages/core/src/trust.js",
|
|
43
|
+
"packages/core/src/user-settings.js",
|
|
44
|
+
"packages/core/src/validation.js",
|
|
45
|
+
"packages/core/src/verify.js",
|
|
46
|
+
"packages/core/src/workflow.js",
|
|
47
|
+
"packages/daemon/src/files.ts",
|
|
48
|
+
"packages/daemon/src/gitignore.ts",
|
|
49
|
+
"packages/daemon/src/lock.ts",
|
|
50
|
+
"packages/daemon/src/main.ts",
|
|
51
|
+
"packages/daemon/src/operations.ts",
|
|
52
|
+
"packages/daemon/src/rpc-error.ts",
|
|
53
|
+
"packages/daemon/src/server.ts",
|
|
54
|
+
"packages/daemon/src/store.ts",
|
|
55
|
+
"packages/protocol/src/contract-v1.js",
|
|
56
|
+
"packages/protocol/src/contract.js",
|
|
57
|
+
"packages/protocol/src/index.ts",
|
|
58
|
+
"packages/protocol/src/validate.js",
|
|
59
|
+
"packages/protocol/src/wire.js",
|
|
60
|
+
"packages/runtime/src/acp-transport.ts",
|
|
61
|
+
"packages/runtime/src/antigravity-transport.ts",
|
|
62
|
+
"packages/runtime/src/board-domain.ts",
|
|
63
|
+
"packages/runtime/src/board.ts",
|
|
64
|
+
"packages/runtime/src/catalog.ts",
|
|
65
|
+
"packages/runtime/src/claude-transport.ts",
|
|
66
|
+
"packages/runtime/src/contracts.ts",
|
|
67
|
+
"packages/runtime/src/github.ts",
|
|
68
|
+
"packages/runtime/src/handoff.ts",
|
|
69
|
+
"packages/runtime/src/index.ts",
|
|
70
|
+
"packages/runtime/src/language.ts",
|
|
71
|
+
"packages/runtime/src/live.ts",
|
|
72
|
+
"packages/runtime/src/mailbox.ts",
|
|
73
|
+
"packages/runtime/src/mcp-tools.ts",
|
|
74
|
+
"packages/runtime/src/permission-path.ts",
|
|
75
|
+
"packages/runtime/src/process-info.ts",
|
|
76
|
+
"packages/runtime/src/providers.ts",
|
|
77
|
+
"packages/runtime/src/recall.ts",
|
|
78
|
+
"packages/runtime/src/resolve.ts",
|
|
79
|
+
"packages/runtime/src/runtime-context.ts",
|
|
80
|
+
"packages/runtime/src/runtime.ts",
|
|
81
|
+
"packages/runtime/src/session-config.ts",
|
|
82
|
+
"packages/runtime/src/tool-detail.ts",
|
|
83
|
+
"packages/runtime/src/tool-results.ts",
|
|
84
|
+
"packages/runtime/src/tools.ts",
|
|
85
|
+
"packages/runtime/src/transport.ts",
|
|
86
|
+
"packages/runtime/src/usage.ts",
|
|
87
|
+
"packages/runtime/src/workspace.ts",
|
|
88
|
+
"packages/runtime/src/worktree-domain.ts"
|
|
89
|
+
],
|
|
90
|
+
"external": [
|
|
91
|
+
"child_process",
|
|
92
|
+
"fs",
|
|
93
|
+
"node:child_process",
|
|
94
|
+
"node:crypto",
|
|
95
|
+
"node:fs",
|
|
96
|
+
"node:fs/promises",
|
|
97
|
+
"node:net",
|
|
98
|
+
"node:os",
|
|
99
|
+
"node:path",
|
|
100
|
+
"node:process",
|
|
101
|
+
"node:readline",
|
|
102
|
+
"node:stream",
|
|
103
|
+
"node:stream/promises",
|
|
104
|
+
"node:string_decoder",
|
|
105
|
+
"node:url",
|
|
106
|
+
"node:zlib",
|
|
107
|
+
"path"
|
|
108
|
+
]
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
package/dist/files.d.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { type GitCheckOptions } from './gitignore.js';
|
|
2
|
+
export { gitChecksRunning, resolveGit, GIT_CHECK } from './gitignore.js';
|
|
3
|
+
/**
|
|
4
|
+
* `workspace.files.search` (docs/protocol/alp-json-rpc-v1.md §12.3): the files of a workspace whose
|
|
5
|
+
* path matches a query, as Paseo's command center finds them (server/utils/directory-suggestions.ts
|
|
6
|
+
* at b63a325, with its workspace settings: relative paths, hidden and heavy directories skipped).
|
|
7
|
+
*
|
|
8
|
+
* Read-only and confined to the workspace: the walk starts at the root's real path and never
|
|
9
|
+
* follows a symbolic link (a link is neither listed nor entered). Each directory is checked before
|
|
10
|
+
* and after it is read: a real directory (not a link), at the real path it was reached by, the same
|
|
11
|
+
* one (device and inode) both times; otherwise what was read is dropped. Only names are read, never
|
|
12
|
+
* contents. A link swapped in and back out between those checks, by a writer racing inside the
|
|
13
|
+
* workspace, can still expose names of the directory it pointed to (Node has no openat/O_NOFOLLOW
|
|
14
|
+
* walk); docs/protocol/alp-json-rpc-v1.md §12.3. The walk stops at {@link LIMITS}, checked per
|
|
15
|
+
* entry; the answer then says `truncated`.
|
|
16
|
+
*/
|
|
17
|
+
export declare const LIMITS: Readonly<{
|
|
18
|
+
/** Directory levels below the root that are entered (Paseo's DEFAULT_MAX_DEPTH). */
|
|
19
|
+
depth: 12;
|
|
20
|
+
/** Directory entries looked at (Paseo's DEFAULT_MAX_ENTRIES_SCANNED). */
|
|
21
|
+
scanned: 20000;
|
|
22
|
+
/** Wall time of one search. */
|
|
23
|
+
ms: 3000;
|
|
24
|
+
/** Results by default and at most (Paseo's DEFAULT_LIMIT, MAX_LIMIT). */
|
|
25
|
+
defaultLimit: 30;
|
|
26
|
+
maxLimit: 100;
|
|
27
|
+
}>;
|
|
28
|
+
export declare class FilesError extends Error {
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The files under `projectRoot` matching `query`, best first. `projectRoot` must be an absolute path
|
|
32
|
+
* of a directory; anything else throws FilesError.
|
|
33
|
+
*/
|
|
34
|
+
export declare function searchWorkspaceFiles(projectRoot: string, { query, limit, now, git }?: {
|
|
35
|
+
query?: string;
|
|
36
|
+
limit?: number;
|
|
37
|
+
now?: () => number;
|
|
38
|
+
git?: GitCheckOptions;
|
|
39
|
+
}): Promise<{
|
|
40
|
+
files: Array<{
|
|
41
|
+
path: string;
|
|
42
|
+
}>;
|
|
43
|
+
truncated?: true;
|
|
44
|
+
}>;
|
|
45
|
+
/**
|
|
46
|
+
* A refusal of workspace.files.list or workspace.files.read: the JSON-RPC code and `data.reason`
|
|
47
|
+
* alpd answers with. Messages are fixed or name only the client's own relative path, never a host
|
|
48
|
+
* path or anything read from a file.
|
|
49
|
+
*/
|
|
50
|
+
export declare class FileAccessError extends Error {
|
|
51
|
+
readonly code: number;
|
|
52
|
+
readonly reason: string;
|
|
53
|
+
constructor(message: string, code: number, reason: string);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* A path relative to the workspace as a client may send it, its segments, or a refusal: '/'
|
|
57
|
+
* separated, no leading slash, drive or backslash, no `.`, `..` or empty segment, no NUL, at most
|
|
58
|
+
* WORKSPACE_FILES.maxPathLength characters. A blocked path (blockedFilePath) is refused as such.
|
|
59
|
+
*/
|
|
60
|
+
export declare function relativeSegments(relative: unknown): string[];
|
|
61
|
+
/**
|
|
62
|
+
* The bytes of a regular file reached by name, or undefined when it is larger than `maxBytes`;
|
|
63
|
+
* throws when it is missing, not a regular file, a link, has more than one name, or is not the
|
|
64
|
+
* file it was a moment before. The file is opened without following a final link and checked by
|
|
65
|
+
* its handle (same device and inode as by name, before and after).
|
|
66
|
+
*/
|
|
67
|
+
/** Test probes: called between the checks of a read, where a racing writer could change the file. */
|
|
68
|
+
export type FileProbe = (step: 'checked' | 'read' | 'listed') => Promise<void> | void;
|
|
69
|
+
export type ListedEntry = {
|
|
70
|
+
name: string;
|
|
71
|
+
path: string;
|
|
72
|
+
kind: 'file' | 'directory';
|
|
73
|
+
size?: number;
|
|
74
|
+
modifiedAt?: string;
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* The entries of a directory under `projectRoot` (its root without `relative`): regular files and
|
|
78
|
+
* directories only (links, sockets, devices are left out), never a blocked one (blockedFilePath)
|
|
79
|
+
* and, unless `includeIgnored`, none .gitignore'd. Directories first, then by name. Each directory
|
|
80
|
+
* on the way is checked (real directory, real path, same device and inode) before and after the
|
|
81
|
+
* read. At most WORKSPACE_FILES.maxListEntries entries; then `truncated`.
|
|
82
|
+
*/
|
|
83
|
+
export declare function listWorkspaceDirectory(projectRoot: string, { path: relative, includeIgnored, now, probe, git }?: {
|
|
84
|
+
path?: string;
|
|
85
|
+
includeIgnored?: boolean;
|
|
86
|
+
now?: () => number;
|
|
87
|
+
probe?: FileProbe;
|
|
88
|
+
git?: GitCheckOptions;
|
|
89
|
+
}): Promise<{
|
|
90
|
+
entries: ListedEntry[];
|
|
91
|
+
truncated?: true;
|
|
92
|
+
}>;
|
|
93
|
+
/**
|
|
94
|
+
* The content of a UTF-8 text file under `projectRoot`, read-only: refused when it is blocked
|
|
95
|
+
* (blockedFilePath), .gitignore'd (unless `includeIgnored`), missing or not a regular file,
|
|
96
|
+
* reached through a link (any directory on the way or the file), has more than one name (a hard
|
|
97
|
+
* link may share a file from anywhere), changes while it is read, is over
|
|
98
|
+
* WORKSPACE_FILES.maxReadBytes, or is not UTF-8 text.
|
|
99
|
+
*/
|
|
100
|
+
export declare function readWorkspaceFile(projectRoot: string, { path: relative, includeIgnored, probe, git }: {
|
|
101
|
+
path: string;
|
|
102
|
+
includeIgnored?: boolean;
|
|
103
|
+
probe?: FileProbe;
|
|
104
|
+
git?: GitCheckOptions;
|
|
105
|
+
}): Promise<{
|
|
106
|
+
path: string;
|
|
107
|
+
content: string;
|
|
108
|
+
size: number;
|
|
109
|
+
modifiedAt: string;
|
|
110
|
+
}>;
|
package/dist/files.js
ADDED
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
// packages/daemon/src/files.ts
|
|
2
|
+
import { constants } from "node:fs";
|
|
3
|
+
import { lstat, open, opendir, realpath, stat } from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { ERROR, RPC_ERROR, WORKSPACE_FILES, blockedFilePath } from "@anhlp/alp-protocol";
|
|
6
|
+
import { gitIgnored, IgnoreUnknown, GIT_CHECK } from "./gitignore.js";
|
|
7
|
+
import { gitChecksRunning, resolveGit, GIT_CHECK as GIT_CHECK2 } from "./gitignore.js";
|
|
8
|
+
var LIMITS = Object.freeze({
|
|
9
|
+
/** Directory levels below the root that are entered (Paseo's DEFAULT_MAX_DEPTH). */
|
|
10
|
+
depth: 12,
|
|
11
|
+
/** Directory entries looked at (Paseo's DEFAULT_MAX_ENTRIES_SCANNED). */
|
|
12
|
+
scanned: 2e4,
|
|
13
|
+
/** Wall time of one search. */
|
|
14
|
+
ms: 3e3,
|
|
15
|
+
/** Results by default and at most (Paseo's DEFAULT_LIMIT, MAX_LIMIT). */
|
|
16
|
+
defaultLimit: 30,
|
|
17
|
+
maxLimit: 100
|
|
18
|
+
});
|
|
19
|
+
var HIDDEN_DIRECTORIES = /* @__PURE__ */ new Set([".agents", ".alp", ".claude", ".codex", ".github", ".opencode", ".paseo", ".vscode"]);
|
|
20
|
+
var IGNORED_DIRECTORIES = /* @__PURE__ */ new Set(["node_modules", "venv", "env", "virtualenv", "dist", "build", "target", "out", "coverage", "vendor", "__pycache__", ".git"]);
|
|
21
|
+
var NONE = Number.MAX_SAFE_INTEGER;
|
|
22
|
+
async function identity(directory) {
|
|
23
|
+
const info = await lstat(directory, { bigint: true });
|
|
24
|
+
if (!info.isDirectory() || await realpath(directory) !== directory) return null;
|
|
25
|
+
return `${info.dev}:${info.ino}`;
|
|
26
|
+
}
|
|
27
|
+
function fuzzy(query, candidate) {
|
|
28
|
+
let at = 0;
|
|
29
|
+
let first = -1;
|
|
30
|
+
let previous = -1;
|
|
31
|
+
let gaps = 0;
|
|
32
|
+
for (let index = 0; index < candidate.length && at < query.length; index++) {
|
|
33
|
+
if (candidate[index] !== query[at]) continue;
|
|
34
|
+
if (first < 0) first = index;
|
|
35
|
+
if (previous >= 0) gaps += index - previous - 1;
|
|
36
|
+
previous = index;
|
|
37
|
+
at++;
|
|
38
|
+
}
|
|
39
|
+
return at === query.length && first >= 0 ? first + gaps : null;
|
|
40
|
+
}
|
|
41
|
+
function rank(relative, query) {
|
|
42
|
+
const lower = relative.toLowerCase();
|
|
43
|
+
const segments = lower.split("/");
|
|
44
|
+
const depth = segments.length;
|
|
45
|
+
if (!query) return { path: relative, tier: 3, segment: NONE, offset: NONE, fuzzy: NONE, depth };
|
|
46
|
+
const offset = lower.indexOf(query);
|
|
47
|
+
if (query.includes("/")) {
|
|
48
|
+
if (offset >= 0) return { path: relative, tier: lower.startsWith(query) ? 1 : 3, segment: NONE, offset, fuzzy: NONE, depth };
|
|
49
|
+
const spread = fuzzy(query, lower);
|
|
50
|
+
return spread === null ? null : { path: relative, tier: 4, segment: NONE, offset: NONE, fuzzy: spread, depth };
|
|
51
|
+
}
|
|
52
|
+
const last = fuzzy(query, segments.at(-1) ?? "");
|
|
53
|
+
const tiers = [(segment) => segment === query, (segment) => segment.startsWith(query), (segment) => segment.includes(query)];
|
|
54
|
+
for (const [tier, test] of tiers.entries()) {
|
|
55
|
+
const segment = segments.findIndex(test);
|
|
56
|
+
if (segment >= 0) return { path: relative, tier, segment, offset: offset >= 0 ? offset : NONE, fuzzy: last ?? NONE, depth };
|
|
57
|
+
}
|
|
58
|
+
if (last !== null) return { path: relative, tier: 4, segment: NONE, offset: NONE, fuzzy: last, depth };
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
var compare = (a, b) => a.tier - b.tier || a.segment - b.segment || a.offset - b.offset || a.fuzzy - b.fuzzy || a.depth - b.depth || (a.path < b.path ? -1 : a.path > b.path ? 1 : 0);
|
|
62
|
+
var FilesError = class extends Error {
|
|
63
|
+
};
|
|
64
|
+
async function searchWorkspaceFiles(projectRoot, { query = "", limit = LIMITS.defaultLimit, now = Date.now, git } = {}) {
|
|
65
|
+
if (!path.isAbsolute(projectRoot)) throw new FilesError("projectRoot must be an absolute path");
|
|
66
|
+
let root;
|
|
67
|
+
try {
|
|
68
|
+
root = await realpath(projectRoot);
|
|
69
|
+
if (!(await stat(root)).isDirectory()) throw new Error("not a directory");
|
|
70
|
+
} catch {
|
|
71
|
+
throw new FilesError("projectRoot is not a directory alpd can read");
|
|
72
|
+
}
|
|
73
|
+
const wanted = query.trim().replace(/\\/g, "/").replace(/^\.?\/+/, "").toLowerCase();
|
|
74
|
+
const count = Math.max(1, Math.min(LIMITS.maxLimit, Math.trunc(limit)));
|
|
75
|
+
const deadline = now() + LIMITS.ms;
|
|
76
|
+
const realDeadline = Date.now() + LIMITS.ms;
|
|
77
|
+
const found = [];
|
|
78
|
+
let scanned = 0;
|
|
79
|
+
let truncated = false;
|
|
80
|
+
let level = [""];
|
|
81
|
+
for (let depth = 0; level.length && !truncated; depth++) {
|
|
82
|
+
const next = [];
|
|
83
|
+
const levelFiles = [];
|
|
84
|
+
for (const relative of level) {
|
|
85
|
+
if (scanned >= LIMITS.scanned || now() > deadline) {
|
|
86
|
+
truncated = true;
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
const directory = relative ? path.join(root, ...relative.split("/")) : root;
|
|
90
|
+
const files = [];
|
|
91
|
+
const directories = [];
|
|
92
|
+
try {
|
|
93
|
+
const before = await identity(directory);
|
|
94
|
+
if (!before) continue;
|
|
95
|
+
for await (const entry of await opendir(directory)) {
|
|
96
|
+
if (++scanned > LIMITS.scanned || now() > deadline) {
|
|
97
|
+
truncated = true;
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
const name = entry.name;
|
|
101
|
+
const child = relative ? `${relative}/${name}` : name;
|
|
102
|
+
if (name.includes("\\") || blockedFilePath(child)) continue;
|
|
103
|
+
if (entry.isFile()) {
|
|
104
|
+
if (name.startsWith(".")) continue;
|
|
105
|
+
const ranked = rank(child, wanted);
|
|
106
|
+
if (ranked) files.push(ranked);
|
|
107
|
+
} else if (entry.isDirectory()) {
|
|
108
|
+
if (IGNORED_DIRECTORIES.has(name) || name.startsWith(".") && !HIDDEN_DIRECTORIES.has(name)) continue;
|
|
109
|
+
if (depth < LIMITS.depth) directories.push(child);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (await identity(directory) !== before) continue;
|
|
113
|
+
} catch {
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
levelFiles.push(...files);
|
|
117
|
+
next.push(...directories);
|
|
118
|
+
if (truncated) break;
|
|
119
|
+
}
|
|
120
|
+
const remaining = Math.min(deadline - now(), realDeadline - Date.now());
|
|
121
|
+
if (remaining <= 0) {
|
|
122
|
+
truncated = true;
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
let ignored;
|
|
126
|
+
try {
|
|
127
|
+
ignored = await gitIgnored(root, [...levelFiles.map((file) => file.path), ...next], {
|
|
128
|
+
...git,
|
|
129
|
+
timeoutMs: Math.min(git?.timeoutMs ?? GIT_CHECK.timeoutMs, remaining)
|
|
130
|
+
});
|
|
131
|
+
} catch (error) {
|
|
132
|
+
if (!(error instanceof IgnoreUnknown)) throw error;
|
|
133
|
+
return { files: [], truncated: true };
|
|
134
|
+
}
|
|
135
|
+
found.push(...levelFiles.filter((file) => !ignored?.has(file.path)));
|
|
136
|
+
level = next.filter((directory) => !ignored?.has(directory));
|
|
137
|
+
}
|
|
138
|
+
found.sort(compare);
|
|
139
|
+
return { files: found.slice(0, count).map((entry) => ({ path: entry.path })), ...truncated ? { truncated: true } : {} };
|
|
140
|
+
}
|
|
141
|
+
var FileAccessError = class extends Error {
|
|
142
|
+
constructor(message, code, reason) {
|
|
143
|
+
super(message);
|
|
144
|
+
this.code = code;
|
|
145
|
+
this.reason = reason;
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
var LIST_LIMITS = Object.freeze({ scanned: 2e4, ms: 3e3 });
|
|
149
|
+
var OPEN_FLAGS = process.platform === "win32" ? constants.O_RDONLY : constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK;
|
|
150
|
+
var notFound = (relative) => new FileAccessError(`No readable file or directory at ${relative}`, ERROR.notFound, "not_found");
|
|
151
|
+
var linked = (relative) => new FileAccessError(`${relative} is reached through a link, has more than one name, or changed while it was read; alpd does not follow links`, ERROR.forbidden, "linked");
|
|
152
|
+
async function realRoot(projectRoot) {
|
|
153
|
+
if (!path.isAbsolute(projectRoot)) throw new FilesError("projectRoot must be an absolute path");
|
|
154
|
+
try {
|
|
155
|
+
const root = await realpath(projectRoot);
|
|
156
|
+
if (!(await stat(root)).isDirectory()) throw new Error("not a directory");
|
|
157
|
+
return root;
|
|
158
|
+
} catch {
|
|
159
|
+
throw new FilesError("projectRoot is not a directory alpd can read");
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
function relativeSegments(relative) {
|
|
163
|
+
if (typeof relative !== "string" || !relative || [...relative].length > WORKSPACE_FILES.maxPathLength || relative.includes("\0") || relative.includes("\\") || relative.startsWith("/") || /^[A-Za-z]:/.test(relative)) {
|
|
164
|
+
throw new FileAccessError("path must be a relative path inside the workspace, with / between names and no . or .. segment", RPC_ERROR.invalidParams, "invalid_path");
|
|
165
|
+
}
|
|
166
|
+
const segments = relative.split("/");
|
|
167
|
+
if (segments.some((segment) => segment === "" || segment === "." || segment === "..")) {
|
|
168
|
+
throw new FileAccessError("path must be a relative path inside the workspace, with / between names and no . or .. segment", RPC_ERROR.invalidParams, "invalid_path");
|
|
169
|
+
}
|
|
170
|
+
if (blockedFilePath(relative)) throw new FileAccessError(`${relative} is not readable remotely: .git, .env files, ALP configuration and key or credential files are always refused`, ERROR.forbidden, "blocked_path");
|
|
171
|
+
return segments;
|
|
172
|
+
}
|
|
173
|
+
async function directoryChain(root, segments, relative) {
|
|
174
|
+
const paths = [root];
|
|
175
|
+
for (let index = 0; index < segments.length; index++) paths.push(path.join(root, ...segments.slice(0, index + 1)));
|
|
176
|
+
const identities = [];
|
|
177
|
+
for (const directory of paths) identities.push(await directoryIdentity(directory, relative));
|
|
178
|
+
return { paths, identities };
|
|
179
|
+
}
|
|
180
|
+
async function directoryIdentity(directory, relative) {
|
|
181
|
+
let info;
|
|
182
|
+
try {
|
|
183
|
+
info = await lstat(directory, { bigint: true });
|
|
184
|
+
} catch {
|
|
185
|
+
throw notFound(relative);
|
|
186
|
+
}
|
|
187
|
+
if (info.isSymbolicLink()) throw linked(relative);
|
|
188
|
+
if (!info.isDirectory()) throw notFound(relative);
|
|
189
|
+
let real;
|
|
190
|
+
try {
|
|
191
|
+
real = await realpath(directory);
|
|
192
|
+
} catch {
|
|
193
|
+
throw notFound(relative);
|
|
194
|
+
}
|
|
195
|
+
if (real !== directory) throw linked(relative);
|
|
196
|
+
return `${info.dev}:${info.ino}`;
|
|
197
|
+
}
|
|
198
|
+
async function sameChain(chain, relative) {
|
|
199
|
+
for (const [index, directory] of chain.paths.entries()) {
|
|
200
|
+
let now;
|
|
201
|
+
try {
|
|
202
|
+
now = await directoryIdentity(directory, relative);
|
|
203
|
+
} catch {
|
|
204
|
+
throw linked(relative);
|
|
205
|
+
}
|
|
206
|
+
if (now !== chain.identities[index]) throw linked(relative);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
async function ignoredOf(root, paths, git) {
|
|
210
|
+
try {
|
|
211
|
+
const ignored = await gitIgnored(root, paths, git);
|
|
212
|
+
return ignored ? (entry) => ignored.has(entry) : () => false;
|
|
213
|
+
} catch (error) {
|
|
214
|
+
if (!(error instanceof IgnoreUnknown)) throw error;
|
|
215
|
+
return () => true;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
async function readConfined(file, maxBytes, probe) {
|
|
219
|
+
const before = await lstat(file, { bigint: true });
|
|
220
|
+
if (before.isSymbolicLink()) throw Object.assign(new Error("link"), { linked: true });
|
|
221
|
+
if (!before.isFile()) throw new Error("not a file");
|
|
222
|
+
if (before.nlink > 1n) throw Object.assign(new Error("hard link"), { linked: true });
|
|
223
|
+
if (await realpath(file) !== file) throw Object.assign(new Error("real path differs"), { linked: true });
|
|
224
|
+
if (before.size > BigInt(maxBytes)) return void 0;
|
|
225
|
+
await probe?.("checked");
|
|
226
|
+
let handle;
|
|
227
|
+
try {
|
|
228
|
+
handle = await open(file, OPEN_FLAGS);
|
|
229
|
+
} catch (error) {
|
|
230
|
+
if (error.code === "ELOOP") throw Object.assign(new Error("link"), { linked: true });
|
|
231
|
+
throw error;
|
|
232
|
+
}
|
|
233
|
+
try {
|
|
234
|
+
const opened = await handle.stat({ bigint: true });
|
|
235
|
+
if (!opened.isFile() || opened.dev !== before.dev || opened.ino !== before.ino || opened.nlink > 1n) throw Object.assign(new Error("changed"), { linked: true });
|
|
236
|
+
const buffer = Buffer.alloc(maxBytes + 1);
|
|
237
|
+
let length = 0;
|
|
238
|
+
while (length < buffer.length) {
|
|
239
|
+
const { bytesRead } = await handle.read(buffer, length, buffer.length - length, length);
|
|
240
|
+
if (!bytesRead) break;
|
|
241
|
+
length += bytesRead;
|
|
242
|
+
}
|
|
243
|
+
if (length > maxBytes) return void 0;
|
|
244
|
+
await probe?.("read");
|
|
245
|
+
const after = await lstat(file, { bigint: true });
|
|
246
|
+
if (after.dev !== before.dev || after.ino !== before.ino) throw Object.assign(new Error("changed"), { linked: true });
|
|
247
|
+
return { bytes: buffer.subarray(0, length), modifiedAt: opened.mtime };
|
|
248
|
+
} finally {
|
|
249
|
+
await handle.close();
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
function likelyBinary(buffer) {
|
|
253
|
+
if (!buffer.length) return false;
|
|
254
|
+
let suspicious = 0;
|
|
255
|
+
for (const byte of buffer) {
|
|
256
|
+
if (byte === 0) return true;
|
|
257
|
+
if (byte < 32 && byte !== 9 && byte !== 10 && byte !== 13 || byte === 127) suspicious++;
|
|
258
|
+
}
|
|
259
|
+
return suspicious / buffer.length > 0.3;
|
|
260
|
+
}
|
|
261
|
+
var PRIVATE_KEY = /-----BEGIN (?:[A-Z0-9]+ )*PRIVATE KEY(?: BLOCK)?-----|PuTTY-User-Key-File-\d+:/;
|
|
262
|
+
var UTF8 = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
|
|
263
|
+
async function listWorkspaceDirectory(projectRoot, { path: relative, includeIgnored = false, now = Date.now, probe, git } = {}) {
|
|
264
|
+
const root = await realRoot(projectRoot);
|
|
265
|
+
const segments = relative === void 0 ? [] : relativeSegments(relative);
|
|
266
|
+
const shown = relative ?? ".";
|
|
267
|
+
const chain = await directoryChain(root, segments, shown);
|
|
268
|
+
const directory = chain.paths.at(-1);
|
|
269
|
+
const prefix = segments.length ? `${relative}/` : "";
|
|
270
|
+
const deadline = now() + LIST_LIMITS.ms;
|
|
271
|
+
const candidates = [];
|
|
272
|
+
let scanned = 0;
|
|
273
|
+
let truncated = false;
|
|
274
|
+
try {
|
|
275
|
+
for await (const entry of await opendir(directory)) {
|
|
276
|
+
if (++scanned > LIST_LIMITS.scanned || now() > deadline) {
|
|
277
|
+
truncated = true;
|
|
278
|
+
break;
|
|
279
|
+
}
|
|
280
|
+
const kind = entry.isFile() ? "file" : entry.isDirectory() ? "directory" : void 0;
|
|
281
|
+
if (!kind) continue;
|
|
282
|
+
const child = `${prefix}${entry.name}`;
|
|
283
|
+
if (entry.name.includes("\\") || blockedFilePath(child)) continue;
|
|
284
|
+
candidates.push({ name: entry.name, child, kind });
|
|
285
|
+
}
|
|
286
|
+
} catch {
|
|
287
|
+
throw notFound(shown);
|
|
288
|
+
}
|
|
289
|
+
if (!includeIgnored) {
|
|
290
|
+
const ignored = await ignoredOf(root, [...segments.length ? [relative] : [], ...candidates.map((entry) => entry.child)], git);
|
|
291
|
+
if (segments.length && ignored(relative)) throw new FileAccessError(`${relative} is ignored by .gitignore; ask with includeIgnored to see it`, ERROR.forbidden, "ignored");
|
|
292
|
+
for (let index = candidates.length - 1; index >= 0; index--) if (ignored(candidates[index].child)) candidates.splice(index, 1);
|
|
293
|
+
}
|
|
294
|
+
const entries = [];
|
|
295
|
+
for (const { name, child, kind } of candidates) {
|
|
296
|
+
if (entries.length >= WORKSPACE_FILES.maxListEntries) {
|
|
297
|
+
truncated = true;
|
|
298
|
+
break;
|
|
299
|
+
}
|
|
300
|
+
let info;
|
|
301
|
+
try {
|
|
302
|
+
info = await lstat(path.join(directory, name));
|
|
303
|
+
} catch {
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
if (kind === "file" ? !info.isFile() : !info.isDirectory()) continue;
|
|
307
|
+
entries.push({ name, path: child, kind, ...kind === "file" ? { size: info.size } : {}, modifiedAt: info.mtime.toISOString() });
|
|
308
|
+
}
|
|
309
|
+
await probe?.("listed");
|
|
310
|
+
await sameChain(chain, shown);
|
|
311
|
+
entries.sort((a, b) => (a.kind === b.kind ? 0 : a.kind === "directory" ? -1 : 1) || (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
312
|
+
return { entries, ...truncated ? { truncated: true } : {} };
|
|
313
|
+
}
|
|
314
|
+
async function readWorkspaceFile(projectRoot, { path: relative, includeIgnored = false, probe, git }) {
|
|
315
|
+
const root = await realRoot(projectRoot);
|
|
316
|
+
const segments = relativeSegments(relative);
|
|
317
|
+
const chain = await directoryChain(root, segments.slice(0, -1), relative);
|
|
318
|
+
if (!includeIgnored && (await ignoredOf(root, [relative], git))(relative)) throw new FileAccessError(`${relative} is ignored by .gitignore; ask with includeIgnored to read it`, ERROR.forbidden, "ignored");
|
|
319
|
+
const file = path.join(root, ...segments);
|
|
320
|
+
let read;
|
|
321
|
+
try {
|
|
322
|
+
read = await readConfined(file, WORKSPACE_FILES.maxReadBytes, probe);
|
|
323
|
+
} catch (error) {
|
|
324
|
+
if (error.linked) throw linked(relative);
|
|
325
|
+
throw notFound(relative);
|
|
326
|
+
}
|
|
327
|
+
if (read === void 0) throw new FileAccessError(`${relative} is larger than ${WORKSPACE_FILES.maxReadBytes} bytes`, ERROR.failed, "too_large");
|
|
328
|
+
await sameChain(chain, relative);
|
|
329
|
+
let content;
|
|
330
|
+
try {
|
|
331
|
+
if (likelyBinary(read.bytes)) throw new Error("binary");
|
|
332
|
+
content = UTF8.decode(read.bytes);
|
|
333
|
+
} catch {
|
|
334
|
+
throw new FileAccessError(`${relative} is not a UTF-8 text file`, ERROR.failed, "binary");
|
|
335
|
+
}
|
|
336
|
+
if (PRIVATE_KEY.test(content)) throw new FileAccessError(`${relative} holds a private key; such files are never readable remotely`, ERROR.forbidden, "blocked_path");
|
|
337
|
+
return { path: relative, content, size: read.bytes.length, modifiedAt: read.modifiedAt.toISOString() };
|
|
338
|
+
}
|
|
339
|
+
export {
|
|
340
|
+
FileAccessError,
|
|
341
|
+
FilesError,
|
|
342
|
+
GIT_CHECK2 as GIT_CHECK,
|
|
343
|
+
LIMITS,
|
|
344
|
+
gitChecksRunning,
|
|
345
|
+
listWorkspaceDirectory,
|
|
346
|
+
readWorkspaceFile,
|
|
347
|
+
relativeSegments,
|
|
348
|
+
resolveGit,
|
|
349
|
+
searchWorkspaceFiles
|
|
350
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
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
|
+
/** Git processes running now, for tests. */
|
|
51
|
+
export declare const gitChecksRunning: () => number;
|
|
52
|
+
/**
|
|
53
|
+
* The paths among `paths` (relative to `root`, '/'-separated, already checked) that Git ignores;
|
|
54
|
+
* null when no rules apply (Git is not installed, or `root` is in no repository). Throws
|
|
55
|
+
* IgnoreUnknown on anything else.
|
|
56
|
+
*/
|
|
57
|
+
export declare function gitIgnored(root: string, paths: readonly string[], { binary, timeoutMs }?: GitCheckOptions): Promise<Set<string> | null>;
|