@sixb/pg 0.2.5 → 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.
- package/dist/agents/agent-storage.d.ts +2 -0
- package/dist/agents/agent-storage.d.ts.map +1 -1
- package/dist/agents/approvals.d.ts +2 -1
- package/dist/agents/approvals.d.ts.map +1 -1
- package/dist/agents/checkpoints.d.ts +2 -1
- package/dist/agents/checkpoints.d.ts.map +1 -1
- package/dist/agents/inbox.d.ts.map +1 -1
- package/dist/agents/messages.d.ts +2 -1
- package/dist/agents/messages.d.ts.map +1 -1
- package/dist/agents/reads.d.ts +9 -0
- package/dist/agents/reads.d.ts.map +1 -0
- package/dist/agents/runs.d.ts +2 -1
- package/dist/agents/runs.d.ts.map +1 -1
- package/dist/agents/threads.d.ts.map +1 -1
- package/dist/index.js +560 -67
- package/dist/index.js.map +16 -15
- package/dist/latest-run-query.d.ts +3 -1
- package/dist/latest-run-query.d.ts.map +1 -1
- package/dist/migrations.d.ts.map +1 -1
- package/dist/pg-ai-cost-storage.d.ts.map +1 -1
- package/dist/pg-artifact-storage.d.ts.map +1 -1
- package/dist/pg-sync-run-storage.d.ts +2 -1
- package/dist/pg-sync-run-storage.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/agents/agent-storage.ts +3 -0
- package/src/agents/approvals.ts +44 -0
- package/src/agents/checkpoints.ts +26 -0
- package/src/agents/inbox.ts +4 -1
- package/src/agents/messages.ts +81 -2
- package/src/agents/reads.ts +160 -0
- package/src/agents/runs.ts +25 -0
- package/src/agents/threads.ts +94 -10
- package/src/latest-run-query.ts +8 -1
- package/src/migrations/107-agent-inbox-edits.sql +3 -0
- package/src/migrations/108-agent-thread-reads.sql +73 -0
- package/src/migrations/109-sync-run-connections.sql +6 -0
- package/src/migrations.ts +6 -0
- package/src/pg-ai-cost-storage.ts +35 -20
- package/src/pg-artifact-storage.ts +21 -6
- package/src/pg-sync-run-storage.ts +69 -0
package/src/agents/threads.ts
CHANGED
|
@@ -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 (
|
|
@@ -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
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
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
|
-
|
|
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
|
|
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)
|
package/src/latest-run-query.ts
CHANGED
|
@@ -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,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" }
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { wallClockInstant } from "@sixb/core/calendar"
|
|
1
2
|
import {
|
|
2
3
|
type AiAccountingAggregateFragment,
|
|
3
4
|
type AiAccountingRecordSetItem,
|
|
@@ -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::
|
|
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
|
-
|
|
196
|
-
|
|
197
|
-
date_trunc('hour', occurred_at AT TIME ZONE
|
|
198
|
-
|
|
199
|
-
date_trunc('
|
|
200
|
-
|
|
201
|
-
|
|
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
|
-
|
|
207
|
-
|
|
208
|
-
date_trunc('hour', occurred_at AT TIME ZONE
|
|
209
|
-
|
|
210
|
-
date_trunc('
|
|
211
|
-
|
|
212
|
-
|
|
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(
|
|
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:
|
|
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
|
-
|
|
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:
|
|
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:
|
|
609
|
+
? { start: wallClockInstant(required(row.bucket_start), timeZone) }
|
|
595
610
|
: {}
|
|
596
611
|
return {
|
|
597
612
|
dimension: row.dimension,
|
|
@@ -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 = tx.array([...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
|
|
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 = tx.array([turnRunId, ...laterTurnRunIds])
|
|
372
382
|
await tx`
|
|
373
383
|
DELETE FROM artifact_restore_points
|
|
374
|
-
WHERE project_id = ${projectId} AND turn_run_id = ${
|
|
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 = ${
|
|
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
|
}
|
|
@@ -11,6 +11,7 @@ import type {
|
|
|
11
11
|
ConfirmRunExecutionInput,
|
|
12
12
|
ExecutionStorage,
|
|
13
13
|
FinishSyncRunInput,
|
|
14
|
+
ListLatestSyncConnectionRunsInput,
|
|
14
15
|
ListLatestSyncRunsInput,
|
|
15
16
|
ListLatestSyncRunsResult,
|
|
16
17
|
ListLostRunsInput,
|
|
@@ -71,6 +72,7 @@ export class PgSyncRunStorage implements SyncRunStorage {
|
|
|
71
72
|
id,
|
|
72
73
|
execution_id,
|
|
73
74
|
sync_id,
|
|
75
|
+
connection_id,
|
|
74
76
|
dataset_id,
|
|
75
77
|
mode,
|
|
76
78
|
status,
|
|
@@ -83,6 +85,7 @@ export class PgSyncRunStorage implements SyncRunStorage {
|
|
|
83
85
|
${input.id},
|
|
84
86
|
${input.executionId},
|
|
85
87
|
${input.syncId},
|
|
88
|
+
${input.connectionId ?? null},
|
|
86
89
|
${input.datasetId},
|
|
87
90
|
${input.mode},
|
|
88
91
|
${"queued"},
|
|
@@ -269,6 +272,22 @@ export class PgSyncRunStorage implements SyncRunStorage {
|
|
|
269
272
|
params.push(...input.syncIds)
|
|
270
273
|
}
|
|
271
274
|
|
|
275
|
+
if (input.connectionId === null) {
|
|
276
|
+
whereClauses.push("connection_id IS NULL")
|
|
277
|
+
} else if (input.connectionId !== undefined) {
|
|
278
|
+
whereClauses.push(`connection_id = $${index++}`)
|
|
279
|
+
params.push(input.connectionId)
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (input.visibleConnectionIds) {
|
|
283
|
+
whereClauses.push(
|
|
284
|
+
visibleConnectionsClause(input.visibleConnectionIds, (value) => {
|
|
285
|
+
params.push(value)
|
|
286
|
+
return `$${index++}`
|
|
287
|
+
})
|
|
288
|
+
)
|
|
289
|
+
}
|
|
290
|
+
|
|
272
291
|
if (input.datasetId) {
|
|
273
292
|
whereClauses.push(`dataset_id = $${index++}`)
|
|
274
293
|
params.push(input.datasetId)
|
|
@@ -302,6 +321,7 @@ export class PgSyncRunStorage implements SyncRunStorage {
|
|
|
302
321
|
}
|
|
303
322
|
|
|
304
323
|
async listLatestBySyncIds(input: ListLatestSyncRunsInput): Promise<ListLatestSyncRunsResult> {
|
|
324
|
+
const { visibleConnectionIds } = input
|
|
305
325
|
const rows = await queryLatestRunsByOwnerId<DatabaseRow>({
|
|
306
326
|
sql: this.sql,
|
|
307
327
|
tableName: "sync_runs",
|
|
@@ -310,12 +330,59 @@ export class PgSyncRunStorage implements SyncRunStorage {
|
|
|
310
330
|
projectId: input.projectId,
|
|
311
331
|
ownerIdFor: (row) => row.sync_id,
|
|
312
332
|
selectList: "*, checkpoint IS NOT NULL AS checkpoint_present",
|
|
333
|
+
...(visibleConnectionIds === undefined
|
|
334
|
+
? {}
|
|
335
|
+
: { filter: (bind) => visibleConnectionsClause(visibleConnectionIds, bind) }),
|
|
313
336
|
})
|
|
314
337
|
|
|
315
338
|
return {
|
|
316
339
|
runs: rows.map(rowToSyncRunRecord),
|
|
317
340
|
}
|
|
318
341
|
}
|
|
342
|
+
|
|
343
|
+
async listLatestByConnection(
|
|
344
|
+
input: ListLatestSyncConnectionRunsInput
|
|
345
|
+
): Promise<ListLatestSyncRunsResult> {
|
|
346
|
+
if (input.syncIds.length === 0 || input.connectionIds?.length === 0) return { runs: [] }
|
|
347
|
+
|
|
348
|
+
const params: SqlParameter[] = [input.projectId]
|
|
349
|
+
const placeholders = (values: readonly string[]) =>
|
|
350
|
+
values
|
|
351
|
+
.map((value) => {
|
|
352
|
+
params.push(value)
|
|
353
|
+
return `$${params.length}`
|
|
354
|
+
})
|
|
355
|
+
.join(", ")
|
|
356
|
+
const syncFilter = `sync_id IN (${placeholders(input.syncIds)})`
|
|
357
|
+
const connectionFilter =
|
|
358
|
+
input.connectionIds === undefined
|
|
359
|
+
? "connection_id IS NOT NULL"
|
|
360
|
+
: `connection_id IN (${placeholders(input.connectionIds)})`
|
|
361
|
+
|
|
362
|
+
const rows = await this.sql.unsafe<DatabaseRow[]>(
|
|
363
|
+
`
|
|
364
|
+
SELECT DISTINCT ON (sync_id, connection_id)
|
|
365
|
+
*, checkpoint IS NOT NULL AS checkpoint_present
|
|
366
|
+
FROM sync_runs
|
|
367
|
+
WHERE project_id = $1 AND ${syncFilter} AND ${connectionFilter}
|
|
368
|
+
ORDER BY sync_id, connection_id, COALESCE(started_at, queued_at) DESC, id DESC
|
|
369
|
+
`,
|
|
370
|
+
params
|
|
371
|
+
)
|
|
372
|
+
|
|
373
|
+
return { runs: rows.map(rowToSyncRunRecord) }
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/** `visibleConnectionIds` as a condition: runs of those connections, and runs that read none. */
|
|
378
|
+
function visibleConnectionsClause(
|
|
379
|
+
connectionIds: readonly string[],
|
|
380
|
+
bind: (value: string) => string
|
|
381
|
+
): string {
|
|
382
|
+
if (connectionIds.length === 0) return "connection_id IS NULL"
|
|
383
|
+
|
|
384
|
+
const placeholders = connectionIds.map((id) => bind(id)).join(", ")
|
|
385
|
+
return `(connection_id IS NULL OR connection_id IN (${placeholders}))`
|
|
319
386
|
}
|
|
320
387
|
|
|
321
388
|
function staleExecution(id: string): SyncRunError {
|
|
@@ -336,6 +403,7 @@ function rowToSyncRunRecord(row: DatabaseRow): SyncRunRecord {
|
|
|
336
403
|
projectId: row.project_id,
|
|
337
404
|
executionId: row.execution_id,
|
|
338
405
|
syncId: row.sync_id,
|
|
406
|
+
...(row.connection_id === null ? {} : { connectionId: row.connection_id }),
|
|
339
407
|
datasetId: row.dataset_id,
|
|
340
408
|
mode: row.mode,
|
|
341
409
|
status: row.status,
|
|
@@ -373,6 +441,7 @@ interface DatabaseRow extends PgRunExecutionColumns {
|
|
|
373
441
|
id: string
|
|
374
442
|
execution_id: string
|
|
375
443
|
sync_id: string
|
|
444
|
+
connection_id: string | null
|
|
376
445
|
dataset_id: string
|
|
377
446
|
mode: SyncRunRecord["mode"]
|
|
378
447
|
status: SyncRunRecord["status"]
|