@anhlp/alp-daemon 0.0.0-stage → 0.0.1-beta.4

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 (66) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +7 -0
  3. package/README.md +14 -2
  4. package/dist/THIRD-PARTY-NOTICES.txt +431 -0
  5. package/dist/alp-templates.d.ts +5 -0
  6. package/dist/alpd.js +49562 -0
  7. package/dist/attachments.d.ts +211 -0
  8. package/dist/attachments.js +819 -0
  9. package/dist/build-manifest.json +131 -0
  10. package/dist/conversation.d.ts +119 -0
  11. package/dist/conversation.js +672 -0
  12. package/dist/files.d.ts +123 -0
  13. package/dist/files.js +365 -0
  14. package/dist/git.d.ts +87 -0
  15. package/dist/git.js +1093 -0
  16. package/dist/gitignore.d.ts +62 -0
  17. package/dist/gitignore.js +254 -0
  18. package/dist/history.d.ts +24 -0
  19. package/dist/history.js +149 -0
  20. package/dist/index.d.ts +10 -0
  21. package/dist/index.js +25 -0
  22. package/dist/lifecycle.d.ts +101 -0
  23. package/dist/lifecycle.js +239 -0
  24. package/dist/lock.d.ts +21 -0
  25. package/dist/lock.js +42 -0
  26. package/dist/main.d.ts +1 -0
  27. package/dist/main.js +277 -0
  28. package/dist/operations.d.ts +23 -0
  29. package/dist/operations.js +275 -0
  30. package/dist/registry.d.ts +89 -0
  31. package/dist/registry.js +343 -0
  32. package/dist/rpc-error.d.ts +6 -0
  33. package/dist/rpc-error.js +11 -0
  34. package/dist/scheduler.d.ts +96 -0
  35. package/dist/scheduler.js +290 -0
  36. package/dist/schedules.d.ts +69 -0
  37. package/dist/schedules.js +244 -0
  38. package/dist/server.d.ts +58 -0
  39. package/dist/server.js +1780 -0
  40. package/dist/store.d.ts +176 -0
  41. package/dist/store.js +385 -0
  42. package/dist/terminals.d.ts +157 -0
  43. package/dist/terminals.js +487 -0
  44. package/dist/worktrees.d.ts +41 -0
  45. package/dist/worktrees.js +246 -0
  46. package/package.json +159 -4
  47. package/src/alp-templates.d.ts +5 -0
  48. package/src/attachments.ts +870 -0
  49. package/src/conversation.ts +837 -0
  50. package/src/files.ts +459 -0
  51. package/src/git.ts +1045 -0
  52. package/src/gitignore.ts +277 -0
  53. package/src/history.ts +118 -0
  54. package/src/index.ts +11 -0
  55. package/src/lifecycle.ts +283 -0
  56. package/src/lock.ts +53 -0
  57. package/src/main.ts +292 -0
  58. package/src/operations.ts +300 -0
  59. package/src/registry.ts +238 -0
  60. package/src/rpc-error.ts +4 -0
  61. package/src/scheduler.ts +422 -0
  62. package/src/schedules.ts +294 -0
  63. package/src/server.ts +2056 -0
  64. package/src/store.ts +448 -0
  65. package/src/terminals.ts +591 -0
  66. package/src/worktrees.ts +287 -0
@@ -0,0 +1,246 @@
1
+ // packages/daemon/src/worktrees.ts
2
+ import { randomBytes } from "node:crypto";
3
+ import { lstat, readFile, realpath, stat } from "node:fs/promises";
4
+ import path from "node:path";
5
+ import { user_worktree } from "@anhlp/alp-core";
6
+ import { RegistryError } from "./registry.js";
7
+ var fail = (reason, message, code = 1004) => {
8
+ throw new RegistryError(code, message, { reason });
9
+ };
10
+ var git = user_worktree.userWorktreeGit;
11
+ async function worktreeMappings(home) {
12
+ const file = path.join(home, "worktree-destinations.json");
13
+ const info = await lstat(file).catch((e) => {
14
+ if (e.code === "ENOENT") return void 0;
15
+ throw e;
16
+ });
17
+ if (!info) return [];
18
+ if (!info.isFile() || process.platform !== "win32" && (info.uid !== process.getuid?.() || info.mode & 63))
19
+ fail("mapping_invalid", "Worktree destination configuration must be private", 1005);
20
+ const value = JSON.parse(await readFile(file, "utf8"));
21
+ if (value?.version !== 1 || !Array.isArray(value.mappings))
22
+ fail("mapping_invalid", "Invalid worktree destination configuration", 1005);
23
+ const ids = /* @__PURE__ */ new Set();
24
+ for (const m of value.mappings) {
25
+ if (!m || typeof m.id !== "string" || !/^[a-zA-Z0-9_-]{1,64}$/.test(m.id) || ids.has(m.id))
26
+ fail("mapping_invalid", "Invalid mapping identity", 1005);
27
+ ids.add(m.id);
28
+ for (const k of ["source", "commonDir", "parent"]) {
29
+ if (typeof m[k] !== "string" || !path.isAbsolute(m[k]) || await realpath(m[k]) !== m[k] || !(await stat(m[k])).isDirectory())
30
+ fail("mapping_invalid", "Mapping paths must be existing canonical directories", 1005);
31
+ }
32
+ const parentInfo = await stat(m.parent);
33
+ if (process.platform !== "win32" && (parentInfo.uid !== process.getuid?.() || parentInfo.mode & 63))
34
+ fail("mapping_invalid", "Destination parent must be owner-private", 1005);
35
+ for (const reserved of [path.join(home, "worktrees"), path.join(home, "copies"), m.commonDir])
36
+ if (m.parent === reserved || m.parent.startsWith(reserved + path.sep) || reserved.startsWith(m.parent + path.sep))
37
+ fail("mapping_invalid", "Destination overlaps protected assignment or Git storage", 1005);
38
+ if (m.parent === m.source || m.parent.startsWith(m.source + path.sep) || m.source.startsWith(m.parent + path.sep))
39
+ fail("mapping_invalid", "Destination parent must be separate from source", 1005);
40
+ }
41
+ return value.mappings;
42
+ }
43
+ function createWorkspaceWorktrees(home, registry, guard, register) {
44
+ const adapter = git();
45
+ async function approved(context, source, mappingId, native = true) {
46
+ const m = (await worktreeMappings(home)).find((m2) => m2.id === mappingId && m2.source === source);
47
+ if (!m) return fail("outside_workspace", "Mapping unavailable", 1003);
48
+ if (context.mappingIds && !context.mappingIds.includes(m.id) || context.authorizedRoots && !context.authorizedRoots.includes(source))
49
+ fail("outside_workspace", "Worktree mapping is not approved for this credential", 1003);
50
+ if ((await (native ? adapter.facts(source) : user_worktree.repositoryIdentity(source))).commonDir !== m.commonDir)
51
+ fail("identity_changed", "Source repository identity changed", 1003);
52
+ return m;
53
+ }
54
+ const workspace = (r, id) => r.workspaces.find((w) => w.id === id) ?? fail("not_found", "No workspace", 1001);
55
+ async function sourceOf(r, id) {
56
+ const w = workspace(r, id);
57
+ if (w.archivedAt || w.worktree?.removedAt || r.projects.find((p) => p.id === w.projectId)?.removedAt)
58
+ fail("not_found", "Workspace unavailable", 1001);
59
+ if (await realpath(w.directory) !== (w.canonical ?? w.directory))
60
+ fail("identity_changed", "Source identity changed", 1003);
61
+ return w;
62
+ }
63
+ async function clean(source, target, branch, head) {
64
+ if (!await adapter.inspect(source, target, branch))
65
+ fail("outcome_unknown", "Worktree creation needs owner reconciliation", 1005);
66
+ if ((await adapter.facts(target)).head !== head) fail("outcome_unknown", "Prepared worktree HEAD changed", 1005);
67
+ await adapter.assertClean(target);
68
+ }
69
+ return {
70
+ async describe(w) {
71
+ const { worktree, ...row } = w;
72
+ let branch = null;
73
+ try {
74
+ const canonical = await realpath(w.directory);
75
+ if (canonical === (w.canonical ?? w.directory))
76
+ branch = (await user_worktree.repositoryIdentity(canonical)).branch;
77
+ } catch {
78
+ }
79
+ return { ...row, branch, ...worktree ? { managedWorktree: true } : {} };
80
+ },
81
+ async assertAvailable(directory) {
82
+ const r = await registry.snapshot();
83
+ const canonical = await realpath(directory).catch(() => path.resolve(directory));
84
+ if (Object.values(r.worktreeJournal ?? {}).some(
85
+ (j) => canonical === j.intent.directory || canonical.startsWith(j.intent.directory + path.sep)
86
+ ))
87
+ fail("reconciliation_required", "Workspace is pending reconciliation");
88
+ },
89
+ async grants(context = {}) {
90
+ const r = await registry.snapshot();
91
+ const mappings = [];
92
+ for (const m of await worktreeMappings(home)) {
93
+ try {
94
+ await approved(context, m.source, m.id, false);
95
+ mappings.push(m);
96
+ } catch {
97
+ }
98
+ }
99
+ const workspaces = [];
100
+ for (const w of r.workspaces) {
101
+ const m = w.worktree;
102
+ if (!m || m.principalId !== (context.principalId ?? "owner") || m.grantEpoch !== (context.principalEpoch ?? "owner") || !mappings.some((a) => a.id === m.mappingId && a.source === m.source && a.commonDir === m.commonDir))
103
+ continue;
104
+ const expected = path.join(mappings.find((a) => a.id === m.mappingId).parent, w.id);
105
+ if (w.directory !== expected || w.canonical !== expected) continue;
106
+ const pending = Object.values(r.worktreeJournal ?? {}).find((j) => j.intent.id === w.id);
107
+ if (!m.removedAt && !pending) {
108
+ try {
109
+ const identity = await user_worktree.repositoryIdentity(w.directory);
110
+ if (identity.gitDir !== m.gitDir || identity.commonDir !== m.commonDir || identity.branch !== m.branch || await lstat(path.join(identity.gitDir, "locked")).catch((e) => {
111
+ if (e.code === "ENOENT") return void 0;
112
+ throw e;
113
+ }))
114
+ continue;
115
+ } catch {
116
+ continue;
117
+ }
118
+ }
119
+ workspaces.push(
120
+ Object.values(r.worktreeJournal ?? {}).some((j) => j.intent.id === w.id) ? { ...w, archivedAt: w.archivedAt ?? "pending" } : w
121
+ );
122
+ }
123
+ return { mappings: mappings.map((m) => ({ id: m.id, source: m.source })), workspaces };
124
+ },
125
+ async mutate(method, p, context = {}) {
126
+ const actor = context.principalId ?? "owner";
127
+ const r = await registry.snapshot();
128
+ const w = workspace(r, p.workspaceId);
129
+ const origin = method === "create" ? await sourceOf(r, w.id) : w;
130
+ const meta = w.worktree;
131
+ if (method === "remove" && (!meta || meta.kind !== "user" || meta.principalId !== actor || meta.grantEpoch !== (context.principalEpoch ?? "owner")))
132
+ fail("not_managed", "Only your managed user worktrees can be removed", 1003);
133
+ const source = method === "create" ? await realpath(origin.directory) : meta.source;
134
+ const mappingId = method === "create" ? p.mappingId : meta.mappingId;
135
+ const mapping = await approved(context, source, mappingId);
136
+ return registry.nativeMutation(
137
+ "workspace.worktree." + method,
138
+ { workspaceId: p.workspaceId, ...method === "create" ? { mappingId: p.mappingId } : {} },
139
+ { actor: actor + ":" + (context.principalEpoch ?? "owner"), requestId: p.requestId, ifRevision: p.ifRevision },
140
+ async (draft) => {
141
+ if (method === "create") {
142
+ const id = "wks_" + randomBytes(8).toString("hex");
143
+ const facts = await adapter.facts(source);
144
+ return {
145
+ method,
146
+ id,
147
+ requestId: p.requestId,
148
+ ifRevision: p.ifRevision,
149
+ directory: path.join(mapping.parent, id),
150
+ projectId: origin.projectId,
151
+ sourceWorkspaceId: origin.id,
152
+ source,
153
+ commonDir: mapping.commonDir,
154
+ mappingId,
155
+ principalId: actor,
156
+ grantEpoch: context.principalEpoch ?? "owner",
157
+ branch: `alp/workspace/${id}`,
158
+ head: facts.head
159
+ };
160
+ }
161
+ if (w.directory !== path.join(mapping.parent, w.id) || w.canonical !== w.directory)
162
+ fail("identity_changed", "Managed worktree path changed", 1003);
163
+ await guard(w.directory, async () => {
164
+ if (Object.values(draft.sessions).includes(w.id))
165
+ fail(
166
+ "workspace_history",
167
+ "Saved sessions reference this checkout; retain its files. This operation does not delete history."
168
+ );
169
+ try {
170
+ await adapter.assertClean(w.directory);
171
+ } catch {
172
+ fail(
173
+ "workspace_dirty",
174
+ "Worktree has dirty, ignored, hidden-index or submodule data; preserve it before removal"
175
+ );
176
+ }
177
+ });
178
+ return { method, id: w.id, directory: w.directory, ...meta };
179
+ },
180
+ async (intent, draft) => {
181
+ await approved(context, intent.source, intent.mappingId);
182
+ if (intent.principalId !== actor || intent.grantEpoch !== (context.principalEpoch ?? "owner") || intent.directory !== path.join(mapping.parent, intent.id) || intent.commonDir !== mapping.commonDir)
183
+ fail("identity_changed", "Worktree journal identity changed", 1003);
184
+ if (method === "create") {
185
+ const row = await adapter.inspect(intent.source, intent.directory, intent.branch);
186
+ if (!row) await adapter.create(intent.source, mapping.parent, intent.id, intent.branch, intent.head);
187
+ await clean(intent.source, intent.directory, intent.branch, intent.head);
188
+ await register(intent.directory);
189
+ const created = {
190
+ id: intent.id,
191
+ projectId: intent.projectId,
192
+ directory: intent.directory,
193
+ canonical: intent.directory,
194
+ labels: [],
195
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
196
+ worktree: {
197
+ kind: "user",
198
+ sourceWorkspaceId: intent.sourceWorkspaceId,
199
+ source: intent.source,
200
+ commonDir: intent.commonDir,
201
+ gitDir: (await user_worktree.repositoryIdentity(intent.directory)).gitDir,
202
+ requestId: intent.requestId,
203
+ ifRevision: intent.ifRevision,
204
+ mappingId: intent.mappingId,
205
+ principalId: actor,
206
+ grantEpoch: context.principalEpoch ?? "owner",
207
+ branch: intent.branch,
208
+ head: intent.head
209
+ }
210
+ };
211
+ draft.workspaces.push(created);
212
+ return { workspace: created };
213
+ }
214
+ const finish = async () => {
215
+ if (Object.values(draft.sessions).includes(intent.id))
216
+ fail(
217
+ "workspace_history",
218
+ "Saved sessions reference this checkout; retain its files. This operation does not delete history."
219
+ );
220
+ const present2 = await lstat(intent.directory).catch((e) => {
221
+ if (e.code === "ENOENT") return void 0;
222
+ throw e;
223
+ });
224
+ if (present2) await adapter.remove(intent.source, intent.directory, intent.branch);
225
+ else if (await adapter.inspect(intent.source, intent.directory, intent.branch))
226
+ fail("outcome_unknown", "Worktree removal needs reconciliation", 1005);
227
+ const removed = workspace(draft, intent.id);
228
+ removed.archivedAt = (/* @__PURE__ */ new Date()).toISOString();
229
+ removed.pinnedAt = null;
230
+ removed.worktree.removedAt = removed.archivedAt;
231
+ return { workspace: removed, filesRemoved: true, branchRetained: true };
232
+ };
233
+ const present = await lstat(intent.directory).catch((e) => {
234
+ if (e.code === "ENOENT") return void 0;
235
+ throw e;
236
+ });
237
+ return present ? guard(intent.directory, finish) : finish();
238
+ }
239
+ );
240
+ }
241
+ };
242
+ }
243
+ export {
244
+ createWorkspaceWorktrees,
245
+ worktreeMappings
246
+ };
package/package.json CHANGED
@@ -1,6 +1,161 @@
1
1
  {
2
2
  "name": "@anhlp/alp-daemon",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.0.1-beta.4",
4
+ "type": "module",
5
+ "description": "ALP daemon module",
6
+ "files": [
7
+ "src",
8
+ "dist",
9
+ "NOTICE"
10
+ ],
11
+ "exports": {
12
+ "./index": {
13
+ "alp-source": {
14
+ "types": "./src/index.ts",
15
+ "default": "./src/index.ts"
16
+ },
17
+ "types": "./dist/index.d.ts",
18
+ "default": "./dist/index.js"
19
+ },
20
+ ".": {
21
+ "alp-source": {
22
+ "types": "./src/index.ts",
23
+ "default": "./src/index.ts"
24
+ },
25
+ "types": "./dist/index.d.ts",
26
+ "default": "./dist/index.js"
27
+ },
28
+ "./attachments": {
29
+ "alp-source": {
30
+ "types": "./src/attachments.ts",
31
+ "default": "./src/attachments.ts"
32
+ },
33
+ "types": "./dist/attachments.d.ts",
34
+ "default": "./dist/attachments.js"
35
+ },
36
+ "./terminals": {
37
+ "alp-source": {
38
+ "types": "./src/terminals.ts",
39
+ "default": "./src/terminals.ts"
40
+ },
41
+ "types": "./dist/terminals.d.ts",
42
+ "default": "./dist/terminals.js"
43
+ },
44
+ "./conversation": {
45
+ "alp-source": {
46
+ "types": "./src/conversation.ts",
47
+ "default": "./src/conversation.ts"
48
+ },
49
+ "types": "./dist/conversation.d.ts",
50
+ "default": "./dist/conversation.js"
51
+ },
52
+ "./lifecycle": {
53
+ "alp-source": {
54
+ "types": "./src/lifecycle.ts",
55
+ "default": "./src/lifecycle.ts"
56
+ },
57
+ "types": "./dist/lifecycle.d.ts",
58
+ "default": "./dist/lifecycle.js"
59
+ },
60
+ "./files": {
61
+ "alp-source": {
62
+ "types": "./src/files.ts",
63
+ "default": "./src/files.ts"
64
+ },
65
+ "types": "./dist/files.d.ts",
66
+ "default": "./dist/files.js"
67
+ },
68
+ "./git": {
69
+ "alp-source": {
70
+ "types": "./src/git.ts",
71
+ "default": "./src/git.ts"
72
+ },
73
+ "types": "./dist/git.d.ts",
74
+ "default": "./dist/git.js"
75
+ },
76
+ "./lock": {
77
+ "alp-source": {
78
+ "types": "./src/lock.ts",
79
+ "default": "./src/lock.ts"
80
+ },
81
+ "types": "./dist/lock.d.ts",
82
+ "default": "./dist/lock.js"
83
+ },
84
+ "./main": {
85
+ "alp-source": {
86
+ "types": "./src/main.ts",
87
+ "default": "./src/main.ts"
88
+ },
89
+ "types": "./dist/main.d.ts",
90
+ "default": "./dist/main.js"
91
+ },
92
+ "./server": {
93
+ "alp-source": {
94
+ "types": "./src/server.ts",
95
+ "default": "./src/server.ts"
96
+ },
97
+ "types": "./dist/server.d.ts",
98
+ "default": "./dist/server.js"
99
+ },
100
+ "./store": {
101
+ "alp-source": {
102
+ "types": "./src/store.ts",
103
+ "default": "./src/store.ts"
104
+ },
105
+ "types": "./dist/store.d.ts",
106
+ "default": "./dist/store.js"
107
+ },
108
+ "./registry": {
109
+ "alp-source": {
110
+ "types": "./src/registry.ts",
111
+ "default": "./src/registry.ts"
112
+ },
113
+ "types": "./dist/registry.d.ts",
114
+ "default": "./dist/registry.js"
115
+ },
116
+ "./worktrees": {
117
+ "alp-source": {
118
+ "types": "./src/worktrees.ts",
119
+ "default": "./src/worktrees.ts"
120
+ },
121
+ "types": "./dist/worktrees.d.ts",
122
+ "default": "./dist/worktrees.js"
123
+ }
124
+ },
125
+ "engines": {
126
+ "node": ">=22.12"
127
+ },
128
+ "dependencies": {
129
+ "@anhlp/alp-core": "0.0.1-beta.4",
130
+ "@anhlp/alp-runtime": "0.0.1-beta.4",
131
+ "@anhlp/alp-client": "0.0.1-beta.4",
132
+ "@anhlp/alp-protocol": "0.0.1-beta.4",
133
+ "@anthropic-ai/sdk": "^0.131.0",
134
+ "ws": "^8.22.0",
135
+ "zod": "4.4.3",
136
+ "@anthropic-ai/claude-agent-sdk": "^0.3.292"
137
+ },
138
+ "scripts": {
139
+ "build": "node ../../scripts/build-workspace.mjs daemon",
140
+ "test": "node ../../scripts/test-package.mjs daemon",
141
+ "prepack": "npm run build"
142
+ },
143
+ "publishConfig": {
144
+ "access": "public",
145
+ "registry": "https://registry.npmjs.org/",
146
+ "tag": "beta"
147
+ },
148
+ "license": "Apache-2.0",
149
+ "repository": {
150
+ "type": "git",
151
+ "url": "git+https://github.com/phucanh08/alp.git",
152
+ "directory": "packages/daemon"
153
+ },
154
+ "homepage": "https://github.com/phucanh08/alp/tree/main/packages/daemon#readme",
155
+ "bugs": {
156
+ "url": "https://github.com/phucanh08/alp/issues"
157
+ },
158
+ "optionalDependencies": {
159
+ "node-pty": "1.2.0-beta.15"
160
+ }
161
+ }
@@ -0,0 +1,5 @@
1
+ // Virtual module supplied by the neutral core build.
2
+ declare module 'alp:templates' {
3
+ const templates: Record<string, string>;
4
+ export default templates;
5
+ }