@sixb/pg 0.2.4 → 0.2.6

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 (48) hide show
  1. package/dist/agents/agent-storage.d.ts +4 -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 +23 -0
  8. package/dist/agents/inbox.d.ts.map +1 -0
  9. package/dist/agents/messages.d.ts +2 -1
  10. package/dist/agents/messages.d.ts.map +1 -1
  11. package/dist/agents/reads.d.ts +9 -0
  12. package/dist/agents/reads.d.ts.map +1 -0
  13. package/dist/agents/runs.d.ts +2 -1
  14. package/dist/agents/runs.d.ts.map +1 -1
  15. package/dist/agents/threads.d.ts.map +1 -1
  16. package/dist/index.js +1212 -87
  17. package/dist/index.js.map +17 -15
  18. package/dist/latest-run-query.d.ts +3 -1
  19. package/dist/latest-run-query.d.ts.map +1 -1
  20. package/dist/migrations.d.ts.map +1 -1
  21. package/dist/ontology-storage/replacement-plans.d.ts.map +1 -1
  22. package/dist/pg-ai-cost-storage.d.ts.map +1 -1
  23. package/dist/pg-artifact-storage.d.ts +4 -1
  24. package/dist/pg-artifact-storage.d.ts.map +1 -1
  25. package/dist/pg-sync-run-storage.d.ts +2 -1
  26. package/dist/pg-sync-run-storage.d.ts.map +1 -1
  27. package/package.json +3 -3
  28. package/src/agents/agent-storage.ts +6 -0
  29. package/src/agents/approvals.ts +44 -0
  30. package/src/agents/checkpoints.ts +26 -0
  31. package/src/agents/inbox.ts +421 -0
  32. package/src/agents/messages.ts +81 -2
  33. package/src/agents/reads.ts +160 -0
  34. package/src/agents/runs.ts +25 -0
  35. package/src/agents/threads.ts +95 -11
  36. package/src/latest-run-query.ts +8 -1
  37. package/src/migrations/103-script-executions.sql +145 -0
  38. package/src/migrations/104-artifact-pdfs.sql +18 -0
  39. package/src/migrations/105-guest-comment-links.sql +17 -0
  40. package/src/migrations/106-agent-thread-inbox.sql +36 -0
  41. package/src/migrations/107-agent-inbox-edits.sql +3 -0
  42. package/src/migrations/108-agent-thread-reads.sql +73 -0
  43. package/src/migrations/109-sync-run-connections.sql +6 -0
  44. package/src/migrations.ts +14 -0
  45. package/src/ontology-storage/replacement-plans.ts +51 -1
  46. package/src/pg-ai-cost-storage.ts +35 -20
  47. package/src/pg-artifact-storage.ts +67 -9
  48. package/src/pg-sync-run-storage.ts +69 -0
@@ -28,6 +28,7 @@ import {
28
28
  type FinishQueuedAgentRunInput,
29
29
  type ListAgentRunsInput,
30
30
  type ListAgentRunsResult,
31
+ type ListLatestAgentRunsInput,
31
32
  type ListThreadSubagentsInput,
32
33
  type ReclaimAgentRunInput,
33
34
  type RequestAgentRunCancelInput,
@@ -472,6 +473,16 @@ export class PgAgentRunStore implements AgentRunStore {
472
473
  AND child.kind = 'subagent'
473
474
  ${input.spawnKey === undefined ? this.sql`` : this.sql`AND child.spawn_key = ${input.spawnKey}`}
474
475
  ${input.finishReason === undefined ? this.sql`` : this.sql`AND child.finish_reason = ${input.finishReason}`}
476
+ ${
477
+ input.retainedTurnsOnly
478
+ ? this.sql`AND EXISTS (
479
+ SELECT 1 FROM agent_messages m
480
+ WHERE m.project_id = turn.project_id
481
+ AND m.thread_id = turn.thread_id
482
+ AND m.id = turn.trigger_message_id
483
+ )`
484
+ : this.sql``
485
+ }
475
486
  ORDER BY COALESCE(turn.started_at, turn.created_at) DESC, turn.id DESC,
476
487
  child.created_at DESC, child.id DESC
477
488
  LIMIT ${input.limit ?? 100}
@@ -482,6 +493,20 @@ export class PgAgentRunStore implements AgentRunStore {
482
493
  })
483
494
  }
484
495
 
496
+ async latestByThread(input: ListLatestAgentRunsInput): Promise<readonly AgentRunRecord[]> {
497
+ if (input.threadIds.length === 0) return []
498
+
499
+ const rows = await this.sql<AgentRunRow[]>`
500
+ SELECT DISTINCT ON (thread_id) *
501
+ FROM agent_runs
502
+ WHERE project_id = ${input.projectId}
503
+ AND thread_id = ANY(${this.sql.array([...input.threadIds])}::text[])
504
+ ORDER BY thread_id, created_at DESC, id DESC
505
+ `
506
+
507
+ return rows.map(rowToRunRecord)
508
+ }
509
+
485
510
  async list(input: ListAgentRunsInput): Promise<ListAgentRunsResult> {
486
511
  if (hasEmptyStatuses(input) || input.kinds?.length === 0) {
487
512
  return { runs: [], hasMore: false, total: 0 }
@@ -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,
@@ -58,7 +59,7 @@ export class PgAgentThreadStore implements AgentThreadStore {
58
59
  return runPgTransaction(this.sql, async (tx) => {
59
60
  // Same lock order as run finalization: run, then thread.
60
61
  const runs =
61
- input.action === "recreate"
62
+ input.action === "recreate" || input.action === "restore"
62
63
  ? []
63
64
  : await tx<AgentRunRow[]>`
64
65
  SELECT * FROM agent_runs WHERE project_id = ${input.projectId} AND id = ${input.runId}
@@ -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 (
@@ -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)
@@ -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,145 @@
1
+ -- `sixb run` records each script run as an execution: a script executor, the script's path as its
2
+ -- source, and authority that holds nothing beyond the run id the executor already names.
3
+ ALTER TABLE executions
4
+ DROP CONSTRAINT executions_executor_kind_check,
5
+ ADD CONSTRAINT executions_executor_kind_check CHECK (
6
+ executor_kind IN (
7
+ 'request', 'action', 'pipeline', 'projection', 'rule', 'sync', 'webhook', 'workflow',
8
+ 'agent', 'kernel', 'script'
9
+ )
10
+ ),
11
+ DROP CONSTRAINT executions_source_kind_check,
12
+ ADD CONSTRAINT executions_source_kind_check CHECK (
13
+ source_kind IN (
14
+ 'http', 'webhook', 'schedule', 'event', 'datasetVersion', 'execution', 'ontologyCommit',
15
+ 'script'
16
+ )
17
+ ),
18
+ DROP CONSTRAINT executions_authority_kind_check,
19
+ ADD CONSTRAINT executions_authority_kind_check CHECK (
20
+ authority_kind IN ('principal', 'trustedPrimitive', 'delegated', 'kernel', 'script', 'disabled')
21
+ ),
22
+ DROP CONSTRAINT executions_authority_shape_check,
23
+ ADD CONSTRAINT executions_authority_shape_check CHECK (
24
+ (
25
+ authority_kind = 'principal'
26
+ AND num_nonnulls(authority_user_id, authority_service_account_id) = 1
27
+ AND num_nonnulls(authority_session_id, authority_access_token_id) <= 1
28
+ AND (authority_session_id IS NULL OR authority_user_id IS NOT NULL)
29
+ AND authority_primitive_kind IS NULL
30
+ AND authority_primitive_id IS NULL
31
+ AND authority_kernel_operation IS NULL
32
+ AND authority_delegation_kind IS NULL
33
+ AND authority_delegation_id IS NULL
34
+ AND authority_delegation_session_id IS NULL
35
+ )
36
+ OR (
37
+ authority_kind = 'trustedPrimitive'
38
+ AND authority_user_id IS NULL
39
+ AND authority_service_account_id IS NULL
40
+ AND authority_session_id IS NULL
41
+ AND authority_access_token_id IS NULL
42
+ AND authority_primitive_kind IS NOT NULL
43
+ AND authority_primitive_id IS NOT NULL
44
+ AND length(trim(authority_primitive_id)) > 0
45
+ AND authority_kernel_operation IS NULL
46
+ AND authority_delegation_kind IS NULL
47
+ AND authority_delegation_id IS NULL
48
+ AND authority_delegation_session_id IS NULL
49
+ )
50
+ OR (
51
+ authority_kind = 'delegated'
52
+ AND authority_user_id IS NULL
53
+ AND authority_service_account_id IS NULL
54
+ AND authority_session_id IS NULL
55
+ AND authority_access_token_id IS NULL
56
+ AND authority_primitive_kind IS NULL
57
+ AND authority_primitive_id IS NULL
58
+ AND authority_kernel_operation IS NULL
59
+ AND authority_delegation_kind IS NOT NULL
60
+ AND authority_delegation_kind = 'share'
61
+ AND authority_delegation_id IS NOT NULL
62
+ AND length(trim(authority_delegation_id)) > 0
63
+ AND authority_delegation_session_id IS NOT NULL
64
+ AND length(trim(authority_delegation_session_id)) > 0
65
+ )
66
+ OR (
67
+ authority_kind = 'kernel'
68
+ AND authority_user_id IS NULL
69
+ AND authority_service_account_id IS NULL
70
+ AND authority_session_id IS NULL
71
+ AND authority_access_token_id IS NULL
72
+ AND authority_primitive_kind IS NULL
73
+ AND authority_primitive_id IS NULL
74
+ AND authority_kernel_operation IS NOT NULL
75
+ AND authority_delegation_kind IS NULL
76
+ AND authority_delegation_id IS NULL
77
+ AND authority_delegation_session_id IS NULL
78
+ )
79
+ OR (
80
+ -- A script's authority is its run id, which `executor_id` already holds.
81
+ authority_kind IN ('script', 'disabled')
82
+ AND authority_user_id IS NULL
83
+ AND authority_service_account_id IS NULL
84
+ AND authority_session_id IS NULL
85
+ AND authority_access_token_id IS NULL
86
+ AND authority_primitive_kind IS NULL
87
+ AND authority_primitive_id IS NULL
88
+ AND authority_kernel_operation IS NULL
89
+ AND authority_delegation_kind IS NULL
90
+ AND authority_delegation_id IS NULL
91
+ AND authority_delegation_session_id IS NULL
92
+ )
93
+ ),
94
+ DROP CONSTRAINT executions_executor_authority_check,
95
+ ADD CONSTRAINT executions_executor_authority_check CHECK (
96
+ (
97
+ executor_kind = 'request'
98
+ AND source_kind = 'http'
99
+ AND executor_id = source_id
100
+ AND (
101
+ (
102
+ authority_kind = 'principal'
103
+ AND requested_by_user_id IS NOT DISTINCT FROM authority_user_id
104
+ AND requested_by_service_account_id IS NOT DISTINCT FROM authority_service_account_id
105
+ )
106
+ OR (
107
+ authority_kind IN ('delegated', 'disabled')
108
+ AND requested_by_user_id IS NULL
109
+ AND requested_by_service_account_id IS NULL
110
+ )
111
+ )
112
+ )
113
+ OR (
114
+ executor_kind IN ('action', 'pipeline', 'projection', 'rule', 'sync', 'webhook', 'workflow')
115
+ AND source_kind <> 'script'
116
+ AND authority_kind = 'trustedPrimitive'
117
+ AND executor_kind = authority_primitive_kind
118
+ )
119
+ OR (
120
+ executor_kind = 'agent'
121
+ AND source_kind = 'execution'
122
+ AND (
123
+ (
124
+ authority_kind = 'principal'
125
+ AND authority_service_account_id IS NOT NULL
126
+ AND authority_session_id IS NULL
127
+ AND authority_access_token_id IS NULL
128
+ )
129
+ OR (
130
+ authority_kind = 'principal'
131
+ AND authority_user_id IS NOT NULL
132
+ AND num_nonnulls(authority_session_id, authority_access_token_id) = 1
133
+ )
134
+ OR authority_kind = 'disabled'
135
+ )
136
+ )
137
+ OR (executor_kind = 'kernel' AND authority_kind = 'kernel' AND source_kind <> 'script')
138
+ OR (
139
+ executor_kind = 'script'
140
+ AND source_kind = 'script'
141
+ AND authority_kind = 'script'
142
+ AND requested_by_user_id IS NULL
143
+ AND requested_by_service_account_id IS NULL
144
+ )
145
+ );
@@ -0,0 +1,18 @@
1
+ -- A designed document printed to PDF, kept under its render key: a hash of what it prints, which
2
+ -- its version names. Versions that print the same share it, so no artifact owns one and deleting
3
+ -- an artifact leaves them. The first PDF kept under a key stays. When it could not be printed, why
4
+ -- is kept in its place, until it prints or someone asks for it again.
5
+ CREATE TABLE artifact_pdfs (
6
+ project_id TEXT NOT NULL,
7
+ render_key TEXT NOT NULL CHECK (length(render_key) > 0),
8
+ file JSONB,
9
+ pages INTEGER CHECK (pages > 0),
10
+ failure TEXT,
11
+ created_at TIMESTAMPTZ NOT NULL,
12
+ PRIMARY KEY (project_id, render_key),
13
+ -- A PDF with its pages, or why there is none.
14
+ CHECK (
15
+ (failure IS NULL AND file IS NOT NULL AND pages IS NOT NULL)
16
+ OR (failure IS NOT NULL AND file IS NULL AND pages IS NULL)
17
+ )
18
+ );
@@ -0,0 +1,17 @@
1
+ -- A guest's comment keeps the shared link they came through, by its grant: whoever opens that link,
2
+ -- in any session, reads the threads its guests started, and no other. Comments written before take
3
+ -- it from their session, which keeps its grant.
4
+ ALTER TABLE artifact_comments ADD COLUMN author_grant_id TEXT;
5
+
6
+ UPDATE artifact_comments AS comment
7
+ SET author_grant_id = session.grant_id
8
+ FROM share_sessions AS session
9
+ WHERE comment.author_principal_type = 'guest'
10
+ AND session.project_id = comment.project_id
11
+ AND session.id = comment.author_principal_id;
12
+
13
+ ALTER TABLE artifact_comments
14
+ ADD CONSTRAINT artifact_comments_author_grant_check CHECK (
15
+ (author_principal_type = 'guest') = (author_grant_id IS NOT NULL)
16
+ AND (author_grant_id IS NULL OR length(author_grant_id) > 0)
17
+ );
@@ -0,0 +1,36 @@
1
+ -- Mid-run steers and queued follow-up messages held outside `agent_messages` until delivered.
2
+ CREATE TABLE agent_thread_inbox (
3
+ project_id TEXT NOT NULL,
4
+ id TEXT NOT NULL CHECK (length(trim(id)) > 0),
5
+ thread_id TEXT NOT NULL,
6
+ message_id TEXT NOT NULL CHECK (length(trim(message_id)) > 0),
7
+ seq INTEGER NOT NULL CHECK (seq > 0),
8
+ author_principal_type TEXT NOT NULL
9
+ CHECK (author_principal_type IN ('user', 'serviceAccount', 'system')),
10
+ author_principal_id TEXT NOT NULL CHECK (length(author_principal_id) > 0),
11
+ mode TEXT NOT NULL CHECK (mode IN ('steer', 'queue')),
12
+ parts JSONB NOT NULL,
13
+ spec JSONB,
14
+ time_zone TEXT,
15
+ locale TEXT,
16
+ immediate BOOLEAN NOT NULL DEFAULT FALSE,
17
+ -- The request that sent it, whose child execution a turn that starts it runs as.
18
+ execution_id TEXT,
19
+ claimed_by_run_id TEXT,
20
+ claimed_by_token TEXT,
21
+ claimed_by_step INTEGER,
22
+ claimed_after_answer BOOLEAN,
23
+ created_at TIMESTAMPTZ NOT NULL,
24
+ updated_at TIMESTAMPTZ NOT NULL,
25
+ PRIMARY KEY (project_id, id),
26
+ UNIQUE (project_id, message_id),
27
+ UNIQUE (project_id, thread_id, seq),
28
+ FOREIGN KEY (project_id, thread_id) REFERENCES agent_threads (project_id, id) ON DELETE CASCADE
29
+ );
30
+
31
+ CREATE INDEX idx_agent_thread_inbox_thread_seq
32
+ ON agent_thread_inbox (project_id, thread_id, seq);
33
+
34
+ CREATE INDEX idx_agent_thread_inbox_claimed_run
35
+ ON agent_thread_inbox (project_id, claimed_by_run_id)
36
+ WHERE claimed_by_run_id IS NOT NULL;
@@ -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
@@ -473,6 +473,13 @@ export const postgresStorageMigrations = defineMigrations<PostgresMigrationConte
473
473
  pgSql("100-shared-link-guests", sharedLinkGuestsSql),
474
474
  pgSql("101-connector-connection-owners", connectorConnectionOwnersSql),
475
475
  pgSql("102-user-suspension-source", userSuspensionSourceSql),
476
+ pgSql("103-script-executions", scriptExecutionsSql),
477
+ pgSql("104-artifact-pdfs", artifactPdfsSql),
478
+ pgSql("105-guest-comment-links", guestCommentLinksSql),
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),
476
483
  ],
477
484
  })
478
485
 
@@ -575,3 +582,10 @@ import connectorConnectionOwnersSql from "./migrations/101-connector-connection-
575
582
  import userSuspensionSourceSql from "./migrations/102-user-suspension-source.sql" with {
576
583
  type: "text",
577
584
  }
585
+ import scriptExecutionsSql from "./migrations/103-script-executions.sql" with { type: "text" }
586
+ import artifactPdfsSql from "./migrations/104-artifact-pdfs.sql" with { type: "text" }
587
+ import guestCommentLinksSql from "./migrations/105-guest-comment-links.sql" with { type: "text" }
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" }
@@ -158,6 +158,48 @@ function revision(kind: "object" | "link"): string {
158
158
  : `${linkRevision(key)}::text`
159
159
  }
160
160
 
161
+ /**
162
+ * Adds the objects of `objectTypeId` an edit created and no live root of the source asserts, and
163
+ * returns how many. An object's root is keyed by the object itself.
164
+ */
165
+ async function planCreatedObjects(
166
+ sql: SQLClient,
167
+ input: OpenReplacementPlanInput,
168
+ versionId: string,
169
+ objectTypeId: string
170
+ ): Promise<number> {
171
+ const rootKey = jsonTupleExpression([
172
+ `'"object"'`,
173
+ "to_jsonb(overrides.object_type_id)::text",
174
+ "to_jsonb(overrides.primary_id)::text",
175
+ ])
176
+ const planned = await sql`
177
+ WITH keyed AS (
178
+ SELECT ${sql.unsafe(columnKeyExpression(["object_type_id", "primary_id"]))} AS identity_key
179
+ FROM ontology_object_overrides AS overrides
180
+ WHERE overrides.project_id = ${input.projectId} AND overrides.object_type_id = ${objectTypeId}
181
+ AND overrides.value->>'kind' = 'create'
182
+ AND NOT EXISTS (
183
+ SELECT 1 FROM ontology_source_roots AS live
184
+ JOIN ontology_sources AS versions ON versions.version_id = live.version_id
185
+ WHERE live.project_id = overrides.project_id
186
+ AND live.root_key = ${sql.unsafe(rootKey)}
187
+ AND live.retired_at IS NULL AND NOT live.deleted
188
+ AND versions.source_id = ${input.source.projectionId}
189
+ AND versions.status IN ${sql.unsafe(PUBLISHED)}
190
+ )
191
+ )
192
+ INSERT INTO ${sql(IDENTITIES)} (
193
+ version_id, entity_kind, identity_key, sort_key, diff_required, expand_at, source_key,
194
+ target_key
195
+ )
196
+ SELECT ${versionId}, 'object', identity_key, ${sql.unsafe(SORT_KEY)}, TRUE, NULL, NULL, NULL
197
+ FROM keyed
198
+ ON CONFLICT DO NOTHING
199
+ `
200
+ return planned.count
201
+ }
202
+
161
203
  export class PgOntologyReplacementPlanStorage implements OntologyReplacementPlanStorage {
162
204
  constructor(private readonly sql: SQLClient) {}
163
205
 
@@ -233,7 +275,15 @@ export class PgOntologyReplacementPlanStorage implements OntologyReplacementPlan
233
275
  target_key
234
276
  FROM keyed
235
277
  `
236
- if (opened.count >= ANALYZE_ROWS) await analyzePlans(sql)
278
+ const created = input.createdObjects
279
+ ? await planCreatedObjects(
280
+ sql,
281
+ input,
282
+ candidate.version_id,
283
+ input.createdObjects.objectTypeId
284
+ )
285
+ : 0
286
+ if (opened.count + created >= ANALYZE_ROWS) await analyzePlans(sql)
237
287
  return { committedAt: commit.committedAt }
238
288
  })
239
289
  }