@sixb/pg 0.1.6 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/dist/agents/rows.d.ts +8 -5
  2. package/dist/agents/rows.d.ts.map +1 -1
  3. package/dist/agents/runs.d.ts +3 -2
  4. package/dist/agents/runs.d.ts.map +1 -1
  5. package/dist/agents/threads.d.ts.map +1 -1
  6. package/dist/ai-cost-groups.d.ts +5 -0
  7. package/dist/ai-cost-groups.d.ts.map +1 -0
  8. package/dist/auth-storage/auth-storage.d.ts +4 -1
  9. package/dist/auth-storage/auth-storage.d.ts.map +1 -1
  10. package/dist/auth-storage/device-authorizations.d.ts +29 -0
  11. package/dist/auth-storage/device-authorizations.d.ts.map +1 -0
  12. package/dist/auth-storage/rows.d.ts +19 -1
  13. package/dist/auth-storage/rows.d.ts.map +1 -1
  14. package/dist/auth-storage/shared.d.ts +1 -1
  15. package/dist/auth-storage/shared.d.ts.map +1 -1
  16. package/dist/index.d.ts +6 -1
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +8327 -5838
  19. package/dist/index.js.map +28 -19
  20. package/dist/migrations.d.ts.map +1 -1
  21. package/dist/objects/index.d.ts +2 -0
  22. package/dist/objects/index.d.ts.map +1 -0
  23. package/dist/{pg-object-query-compiler.d.ts → objects/query-compiler.d.ts} +25 -4
  24. package/dist/objects/query-compiler.d.ts.map +1 -0
  25. package/dist/objects/read-scope.d.ts +17 -0
  26. package/dist/objects/read-scope.d.ts.map +1 -0
  27. package/dist/objects/reader.d.ts +50 -0
  28. package/dist/objects/reader.d.ts.map +1 -0
  29. package/dist/objects/rows.d.ts +47 -0
  30. package/dist/objects/rows.d.ts.map +1 -0
  31. package/dist/objects/selected-reader.d.ts +8 -0
  32. package/dist/objects/selected-reader.d.ts.map +1 -0
  33. package/dist/{pg-object-storage.d.ts → objects/storage.d.ts} +12 -20
  34. package/dist/objects/storage.d.ts.map +1 -0
  35. package/dist/pg-ai-cost-storage.d.ts +2 -1
  36. package/dist/pg-ai-cost-storage.d.ts.map +1 -1
  37. package/dist/pg-ai-limit-storage.d.ts +34 -0
  38. package/dist/pg-ai-limit-storage.d.ts.map +1 -0
  39. package/dist/pg-ai-usage-storage.d.ts +28 -0
  40. package/dist/pg-ai-usage-storage.d.ts.map +1 -1
  41. package/dist/pg-execution-storage.d.ts +3 -2
  42. package/dist/pg-execution-storage.d.ts.map +1 -1
  43. package/dist/share-grant-storage.d.ts +11 -0
  44. package/dist/share-grant-storage.d.ts.map +1 -0
  45. package/dist/share-session-storage.d.ts +11 -0
  46. package/dist/share-session-storage.d.ts.map +1 -0
  47. package/dist/storage-errors.d.ts +2 -0
  48. package/dist/storage-errors.d.ts.map +1 -1
  49. package/dist/transactions.d.ts +9 -2
  50. package/dist/transactions.d.ts.map +1 -1
  51. package/package.json +4 -4
  52. package/src/agents/rows.ts +38 -9
  53. package/src/agents/runs.ts +148 -11
  54. package/src/agents/threads.ts +1 -17
  55. package/src/ai-cost-groups.ts +102 -0
  56. package/src/auth-storage/auth-storage.ts +84 -0
  57. package/src/auth-storage/device-authorizations.ts +131 -0
  58. package/src/auth-storage/rows.ts +41 -0
  59. package/src/auth-storage/shared.ts +1 -0
  60. package/src/index.ts +24 -4
  61. package/src/migrations/029-model-accounting.sql +24 -0
  62. package/src/migrations/030-ai-usage-limits.sql +94 -0
  63. package/src/migrations/031-subagent-runs.sql +97 -0
  64. package/src/migrations/032-conversation-run-spec.sql +23 -0
  65. package/src/migrations/033-retire-agent-definitions.sql +28 -0
  66. package/src/migrations/034-device-authorizations.sql +24 -0
  67. package/src/migrations/035-share-grants.sql +57 -0
  68. package/src/migrations/036-share-sessions.sql +173 -0
  69. package/src/migrations.ts +22 -0
  70. package/src/objects/index.ts +1 -0
  71. package/src/{pg-object-query-compiler.ts → objects/query-compiler.ts} +185 -100
  72. package/src/objects/read-scope.ts +324 -0
  73. package/src/objects/reader.ts +604 -0
  74. package/src/objects/rows.ts +146 -0
  75. package/src/objects/selected-reader.ts +118 -0
  76. package/src/objects/storage.ts +305 -0
  77. package/src/pg-ai-cost-storage.ts +71 -24
  78. package/src/pg-ai-limit-storage.ts +901 -0
  79. package/src/pg-ai-usage-storage.ts +34 -0
  80. package/src/pg-execution-storage.ts +21 -2
  81. package/src/pg-workflow-run-storage.ts +8 -8
  82. package/src/share-grant-storage.ts +236 -0
  83. package/src/share-session-storage.ts +127 -0
  84. package/src/storage-errors.ts +6 -0
  85. package/src/transactions.ts +46 -5
  86. package/dist/pg-object-query-compiler.d.ts.map +0 -1
  87. package/dist/pg-object-storage.d.ts.map +0 -1
  88. package/src/pg-object-storage.ts +0 -734
@@ -0,0 +1,173 @@
1
+ -- Shared sessions and their execution provenance.
2
+ CREATE TABLE share_sessions (
3
+ project_id TEXT NOT NULL CHECK (length(trim(project_id)) > 0),
4
+ id TEXT NOT NULL CHECK (length(trim(id)) > 0),
5
+ grant_id TEXT NOT NULL CHECK (length(trim(grant_id)) > 0),
6
+ token_hash TEXT NOT NULL CHECK (token_hash ~ '^[0-9a-f]{64}$'),
7
+ created_at TIMESTAMPTZ NOT NULL,
8
+ expires_at TIMESTAMPTZ NOT NULL,
9
+ absolute_expires_at TIMESTAMPTZ NOT NULL,
10
+ revoked_at TIMESTAMPTZ,
11
+ PRIMARY KEY (project_id, id),
12
+ UNIQUE (project_id, id, grant_id),
13
+ UNIQUE (project_id, token_hash),
14
+ FOREIGN KEY (project_id, grant_id)
15
+ REFERENCES share_grants (project_id, id),
16
+ CHECK (expires_at > created_at),
17
+ CHECK (absolute_expires_at > created_at),
18
+ CHECK (expires_at <= absolute_expires_at),
19
+ CHECK (revoked_at IS NULL OR revoked_at >= created_at)
20
+ );
21
+
22
+ CREATE INDEX idx_share_sessions_grant
23
+ ON share_sessions (project_id, grant_id, created_at DESC, id DESC);
24
+
25
+ CREATE INDEX idx_share_sessions_expiry
26
+ ON share_sessions (project_id, expires_at)
27
+ WHERE revoked_at IS NULL;
28
+
29
+ ALTER TABLE executions
30
+ ADD COLUMN authority_delegation_kind TEXT,
31
+ ADD COLUMN authority_delegation_id TEXT,
32
+ ADD COLUMN authority_delegation_session_id TEXT;
33
+
34
+ -- The original authority checks were unnamed. Drop exactly the checks that mention
35
+ -- authority_kind, then replace them with stable names and the delegated variant below.
36
+ DO $$
37
+ DECLARE authority_constraint RECORD;
38
+ BEGIN
39
+ FOR authority_constraint IN
40
+ SELECT conname
41
+ FROM pg_constraint
42
+ WHERE conrelid = 'executions'::regclass
43
+ AND contype = 'c'
44
+ AND pg_get_constraintdef(oid) LIKE '%authority_kind%'
45
+ LOOP
46
+ EXECUTE format('ALTER TABLE executions DROP CONSTRAINT %I', authority_constraint.conname);
47
+ END LOOP;
48
+ END $$;
49
+
50
+ ALTER TABLE executions
51
+ ADD CONSTRAINT executions_authority_kind_check CHECK (
52
+ authority_kind IN ('principal', 'trustedPrimitive', 'delegated', 'kernel', 'disabled')
53
+ ),
54
+ ADD CONSTRAINT executions_authority_delegation_kind_check CHECK (
55
+ authority_delegation_kind IS NULL OR authority_delegation_kind = 'share'
56
+ ),
57
+ ADD CONSTRAINT executions_authority_shape_check CHECK (
58
+ (
59
+ authority_kind = 'principal'
60
+ AND num_nonnulls(authority_user_id, authority_service_account_id) = 1
61
+ AND num_nonnulls(authority_session_id, authority_access_token_id) <= 1
62
+ AND (authority_session_id IS NULL OR authority_user_id IS NOT NULL)
63
+ AND authority_primitive_kind IS NULL
64
+ AND authority_primitive_id IS NULL
65
+ AND authority_kernel_operation IS NULL
66
+ AND authority_delegation_kind IS NULL
67
+ AND authority_delegation_id IS NULL
68
+ AND authority_delegation_session_id IS NULL
69
+ )
70
+ OR (
71
+ authority_kind = 'trustedPrimitive'
72
+ AND authority_user_id IS NULL
73
+ AND authority_service_account_id IS NULL
74
+ AND authority_session_id IS NULL
75
+ AND authority_access_token_id IS NULL
76
+ AND authority_primitive_kind IS NOT NULL
77
+ AND authority_primitive_id IS NOT NULL
78
+ AND length(trim(authority_primitive_id)) > 0
79
+ AND authority_kernel_operation IS NULL
80
+ AND authority_delegation_kind IS NULL
81
+ AND authority_delegation_id IS NULL
82
+ AND authority_delegation_session_id IS NULL
83
+ )
84
+ OR (
85
+ authority_kind = 'delegated'
86
+ AND authority_user_id IS NULL
87
+ AND authority_service_account_id IS NULL
88
+ AND authority_session_id IS NULL
89
+ AND authority_access_token_id IS NULL
90
+ AND authority_primitive_kind IS NULL
91
+ AND authority_primitive_id IS NULL
92
+ AND authority_kernel_operation IS NULL
93
+ AND authority_delegation_kind IS NOT NULL
94
+ AND authority_delegation_kind = 'share'
95
+ AND authority_delegation_id IS NOT NULL
96
+ AND length(trim(authority_delegation_id)) > 0
97
+ AND authority_delegation_session_id IS NOT NULL
98
+ AND length(trim(authority_delegation_session_id)) > 0
99
+ )
100
+ OR (
101
+ authority_kind = 'kernel'
102
+ AND authority_user_id IS NULL
103
+ AND authority_service_account_id IS NULL
104
+ AND authority_session_id IS NULL
105
+ AND authority_access_token_id IS NULL
106
+ AND authority_primitive_kind IS NULL
107
+ AND authority_primitive_id IS NULL
108
+ AND authority_kernel_operation IS NOT NULL
109
+ AND authority_delegation_kind IS NULL
110
+ AND authority_delegation_id IS NULL
111
+ AND authority_delegation_session_id IS NULL
112
+ )
113
+ OR (
114
+ authority_kind = 'disabled'
115
+ AND authority_user_id IS NULL
116
+ AND authority_service_account_id IS NULL
117
+ AND authority_session_id IS NULL
118
+ AND authority_access_token_id IS NULL
119
+ AND authority_primitive_kind IS NULL
120
+ AND authority_primitive_id IS NULL
121
+ AND authority_kernel_operation IS NULL
122
+ AND authority_delegation_kind IS NULL
123
+ AND authority_delegation_id IS NULL
124
+ AND authority_delegation_session_id IS NULL
125
+ )
126
+ ),
127
+ ADD CONSTRAINT executions_executor_authority_check CHECK (
128
+ (
129
+ executor_kind = 'request'
130
+ AND source_kind = 'http'
131
+ AND executor_id = source_id
132
+ AND (
133
+ (
134
+ authority_kind = 'principal'
135
+ AND requested_by_user_id IS NOT DISTINCT FROM authority_user_id
136
+ AND requested_by_service_account_id IS NOT DISTINCT FROM authority_service_account_id
137
+ )
138
+ OR (
139
+ authority_kind IN ('delegated', 'disabled')
140
+ AND requested_by_user_id IS NULL
141
+ AND requested_by_service_account_id IS NULL
142
+ )
143
+ )
144
+ )
145
+ OR (
146
+ executor_kind IN ('action', 'pipeline', 'projection', 'rule', 'sync', 'webhook', 'workflow')
147
+ AND authority_kind = 'trustedPrimitive'
148
+ AND executor_kind = authority_primitive_kind
149
+ )
150
+ OR (
151
+ executor_kind = 'agent'
152
+ AND source_kind = 'execution'
153
+ AND (
154
+ (
155
+ authority_kind = 'principal'
156
+ AND authority_service_account_id IS NOT NULL
157
+ AND authority_session_id IS NULL
158
+ AND authority_access_token_id IS NULL
159
+ )
160
+ OR (
161
+ authority_kind = 'principal'
162
+ AND authority_user_id IS NOT NULL
163
+ AND num_nonnulls(authority_session_id, authority_access_token_id) = 1
164
+ )
165
+ OR authority_kind = 'disabled'
166
+ )
167
+ )
168
+ OR (executor_kind = 'kernel' AND authority_kind = 'kernel')
169
+ ),
170
+ ADD CONSTRAINT fk_executions_share_session
171
+ FOREIGN KEY (project_id, authority_delegation_session_id, authority_delegation_id)
172
+ REFERENCES share_sessions (project_id, id, grant_id)
173
+ ON DELETE RESTRICT;
package/src/migrations.ts CHANGED
@@ -73,6 +73,20 @@ import agentContextCheckpointsSql from "./migrations/027-agent-context-checkpoin
73
73
  import objectOverrideEditTimesSql from "./migrations/028-object-override-edit-times.sql" with {
74
74
  type: "text",
75
75
  }
76
+ import modelAccountingSql from "./migrations/029-model-accounting.sql" with { type: "text" }
77
+ import aiUsageLimitsSql from "./migrations/030-ai-usage-limits.sql" with { type: "text" }
78
+ import subagentRunsSql from "./migrations/031-subagent-runs.sql" with { type: "text" }
79
+ import conversationRunSpecSql from "./migrations/032-conversation-run-spec.sql" with {
80
+ type: "text",
81
+ }
82
+ import retireAgentDefinitionsSql from "./migrations/033-retire-agent-definitions.sql" with {
83
+ type: "text",
84
+ }
85
+ import deviceAuthorizationsSql from "./migrations/034-device-authorizations.sql" with {
86
+ type: "text",
87
+ }
88
+ import shareGrantsSql from "./migrations/035-share-grants.sql" with { type: "text" }
89
+ import shareSessionsSql from "./migrations/036-share-sessions.sql" with { type: "text" }
76
90
  import type { SQL, SQLClient } from "./pg-client"
77
91
 
78
92
  export interface PostgresMigrationContext {
@@ -354,6 +368,14 @@ export const postgresStorageMigrations = defineMigrations<PostgresMigrationConte
354
368
  pgSql("026-ai-cost-accounting", aiCostAccountingSql),
355
369
  pgSql("027-agent-context-checkpoints", agentContextCheckpointsSql),
356
370
  pgSql("028-object-override-edit-times", objectOverrideEditTimesSql),
371
+ pgSql("029-model-accounting", modelAccountingSql),
372
+ pgSql("030-ai-usage-limits", aiUsageLimitsSql),
373
+ pgSql("031-subagent-runs", subagentRunsSql),
374
+ pgSql("032-conversation-run-spec", conversationRunSpecSql),
375
+ pgSql("033-retire-agent-definitions", retireAgentDefinitionsSql),
376
+ pgSql("034-device-authorizations", deviceAuthorizationsSql),
377
+ pgSql("035-share-grants", shareGrantsSql),
378
+ pgSql("036-share-sessions", shareSessionsSql),
357
379
  ],
358
380
  })
359
381
 
@@ -0,0 +1 @@
1
+ export { PgObjectStorage } from "./storage"