@sixb/pg 0.1.0 → 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 (99) hide show
  1. package/README.md +7 -6
  2. package/dist/agents/agent-storage.d.ts +2 -1
  3. package/dist/agents/agent-storage.d.ts.map +1 -1
  4. package/dist/agents/rows.d.ts +4 -11
  5. package/dist/agents/rows.d.ts.map +1 -1
  6. package/dist/agents/runs.d.ts +3 -2
  7. package/dist/agents/runs.d.ts.map +1 -1
  8. package/dist/auth-storage/service-accounts.d.ts.map +1 -1
  9. package/dist/auth-storage/shared.d.ts +0 -4
  10. package/dist/auth-storage/shared.d.ts.map +1 -1
  11. package/dist/connector-connection-storage.d.ts +9 -0
  12. package/dist/connector-connection-storage.d.ts.map +1 -0
  13. package/dist/index.d.ts +8 -5
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +3131 -732
  16. package/dist/index.js.map +28 -25
  17. package/dist/latest-run-query.d.ts.map +1 -1
  18. package/dist/migrations.d.ts.map +1 -1
  19. package/dist/ontology-storage/materialization-session.d.ts.map +1 -1
  20. package/dist/ontology-storage/materialization-state.d.ts +0 -2
  21. package/dist/ontology-storage/materialization-state.d.ts.map +1 -1
  22. package/dist/ontology-storage/materialization-writer.d.ts.map +1 -1
  23. package/dist/ontology-storage/materializations.d.ts.map +1 -1
  24. package/dist/ontology-storage/outbox.d.ts.map +1 -1
  25. package/dist/ontology-storage/shared.d.ts +3 -6
  26. package/dist/ontology-storage/shared.d.ts.map +1 -1
  27. package/dist/pg-action-run-storage.d.ts +3 -2
  28. package/dist/pg-action-run-storage.d.ts.map +1 -1
  29. package/dist/pg-ai-usage-storage.d.ts +16 -0
  30. package/dist/pg-ai-usage-storage.d.ts.map +1 -0
  31. package/dist/pg-execution-storage.d.ts +14 -0
  32. package/dist/pg-execution-storage.d.ts.map +1 -0
  33. package/dist/pg-pipeline-run-storage.d.ts +5 -2
  34. package/dist/pg-pipeline-run-storage.d.ts.map +1 -1
  35. package/dist/pg-projection-run-storage.d.ts +5 -2
  36. package/dist/pg-projection-run-storage.d.ts.map +1 -1
  37. package/dist/pg-sync-run-storage.d.ts +5 -2
  38. package/dist/pg-sync-run-storage.d.ts.map +1 -1
  39. package/dist/pg-webhook-run-storage.d.ts +5 -2
  40. package/dist/pg-webhook-run-storage.d.ts.map +1 -1
  41. package/dist/pg-workflow-run-storage.d.ts +5 -3
  42. package/dist/pg-workflow-run-storage.d.ts.map +1 -1
  43. package/dist/run-list-query.d.ts +1 -1
  44. package/dist/run-list-query.d.ts.map +1 -1
  45. package/dist/testing.d.ts +6 -0
  46. package/dist/testing.d.ts.map +1 -0
  47. package/package.json +5 -2
  48. package/src/agents/agent-storage.ts +3 -2
  49. package/src/agents/rows.ts +12 -43
  50. package/src/agents/runs.ts +29 -15
  51. package/src/auth-storage/service-accounts.ts +14 -15
  52. package/src/auth-storage/shared.ts +0 -8
  53. package/src/connector-connection-storage.ts +760 -0
  54. package/src/index.ts +32 -15
  55. package/src/latest-run-query.ts +7 -1
  56. package/src/migrations/002-workflow-run-output.sql +27 -0
  57. package/src/migrations/003-merge-sync-runs.sql +3 -0
  58. package/src/migrations/004-executions.sql +140 -0
  59. package/src/migrations/005-workflow-executions.sql +10 -0
  60. package/src/migrations/006-narrow-ontology-source-root-index.sql +4 -0
  61. package/src/migrations/007-split-overrides.sql +153 -0
  62. package/src/migrations/008-action-executions.sql +7 -0
  63. package/src/migrations/009-agent-executions.sql +21 -0
  64. package/src/migrations/010-ai-usage-accounting-foundation.sql +76 -0
  65. package/src/migrations/011-sync-failure-record.sql +23 -0
  66. package/src/migrations/012-pipeline-failure-record.sql +47 -0
  67. package/src/migrations/013-workflow-failure-record.sql +45 -0
  68. package/src/migrations/014-agent-failure-record.sql +56 -0
  69. package/src/migrations/015-projection-failure-record.sql +20 -0
  70. package/src/migrations/016-webhook-run-failure-record.sql +19 -0
  71. package/src/migrations/017-action-failure-record.sql +65 -0
  72. package/src/migrations/018-ontology-outbox-failure-record.sql +29 -0
  73. package/src/migrations/019-webhook-delivery-failure-record.sql +27 -0
  74. package/src/migrations/020-drop-run-usage-projections.sql +8 -0
  75. package/src/migrations/021-sync-pipeline-executions.sql +50 -0
  76. package/src/migrations/022-projection-executions.sql +115 -0
  77. package/src/migrations/023-webhook-executions.sql +30 -0
  78. package/src/migrations/024-ontology-commit-executions.sql +12 -0
  79. package/src/migrations/025-connector-connections.sql +231 -0
  80. package/src/migrations.ts +79 -1
  81. package/src/ontology-storage/materialization-session.ts +25 -28
  82. package/src/ontology-storage/materialization-state.ts +159 -43
  83. package/src/ontology-storage/materialization-writer.ts +146 -75
  84. package/src/ontology-storage/materializations.ts +3 -1
  85. package/src/ontology-storage/outbox.ts +9 -2
  86. package/src/ontology-storage/shared.ts +10 -20
  87. package/src/pg-action-run-storage.ts +43 -67
  88. package/src/pg-ai-usage-storage.ts +290 -0
  89. package/src/pg-execution-storage.ts +147 -0
  90. package/src/pg-pipeline-run-storage.ts +107 -41
  91. package/src/pg-projection-run-storage.ts +159 -91
  92. package/src/pg-sync-run-storage.ts +102 -30
  93. package/src/pg-webhook-run-storage.ts +89 -57
  94. package/src/pg-workflow-run-storage.ts +101 -130
  95. package/src/run-list-query.ts +9 -4
  96. package/src/testing.ts +18 -0
  97. package/dist/pg-webhook-delivery-storage.d.ts +0 -17
  98. package/dist/pg-webhook-delivery-storage.d.ts.map +0 -1
  99. package/src/pg-webhook-delivery-storage.ts +0 -180
@@ -1,9 +1,14 @@
1
+ import { assertAgentRunExecution } from "@sixb/core/internal/agent-run-storage-provider"
2
+ import { normalizeRequesterGroupIds } from "@sixb/core/internal/auth"
3
+ import { serializeSixbFailure } from "@sixb/core/internal/errors"
1
4
  import {
5
+ AGENT_RUN_FAILURE_CODES,
2
6
  type AgentRunRecord,
3
7
  type AgentRunStore,
4
8
  AgentStorageError,
5
9
  type ConfirmAgentRunExecutionOwnershipInput,
6
10
  type CreateAgentRunInput,
11
+ type ExecutionStorage,
7
12
  type FinishAgentRunInput,
8
13
  type FinishQueuedAgentRunInput,
9
14
  type ListAgentRunsInput,
@@ -20,10 +25,20 @@ import { type AgentRunRow, rowToRunRecord } from "./rows"
20
25
  const PG_RUN_ID_BATCH_SIZE = 10_000
21
26
 
22
27
  export class PgAgentRunStore implements AgentRunStore {
23
- constructor(private readonly sql: PgStoreClient) {}
28
+ constructor(
29
+ private readonly sql: PgStoreClient,
30
+ private readonly executions: ExecutionStorage
31
+ ) {}
24
32
 
25
33
  async create(input: CreateAgentRunInput): Promise<AgentRunRecord> {
26
34
  const createdAt = input.createdAt ?? new Date()
35
+ await assertAgentRunExecution({
36
+ executions: this.executions,
37
+ projectId: input.projectId,
38
+ executionId: input.executionId,
39
+ runId: input.id,
40
+ agentId: input.agentId,
41
+ })
27
42
 
28
43
  try {
29
44
  return await runPgTransaction(this.sql, async (tx) => {
@@ -51,22 +66,22 @@ export class PgAgentRunStore implements AgentRunStore {
51
66
  INSERT INTO agent_runs (
52
67
  project_id,
53
68
  id,
69
+ execution_id,
54
70
  thread_id,
55
71
  agent_id,
56
72
  trigger_message_id,
57
- requested_by_principal_type,
58
- requested_by_principal_id,
73
+ requester_group_ids,
59
74
  status,
60
75
  attempt,
61
76
  created_at
62
77
  ) VALUES (
63
78
  ${input.projectId},
64
79
  ${input.id},
80
+ ${input.executionId},
65
81
  ${input.threadId},
66
82
  ${input.agentId},
67
83
  ${input.triggerMessageId},
68
- ${input.requestedByPrincipal.type},
69
- ${input.requestedByPrincipal.id},
84
+ ${JSON.stringify(normalizeRequesterGroupIds(input.requesterGroupIds))}::text::jsonb,
70
85
  ${"queued"},
71
86
  ${0},
72
87
  ${createdAt}
@@ -101,8 +116,6 @@ export class PgAgentRunStore implements AgentRunStore {
101
116
  UPDATE agent_runs
102
117
  SET
103
118
  status = ${"running"},
104
- execution_principal_type = ${input.executionPrincipal?.type ?? null},
105
- execution_principal_id = ${input.executionPrincipal?.id ?? null},
106
119
  model_id = ${input.modelId ?? null},
107
120
  attempt = ${1},
108
121
  execution_token = ${input.execution.token},
@@ -121,7 +134,10 @@ export class PgAgentRunStore implements AgentRunStore {
121
134
  const run = await this.lockStatus(tx, input.projectId, input.id, "queued")
122
135
  const [row] = await tx<AgentRunRow[]>`
123
136
  UPDATE agent_runs
124
- SET status = ${input.status}, error = ${input.error ?? null}, completed_at = ${completedAt}
137
+ SET
138
+ status = ${input.status},
139
+ error = ${input.error === undefined ? null : serializeSixbFailure(input.error, AGENT_RUN_FAILURE_CODES)}::text::jsonb,
140
+ completed_at = ${completedAt}
125
141
  WHERE project_id = ${input.projectId} AND id = ${input.id}
126
142
  RETURNING *
127
143
  `
@@ -174,7 +190,10 @@ export class PgAgentRunStore implements AgentRunStore {
174
190
 
175
191
  async finish(input: FinishAgentRunInput): Promise<AgentRunRecord> {
176
192
  const completedAt = input.completedAt ?? new Date()
177
- const errorValue = input.status === "succeeded" ? null : (input.error ?? null)
193
+ const errorValue =
194
+ input.status === "succeeded" || input.error === undefined
195
+ ? null
196
+ : serializeSixbFailure(input.error, AGENT_RUN_FAILURE_CODES)
178
197
 
179
198
  return runPgTransaction(this.sql, async (tx) => {
180
199
  const run = await this.lockRunning(tx, input.projectId, input.id)
@@ -192,12 +211,7 @@ export class PgAgentRunStore implements AgentRunStore {
192
211
  status = ${input.status},
193
212
  model_id = COALESCE(${input.modelId ?? null}, model_id),
194
213
  finish_reason = ${input.finishReason ?? null},
195
- usage_input_tokens = ${input.usage?.inputTokens ?? null},
196
- usage_output_tokens = ${input.usage?.outputTokens ?? null},
197
- usage_total_tokens = ${input.usage?.totalTokens ?? null},
198
- usage_reasoning_tokens = ${input.usage?.reasoningTokens ?? null},
199
- usage_cached_input_tokens = ${input.usage?.cachedInputTokens ?? null},
200
- error = ${errorValue},
214
+ error = ${errorValue}::text::jsonb,
201
215
  diagnostics = ${input.diagnostics === undefined ? null : JSON.stringify(input.diagnostics)}::text::jsonb,
202
216
  execution_token = ${null},
203
217
  execution_queue_lease_expires_at = ${null},
@@ -85,28 +85,27 @@ export class PgAuthServiceAccountStore implements AuthServiceAccountStore {
85
85
  }
86
86
 
87
87
  async update(input: UpdateAuthServiceAccountInput): Promise<ServiceAccountRecord> {
88
- const existing = await this.getById(input)
89
- if (!existing) {
90
- throw new AuthStorageError(
91
- "missing_service_account",
92
- `[Sixb] Service account '${input.id}' not found for project '${input.projectId}'.`
93
- )
94
- }
95
-
88
+ const id = assertNonEmpty(input.id, "Service account id")
89
+ const projectId = assertNonEmpty(input.projectId, "Project id")
96
90
  const updatedAt = dateOrNow(input.updatedAt)
97
91
  const name =
98
- input.name === undefined ? existing.name : assertNonEmpty(input.name, "Service account name")
99
- const description = input.description === undefined ? existing.description : input.description
92
+ input.name === undefined ? null : assertNonEmpty(input.name, "Service account name")
100
93
  const [row] = await this.sql<PgAuthServiceAccountRow[]>`
101
94
  UPDATE auth_service_accounts
102
- SET name = ${name},
103
- description = ${description ?? null},
104
- status = ${input.status ?? existing.status},
95
+ SET name = COALESCE(${name}, name),
96
+ description = COALESCE(${input.description ?? null}, description),
97
+ status = COALESCE(${input.status ?? null}, status),
105
98
  updated_at = ${updatedAt}
106
- WHERE project_id = ${input.projectId}
107
- AND id = ${input.id}
99
+ WHERE project_id = ${projectId}
100
+ AND id = ${id}
108
101
  RETURNING *
109
102
  `
103
+ if (!row) {
104
+ throw new AuthStorageError(
105
+ "missing_service_account",
106
+ `[Sixb] Service account '${id}' not found for project '${projectId}'.`
107
+ )
108
+ }
110
109
 
111
110
  return rowToServiceAccountRecord(row)
112
111
  }
@@ -128,14 +128,6 @@ export async function getUserRowByEmail(
128
128
  return row ?? null
129
129
  }
130
130
 
131
- export async function getUserByEmail(
132
- sql: SQLClient,
133
- params: { readonly projectId: string; readonly email: string }
134
- ): Promise<UserRecord | null> {
135
- const row = await getUserRowByEmail(sql, params)
136
- return row ? rowToUserRecord(row) : null
137
- }
138
-
139
131
  export async function requireUserById(
140
132
  sql: SQLClient,
141
133
  params: { readonly projectId: string; readonly id: string }