@sixb/pg 0.2.4 → 0.2.5

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.
@@ -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;
package/src/migrations.ts CHANGED
@@ -473,6 +473,10 @@ 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),
476
480
  ],
477
481
  })
478
482
 
@@ -575,3 +579,7 @@ import connectorConnectionOwnersSql from "./migrations/101-connector-connection-
575
579
  import userSuspensionSourceSql from "./migrations/102-user-suspension-source.sql" with {
576
580
  type: "text",
577
581
  }
582
+ import scriptExecutionsSql from "./migrations/103-script-executions.sql" with { type: "text" }
583
+ import artifactPdfsSql from "./migrations/104-artifact-pdfs.sql" with { type: "text" }
584
+ import guestCommentLinksSql from "./migrations/105-guest-comment-links.sql" with { type: "text" }
585
+ import agentThreadInboxSql from "./migrations/106-agent-thread-inbox.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
  }
@@ -9,6 +9,7 @@ import {
9
9
  parseArtifactBoardPreviewRow,
10
10
  parseArtifactCommentRow,
11
11
  parseArtifactFrameGrantRow,
12
+ parseArtifactPdfRow,
12
13
  parseArtifactRestorePointRow,
13
14
  parseArtifactRow,
14
15
  parseArtifactStateRow,
@@ -22,6 +23,8 @@ import type {
22
23
  ArtifactCommentKey,
23
24
  ArtifactCommentRecord,
24
25
  ArtifactFrameGrantRecord,
26
+ ArtifactPdfKeyInput,
27
+ ArtifactPdfRecord,
25
28
  ArtifactRecord,
26
29
  ArtifactRestorePointReason,
27
30
  ArtifactRestorePointRecord,
@@ -103,7 +106,8 @@ const FRAME_GRANT_COLUMNS = `
103
106
  const COMMENT_COLUMNS = `
104
107
  project_id AS "projectId", artifact_id AS "artifactId", id, parent_id AS "parentId", anchor,
105
108
  body, author_principal_type AS "authorPrincipalType", author_principal_id AS "authorPrincipalId",
106
- author_name AS "authorName", created_at AS "createdAt", resolved_at AS "resolvedAt",
109
+ author_name AS "authorName", author_grant_id AS "authorGrantId",
110
+ created_at AS "createdAt", resolved_at AS "resolvedAt",
107
111
  resolved_by_principal_type AS "resolvedByPrincipalType",
108
112
  resolved_by_principal_id AS "resolvedByPrincipalId",
109
113
  origin_thread_id AS "originThreadId", origin_run_id AS "originRunId"
@@ -116,6 +120,11 @@ const BOARD_PREVIEW_COLUMNS = `
116
120
  created_at AS "createdAt"
117
121
  `
118
122
 
123
+ const PDF_COLUMNS = `
124
+ project_id AS "projectId", render_key AS "renderKey", file, pages, failure,
125
+ created_at AS "createdAt"
126
+ `
127
+
119
128
  export class PgArtifactStorage implements ArtifactStorage {
120
129
  constructor(private readonly sql: PgStoreClient) {}
121
130
 
@@ -709,6 +718,38 @@ export class PgArtifactStorage implements ArtifactStorage {
709
718
  })
710
719
  }
711
720
 
721
+ async getPdf(input: ArtifactPdfKeyInput): Promise<ArtifactPdfRecord | null> {
722
+ const [row] = await this.sql<PgRow[]>`
723
+ SELECT ${this.sql.unsafe(PDF_COLUMNS)} FROM artifact_pdfs
724
+ WHERE project_id = ${input.projectId} AND render_key = ${input.renderKey}
725
+ `
726
+ return row ? parseArtifactPdfRow(row) : null
727
+ }
728
+
729
+ async putPdf(input: ArtifactPdfRecord): Promise<void> {
730
+ const printed = "failure" in input ? null : input
731
+ await this.sql`
732
+ INSERT INTO artifact_pdfs (project_id, render_key, file, pages, failure, created_at)
733
+ VALUES (
734
+ ${input.projectId}, ${input.renderKey},
735
+ ${printed ? JSON.stringify(printed.file) : null}::text::jsonb, ${printed?.pages ?? null},
736
+ ${"failure" in input ? input.failure : null}, ${input.createdAt}
737
+ )
738
+ ON CONFLICT (project_id, render_key) DO UPDATE SET
739
+ file = excluded.file, pages = excluded.pages, failure = NULL,
740
+ created_at = excluded.created_at
741
+ WHERE artifact_pdfs.failure IS NOT NULL AND excluded.failure IS NULL
742
+ `
743
+ }
744
+
745
+ async clearPdfFailure(input: ArtifactPdfKeyInput): Promise<void> {
746
+ await this.sql`
747
+ DELETE FROM artifact_pdfs
748
+ WHERE project_id = ${input.projectId} AND render_key = ${input.renderKey}
749
+ AND failure IS NOT NULL
750
+ `
751
+ }
752
+
712
753
  async createComment(input: CreateArtifactCommentInput): Promise<ArtifactCommentRecord> {
713
754
  return runPgTransaction(this.sql, async (tx) => {
714
755
  // Under the artifact's lock: two comments at once cannot both pass the limit.
@@ -739,15 +780,17 @@ export class PgArtifactStorage implements ArtifactStorage {
739
780
  throw artifactStorageErrors.guestCommentsFull(input.artifactId)
740
781
  }
741
782
 
783
+ const guest = input.author.type === "guest" ? input.author : undefined
742
784
  const [row] = await tx<PgRow[]>`
743
785
  INSERT INTO artifact_comments (
744
786
  project_id, artifact_id, id, parent_id, anchor, body, author_principal_type,
745
- author_principal_id, author_name, created_at, origin_thread_id, origin_run_id
787
+ author_principal_id, author_name, author_grant_id, created_at, origin_thread_id,
788
+ origin_run_id
746
789
  ) VALUES (
747
790
  ${input.projectId}, ${input.artifactId}, ${input.id}, ${input.parentId ?? null},
748
791
  ${JSON.stringify(commentAnchor(input.anchor))}::text::jsonb, ${input.body},
749
792
  ${input.author.type}, ${input.author.id},
750
- ${input.author.type === "guest" ? input.author.name : null}, ${input.createdAt},
793
+ ${guest?.name ?? null}, ${guest?.grantId ?? null}, ${input.createdAt},
751
794
  ${input.origin?.threadId ?? null}, ${input.origin?.runId ?? null}
752
795
  )
753
796
  RETURNING ${tx.unsafe(COMMENT_COLUMNS)}