my-durable 0.0.61 → 0.0.62
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/apps/dpi-backend/scripts/bench-store.mjs +284 -0
- package/apps/dpi-backend/src/archived.ts +1 -1
- package/apps/dpi-backend/src/gc.ts +27 -6
- package/apps/dpi-backend/src/routes.ts +24 -1
- package/apps/dpi-backend/src/schedules.ts +65 -11
- package/apps/dpi-backend/src/store.ts +191 -39
- package/apps/dpi-backend/src/tunnel.ts +45 -13
- package/apps/dpi-backend/src/zip.ts +376 -10
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/assets/{index-4Kyozhl0.js → index-D10XyhhO.js} +23 -23
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/assets/{index-BkSQ2clA.css → index-DCz-aq0b.css} +1 -1
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/index.html +2 -2
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/export-bundle.ts +10 -13
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/Inspector.vue +1 -1
- package/apps/dpi-playground/apps/dpi-playground-hub/src/proxy.ts +19 -5
- package/apps/dpi-playground/apps/dpi-playground-hub/src/transport.ts +25 -4
- package/apps/dpi-playground/apps/dpi-playground-hub/src/tunnel-transport.ts +23 -1
- package/apps/dpi-sdk/src/client.ts +73 -6
- package/apps/dpi-sdk/src/protocol/tunnel.ts +4 -0
- package/package.json +1 -1
|
@@ -8,8 +8,27 @@
|
|
|
8
8
|
*
|
|
9
9
|
* Entries are deflated (`zlib.deflateRawSync`, method 8) or stored (method 0)
|
|
10
10
|
* when deflating makes them bigger (already-compressed or tiny payloads).
|
|
11
|
+
*
|
|
12
|
+
* R8 adds the streaming side: the session-export route no longer buffers a
|
|
13
|
+
* whole bundle (raw texts + deflate + base64 ≈ 2-3× transcript size, the R5
|
|
14
|
+
* audit's scale cliff) — `streamSessionBundle` composes the same entries from
|
|
15
|
+
* the store's public APIs, deflates each one through a pipe while writing a
|
|
16
|
+
* temp file under `os.tmpdir()`, and the route pipes that file to the HTTP
|
|
17
|
+
* response before unlinking it. Entries keep today's paths/payloads; the only
|
|
18
|
+
* layout difference is the streaming form's data descriptor (spec bit 3) so
|
|
19
|
+
* entry sizes can be written after the deflated body without buffering it.
|
|
11
20
|
*/
|
|
12
|
-
import {
|
|
21
|
+
import { randomUUID } from "node:crypto";
|
|
22
|
+
import { open, rm } from "node:fs/promises";
|
|
23
|
+
import { tmpdir, hostname, platform } from "node:os";
|
|
24
|
+
import { join } from "node:path";
|
|
25
|
+
import { Readable } from "node:stream";
|
|
26
|
+
import { pipeline } from "node:stream/promises";
|
|
27
|
+
import { createDeflateRaw, deflateRawSync } from "node:zlib";
|
|
28
|
+
import { invalid } from "./errors.js";
|
|
29
|
+
import { DURABLE_VERSION, type SessionRow, type Store } from "./store.js";
|
|
30
|
+
import { BACKGROUND_CONTEXT as ctx } from "@earendil-works/chord/context";
|
|
31
|
+
import type { ConversationId } from "@earendil-works/pi-durable";
|
|
13
32
|
|
|
14
33
|
export interface ZipEntry {
|
|
15
34
|
/** Forward-slash relative path inside the archive. */
|
|
@@ -19,15 +38,53 @@ export interface ZipEntry {
|
|
|
19
38
|
}
|
|
20
39
|
|
|
21
40
|
/**
|
|
22
|
-
* R7 export memory guard
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
* refusal names the size and tells the operator to compact first.
|
|
41
|
+
* R7 export memory guard — the LEGACY in-memory path only (`Store.exportSession`,
|
|
42
|
+
* the base64 JSON surface). R8's streamed route (`streamSessionBundle`) never
|
|
43
|
+
* holds the whole bundle, so its sanity ceiling is `EXPORT_TOTAL_LIMIT_BYTES`.
|
|
44
|
+
* Kept for the untouched legacy callers: the same refusal style — an `invalid`
|
|
45
|
+
* fault that names the size and tells the operator to compact first.
|
|
28
46
|
*/
|
|
29
47
|
export const EXPORT_RAW_LIMIT_BYTES = 32 * 1024 * 1024;
|
|
30
48
|
|
|
49
|
+
/**
|
|
50
|
+
* R8 streaming sanity ceiling: total UNCOMPRESSED bytes an export may stream
|
|
51
|
+
* (before deflate). Streaming lifts the R7 32 MiB in-memory cliff, but an
|
|
52
|
+
* unbounded export could still fill the disk / take a device down — so an
|
|
53
|
+
* absurd total (a transcript nobody has compacted in weeks) still refuses with
|
|
54
|
+
* the same invalid-fault style, size named, compaction suggested.
|
|
55
|
+
*/
|
|
56
|
+
export const EXPORT_TOTAL_LIMIT_BYTES = 256 * 1024 * 1024;
|
|
57
|
+
|
|
58
|
+
/** Biggest value a classic (zip64-less) 32-bit zip field can hold. */
|
|
59
|
+
export const ZIP_U32_MAX = 0xffffffff;
|
|
60
|
+
/** Biggest file count the 16-bit EOCD field can hold. */
|
|
61
|
+
export const ZIP_MAX_ENTRIES = 0xffff;
|
|
62
|
+
|
|
63
|
+
const mb = (n: number) => `${(n / (1024 * 1024)).toFixed(1)} MiB`;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The zip64-less refusal reason for a would-be archive, or undefined when every
|
|
67
|
+
* quantity still fits the classic format's fields. All four are checked because
|
|
68
|
+
* the classic zip caps EACH of them: per-entry sizes, total archive size (the
|
|
69
|
+
* local-header offset field), and the 16-bit entry count.
|
|
70
|
+
*/
|
|
71
|
+
export function zip64Problem(parts: {
|
|
72
|
+
raw?: number;
|
|
73
|
+
compressed?: number;
|
|
74
|
+
archive?: number;
|
|
75
|
+
entries?: number;
|
|
76
|
+
}): string | undefined {
|
|
77
|
+
if ((parts.raw ?? 0) > ZIP_U32_MAX)
|
|
78
|
+
return `an entry is ${mb(parts.raw ?? 0)} uncompressed, over the 4 GiB zip limit`;
|
|
79
|
+
if ((parts.compressed ?? 0) > ZIP_U32_MAX)
|
|
80
|
+
return `an entry is ${mb(parts.compressed ?? 0)} deflated, over the 4 GiB zip limit`;
|
|
81
|
+
if ((parts.archive ?? 0) > ZIP_U32_MAX)
|
|
82
|
+
return `the archive is ${mb(parts.archive ?? 0)}, over the 4 GiB zip limit`;
|
|
83
|
+
if ((parts.entries ?? 0) > ZIP_MAX_ENTRIES)
|
|
84
|
+
return `the archive holds ${(parts.entries ?? 0).toLocaleString()} files, over the ${ZIP_MAX_ENTRIES} zip limit`;
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
|
|
31
88
|
/** Total UTF-8 bytes an archive would hold — the "raw" side of the guard (pre-deflate). */
|
|
32
89
|
export function zipRawBytes(entries: readonly ZipEntry[]): number {
|
|
33
90
|
let bytes = 0;
|
|
@@ -52,10 +109,27 @@ const CRC_TABLE = (() => {
|
|
|
52
109
|
return table;
|
|
53
110
|
})();
|
|
54
111
|
|
|
112
|
+
/** Incremental CRC-32 (IEEE) fed chunk by chunk — the streaming writer's checksum. */
|
|
113
|
+
export class Crc32 {
|
|
114
|
+
#crc = 0xffffffff;
|
|
115
|
+
|
|
116
|
+
update(chunk: Uint8Array): this {
|
|
117
|
+
let crc = this.#crc;
|
|
118
|
+
for (let i = 0; i < chunk.length; i += 1) crc = CRC_TABLE[(crc ^ chunk[i]) & 0xff] ^ (crc >>> 8);
|
|
119
|
+
this.#crc = crc;
|
|
120
|
+
return this;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** The finalized checksum, exactly what the one-shot `crc32` returns. */
|
|
124
|
+
get(): number {
|
|
125
|
+
return (this.#crc ^ 0xffffffff) >>> 0;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
55
129
|
export function crc32(data: Uint8Array): number {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
return (
|
|
130
|
+
const crc = new Crc32();
|
|
131
|
+
crc.update(data);
|
|
132
|
+
return crc.get();
|
|
59
133
|
}
|
|
60
134
|
|
|
61
135
|
/** MS-DOS time/date: 2-second resolution, 1980 epoch. */
|
|
@@ -132,3 +206,295 @@ export function zipSync(entries: readonly ZipEntry[], at: Date = new Date()): Bu
|
|
|
132
206
|
|
|
133
207
|
return Buffer.concat([...locals, centralBuf, eocd]);
|
|
134
208
|
}
|
|
209
|
+
|
|
210
|
+
// ------------------------------------------------------------ R8 streamed export
|
|
211
|
+
|
|
212
|
+
/** Deflate + CRC move in slices, not per byte; 256 KiB keeps syscall count sane. */
|
|
213
|
+
const CHUNK_BYTES = 1 << 18;
|
|
214
|
+
/** Spec bit 3: sizes/crc follow the deflated body in a data descriptor. */
|
|
215
|
+
const FLAG_DESCRIPTOR = 0x0008;
|
|
216
|
+
/** Data descriptor records MAY carry the local-header signature; readers that
|
|
217
|
+
* scan forward prefer it, and python's zipfile writes it the same way. */
|
|
218
|
+
const DATA_DESCRIPTOR_SIG = 0x08074b50;
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* A temp file under the OS temp dir (never a hardcoded path — Windows-safe),
|
|
222
|
+
* written 0600 like every other transcript-bearing file on the device.
|
|
223
|
+
*/
|
|
224
|
+
const tempZipPath = () => join(tmpdir(), `dpi-export-${process.pid}-${randomUUID()}.zip`);
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* The streaming half of the zip writer. Entries are deflated through a pipe and
|
|
228
|
+
* written to disk as the deflate stream emits, so RAM holds one entry at a time
|
|
229
|
+
* instead of the whole archive. Classic-layout difference: because sizes are
|
|
230
|
+
* only known after an entry's body has streamed, local headers carry spec bit 3
|
|
231
|
+
* (data descriptor) and the real sizes/crc land in the descriptor + central
|
|
232
|
+
* directory — the form python's `zipfile`, Java's `ZipOutputStream` and every
|
|
233
|
+
* mainstream reader accepts. Central-directory and EOCD records are identical
|
|
234
|
+
* to the one-shot writer's.
|
|
235
|
+
*/
|
|
236
|
+
class ZipStreamWriter {
|
|
237
|
+
readonly path: string;
|
|
238
|
+
/** Total UNCOMPRESSED bytes committed so far — the ceiling's running total. */
|
|
239
|
+
rawTotal = 0;
|
|
240
|
+
bytes = 0;
|
|
241
|
+
private handle: Awaited<ReturnType<typeof open>>;
|
|
242
|
+
private stamp: { time: number; date: number };
|
|
243
|
+
private offset = 0;
|
|
244
|
+
count = 0;
|
|
245
|
+
private central: Buffer[] = [];
|
|
246
|
+
private centralBytes = 0;
|
|
247
|
+
|
|
248
|
+
private constructor(path: string, handle: Awaited<ReturnType<typeof open>>) {
|
|
249
|
+
this.path = path;
|
|
250
|
+
this.handle = handle;
|
|
251
|
+
this.stamp = dosStamp(new Date());
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** Create the temp file exclusively, so two exports never share one archive. */
|
|
255
|
+
static async create(): Promise<ZipStreamWriter> {
|
|
256
|
+
const path = tempZipPath();
|
|
257
|
+
const handle = await open(path, "wx", 0o600);
|
|
258
|
+
return new ZipStreamWriter(path, handle);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
async add(entry: ZipEntry, ceilingBytes: number): Promise<void> {
|
|
262
|
+
const name = Buffer.from(entry.path.replace(/^\/+/, ""), "utf8");
|
|
263
|
+
if (!name.length) throw invalid("zip entry name is empty");
|
|
264
|
+
if (name.length > 0xffff) throw invalid(`zip entry name is over the 64 KiB zip limit: ${entry.path.slice(0, 100)}`);
|
|
265
|
+
const raw = Buffer.from(entry.text, "utf8");
|
|
266
|
+
// The ceiling is checked as raw bytes accumulate, BEFORE this entry's copy
|
|
267
|
+
// is even built for deflate — a refusal never pays for the oversized entry.
|
|
268
|
+
this.rawTotal += raw.length;
|
|
269
|
+
if (this.rawTotal > ceilingBytes)
|
|
270
|
+
throw invalid(
|
|
271
|
+
`Export refused: transcripts are ${mb(this.rawTotal)} of text, over the ${mb(ceilingBytes)} streaming ceiling. ` +
|
|
272
|
+
`Compact the session first, then export again`,
|
|
273
|
+
);
|
|
274
|
+
const zip64 = zip64Problem({ raw: raw.length, archive: this.offset + this.centralBytes + 22 });
|
|
275
|
+
if (zip64) throw invalid(`Export refused: ${zip64}`);
|
|
276
|
+
if (this.count >= ZIP_MAX_ENTRIES) throw invalid(`Export refused: ${zip64Problem({ entries: this.count + 1 })}`);
|
|
277
|
+
|
|
278
|
+
// Deflate through a pipe: the compressed bytes hit the disk as they emit,
|
|
279
|
+
// with backpressure, instead of accumulating one body-sized Buffer.
|
|
280
|
+
const crc = new Crc32();
|
|
281
|
+
let compressed = 0;
|
|
282
|
+
// Descriptor form: the local header goes down FIRST with placeholder sizes
|
|
283
|
+
// (spec bit 3), the deflated body streams after it, and the real crc/sizes
|
|
284
|
+
// land in the trailing data descriptor + central directory.
|
|
285
|
+
const local = Buffer.alloc(30);
|
|
286
|
+
local.writeUInt32LE(LOCAL_SIG, 0);
|
|
287
|
+
local.writeUInt16LE(VERSION, 4);
|
|
288
|
+
local.writeUInt16LE(FLAG_UTF8 | FLAG_DESCRIPTOR, 6);
|
|
289
|
+
local.writeUInt16LE(8, 8); // method 8: deflate (the streamed path always deflates)
|
|
290
|
+
local.writeUInt16LE(this.stamp.time, 10);
|
|
291
|
+
local.writeUInt16LE(this.stamp.date, 12);
|
|
292
|
+
local.writeUInt32LE(0, 14); // crc: in the descriptor below
|
|
293
|
+
local.writeUInt32LE(0, 18); // compressed: in the descriptor below
|
|
294
|
+
local.writeUInt32LE(0, 22); // uncompressed: in the descriptor below
|
|
295
|
+
local.writeUInt16LE(name.length, 26);
|
|
296
|
+
local.writeUInt16LE(0, 28);
|
|
297
|
+
await this.handle.write(local);
|
|
298
|
+
await this.handle.write(name);
|
|
299
|
+
// The body streams through deflate in chunks: pipeline wires source →
|
|
300
|
+
// deflate with proper error forwarding/backpressure, the final async sink
|
|
301
|
+
// writes each emitted chunk straight to the temp file.
|
|
302
|
+
await pipeline(
|
|
303
|
+
Readable.from(ZipStreamWriter.slices(raw, crc)),
|
|
304
|
+
createDeflateRaw(),
|
|
305
|
+
async (chunks: AsyncIterable<Buffer>) => {
|
|
306
|
+
for await (const chunk of chunks) {
|
|
307
|
+
compressed += chunk.length;
|
|
308
|
+
await this.handle.write(chunk);
|
|
309
|
+
}
|
|
310
|
+
},
|
|
311
|
+
);
|
|
312
|
+
if (zip64Problem({ compressed })) throw invalid(`Export refused: ${zip64Problem({ compressed })}`);
|
|
313
|
+
|
|
314
|
+
const descriptor = Buffer.alloc(16);
|
|
315
|
+
descriptor.writeUInt32LE(DATA_DESCRIPTOR_SIG, 0);
|
|
316
|
+
descriptor.writeUInt32LE(crc.get(), 4);
|
|
317
|
+
descriptor.writeUInt32LE(compressed, 8);
|
|
318
|
+
descriptor.writeUInt32LE(raw.length, 12);
|
|
319
|
+
await this.handle.write(descriptor);
|
|
320
|
+
|
|
321
|
+
// Central directory carries the real sizes/crc like the one-shot writer's.
|
|
322
|
+
const central = Buffer.alloc(46);
|
|
323
|
+
central.writeUInt32LE(CENTRAL_SIG, 0);
|
|
324
|
+
central.writeUInt16LE(VERSION, 4);
|
|
325
|
+
central.writeUInt16LE(VERSION, 6);
|
|
326
|
+
central.writeUInt16LE(FLAG_UTF8 | FLAG_DESCRIPTOR, 8);
|
|
327
|
+
central.writeUInt16LE(8, 10);
|
|
328
|
+
central.writeUInt16LE(this.stamp.time, 12);
|
|
329
|
+
central.writeUInt16LE(this.stamp.date, 14);
|
|
330
|
+
central.writeUInt32LE(crc.get(), 16);
|
|
331
|
+
central.writeUInt32LE(compressed, 20);
|
|
332
|
+
central.writeUInt32LE(raw.length, 24);
|
|
333
|
+
central.writeUInt16LE(name.length, 28);
|
|
334
|
+
central.writeUInt16LE(0, 30); // extra
|
|
335
|
+
central.writeUInt16LE(0, 32); // comment
|
|
336
|
+
central.writeUInt16LE(0, 34); // disk start
|
|
337
|
+
central.writeUInt16LE(0, 36); // internal attrs
|
|
338
|
+
central.writeUInt32LE(0o644 << 16, 38); // external attrs: rw-r--r--
|
|
339
|
+
central.writeUInt32LE(this.offset, 42);
|
|
340
|
+
this.central.push(central, name);
|
|
341
|
+
this.centralBytes += central.length + name.length;
|
|
342
|
+
|
|
343
|
+
this.offset += local.length + name.length + compressed + descriptor.length;
|
|
344
|
+
this.count += 1;
|
|
345
|
+
this.bytes = this.offset + this.centralBytes + 22;
|
|
346
|
+
if (zip64Problem({ archive: this.bytes })) throw invalid(`Export refused: ${zip64Problem({ archive: this.bytes })}`);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/** Central directory + EOCD; the handle closes and the archive is complete. */
|
|
350
|
+
async finish(): Promise<number> {
|
|
351
|
+
const centralBuf = Buffer.concat(this.central);
|
|
352
|
+
const total = this.offset + centralBuf.length + 22;
|
|
353
|
+
if (zip64Problem({ archive: total, entries: this.count }))
|
|
354
|
+
throw invalid(`Export refused: ${zip64Problem({ archive: total, entries: this.count })}`);
|
|
355
|
+
const eocd = Buffer.alloc(22);
|
|
356
|
+
eocd.writeUInt32LE(EOCD_SIG, 0);
|
|
357
|
+
eocd.writeUInt16LE(0, 4); // this disk
|
|
358
|
+
eocd.writeUInt16LE(0, 6); // disk with central directory
|
|
359
|
+
eocd.writeUInt16LE(this.count, 8);
|
|
360
|
+
eocd.writeUInt16LE(this.count, 10);
|
|
361
|
+
eocd.writeUInt32LE(centralBuf.length, 12);
|
|
362
|
+
eocd.writeUInt32LE(this.offset, 16);
|
|
363
|
+
eocd.writeUInt16LE(0, 20); // comment length
|
|
364
|
+
await this.handle.write(centralBuf);
|
|
365
|
+
await this.handle.write(eocd);
|
|
366
|
+
await this.handle.close();
|
|
367
|
+
this.handle = undefined as unknown as Awaited<ReturnType<typeof open>>;
|
|
368
|
+
return total;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/** Close (if still open) and unlink — every failure path leaves no temp file. */
|
|
372
|
+
async dispose(): Promise<void> {
|
|
373
|
+
if (this.handle) {
|
|
374
|
+
try {
|
|
375
|
+
await this.handle.close();
|
|
376
|
+
} catch {
|
|
377
|
+
/* already closed */
|
|
378
|
+
}
|
|
379
|
+
this.handle = undefined as unknown as Awaited<ReturnType<typeof open>>;
|
|
380
|
+
}
|
|
381
|
+
await rm(this.path, { force: true });
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/** Yield the buffer in CHUNK_BYTES slices, checksumming each before it flows. */
|
|
385
|
+
private static async *slices(raw: Buffer, crc: Crc32): AsyncGenerator<Buffer> {
|
|
386
|
+
for (let at = 0; at < raw.length; at += CHUNK_BYTES) {
|
|
387
|
+
const chunk = raw.subarray(at, Math.min(at + CHUNK_BYTES, raw.length));
|
|
388
|
+
crc.update(chunk);
|
|
389
|
+
yield chunk;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* A session bundle streamed to a temp file: same entries as the legacy
|
|
396
|
+
* `Store.exportSession`, minus the whole-archive-in-RAM step. The caller pipes
|
|
397
|
+
* `path` to its sink and then MUST call `cleanup()` — every refusal/failure
|
|
398
|
+
* cleans up by itself, only a completed archive is handed over.
|
|
399
|
+
*/
|
|
400
|
+
export interface StreamedBundle {
|
|
401
|
+
/** Absolute temp-file path; read it, pipe it, then `cleanup()`. */
|
|
402
|
+
path: string;
|
|
403
|
+
/** `${slugName(name|id)}.bundle.zip` — the download name, unchanged from R7. */
|
|
404
|
+
filename: string;
|
|
405
|
+
mime: string;
|
|
406
|
+
/** Zip byte length (the temp file's size) — the Content-Length for the route. */
|
|
407
|
+
bytes: number;
|
|
408
|
+
entries: number;
|
|
409
|
+
/** Unlink the temp file. Safe to call once; the route calls it in `finally`. */
|
|
410
|
+
cleanup: () => Promise<void>;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
export async function streamSessionBundle(
|
|
414
|
+
store: Store,
|
|
415
|
+
row: SessionRow,
|
|
416
|
+
opts: { ceilingBytes?: number } = {},
|
|
417
|
+
): Promise<StreamedBundle> {
|
|
418
|
+
const ceilingBytes = opts.ceilingBytes ?? EXPORT_TOTAL_LIMIT_BYTES;
|
|
419
|
+
const writer = await ZipStreamWriter.create();
|
|
420
|
+
try {
|
|
421
|
+
for await (const entry of sessionBundleEntries(store, row)) await writer.add(entry, ceilingBytes);
|
|
422
|
+
const bytes = await writer.finish();
|
|
423
|
+
return {
|
|
424
|
+
path: writer.path,
|
|
425
|
+
filename: `${slugName(row.name || row.id)}.bundle.zip`,
|
|
426
|
+
mime: "application/zip",
|
|
427
|
+
bytes,
|
|
428
|
+
entries: writer.count,
|
|
429
|
+
cleanup: () => writer.dispose(),
|
|
430
|
+
};
|
|
431
|
+
} catch (cause) {
|
|
432
|
+
await writer.dispose();
|
|
433
|
+
throw cause;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* The bundle composition, entry by entry, mirroring `Store.exportSession`'s
|
|
439
|
+
* layout exactly (manifest.json, session.json, per-agent transcript/config,
|
|
440
|
+
* README when the session's store is closed). It pulls only public store APIs
|
|
441
|
+
* and yields one entry at a time so the writer never holds more than the
|
|
442
|
+
* current transcript in memory. The three mirrors below (`NOT_OPENED_README`,
|
|
443
|
+
* `slugName`, the manifest shape) are byte-for-byte copies of store.ts's
|
|
444
|
+
* module-private originals — keep them in sync if export's layout ever moves.
|
|
445
|
+
*/
|
|
446
|
+
const NOT_OPENED_README = [
|
|
447
|
+
"This bundle was exported while the session's store was closed.",
|
|
448
|
+
"",
|
|
449
|
+
"It contains manifest.json and session.json only. Start the device and re-export",
|
|
450
|
+
"from the running session to include per-agent transcripts and configs.",
|
|
451
|
+
"",
|
|
452
|
+
].join("\n");
|
|
453
|
+
|
|
454
|
+
/** "My session #1" → "my-session-1" for the download name (mirrors store.ts). */
|
|
455
|
+
function slugName(input: string): string {
|
|
456
|
+
const slug = input
|
|
457
|
+
.normalize("NFKD")
|
|
458
|
+
.replace(/[^\w.-]+/g, "-")
|
|
459
|
+
.replace(/^-+|-+$/g, "")
|
|
460
|
+
.slice(0, 60)
|
|
461
|
+
.toLowerCase();
|
|
462
|
+
return slug || "session";
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
async function* sessionBundleEntries(store: Store, row: SessionRow): AsyncGenerator<ZipEntry> {
|
|
466
|
+
const opened = store.opened.get(row.id);
|
|
467
|
+
const agents = opened ? await store.agents(row) : [];
|
|
468
|
+
const manifest = {
|
|
469
|
+
format: "dpi-session-bundle",
|
|
470
|
+
version: 1,
|
|
471
|
+
exportedAt: new Date().toISOString(),
|
|
472
|
+
device: {
|
|
473
|
+
uid: store.uid,
|
|
474
|
+
name: store.name,
|
|
475
|
+
hostname: hostname(),
|
|
476
|
+
platform: `${platform()} node ${process.version}`,
|
|
477
|
+
durableVersion: DURABLE_VERSION,
|
|
478
|
+
},
|
|
479
|
+
session: { id: row.id, name: row.name, cwd: row.cwd, model: row.model, createdAt: row.createdAt },
|
|
480
|
+
agents: agents.map((a) => ({ id: a.id, name: a.name, kind: a.kind })),
|
|
481
|
+
};
|
|
482
|
+
yield { path: "manifest.json", text: `${JSON.stringify(manifest, null, 2)}\n` };
|
|
483
|
+
yield { path: "session.json", text: `${JSON.stringify(store.sessionSummaryBase(row), null, 2)}\n` };
|
|
484
|
+
for (const agent of agents) {
|
|
485
|
+
const c = opened ? await opened.harness.conversation(agent.id as ConversationId, ctx) : undefined;
|
|
486
|
+
if (!c) continue;
|
|
487
|
+
const dir = `agents/${agent.id}`;
|
|
488
|
+
yield {
|
|
489
|
+
path: `${dir}/transcript.json`,
|
|
490
|
+
text: `${JSON.stringify(await store.transcript(c, "json"), null, 2)}\n`,
|
|
491
|
+
};
|
|
492
|
+
const md = await store.transcript(c, "markdown");
|
|
493
|
+
if (typeof md.text === "string") yield { path: `${dir}/transcript.md`, text: md.text };
|
|
494
|
+
yield {
|
|
495
|
+
path: `${dir}/config.json`,
|
|
496
|
+
text: `${JSON.stringify((await store.agentDetail(row, c)).config, null, 2)}\n`,
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
if (!agents.length) yield { path: "README.txt", text: NOT_OPENED_README };
|
|
500
|
+
}
|