@axiom-lattice/local-stores 3.1.12 → 3.1.13

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.mjs CHANGED
@@ -808,6 +808,8 @@ CREATE UNIQUE INDEX IF NOT EXISTS uq_lt_project_room_messages_idempotency
808
808
  ON lt_project_room_messages (tenant_id, room_id, idempotency_key) WHERE idempotency_key IS NOT NULL;
809
809
  CREATE INDEX IF NOT EXISTS idx_lt_project_room_messages_room_created
810
810
  ON lt_project_room_messages (tenant_id, room_id, created_at DESC, id DESC);
811
+ CREATE INDEX IF NOT EXISTS idx_lt_project_room_messages_chat_created
812
+ ON lt_project_room_messages (created_at) WHERE source IN ('user','agent');
811
813
  CREATE TABLE IF NOT EXISTS lt_project_room_read_states (
812
814
  tenant_id TEXT NOT NULL, room_id TEXT NOT NULL, user_id TEXT NOT NULL,
813
815
  last_read_at TEXT NOT NULL, updated_at TEXT NOT NULL,
@@ -964,6 +966,25 @@ function ensureProjectRoomSchema(db) {
964
966
  }
965
967
  }
966
968
  validateProjectRoomSchema(db);
969
+ const hasRetentionIndex = rows(db, "SELECT name FROM sqlite_master WHERE type = 'index' AND name = 'idx_lt_project_room_messages_chat_created'").length > 0;
970
+ if (!hasRetentionIndex) {
971
+ raw.exec("BEGIN IMMEDIATE");
972
+ try {
973
+ raw.exec("CREATE INDEX IF NOT EXISTS idx_lt_project_room_messages_chat_created ON lt_project_room_messages (created_at) WHERE source IN ('user','agent');");
974
+ raw.run(
975
+ "INSERT INTO lattice_schema_migrations (store_name, name, version, applied_at) VALUES (?, ?, ?, ?)",
976
+ ["project_room", "add_project_room_message_retention_index", 3, (/* @__PURE__ */ new Date()).toISOString()]
977
+ );
978
+ raw.exec("COMMIT");
979
+ db.save();
980
+ } catch (error) {
981
+ try {
982
+ raw.exec("ROLLBACK");
983
+ } catch {
984
+ }
985
+ throw error;
986
+ }
987
+ }
967
988
  marked[schemaMarker] = true;
968
989
  return;
969
990
  }
@@ -1430,6 +1451,26 @@ var LocalProjectRoomMessageStore = class {
1430
1451
  return !(record(parsed) && parsed.type === "human" && parsed.userId === input.excludeAuthorUserId);
1431
1452
  }).length;
1432
1453
  }
1454
+ /**
1455
+ * Deletes one bounded batch of chat messages older than a horizon.
1456
+ *
1457
+ * @param input Exclusive upper time bound and requested batch size (clamped to 1..1000).
1458
+ * @returns The number of deleted rows.
1459
+ * @remarks Only `source: "user" | "agent"` rows are eligible; task/system events are retained.
1460
+ */
1461
+ async deleteChatOlderThan(input) {
1462
+ if (!validDate(input.before)) throw new RangeError("Project room message retention horizon date is invalid");
1463
+ const limit = Math.min(1e3, Math.max(1, Math.trunc(Number.isFinite(input.limit) ? input.limit : 1)));
1464
+ return transaction(this.db, () => write(
1465
+ this.db,
1466
+ `DELETE FROM lt_project_room_messages WHERE rowid IN (
1467
+ SELECT rowid FROM lt_project_room_messages
1468
+ WHERE created_at < ? AND source IN ('user','agent')
1469
+ ORDER BY created_at ASC LIMIT ?
1470
+ )`,
1471
+ [input.before.toISOString(), limit]
1472
+ ));
1473
+ }
1433
1474
  insert(input, idempotent) {
1434
1475
  return transaction(this.db, () => {
1435
1476
  if (idempotent && input.idempotencyKey) {