@morlay/session-rdb 0.1.9 → 0.1.10-alpha.0

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.
@@ -8,7 +8,6 @@ import { Context } from "@deepseek-ai/cordis";
8
8
  import { Volatile } from "@deepseek-ai/cosmokit";
9
9
  import z from "@deepseek-ai/schemastery";
10
10
  import { BranchAnchorMode, BranchBoundary, ForkFromOptions, SessionBranch, SessionBranchProvider } from "@morlay/session-branch";
11
- //#region src/storage-takeover/types.d.ts
12
11
  interface StoredProjcacheEntry {
13
12
  sessionId: SessionId;
14
13
  identity: CheckpointIdentity;
@@ -23,6 +22,7 @@ interface StorageRepository {
23
22
  }>>;
24
23
  putWorkspace(id: string, record: WorkspaceRecord): Promise<void>;
25
24
  deleteWorkspace(id: string): Promise<void>;
25
+ deleteWorkspaces(ids: readonly string[]): Promise<void>;
26
26
  readWorkspaceState(): Promise<WorkspaceDomainState | null>;
27
27
  writeWorkspaceState(state: WorkspaceDomainState): Promise<void>;
28
28
  listScheduleTasks(): Promise<Array<{
@@ -31,6 +31,7 @@ interface StorageRepository {
31
31
  }>>;
32
32
  putScheduleTask(id: string, task: ScheduleTask): Promise<void>;
33
33
  deleteScheduleTask(id: string): Promise<void>;
34
+ deleteScheduleTasks(ids: readonly string[]): Promise<void>;
34
35
  loadProjcache(): Promise<StoredProjcacheEntry[]>;
35
36
  readProjcacheDirect?(sessionId: string): StoredProjcacheEntry | undefined;
36
37
  readSessionTitleDirect?(sessionId: string): {
@@ -40,24 +41,18 @@ interface StorageRepository {
40
41
  putProjcache(sessionId: string, rows: ProjectionCheckpoint): Promise<void>;
41
42
  pruneStaleProjcache(): Promise<number>;
42
43
  }
43
- //#endregion
44
- //#region src/routes.d.ts
45
44
  declare const SESSION_ROWS_PATH = "/api/morlay/v1/session/rows";
46
45
  declare const SESSION_EXPORT_PATH = "/api/morlay/v1/session/export";
47
46
  declare const SESSION_IMPORT_PATH = "/api/morlay/v1/session/import";
48
47
  declare const SESSION_DELETE_PATH = "/api/morlay/v1/session/delete";
49
48
  declare const SESSION_GC_PATH = "/api/morlay/v1/session/gc";
50
49
  declare const SESSION_USAGE_PATH = "/api/morlay/v1/session/usage";
51
- //#endregion
52
- //#region src/write-guard.d.ts
53
50
  declare class WriteGuard {
54
51
  private readonly headSeqs;
55
52
  confirmHead(id: SessionId, head: number): void;
56
53
  has(id: SessionId): boolean;
57
54
  assertNoConcurrentWriter(id: SessionId, storedHead: number): void;
58
55
  }
59
- //#endregion
60
- //#region src/schema.d.ts
61
56
  declare const SCHEMA_VERSION = 3;
62
57
  declare const SESSION_PERSISTENCE_SQLITE_APPLICATION_ID = 1146308688;
63
58
  declare const EVENT_ENCODING = "json";
@@ -89,8 +84,6 @@ declare function eventDimensions(event: SessionEvent): {
89
84
  name: string;
90
85
  actionId: string;
91
86
  };
92
- //#endregion
93
- //#region src/rows.d.ts
94
87
  interface SessionRowsItem {
95
88
  sessionId: string;
96
89
  title: string | null;
@@ -107,8 +100,6 @@ interface SessionRowsValue {
107
100
  page: number;
108
101
  pageSize: number;
109
102
  }
110
- //#endregion
111
- //#region src/branch.d.ts
112
103
  declare function locateTurnEnd(events: readonly SessionEvent[], atSeq?: number, mode?: BranchAnchorMode): number;
113
104
  interface LiveSessionHooks {
114
105
  getSession(id: SessionId): Session | undefined;
@@ -133,7 +124,16 @@ interface LiveAgentLike {
133
124
  clear(): void;
134
125
  };
135
126
  }
136
- declare function truncateLiveSession(session: Session, newLength: number): void;
127
+ /**
128
+ * 会话**当前内存 log** 的快照(复制一份)。用于"需要完整序列"的操作(首次落库的 seed、rewind / fork 的历史),
129
+ * 不走已弃用的同步历史读 `snapshotEvents()`——那三个读口上游已声明「新调用禁止」,而这里要的是当前内存里
130
+ * 那份完整日志,不是按需分页的历史。
131
+ * @param session - 会话。
132
+ * @returns 事件副本(调用方可以自由遍历)。
133
+ */
134
+ declare function liveEventsOf(session: Session): readonly SessionEvent[];
135
+ /** @returns 截断后日志里最后一个 `turn/start` 的 turn 号(没有则 0)。 */
136
+ declare function truncateLiveSession(session: Session, newLength: number): number;
137
137
  declare class SessionBranchRdbProvider implements SessionBranchProvider {
138
138
  private readonly persistence;
139
139
  private readonly live;
@@ -166,8 +166,6 @@ declare class SessionBranchRdb extends SessionBranch {
166
166
  forkFrom(sourceId: SessionId, options?: ForkFromOptions, signal?: AbortSignal): Promise<SessionId>;
167
167
  rewind(id: SessionId, toBoundary: number, signal?: AbortSignal): Promise<SessionPersistenceSnapshot>;
168
168
  }
169
- //#endregion
170
- //#region src/index.d.ts
171
169
  interface SessionPersistenceRdbInternals {
172
170
  readonly backend: Backend;
173
171
  readonly writeGuard: WriteGuard;
@@ -195,11 +193,13 @@ type Config = {
195
193
  journalMode?: JournalMode | undefined;
196
194
  busyTimeout?: number | undefined;
197
195
  projectionCache?: ProjectionCacheOptions | undefined;
196
+ preparedSessionCacheSize?: number | undefined;
198
197
  } | {
199
198
  type: "postgres";
200
199
  connectionString: string;
201
200
  schema?: string | undefined;
202
201
  projectionCache?: ProjectionCacheOptions | undefined;
202
+ preparedSessionCacheSize?: number | undefined;
203
203
  };
204
204
  type ResolvedConfig = Volatile<Config>;
205
205
  type SessionDeletionErrorCode = "SESSION_NOT_FOUND" | "SESSION_NOT_ARCHIVED" | "SESSION_LIVE";
@@ -299,6 +299,12 @@ declare class SessionPersistenceRdb extends SessionPersistence {
299
299
  collectOrphanSessions(): Promise<number>;
300
300
  usageReport(sinceMs?: number): Promise<UsageAggregate>;
301
301
  vacuum(): Promise<void>;
302
+ /**
303
+ * 已归档的会话 id(`t_sessions.f_archived_at` 非空)。`session/list` 默认过滤掉它们。
304
+ * 读的是持久化里的归档标记,不依赖 `workspaceRegistry` 服务的就绪时机。
305
+ * @returns 归档过的 Session id 集。
306
+ */
307
+ archivedSessionIds(): Promise<ReadonlySet<string>>;
302
308
  private unarchiveBeforeDeletion;
303
309
  readRaw(id: SessionId, signal?: AbortSignal): Promise<{
304
310
  meta: SessionHeader;
@@ -348,8 +354,6 @@ declare class SessionPersistenceRdb extends SessionPersistence {
348
354
  private installLiveRouting;
349
355
  private ensureLiveHandle;
350
356
  }
351
- //#endregion
352
- //#region src/usage.d.ts
353
357
  type UsageRangeKey = "all" | "day" | "week" | "7d" | "30d" | "90d";
354
358
  declare function resolveUsageSince(range: UsageRangeKey, now: number): number | undefined;
355
359
  interface UsageTokenTotals {
@@ -394,8 +398,6 @@ declare function addTokenTotals(target: UsageTokenTotals, row: UsageTokenTotals)
394
398
  declare function addActivityTotals(target: UsageActivityTotals, row: UsageActivityTotals): UsageActivityTotals;
395
399
  declare function addTotals(target: UsageTotals, row: UsageTotals): UsageTotals;
396
400
  declare function registerSessionUsage(ctx: Context, persistence: SessionPersistenceRdb): void;
397
- //#endregion
398
- //#region src/backend.d.ts
399
401
  interface SessionRow {
400
402
  fSessionId: string;
401
403
  fHeadEventId: string;
@@ -516,8 +518,6 @@ interface Backend {
516
518
  vacuum(): Promise<void>;
517
519
  close(): Promise<void>;
518
520
  }
519
- //#endregion
520
- //#region src/log.d.ts
521
521
  declare function rowToMeta(row: SessionRow): SessionHeader;
522
522
  declare function sessionInsertRow(storage: SessionStorageMetadata, incarnation: string): {
523
523
  fSessionId: string;
@@ -590,5 +590,4 @@ declare function usageRowOf(event: {
590
590
  fType: string;
591
591
  fData: string;
592
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 };
593
+ export { SessionDeletionErrorCode as $, SessionRow as A, tWorkspaceState as At, UsageTotals as B, usageRowOf as C, tSessionCounts as Ct, SessionCountBucket as D, tSessions as Dt, EventRow as E, tSessionUsage as Et, UsageAggregate as F, SESSION_GC_PATH as Ft, emptyTotals as G, addActivityTotals as H, UsageBucket as I, SESSION_IMPORT_PATH as It, resolveUsageSince as J, localDayKey as K, UsageRangeKey as L, SESSION_ROWS_PATH as Lt, COUNTED_EVENT_TYPES as M, WriteGuard as Mt, SessionUsageReport as N, SESSION_DELETE_PATH as Nt, SessionListRowsPage as O, tStorageUnits as Ot, UsageActivityTotals as P, SESSION_EXPORT_PATH as Pt, SessionDeletionError as Q, UsageSessionRow as R, SESSION_USAGE_PATH as Rt, toJsonlArtifact as S, tSchemaMeta as St, BackendTx as T, tSessionProjcacheRows as Tt, addTokenTotals as U, addActivityCount as V, addTotals as W, ProjectionCacheOptions as X, Config as Y, ResolvedConfig as Z, sealOwnEvents as _, eventKind as _t, normalizeToCurrentShape as a, locateTurnEnd as at, syncMeteringRanges as b, tPersistenceState as bt, renameLegacyPtcEvents as c, SessionRowsValue as ct, repairReadView as d, EventKind as dt, SessionPersistenceRdb as et, repairRequestHeaders as f, EventRole as ft, scanRows as g, eventDimensions as gt, rowToMeta as h, SESSION_PERSISTENCE_SQLITE_APPLICATION_ID as ht, needsShapeAdoption as i, liveEventsOf as it, SessionUsageBucket as j, tWorkspaces as jt, SessionListRowsQuery as k, tWorkspaceSessions as kt, repairAssistantSettlement as l, DEFAULT_BUSY_TIMEOUT_MS as lt, rowToEvent as m, SCHEMA_VERSION as mt, OWN_EVENT_TYPES as n, SessionBranchRdb as nt, orphanInboxSpliceSeqs as o, truncateLiveSession as ot, repairSurfaceOps as p, JournalMode as pt, registerSessionUsage as q, findSurfaceRepairs as r, SessionBranchRdbProvider as rt, recomputeReplaceProvenance as s, SessionRowsItem as st, EventUsageRow as t, SessionPersistenceRdbInternals as tt, repairOrphanInboxSplices as u, EVENT_ENCODING as ut, sessionConflictRow as v, tEventUsage as vt, Backend as w, tSessionEvents as wt, titleOfEventData as x, tScheduleTasks as xt, sessionInsertRow as y, tEvents as yt, UsageTokenTotals as z, StorageRepository as zt };
@@ -1,4 +1,4 @@
1
- //#region src/routes.ts
1
+ // src/routes.ts
2
2
  const MORLAY_SESSION_API_PREFIX = "/api/morlay/v1/session";
3
3
  const SESSION_ROWS_PATH = `${MORLAY_SESSION_API_PREFIX}/rows`;
4
4
  const SESSION_EXPORT_PATH = `${MORLAY_SESSION_API_PREFIX}/export`;
@@ -6,5 +6,4 @@ const SESSION_IMPORT_PATH = `${MORLAY_SESSION_API_PREFIX}/import`;
6
6
  const SESSION_DELETE_PATH = `${MORLAY_SESSION_API_PREFIX}/delete`;
7
7
  const SESSION_GC_PATH = `${MORLAY_SESSION_API_PREFIX}/gc`;
8
8
  const SESSION_USAGE_PATH = `${MORLAY_SESSION_API_PREFIX}/usage`;
9
- //#endregion
10
9
  export { SESSION_ROWS_PATH as a, SESSION_IMPORT_PATH as i, SESSION_EXPORT_PATH as n, SESSION_USAGE_PATH as o, SESSION_GC_PATH as r, SESSION_DELETE_PATH as t };
@@ -1,5 +1,5 @@
1
1
  import { COUNTED_EVENT_TYPES, addActivityTotals, addTokenTotals, addTotals, emptyTotals, localDayKey } from "./usage.mjs";
2
- import { _ as sessionConflictRow, b as titleOfEventData, v as sessionInsertRow } from "./log-DYxdjJ-l.mjs";
2
+ import { _ as sessionConflictRow, b as titleOfEventData, v as sessionInsertRow } from "./log-CcWy46VE.mjs";
3
3
  import { SESSION_FORMAT_VERSION, SessionId, SessionLogOffset, SessionSeq } from "@deepseek-ai/dsh-session";
4
4
  import { randomUUID } from "node:crypto";
5
5
  import { and, desc, eq, gte, inArray, isNotNull, lt, notInArray, sql } from "drizzle-orm";
@@ -11,7 +11,7 @@ import { fileURLToPath } from "node:url";
11
11
  import { DatabaseSync } from "node:sqlite";
12
12
  import { drizzle } from "drizzle-orm/node-sqlite";
13
13
  import { migrate } from "drizzle-orm/node-sqlite/migrator";
14
- //#region src/write-guard.ts
14
+ // src/write-guard.ts
15
15
  var WriteGuard = class {
16
16
  headSeqs = /* @__PURE__ */ new Map();
17
17
  confirmHead(id, head) {
@@ -29,13 +29,11 @@ var WriteGuard = class {
29
29
  if (known !== storedHead) throw new Error(`session "${id}" was modified by another writer (stored head ${storedHead}, this instance last confirmed head ${known}); concurrent writers on one session are not supported`);
30
30
  }
31
31
  };
32
- //#endregion
33
- //#region src/adapters/types.ts
32
+ // src/adapters/types.ts
34
33
  function toProperty(name) {
35
34
  return name.replace(/_([a-z])/g, (_match, char) => char.toUpperCase());
36
35
  }
37
- //#endregion
38
- //#region src/adapters/to-sqlite.ts
36
+ // src/adapters/to-sqlite.ts
39
37
  function buildColumn$1(name, c, tables) {
40
38
  let col;
41
39
  switch (c.type) {
@@ -77,8 +75,7 @@ function toSqliteSchema(defs) {
77
75
  }
78
76
  return tables;
79
77
  }
80
- //#endregion
81
- //#region src/adapters/to-postgres.ts
78
+ // src/adapters/to-postgres.ts
82
79
  function buildColumn(name, c, tables) {
83
80
  let col;
84
81
  switch (c.type) {
@@ -125,8 +122,7 @@ function toPostgresSchema(defs, schemaName = "public") {
125
122
  }
126
123
  return tables;
127
124
  }
128
- //#endregion
129
- //#region src/entities/v3/persistence-state.ts
125
+ // src/entities/v3/persistence-state.ts
130
126
  const persistenceState = {
131
127
  name: "t_persistence_state",
132
128
  columns: {
@@ -141,8 +137,7 @@ const persistenceState = {
141
137
  },
142
138
  checks: { ck_persistence_state_singleton: { expression: "f_singleton = 1" } }
143
139
  };
144
- //#endregion
145
- //#region src/entities/v3/schema-meta.ts
140
+ // src/entities/v3/schema-meta.ts
146
141
  const schemaMeta = {
147
142
  name: "t_schema_meta",
148
143
  columns: {
@@ -156,8 +151,7 @@ const schemaMeta = {
156
151
  }
157
152
  }
158
153
  };
159
- //#endregion
160
- //#region src/entities/v3/sessions.ts
154
+ // src/entities/v3/sessions.ts
161
155
  const sessions = {
162
156
  name: "t_sessions",
163
157
  columns: {
@@ -209,8 +203,7 @@ const sessions = {
209
203
  f_title_seq: { type: "integer" }
210
204
  }
211
205
  };
212
- //#endregion
213
- //#region src/entities/v3/events.ts
206
+ // src/entities/v3/events.ts
214
207
  const events = {
215
208
  name: "t_events",
216
209
  columns: {
@@ -275,8 +268,7 @@ const events = {
275
268
  idx_events_action_id: { columns: ["f_action_id"] }
276
269
  }
277
270
  };
278
- //#endregion
279
- //#region src/entities/v3/session-events.ts
271
+ // src/entities/v3/session-events.ts
280
272
  const sessionEvents$1 = {
281
273
  name: "t_session_events",
282
274
  columns: {
@@ -311,8 +303,7 @@ const sessionEvents$1 = {
311
303
  uniques: { uq_session_events_session_sequence: { columns: ["f_session_id", "f_sequence"] } },
312
304
  indexes: { idx_session_events_event_id: { columns: ["f_event_id"] } }
313
305
  };
314
- //#endregion
315
- //#region src/entities/v3/storage-units.ts
306
+ // src/entities/v3/storage-units.ts
316
307
  const storageUnits = {
317
308
  name: "t_storage_units",
318
309
  columns: {
@@ -326,8 +317,7 @@ const storageUnits = {
326
317
  }
327
318
  }
328
319
  };
329
- //#endregion
330
- //#region src/entities/v3/workspaces.ts
320
+ // src/entities/v3/workspaces.ts
331
321
  const workspaces = {
332
322
  name: "t_workspaces",
333
323
  columns: {
@@ -363,8 +353,7 @@ const workspaces = {
363
353
  }
364
354
  }
365
355
  };
366
- //#endregion
367
- //#region src/entities/v3/workspace-sessions.ts
356
+ // src/entities/v3/workspace-sessions.ts
368
357
  const workspaceSessions = {
369
358
  name: "t_workspace_sessions",
370
359
  columns: {
@@ -393,8 +382,7 @@ const workspaceSessions = {
393
382
  uniques: { uq_workspace_sessions_workspace_session: { columns: ["f_workspace_id", "f_session_id"] } },
394
383
  indexes: { idx_workspace_sessions_session_id: { columns: ["f_session_id"] } }
395
384
  };
396
- //#endregion
397
- //#region src/entities/v3/workspace-state.ts
385
+ // src/entities/v3/workspace-state.ts
398
386
  const workspaceState = {
399
387
  name: "t_workspace_state",
400
388
  columns: {
@@ -411,8 +399,7 @@ const workspaceState = {
411
399
  },
412
400
  checks: { ck_workspace_state_singleton: { expression: "f_singleton = 1" } }
413
401
  };
414
- //#endregion
415
- //#region src/entities/v3/schedule-tasks.ts
402
+ // src/entities/v3/schedule-tasks.ts
416
403
  const scheduleTasks = {
417
404
  name: "t_schedule_tasks",
418
405
  columns: {
@@ -437,8 +424,7 @@ const scheduleTasks = {
437
424
  },
438
425
  indexes: { idx_schedule_tasks_session_id: { columns: ["f_session_id"] } }
439
426
  };
440
- //#endregion
441
- //#region src/entities/v3/session-projcache-rows.ts
427
+ // src/entities/v3/session-projcache-rows.ts
442
428
  const sessionProjcacheRows = {
443
429
  name: "t_session_projcache_row",
444
430
  columns: {
@@ -475,8 +461,7 @@ const sessionProjcacheRows = {
475
461
  uniques: { uq_session_projcache_row_session_key: { columns: ["f_session_id", "f_key"] } },
476
462
  indexes: { idx_session_projcache_row_key: { columns: ["f_key"] } }
477
463
  };
478
- //#endregion
479
- //#region src/entities/v3/event-usage.ts
464
+ // src/entities/v3/event-usage.ts
480
465
  const eventUsage = {
481
466
  name: "t_event_usage",
482
467
  columns: {
@@ -537,8 +522,7 @@ const eventUsage = {
537
522
  },
538
523
  indexes: { idx_event_usage_created_at: { columns: ["f_created_at"] } }
539
524
  };
540
- //#endregion
541
- //#region src/entities/v3/session-usage.ts
525
+ // src/entities/v3/session-usage.ts
542
526
  const sessionUsage = {
543
527
  name: "t_session_usage",
544
528
  columns: {
@@ -598,8 +582,7 @@ const sessionUsage = {
598
582
  "f_model"
599
583
  ] } }
600
584
  };
601
- //#endregion
602
- //#region src/entities/v3/session-counts.ts
585
+ // src/entities/v3/session-counts.ts
603
586
  const sessionCounts = {
604
587
  name: "t_session_counts",
605
588
  columns: {
@@ -639,8 +622,7 @@ const sessionCounts = {
639
622
  },
640
623
  uniques: { uq_session_counts_key: { columns: ["f_session_id", "f_day"] } }
641
624
  };
642
- //#endregion
643
- //#region src/entities/v3/index.ts
625
+ // src/entities/v3/index.ts
644
626
  const sqliteTableDefs = [
645
627
  persistenceState,
646
628
  schemaMeta,
@@ -674,8 +656,7 @@ const postgresTableDefs = [
674
656
  sessionCounts
675
657
  ];
676
658
  ({ ...sessionEvents$1 }), { ...sessionEvents$1.columns };
677
- //#endregion
678
- //#region src/schema.ts
659
+ // src/schema.ts
679
660
  const SCHEMA_VERSION = 3;
680
661
  const SESSION_PERSISTENCE_SQLITE_APPLICATION_ID = 1146308688;
681
662
  const EVENT_ENCODING = "json";
@@ -849,8 +830,7 @@ function eventDimensions(event) {
849
830
  };
850
831
  }
851
832
  }
852
- //#endregion
853
- //#region src/storage-takeover/repository.ts
833
+ // src/storage-takeover/repository.ts
854
834
  async function allRows(query) {
855
835
  const q = query;
856
836
  if (typeof q.all === "function") return q.all();
@@ -989,6 +969,14 @@ function createStorageRepository(host) {
989
969
  await runQuery(db.delete(tWorkspaces).where(eq(tWorkspaces.fWorkspaceId, id)));
990
970
  });
991
971
  },
972
+ async deleteWorkspaces(ids) {
973
+ if (ids.length === 0) return;
974
+ await host.writeAtomically(async () => {
975
+ const db = await dbx();
976
+ await runQuery(db.delete(tWorkspaceSessions).where(inArray(tWorkspaceSessions.fWorkspaceId, [...ids])));
977
+ await runQuery(db.delete(tWorkspaces).where(inArray(tWorkspaces.fWorkspaceId, [...ids])));
978
+ });
979
+ },
992
980
  async readWorkspaceState() {
993
981
  const db = await dbx();
994
982
  const row = await oneRow(db.select().from(tWorkspaceState).where(eq(tWorkspaceState.fSingleton, 1)));
@@ -1071,6 +1059,12 @@ function createStorageRepository(host) {
1071
1059
  await runQuery((await dbx()).delete(tScheduleTasks).where(eq(tScheduleTasks.fId, id)));
1072
1060
  });
1073
1061
  },
1062
+ async deleteScheduleTasks(ids) {
1063
+ if (ids.length === 0) return;
1064
+ await host.writeAtomically(async () => {
1065
+ await runQuery((await dbx()).delete(tScheduleTasks).where(inArray(tScheduleTasks.fId, [...ids])));
1066
+ });
1067
+ },
1074
1068
  async loadProjcache() {
1075
1069
  const db = await dbx();
1076
1070
  const rows = await allRows(db.select().from(tProjcacheRows));
@@ -1147,8 +1141,7 @@ function createStorageRepository(host) {
1147
1141
  }
1148
1142
  };
1149
1143
  }
1150
- //#endregion
1151
- //#region src/sqlite.ts
1144
+ // src/sqlite.ts
1152
1145
  const EVENT_REFERENCED_SQL = `EXISTS (SELECT 1 FROM t_session_events rb
1153
1146
  WHERE rb.f_event_id = %EVENT%)`;
1154
1147
  const EVENT_SUBAGENT_SQL = `EXISTS (SELECT 1 FROM t_session_events sb
@@ -1318,6 +1311,8 @@ async function baselineV2(db) {
1318
1311
  function configureDatabase(db, path, journalMode, busyTimeout) {
1319
1312
  db.exec("PRAGMA foreign_keys = ON");
1320
1313
  db.exec(`PRAGMA busy_timeout = ${busyTimeout}`);
1314
+ db.exec("PRAGMA cache_size = -65536");
1315
+ db.exec("PRAGMA mmap_size = 268435456");
1321
1316
  drizzle({ client: db }).transaction((tx) => {
1322
1317
  const { user_version: onDisk } = tx.get(sql`PRAGMA user_version`);
1323
1318
  const { application_id: applicationId } = tx.get(sql`PRAGMA application_id`);
@@ -1423,7 +1418,14 @@ var SqliteBackend = class SqliteBackend {
1423
1418
  return this.db.select().from(tSessions).where(eq(tSessions.fSessionId, id)).get();
1424
1419
  }
1425
1420
  async getEventRows(id, fromSequence) {
1426
- return (fromSequence === void 0 ? this.eventRows().where(eq(tSessionEvents.fSessionId, id)) : this.eventRows().where(and(eq(tSessionEvents.fSessionId, id), gte(tSessionEvents.fSequence, fromSequence)))).orderBy(tSessionEvents.fSequence).all();
1421
+ const sql = `select se.f_event_id as fEventId, se.f_sequence as fSequence, e.f_type as fType,
1422
+ e.f_kind as fKind, e.f_role as fRole, e.f_name as fName, e.f_action_id as fActionId,
1423
+ e.f_created_at as fCreatedAt, e.f_data as fData, se.f_surface_op as fSurfaceOp
1424
+ from t_session_events se join t_events e on se.f_event_id = e.f_event_id
1425
+ where se.f_session_id = ?${fromSequence === void 0 ? "" : " and se.f_sequence >= ?"}
1426
+ order by se.f_sequence`;
1427
+ const statement = this.db.$client.prepare(sql);
1428
+ return fromSequence === void 0 ? statement.all(id) : statement.all(id, fromSequence);
1427
1429
  }
1428
1430
  async getEventTypeAt(id, sequence) {
1429
1431
  return this.db.select({ fType: tEvents.fType }).from(tSessionEvents).innerJoin(tEvents, eq(tSessionEvents.fEventId, tEvents.fEventId)).where(and(eq(tSessionEvents.fSessionId, id), eq(tSessionEvents.fSequence, sequence))).get()?.fType;
@@ -1773,5 +1775,4 @@ var SqliteBackend = class SqliteBackend {
1773
1775
  }).from(tSessionEvents).innerJoin(tEvents, eq(tSessionEvents.fEventId, tEvents.fEventId));
1774
1776
  }
1775
1777
  };
1776
- //#endregion
1777
1778
  export { tWorkspaces as C, WriteGuard as E, tWorkspaceState as S, toPostgresSchema as T, tSessionProjcacheRows as _, EVENT_ENCODING as a, tStorageUnits as b, eventDimensions as c, tEvents as d, tPersistenceState as f, tSessionEvents as g, tSessionCounts as h, DEFAULT_BUSY_TIMEOUT_MS as i, eventKind as l, tSchemaMeta as m, openDatabase as n, SCHEMA_VERSION as o, tScheduleTasks as p, createStorageRepository as r, SESSION_PERSISTENCE_SQLITE_APPLICATION_ID as s, SqliteBackend as t, tEventUsage as u, tSessionUsage as v, postgresTableDefs as w, tWorkspaceSessions as x, tSessions as y };
@@ -1,7 +1,6 @@
1
- import { A as SessionRow, At as tWorkspaces, Ct as tSessionEvents, D as SessionCountBucket, Dt as tStorageUnits, E as EventRow, Et as tSessions, F as UsageAggregate, O as SessionListRowsPage, Ot as tWorkspaceSessions, Rt as StorageRepository, St as tSessionCounts, T as BackendTx, Tt as tSessionUsage, _t as tEventUsage, bt as tScheduleTasks, ct as DEFAULT_BUSY_TIMEOUT_MS, dt as EventRole, ft as JournalMode, gt as eventKind, ht as eventDimensions, j as SessionUsageBucket, jt as WriteGuard, k as SessionListRowsQuery, kt as tWorkspaceState, lt as EVENT_ENCODING, mt as SESSION_PERSISTENCE_SQLITE_APPLICATION_ID, pt as SCHEMA_VERSION, ut as EventKind, vt as tEvents, w as Backend, wt as tSessionProjcacheRows, xt as tSchemaMeta, yt as tPersistenceState } from "./log-C0q5svm7.mjs";
1
+ import { A as SessionRow, At as tWorkspaceState, Ct as tSessionCounts, D as SessionCountBucket, Dt as tSessions, E as EventRow, Et as tSessionUsage, F as UsageAggregate, Mt as WriteGuard, O as SessionListRowsPage, Ot as tStorageUnits, St as tSchemaMeta, T as BackendTx, Tt as tSessionProjcacheRows, _t as eventKind, bt as tPersistenceState, dt as EventKind, ft as EventRole, gt as eventDimensions, ht as SESSION_PERSISTENCE_SQLITE_APPLICATION_ID, j as SessionUsageBucket, jt as tWorkspaces, k as SessionListRowsQuery, kt as tWorkspaceSessions, lt as DEFAULT_BUSY_TIMEOUT_MS, mt as SCHEMA_VERSION, pt as JournalMode, ut as EVENT_ENCODING, vt as tEventUsage, w as Backend, wt as tSessionEvents, xt as tScheduleTasks, yt as tEvents, zt as StorageRepository } from "./log-TYgd8Xkr.mjs";
2
2
  import { SessionId } from "@deepseek-ai/dsh-session";
3
3
  import { DatabaseSync } from "node:sqlite";
4
- //#region src/sqlite.d.ts
5
4
  export declare function openDatabase(path: string, journalMode: JournalMode, busyTimeout?: number): Promise<DatabaseSync>;
6
5
  export interface SqliteBackendOptions {
7
6
  path: string;
@@ -55,5 +54,4 @@ export declare class SqliteBackend implements Backend {
55
54
  vacuum(): Promise<void>;
56
55
  private eventRows;
57
56
  }
58
- //#endregion
59
57
  export { DEFAULT_BUSY_TIMEOUT_MS, EVENT_ENCODING, EventKind, EventRole, type EventRow, JournalMode, SCHEMA_VERSION, SESSION_PERSISTENCE_SQLITE_APPLICATION_ID, type SessionRow, WriteGuard, eventDimensions, eventKind, tEventUsage, tEvents, tPersistenceState, tScheduleTasks, tSchemaMeta, tSessionCounts, tSessionEvents, tSessionProjcacheRows, tSessionUsage, tSessions, tStorageUnits, tWorkspaceSessions, tWorkspaceState, tWorkspaces };
package/dist/storage.mjs CHANGED
@@ -1 +1 @@
1
- export { i as DEFAULT_BUSY_TIMEOUT_MS, a as EVENT_ENCODING, o as SCHEMA_VERSION, s as SESSION_PERSISTENCE_SQLITE_APPLICATION_ID, t as SqliteBackend, E as WriteGuard, c as eventDimensions, l as eventKind, n as openDatabase, u as tEventUsage, d as tEvents, f as tPersistenceState, p as tScheduleTasks, m as tSchemaMeta, h as tSessionCounts, g as tSessionEvents, _ as tSessionProjcacheRows, v as tSessionUsage, y as tSessions, b as tStorageUnits, x as tWorkspaceSessions, S as tWorkspaceState, C as tWorkspaces } from "./sqlite-DjOnMRh2.mjs";
1
+ export { i as DEFAULT_BUSY_TIMEOUT_MS, a as EVENT_ENCODING, o as SCHEMA_VERSION, s as SESSION_PERSISTENCE_SQLITE_APPLICATION_ID, t as SqliteBackend, E as WriteGuard, c as eventDimensions, l as eventKind, n as openDatabase, u as tEventUsage, d as tEvents, f as tPersistenceState, p as tScheduleTasks, m as tSchemaMeta, h as tSessionCounts, g as tSessionEvents, _ as tSessionProjcacheRows, v as tSessionUsage, y as tSessions, b as tStorageUnits, x as tWorkspaceSessions, S as tWorkspaceState, C as tWorkspaces } from "./sqlite-BNRd1aqR.mjs";
@@ -1,8 +1,7 @@
1
- import { at as truncateLiveSession } from "./log-C0q5svm7.mjs";
1
+ import { ot as truncateLiveSession } from "./log-TYgd8Xkr.mjs";
2
2
  import { Session, SessionEvent, SessionHeader, SessionId } from "@deepseek-ai/dsh-session";
3
3
  import { SessionPersistence } from "@deepseek-ai/dsh-session-persistence";
4
4
  import { Context, Fiber } from "@deepseek-ai/cordis";
5
- //#region src/testing/contract.d.ts
6
5
  interface ContractBackendInstance {
7
6
  persistence: SessionPersistence;
8
7
  dispose: () => Promise<void>;
@@ -15,13 +14,10 @@ export declare function meta(id: string, cwd?: string): SessionHeader;
15
14
  export declare function oneTurnLog(): SessionEvent[];
16
15
  export declare function appendLog(session: Session, events: readonly SessionEvent[]): void;
17
16
  export declare function runPersistenceContract(name: string, make: () => Promise<ContractBackend>): void;
18
- //#endregion
19
- //#region src/testing/coordinator-contract.d.ts
20
17
  interface CoordinatorFixture {
21
18
  mount: (ctx: Context) => Promise<Fiber>;
22
19
  corruptTail?: (id: SessionId, cwd: string | undefined) => Promise<void>;
23
20
  cleanup: () => Promise<void>;
24
21
  }
25
22
  export declare function runCoordinatorContract(name: string, makeFixture: () => Promise<CoordinatorFixture>): void;
26
- //#endregion
27
23
  export { type ContractBackend, type CoordinatorFixture, truncateLiveSession };
package/dist/testing.mjs CHANGED
@@ -1,10 +1,10 @@
1
- import { a as truncateLiveSession } from "./branch-DbFW3rJX.mjs";
1
+ import { o as truncateLiveSession } from "./branch-CoPCRKer.mjs";
2
2
  import SessionStore, { SESSION_FORMAT_VERSION, SessionId, SessionLogOffset, SessionSeq } from "@deepseek-ai/dsh-session";
3
3
  import { SessionAlreadyExistsError, SessionAlreadyOwnedError, SessionFormatUnsupportedError, SessionHandleClosedError, SessionPersistenceNotFoundError, SessionReadOnlyError } from "@deepseek-ai/dsh-session-persistence";
4
4
  import { Context } from "@deepseek-ai/cordis";
5
5
  import { describe, expect, it } from "vitest";
6
6
  import { MessageId, createUserMessage, freezeMessage } from "@deepseek-ai/dsh-llm";
7
- //#region src/testing/contract.ts
7
+ // src/testing/contract.ts
8
8
  function meta(id, cwd) {
9
9
  return {
10
10
  version: SESSION_FORMAT_VERSION,
@@ -636,8 +636,7 @@ function runPersistenceContract(name, make) {
636
636
  });
637
637
  });
638
638
  }
639
- //#endregion
640
- //#region src/testing/coordinator-contract.ts
639
+ // src/testing/coordinator-contract.ts
641
640
  const WORK = "/w";
642
641
  function send(session, events) {
643
642
  appendLog(session, events);
@@ -1048,5 +1047,4 @@ function runCoordinatorContract(name, makeFixture) {
1048
1047
  });
1049
1048
  });
1050
1049
  }
1051
- //#endregion
1052
1050
  export { appendLog, meta, oneTurnLog, runCoordinatorContract, runPersistenceContract, truncateLiveSession };
package/dist/usage.d.mts CHANGED
@@ -1,2 +1,2 @@
1
- import { B as UsageTotals, F as UsageAggregate, G as emptyTotals, H as addActivityTotals, I as UsageBucket, J as resolveUsageSince, K as localDayKey, L as UsageRangeKey, Lt as SESSION_USAGE_PATH, M as COUNTED_EVENT_TYPES, N as SessionUsageReport, P as UsageActivityTotals, R as UsageSessionRow, U as addTokenTotals, V as addActivityCount, W as addTotals, q as registerSessionUsage, z as UsageTokenTotals } from "./log-C0q5svm7.mjs";
1
+ import { B as UsageTotals, F as UsageAggregate, G as emptyTotals, H as addActivityTotals, I as UsageBucket, J as resolveUsageSince, K as localDayKey, L as UsageRangeKey, M as COUNTED_EVENT_TYPES, N as SessionUsageReport, P as UsageActivityTotals, R as UsageSessionRow, Rt as SESSION_USAGE_PATH, U as addTokenTotals, V as addActivityCount, W as addTotals, q as registerSessionUsage, z as UsageTokenTotals } from "./log-TYgd8Xkr.mjs";
2
2
  export { COUNTED_EVENT_TYPES, SESSION_USAGE_PATH, SessionUsageReport, UsageActivityTotals, UsageAggregate, UsageBucket, UsageRangeKey, UsageSessionRow, UsageTokenTotals, UsageTotals, addActivityCount, addActivityTotals, addTokenTotals, addTotals, emptyTotals, localDayKey, registerSessionUsage, resolveUsageSince };
package/dist/usage.mjs CHANGED
@@ -1,5 +1,5 @@
1
- import { o as SESSION_USAGE_PATH } from "./routes-DGBlBE_I.mjs";
2
- //#region src/usage.ts
1
+ import { o as SESSION_USAGE_PATH } from "./routes-hZjTcX_R.mjs";
2
+ // src/usage.ts
3
3
  const ROLLING_DAYS = {
4
4
  "7d": 7,
5
5
  "30d": 30,
@@ -132,5 +132,4 @@ function registerSessionUsage(ctx, persistence) {
132
132
  }), `session-rdb: ${SESSION_USAGE_PATH} route`);
133
133
  });
134
134
  }
135
- //#endregion
136
135
  export { COUNTED_EVENT_TYPES, SESSION_USAGE_PATH, addActivityCount, addActivityTotals, addTokenTotals, addTotals, emptyTotals, localDayKey, registerSessionUsage, resolveUsageSince };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@morlay/session-rdb",
3
- "version": "0.1.9",
3
+ "version": "0.1.10-alpha.0",
4
4
  "description": "RDB durable session backend for DeepSeek Harness: implements both session-persistence and session-branch (rewind / retry / fork) providers.",
5
5
  "keywords": [
6
6
  "branch",
@@ -65,41 +65,41 @@
65
65
  "./locale/*.json": "./locale/*.json"
66
66
  },
67
67
  "dependencies": {
68
- "@morlay/session-branch": "0.1.9",
68
+ "@morlay/session-branch": "0.1.10-alpha.0",
69
69
  "drizzle-orm": "^1.0.0-rc.5-ab785fc",
70
70
  "fflate": "^0.8.2",
71
71
  "pg": "^8.23.0"
72
72
  },
73
73
  "devDependencies": {
74
- "@deepseek-ai/cosmokit": "^1.8.6",
75
- "@deepseek-ai/dsh-agent-loop-testkit": "^0.2.1-rc.1",
76
- "@deepseek-ai/dsh-compaction": "^0.2.1-rc.1",
77
- "@deepseek-ai/dsh-session-turn-outline": "^0.2.1-rc.1",
78
- "@deepseek-ai/dsh-storage": "^0.2.1-rc.1",
79
- "@deepseek-ai/dsh-storage-domain": "^0.2.1-rc.1",
80
- "@deepseek-ai/dsh-token-meter": "^0.2.1-rc.1",
74
+ "@deepseek-ai/cosmokit": "^1.8.7-alpha.1",
75
+ "@deepseek-ai/dsh-agent-loop-testkit": "^0.2.2-alpha.2",
76
+ "@deepseek-ai/dsh-compaction": "^0.2.2-alpha.2",
77
+ "@deepseek-ai/dsh-session-turn-outline": "^0.2.2-alpha.2",
78
+ "@deepseek-ai/dsh-storage": "^0.2.2-alpha.2",
79
+ "@deepseek-ai/dsh-storage-domain": "^0.2.2-alpha.2",
80
+ "@deepseek-ai/dsh-token-meter": "^0.2.2-alpha.2",
81
81
  "@types/pg": "^8.23.1",
82
82
  "drizzle-kit": "1.0.0-rc.5-ab785fc"
83
83
  },
84
84
  "peerDependencies": {
85
- "@deepseek-ai/cordis": "^4.0.5",
86
- "@deepseek-ai/cosmokit": "^1.8.6",
87
- "@deepseek-ai/dsh-llm": "^0.2.1-rc.1",
88
- "@deepseek-ai/dsh-schedule": "^0.2.1-rc.1",
89
- "@deepseek-ai/dsh-scope": "^0.2.1-rc.1",
90
- "@deepseek-ai/dsh-session": "^0.2.1-rc.1",
91
- "@deepseek-ai/dsh-session-format": "^0.2.1-rc.1",
92
- "@deepseek-ai/dsh-session-format-catalog": "^0.2.1-rc.1",
93
- "@deepseek-ai/dsh-session-persistence": "^0.2.1-rc.1",
94
- "@deepseek-ai/dsh-session-projection": "^0.2.1-rc.1",
95
- "@deepseek-ai/dsh-session-projection-cache": "^0.2.1-rc.1",
96
- "@deepseek-ai/dsh-session-query": "^0.2.1-rc.1",
97
- "@deepseek-ai/dsh-storage": "^0.2.1-rc.1",
98
- "@deepseek-ai/dsh-workspace": "^0.2.1-rc.1",
99
- "@deepseek-ai/schemastery": "^3.18.5"
85
+ "@deepseek-ai/cordis": "^4.0.6-alpha.1",
86
+ "@deepseek-ai/cosmokit": "^1.8.7-alpha.1",
87
+ "@deepseek-ai/dsh-llm": "^0.2.2-alpha.2",
88
+ "@deepseek-ai/dsh-schedule": "^0.2.2-alpha.2",
89
+ "@deepseek-ai/dsh-scope": "^0.2.2-alpha.2",
90
+ "@deepseek-ai/dsh-session": "^0.2.2-alpha.2",
91
+ "@deepseek-ai/dsh-session-format": "^0.2.2-alpha.2",
92
+ "@deepseek-ai/dsh-session-format-catalog": "^0.2.2-alpha.2",
93
+ "@deepseek-ai/dsh-session-persistence": "^0.2.2-alpha.2",
94
+ "@deepseek-ai/dsh-session-projection": "^0.2.2-alpha.2",
95
+ "@deepseek-ai/dsh-session-projection-cache": "^0.2.2-alpha.2",
96
+ "@deepseek-ai/dsh-session-query": "^0.2.2-alpha.2",
97
+ "@deepseek-ai/dsh-storage": "^0.2.2-alpha.2",
98
+ "@deepseek-ai/dsh-workspace": "^0.2.2-alpha.2",
99
+ "@deepseek-ai/schemastery": "^3.18.6-alpha.1"
100
100
  },
101
101
  "engines": {
102
- "dsh": ">= 0.2.1-rc.1 < 0.2.2"
102
+ "dsh": ">= 0.2.2-alpha.2 < 0.2.3"
103
103
  },
104
104
  "scripts": {
105
105
  "build": "pnpm exec tsdown",