@axiom-lattice/pg-stores 3.4.0 → 4.0.1

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
@@ -4291,6 +4291,21 @@ var addTaskIdToEvalRuns = {
4291
4291
  `);
4292
4292
  }
4293
4293
  };
4294
+ var addCancelRequestedToEvalRuns = {
4295
+ version: 205,
4296
+ name: "add_cancel_requested_to_eval_runs",
4297
+ up: async (client) => {
4298
+ await client.query(`
4299
+ ALTER TABLE lattice_eval_runs
4300
+ ADD COLUMN IF NOT EXISTS cancel_requested BOOLEAN NOT NULL DEFAULT FALSE
4301
+ `);
4302
+ },
4303
+ down: async (client) => {
4304
+ await client.query(`
4305
+ ALTER TABLE lattice_eval_runs DROP COLUMN IF EXISTS cancel_requested
4306
+ `);
4307
+ }
4308
+ };
4294
4309
  var evalMigrations = [
4295
4310
  createEvalProjectsTable,
4296
4311
  createEvalSuitesTable,
@@ -4302,7 +4317,8 @@ var evalMigrations = [
4302
4317
  addInterruptColumnsToEval,
4303
4318
  addInterruptPolicyToEvalCases,
4304
4319
  enforceUniqueEvalProjectName,
4305
- addTaskIdToEvalRuns
4320
+ addTaskIdToEvalRuns,
4321
+ addCancelRequestedToEvalRuns
4306
4322
  ];
4307
4323
 
4308
4324
  // src/stores/PostgreSQLEvalStore.ts
@@ -4427,6 +4443,7 @@ var PostgreSQLEvalStore = class {
4427
4443
  interruptedCases: row.interrupted_cases ?? 0,
4428
4444
  avgScore: row.avg_score ?? 0,
4429
4445
  error: row.error,
4446
+ cancelRequested: row.cancel_requested ?? false,
4430
4447
  holdout: row.holdout,
4431
4448
  envProjectId: row.env_project_id,
4432
4449
  envWorkspaceId: row.env_workspace_id,
@@ -4798,7 +4815,7 @@ var PostgreSQLEvalStore = class {
4798
4815
  `SELECT id, project_id, tenant_id,
4799
4816
  status, concurrency, total_cases,
4800
4817
  passed_cases, failed_cases, interrupted_cases, avg_score,
4801
- error, holdout, env_project_id, env_workspace_id, task_id, created_at, started_at, completed_at
4818
+ error, holdout, env_project_id, env_workspace_id, task_id, cancel_requested, created_at, started_at, completed_at
4802
4819
  FROM lattice_eval_runs
4803
4820
  WHERE ${conditions.join(" AND ")}
4804
4821
  ORDER BY created_at DESC`,
@@ -4813,7 +4830,7 @@ var PostgreSQLEvalStore = class {
4813
4830
  `SELECT id, project_id, tenant_id,
4814
4831
  status, concurrency, total_cases,
4815
4832
  passed_cases, failed_cases, interrupted_cases, avg_score,
4816
- error, holdout, env_project_id, env_workspace_id, task_id, created_at, started_at, completed_at
4833
+ error, holdout, env_project_id, env_workspace_id, task_id, cancel_requested, created_at, started_at, completed_at
4817
4834
  FROM lattice_eval_runs
4818
4835
  WHERE id = $1 AND tenant_id = $2`,
4819
4836
  [id, tenantId]
@@ -4831,7 +4848,7 @@ var PostgreSQLEvalStore = class {
4831
4848
  RETURNING id, project_id, tenant_id,
4832
4849
  status, concurrency, total_cases,
4833
4850
  passed_cases, failed_cases, interrupted_cases, avg_score,
4834
- error, holdout, env_project_id, env_workspace_id, task_id, created_at, started_at, completed_at`,
4851
+ error, holdout, env_project_id, env_workspace_id, task_id, cancel_requested, created_at, started_at, completed_at`,
4835
4852
  [
4836
4853
  actualId,
4837
4854
  projectId,
@@ -4893,11 +4910,22 @@ var PostgreSQLEvalStore = class {
4893
4910
  RETURNING id, project_id, tenant_id,
4894
4911
  status, concurrency, total_cases,
4895
4912
  passed_cases, failed_cases, interrupted_cases, avg_score,
4896
- error, holdout, env_project_id, env_workspace_id, task_id, created_at, started_at, completed_at`,
4913
+ error, holdout, env_project_id, env_workspace_id, task_id, cancel_requested, created_at, started_at, completed_at`,
4897
4914
  vals
4898
4915
  );
4899
4916
  return rows.length ? this.mapRowToRun(rows[0]) : null;
4900
4917
  }
4918
+ /** Request cooperative cancellation for a running evaluation. */
4919
+ async requestCancel(tenantId, id) {
4920
+ await this.ensureInitialized();
4921
+ const { rowCount } = await this.pool.query(
4922
+ `UPDATE lattice_eval_runs
4923
+ SET cancel_requested = TRUE
4924
+ WHERE tenant_id = $1 AND id = $2 AND status = 'running'`,
4925
+ [tenantId, id]
4926
+ );
4927
+ return (rowCount ?? 0) > 0;
4928
+ }
4901
4929
  /** Delete a run and its results */
4902
4930
  async deleteRun(tenantId, id) {
4903
4931
  await this.ensureInitialized();
@@ -7650,12 +7678,27 @@ var addTaskDependenciesGinIndex = {
7650
7678
  await client.query("DROP INDEX IF EXISTS idx_lattice_tasks_dependencies_gin");
7651
7679
  }
7652
7680
  };
7681
+ var addTaskContextThreadIndex = {
7682
+ version: 206,
7683
+ name: "add_task_context_thread_index",
7684
+ up: async (client) => {
7685
+ await client.query(`
7686
+ CREATE INDEX IF NOT EXISTS idx_lattice_tasks_context_thread
7687
+ ON lattice_tasks (tenant_id, (context->>'thread_id'))
7688
+ WHERE context->>'thread_id' IS NOT NULL
7689
+ `);
7690
+ },
7691
+ down: async (client) => {
7692
+ await client.query("DROP INDEX IF EXISTS idx_lattice_tasks_context_thread");
7693
+ }
7694
+ };
7653
7695
  var taskMigrations = [
7654
7696
  createTasksTable,
7655
7697
  addTaskFieldsMigration,
7656
7698
  addTaskProjectFieldsMigration,
7657
7699
  addFilesToTasks,
7658
- addTaskDependenciesGinIndex
7700
+ addTaskDependenciesGinIndex,
7701
+ addTaskContextThreadIndex
7659
7702
  ];
7660
7703
 
7661
7704
  // src/stores/PostgreSQLTaskStore.ts
@@ -7866,6 +7909,10 @@ var PostgreSQLTaskStore = class {
7866
7909
  conditions.push(`priority = $${paramIndex++}`);
7867
7910
  params.push(filter.priority);
7868
7911
  }
7912
+ if (filter.threadId) {
7913
+ conditions.push(`context->>'thread_id' = $${paramIndex++}`);
7914
+ params.push(filter.threadId);
7915
+ }
7869
7916
  if (filter.workspaceId) {
7870
7917
  conditions.push(`workspace_id = $${paramIndex++}`);
7871
7918
  params.push(filter.workspaceId);
@@ -9512,6 +9559,9 @@ import { Pool as Pool25 } from "pg";
9512
9559
  import { PGVectorStore } from "@langchain/community/vectorstores/pgvector";
9513
9560
  import { Document } from "@langchain/core/documents";
9514
9561
  import { embeddingsLatticeManager } from "@axiom-lattice/core";
9562
+ import {
9563
+ EMBEDDING_DEFAULT_KEY
9564
+ } from "@axiom-lattice/protocols";
9515
9565
  function safeTableName(name) {
9516
9566
  if (!/^[a-z][a-z0-9_-]*$/.test(name)) {
9517
9567
  throw new Error(`Invalid table name: ${name}`);
@@ -9526,16 +9576,20 @@ var PGVectorStoreProvider = class {
9526
9576
  async create(params) {
9527
9577
  const { tableName, embeddingKey } = params;
9528
9578
  const tenantId = params.tenantId ?? "default";
9579
+ const requestedKey = embeddingKey || EMBEDDING_DEFAULT_KEY;
9529
9580
  const embeddings = await embeddingsLatticeManager.getEmbeddingsClientWithTenant(
9530
9581
  tenantId,
9531
- embeddingKey
9532
- ) ?? await embeddingsLatticeManager.getEmbeddingsClientWithTenant(
9582
+ requestedKey
9583
+ ) ?? (requestedKey === EMBEDDING_DEFAULT_KEY ? void 0 : await embeddingsLatticeManager.getEmbeddingsClientWithTenant(
9584
+ tenantId,
9585
+ EMBEDDING_DEFAULT_KEY
9586
+ )) ?? await embeddingsLatticeManager.getEmbeddingsClientWithTenant(
9533
9587
  tenantId,
9534
9588
  "default"
9535
9589
  );
9536
9590
  if (!embeddings) {
9537
9591
  throw new Error(
9538
- `No embedding model "${embeddingKey}" registered for tenant "${tenantId}", and no deployment default. Configure one in the model provider panel.`
9592
+ `No embedding model "${embeddingKey}" registered for tenant "${tenantId}", and no tenant or deployment default. Configure one in the model service panel.`
9539
9593
  );
9540
9594
  }
9541
9595
  return PGVectorStore.initialize(embeddings, {
@@ -10704,6 +10758,28 @@ var addModelProviderModelKind = {
10704
10758
  `);
10705
10759
  }
10706
10760
  };
10761
+ var addModelProviderKindDefault = {
10762
+ version: 207,
10763
+ name: "add_model_provider_kind_default",
10764
+ up: async (client) => {
10765
+ await client.query(`
10766
+ DROP INDEX IF EXISTS uk_lattice_model_provider_models_tenant_default
10767
+ `);
10768
+ await client.query(`
10769
+ CREATE UNIQUE INDEX IF NOT EXISTS uk_lattice_model_provider_models_tenant_kind_default
10770
+ ON lattice_model_provider_models (tenant_id, kind) WHERE is_default
10771
+ `);
10772
+ },
10773
+ down: async (client) => {
10774
+ await client.query(`
10775
+ DROP INDEX IF EXISTS uk_lattice_model_provider_models_tenant_kind_default
10776
+ `);
10777
+ await client.query(`
10778
+ CREATE UNIQUE INDEX IF NOT EXISTS uk_lattice_model_provider_models_tenant_default
10779
+ ON lattice_model_provider_models (tenant_id) WHERE is_default
10780
+ `);
10781
+ }
10782
+ };
10707
10783
 
10708
10784
  // src/stores/PostgreSQLCapabilityBundleStore.ts
10709
10785
  import { randomUUID as randomUUID3 } from "crypto";
@@ -11698,6 +11774,7 @@ var PostgreSQLModelProviderStore = class {
11698
11774
  this.migrationManager.register(addModelProviderModelExtra);
11699
11775
  this.migrationManager.register(addModelProviderModelDefault);
11700
11776
  this.migrationManager.register(addModelProviderModelKind);
11777
+ this.migrationManager.register(addModelProviderKindDefault);
11701
11778
  if (options.autoMigrate !== false) {
11702
11779
  this.initialize().catch((error) => {
11703
11780
  console.error("Failed to initialize PostgreSQLModelProviderStore:", error);
@@ -12036,15 +12113,22 @@ var PostgreSQLModelProviderStore = class {
12036
12113
  const effectiveKind = rows.map(
12037
12114
  (model) => model.kind ?? currentKind.get(model.id) ?? "chat"
12038
12115
  );
12039
- const namesDefault = rows.some((model) => model.isDefault === true);
12040
- if (namesDefault) {
12116
+ const namedDefaultKinds = /* @__PURE__ */ new Set();
12117
+ rows.forEach((model, index) => {
12118
+ if (model.isDefault === true) {
12119
+ namedDefaultKinds.add(effectiveKind[index]);
12120
+ }
12121
+ });
12122
+ for (const kind of namedDefaultKinds) {
12041
12123
  await client.query(
12042
- `UPDATE lattice_model_provider_models SET is_default = FALSE WHERE tenant_id = $1`,
12043
- [tenantId]
12124
+ `UPDATE lattice_model_provider_models
12125
+ SET is_default = FALSE
12126
+ WHERE tenant_id = $1 AND kind = $2 AND is_default`,
12127
+ [tenantId, kind]
12044
12128
  );
12045
12129
  }
12046
12130
  const effectiveIsDefault = rows.map(
12047
- (model) => model.isDefault ?? (namesDefault ? false : currentIsDefault.get(model.id)) ?? false
12131
+ (model, index) => model.isDefault ?? (namedDefaultKinds.has(effectiveKind[index]) ? false : currentIsDefault.get(model.id)) ?? false
12048
12132
  );
12049
12133
  await client.query(
12050
12134
  `
@@ -12741,7 +12825,7 @@ var PostgreSQLAgentRunLedger = class {
12741
12825
  throw new AgentRunInputCommandConflictError();
12742
12826
  }
12743
12827
  const inputMessageIds = input.inputMessageIds ? normalizeAgentRunMessageIds(input.inputMessageIds) : extractAgentRunInputMessageIds(input.input);
12744
- const availableAt = input.availableAt ?? Date.now();
12828
+ const availableAt = input.availableAt ?? null;
12745
12829
  const limit = input.maxActiveRunsForTenant;
12746
12830
  const client = await this.pool.connect();
12747
12831
  try {
@@ -12791,7 +12875,10 @@ var PostgreSQLAgentRunLedger = class {
12791
12875
  available_at, created_at, updated_at, task_id)
12792
12876
  VALUES ($1, $2, $3, $4, $5, $6, 'pending', $7, NULL, NULL, $8, 0,
12793
12877
  $9::jsonb, $10::jsonb, $11::jsonb, $12::jsonb, $13,
12794
- to_timestamp($14::double precision / 1000.0),
12878
+ COALESCE(
12879
+ to_timestamp($14::double precision / 1000.0),
12880
+ date_trunc('milliseconds', now())
12881
+ ),
12795
12882
  date_trunc('milliseconds', now()),
12796
12883
  date_trunc('milliseconds', now()), $15)
12797
12884
  RETURNING *`,
@@ -13310,14 +13397,17 @@ var PostgreSQLAgentRunLedger = class {
13310
13397
  /** Atomically requeues or interrupts an expired run-queue row. */
13311
13398
  async settleExpiredRun(runId, outcome, options) {
13312
13399
  if (outcome === "pending") {
13313
- const availableAt = options?.availableAt ?? Date.now();
13400
+ const availableAt = options?.availableAt ?? null;
13314
13401
  const result2 = await this.pool.query(
13315
13402
  `UPDATE lattice_agent_runs
13316
13403
  SET status = 'pending',
13317
13404
  owner = NULL,
13318
13405
  lease_expires_at = NULL,
13319
13406
  started_at = NULL,
13320
- available_at = to_timestamp($2::double precision / 1000.0),
13407
+ available_at = COALESCE(
13408
+ to_timestamp($2::double precision / 1000.0),
13409
+ date_trunc('milliseconds', now())
13410
+ ),
13321
13411
  finished_at = NULL,
13322
13412
  updated_at = now()
13323
13413
  WHERE run_id = $1
@@ -14507,6 +14597,7 @@ async function createPgStoreConfig(connectionString) {
14507
14597
  mm.register(createAgentRunRetentionMigration);
14508
14598
  mm.register(createAgentRunProjectTaskMigration);
14509
14599
  mm.register(agentRunProjectTaskRunningIndexMigration);
14600
+ mm.register(addModelProviderKindDefault);
14510
14601
  await mm.migrate();
14511
14602
  const checkpoint = PostgresSaver.fromConnString(connectionString);
14512
14603
  checkpoint.setup().catch((err) => {
@@ -15157,6 +15248,7 @@ export {
15157
15248
  ProjectRoomMessageIdConflictError,
15158
15249
  ThreadMessageQueueStore,
15159
15250
  addAssistantTenantId,
15251
+ addCancelRequestedToEvalRuns,
15160
15252
  addEnvToEvalRuns,
15161
15253
  addFileContentType,
15162
15254
  addHoldoutToEvalRuns,