@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.
- 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 +593 -100
- package/dist/index.js.map +32 -31
- 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 +46 -2
- 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 +99 -15
- 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/ontology-storage/materialization-session.ts +1 -1
- package/src/ontology-storage/materializations.ts +1 -1
- package/src/ontology-storage/outbox.ts +3 -3
- package/src/ontology-storage/replacement-plans.ts +1 -1
- package/src/ontology-storage/source-roots.ts +1 -1
- package/src/ontology-storage/sources.ts +2 -2
- package/src/ontology-storage/vector-indexing.ts +1 -1
- package/src/pg-action-run-storage.ts +1 -1
- package/src/pg-activity-storage.ts +3 -3
- package/src/pg-ai-cost-storage.ts +36 -21
- package/src/pg-ai-usage-storage.ts +1 -1
- package/src/pg-artifact-storage.ts +22 -7
- package/src/pg-dataset-ingest-storage.ts +3 -3
- package/src/pg-execution-storage.ts +1 -1
- package/src/pg-projection-run-storage.ts +1 -1
- package/src/pg-sync-run-storage.ts +69 -0
- package/src/pg-workflow-run-storage.ts +1 -1
- package/src/share-grant-storage.ts +1 -1
- package/src/transactions.ts +2 -2
package/dist/index.js
CHANGED
|
@@ -214,8 +214,8 @@ async function lockAdvisoryKeys(sql, keys) {
|
|
|
214
214
|
await sql`
|
|
215
215
|
SELECT pg_advisory_xact_lock(locks.first_key, locks.second_key)
|
|
216
216
|
FROM unnest(
|
|
217
|
-
${
|
|
218
|
-
${
|
|
217
|
+
${locks.map(([first]) => first)}::integer[],
|
|
218
|
+
${locks.map(([, second]) => second)}::integer[]
|
|
219
219
|
) WITH ORDINALITY AS locks(first_key, second_key, lock_order)
|
|
220
220
|
ORDER BY locks.lock_order
|
|
221
221
|
`;
|
|
@@ -513,6 +513,40 @@ class PgAgentApprovalStore {
|
|
|
513
513
|
const stopped = rows.filter((row) => row.newly_stopped).length;
|
|
514
514
|
return { cancelled: cancelled.sort(compareApprovals), stopped };
|
|
515
515
|
}
|
|
516
|
+
async rewind(input) {
|
|
517
|
+
const runIds = [...new Set(input.runIds)];
|
|
518
|
+
const clearedIds = [...new Set(input.clearedMessageIds ?? [])];
|
|
519
|
+
const cancelledAt = new Date(input.cancelledAt ?? new Date);
|
|
520
|
+
return runPgTransaction(this.sql, async (tx) => {
|
|
521
|
+
if (clearedIds.length > 0) {
|
|
522
|
+
await tx`
|
|
523
|
+
UPDATE agent_approvals
|
|
524
|
+
SET reported_in_message_id = NULL
|
|
525
|
+
WHERE project_id = ${input.projectId}
|
|
526
|
+
AND thread_id = ${input.threadId}
|
|
527
|
+
AND reported_in_message_id = ANY(${clearedIds}::text[])
|
|
528
|
+
`;
|
|
529
|
+
}
|
|
530
|
+
if (runIds.length === 0)
|
|
531
|
+
return { cancelled: [], deleted: 0 };
|
|
532
|
+
const removed = await tx`
|
|
533
|
+
DELETE FROM agent_approvals
|
|
534
|
+
WHERE project_id = ${input.projectId}
|
|
535
|
+
AND thread_id = ${input.threadId}
|
|
536
|
+
AND run_id = ANY(${runIds}::text[])
|
|
537
|
+
RETURNING *
|
|
538
|
+
`;
|
|
539
|
+
const cancelled = removed.filter((row) => row.status === "pending").map((row) => {
|
|
540
|
+
const parsed = rowToApproval(row);
|
|
541
|
+
return {
|
|
542
|
+
...parsed,
|
|
543
|
+
status: "cancelled",
|
|
544
|
+
turnStoppedAt: parsed.turnStoppedAt ?? cancelledAt
|
|
545
|
+
};
|
|
546
|
+
}).sort(compareApprovals);
|
|
547
|
+
return { cancelled, deleted: removed.length };
|
|
548
|
+
});
|
|
549
|
+
}
|
|
516
550
|
async holdsRun(input) {
|
|
517
551
|
return pgHoldsRun(this.sql, input.projectId, input.runId);
|
|
518
552
|
}
|
|
@@ -568,7 +602,7 @@ class PgAgentApprovalStore {
|
|
|
568
602
|
const rows = await this.sql`
|
|
569
603
|
UPDATE agent_approvals SET reported_in_message_id = ${input.messageId}
|
|
570
604
|
WHERE project_id = ${input.projectId} AND thread_id = ${input.threadId}
|
|
571
|
-
AND id = ANY(${
|
|
605
|
+
AND id = ANY(${ids}::text[]) AND reported_in_message_id IS NULL
|
|
572
606
|
AND (${input.resuming !== true} OR turn_stopped_at IS NULL)
|
|
573
607
|
RETURNING id
|
|
574
608
|
`;
|
|
@@ -612,7 +646,7 @@ function listFilter(sql, input) {
|
|
|
612
646
|
AND (${runId}::text IS NULL OR run_id = ${runId})
|
|
613
647
|
AND (${requesterType}::text IS NULL
|
|
614
648
|
OR (requested_by_type = ${requesterType} AND requested_by_id = ${requesterId}))
|
|
615
|
-
AND (${statuses === null}::boolean OR status = ANY(${
|
|
649
|
+
AND (${statuses === null}::boolean OR status = ANY(${[...statuses ?? []]}::text[]))
|
|
616
650
|
AND (${reported}::boolean IS NULL OR (reported_in_message_id IS NOT NULL) = ${reported})
|
|
617
651
|
`;
|
|
618
652
|
}
|
|
@@ -674,7 +708,8 @@ import {
|
|
|
674
708
|
assertAgentContextCheckpointAnchors,
|
|
675
709
|
assertAgentContextCheckpointAuthority,
|
|
676
710
|
assertAgentContextCheckpointReplayState,
|
|
677
|
-
assertCreateAgentContextCheckpointInput
|
|
711
|
+
assertCreateAgentContextCheckpointInput,
|
|
712
|
+
assertDeleteAgentContextCheckpointsAfterSeqInput
|
|
678
713
|
} from "@sixb/core/internal/agent-run-storage-provider";
|
|
679
714
|
import {
|
|
680
715
|
AgentStorageError as AgentStorageError2
|
|
@@ -814,6 +849,28 @@ class PgAgentContextCheckpointStore {
|
|
|
814
849
|
`;
|
|
815
850
|
return row ? rowToContextCheckpointRecord(row) : null;
|
|
816
851
|
}
|
|
852
|
+
async deleteAfterSeq(input) {
|
|
853
|
+
assertDeleteAgentContextCheckpointsAfterSeqInput(input, "SixbPg");
|
|
854
|
+
return runPgTransaction(this.sql, async (tx) => {
|
|
855
|
+
const rows = await tx`
|
|
856
|
+
SELECT id FROM agent_context_checkpoints
|
|
857
|
+
WHERE project_id = ${input.projectId}
|
|
858
|
+
AND thread_id = ${input.threadId}
|
|
859
|
+
AND observed_head_seq > ${input.afterSeq}
|
|
860
|
+
ORDER BY summarized_through_seq DESC, created_at DESC, id DESC
|
|
861
|
+
FOR UPDATE
|
|
862
|
+
`;
|
|
863
|
+
if (rows.length === 0)
|
|
864
|
+
return 0;
|
|
865
|
+
for (const row of rows) {
|
|
866
|
+
await tx`
|
|
867
|
+
DELETE FROM agent_context_checkpoints
|
|
868
|
+
WHERE project_id = ${input.projectId} AND id = ${row.id}
|
|
869
|
+
`;
|
|
870
|
+
}
|
|
871
|
+
return rows.length;
|
|
872
|
+
});
|
|
873
|
+
}
|
|
817
874
|
async getByRunIds(input) {
|
|
818
875
|
const runIds = [...new Set(input.runIds)];
|
|
819
876
|
if (runIds.length === 0)
|
|
@@ -993,7 +1050,7 @@ class PgAgentInboxStore {
|
|
|
993
1050
|
INSERT INTO agent_thread_inbox (
|
|
994
1051
|
project_id, id, thread_id, message_id, seq,
|
|
995
1052
|
author_principal_type, author_principal_id, mode, parts, spec,
|
|
996
|
-
time_zone, locale, immediate, execution_id, created_at, updated_at
|
|
1053
|
+
time_zone, locale, immediate, replaces_message_id, execution_id, created_at, updated_at
|
|
997
1054
|
) VALUES (
|
|
998
1055
|
${record.projectId},
|
|
999
1056
|
${record.id},
|
|
@@ -1008,6 +1065,7 @@ class PgAgentInboxStore {
|
|
|
1008
1065
|
${record.timeZone ?? null},
|
|
1009
1066
|
${record.locale ?? null},
|
|
1010
1067
|
${Boolean(record.immediate)},
|
|
1068
|
+
${record.replacesMessageId ?? null},
|
|
1011
1069
|
${record.executionId ?? null},
|
|
1012
1070
|
${record.createdAt},
|
|
1013
1071
|
${record.updatedAt}
|
|
@@ -1258,6 +1316,7 @@ function rowToInboxItem(row) {
|
|
|
1258
1316
|
...row.time_zone ? { timeZone: row.time_zone } : {},
|
|
1259
1317
|
...row.locale ? { locale: row.locale } : {},
|
|
1260
1318
|
...row.immediate ? { immediate: true } : {},
|
|
1319
|
+
...row.replaces_message_id ? { replacesMessageId: row.replaces_message_id } : {},
|
|
1261
1320
|
...row.execution_id ? { executionId: row.execution_id } : {},
|
|
1262
1321
|
...claimedBy ? { claimedBy } : {},
|
|
1263
1322
|
createdAt: new Date(row.created_at),
|
|
@@ -1394,7 +1453,10 @@ class PgAgentMemoryStore {
|
|
|
1394
1453
|
}
|
|
1395
1454
|
|
|
1396
1455
|
// src/agents/messages.ts
|
|
1397
|
-
import {
|
|
1456
|
+
import {
|
|
1457
|
+
assertDeleteAgentMessagesFromSeqInput,
|
|
1458
|
+
countsAsThreadMessage
|
|
1459
|
+
} from "@sixb/core/internal/agent-run-storage-provider";
|
|
1398
1460
|
import { AGENT_MESSAGE_CONTENT_VERSION } from "@sixb/core/internal/agents";
|
|
1399
1461
|
import {
|
|
1400
1462
|
AgentStorageError as AgentStorageError4
|
|
@@ -1510,6 +1572,48 @@ class PgAgentMessageStore {
|
|
|
1510
1572
|
if (deleted.length === 0) {
|
|
1511
1573
|
return 0;
|
|
1512
1574
|
}
|
|
1575
|
+
await clampReadsToHead(tx, input);
|
|
1576
|
+
const counted = deleted.filter((row) => countsAsThreadMessage({
|
|
1577
|
+
parts: typeof row.parts === "string" ? JSON.parse(row.parts) : row.parts
|
|
1578
|
+
})).length;
|
|
1579
|
+
const updatedAt = new Date;
|
|
1580
|
+
await tx`
|
|
1581
|
+
UPDATE agent_threads
|
|
1582
|
+
SET
|
|
1583
|
+
message_count = GREATEST(message_count - ${counted}, 0),
|
|
1584
|
+
last_message_at = (
|
|
1585
|
+
SELECT created_at FROM agent_messages
|
|
1586
|
+
WHERE project_id = ${input.projectId} AND thread_id = ${input.threadId}
|
|
1587
|
+
ORDER BY seq DESC
|
|
1588
|
+
LIMIT 1
|
|
1589
|
+
),
|
|
1590
|
+
updated_at = ${updatedAt}
|
|
1591
|
+
WHERE project_id = ${input.projectId} AND id = ${input.threadId}
|
|
1592
|
+
`;
|
|
1593
|
+
return deleted.length;
|
|
1594
|
+
});
|
|
1595
|
+
}
|
|
1596
|
+
async deleteFromSeq(input) {
|
|
1597
|
+
assertDeleteAgentMessagesFromSeqInput(input, "SixbPg");
|
|
1598
|
+
return runPgTransaction(this.sql, async (tx) => {
|
|
1599
|
+
const [thread] = await tx`
|
|
1600
|
+
SELECT id FROM agent_threads
|
|
1601
|
+
WHERE project_id = ${input.projectId} AND id = ${input.threadId}
|
|
1602
|
+
FOR UPDATE
|
|
1603
|
+
`;
|
|
1604
|
+
if (!thread) {
|
|
1605
|
+
throw new AgentStorageError4("thread_not_found", `[SixbPg] Agent thread '${input.threadId}' not found for project '${input.projectId}'.`);
|
|
1606
|
+
}
|
|
1607
|
+
const deleted = await tx`
|
|
1608
|
+
DELETE FROM agent_messages
|
|
1609
|
+
WHERE project_id = ${input.projectId}
|
|
1610
|
+
AND thread_id = ${input.threadId}
|
|
1611
|
+
AND seq >= ${input.fromSeq}
|
|
1612
|
+
RETURNING id, parts
|
|
1613
|
+
`;
|
|
1614
|
+
if (deleted.length === 0)
|
|
1615
|
+
return 0;
|
|
1616
|
+
await clampReadsToHead(tx, input);
|
|
1513
1617
|
const counted = deleted.filter((row) => countsAsThreadMessage({
|
|
1514
1618
|
parts: typeof row.parts === "string" ? JSON.parse(row.parts) : row.parts
|
|
1515
1619
|
})).length;
|
|
@@ -1566,6 +1670,142 @@ class PgAgentMessageStore {
|
|
|
1566
1670
|
return { messages: rows.map(rowToMessageRecord), hasMore, total };
|
|
1567
1671
|
}
|
|
1568
1672
|
}
|
|
1673
|
+
async function clampReadsToHead(tx, input) {
|
|
1674
|
+
await tx`
|
|
1675
|
+
UPDATE agent_thread_reads
|
|
1676
|
+
SET through_seq = head.seq
|
|
1677
|
+
FROM (
|
|
1678
|
+
SELECT COALESCE(MAX(seq), 0) AS seq FROM agent_messages
|
|
1679
|
+
WHERE project_id = ${input.projectId} AND thread_id = ${input.threadId}
|
|
1680
|
+
) AS head
|
|
1681
|
+
WHERE agent_thread_reads.project_id = ${input.projectId}
|
|
1682
|
+
AND agent_thread_reads.thread_id = ${input.threadId}
|
|
1683
|
+
AND agent_thread_reads.through_seq > head.seq
|
|
1684
|
+
`;
|
|
1685
|
+
}
|
|
1686
|
+
|
|
1687
|
+
// src/agents/reads.ts
|
|
1688
|
+
import {
|
|
1689
|
+
AgentStorageError as AgentStorageError5
|
|
1690
|
+
} from "@sixb/core/storage";
|
|
1691
|
+
class PgAgentThreadReadStore {
|
|
1692
|
+
sql;
|
|
1693
|
+
constructor(sql) {
|
|
1694
|
+
this.sql = sql;
|
|
1695
|
+
}
|
|
1696
|
+
async mark(input) {
|
|
1697
|
+
return runPgTransaction(this.sql, async (tx) => {
|
|
1698
|
+
const [thread] = await tx`
|
|
1699
|
+
SELECT id FROM agent_threads
|
|
1700
|
+
WHERE project_id = ${input.projectId} AND id = ${input.threadId}
|
|
1701
|
+
FOR SHARE
|
|
1702
|
+
`;
|
|
1703
|
+
if (!thread) {
|
|
1704
|
+
throw new AgentStorageError5("thread_not_found", `[SixbPg] Agent thread '${input.threadId}' not found for project '${input.projectId}'.`);
|
|
1705
|
+
}
|
|
1706
|
+
const reached = await reachedSeq(tx, input);
|
|
1707
|
+
if (reached !== null) {
|
|
1708
|
+
await tx`
|
|
1709
|
+
INSERT INTO agent_thread_reads (
|
|
1710
|
+
project_id,
|
|
1711
|
+
thread_id,
|
|
1712
|
+
principal_type,
|
|
1713
|
+
principal_id,
|
|
1714
|
+
through_seq,
|
|
1715
|
+
read_at
|
|
1716
|
+
) VALUES (
|
|
1717
|
+
${input.projectId},
|
|
1718
|
+
${input.threadId},
|
|
1719
|
+
'user',
|
|
1720
|
+
${input.principal.id},
|
|
1721
|
+
${reached},
|
|
1722
|
+
${input.readAt ?? new Date}
|
|
1723
|
+
)
|
|
1724
|
+
ON CONFLICT (project_id, thread_id, principal_type, principal_id) DO UPDATE SET
|
|
1725
|
+
through_seq = GREATEST(agent_thread_reads.through_seq, EXCLUDED.through_seq),
|
|
1726
|
+
read_at = GREATEST(agent_thread_reads.read_at, EXCLUDED.read_at)
|
|
1727
|
+
`;
|
|
1728
|
+
}
|
|
1729
|
+
const [state] = await readStates(tx, input.projectId, input.principal.id, [input.threadId]);
|
|
1730
|
+
if (!state) {
|
|
1731
|
+
throw new Error(`[SixbPg] Agent thread '${input.threadId}' lost its read state.`);
|
|
1732
|
+
}
|
|
1733
|
+
return state;
|
|
1734
|
+
});
|
|
1735
|
+
}
|
|
1736
|
+
async list(input) {
|
|
1737
|
+
if (input.threadIds.length === 0)
|
|
1738
|
+
return [];
|
|
1739
|
+
return readStates(this.sql, input.projectId, input.principal.id, [...new Set(input.threadIds)]);
|
|
1740
|
+
}
|
|
1741
|
+
}
|
|
1742
|
+
async function reachedSeq(tx, input) {
|
|
1743
|
+
if (input.throughMessageId === undefined) {
|
|
1744
|
+
const [head] = await tx`
|
|
1745
|
+
SELECT COALESCE(MAX(seq), 0) AS seq FROM agent_messages
|
|
1746
|
+
WHERE project_id = ${input.projectId} AND thread_id = ${input.threadId}
|
|
1747
|
+
`;
|
|
1748
|
+
return Number(head?.seq ?? 0);
|
|
1749
|
+
}
|
|
1750
|
+
const [message] = await tx`
|
|
1751
|
+
SELECT seq FROM agent_messages
|
|
1752
|
+
WHERE project_id = ${input.projectId}
|
|
1753
|
+
AND thread_id = ${input.threadId}
|
|
1754
|
+
AND id = ${input.throughMessageId}
|
|
1755
|
+
`;
|
|
1756
|
+
return message ? Number(message.seq) : null;
|
|
1757
|
+
}
|
|
1758
|
+
async function readStates(sql, projectId, principalId, threadIds) {
|
|
1759
|
+
const rows = await sql`
|
|
1760
|
+
SELECT
|
|
1761
|
+
place.thread_id,
|
|
1762
|
+
place.head_seq,
|
|
1763
|
+
LEAST(COALESCE(place.through_seq, 0), place.head_seq) AS through_seq,
|
|
1764
|
+
place.read_at,
|
|
1765
|
+
(
|
|
1766
|
+
SELECT COUNT(*) FROM agent_messages AS message
|
|
1767
|
+
WHERE message.project_id = ${projectId}
|
|
1768
|
+
AND message.thread_id = place.thread_id
|
|
1769
|
+
AND message.seq > COALESCE(place.through_seq, 0)
|
|
1770
|
+
AND (
|
|
1771
|
+
message.role <> 'user'
|
|
1772
|
+
OR message.author_principal_type IS DISTINCT FROM 'user'
|
|
1773
|
+
OR message.author_principal_id IS DISTINCT FROM ${principalId}
|
|
1774
|
+
)
|
|
1775
|
+
AND NOT (
|
|
1776
|
+
jsonb_array_length(message.parts) > 0 AND NOT EXISTS (
|
|
1777
|
+
SELECT 1 FROM jsonb_array_elements(message.parts) AS part
|
|
1778
|
+
WHERE part->>'type' <> 'approval-outcome'
|
|
1779
|
+
)
|
|
1780
|
+
)
|
|
1781
|
+
) AS unread
|
|
1782
|
+
FROM (
|
|
1783
|
+
SELECT
|
|
1784
|
+
thread.id AS thread_id,
|
|
1785
|
+
(
|
|
1786
|
+
SELECT COALESCE(MAX(seq), 0) FROM agent_messages
|
|
1787
|
+
WHERE project_id = thread.project_id AND thread_id = thread.id
|
|
1788
|
+
) AS head_seq,
|
|
1789
|
+
reading.through_seq,
|
|
1790
|
+
reading.read_at
|
|
1791
|
+
FROM agent_threads AS thread
|
|
1792
|
+
LEFT JOIN agent_thread_reads AS reading
|
|
1793
|
+
ON reading.project_id = thread.project_id
|
|
1794
|
+
AND reading.thread_id = thread.id
|
|
1795
|
+
AND reading.principal_type = 'user'
|
|
1796
|
+
AND reading.principal_id = ${principalId}
|
|
1797
|
+
WHERE thread.project_id = ${projectId}
|
|
1798
|
+
AND thread.id = ANY(${[...threadIds]}::text[])
|
|
1799
|
+
) AS place
|
|
1800
|
+
`;
|
|
1801
|
+
return rows.map((row) => ({
|
|
1802
|
+
threadId: row.thread_id,
|
|
1803
|
+
throughSeq: Number(row.through_seq),
|
|
1804
|
+
headSeq: Number(row.head_seq),
|
|
1805
|
+
unread: Number(row.unread),
|
|
1806
|
+
...row.read_at === null ? {} : { readAt: new Date(row.read_at) }
|
|
1807
|
+
}));
|
|
1808
|
+
}
|
|
1569
1809
|
|
|
1570
1810
|
// src/agents/runs.ts
|
|
1571
1811
|
import {
|
|
@@ -1586,7 +1826,7 @@ import { AGENT_MESSAGE_CONTENT_VERSION as AGENT_MESSAGE_CONTENT_VERSION2 } from
|
|
|
1586
1826
|
import { serializeSixbFailure } from "@sixb/core/internal/errors";
|
|
1587
1827
|
import {
|
|
1588
1828
|
AGENT_RUN_FAILURE_CODES as AGENT_RUN_FAILURE_CODES2,
|
|
1589
|
-
AgentStorageError as
|
|
1829
|
+
AgentStorageError as AgentStorageError6
|
|
1590
1830
|
} from "@sixb/core/storage";
|
|
1591
1831
|
|
|
1592
1832
|
// src/run-list-query.ts
|
|
@@ -1671,11 +1911,11 @@ class PgAgentRunStore {
|
|
|
1671
1911
|
FOR UPDATE
|
|
1672
1912
|
`;
|
|
1673
1913
|
if (!thread) {
|
|
1674
|
-
throw new
|
|
1914
|
+
throw new AgentStorageError6("thread_not_found", `[SixbPg] Agent thread '${input.threadId}' not found for project '${input.projectId}'.`);
|
|
1675
1915
|
}
|
|
1676
1916
|
assertConversationThreadRow(thread, "takes turns", "SixbPg");
|
|
1677
1917
|
if (thread.active_run_id !== null) {
|
|
1678
|
-
throw new
|
|
1918
|
+
throw new AgentStorageError6("active_run_exists", `[SixbPg] Agent thread '${input.threadId}' already has an active run '${thread.active_run_id}'.`);
|
|
1679
1919
|
}
|
|
1680
1920
|
assertTurnInThreadPlace(thread.runs_on, input.spec, input.threadId, "SixbPg");
|
|
1681
1921
|
const [row] = await tx`
|
|
@@ -1716,7 +1956,7 @@ class PgAgentRunStore {
|
|
|
1716
1956
|
});
|
|
1717
1957
|
} catch (error) {
|
|
1718
1958
|
if (isUniqueViolation(error)) {
|
|
1719
|
-
throw new
|
|
1959
|
+
throw new AgentStorageError6("duplicate_id", `[SixbPg] Agent run '${input.id}' already exists for project '${input.projectId}'.`);
|
|
1720
1960
|
}
|
|
1721
1961
|
throw error;
|
|
1722
1962
|
}
|
|
@@ -1737,7 +1977,7 @@ class PgAgentRunStore {
|
|
|
1737
1977
|
FOR UPDATE
|
|
1738
1978
|
`;
|
|
1739
1979
|
if (!parent || parent.kind !== "conversation") {
|
|
1740
|
-
throw new
|
|
1980
|
+
throw new AgentStorageError6("run_not_found", `[SixbPg] Parent Agent run '${input.parentRunId}' was not found.`);
|
|
1741
1981
|
}
|
|
1742
1982
|
const [existingRow] = await tx`
|
|
1743
1983
|
SELECT * FROM agent_runs
|
|
@@ -1747,16 +1987,16 @@ class PgAgentRunStore {
|
|
|
1747
1987
|
const existing = rowToRunRecord(existingRow);
|
|
1748
1988
|
if (subagentRunMatchesCreateInput(existing, input))
|
|
1749
1989
|
return existing;
|
|
1750
|
-
throw new
|
|
1990
|
+
throw new AgentStorageError6("duplicate_id", `[SixbPg] Subagent run '${input.id}' already exists with different immutable inputs.`);
|
|
1751
1991
|
}
|
|
1752
1992
|
if (parent.status !== "running") {
|
|
1753
|
-
throw new
|
|
1993
|
+
throw new AgentStorageError6("invalid_state", `[SixbPg] Parent Agent run '${parent.id}' is not running (status '${parent.status}').`);
|
|
1754
1994
|
}
|
|
1755
1995
|
if (execution.source.type !== "execution" || execution.source.executionId !== parent.execution_id) {
|
|
1756
|
-
throw new
|
|
1996
|
+
throw new AgentStorageError6("invalid_input", `[SixbPg] Subagent execution '${execution.id}' is not a child of parent execution '${parent.execution_id}'.`);
|
|
1757
1997
|
}
|
|
1758
1998
|
if (parent.execution_token !== input.parentExecutionToken) {
|
|
1759
|
-
throw new
|
|
1999
|
+
throw new AgentStorageError6("execution_lost", `[SixbPg] Parent Agent run '${parent.id}' is no longer owned by this execution.`);
|
|
1760
2000
|
}
|
|
1761
2001
|
const [countRow] = await tx`
|
|
1762
2002
|
SELECT COUNT(*)::bigint AS count FROM agent_runs
|
|
@@ -1766,7 +2006,7 @@ class PgAgentRunStore {
|
|
|
1766
2006
|
`;
|
|
1767
2007
|
const activeChildren = Number(countRow?.count ?? 0);
|
|
1768
2008
|
if (activeChildren >= input.maxActiveChildren) {
|
|
1769
|
-
throw new
|
|
2009
|
+
throw new AgentStorageError6("active_child_limit", `[SixbPg] Agent run '${parent.id}' already has ${activeChildren} active subagents.`);
|
|
1770
2010
|
}
|
|
1771
2011
|
const createdAt = input.createdAt ?? new Date;
|
|
1772
2012
|
await claimChildThread(tx, input, parent, createdAt);
|
|
@@ -1803,7 +2043,7 @@ class PgAgentRunStore {
|
|
|
1803
2043
|
});
|
|
1804
2044
|
} catch (error) {
|
|
1805
2045
|
if (isUniqueViolation(error)) {
|
|
1806
|
-
throw new
|
|
2046
|
+
throw new AgentStorageError6("duplicate_id", `[SixbPg] Subagent run '${input.id}' conflicts with an existing durable spawn.`);
|
|
1807
2047
|
}
|
|
1808
2048
|
throw error;
|
|
1809
2049
|
}
|
|
@@ -1855,7 +2095,7 @@ class PgAgentRunStore {
|
|
|
1855
2095
|
return runPgTransaction(this.sql, async (tx) => {
|
|
1856
2096
|
const run = await this.lockRunning(tx, input.projectId, input.id);
|
|
1857
2097
|
if (input.ifLapsedBy && run.execution_queue_lease_expires_at !== null && new Date(run.execution_queue_lease_expires_at).getTime() > input.ifLapsedBy.getTime()) {
|
|
1858
|
-
throw new
|
|
2098
|
+
throw new AgentStorageError6("invalid_state", `[SixbPg] Agent run '${input.id}' is still held by its runner, whose lease has not lapsed.`);
|
|
1859
2099
|
}
|
|
1860
2100
|
const [row] = await tx`
|
|
1861
2101
|
UPDATE agent_runs
|
|
@@ -1873,7 +2113,7 @@ class PgAgentRunStore {
|
|
|
1873
2113
|
return runPgTransaction(this.sql, async (tx) => {
|
|
1874
2114
|
const run = await this.lockRunning(tx, input.projectId, input.id);
|
|
1875
2115
|
if (run.execution_token !== input.executionToken) {
|
|
1876
|
-
throw new
|
|
2116
|
+
throw new AgentStorageError6("execution_lost", `[SixbPg] Execution token is no longer current on agent run '${input.id}'.`);
|
|
1877
2117
|
}
|
|
1878
2118
|
const [row] = await tx`
|
|
1879
2119
|
UPDATE agent_runs
|
|
@@ -1899,7 +2139,7 @@ class PgAgentRunStore {
|
|
|
1899
2139
|
return rowToRunRecord(updated);
|
|
1900
2140
|
const current = await this.getById(input);
|
|
1901
2141
|
if (!current) {
|
|
1902
|
-
throw new
|
|
2142
|
+
throw new AgentStorageError6("run_not_found", `[SixbPg] Agent run '${input.id}' not found for project '${input.projectId}'.`);
|
|
1903
2143
|
}
|
|
1904
2144
|
return current;
|
|
1905
2145
|
}
|
|
@@ -1910,7 +2150,7 @@ class PgAgentRunStore {
|
|
|
1910
2150
|
const run = await this.lockRunning(tx, input.projectId, input.id);
|
|
1911
2151
|
assertAgentRunFinishPayload(run.kind, input, run.id, "SixbPg");
|
|
1912
2152
|
if (run.execution_token !== input.executionToken) {
|
|
1913
|
-
throw new
|
|
2153
|
+
throw new AgentStorageError6("execution_lost", `[SixbPg] Execution token is no longer current on agent run '${input.id}'.`);
|
|
1914
2154
|
}
|
|
1915
2155
|
if (input.finishReason === "awaiting_approval") {
|
|
1916
2156
|
assertAwaitingApprovalFinish({
|
|
@@ -1975,6 +2215,12 @@ class PgAgentRunStore {
|
|
|
1975
2215
|
AND child.kind = 'subagent'
|
|
1976
2216
|
${input.spawnKey === undefined ? this.sql`` : this.sql`AND child.spawn_key = ${input.spawnKey}`}
|
|
1977
2217
|
${input.finishReason === undefined ? this.sql`` : this.sql`AND child.finish_reason = ${input.finishReason}`}
|
|
2218
|
+
${input.retainedTurnsOnly ? this.sql`AND EXISTS (
|
|
2219
|
+
SELECT 1 FROM agent_messages m
|
|
2220
|
+
WHERE m.project_id = turn.project_id
|
|
2221
|
+
AND m.thread_id = turn.thread_id
|
|
2222
|
+
AND m.id = turn.trigger_message_id
|
|
2223
|
+
)` : this.sql``}
|
|
1978
2224
|
ORDER BY COALESCE(turn.started_at, turn.created_at) DESC, turn.id DESC,
|
|
1979
2225
|
child.created_at DESC, child.id DESC
|
|
1980
2226
|
LIMIT ${input.limit ?? 100}
|
|
@@ -1984,6 +2230,18 @@ class PgAgentRunStore {
|
|
|
1984
2230
|
return run.kind === "subagent" ? [run] : [];
|
|
1985
2231
|
});
|
|
1986
2232
|
}
|
|
2233
|
+
async latestByThread(input) {
|
|
2234
|
+
if (input.threadIds.length === 0)
|
|
2235
|
+
return [];
|
|
2236
|
+
const rows = await this.sql`
|
|
2237
|
+
SELECT DISTINCT ON (thread_id) *
|
|
2238
|
+
FROM agent_runs
|
|
2239
|
+
WHERE project_id = ${input.projectId}
|
|
2240
|
+
AND thread_id = ANY(${[...input.threadIds]}::text[])
|
|
2241
|
+
ORDER BY thread_id, created_at DESC, id DESC
|
|
2242
|
+
`;
|
|
2243
|
+
return rows.map(rowToRunRecord);
|
|
2244
|
+
}
|
|
1987
2245
|
async list(input) {
|
|
1988
2246
|
if (hasEmptyStatuses(input) || input.kinds?.length === 0) {
|
|
1989
2247
|
return { runs: [], hasMore: false, total: 0 };
|
|
@@ -2029,10 +2287,10 @@ class PgAgentRunStore {
|
|
|
2029
2287
|
FOR UPDATE
|
|
2030
2288
|
`;
|
|
2031
2289
|
if (!row) {
|
|
2032
|
-
throw new
|
|
2290
|
+
throw new AgentStorageError6("run_not_found", `[SixbPg] Agent run '${id}' not found for project '${projectId}'.`);
|
|
2033
2291
|
}
|
|
2034
2292
|
if (row.status !== status) {
|
|
2035
|
-
throw new
|
|
2293
|
+
throw new AgentStorageError6("invalid_state", `[SixbPg] Agent run '${id}' is not ${status} (status '${row.status}').`);
|
|
2036
2294
|
}
|
|
2037
2295
|
return row;
|
|
2038
2296
|
}
|
|
@@ -2063,7 +2321,7 @@ async function claimChildThread(tx, input, parent, createdAt) {
|
|
|
2063
2321
|
SELECT * FROM agent_threads WHERE project_id = ${input.projectId} AND id = ${parent.thread_id}
|
|
2064
2322
|
`;
|
|
2065
2323
|
if (!conversation) {
|
|
2066
|
-
throw new
|
|
2324
|
+
throw new AgentStorageError6("thread_not_found", `[SixbPg] Agent thread '${parent.thread_id}' not found for project '${input.projectId}'.`);
|
|
2067
2325
|
}
|
|
2068
2326
|
const thread = newSubagentThreadRecord(input, rowToThreadRecord(conversation), createdAt);
|
|
2069
2327
|
await tx`
|
|
@@ -2102,7 +2360,7 @@ async function settleFinishedRunApprovals(tx, kind, input, completedAt) {
|
|
|
2102
2360
|
// src/agents/settings.ts
|
|
2103
2361
|
import { assertAgentApprovalModeInput } from "@sixb/core/internal/agent-run-storage-provider";
|
|
2104
2362
|
import {
|
|
2105
|
-
AgentStorageError as
|
|
2363
|
+
AgentStorageError as AgentStorageError7
|
|
2106
2364
|
} from "@sixb/core/storage";
|
|
2107
2365
|
class PgAgentApprovalModeStore {
|
|
2108
2366
|
sql;
|
|
@@ -2176,7 +2434,7 @@ class PgAgentApprovalModeStore {
|
|
|
2176
2434
|
}
|
|
2177
2435
|
}
|
|
2178
2436
|
function threadNotFound(input) {
|
|
2179
|
-
return new
|
|
2437
|
+
return new AgentStorageError7("thread_not_found", `[SixbPg] Agent thread '${input.threadId}' not found for project '${input.projectId}'.`);
|
|
2180
2438
|
}
|
|
2181
2439
|
|
|
2182
2440
|
// src/agents/threads.ts
|
|
@@ -2190,12 +2448,13 @@ import {
|
|
|
2190
2448
|
participantsToAdd,
|
|
2191
2449
|
planAgentThreadFork,
|
|
2192
2450
|
queuedTurnInCloud,
|
|
2451
|
+
queuedTurnOnComputer,
|
|
2193
2452
|
remapArtifactIds,
|
|
2194
2453
|
snapshotAgentThreadSandboxParams as snapshotAgentThreadSandboxParams2,
|
|
2195
2454
|
transitionAgentThreadSandbox
|
|
2196
2455
|
} from "@sixb/core/internal/agent-run-storage-provider";
|
|
2197
2456
|
import {
|
|
2198
|
-
AgentStorageError as
|
|
2457
|
+
AgentStorageError as AgentStorageError8
|
|
2199
2458
|
} from "@sixb/core/storage";
|
|
2200
2459
|
class PgAgentThreadStore {
|
|
2201
2460
|
sql;
|
|
@@ -2257,7 +2516,7 @@ class PgAgentThreadStore {
|
|
|
2257
2516
|
return rowToThreadRecord(row);
|
|
2258
2517
|
} catch (error) {
|
|
2259
2518
|
if (isUniqueViolation(error)) {
|
|
2260
|
-
throw new
|
|
2519
|
+
throw new AgentStorageError8("duplicate_id", `[SixbPg] Agent thread '${input.id}' already exists for project '${input.projectId}'.`);
|
|
2261
2520
|
}
|
|
2262
2521
|
throw error;
|
|
2263
2522
|
}
|
|
@@ -2271,7 +2530,7 @@ class PgAgentThreadStore {
|
|
|
2271
2530
|
FOR SHARE
|
|
2272
2531
|
`;
|
|
2273
2532
|
if (!source) {
|
|
2274
|
-
throw new
|
|
2533
|
+
throw new AgentStorageError8("thread_not_found", `[SixbPg] Agent thread '${input.sourceThreadId}' not found for project '${input.projectId}'.`);
|
|
2275
2534
|
}
|
|
2276
2535
|
assertConversationThreadRow2(source, "can be forked", "SixbPg");
|
|
2277
2536
|
const [activeRun] = source.active_run_id === null ? [] : await tx`
|
|
@@ -2338,6 +2597,31 @@ class PgAgentThreadStore {
|
|
|
2338
2597
|
)
|
|
2339
2598
|
RETURNING *
|
|
2340
2599
|
`;
|
|
2600
|
+
if (input.ownerPrincipal.type === "user") {
|
|
2601
|
+
await tx`
|
|
2602
|
+
INSERT INTO agent_thread_reads (
|
|
2603
|
+
project_id,
|
|
2604
|
+
thread_id,
|
|
2605
|
+
principal_type,
|
|
2606
|
+
principal_id,
|
|
2607
|
+
through_seq,
|
|
2608
|
+
read_at
|
|
2609
|
+
)
|
|
2610
|
+
SELECT
|
|
2611
|
+
${input.projectId},
|
|
2612
|
+
${input.id},
|
|
2613
|
+
'user',
|
|
2614
|
+
${input.ownerPrincipal.id},
|
|
2615
|
+
LEAST(source.through_seq, ${plan.headSeq}),
|
|
2616
|
+
source.read_at
|
|
2617
|
+
FROM agent_thread_reads AS source
|
|
2618
|
+
WHERE source.project_id = ${input.projectId}
|
|
2619
|
+
AND source.thread_id = ${input.sourceThreadId}
|
|
2620
|
+
AND source.principal_type = 'user'
|
|
2621
|
+
AND source.principal_id = ${input.ownerPrincipal.id}
|
|
2622
|
+
ON CONFLICT DO NOTHING
|
|
2623
|
+
`;
|
|
2624
|
+
}
|
|
2341
2625
|
if (plan.messages.length > 0) {
|
|
2342
2626
|
await tx`
|
|
2343
2627
|
INSERT INTO agent_messages (
|
|
@@ -2370,8 +2654,8 @@ class PgAgentThreadStore {
|
|
|
2370
2654
|
message.created_at,
|
|
2371
2655
|
message.completed_at
|
|
2372
2656
|
FROM unnest(
|
|
2373
|
-
${
|
|
2374
|
-
${
|
|
2657
|
+
${plan.messages.map((copy) => copy.sourceId)}::text[],
|
|
2658
|
+
${plan.messages.map((copy) => copy.id)}::text[]
|
|
2375
2659
|
) AS copy(source_id, id)
|
|
2376
2660
|
JOIN agent_messages AS message
|
|
2377
2661
|
ON message.project_id = ${input.projectId} AND message.id = copy.source_id
|
|
@@ -2385,7 +2669,7 @@ class PgAgentThreadStore {
|
|
|
2385
2669
|
});
|
|
2386
2670
|
} catch (error) {
|
|
2387
2671
|
if (isUniqueViolationOf(error, "agent_threads_pkey")) {
|
|
2388
|
-
throw new
|
|
2672
|
+
throw new AgentStorageError8("duplicate_id", `[SixbPg] Agent thread '${input.id}' already exists for project '${input.projectId}'.`);
|
|
2389
2673
|
}
|
|
2390
2674
|
throw error;
|
|
2391
2675
|
}
|
|
@@ -2405,7 +2689,7 @@ class PgAgentThreadStore {
|
|
|
2405
2689
|
SELECT *
|
|
2406
2690
|
FROM agent_threads
|
|
2407
2691
|
WHERE project_id = ${params.projectId}
|
|
2408
|
-
AND id = ANY(${
|
|
2692
|
+
AND id = ANY(${ids}::text[])
|
|
2409
2693
|
`;
|
|
2410
2694
|
const byId = new Map(rows.map((row) => [row.id, row]));
|
|
2411
2695
|
return ids.flatMap((id) => {
|
|
@@ -2478,7 +2762,7 @@ class PgAgentThreadStore {
|
|
|
2478
2762
|
FOR UPDATE
|
|
2479
2763
|
`;
|
|
2480
2764
|
if (!thread) {
|
|
2481
|
-
throw new
|
|
2765
|
+
throw new AgentStorageError8("thread_not_found", `[SixbPg] Agent thread '${input.threadId}' not found for project '${input.projectId}'.`);
|
|
2482
2766
|
}
|
|
2483
2767
|
assertConversationThreadRow2(thread, "has participants", "SixbPg");
|
|
2484
2768
|
const existing = await selectParticipants(tx, input.projectId, [input.threadId]);
|
|
@@ -2505,29 +2789,80 @@ class PgAgentThreadStore {
|
|
|
2505
2789
|
)
|
|
2506
2790
|
`;
|
|
2507
2791
|
}
|
|
2792
|
+
for (const principal of added) {
|
|
2793
|
+
await tx`
|
|
2794
|
+
INSERT INTO agent_thread_reads (
|
|
2795
|
+
project_id,
|
|
2796
|
+
thread_id,
|
|
2797
|
+
principal_type,
|
|
2798
|
+
principal_id,
|
|
2799
|
+
through_seq,
|
|
2800
|
+
read_at
|
|
2801
|
+
) VALUES (
|
|
2802
|
+
${input.projectId},
|
|
2803
|
+
${input.threadId},
|
|
2804
|
+
${principal.type},
|
|
2805
|
+
${principal.id},
|
|
2806
|
+
(
|
|
2807
|
+
SELECT COALESCE(MAX(seq), 0) FROM agent_messages
|
|
2808
|
+
WHERE project_id = ${input.projectId} AND thread_id = ${input.threadId}
|
|
2809
|
+
),
|
|
2810
|
+
${addedAt}
|
|
2811
|
+
)
|
|
2812
|
+
ON CONFLICT (project_id, thread_id, principal_type, principal_id) DO UPDATE SET
|
|
2813
|
+
through_seq = EXCLUDED.through_seq,
|
|
2814
|
+
read_at = EXCLUDED.read_at
|
|
2815
|
+
`;
|
|
2816
|
+
}
|
|
2508
2817
|
return added.length === 0 ? existing : selectParticipants(tx, input.projectId, [input.threadId]);
|
|
2509
2818
|
});
|
|
2510
2819
|
}
|
|
2511
2820
|
async removeParticipant(input) {
|
|
2512
|
-
|
|
2513
|
-
|
|
2514
|
-
|
|
2515
|
-
|
|
2516
|
-
|
|
2517
|
-
|
|
2518
|
-
|
|
2519
|
-
|
|
2520
|
-
|
|
2821
|
+
return runPgTransaction(this.sql, async (tx) => {
|
|
2822
|
+
const rows = await tx`
|
|
2823
|
+
DELETE FROM agent_thread_participants
|
|
2824
|
+
WHERE project_id = ${input.projectId}
|
|
2825
|
+
AND thread_id = ${input.threadId}
|
|
2826
|
+
AND principal_type = ${input.principal.type}
|
|
2827
|
+
AND principal_id = ${input.principal.id}
|
|
2828
|
+
RETURNING principal_id
|
|
2829
|
+
`;
|
|
2830
|
+
await tx`
|
|
2831
|
+
DELETE FROM agent_thread_reads
|
|
2832
|
+
WHERE project_id = ${input.projectId}
|
|
2833
|
+
AND thread_id = ${input.threadId}
|
|
2834
|
+
AND principal_type = ${input.principal.type}
|
|
2835
|
+
AND principal_id = ${input.principal.id}
|
|
2836
|
+
`;
|
|
2837
|
+
return rows.length > 0;
|
|
2838
|
+
});
|
|
2521
2839
|
}
|
|
2522
2840
|
async assignComputer(input) {
|
|
2523
2841
|
assertAgentThreadComputer(input.computer, "SixbPg");
|
|
2524
2842
|
return runPgTransaction(this.sql, async (tx) => {
|
|
2525
2843
|
const { thread, active } = await lockThreadAndActiveRun(tx, input);
|
|
2526
2844
|
assertAssignableComputer(thread, active, input.computer, input.id, "SixbPg");
|
|
2845
|
+
const queued = queuedTurnOnComputer(active, input.computer);
|
|
2846
|
+
if (queued) {
|
|
2847
|
+
await tx`
|
|
2848
|
+
UPDATE agent_runs SET spec = ${JSON.stringify(queued.spec)}::text::jsonb
|
|
2849
|
+
WHERE project_id = ${input.projectId} AND id = ${queued.id}
|
|
2850
|
+
`;
|
|
2851
|
+
}
|
|
2852
|
+
if (thread.runsOn !== "computer") {
|
|
2853
|
+
await tx`
|
|
2854
|
+
UPDATE agent_threads
|
|
2855
|
+
SET sandbox_state = jsonb_set(sandbox_state, '{status}', '"unavailable"'::jsonb)
|
|
2856
|
+
WHERE project_id = ${input.projectId}
|
|
2857
|
+
AND parent_thread_id = ${input.id}
|
|
2858
|
+
AND sandbox_state->>'status' IN ('new', 'ready')
|
|
2859
|
+
`;
|
|
2860
|
+
}
|
|
2527
2861
|
const movedAt = movesComputerPlace(thread, input.computer) ? input.movedAt ?? new Date : null;
|
|
2528
2862
|
const [assigned] = await tx`
|
|
2529
2863
|
UPDATE agent_threads
|
|
2530
|
-
SET
|
|
2864
|
+
SET runs_on = 'computer',
|
|
2865
|
+
computer_id = ${input.computer.id},
|
|
2531
2866
|
computer_name = ${input.computer.name},
|
|
2532
2867
|
computer_folder = ${input.computer.folder},
|
|
2533
2868
|
moved_at = COALESCE(${movedAt}::timestamptz, moved_at)
|
|
@@ -2587,7 +2922,7 @@ async function lockThreadAndActiveRun(tx, input) {
|
|
|
2587
2922
|
};
|
|
2588
2923
|
}
|
|
2589
2924
|
if (attempt >= 3) {
|
|
2590
|
-
throw new
|
|
2925
|
+
throw new AgentStorageError8("invalid_state", `[SixbPg] Agent thread '${input.id}' kept changing turns while it moved: try again.`);
|
|
2591
2926
|
}
|
|
2592
2927
|
}
|
|
2593
2928
|
}
|
|
@@ -2599,7 +2934,7 @@ async function selectParticipants(sql, projectId, threadIds) {
|
|
|
2599
2934
|
SELECT *
|
|
2600
2935
|
FROM agent_thread_participants
|
|
2601
2936
|
WHERE project_id = ${projectId}
|
|
2602
|
-
AND thread_id = ANY(${
|
|
2937
|
+
AND thread_id = ANY(${[...threadIds]}::text[])
|
|
2603
2938
|
ORDER BY added_at, principal_id COLLATE "C", thread_id COLLATE "C"
|
|
2604
2939
|
`;
|
|
2605
2940
|
return rows.map(rowToParticipantRecord);
|
|
@@ -2642,7 +2977,7 @@ async function remapCopiedParts(sql, projectId, messageIds, copies) {
|
|
|
2642
2977
|
return;
|
|
2643
2978
|
const rows = await sql`
|
|
2644
2979
|
SELECT id, parts::text AS parts FROM agent_messages
|
|
2645
|
-
WHERE project_id = ${projectId} AND id = ANY(${
|
|
2980
|
+
WHERE project_id = ${projectId} AND id = ANY(${[...messageIds]}::text[])
|
|
2646
2981
|
`;
|
|
2647
2982
|
for (const row of rows) {
|
|
2648
2983
|
const parts = remapArtifactIds(row.parts, copies);
|
|
@@ -2717,6 +3052,7 @@ class PgAgentTokenRateStore {
|
|
|
2717
3052
|
// src/agents/agent-storage.ts
|
|
2718
3053
|
class PgAgentStorage {
|
|
2719
3054
|
threads;
|
|
3055
|
+
reads;
|
|
2720
3056
|
runs;
|
|
2721
3057
|
messages;
|
|
2722
3058
|
checkpoints;
|
|
@@ -2728,6 +3064,7 @@ class PgAgentStorage {
|
|
|
2728
3064
|
inbox;
|
|
2729
3065
|
constructor(options) {
|
|
2730
3066
|
this.threads = new PgAgentThreadStore(options.sql);
|
|
3067
|
+
this.reads = new PgAgentThreadReadStore(options.sql);
|
|
2731
3068
|
this.runs = new PgAgentRunStore(options.sql, options.executions);
|
|
2732
3069
|
this.messages = new PgAgentMessageStore(options.sql);
|
|
2733
3070
|
this.checkpoints = new PgAgentContextCheckpointStore(options.sql);
|
|
@@ -11245,6 +11582,97 @@ CREATE INDEX idx_agent_thread_inbox_claimed_run
|
|
|
11245
11582
|
WHERE claimed_by_run_id IS NOT NULL;
|
|
11246
11583
|
`;
|
|
11247
11584
|
|
|
11585
|
+
// src/migrations/107-agent-inbox-edits.sql
|
|
11586
|
+
var _107_agent_inbox_edits_default = `-- An edit sent while a turn runs waits here for that turn to end, then rewinds the thread to the
|
|
11587
|
+
-- message it replaces.
|
|
11588
|
+
ALTER TABLE agent_thread_inbox ADD COLUMN replaces_message_id TEXT;
|
|
11589
|
+
`;
|
|
11590
|
+
|
|
11591
|
+
// src/migrations/108-agent-thread-reads.sql
|
|
11592
|
+
var _108_agent_thread_reads_default = `-- Where each person's reading of a conversation has reached: the seq of the last message they saw.
|
|
11593
|
+
-- Nothing writes here when a message arrives: new ones sit past it until the reader moves it. A
|
|
11594
|
+
-- retry or an edit that deletes messages pulls every place inside them back to just before them,
|
|
11595
|
+
-- because the numbers they held go to what replaces them.
|
|
11596
|
+
CREATE TABLE agent_thread_reads (
|
|
11597
|
+
project_id TEXT NOT NULL,
|
|
11598
|
+
thread_id TEXT NOT NULL,
|
|
11599
|
+
principal_type TEXT NOT NULL CHECK (principal_type = 'user'),
|
|
11600
|
+
principal_id TEXT NOT NULL CHECK (length(principal_id) > 0),
|
|
11601
|
+
through_seq INTEGER NOT NULL CHECK (through_seq >= 0),
|
|
11602
|
+
read_at TIMESTAMPTZ NOT NULL,
|
|
11603
|
+
PRIMARY KEY (project_id, thread_id, principal_type, principal_id),
|
|
11604
|
+
FOREIGN KEY (project_id, thread_id) REFERENCES agent_threads (project_id, id) ON DELETE CASCADE
|
|
11605
|
+
);
|
|
11606
|
+
|
|
11607
|
+
-- Everyone starts caught up: the first deploy must not mark every thread that already existed as
|
|
11608
|
+
-- new. Only a person reads, so an owner that is system code or a service account gets no row; it
|
|
11609
|
+
-- would break the CHECK above. From here on, a participant gets a row when they join, and an
|
|
11610
|
+
-- owner when they first mark the thread read: until then, they read as 0.
|
|
11611
|
+
INSERT INTO agent_thread_reads (
|
|
11612
|
+
project_id,
|
|
11613
|
+
thread_id,
|
|
11614
|
+
principal_type,
|
|
11615
|
+
principal_id,
|
|
11616
|
+
through_seq,
|
|
11617
|
+
read_at
|
|
11618
|
+
)
|
|
11619
|
+
SELECT
|
|
11620
|
+
project_id,
|
|
11621
|
+
id,
|
|
11622
|
+
owner_principal_type,
|
|
11623
|
+
owner_principal_id,
|
|
11624
|
+
COALESCE(
|
|
11625
|
+
(
|
|
11626
|
+
SELECT MAX(seq) FROM agent_messages
|
|
11627
|
+
WHERE agent_messages.project_id = agent_threads.project_id
|
|
11628
|
+
AND agent_messages.thread_id = agent_threads.id
|
|
11629
|
+
),
|
|
11630
|
+
0
|
|
11631
|
+
),
|
|
11632
|
+
now()
|
|
11633
|
+
FROM agent_threads
|
|
11634
|
+
WHERE kind = 'conversation'
|
|
11635
|
+
AND owner_principal_type = 'user'
|
|
11636
|
+
ON CONFLICT DO NOTHING;
|
|
11637
|
+
|
|
11638
|
+
INSERT INTO agent_thread_reads (
|
|
11639
|
+
project_id,
|
|
11640
|
+
thread_id,
|
|
11641
|
+
principal_type,
|
|
11642
|
+
principal_id,
|
|
11643
|
+
through_seq,
|
|
11644
|
+
read_at
|
|
11645
|
+
)
|
|
11646
|
+
SELECT
|
|
11647
|
+
participant.project_id,
|
|
11648
|
+
participant.thread_id,
|
|
11649
|
+
participant.principal_type,
|
|
11650
|
+
participant.principal_id,
|
|
11651
|
+
COALESCE(
|
|
11652
|
+
(
|
|
11653
|
+
SELECT MAX(seq) FROM agent_messages
|
|
11654
|
+
WHERE agent_messages.project_id = thread.project_id
|
|
11655
|
+
AND agent_messages.thread_id = thread.id
|
|
11656
|
+
),
|
|
11657
|
+
0
|
|
11658
|
+
),
|
|
11659
|
+
now()
|
|
11660
|
+
FROM agent_thread_participants AS participant
|
|
11661
|
+
JOIN agent_threads AS thread
|
|
11662
|
+
ON thread.project_id = participant.project_id
|
|
11663
|
+
AND thread.id = participant.thread_id
|
|
11664
|
+
ON CONFLICT DO NOTHING;
|
|
11665
|
+
`;
|
|
11666
|
+
|
|
11667
|
+
// src/migrations/109-sync-run-connections.sql
|
|
11668
|
+
var _109_sync_run_connections_default = `-- A sync of an OAuth connector runs once per connection. The column stays NULL for a sync of a
|
|
11669
|
+
-- static connector, and for runs from before this migration.
|
|
11670
|
+
ALTER TABLE sync_runs ADD COLUMN connection_id TEXT;
|
|
11671
|
+
|
|
11672
|
+
CREATE INDEX idx_sync_runs_project_sync_connection_started
|
|
11673
|
+
ON sync_runs (project_id, sync_id, connection_id, COALESCE(started_at, queued_at) DESC);
|
|
11674
|
+
`;
|
|
11675
|
+
|
|
11248
11676
|
// src/migrations.ts
|
|
11249
11677
|
var POSTGRES_STORAGE_ADAPTER_ID = "SixbPostgresStorage";
|
|
11250
11678
|
function pgSql(id, sqlText) {
|
|
@@ -11517,7 +11945,10 @@ var postgresStorageMigrations = defineMigrations({
|
|
|
11517
11945
|
pgSql("103-script-executions", _103_script_executions_default),
|
|
11518
11946
|
pgSql("104-artifact-pdfs", _104_artifact_pdfs_default),
|
|
11519
11947
|
pgSql("105-guest-comment-links", _105_guest_comment_links_default),
|
|
11520
|
-
pgSql("106-agent-thread-inbox", _106_agent_thread_inbox_default)
|
|
11948
|
+
pgSql("106-agent-thread-inbox", _106_agent_thread_inbox_default),
|
|
11949
|
+
pgSql("107-agent-inbox-edits", _107_agent_inbox_edits_default),
|
|
11950
|
+
pgSql("108-agent-thread-reads", _108_agent_thread_reads_default),
|
|
11951
|
+
pgSql("109-sync-run-connections", _109_sync_run_connections_default)
|
|
11521
11952
|
]
|
|
11522
11953
|
});
|
|
11523
11954
|
|
|
@@ -15852,7 +16283,7 @@ async function stageSourceRoots(sql, manifest, input) {
|
|
|
15852
16283
|
const stored = await sql`
|
|
15853
16284
|
SELECT root_key, staging_ordinal, deleted FROM ontology_source_roots
|
|
15854
16285
|
WHERE version_id = ${manifest.version_id}
|
|
15855
|
-
AND root_key = ANY(${
|
|
16286
|
+
AND root_key = ANY(${skipped.map((root) => root.rootKey)}::text[])
|
|
15856
16287
|
`;
|
|
15857
16288
|
const existing = new Map(stored.map((root) => [root.root_key, root]));
|
|
15858
16289
|
if (skipped.some((root) => {
|
|
@@ -17109,7 +17540,7 @@ async function endpoints(sql, versionId, projectId, refs) {
|
|
|
17109
17540
|
AND primary_id = requested.key::jsonb->>1
|
|
17110
17541
|
)
|
|
17111
17542
|
) AS exists
|
|
17112
|
-
FROM unnest(${
|
|
17543
|
+
FROM unnest(${unique.map((ref) => objectRefKey2(ref))}::text[]) AS requested(key)
|
|
17113
17544
|
`;
|
|
17114
17545
|
const existence = new Map(rows.map((row) => [row.key, row.exists]));
|
|
17115
17546
|
return unique.map((ref) => ({ ref, exists: existence.get(objectRefKey2(ref)) ?? false }));
|
|
@@ -17596,7 +18027,7 @@ class PgMaterializationSessions {
|
|
|
17596
18027
|
AND (sort_one, sort_two, record_key) > (${after[0]}, ${after[1]}, ${after[2]})
|
|
17597
18028
|
)
|
|
17598
18029
|
SELECT sort_one, sort_two, record_key, payload FROM changes
|
|
17599
|
-
WHERE ref->>'objectTypeId' = ANY(${
|
|
18030
|
+
WHERE ref->>'objectTypeId' = ANY(${[...objectTypeIds]}::text[])
|
|
17600
18031
|
OR EXISTS (
|
|
17601
18032
|
SELECT 1 FROM object_vectors
|
|
17602
18033
|
WHERE project_id = ${session.header.commit.projectId}
|
|
@@ -18675,7 +19106,7 @@ class PgOntologyMaterializationStorage {
|
|
|
18675
19106
|
SELECT * FROM ontology_sources
|
|
18676
19107
|
WHERE project_id = ${projectId}
|
|
18677
19108
|
AND source_id = ANY(
|
|
18678
|
-
${
|
|
19109
|
+
${expectedSources.map((expected) => expected.source.projectionId)}::text[]
|
|
18679
19110
|
)
|
|
18680
19111
|
AND status = 'active'
|
|
18681
19112
|
FOR UPDATE
|
|
@@ -19123,7 +19554,7 @@ class PgExecutionStorage {
|
|
|
19123
19554
|
async listDescendants(input) {
|
|
19124
19555
|
if (input.executionIds.length === 0)
|
|
19125
19556
|
return [];
|
|
19126
|
-
const ids =
|
|
19557
|
+
const ids = [...new Set(input.executionIds)];
|
|
19127
19558
|
const rows = await this.sql`
|
|
19128
19559
|
WITH RECURSIVE tree(id, parent_id) AS (
|
|
19129
19560
|
SELECT child.id, child.parent_execution_id
|
|
@@ -19226,7 +19657,7 @@ class PgOntologyOutboxStorage {
|
|
|
19226
19657
|
JOIN executions
|
|
19227
19658
|
ON executions.project_id = commits.project_id AND executions.id = commits.execution_id
|
|
19228
19659
|
WHERE commits.project_id = ${input.projectId}
|
|
19229
|
-
AND commits.id = ANY(${
|
|
19660
|
+
AND commits.id = ANY(${commitIds}::text[])
|
|
19230
19661
|
`;
|
|
19231
19662
|
const contextOf = new Map(contexts.map((row) => [row.commit_id, outboxEventContext(input.projectId, row)]));
|
|
19232
19663
|
return claimedOutboxRows(rows.map(claimedOutboxDraft), (commitId) => contextOf.get(commitId));
|
|
@@ -19243,7 +19674,7 @@ class PgOntologyOutboxStorage {
|
|
|
19243
19674
|
WITH leased AS MATERIALIZED (
|
|
19244
19675
|
SELECT id FROM ontology_outbox
|
|
19245
19676
|
WHERE project_id = ${input.projectId}
|
|
19246
|
-
AND id = ANY(${
|
|
19677
|
+
AND id = ANY(${ids}::text[])
|
|
19247
19678
|
AND lease_id = ${input.leaseId}
|
|
19248
19679
|
AND lease_expires_at IS NOT NULL
|
|
19249
19680
|
FOR UPDATE
|
|
@@ -19273,7 +19704,7 @@ class PgOntologyOutboxStorage {
|
|
|
19273
19704
|
WITH leased AS MATERIALIZED (
|
|
19274
19705
|
SELECT id FROM ontology_outbox
|
|
19275
19706
|
WHERE project_id = ${input.projectId}
|
|
19276
|
-
AND id = ANY(${
|
|
19707
|
+
AND id = ANY(${ids}::text[])
|
|
19277
19708
|
AND lease_id = ${input.leaseId}
|
|
19278
19709
|
AND lease_expires_at IS NOT NULL
|
|
19279
19710
|
FOR UPDATE
|
|
@@ -19814,8 +20245,8 @@ class PgOntologySourceStorage {
|
|
|
19814
20245
|
JOIN ontology_source_rows AS rows ON rows.root_id = roots.id
|
|
19815
20246
|
WHERE roots.version_id = ${manifest.version_id}
|
|
19816
20247
|
AND (
|
|
19817
|
-
roots.root_key = ANY(${
|
|
19818
|
-
OR roots.staging_ordinal = ANY(${
|
|
20248
|
+
roots.root_key = ANY(${rootKeys}::text[])
|
|
20249
|
+
OR roots.staging_ordinal = ANY(${ordinals}::bigint[])
|
|
19819
20250
|
)
|
|
19820
20251
|
`;
|
|
19821
20252
|
return existing.map((row) => sourceStageRow(sourceAssertion(row)));
|
|
@@ -19958,7 +20389,7 @@ class PgVectorIndexingStorage {
|
|
|
19958
20389
|
if (!input.ids.length)
|
|
19959
20390
|
return;
|
|
19960
20391
|
await this.run(async (sql) => {
|
|
19961
|
-
await sql`UPDATE object_vector_indexing SET dispatch_at = ${input.nextDispatchAt} WHERE project_id = ${input.projectId} AND id = ANY(${
|
|
20392
|
+
await sql`UPDATE object_vector_indexing SET dispatch_at = ${input.nextDispatchAt} WHERE project_id = ${input.projectId} AND id = ANY(${[...input.ids]}::text[])`;
|
|
19962
20393
|
});
|
|
19963
20394
|
}
|
|
19964
20395
|
async get(input) {
|
|
@@ -20318,7 +20749,7 @@ class PgActionRunStorage {
|
|
|
20318
20749
|
SELECT *
|
|
20319
20750
|
FROM action_runs
|
|
20320
20751
|
WHERE project_id = ${params.projectId}
|
|
20321
|
-
AND execution_id = ANY(${
|
|
20752
|
+
AND execution_id = ANY(${executionIds}::text[])
|
|
20322
20753
|
`;
|
|
20323
20754
|
const byExecution = new Map(rows.map((row) => [row.execution_id, row]));
|
|
20324
20755
|
return executionIds.flatMap((executionId) => {
|
|
@@ -20730,7 +21161,7 @@ class PgActivityStorage {
|
|
|
20730
21161
|
FROM activity_visits v
|
|
20731
21162
|
WHERE v.project_id = ${input.projectId}
|
|
20732
21163
|
AND v.actor_type = 'user'
|
|
20733
|
-
AND v.session_id = ANY(${
|
|
21164
|
+
AND v.session_id = ANY(${[...input.sessionIds]}::text[])
|
|
20734
21165
|
AND v.user_id = ${input.userId}
|
|
20735
21166
|
) ranked
|
|
20736
21167
|
WHERE position <= ${input.limit}
|
|
@@ -20792,7 +21223,7 @@ async function readStoredEvents(sql, input) {
|
|
|
20792
21223
|
FROM activity_page_views p
|
|
20793
21224
|
JOIN activity_visits v ON v.project_id = p.project_id AND v.id = p.visit_id
|
|
20794
21225
|
WHERE p.project_id = ${input.projectId}
|
|
20795
|
-
AND p.id = ANY(${
|
|
21226
|
+
AND p.id = ANY(${ids}::text[])
|
|
20796
21227
|
AND v.actor_type = ${input.actor.type}
|
|
20797
21228
|
AND v.session_id = ${input.sessionId}
|
|
20798
21229
|
UNION ALL
|
|
@@ -20800,7 +21231,7 @@ async function readStoredEvents(sql, input) {
|
|
|
20800
21231
|
FROM activity_interactions i
|
|
20801
21232
|
JOIN activity_visits v ON v.project_id = i.project_id AND v.id = i.visit_id
|
|
20802
21233
|
WHERE i.project_id = ${input.projectId}
|
|
20803
|
-
AND i.id = ANY(${
|
|
21234
|
+
AND i.id = ANY(${ids}::text[])
|
|
20804
21235
|
AND v.actor_type = ${input.actor.type}
|
|
20805
21236
|
AND v.session_id = ${input.sessionId}
|
|
20806
21237
|
`;
|
|
@@ -20834,6 +21265,7 @@ function pageViewFromRow(row) {
|
|
|
20834
21265
|
}
|
|
20835
21266
|
|
|
20836
21267
|
// src/pg-ai-cost-storage.ts
|
|
21268
|
+
import { wallClockInstant } from "@sixb/core/calendar";
|
|
20837
21269
|
import {
|
|
20838
21270
|
aiAccountingModelKind,
|
|
20839
21271
|
aiModelCallCostDetails,
|
|
@@ -21201,7 +21633,7 @@ class PgAiUsageStorage {
|
|
|
21201
21633
|
const executionIds = [...new Set(input.executionIds)];
|
|
21202
21634
|
return sql`
|
|
21203
21635
|
WITH requested(execution_id) AS (
|
|
21204
|
-
SELECT * FROM unnest(${
|
|
21636
|
+
SELECT * FROM unnest(${executionIds}::text[])
|
|
21205
21637
|
)
|
|
21206
21638
|
SELECT usage.* FROM requested
|
|
21207
21639
|
JOIN ai_model_call_usage AS usage
|
|
@@ -21361,7 +21793,7 @@ class PgAiCostStorage {
|
|
|
21361
21793
|
const ids = [...new Set(input.executionIds)];
|
|
21362
21794
|
const rows = await this.sql`
|
|
21363
21795
|
WITH requested(execution_id) AS (
|
|
21364
|
-
SELECT DISTINCT * FROM unnest(${
|
|
21796
|
+
SELECT DISTINCT * FROM unnest(${ids}::text[])
|
|
21365
21797
|
)
|
|
21366
21798
|
SELECT usage.execution_id,
|
|
21367
21799
|
cost.status,
|
|
@@ -21379,6 +21811,7 @@ class PgAiCostStorage {
|
|
|
21379
21811
|
const query = normalizeAiAccountingQuery(input);
|
|
21380
21812
|
const providerId = query.providerId ?? null;
|
|
21381
21813
|
const modelId = query.modelId ?? null;
|
|
21814
|
+
const timeZone = query.timeZone;
|
|
21382
21815
|
const filter = pgAccountingFilterValues(query);
|
|
21383
21816
|
const rows = await this.sql`
|
|
21384
21817
|
WITH filtered AS (
|
|
@@ -21465,29 +21898,31 @@ class PgAiCostStorage {
|
|
|
21465
21898
|
NULL::text AS key_1,
|
|
21466
21899
|
NULL::text AS key_2,
|
|
21467
21900
|
NULL::text AS key_3,
|
|
21468
|
-
NULL::
|
|
21901
|
+
NULL::text AS bucket_start,
|
|
21469
21902
|
classified.*
|
|
21470
21903
|
FROM classified
|
|
21471
21904
|
UNION ALL
|
|
21472
21905
|
SELECT 'series', NULL, NULL, NULL,
|
|
21473
|
-
|
|
21474
|
-
|
|
21475
|
-
date_trunc('hour', occurred_at AT TIME ZONE
|
|
21476
|
-
|
|
21477
|
-
date_trunc('
|
|
21478
|
-
|
|
21479
|
-
|
|
21906
|
+
to_char(
|
|
21907
|
+
CASE ${input.bucket}::text
|
|
21908
|
+
WHEN 'hour' THEN date_trunc('hour', occurred_at AT TIME ZONE ${timeZone})
|
|
21909
|
+
WHEN 'day' THEN date_trunc('day', occurred_at AT TIME ZONE ${timeZone})
|
|
21910
|
+
ELSE date_trunc('week', occurred_at AT TIME ZONE ${timeZone})
|
|
21911
|
+
END,
|
|
21912
|
+
'YYYY-MM-DD"T"HH24:MI:SS"Z"'
|
|
21913
|
+
),
|
|
21480
21914
|
classified.*
|
|
21481
21915
|
FROM classified
|
|
21482
21916
|
UNION ALL
|
|
21483
21917
|
SELECT 'modelSeries', provider_id, requested_model_id, NULL,
|
|
21484
|
-
|
|
21485
|
-
|
|
21486
|
-
date_trunc('hour', occurred_at AT TIME ZONE
|
|
21487
|
-
|
|
21488
|
-
date_trunc('
|
|
21489
|
-
|
|
21490
|
-
|
|
21918
|
+
to_char(
|
|
21919
|
+
CASE ${input.bucket}::text
|
|
21920
|
+
WHEN 'hour' THEN date_trunc('hour', occurred_at AT TIME ZONE ${timeZone})
|
|
21921
|
+
WHEN 'day' THEN date_trunc('day', occurred_at AT TIME ZONE ${timeZone})
|
|
21922
|
+
ELSE date_trunc('week', occurred_at AT TIME ZONE ${timeZone})
|
|
21923
|
+
END,
|
|
21924
|
+
'YYYY-MM-DD"T"HH24:MI:SS"Z"'
|
|
21925
|
+
),
|
|
21491
21926
|
classified.*
|
|
21492
21927
|
FROM classified
|
|
21493
21928
|
UNION ALL
|
|
@@ -21553,7 +21988,7 @@ class PgAiCostStorage {
|
|
|
21553
21988
|
FROM expanded
|
|
21554
21989
|
GROUP BY dimension, key_1, key_2, key_3, bucket_start, amount_currency
|
|
21555
21990
|
`;
|
|
21556
|
-
return buildAiAccountingOverviewFromFragments(input, rows.map(aggregateFragmentFromRow));
|
|
21991
|
+
return buildAiAccountingOverviewFromFragments(input, rows.map((row) => aggregateFragmentFromRow(row, timeZone)));
|
|
21557
21992
|
}
|
|
21558
21993
|
async listModelCalls(input) {
|
|
21559
21994
|
const query = normalizeAiModelCallAccountingQuery2(input);
|
|
@@ -21711,15 +22146,15 @@ async function findUsage(sql, projectId, id) {
|
|
|
21711
22146
|
`;
|
|
21712
22147
|
return row ? usageFromRow2(row, []) : null;
|
|
21713
22148
|
}
|
|
21714
|
-
function aggregateFragmentFromRow(row) {
|
|
22149
|
+
function aggregateFragmentFromRow(row, timeZone) {
|
|
21715
22150
|
const dimensionKeys = row.dimension === "model" ? { providerId: required2(row.key_1), modelId: required2(row.key_2) } : row.dimension === "modelSeries" ? {
|
|
21716
22151
|
providerId: required2(row.key_1),
|
|
21717
22152
|
modelId: required2(row.key_2),
|
|
21718
|
-
start:
|
|
22153
|
+
start: wallClockInstant(required2(row.bucket_start), timeZone)
|
|
21719
22154
|
} : row.dimension === "person" ? row.key_1 && row.key_2 ? { requesterType: row.key_1, requesterId: row.key_2 } : {} : row.dimension === "kind" ? withModelKind(aiAccountingModelKind(row.key_1)) : row.dimension === "agent" ? {
|
|
21720
22155
|
agentKind: required2(row.key_1),
|
|
21721
22156
|
...row.key_1 === "workflowAgent" ? { workflowId: required2(row.key_2), agentStepId: required2(row.key_3) } : {}
|
|
21722
|
-
} : row.dimension === "workflow" ? { workflowId: required2(row.key_1) } : row.dimension === "series" ? { start:
|
|
22157
|
+
} : row.dimension === "workflow" ? { workflowId: required2(row.key_1) } : row.dimension === "series" ? { start: wallClockInstant(required2(row.bucket_start), timeZone) } : {};
|
|
21723
22158
|
return {
|
|
21724
22159
|
dimension: row.dimension,
|
|
21725
22160
|
...dimensionKeys,
|
|
@@ -22796,7 +23231,8 @@ class PgArtifactStorage {
|
|
|
22796
23231
|
}
|
|
22797
23232
|
async undoTurnWrites(input) {
|
|
22798
23233
|
return runPgTransaction(this.sql, async (tx) => {
|
|
22799
|
-
const { projectId, turnRunId } = input;
|
|
23234
|
+
const { projectId, turnRunId, laterTurnRunIds = [] } = input;
|
|
23235
|
+
const later = [...laterTurnRunIds];
|
|
22800
23236
|
await tx`
|
|
22801
23237
|
SELECT id FROM artifacts
|
|
22802
23238
|
WHERE project_id = ${projectId} AND id IN (
|
|
@@ -22808,7 +23244,10 @@ class PgArtifactStorage {
|
|
|
22808
23244
|
`;
|
|
22809
23245
|
const rows = await tx`
|
|
22810
23246
|
SELECT w.artifact_id AS "artifactId", w.created, w.revision, w.writes,
|
|
22811
|
-
a.revision AS "currentRevision"
|
|
23247
|
+
a.revision AS "currentRevision",
|
|
23248
|
+
(SELECT COALESCE(SUM(l.writes), 0) FROM artifact_turn_writes l
|
|
23249
|
+
WHERE l.project_id = w.project_id AND l.artifact_id = w.artifact_id
|
|
23250
|
+
AND l.turn_run_id = ANY(${later}::text[])) AS "laterWrites"
|
|
22812
23251
|
FROM artifact_turn_writes w
|
|
22813
23252
|
JOIN artifacts a ON a.project_id = w.project_id AND a.id = w.artifact_id
|
|
22814
23253
|
WHERE w.project_id = ${projectId} AND w.turn_run_id = ${turnRunId}
|
|
@@ -22817,7 +23256,8 @@ class PgArtifactStorage {
|
|
|
22817
23256
|
const restored = [];
|
|
22818
23257
|
const deleted = [];
|
|
22819
23258
|
for (const row of rows) {
|
|
22820
|
-
const
|
|
23259
|
+
const own = parseArtifactTurnWriteRow(row);
|
|
23260
|
+
const write = { ...own, writes: own.writes + Number(row.laterWrites) };
|
|
22821
23261
|
const revision = Number(row.currentRevision);
|
|
22822
23262
|
if (write.created) {
|
|
22823
23263
|
if (!turnUndoable({ write, revision }))
|
|
@@ -22838,14 +23278,20 @@ class PgArtifactStorage {
|
|
|
22838
23278
|
}
|
|
22839
23279
|
if (restored.length > 0) {
|
|
22840
23280
|
const undone = restored.map(({ artifact }) => artifact.id);
|
|
23281
|
+
const turns = [turnRunId, ...laterTurnRunIds];
|
|
22841
23282
|
await tx`
|
|
22842
23283
|
DELETE FROM artifact_restore_points
|
|
22843
|
-
WHERE project_id = ${projectId} AND turn_run_id = ${
|
|
23284
|
+
WHERE project_id = ${projectId} AND turn_run_id = ANY(${turns}::text[])
|
|
22844
23285
|
AND artifact_id IN ${tx(undone)}
|
|
22845
23286
|
`;
|
|
22846
23287
|
await tx`
|
|
22847
23288
|
DELETE FROM artifact_threads
|
|
22848
|
-
WHERE project_id = ${projectId} AND linked_by_run_id = ${
|
|
23289
|
+
WHERE project_id = ${projectId} AND linked_by_run_id = ANY(${turns}::text[])
|
|
23290
|
+
AND NOT copied AND artifact_id IN ${tx(undone)}
|
|
23291
|
+
`;
|
|
23292
|
+
await tx`
|
|
23293
|
+
DELETE FROM artifact_turn_writes
|
|
23294
|
+
WHERE project_id = ${projectId} AND turn_run_id = ANY(${later}::text[])
|
|
22849
23295
|
AND artifact_id IN ${tx(undone)}
|
|
22850
23296
|
`;
|
|
22851
23297
|
}
|
|
@@ -23124,7 +23570,7 @@ class PgArtifactStorage {
|
|
|
23124
23570
|
const rows = await this.sql`
|
|
23125
23571
|
SELECT ${this.sql.unsafe(BOARD_PREVIEW_COLUMNS)} FROM artifact_board_previews
|
|
23126
23572
|
WHERE project_id = ${input.projectId}
|
|
23127
|
-
AND render_key = ANY(${
|
|
23573
|
+
AND render_key = ANY(${[...new Set(input.renderKeys)]}::text[])
|
|
23128
23574
|
ORDER BY render_key COLLATE "C", theme COLLATE "C", size COLLATE "C"
|
|
23129
23575
|
`;
|
|
23130
23576
|
return rows.map(parseArtifactBoardPreviewRow);
|
|
@@ -23728,7 +24174,7 @@ class PgDatasetIngestPersistence {
|
|
|
23728
24174
|
UPDATE dataset_ingest_deliveries
|
|
23729
24175
|
SET flush_id = ${flushId}
|
|
23730
24176
|
WHERE project_id = ${projectId} AND dataset_id = ${datasetId}
|
|
23731
|
-
AND id = ANY(${
|
|
24177
|
+
AND id = ANY(${[...deliveryIds]}::bigint[])
|
|
23732
24178
|
`;
|
|
23733
24179
|
}
|
|
23734
24180
|
async listFlush(projectId, datasetId, flushId) {
|
|
@@ -23796,7 +24242,7 @@ class PgDatasetIngestPersistence {
|
|
|
23796
24242
|
flush_id, flush_started_at, failure_streak, last_flushed_at
|
|
23797
24243
|
FROM dataset_ingest_state
|
|
23798
24244
|
WHERE project_id = ${projectId}
|
|
23799
|
-
AND (${datasetIds === undefined} OR dataset_id = ANY(${
|
|
24245
|
+
AND (${datasetIds === undefined} OR dataset_id = ANY(${[...datasetIds ?? []]}::text[]))
|
|
23800
24246
|
`;
|
|
23801
24247
|
return rows.map(stateFromRow);
|
|
23802
24248
|
}
|
|
@@ -23809,7 +24255,7 @@ class PgDatasetIngestPersistence {
|
|
|
23809
24255
|
count(*) FILTER (WHERE rejected_at IS NOT NULL)::text AS rejected
|
|
23810
24256
|
FROM dataset_ingest_deliveries
|
|
23811
24257
|
WHERE project_id = ${projectId}
|
|
23812
|
-
AND (${datasetIds === undefined} OR dataset_id = ANY(${
|
|
24258
|
+
AND (${datasetIds === undefined} OR dataset_id = ANY(${[...datasetIds ?? []]}::text[]))
|
|
23813
24259
|
GROUP BY dataset_id
|
|
23814
24260
|
`;
|
|
23815
24261
|
return rows.map((row) => ({
|
|
@@ -24132,11 +24578,16 @@ async function queryLatestRunsByOwnerId(input) {
|
|
|
24132
24578
|
parameters.push(input.excludedStatus);
|
|
24133
24579
|
statusFilter = ` AND status <> $${parameters.length}`;
|
|
24134
24580
|
}
|
|
24581
|
+
const bind = (value) => {
|
|
24582
|
+
parameters.push(value);
|
|
24583
|
+
return `$${parameters.length}`;
|
|
24584
|
+
};
|
|
24585
|
+
const filter = input.filter === undefined ? "" : ` AND ${input.filter(bind)}`;
|
|
24135
24586
|
const orderColumn = input.tableName === "sync_runs" || input.tableName === "pipeline_runs" || input.tableName === "projection_runs" ? "COALESCE(started_at, queued_at)" : "started_at";
|
|
24136
24587
|
const rows = await input.sql.unsafe(`
|
|
24137
24588
|
SELECT DISTINCT ON (${input.ownerColumn}) ${input.selectList ?? "*"}
|
|
24138
24589
|
FROM ${input.tableName}
|
|
24139
|
-
WHERE project_id = $1 AND ${input.ownerColumn} IN (${placeholders})${statusFilter}
|
|
24590
|
+
WHERE project_id = $1 AND ${input.ownerColumn} IN (${placeholders})${statusFilter}${filter}
|
|
24140
24591
|
ORDER BY ${input.ownerColumn}, ${orderColumn} DESC, id DESC
|
|
24141
24592
|
`, parameters);
|
|
24142
24593
|
const latestByOwnerId = new Map(rows.map((row) => [input.ownerIdFor(row), row]));
|
|
@@ -25048,7 +25499,7 @@ class PgProjectionRunStorage {
|
|
|
25048
25499
|
WHERE project_id = ${input.projectId}
|
|
25049
25500
|
AND projection_id = ${run.projection_id}
|
|
25050
25501
|
AND materialization_protocol = ${"replacement"}
|
|
25051
|
-
AND status = ANY(${
|
|
25502
|
+
AND status = ANY(${[...SUPERSEDING_PROJECTION_RUN_STATUSES]}::text[])
|
|
25052
25503
|
AND dataset_version_created_at COLLATE "C" > ${run.dataset_version_created_at} COLLATE "C"
|
|
25053
25504
|
ORDER BY dataset_version_created_at COLLATE "C" DESC, id DESC
|
|
25054
25505
|
LIMIT 1
|
|
@@ -25455,6 +25906,7 @@ class PgSyncRunStorage {
|
|
|
25455
25906
|
id,
|
|
25456
25907
|
execution_id,
|
|
25457
25908
|
sync_id,
|
|
25909
|
+
connection_id,
|
|
25458
25910
|
dataset_id,
|
|
25459
25911
|
mode,
|
|
25460
25912
|
status,
|
|
@@ -25467,6 +25919,7 @@ class PgSyncRunStorage {
|
|
|
25467
25919
|
${input.id},
|
|
25468
25920
|
${input.executionId},
|
|
25469
25921
|
${input.syncId},
|
|
25922
|
+
${input.connectionId ?? null},
|
|
25470
25923
|
${input.datasetId},
|
|
25471
25924
|
${input.mode},
|
|
25472
25925
|
${"queued"},
|
|
@@ -25617,6 +26070,18 @@ class PgSyncRunStorage {
|
|
|
25617
26070
|
whereClauses.push(`sync_id IN (${placeholders.join(", ")})`);
|
|
25618
26071
|
params.push(...input.syncIds);
|
|
25619
26072
|
}
|
|
26073
|
+
if (input.connectionId === null) {
|
|
26074
|
+
whereClauses.push("connection_id IS NULL");
|
|
26075
|
+
} else if (input.connectionId !== undefined) {
|
|
26076
|
+
whereClauses.push(`connection_id = $${index++}`);
|
|
26077
|
+
params.push(input.connectionId);
|
|
26078
|
+
}
|
|
26079
|
+
if (input.visibleConnectionIds) {
|
|
26080
|
+
whereClauses.push(visibleConnectionsClause(input.visibleConnectionIds, (value) => {
|
|
26081
|
+
params.push(value);
|
|
26082
|
+
return `$${index++}`;
|
|
26083
|
+
}));
|
|
26084
|
+
}
|
|
25620
26085
|
if (input.datasetId) {
|
|
25621
26086
|
whereClauses.push(`dataset_id = $${index++}`);
|
|
25622
26087
|
params.push(input.datasetId);
|
|
@@ -25640,6 +26105,7 @@ class PgSyncRunStorage {
|
|
|
25640
26105
|
};
|
|
25641
26106
|
}
|
|
25642
26107
|
async listLatestBySyncIds(input) {
|
|
26108
|
+
const { visibleConnectionIds } = input;
|
|
25643
26109
|
const rows = await queryLatestRunsByOwnerId({
|
|
25644
26110
|
sql: this.sql,
|
|
25645
26111
|
tableName: "sync_runs",
|
|
@@ -25647,12 +26113,38 @@ class PgSyncRunStorage {
|
|
|
25647
26113
|
ownerIds: input.syncIds,
|
|
25648
26114
|
projectId: input.projectId,
|
|
25649
26115
|
ownerIdFor: (row) => row.sync_id,
|
|
25650
|
-
selectList: "*, checkpoint IS NOT NULL AS checkpoint_present"
|
|
26116
|
+
selectList: "*, checkpoint IS NOT NULL AS checkpoint_present",
|
|
26117
|
+
...visibleConnectionIds === undefined ? {} : { filter: (bind) => visibleConnectionsClause(visibleConnectionIds, bind) }
|
|
25651
26118
|
});
|
|
25652
26119
|
return {
|
|
25653
26120
|
runs: rows.map(rowToSyncRunRecord)
|
|
25654
26121
|
};
|
|
25655
26122
|
}
|
|
26123
|
+
async listLatestByConnection(input) {
|
|
26124
|
+
if (input.syncIds.length === 0 || input.connectionIds?.length === 0)
|
|
26125
|
+
return { runs: [] };
|
|
26126
|
+
const params = [input.projectId];
|
|
26127
|
+
const placeholders = (values) => values.map((value) => {
|
|
26128
|
+
params.push(value);
|
|
26129
|
+
return `$${params.length}`;
|
|
26130
|
+
}).join(", ");
|
|
26131
|
+
const syncFilter = `sync_id IN (${placeholders(input.syncIds)})`;
|
|
26132
|
+
const connectionFilter = input.connectionIds === undefined ? "connection_id IS NOT NULL" : `connection_id IN (${placeholders(input.connectionIds)})`;
|
|
26133
|
+
const rows = await this.sql.unsafe(`
|
|
26134
|
+
SELECT DISTINCT ON (sync_id, connection_id)
|
|
26135
|
+
*, checkpoint IS NOT NULL AS checkpoint_present
|
|
26136
|
+
FROM sync_runs
|
|
26137
|
+
WHERE project_id = $1 AND ${syncFilter} AND ${connectionFilter}
|
|
26138
|
+
ORDER BY sync_id, connection_id, COALESCE(started_at, queued_at) DESC, id DESC
|
|
26139
|
+
`, params);
|
|
26140
|
+
return { runs: rows.map(rowToSyncRunRecord) };
|
|
26141
|
+
}
|
|
26142
|
+
}
|
|
26143
|
+
function visibleConnectionsClause(connectionIds, bind) {
|
|
26144
|
+
if (connectionIds.length === 0)
|
|
26145
|
+
return "connection_id IS NULL";
|
|
26146
|
+
const placeholders = connectionIds.map((id) => bind(id)).join(", ");
|
|
26147
|
+
return `(connection_id IS NULL OR connection_id IN (${placeholders}))`;
|
|
25656
26148
|
}
|
|
25657
26149
|
function staleExecution2(id) {
|
|
25658
26150
|
return new SyncRunError(`[SixbPg] Execution token is no longer current on sync run '${id}'.`);
|
|
@@ -25669,6 +26161,7 @@ function rowToSyncRunRecord(row) {
|
|
|
25669
26161
|
projectId: row.project_id,
|
|
25670
26162
|
executionId: row.execution_id,
|
|
25671
26163
|
syncId: row.sync_id,
|
|
26164
|
+
...row.connection_id === null ? {} : { connectionId: row.connection_id },
|
|
25672
26165
|
datasetId: row.dataset_id,
|
|
25673
26166
|
mode: row.mode,
|
|
25674
26167
|
status: row.status,
|
|
@@ -26424,7 +26917,7 @@ class PgWorkflowRunStorage {
|
|
|
26424
26917
|
SELECT *
|
|
26425
26918
|
FROM workflow_runs
|
|
26426
26919
|
WHERE project_id = ${params.projectId}
|
|
26427
|
-
AND execution_id = ANY(${
|
|
26920
|
+
AND execution_id = ANY(${executionIds}::text[])
|
|
26428
26921
|
`;
|
|
26429
26922
|
const byExecution = new Map(rows.map((row) => [row.execution_id, row]));
|
|
26430
26923
|
return executionIds.flatMap((executionId) => {
|
|
@@ -27026,7 +27519,7 @@ class PgShareGrantStorage {
|
|
|
27026
27519
|
SELECT *
|
|
27027
27520
|
FROM share_grants
|
|
27028
27521
|
WHERE project_id = ${input.projectId}
|
|
27029
|
-
AND id = ANY(${
|
|
27522
|
+
AND id = ANY(${ids}::text[])
|
|
27030
27523
|
`;
|
|
27031
27524
|
const byId = new Map(rows.map((row) => [row.id, row]));
|
|
27032
27525
|
return ids.flatMap((id) => {
|
|
@@ -27496,4 +27989,4 @@ export {
|
|
|
27496
27989
|
migratePostgresStorage
|
|
27497
27990
|
};
|
|
27498
27991
|
|
|
27499
|
-
//# debugId=
|
|
27992
|
+
//# debugId=DC1D078F3E8B5D6064756E2164756E21
|