@morlay/session-rdb 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/README.md +4 -7
  2. package/dist/artifact.d.mts +2 -1
  3. package/dist/{src-BmogreEz.mjs → deletion-ODZFcbZm.mjs} +134 -90
  4. package/dist/deletion.d.mts +1 -1
  5. package/dist/deletion.mjs +1 -1
  6. package/dist/export.d.mts +1 -1
  7. package/dist/gc.d.mts +1 -1
  8. package/dist/import.d.mts +2 -2
  9. package/dist/import.mjs +2 -2
  10. package/dist/index.d.mts +1 -1
  11. package/dist/index.mjs +3 -3
  12. package/dist/log-C0q5svm7.d.mts +594 -0
  13. package/dist/{sqlite-YCr2-Fov.mjs → sqlite-DjOnMRh2.mjs} +73 -2
  14. package/dist/storage.d.mts +2 -2
  15. package/dist/storage.mjs +1 -2
  16. package/dist/testing.d.mts +2 -2
  17. package/dist/testing.mjs +95 -14669
  18. package/dist/usage.d.mts +1 -1
  19. package/drizzle/postgres/20261008103001_v3_schedule_tasks/migration.sql +11 -0
  20. package/drizzle/sqlite/20261008103000_v3_schedule_tasks/migration.sql +14 -0
  21. package/package.json +60 -42
  22. package/src/branch.ts +1 -1
  23. package/src/entities/v3/index.ts +4 -0
  24. package/src/entities/v3/schedule-tasks.ts +16 -0
  25. package/src/index.ts +2 -1
  26. package/src/schema.ts +2 -0
  27. package/src/sqlite.ts +5 -2
  28. package/src/storage-takeover/repository.ts +61 -0
  29. package/src/storage-takeover/storage-backend.ts +120 -42
  30. package/src/storage-takeover/types.ts +9 -0
  31. package/dist/dist-DHer1cql.mjs +0 -1531
  32. package/dist/invariant.d.mts +0 -6
  33. package/dist/invariant.mjs +0 -8
  34. package/dist/log-DYyxRh7j.d.mts +0 -6078
  35. package/dist/rolldown-runtime-EAsCxrDo.mjs +0 -14
  36. package/src/invariant.ts +0 -13
  37. package/dist/{branch-aiXPmnKC.mjs → branch-DbFW3rJX.mjs} +1 -1
@@ -0,0 +1,594 @@
1
+ import { Session, SessionEvent, SessionHeader, SessionId, SessionLogOffset } from "@deepseek-ai/dsh-session";
2
+ import { SessionAccess, SessionHandle, SessionHandleAppendOptions, SessionHandleFlushOptions, SessionHandleReadOptions, SessionHandleReadResult, SessionPersistence, SessionPersistenceCreateOptions, SessionPersistenceListOptions, SessionPersistenceOpenOptions, SessionPersistenceRevision, SessionPersistenceSnapshot, SessionPersistenceStatOptions, SessionStorageMetadata } from "@deepseek-ai/dsh-session-persistence";
3
+ import { ScheduleTask } from "@deepseek-ai/dsh-schedule";
4
+ import { CheckpointIdentity } from "@deepseek-ai/dsh-session-projection-cache";
5
+ import { ProjectionCheckpoint } from "@deepseek-ai/dsh-session-projection";
6
+ import { WorkspaceDomainState, WorkspaceRecord } from "@deepseek-ai/dsh-workspace";
7
+ import { Context } from "@deepseek-ai/cordis";
8
+ import { Volatile } from "@deepseek-ai/cosmokit";
9
+ import z from "@deepseek-ai/schemastery";
10
+ import { BranchAnchorMode, BranchBoundary, ForkFromOptions, SessionBranch, SessionBranchProvider } from "@morlay/session-branch";
11
+ //#region src/storage-takeover/types.d.ts
12
+ interface StoredProjcacheEntry {
13
+ sessionId: SessionId;
14
+ identity: CheckpointIdentity;
15
+ rows: ProjectionCheckpoint;
16
+ }
17
+ interface StorageRepository {
18
+ readUnitVersion(name: string): Promise<number | undefined>;
19
+ insertUnitVersion(name: string, version: number): Promise<void>;
20
+ listWorkspaces(): Promise<Array<{
21
+ id: string;
22
+ record: WorkspaceRecord;
23
+ }>>;
24
+ putWorkspace(id: string, record: WorkspaceRecord): Promise<void>;
25
+ deleteWorkspace(id: string): Promise<void>;
26
+ readWorkspaceState(): Promise<WorkspaceDomainState | null>;
27
+ writeWorkspaceState(state: WorkspaceDomainState): Promise<void>;
28
+ listScheduleTasks(): Promise<Array<{
29
+ id: string;
30
+ task: ScheduleTask;
31
+ }>>;
32
+ putScheduleTask(id: string, task: ScheduleTask): Promise<void>;
33
+ deleteScheduleTask(id: string): Promise<void>;
34
+ loadProjcache(): Promise<StoredProjcacheEntry[]>;
35
+ readProjcacheDirect?(sessionId: string): StoredProjcacheEntry | undefined;
36
+ readSessionTitleDirect?(sessionId: string): {
37
+ title: string;
38
+ seq: number;
39
+ } | undefined;
40
+ putProjcache(sessionId: string, rows: ProjectionCheckpoint): Promise<void>;
41
+ pruneStaleProjcache(): Promise<number>;
42
+ }
43
+ //#endregion
44
+ //#region src/routes.d.ts
45
+ declare const SESSION_ROWS_PATH = "/api/morlay/v1/session/rows";
46
+ declare const SESSION_EXPORT_PATH = "/api/morlay/v1/session/export";
47
+ declare const SESSION_IMPORT_PATH = "/api/morlay/v1/session/import";
48
+ declare const SESSION_DELETE_PATH = "/api/morlay/v1/session/delete";
49
+ declare const SESSION_GC_PATH = "/api/morlay/v1/session/gc";
50
+ declare const SESSION_USAGE_PATH = "/api/morlay/v1/session/usage";
51
+ //#endregion
52
+ //#region src/write-guard.d.ts
53
+ declare class WriteGuard {
54
+ private readonly headSeqs;
55
+ confirmHead(id: SessionId, head: number): void;
56
+ has(id: SessionId): boolean;
57
+ assertNoConcurrentWriter(id: SessionId, storedHead: number): void;
58
+ }
59
+ //#endregion
60
+ //#region src/schema.d.ts
61
+ declare const SCHEMA_VERSION = 3;
62
+ declare const SESSION_PERSISTENCE_SQLITE_APPLICATION_ID = 1146308688;
63
+ declare const EVENT_ENCODING = "json";
64
+ declare const tPersistenceState: any;
65
+ declare const tSchemaMeta: any;
66
+ declare const tSessions: any;
67
+ declare const tEvents: any;
68
+ declare const tSessionEvents: any;
69
+ declare const tStorageUnits: any;
70
+ declare const tWorkspaces: any;
71
+ declare const tWorkspaceSessions: any;
72
+ declare const tWorkspaceState: any;
73
+ declare const tScheduleTasks: any;
74
+ declare const tSessionProjcacheRows: any;
75
+ declare const tEventUsage: any;
76
+ declare const tSessionUsage: any;
77
+ declare const tSessionCounts: any;
78
+ type JournalMode = "wal" | "delete" | "truncate" | "persist";
79
+ declare const DEFAULT_BUSY_TIMEOUT_MS = 5000;
80
+ type EventKind = "message" | "thinking" | "turn" | "tool" | "request" | "config" | "audit" | "lifecycle" | "inbox" | "compaction" | "llm" | "subagent" | "team" | "workflow" | "goal" | "schedule" | "todo" | "web";
81
+ type EventRole = "user" | "assistant" | "tool";
82
+ declare function eventKind(event: {
83
+ type: string;
84
+ data?: unknown;
85
+ }): EventKind | "";
86
+ declare function eventDimensions(event: SessionEvent): {
87
+ kind: string;
88
+ role: string;
89
+ name: string;
90
+ actionId: string;
91
+ };
92
+ //#endregion
93
+ //#region src/rows.d.ts
94
+ interface SessionRowsItem {
95
+ sessionId: string;
96
+ title: string | null;
97
+ origin: string | null;
98
+ cwd: string | null;
99
+ createdAt: number;
100
+ updatedAt: number;
101
+ archived: boolean;
102
+ workspace: string | null;
103
+ }
104
+ interface SessionRowsValue {
105
+ items: SessionRowsItem[];
106
+ total: number;
107
+ page: number;
108
+ pageSize: number;
109
+ }
110
+ //#endregion
111
+ //#region src/branch.d.ts
112
+ declare function locateTurnEnd(events: readonly SessionEvent[], atSeq?: number, mode?: BranchAnchorMode): number;
113
+ interface LiveSessionHooks {
114
+ getSession(id: SessionId): Session | undefined;
115
+ getAgent(id: SessionId): LiveAgentLike | undefined;
116
+ flush(session: Session): Promise<boolean>;
117
+ warn?(message: string): void;
118
+ resetProjections?(session: Session): void;
119
+ resetTokenMeter?(session: Session): void;
120
+ refreshProjectionCache?(session: ProjectionCacheSession): Promise<void>;
121
+ }
122
+ interface ProjectionCacheSession {
123
+ readonly id: SessionId;
124
+ readonly header: SessionHeader;
125
+ readonly inheritedEventCount: SessionLogOffset;
126
+ readonly headSeq?: number;
127
+ snapshotEvents(): readonly SessionEvent[];
128
+ }
129
+ interface LiveAgentLike {
130
+ session: Session;
131
+ requestHeaderLogged?: boolean;
132
+ inbox?: {
133
+ clear(): void;
134
+ };
135
+ }
136
+ declare function truncateLiveSession(session: Session, newLength: number): void;
137
+ declare class SessionBranchRdbProvider implements SessionBranchProvider {
138
+ private readonly persistence;
139
+ private readonly live;
140
+ readonly name = "session-rdb";
141
+ constructor(persistence: SessionPersistenceRdb, live?: LiveSessionHooks);
142
+ readBranchPrefix(id: SessionId, atSeq?: number, mode?: BranchAnchorMode, signal?: AbortSignal): Promise<BranchBoundary>;
143
+ readRawEvents(id: SessionId, signal?: AbortSignal): Promise<{
144
+ meta: SessionHeader;
145
+ events: readonly SessionEvent[];
146
+ }>;
147
+ forkFrom(sourceId: SessionId, options?: ForkFromOptions, signal?: AbortSignal): Promise<SessionId>;
148
+ rewind(id: SessionId, toBoundary: number, signal?: AbortSignal): Promise<SessionPersistenceSnapshot>;
149
+ private rewindKeepLength;
150
+ private refreshProjectionCache;
151
+ }
152
+ declare class SessionBranchRdb extends SessionBranch {
153
+ static inject: string[];
154
+ private readonly warned;
155
+ constructor(ctx: import("@deepseek-ai/cordis").Context);
156
+ resetLiveDerivedState(session: Session): void;
157
+ private warnOnce;
158
+ private resetProjectionCells;
159
+ private resetTokenMeterFold;
160
+ private readonly provider;
161
+ readBranchPrefix(id: SessionId, atSeq?: number, mode?: BranchAnchorMode, signal?: AbortSignal): Promise<BranchBoundary>;
162
+ readRawEvents(id: SessionId, signal?: AbortSignal): Promise<{
163
+ meta: SessionHeader;
164
+ events: readonly SessionEvent[];
165
+ }>;
166
+ forkFrom(sourceId: SessionId, options?: ForkFromOptions, signal?: AbortSignal): Promise<SessionId>;
167
+ rewind(id: SessionId, toBoundary: number, signal?: AbortSignal): Promise<SessionPersistenceSnapshot>;
168
+ }
169
+ //#endregion
170
+ //#region src/index.d.ts
171
+ interface SessionPersistenceRdbInternals {
172
+ readonly backend: Backend;
173
+ readonly writeGuard: WriteGuard;
174
+ create(meta: SessionHeader, inheritedEventCount?: number): Promise<SessionHandle>;
175
+ append(id: SessionId, events: readonly SessionEvent[]): Promise<void>;
176
+ load(id: SessionId): Promise<import("@deepseek-ai/dsh-session-persistence").SessionInspection>;
177
+ inspect(id: SessionId, signal?: AbortSignal): Promise<import("@deepseek-ai/dsh-session-persistence").SessionInspection>;
178
+ readFrom(id: SessionId, fromSeq: number, signal?: AbortSignal): Promise<{
179
+ meta: SessionHeader;
180
+ inheritedEventCount: number;
181
+ events: readonly SessionEvent[];
182
+ }>;
183
+ listSnapshots(signal?: AbortSignal): Promise<SessionPersistenceSnapshot[]>;
184
+ readStoredRevision(id: SessionId, signal?: AbortSignal): Promise<import("@deepseek-ai/dsh-session-persistence").SessionPersistenceRevision | undefined>;
185
+ registerReuseEventIds(childId: SessionId, map: ReadonlyMap<number, string>): void;
186
+ dropReuseEventIds(childId: SessionId): void;
187
+ }
188
+ interface ProjectionCacheOptions {
189
+ writeEveryEvents?: number;
190
+ writeIntervalMs?: number;
191
+ }
192
+ type Config = {
193
+ type: "sqlite";
194
+ path: string;
195
+ journalMode?: JournalMode | undefined;
196
+ busyTimeout?: number | undefined;
197
+ projectionCache?: ProjectionCacheOptions | undefined;
198
+ } | {
199
+ type: "postgres";
200
+ connectionString: string;
201
+ schema?: string | undefined;
202
+ projectionCache?: ProjectionCacheOptions | undefined;
203
+ };
204
+ type ResolvedConfig = Volatile<Config>;
205
+ type SessionDeletionErrorCode = "SESSION_NOT_FOUND" | "SESSION_NOT_ARCHIVED" | "SESSION_LIVE";
206
+ declare class SessionDeletionError extends Error {
207
+ readonly code: SessionDeletionErrorCode;
208
+ constructor(message: string, code: SessionDeletionErrorCode);
209
+ }
210
+ interface PendingSession {
211
+ readonly header: SessionHeader;
212
+ readonly revision: SessionPersistenceRevision;
213
+ readonly inheritedEventCount: SessionLogOffset;
214
+ readonly cursor: number;
215
+ readonly everAppended: boolean;
216
+ }
217
+ declare class RdbBackendTracker {
218
+ private readonly name;
219
+ private readonly writers;
220
+ private readonly pending;
221
+ private readonly openHandles;
222
+ private counter;
223
+ constructor(name: string);
224
+ registerCreated(header: SessionHeader, inheritedEventCount: SessionLogOffset): void;
225
+ updatePending(id: SessionId, cursor: number, everAppended: boolean): void;
226
+ claimWrite(id: SessionId): void;
227
+ releaseClaim(id: SessionId): void;
228
+ pendingOf(id: SessionId): PendingSession | undefined;
229
+ hasPending(id: SessionId): boolean;
230
+ pendingEntries(): IterableIterator<[SessionId, PendingSession]>;
231
+ materialized(id: SessionId): void;
232
+ adopt(handle: RdbSessionHandle): RdbSessionHandle;
233
+ release(handle: RdbSessionHandle, materialized: boolean): void;
234
+ writerOf(id: SessionId): RdbSessionHandle | undefined;
235
+ hasOpenHandle(id: SessionId): boolean;
236
+ flushAll(): Promise<void>;
237
+ closeAll(): Promise<void>;
238
+ }
239
+ declare class RdbSessionHandle implements SessionHandle {
240
+ private readonly persistence;
241
+ readonly id: SessionId;
242
+ readonly header: SessionHeader;
243
+ readonly access: SessionAccess;
244
+ private readonly state;
245
+ private chain;
246
+ private closing;
247
+ private cursor;
248
+ private materialized;
249
+ private buffered;
250
+ private batchTimer;
251
+ private drainPaused;
252
+ private draining;
253
+ private tornTruncateTo;
254
+ private everAppended;
255
+ constructor(persistence: SessionPersistenceRdb, id: SessionId, header: SessionHeader, access: SessionAccess, state: {
256
+ cursor: number;
257
+ materialized: boolean;
258
+ inheritedEventCount: SessionLogOffset;
259
+ tornTruncateTo?: number;
260
+ });
261
+ get inheritedEventCount(): SessionLogOffset;
262
+ get cursorValue(): number;
263
+ read(offset?: number, length?: number, options?: SessionHandleReadOptions): Promise<SessionHandleReadResult>;
264
+ append(events: readonly SessionEvent[], options?: SessionHandleAppendOptions): Promise<void>;
265
+ flush(options?: SessionHandleFlushOptions): Promise<void>;
266
+ close(): Promise<void>;
267
+ [Symbol.asyncDispose](): Promise<void>;
268
+ enqueueLive(event: SessionEvent, reportBackgroundFailure: (error: unknown) => void): void;
269
+ resetAfterRewind(cursor: number, inheritedEventCount?: number): void;
270
+ drainLive(): Promise<void>;
271
+ private drainBuffered;
272
+ private enqueueChain;
273
+ private run;
274
+ private assertOpen;
275
+ static readonly LIVE_WRITE_BATCH_MAX_DELAY_MS = 200;
276
+ }
277
+ declare class SessionPersistenceRdb extends SessionPersistence {
278
+ static inject: string[];
279
+ static Config: z<Config, ResolvedConfig>;
280
+ readonly name = "session-rdb";
281
+ readonly tracker: RdbBackendTracker;
282
+ private readonly backend;
283
+ private storeIdentity;
284
+ private readonly ready;
285
+ private readonly writeGuard;
286
+ private readonly reuseEventIds;
287
+ private readonly liveBuffers;
288
+ private readonly liveReady;
289
+ readonly config: Config;
290
+ constructor(ctx: Context, config: ResolvedConfig, injectedBackend?: Backend);
291
+ private init;
292
+ create(header: SessionHeader, options?: SessionPersistenceCreateOptions): Promise<SessionHandle>;
293
+ open(id: SessionId, access: SessionAccess, options?: SessionPersistenceOpenOptions): Promise<SessionHandle>;
294
+ flush(): Promise<void>;
295
+ stat(id: SessionId, options?: SessionPersistenceStatOptions): Promise<SessionPersistenceSnapshot | undefined>;
296
+ list(options?: SessionPersistenceListOptions): Promise<readonly SessionPersistenceSnapshot[]>;
297
+ deleteSession(id: SessionId, signal?: AbortSignal): Promise<void>;
298
+ collectOrphans(): Promise<number>;
299
+ collectOrphanSessions(): Promise<number>;
300
+ usageReport(sinceMs?: number): Promise<UsageAggregate>;
301
+ vacuum(): Promise<void>;
302
+ private unarchiveBeforeDeletion;
303
+ readRaw(id: SessionId, signal?: AbortSignal): Promise<{
304
+ meta: SessionHeader;
305
+ inheritedEventCount: number;
306
+ filename: string;
307
+ content: string;
308
+ } | undefined>;
309
+ materializeEmpty(meta: SessionHeader, inheritedEventCount: SessionLogOffset): Promise<void>;
310
+ appendBatch(meta: SessionHeader, inheritedEventCount: SessionLogOffset, events: readonly SessionEvent[], tornTruncateTo?: number): Promise<boolean>;
311
+ readLog(id: SessionId, options?: {
312
+ fromSeq?: number;
313
+ }, signal?: AbortSignal): Promise<{
314
+ meta: SessionHeader;
315
+ inheritedEventCount: number;
316
+ events: SessionEvent[];
317
+ tornFrom?: number;
318
+ incarnation: string;
319
+ revision: number;
320
+ storedCount: number;
321
+ migrated: boolean;
322
+ } | undefined>;
323
+ private recordSessionStats;
324
+ rebuildSessionStats(id: SessionId): Promise<void>;
325
+ private refreshEventUsageFlags;
326
+ private dropSessionStats;
327
+ private rewriteMigratedLog;
328
+ listSnapshots(signal?: AbortSignal): Promise<Array<SessionPersistenceSnapshot & {
329
+ inheritedEventCount: number;
330
+ }>>;
331
+ readStoredRevision(id: SessionId, signal?: AbortSignal): Promise<SessionPersistenceRevision | undefined>;
332
+ createAndAppend(header: SessionHeader, events: readonly SessionEvent[], inheritedEventCount?: number): Promise<void>;
333
+ load(id: SessionId, signal?: AbortSignal): Promise<import("@deepseek-ai/dsh-session-persistence").SessionInspection>;
334
+ append(id: SessionId, events: readonly SessionEvent[]): Promise<void>;
335
+ readFrom(id: SessionId, fromSeq: number, signal?: AbortSignal): Promise<{
336
+ meta: SessionHeader;
337
+ inheritedEventCount: number;
338
+ events: readonly SessionEvent[];
339
+ }>;
340
+ close(): Promise<void>;
341
+ registerReuseEventIds(childId: SessionId, map: ReadonlyMap<number, string>): void;
342
+ dropReuseEventIds(childId: SessionId): void;
343
+ internals(): SessionPersistenceRdbInternals;
344
+ private rowRevision;
345
+ private readonly liveFailures;
346
+ private readonly liveDropWarned;
347
+ private watchLiveReady;
348
+ private installLiveRouting;
349
+ private ensureLiveHandle;
350
+ }
351
+ //#endregion
352
+ //#region src/usage.d.ts
353
+ type UsageRangeKey = "all" | "day" | "week" | "7d" | "30d" | "90d";
354
+ declare function resolveUsageSince(range: UsageRangeKey, now: number): number | undefined;
355
+ interface UsageTokenTotals {
356
+ inputTokens: number;
357
+ outputTokens: number;
358
+ cacheReadTokens: number;
359
+ reasoningTokens: number;
360
+ totalTokens: number;
361
+ }
362
+ interface UsageActivityTotals {
363
+ turns: number;
364
+ steps: number;
365
+ userInputs: number;
366
+ toolCalls: number;
367
+ }
368
+ interface UsageTotals extends UsageTokenTotals, UsageActivityTotals {}
369
+ interface UsageBucket extends UsageTokenTotals {
370
+ day: string;
371
+ provider: string | null;
372
+ model: string | null;
373
+ subagent: boolean;
374
+ }
375
+ interface UsageSessionRow extends UsageTotals {
376
+ sessionId: string;
377
+ title: string | null;
378
+ subagent: boolean;
379
+ archived: boolean;
380
+ }
381
+ interface UsageAggregate {
382
+ totals: UsageTotals;
383
+ subagent: UsageTotals;
384
+ human: UsageTotals;
385
+ buckets: UsageBucket[];
386
+ sessions: UsageSessionRow[];
387
+ }
388
+ type SessionUsageReport = UsageAggregate;
389
+ declare const COUNTED_EVENT_TYPES: readonly ["turn/start", "step/start", "user/message", "tool/call"];
390
+ declare function addActivityCount(target: UsageActivityTotals, type: string, count: number): UsageActivityTotals;
391
+ declare function localDayKey(ms: number): string;
392
+ declare function emptyTotals(): UsageTotals;
393
+ declare function addTokenTotals(target: UsageTokenTotals, row: UsageTokenTotals): UsageTokenTotals;
394
+ declare function addActivityTotals(target: UsageActivityTotals, row: UsageActivityTotals): UsageActivityTotals;
395
+ declare function addTotals(target: UsageTotals, row: UsageTotals): UsageTotals;
396
+ declare function registerSessionUsage(ctx: Context, persistence: SessionPersistenceRdb): void;
397
+ //#endregion
398
+ //#region src/backend.d.ts
399
+ interface SessionRow {
400
+ fSessionId: string;
401
+ fHeadEventId: string;
402
+ fHeadSequence: number;
403
+ fVersion: number;
404
+ fCreatedAt: number;
405
+ fCwd: string | null;
406
+ fParentSession: string | null;
407
+ fSeedLength: number | null;
408
+ fOrigin: string | null;
409
+ fDelegationDepth: number | null;
410
+ fAgentPreset: string | null;
411
+ fIncarnation: string;
412
+ fRevision: number;
413
+ fArchivedAt: number | null;
414
+ fPinnedSeq: number | null;
415
+ fLastEventAt: number | null;
416
+ }
417
+ interface EventInsert {
418
+ fEventId: string;
419
+ fParentId: string;
420
+ fType: string;
421
+ fKind: string;
422
+ fRole: string;
423
+ fName: string;
424
+ fActionId: string;
425
+ fEncoding: string;
426
+ fData: string;
427
+ fCreatedAt: number;
428
+ }
429
+ interface EventRow {
430
+ fEventId: string;
431
+ fSequence: number;
432
+ fType: string;
433
+ fKind: string;
434
+ fRole: string;
435
+ fName: string;
436
+ fActionId: string;
437
+ fCreatedAt: number;
438
+ fData: string;
439
+ fSurfaceOp: string | null;
440
+ }
441
+ interface BackendTx {
442
+ upsertSession(storage: SessionStorageMetadata, incarnation: string): Promise<void>;
443
+ getHead(id: SessionId): Promise<Pick<SessionRow, "fHeadEventId" | "fHeadSequence">>;
444
+ getSeedLength(id: SessionId): Promise<number | null>;
445
+ updateSeedLength(id: SessionId, seedLength: number): Promise<void>;
446
+ refreshTitle(id: SessionId): Promise<void>;
447
+ insertEvents(events: EventInsert[], usageRows: readonly EventUsageRow[]): Promise<void>;
448
+ insertBridges(rows: Array<{
449
+ fSessionId: SessionId;
450
+ fEventId: string;
451
+ fSequence: number;
452
+ fSurfaceOp: string | null;
453
+ }>): Promise<void>;
454
+ updateHead(id: SessionId, headEventId: string, headSequence: number): Promise<void>;
455
+ bumpRevision(id: SessionId, lastEventAt?: number): Promise<void>;
456
+ deleteBridgeTail(id: SessionId, fromSequence: number): Promise<readonly string[]>;
457
+ getPrevBridge(id: SessionId, sequence: number): Promise<{
458
+ fEventId: string;
459
+ fSequence: number;
460
+ } | undefined>;
461
+ deleteSession(id: SessionId): Promise<readonly string[]>;
462
+ deleteSessions(ids: SessionId[]): Promise<{
463
+ deleted: number;
464
+ eventIds: readonly string[];
465
+ }>;
466
+ }
467
+ interface SessionListRowRecord {
468
+ sessionId: string;
469
+ title: string | null;
470
+ origin: string | null;
471
+ cwd: string | null;
472
+ createdAt: number;
473
+ updatedAt: number;
474
+ archived: boolean;
475
+ subagent: boolean;
476
+ workspace: string | null;
477
+ }
478
+ interface SessionListRowsQuery {
479
+ query?: string;
480
+ includeSubagents?: boolean;
481
+ limit?: number;
482
+ offset?: number;
483
+ }
484
+ interface SessionListRowsPage {
485
+ items: SessionListRowRecord[];
486
+ total: number;
487
+ }
488
+ interface SessionCountBucket extends UsageActivityTotals {
489
+ day: string;
490
+ }
491
+ interface SessionUsageBucket extends UsageTokenTotals {
492
+ day: string;
493
+ provider: string | null;
494
+ model: string | null;
495
+ }
496
+ interface Backend {
497
+ readonly kind: "sqlite" | "postgres";
498
+ readonly storeIdentity: string;
499
+ readonly storage: StorageRepository;
500
+ open(): Promise<void>;
501
+ getSession(id: SessionId): Promise<SessionRow | undefined>;
502
+ getEventRows(id: SessionId, fromSequence?: number): Promise<EventRow[]>;
503
+ getEventTypeAt(id: SessionId, sequence: number): Promise<string | undefined>;
504
+ getEventTypesBefore(id: SessionId, beforeSequence: number, limit: number): Promise<Array<Pick<EventRow, "fSequence" | "fType">>>;
505
+ listSessions(): Promise<SessionRow[]>;
506
+ transaction<T>(fn: (tx: BackendTx) => Promise<T>): Promise<T>;
507
+ collectOrphans(): Promise<number>;
508
+ listOrphanSubagentSessions(): Promise<SessionId[]>;
509
+ usageReport(sinceMs?: number): Promise<UsageAggregate>;
510
+ listSessionRows(query?: SessionListRowsQuery): Promise<SessionListRowsPage>;
511
+ incrementSessionUsage(id: SessionId, buckets: readonly SessionUsageBucket[]): Promise<void>;
512
+ incrementSessionCounts(id: SessionId, buckets: readonly SessionCountBucket[]): Promise<void>;
513
+ rebuildSessionStats(id: SessionId): Promise<void>;
514
+ refreshEventUsageFlags(eventIds: readonly string[]): Promise<void>;
515
+ deleteSessionStats(id: SessionId): Promise<void>;
516
+ vacuum(): Promise<void>;
517
+ close(): Promise<void>;
518
+ }
519
+ //#endregion
520
+ //#region src/log.d.ts
521
+ declare function rowToMeta(row: SessionRow): SessionHeader;
522
+ declare function sessionInsertRow(storage: SessionStorageMetadata, incarnation: string): {
523
+ fSessionId: string;
524
+ fHeadEventId: string;
525
+ fHeadSequence: number;
526
+ fVersion: number;
527
+ fCreatedAt: number;
528
+ fCwd: string | null;
529
+ fParentSession: string | null;
530
+ fSeedLength: number | null;
531
+ fOrigin: string | null;
532
+ fDelegationDepth: number | null;
533
+ fAgentPreset: string | null;
534
+ fIncarnation: string;
535
+ fRevision: number;
536
+ };
537
+ declare function sessionConflictRow(storage: SessionStorageMetadata): {
538
+ fVersion: number;
539
+ fCreatedAt: number;
540
+ fCwd: string | null;
541
+ fParentSession: string | null;
542
+ fSeedLength: number | null;
543
+ fOrigin: string | null;
544
+ fDelegationDepth: number | null;
545
+ fAgentPreset: string | null;
546
+ };
547
+ declare function rowToEvent(row: EventRow): SessionEvent;
548
+ declare function recomputeReplaceProvenance(events: SessionEvent[]): void;
549
+ declare function findSurfaceRepairs(events: readonly SessionEvent[]): {
550
+ degradeToAppend: Set<number>;
551
+ addAppendMarker: Set<number>;
552
+ clearSurfaceOp: Set<number>;
553
+ clampEnd: Map<number, number>;
554
+ };
555
+ declare function repairSurfaceOps(events: SessionEvent[]): void;
556
+ declare function repairAssistantSettlement(events: SessionEvent[]): void;
557
+ declare function repairRequestHeaders(events: SessionEvent[]): void;
558
+ declare function syncMeteringRanges(events: SessionEvent[]): void;
559
+ declare function renameLegacyPtcEvents(events: SessionEvent[]): void;
560
+ declare function normalizeToCurrentShape(events: SessionEvent[]): void;
561
+ declare function sealOwnEvents(events: readonly SessionEvent[]): void;
562
+ declare const OWN_EVENT_TYPES: ReadonlySet<string>;
563
+ declare function needsShapeAdoption(events: readonly SessionEvent[]): boolean;
564
+ declare function repairReadView(events: SessionEvent[]): void;
565
+ declare function orphanInboxSpliceSeqs(events: readonly SessionEvent[]): Set<number>;
566
+ declare function repairOrphanInboxSplices(events: SessionEvent[]): void;
567
+ declare function scanRows(rows: readonly EventRow[], base?: number): {
568
+ preserved: SessionEvent[];
569
+ tornFrom?: number;
570
+ };
571
+ declare function toJsonlArtifact(meta: SessionHeader, inheritedEventCount: number, events: readonly SessionEvent[]): string;
572
+ declare function titleOfEventData(data: string): string | undefined;
573
+ interface EventUsageRow {
574
+ fEventId: string;
575
+ fCreatedAt: number;
576
+ fDay: string;
577
+ fProvider: string | null;
578
+ fModel: string | null;
579
+ fReferenced: number;
580
+ fSubagent: number;
581
+ fInputTokens: number;
582
+ fOutputTokens: number;
583
+ fCacheReadTokens: number;
584
+ fReasoningTokens: number;
585
+ fTotalTokens: number;
586
+ }
587
+ declare function usageRowOf(event: {
588
+ fEventId: string;
589
+ fCreatedAt: number;
590
+ fType: string;
591
+ fData: string;
592
+ }, subagent: boolean): EventUsageRow | undefined;
593
+ //#endregion
594
+ export { SessionDeletionErrorCode as $, SessionRow as A, tWorkspaces as At, UsageTotals as B, usageRowOf as C, tSessionEvents as Ct, SessionCountBucket as D, tStorageUnits as Dt, EventRow as E, tSessions as Et, UsageAggregate as F, SESSION_IMPORT_PATH as Ft, emptyTotals as G, addActivityTotals as H, UsageBucket as I, SESSION_ROWS_PATH as It, resolveUsageSince as J, localDayKey as K, UsageRangeKey as L, SESSION_USAGE_PATH as Lt, COUNTED_EVENT_TYPES as M, SESSION_DELETE_PATH as Mt, SessionUsageReport as N, SESSION_EXPORT_PATH as Nt, SessionListRowsPage as O, tWorkspaceSessions as Ot, UsageActivityTotals as P, SESSION_GC_PATH as Pt, SessionDeletionError as Q, UsageSessionRow as R, StorageRepository as Rt, toJsonlArtifact as S, tSessionCounts as St, BackendTx as T, tSessionUsage as Tt, addTokenTotals as U, addActivityCount as V, addTotals as W, ProjectionCacheOptions as X, Config as Y, ResolvedConfig as Z, sealOwnEvents as _, tEventUsage as _t, normalizeToCurrentShape as a, truncateLiveSession as at, syncMeteringRanges as b, tScheduleTasks as bt, renameLegacyPtcEvents as c, DEFAULT_BUSY_TIMEOUT_MS as ct, repairReadView as d, EventRole as dt, SessionPersistenceRdb as et, repairRequestHeaders as f, JournalMode as ft, scanRows as g, eventKind as gt, rowToMeta as h, eventDimensions as ht, needsShapeAdoption as i, locateTurnEnd as it, SessionUsageBucket as j, WriteGuard as jt, SessionListRowsQuery as k, tWorkspaceState as kt, repairAssistantSettlement as l, EVENT_ENCODING as lt, rowToEvent as m, SESSION_PERSISTENCE_SQLITE_APPLICATION_ID as mt, OWN_EVENT_TYPES as n, SessionBranchRdb as nt, orphanInboxSpliceSeqs as o, SessionRowsItem as ot, repairSurfaceOps as p, SCHEMA_VERSION as pt, registerSessionUsage as q, findSurfaceRepairs as r, SessionBranchRdbProvider as rt, recomputeReplaceProvenance as s, SessionRowsValue as st, EventUsageRow as t, SessionPersistenceRdbInternals as tt, repairOrphanInboxSplices as u, EventKind as ut, sessionConflictRow as v, tEvents as vt, Backend as w, tSessionProjcacheRows as wt, titleOfEventData as x, tSchemaMeta as xt, sessionInsertRow as y, tPersistenceState as yt, UsageTokenTotals as z };