@sixb/sqlite 0.1.1 → 0.1.3

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 (123) 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 +9 -5
  5. package/dist/agents/agent-storage.d.ts.map +1 -1
  6. package/dist/agents/checkpoints.d.ts +14 -0
  7. package/dist/agents/checkpoints.d.ts.map +1 -0
  8. package/dist/agents/index.d.ts +1 -0
  9. package/dist/agents/index.d.ts.map +1 -1
  10. package/dist/agents/messages.d.ts +2 -1
  11. package/dist/agents/messages.d.ts.map +1 -1
  12. package/dist/agents/rows.d.ts +21 -11
  13. package/dist/agents/rows.d.ts.map +1 -1
  14. package/dist/agents/runs.d.ts +3 -2
  15. package/dist/agents/runs.d.ts.map +1 -1
  16. package/dist/ai-cost-storage.d.ts +15 -0
  17. package/dist/ai-cost-storage.d.ts.map +1 -0
  18. package/dist/ai-usage-storage.d.ts +22 -0
  19. package/dist/ai-usage-storage.d.ts.map +1 -0
  20. package/dist/auth-storage/service-accounts.d.ts.map +1 -1
  21. package/dist/auth-storage/shared.d.ts +1 -10
  22. package/dist/auth-storage/shared.d.ts.map +1 -1
  23. package/dist/connector-connection-storage.d.ts +9 -0
  24. package/dist/connector-connection-storage.d.ts.map +1 -0
  25. package/dist/execution-storage.d.ts +14 -0
  26. package/dist/execution-storage.d.ts.map +1 -0
  27. package/dist/index.d.ts +14 -9
  28. package/dist/index.d.ts.map +1 -1
  29. package/dist/index.js +4633 -1066
  30. package/dist/index.js.map +33 -28
  31. package/dist/latest-run-query.d.ts.map +1 -1
  32. package/dist/migrations.d.ts +0 -1
  33. package/dist/migrations.d.ts.map +1 -1
  34. package/dist/ontology-storage/materialization-session.d.ts +50 -8
  35. package/dist/ontology-storage/materialization-session.d.ts.map +1 -1
  36. package/dist/ontology-storage/materialization-state.d.ts +4 -5
  37. package/dist/ontology-storage/materialization-state.d.ts.map +1 -1
  38. package/dist/ontology-storage/materialization-writer.d.ts.map +1 -1
  39. package/dist/ontology-storage/materializations.d.ts +0 -2
  40. package/dist/ontology-storage/materializations.d.ts.map +1 -1
  41. package/dist/ontology-storage/outbox.d.ts.map +1 -1
  42. package/dist/ontology-storage/shared.d.ts +3 -3
  43. package/dist/ontology-storage/shared.d.ts.map +1 -1
  44. package/dist/ontology-storage/sources.d.ts.map +1 -1
  45. package/dist/pipeline-run-storage.d.ts +7 -2
  46. package/dist/pipeline-run-storage.d.ts.map +1 -1
  47. package/dist/projection-run-storage.d.ts +9 -2
  48. package/dist/projection-run-storage.d.ts.map +1 -1
  49. package/dist/run-list-query.d.ts +1 -1
  50. package/dist/run-list-query.d.ts.map +1 -1
  51. package/dist/sync-run-storage.d.ts +7 -2
  52. package/dist/sync-run-storage.d.ts.map +1 -1
  53. package/dist/testing.d.ts +6 -0
  54. package/dist/testing.d.ts.map +1 -0
  55. package/dist/transactions.d.ts +3 -0
  56. package/dist/transactions.d.ts.map +1 -1
  57. package/dist/webhook-run-storage.d.ts +9 -2
  58. package/dist/webhook-run-storage.d.ts.map +1 -1
  59. package/dist/workflow-run-storage.d.ts +7 -3
  60. package/dist/workflow-run-storage.d.ts.map +1 -1
  61. package/package.json +3 -3
  62. package/src/action-run-storage.ts +51 -74
  63. package/src/agents/agent-storage.ts +11 -6
  64. package/src/agents/checkpoints.ts +175 -0
  65. package/src/agents/index.ts +1 -0
  66. package/src/agents/messages.ts +59 -0
  67. package/src/agents/rows.ts +52 -40
  68. package/src/agents/runs.ts +27 -21
  69. package/src/ai-cost-storage.ts +744 -0
  70. package/src/ai-usage-storage.ts +324 -0
  71. package/src/auth-storage/service-accounts.ts +14 -21
  72. package/src/auth-storage/shared.ts +0 -22
  73. package/src/connector-connection-storage.ts +784 -0
  74. package/src/execution-storage.ts +158 -0
  75. package/src/index.ts +69 -22
  76. package/src/latest-run-query.ts +7 -1
  77. package/src/migrations/004-executions.sql +142 -0
  78. package/src/migrations/005-workflow-executions.sql +7 -0
  79. package/src/migrations/006-narrow-ontology-source-root-index.sql +4 -0
  80. package/src/migrations/007-split-overrides.sql +154 -0
  81. package/src/migrations/008-action-executions.sql +4 -0
  82. package/src/migrations/009-agent-executions.sql +115 -0
  83. package/src/migrations/010-ai-usage-accounting-foundation.sql +74 -0
  84. package/src/migrations/011-sync-failure-record.sql +22 -0
  85. package/src/migrations/012-pipeline-failure-record.sql +45 -0
  86. package/src/migrations/013-workflow-failure-record.sql +45 -0
  87. package/src/migrations/014-agent-failure-record.sql +59 -0
  88. package/src/migrations/015-projection-failure-record.sql +20 -0
  89. package/src/migrations/016-webhook-run-failure-record.sql +19 -0
  90. package/src/migrations/017-action-failure-record.sql +65 -0
  91. package/src/migrations/018-ontology-outbox-failure-record.sql +80 -0
  92. package/src/migrations/019-webhook-delivery-failure-record.sql +62 -0
  93. package/src/migrations/020-drop-run-usage-projections.sql +7 -0
  94. package/src/migrations/021-sync-pipeline-executions.sql +101 -0
  95. package/src/migrations/022-projection-executions.sql +244 -0
  96. package/src/migrations/023-webhook-executions.sql +51 -0
  97. package/src/migrations/024-ontology-commit-executions.sql +58 -0
  98. package/src/migrations/025-connector-connections.sql +257 -0
  99. package/src/migrations/026-ai-cost-accounting.sql +57 -0
  100. package/src/migrations/027-agent-context-checkpoints.sql +28 -0
  101. package/src/migrations/028-object-override-edit-times.sql +3 -0
  102. package/src/migrations.ts +129 -4
  103. package/src/ontology-storage/materialization-session.ts +317 -143
  104. package/src/ontology-storage/materialization-state.ts +323 -112
  105. package/src/ontology-storage/materialization-writer.ts +145 -67
  106. package/src/ontology-storage/materializations.ts +67 -141
  107. package/src/ontology-storage/outbox.ts +10 -3
  108. package/src/ontology-storage/shared.ts +10 -4
  109. package/src/ontology-storage/sources.ts +4 -1
  110. package/src/pipeline-run-storage.ts +128 -43
  111. package/src/projection-run-storage.ts +209 -97
  112. package/src/run-list-query.ts +9 -4
  113. package/src/sync-run-storage.ts +118 -32
  114. package/src/testing.ts +20 -0
  115. package/src/transactions.ts +10 -3
  116. package/src/webhook-run-storage.ts +118 -74
  117. package/src/workflow-run-storage.ts +117 -131
  118. package/dist/validation.d.ts +0 -5
  119. package/dist/validation.d.ts.map +0 -1
  120. package/dist/webhook-delivery-storage.d.ts +0 -27
  121. package/dist/webhook-delivery-storage.d.ts.map +0 -1
  122. package/src/validation.ts +0 -30
  123. package/src/webhook-delivery-storage.ts +0 -272
@@ -1,10 +1,12 @@
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,
6
+ type AgentContextCheckpointRecord,
4
7
  type AgentMessageRecord,
5
8
  type AgentRunDiagnostic,
6
9
  type AgentRunRecord,
7
- type AgentRunUsage,
8
10
  type AgentThreadRecord,
9
11
  coerceAgentRunFinishReason,
10
12
  } from "@sixb/core/storage"
@@ -30,21 +32,14 @@ export interface AgentThreadRow {
30
32
  export interface AgentRunRow {
31
33
  project_id: string
32
34
  id: string
35
+ execution_id: string
33
36
  thread_id: string
34
37
  agent_id: string
35
38
  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
39
+ requester_group_ids: string
40
40
  status: AgentRunRecord["status"]
41
41
  model_id: string | null
42
42
  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
43
  error: string | null
49
44
  diagnostics: string | null
50
45
  attempt: number
@@ -71,6 +66,23 @@ export interface AgentMessageRow {
71
66
  completed_at: string | null
72
67
  }
73
68
 
69
+ export interface AgentContextCheckpointRow {
70
+ project_id: string
71
+ id: string
72
+ thread_id: string
73
+ created_by_run_id: string
74
+ previous_checkpoint_id: string | null
75
+ reason: AgentContextCheckpointRecord["reason"]
76
+ summary: string
77
+ summary_format_version: number
78
+ summarized_through_seq: number
79
+ observed_head_seq: number
80
+ estimated_input_tokens_before: number
81
+ estimated_input_tokens_after: number
82
+ summary_model_id: string
83
+ created_at: string
84
+ }
85
+
74
86
  // ── row → record mappers ────────────────────────────────────────────────────────────────────────
75
87
 
76
88
  export function rowToThreadRecord(row: AgentThreadRow): AgentThreadRecord {
@@ -93,21 +105,15 @@ export function rowToRunRecord(row: AgentRunRow): AgentRunRecord {
93
105
  return {
94
106
  id: row.id,
95
107
  projectId: row.project_id,
108
+ executionId: row.execution_id,
96
109
  threadId: row.thread_id,
97
110
  agentId: row.agent_id,
98
111
  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
- ),
112
+ requesterGroupIds: JSON.parse(row.requester_group_ids) as string[],
106
113
  status: row.status,
107
114
  modelId: row.model_id ?? undefined,
108
115
  finishReason: coerceAgentRunFinishReason(row.finish_reason),
109
- usage: rowToUsage(row),
110
- error: row.error ?? undefined,
116
+ error: row.error === null ? undefined : parseSixbFailure(row.error, AGENT_RUN_FAILURE_CODES),
111
117
  diagnostics:
112
118
  row.diagnostics === null ? undefined : (JSON.parse(row.diagnostics) as AgentRunDiagnostic[]),
113
119
  attempt: row.attempt,
@@ -143,6 +149,32 @@ export function rowToMessageRecord(row: AgentMessageRow): AgentMessageRecord {
143
149
  }
144
150
  }
145
151
 
152
+ export function rowToContextCheckpointRecord(
153
+ row: AgentContextCheckpointRow
154
+ ): AgentContextCheckpointRecord {
155
+ if (row.summary_format_version !== 1) {
156
+ throw new Error(
157
+ `[SixbSqlite] Unsupported agent context checkpoint summary format version '${row.summary_format_version}'.`
158
+ )
159
+ }
160
+ return {
161
+ id: row.id,
162
+ projectId: row.project_id,
163
+ threadId: row.thread_id,
164
+ createdByRunId: row.created_by_run_id,
165
+ previousCheckpointId: row.previous_checkpoint_id ?? undefined,
166
+ reason: row.reason,
167
+ summary: row.summary,
168
+ summaryFormatVersion: 1,
169
+ summarizedThroughSeq: row.summarized_through_seq,
170
+ observedHeadSeq: row.observed_head_seq,
171
+ estimatedInputTokensBefore: row.estimated_input_tokens_before,
172
+ estimatedInputTokensAfter: row.estimated_input_tokens_after,
173
+ summaryModelId: row.summary_model_id,
174
+ createdAt: new Date(row.created_at),
175
+ }
176
+ }
177
+
146
178
  function principalFromColumns(
147
179
  type: Principal["type"] | null | undefined,
148
180
  id: string | null | undefined
@@ -150,26 +182,6 @@ function principalFromColumns(
150
182
  return type && id ? { type, id } : undefined
151
183
  }
152
184
 
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
185
  // ── offset pagination (threads / messages order by their own column, not started_at) ─────────────
174
186
 
175
187
  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(),