@sixb/sqlite 0.1.1 → 0.1.2

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.
Files changed (108) hide show
  1. package/README.md +18 -13
  2. package/dist/action-run-storage.d.ts +5 -2
  3. package/dist/action-run-storage.d.ts.map +1 -1
  4. package/dist/agents/agent-storage.d.ts +4 -2
  5. package/dist/agents/agent-storage.d.ts.map +1 -1
  6. package/dist/agents/rows.d.ts +3 -10
  7. package/dist/agents/rows.d.ts.map +1 -1
  8. package/dist/agents/runs.d.ts +3 -2
  9. package/dist/agents/runs.d.ts.map +1 -1
  10. package/dist/ai-usage-storage.d.ts +22 -0
  11. package/dist/ai-usage-storage.d.ts.map +1 -0
  12. package/dist/auth-storage/service-accounts.d.ts.map +1 -1
  13. package/dist/auth-storage/shared.d.ts +1 -10
  14. package/dist/auth-storage/shared.d.ts.map +1 -1
  15. package/dist/connector-connection-storage.d.ts +9 -0
  16. package/dist/connector-connection-storage.d.ts.map +1 -0
  17. package/dist/execution-storage.d.ts +14 -0
  18. package/dist/execution-storage.d.ts.map +1 -0
  19. package/dist/index.d.ts +13 -9
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +3862 -1042
  22. package/dist/index.js.map +30 -27
  23. package/dist/latest-run-query.d.ts.map +1 -1
  24. package/dist/migrations.d.ts +0 -1
  25. package/dist/migrations.d.ts.map +1 -1
  26. package/dist/ontology-storage/materialization-session.d.ts +50 -8
  27. package/dist/ontology-storage/materialization-session.d.ts.map +1 -1
  28. package/dist/ontology-storage/materialization-state.d.ts +4 -5
  29. package/dist/ontology-storage/materialization-state.d.ts.map +1 -1
  30. package/dist/ontology-storage/materialization-writer.d.ts.map +1 -1
  31. package/dist/ontology-storage/materializations.d.ts +0 -2
  32. package/dist/ontology-storage/materializations.d.ts.map +1 -1
  33. package/dist/ontology-storage/outbox.d.ts.map +1 -1
  34. package/dist/ontology-storage/shared.d.ts +3 -3
  35. package/dist/ontology-storage/shared.d.ts.map +1 -1
  36. package/dist/ontology-storage/sources.d.ts.map +1 -1
  37. package/dist/pipeline-run-storage.d.ts +7 -2
  38. package/dist/pipeline-run-storage.d.ts.map +1 -1
  39. package/dist/projection-run-storage.d.ts +9 -2
  40. package/dist/projection-run-storage.d.ts.map +1 -1
  41. package/dist/run-list-query.d.ts +1 -1
  42. package/dist/run-list-query.d.ts.map +1 -1
  43. package/dist/sync-run-storage.d.ts +7 -2
  44. package/dist/sync-run-storage.d.ts.map +1 -1
  45. package/dist/testing.d.ts +6 -0
  46. package/dist/testing.d.ts.map +1 -0
  47. package/dist/transactions.d.ts +3 -0
  48. package/dist/transactions.d.ts.map +1 -1
  49. package/dist/webhook-run-storage.d.ts +9 -2
  50. package/dist/webhook-run-storage.d.ts.map +1 -1
  51. package/dist/workflow-run-storage.d.ts +7 -3
  52. package/dist/workflow-run-storage.d.ts.map +1 -1
  53. package/package.json +3 -3
  54. package/src/action-run-storage.ts +51 -74
  55. package/src/agents/agent-storage.ts +5 -3
  56. package/src/agents/rows.ts +8 -40
  57. package/src/agents/runs.ts +27 -21
  58. package/src/ai-usage-storage.ts +324 -0
  59. package/src/auth-storage/service-accounts.ts +14 -21
  60. package/src/auth-storage/shared.ts +0 -22
  61. package/src/connector-connection-storage.ts +784 -0
  62. package/src/execution-storage.ts +158 -0
  63. package/src/index.ts +63 -22
  64. package/src/latest-run-query.ts +7 -1
  65. package/src/migrations/004-executions.sql +142 -0
  66. package/src/migrations/005-workflow-executions.sql +7 -0
  67. package/src/migrations/006-narrow-ontology-source-root-index.sql +4 -0
  68. package/src/migrations/007-split-overrides.sql +154 -0
  69. package/src/migrations/008-action-executions.sql +4 -0
  70. package/src/migrations/009-agent-executions.sql +115 -0
  71. package/src/migrations/010-ai-usage-accounting-foundation.sql +74 -0
  72. package/src/migrations/011-sync-failure-record.sql +22 -0
  73. package/src/migrations/012-pipeline-failure-record.sql +45 -0
  74. package/src/migrations/013-workflow-failure-record.sql +45 -0
  75. package/src/migrations/014-agent-failure-record.sql +59 -0
  76. package/src/migrations/015-projection-failure-record.sql +20 -0
  77. package/src/migrations/016-webhook-run-failure-record.sql +19 -0
  78. package/src/migrations/017-action-failure-record.sql +65 -0
  79. package/src/migrations/018-ontology-outbox-failure-record.sql +80 -0
  80. package/src/migrations/019-webhook-delivery-failure-record.sql +62 -0
  81. package/src/migrations/020-drop-run-usage-projections.sql +7 -0
  82. package/src/migrations/021-sync-pipeline-executions.sql +101 -0
  83. package/src/migrations/022-projection-executions.sql +244 -0
  84. package/src/migrations/023-webhook-executions.sql +51 -0
  85. package/src/migrations/024-ontology-commit-executions.sql +58 -0
  86. package/src/migrations/025-connector-connections.sql +257 -0
  87. package/src/migrations.ts +119 -4
  88. package/src/ontology-storage/materialization-session.ts +317 -143
  89. package/src/ontology-storage/materialization-state.ts +321 -112
  90. package/src/ontology-storage/materialization-writer.ts +142 -67
  91. package/src/ontology-storage/materializations.ts +67 -141
  92. package/src/ontology-storage/outbox.ts +10 -3
  93. package/src/ontology-storage/shared.ts +10 -4
  94. package/src/ontology-storage/sources.ts +4 -1
  95. package/src/pipeline-run-storage.ts +128 -43
  96. package/src/projection-run-storage.ts +209 -97
  97. package/src/run-list-query.ts +9 -4
  98. package/src/sync-run-storage.ts +118 -32
  99. package/src/testing.ts +20 -0
  100. package/src/transactions.ts +10 -3
  101. package/src/webhook-run-storage.ts +118 -74
  102. package/src/workflow-run-storage.ts +117 -131
  103. package/dist/validation.d.ts +0 -5
  104. package/dist/validation.d.ts.map +0 -1
  105. package/dist/webhook-delivery-storage.d.ts +0 -27
  106. package/dist/webhook-delivery-storage.d.ts.map +0 -1
  107. package/src/validation.ts +0 -30
  108. package/src/webhook-delivery-storage.ts +0 -272
@@ -1,5 +1,10 @@
1
1
  import type { Database } from "bun:sqlite"
2
2
  import type { ActionSubject, JsonValue } from "@sixb/core"
3
+ import {
4
+ parseActionRunFailure,
5
+ serializeActionRunFailure,
6
+ } from "@sixb/core/internal/action-run-storage"
7
+ import { assertActionRunExecution } from "@sixb/core/internal/action-run-storage-provider"
3
8
  import type {
4
9
  ActionRunEffectsRecord,
5
10
  ActionRunFailure,
@@ -9,6 +14,7 @@ import type {
9
14
  ActionRunStorage,
10
15
  ActionRunWritebackRecord,
11
16
  EnterActionRunPhaseInput,
17
+ ExecutionStorage,
12
18
  FinishActionRunInput,
13
19
  ListActionRunsInput,
14
20
  ListActionRunsResult,
@@ -33,6 +39,8 @@ import {
33
39
  } from "./transactions"
34
40
 
35
41
  export interface SqliteActionRunStorageOptions {
42
+ /** Execution lookup sharing the same provider transaction. */
43
+ executions: ExecutionStorage
36
44
  /** Path to SQLite database file. Defaults to ':memory:' for in-memory database. */
37
45
  path?: string
38
46
  /** Internal shared connection used by bundled SqliteStorage. */
@@ -43,9 +51,12 @@ export class SqliteActionRunStorage implements ActionRunStorage {
43
51
  private readonly connection: SqliteStoreConnection
44
52
  private readonly db: Database
45
53
 
46
- constructor(options: SqliteActionRunStorageOptions = {}) {
54
+ private readonly executions: ExecutionStorage
55
+
56
+ constructor(options: SqliteActionRunStorageOptions) {
47
57
  this.connection = openSqliteStoreConnection(options)
48
58
  this.db = this.connection.db
59
+ this.executions = options.executions
49
60
 
50
61
  if (this.connection.installFreshSchema) {
51
62
  installFreshSqliteSchema(this.db)
@@ -85,6 +96,13 @@ export class SqliteActionRunStorage implements ActionRunStorage {
85
96
 
86
97
  async queue(input: QueueActionRunInput): Promise<ActionRunRecord> {
87
98
  const queuedAt = input.queuedAt ?? new Date()
99
+ await assertActionRunExecution({
100
+ executions: this.executions,
101
+ projectId: input.projectId,
102
+ executionId: input.executionId,
103
+ runId: input.id,
104
+ actionId: input.actionId,
105
+ })
88
106
 
89
107
  try {
90
108
  this.db
@@ -93,6 +111,7 @@ export class SqliteActionRunStorage implements ActionRunStorage {
93
111
  INSERT INTO action_runs (
94
112
  project_id,
95
113
  id,
114
+ execution_id,
96
115
  action_id,
97
116
  subject_kind,
98
117
  object_type_id,
@@ -107,28 +126,23 @@ export class SqliteActionRunStorage implements ActionRunStorage {
107
126
  writeback_status,
108
127
  writeback_completed_at,
109
128
  writeback_result,
110
- writeback_error_name,
111
- writeback_error_message,
112
- writeback_error_phase,
129
+ writeback_error,
113
130
  effects_status,
114
131
  effects_completed_at,
115
- effects_error_name,
116
- effects_error_message,
117
- effects_error_phase,
118
- error_name,
119
- error_message,
120
- error_phase
132
+ effects_error,
133
+ error
121
134
  ) VALUES (
122
- ?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, NULL, ?, ?,
123
- NULL, NULL, NULL, NULL, NULL, NULL,
124
- NULL, NULL, NULL, NULL, NULL,
125
- NULL, NULL, NULL
135
+ ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, NULL, ?, ?,
136
+ NULL, NULL, NULL, NULL,
137
+ NULL, NULL, NULL,
138
+ NULL
126
139
  )
127
140
  `
128
141
  )
129
142
  .run(
130
143
  input.projectId,
131
144
  input.id,
145
+ input.executionId,
132
146
  input.actionId,
133
147
  input.subject.kind,
134
148
  input.subject.kind === "object" ? input.subject.objectTypeId : null,
@@ -188,17 +202,11 @@ export class SqliteActionRunStorage implements ActionRunStorage {
188
202
  writeback_status = NULL,
189
203
  writeback_completed_at = NULL,
190
204
  writeback_result = NULL,
191
- writeback_error_name = NULL,
192
- writeback_error_message = NULL,
193
- writeback_error_phase = NULL,
205
+ writeback_error = NULL,
194
206
  effects_status = NULL,
195
207
  effects_completed_at = NULL,
196
- effects_error_name = NULL,
197
- effects_error_message = NULL,
198
- effects_error_phase = NULL,
199
- error_name = NULL,
200
- error_message = NULL,
201
- error_phase = NULL
208
+ effects_error = NULL,
209
+ error = NULL
202
210
  WHERE project_id = ? AND id = ?
203
211
  `
204
212
  )
@@ -238,9 +246,7 @@ export class SqliteActionRunStorage implements ActionRunStorage {
238
246
  status = ?,
239
247
  phase = ?,
240
248
  started_at = ?,
241
- error_name = NULL,
242
- error_message = NULL,
243
- error_phase = NULL
249
+ error = NULL
244
250
  WHERE project_id = ? AND id = ?
245
251
  `
246
252
  )
@@ -307,9 +313,7 @@ export class SqliteActionRunStorage implements ActionRunStorage {
307
313
  writeback_status = ?,
308
314
  writeback_completed_at = ?,
309
315
  writeback_result = ?,
310
- writeback_error_name = ?,
311
- writeback_error_message = ?,
312
- writeback_error_phase = ?
316
+ writeback_error = ?
313
317
  WHERE project_id = ? AND id = ?
314
318
  `
315
319
  )
@@ -318,9 +322,7 @@ export class SqliteActionRunStorage implements ActionRunStorage {
318
322
  input.status,
319
323
  nextWriteback.completedAt.toISOString(),
320
324
  input.status === "succeeded" ? serializeJsonValue(input.result) : null,
321
- input.status === "failed" ? (input.error.name ?? null) : null,
322
- input.status === "failed" ? input.error.message : null,
323
- input.status === "failed" ? (input.error.phase ?? "writeback") : null,
325
+ input.status === "failed" ? serializeActionRunFailure(input.error, "writeback") : null,
324
326
  input.projectId,
325
327
  input.id
326
328
  )
@@ -357,9 +359,7 @@ export class SqliteActionRunStorage implements ActionRunStorage {
357
359
  phase = ?,
358
360
  effects_status = ?,
359
361
  effects_completed_at = ?,
360
- effects_error_name = ?,
361
- effects_error_message = ?,
362
- effects_error_phase = ?
362
+ effects_error = ?
363
363
  WHERE project_id = ? AND id = ?
364
364
  `
365
365
  )
@@ -367,9 +367,7 @@ export class SqliteActionRunStorage implements ActionRunStorage {
367
367
  "effects",
368
368
  input.status,
369
369
  nextEffects.completedAt.toISOString(),
370
- input.status === "failed" ? (input.error.name ?? null) : null,
371
- input.status === "failed" ? input.error.message : null,
372
- input.status === "failed" ? (input.error.phase ?? "effects") : null,
370
+ input.status === "failed" ? serializeActionRunFailure(input.error, "effects") : null,
373
371
  input.projectId,
374
372
  input.id
375
373
  )
@@ -410,9 +408,7 @@ export class SqliteActionRunStorage implements ActionRunStorage {
410
408
  status = ?,
411
409
  phase = ?,
412
410
  finished_at = ?,
413
- error_name = ?,
414
- error_message = ?,
415
- error_phase = ?
411
+ error = ?
416
412
  WHERE project_id = ? AND id = ?
417
413
  `
418
414
  )
@@ -420,9 +416,7 @@ export class SqliteActionRunStorage implements ActionRunStorage {
420
416
  input.status,
421
417
  phase,
422
418
  (input.finishedAt ?? new Date()).toISOString(),
423
- input.status === "succeeded" ? null : (input.error?.name ?? null),
424
- input.status === "succeeded" ? null : (input.error?.message ?? null),
425
- input.status === "succeeded" ? null : (input.error?.phase ?? phase),
419
+ input.status === "succeeded" ? null : serializeActionRunFailure(input.error),
426
420
  input.projectId,
427
421
  input.id
428
422
  )
@@ -583,23 +577,14 @@ function serializeJsonValue(value: JsonValue): string {
583
577
  }
584
578
 
585
579
  function toActionRunFailure(row: DatabaseRow): ActionRunFailure | undefined {
586
- return toFailure(row.error_name, row.error_message, row.error_phase)
580
+ return row.error === null ? undefined : parseActionRunFailure(row.error)
587
581
  }
588
582
 
589
- function toFailure(
590
- name: string | null,
591
- message: string | null,
592
- phase: ActionRunPhase | null
593
- ): ActionRunFailure | undefined {
594
- if (!message) {
595
- return undefined
596
- }
597
-
598
- return {
599
- name: name ?? undefined,
600
- message,
601
- phase: phase ?? undefined,
602
- }
583
+ function parsePhaseFailure<TPhase extends Extract<ActionRunPhase, "writeback" | "effects">>(
584
+ value: string | null,
585
+ expectedPhase: TPhase
586
+ ): ActionRunFailure<TPhase> {
587
+ return parseActionRunFailure(value, expectedPhase)
603
588
  }
604
589
 
605
590
  function toActionRunWritebackRecord(row: DatabaseRow): ActionRunWritebackRecord | undefined {
@@ -623,11 +608,7 @@ function toActionRunWritebackRecord(row: DatabaseRow): ActionRunWritebackRecord
623
608
  return {
624
609
  status: "failed",
625
610
  completedAt,
626
- error: toFailure(
627
- row.writeback_error_name,
628
- row.writeback_error_message,
629
- row.writeback_error_phase
630
- ),
611
+ error: parsePhaseFailure(row.writeback_error, "writeback"),
631
612
  }
632
613
  }
633
614
 
@@ -650,7 +631,7 @@ function toActionRunEffectsRecord(row: DatabaseRow): ActionRunEffectsRecord | un
650
631
  return {
651
632
  status: "failed",
652
633
  completedAt,
653
- error: toFailure(row.effects_error_name, row.effects_error_message, row.effects_error_phase),
634
+ error: parsePhaseFailure(row.effects_error, "effects"),
654
635
  }
655
636
  }
656
637
 
@@ -695,6 +676,7 @@ function rowToActionRunRecord(row: DatabaseRow): ActionRunRecord {
695
676
  return {
696
677
  id: row.id,
697
678
  projectId: row.project_id,
679
+ executionId: row.execution_id,
698
680
  actionId: row.action_id,
699
681
  subject: rowToActionSubject(row),
700
682
  status: row.status,
@@ -733,6 +715,7 @@ function isUniqueConstraintError(error: unknown): boolean {
733
715
  interface DatabaseRow {
734
716
  project_id: string
735
717
  id: string
718
+ execution_id: string
736
719
  action_id: string
737
720
  subject_kind: ActionSubject["kind"]
738
721
  object_type_id: string | null
@@ -747,15 +730,9 @@ interface DatabaseRow {
747
730
  writeback_status: ActionRunWritebackRecord["status"] | null
748
731
  writeback_completed_at: string | null
749
732
  writeback_result: string | null
750
- writeback_error_name: string | null
751
- writeback_error_message: string | null
752
- writeback_error_phase: ActionRunPhase | null
733
+ writeback_error: string | null
753
734
  effects_status: ActionRunEffectsRecord["status"] | null
754
735
  effects_completed_at: string | null
755
- effects_error_name: string | null
756
- effects_error_message: string | null
757
- effects_error_phase: ActionRunPhase | null
758
- error_name: string | null
759
- error_message: string | null
760
- error_phase: ActionRunPhase | null
736
+ effects_error: string | null
737
+ error: string | null
761
738
  }
@@ -1,4 +1,4 @@
1
- import type { AgentStorage } from "@sixb/core/storage"
1
+ import type { AgentStorage, ExecutionStorage } from "@sixb/core/storage"
2
2
  import { installFreshSqliteSchema } from "../migrations"
3
3
  import {
4
4
  closeSqliteStoreConnection,
@@ -14,6 +14,8 @@ export interface SqliteAgentStorageOptions {
14
14
  path?: string
15
15
  /** Internal shared connection used by bundled SqliteStorage. */
16
16
  connection?: SqliteStoreConnection
17
+ /** Execution ledger used to validate immutable run ownership. */
18
+ executions: ExecutionStorage
17
19
  }
18
20
 
19
21
  /**
@@ -28,7 +30,7 @@ export class SqliteAgentStorage implements AgentStorage {
28
30
 
29
31
  private readonly connection: SqliteStoreConnection
30
32
 
31
- constructor(options: SqliteAgentStorageOptions = {}) {
33
+ constructor(options: SqliteAgentStorageOptions) {
32
34
  this.connection = openSqliteStoreConnection(options)
33
35
 
34
36
  if (this.connection.installFreshSchema) {
@@ -36,7 +38,7 @@ export class SqliteAgentStorage implements AgentStorage {
36
38
  }
37
39
 
38
40
  this.threads = new SqliteAgentThreadStore(this.connection.db)
39
- this.runs = new SqliteAgentRunStore(this.connection.db)
41
+ this.runs = new SqliteAgentRunStore(this.connection.db, options.executions)
40
42
  this.messages = new SqliteAgentMessageStore(this.connection.db)
41
43
  }
42
44
 
@@ -1,10 +1,11 @@
1
1
  import type { Database } from "bun:sqlite"
2
- import { type AgentMessagePart, type Principal, SYSTEM_PRINCIPAL } from "@sixb/core"
2
+ import type { AgentMessagePart, Principal } from "@sixb/core"
3
+ import { parseSixbFailure } from "@sixb/core/internal/errors"
3
4
  import {
5
+ AGENT_RUN_FAILURE_CODES,
4
6
  type AgentMessageRecord,
5
7
  type AgentRunDiagnostic,
6
8
  type AgentRunRecord,
7
- type AgentRunUsage,
8
9
  type AgentThreadRecord,
9
10
  coerceAgentRunFinishReason,
10
11
  } from "@sixb/core/storage"
@@ -30,21 +31,14 @@ export interface AgentThreadRow {
30
31
  export interface AgentRunRow {
31
32
  project_id: string
32
33
  id: string
34
+ execution_id: string
33
35
  thread_id: string
34
36
  agent_id: string
35
37
  trigger_message_id: string
36
- requested_by_principal_type: Principal["type"] | null
37
- requested_by_principal_id: string | null
38
- execution_principal_type: "serviceAccount" | null
39
- execution_principal_id: string | null
38
+ requester_group_ids: string
40
39
  status: AgentRunRecord["status"]
41
40
  model_id: string | null
42
41
  finish_reason: string | null
43
- usage_input_tokens: number | null
44
- usage_output_tokens: number | null
45
- usage_total_tokens: number | null
46
- usage_reasoning_tokens: number | null
47
- usage_cached_input_tokens: number | null
48
42
  error: string | null
49
43
  diagnostics: string | null
50
44
  attempt: number
@@ -93,21 +87,15 @@ export function rowToRunRecord(row: AgentRunRow): AgentRunRecord {
93
87
  return {
94
88
  id: row.id,
95
89
  projectId: row.project_id,
90
+ executionId: row.execution_id,
96
91
  threadId: row.thread_id,
97
92
  agentId: row.agent_id,
98
93
  triggerMessageId: row.trigger_message_id,
99
- requestedByPrincipal:
100
- principalFromColumns(row.requested_by_principal_type, row.requested_by_principal_id) ??
101
- SYSTEM_PRINCIPAL,
102
- executionPrincipal: serviceAccountPrincipalFromColumns(
103
- row.execution_principal_type,
104
- row.execution_principal_id
105
- ),
94
+ requesterGroupIds: JSON.parse(row.requester_group_ids) as string[],
106
95
  status: row.status,
107
96
  modelId: row.model_id ?? undefined,
108
97
  finishReason: coerceAgentRunFinishReason(row.finish_reason),
109
- usage: rowToUsage(row),
110
- error: row.error ?? undefined,
98
+ error: row.error === null ? undefined : parseSixbFailure(row.error, AGENT_RUN_FAILURE_CODES),
111
99
  diagnostics:
112
100
  row.diagnostics === null ? undefined : (JSON.parse(row.diagnostics) as AgentRunDiagnostic[]),
113
101
  attempt: row.attempt,
@@ -150,26 +138,6 @@ function principalFromColumns(
150
138
  return type && id ? { type, id } : undefined
151
139
  }
152
140
 
153
- function serviceAccountPrincipalFromColumns(
154
- type: "serviceAccount" | null | undefined,
155
- id: string | null | undefined
156
- ): Extract<Principal, { readonly type: "serviceAccount" }> | undefined {
157
- return type === "serviceAccount" && id ? { type, id } : undefined
158
- }
159
-
160
- function rowToUsage(row: AgentRunRow): AgentRunUsage | undefined {
161
- const usage: AgentRunUsage = {
162
- ...(row.usage_input_tokens === null ? {} : { inputTokens: row.usage_input_tokens }),
163
- ...(row.usage_output_tokens === null ? {} : { outputTokens: row.usage_output_tokens }),
164
- ...(row.usage_total_tokens === null ? {} : { totalTokens: row.usage_total_tokens }),
165
- ...(row.usage_reasoning_tokens === null ? {} : { reasoningTokens: row.usage_reasoning_tokens }),
166
- ...(row.usage_cached_input_tokens === null
167
- ? {}
168
- : { cachedInputTokens: row.usage_cached_input_tokens }),
169
- }
170
- return Object.keys(usage).length === 0 ? undefined : usage
171
- }
172
-
173
141
  // ── offset pagination (threads / messages order by their own column, not started_at) ─────────────
174
142
 
175
143
  export function queryAgentList<TRow>(input: {
@@ -1,10 +1,15 @@
1
1
  import type { Database } from "bun:sqlite"
2
+ import { assertAgentRunExecution } from "@sixb/core/internal/agent-run-storage-provider"
3
+ import { normalizeRequesterGroupIds } from "@sixb/core/internal/auth"
4
+ import { serializeSixbFailure } from "@sixb/core/internal/errors"
2
5
  import {
6
+ AGENT_RUN_FAILURE_CODES,
3
7
  type AgentRunRecord,
4
8
  type AgentRunStore,
5
9
  AgentStorageError,
6
10
  type ConfirmAgentRunExecutionOwnershipInput,
7
11
  type CreateAgentRunInput,
12
+ type ExecutionStorage,
8
13
  type FinishAgentRunInput,
9
14
  type FinishQueuedAgentRunInput,
10
15
  type ListAgentRunsInput,
@@ -24,10 +29,20 @@ import { type AgentRunRow, rowToRunRecord } from "./rows"
24
29
  const SQLITE_RUN_ID_BATCH_SIZE = 500
25
30
 
26
31
  export class SqliteAgentRunStore implements AgentRunStore {
27
- constructor(private readonly db: Database) {}
32
+ constructor(
33
+ private readonly db: Database,
34
+ private readonly executions: ExecutionStorage
35
+ ) {}
28
36
 
29
37
  async create(input: CreateAgentRunInput): Promise<AgentRunRecord> {
30
38
  const createdAt = input.createdAt ?? new Date()
39
+ await assertAgentRunExecution({
40
+ executions: this.executions,
41
+ projectId: input.projectId,
42
+ executionId: input.executionId,
43
+ runId: input.id,
44
+ agentId: input.agentId,
45
+ })
31
46
 
32
47
  return this.db.transaction(() => {
33
48
  const thread = this.db
@@ -55,11 +70,11 @@ export class SqliteAgentRunStore implements AgentRunStore {
55
70
  INSERT INTO agent_runs (
56
71
  project_id,
57
72
  id,
73
+ execution_id,
58
74
  thread_id,
59
75
  agent_id,
60
76
  trigger_message_id,
61
- requested_by_principal_type,
62
- requested_by_principal_id,
77
+ requester_group_ids,
63
78
  status,
64
79
  attempt,
65
80
  created_at
@@ -69,11 +84,11 @@ export class SqliteAgentRunStore implements AgentRunStore {
69
84
  .run(
70
85
  input.projectId,
71
86
  input.id,
87
+ input.executionId,
72
88
  input.threadId,
73
89
  input.agentId,
74
90
  input.triggerMessageId,
75
- input.requestedByPrincipal.type,
76
- input.requestedByPrincipal.id,
91
+ JSON.stringify(normalizeRequesterGroupIds(input.requesterGroupIds)),
77
92
  createdAt.toISOString()
78
93
  )
79
94
  } catch (error) {
@@ -107,8 +122,6 @@ export class SqliteAgentRunStore implements AgentRunStore {
107
122
  UPDATE agent_runs
108
123
  SET
109
124
  status = 'running',
110
- execution_principal_type = ?,
111
- execution_principal_id = ?,
112
125
  model_id = ?,
113
126
  attempt = 1,
114
127
  execution_token = ?,
@@ -118,8 +131,6 @@ export class SqliteAgentRunStore implements AgentRunStore {
118
131
  `
119
132
  )
120
133
  .run(
121
- input.executionPrincipal?.type ?? null,
122
- input.executionPrincipal?.id ?? null,
123
134
  input.modelId ?? null,
124
135
  input.execution.token,
125
136
  input.execution.queueLeaseExpiresAt.toISOString(),
@@ -146,7 +157,9 @@ export class SqliteAgentRunStore implements AgentRunStore {
146
157
  )
147
158
  .run(
148
159
  input.status,
149
- input.error ?? null,
160
+ input.error === undefined
161
+ ? null
162
+ : serializeSixbFailure(input.error, AGENT_RUN_FAILURE_CODES),
150
163
  completedAt.toISOString(),
151
164
  input.projectId,
152
165
  input.id
@@ -208,7 +221,10 @@ export class SqliteAgentRunStore implements AgentRunStore {
208
221
 
209
222
  async finish(input: FinishAgentRunInput): Promise<AgentRunRecord> {
210
223
  const completedAt = input.completedAt ?? new Date()
211
- const errorValue = input.status === "succeeded" ? null : (input.error ?? null)
224
+ const errorValue =
225
+ input.status === "succeeded" || input.error === undefined
226
+ ? null
227
+ : serializeSixbFailure(input.error, AGENT_RUN_FAILURE_CODES)
212
228
 
213
229
  return this.db.transaction(() => {
214
230
  const run = this.requireRunning(input.projectId, input.id)
@@ -228,11 +244,6 @@ export class SqliteAgentRunStore implements AgentRunStore {
228
244
  status = ?,
229
245
  model_id = COALESCE(?, model_id),
230
246
  finish_reason = ?,
231
- usage_input_tokens = ?,
232
- usage_output_tokens = ?,
233
- usage_total_tokens = ?,
234
- usage_reasoning_tokens = ?,
235
- usage_cached_input_tokens = ?,
236
247
  error = ?,
237
248
  diagnostics = ?,
238
249
  execution_token = NULL,
@@ -245,11 +256,6 @@ export class SqliteAgentRunStore implements AgentRunStore {
245
256
  input.status,
246
257
  input.modelId ?? null,
247
258
  input.finishReason ?? null,
248
- input.usage?.inputTokens ?? null,
249
- input.usage?.outputTokens ?? null,
250
- input.usage?.totalTokens ?? null,
251
- input.usage?.reasoningTokens ?? null,
252
- input.usage?.cachedInputTokens ?? null,
253
259
  errorValue,
254
260
  input.diagnostics === undefined ? null : JSON.stringify(input.diagnostics),
255
261
  completedAt.toISOString(),