Repository navigation
Expand file tree
/
Copy pathjournal.ts
More file actions
582 lines (558 loc) · 20.6 KB
/
Copy pathjournal.ts
File metadata and controls
582 lines (558 loc) · 20.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
import * as fs from 'fs';
import * as path from 'path';
import { createHash } from 'crypto';
import { AgentSnapshot, ExecutionRecord, SessionEvent, Submission, identifier } from './protocol';
import {
JournalIndex,
getJournalIndex,
getJournalIndexAsync,
indexedAssetIds,
indexEvent,
recentEvents,
recentEventsAsync,
saveJournalIndex,
segmentNames,
repairJournalTail,
replayEvents,
indexSegment,
} from './journalIndex';
const maxRecentBytes = 3 * 1024 * 1024;
const maxMemoryBytes = 4 * 1024 * 1024;
/** Directory timestamps can be coarse; the durable admission set is authoritative. */
function sameExecutionFiles(entries: [string, unknown][], names: string[]): boolean {
const ids = new Set(entries.map(([id]) => id));
return (
entries.length === names.length &&
ids.size === names.length &&
names.every((name) => ids.has(name.slice(0, -5)))
);
}
/** Durable records stay on disk; only a byte-bounded working set retains source code. */
class ExecutionLedger {
private metadata = new Map<string, { order: number; state: ExecutionRecord['state'] }>();
private cache = new Map<string, { record: ExecutionRecord; bytes: number }>();
private cacheBytes = 0;
constructor(private directory: string) {
const folder = path.join(directory, 'executions');
const names = fs.readdirSync(folder).filter((name) => name.endsWith('.json'));
let entries: [string, { order: number; state: ExecutionRecord['state'] }][] | undefined;
try {
const saved = JSON.parse(
fs.readFileSync(path.join(directory, 'executions-index.json'), 'utf8'),
) as { data: string; hash: string; mtime: number };
if (
saved.mtime === fs.statSync(folder).mtimeMs &&
createHash('sha256').update(saved.data).digest('hex') === saved.hash
) {
entries = JSON.parse(saved.data) as typeof entries;
if (
!Array.isArray(entries) ||
!sameExecutionFiles(entries, names) ||
entries.some(([id, data]) => {
identifier(id);
return !Number.isSafeInteger(data.order);
})
) {
entries = undefined;
}
}
} catch {
entries = undefined;
}
if (entries) {
this.metadata = new Map(entries);
} else {
for (const name of names) {
const record = JSON.parse(
fs.readFileSync(path.join(folder, name), 'utf8'),
) as ExecutionRecord;
identifier(record.id);
this.set(record);
}
this.metadata = new Map([...this.metadata].sort((a, b) => a[1].order - b[1].order));
}
}
get size(): number {
return this.metadata.size;
}
get(id: string): ExecutionRecord | undefined {
if (!this.metadata.has(id)) {
return;
}
const cached = this.cache.get(id);
if (cached) {
this.cache.delete(id);
this.cache.set(id, cached);
return cached.record;
}
const record = JSON.parse(
fs.readFileSync(
path.join(this.directory, 'executions', identifier(id) + '.json'),
'utf8',
),
) as ExecutionRecord;
this.remember(record);
return record;
}
set(record: ExecutionRecord): void {
this.metadata.set(record.id, { order: record.order, state: record.state });
this.remember(record);
}
*values(): Generator<ExecutionRecord> {
for (const id of this.metadata.keys()) {
yield this.get(id)!;
}
}
*unfinished(): Generator<ExecutionRecord> {
for (const [id, meta] of this.metadata) {
if (meta.state === 'queued' || meta.state === 'running') {
yield this.get(id)!;
}
}
}
recent(limit: number, budget = Number.POSITIVE_INFINITY): ExecutionRecord[] {
const records: ExecutionRecord[] = [];
for (const id of [...this.metadata.keys()].slice(-limit).reverse()) {
const record = this.get(id)!,
bytes = Buffer.byteLength(JSON.stringify(record));
if (bytes > budget) {
break;
}
records.unshift(record);
budget -= bytes;
}
return records;
}
history(
query: string,
before: number,
limit = 100,
): { executions: ExecutionRecord[]; more: boolean } {
const needle = query.toLocaleLowerCase(),
executions: ExecutionRecord[] = [];
let bytes = 0;
for (const id of [...this.metadata.keys()].reverse()) {
if (this.metadata.get(id)!.order >= before) {
continue;
}
const record = this.get(id)!;
if (!record.code.toLocaleLowerCase().includes(needle)) {
continue;
}
const size = Buffer.byteLength(JSON.stringify(record));
if (executions.length >= limit || bytes + size > 2 * 1024 * 1024) {
return { executions, more: true };
}
executions.push(record);
bytes += size;
}
return { executions, more: false };
}
save(): void {
const data = JSON.stringify([...this.metadata]);
atomicJson(path.join(this.directory, 'executions-index.json'), {
data,
hash: createHash('sha256').update(data).digest('hex'),
mtime: fs.statSync(path.join(this.directory, 'executions')).mtimeMs,
});
}
private remember(record: ExecutionRecord): void {
const previous = this.cache.get(record.id);
if (previous) {
this.cacheBytes -= previous.bytes;
this.cache.delete(record.id);
}
const bytes = Buffer.byteLength(JSON.stringify(record));
this.cache.set(record.id, { record, bytes });
this.cacheBytes += bytes;
while (this.cacheBytes > maxMemoryBytes && this.cache.size) {
const id = this.cache.keys().next().value as string;
this.cacheBytes -= this.cache.get(id)!.bytes;
this.cache.delete(id);
}
}
}
function recentRecords(directory: string, limit: number): ExecutionRecord[] {
return new ExecutionLedger(directory).recent(Math.max(1, limit), 1400 * 1024);
}
async function recentRecordsAsync(directory: string, limit: number): Promise<ExecutionRecord[]> {
const folder = path.join(directory, 'executions');
const names = (await fs.promises.readdir(folder)).filter((name) => name.endsWith('.json'));
let metadata: [string, { order: number }][] | undefined;
try {
const saved = JSON.parse(
await fs.promises.readFile(path.join(directory, 'executions-index.json'), 'utf8'),
) as { data: string; hash: string; mtime: number };
if (
saved.mtime === (await fs.promises.stat(folder)).mtimeMs &&
createHash('sha256').update(saved.data).digest('hex') === saved.hash
) {
metadata = JSON.parse(saved.data) as typeof metadata;
if (
!Array.isArray(metadata) ||
!sameExecutionFiles(metadata, names) ||
metadata.some(([id, data]) => {
identifier(id);
return !Number.isSafeInteger(data.order);
})
) {
metadata = undefined;
}
}
} catch {
metadata = undefined;
}
if (!metadata) {
metadata = [];
for (let start = 0; start < names.length; start += 16) {
const records = await Promise.all(
names
.slice(start, start + 16)
.map(
async (name) =>
JSON.parse(
await fs.promises.readFile(path.join(folder, name), 'utf8'),
) as ExecutionRecord,
),
);
for (const record of records) {
identifier(record.id);
metadata.push([record.id, { order: record.order }]);
}
}
metadata.sort((a, b) => a[1].order - b[1].order);
}
const records: ExecutionRecord[] = [];
let budget = 1400 * 1024;
for (const [id] of metadata.slice(-Math.max(1, limit)).reverse()) {
const record = JSON.parse(
await fs.promises.readFile(path.join(folder, identifier(id) + '.json'), 'utf8'),
) as ExecutionRecord;
const bytes = Buffer.byteLength(JSON.stringify(record));
if (bytes > budget) {
break;
}
records.unshift(record);
budget -= bytes;
}
return records;
}
function previousSnapshot(
executions: ExecutionRecord[],
events: SessionEvent[],
seq: number,
): Pick<AgentSnapshot, 'executions' | 'events' | 'seq' | 'truncated'> {
const admitted = new Set(
events.filter((event) => event.type === 'accepted').map((event) => event.executionId),
);
return {
executions,
events,
seq,
truncated: executions
.filter((record) => !admitted.has(record.id))
.map((record) => record.id),
};
}
/** Read a completed process without opening a writer or repairing its journal. */
export function readPreviousJournal(
storage: string,
generation: string,
limit: number,
): Pick<AgentSnapshot, 'executions' | 'events' | 'seq' | 'truncated'> {
const directory = path.join(storage, identifier(generation));
const executions = recentRecords(directory, limit),
index = getJournalIndex(directory, generation);
const budget =
maxRecentBytes -
executions.reduce((total, record) => total + Buffer.byteLength(JSON.stringify(record)), 0);
return previousSnapshot(
executions,
recentEvents(directory, index, new Set(executions.map((record) => record.id)), budget),
index.seq,
);
}
/** Cold legacy indexes and event ranges are read in chunks without blocking the editor. */
export async function readPreviousJournalAsync(
storage: string,
generation: string,
limit: number,
): Promise<Pick<AgentSnapshot, 'executions' | 'events' | 'seq' | 'truncated'>> {
const directory = path.join(storage, identifier(generation));
const index = await getJournalIndexAsync(directory, generation);
const executions = await recentRecordsAsync(directory, limit);
const budget =
maxRecentBytes -
executions.reduce((total, record) => total + Buffer.byteLength(JSON.stringify(record)), 0);
return previousSnapshot(
executions,
await recentEventsAsync(
directory,
index,
new Set(executions.map((record) => record.id)),
budget,
),
index.seq,
);
}
export function readExecutionRecords(directory: string): ExecutionRecord[] {
return [...new ExecutionLedger(directory).values()];
}
export async function readExecutionRecordsAsync(directory: string): Promise<ExecutionRecord[]> {
const folder = path.join(directory, 'executions');
const names = (await fs.promises.readdir(folder)).filter((name) => name.endsWith('.json'));
const records: ExecutionRecord[] = [];
for (let start = 0; start < names.length; start += 16) {
const batch = await Promise.all(
names
.slice(start, start + 16)
.map(
async (name) =>
JSON.parse(
await fs.promises.readFile(path.join(folder, name), 'utf8'),
) as ExecutionRecord,
),
);
for (const record of batch) {
identifier(record.id);
records.push(record);
}
}
return records.sort((a, b) => a.order - b.order);
}
export function atomicJson(file: string, value: unknown): void {
const temporary = `${file}.${process.pid}.tmp`;
const fd = fs.openSync(temporary, 'w', 0o600);
try {
fs.writeFileSync(fd, JSON.stringify(value));
fs.fsyncSync(fd);
} finally {
fs.closeSync(fd);
}
fs.renameSync(temporary, file);
}
/** Latest committed display versions; changed/corrupt journals force an index rebuild. */
export function retainedAssetIds(storage: string): Set<string> {
const retained = new Set<string>();
for (const generation of fs.readdirSync(storage, { withFileTypes: true })) {
if (!generation.isDirectory() || generation.name === 'assets') {
continue;
}
const index = getJournalIndex(path.join(storage, generation.name), generation.name, true);
for (const id of indexedAssetIds(index)) {
retained.add(id);
}
}
return retained;
}
/** Durable admission ledger plus a bounded, segmented output journal. */
export class SessionJournal {
readonly executions: ExecutionLedger;
private index: JournalIndex;
private memoryBytes = 0;
private eventSizes: number[] = [];
private events: SessionEvent[] = [];
private fd: number;
private size = 0;
private segment = 0;
private nextOrder = 1;
seq = 0;
earliestSeq = 1;
constructor(
readonly directory: string,
private readonly generation: string,
private readonly maxBytes = 128 * 1024 * 1024,
) {
fs.mkdirSync(directory, { recursive: true, mode: 0o700 });
fs.mkdirSync(path.join(directory, 'executions'), { recursive: true, mode: 0o700 });
this.executions = new ExecutionLedger(directory);
this.nextOrder = (this.executions.recent(1)[0]?.order ?? 0) + 1;
const segments = this.segments();
for (const name of segments) {
repairJournalTail(path.join(directory, name));
}
if (segments.length) {
this.segment = Number(segments[segments.length - 1].slice(7, -6));
}
this.index = getJournalIndex(directory, generation);
this.seq = this.index.seq;
this.earliestSeq =
this.index.segments.find((segment) => segment.first)?.first ?? this.seq + 1;
const file = this.segmentFile();
this.fd = fs.openSync(file, 'a', 0o600);
this.size = fs.fstatSync(this.fd).size;
indexSegment(this.index, path.basename(file), fs.fstatSync(this.fd));
this.trimMemory();
}
accept(request: Submission): { record: ExecutionRecord; duplicate: boolean } {
const hash = createHash('sha256').update(request.code).digest('hex');
const previous = this.executions.get(request.id);
if (previous) {
if (previous.hash !== hash) {
throw new Error('Execution ID already used for different code');
}
return { record: previous, duplicate: true };
}
const record: ExecutionRecord = {
...request,
hash,
state: 'queued',
order: this.nextOrder++,
accepted: Date.now(),
};
this.update(record);
return { record, duplicate: false };
}
update(record: ExecutionRecord): void {
atomicJson(
path.join(this.directory, 'executions', `${identifier(record.id)}.json`),
record,
);
this.executions.set(record);
}
append(
type: string,
data: Record<string, unknown>,
executionId?: string,
durable = false,
): SessionEvent {
const event: SessionEvent = {
seq: this.seq + 1,
generation: this.generation,
type,
data,
executionId,
time: Date.now(),
};
const line = `${JSON.stringify(event)}\n`;
fs.writeSync(this.fd, line);
if (durable) {
fs.fsyncSync(this.fd);
}
this.seq = event.seq;
this.size += Buffer.byteLength(line);
indexEvent(
this.index,
path.basename(this.segmentFile()),
event,
this.size - Buffer.byteLength(line),
Buffer.byteLength(line),
fs.fstatSync(this.fd),
);
this.remember(event, Buffer.byteLength(line));
if (this.size >= 4 * 1024 * 1024) {
this.rotate();
}
this.trimMemory();
return event;
}
replay(after: number, limit = 1000): { events: SessionEvent[]; reset: boolean; seq: number } {
if (after < this.earliestSeq - 1) {
return { events: [], reset: true, seq: this.seq };
}
const events: SessionEvent[] = [];
if (after >= (this.events[0]?.seq ?? this.seq + 1) - 1) {
let bytes = 0;
for (const event of this.events) {
if (event.seq <= after) {
continue;
}
const size = Buffer.byteLength(JSON.stringify(event));
if (events.length && (bytes + size > 3 * 1024 * 1024 || events.length >= limit)) {
break;
}
events.push(event);
bytes += size;
}
} else {
return {
events: replayEvents(this.directory, this.index, after, maxRecentBytes, limit),
reset: false,
seq: this.seq,
};
}
return { events, reset: false, seq: this.seq };
}
recent(
limit: number,
activeId?: string,
): { executions: ExecutionRecord[]; events: SessionEvent[] } {
const records = this.executions.recent(limit, 1400 * 1024);
const active = activeId && this.executions.get(activeId);
if (active && !records.some((record) => record.id === active.id)) {
records.push(active);
}
records.sort((a, b) =>
a.id === activeId ? -1 : b.id === activeId ? 1 : b.order - a.order,
);
const executions: ExecutionRecord[] = [];
let recordBytes = 0;
for (const record of records) {
const bytes = Buffer.byteLength(JSON.stringify(record));
// Raw code is bounded at admission, but JSON escaping can exceed the
// history allowance. Active work must survive reconnect regardless.
if (record.id !== activeId && recordBytes + bytes > 1400 * 1024) {
continue;
}
executions.push(record);
recordBytes += bytes;
}
executions.sort((a, b) => a.order - b.order);
return {
executions,
events: recentEvents(
this.directory,
this.index,
new Set(executions.map((record) => record.id)),
Math.max(0, maxRecentBytes - recordBytes),
),
};
}
close(): void {
fs.fsyncSync(this.fd);
fs.closeSync(this.fd);
saveJournalIndex(this.directory, this.index);
this.executions.save();
}
private segments(): string[] {
return segmentNames(this.directory);
}
private segmentFile(): string {
return path.join(this.directory, `events-${String(this.segment).padStart(6, '0')}.jsonl`);
}
private remember(event: SessionEvent, bytes: number): void {
this.events.push(event);
this.eventSizes.push(bytes);
this.memoryBytes += bytes;
this.trimMemory();
}
private trimMemory(): void {
while (this.events.length > 2000 || this.memoryBytes > maxMemoryBytes) {
this.events.shift();
this.memoryBytes -= this.eventSizes.shift()!;
}
}
private rotate(): void {
fs.fsyncSync(this.fd);
fs.closeSync(this.fd);
this.segment++;
this.fd = fs.openSync(this.segmentFile(), 'a', 0o600);
indexSegment(this.index, path.basename(this.segmentFile()), fs.fstatSync(this.fd));
this.size = 0;
const segments = this.segments();
let total = segments.reduce(
(sum, name) => sum + fs.statSync(path.join(this.directory, name)).size,
0,
);
while (total > this.maxBytes && segments.length > 1) {
const file = path.join(this.directory, segments.shift()!);
total -= fs.statSync(file).size;
fs.unlinkSync(file);
}
const retained = new Set(segments);
this.index.segments = this.index.segments.filter((segment) => retained.has(segment.name));
this.earliestSeq =
this.index.segments.find((segment) => segment.first)?.first ?? this.seq + 1;
saveJournalIndex(this.directory, this.index);
}
}