@anhlp/alp-daemon 0.0.1-beta.4 → 0.0.1-beta.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/dist/THIRD-PARTY-NOTICES.txt +26 -1
  2. package/dist/alpd.js +11047 -3021
  3. package/dist/attachments.d.ts +25 -49
  4. package/dist/attachments.js +45 -8
  5. package/dist/build-manifest.json +14 -1
  6. package/dist/conversation.js +18 -2
  7. package/dist/disk.d.ts +67 -0
  8. package/dist/disk.js +152 -0
  9. package/dist/files.d.ts +141 -2
  10. package/dist/files.js +377 -68
  11. package/dist/git.d.ts +8 -2
  12. package/dist/git.js +111 -12
  13. package/dist/index.d.ts +1 -0
  14. package/dist/index.js +3 -0
  15. package/dist/main.js +37 -11
  16. package/dist/native-terminal.d.ts +45 -0
  17. package/dist/native-terminal.js +198 -0
  18. package/dist/operations.d.ts +8 -2
  19. package/dist/operations.js +20 -10
  20. package/dist/registry.js +1 -1
  21. package/dist/scheduler.js +8 -2
  22. package/dist/schedules.d.ts +2 -1
  23. package/dist/schedules.js +6 -4
  24. package/dist/server.d.ts +4 -2
  25. package/dist/server.js +166 -42
  26. package/dist/store.d.ts +3 -1
  27. package/dist/store.js +4 -3
  28. package/dist/terminal-native/README.md +201 -0
  29. package/dist/terminal-native/prebuilt/darwin-arm64/manifest.json +11 -0
  30. package/dist/terminal-native/prebuilt/darwin-arm64/terminal-owner +0 -0
  31. package/dist/terminal-native/prebuilt/darwin-x64/manifest.json +11 -0
  32. package/dist/terminal-native/prebuilt/darwin-x64/terminal-owner +0 -0
  33. package/dist/terminal-native/prebuilt/linux-arm64-glibc/manifest.json +10 -0
  34. package/dist/terminal-native/prebuilt/linux-arm64-glibc/terminal-owner +0 -0
  35. package/dist/terminal-native/prebuilt/linux-arm64-musl/manifest.json +10 -0
  36. package/dist/terminal-native/prebuilt/linux-arm64-musl/terminal-owner +0 -0
  37. package/dist/terminal-native/prebuilt/linux-x64-glibc/manifest.json +10 -0
  38. package/dist/terminal-native/prebuilt/linux-x64-glibc/terminal-owner +0 -0
  39. package/dist/terminal-native/prebuilt/linux-x64-musl/manifest.json +10 -0
  40. package/dist/terminal-native/prebuilt/linux-x64-musl/terminal-owner +0 -0
  41. package/dist/terminal-native/terminal-owner.c +389 -0
  42. package/dist/terminal-screen.d.ts +24 -0
  43. package/dist/terminal-screen.js +99 -0
  44. package/dist/terminals.d.ts +43 -7
  45. package/dist/terminals.js +222 -139
  46. package/dist/windows-terminal.d.ts +29 -0
  47. package/dist/windows-terminal.js +122 -0
  48. package/dist/worktrees.d.ts +25 -1
  49. package/dist/worktrees.js +74 -2
  50. package/native/README.md +201 -0
  51. package/native/prebuilt/darwin-arm64/manifest.json +11 -0
  52. package/native/prebuilt/darwin-arm64/terminal-owner +0 -0
  53. package/native/prebuilt/darwin-x64/manifest.json +11 -0
  54. package/native/prebuilt/darwin-x64/terminal-owner +0 -0
  55. package/native/prebuilt/linux-arm64-glibc/manifest.json +10 -0
  56. package/native/prebuilt/linux-arm64-glibc/terminal-owner +0 -0
  57. package/native/prebuilt/linux-arm64-musl/manifest.json +10 -0
  58. package/native/prebuilt/linux-arm64-musl/terminal-owner +0 -0
  59. package/native/prebuilt/linux-x64-glibc/manifest.json +10 -0
  60. package/native/prebuilt/linux-x64-glibc/terminal-owner +0 -0
  61. package/native/prebuilt/linux-x64-musl/manifest.json +10 -0
  62. package/native/prebuilt/linux-x64-musl/terminal-owner +0 -0
  63. package/native/terminal-owner.c +389 -0
  64. package/package.json +11 -7
  65. package/src/attachments.ts +46 -8
  66. package/src/conversation.ts +22 -2
  67. package/src/disk.ts +175 -0
  68. package/src/files.ts +505 -80
  69. package/src/git.ts +113 -18
  70. package/src/index.ts +1 -0
  71. package/src/main.ts +30 -11
  72. package/src/native-terminal.ts +122 -0
  73. package/src/operations.ts +27 -11
  74. package/src/registry.ts +1 -1
  75. package/src/scheduler.ts +13 -3
  76. package/src/schedules.ts +5 -2
  77. package/src/server.ts +157 -35
  78. package/src/store.ts +2 -1
  79. package/src/terminal-screen.ts +74 -0
  80. package/src/terminals.ts +205 -155
  81. package/src/windows-terminal.ts +123 -0
  82. package/src/worktrees.ts +97 -2
package/dist/git.js CHANGED
@@ -1,8 +1,7 @@
1
1
  // packages/daemon/src/git.ts
2
2
  import { spawn } from "node:child_process";
3
3
  import { createHash, createHmac, randomBytes, timingSafeEqual } from "node:crypto";
4
- import { lstat, realpath } from "node:fs/promises";
5
- import os from "node:os";
4
+ import { lstat, realpath, stat } from "node:fs/promises";
6
5
  import path from "node:path";
7
6
  import { user_worktree } from "@anhlp/alp-core";
8
7
  import { ERROR, RPC_ERROR, WORKSPACE_GIT, blockedFilePath } from "@anhlp/alp-protocol";
@@ -41,7 +40,11 @@ var FIXED = Object.freeze({
41
40
  unknownRef: "compare.baseRef names no branch of the repository",
42
41
  stale: "The checkout changed since the cursor was given; ask again without a cursor",
43
42
  invalidCursor: "cursor is not one alpd gave for this workspace and comparison",
44
- notFound: "No workspace with this workspaceId"
43
+ notFound: "No workspace with this workspaceId",
44
+ noRegistry: "Registry needs ALP_HOME",
45
+ invalidDirectory: "directory must be an absolute, normalised path with no . or .. segment",
46
+ noDirectory: "No directory at this path",
47
+ notDirectory: "The path is not a directory"
45
48
  });
46
49
  var GitReadFailed = class extends Error {
47
50
  constructor(cause) {
@@ -83,13 +86,14 @@ function acquire(deadline) {
83
86
  });
84
87
  }
85
88
  var gitReadsRunning = () => running;
89
+ var NULL_DEVICE = process.platform === "win32" ? "NUL" : "/dev/null";
86
90
  var BOXED = [
87
91
  "--no-pager",
88
92
  "--literal-pathspecs",
89
93
  "-c",
90
94
  "core.fsmonitor=false",
91
95
  "-c",
92
- `core.hooksPath=${os.devNull}`,
96
+ `core.hooksPath=${NULL_DEVICE}`,
93
97
  "-c",
94
98
  "core.pager=cat",
95
99
  "-c",
@@ -109,7 +113,7 @@ var BOXED = [
109
113
  "-c",
110
114
  "diff.suppressBlankEmpty=false",
111
115
  "-c",
112
- `diff.orderFile=${os.devNull}`,
116
+ `diff.orderFile=${NULL_DEVICE}`,
113
117
  "-c",
114
118
  "log.showSignature=false",
115
119
  "-c",
@@ -234,7 +238,7 @@ async function locate(root) {
234
238
  for (let directory = path.dirname(root); ; directory = path.dirname(directory)) {
235
239
  try {
236
240
  await lstat(path.join(directory, ".git"));
237
- return { kind: "unsupported" };
241
+ return { kind: "unsupported", top: directory };
238
242
  } catch (error) {
239
243
  if (!missing(error)) failed("cannot examine .git above the workspace");
240
244
  }
@@ -264,7 +268,7 @@ function environment(root, gitDir) {
264
268
  GIT_DIR: gitDir,
265
269
  GIT_WORK_TREE: root,
266
270
  GIT_CONFIG_NOSYSTEM: "1",
267
- GIT_CONFIG_GLOBAL: os.devNull,
271
+ GIT_CONFIG_GLOBAL: NULL_DEVICE,
268
272
  GIT_ATTR_NOSYSTEM: "1",
269
273
  GIT_OPTIONAL_LOCKS: "0",
270
274
  GIT_TERMINAL_PROMPT: "0",
@@ -324,12 +328,18 @@ function scrubRemoteUrl(url) {
324
328
  if (scheme) {
325
329
  const protocol = scheme[1].toLowerCase();
326
330
  if (protocol === "file") return null;
331
+ try {
332
+ if (!new URL(value).host) return null;
333
+ } catch {
334
+ return null;
335
+ }
327
336
  const host = scheme[2].slice(scheme[2].lastIndexOf("@") + 1);
328
337
  return host ? `${protocol}://${host}${scheme[3]}` : null;
329
338
  }
330
339
  if (/^file:/i.test(value)) return null;
331
340
  const slash = value.indexOf("/");
332
341
  const at = (slash < 0 ? value : value.slice(0, slash)).lastIndexOf("@");
342
+ if (value.includes("@") && value.indexOf(":") >= 0 && value.indexOf(":") < value.lastIndexOf("@")) return null;
333
343
  const scp = /^([^@/:]+):(.*)$/.exec(at < 0 ? value : value.slice(at + 1));
334
344
  if (scp && scp[1].length > 1 && !scp[2].startsWith("\\")) return `${scp[1]}:${scp[2]}`;
335
345
  return null;
@@ -545,7 +555,7 @@ function sized(file) {
545
555
  for (const hunk of file.hunks) for (const line of hunk.lines) content += Buffer.byteLength(line.content, "utf8");
546
556
  const bytes = cost(file);
547
557
  if (content <= WORKSPACE_GIT.maxFileBytes && bytes <= PAGE_BYTES) return { file, cost: bytes };
548
- const large = { ...file, kind: "too_large", hunks: [] };
558
+ const large = { ...file, kind: "too_large", additions: 0, deletions: 0, hunks: [] };
549
559
  return { file: large, cost: cost(large) };
550
560
  }
551
561
  var PAGE_BYTES = WORKSPACE_GIT.maxTotalBytes - 16 * 1024;
@@ -555,6 +565,7 @@ function createGitReader({ registry, binary = "git", timeoutMs = GIT_READ.timeou
555
565
  const mac = (scope) => createHmac("sha256", key).update(scope).digest("base64url").slice(0, 32);
556
566
  async function workspaceRoot(workspaceId) {
557
567
  if (typeof workspaceId !== "string" || !workspaceId) throw new RpcError(RPC_ERROR.invalidParams, "workspaceId is required", { reason: "invalid_params" });
568
+ if (!registry) throw new RpcError(ERROR.unavailable, FIXED.noRegistry);
558
569
  try {
559
570
  return await registry.resolve(workspaceId);
560
571
  } catch (error) {
@@ -740,6 +751,93 @@ function createGitReader({ registry, binary = "git", timeoutMs = GIT_READ.timeou
740
751
  snapshot: `s1.${digest.digest("hex").slice(0, 40)}`
741
752
  };
742
753
  }
754
+ async function requestedDirectory(directory) {
755
+ const invalid = () => new RpcError(RPC_ERROR.invalidParams, FIXED.invalidDirectory, { reason: "invalid_path" });
756
+ if (typeof directory !== "string" || !directory || [...directory].length > 4096 || directory.includes("\0")) throw invalid();
757
+ if (!path.isAbsolute(directory) || path.normalize(directory) !== directory) throw invalid();
758
+ const { root } = path.parse(directory);
759
+ const rest = directory.slice(root.length);
760
+ if (rest && rest.split(/[\\/]/).some((segment) => segment === "" || segment === "." || segment === "..")) throw invalid();
761
+ const absent = () => new RpcError(ERROR.notFound, FIXED.noDirectory, { reason: "not_found" });
762
+ let real;
763
+ try {
764
+ real = await realpath(directory);
765
+ } catch (error) {
766
+ if (missing(error)) throw absent();
767
+ throw error;
768
+ }
769
+ let info;
770
+ try {
771
+ info = await stat(real);
772
+ } catch (error) {
773
+ if (missing(error)) throw absent();
774
+ throw error;
775
+ }
776
+ if (!info.isDirectory()) throw new RpcError(ERROR.notFound, FIXED.notDirectory, { reason: "not_directory" });
777
+ return real;
778
+ }
779
+ async function facts(directory) {
780
+ const real = await requestedDirectory(directory);
781
+ const deadline = Date.now() + timeoutMs;
782
+ let located = await locate(real);
783
+ if (located.kind === "not_repository") return { kind: "unknown" };
784
+ let kind = "ok";
785
+ if (located.kind === "unsupported") {
786
+ if (path.relative(located.top, real).split(path.sep).includes(".git")) return { kind: "unsupported", reason: "inside_git_dir" };
787
+ located = await locate(located.top);
788
+ if (located.kind !== "repository") failed("the enclosing repository changed during the read");
789
+ kind = "not_repository_root";
790
+ }
791
+ const found = located;
792
+ let git;
793
+ try {
794
+ git = resolveGit(binary, void 0, found.root);
795
+ } catch (error) {
796
+ if (error instanceof IgnoreUnknown) failed("the directory cannot be examined");
797
+ throw error;
798
+ }
799
+ if (!git) throw new RpcError(ERROR.unavailable, FIXED.unavailable, { reason: "git_unavailable" });
800
+ const repo = await boxFor(found, git, deadline);
801
+ if (repo === "unsupported") return { kind: "unsupported", reason: "worktree_elsewhere" };
802
+ const branch = {};
803
+ let dirty = false;
804
+ let pending = "";
805
+ const { stopped } = await runGit(repo, ["status", "--porcelain=v2", "-z", "--branch", "--no-ahead-behind", "--untracked-files=all", "--ignored=no", "--ignore-submodules=dirty"], {
806
+ onData: (chunk) => {
807
+ const fields = (pending + chunk.toString("utf8")).split("\0");
808
+ pending = fields.pop();
809
+ for (const field of fields) {
810
+ if (!field) continue;
811
+ if (!field.startsWith("# ")) {
812
+ dirty = true;
813
+ return true;
814
+ }
815
+ const [name, ...rest] = field.slice(2).split(" ");
816
+ if (name === "branch.oid") branch.oid = rest.join(" ");
817
+ else if (name === "branch.head") branch.head = rest.join(" ");
818
+ }
819
+ if (pending && !pending.startsWith("#")) {
820
+ dirty = true;
821
+ return true;
822
+ }
823
+ if (pending.length > 64 * 1024) failed("status header over its bound");
824
+ return false;
825
+ }
826
+ });
827
+ if (!stopped && pending) failed("unterminated status");
828
+ await sameConfig(repo);
829
+ const detached = branch.head === "(detached)";
830
+ return {
831
+ kind,
832
+ repoRoot: repo.root,
833
+ branch: !detached && branch.head && validBranchName(branch.head) ? branch.head : null,
834
+ detached,
835
+ head: branch.oid && /^[0-9a-f]{40,64}$/.test(branch.oid) ? branch.oid : null,
836
+ hasRemote: repo.remoteUrl !== null,
837
+ remoteUrl: scrubRemoteUrl(repo.remoteUrl),
838
+ dirty
839
+ };
840
+ }
743
841
  async function untrackedOf(repo, relative, limit) {
744
842
  const found = [];
745
843
  let hidden = 0;
@@ -934,7 +1032,7 @@ function createGitReader({ registry, binary = "git", timeoutMs = GIT_READ.timeou
934
1032
  return contents;
935
1033
  }
936
1034
  async function diffWindow(repo, planned, mode, range, diffFlags, ignoreWhitespace) {
937
- const shown = (entry, kind, extra = {}, _cost = 0) => sized({ path: entry.path, ...entry.oldPath ? { oldPath: entry.oldPath } : {}, change: entry.change, kind, additions: 0, deletions: 0, hunks: [], ...extra });
1035
+ const shown = (entry, kind, extra = {}) => sized({ path: entry.path, ...entry.oldPath ? { oldPath: entry.oldPath } : {}, change: entry.change, kind, additions: 0, deletions: 0, hunks: [], ...extra });
938
1036
  const readable = (size) => size <= GIT_READ.maxScanBytes;
939
1037
  const wanted = /* @__PURE__ */ new Map();
940
1038
  for (const entry of planned) {
@@ -1032,7 +1130,7 @@ function createGitReader({ registry, binary = "git", timeoutMs = GIT_READ.timeou
1032
1130
  continue;
1033
1131
  }
1034
1132
  const parsed = parseSection(section);
1035
- results[item.index] = parsed.binary ? shown(item.entry, "binary") : shown(item.entry, "text", { additions: parsed.additions, deletions: parsed.deletions, hunks: parsed.hunks }, section.bytes);
1133
+ results[item.index] = parsed.binary ? shown(item.entry, "binary") : shown(item.entry, "text", { additions: parsed.additions, deletions: parsed.deletions, hunks: parsed.hunks });
1036
1134
  }
1037
1135
  }
1038
1136
  }
@@ -1071,7 +1169,7 @@ function createGitReader({ registry, binary = "git", timeoutMs = GIT_READ.timeou
1071
1169
  if (holdsKey(bytes)) return shown(entry, "withheld");
1072
1170
  if (likelyBinary(bytes)) return shown(entry, "binary");
1073
1171
  const { hunks, additions } = addedHunks(DECODER.decode(bytes));
1074
- return shown(entry, "text", { additions, hunks }, bytes.length + additions);
1172
+ return shown(entry, "text", { additions, hunks });
1075
1173
  }
1076
1174
  const guarded = (method, run) => run().catch((error) => {
1077
1175
  if (error instanceof RpcError) throw error;
@@ -1080,7 +1178,8 @@ function createGitReader({ registry, binary = "git", timeoutMs = GIT_READ.timeou
1080
1178
  });
1081
1179
  return {
1082
1180
  status: (workspaceId) => guarded("workspace.git.status", () => status(workspaceId)),
1083
- diff: (params) => guarded("workspace.git.diff", () => diff(params))
1181
+ diff: (params) => guarded("workspace.git.diff", () => diff(params)),
1182
+ facts: (directory) => guarded("workspace.git.facts", () => facts(directory))
1084
1183
  };
1085
1184
  }
1086
1185
  export {
package/dist/index.d.ts CHANGED
@@ -8,3 +8,4 @@ export { createScheduler, duration, renderPrompt, SCHEDULE_GRACE_MS, SCHEDULE_TI
8
8
  export type { ScheduleDelivery, Scheduler } from './scheduler.js';
9
9
  export { attachmentLimits, attachmentName, createAttachmentStore, readAttachmentConfig, sniffMimeType } from './attachments.js';
10
10
  export type { AttachmentReader, AttachmentRuntime, AttachmentStore, Principal } from './attachments.js';
11
+ export { createDiskGuard, isDiskError } from './disk.js';
package/dist/index.js CHANGED
@@ -4,6 +4,7 @@ import { createStore } from "./store.js";
4
4
  import { createScheduleStore, PROMPT_CHARS, SCHEDULE_ID, SCHEDULE_VERSION } from "./schedules.js";
5
5
  import { createScheduler, duration, renderPrompt, SCHEDULE_GRACE_MS, SCHEDULE_TICK_MS } from "./scheduler.js";
6
6
  import { attachmentLimits, attachmentName, createAttachmentStore, readAttachmentConfig, sniffMimeType } from "./attachments.js";
7
+ import { createDiskGuard, isDiskError } from "./disk.js";
7
8
  export {
8
9
  ERROR,
9
10
  PROMPT_CHARS,
@@ -15,10 +16,12 @@ export {
15
16
  attachmentName,
16
17
  createAttachmentStore,
17
18
  createDaemonServer,
19
+ createDiskGuard,
18
20
  createScheduleStore,
19
21
  createScheduler,
20
22
  createStore,
21
23
  duration,
24
+ isDiskError,
22
25
  readAttachmentConfig,
23
26
  renderPrompt,
24
27
  sniffMimeType