@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
@@ -47,6 +47,11 @@ export type GitCheckOptions = {
47
47
  * nothing. A workspace that cannot be examined finds no Git at all (IgnoreUnknown).
48
48
  */
49
49
  export declare function resolveGit(name?: string, searchPath?: string, root?: string): string | undefined;
50
+ /**
51
+ * The absolute entries of PATH outside `root` (insideRoot), for the environment of a Git alpd runs
52
+ * in a workspace: whatever Git itself starts is then never found in the workspace either.
53
+ */
54
+ export declare function searchPathOutside(root: string, searchPath?: string): string;
50
55
  /** Git processes running now, for tests. */
51
56
  export declare const gitChecksRunning: () => number;
52
57
  /**
package/dist/gitignore.js CHANGED
@@ -93,12 +93,18 @@ function resolveGit(name = "git", searchPath = process.env.PATH ?? process.env.P
93
93
  }
94
94
  return void 0;
95
95
  }
96
+ function searchPathOutside(root, searchPath = process.env.PATH ?? process.env.Path ?? "") {
97
+ const box = rootOf(root);
98
+ if (!box) throw new IgnoreUnknown("the workspace cannot be examined");
99
+ return searchPath.split(path.delimiter).filter((directory) => directory && path.isAbsolute(directory) && !insideRoot(directory, box)).join(path.delimiter);
100
+ }
96
101
  function hasGitEntry(root) {
97
102
  for (let directory = root; ; directory = path.dirname(directory)) {
98
103
  try {
99
104
  lstatSync(path.join(directory, ".git"));
100
105
  return true;
101
- } catch {
106
+ } catch (error) {
107
+ if (error.code !== "ENOENT") throw new IgnoreUnknown("cannot examine .git");
102
108
  }
103
109
  if (path.dirname(directory) === directory) return false;
104
110
  }
@@ -195,22 +201,34 @@ function check(git, root, paths, deadline, timeoutMs) {
195
201
  if (err.length < GIT_CHECK.maxErrorBytes) err += chunk.toString("utf8");
196
202
  });
197
203
  child.on("error", (error) => finish(() => {
198
- if (error.code === "ENOENT") resolve(null);
199
- else reject(new IgnoreUnknown(error.message));
204
+ reject(new IgnoreUnknown(error.message));
200
205
  }));
201
206
  child.on("close", (code) => finish(() => {
202
207
  if (code === 128 && /not a git repository/i.test(err)) {
203
- if (hasGitEntry(root)) reject(new IgnoreUnknown("a .git Git cannot use"));
204
- else resolve(null);
208
+ try {
209
+ if (hasGitEntry(root)) reject(new IgnoreUnknown("a .git Git cannot use"));
210
+ else resolve(null);
211
+ } catch (error) {
212
+ reject(error);
213
+ }
205
214
  return;
206
215
  }
207
216
  if (code !== 0 && code !== 1) {
208
217
  reject(new IgnoreUnknown(`exit ${code}`));
209
218
  return;
210
219
  }
220
+ if (err.length || code === 1 && outBytes || code === 0 && !outBytes) {
221
+ reject(new IgnoreUnknown("inconsistent exit status or diagnostics"));
222
+ return;
223
+ }
224
+ const output = Buffer.concat(out).toString("utf8");
225
+ if (outBytes && !output.endsWith("\0")) {
226
+ reject(new IgnoreUnknown("unterminated answer"));
227
+ return;
228
+ }
211
229
  const asked = new Set(paths);
212
230
  const ignored = /* @__PURE__ */ new Set();
213
- for (const entry of Buffer.concat(out).toString("utf8").split("\0")) {
231
+ for (const entry of output.split("\0")) {
214
232
  if (!entry) continue;
215
233
  const relative = entry.startsWith("./") ? entry.slice(2) : void 0;
216
234
  if (relative === void 0 || !asked.has(relative)) {
@@ -231,5 +249,6 @@ export {
231
249
  IgnoreUnknown,
232
250
  gitChecksRunning,
233
251
  gitIgnored,
234
- resolveGit
252
+ resolveGit,
253
+ searchPathOutside
235
254
  };
@@ -0,0 +1,24 @@
1
+ import type { Envelope } from '@anhlp/alp-protocol/events';
2
+ export type HistoryItem = Envelope | {
3
+ cursor: number;
4
+ oversized: {
5
+ bytes: number;
6
+ };
7
+ };
8
+ export declare class HistoryError extends Error {
9
+ reason: 'invalid_cursor' | 'stale_cursor';
10
+ constructor(reason: 'invalid_cursor' | 'stale_cursor', message: string);
11
+ }
12
+ export declare function createHistoryIndex(file: string): {
13
+ page(sessionId: string, options?: {
14
+ before?: string;
15
+ limit?: number;
16
+ maxBytes?: number;
17
+ }): Promise<{
18
+ tipCursor: number;
19
+ olderCursor?: string | undefined;
20
+ generation: string;
21
+ items: HistoryItem[];
22
+ hasOlder: boolean;
23
+ }>;
24
+ };
@@ -0,0 +1,149 @@
1
+ // packages/daemon/src/history.ts
2
+ import { open, readFile, writeFile, rename, stat } from "node:fs/promises";
3
+ import { createReadStream } from "node:fs";
4
+ import { randomBytes, createHmac, timingSafeEqual } from "node:crypto";
5
+ var WIDTH = 24;
6
+ var HistoryError = class extends Error {
7
+ constructor(reason, message) {
8
+ super(message);
9
+ this.reason = reason;
10
+ }
11
+ };
12
+ function createHistoryIndex(file) {
13
+ let pending = Promise.resolve();
14
+ const index = `${file}.idx`;
15
+ const metadata = `${file}.history.json`;
16
+ async function sync() {
17
+ const info = await stat(file).catch((error) => {
18
+ if (error.code === "ENOENT") return void 0;
19
+ throw error;
20
+ });
21
+ let meta;
22
+ try {
23
+ meta = JSON.parse(await readFile(metadata, "utf8"));
24
+ } catch (error) {
25
+ if (error.code !== "ENOENT" && !(error instanceof SyntaxError)) throw error;
26
+ }
27
+ if (meta && (!/^[0-9a-f]{32}$/.test(meta.generation) || !/^[0-9a-f]{64}$/.test(meta.secret) || ![meta.bytes, meta.count, meta.tip, meta.ino].every((value) => Number.isSafeInteger(value) && value >= 0))) meta = void 0;
28
+ const indexInfo = await stat(index).catch((error) => {
29
+ if (error.code === "ENOENT") return void 0;
30
+ throw error;
31
+ });
32
+ if (!meta || !indexInfo || indexInfo.size < meta.count * WIDTH || meta.ino !== 0 && meta.ino !== (info?.ino ?? 0) || meta.bytes > (info?.size ?? 0)) {
33
+ meta = { generation: randomBytes(16).toString("hex"), secret: randomBytes(32).toString("hex"), ino: info?.ino ?? 0, bytes: 0, count: 0, tip: 0 };
34
+ await writeFile(index, "", { mode: 384 });
35
+ }
36
+ if (meta.ino === 0 && info) meta.ino = info.ino;
37
+ const handle = await open(index, "a+", 384);
38
+ try {
39
+ await handle.truncate(meta.count * WIDTH);
40
+ if (info && info.size > meta.bytes) {
41
+ let offset = meta.bytes;
42
+ let start = offset;
43
+ let fragments = [];
44
+ for await (const raw of createReadStream(file, { start: offset, end: info.size - 1, highWaterMark: 65536 })) {
45
+ const chunk = raw;
46
+ let from = 0;
47
+ for (let at = 0; at < chunk.length; at++) if (chunk[at] === 10) {
48
+ const part = chunk.subarray(from, at);
49
+ fragments.push(part);
50
+ const length = offset + at + 1 - start;
51
+ let cursor = meta.tip + 1;
52
+ const body = Buffer.concat(fragments).toString("utf8");
53
+ try {
54
+ const value = JSON.parse(body);
55
+ if (Number.isSafeInteger(value.cursor) && value.cursor > meta.tip) cursor = value.cursor;
56
+ } catch {
57
+ throw new Error("Unreadable history line");
58
+ }
59
+ const entry = Buffer.alloc(WIDTH);
60
+ [start, length, cursor].forEach((value, i) => entry.writeBigUInt64LE(BigInt(value), i * 8));
61
+ await handle.write(entry);
62
+ meta.count++;
63
+ meta.tip = cursor;
64
+ meta.bytes = offset + at + 1;
65
+ start = meta.bytes;
66
+ fragments = [];
67
+ from = at + 1;
68
+ }
69
+ const rest = chunk.subarray(from);
70
+ fragments.push(rest);
71
+ offset += chunk.length;
72
+ }
73
+ }
74
+ const temporary = `${metadata}.tmp`;
75
+ await writeFile(temporary, JSON.stringify(meta), { mode: 384 });
76
+ await rename(temporary, metadata);
77
+ return meta;
78
+ } finally {
79
+ await handle.close();
80
+ }
81
+ }
82
+ return {
83
+ page(sessionId, options = {}) {
84
+ const run = pending.then(async () => {
85
+ const { before, limit = 200 } = options;
86
+ if (!Number.isInteger(limit) || limit < 1 || limit > 500) throw new Error("limit must be between 1 and 500");
87
+ const maxBytes = Math.min(512 * 1024, options.maxBytes ?? 512 * 1024);
88
+ if (maxBytes < 2048) throw new Error("history frame budget too small");
89
+ const meta = await sync();
90
+ const sign = (body2) => createHmac("sha256", meta.secret).update(body2).digest("base64url");
91
+ let position = meta.count, tip = meta.tip;
92
+ if (before !== void 0) {
93
+ if (typeof before !== "string" || before.length > 4096) throw new HistoryError("invalid_cursor", "Invalid history cursor");
94
+ try {
95
+ const [body2, signature, extra] = before.split(".");
96
+ const value = JSON.parse(Buffer.from(body2, "base64url").toString("utf8"));
97
+ if (value.sessionId !== sessionId) throw new Error();
98
+ if (value.generation !== meta.generation) throw new HistoryError("stale_cursor", "History generation changed");
99
+ const expected = Buffer.from(sign(body2));
100
+ const received = Buffer.from(signature ?? "");
101
+ if (extra || expected.length !== received.length || !timingSafeEqual(expected, received)) throw new Error();
102
+ if (!Number.isSafeInteger(value.position) || value.position < 0 || value.position > meta.count || !Number.isSafeInteger(value.tip) || value.tip < 0 || value.tip > meta.tip) throw new Error();
103
+ position = value.position;
104
+ tip = value.tip;
105
+ } catch (error) {
106
+ if (error instanceof HistoryError) throw error;
107
+ throw new HistoryError("invalid_cursor", "Invalid history cursor");
108
+ }
109
+ }
110
+ const items = [];
111
+ let bytes = 1024;
112
+ const idx = await open(index, "r");
113
+ const timeline = meta.count ? await open(file, "r") : void 0;
114
+ try {
115
+ while (position > 0 && items.length < limit) {
116
+ const entry = Buffer.alloc(WIDTH);
117
+ if ((await idx.read(entry, 0, WIDTH, (position - 1) * WIDTH)).bytesRead !== WIDTH) throw new Error("Incomplete history index");
118
+ const [offset, length, cursor] = [0, 8, 16].map((at) => Number(entry.readBigUInt64LE(at)));
119
+ let item;
120
+ if (length > maxBytes - 1024) item = { cursor, oversized: { bytes: length } };
121
+ else {
122
+ if (bytes + length > maxBytes) break;
123
+ const data = Buffer.alloc(length);
124
+ if ((await timeline.read(data, 0, length, offset)).bytesRead !== length) throw new Error("History changed during read");
125
+ item = { ...JSON.parse(data.toString("utf8")), cursor };
126
+ }
127
+ const size = Buffer.byteLength(JSON.stringify(item)) + 1;
128
+ if (bytes + size > maxBytes) break;
129
+ items.unshift(item);
130
+ bytes += size;
131
+ position--;
132
+ }
133
+ } finally {
134
+ await idx.close();
135
+ await timeline?.close();
136
+ }
137
+ const body = Buffer.from(JSON.stringify({ sessionId, generation: meta.generation, position, tip })).toString("base64url");
138
+ return { generation: meta.generation, items, hasOlder: position > 0, ...position > 0 ? { olderCursor: `${body}.${sign(body)}` } : {}, tipCursor: tip };
139
+ });
140
+ pending = run.catch(() => {
141
+ });
142
+ return run;
143
+ }
144
+ };
145
+ }
146
+ export {
147
+ HistoryError,
148
+ createHistoryIndex
149
+ };
package/dist/index.d.ts CHANGED
@@ -2,3 +2,10 @@ export { createDaemonServer, ERROR } from './server.js';
2
2
  export type { DaemonConnection, DaemonServer, SessionSummary } from './server.js';
3
3
  export { createStore } from './store.js';
4
4
  export type { SessionRecord, SessionStatus, Store } from './store.js';
5
+ export { createScheduleStore, PROMPT_CHARS, SCHEDULE_ID, SCHEDULE_VERSION } from './schedules.js';
6
+ export type { ScheduleRecord, ScheduleStore } from './schedules.js';
7
+ export { createScheduler, duration, renderPrompt, SCHEDULE_GRACE_MS, SCHEDULE_TICK_MS } from './scheduler.js';
8
+ export type { ScheduleDelivery, Scheduler } from './scheduler.js';
9
+ export { attachmentLimits, attachmentName, createAttachmentStore, readAttachmentConfig, sniffMimeType } from './attachments.js';
10
+ export type { AttachmentReader, AttachmentRuntime, AttachmentStore, Principal } from './attachments.js';
11
+ export { createDiskGuard, isDiskError } from './disk.js';
package/dist/index.js CHANGED
@@ -1,8 +1,28 @@
1
1
  // packages/daemon/src/index.ts
2
2
  import { createDaemonServer, ERROR } from "./server.js";
3
3
  import { createStore } from "./store.js";
4
+ import { createScheduleStore, PROMPT_CHARS, SCHEDULE_ID, SCHEDULE_VERSION } from "./schedules.js";
5
+ import { createScheduler, duration, renderPrompt, SCHEDULE_GRACE_MS, SCHEDULE_TICK_MS } from "./scheduler.js";
6
+ import { attachmentLimits, attachmentName, createAttachmentStore, readAttachmentConfig, sniffMimeType } from "./attachments.js";
7
+ import { createDiskGuard, isDiskError } from "./disk.js";
4
8
  export {
5
9
  ERROR,
10
+ PROMPT_CHARS,
11
+ SCHEDULE_GRACE_MS,
12
+ SCHEDULE_ID,
13
+ SCHEDULE_TICK_MS,
14
+ SCHEDULE_VERSION,
15
+ attachmentLimits,
16
+ attachmentName,
17
+ createAttachmentStore,
6
18
  createDaemonServer,
7
- createStore
19
+ createDiskGuard,
20
+ createScheduleStore,
21
+ createScheduler,
22
+ createStore,
23
+ duration,
24
+ isDiskError,
25
+ readAttachmentConfig,
26
+ renderPrompt,
27
+ sniffMimeType
8
28
  };
@@ -0,0 +1,101 @@
1
+ import { RpcError } from './rpc-error.js';
2
+ /**
3
+ * The lifecycle journal (docs/protocol/alp-json-rpc-v1.md §12.7): one record per mutation of a session's
4
+ * native conversation (fork, rewind; later reload and live configure), keyed by resource, operation and
5
+ * the client's requestId. A mutation crosses the native runtime and alpd's own files, which no rename
6
+ * makes atomic, so it is journaled before the native step and replayed from the journal:
7
+ *
8
+ * prepared → written before the native step; nothing native happened yet, or it is in flight.
9
+ * applied → the native step answered; `native` holds what it made. alpd's own commit may follow.
10
+ * committed → alpd's commit is on disk; `result` answers every repeat of the request.
11
+ * failed → the native runtime refused it for certain; `error` answers every repeat.
12
+ * unknown → the native runtime may or may not have acted (a crash or a lost answer after prepared).
13
+ *
14
+ * A start of alpd turns `prepared` into `unknown` (never sends the native step again) and rolls `applied`
15
+ * forward with the operation's commit, which must therefore be idempotent. Refusals before `prepared`
16
+ * (validation, busy, stale) record nothing, so the same requestId may be tried again. As the rest of
17
+ * alpd's store, a resolved write survives a crash of alpd, not a power loss (no fsync).
18
+ */
19
+ export type LifecycleState = 'prepared' | 'applied' | 'committed' | 'failed' | 'unknown';
20
+ export type LifecycleEntry<Plan = any, Native = any, Result = any> = {
21
+ version: 1;
22
+ key: string;
23
+ /** The resource the operation changes or reads: a root session id. */
24
+ resource: string;
25
+ operation: string;
26
+ requestId: string;
27
+ /** A digest of the request; another payload under the same requestId is a conflict. */
28
+ fingerprint: string;
29
+ /** Unique attempt, so a retained completion witness cannot match a reused journal key. */
30
+ attemptId?: string;
31
+ state: LifecycleState;
32
+ /** Startup has observed this unknown outcome. It remains unknown for retries and audit. */
33
+ recoverySeenAt?: string;
34
+ /** What prepare decided; everything commit needs, so a restart can roll the operation forward. */
35
+ plan: Plan;
36
+ native?: Native;
37
+ result?: Result;
38
+ error?: {
39
+ code: number;
40
+ message: string;
41
+ data?: unknown;
42
+ };
43
+ createdAt: string;
44
+ updatedAt: string;
45
+ };
46
+ /** Terminal entries are kept this long after their last write, as prompt receipts are (§10). */
47
+ export declare const LIFECYCLE_RETENTION_MS: number;
48
+ export type LifecycleJournal = ReturnType<typeof createLifecycleJournal>;
49
+ export declare function createLifecycleJournal(directory: string): {
50
+ keyOf: (resource: string, operation: string, requestId: string) => string;
51
+ get(resource: string, operation: string, requestId: string): Promise<LifecycleEntry | undefined>;
52
+ /** Writes an entry atomically; writes of one key never land out of order. */
53
+ put(entry: LifecycleEntry): Promise<void>;
54
+ /** Every readable entry; an unreadable one is skipped (and named), never deleted. */
55
+ list(onUnreadable?: (name: string, error: unknown) => void): Promise<LifecycleEntry[]>;
56
+ /** Removes terminal entries not written for `maxAgeMs`, and temporary files a crash left (only at start). */
57
+ prune(maxAgeMs?: number, { temporaries }?: {
58
+ temporaries?: boolean | undefined;
59
+ }): Promise<void>;
60
+ flush(): Promise<void>;
61
+ };
62
+ export declare const outcomeUnknown: (operation: string, requestId: string) => RpcError;
63
+ export type LifecycleSteps<Plan, Native, Result> = {
64
+ /** Validates and decides; may refuse (nothing is recorded). Runs inside the resource's queue. */
65
+ prepare(): Promise<Plan>;
66
+ /** The native step; a thrown error with `outcomeUnknown: true` records unknown, any other records failed. */
67
+ apply(plan: Plan): Promise<Native>;
68
+ /** alpd's own, idempotent commit from the entry alone (a restart replays it). */
69
+ commit(entry: LifecycleEntry<Plan, Native, Result>): Promise<Result>;
70
+ /** How a repeat answers from a committed entry (e.g. with a fresh session summary). */
71
+ repeat?(entry: LifecycleEntry<Plan, Native, Result>): Result | Promise<Result>;
72
+ };
73
+ /**
74
+ * Runs journaled operations: one at a time per resource, at most once per (resource, operation,
75
+ * requestId). The seam later lifecycle operations (live configure, reload) reuse with their own steps.
76
+ */
77
+ export declare function createLifecycle(journal: LifecycleJournal, { crashPoint, completed, }?: {
78
+ crashPoint?: (name: string) => void;
79
+ completed?: (entry: LifecycleEntry) => Promise<{
80
+ result: any;
81
+ } | undefined>;
82
+ }): {
83
+ run: <Plan, Native, Result>(request: {
84
+ resource: string;
85
+ operation: string;
86
+ requestId: string;
87
+ fingerprint: string;
88
+ }, steps: LifecycleSteps<Plan, Native, Result>) => Promise<{
89
+ result: Result;
90
+ duplicate: boolean;
91
+ entry: LifecycleEntry<Plan, Native, Result>;
92
+ }>;
93
+ serial: <T>(resource: string, work: () => Promise<T>) => Promise<T>;
94
+ /**
95
+ * At start, before any request: `prepared` becomes `unknown` (the native step is never sent again),
96
+ * and unknown outcomes are durably observed once, without claiming success or replaying a commit.
97
+ * `applied` rolls forward through the operation's commit (left `applied` when that fails, to be
98
+ * finished by a repeat or the next start; such entries are answered).
99
+ */
100
+ recover(commits: Record<string, (entry: LifecycleEntry) => Promise<unknown>>, log?: (message: string, fields: Record<string, unknown>) => void): Promise<LifecycleEntry<any, any, any>[]>;
101
+ };
@@ -0,0 +1,239 @@
1
+ // packages/daemon/src/lifecycle.ts
2
+ import { createHash, randomBytes } from "node:crypto";
3
+ import { mkdir, readdir, readFile, rm, stat, writeFile } from "node:fs/promises";
4
+ import path from "node:path";
5
+ import { replaceFile } from "./lock.js";
6
+ import { RpcError } from "./rpc-error.js";
7
+ var LIFECYCLE_RETENTION_MS = 7 * 24 * 60 * 60 * 1e3;
8
+ var keyOf = (resource, operation, requestId) => createHash("sha256").update(JSON.stringify(["lifecycle", resource, operation, requestId])).digest("hex");
9
+ function createLifecycleJournal(directory) {
10
+ const writes = /* @__PURE__ */ new Map();
11
+ let ready;
12
+ const prepare = () => ready ??= mkdir(directory, { recursive: true, mode: 448 }).then(() => {
13
+ }, (error) => {
14
+ ready = void 0;
15
+ throw error;
16
+ });
17
+ const file = (key) => path.join(directory, `${key}.json`);
18
+ async function read(name) {
19
+ let text;
20
+ try {
21
+ text = await readFile(path.join(directory, name), "utf8");
22
+ } catch (error) {
23
+ if (error?.code === "ENOENT") return void 0;
24
+ throw error;
25
+ }
26
+ const entry = JSON.parse(text);
27
+ if (entry?.version !== 1 || typeof entry.key !== "string" || typeof entry.state !== "string") throw new Error(`unreadable lifecycle entry ${name}`);
28
+ return entry;
29
+ }
30
+ return {
31
+ keyOf,
32
+ async get(resource, operation, requestId) {
33
+ const key = keyOf(resource, operation, requestId);
34
+ await writes.get(key);
35
+ await prepare();
36
+ return read(`${key}.json`);
37
+ },
38
+ /** Writes an entry atomically; writes of one key never land out of order. */
39
+ put(entry) {
40
+ const body = JSON.stringify({ ...entry, updatedAt: (/* @__PURE__ */ new Date()).toISOString() });
41
+ const next = (writes.get(entry.key) ?? Promise.resolve()).catch(() => {
42
+ }).then(prepare).then(async () => {
43
+ const temporary = `${file(entry.key)}.${process.pid}.${randomBytes(4).toString("hex")}.tmp`;
44
+ try {
45
+ await writeFile(temporary, body, { mode: 384 });
46
+ await replaceFile(temporary, file(entry.key));
47
+ } catch (error) {
48
+ await rm(temporary, { force: true }).catch(() => {
49
+ });
50
+ throw error;
51
+ }
52
+ });
53
+ writes.set(entry.key, next);
54
+ void next.finally(() => {
55
+ if (writes.get(entry.key) === next) writes.delete(entry.key);
56
+ }).catch(() => {
57
+ });
58
+ return next;
59
+ },
60
+ /** Every readable entry; an unreadable one is skipped (and named), never deleted. */
61
+ async list(onUnreadable) {
62
+ await prepare();
63
+ const entries = [];
64
+ for (const name of await readdir(directory)) {
65
+ if (!name.endsWith(".json")) continue;
66
+ try {
67
+ const entry = await read(name);
68
+ if (entry) entries.push(entry);
69
+ } catch (error) {
70
+ onUnreadable?.(name, error);
71
+ }
72
+ }
73
+ return entries;
74
+ },
75
+ /** Removes terminal entries not written for `maxAgeMs`, and temporary files a crash left (only at start). */
76
+ async prune(maxAgeMs = LIFECYCLE_RETENTION_MS, { temporaries = false } = {}) {
77
+ await prepare();
78
+ const now = Date.now();
79
+ for (const name of await readdir(directory)) {
80
+ const target = path.join(directory, name);
81
+ if (name.endsWith(".tmp")) {
82
+ if (temporaries) await rm(target, { force: true });
83
+ continue;
84
+ }
85
+ if (!name.endsWith(".json")) continue;
86
+ const entry = await read(name).catch(() => void 0);
87
+ if (!entry || !["committed", "failed", "unknown"].includes(entry.state)) continue;
88
+ const info = await stat(target).catch(() => void 0);
89
+ if (info && now - info.mtimeMs > maxAgeMs) await rm(target, { force: true });
90
+ }
91
+ },
92
+ async flush() {
93
+ await Promise.all([...writes.values()].map((write) => write.catch(() => {
94
+ })));
95
+ }
96
+ };
97
+ }
98
+ var answerOf = (error) => error instanceof RpcError ? { code: error.code, message: error.message, ...error.data !== void 0 ? { data: error.data } : {} } : { code: 1e3, message: error instanceof Error ? error.message : String(error) };
99
+ var outcomeUnknown = (operation, requestId) => new RpcError(1004, `alpd cannot tell whether ${operation} ${requestId} reached the runtime; check the session, then try again with a new requestId`, { reason: "outcome_unknown" });
100
+ function createLifecycle(journal, {
101
+ crashPoint = (_name) => {
102
+ },
103
+ completed = async (_entry) => void 0
104
+ } = {}) {
105
+ const queues = /* @__PURE__ */ new Map();
106
+ const inflight = /* @__PURE__ */ new Map();
107
+ function serial(resource, work) {
108
+ const run2 = (queues.get(resource) ?? Promise.resolve()).catch(() => {
109
+ }).then(work);
110
+ queues.set(resource, run2);
111
+ void run2.finally(() => {
112
+ if (queues.get(resource) === run2) queues.delete(resource);
113
+ }).catch(() => {
114
+ });
115
+ return run2;
116
+ }
117
+ async function run(request, steps) {
118
+ const key = journal.keyOf(request.resource, request.operation, request.requestId);
119
+ const earlier = inflight.get(key);
120
+ if (earlier) await earlier.catch(() => {
121
+ });
122
+ const attempt = serial(request.resource, async () => {
123
+ let entry;
124
+ try {
125
+ entry = await journal.get(request.resource, request.operation, request.requestId);
126
+ } catch (error) {
127
+ throw new RpcError(1e3, `alpd cannot read the journal of ${request.operation} ${request.requestId}`, { reason: "persistence_failed" });
128
+ }
129
+ if (entry) {
130
+ if (entry.fingerprint !== request.fingerprint) throw new RpcError(1004, `${request.operation} ${request.requestId} was already requested with other parameters`, { reason: "key_conflict" });
131
+ if (entry.state === "committed") return { result: steps.repeat ? await steps.repeat(entry) : entry.result, duplicate: true, entry };
132
+ if (entry.state === "failed") throw new RpcError(entry.error?.code ?? 1e3, entry.error?.message ?? `${request.operation} failed`, entry.error?.data);
133
+ if (entry.state === "applied") {
134
+ const saved = await completed(entry);
135
+ const done2 = await commit(entry, steps, saved);
136
+ return { result: done2.result, duplicate: saved !== void 0, entry: done2 };
137
+ }
138
+ throw outcomeUnknown(request.operation, request.requestId);
139
+ }
140
+ for (const pending of await journal.list()) {
141
+ if (pending.resource === request.resource && ["configure", "rewind"].includes(pending.operation) && pending.state === "applied" && !await completed(pending)) {
142
+ throw new RpcError(1e3, `alpd must finish ${pending.operation} ${pending.requestId} before another conversation mutation`, { reason: "persistence_failed" });
143
+ }
144
+ }
145
+ const plan = await steps.prepare();
146
+ const now = (/* @__PURE__ */ new Date()).toISOString();
147
+ entry = { version: 1, key, ...request, attemptId: randomBytes(16).toString("hex"), state: "prepared", plan, createdAt: now, updatedAt: now };
148
+ try {
149
+ await journal.put(entry);
150
+ } catch (error) {
151
+ throw new RpcError(1e3, `alpd could not record ${request.operation} ${request.requestId}; nothing was changed`, { reason: "persistence_failed" });
152
+ }
153
+ crashPoint(`${request.operation}.after-prepared`);
154
+ let native;
155
+ try {
156
+ native = await steps.apply(plan);
157
+ } catch (error) {
158
+ const unknown = error?.outcomeUnknown === true;
159
+ entry = { ...entry, state: unknown ? "unknown" : "failed", ...unknown ? {} : { error: answerOf(error) } };
160
+ await journal.put(entry).catch(() => {
161
+ });
162
+ if (unknown) throw outcomeUnknown(request.operation, request.requestId);
163
+ throw error;
164
+ }
165
+ entry = { ...entry, state: "applied", native };
166
+ try {
167
+ await journal.put(entry);
168
+ } catch {
169
+ throw outcomeUnknown(request.operation, request.requestId);
170
+ }
171
+ crashPoint(`${request.operation}.after-applied`);
172
+ const done = await commit(entry, steps);
173
+ return { result: done.result, duplicate: false, entry: done };
174
+ });
175
+ inflight.set(key, attempt);
176
+ void attempt.finally(() => {
177
+ if (inflight.get(key) === attempt) inflight.delete(key);
178
+ }).catch(() => {
179
+ });
180
+ return attempt;
181
+ }
182
+ async function commit(entry, steps, saved) {
183
+ let result;
184
+ try {
185
+ if (!saved && !entry.attemptId && ["configure", "rewind"].includes(entry.operation)) {
186
+ const newer = (await journal.list()).some((other) => other.resource === entry.resource && other.key !== entry.key && other.createdAt > entry.createdAt && ["applied", "committed"].includes(other.state));
187
+ if (newer) throw new RpcError(1e3, `alpd cannot safely replay ${entry.operation} ${entry.requestId} over a later conversation mutation`, { reason: "persistence_failed" });
188
+ }
189
+ result = saved ? saved.result : await steps.commit(entry);
190
+ } catch (error) {
191
+ if (error instanceof RpcError) throw error;
192
+ throw new RpcError(1e3, `alpd could not finish ${entry.operation} ${entry.requestId}; it finishes when tried again or when alpd starts`, { reason: "persistence_failed" });
193
+ }
194
+ crashPoint(`${entry.operation}.before-committed`);
195
+ const done = { ...entry, state: "committed", result };
196
+ await journal.put(done).catch(() => {
197
+ });
198
+ crashPoint(`${entry.operation}.after-committed`);
199
+ return done;
200
+ }
201
+ return {
202
+ run,
203
+ serial,
204
+ /**
205
+ * At start, before any request: `prepared` becomes `unknown` (the native step is never sent again),
206
+ * and unknown outcomes are durably observed once, without claiming success or replaying a commit.
207
+ * `applied` rolls forward through the operation's commit (left `applied` when that fails, to be
208
+ * finished by a repeat or the next start; such entries are answered).
209
+ */
210
+ async recover(commits, log) {
211
+ const entries = await journal.list((name, error) => log?.(`unreadable lifecycle entry ${name}`, { error }));
212
+ const unfinished = [];
213
+ for (const entry of entries) {
214
+ if (entry.state === "prepared" || entry.state === "unknown" && !entry.recoverySeenAt) {
215
+ try {
216
+ await journal.put({ ...entry, state: "unknown", recoverySeenAt: (/* @__PURE__ */ new Date()).toISOString() });
217
+ log?.(`${entry.operation} ${entry.requestId} of ${entry.resource} may or may not have reached the runtime`, { code: "ALPD_LIFECYCLE_UNKNOWN", operation: entry.operation, resource: entry.resource, requestId: entry.requestId });
218
+ } catch (error) {
219
+ log?.(`could not record unknown ${entry.operation} ${entry.requestId} of ${entry.resource}`, { code: "ALPD_LIFECYCLE_OBSERVE_FAILED", error });
220
+ }
221
+ } else if (entry.state === "applied") {
222
+ const committer = commits[entry.operation];
223
+ if (!committer) continue;
224
+ await serial(entry.resource, async () => commit(entry, { commit: committer }, await completed(entry))).catch((error) => {
225
+ unfinished.push(entry);
226
+ log?.(`could not finish ${entry.operation} ${entry.requestId} of ${entry.resource}`, { code: "ALPD_LIFECYCLE_ROLLFORWARD_FAILED", error });
227
+ });
228
+ }
229
+ }
230
+ return unfinished;
231
+ }
232
+ };
233
+ }
234
+ export {
235
+ LIFECYCLE_RETENTION_MS,
236
+ createLifecycle,
237
+ createLifecycleJournal,
238
+ outcomeUnknown
239
+ };