@thready-web/dsh-bot-plugin 0.1.0-beta.2 → 0.1.0-beta.4

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/dist/index.js CHANGED
@@ -5,16 +5,29 @@ import { createEngine } from "./engine.js";
5
5
  import { renderPeers, renderSession, renderTickets } from "./context.js";
6
6
  import { toolDefinitions } from "./tools.js";
7
7
  import { createTriggers } from "./triggers.js";
8
- import { followupDurable, liveAgent, logHasMessage, removePending, sessionEvents } from "./inbox.js";
8
+ import { followupDurable, liveAgent, logHasMessage, removePending } from "./inbox.js";
9
+ import { createLogSource } from "./log/source.js";
9
10
  import { toolError } from "./errors.js";
11
+ import { createQuestionAnswerer, publishQuestionAnswerer, questionAnswererFor } from "./questions.js";
12
+ import { createApprovalAnswerer, publishApprovalAnswerer, approvalAnswererFor } from "./approvals.js";
13
+ import { createAuthorizationController, publishAuthorization, authorizationFor } from "./authorization.js";
10
14
  import { publishRuntime, publishWorkRuntime, runtimeFor } from "./runtime-registry.js";
11
- import { childCreateOptions } from "./work-admit.js";
15
+ import { childCreateOptions, effectiveWorkLimits, resolveWorkBudget, WORK_BUDGET_ENV, } from "./work-admit.js";
12
16
  import { ensureWorkerFilePreset, WORKER_FILE_PRESET, workerFileSetup } from "./work-child.js";
13
- import { createWorkRuntime } from "./work-worker.js";
17
+ import { BUDGET_NOTICE_PREFIX, createWorkRuntime } from "./work-worker.js";
14
18
  import { workToolDefinitions } from "./work-tools.js";
15
19
  export const name = 'session-foundations';
16
20
  // Cordis throws if a plugin reads a service it did not inject. Crash recovery reads the session log through this one.
17
- export const inject = ['systemPrompt', 'tools', 'sessions', 'agents', 'agentPresets', 'sessionPersistence'];
21
+ export const inject = [
22
+ 'systemPrompt',
23
+ 'tools',
24
+ 'sessions',
25
+ 'agents',
26
+ 'agentPresets',
27
+ 'sessionPersistence',
28
+ 'sessionQuery',
29
+ 'userQuestions',
30
+ ];
18
31
  /**
19
32
  * @param {import('./host.ts').PluginHost} ctx
20
33
  * @param {{ dataDir?: string, contextWindow?: number, traceEvents?: boolean, timerTickMs?: number }} [config]
@@ -197,6 +210,24 @@ export function apply(ctx, config = {}) {
197
210
  if (work) {
198
211
  for (const definition of workToolDefinitions(work, engine))
199
212
  ctx.tools.register(definition);
213
+ const gate = work;
214
+ // Main-session step budget: refuse the next step instead of cancelling a
215
+ // step that already ended. A refused step ends the turn as `blocked`.
216
+ ctx.on('agent/pre-step', async (payload, next) => {
217
+ const sessionId = payload?.agent?.id;
218
+ if (typeof sessionId !== 'string' || sessionId === '')
219
+ return next();
220
+ if (!(await gate.waitForBudget(sessionId, payload?.signal)))
221
+ return { kind: 'reject' };
222
+ const decision = gate.gateStep(sessionId, {
223
+ turn: payload?.turn,
224
+ step: payload?.step,
225
+ claimed: Array.isArray(payload?.messages) ? payload.messages.length : 0,
226
+ });
227
+ if (decision.kind === 'reject')
228
+ return Promise.resolve({ kind: 'reject' });
229
+ return next();
230
+ });
200
231
  }
201
232
  if (triggers || work) {
202
233
  const timer = setInterval(() => {
@@ -208,6 +239,27 @@ export function apply(ctx, config = {}) {
208
239
  triggers?.tick().catch(() => { });
209
240
  work?.tick().catch(() => { });
210
241
  }
242
+ const questions = createQuestionAnswerer();
243
+ publishQuestionAnswerer(store.root, questions);
244
+ ctx.effect(() => questions.listen(ctx));
245
+ ctx.effect(() => () => {
246
+ if (questionAnswererFor(store.root) === questions)
247
+ publishQuestionAnswerer(store.root, null);
248
+ });
249
+ const approvals = createApprovalAnswerer();
250
+ publishApprovalAnswerer(store.root, approvals);
251
+ ctx.effect(() => approvals.listen(ctx));
252
+ ctx.effect(() => () => {
253
+ if (approvalAnswererFor(store.root) === approvals)
254
+ publishApprovalAnswerer(store.root, null);
255
+ });
256
+ const authorization = createAuthorizationController();
257
+ publishAuthorization(store.root, authorization);
258
+ ctx.effect(() => authorization.bind(ctx));
259
+ ctx.effect(() => () => {
260
+ if (authorizationFor(store.root) === authorization)
261
+ publishAuthorization(store.root, null);
262
+ });
211
263
  ctx.logger?.info?.('session-foundations: ready dir=%s bindings=%s promoted=%s', dataDir, recovery.bindings, recovery.promoted.length);
212
264
  }
213
265
  /**
@@ -257,8 +309,36 @@ function startWork(ctx, store, engine, config) {
257
309
  /** @type {Map<string, { dispose?: () => Promise<void> }>} */
258
310
  const workerHandles = new Map();
259
311
  const filePreset = ensureWorkerFilePreset(ctx);
312
+ const resolved = resolveWorkBudget(config.work ?? {}, process.env[WORK_BUDGET_ENV]);
313
+ if (resolved.ignored.length > 0) {
314
+ ctx.logger?.warn?.('session-foundations: ignored work budget keys %s', resolved.ignored.join(','));
315
+ }
316
+ if (resolved.clamped.length > 0) {
317
+ ctx.logger?.warn?.('session-foundations: clamped work budget keys %s', resolved.clamped.join(','));
318
+ }
319
+ const slots = effectiveWorkLimits(resolved.budget);
320
+ ctx.logger?.info?.('session-foundations: main budget chat=%s steps/%sms main=%s steps/%sms slots perBot=%s global=%s queue perBot=%s global=%s', resolved.budget.chatMaxSteps, resolved.budget.chatWallMs, resolved.budget.mainMaxSteps, resolved.budget.mainWallMs, slots.perBot, slots.global, slots.perBotQueue, slots.globalQueue);
321
+ if (resolved.budget.perBot > resolved.budget.global || resolved.budget.perBotQueue > resolved.budget.globalQueue) {
322
+ ctx.logger?.info?.('session-foundations: per-bot cap reduced to the global cap (perBot=%s global=%s perBotQueue=%s globalQueue=%s)', resolved.budget.perBot, resolved.budget.global, resolved.budget.perBotQueue, resolved.budget.globalQueue);
323
+ }
260
324
  const work = createWorkRuntime(store, engine, {
261
- budget: config.work ?? {},
325
+ resumePaused: true,
326
+ budget: resolved.budget,
327
+ log: (level, message) => {
328
+ const sink = level === 'warn' ? (ctx.logger?.warn ?? ctx.logger?.info) : ctx.logger?.info;
329
+ sink?.call(ctx.logger, message);
330
+ // DSH 0.2.2-rc.1 does not print plugin ctx.logger output by default, so a
331
+ // budget trip also goes to stderr (journald under systemd). No secrets.
332
+ if (level === 'warn')
333
+ console.warn(message);
334
+ },
335
+ onMainBudget: (sessionId, trip) => {
336
+ if (config.budgetNotice === false)
337
+ return;
338
+ postBudgetNotice(ctx, sessionId, trip).catch(error => {
339
+ ctx.logger?.warn?.('session-foundations: budget notice failed session=%s %s', sessionId, String(error));
340
+ });
341
+ },
262
342
  createChild: async (spec) => {
263
343
  let cwd = '';
264
344
  let model = null;
@@ -306,16 +386,19 @@ function startWork(ctx, store, engine, config) {
306
386
  const agent = liveAgent(ctx, sessionId);
307
387
  if (!agent)
308
388
  return;
309
- agent.cancel({ kind: 'user' }, { keepInbox: options.keepInbox === true });
389
+ // Never report a host cancel as a user cancel: the turn records
390
+ // { kind: 'aborted', reason: { kind: 'hook', reason } }.
391
+ agent.cancel({ kind: 'hook', reason: options.reason || 'session-foundations' }, { keepInbox: options.keepInbox === true });
310
392
  },
311
393
  snapshot: async (sessionId) => {
312
394
  try {
313
- return await sessionEvents(ctx, sessionId);
395
+ return (await createLogSource(ctx).read(sessionId, -1)).events;
314
396
  }
315
397
  catch {
316
398
  return [];
317
399
  }
318
400
  },
401
+ childPresent: sessionId => liveAgent(ctx, sessionId) != null,
319
402
  });
320
403
  publishWorkRuntime(store.root, work);
321
404
  ctx.effect(() => () => {
@@ -332,6 +415,40 @@ function startWork(ctx, store, engine, config) {
332
415
  });
333
416
  return work;
334
417
  }
418
+ /**
419
+ * Text of the inbox notice that follows a main-session budget trip.
420
+ * @param {MainBudgetTrip} trip
421
+ */
422
+ export function budgetNoticeText(trip) {
423
+ const seconds = Math.round(trip.elapsedMs / 1000);
424
+ const what = trip.reason === 'steps'
425
+ ? `it reached the step limit (${trip.steps}/${trip.maxSteps} steps)`
426
+ : `it reached the time limit (${seconds}s of ${Math.round(trip.wallMs / 1000)}s)`;
427
+ return [
428
+ '[foundations:budget]',
429
+ `The previous turn (turn ${trip.turn}) was stopped by the bot host because ${what}. The user did not cancel it.`,
430
+ `cause: ${trip.cause}`,
431
+ 'In one short message, tell the user the turn was cut off by the budget and what is already done.',
432
+ 'If work remains, call foundations_dispatch with a briefing for the remaining work so a worker continues it, and say so. Otherwise ask the user whether to continue.',
433
+ ].join('\n');
434
+ }
435
+ /**
436
+ * Wait until the stopped turn is idle, then post the notice as a next-turn
437
+ * message. Posting while the turn is still unwinding could leave it unclaimed.
438
+ * @param {import('./host.ts').HostContext} ctx
439
+ * @param {string} sessionId
440
+ * @param {MainBudgetTrip} trip
441
+ */
442
+ async function postBudgetNotice(ctx, sessionId, trip) {
443
+ const agent = liveAgent(ctx, sessionId);
444
+ if (!agent)
445
+ return;
446
+ await new Promise(resolve => setImmediate(resolve));
447
+ if (typeof agent.whenIdle === 'function')
448
+ await agent.whenIdle();
449
+ const messageId = `${BUDGET_NOTICE_PREFIX}${trip.turn}:${Date.now().toString(36)}`;
450
+ await followupDurable(ctx, sessionId, messageId, budgetNoticeText(trip));
451
+ }
335
452
  /**
336
453
  * @param {ReturnType<typeof createTriggers>} triggers
337
454
  * @param {import('./src/engine.js').Engine} engine
@@ -408,8 +525,8 @@ function triggerTool(name, description, parameters, run, isBot) {
408
525
  * @param {string} messageId
409
526
  */
410
527
  async function logHasDurable(ctx, sessionId, messageId) {
411
- const events = await sessionEvents(ctx, sessionId);
412
- return logHasMessage({ snapshotEvents: () => events }, messageId);
528
+ const { events } = await createLogSource(ctx).read(sessionId, -1);
529
+ return logHasMessage(events, messageId);
413
530
  }
414
531
  /**
415
532
  * @param {{ data?: { message?: { content?: Array<{ type?: string, text?: string }> }, content?: Array<{ type?: string, text?: string }> } }} event
@@ -1,4 +1,5 @@
1
1
  import { planChunks } from "./cdc.js";
2
+ import { stableStringify } from "./log/canonical.js";
2
3
  /**
3
4
  * Session history is one canonical JSON array of the host event log.
4
5
  * FastCDC is only for that blob, and only when a session is opened.
@@ -9,21 +10,7 @@ const PLAN_LIMIT = 8;
9
10
  * Sorted-key JSON, so the same log cuts into the same chunks.
10
11
  * Array order is the host order.
11
12
  */
12
- export function stableStringify(value) {
13
- if (value === undefined)
14
- return 'null';
15
- if (value instanceof Date)
16
- return JSON.stringify(value);
17
- if (value === null || typeof value !== 'object')
18
- return JSON.stringify(value) ?? 'null';
19
- if (Array.isArray(value))
20
- return `[${value.map(item => stableStringify(item)).join(',')}]`;
21
- const record = value;
22
- const keys = Object.keys(record)
23
- .filter(key => record[key] !== undefined)
24
- .sort();
25
- return `{${keys.map(key => `${JSON.stringify(key)}:${stableStringify(record[key])}`).join(',')}}`;
26
- }
13
+ export { stableStringify } from "./log/canonical.js";
27
14
  export function canonicalBytes(value) {
28
15
  return Buffer.from(stableStringify(value), 'utf8');
29
16
  }
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Sorted-key JSON used as the event-log canonical form.
3
+ * NDJSON is one event per line, so an append only changes the tail.
4
+ */
5
+ export function stableStringify(value) {
6
+ if (value === undefined)
7
+ return 'null';
8
+ if (value instanceof Date)
9
+ return JSON.stringify(value);
10
+ if (value === null || typeof value !== 'object')
11
+ return JSON.stringify(value) ?? 'null';
12
+ if (Array.isArray(value))
13
+ return `[${value.map(item => stableStringify(item)).join(',')}]`;
14
+ const record = value;
15
+ const keys = Object.keys(record)
16
+ .filter(key => record[key] !== undefined)
17
+ .sort();
18
+ return `{${keys.map(key => `${JSON.stringify(key)}:${stableStringify(record[key])}`).join(',')}}`;
19
+ }
20
+ export function canonicalBytes(value) {
21
+ return Buffer.from(stableStringify(value), 'utf8');
22
+ }
23
+ export function eventNdjson(events) {
24
+ const parts = [];
25
+ const lines = [];
26
+ let offset = 0;
27
+ for (const event of events) {
28
+ const seq = event && typeof event === 'object' && typeof event.seq === 'number'
29
+ ? event.seq
30
+ : lines.length;
31
+ const line = Buffer.from(`${stableStringify(event)}\n`, 'utf8');
32
+ lines.push({ seq, start: offset, end: offset + line.length });
33
+ parts.push(line);
34
+ offset += line.length;
35
+ }
36
+ return { bytes: parts.length ? Buffer.concat(parts) : Buffer.alloc(0), lines };
37
+ }
@@ -0,0 +1,62 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { chunkBuffer } from "../cdc.js";
3
+ import { eventNdjson } from "./canonical.js";
4
+ /** FastCDC cuts, snapped forward to the next newline so each chunk is a whole event range. */
5
+ export function alignCutsToLines(data, cuts) {
6
+ const n = data.length;
7
+ const out = [];
8
+ let offset = 0;
9
+ for (const cut of cuts) {
10
+ if (offset >= n)
11
+ break;
12
+ let end = Math.max(offset, cut.offset + cut.length);
13
+ if (end < n) {
14
+ while (end < n && data[end - 1] !== 10)
15
+ end += 1;
16
+ }
17
+ else {
18
+ end = n;
19
+ }
20
+ if (end > offset)
21
+ out.push({ offset, length: end - offset });
22
+ offset = end;
23
+ }
24
+ if (offset < n)
25
+ out.push({ offset, length: n - offset });
26
+ return out;
27
+ }
28
+ export function planEventChunks(events) {
29
+ const { bytes, lines } = eventNdjson(events);
30
+ if (bytes.length === 0 || lines.length === 0) {
31
+ return { throughSeq: -1, count: 0, totalSize: 0, chunks: [], epoch: '', format: 'ndjson-v1' };
32
+ }
33
+ const cuts = alignCutsToLines(bytes, chunkBuffer(bytes));
34
+ const chunks = [];
35
+ const hashes = [];
36
+ for (const cut of cuts) {
37
+ const body = bytes.subarray(cut.offset, cut.offset + cut.length);
38
+ const covered = lines.filter(line => line.start >= cut.offset && line.end <= cut.offset + cut.length);
39
+ if (covered.length === 0)
40
+ continue;
41
+ const hash = createHash('sha256').update(body).digest('hex');
42
+ hashes.push(hash);
43
+ chunks.push({
44
+ fromSeq: covered[0].seq,
45
+ toSeq: covered[covered.length - 1].seq,
46
+ bytes: cut.length,
47
+ hash,
48
+ });
49
+ }
50
+ const epoch = createHash('sha256').update(hashes.join('\n')).digest('hex');
51
+ return {
52
+ throughSeq: lines[lines.length - 1].seq,
53
+ count: lines.length,
54
+ totalSize: bytes.length,
55
+ chunks,
56
+ epoch,
57
+ format: 'ndjson-v1',
58
+ };
59
+ }
60
+ export function sha256Hex(data) {
61
+ return createHash('sha256').update(data).digest('hex');
62
+ }
@@ -0,0 +1,144 @@
1
+ import { HttpError, RANGE_PAGE_LIMIT } from "./snapshot-service.js";
2
+ export const LOG_ROUTE_PREFIX = '/api/dsh-bot/log';
3
+ export const LOG_MANIFEST_PATH = `${LOG_ROUTE_PREFIX}/manifest`;
4
+ export const LOG_CHUNK_PATH = `${LOG_ROUTE_PREFIX}/chunk`;
5
+ export const LOG_RANGE_PATH = `${LOG_ROUTE_PREFIX}/range`;
6
+ /**
7
+ * Optional `connection` must be read with `ctx.get`, never `ctx.connection`.
8
+ * Cordis throws `cannot get property "connection" without inject` on the
9
+ * property access when the HTTP plugin did not declare that service.
10
+ */
11
+ export function probeConnection(ctx) {
12
+ if (typeof ctx.get !== 'function')
13
+ return undefined;
14
+ return ctx.get('connection');
15
+ }
16
+ export function registerLogRoutes(ctx, service) {
17
+ const connection = probeConnection(ctx);
18
+ if (connection?.fetch?.register === undefined)
19
+ return false;
20
+ const handlers = logFetchHandlers(service);
21
+ for (const [path, handler] of handlers) {
22
+ ctx.effect?.(() => {
23
+ const dispose = connection.fetch.register({
24
+ path,
25
+ methods: ['GET', 'HEAD'],
26
+ requestBody: 'buffered',
27
+ fetch: handler,
28
+ });
29
+ return typeof dispose === 'function'
30
+ ? () => {
31
+ void dispose();
32
+ }
33
+ : undefined;
34
+ });
35
+ }
36
+ return true;
37
+ }
38
+ export function logFetchHandlers(service) {
39
+ return [
40
+ [LOG_MANIFEST_PATH, request => handleManifest(service, request)],
41
+ [LOG_CHUNK_PATH, request => handleChunk(service, request)],
42
+ [LOG_RANGE_PATH, request => handleRange(service, request)],
43
+ ];
44
+ }
45
+ export async function dispatchLogRequest(service, request) {
46
+ const path = new URL(request.url).pathname.replace(/\/+$/, '') || '/';
47
+ const handler = logFetchHandlers(service).find(([route]) => route === path)?.[1];
48
+ if (!handler)
49
+ return null;
50
+ return handler(request);
51
+ }
52
+ async function handleManifest(service, request) {
53
+ return wrap(request, async (url) => {
54
+ const sessionId = requiredSession(url);
55
+ const throughSeq = readInt(url, 'throughSeq', true);
56
+ const afterSeq = readInt(url, 'afterSeq', true);
57
+ const body = await service.manifest(sessionId, throughSeq ?? undefined, afterSeq ?? undefined);
58
+ return json(200, body);
59
+ });
60
+ }
61
+ async function handleChunk(service, request) {
62
+ return wrap(request, async (url) => {
63
+ const sessionId = requiredSession(url);
64
+ const fromSeq = readInt(url, 'fromSeq', false);
65
+ const toSeq = readInt(url, 'toSeq', false);
66
+ const hash = url.searchParams.get('hash') ?? '';
67
+ if (fromSeq == null || toSeq == null)
68
+ throw new HttpError(400, 'VALIDATION', 'fromSeq and toSeq are required.');
69
+ const bytes = await service.chunk(sessionId, fromSeq, toSeq, hash);
70
+ const headers = {
71
+ 'content-type': 'application/octet-stream',
72
+ 'content-length': String(bytes.length),
73
+ 'cache-control': 'private, immutable',
74
+ };
75
+ if (request.method === 'HEAD')
76
+ return new Response(null, { status: 200, headers });
77
+ const out = new Uint8Array(bytes.byteLength);
78
+ out.set(bytes);
79
+ return new Response(out, { status: 200, headers });
80
+ });
81
+ }
82
+ async function handleRange(service, request) {
83
+ if (request.method === 'HEAD') {
84
+ return new Response(null, {
85
+ status: 200,
86
+ headers: { 'cache-control': 'no-store', 'content-type': 'application/json; charset=utf-8' },
87
+ });
88
+ }
89
+ return wrap(request, async (url) => {
90
+ const sessionId = requiredSession(url);
91
+ const afterSeq = readInt(url, 'afterSeq', true) ?? -1;
92
+ const throughSeq = readInt(url, 'throughSeq', true);
93
+ const limit = readInt(url, 'limit', true) ?? RANGE_PAGE_LIMIT;
94
+ if (limit != null && limit <= 0)
95
+ throw new HttpError(400, 'VALIDATION', 'limit must be a positive integer.');
96
+ const body = await service.range(sessionId, afterSeq, throughSeq ?? undefined, limit ?? RANGE_PAGE_LIMIT);
97
+ return json(200, body);
98
+ });
99
+ }
100
+ async function wrap(request, run) {
101
+ try {
102
+ const response = await run(new URL(request.url));
103
+ if (request.method === 'HEAD')
104
+ return new Response(null, { status: response.status, headers: response.headers });
105
+ return response;
106
+ }
107
+ catch (error) {
108
+ if (error instanceof HttpError)
109
+ return json(error.status, { ok: false, code: error.code, message: error.message });
110
+ return json(500, {
111
+ ok: false,
112
+ code: 'UNAVAILABLE',
113
+ message: error instanceof Error ? error.message : String(error),
114
+ });
115
+ }
116
+ }
117
+ function requiredSession(url) {
118
+ const sessionId = url.searchParams.get('sessionId') ?? '';
119
+ if (sessionId === '')
120
+ throw new HttpError(400, 'VALIDATION', 'sessionId is required.');
121
+ return sessionId;
122
+ }
123
+ function readInt(url, name, optional) {
124
+ const raw = url.searchParams.get(name);
125
+ if (raw == null || raw === '')
126
+ return optional ? null : null;
127
+ if (!/^-?\d+$/.test(raw))
128
+ throw new HttpError(400, 'VALIDATION', `${name} must be an integer.`);
129
+ const value = Number(raw);
130
+ if (!Number.isSafeInteger(value))
131
+ throw new HttpError(400, 'VALIDATION', `${name} must be an integer.`);
132
+ return value;
133
+ }
134
+ function json(status, body) {
135
+ const text = JSON.stringify(body);
136
+ return new Response(text, {
137
+ status,
138
+ headers: {
139
+ 'content-type': 'application/json; charset=utf-8',
140
+ 'content-length': String(Buffer.byteLength(text)),
141
+ 'cache-control': 'no-store',
142
+ },
143
+ });
144
+ }
@@ -0,0 +1,76 @@
1
+ import { eventNdjson } from "./canonical.js";
2
+ import { planEventChunks, sha256Hex } from "./plan.js";
3
+ export const RANGE_PAGE_LIMIT = 500;
4
+ export const LOG_FORMAT = 'ndjson-v1';
5
+ export class HttpError extends Error {
6
+ status;
7
+ code;
8
+ constructor(status, code, message) {
9
+ super(message);
10
+ this.status = status;
11
+ this.code = code;
12
+ }
13
+ }
14
+ export function createLogSnapshotService(source) {
15
+ const plans = new Map();
16
+ const inflight = new Map();
17
+ async function planOf(sessionId, throughSeq) {
18
+ const head = await source.head(sessionId);
19
+ const pin = throughSeq == null ? head : throughSeq;
20
+ if (!Number.isInteger(pin) || pin < -1)
21
+ throw new HttpError(400, 'VALIDATION', 'throughSeq must be an integer >= -1.');
22
+ if (pin > head)
23
+ throw new HttpError(409, 'STALE', 'throughSeq is past the current log head.');
24
+ const cached = plans.get(sessionId);
25
+ if (cached && cached.throughSeq === pin)
26
+ return cached;
27
+ const { events } = await source.read(sessionId, -1, pin);
28
+ const planned = { sessionId, ...planEventChunks(events), throughSeq: pin, count: events.length };
29
+ if (pin === -1)
30
+ planned.throughSeq = -1;
31
+ plans.set(sessionId, planned);
32
+ return planned;
33
+ }
34
+ async function chunkBytes(sessionId, fromSeq, toSeq, hash) {
35
+ const key = `${sessionId}:${fromSeq}:${toSeq}:${hash}`;
36
+ const existing = inflight.get(key);
37
+ if (existing)
38
+ return existing;
39
+ const job = (async () => {
40
+ const { events } = await source.read(sessionId, fromSeq - 1, toSeq);
41
+ const { bytes } = eventNdjson(events);
42
+ if (sha256Hex(bytes) !== hash)
43
+ throw new HttpError(409, 'STALE', 'Chunk hash does not match this range.');
44
+ return bytes;
45
+ })();
46
+ inflight.set(key, job);
47
+ try {
48
+ return await job;
49
+ }
50
+ finally {
51
+ inflight.delete(key);
52
+ }
53
+ }
54
+ return {
55
+ async manifest(sessionId, throughSeq, afterSeq) {
56
+ const plan = await planOf(sessionId, throughSeq);
57
+ const after = afterSeq == null ? -1 : afterSeq;
58
+ const chunks = after < 0 ? plan.chunks : plan.chunks.filter(chunk => chunk.toSeq > after);
59
+ return { ...plan, chunks };
60
+ },
61
+ async chunk(sessionId, fromSeq, toSeq, hash) {
62
+ if (hash === '' || !Number.isInteger(fromSeq) || !Number.isInteger(toSeq) || fromSeq > toSeq) {
63
+ throw new HttpError(400, 'VALIDATION', 'fromSeq, toSeq, and hash are required.');
64
+ }
65
+ return chunkBytes(sessionId, fromSeq, toSeq, hash);
66
+ },
67
+ async range(sessionId, afterSeq, throughSeq, limit) {
68
+ const pageLimit = Math.min(Math.max(1, limit), RANGE_PAGE_LIMIT);
69
+ const { events, throughSeq: pin } = await source.read(sessionId, afterSeq, throughSeq);
70
+ const page = events.slice(0, pageLimit);
71
+ const last = page.length === 0 ? afterSeq : page[page.length - 1].seq;
72
+ const nextAfterSeq = last < pin && page.length === pageLimit ? last : null;
73
+ return { events: page, throughSeq: pin, nextAfterSeq };
74
+ },
75
+ };
76
+ }