@anhlp/alp-daemon 0.0.1-beta.3 → 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 (95) hide show
  1. package/README.md +4 -0
  2. package/dist/THIRD-PARTY-NOTICES.txt +26 -1
  3. package/dist/alpd.js +29928 -14898
  4. package/dist/attachments.d.ts +187 -0
  5. package/dist/attachments.js +856 -0
  6. package/dist/build-manifest.json +34 -1
  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 +154 -2
  12. package/dist/files.js +392 -68
  13. package/dist/git.d.ts +93 -0
  14. package/dist/git.js +1192 -0
  15. package/dist/gitignore.d.ts +5 -0
  16. package/dist/gitignore.js +7 -1
  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 +43 -11
  24. package/dist/native-terminal.d.ts +45 -0
  25. package/dist/native-terminal.js +198 -0
  26. package/dist/operations.d.ts +8 -2
  27. package/dist/operations.js +20 -10
  28. package/dist/registry.d.ts +89 -0
  29. package/dist/registry.js +343 -0
  30. package/dist/scheduler.d.ts +96 -0
  31. package/dist/scheduler.js +296 -0
  32. package/dist/schedules.d.ts +70 -0
  33. package/dist/schedules.js +246 -0
  34. package/dist/server.d.ts +14 -2
  35. package/dist/server.js +836 -90
  36. package/dist/store.d.ts +72 -2
  37. package/dist/store.js +111 -4
  38. package/dist/terminal-native/README.md +201 -0
  39. package/dist/terminal-native/prebuilt/darwin-arm64/manifest.json +11 -0
  40. package/dist/terminal-native/prebuilt/darwin-arm64/terminal-owner +0 -0
  41. package/dist/terminal-native/prebuilt/darwin-x64/manifest.json +11 -0
  42. package/dist/terminal-native/prebuilt/darwin-x64/terminal-owner +0 -0
  43. package/dist/terminal-native/prebuilt/linux-arm64-glibc/manifest.json +10 -0
  44. package/dist/terminal-native/prebuilt/linux-arm64-glibc/terminal-owner +0 -0
  45. package/dist/terminal-native/prebuilt/linux-arm64-musl/manifest.json +10 -0
  46. package/dist/terminal-native/prebuilt/linux-arm64-musl/terminal-owner +0 -0
  47. package/dist/terminal-native/prebuilt/linux-x64-glibc/manifest.json +10 -0
  48. package/dist/terminal-native/prebuilt/linux-x64-glibc/terminal-owner +0 -0
  49. package/dist/terminal-native/prebuilt/linux-x64-musl/manifest.json +10 -0
  50. package/dist/terminal-native/prebuilt/linux-x64-musl/terminal-owner +0 -0
  51. package/dist/terminal-native/terminal-owner.c +389 -0
  52. package/dist/terminal-screen.d.ts +24 -0
  53. package/dist/terminal-screen.js +99 -0
  54. package/dist/terminals.d.ts +193 -0
  55. package/dist/terminals.js +570 -0
  56. package/dist/windows-terminal.d.ts +29 -0
  57. package/dist/windows-terminal.js +122 -0
  58. package/dist/worktrees.d.ts +65 -0
  59. package/dist/worktrees.js +318 -0
  60. package/native/README.md +201 -0
  61. package/native/prebuilt/darwin-arm64/manifest.json +11 -0
  62. package/native/prebuilt/darwin-arm64/terminal-owner +0 -0
  63. package/native/prebuilt/darwin-x64/manifest.json +11 -0
  64. package/native/prebuilt/darwin-x64/terminal-owner +0 -0
  65. package/native/prebuilt/linux-arm64-glibc/manifest.json +10 -0
  66. package/native/prebuilt/linux-arm64-glibc/terminal-owner +0 -0
  67. package/native/prebuilt/linux-arm64-musl/manifest.json +10 -0
  68. package/native/prebuilt/linux-arm64-musl/terminal-owner +0 -0
  69. package/native/prebuilt/linux-x64-glibc/manifest.json +10 -0
  70. package/native/prebuilt/linux-x64-glibc/terminal-owner +0 -0
  71. package/native/prebuilt/linux-x64-musl/manifest.json +10 -0
  72. package/native/prebuilt/linux-x64-musl/terminal-owner +0 -0
  73. package/native/terminal-owner.c +389 -0
  74. package/package.json +70 -7
  75. package/src/attachments.ts +908 -0
  76. package/src/conversation.ts +857 -0
  77. package/src/disk.ts +175 -0
  78. package/src/files.ts +528 -82
  79. package/src/git.ts +1140 -0
  80. package/src/gitignore.ts +10 -0
  81. package/src/history.ts +118 -0
  82. package/src/index.ts +7 -0
  83. package/src/lifecycle.ts +283 -0
  84. package/src/main.ts +37 -12
  85. package/src/native-terminal.ts +122 -0
  86. package/src/operations.ts +27 -11
  87. package/src/registry.ts +238 -0
  88. package/src/scheduler.ts +432 -0
  89. package/src/schedules.ts +297 -0
  90. package/src/server.ts +948 -102
  91. package/src/store.ts +117 -4
  92. package/src/terminal-screen.ts +74 -0
  93. package/src/terminals.ts +641 -0
  94. package/src/windows-terminal.ts +123 -0
  95. package/src/worktrees.ts +382 -0
package/src/store.ts CHANGED
@@ -1,10 +1,23 @@
1
- import { appendFile, mkdir, open, readdir, readFile, rm, stat, writeFile } from 'node:fs/promises';
1
+ import { createHistoryIndex } from './history.js';
2
+ import { appendFile, mkdir, open, readdir, readFile, rename, rm, stat, writeFile } from 'node:fs/promises';
2
3
  import { replaceFile } from './lock.js';
3
4
  import { randomBytes } from 'node:crypto';
4
5
  import path from 'node:path';
5
6
  import { createLogger } from '@anhlp/alp-core/log';
6
7
  import type { Envelope, SessionSnapshot, SessionSpec } from '@anhlp/alp-runtime/index';
7
8
  import type { SessionError, SessionStatus } from '@anhlp/alp-protocol/session';
9
+ import type { TurnAttention } from '@anhlp/alp-protocol/events';
10
+ import type { ConversationBranch, ForkProvenance, NativeBase } from '@anhlp/alp-protocol/session';
11
+
12
+ /** A branch a rewind set aside (§12.7): its public description, the native conversation and where its timeline lies. */
13
+ export type StoredBranch = ConversationBranch & {
14
+ /** The native conversation the root continued before the rewind: never deleted, so the branch stays recoverable. */
15
+ threadId: string;
16
+ base?: NativeBase;
17
+ /** The first discarded cursor, and the rewind's marker cursor (the branch holds what lay between). */
18
+ cut: number;
19
+ marker: number;
20
+ };
8
21
 
9
22
  /**
10
23
  * alpd's durable state (plans/reference/ALPD.md §5, §66): one JSON record per session,
@@ -30,6 +43,18 @@ export type SessionRecord = {
30
43
  title?: string;
31
44
  /** Set aside by the user: closed, and listed only when asked (ALPD §61). */
32
45
  archived?: boolean;
46
+ /** A root's last turn that ended unseen, until a client acknowledges it or another turn starts (Paseo's requiresAttention). */
47
+ attention?: TurnAttention;
48
+ /** Claude: the conversation `session.threadId` is made from on its first prompt, after a fork or rewind (§12.7). */
49
+ base?: NativeBase;
50
+ /** Where this root was forked from (§12.7). */
51
+ forkedFrom?: ForkProvenance;
52
+ /** Stable creation ownership; current native binding/title may change after a fork. */
53
+ forkAttempt?: { key: string; attemptId?: string; createdAt: string; fingerprint: string };
54
+ /** Branches rewinds set aside, oldest first (§12.7). */
55
+ branches?: StoredBranch[];
56
+ /** Atomic proof of completed conversation commits whose journal marker may be missing. */
57
+ lifecycleCommits?: Record<string, { operation: string; requestId: string; createdAt: string; fingerprint: string; attemptId?: string; result: any }>;
33
58
  createdAt: string;
34
59
  updatedAt: string;
35
60
  };
@@ -66,9 +91,10 @@ const TAIL_CHUNK = 64 * 1024;
66
91
 
67
92
  export type Store = ReturnType<typeof createStore>;
68
93
 
69
- export function createStore(root: string) {
94
+ export function createStore(root: string, options: { onWriteError?: (error: unknown) => void } = {}) {
70
95
  const sessionsDir = path.join(root, 'sessions');
71
96
  const timelineDir = path.join(root, 'timeline');
97
+ const historyIndexes = new Map<string, ReturnType<typeof createHistoryIndex>>();
72
98
  const receiptsDir = path.join(root, 'receipts');
73
99
  const writes = new Map<string, Promise<void>>();
74
100
  const appends = new Map<string, Promise<void>>();
@@ -110,6 +136,7 @@ export function createStore(root: string) {
110
136
  health.failures++;
111
137
  health.lastError = { at: new Date().toISOString(), operation, message: error?.message ?? String(error) };
112
138
  log.error(`${operation} failed`, { code: 'ALP_PERSISTENCE_WRITE_FAILED', operation, error, ...ids });
139
+ options.onWriteError?.(error);
113
140
  });
114
141
  map.set(key, settled);
115
142
  void settled.then(() => { if (map.get(key) === settled) map.delete(key); });
@@ -167,8 +194,12 @@ export function createStore(root: string) {
167
194
  }
168
195
 
169
196
  const receiptFile = (key: string) => path.join(receiptsDir, `${fileName(key)}.json`);
197
+ const branchFile = (rootId: string, branchId: string) => path.join(timelineDir, `${fileName(rootId)}.branch-${fileName(branchId)}.jsonl`);
170
198
 
171
199
  return {
200
+ /** The state directory this store keeps its files in. */
201
+ root,
202
+
172
203
  put(record: SessionRecord) {
173
204
  const file = path.join(sessionsDir, `${fileName(record.id)}.json`);
174
205
  const body = JSON.stringify(record);
@@ -241,6 +272,13 @@ export function createStore(root: string) {
241
272
  }
242
273
  },
243
274
 
275
+ /** Authoritative record after queued writes, including failed writes (which do not prove completion). */
276
+ async get(id: string): Promise<SessionRecord | undefined> {
277
+ await writes.get(id)?.catch(() => {});
278
+ try { return JSON.parse(await readFile(path.join(sessionsDir, `${fileName(id)}.json`), 'utf8')); }
279
+ catch (error: any) { if (error?.code === 'ENOENT') return undefined; throw error; }
280
+ },
281
+
244
282
  async list(): Promise<SessionRecord[]> {
245
283
  await prepare();
246
284
  const records: SessionRecord[] = [];
@@ -267,6 +305,78 @@ export function createStore(root: string) {
267
305
  }, true, { rootId });
268
306
  },
269
307
 
308
+ async history(rootId: string, sessionId: string, options: { before?: string; limit?: number; maxBytes?: number } = {}) {
309
+ await prepare();
310
+ await appends.get(rootId);
311
+ let index = historyIndexes.get(rootId);
312
+ if (!index) { index = createHistoryIndex(path.join(timelineDir, `${fileName(rootId)}.jsonl`)); historyIndexes.set(rootId, index); }
313
+ return index.page(sessionId, options);
314
+ },
315
+
316
+ /**
317
+ * Rewinds a tree's timeline (§12.7), idempotently: the whole timeline moves to the branch file
318
+ * `<root>.branch-<id>.jsonl`, and the root's timeline becomes its lines before `cut`, the root's own
319
+ * session lifecycle lines after it (opened, updated, closed: not conversation), `marker`, and any line
320
+ * whose cursor follows the marker's, in cursor order. Runs in
321
+ * the tree's append queue, so no append lands in between. A crash leaves the old timeline, or the
322
+ * branch and the new one beside it (finished by the next call); the history index is rebuilt with a
323
+ * new generation, so every older history cursor is stale.
324
+ */
325
+ rewind(rootId: string, { cut, branchId, marker }: { cut: number; branchId: string; marker: Envelope }) {
326
+ const file = path.join(timelineDir, `${fileName(rootId)}.jsonl`);
327
+ const branch = branchFile(rootId, branchId);
328
+ const temporary = `${file}.rewind-${fileName(branchId)}.tmp`;
329
+ return queue(appends, rootId, `timeline rewind ${rootId}`, async () => {
330
+ const exists = (target: string) => stat(target).then(() => true, (error: any) => { if (error?.code === 'ENOENT') return false; throw error; });
331
+ if (!await exists(branch)) {
332
+ if (!repaired.has(rootId)) { await repairTail(file); repaired.add(rootId); }
333
+ const text = await readFile(file, 'utf8').catch((error: any) => { if (error?.code === 'ENOENT') return ''; throw error; });
334
+ const kept: Array<{ cursor: number; line: string }> = [];
335
+ const markerCursor = marker.cursor ?? Number.MAX_SAFE_INTEGER;
336
+ let cursor = 0;
337
+ for (const line of text.split('\n')) {
338
+ if (!line) continue;
339
+ let envelope: Envelope;
340
+ try { envelope = JSON.parse(line); } catch { continue; }
341
+ cursor = typeof envelope.cursor === 'number' && envelope.cursor > cursor ? envelope.cursor : cursor + 1;
342
+ const lifecycle = envelope.sessionId === rootId && ['session.opened', 'session.ready', 'session.updated', 'session.closed', 'session.failed'].includes(envelope.event?.type);
343
+ // Before the cut: the kept conversation. Between the cut and the marker: only the root's own
344
+ // lifecycle. After the marker (an event kept while the rewind waited for this queue): the new
345
+ // conversation's. A line kept past the cut carries its cursor, so cursors stay what clients saw.
346
+ if (cursor < cut) kept.push({ cursor, line });
347
+ else if (lifecycle || cursor > markerCursor) kept.push({ cursor, line: JSON.stringify({ ...envelope, cursor }) });
348
+ }
349
+ kept.push({ cursor: markerCursor, line: JSON.stringify(marker) });
350
+ kept.sort((a, b) => a.cursor - b.cursor);
351
+ await writeFile(temporary, `${kept.map(entry => entry.line).join('\n')}\n`, { mode: 0o600 });
352
+ if (await exists(file)) await rename(file, branch);
353
+ else await writeFile(branch, '', { mode: 0o600 });
354
+ }
355
+ if (await exists(temporary)) await rename(temporary, file);
356
+ await Promise.all(['.jsonl.idx', '.jsonl.history.json'].map(suffix => rm(path.join(timelineDir, `${fileName(rootId)}${suffix}`), { force: true })));
357
+ historyIndexes.delete(rootId);
358
+ repaired.add(rootId);
359
+ }, false, { rootId });
360
+ },
361
+
362
+ /** Writes a new tree's whole timeline at once (a fork's, §12.7); written again whole by a repeat. */
363
+ createTimeline(rootId: string, envelopes: Envelope[]) {
364
+ const file = path.join(timelineDir, `${fileName(rootId)}.jsonl`);
365
+ return queue(appends, rootId, `timeline ${rootId}`, async () => {
366
+ await writeAtomic(file, envelopes.map(envelope => `${JSON.stringify(envelope)}\n`).join(''));
367
+ await Promise.all(['.jsonl.idx', '.jsonl.history.json'].map(suffix => rm(path.join(timelineDir, `${fileName(rootId)}${suffix}`), { force: true })));
368
+ historyIndexes.delete(rootId);
369
+ repaired.add(rootId);
370
+ }, false, { rootId });
371
+ },
372
+
373
+ /** The timeline of a branch a rewind set aside; empty when it is not there. */
374
+ async branchTimeline(rootId: string, branchId: string): Promise<Envelope[]> {
375
+ await appends.get(rootId);
376
+ try { return parseTimeline(await readFile(branchFile(rootId, branchId), 'utf8'), path.basename(branchFile(rootId, branchId))); }
377
+ catch (error: any) { if (error?.code === 'ENOENT') return []; throw error; }
378
+ },
379
+
270
380
  async timeline(rootId: string): Promise<Envelope[]> {
271
381
  await appends.get(rootId);
272
382
  const name = `${fileName(rootId)}.jsonl`;
@@ -317,15 +427,18 @@ export function createStore(root: string) {
317
427
  return { ...health, ...(health.lastError ? { lastError: { ...health.lastError } } : {}) };
318
428
  },
319
429
 
320
- /** Removes a whole tree: its records and its timeline. */
430
+ /** Removes a whole tree: its records, its timeline and the branches rewinds set aside. */
321
431
  async remove(rootId: string, ids: string[]) {
322
432
  await Promise.all([...ids.map(id => writes.get(id)), appends.get(rootId)]);
433
+ const branches = (await readdir(timelineDir).catch(() => [] as string[])).filter(name => name.startsWith(`${fileName(rootId)}.branch-`));
323
434
  await Promise.all([
324
435
  ...ids.map(id => rm(path.join(sessionsDir, `${fileName(id)}.json`), { force: true })),
325
- rm(path.join(timelineDir, `${fileName(rootId)}.jsonl`), { force: true }),
436
+ ...['.jsonl', '.jsonl.idx', '.jsonl.history.json'].map(suffix => rm(path.join(timelineDir, `${fileName(rootId)}${suffix}`), { force: true })),
437
+ ...branches.map(name => rm(path.join(timelineDir, name), { force: true })),
326
438
  ]);
327
439
  for (const id of ids) failed.delete(id);
328
440
  failed.delete(`timeline:${rootId}`);
441
+ historyIndexes.delete(rootId);
329
442
  repaired.delete(rootId);
330
443
  },
331
444
 
@@ -0,0 +1,74 @@
1
+ import headless from '@xterm/headless';
2
+ import { ERROR } from '@anhlp/alp-protocol';
3
+ import { RpcError } from './rpc-error.js';
4
+
5
+ // Exact xterm 6.0.0, also used by Paseo 0.12. No raw replay reconstruction: one parser from spawn.
6
+ // Bound both queued work and lifetime input (including OSC/combining payloads hidden from rows).
7
+ export const SCREEN_LIMITS = Object.freeze({ scrollback: 1000, pendingBytes: 1024 * 1024, pendingOperations: 256, inputBytes: 8 * 1024 * 1024, resultBytes: 1024 * 1024, retained: 16 });
8
+ export class TerminalScreen {
9
+ #term: InstanceType<typeof headless.Terminal> | undefined;
10
+ #pending = 0;
11
+ #input = 0;
12
+ #operations = 0;
13
+ #waiters = new Set<() => void>();
14
+ constructor(cols: number, rows: number) {
15
+ this.#term = new headless.Terminal({ cols, rows, scrollback: SCREEN_LIMITS.scrollback, allowProposedApi: true, logLevel: 'off' });
16
+ }
17
+ get available() { return this.#term !== undefined; }
18
+ #unavailable() { return new RpcError(ERROR.unavailable, 'Terminal capture is unavailable: its bounded screen was released or exceeded its resource limit', { reason: 'terminal_capture_unavailable' }); }
19
+ #queue(term: InstanceType<typeof headless.Terminal>, data: string, callback: () => void) {
20
+ if (this.#operations >= SCREEN_LIMITS.pendingOperations) { this.dispose(); return; }
21
+ this.#operations++;
22
+ try {
23
+ term.write(data, () => { this.#operations--; callback(); });
24
+ } catch { this.#operations--; this.dispose(); }
25
+ }
26
+ write(data: string) {
27
+ const term = this.#term;
28
+ if (!term) return;
29
+ const bytes = Buffer.byteLength(data);
30
+ this.#input += bytes;
31
+ if (this.#input > SCREEN_LIMITS.inputBytes || this.#pending + bytes > SCREEN_LIMITS.pendingBytes) { this.dispose(); return; }
32
+ this.#pending += bytes;
33
+ this.#queue(term, data, () => { this.#pending -= bytes; });
34
+ }
35
+ resize(cols: number, rows: number) {
36
+ const term = this.#term;
37
+ // Keep resize between the old output and output caused by the new PTY size.
38
+ if (term) this.#queue(term, '', () => { if (this.#term === term) term.resize(cols, rows); });
39
+ }
40
+ capture(options: { start?: number; end?: number; stripAnsi?: boolean } = {}): Promise<{ lines: string[]; totalLines: number }> {
41
+ const term = this.#term;
42
+ if (!term) return Promise.reject(this.#unavailable());
43
+ return new Promise((resolve, reject) => {
44
+ const released = () => reject(this.#unavailable());
45
+ this.#waiters.add(released);
46
+ // The callback is a parser barrier, not a delay or a ring-buffer approximation.
47
+ this.#queue(term, '', () => {
48
+ this.#waiters.delete(released);
49
+ if (this.#term !== term) { reject(this.#unavailable()); return; }
50
+ const buffer = term.buffer.active;
51
+ const totalLines = buffer.length;
52
+ const index = (value: number | undefined, fallback: number) => value === undefined ? fallback : Math.max(0, Math.min(totalLines - 1, value < 0 ? totalLines + value : value));
53
+ const start = index(options.start, 0);
54
+ const end = index(options.end, totalLines - 1);
55
+ const lines: string[] = [];
56
+ let bytes = 0;
57
+ for (let i = start; i <= end; i++) {
58
+ // Cell text, like Paseo: styling escapes are not regenerated when stripAnsi=false.
59
+ const line = buffer.getLine(i)?.translateToString(true).trimEnd() ?? '';
60
+ bytes += Buffer.byteLength(JSON.stringify(line)) + 1;
61
+ if (bytes > SCREEN_LIMITS.resultBytes - 128) { reject(new RpcError(ERROR.unavailable, 'Terminal capture exceeds the response limit; select a smaller line range', { reason: 'terminal_capture_too_large' })); return; }
62
+ lines.push(line);
63
+ }
64
+ resolve({ lines, totalLines });
65
+ });
66
+ });
67
+ }
68
+ dispose() {
69
+ this.#term?.dispose();
70
+ this.#term = undefined;
71
+ for (const waiter of this.#waiters) waiter();
72
+ this.#waiters.clear();
73
+ }
74
+ }