@anhlp/alp-daemon 0.0.0-stage → 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.
- package/LICENSE +202 -0
- package/NOTICE +7 -0
- package/README.md +14 -2
- package/dist/THIRD-PARTY-NOTICES.txt +456 -0
- package/dist/alp-templates.d.ts +5 -0
- package/dist/alpd.js +64050 -0
- package/dist/attachments.d.ts +187 -0
- package/dist/attachments.js +856 -0
- package/dist/build-manifest.json +150 -0
- package/dist/conversation.d.ts +119 -0
- package/dist/conversation.js +688 -0
- package/dist/disk.d.ts +67 -0
- package/dist/disk.js +152 -0
- package/dist/files.d.ts +262 -0
- package/dist/files.js +674 -0
- package/dist/git.d.ts +98 -0
- package/dist/git.js +1246 -0
- package/dist/gitignore.d.ts +62 -0
- package/dist/gitignore.js +254 -0
- package/dist/history.d.ts +24 -0
- package/dist/history.js +149 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +28 -0
- package/dist/lifecycle.d.ts +101 -0
- package/dist/lifecycle.js +239 -0
- package/dist/lock.d.ts +21 -0
- package/dist/lock.js +42 -0
- package/dist/main.d.ts +1 -0
- package/dist/main.js +363 -0
- package/dist/model-catalog.d.ts +2 -0
- package/dist/model-catalog.js +44 -0
- package/dist/native-entry.d.ts +2 -0
- package/dist/native-entry.js +3 -0
- package/dist/native-register.d.ts +1 -0
- package/dist/native-register.js +13 -0
- package/dist/native-terminal.d.ts +45 -0
- package/dist/native-terminal.js +201 -0
- package/dist/operations.d.ts +40 -0
- package/dist/operations.js +313 -0
- package/dist/registry.d.ts +89 -0
- package/dist/registry.js +343 -0
- package/dist/rpc-error.d.ts +6 -0
- package/dist/rpc-error.js +11 -0
- package/dist/scheduler.d.ts +96 -0
- package/dist/scheduler.js +298 -0
- package/dist/schedules.d.ts +70 -0
- package/dist/schedules.js +246 -0
- package/dist/server.d.ts +60 -0
- package/dist/server.js +1958 -0
- package/dist/store.d.ts +178 -0
- package/dist/store.js +386 -0
- package/dist/terminal-native/README.md +201 -0
- package/dist/terminal-native/prebuilt/darwin-arm64/manifest.json +11 -0
- package/dist/terminal-native/prebuilt/darwin-arm64/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/darwin-x64/manifest.json +11 -0
- package/dist/terminal-native/prebuilt/darwin-x64/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-arm64-glibc/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-arm64-glibc/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-arm64-musl/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-arm64-musl/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-x64-glibc/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-x64-glibc/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-x64-musl/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-x64-musl/terminal-owner +0 -0
- package/dist/terminal-native/terminal-owner.c +389 -0
- package/dist/terminal-screen.d.ts +24 -0
- package/dist/terminal-screen.js +99 -0
- package/dist/terminals.d.ts +203 -0
- package/dist/terminals.js +570 -0
- package/dist/windows-terminal.d.ts +32 -0
- package/dist/windows-terminal.js +79 -0
- package/dist/worktrees.d.ts +65 -0
- package/dist/worktrees.js +318 -0
- package/native/README.md +201 -0
- package/native/prebuilt/darwin-arm64/manifest.json +11 -0
- package/native/prebuilt/darwin-arm64/terminal-owner +0 -0
- package/native/prebuilt/darwin-x64/manifest.json +11 -0
- package/native/prebuilt/darwin-x64/terminal-owner +0 -0
- package/native/prebuilt/linux-arm64-glibc/manifest.json +10 -0
- package/native/prebuilt/linux-arm64-glibc/terminal-owner +0 -0
- package/native/prebuilt/linux-arm64-musl/manifest.json +10 -0
- package/native/prebuilt/linux-arm64-musl/terminal-owner +0 -0
- package/native/prebuilt/linux-x64-glibc/manifest.json +10 -0
- package/native/prebuilt/linux-x64-glibc/terminal-owner +0 -0
- package/native/prebuilt/linux-x64-musl/manifest.json +10 -0
- package/native/prebuilt/linux-x64-musl/terminal-owner +0 -0
- package/native/terminal-owner.c +389 -0
- package/package.json +163 -4
- package/src/alp-templates.d.ts +5 -0
- package/src/attachments.ts +908 -0
- package/src/conversation.ts +857 -0
- package/src/disk.ts +175 -0
- package/src/files.ts +884 -0
- package/src/git.ts +1220 -0
- package/src/gitignore.ts +277 -0
- package/src/history.ts +118 -0
- package/src/index.ts +12 -0
- package/src/lifecycle.ts +283 -0
- package/src/lock.ts +53 -0
- package/src/main.ts +381 -0
- package/src/model-catalog.ts +36 -0
- package/src/native-entry.ts +5 -0
- package/src/native-register.ts +21 -0
- package/src/native-terminal.ts +127 -0
- package/src/operations.ts +329 -0
- package/src/registry.ts +238 -0
- package/src/rpc-error.ts +4 -0
- package/src/scheduler.ts +438 -0
- package/src/schedules.ts +297 -0
- package/src/server.ts +2238 -0
- package/src/store.ts +449 -0
- package/src/terminal-screen.ts +74 -0
- package/src/terminals.ts +653 -0
- package/src/windows-terminal.ts +74 -0
- package/src/worktrees.ts +382 -0
package/dist/store.d.ts
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import type { Envelope, SessionSnapshot, SessionSpec } from '@anhlp/alp-runtime/index';
|
|
2
|
+
import type { SessionError, SessionStatus } from '@anhlp/alp-protocol/session';
|
|
3
|
+
import type { TurnAttention } from '@anhlp/alp-protocol/events';
|
|
4
|
+
import type { ConversationBranch, ForkProvenance, NativeBase } from '@anhlp/alp-protocol/session';
|
|
5
|
+
/** A branch a rewind set aside (§12.7): its public description, the native conversation and where its timeline lies. */
|
|
6
|
+
export type StoredBranch = ConversationBranch & {
|
|
7
|
+
/** The native conversation the root continued before the rewind: never deleted, so the branch stays recoverable. */
|
|
8
|
+
threadId: string;
|
|
9
|
+
base?: NativeBase;
|
|
10
|
+
/** The first discarded cursor, and the rewind's marker cursor (the branch holds what lay between). */
|
|
11
|
+
cut: number;
|
|
12
|
+
marker: number;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* alpd's durable state (plans/reference/ALPD.md §5, §66): one JSON record per session,
|
|
16
|
+
* written atomically, one append-only JSONL timeline per tree, and one receipt per
|
|
17
|
+
* delivered prompt. Writes survive a crash of the alpd process once their promise
|
|
18
|
+
* resolves; nothing is fsynced, so a power loss or a failing disk may lose recent writes.
|
|
19
|
+
*/
|
|
20
|
+
export type { SessionError, SessionStatus } from '@anhlp/alp-protocol/session';
|
|
21
|
+
export type SessionRecord = {
|
|
22
|
+
version: 1;
|
|
23
|
+
id: string;
|
|
24
|
+
rootId: string;
|
|
25
|
+
/** The client's request, kept for roots so they can be resumed. */
|
|
26
|
+
spec?: SessionSpec;
|
|
27
|
+
delegation?: boolean;
|
|
28
|
+
session?: SessionSnapshot;
|
|
29
|
+
status: SessionStatus;
|
|
30
|
+
lastError?: SessionError;
|
|
31
|
+
title?: string;
|
|
32
|
+
/** Set aside by the user: closed, and listed only when asked (ALPD §61). */
|
|
33
|
+
archived?: boolean;
|
|
34
|
+
/** A root's last turn that ended unseen, until a client acknowledges it or another turn starts (Paseo's requiresAttention). */
|
|
35
|
+
attention?: TurnAttention;
|
|
36
|
+
/** Claude: the conversation `session.threadId` is made from on its first prompt, after a fork or rewind (§12.7). */
|
|
37
|
+
base?: NativeBase;
|
|
38
|
+
/** Where this root was forked from (§12.7). */
|
|
39
|
+
forkedFrom?: ForkProvenance;
|
|
40
|
+
/** Stable creation ownership; current native binding/title may change after a fork. */
|
|
41
|
+
forkAttempt?: {
|
|
42
|
+
key: string;
|
|
43
|
+
attemptId?: string;
|
|
44
|
+
createdAt: string;
|
|
45
|
+
fingerprint: string;
|
|
46
|
+
};
|
|
47
|
+
/** Branches rewinds set aside, oldest first (§12.7). */
|
|
48
|
+
branches?: StoredBranch[];
|
|
49
|
+
/** Atomic proof of completed conversation commits whose journal marker may be missing. */
|
|
50
|
+
lifecycleCommits?: Record<string, {
|
|
51
|
+
operation: string;
|
|
52
|
+
requestId: string;
|
|
53
|
+
createdAt: string;
|
|
54
|
+
fingerprint: string;
|
|
55
|
+
attemptId?: string;
|
|
56
|
+
result: any;
|
|
57
|
+
}>;
|
|
58
|
+
createdAt: string;
|
|
59
|
+
updatedAt: string;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Delivery of one clientMessageId; `pending` means alpd may have stopped while sending it.
|
|
63
|
+
* A completed receipt says what the runtime answered: `unknown` when the provider may or
|
|
64
|
+
* may not have the message (its process died or timed out mid-request).
|
|
65
|
+
*/
|
|
66
|
+
export type Receipt = {
|
|
67
|
+
version: 1;
|
|
68
|
+
sessionId: string;
|
|
69
|
+
clientMessageId: string;
|
|
70
|
+
fingerprint: string;
|
|
71
|
+
state: 'pending' | 'completed';
|
|
72
|
+
outcome?: 'accepted' | 'failed' | 'unknown';
|
|
73
|
+
error?: {
|
|
74
|
+
message: string;
|
|
75
|
+
code?: string;
|
|
76
|
+
};
|
|
77
|
+
/** Completed at the next start from the tree's timeline, not by the delivery itself. */
|
|
78
|
+
reconciled?: boolean;
|
|
79
|
+
updatedAt: string;
|
|
80
|
+
};
|
|
81
|
+
/** A receipt file that exists but cannot be read as one: its delivery's outcome is unknown. */
|
|
82
|
+
export type UnreadableReceipt = {
|
|
83
|
+
version: 1;
|
|
84
|
+
state: 'unreadable';
|
|
85
|
+
};
|
|
86
|
+
/** Failed writes since alpd started; `ok` until the first one (ALPD §66). */
|
|
87
|
+
export type StoreHealth = {
|
|
88
|
+
ok: boolean;
|
|
89
|
+
failures: number;
|
|
90
|
+
lastError?: {
|
|
91
|
+
at: string;
|
|
92
|
+
operation: string;
|
|
93
|
+
message: string;
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
export type Store = ReturnType<typeof createStore>;
|
|
97
|
+
export declare function createStore(root: string, options?: {
|
|
98
|
+
onWriteError?: (error: unknown) => void;
|
|
99
|
+
}): {
|
|
100
|
+
/** The state directory this store keeps its files in. */
|
|
101
|
+
root: string;
|
|
102
|
+
put(record: SessionRecord): Promise<void>;
|
|
103
|
+
receipt(key: string): Promise<Receipt | UnreadableReceipt | undefined>;
|
|
104
|
+
putReceipt(key: string, receipt: Receipt): Promise<void>;
|
|
105
|
+
removeReceipt(key: string): Promise<void>;
|
|
106
|
+
/** Every readable receipt with its key and when its file was last written. */
|
|
107
|
+
receipts(): Promise<Array<{
|
|
108
|
+
key: string;
|
|
109
|
+
receipt: Receipt;
|
|
110
|
+
writtenAt: number;
|
|
111
|
+
}>>;
|
|
112
|
+
/** When the newest of these records' files was written (a restore writes them anew); 0 when none is there. */
|
|
113
|
+
writtenAt(ids: string[]): Promise<number>;
|
|
114
|
+
/**
|
|
115
|
+
* Deletes receipts not written for `maxAgeMs` and, with `temporaries` (only at start, when no
|
|
116
|
+
* write can be under way), temporary files a crash left behind.
|
|
117
|
+
*/
|
|
118
|
+
pruneReceipts(maxAgeMs: number, { temporaries }?: {
|
|
119
|
+
temporaries?: boolean | undefined;
|
|
120
|
+
}): Promise<void>;
|
|
121
|
+
/** Authoritative record after queued writes, including failed writes (which do not prove completion). */
|
|
122
|
+
get(id: string): Promise<SessionRecord | undefined>;
|
|
123
|
+
list(): Promise<SessionRecord[]>;
|
|
124
|
+
append(rootId: string, envelope: Envelope): Promise<void>;
|
|
125
|
+
history(rootId: string, sessionId: string, options?: {
|
|
126
|
+
before?: string;
|
|
127
|
+
limit?: number;
|
|
128
|
+
maxBytes?: number;
|
|
129
|
+
}): Promise<{
|
|
130
|
+
tipCursor: number;
|
|
131
|
+
olderCursor?: string | undefined;
|
|
132
|
+
generation: string;
|
|
133
|
+
items: import("./history.js").HistoryItem[];
|
|
134
|
+
hasOlder: boolean;
|
|
135
|
+
}>;
|
|
136
|
+
/**
|
|
137
|
+
* Rewinds a tree's timeline (§12.7), idempotently: the whole timeline moves to the branch file
|
|
138
|
+
* `<root>.branch-<id>.jsonl`, and the root's timeline becomes its lines before `cut`, the root's own
|
|
139
|
+
* session lifecycle lines after it (opened, updated, closed: not conversation), `marker`, and any line
|
|
140
|
+
* whose cursor follows the marker's, in cursor order. Runs in
|
|
141
|
+
* the tree's append queue, so no append lands in between. A crash leaves the old timeline, or the
|
|
142
|
+
* branch and the new one beside it (finished by the next call); the history index is rebuilt with a
|
|
143
|
+
* new generation, so every older history cursor is stale.
|
|
144
|
+
*/
|
|
145
|
+
rewind(rootId: string, { cut, branchId, marker }: {
|
|
146
|
+
cut: number;
|
|
147
|
+
branchId: string;
|
|
148
|
+
marker: Envelope;
|
|
149
|
+
}): Promise<void>;
|
|
150
|
+
/** Writes a new tree's whole timeline at once (a fork's, §12.7); written again whole by a repeat. */
|
|
151
|
+
createTimeline(rootId: string, envelopes: Envelope[]): Promise<void>;
|
|
152
|
+
/** The timeline of a branch a rewind set aside; empty when it is not there. */
|
|
153
|
+
branchTimeline(rootId: string, branchId: string): Promise<Envelope[]>;
|
|
154
|
+
timeline(rootId: string): Promise<Envelope[]>;
|
|
155
|
+
/**
|
|
156
|
+
* Waits for the queued writes of these records and timelines, and names those that
|
|
157
|
+
* failed: a record whose last write failed, or a timeline that lost a line.
|
|
158
|
+
*/
|
|
159
|
+
drain({ records, timelines }: {
|
|
160
|
+
records?: string[] | undefined;
|
|
161
|
+
timelines?: string[] | undefined;
|
|
162
|
+
}): Promise<string[]>;
|
|
163
|
+
/**
|
|
164
|
+
* Whether the state directory can be read and written right now: a small file is written,
|
|
165
|
+
* read back and removed. `StoreHealth` only says what failed since alpd started.
|
|
166
|
+
*/
|
|
167
|
+
probe(): Promise<{
|
|
168
|
+
ok: boolean;
|
|
169
|
+
code?: string;
|
|
170
|
+
message?: string;
|
|
171
|
+
}>;
|
|
172
|
+
/** How many records, receipts and timelines are failed now: a later successful write clears a record, a lost timeline line never clears. */
|
|
173
|
+
failing(): number;
|
|
174
|
+
health(): StoreHealth;
|
|
175
|
+
/** Removes a whole tree: its records, its timeline and the branches rewinds set aside. */
|
|
176
|
+
remove(rootId: string, ids: string[]): Promise<void>;
|
|
177
|
+
flush(): Promise<void>;
|
|
178
|
+
};
|
package/dist/store.js
ADDED
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
// packages/daemon/src/store.ts
|
|
2
|
+
import { createHistoryIndex } from "./history.js";
|
|
3
|
+
import { appendFile, mkdir, open, readdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
4
|
+
import { replaceFile } from "./lock.js";
|
|
5
|
+
import { randomBytes } from "node:crypto";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { createLogger } from "@anhlp/alp-core/log";
|
|
8
|
+
var log = createLogger({ component: "store" });
|
|
9
|
+
var SAFE = /[^\w.:-]/g;
|
|
10
|
+
var fileName = (id) => id.replace(SAFE, "_");
|
|
11
|
+
var TAIL_CHUNK = 64 * 1024;
|
|
12
|
+
function createStore(root, options = {}) {
|
|
13
|
+
const sessionsDir = path.join(root, "sessions");
|
|
14
|
+
const timelineDir = path.join(root, "timeline");
|
|
15
|
+
const historyIndexes = /* @__PURE__ */ new Map();
|
|
16
|
+
const receiptsDir = path.join(root, "receipts");
|
|
17
|
+
const writes = /* @__PURE__ */ new Map();
|
|
18
|
+
const appends = /* @__PURE__ */ new Map();
|
|
19
|
+
const failed = /* @__PURE__ */ new Map();
|
|
20
|
+
const repaired = /* @__PURE__ */ new Set();
|
|
21
|
+
const health = { ok: true, failures: 0 };
|
|
22
|
+
let ready;
|
|
23
|
+
const prepare = () => ready ??= Promise.all([
|
|
24
|
+
mkdir(sessionsDir, { recursive: true, mode: 448 }),
|
|
25
|
+
mkdir(timelineDir, { recursive: true, mode: 448 }),
|
|
26
|
+
mkdir(receiptsDir, { recursive: true, mode: 448 })
|
|
27
|
+
]).then(() => {
|
|
28
|
+
}, (error) => {
|
|
29
|
+
ready = void 0;
|
|
30
|
+
throw error;
|
|
31
|
+
});
|
|
32
|
+
async function writeAtomic(file, body) {
|
|
33
|
+
const temporary = `${file}.${process.pid}.${randomBytes(4).toString("hex")}.tmp`;
|
|
34
|
+
try {
|
|
35
|
+
await writeFile(temporary, body, { mode: 384 });
|
|
36
|
+
await replaceFile(temporary, file);
|
|
37
|
+
} catch (error) {
|
|
38
|
+
await rm(temporary, { force: true }).catch(() => {
|
|
39
|
+
});
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function queue(map, key, operation, work, sticky = false, ids = {}) {
|
|
44
|
+
const next = (map.get(key) ?? Promise.resolve()).then(prepare).then(work);
|
|
45
|
+
const failure = map === appends ? `timeline:${key}` : key;
|
|
46
|
+
const settled = next.then(
|
|
47
|
+
() => {
|
|
48
|
+
if (!sticky) failed.delete(failure);
|
|
49
|
+
},
|
|
50
|
+
(error) => {
|
|
51
|
+
failed.set(failure, error);
|
|
52
|
+
health.ok = false;
|
|
53
|
+
health.failures++;
|
|
54
|
+
health.lastError = { at: (/* @__PURE__ */ new Date()).toISOString(), operation, message: error?.message ?? String(error) };
|
|
55
|
+
log.error(`${operation} failed`, { code: "ALP_PERSISTENCE_WRITE_FAILED", operation, error, ...ids });
|
|
56
|
+
options.onWriteError?.(error);
|
|
57
|
+
}
|
|
58
|
+
);
|
|
59
|
+
map.set(key, settled);
|
|
60
|
+
void settled.then(() => {
|
|
61
|
+
if (map.get(key) === settled) map.delete(key);
|
|
62
|
+
});
|
|
63
|
+
return next;
|
|
64
|
+
}
|
|
65
|
+
async function repairTail(file) {
|
|
66
|
+
const handle = await open(file, "r+").catch((error) => {
|
|
67
|
+
if (error?.code === "ENOENT") return void 0;
|
|
68
|
+
throw error;
|
|
69
|
+
});
|
|
70
|
+
if (!handle) return;
|
|
71
|
+
try {
|
|
72
|
+
const { size } = await handle.stat();
|
|
73
|
+
if (!size) return;
|
|
74
|
+
const last = Buffer.alloc(1);
|
|
75
|
+
await handle.read(last, 0, 1, size - 1);
|
|
76
|
+
if (last[0] === 10) return;
|
|
77
|
+
let start = 0;
|
|
78
|
+
for (let end = size; end > 0 && !start; end -= TAIL_CHUNK) {
|
|
79
|
+
const from = Math.max(0, end - TAIL_CHUNK);
|
|
80
|
+
const chunk = Buffer.alloc(end - from);
|
|
81
|
+
await handle.read(chunk, 0, chunk.length, from);
|
|
82
|
+
const newline = chunk.lastIndexOf(10);
|
|
83
|
+
if (newline >= 0) start = from + newline + 1;
|
|
84
|
+
}
|
|
85
|
+
const tail = Buffer.alloc(size - start);
|
|
86
|
+
await handle.read(tail, 0, tail.length, start);
|
|
87
|
+
let whole = false;
|
|
88
|
+
try {
|
|
89
|
+
JSON.parse(tail.toString("utf8"));
|
|
90
|
+
whole = true;
|
|
91
|
+
} catch {
|
|
92
|
+
}
|
|
93
|
+
if (whole) await handle.write("\n", size);
|
|
94
|
+
else await handle.truncate(start);
|
|
95
|
+
log.warn(`${whole ? "completed" : "removed"} a timeline line cut short in ${path.basename(file)} (${tail.length} bytes)`, { code: "ALP_TIMELINE_REPAIRED", file: path.basename(file), bytes: tail.length });
|
|
96
|
+
} finally {
|
|
97
|
+
await handle.close();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
function parseTimeline(text, name) {
|
|
101
|
+
const envelopes = [];
|
|
102
|
+
let cursor = 0;
|
|
103
|
+
let skipped = 0;
|
|
104
|
+
for (const line of text.split("\n")) {
|
|
105
|
+
if (!line) continue;
|
|
106
|
+
let envelope;
|
|
107
|
+
try {
|
|
108
|
+
envelope = JSON.parse(line);
|
|
109
|
+
} catch {
|
|
110
|
+
skipped++;
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
cursor = typeof envelope.cursor === "number" && envelope.cursor > cursor ? envelope.cursor : cursor + 1;
|
|
114
|
+
envelopes.push({ ...envelope, cursor });
|
|
115
|
+
}
|
|
116
|
+
if (skipped) log.warn(`skipped ${skipped} unreadable timeline lines in ${name}`, { code: "ALP_TIMELINE_UNREADABLE", file: name, skipped });
|
|
117
|
+
return envelopes;
|
|
118
|
+
}
|
|
119
|
+
const receiptFile = (key) => path.join(receiptsDir, `${fileName(key)}.json`);
|
|
120
|
+
const branchFile = (rootId, branchId) => path.join(timelineDir, `${fileName(rootId)}.branch-${fileName(branchId)}.jsonl`);
|
|
121
|
+
return {
|
|
122
|
+
/** The state directory this store keeps its files in. */
|
|
123
|
+
root,
|
|
124
|
+
put(record) {
|
|
125
|
+
const file = path.join(sessionsDir, `${fileName(record.id)}.json`);
|
|
126
|
+
const body = JSON.stringify(record);
|
|
127
|
+
return queue(writes, record.id, `record ${record.id}`, () => writeAtomic(file, body), false, { sessionId: record.id, rootId: record.rootId });
|
|
128
|
+
},
|
|
129
|
+
async receipt(key) {
|
|
130
|
+
await writes.get(`receipt:${key}`);
|
|
131
|
+
let text;
|
|
132
|
+
try {
|
|
133
|
+
text = await readFile(receiptFile(key), "utf8");
|
|
134
|
+
} catch (error) {
|
|
135
|
+
if (error?.code === "ENOENT") return void 0;
|
|
136
|
+
throw error;
|
|
137
|
+
}
|
|
138
|
+
try {
|
|
139
|
+
const receipt = JSON.parse(text);
|
|
140
|
+
if (receipt?.version === 1 && typeof receipt.fingerprint === "string" && (receipt.state === "pending" || receipt.state === "completed")) return receipt;
|
|
141
|
+
} catch {
|
|
142
|
+
}
|
|
143
|
+
return { version: 1, state: "unreadable" };
|
|
144
|
+
},
|
|
145
|
+
putReceipt(key, receipt) {
|
|
146
|
+
const body = JSON.stringify(receipt);
|
|
147
|
+
return queue(writes, `receipt:${key}`, `receipt ${receipt.clientMessageId}`, () => writeAtomic(receiptFile(key), body), false, { sessionId: receipt.sessionId, clientMessageId: receipt.clientMessageId });
|
|
148
|
+
},
|
|
149
|
+
removeReceipt(key) {
|
|
150
|
+
return queue(writes, `receipt:${key}`, `receipt removal ${key}`, () => rm(receiptFile(key), { force: true }));
|
|
151
|
+
},
|
|
152
|
+
/** Every readable receipt with its key and when its file was last written. */
|
|
153
|
+
async receipts() {
|
|
154
|
+
await prepare();
|
|
155
|
+
const found = [];
|
|
156
|
+
for (const name of await readdir(receiptsDir)) {
|
|
157
|
+
if (!name.endsWith(".json")) continue;
|
|
158
|
+
const key = name.slice(0, -".json".length);
|
|
159
|
+
try {
|
|
160
|
+
const file = path.join(receiptsDir, name);
|
|
161
|
+
const receipt = JSON.parse(await readFile(file, "utf8"));
|
|
162
|
+
if (receipt?.version === 1 && typeof receipt.sessionId === "string") found.push({ key, receipt, writtenAt: (await stat(file)).mtimeMs });
|
|
163
|
+
} catch {
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return found;
|
|
167
|
+
},
|
|
168
|
+
/** When the newest of these records' files was written (a restore writes them anew); 0 when none is there. */
|
|
169
|
+
async writtenAt(ids) {
|
|
170
|
+
let newest = 0;
|
|
171
|
+
for (const id of ids) newest = Math.max(newest, await stat(path.join(sessionsDir, `${fileName(id)}.json`)).then((info) => info.mtimeMs, () => 0));
|
|
172
|
+
return newest;
|
|
173
|
+
},
|
|
174
|
+
/**
|
|
175
|
+
* Deletes receipts not written for `maxAgeMs` and, with `temporaries` (only at start, when no
|
|
176
|
+
* write can be under way), temporary files a crash left behind.
|
|
177
|
+
*/
|
|
178
|
+
async pruneReceipts(maxAgeMs, { temporaries = true } = {}) {
|
|
179
|
+
await prepare();
|
|
180
|
+
const now = Date.now();
|
|
181
|
+
for (const directory of [sessionsDir, receiptsDir]) {
|
|
182
|
+
for (const name of await readdir(directory)) {
|
|
183
|
+
const file = path.join(directory, name);
|
|
184
|
+
if (name.endsWith(".tmp")) {
|
|
185
|
+
if (temporaries) await rm(file, { force: true });
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
if (directory !== receiptsDir) continue;
|
|
189
|
+
const info = await stat(file).catch(() => void 0);
|
|
190
|
+
if (info && now - info.mtimeMs > maxAgeMs) await rm(file, { force: true });
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
},
|
|
194
|
+
/** Authoritative record after queued writes, including failed writes (which do not prove completion). */
|
|
195
|
+
async get(id) {
|
|
196
|
+
await writes.get(id)?.catch(() => {
|
|
197
|
+
});
|
|
198
|
+
try {
|
|
199
|
+
return JSON.parse(await readFile(path.join(sessionsDir, `${fileName(id)}.json`), "utf8"));
|
|
200
|
+
} catch (error) {
|
|
201
|
+
if (error?.code === "ENOENT") return void 0;
|
|
202
|
+
throw error;
|
|
203
|
+
}
|
|
204
|
+
},
|
|
205
|
+
async list() {
|
|
206
|
+
await prepare();
|
|
207
|
+
const records = [];
|
|
208
|
+
for (const name of await readdir(sessionsDir)) {
|
|
209
|
+
if (!name.endsWith(".json")) continue;
|
|
210
|
+
try {
|
|
211
|
+
const record = JSON.parse(await readFile(path.join(sessionsDir, name), "utf8"));
|
|
212
|
+
if (record?.version === 1 && typeof record.id === "string") records.push(record);
|
|
213
|
+
else log.warn(`skipped invalid record ${name}`, { code: "ALP_RECORD_UNREADABLE", file: name });
|
|
214
|
+
} catch {
|
|
215
|
+
log.warn(`skipped unreadable record ${name}`, { code: "ALP_RECORD_UNREADABLE", file: name });
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return records;
|
|
219
|
+
},
|
|
220
|
+
append(rootId, envelope) {
|
|
221
|
+
const file = path.join(timelineDir, `${fileName(rootId)}.jsonl`);
|
|
222
|
+
const line = `${JSON.stringify(envelope)}
|
|
223
|
+
`;
|
|
224
|
+
return queue(appends, rootId, `timeline ${rootId}`, async () => {
|
|
225
|
+
if (!repaired.has(rootId)) {
|
|
226
|
+
await repairTail(file);
|
|
227
|
+
repaired.add(rootId);
|
|
228
|
+
}
|
|
229
|
+
await appendFile(file, line, { mode: 384 });
|
|
230
|
+
}, true, { rootId });
|
|
231
|
+
},
|
|
232
|
+
async history(rootId, sessionId, options2 = {}) {
|
|
233
|
+
await prepare();
|
|
234
|
+
await appends.get(rootId);
|
|
235
|
+
let index = historyIndexes.get(rootId);
|
|
236
|
+
if (!index) {
|
|
237
|
+
index = createHistoryIndex(path.join(timelineDir, `${fileName(rootId)}.jsonl`));
|
|
238
|
+
historyIndexes.set(rootId, index);
|
|
239
|
+
}
|
|
240
|
+
return index.page(sessionId, options2);
|
|
241
|
+
},
|
|
242
|
+
/**
|
|
243
|
+
* Rewinds a tree's timeline (§12.7), idempotently: the whole timeline moves to the branch file
|
|
244
|
+
* `<root>.branch-<id>.jsonl`, and the root's timeline becomes its lines before `cut`, the root's own
|
|
245
|
+
* session lifecycle lines after it (opened, updated, closed: not conversation), `marker`, and any line
|
|
246
|
+
* whose cursor follows the marker's, in cursor order. Runs in
|
|
247
|
+
* the tree's append queue, so no append lands in between. A crash leaves the old timeline, or the
|
|
248
|
+
* branch and the new one beside it (finished by the next call); the history index is rebuilt with a
|
|
249
|
+
* new generation, so every older history cursor is stale.
|
|
250
|
+
*/
|
|
251
|
+
rewind(rootId, { cut, branchId, marker }) {
|
|
252
|
+
const file = path.join(timelineDir, `${fileName(rootId)}.jsonl`);
|
|
253
|
+
const branch = branchFile(rootId, branchId);
|
|
254
|
+
const temporary = `${file}.rewind-${fileName(branchId)}.tmp`;
|
|
255
|
+
return queue(appends, rootId, `timeline rewind ${rootId}`, async () => {
|
|
256
|
+
const exists = (target) => stat(target).then(() => true, (error) => {
|
|
257
|
+
if (error?.code === "ENOENT") return false;
|
|
258
|
+
throw error;
|
|
259
|
+
});
|
|
260
|
+
if (!await exists(branch)) {
|
|
261
|
+
if (!repaired.has(rootId)) {
|
|
262
|
+
await repairTail(file);
|
|
263
|
+
repaired.add(rootId);
|
|
264
|
+
}
|
|
265
|
+
const text = await readFile(file, "utf8").catch((error) => {
|
|
266
|
+
if (error?.code === "ENOENT") return "";
|
|
267
|
+
throw error;
|
|
268
|
+
});
|
|
269
|
+
const kept = [];
|
|
270
|
+
const markerCursor = marker.cursor ?? Number.MAX_SAFE_INTEGER;
|
|
271
|
+
let cursor = 0;
|
|
272
|
+
for (const line of text.split("\n")) {
|
|
273
|
+
if (!line) continue;
|
|
274
|
+
let envelope;
|
|
275
|
+
try {
|
|
276
|
+
envelope = JSON.parse(line);
|
|
277
|
+
} catch {
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
cursor = typeof envelope.cursor === "number" && envelope.cursor > cursor ? envelope.cursor : cursor + 1;
|
|
281
|
+
const lifecycle = envelope.sessionId === rootId && ["session.opened", "session.ready", "session.updated", "session.closed", "session.failed"].includes(envelope.event?.type);
|
|
282
|
+
if (cursor < cut) kept.push({ cursor, line });
|
|
283
|
+
else if (lifecycle || cursor > markerCursor) kept.push({ cursor, line: JSON.stringify({ ...envelope, cursor }) });
|
|
284
|
+
}
|
|
285
|
+
kept.push({ cursor: markerCursor, line: JSON.stringify(marker) });
|
|
286
|
+
kept.sort((a, b) => a.cursor - b.cursor);
|
|
287
|
+
await writeFile(temporary, `${kept.map((entry) => entry.line).join("\n")}
|
|
288
|
+
`, { mode: 384 });
|
|
289
|
+
if (await exists(file)) await rename(file, branch);
|
|
290
|
+
else await writeFile(branch, "", { mode: 384 });
|
|
291
|
+
}
|
|
292
|
+
if (await exists(temporary)) await rename(temporary, file);
|
|
293
|
+
await Promise.all([".jsonl.idx", ".jsonl.history.json"].map((suffix) => rm(path.join(timelineDir, `${fileName(rootId)}${suffix}`), { force: true })));
|
|
294
|
+
historyIndexes.delete(rootId);
|
|
295
|
+
repaired.add(rootId);
|
|
296
|
+
}, false, { rootId });
|
|
297
|
+
},
|
|
298
|
+
/** Writes a new tree's whole timeline at once (a fork's, §12.7); written again whole by a repeat. */
|
|
299
|
+
createTimeline(rootId, envelopes) {
|
|
300
|
+
const file = path.join(timelineDir, `${fileName(rootId)}.jsonl`);
|
|
301
|
+
return queue(appends, rootId, `timeline ${rootId}`, async () => {
|
|
302
|
+
await writeAtomic(file, envelopes.map((envelope) => `${JSON.stringify(envelope)}
|
|
303
|
+
`).join(""));
|
|
304
|
+
await Promise.all([".jsonl.idx", ".jsonl.history.json"].map((suffix) => rm(path.join(timelineDir, `${fileName(rootId)}${suffix}`), { force: true })));
|
|
305
|
+
historyIndexes.delete(rootId);
|
|
306
|
+
repaired.add(rootId);
|
|
307
|
+
}, false, { rootId });
|
|
308
|
+
},
|
|
309
|
+
/** The timeline of a branch a rewind set aside; empty when it is not there. */
|
|
310
|
+
async branchTimeline(rootId, branchId) {
|
|
311
|
+
await appends.get(rootId);
|
|
312
|
+
try {
|
|
313
|
+
return parseTimeline(await readFile(branchFile(rootId, branchId), "utf8"), path.basename(branchFile(rootId, branchId)));
|
|
314
|
+
} catch (error) {
|
|
315
|
+
if (error?.code === "ENOENT") return [];
|
|
316
|
+
throw error;
|
|
317
|
+
}
|
|
318
|
+
},
|
|
319
|
+
async timeline(rootId) {
|
|
320
|
+
await appends.get(rootId);
|
|
321
|
+
const name = `${fileName(rootId)}.jsonl`;
|
|
322
|
+
try {
|
|
323
|
+
return parseTimeline(await readFile(path.join(timelineDir, name), "utf8"), name);
|
|
324
|
+
} catch (error) {
|
|
325
|
+
if (error?.code === "ENOENT") return [];
|
|
326
|
+
throw error;
|
|
327
|
+
}
|
|
328
|
+
},
|
|
329
|
+
/**
|
|
330
|
+
* Waits for the queued writes of these records and timelines, and names those that
|
|
331
|
+
* failed: a record whose last write failed, or a timeline that lost a line.
|
|
332
|
+
*/
|
|
333
|
+
async drain({ records = [], timelines = [] }) {
|
|
334
|
+
await Promise.all([...records.map((id) => writes.get(id)), ...timelines.map((id) => appends.get(id))]);
|
|
335
|
+
return [...records.filter((id) => failed.has(id)), ...timelines.filter((id) => failed.has(`timeline:${id}`))];
|
|
336
|
+
},
|
|
337
|
+
/**
|
|
338
|
+
* Whether the state directory can be read and written right now: a small file is written,
|
|
339
|
+
* read back and removed. `StoreHealth` only says what failed since alpd started.
|
|
340
|
+
*/
|
|
341
|
+
async probe() {
|
|
342
|
+
const file = path.join(root, `.probe-${process.pid}-${randomBytes(3).toString("hex")}.tmp`);
|
|
343
|
+
const token = randomBytes(8).toString("hex");
|
|
344
|
+
let step = "write";
|
|
345
|
+
try {
|
|
346
|
+
await prepare();
|
|
347
|
+
await writeFile(file, token, { mode: 384 });
|
|
348
|
+
step = "read";
|
|
349
|
+
if (await readFile(file, "utf8") !== token) throw new Error("what was written is not what was read back");
|
|
350
|
+
return { ok: true };
|
|
351
|
+
} catch (error) {
|
|
352
|
+
return { ok: false, code: step === "write" ? "ALP_STATE_NOT_WRITABLE" : "ALP_STATE_NOT_READABLE", message: `${error?.code ? `${error.code}: ` : ""}${error?.message ?? error}` };
|
|
353
|
+
} finally {
|
|
354
|
+
await rm(file, { force: true }).catch(() => {
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
},
|
|
358
|
+
/** How many records, receipts and timelines are failed now: a later successful write clears a record, a lost timeline line never clears. */
|
|
359
|
+
failing() {
|
|
360
|
+
return failed.size;
|
|
361
|
+
},
|
|
362
|
+
health() {
|
|
363
|
+
return { ...health, ...health.lastError ? { lastError: { ...health.lastError } } : {} };
|
|
364
|
+
},
|
|
365
|
+
/** Removes a whole tree: its records, its timeline and the branches rewinds set aside. */
|
|
366
|
+
async remove(rootId, ids) {
|
|
367
|
+
await Promise.all([...ids.map((id) => writes.get(id)), appends.get(rootId)]);
|
|
368
|
+
const branches = (await readdir(timelineDir).catch(() => [])).filter((name) => name.startsWith(`${fileName(rootId)}.branch-`));
|
|
369
|
+
await Promise.all([
|
|
370
|
+
...ids.map((id) => rm(path.join(sessionsDir, `${fileName(id)}.json`), { force: true })),
|
|
371
|
+
...[".jsonl", ".jsonl.idx", ".jsonl.history.json"].map((suffix) => rm(path.join(timelineDir, `${fileName(rootId)}${suffix}`), { force: true })),
|
|
372
|
+
...branches.map((name) => rm(path.join(timelineDir, name), { force: true }))
|
|
373
|
+
]);
|
|
374
|
+
for (const id of ids) failed.delete(id);
|
|
375
|
+
failed.delete(`timeline:${rootId}`);
|
|
376
|
+
historyIndexes.delete(rootId);
|
|
377
|
+
repaired.delete(rootId);
|
|
378
|
+
},
|
|
379
|
+
async flush() {
|
|
380
|
+
await Promise.all([...writes.values(), ...appends.values()]);
|
|
381
|
+
}
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
export {
|
|
385
|
+
createStore
|
|
386
|
+
};
|