@axiom-lattice/pg-stores 3.1.11 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axiom-lattice/pg-stores",
3
- "version": "3.1.11",
3
+ "version": "3.1.13",
4
4
  "description": "PG stores implementation for Axiom Lattice framework",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -25,8 +25,8 @@
25
25
  "@langchain/core": "1.1.30",
26
26
  "pg": "^8.16.3",
27
27
  "uuid": "^9.0.1",
28
- "@axiom-lattice/core": "4.2.11",
29
- "@axiom-lattice/protocols": "4.2.2"
28
+ "@axiom-lattice/core": "4.2.13",
29
+ "@axiom-lattice/protocols": "4.2.3"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@types/jest": "^29.5.14",
@@ -114,4 +114,29 @@ describe("PostgreSQLProjectRoomMessageStore", () => {
114
114
  );
115
115
  warn.mockRestore();
116
116
  });
117
+
118
+ it("deletes one bounded batch of old chat messages and returns the row count", async () => {
119
+ query.mockResolvedValueOnce({ rows: [], rowCount: 3 });
120
+ const store = new PostgreSQLProjectRoomMessageStore({ pool });
121
+
122
+ await expect(store.deleteChatOlderThan({ before: now, limit: 500 })).resolves.toBe(3);
123
+
124
+ const [sql, params] = query.mock.calls[0] as [string, unknown[]];
125
+ expect(sql).toContain("DELETE FROM lattice_project_room_messages");
126
+ expect(sql).toContain("ctid IN");
127
+ expect(sql).toContain("source IN ('user','agent')");
128
+ expect(sql).toContain("ORDER BY created_at ASC");
129
+ expect(params).toEqual([now, 500]);
130
+ });
131
+
132
+ it("rejects an invalid retention horizon and clamps the delete batch", async () => {
133
+ const store = new PostgreSQLProjectRoomMessageStore({ pool });
134
+
135
+ await expect(store.deleteChatOlderThan({ before: new Date("invalid"), limit: 10 }))
136
+ .rejects.toBeInstanceOf(RangeError);
137
+
138
+ query.mockResolvedValueOnce({ rows: [], rowCount: 0 });
139
+ await store.deleteChatOlderThan({ before: now, limit: 99_999 });
140
+ expect(query.mock.calls[0][1]).toEqual([now, 1_000]);
141
+ });
117
142
  });
@@ -87,6 +87,7 @@ import { createAgentWebAppsTable } from "./migrations/agent_web_apps_migration";
87
87
  import { addAgentWebAppsIdentity } from "./migrations/add_agent_web_apps_identity_migration";
88
88
  import { createCapabilityBundlesTable } from "./migrations/capability_bundle_migration";
89
89
  import { createProjectRoomReadStates, createProjectRoomTables } from "./migrations/project_room_migration";
90
+ import { addProjectRoomMessageRetentionIndex } from "./migrations/project_room_message_retention_migration";
90
91
  import { createUserPushSubscriptions } from "./migrations/user_push_subscription_migration";
91
92
  import { PostgreSQLCapabilityBundleStore } from "./stores/PostgreSQLCapabilityBundleStore";
92
93
  import { PostgreSQLProjectRoomStore } from "./stores/PostgreSQLProjectRoomStore";
@@ -167,6 +168,7 @@ export async function createPgStoreConfig(connectionString: string) {
167
168
  mm.register(addAgentWebAppsIdentity); // v176
168
169
  mm.register(createProjectRoomReadStates); // v177
169
170
  mm.register(createUserPushSubscriptions); // v178
171
+ mm.register(addProjectRoomMessageRetentionIndex); // v179
170
172
 
171
173
  await mm.migrate();
172
174
 
@@ -0,0 +1,23 @@
1
+ import type { Migration } from "./migration";
2
+
3
+ /**
4
+ * Adds a partial `created_at` index for the room chat-message retention sweep.
5
+ *
6
+ * The maintenance job deletes `source IN ('user','agent')` rows older than a horizon;
7
+ * the partial index keeps that global (cross-room) scan bounded without affecting
8
+ * task/system event rows.
9
+ */
10
+ export const addProjectRoomMessageRetentionIndex: Migration = {
11
+ version: 179,
12
+ name: "add_project_room_message_retention_index",
13
+ up: async (client) => {
14
+ await client.query(`
15
+ CREATE INDEX IF NOT EXISTS idx_lattice_project_room_messages_chat_created
16
+ ON lattice_project_room_messages (created_at)
17
+ WHERE source IN ('user', 'agent')
18
+ `);
19
+ },
20
+ down: async (client) => {
21
+ await client.query("DROP INDEX IF EXISTS idx_lattice_project_room_messages_chat_created");
22
+ },
23
+ };
@@ -205,6 +205,31 @@ export class PostgreSQLProjectRoomMessageStore implements ProjectRoomMessageStor
205
205
  return result.rows[0]?.count ?? 0;
206
206
  }
207
207
 
208
+ /**
209
+ * Deletes one bounded batch of chat messages older than a horizon.
210
+ *
211
+ * @param input Exclusive upper time bound and requested batch size (clamped to 1..1000).
212
+ * @returns The number of deleted rows.
213
+ * @remarks Only `source IN ('user','agent')` rows are eligible; task/system events are retained.
214
+ */
215
+ async deleteChatOlderThan(input: { before: Date; limit: number }): Promise<number> {
216
+ if (!(input.before instanceof Date) || Number.isNaN(input.before.getTime())) {
217
+ throw new RangeError("Project room message retention horizon date is invalid");
218
+ }
219
+ const limit = Math.min(1_000, Math.max(1, Math.trunc(Number.isFinite(input.limit) ? input.limit : 1)));
220
+ const result = await this.pool.query(
221
+ `DELETE FROM lattice_project_room_messages
222
+ WHERE ctid IN (
223
+ SELECT ctid FROM lattice_project_room_messages
224
+ WHERE created_at < $1 AND source IN ('user','agent')
225
+ ORDER BY created_at ASC
226
+ LIMIT $2
227
+ )`,
228
+ [input.before, limit],
229
+ );
230
+ return result.rowCount ?? 0;
231
+ }
232
+
208
233
  private parameters(input: Omit<ProjectRoomMessage, "createdAt">): unknown[] {
209
234
  return [input.id, input.tenantId, input.workspaceId, input.projectId, input.roomId, JSON.stringify(input.author), JSON.stringify(input.content),
210
235
  JSON.stringify(input.mentions), input.replyToMessageId ?? null, input.source, input.sourceId ?? null, input.idempotencyKey ?? null];