@sixb/pg 0.2.5 → 0.2.7

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 (56) hide show
  1. package/dist/agents/agent-storage.d.ts +2 -0
  2. package/dist/agents/agent-storage.d.ts.map +1 -1
  3. package/dist/agents/approvals.d.ts +2 -1
  4. package/dist/agents/approvals.d.ts.map +1 -1
  5. package/dist/agents/checkpoints.d.ts +2 -1
  6. package/dist/agents/checkpoints.d.ts.map +1 -1
  7. package/dist/agents/inbox.d.ts.map +1 -1
  8. package/dist/agents/messages.d.ts +2 -1
  9. package/dist/agents/messages.d.ts.map +1 -1
  10. package/dist/agents/reads.d.ts +9 -0
  11. package/dist/agents/reads.d.ts.map +1 -0
  12. package/dist/agents/runs.d.ts +2 -1
  13. package/dist/agents/runs.d.ts.map +1 -1
  14. package/dist/agents/threads.d.ts.map +1 -1
  15. package/dist/index.js +593 -100
  16. package/dist/index.js.map +32 -31
  17. package/dist/latest-run-query.d.ts +3 -1
  18. package/dist/latest-run-query.d.ts.map +1 -1
  19. package/dist/migrations.d.ts.map +1 -1
  20. package/dist/pg-ai-cost-storage.d.ts.map +1 -1
  21. package/dist/pg-artifact-storage.d.ts.map +1 -1
  22. package/dist/pg-sync-run-storage.d.ts +2 -1
  23. package/dist/pg-sync-run-storage.d.ts.map +1 -1
  24. package/package.json +3 -3
  25. package/src/agents/agent-storage.ts +3 -0
  26. package/src/agents/approvals.ts +46 -2
  27. package/src/agents/checkpoints.ts +26 -0
  28. package/src/agents/inbox.ts +4 -1
  29. package/src/agents/messages.ts +81 -2
  30. package/src/agents/reads.ts +160 -0
  31. package/src/agents/runs.ts +25 -0
  32. package/src/agents/threads.ts +99 -15
  33. package/src/latest-run-query.ts +8 -1
  34. package/src/migrations/107-agent-inbox-edits.sql +3 -0
  35. package/src/migrations/108-agent-thread-reads.sql +73 -0
  36. package/src/migrations/109-sync-run-connections.sql +6 -0
  37. package/src/migrations.ts +6 -0
  38. package/src/ontology-storage/materialization-session.ts +1 -1
  39. package/src/ontology-storage/materializations.ts +1 -1
  40. package/src/ontology-storage/outbox.ts +3 -3
  41. package/src/ontology-storage/replacement-plans.ts +1 -1
  42. package/src/ontology-storage/source-roots.ts +1 -1
  43. package/src/ontology-storage/sources.ts +2 -2
  44. package/src/ontology-storage/vector-indexing.ts +1 -1
  45. package/src/pg-action-run-storage.ts +1 -1
  46. package/src/pg-activity-storage.ts +3 -3
  47. package/src/pg-ai-cost-storage.ts +36 -21
  48. package/src/pg-ai-usage-storage.ts +1 -1
  49. package/src/pg-artifact-storage.ts +22 -7
  50. package/src/pg-dataset-ingest-storage.ts +3 -3
  51. package/src/pg-execution-storage.ts +1 -1
  52. package/src/pg-projection-run-storage.ts +1 -1
  53. package/src/pg-sync-run-storage.ts +69 -0
  54. package/src/pg-workflow-run-storage.ts +1 -1
  55. package/src/share-grant-storage.ts +1 -1
  56. package/src/transactions.ts +2 -2
@@ -8,6 +8,7 @@ import {
8
8
  participantsToAdd,
9
9
  planAgentThreadFork,
10
10
  queuedTurnInCloud,
11
+ queuedTurnOnComputer,
11
12
  remapArtifactIds,
12
13
  snapshotAgentThreadSandboxParams,
13
14
  transitionAgentThreadSandbox,
@@ -238,6 +239,34 @@ export class PgAgentThreadStore implements AgentThreadStore {
238
239
  RETURNING *
239
240
  `
240
241
 
242
+ // The owner of the copy keeps their place: copied messages keep their seq, so what they
243
+ // had not read in the source is what they have not read here. Only a user reads.
244
+ if (input.ownerPrincipal.type === "user") {
245
+ await tx`
246
+ INSERT INTO agent_thread_reads (
247
+ project_id,
248
+ thread_id,
249
+ principal_type,
250
+ principal_id,
251
+ through_seq,
252
+ read_at
253
+ )
254
+ SELECT
255
+ ${input.projectId},
256
+ ${input.id},
257
+ 'user',
258
+ ${input.ownerPrincipal.id},
259
+ LEAST(source.through_seq, ${plan.headSeq}),
260
+ source.read_at
261
+ FROM agent_thread_reads AS source
262
+ WHERE source.project_id = ${input.projectId}
263
+ AND source.thread_id = ${input.sourceThreadId}
264
+ AND source.principal_type = 'user'
265
+ AND source.principal_id = ${input.ownerPrincipal.id}
266
+ ON CONFLICT DO NOTHING
267
+ `
268
+ }
269
+
241
270
  if (plan.messages.length > 0) {
242
271
  await tx`
243
272
  INSERT INTO agent_messages (
@@ -270,8 +299,8 @@ export class PgAgentThreadStore implements AgentThreadStore {
270
299
  message.created_at,
271
300
  message.completed_at
272
301
  FROM unnest(
273
- ${tx.array(plan.messages.map((copy) => copy.sourceId))}::text[],
274
- ${tx.array(plan.messages.map((copy) => copy.id))}::text[]
302
+ ${plan.messages.map((copy) => copy.sourceId)}::text[],
303
+ ${plan.messages.map((copy) => copy.id)}::text[]
275
304
  ) AS copy(source_id, id)
276
305
  JOIN agent_messages AS message
277
306
  ON message.project_id = ${input.projectId} AND message.id = copy.source_id
@@ -323,7 +352,7 @@ export class PgAgentThreadStore implements AgentThreadStore {
323
352
  SELECT *
324
353
  FROM agent_threads
325
354
  WHERE project_id = ${params.projectId}
326
- AND id = ANY(${this.sql.array(ids)}::text[])
355
+ AND id = ANY(${ids}::text[])
327
356
  `
328
357
 
329
358
  const byId = new Map(rows.map((row) => [row.id, row]))
@@ -447,6 +476,34 @@ export class PgAgentThreadStore implements AgentThreadStore {
447
476
  `
448
477
  }
449
478
 
479
+ // Someone who joins starts caught up: what came before is not new to them. A place left
480
+ // from an earlier stay, or written by a mark that raced the removal, does not carry over.
481
+ for (const principal of added) {
482
+ await tx`
483
+ INSERT INTO agent_thread_reads (
484
+ project_id,
485
+ thread_id,
486
+ principal_type,
487
+ principal_id,
488
+ through_seq,
489
+ read_at
490
+ ) VALUES (
491
+ ${input.projectId},
492
+ ${input.threadId},
493
+ ${principal.type},
494
+ ${principal.id},
495
+ (
496
+ SELECT COALESCE(MAX(seq), 0) FROM agent_messages
497
+ WHERE project_id = ${input.projectId} AND thread_id = ${input.threadId}
498
+ ),
499
+ ${addedAt}
500
+ )
501
+ ON CONFLICT (project_id, thread_id, principal_type, principal_id) DO UPDATE SET
502
+ through_seq = EXCLUDED.through_seq,
503
+ read_at = EXCLUDED.read_at
504
+ `
505
+ }
506
+
450
507
  return added.length === 0
451
508
  ? existing
452
509
  : selectParticipants(tx, input.projectId, [input.threadId])
@@ -454,16 +511,26 @@ export class PgAgentThreadStore implements AgentThreadStore {
454
511
  }
455
512
 
456
513
  async removeParticipant(input: RemoveAgentThreadParticipantInput): Promise<boolean> {
457
- const rows = await this.sql<{ principal_id: string }[]>`
458
- DELETE FROM agent_thread_participants
459
- WHERE project_id = ${input.projectId}
460
- AND thread_id = ${input.threadId}
461
- AND principal_type = ${input.principal.type}
462
- AND principal_id = ${input.principal.id}
463
- RETURNING principal_id
464
- `
514
+ return runPgTransaction(this.sql, async (tx) => {
515
+ const rows = await tx<{ principal_id: string }[]>`
516
+ DELETE FROM agent_thread_participants
517
+ WHERE project_id = ${input.projectId}
518
+ AND thread_id = ${input.threadId}
519
+ AND principal_type = ${input.principal.type}
520
+ AND principal_id = ${input.principal.id}
521
+ RETURNING principal_id
522
+ `
465
523
 
466
- return rows.length > 0
524
+ await tx`
525
+ DELETE FROM agent_thread_reads
526
+ WHERE project_id = ${input.projectId}
527
+ AND thread_id = ${input.threadId}
528
+ AND principal_type = ${input.principal.type}
529
+ AND principal_id = ${input.principal.id}
530
+ `
531
+
532
+ return rows.length > 0
533
+ })
467
534
  }
468
535
 
469
536
  async assignComputer(input: AssignAgentThreadComputerInput): Promise<AgentThreadRecord> {
@@ -471,12 +538,29 @@ export class PgAgentThreadStore implements AgentThreadStore {
471
538
  return runPgTransaction(this.sql, async (tx) => {
472
539
  const { thread, active } = await lockThreadAndActiveRun(tx, input)
473
540
  assertAssignableComputer(thread, active, input.computer, input.id, "SixbPg")
541
+ const queued = queuedTurnOnComputer(active, input.computer)
542
+ if (queued) {
543
+ await tx`
544
+ UPDATE agent_runs SET spec = ${JSON.stringify(queued.spec)}::text::jsonb
545
+ WHERE project_id = ${input.projectId} AND id = ${queued.id}
546
+ `
547
+ }
548
+ if (thread.runsOn !== "computer") {
549
+ await tx`
550
+ UPDATE agent_threads
551
+ SET sandbox_state = jsonb_set(sandbox_state, '{status}', '"unavailable"'::jsonb)
552
+ WHERE project_id = ${input.projectId}
553
+ AND parent_thread_id = ${input.id}
554
+ AND sandbox_state->>'status' IN ('new', 'ready')
555
+ `
556
+ }
474
557
  const movedAt = movesComputerPlace(thread, input.computer)
475
558
  ? (input.movedAt ?? new Date())
476
559
  : null
477
560
  const [assigned] = await tx<AgentThreadRow[]>`
478
561
  UPDATE agent_threads
479
- SET computer_id = ${input.computer.id},
562
+ SET runs_on = 'computer',
563
+ computer_id = ${input.computer.id},
480
564
  computer_name = ${input.computer.name},
481
565
  computer_folder = ${input.computer.folder},
482
566
  moved_at = COALESCE(${movedAt}::timestamptz, moved_at)
@@ -573,7 +657,7 @@ async function selectParticipants(
573
657
  SELECT *
574
658
  FROM agent_thread_participants
575
659
  WHERE project_id = ${projectId}
576
- AND thread_id = ANY(${sql.array([...threadIds])}::text[])
660
+ AND thread_id = ANY(${[...threadIds]}::text[])
577
661
  ORDER BY added_at, principal_id COLLATE "C", thread_id COLLATE "C"
578
662
  `
579
663
 
@@ -628,7 +712,7 @@ async function remapCopiedParts(
628
712
 
629
713
  const rows = await sql<{ id: string; parts: string }[]>`
630
714
  SELECT id, parts::text AS parts FROM agent_messages
631
- WHERE project_id = ${projectId} AND id = ANY(${sql.array([...messageIds])}::text[])
715
+ WHERE project_id = ${projectId} AND id = ANY(${[...messageIds]}::text[])
632
716
  `
633
717
  for (const row of rows) {
634
718
  const parts = remapArtifactIds(row.parts, copies)
@@ -17,6 +17,8 @@ export async function queryLatestRunsByOwnerId<TRow>(
17
17
  readonly selectList?: PgLatestRunSelectList
18
18
  /** A status that never counts as the latest run, such as a run made stale by a newer one. */
19
19
  readonly excludedStatus?: string
20
+ /** One more condition a row must meet to count. `bind` adds a parameter, named as it returns. */
21
+ readonly filter?: (bind: (value: SqlParameter) => string) => string
20
22
  } & PgLatestRunTarget
21
23
  ): Promise<readonly TRow[]> {
22
24
  const ownerIds = [...new Set(input.ownerIds)]
@@ -31,6 +33,11 @@ export async function queryLatestRunsByOwnerId<TRow>(
31
33
  parameters.push(input.excludedStatus)
32
34
  statusFilter = ` AND status <> $${parameters.length}`
33
35
  }
36
+ const bind = (value: SqlParameter) => {
37
+ parameters.push(value)
38
+ return `$${parameters.length}`
39
+ }
40
+ const filter = input.filter === undefined ? "" : ` AND ${input.filter(bind)}`
34
41
  const orderColumn =
35
42
  input.tableName === "sync_runs" ||
36
43
  input.tableName === "pipeline_runs" ||
@@ -41,7 +48,7 @@ export async function queryLatestRunsByOwnerId<TRow>(
41
48
  `
42
49
  SELECT DISTINCT ON (${input.ownerColumn}) ${input.selectList ?? "*"}
43
50
  FROM ${input.tableName}
44
- WHERE project_id = $1 AND ${input.ownerColumn} IN (${placeholders})${statusFilter}
51
+ WHERE project_id = $1 AND ${input.ownerColumn} IN (${placeholders})${statusFilter}${filter}
45
52
  ORDER BY ${input.ownerColumn}, ${orderColumn} DESC, id DESC
46
53
  `,
47
54
  parameters
@@ -0,0 +1,3 @@
1
+ -- An edit sent while a turn runs waits here for that turn to end, then rewinds the thread to the
2
+ -- message it replaces.
3
+ ALTER TABLE agent_thread_inbox ADD COLUMN replaces_message_id TEXT;
@@ -0,0 +1,73 @@
1
+ -- Where each person's reading of a conversation has reached: the seq of the last message they saw.
2
+ -- Nothing writes here when a message arrives: new ones sit past it until the reader moves it. A
3
+ -- retry or an edit that deletes messages pulls every place inside them back to just before them,
4
+ -- because the numbers they held go to what replaces them.
5
+ CREATE TABLE agent_thread_reads (
6
+ project_id TEXT NOT NULL,
7
+ thread_id TEXT NOT NULL,
8
+ principal_type TEXT NOT NULL CHECK (principal_type = 'user'),
9
+ principal_id TEXT NOT NULL CHECK (length(principal_id) > 0),
10
+ through_seq INTEGER NOT NULL CHECK (through_seq >= 0),
11
+ read_at TIMESTAMPTZ NOT NULL,
12
+ PRIMARY KEY (project_id, thread_id, principal_type, principal_id),
13
+ FOREIGN KEY (project_id, thread_id) REFERENCES agent_threads (project_id, id) ON DELETE CASCADE
14
+ );
15
+
16
+ -- Everyone starts caught up: the first deploy must not mark every thread that already existed as
17
+ -- new. Only a person reads, so an owner that is system code or a service account gets no row; it
18
+ -- would break the CHECK above. From here on, a participant gets a row when they join, and an
19
+ -- owner when they first mark the thread read: until then, they read as 0.
20
+ INSERT INTO agent_thread_reads (
21
+ project_id,
22
+ thread_id,
23
+ principal_type,
24
+ principal_id,
25
+ through_seq,
26
+ read_at
27
+ )
28
+ SELECT
29
+ project_id,
30
+ id,
31
+ owner_principal_type,
32
+ owner_principal_id,
33
+ COALESCE(
34
+ (
35
+ SELECT MAX(seq) FROM agent_messages
36
+ WHERE agent_messages.project_id = agent_threads.project_id
37
+ AND agent_messages.thread_id = agent_threads.id
38
+ ),
39
+ 0
40
+ ),
41
+ now()
42
+ FROM agent_threads
43
+ WHERE kind = 'conversation'
44
+ AND owner_principal_type = 'user'
45
+ ON CONFLICT DO NOTHING;
46
+
47
+ INSERT INTO agent_thread_reads (
48
+ project_id,
49
+ thread_id,
50
+ principal_type,
51
+ principal_id,
52
+ through_seq,
53
+ read_at
54
+ )
55
+ SELECT
56
+ participant.project_id,
57
+ participant.thread_id,
58
+ participant.principal_type,
59
+ participant.principal_id,
60
+ COALESCE(
61
+ (
62
+ SELECT MAX(seq) FROM agent_messages
63
+ WHERE agent_messages.project_id = thread.project_id
64
+ AND agent_messages.thread_id = thread.id
65
+ ),
66
+ 0
67
+ ),
68
+ now()
69
+ FROM agent_thread_participants AS participant
70
+ JOIN agent_threads AS thread
71
+ ON thread.project_id = participant.project_id
72
+ AND thread.id = participant.thread_id
73
+ ON CONFLICT DO NOTHING;
@@ -0,0 +1,6 @@
1
+ -- A sync of an OAuth connector runs once per connection. The column stays NULL for a sync of a
2
+ -- static connector, and for runs from before this migration.
3
+ ALTER TABLE sync_runs ADD COLUMN connection_id TEXT;
4
+
5
+ CREATE INDEX idx_sync_runs_project_sync_connection_started
6
+ ON sync_runs (project_id, sync_id, connection_id, COALESCE(started_at, queued_at) DESC);
package/src/migrations.ts CHANGED
@@ -477,6 +477,9 @@ export const postgresStorageMigrations = defineMigrations<PostgresMigrationConte
477
477
  pgSql("104-artifact-pdfs", artifactPdfsSql),
478
478
  pgSql("105-guest-comment-links", guestCommentLinksSql),
479
479
  pgSql("106-agent-thread-inbox", agentThreadInboxSql),
480
+ pgSql("107-agent-inbox-edits", agentInboxEditsSql),
481
+ pgSql("108-agent-thread-reads", agentThreadReadsSql),
482
+ pgSql("109-sync-run-connections", syncRunConnectionsSql),
480
483
  ],
481
484
  })
482
485
 
@@ -583,3 +586,6 @@ import scriptExecutionsSql from "./migrations/103-script-executions.sql" with {
583
586
  import artifactPdfsSql from "./migrations/104-artifact-pdfs.sql" with { type: "text" }
584
587
  import guestCommentLinksSql from "./migrations/105-guest-comment-links.sql" with { type: "text" }
585
588
  import agentThreadInboxSql from "./migrations/106-agent-thread-inbox.sql" with { type: "text" }
589
+ import agentInboxEditsSql from "./migrations/107-agent-inbox-edits.sql" with { type: "text" }
590
+ import agentThreadReadsSql from "./migrations/108-agent-thread-reads.sql" with { type: "text" }
591
+ import syncRunConnectionsSql from "./migrations/109-sync-run-connections.sql" with { type: "text" }
@@ -227,7 +227,7 @@ export class PgMaterializationSessions {
227
227
  AND (sort_one, sort_two, record_key) > (${after[0]}, ${after[1]}, ${after[2]})
228
228
  )
229
229
  SELECT sort_one, sort_two, record_key, payload FROM changes
230
- WHERE ref->>'objectTypeId' = ANY(${this.sql.array([...objectTypeIds])}::text[])
230
+ WHERE ref->>'objectTypeId' = ANY(${[...objectTypeIds]}::text[])
231
231
  OR EXISTS (
232
232
  SELECT 1 FROM object_vectors
233
233
  WHERE project_id = ${session.header.commit.projectId}
@@ -373,7 +373,7 @@ export class PgOntologyMaterializationStorage implements OntologyMaterialization
373
373
  SELECT * FROM ontology_sources
374
374
  WHERE project_id = ${projectId}
375
375
  AND source_id = ANY(
376
- ${this.sql.array(expectedSources.map((expected) => expected.source.projectionId))}::text[]
376
+ ${expectedSources.map((expected) => expected.source.projectionId)}::text[]
377
377
  )
378
378
  AND status = 'active'
379
379
  FOR UPDATE
@@ -87,7 +87,7 @@ export class PgOntologyOutboxStorage implements OntologyOutboxStorage {
87
87
  JOIN executions
88
88
  ON executions.project_id = commits.project_id AND executions.id = commits.execution_id
89
89
  WHERE commits.project_id = ${input.projectId}
90
- AND commits.id = ANY(${sql.array(commitIds)}::text[])
90
+ AND commits.id = ANY(${commitIds}::text[])
91
91
  `
92
92
  const contextOf = new Map(
93
93
  contexts.map((row) => [row.commit_id, outboxEventContext(input.projectId, row)])
@@ -106,7 +106,7 @@ export class PgOntologyOutboxStorage implements OntologyOutboxStorage {
106
106
  WITH leased AS MATERIALIZED (
107
107
  SELECT id FROM ontology_outbox
108
108
  WHERE project_id = ${input.projectId}
109
- AND id = ANY(${sql.array(ids)}::text[])
109
+ AND id = ANY(${ids}::text[])
110
110
  AND lease_id = ${input.leaseId}
111
111
  AND lease_expires_at IS NOT NULL
112
112
  FOR UPDATE
@@ -138,7 +138,7 @@ export class PgOntologyOutboxStorage implements OntologyOutboxStorage {
138
138
  WITH leased AS MATERIALIZED (
139
139
  SELECT id FROM ontology_outbox
140
140
  WHERE project_id = ${input.projectId}
141
- AND id = ANY(${sql.array(ids)}::text[])
141
+ AND id = ANY(${ids}::text[])
142
142
  AND lease_id = ${input.leaseId}
143
143
  AND lease_expires_at IS NOT NULL
144
144
  FOR UPDATE
@@ -708,7 +708,7 @@ async function endpoints(
708
708
  AND primary_id = requested.key::jsonb->>1
709
709
  )
710
710
  ) AS exists
711
- FROM unnest(${sql.array(unique.map((ref) => objectRefKey(ref)))}::text[]) AS requested(key)
711
+ FROM unnest(${unique.map((ref) => objectRefKey(ref))}::text[]) AS requested(key)
712
712
  `
713
713
  const existence = new Map(rows.map((row) => [row.key, row.exists] as const))
714
714
  return unique.map((ref) => ({ ref, exists: existence.get(objectRefKey(ref)) ?? false }))
@@ -160,7 +160,7 @@ export async function stageSourceRoots(
160
160
  >`
161
161
  SELECT root_key, staging_ordinal, deleted FROM ontology_source_roots
162
162
  WHERE version_id = ${manifest.version_id}
163
- AND root_key = ANY(${sql.array(skipped.map((root) => root.rootKey))}::text[])
163
+ AND root_key = ANY(${skipped.map((root) => root.rootKey)}::text[])
164
164
  `
165
165
  const existing = new Map(stored.map((root) => [root.root_key, root]))
166
166
  if (
@@ -532,8 +532,8 @@ export class PgOntologySourceStorage implements OntologySourceStorage {
532
532
  JOIN ontology_source_rows AS rows ON rows.root_id = roots.id
533
533
  WHERE roots.version_id = ${manifest.version_id}
534
534
  AND (
535
- roots.root_key = ANY(${sql.array(rootKeys)}::text[])
536
- OR roots.staging_ordinal = ANY(${sql.array(ordinals)}::bigint[])
535
+ roots.root_key = ANY(${rootKeys}::text[])
536
+ OR roots.staging_ordinal = ANY(${ordinals}::bigint[])
537
537
  )
538
538
  `
539
539
  return existing.map((row) => sourceStageRow(sourceAssertion(row)))
@@ -68,7 +68,7 @@ export class PgVectorIndexingStorage implements OntologyVectorIndexingStorage {
68
68
  async dispatched(input: Parameters<OntologyVectorIndexingStorage["dispatched"]>[0]) {
69
69
  if (!input.ids.length) return
70
70
  await this.run(async (sql) => {
71
- await sql`UPDATE object_vector_indexing SET dispatch_at = ${input.nextDispatchAt} WHERE project_id = ${input.projectId} AND id = ANY(${sql.array([...input.ids])}::text[])`
71
+ await sql`UPDATE object_vector_indexing SET dispatch_at = ${input.nextDispatchAt} WHERE project_id = ${input.projectId} AND id = ANY(${[...input.ids]}::text[])`
72
72
  })
73
73
  }
74
74
  async get(input: Parameters<OntologyVectorIndexingStorage["get"]>[0]) {
@@ -149,7 +149,7 @@ export class PgActionRunStorage implements ActionRunStorage {
149
149
  SELECT *
150
150
  FROM action_runs
151
151
  WHERE project_id = ${params.projectId}
152
- AND execution_id = ANY(${this.sql.array(executionIds)}::text[])
152
+ AND execution_id = ANY(${executionIds}::text[])
153
153
  `
154
154
 
155
155
  const byExecution = new Map(rows.map((row) => [row.execution_id, row]))
@@ -302,7 +302,7 @@ export class PgActivityStorage implements ActivityStorage {
302
302
  FROM activity_visits v
303
303
  WHERE v.project_id = ${input.projectId}
304
304
  AND v.actor_type = 'user'
305
- AND v.session_id = ANY(${this.sql.array([...input.sessionIds])}::text[])
305
+ AND v.session_id = ANY(${[...input.sessionIds]}::text[])
306
306
  AND v.user_id = ${input.userId}
307
307
  ) ranked
308
308
  WHERE position <= ${input.limit}
@@ -387,7 +387,7 @@ async function readStoredEvents(
387
387
  FROM activity_page_views p
388
388
  JOIN activity_visits v ON v.project_id = p.project_id AND v.id = p.visit_id
389
389
  WHERE p.project_id = ${input.projectId}
390
- AND p.id = ANY(${sql.array(ids)}::text[])
390
+ AND p.id = ANY(${ids}::text[])
391
391
  AND v.actor_type = ${input.actor.type}
392
392
  AND v.session_id = ${input.sessionId}
393
393
  UNION ALL
@@ -395,7 +395,7 @@ async function readStoredEvents(
395
395
  FROM activity_interactions i
396
396
  JOIN activity_visits v ON v.project_id = i.project_id AND v.id = i.visit_id
397
397
  WHERE i.project_id = ${input.projectId}
398
- AND i.id = ANY(${sql.array(ids)}::text[])
398
+ AND i.id = ANY(${ids}::text[])
399
399
  AND v.actor_type = ${input.actor.type}
400
400
  AND v.session_id = ${input.sessionId}
401
401
  `
@@ -1,3 +1,4 @@
1
+ import { wallClockInstant } from "@sixb/core/calendar"
1
2
  import {
2
3
  type AiAccountingAggregateFragment,
3
4
  type AiAccountingRecordSetItem,
@@ -82,7 +83,7 @@ export class PgAiCostStorage implements AiCostStorage {
82
83
  const ids = [...new Set(input.executionIds)]
83
84
  const rows = await this.sql<SummaryRow[]>`
84
85
  WITH requested(execution_id) AS (
85
- SELECT DISTINCT * FROM unnest(${this.sql.array(ids)}::text[])
86
+ SELECT DISTINCT * FROM unnest(${ids}::text[])
86
87
  )
87
88
  SELECT usage.execution_id,
88
89
  cost.status,
@@ -101,6 +102,7 @@ export class PgAiCostStorage implements AiCostStorage {
101
102
  const query = normalizeAiAccountingQuery(input)
102
103
  const providerId = query.providerId ?? null
103
104
  const modelId = query.modelId ?? null
105
+ const timeZone = query.timeZone
104
106
  const filter = pgAccountingFilterValues(query)
105
107
  const rows = await this.sql<AggregateRow[]>`
106
108
  WITH filtered AS (
@@ -187,29 +189,31 @@ export class PgAiCostStorage implements AiCostStorage {
187
189
  NULL::text AS key_1,
188
190
  NULL::text AS key_2,
189
191
  NULL::text AS key_3,
190
- NULL::timestamptz AS bucket_start,
192
+ NULL::text AS bucket_start,
191
193
  classified.*
192
194
  FROM classified
193
195
  UNION ALL
194
196
  SELECT 'series', NULL, NULL, NULL,
195
- CASE ${input.bucket}::text
196
- WHEN 'hour' THEN
197
- date_trunc('hour', occurred_at AT TIME ZONE 'UTC') AT TIME ZONE 'UTC'
198
- WHEN 'day' THEN
199
- date_trunc('day', occurred_at AT TIME ZONE 'UTC') AT TIME ZONE 'UTC'
200
- ELSE date_trunc('week', occurred_at AT TIME ZONE 'UTC') AT TIME ZONE 'UTC'
201
- END,
197
+ to_char(
198
+ CASE ${input.bucket}::text
199
+ WHEN 'hour' THEN date_trunc('hour', occurred_at AT TIME ZONE ${timeZone})
200
+ WHEN 'day' THEN date_trunc('day', occurred_at AT TIME ZONE ${timeZone})
201
+ ELSE date_trunc('week', occurred_at AT TIME ZONE ${timeZone})
202
+ END,
203
+ 'YYYY-MM-DD"T"HH24:MI:SS"Z"'
204
+ ),
202
205
  classified.*
203
206
  FROM classified
204
207
  UNION ALL
205
208
  SELECT 'modelSeries', provider_id, requested_model_id, NULL,
206
- CASE ${input.bucket}::text
207
- WHEN 'hour' THEN
208
- date_trunc('hour', occurred_at AT TIME ZONE 'UTC') AT TIME ZONE 'UTC'
209
- WHEN 'day' THEN
210
- date_trunc('day', occurred_at AT TIME ZONE 'UTC') AT TIME ZONE 'UTC'
211
- ELSE date_trunc('week', occurred_at AT TIME ZONE 'UTC') AT TIME ZONE 'UTC'
212
- END,
209
+ to_char(
210
+ CASE ${input.bucket}::text
211
+ WHEN 'hour' THEN date_trunc('hour', occurred_at AT TIME ZONE ${timeZone})
212
+ WHEN 'day' THEN date_trunc('day', occurred_at AT TIME ZONE ${timeZone})
213
+ ELSE date_trunc('week', occurred_at AT TIME ZONE ${timeZone})
214
+ END,
215
+ 'YYYY-MM-DD"T"HH24:MI:SS"Z"'
216
+ ),
213
217
  classified.*
214
218
  FROM classified
215
219
  UNION ALL
@@ -275,7 +279,10 @@ export class PgAiCostStorage implements AiCostStorage {
275
279
  FROM expanded
276
280
  GROUP BY dimension, key_1, key_2, key_3, bucket_start, amount_currency
277
281
  `
278
- return buildAiAccountingOverviewFromFragments(input, rows.map(aggregateFragmentFromRow))
282
+ return buildAiAccountingOverviewFromFragments(
283
+ input,
284
+ rows.map((row) => aggregateFragmentFromRow(row, timeZone))
285
+ )
279
286
  }
280
287
 
281
288
  async listModelCalls(
@@ -496,7 +503,7 @@ interface AggregateRow {
496
503
  readonly key_1: string | null
497
504
  readonly key_2: string | null
498
505
  readonly key_3: string | null
499
- readonly bucket_start: Date | string | null
506
+ readonly bucket_start: string | null
500
507
  readonly amount_currency: string | null
501
508
  readonly model_call_count: string
502
509
  readonly reported_usage_call_count: string
@@ -565,7 +572,15 @@ async function findUsage(
565
572
  return row ? usageFromRow(row, []) : null
566
573
  }
567
574
 
568
- function aggregateFragmentFromRow(row: AggregateRow): AiAccountingAggregateFragment {
575
+ /**
576
+ * One aggregated row as a fragment. A series row names its period as the zone's wall clock, which
577
+ * is resolved here: PostgreSQL's own reading of a repeated or skipped wall clock differs from the
578
+ * calendar the page draws its bars on.
579
+ */
580
+ function aggregateFragmentFromRow(
581
+ row: AggregateRow,
582
+ timeZone: string
583
+ ): AiAccountingAggregateFragment {
569
584
  const dimensionKeys =
570
585
  row.dimension === "model"
571
586
  ? { providerId: required(row.key_1), modelId: required(row.key_2) }
@@ -573,7 +588,7 @@ function aggregateFragmentFromRow(row: AggregateRow): AiAccountingAggregateFragm
573
588
  ? {
574
589
  providerId: required(row.key_1),
575
590
  modelId: required(row.key_2),
576
- start: new Date(required(row.bucket_start)),
591
+ start: wallClockInstant(required(row.bucket_start), timeZone),
577
592
  }
578
593
  : row.dimension === "person"
579
594
  ? row.key_1 && row.key_2
@@ -591,7 +606,7 @@ function aggregateFragmentFromRow(row: AggregateRow): AiAccountingAggregateFragm
591
606
  : row.dimension === "workflow"
592
607
  ? { workflowId: required(row.key_1) }
593
608
  : row.dimension === "series"
594
- ? { start: new Date(required(row.bucket_start)) }
609
+ ? { start: wallClockInstant(required(row.bucket_start), timeZone) }
595
610
  : {}
596
611
  return {
597
612
  dimension: row.dimension,
@@ -243,7 +243,7 @@ export class PgAiUsageStorage implements AiUsageStorage {
243
243
 
244
244
  return sql<AiUsageRow[]>`
245
245
  WITH requested(execution_id) AS (
246
- SELECT * FROM unnest(${sql.array(executionIds)}::text[])
246
+ SELECT * FROM unnest(${executionIds}::text[])
247
247
  )
248
248
  SELECT usage.* FROM requested
249
249
  JOIN ai_model_call_usage AS usage
@@ -312,7 +312,8 @@ export class PgArtifactStorage implements ArtifactStorage {
312
312
 
313
313
  async undoTurnWrites(input: UndoArtifactTurnInput): Promise<UndoneArtifactTurn> {
314
314
  return runPgTransaction(this.sql, async (tx) => {
315
- const { projectId, turnRunId } = input
315
+ const { projectId, turnRunId, laterTurnRunIds = [] } = input
316
+ const later = [...laterTurnRunIds]
316
317
 
317
318
  // Lock the turn's artifacts first, in id order so two of these never deadlock. A write
318
319
  // holds the same lock, and under READ COMMITTED each statement below then reads a snapshot
@@ -326,9 +327,14 @@ export class PgArtifactStorage implements ArtifactStorage {
326
327
  ORDER BY id
327
328
  FOR UPDATE
328
329
  `
330
+ // Each write beside the artifact's revision now, and the writes of the later turns undone
331
+ // with it.
329
332
  const rows = await tx<PgRow[]>`
330
333
  SELECT w.artifact_id AS "artifactId", w.created, w.revision, w.writes,
331
- a.revision AS "currentRevision"
334
+ a.revision AS "currentRevision",
335
+ (SELECT COALESCE(SUM(l.writes), 0) FROM artifact_turn_writes l
336
+ WHERE l.project_id = w.project_id AND l.artifact_id = w.artifact_id
337
+ AND l.turn_run_id = ANY(${later}::text[])) AS "laterWrites"
332
338
  FROM artifact_turn_writes w
333
339
  JOIN artifacts a ON a.project_id = w.project_id AND a.id = w.artifact_id
334
340
  WHERE w.project_id = ${projectId} AND w.turn_run_id = ${turnRunId}
@@ -338,7 +344,8 @@ export class PgArtifactStorage implements ArtifactStorage {
338
344
  const restored: PublishedArtifactVersion[] = []
339
345
  const deleted: string[] = []
340
346
  for (const row of rows) {
341
- const write = parseArtifactTurnWriteRow(row)
347
+ const own = parseArtifactTurnWriteRow(row)
348
+ const write = { ...own, writes: own.writes + Number(row.laterWrites) }
342
349
  const revision = Number(row.currentRevision)
343
350
 
344
351
  if (write.created) {
@@ -366,17 +373,25 @@ export class PgArtifactStorage implements ArtifactStorage {
366
373
  }
367
374
 
368
375
  // An artifact undone holds its restore point again, and leaves the threads only the turn
369
- // shared it in; one kept stays where it was made. A deleted one took its own with it.
376
+ // shared it in; one kept stays where it was made. A deleted one took its own with it. The
377
+ // later turns undone with it leave it too, and one kept keeps their records for their own
378
+ // undo.
370
379
  if (restored.length > 0) {
371
380
  const undone = restored.map(({ artifact }) => artifact.id)
381
+ const turns = [turnRunId, ...laterTurnRunIds]
372
382
  await tx`
373
383
  DELETE FROM artifact_restore_points
374
- WHERE project_id = ${projectId} AND turn_run_id = ${turnRunId}
384
+ WHERE project_id = ${projectId} AND turn_run_id = ANY(${turns}::text[])
375
385
  AND artifact_id IN ${tx(undone)}
376
386
  `
377
387
  await tx`
378
388
  DELETE FROM artifact_threads
379
- WHERE project_id = ${projectId} AND linked_by_run_id = ${turnRunId} AND NOT copied
389
+ WHERE project_id = ${projectId} AND linked_by_run_id = ANY(${turns}::text[])
390
+ AND NOT copied AND artifact_id IN ${tx(undone)}
391
+ `
392
+ await tx`
393
+ DELETE FROM artifact_turn_writes
394
+ WHERE project_id = ${projectId} AND turn_run_id = ANY(${later}::text[])
380
395
  AND artifact_id IN ${tx(undone)}
381
396
  `
382
397
  }
@@ -694,7 +709,7 @@ export class PgArtifactStorage implements ArtifactStorage {
694
709
  const rows = await this.sql<PgRow[]>`
695
710
  SELECT ${this.sql.unsafe(BOARD_PREVIEW_COLUMNS)} FROM artifact_board_previews
696
711
  WHERE project_id = ${input.projectId}
697
- AND render_key = ANY(${this.sql.array([...new Set(input.renderKeys)])}::text[])
712
+ AND render_key = ANY(${[...new Set(input.renderKeys)]}::text[])
698
713
  ORDER BY render_key COLLATE "C", theme COLLATE "C", size COLLATE "C"
699
714
  `
700
715
  return rows.map(parseArtifactBoardPreviewRow)