@anhlp/alp-daemon 0.0.0-beta.2 → 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.
Files changed (104) hide show
  1. package/README.md +4 -0
  2. package/dist/THIRD-PARTY-NOTICES.txt +27 -2
  3. package/dist/alpd.js +39374 -17503
  4. package/dist/attachments.d.ts +187 -0
  5. package/dist/attachments.js +856 -0
  6. package/dist/build-manifest.json +41 -2
  7. package/dist/conversation.d.ts +119 -0
  8. package/dist/conversation.js +688 -0
  9. package/dist/disk.d.ts +67 -0
  10. package/dist/disk.js +152 -0
  11. package/dist/files.d.ts +156 -3
  12. package/dist/files.js +402 -59
  13. package/dist/git.d.ts +98 -0
  14. package/dist/git.js +1246 -0
  15. package/dist/gitignore.d.ts +5 -0
  16. package/dist/gitignore.js +26 -7
  17. package/dist/history.d.ts +24 -0
  18. package/dist/history.js +149 -0
  19. package/dist/index.d.ts +7 -0
  20. package/dist/index.js +21 -1
  21. package/dist/lifecycle.d.ts +101 -0
  22. package/dist/lifecycle.js +239 -0
  23. package/dist/main.js +146 -21
  24. package/dist/model-catalog.d.ts +2 -0
  25. package/dist/model-catalog.js +44 -0
  26. package/dist/native-entry.d.ts +2 -0
  27. package/dist/native-entry.js +3 -0
  28. package/dist/native-register.d.ts +1 -0
  29. package/dist/native-register.js +13 -0
  30. package/dist/native-terminal.d.ts +45 -0
  31. package/dist/native-terminal.js +201 -0
  32. package/dist/operations.d.ts +20 -3
  33. package/dist/operations.js +62 -21
  34. package/dist/registry.d.ts +89 -0
  35. package/dist/registry.js +343 -0
  36. package/dist/scheduler.d.ts +96 -0
  37. package/dist/scheduler.js +298 -0
  38. package/dist/schedules.d.ts +70 -0
  39. package/dist/schedules.js +246 -0
  40. package/dist/server.d.ts +14 -2
  41. package/dist/server.js +903 -97
  42. package/dist/store.d.ts +72 -2
  43. package/dist/store.js +111 -4
  44. package/dist/terminal-native/README.md +201 -0
  45. package/dist/terminal-native/prebuilt/darwin-arm64/manifest.json +11 -0
  46. package/dist/terminal-native/prebuilt/darwin-arm64/terminal-owner +0 -0
  47. package/dist/terminal-native/prebuilt/darwin-x64/manifest.json +11 -0
  48. package/dist/terminal-native/prebuilt/darwin-x64/terminal-owner +0 -0
  49. package/dist/terminal-native/prebuilt/linux-arm64-glibc/manifest.json +10 -0
  50. package/dist/terminal-native/prebuilt/linux-arm64-glibc/terminal-owner +0 -0
  51. package/dist/terminal-native/prebuilt/linux-arm64-musl/manifest.json +10 -0
  52. package/dist/terminal-native/prebuilt/linux-arm64-musl/terminal-owner +0 -0
  53. package/dist/terminal-native/prebuilt/linux-x64-glibc/manifest.json +10 -0
  54. package/dist/terminal-native/prebuilt/linux-x64-glibc/terminal-owner +0 -0
  55. package/dist/terminal-native/prebuilt/linux-x64-musl/manifest.json +10 -0
  56. package/dist/terminal-native/prebuilt/linux-x64-musl/terminal-owner +0 -0
  57. package/dist/terminal-native/terminal-owner.c +389 -0
  58. package/dist/terminal-screen.d.ts +24 -0
  59. package/dist/terminal-screen.js +99 -0
  60. package/dist/terminals.d.ts +203 -0
  61. package/dist/terminals.js +570 -0
  62. package/dist/windows-terminal.d.ts +32 -0
  63. package/dist/windows-terminal.js +79 -0
  64. package/dist/worktrees.d.ts +65 -0
  65. package/dist/worktrees.js +318 -0
  66. package/native/README.md +201 -0
  67. package/native/prebuilt/darwin-arm64/manifest.json +11 -0
  68. package/native/prebuilt/darwin-arm64/terminal-owner +0 -0
  69. package/native/prebuilt/darwin-x64/manifest.json +11 -0
  70. package/native/prebuilt/darwin-x64/terminal-owner +0 -0
  71. package/native/prebuilt/linux-arm64-glibc/manifest.json +10 -0
  72. package/native/prebuilt/linux-arm64-glibc/terminal-owner +0 -0
  73. package/native/prebuilt/linux-arm64-musl/manifest.json +10 -0
  74. package/native/prebuilt/linux-arm64-musl/terminal-owner +0 -0
  75. package/native/prebuilt/linux-x64-glibc/manifest.json +10 -0
  76. package/native/prebuilt/linux-x64-glibc/terminal-owner +0 -0
  77. package/native/prebuilt/linux-x64-musl/manifest.json +10 -0
  78. package/native/prebuilt/linux-x64-musl/terminal-owner +0 -0
  79. package/native/terminal-owner.c +389 -0
  80. package/package.json +71 -8
  81. package/src/attachments.ts +908 -0
  82. package/src/conversation.ts +857 -0
  83. package/src/disk.ts +175 -0
  84. package/src/files.ts +535 -71
  85. package/src/git.ts +1220 -0
  86. package/src/gitignore.ts +25 -8
  87. package/src/history.ts +118 -0
  88. package/src/index.ts +7 -0
  89. package/src/lifecycle.ts +283 -0
  90. package/src/main.ts +154 -23
  91. package/src/model-catalog.ts +36 -0
  92. package/src/native-entry.ts +5 -0
  93. package/src/native-register.ts +21 -0
  94. package/src/native-terminal.ts +127 -0
  95. package/src/operations.ts +53 -22
  96. package/src/registry.ts +238 -0
  97. package/src/scheduler.ts +438 -0
  98. package/src/schedules.ts +297 -0
  99. package/src/server.ts +1019 -109
  100. package/src/store.ts +117 -4
  101. package/src/terminal-screen.ts +74 -0
  102. package/src/terminals.ts +653 -0
  103. package/src/windows-terminal.ts +74 -0
  104. package/src/worktrees.ts +382 -0
package/dist/files.js CHANGED
@@ -1,10 +1,12 @@
1
1
  // packages/daemon/src/files.ts
2
+ import { createHash, randomBytes } from "node:crypto";
2
3
  import { constants } from "node:fs";
3
4
  import { lstat, open, opendir, realpath, stat } from "node:fs/promises";
5
+ import os from "node:os";
4
6
  import path from "node:path";
5
7
  import { ERROR, RPC_ERROR, WORKSPACE_FILES, blockedFilePath } from "@anhlp/alp-protocol";
6
- import { gitIgnored, IgnoreUnknown } from "./gitignore.js";
7
- import { gitChecksRunning, resolveGit, GIT_CHECK } from "./gitignore.js";
8
+ import { gitIgnored, IgnoreUnknown, GIT_CHECK } from "./gitignore.js";
9
+ import { gitChecksRunning, resolveGit, GIT_CHECK as GIT_CHECK2 } from "./gitignore.js";
8
10
  var LIMITS = Object.freeze({
9
11
  /** Directory levels below the root that are entered (Paseo's DEFAULT_MAX_DEPTH). */
10
12
  depth: 12,
@@ -61,63 +63,147 @@ function rank(relative, query) {
61
63
  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
64
  var FilesError = class extends Error {
63
65
  };
64
- async function searchWorkspaceFiles(projectRoot, { query = "", limit = LIMITS.defaultLimit, now = Date.now } = {}) {
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;
66
+ async function walk(root, options) {
67
+ const { now, git } = options;
68
+ const deadline = now() + options.ms;
69
+ const realDeadline = Date.now() + options.ms;
76
70
  const found = [];
77
71
  let scanned = 0;
78
72
  let truncated = false;
73
+ let repository;
79
74
  let level = [""];
80
75
  for (let depth = 0; level.length && !truncated; depth++) {
81
- const next = [];
76
+ const levelEntries = [];
82
77
  for (const relative of level) {
83
- if (scanned >= LIMITS.scanned || now() > deadline) {
78
+ if (scanned >= options.scanned || now() > deadline) {
84
79
  truncated = true;
85
80
  break;
86
81
  }
87
82
  const directory = relative ? path.join(root, ...relative.split("/")) : root;
88
- const files = [];
89
- const directories = [];
83
+ const entries = [];
90
84
  try {
91
85
  const before = await identity(directory);
92
86
  if (!before) continue;
93
87
  for await (const entry of await opendir(directory)) {
94
- if (++scanned > LIMITS.scanned || now() > deadline) {
88
+ if (++scanned > options.scanned || now() > deadline) {
95
89
  truncated = true;
96
90
  break;
97
91
  }
98
92
  const name = entry.name;
99
93
  const child = relative ? `${relative}/${name}` : name;
100
- if (entry.isFile()) {
101
- if (name.startsWith(".")) continue;
102
- const ranked = rank(child, wanted);
103
- if (ranked) files.push(ranked);
104
- } else if (entry.isDirectory()) {
105
- if (IGNORED_DIRECTORIES.has(name) || name.startsWith(".") && !HIDDEN_DIRECTORIES.has(name)) continue;
106
- if (depth < LIMITS.depth) directories.push(child);
107
- }
94
+ if (name.includes("\\") || blockedFilePath(child)) continue;
95
+ const kind = entry.isFile() ? "file" : entry.isDirectory() ? "directory" : options.links && entry.isSymbolicLink() ? (await internalTarget(root, path.join(directory, name)))?.kind : void 0;
96
+ if (!kind || !options.keep(child, name, kind, depth)) continue;
97
+ entries.push({ path: child, name, kind, depth, ...entry.isSymbolicLink() ? { link: true } : {} });
108
98
  }
109
99
  if (await identity(directory) !== before) continue;
110
100
  } catch {
111
101
  continue;
112
102
  }
113
- found.push(...files);
114
- next.push(...directories);
103
+ levelEntries.push(...entries);
115
104
  if (truncated) break;
116
105
  }
117
- level = next;
106
+ const remaining = Math.min(deadline - now(), realDeadline - Date.now());
107
+ if (remaining <= 0) {
108
+ truncated = true;
109
+ break;
110
+ }
111
+ let ignored = null;
112
+ if (repository !== false) {
113
+ const asked = levelEntries.map((entry) => entry.path);
114
+ ignored = await gitIgnored(root, asked, { ...git, timeoutMs: Math.min(git?.timeoutMs ?? GIT_CHECK.timeoutMs, remaining) });
115
+ if (asked.length) repository = ignored !== null;
116
+ }
117
+ const admitted = levelEntries.filter((entry) => !ignored?.has(entry.path) && (options.admit?.(entry, repository === true) ?? true));
118
+ found.push(...admitted);
119
+ level = admitted.filter((entry) => entry.kind === "directory" && !entry.link && depth < options.depth).map((entry) => entry.path);
120
+ }
121
+ return { entries: found, truncated };
122
+ }
123
+ async function searchWorkspaceFiles(projectRoot, { query = "", limit = LIMITS.defaultLimit, now = Date.now, git } = {}) {
124
+ const root = await realRoot(projectRoot);
125
+ const wanted = query.trim().replace(/\\/g, "/").replace(/^\.?\/+/, "").toLowerCase();
126
+ const count = Math.max(1, Math.min(LIMITS.maxLimit, Math.trunc(limit)));
127
+ let walked;
128
+ try {
129
+ walked = await walk(root, {
130
+ depth: LIMITS.depth,
131
+ scanned: LIMITS.scanned,
132
+ ms: LIMITS.ms,
133
+ now,
134
+ git,
135
+ keep: (child, name, kind, depth) => kind === "file" ? !name.startsWith(".") && rank(child, wanted) !== null : !IGNORED_DIRECTORIES.has(name) && (!name.startsWith(".") || HIDDEN_DIRECTORIES.has(name)) && depth < LIMITS.depth
136
+ });
137
+ } catch (error) {
138
+ if (!(error instanceof IgnoreUnknown)) throw error;
139
+ return { files: [], truncated: true };
118
140
  }
141
+ const found = walked.entries.filter((entry) => entry.kind === "file").map((entry) => rank(entry.path, wanted));
119
142
  found.sort(compare);
120
- return { files: found.slice(0, count).map((entry) => ({ path: entry.path })), ...truncated ? { truncated: true } : {} };
143
+ return { files: found.slice(0, count).map((entry) => ({ path: entry.path })), ...walked.truncated ? { truncated: true } : {} };
144
+ }
145
+ var FIND_LIMITS = Object.freeze({ depth: 64, scanned: 1e5, ms: 1e4, ttlMs: 6e4, snapshots: 16 });
146
+ var SKIPPED_FOLDERS = /* @__PURE__ */ new Set(["node_modules", "Library", ".git", "dist", "build", ".cache", "target", ".venv", "venv", "__pycache__"]);
147
+ function inOrder(query, candidate) {
148
+ let at = 0;
149
+ for (const char of candidate) if (char === query[at]) at += 1;
150
+ return at >= query.length;
151
+ }
152
+ async function findWorkspaceEntries(projectRoot, { query = "", kinds = ["file"], matchMode = "subsequence", now = Date.now, git } = {}) {
153
+ const root = await realRoot(projectRoot);
154
+ const wanted = query.trim().replace(/^\.\//, "").toLowerCase();
155
+ const matches = (candidate) => !wanted || (matchMode === "suffix" ? candidate === wanted || candidate.endsWith(`/${wanted}`) : inOrder(wanted, candidate));
156
+ let walked;
157
+ try {
158
+ walked = await walk(root, {
159
+ depth: FIND_LIMITS.depth,
160
+ scanned: FIND_LIMITS.scanned,
161
+ ms: FIND_LIMITS.ms,
162
+ now,
163
+ git,
164
+ links: true,
165
+ keep: () => true,
166
+ admit: (entry, repository) => repository || !(entry.name.startsWith(".") || SKIPPED_FOLDERS.has(entry.name))
167
+ });
168
+ } catch (error) {
169
+ if (!(error instanceof IgnoreUnknown)) throw error;
170
+ return { entries: [], truncated: true };
171
+ }
172
+ const entries = walked.entries.filter((entry) => kinds.includes(entry.kind) && matches(entry.path.toLowerCase())).map((entry) => ({ path: entry.path, kind: entry.kind })).sort((a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0);
173
+ return { entries, ...walked.truncated ? { truncated: true } : {} };
174
+ }
175
+ var invalidCursor = () => new FileAccessError("cursor is not one alpd gave for these params, or it expired; ask again without it", RPC_ERROR.invalidParams, "invalid_cursor");
176
+ function createFindPages({ now = Date.now, git } = {}) {
177
+ const snapshots = /* @__PURE__ */ new Map();
178
+ return async function page(owner, projectRoot, { query = "", kinds = ["file"], matchMode = "subsequence", limit = WORKSPACE_FILES.maxFindEntries, cursor } = {}) {
179
+ const count = Math.max(1, Math.min(WORKSPACE_FILES.maxFindEntries, Math.trunc(limit)));
180
+ const key = JSON.stringify([projectRoot, query, [...new Set(kinds)].sort(), matchMode]);
181
+ for (const [id2, snapshot2] of snapshots) if (now() - snapshot2.at > FIND_LIMITS.ttlMs) snapshots.delete(id2);
182
+ let id;
183
+ let offset;
184
+ let snapshot;
185
+ if (cursor !== void 0) {
186
+ const named = /^([A-Za-z0-9_-]{22})\.(\d{1,7})$/.exec(cursor);
187
+ snapshot = named ? snapshots.get(named[1]) : void 0;
188
+ if (!named || !snapshot || snapshot.owner !== owner || snapshot.key !== key || Number(named[2]) > snapshot.entries.length) throw invalidCursor();
189
+ [id, offset] = [named[1], Number(named[2])];
190
+ } else {
191
+ const found = await findWorkspaceEntries(projectRoot, { query, kinds, matchMode, now, git });
192
+ snapshot = { owner, key, entries: found.entries, truncated: found.truncated === true, at: now() };
193
+ [id, offset] = [randomBytes(16).toString("base64url"), 0];
194
+ }
195
+ const entries = snapshot.entries.slice(offset, offset + count);
196
+ const end = offset + entries.length;
197
+ if (end < snapshot.entries.length) {
198
+ snapshot.at = now();
199
+ snapshots.set(id, snapshot);
200
+ for (const old of snapshots.keys()) {
201
+ if (snapshots.size <= FIND_LIMITS.snapshots) break;
202
+ snapshots.delete(old);
203
+ }
204
+ } else snapshots.delete(id);
205
+ return { entries, ...end < snapshot.entries.length ? { nextCursor: `${id}.${end}` } : {}, ...snapshot.truncated ? { truncated: true } : {} };
206
+ };
121
207
  }
122
208
  var FileAccessError = class extends Error {
123
209
  constructor(message, code, reason) {
@@ -130,6 +216,41 @@ var LIST_LIMITS = Object.freeze({ scanned: 2e4, ms: 3e3 });
130
216
  var OPEN_FLAGS = process.platform === "win32" ? constants.O_RDONLY : constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK;
131
217
  var notFound = (relative) => new FileAccessError(`No readable file or directory at ${relative}`, ERROR.notFound, "not_found");
132
218
  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");
219
+ var outside = (relative) => new FileAccessError(`${relative} is a link that leads outside the workspace; alpd follows only links that stay inside it`, ERROR.forbidden, "outside");
220
+ function insideRoot(root, real) {
221
+ const relative = path.relative(root, real);
222
+ if (!relative || relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)) return void 0;
223
+ const inside = relative.split(path.sep).join("/");
224
+ return inside.includes("\\") ? void 0 : inside;
225
+ }
226
+ async function internalSegments(root, segments, relative) {
227
+ let real;
228
+ try {
229
+ real = await realpath(path.join(root, ...segments));
230
+ } catch {
231
+ throw notFound(relative);
232
+ }
233
+ let via;
234
+ for (let index = 1; index <= segments.length && via === void 0; index++) {
235
+ if ((await lstat(path.join(root, ...segments.slice(0, index))).catch(() => void 0))?.isSymbolicLink()) via = segments.slice(0, index).join("/");
236
+ }
237
+ if (real === root) return { segments: [], via };
238
+ const inside = insideRoot(root, real);
239
+ if (inside === void 0) throw outside(relative);
240
+ if (blockedFilePath(inside)) throw new FileAccessError(`${relative} leads to a path that is not readable remotely: .git, .env files, ALP configuration and key or credential files are always refused`, ERROR.forbidden, "blocked_path");
241
+ return { segments: inside.split("/"), via };
242
+ }
243
+ async function internalTarget(root, file) {
244
+ try {
245
+ const real = await realpath(file);
246
+ const target = insideRoot(root, real);
247
+ if (target === void 0 || blockedFilePath(target)) return void 0;
248
+ const info = await lstat(real);
249
+ return info.isFile() ? { kind: "file", target } : info.isDirectory() ? { kind: "directory", target } : void 0;
250
+ } catch {
251
+ return void 0;
252
+ }
253
+ }
133
254
  async function realRoot(projectRoot) {
134
255
  if (!path.isAbsolute(projectRoot)) throw new FilesError("projectRoot must be an absolute path");
135
256
  try {
@@ -197,6 +318,27 @@ async function ignoredOf(root, paths, git) {
197
318
  }
198
319
  }
199
320
  async function readConfined(file, maxBytes, probe) {
321
+ const confined = await openConfined(file, maxBytes, probe);
322
+ if (!confined) return void 0;
323
+ const { handle, before, opened } = confined;
324
+ try {
325
+ const buffer = Buffer.alloc(maxBytes + 1);
326
+ let length = 0;
327
+ while (length < buffer.length) {
328
+ const { bytesRead } = await handle.read(buffer, length, buffer.length - length, length);
329
+ if (!bytesRead) break;
330
+ length += bytesRead;
331
+ }
332
+ if (length > maxBytes) return void 0;
333
+ await probe?.("read");
334
+ const after = await lstat(file, { bigint: true });
335
+ if (after.dev !== before.dev || after.ino !== before.ino) throw Object.assign(new Error("changed"), { linked: true });
336
+ return { bytes: buffer.subarray(0, length), modifiedAt: opened.mtime };
337
+ } finally {
338
+ await handle.close();
339
+ }
340
+ }
341
+ async function openConfined(file, maxBytes, probe) {
200
342
  const before = await lstat(file, { bigint: true });
201
343
  if (before.isSymbolicLink()) throw Object.assign(new Error("link"), { linked: true });
202
344
  if (!before.isFile()) throw new Error("not a file");
@@ -214,21 +356,23 @@ async function readConfined(file, maxBytes, probe) {
214
356
  try {
215
357
  const opened = await handle.stat({ bigint: true });
216
358
  if (!opened.isFile() || opened.dev !== before.dev || opened.ino !== before.ino || opened.nlink > 1n) throw Object.assign(new Error("changed"), { linked: true });
217
- const buffer = Buffer.alloc(maxBytes + 1);
218
- let length = 0;
219
- while (length < buffer.length) {
220
- const { bytesRead } = await handle.read(buffer, length, buffer.length - length, length);
221
- if (!bytesRead) break;
222
- length += bytesRead;
223
- }
224
- if (length > maxBytes) return void 0;
225
- await probe?.("read");
226
- const after = await lstat(file, { bigint: true });
227
- if (after.dev !== before.dev || after.ino !== before.ino) throw Object.assign(new Error("changed"), { linked: true });
228
- return { bytes: buffer.subarray(0, length), modifiedAt: opened.mtime };
229
- } finally {
359
+ return { handle, before, opened };
360
+ } catch (error) {
230
361
  await handle.close();
362
+ throw error;
363
+ }
364
+ }
365
+ async function readConfinedFile(root, segments, relative, maxBytes) {
366
+ const chain = await directoryChain(root, segments.slice(0, -1), relative);
367
+ let read;
368
+ try {
369
+ read = await readConfined(path.join(root, ...segments), maxBytes);
370
+ } catch (error) {
371
+ if (error.linked) throw linked(relative);
372
+ throw notFound(relative);
231
373
  }
374
+ await sameChain(chain, relative);
375
+ return read === void 0 ? "too_large" : read.bytes;
232
376
  }
233
377
  function likelyBinary(buffer) {
234
378
  if (!buffer.length) return false;
@@ -241,13 +385,31 @@ function likelyBinary(buffer) {
241
385
  }
242
386
  var PRIVATE_KEY = /-----BEGIN (?:[A-Z0-9]+ )*PRIVATE KEY(?: BLOCK)?-----|PuTTY-User-Key-File-\d+:/;
243
387
  var UTF8 = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
244
- async function listWorkspaceDirectory(projectRoot, { path: relative, includeIgnored = false, now = Date.now, probe, git } = {}) {
388
+ var listOrder = (a, b) => (a.kind === b.kind ? 0 : a.kind === "directory" ? -1 : 1) || (a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
389
+ var listCursor = (entry) => Buffer.from(JSON.stringify([entry.kind === "directory" ? 0 : 1, entry.name])).toString("base64url");
390
+ function afterCursor(cursor) {
391
+ let value;
392
+ try {
393
+ value = JSON.parse(Buffer.from(cursor, "base64url").toString("utf8"));
394
+ } catch {
395
+ value = void 0;
396
+ }
397
+ if (!Array.isArray(value) || value.length !== 2 || value[0] !== 0 && value[0] !== 1 || typeof value[1] !== "string" || !value[1]) {
398
+ throw new FileAccessError("cursor is not one alpd gave for a listing", RPC_ERROR.invalidParams, "invalid_cursor");
399
+ }
400
+ return { kind: value[0] === 0 ? "directory" : "file", name: value[1] };
401
+ }
402
+ async function listWorkspaceDirectory(projectRoot, { path: relative, includeIgnored = false, cursor, internalLinks = false, now = Date.now, probe, git } = {}) {
245
403
  const root = await realRoot(projectRoot);
246
- const segments = relative === void 0 ? [] : relativeSegments(relative);
404
+ const requested = relative === void 0 ? [] : relativeSegments(relative);
247
405
  const shown = relative ?? ".";
406
+ const after = cursor === void 0 ? void 0 : afterCursor(cursor);
407
+ const { segments, via } = internalLinks && requested.length ? await internalSegments(root, requested, shown) : { segments: requested, via: void 0 };
408
+ const real = segments.join("/");
409
+ const target = real !== requested.join("/") ? real || "." : void 0;
248
410
  const chain = await directoryChain(root, segments, shown);
249
411
  const directory = chain.paths.at(-1);
250
- const prefix = segments.length ? `${relative}/` : "";
412
+ const prefix = segments.length ? `${real}/` : "";
251
413
  const deadline = now() + LIST_LIMITS.ms;
252
414
  const candidates = [];
253
415
  let scanned = 0;
@@ -258,39 +420,49 @@ async function listWorkspaceDirectory(projectRoot, { path: relative, includeIgno
258
420
  truncated = true;
259
421
  break;
260
422
  }
261
- const kind = entry.isFile() ? "file" : entry.isDirectory() ? "directory" : void 0;
262
- if (!kind) continue;
263
423
  const child = `${prefix}${entry.name}`;
264
424
  if (entry.name.includes("\\") || blockedFilePath(child)) continue;
265
- candidates.push({ name: entry.name, child, kind });
425
+ const kind = entry.isFile() ? "file" : entry.isDirectory() ? "directory" : void 0;
426
+ if (kind) candidates.push({ name: entry.name, child, kind });
427
+ else if (internalLinks && entry.isSymbolicLink()) {
428
+ const link = await internalTarget(root, path.join(directory, entry.name));
429
+ if (link) candidates.push({ name: entry.name, child, ...link });
430
+ }
266
431
  }
267
432
  } catch {
268
433
  throw notFound(shown);
269
434
  }
270
435
  if (!includeIgnored) {
271
- const ignored = await ignoredOf(root, [...segments.length ? [relative] : [], ...candidates.map((entry) => entry.child)], git);
272
- if (segments.length && ignored(relative)) throw new FileAccessError(`${relative} is ignored by .gitignore; ask with includeIgnored to see it`, ERROR.forbidden, "ignored");
273
- for (let index = candidates.length - 1; index >= 0; index--) if (ignored(candidates[index].child)) candidates.splice(index, 1);
436
+ const listed = [.../* @__PURE__ */ new Set([...requested.length ? [via ?? relative] : [], ...segments.length ? [real] : []])];
437
+ const ignored = await ignoredOf(root, [...listed, ...candidates.flatMap((entry) => entry.target ? [entry.child, entry.target] : [entry.child])], git);
438
+ if (listed.some(ignored)) throw new FileAccessError(`${relative} is ignored by .gitignore; ask with includeIgnored to see it`, ERROR.forbidden, "ignored");
439
+ for (let index = candidates.length - 1; index >= 0; index--) {
440
+ const candidate = candidates[index];
441
+ if (ignored(candidate.child) || candidate.target !== void 0 && ignored(candidate.target)) candidates.splice(index, 1);
442
+ }
274
443
  }
444
+ candidates.sort(listOrder);
445
+ const start = after ? candidates.findIndex((candidate) => listOrder(candidate, after) > 0) : 0;
275
446
  const entries = [];
276
- for (const { name, child, kind } of candidates) {
447
+ let nextCursor;
448
+ for (const { name, child, kind, target: leadsTo } of start < 0 ? [] : candidates.slice(start)) {
277
449
  if (entries.length >= WORKSPACE_FILES.maxListEntries) {
278
450
  truncated = true;
451
+ nextCursor = listCursor(entries.at(-1));
279
452
  break;
280
453
  }
281
454
  let info;
282
455
  try {
283
- info = await lstat(path.join(directory, name));
456
+ info = await lstat(leadsTo === void 0 ? path.join(directory, name) : path.join(root, ...leadsTo.split("/")));
284
457
  } catch {
285
458
  continue;
286
459
  }
287
460
  if (kind === "file" ? !info.isFile() : !info.isDirectory()) continue;
288
- entries.push({ name, path: child, kind, ...kind === "file" ? { size: info.size } : {}, modifiedAt: info.mtime.toISOString() });
461
+ entries.push({ name, path: child, kind, ...kind === "file" ? { size: info.size } : {}, modifiedAt: info.mtime.toISOString(), ...leadsTo === void 0 ? {} : { target: leadsTo } });
289
462
  }
290
463
  await probe?.("listed");
291
464
  await sameChain(chain, shown);
292
- 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));
293
- return { entries, ...truncated ? { truncated: true } : {} };
465
+ return { entries, ...truncated ? { truncated: true } : {}, ...nextCursor ? { nextCursor } : {}, ...target ? { target } : {} };
294
466
  }
295
467
  async function readWorkspaceFile(projectRoot, { path: relative, includeIgnored = false, probe, git }) {
296
468
  const root = await realRoot(projectRoot);
@@ -317,15 +489,186 @@ async function readWorkspaceFile(projectRoot, { path: relative, includeIgnored =
317
489
  if (PRIVATE_KEY.test(content)) throw new FileAccessError(`${relative} holds a private key; such files are never readable remotely`, ERROR.forbidden, "blocked_path");
318
490
  return { path: relative, content, size: read.bytes.length, modifiedAt: read.modifiedAt.toISOString() };
319
491
  }
492
+ var PREVIEW_IMAGES = Object.freeze({ ".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".gif": "image/gif", ".webp": "image/webp", ".svg": "image/svg+xml" });
493
+ var KEY_OVERLAP = 256;
494
+ var CLASSIFY_BLOCK = 1024 * 1024;
495
+ var revisionOf = (info, kind) => `r${createHash("sha256").update(`${info.dev}:${info.ino}:${info.size}:${info.mtimeNs}:${info.ctimeNs}:${kind}`).digest("base64url").slice(0, 22)}`;
496
+ async function classify(handle, size) {
497
+ const decoder = new TextDecoder("utf-8", { fatal: true });
498
+ const block = Buffer.alloc(Math.max(1, Math.min(CLASSIFY_BLOCK, size)));
499
+ let tail = "";
500
+ let key = false;
501
+ for (let position = 0; position < size; ) {
502
+ const { bytesRead } = await handle.read(block, 0, Math.min(block.length, size - position), position);
503
+ if (!bytesRead) throw Object.assign(new Error("shrank"), { linked: true });
504
+ const bytes = block.subarray(0, bytesRead);
505
+ if (bytes.includes(0)) return "binary";
506
+ if (position === 0) {
507
+ const head = bytes.subarray(0, 16 * 1024);
508
+ let control = 0;
509
+ for (const byte of head) if (byte < 9 || byte > 13 && byte < 32) control++;
510
+ if (control / head.length > 0.3) return "binary";
511
+ }
512
+ try {
513
+ decoder.decode(bytes, { stream: true });
514
+ } catch {
515
+ return "binary";
516
+ }
517
+ const text = tail + bytes.toString("latin1");
518
+ key ||= PRIVATE_KEY.test(text);
519
+ tail = text.slice(-KEY_OVERLAP);
520
+ position += bytesRead;
521
+ }
522
+ try {
523
+ decoder.decode();
524
+ } catch {
525
+ return "binary";
526
+ }
527
+ return key ? "key" : "text";
528
+ }
529
+ async function readRange(handle, from, end) {
530
+ const buffer = Buffer.alloc(end - from);
531
+ let length = 0;
532
+ while (length < buffer.length) {
533
+ const { bytesRead } = await handle.read(buffer, length, buffer.length - length, from + length);
534
+ if (!bytesRead) throw Object.assign(new Error("shrank"), { linked: true });
535
+ length += bytesRead;
536
+ }
537
+ return buffer;
538
+ }
539
+ async function previewWorkspaceFile(projectRoot, { path: relative, includeIgnored = false, internalLinks = false, maxBytes = WORKSPACE_FILES.maxPreviewBytes, offset = 0, revision, probe, git }) {
540
+ const root = await realRoot(projectRoot);
541
+ const requested = relativeSegments(relative);
542
+ const { segments, via } = internalLinks ? await internalSegments(root, requested, relative) : { segments: requested, via: void 0 };
543
+ if (!segments.length) throw notFound(relative);
544
+ const real = segments.join("/");
545
+ const target = real !== relative ? real : void 0;
546
+ const limit = Math.max(1, Math.min(WORKSPACE_FILES.maxPreviewBytes, Math.trunc(maxBytes)));
547
+ const chain = await directoryChain(root, segments.slice(0, -1), relative);
548
+ if (!includeIgnored) {
549
+ const asked = [.../* @__PURE__ */ new Set([via ?? relative, real])];
550
+ if (asked.some(await ignoredOf(root, asked, git))) throw new FileAccessError(`${relative} is ignored by .gitignore; ask with includeIgnored to read it`, ERROR.forbidden, "ignored");
551
+ }
552
+ const file = path.join(root, ...segments);
553
+ const extension = path.extname(file).toLowerCase();
554
+ let preview;
555
+ try {
556
+ const confined = await openConfined(file, limit, probe);
557
+ if (!confined) throw new FileAccessError(`${relative} is larger than ${limit} bytes`, ERROR.failed, "too_large");
558
+ const { handle, before, opened } = confined;
559
+ try {
560
+ const size = Number(opened.size);
561
+ if (offset > 0 && revision === void 0) throw new FileAccessError("a chunk past offset 0 needs the revision its first chunk answered", RPC_ERROR.invalidParams, "invalid_params");
562
+ const image = PREVIEW_IMAGES[extension];
563
+ const kind = image ? "image" : offset > 0 ? "text" : await classify(handle, size);
564
+ if (kind === "key") throw new FileAccessError(`${relative} holds a private key; such files are never readable remotely`, ERROR.forbidden, "blocked_path");
565
+ const current = revisionOf(opened, kind);
566
+ if (revision !== void 0 && revision !== current) throw new FileAccessError(`${relative} changed since its first chunk was read; read it again from offset 0`, ERROR.conflict, "changed");
567
+ if (offset > 0 && offset >= size) throw new FileAccessError(`offset is past the end of ${relative}`, RPC_ERROR.invalidParams, "invalid_params");
568
+ const mimeType = image ?? (kind === "binary" ? "application/octet-stream" : extension === ".json" ? "application/json" : "text/plain");
569
+ const modifiedAt = new Date(Number(opened.mtimeNs / 1000000n) + (opened.mtimeNs % 1000000n >= 500000n ? 1 : 0)).toISOString();
570
+ const base = { path: relative, ...target === void 0 ? {} : { target }, kind, mimeType, size, modifiedAt, revision: current };
571
+ if (kind === "binary") preview = { ...base, encoding: "none" };
572
+ else {
573
+ const end = Math.min(size, offset + WORKSPACE_FILES.maxPreviewChunkBytes);
574
+ const from = Math.max(0, offset - KEY_OVERLAP);
575
+ const bytes = await readRange(handle, from, end);
576
+ if (PRIVATE_KEY.test(bytes.toString("latin1"))) throw new FileAccessError(`${relative} holds a private key; such files are never readable remotely`, ERROR.forbidden, "blocked_path");
577
+ preview = { ...base, encoding: "base64", content: bytes.subarray(offset - from).toString("base64"), offset, ...end < size ? { nextOffset: end } : {} };
578
+ }
579
+ await probe?.("read");
580
+ const after = await lstat(file, { bigint: true });
581
+ if (after.dev !== before.dev || after.ino !== before.ino) throw Object.assign(new Error("changed"), { linked: true });
582
+ } finally {
583
+ await handle.close();
584
+ }
585
+ } catch (error) {
586
+ if (error instanceof FileAccessError) throw error;
587
+ if (error.linked) throw linked(relative);
588
+ throw notFound(relative);
589
+ }
590
+ await sameChain(chain, relative);
591
+ return preview;
592
+ }
593
+ var HOST_DIRECTORIES = Object.freeze({ depth: 4, scanned: 5e3, listed: 2e4, defaultLimit: 30, maxLimit: 100 });
594
+ function nameRank(candidate, query) {
595
+ const name = candidate.toLowerCase();
596
+ const wanted = query.toLowerCase();
597
+ if (name === wanted) return 0;
598
+ if (name.startsWith(wanted)) return 1;
599
+ if (name.includes(wanted)) return 2;
600
+ return inOrder(wanted, name) ? 3 : void 0;
601
+ }
602
+ async function subdirectories(directory, max) {
603
+ const names = [];
604
+ let looked = 0;
605
+ try {
606
+ for await (const entry of await opendir(directory)) {
607
+ if (++looked > max) return { names, truncated: true };
608
+ if (entry.isDirectory()) names.push(entry.name);
609
+ }
610
+ } catch {
611
+ }
612
+ return { names, truncated: false };
613
+ }
614
+ async function searchHostDirectories(query, { limit = HOST_DIRECTORIES.defaultLimit, home = os.homedir() } = {}) {
615
+ const typed = typeof query === "string" ? query.trim() : "";
616
+ const count = Math.max(1, Math.min(HOST_DIRECTORIES.maxLimit, Math.trunc(limit)));
617
+ const done = (paths, truncated) => ({ entries: paths.slice(0, count).map((entry) => ({ path: entry })), ...truncated ? { truncated: true } : {} });
618
+ if (!typed) return done([], false);
619
+ if (path.isAbsolute(typed) || typed.startsWith("~")) {
620
+ const expanded = typed === "~" ? home : typed.startsWith("~/") ? path.join(home, typed.slice(2)) : typed;
621
+ if (!path.isAbsolute(expanded)) return done([], false);
622
+ const folder = expanded.endsWith("/") || expanded.endsWith(path.sep);
623
+ const parent = folder ? expanded : path.dirname(expanded);
624
+ const prefix = folder ? "" : path.basename(expanded).toLowerCase();
625
+ const { names, truncated } = await subdirectories(parent, HOST_DIRECTORIES.listed);
626
+ const matching = names.filter((name) => name.toLowerCase().startsWith(prefix) && (!name.startsWith(".") || prefix.startsWith("."))).sort((a, b) => a.localeCompare(b));
627
+ return done(matching.map((name) => path.join(parent, name)), truncated);
628
+ }
629
+ const found = [];
630
+ let queue = [home];
631
+ let scanned = 0;
632
+ for (let depth = 1; depth <= HOST_DIRECTORIES.depth && queue.length && scanned < HOST_DIRECTORIES.scanned; depth += 1) {
633
+ const next = [];
634
+ for (const directory of queue) {
635
+ for (const name of (await subdirectories(directory, HOST_DIRECTORIES.listed)).names) {
636
+ if (name.startsWith(".") || SKIPPED_FOLDERS.has(name)) continue;
637
+ const full = path.join(directory, name);
638
+ scanned += 1;
639
+ const score = nameRank(name, typed);
640
+ if (score !== void 0) found.push({ path: full, score, depth });
641
+ next.push(full);
642
+ if (scanned >= HOST_DIRECTORIES.scanned) break;
643
+ }
644
+ if (scanned >= HOST_DIRECTORIES.scanned) break;
645
+ }
646
+ queue = next;
647
+ }
648
+ found.sort((a, b) => a.score - b.score || a.depth - b.depth || a.path.localeCompare(b.path));
649
+ return done(found.map((entry) => entry.path), scanned >= HOST_DIRECTORIES.scanned);
650
+ }
320
651
  export {
652
+ FIND_LIMITS,
321
653
  FileAccessError,
322
654
  FilesError,
323
- GIT_CHECK,
655
+ GIT_CHECK2 as GIT_CHECK,
656
+ HOST_DIRECTORIES,
324
657
  LIMITS,
658
+ PREVIEW_IMAGES,
659
+ PRIVATE_KEY,
660
+ createFindPages,
661
+ findWorkspaceEntries,
325
662
  gitChecksRunning,
663
+ inOrder,
664
+ likelyBinary,
326
665
  listWorkspaceDirectory,
666
+ nameRank,
667
+ previewWorkspaceFile,
668
+ readConfinedFile,
327
669
  readWorkspaceFile,
328
670
  relativeSegments,
329
671
  resolveGit,
672
+ searchHostDirectories,
330
673
  searchWorkspaceFiles
331
674
  };