@axiom-lattice/local-stores 3.2.12 → 3.3.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axiom-lattice/local-stores",
3
- "version": "3.2.12",
3
+ "version": "3.3.0",
4
4
  "description": "Local SQLite-based stores for Axiom Lattice framework",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -25,8 +25,8 @@
25
25
  "@types/sql.js": "^1.4.11",
26
26
  "sql.js": "^1.14.1",
27
27
  "uuid": "^14.0.1",
28
- "@axiom-lattice/core": "8.0.2",
29
- "@axiom-lattice/protocols": "4.10.0"
28
+ "@axiom-lattice/core": "9.0.0",
29
+ "@axiom-lattice/protocols": "5.0.0"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@types/jest": "^29.5.14",
@@ -0,0 +1,151 @@
1
+ import { afterEach, beforeEach, describe, expect, it, jest } from "@jest/globals";
2
+ import { tmpdir } from "os";
3
+ import path from "path";
4
+ import { closeDatabase, initDatabase } from "../database";
5
+ import { LocalStore } from "../stores/LocalStore";
6
+
7
+ const nextDbPath = () =>
8
+ path.join(tmpdir(), `lt-store-test-${Date.now()}-${Math.random().toString(36).slice(2)}.db`);
9
+
10
+ describe("LocalStore", () => {
11
+ const tenant = "tenant-1";
12
+ let store: LocalStore;
13
+
14
+ beforeEach(async () => {
15
+ jest.useFakeTimers();
16
+ jest.setSystemTime(new Date("2026-01-01T00:00:00.000Z"));
17
+ const db = await initDatabase({ dbPath: nextDbPath() });
18
+ store = new LocalStore(db);
19
+ });
20
+
21
+ afterEach(() => {
22
+ jest.useRealTimers();
23
+ closeDatabase();
24
+ });
25
+
26
+ it("round-trips items and isolates tenants", async () => {
27
+ await store.put(tenant, { namespace: ["a", "b"], key: "one", value: { n: 1 } });
28
+
29
+ expect(await store.get(tenant, ["a", "b"], "one")).toMatchObject({
30
+ namespace: ["a", "b"],
31
+ key: "one",
32
+ value: { n: 1 },
33
+ });
34
+ expect(await store.get("tenant-2", ["a", "b"], "one")).toBeNull();
35
+ expect(await store.delete(tenant, ["a", "b"], "one")).toBe(true);
36
+ expect(await store.get(tenant, ["a", "b"], "one")).toBeNull();
37
+ });
38
+
39
+ it("searches by namespace prefix, deep filter, and query", async () => {
40
+ await store.put(tenant, {
41
+ namespace: ["app", "a"], key: "one",
42
+ value: { message: "Hello World", nested: { tag: "x" }, list: [1, 2] },
43
+ });
44
+ await store.put(tenant, {
45
+ namespace: ["app", "b"], key: "two",
46
+ value: { message: "other", nested: { tag: "y" }, list: [1, 2] },
47
+ });
48
+ await store.put(tenant, {
49
+ namespace: ["other"], key: "three", value: { message: "Hello other" },
50
+ });
51
+
52
+ expect((await store.search(tenant, { namespacePrefix: ["app"] })).map((item) => item.key))
53
+ .toEqual(["one", "two"]);
54
+ expect((await store.search(tenant, { filter: { nested: { tag: "x" } } })).map((item) => item.key))
55
+ .toEqual(["one"]);
56
+ expect((await store.search(tenant, { query: "HELLO" })).map((item) => item.key))
57
+ .toEqual(["one", "three"]);
58
+ expect((await store.search(tenant, { filter: { list: [2] } })).map((item) => item.key).sort())
59
+ .toEqual(["one", "two"]);
60
+ });
61
+
62
+ it("treats percent and underscore literally in query", async () => {
63
+ await store.put(tenant, { namespace: ["q"], key: "percent", value: { text: "100% done" } });
64
+ await store.put(tenant, { namespace: ["q"], key: "underscore", value: { text: "under_score" } });
65
+
66
+ expect((await store.search(tenant, { query: "%" })).map((item) => item.key))
67
+ .toEqual(["percent"]);
68
+ expect((await store.search(tenant, { query: "_" })).map((item) => item.key))
69
+ .toEqual(["underscore"]);
70
+ });
71
+
72
+ it("upserts per tenant and does not leak across tenants", async () => {
73
+ await store.put(tenant, { namespace: ["shared"], key: "one", value: { owner: "a" } });
74
+ await store.put("tenant-2", { namespace: ["shared"], key: "one", value: { owner: "b" } });
75
+
76
+ expect((await store.get(tenant, ["shared"], "one"))?.value.owner).toBe("a");
77
+ expect((await store.get("tenant-2", ["shared"], "one"))?.value.owner).toBe("b");
78
+ expect((await store.search(tenant, { namespacePrefix: ["shared"] })).map((item) => item.value.owner))
79
+ .toEqual(["a"]);
80
+
81
+ expect(await store.delete(tenant, ["shared"], "one")).toBe(true);
82
+ expect((await store.get("tenant-2", ["shared"], "one"))?.value.owner).toBe("b");
83
+ });
84
+
85
+ it("clears a previous TTL when upserting without one", async () => {
86
+ await store.put(tenant, {
87
+ namespace: ["ttl"], key: "reset", value: { v: 1 }, ttlMinutes: 1,
88
+ });
89
+ await store.put(tenant, { namespace: ["ttl"], key: "reset", value: { v: 2 } });
90
+ jest.setSystemTime(new Date("2026-01-01T00:02:00.000Z"));
91
+
92
+ expect((await store.get(tenant, ["ttl"], "reset"))?.value.v).toBe(2);
93
+ });
94
+
95
+ it("rejects identity collisions and invalid TTL/pagination at the store", async () => {
96
+ await expect(store.put(tenant, { namespace: ["a\u001fb"], key: "k", value: {} }))
97
+ .rejects.toThrow("reserved separator");
98
+ await expect(store.put(tenant, { namespace: ["a"], key: "b\u001fc", value: {} }))
99
+ .rejects.toThrow("reserved separator");
100
+ await expect(store.put(tenant, {
101
+ namespace: ["a"], key: "k", value: {}, ttlMinutes: 0,
102
+ })).rejects.toThrow("ttlMinutes");
103
+ await expect(store.search(tenant, { limit: -1 })).rejects.toThrow("limit/offset");
104
+ });
105
+
106
+ it("uses namespace and key as stable tie-breakers", async () => {
107
+ jest.setSystemTime(new Date("2026-01-01T00:00:00.000Z"));
108
+ await store.put(tenant, { namespace: ["b"], key: "one", value: {} });
109
+ await store.put(tenant, { namespace: ["a"], key: "two", value: {} });
110
+ await store.put(tenant, { namespace: ["a"], key: "one", value: {} });
111
+
112
+ expect((await store.search(tenant, {})).map((item) => `${item.namespace[0]}:${item.key}`))
113
+ .toEqual(["a:one", "a:two", "b:one"]);
114
+ });
115
+
116
+ it("paginates in updated-at descending order", async () => {
117
+ for (const [index, key] of ["one", "two", "three"].entries()) {
118
+ jest.setSystemTime(new Date(Date.UTC(2026, 0, 1, 0, 0, index)));
119
+ await store.put(tenant, { namespace: ["app"], key, value: { index } });
120
+ }
121
+
122
+ expect((await store.search(tenant, { limit: 2 })).map((item) => item.key))
123
+ .toEqual(["three", "two"]);
124
+ expect((await store.search(tenant, { limit: 2, offset: 1 })).map((item) => item.key))
125
+ .toEqual(["two", "one"]);
126
+ });
127
+
128
+ it("hides expired items and hard-deletes them", async () => {
129
+ await store.put(tenant, {
130
+ namespace: ["ttl"], key: "short", value: { ok: true }, ttlMinutes: 1,
131
+ });
132
+ jest.setSystemTime(new Date("2026-01-01T00:01:01.000Z"));
133
+
134
+ expect(await store.get(tenant, ["ttl"], "short")).toBeNull();
135
+ expect(await store.search(tenant, { namespacePrefix: ["ttl"] })).toEqual([]);
136
+ expect(await store.listNamespaces(tenant)).toEqual([]);
137
+
138
+ expect(await store.deleteExpired(new Date("2026-01-01T00:02:00.000Z"), 10)).toBe(1);
139
+ expect(await store.deleteExpired(new Date("2026-01-01T00:02:00.000Z"), 10)).toBe(0);
140
+ });
141
+
142
+ it("lists distinct sorted namespaces with limit", async () => {
143
+ await store.put(tenant, { namespace: ["b"], key: "1", value: {} });
144
+ await store.put(tenant, { namespace: ["a", "x"], key: "2", value: {} });
145
+ await store.put(tenant, { namespace: ["a", "x"], key: "3", value: {} });
146
+
147
+ expect(await store.listNamespaces(tenant)).toEqual([["a", "x"], ["b"]]);
148
+ expect(await store.listNamespaces(tenant, [], 1)).toEqual([["a", "x"]]);
149
+ expect(await store.listNamespaces(tenant, ["a"])).toEqual([["a", "x"]]);
150
+ });
151
+ });
@@ -60,6 +60,7 @@ describe("LocalThreadMessageQueueStore scope isolation", () => {
60
60
  threadId: "legacy",
61
61
  workspaceId: null,
62
62
  projectId: null,
63
+ hasPending: true,
63
64
  }]);
64
65
  expect(await store.getPendingMessages("legacy", scope)).toHaveLength(1);
65
66
  await store.markProcessing(message.id, undefined, scope);
@@ -0,0 +1,51 @@
1
+ import { afterEach, beforeEach, describe, expect, it } from "@jest/globals";
2
+ import { tmpdir } from "os";
3
+ import path from "path";
4
+ import { closeDatabase, initDatabase } from "../database";
5
+ import { LocalThreadStore } from "../stores/LocalThreadStore";
6
+
7
+ const nextDbPath = () =>
8
+ path.join(tmpdir(), `lt-thread-test-${Date.now()}-${Math.random().toString(36).slice(2)}.db`);
9
+
10
+ describe("LocalThreadStore TTL", () => {
11
+ let store: LocalThreadStore;
12
+
13
+ beforeEach(async () => {
14
+ const db = await initDatabase({ dbPath: nextDbPath() });
15
+ store = new LocalThreadStore(db);
16
+ });
17
+
18
+ afterEach(() => {
19
+ closeDatabase();
20
+ });
21
+
22
+ it("round-trips expiresAt through create, get, and update", async () => {
23
+ const expiresAt = new Date("2026-01-01T00:00:00.000Z");
24
+ await store.createThread("t1", "a1", "th1", { expiresAt });
25
+
26
+ expect((await store.getThreadById("t1", "th1"))?.expiresAt).toEqual(expiresAt);
27
+
28
+ const next = new Date("2026-02-01T00:00:00.000Z");
29
+ await store.updateThread("t1", "th1", { expiresAt: next });
30
+ expect((await store.getThreadById("t1", "th1"))?.expiresAt).toEqual(next);
31
+ });
32
+
33
+ it("lists expired threads across tenants, oldest first, up to the limit", async () => {
34
+ const now = new Date("2026-01-01T00:00:00.000Z");
35
+ await store.createThread("t1", "a1", "later", {
36
+ expiresAt: new Date("2025-12-31T23:00:00.000Z"),
37
+ });
38
+ await store.createThread("t2", "a2", "earlier", {
39
+ expiresAt: new Date("2025-12-31T22:00:00.000Z"),
40
+ });
41
+ await store.createThread("t1", "a1", "future", {
42
+ expiresAt: new Date("2026-01-01T01:00:00.000Z"),
43
+ });
44
+ await store.createThread("t1", "a1", "never", {});
45
+
46
+ expect((await store.listExpiredThreads(now, 10)).map((thread) => thread.id))
47
+ .toEqual(["earlier", "later"]);
48
+ expect((await store.listExpiredThreads(now, 1)).map((thread) => thread.id))
49
+ .toEqual(["earlier"]);
50
+ });
51
+ });
@@ -17,6 +17,7 @@
17
17
  import { initDatabase } from "./database";
18
18
  import { SqliteSaver } from "@langchain/langgraph-checkpoint-sqlite";
19
19
  import { LocalThreadStore } from "./stores/LocalThreadStore";
20
+ import { LocalStore } from "./stores/LocalStore";
20
21
  import { LocalAssistantStore } from "./stores/LocalAssistantStore";
21
22
  import { LocalWorkspaceStore } from "./stores/LocalWorkspaceStore";
22
23
  import { LocalProjectStore } from "./stores/LocalProjectStore";
@@ -70,6 +71,7 @@ export async function createLocalStoreConfig(options: LocalStoreConfigOptions =
70
71
 
71
72
  return {
72
73
  thread: new LocalThreadStore(db),
74
+ store: new LocalStore(db),
73
75
  assistant: new LocalAssistantStore(db),
74
76
  workspace: new LocalWorkspaceStore(db),
75
77
  project: new LocalProjectStore(db),
package/src/index.ts CHANGED
@@ -28,6 +28,7 @@ export { createLocalStoreConfig } from "./createLocalStoreConfig";
28
28
  export type { LocalStoreConfigOptions } from "./createLocalStoreConfig";
29
29
 
30
30
  export { LocalThreadStore } from "./stores/LocalThreadStore";
31
+ export { LocalStore } from "./stores/LocalStore";
31
32
  export { LocalAssistantStore } from "./stores/LocalAssistantStore";
32
33
  export { LocalWorkspaceStore } from "./stores/LocalWorkspaceStore";
33
34
  export { LocalProjectStore } from "./stores/LocalProjectStore";
@@ -11,6 +11,7 @@ import type {
11
11
  ModelProviderWireProtocol,
12
12
  ModelProviderApiStyle,
13
13
  ModelProviderLlmProvider,
14
+ ModelKind,
14
15
  CreateModelProviderRequest,
15
16
  UpdateModelProviderRequest,
16
17
  ProviderModelInput,
@@ -52,6 +53,7 @@ CREATE TABLE IF NOT EXISTS lt_model_provider_models (
52
53
  upstream_created TEXT,
53
54
  enabled INTEGER NOT NULL DEFAULT 1,
54
55
  stale INTEGER NOT NULL DEFAULT 0,
56
+ kind TEXT NOT NULL DEFAULT 'chat',
55
57
  raw TEXT,
56
58
  discovered_at TEXT NOT NULL,
57
59
  PRIMARY KEY (tenant_id, provider_id, model_id)
@@ -90,6 +92,7 @@ interface ModelRow {
90
92
  upstream_created: string | null;
91
93
  enabled: number;
92
94
  stale: number;
95
+ kind: string;
93
96
  discovered_at: string;
94
97
  }
95
98
 
@@ -118,6 +121,11 @@ export class LocalModelProviderStore implements ModelProviderStore {
118
121
  "stale",
119
122
  "stale INTEGER NOT NULL DEFAULT 0"
120
123
  );
124
+ this.ensureColumn(
125
+ "lt_model_provider_models",
126
+ "kind",
127
+ "kind TEXT NOT NULL DEFAULT 'chat'"
128
+ );
121
129
  }
122
130
 
123
131
  /** Add a column if it does not exist (SQLite version compatible). */
@@ -372,7 +380,7 @@ export class LocalModelProviderStore implements ModelProviderStore {
372
380
  const current = new Map(
373
381
  (await this.listModels(tenantId, providerId)).map((model) => [
374
382
  model.modelId,
375
- model.enabled,
383
+ model,
376
384
  ])
377
385
  );
378
386
 
@@ -389,8 +397,8 @@ export class LocalModelProviderStore implements ModelProviderStore {
389
397
  .prepare(
390
398
  `INSERT INTO lt_model_provider_models (
391
399
  id, tenant_id, provider_id, model_id, display_name, owned_by,
392
- upstream_created, raw, enabled, stale, discovered_at
393
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
400
+ upstream_created, raw, enabled, stale, kind, discovered_at
401
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
394
402
  ON CONFLICT (tenant_id, provider_id, model_id) DO UPDATE SET
395
403
  display_name = excluded.display_name,
396
404
  owned_by = excluded.owned_by,
@@ -398,6 +406,7 @@ export class LocalModelProviderStore implements ModelProviderStore {
398
406
  raw = excluded.raw,
399
407
  enabled = excluded.enabled,
400
408
  stale = excluded.stale,
409
+ kind = excluded.kind,
401
410
  discovered_at = excluded.discovered_at`
402
411
  )
403
412
  .run(
@@ -409,8 +418,10 @@ export class LocalModelProviderStore implements ModelProviderStore {
409
418
  model.ownedBy || null,
410
419
  model.createdAt || null,
411
420
  model.raw === undefined ? null : JSON.stringify(model.raw),
412
- model.enabled ?? current.get(model.id) ?? true ? 1 : 0,
421
+ model.enabled ?? current.get(model.id)?.enabled ?? true ? 1 : 0,
413
422
  model.stale ? 1 : 0,
423
+ // Omitted means "keep the stored kind"; a new row starts as chat.
424
+ model.kind ?? current.get(model.id)?.kind ?? "chat",
414
425
  now
415
426
  );
416
427
  }
@@ -496,6 +507,7 @@ function mapRowToModel(row: ModelRow): ModelProviderModelEntry {
496
507
  : undefined,
497
508
  enabled: row.enabled === 1,
498
509
  stale: row.stale === 1,
510
+ kind: (row.kind as ModelKind) || "chat",
499
511
  discoveredAt: parseISO(row.discovered_at),
500
512
  };
501
513
  }
@@ -0,0 +1,218 @@
1
+ import {
2
+ assertStoreIdentity,
3
+ assertStoreNamespacePrefix,
4
+ storeExpiresAt,
5
+ storePaginationValue,
6
+ STORE_IDENTITY_SEPARATOR,
7
+ type StoreItem,
8
+ type StoreProtocol,
9
+ type StorePutInput,
10
+ type StoreSearchInput,
11
+ } from "@axiom-lattice/protocols";
12
+ import { nowISO, parseISO, type DatabaseWrapper } from "../database";
13
+ import { MigrationManager } from "../migrations/migration";
14
+
15
+ const STORE_NAME = "lt_store_items";
16
+ const NAMESPACE_SEPARATOR = STORE_IDENTITY_SEPARATOR;
17
+
18
+ const DDL = `
19
+ CREATE TABLE IF NOT EXISTS lt_store_items (
20
+ tenant_id TEXT NOT NULL,
21
+ namespace_path TEXT NOT NULL,
22
+ key TEXT NOT NULL,
23
+ value TEXT NOT NULL,
24
+ created_at TEXT NOT NULL,
25
+ updated_at TEXT NOT NULL,
26
+ expires_at TEXT,
27
+ PRIMARY KEY (tenant_id, namespace_path, key)
28
+ );
29
+ CREATE INDEX IF NOT EXISTS idx_lt_store_items_tenant_updated
30
+ ON lt_store_items(tenant_id, updated_at DESC);
31
+ CREATE INDEX IF NOT EXISTS idx_lt_store_items_expires
32
+ ON lt_store_items(expires_at) WHERE expires_at IS NOT NULL;
33
+ `;
34
+
35
+ interface StoreRow {
36
+ tenant_id: string;
37
+ namespace_path: string;
38
+ key: string;
39
+ value: string;
40
+ created_at: string;
41
+ updated_at: string;
42
+ expires_at: string | null;
43
+ }
44
+
45
+ function namespacePath(namespace: string[]): string {
46
+ return namespace.join(NAMESPACE_SEPARATOR);
47
+ }
48
+
49
+ function isPlainObject(value: unknown): value is Record<string, unknown> {
50
+ return typeof value === "object" && value !== null && !Array.isArray(value);
51
+ }
52
+
53
+ function deepContains(actual: unknown, expected: unknown): boolean {
54
+ if (Array.isArray(expected)) {
55
+ return Array.isArray(actual)
56
+ && expected.every((item) => actual.some((candidate) => deepContains(candidate, item)));
57
+ }
58
+ if (isPlainObject(expected)) {
59
+ if (!isPlainObject(actual)) return false;
60
+ return Object.entries(expected).every(([key, value]) => deepContains(actual[key], value));
61
+ }
62
+ return Object.is(actual, expected);
63
+ }
64
+
65
+ function matchesFilter(value: Record<string, unknown>, filter: Record<string, unknown>): boolean {
66
+ return deepContains(value, filter);
67
+ }
68
+
69
+ function hasPrefix(namespace: string[], prefix: string[]): boolean {
70
+ return prefix.length === 0
71
+ || (namespace.length >= prefix.length
72
+ && prefix.every((segment, index) => namespace[index] === segment));
73
+ }
74
+
75
+ function mapRow(row: StoreRow): StoreItem {
76
+ return {
77
+ namespace: row.namespace_path.split(NAMESPACE_SEPARATOR),
78
+ key: row.key,
79
+ value: JSON.parse(row.value) as Record<string, unknown>,
80
+ createdAt: parseISO(row.created_at),
81
+ updatedAt: parseISO(row.updated_at),
82
+ expiresAt: row.expires_at ? parseISO(row.expires_at) : undefined,
83
+ };
84
+ }
85
+
86
+ /**
87
+ * Local SQLite tenant-scoped namespaced key-value store.
88
+ */
89
+ export class LocalStore implements StoreProtocol {
90
+ constructor(private readonly db: DatabaseWrapper) {
91
+ const mm = new MigrationManager(db, STORE_NAME);
92
+ mm.register({
93
+ version: 1,
94
+ name: "create_lt_store_items",
95
+ up: (database) => { database.exec(DDL); },
96
+ });
97
+ mm.migrate();
98
+ }
99
+
100
+ async put(tenantId: string, input: StorePutInput): Promise<StoreItem> {
101
+ assertStoreIdentity(input.namespace, input.key);
102
+ const now = nowISO();
103
+ const path = namespacePath(input.namespace);
104
+ const existing = this.db.prepare(
105
+ `SELECT created_at FROM lt_store_items WHERE tenant_id = ? AND namespace_path = ? AND key = ?`,
106
+ ).get(tenantId, path, input.key) as unknown as { created_at: string } | undefined;
107
+ const expiresAt = storeExpiresAt(input.ttlMinutes)?.toISOString() ?? null;
108
+ this.db.prepare(
109
+ `INSERT INTO lt_store_items
110
+ (tenant_id, namespace_path, key, value, created_at, updated_at, expires_at)
111
+ VALUES (?, ?, ?, ?, ?, ?, ?)
112
+ ON CONFLICT(tenant_id, namespace_path, key) DO UPDATE SET
113
+ value = excluded.value,
114
+ updated_at = excluded.updated_at,
115
+ expires_at = excluded.expires_at`,
116
+ ).run(
117
+ tenantId,
118
+ path,
119
+ input.key,
120
+ JSON.stringify(input.value),
121
+ existing?.created_at ?? now,
122
+ now,
123
+ expiresAt,
124
+ );
125
+ return {
126
+ namespace: [...input.namespace],
127
+ key: input.key,
128
+ value: structuredClone(input.value),
129
+ createdAt: parseISO(existing?.created_at ?? now),
130
+ updatedAt: parseISO(now),
131
+ expiresAt: expiresAt ? parseISO(expiresAt) : undefined,
132
+ };
133
+ }
134
+
135
+ async get(tenantId: string, namespace: string[], key: string): Promise<StoreItem | null> {
136
+ assertStoreIdentity(namespace, key);
137
+ const now = nowISO();
138
+ const row = this.db.prepare(
139
+ `SELECT * FROM lt_store_items
140
+ WHERE tenant_id = ? AND namespace_path = ? AND key = ?
141
+ AND (expires_at IS NULL OR expires_at > ?)`,
142
+ ).get(tenantId, namespacePath(namespace), key, now) as unknown as StoreRow | undefined;
143
+ return row ? mapRow(row) : null;
144
+ }
145
+
146
+ async delete(tenantId: string, namespace: string[], key: string): Promise<boolean> {
147
+ assertStoreIdentity(namespace, key);
148
+ const result = this.db.prepare(
149
+ `DELETE FROM lt_store_items WHERE tenant_id = ? AND namespace_path = ? AND key = ?`,
150
+ ).run(tenantId, namespacePath(namespace), key);
151
+ return result.changes > 0;
152
+ }
153
+
154
+ async search(tenantId: string, input: StoreSearchInput): Promise<StoreItem[]> {
155
+ const now = nowISO();
156
+ const prefix = input.namespacePrefix ?? [];
157
+ assertStoreNamespacePrefix(prefix);
158
+ const query = input.query?.toLowerCase();
159
+ const limit = storePaginationValue(input.limit, 10);
160
+ const offset = storePaginationValue(input.offset, 0);
161
+ const rows = this.db.prepare(
162
+ `SELECT * FROM lt_store_items
163
+ WHERE tenant_id = ? AND (expires_at IS NULL OR expires_at > ?)`,
164
+ ).all(tenantId, now) as unknown as StoreRow[];
165
+ return rows
166
+ .map(mapRow)
167
+ .filter((item) => hasPrefix(item.namespace, prefix))
168
+ .filter((item) => input.filter === undefined || matchesFilter(item.value, input.filter))
169
+ .filter((item) => query === undefined
170
+ || JSON.stringify(item.value).toLowerCase().includes(query))
171
+ .sort((left, right) =>
172
+ right.updatedAt.getTime() - left.updatedAt.getTime()
173
+ || namespacePath(left.namespace).localeCompare(namespacePath(right.namespace))
174
+ || left.key.localeCompare(right.key))
175
+ .slice(offset, offset + limit);
176
+ }
177
+
178
+ async listNamespaces(
179
+ tenantId: string,
180
+ prefix: string[] = [],
181
+ limit = 100,
182
+ ): Promise<string[][]> {
183
+ assertStoreNamespacePrefix(prefix);
184
+ const rows = this.db.prepare(
185
+ `SELECT DISTINCT namespace_path FROM lt_store_items
186
+ WHERE tenant_id = ? AND (expires_at IS NULL OR expires_at > ?)`,
187
+ ).all(tenantId, nowISO()) as unknown as Array<{ namespace_path: string }>;
188
+ const namespaces = new Map<string, string[]>();
189
+ for (const row of rows) {
190
+ const namespace = row.namespace_path.split(NAMESPACE_SEPARATOR);
191
+ if (hasPrefix(namespace, prefix)) namespaces.set(row.namespace_path, namespace);
192
+ }
193
+ return Array.from(namespaces.entries())
194
+ .sort(([left], [right]) => left.localeCompare(right))
195
+ .slice(0, storePaginationValue(limit, 100))
196
+ .map(([, namespace]) => namespace);
197
+ }
198
+
199
+ async deleteExpired(now: Date, limit: number): Promise<number> {
200
+ const rows = this.db.prepare(
201
+ `SELECT tenant_id, namespace_path, key FROM lt_store_items
202
+ WHERE expires_at IS NOT NULL AND expires_at <= ?
203
+ LIMIT ?`,
204
+ ).all(now.toISOString(), storePaginationValue(limit, 0)) as unknown as Array<{
205
+ tenant_id: string;
206
+ namespace_path: string;
207
+ key: string;
208
+ }>;
209
+ const statement = this.db.prepare(
210
+ `DELETE FROM lt_store_items WHERE tenant_id = ? AND namespace_path = ? AND key = ?`,
211
+ );
212
+ let deleted = 0;
213
+ for (const row of rows) {
214
+ deleted += statement.run(row.tenant_id, row.namespace_path, row.key).changes;
215
+ }
216
+ return deleted;
217
+ }
218
+ }
@@ -254,14 +254,20 @@ export class LocalThreadMessageQueueStore implements IMessageQueueStore {
254
254
 
255
255
  async getThreadsWithPendingMessages(): Promise<ThreadInfo[]> {
256
256
  const rows = this.db.prepare(
257
- `SELECT tenant_id, assistant_id, thread_id, workspace_id, project_id FROM lt_thread_message_queue WHERE status IN ('pending', 'processing') GROUP BY tenant_id, assistant_id, thread_id, workspace_id, project_id ORDER BY thread_id`,
258
- ).all() as { tenant_id: string; assistant_id: string; thread_id: string; workspace_id: string | null; project_id: string | null }[];
257
+ `SELECT tenant_id, assistant_id, thread_id, workspace_id, project_id,
258
+ MAX(status = 'pending') AS has_pending
259
+ FROM lt_thread_message_queue
260
+ WHERE status IN ('pending', 'processing')
261
+ GROUP BY tenant_id, assistant_id, thread_id, workspace_id, project_id
262
+ ORDER BY thread_id`,
263
+ ).all() as { tenant_id: string; assistant_id: string; thread_id: string; workspace_id: string | null; project_id: string | null; has_pending: number }[];
259
264
  return rows.map((r) => ({
260
265
  tenantId: r.tenant_id,
261
266
  assistantId: r.assistant_id,
262
267
  threadId: r.thread_id,
263
268
  workspaceId: r.workspace_id,
264
269
  projectId: r.project_id,
270
+ hasPending: r.has_pending === 1,
265
271
  }));
266
272
  }
267
273
 
@@ -21,6 +21,7 @@ CREATE TABLE IF NOT EXISTS lt_threads (
21
21
  metadata TEXT DEFAULT '{}',
22
22
  created_at TEXT NOT NULL,
23
23
  updated_at TEXT NOT NULL,
24
+ expires_at TEXT,
24
25
  PRIMARY KEY (tenant_id, id)
25
26
  );
26
27
  CREATE INDEX IF NOT EXISTS idx_lt_threads_assistant ON lt_threads(tenant_id, assistant_id);
@@ -33,6 +34,7 @@ interface ThreadRow {
33
34
  metadata: string;
34
35
  created_at: string;
35
36
  updated_at: string;
37
+ expires_at: string | null;
36
38
  }
37
39
 
38
40
  export class LocalThreadStore implements ThreadStore {
@@ -47,6 +49,15 @@ export class LocalThreadStore implements ThreadStore {
47
49
  up: (d) => { d.exec(DDL); },
48
50
  });
49
51
  mm.migrate();
52
+ this.ensureColumn("expires_at", "TEXT");
53
+ }
54
+
55
+ /** Add a column if it does not exist (SQLite version compatible). */
56
+ private ensureColumn(column: string, ddl: string): void {
57
+ const cols = this.db.prepare("PRAGMA table_info(lt_threads)").all() as unknown as Array<{ name: string }>;
58
+ if (!cols.some((c) => c.name === column)) {
59
+ this.db.exec(`ALTER TABLE lt_threads ADD COLUMN ${column} ${ddl}`);
60
+ }
50
61
  }
51
62
 
52
63
  async getThreadsByAssistantId(
@@ -88,13 +99,14 @@ export class LocalThreadStore implements ThreadStore {
88
99
  const metadata = JSON.stringify(data.metadata || {});
89
100
 
90
101
  this.db.prepare(
91
- `INSERT INTO lt_threads (id, tenant_id, assistant_id, metadata, created_at, updated_at)
92
- VALUES (?, ?, ?, ?, ?, ?)
102
+ `INSERT INTO lt_threads (id, tenant_id, assistant_id, metadata, created_at, updated_at, expires_at)
103
+ VALUES (?, ?, ?, ?, ?, ?, ?)
93
104
  ON CONFLICT(tenant_id, id) DO UPDATE SET
94
105
  assistant_id = excluded.assistant_id,
95
106
  metadata = excluded.metadata,
96
- updated_at = excluded.updated_at`,
97
- ).run(threadId, tenantId, assistantId, metadata, now, now);
107
+ updated_at = excluded.updated_at,
108
+ expires_at = excluded.expires_at`,
109
+ ).run(threadId, tenantId, assistantId, metadata, now, now, data.expiresAt?.toISOString() ?? null);
98
110
 
99
111
  return {
100
112
  id: threadId,
@@ -103,6 +115,7 @@ export class LocalThreadStore implements ThreadStore {
103
115
  metadata: data.metadata || {},
104
116
  createdAt: parseISO(now),
105
117
  updatedAt: parseISO(now),
118
+ expiresAt: data.expiresAt,
106
119
  };
107
120
  }
108
121
 
@@ -119,15 +132,19 @@ export class LocalThreadStore implements ThreadStore {
119
132
  ...(updates.metadata || {}),
120
133
  };
121
134
  const now = nowISO();
135
+ const expiresAt = updates.expiresAt !== undefined
136
+ ? updates.expiresAt.toISOString()
137
+ : existing.expiresAt?.toISOString() ?? null;
122
138
 
123
139
  this.db.prepare(
124
- `UPDATE lt_threads SET metadata = ?, updated_at = ? WHERE tenant_id = ? AND id = ?`,
125
- ).run(JSON.stringify(updatedMetadata), now, tenantId, threadId);
140
+ `UPDATE lt_threads SET metadata = ?, updated_at = ?, expires_at = ? WHERE tenant_id = ? AND id = ?`,
141
+ ).run(JSON.stringify(updatedMetadata), now, expiresAt, tenantId, threadId);
126
142
 
127
143
  return {
128
144
  ...existing,
129
145
  metadata: updatedMetadata,
130
146
  updatedAt: parseISO(now),
147
+ expiresAt: expiresAt ? parseISO(expiresAt) : undefined,
131
148
  };
132
149
  }
133
150
 
@@ -144,6 +161,16 @@ export class LocalThreadStore implements ThreadStore {
144
161
  ).get(tenantId, threadId);
145
162
  return row !== undefined;
146
163
  }
164
+
165
+ async listExpiredThreads(now: Date, limit: number): Promise<Thread[]> {
166
+ const rows = this.db.prepare(
167
+ `SELECT * FROM lt_threads
168
+ WHERE expires_at IS NOT NULL AND expires_at <= ?
169
+ ORDER BY expires_at ASC
170
+ LIMIT ?`,
171
+ ).all(now.toISOString(), Math.max(0, limit)) as unknown as ThreadRow[];
172
+ return rows.map(mapRowToThread);
173
+ }
147
174
  }
148
175
 
149
176
  function mapRowToThread(row: ThreadRow): Thread {
@@ -154,5 +181,6 @@ function mapRowToThread(row: ThreadRow): Thread {
154
181
  metadata: JSON.parse(row.metadata || "{}"),
155
182
  createdAt: parseISO(row.created_at),
156
183
  updatedAt: parseISO(row.updated_at),
184
+ expiresAt: row.expires_at ? parseISO(row.expires_at) : undefined,
157
185
  };
158
186
  }