@axiom-lattice/pg-stores 3.4.0 → 4.0.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/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);
@@ -12741,7 +12788,7 @@ var PostgreSQLAgentRunLedger = class {
12741
12788
  throw new AgentRunInputCommandConflictError();
12742
12789
  }
12743
12790
  const inputMessageIds = input.inputMessageIds ? normalizeAgentRunMessageIds(input.inputMessageIds) : extractAgentRunInputMessageIds(input.input);
12744
- const availableAt = input.availableAt ?? Date.now();
12791
+ const availableAt = input.availableAt ?? null;
12745
12792
  const limit = input.maxActiveRunsForTenant;
12746
12793
  const client = await this.pool.connect();
12747
12794
  try {
@@ -12791,7 +12838,10 @@ var PostgreSQLAgentRunLedger = class {
12791
12838
  available_at, created_at, updated_at, task_id)
12792
12839
  VALUES ($1, $2, $3, $4, $5, $6, 'pending', $7, NULL, NULL, $8, 0,
12793
12840
  $9::jsonb, $10::jsonb, $11::jsonb, $12::jsonb, $13,
12794
- to_timestamp($14::double precision / 1000.0),
12841
+ COALESCE(
12842
+ to_timestamp($14::double precision / 1000.0),
12843
+ date_trunc('milliseconds', now())
12844
+ ),
12795
12845
  date_trunc('milliseconds', now()),
12796
12846
  date_trunc('milliseconds', now()), $15)
12797
12847
  RETURNING *`,
@@ -13310,14 +13360,17 @@ var PostgreSQLAgentRunLedger = class {
13310
13360
  /** Atomically requeues or interrupts an expired run-queue row. */
13311
13361
  async settleExpiredRun(runId, outcome, options) {
13312
13362
  if (outcome === "pending") {
13313
- const availableAt = options?.availableAt ?? Date.now();
13363
+ const availableAt = options?.availableAt ?? null;
13314
13364
  const result2 = await this.pool.query(
13315
13365
  `UPDATE lattice_agent_runs
13316
13366
  SET status = 'pending',
13317
13367
  owner = NULL,
13318
13368
  lease_expires_at = NULL,
13319
13369
  started_at = NULL,
13320
- available_at = to_timestamp($2::double precision / 1000.0),
13370
+ available_at = COALESCE(
13371
+ to_timestamp($2::double precision / 1000.0),
13372
+ date_trunc('milliseconds', now())
13373
+ ),
13321
13374
  finished_at = NULL,
13322
13375
  updated_at = now()
13323
13376
  WHERE run_id = $1
@@ -15157,6 +15210,7 @@ export {
15157
15210
  ProjectRoomMessageIdConflictError,
15158
15211
  ThreadMessageQueueStore,
15159
15212
  addAssistantTenantId,
15213
+ addCancelRequestedToEvalRuns,
15160
15214
  addEnvToEvalRuns,
15161
15215
  addFileContentType,
15162
15216
  addHoldoutToEvalRuns,