@systemfsoftware/agent-storage-postgres 0.0.0-bootstrap.0 → 0.1.0-beta.192
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/LICENSE +21 -0
- package/dist/PostgresStorage-D9IqvmQ2.mjs +1860 -0
- package/dist/PostgresStorage-D9IqvmQ2.mjs.map +1 -0
- package/dist/PostgresStorage-zKTn_ufd.d.mts +63 -0
- package/dist/PostgresStorage.d.mts +2 -0
- package/dist/PostgresStorage.mjs +2 -0
- package/dist/PostgresStorageError-DCuaf7EF.mjs +50 -0
- package/dist/PostgresStorageError-DCuaf7EF.mjs.map +1 -0
- package/dist/PostgresStorageError-mnbXuBHP.d.mts +48 -0
- package/dist/PostgresStorageError.d.mts +2 -0
- package/dist/PostgresStorageError.mjs +1 -0
- package/dist/index.d.mts +3 -0
- package/dist/index.mjs +3 -0
- package/dist/rolldown-runtime-D3KUYPsf.mjs +12 -0
- package/package.json +55 -5
- package/src/PostgresStorage.ts +339 -0
- package/src/PostgresStorageError.ts +69 -0
- package/src/index.ts +2 -0
- package/src/internal/layout-expression.ts +209 -0
- package/src/internal/postgres-storage.ts +144 -0
- package/src/internal/storage-layout.ts +1591 -0
- package/README.md +0 -3
|
@@ -0,0 +1,1860 @@
|
|
|
1
|
+
import { t as __exportAll } from "./rolldown-runtime-D3KUYPsf.mjs";
|
|
2
|
+
import { makeSqlActivityStore } from "@systemfsoftware/agent-storage-sql/sql-activity-store";
|
|
3
|
+
import { makeSqlJournal, sqlStorageOwner } from "@systemfsoftware/agent-storage-sql/sql-journal";
|
|
4
|
+
import { assertLegacyMessagePublicationDrained, backfillPeerMessageRetention, makeSqlMessageDeliveryStore, registerCanonicalMessageWriter } from "@systemfsoftware/agent-storage-sql/sql-message-delivery-store";
|
|
5
|
+
import { SqlScheduleWriteTransaction, makeSqlScheduleStore } from "@systemfsoftware/agent-storage-sql/sql-schedule-store";
|
|
6
|
+
import { SqlInteger, makeSqlQuery, makeSqlTransaction } from "@systemfsoftware/agent-storage-sql/sql-storage";
|
|
7
|
+
import { makeSqlSubmissionLedgerKernel } from "@systemfsoftware/agent-storage-sql/sql-submission-ledger";
|
|
8
|
+
import { makeSqlSubscriptionStore } from "@systemfsoftware/agent-storage-sql/sql-subscription-store";
|
|
9
|
+
import { makeSqlThreadImport } from "@systemfsoftware/agent-storage-sql/sql-thread-import";
|
|
10
|
+
import { makeSqlThreadStoreKernel } from "@systemfsoftware/agent-storage-sql/sql-thread-store";
|
|
11
|
+
import { ActivityMutationFailpoint, ActivityProcessorStore, ActivityStoreError } from "@systemfsoftware/agent/activity-store";
|
|
12
|
+
import { MessageDeliveryStore } from "@systemfsoftware/agent/message-delivery";
|
|
13
|
+
import { StorageOwner } from "@systemfsoftware/agent/run-storage";
|
|
14
|
+
import { ScheduleStore } from "@systemfsoftware/agent/schedule";
|
|
15
|
+
import { SettlementPublisher } from "@systemfsoftware/agent/settlement-publisher";
|
|
16
|
+
import { LedgerError, SubmissionLedger } from "@systemfsoftware/agent/submission-ledger";
|
|
17
|
+
import { SourcePartition, SubscriptionError, SubscriptionStore } from "@systemfsoftware/agent/subscription";
|
|
18
|
+
import { ThreadImport } from "@systemfsoftware/agent/thread-import";
|
|
19
|
+
import { ThreadReader, ThreadStore } from "@systemfsoftware/agent/thread-store";
|
|
20
|
+
import { checkExportSource } from "@systemfsoftware/agent/thread-transfer";
|
|
21
|
+
import * as Context from "effect/Context";
|
|
22
|
+
import * as Effect from "effect/Effect";
|
|
23
|
+
import * as Layer from "effect/Layer";
|
|
24
|
+
import * as Schema from "effect/Schema";
|
|
25
|
+
import * as SqlClient from "effect/sql/SqlClient";
|
|
26
|
+
import * as SqlError from "effect/sql/SqlError";
|
|
27
|
+
import { n as PostgresStorageCorruptionError, r as PostgresStorageError, s as PostgresWriteContention, t as PostgresStorageCompatibilityError } from "./PostgresStorageError-DCuaf7EF.mjs";
|
|
28
|
+
import { RECORD_TAGS } from "@systemfsoftware/agent-storage-sql/sql-canonical-storage";
|
|
29
|
+
import { SQL_PROMPT_PREDICATE, upgradeExecutionCheckpoints } from "@systemfsoftware/agent-storage-sql/sql-thread-native-reads";
|
|
30
|
+
import { createSqlThreadWorkTables, upgradeSqlThreadWorkIndex } from "@systemfsoftware/agent-storage-sql/sql-thread-work";
|
|
31
|
+
import { CURRENT_RECORD_FORMAT, PROMPT_EVIDENCE_TAGS } from "@systemfsoftware/agent/records";
|
|
32
|
+
// src/internal/layout-expression.ts
|
|
33
|
+
/** Decode deparser syntax into meaning; unknown tokens/forms fail closed. Never execute it. */
|
|
34
|
+
const readExpressions = (input) => {
|
|
35
|
+
if (input.length > 16384) return void 0;
|
|
36
|
+
const tokens = [];
|
|
37
|
+
const token = /\s+|"(?:[^"]|"")*"|'(?:[^']|'')*'|->>|>>|::|<>|[a-z_][a-z_0-9]*|(?:256|[0-9]|[1-4][0-9]|5[0-2])(?![0-9])|>=|<=|[=(),[\]>.<-]/giy;
|
|
38
|
+
while (token.lastIndex < input.length) {
|
|
39
|
+
const match = token.exec(input)?.[0];
|
|
40
|
+
if (match === void 0 || tokens.length >= 512) return void 0;
|
|
41
|
+
if (/^\s/.test(match)) continue;
|
|
42
|
+
const quote = match[0];
|
|
43
|
+
tokens.push(quote === "\"" || quote === "'" ? {
|
|
44
|
+
kind: quote,
|
|
45
|
+
value: match.slice(1, -1).replaceAll(quote + quote, quote)
|
|
46
|
+
} : {
|
|
47
|
+
kind: "bare",
|
|
48
|
+
value: match.toLowerCase()
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
let position = 0;
|
|
52
|
+
const take = (value) => {
|
|
53
|
+
if (tokens[position]?.kind !== "bare" || tokens[position]?.value !== value) return false;
|
|
54
|
+
position++;
|
|
55
|
+
return true;
|
|
56
|
+
};
|
|
57
|
+
const identifier = () => {
|
|
58
|
+
const next = tokens[position];
|
|
59
|
+
if (next === void 0 || next.kind === "'" || !/^[a-z_][a-z_0-9]*$/i.test(next.value)) return;
|
|
60
|
+
position++;
|
|
61
|
+
if (!take(".")) return next.value;
|
|
62
|
+
const member = identifier();
|
|
63
|
+
return next.value === "pg_catalog" && member !== void 0 ? `pg_catalog.${member}` : void 0;
|
|
64
|
+
};
|
|
65
|
+
const operand = () => {
|
|
66
|
+
let value;
|
|
67
|
+
const next = tokens[position];
|
|
68
|
+
if (take("(")) {
|
|
69
|
+
value = expression();
|
|
70
|
+
if (!take(")")) return void 0;
|
|
71
|
+
} else if (next?.kind === "'") {
|
|
72
|
+
position++;
|
|
73
|
+
value = ["text", next.value];
|
|
74
|
+
if (take("::")) {
|
|
75
|
+
const type = identifier();
|
|
76
|
+
if (type !== "text" && type !== "pg_catalog.text") return void 0;
|
|
77
|
+
}
|
|
78
|
+
} else if (next?.kind === "bare" && /^(?:256|[0-9]|[1-4][0-9]|5[0-2])$/.test(next.value)) {
|
|
79
|
+
position++;
|
|
80
|
+
value = ["integer", Number(next.value)];
|
|
81
|
+
} else {
|
|
82
|
+
const name = identifier();
|
|
83
|
+
if (name === void 0) return void 0;
|
|
84
|
+
if ((name === "length" || name === "pg_catalog.length") && take("(")) {
|
|
85
|
+
const column = identifier();
|
|
86
|
+
if (column === void 0 || !take(")")) return void 0;
|
|
87
|
+
value = ["length", column];
|
|
88
|
+
} else value = ["column", name];
|
|
89
|
+
}
|
|
90
|
+
if (take("::")) {
|
|
91
|
+
const type = identifier();
|
|
92
|
+
if (value?.[0] !== "integer" || type !== "bigint" && type !== "pg_catalog.int8") return;
|
|
93
|
+
}
|
|
94
|
+
if (take("->>")) {
|
|
95
|
+
const key = operand();
|
|
96
|
+
if (value?.[0] !== "column" || value[1] !== "read_metadata" || key?.[0] !== "text") return;
|
|
97
|
+
return ["json", key[1]];
|
|
98
|
+
}
|
|
99
|
+
if (take("-")) {
|
|
100
|
+
const right = operand();
|
|
101
|
+
return right === void 0 || value === void 0 ? void 0 : [
|
|
102
|
+
"subtract",
|
|
103
|
+
value,
|
|
104
|
+
right
|
|
105
|
+
];
|
|
106
|
+
}
|
|
107
|
+
if (take(">>")) {
|
|
108
|
+
const right = operand();
|
|
109
|
+
return value === void 0 || right?.[0] !== "integer" || right[1] > 52 ? void 0 : [
|
|
110
|
+
"shift",
|
|
111
|
+
value,
|
|
112
|
+
right
|
|
113
|
+
];
|
|
114
|
+
}
|
|
115
|
+
return value;
|
|
116
|
+
};
|
|
117
|
+
const textList = (array) => {
|
|
118
|
+
if (!take("(")) return void 0;
|
|
119
|
+
if (array && (!take("array") || !take("["))) return void 0;
|
|
120
|
+
const values = [];
|
|
121
|
+
do {
|
|
122
|
+
const item = operand();
|
|
123
|
+
if (item?.[0] !== "text" && item?.[0] !== "integer") return void 0;
|
|
124
|
+
values.push(item[1]);
|
|
125
|
+
} while (take(","));
|
|
126
|
+
if (array && !take("]")) return void 0;
|
|
127
|
+
return take(")") ? values : void 0;
|
|
128
|
+
};
|
|
129
|
+
const comparison = () => {
|
|
130
|
+
const left = operand();
|
|
131
|
+
if (left === void 0) return void 0;
|
|
132
|
+
if (take("is")) {
|
|
133
|
+
const tag = take("not") ? "notNull" : "null";
|
|
134
|
+
return take("null") ? [tag, left] : void 0;
|
|
135
|
+
}
|
|
136
|
+
const negated = take("not");
|
|
137
|
+
if (negated || take("in")) {
|
|
138
|
+
if (negated && !take("in")) return void 0;
|
|
139
|
+
const values = textList(false);
|
|
140
|
+
return values === void 0 ? void 0 : [
|
|
141
|
+
negated ? "notIn" : "in",
|
|
142
|
+
left,
|
|
143
|
+
values
|
|
144
|
+
];
|
|
145
|
+
}
|
|
146
|
+
const operator = take("=") ? "eq" : take("<>") ? "ne" : take(">=") ? "ge" : take("<=") ? "le" : take(">") ? "gt" : take("<") ? "lt" : void 0;
|
|
147
|
+
if (operator === void 0) return left;
|
|
148
|
+
if (operator === "eq" && take("any") || operator === "ne" && take("all")) {
|
|
149
|
+
const values = textList(true);
|
|
150
|
+
return values === void 0 ? void 0 : [
|
|
151
|
+
operator === "eq" ? "in" : "notIn",
|
|
152
|
+
left,
|
|
153
|
+
values
|
|
154
|
+
];
|
|
155
|
+
}
|
|
156
|
+
const right = operand();
|
|
157
|
+
return right === void 0 ? void 0 : [
|
|
158
|
+
operator,
|
|
159
|
+
left,
|
|
160
|
+
right
|
|
161
|
+
];
|
|
162
|
+
};
|
|
163
|
+
const expression = () => {
|
|
164
|
+
const parts = [];
|
|
165
|
+
do {
|
|
166
|
+
const part = comparison();
|
|
167
|
+
if (part === void 0) return void 0;
|
|
168
|
+
parts.push(...part[0] === "and" ? part[1] : [part]);
|
|
169
|
+
} while (take("and"));
|
|
170
|
+
return parts.length === 1 ? parts[0] : ["and", parts];
|
|
171
|
+
};
|
|
172
|
+
const expressions = [];
|
|
173
|
+
do {
|
|
174
|
+
const value = expression();
|
|
175
|
+
if (value === void 0) return void 0;
|
|
176
|
+
expressions.push(value);
|
|
177
|
+
} while (take(","));
|
|
178
|
+
return position === tokens.length ? expressions : void 0;
|
|
179
|
+
};
|
|
180
|
+
const matchesLayoutExpressions = (actual, expected) => actual === null ? expected.length === 0 : JSON.stringify(readExpressions(actual)) === JSON.stringify(expected);
|
|
181
|
+
// src/internal/storage-layout.ts
|
|
182
|
+
const CurrentPostgresStorageVersion = 31;
|
|
183
|
+
/** The released layout 25 already has observe_settlement; only these indexes are new. */
|
|
184
|
+
const layout26IndexStatements = {
|
|
185
|
+
effect_agent_subscriptions_capacity: "CREATE INDEX effect_agent_subscriptions_capacity ON __NAMESPACE__.effect_agent_subscriptions (tenant_id, source_address, expires_at_millis) WHERE state IN ('active', 'paused')",
|
|
186
|
+
effect_agent_subscriptions_owner_capacity: "CREATE INDEX effect_agent_subscriptions_owner_capacity ON __NAMESPACE__.effect_agent_subscriptions (tenant_id, source_address, owner_id, expires_at_millis) WHERE state IN ('active', 'paused')",
|
|
187
|
+
effect_agent_subscription_deliveries_capacity: "CREATE INDEX effect_agent_subscription_deliveries_capacity ON __NAMESPACE__.effect_agent_subscription_deliveries (tenant_id, source_address, owner_id) WHERE state IN ('selected', 'prepared')",
|
|
188
|
+
effect_agent_subscription_deliveries_observing: "CREATE INDEX effect_agent_subscription_deliveries_observing ON __NAMESPACE__.effect_agent_subscription_deliveries (tenant_id, source_address, owner_id) WHERE state='delivered' AND observe_settlement",
|
|
189
|
+
effect_agent_message_deliveries_peer_retained: "CREATE INDEX effect_agent_message_deliveries_peer_retained ON __NAMESPACE__.effect_agent_message_deliveries(owner_thread_id, peer_retained_until_millis, message_id) WHERE peer_retained_until_millis IS NOT NULL"
|
|
190
|
+
};
|
|
191
|
+
/** Only live child membership changes on reservation mutations. */
|
|
192
|
+
const liveChildFunctionBody = `
|
|
193
|
+
DECLARE owner_id TEXT;
|
|
194
|
+
BEGIN
|
|
195
|
+
IF TG_OP <> 'INSERT' THEN
|
|
196
|
+
DELETE FROM __NAMESPACE__.effect_agent_live_child_reservations WHERE reservation_id=OLD.reservation_id;
|
|
197
|
+
END IF;
|
|
198
|
+
IF TG_OP <> 'DELETE' AND NEW.status <> 'released' THEN
|
|
199
|
+
SELECT thread_id INTO STRICT owner_id FROM __NAMESPACE__.effect_agent_submissions WHERE submission_id=NEW.parent_submission_id;
|
|
200
|
+
INSERT INTO __NAMESPACE__.effect_agent_live_child_reservations (reservation_id, thread_id) VALUES (NEW.reservation_id, owner_id);
|
|
201
|
+
END IF;
|
|
202
|
+
IF TG_OP = 'DELETE' THEN RETURN OLD; END IF;
|
|
203
|
+
RETURN NEW;
|
|
204
|
+
END;
|
|
205
|
+
`;
|
|
206
|
+
const liveChildTriggers = [
|
|
207
|
+
{
|
|
208
|
+
name: "effect_agent_live_child_insert",
|
|
209
|
+
table: "effect_agent_child_reservations",
|
|
210
|
+
event: "INSERT"
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
name: "effect_agent_live_child_update",
|
|
214
|
+
table: "effect_agent_child_reservations",
|
|
215
|
+
event: "UPDATE"
|
|
216
|
+
},
|
|
217
|
+
{
|
|
218
|
+
name: "effect_agent_live_child_delete",
|
|
219
|
+
table: "effect_agent_child_reservations",
|
|
220
|
+
event: "DELETE"
|
|
221
|
+
}
|
|
222
|
+
];
|
|
223
|
+
/** Fresh layout. Released predecessors take the ordered steps below. */
|
|
224
|
+
const layoutStatements = [
|
|
225
|
+
"CREATE TABLE __NAMESPACE__.\"effect_agent_storage_version\" ( id BOOLEAN PRIMARY KEY NOT NULL, version BIGINT NOT NULL, CONSTRAINT effect_agent_storage_version_single_row CHECK (id) )",
|
|
226
|
+
"CREATE TABLE __NAMESPACE__.\"effect_agent_threads\" ( thread_id TEXT COLLATE \"C\" PRIMARY KEY NOT NULL, created_at TEXT COLLATE \"C\", tail_sequence BIGINT NOT NULL, work_version BIGINT NOT NULL DEFAULT 3, work_state TEXT COLLATE \"C\" NOT NULL DEFAULT 'ready', work_through_sequence BIGINT NOT NULL DEFAULT 0, work_reporting BIGINT NOT NULL DEFAULT 0, work_entry_count BIGINT NOT NULL DEFAULT 0, worker_input_sequence BIGINT, worker_settlement_sequence BIGINT )",
|
|
227
|
+
"CREATE TABLE __NAMESPACE__.effect_agent_envelopes (envelope_key BIGSERIAL PRIMARY KEY NOT NULL, content_digest TEXT COLLATE \"C\" UNIQUE NOT NULL, prefix TEXT COLLATE \"C\" NOT NULL, before_payload TEXT COLLATE \"C\" NOT NULL, before_record_id TEXT COLLATE \"C\" NOT NULL, suffix TEXT COLLATE \"C\" NOT NULL)",
|
|
228
|
+
"CREATE TABLE __NAMESPACE__.effect_agent_run_keys (run_key BIGSERIAL PRIMARY KEY NOT NULL, thread_id TEXT COLLATE \"C\" NOT NULL, run_id TEXT COLLATE \"C\" NOT NULL, UNIQUE (thread_id, run_id), FOREIGN KEY (thread_id) REFERENCES __NAMESPACE__.effect_agent_threads(thread_id) ON DELETE RESTRICT)",
|
|
229
|
+
"CREATE TABLE __NAMESPACE__.effect_agent_definitions (definition_key BIGSERIAL PRIMARY KEY NOT NULL, content_digest TEXT COLLATE \"C\" UNIQUE NOT NULL, value_json TEXT COLLATE \"C\" NOT NULL)",
|
|
230
|
+
"CREATE TABLE __NAMESPACE__.effect_agent_executions (thread_id TEXT COLLATE \"C\" NOT NULL, run_id TEXT COLLATE \"C\" NOT NULL, input_sequence BIGINT NOT NULL, revision BIGINT NOT NULL, checkpoint_json TEXT COLLATE \"C\" NOT NULL, last_commit_id TEXT COLLATE \"C\", last_commit_digest TEXT COLLATE \"C\", PRIMARY KEY (thread_id, run_id), FOREIGN KEY (thread_id) REFERENCES __NAMESPACE__.effect_agent_threads(thread_id) ON DELETE RESTRICT)",
|
|
231
|
+
"CREATE TABLE __NAMESPACE__.\"effect_agent_canonical_batches\" ( thread_id TEXT COLLATE \"C\" NOT NULL, batch_id TEXT COLLATE \"C\" NOT NULL, first_sequence BIGINT NOT NULL, last_sequence BIGINT NOT NULL, producer_id_json TEXT COLLATE \"C\" NOT NULL, CONSTRAINT effect_agent_canonical_batches_span CHECK (first_sequence >= 1 AND last_sequence >= first_sequence AND last_sequence - first_sequence < 256), PRIMARY KEY (thread_id, batch_id), FOREIGN KEY (thread_id) REFERENCES __NAMESPACE__.\"effect_agent_threads\"(thread_id) ON DELETE RESTRICT )",
|
|
232
|
+
"CREATE TABLE __NAMESPACE__.\"effect_agent_canonical_records\" ( thread_id TEXT COLLATE \"C\" NOT NULL, sequence BIGINT NOT NULL, record_id TEXT COLLATE \"C\" NOT NULL, created_at_json TEXT COLLATE \"C\" NOT NULL, envelope_key BIGINT NOT NULL, payload_json TEXT COLLATE \"C\", record_tag BIGINT NOT NULL, run_id BIGINT, tool_call_id TEXT COLLATE \"C\", input_kind TEXT COLLATE \"C\", source_submission_id TEXT COLLATE \"C\", message_id TEXT COLLATE \"C\", submission_id TEXT COLLATE \"C\", application_input BIGINT NOT NULL, context_through BIGINT, context_kind TEXT COLLATE \"C\", worker_thread_id TEXT COLLATE \"C\", handoff BIGINT NOT NULL, PRIMARY KEY (thread_id, sequence), UNIQUE (thread_id, record_id), FOREIGN KEY (thread_id) REFERENCES __NAMESPACE__.\"effect_agent_threads\"(thread_id) ON DELETE RESTRICT )",
|
|
233
|
+
"CREATE TABLE __NAMESPACE__.\"effect_agent_record_runs\" ( thread_id TEXT COLLATE \"C\" NOT NULL, run_id BIGINT NOT NULL, sequence BIGINT NOT NULL, PRIMARY KEY (thread_id, run_id, sequence), FOREIGN KEY (thread_id, sequence) REFERENCES __NAMESPACE__.\"effect_agent_canonical_records\"(thread_id, sequence) ON DELETE RESTRICT )",
|
|
234
|
+
"CREATE TABLE __NAMESPACE__.effect_agent_rollover_buckets (thread_id TEXT COLLATE \"C\" NOT NULL, height BIGINT NOT NULL, bucket BIGINT NOT NULL, sequence BIGINT NOT NULL, PRIMARY KEY (thread_id, height, bucket, sequence), FOREIGN KEY (thread_id, sequence) REFERENCES __NAMESPACE__.effect_agent_canonical_records(thread_id, sequence) ON DELETE RESTRICT)",
|
|
235
|
+
"CREATE TABLE __NAMESPACE__.effect_agent_tool_declarations (thread_id TEXT COLLATE \"C\" NOT NULL, settlement_record_id TEXT COLLATE \"C\" NOT NULL, sequence BIGINT NOT NULL, PRIMARY KEY (thread_id, settlement_record_id, sequence), FOREIGN KEY (thread_id, sequence) REFERENCES __NAMESPACE__.effect_agent_canonical_records(thread_id, sequence) ON DELETE RESTRICT)",
|
|
236
|
+
"CREATE INDEX effect_agent_tool_declarations_sequence ON __NAMESPACE__.effect_agent_tool_declarations(thread_id, sequence)",
|
|
237
|
+
"CREATE TABLE __NAMESPACE__.effect_agent_record_refusals (thread_id TEXT COLLATE \"C\" NOT NULL, reservation_record_id TEXT COLLATE \"C\" NOT NULL, sequence BIGINT NOT NULL, PRIMARY KEY (thread_id, reservation_record_id, sequence), FOREIGN KEY (thread_id, sequence) REFERENCES __NAMESPACE__.effect_agent_canonical_records(thread_id, sequence) ON DELETE RESTRICT)",
|
|
238
|
+
"CREATE INDEX effect_agent_record_refusals_sequence ON __NAMESPACE__.effect_agent_record_refusals(thread_id, sequence)",
|
|
239
|
+
"CREATE TABLE __NAMESPACE__.\"effect_agent_submissions\" ( submission_id TEXT COLLATE \"C\" PRIMARY KEY NOT NULL, thread_id TEXT COLLATE \"C\" NOT NULL, queue_sequence BIGINT NOT NULL, principal TEXT COLLATE \"C\" NOT NULL, idempotency_key TEXT COLLATE \"C\" NOT NULL, agent_id TEXT COLLATE \"C\" NOT NULL, definition_key BIGINT NOT NULL, deployment_id TEXT COLLATE \"C\" NOT NULL, input_json TEXT COLLATE \"C\" NOT NULL, input_digest TEXT COLLATE \"C\" NOT NULL, receipt_id TEXT COLLATE \"C\" NOT NULL, state TEXT COLLATE \"C\" NOT NULL, settled_outcome TEXT COLLATE \"C\", settled_record_id TEXT COLLATE \"C\", finalized_at TEXT COLLATE \"C\", created_at TEXT COLLATE \"C\" NOT NULL, ready_at TEXT COLLATE \"C\", input_applied_record_id TEXT COLLATE \"C\", input_applied_sequence BIGINT, joined_host_submission_id TEXT COLLATE \"C\", suspended_reason_json TEXT COLLATE \"C\", suspended_at TEXT COLLATE \"C\", unknown_reason TEXT COLLATE \"C\", unknown_tool_call_ids_json TEXT COLLATE \"C\", parent_submission_id TEXT COLLATE \"C\", parent_tool_call_id TEXT COLLATE \"C\", admission_group TEXT COLLATE \"C\", admission_fence_json TEXT COLLATE \"C\", worker_admission_json TEXT COLLATE \"C\", message_admission_json TEXT COLLATE \"C\", UNIQUE (thread_id, principal, idempotency_key), UNIQUE (thread_id, queue_sequence) )",
|
|
240
|
+
"CREATE INDEX effect_agent_submissions_group ON __NAMESPACE__.\"effect_agent_submissions\" (thread_id, admission_group) WHERE admission_group IS NOT NULL AND state <> 'settled'",
|
|
241
|
+
"CREATE TABLE __NAMESPACE__.\"effect_agent_abort_intents\" ( submission_id TEXT COLLATE \"C\" PRIMARY KEY NOT NULL, author TEXT COLLATE \"C\" NOT NULL, reason TEXT COLLATE \"C\" NOT NULL, requested_at TEXT COLLATE \"C\" NOT NULL, canonical_record_id TEXT COLLATE \"C\", FOREIGN KEY (submission_id) REFERENCES __NAMESPACE__.\"effect_agent_submissions\"(submission_id) ON DELETE RESTRICT )",
|
|
242
|
+
"CREATE INDEX effect_agent_submissions_joined_host ON __NAMESPACE__.\"effect_agent_submissions\" (joined_host_submission_id) WHERE joined_host_submission_id IS NOT NULL",
|
|
243
|
+
"CREATE TABLE __NAMESPACE__.\"effect_agent_approval_decisions\" ( submission_id TEXT COLLATE \"C\" NOT NULL, tool_call_id TEXT COLLATE \"C\" NOT NULL, decision TEXT COLLATE \"C\" NOT NULL, resolver TEXT COLLATE \"C\" NOT NULL, reason TEXT COLLATE \"C\" NOT NULL, decided_at TEXT COLLATE \"C\" NOT NULL, PRIMARY KEY (submission_id, tool_call_id), FOREIGN KEY (submission_id) REFERENCES __NAMESPACE__.\"effect_agent_submissions\"(submission_id) ON DELETE RESTRICT )",
|
|
244
|
+
"CREATE TABLE __NAMESPACE__.\"effect_agent_unknown_resolutions\" ( submission_id TEXT COLLATE \"C\" NOT NULL, tool_call_id TEXT COLLATE \"C\" NOT NULL, resolution_kind TEXT COLLATE \"C\" NOT NULL, author TEXT COLLATE \"C\" NOT NULL, reason TEXT COLLATE \"C\" NOT NULL, resolution_json TEXT COLLATE \"C\" NOT NULL, resolved_at TEXT COLLATE \"C\" NOT NULL, PRIMARY KEY (submission_id, tool_call_id, resolution_kind), FOREIGN KEY (submission_id) REFERENCES __NAMESPACE__.\"effect_agent_submissions\"(submission_id) ON DELETE RESTRICT )",
|
|
245
|
+
"CREATE INDEX effect_agent_submissions_parent ON __NAMESPACE__.\"effect_agent_submissions\" (parent_submission_id) WHERE parent_submission_id IS NOT NULL",
|
|
246
|
+
"CREATE TABLE __NAMESPACE__.\"effect_agent_child_reservations\" ( reservation_id TEXT COLLATE \"C\" PRIMARY KEY NOT NULL, parent_submission_id TEXT COLLATE \"C\" NOT NULL, parent_tool_call_id TEXT COLLATE \"C\" NOT NULL, child_submission_id TEXT COLLATE \"C\", status TEXT COLLATE \"C\" NOT NULL, allocation_json TEXT COLLATE \"C\" NOT NULL, allocation_digest TEXT COLLATE \"C\" NOT NULL, accounting_json TEXT COLLATE \"C\", reserved_at TEXT COLLATE \"C\" NOT NULL, release_began_at TEXT COLLATE \"C\", released_at TEXT COLLATE \"C\", UNIQUE (parent_submission_id, parent_tool_call_id), FOREIGN KEY (parent_submission_id) REFERENCES __NAMESPACE__.\"effect_agent_submissions\"(submission_id) ON DELETE RESTRICT )",
|
|
247
|
+
"CREATE TABLE __NAMESPACE__.\"effect_agent_schedules\" ( tenant_id TEXT COLLATE \"C\" NOT NULL, owner_id TEXT COLLATE \"C\" NOT NULL, schedule_id TEXT COLLATE \"C\" NOT NULL, deadline_at_millis BIGINT, record_json TEXT COLLATE \"C\" NOT NULL, uses_capacity BOOLEAN NOT NULL, PRIMARY KEY (tenant_id, owner_id, schedule_id) )",
|
|
248
|
+
"CREATE INDEX effect_agent_schedules_deadline ON __NAMESPACE__.\"effect_agent_schedules\" (deadline_at_millis, tenant_id, owner_id, schedule_id) WHERE deadline_at_millis IS NOT NULL",
|
|
249
|
+
"CREATE INDEX effect_agent_schedules_owner_deadline ON __NAMESPACE__.\"effect_agent_schedules\" (tenant_id, owner_id, deadline_at_millis, schedule_id) WHERE deadline_at_millis IS NOT NULL",
|
|
250
|
+
"CREATE TABLE __NAMESPACE__.\"effect_agent_subscription_sequences\" ( tenant_id TEXT COLLATE \"C\" NOT NULL, source_address TEXT COLLATE \"C\" NOT NULL, sequence BIGINT NOT NULL, event_scan_cursor TEXT COLLATE \"C\" NOT NULL, delivery_scan_cursor TEXT COLLATE \"C\" NOT NULL, recovery_scan_cursor BIGINT NOT NULL, PRIMARY KEY (tenant_id, source_address) )",
|
|
251
|
+
"CREATE TABLE __NAMESPACE__.\"effect_agent_subscriptions\" ( tenant_id TEXT COLLATE \"C\" NOT NULL, source_address TEXT COLLATE \"C\" NOT NULL, owner_id TEXT COLLATE \"C\" NOT NULL, subscription_id TEXT COLLATE \"C\" NOT NULL, ordinal BIGINT NOT NULL, source_name TEXT COLLATE \"C\" NOT NULL, source_version TEXT COLLATE \"C\" NOT NULL, matching_key TEXT COLLATE \"C\" NOT NULL, state TEXT COLLATE \"C\" NOT NULL, expires_at_millis BIGINT, recovery_at_millis BIGINT, recovery_present BIGINT NOT NULL DEFAULT 0, record_json TEXT COLLATE \"C\" NOT NULL, PRIMARY KEY (tenant_id, source_address, owner_id, subscription_id), UNIQUE (tenant_id, source_address, ordinal) )",
|
|
252
|
+
"CREATE INDEX effect_agent_subscriptions_owner ON __NAMESPACE__.\"effect_agent_subscriptions\" (tenant_id, source_address, owner_id, ordinal)",
|
|
253
|
+
"CREATE INDEX effect_agent_subscriptions_candidates ON __NAMESPACE__.\"effect_agent_subscriptions\" (tenant_id, source_address, source_name, source_version, matching_key, ordinal)",
|
|
254
|
+
"CREATE INDEX effect_agent_subscriptions_recovery ON __NAMESPACE__.\"effect_agent_subscriptions\" (tenant_id, source_address, recovery_at_millis, ordinal) WHERE recovery_at_millis IS NOT NULL",
|
|
255
|
+
"CREATE TABLE __NAMESPACE__.\"effect_agent_subscription_events\" ( tenant_id TEXT COLLATE \"C\" NOT NULL, source_address TEXT COLLATE \"C\" NOT NULL, event_id TEXT COLLATE \"C\" NOT NULL, source_name TEXT COLLATE \"C\" NOT NULL, source_version TEXT COLLATE \"C\" NOT NULL, matching_key TEXT COLLATE \"C\" NOT NULL, payload_digest TEXT COLLATE \"C\" NOT NULL, cutoff BIGINT NOT NULL, cursor BIGINT NOT NULL, routing_complete BIGINT NOT NULL, tombstone BIGINT NOT NULL DEFAULT 0, next_attempt_at_millis BIGINT NOT NULL, record_json TEXT COLLATE \"C\" NOT NULL, PRIMARY KEY (tenant_id, source_address, event_id) )",
|
|
256
|
+
"CREATE INDEX effect_agent_subscription_events_pending ON __NAMESPACE__.\"effect_agent_subscription_events\" (tenant_id, source_address, routing_complete, next_attempt_at_millis, event_id)",
|
|
257
|
+
"CREATE TABLE __NAMESPACE__.\"effect_agent_subscription_deliveries\" ( tenant_id TEXT COLLATE \"C\" NOT NULL, source_address TEXT COLLATE \"C\" NOT NULL, owner_id TEXT COLLATE \"C\" NOT NULL, subscription_id TEXT COLLATE \"C\" NOT NULL, event_id TEXT COLLATE \"C\" NOT NULL, delivery_key TEXT COLLATE \"C\" NOT NULL, state TEXT COLLATE \"C\" NOT NULL, next_attempt_at_millis BIGINT NOT NULL, record_json TEXT COLLATE \"C\" NOT NULL, retry_parked BOOLEAN NOT NULL, observe_settlement BOOLEAN NOT NULL, PRIMARY KEY (tenant_id, source_address, owner_id, subscription_id, event_id), UNIQUE (tenant_id, source_address, delivery_key) )",
|
|
258
|
+
"CREATE INDEX effect_agent_subscription_deliveries_pending ON __NAMESPACE__.\"effect_agent_subscription_deliveries\" (tenant_id, source_address, state, next_attempt_at_millis, delivery_key)",
|
|
259
|
+
"CREATE INDEX effect_agent_subscription_deliveries_registration ON __NAMESPACE__.\"effect_agent_subscription_deliveries\" (tenant_id, source_address, owner_id, subscription_id, delivery_key)",
|
|
260
|
+
"CREATE TABLE __NAMESPACE__.\"effect_agent_message_deliveries\" ( owner_thread_id TEXT COLLATE \"C\" NOT NULL, message_id TEXT COLLATE \"C\" NOT NULL, version BIGINT NOT NULL, state TEXT COLLATE \"C\" NOT NULL, deadline_at_millis BIGINT, peer_retained_until_millis BIGINT, record_json TEXT COLLATE \"C\" NOT NULL, read_metadata JSONB NOT NULL, PRIMARY KEY (owner_thread_id, message_id) )",
|
|
261
|
+
"CREATE INDEX effect_agent_message_deliveries_due ON __NAMESPACE__.\"effect_agent_message_deliveries\" (deadline_at_millis, owner_thread_id, message_id) WHERE deadline_at_millis IS NOT NULL",
|
|
262
|
+
"CREATE INDEX effect_agent_submissions_nonterminal ON __NAMESPACE__.\"effect_agent_submissions\" (thread_id, queue_sequence) WHERE state <> 'settled'",
|
|
263
|
+
"CREATE TABLE __NAMESPACE__.\"effect_agent_worker_stops\" (thread_id TEXT COLLATE \"C\" PRIMARY KEY NOT NULL, terminal TEXT COLLATE \"C\")",
|
|
264
|
+
"CREATE INDEX effect_agent_worker_starts ON __NAMESPACE__.\"effect_agent_message_deliveries\"(owner_thread_id, (read_metadata ->> 'delegationId'), (read_metadata ->> 'targetAgentId'), message_id) WHERE (read_metadata ->> 'workerStart') = 'true'",
|
|
265
|
+
"CREATE INDEX effect_agent_worker_pending ON __NAMESPACE__.\"effect_agent_message_deliveries\"(owner_thread_id, (read_metadata ->> 'threadId'), message_id) WHERE state IN ('pending', 'parked') AND (read_metadata ->> 'hasReceipt') = 'false'",
|
|
266
|
+
"CREATE INDEX effect_agent_records_call ON __NAMESPACE__.\"effect_agent_canonical_records\"(thread_id, record_tag, run_id, tool_call_id) WHERE tool_call_id IS NOT NULL",
|
|
267
|
+
"CREATE INDEX effect_agent_records_run_input ON __NAMESPACE__.\"effect_agent_canonical_records\"(thread_id, run_id) WHERE record_tag = 30 AND input_kind = 'user'",
|
|
268
|
+
"CREATE INDEX effect_agent_records_subtree ON __NAMESPACE__.\"effect_agent_canonical_records\"(thread_id, source_submission_id, sequence) WHERE record_tag = 22",
|
|
269
|
+
"CREATE INDEX effect_agent_records_worker_input ON __NAMESPACE__.\"effect_agent_canonical_records\"(thread_id, message_id) WHERE record_tag = 34",
|
|
270
|
+
"CREATE INDEX effect_agent_message_deliveries_pending ON __NAMESPACE__.\"effect_agent_message_deliveries\"(owner_thread_id, message_id) WHERE state NOT IN ('processed', 'refused')",
|
|
271
|
+
"CREATE INDEX effect_agent_records_tag ON __NAMESPACE__.\"effect_agent_canonical_records\"(thread_id, record_tag, sequence)",
|
|
272
|
+
"CREATE INDEX effect_agent_records_handoff ON __NAMESPACE__.\"effect_agent_canonical_records\"(thread_id, sequence) WHERE handoff = 1",
|
|
273
|
+
"CREATE INDEX effect_agent_records_application_input ON __NAMESPACE__.effect_agent_canonical_records(thread_id, sequence) WHERE application_input = 1",
|
|
274
|
+
`CREATE INDEX effect_agent_records_prompt ON __NAMESPACE__.effect_agent_canonical_records(thread_id, sequence) WHERE ${SQL_PROMPT_PREDICATE}`,
|
|
275
|
+
"CREATE INDEX effect_agent_records_admitted_input ON __NAMESPACE__.effect_agent_canonical_records(thread_id, sequence) WHERE record_tag = 30 AND submission_id IS NOT NULL",
|
|
276
|
+
"CREATE INDEX effect_agent_records_worker_funding ON __NAMESPACE__.effect_agent_canonical_records(thread_id, source_submission_id, sequence, message_id) WHERE record_tag = 34",
|
|
277
|
+
"CREATE INDEX effect_agent_records_worker_completed ON __NAMESPACE__.effect_agent_canonical_records(thread_id, message_id, sequence) WHERE record_tag = 32",
|
|
278
|
+
"CREATE INDEX effect_agent_records_worker_stop ON __NAMESPACE__.effect_agent_canonical_records(thread_id, worker_thread_id, sequence) WHERE record_tag = 38",
|
|
279
|
+
"CREATE INDEX effect_agent_message_deliveries_identity ON __NAMESPACE__.effect_agent_message_deliveries(owner_thread_id, (read_metadata ->> 'messageId'))",
|
|
280
|
+
"CREATE INDEX effect_agent_message_deliveries_peer ON __NAMESPACE__.effect_agent_message_deliveries(owner_thread_id, message_id) WHERE state NOT IN ('processed', 'refused') AND (read_metadata ->> 'peer') = 'true'",
|
|
281
|
+
"CREATE TABLE __NAMESPACE__.effect_agent_journal_ranges (thread_id TEXT COLLATE \"C\" NOT NULL, first_sequence BIGINT NOT NULL, last_sequence BIGINT NOT NULL, record_count BIGINT NOT NULL, batch_count BIGINT NOT NULL, byte_count BIGINT NOT NULL, state TEXT COLLATE \"C\" NOT NULL, locator TEXT COLLATE \"C\", PRIMARY KEY (thread_id, first_sequence), FOREIGN KEY (thread_id) REFERENCES __NAMESPACE__.effect_agent_threads(thread_id) ON DELETE RESTRICT)",
|
|
282
|
+
"CREATE TABLE __NAMESPACE__.effect_agent_archive_batches (thread_id TEXT COLLATE \"C\" NOT NULL, batch_id TEXT COLLATE \"C\" NOT NULL, range_first_sequence BIGINT NOT NULL, batch_json TEXT COLLATE \"C\" NOT NULL, PRIMARY KEY (thread_id, batch_id), FOREIGN KEY (thread_id, batch_id) REFERENCES __NAMESPACE__.effect_agent_canonical_batches(thread_id, batch_id) ON DELETE RESTRICT, CONSTRAINT effect_agent_archive_batches_range_fkey FOREIGN KEY (thread_id, range_first_sequence) REFERENCES __NAMESPACE__.effect_agent_journal_ranges(thread_id, first_sequence) ON DELETE RESTRICT)",
|
|
283
|
+
"CREATE TABLE __NAMESPACE__.effect_agent_archive_records (thread_id TEXT COLLATE \"C\" NOT NULL, sequence BIGINT NOT NULL, range_first_sequence BIGINT NOT NULL, record_json TEXT COLLATE \"C\" NOT NULL, PRIMARY KEY (thread_id, sequence), FOREIGN KEY (thread_id, sequence) REFERENCES __NAMESPACE__.effect_agent_canonical_records(thread_id, sequence) ON DELETE RESTRICT, CONSTRAINT effect_agent_archive_records_range_fkey FOREIGN KEY (thread_id, range_first_sequence) REFERENCES __NAMESPACE__.effect_agent_journal_ranges(thread_id, first_sequence) ON DELETE RESTRICT)",
|
|
284
|
+
"CREATE UNIQUE INDEX effect_agent_canonical_batches_sequence ON __NAMESPACE__.effect_agent_canonical_batches(thread_id, first_sequence)",
|
|
285
|
+
"CREATE TABLE __NAMESPACE__.effect_agent_live_child_reservations (reservation_id TEXT COLLATE \"C\" PRIMARY KEY NOT NULL, thread_id TEXT COLLATE \"C\" NOT NULL)",
|
|
286
|
+
"CREATE INDEX effect_agent_live_child_reservations_thread ON __NAMESPACE__.effect_agent_live_child_reservations(thread_id, reservation_id)",
|
|
287
|
+
"CREATE TABLE __NAMESPACE__.effect_agent_import_delivery_visits (thread_id TEXT COLLATE \"C\" NOT NULL, message_id TEXT COLLATE \"C\" NOT NULL, path_id TEXT COLLATE \"C\" NOT NULL, completed BIGINT NOT NULL, PRIMARY KEY (thread_id, message_id))",
|
|
288
|
+
"CREATE INDEX effect_agent_import_delivery_visits_path ON __NAMESPACE__.effect_agent_import_delivery_visits(thread_id, path_id)",
|
|
289
|
+
`CREATE FUNCTION __NAMESPACE__.effect_agent_live_child_mutation() RETURNS trigger LANGUAGE plpgsql AS $transfer$${liveChildFunctionBody}$transfer$`,
|
|
290
|
+
"CREATE INDEX effect_agent_message_deliveries_capacity ON __NAMESPACE__.effect_agent_message_deliveries(owner_thread_id, (read_metadata ->> 'workerUpdate'), message_id) WHERE state IN ('pending', 'accepted', 'parked')",
|
|
291
|
+
...liveChildTriggers.map((trigger) => `CREATE TRIGGER ${trigger.name} AFTER ${trigger.event} ON __NAMESPACE__.${trigger.table} FOR EACH ROW EXECUTE FUNCTION __NAMESPACE__.effect_agent_live_child_mutation()`),
|
|
292
|
+
...Object.values(layout26IndexStatements)
|
|
293
|
+
];
|
|
294
|
+
const layoutShape = {
|
|
295
|
+
effect_agent_executions: {
|
|
296
|
+
columns: [
|
|
297
|
+
"thread_id:text:true",
|
|
298
|
+
"run_id:text:true",
|
|
299
|
+
"input_sequence:bigint:true",
|
|
300
|
+
"revision:bigint:true",
|
|
301
|
+
"checkpoint_json:text:true",
|
|
302
|
+
"last_commit_id:text:false",
|
|
303
|
+
"last_commit_digest:text:false"
|
|
304
|
+
],
|
|
305
|
+
constraints: {
|
|
306
|
+
effect_agent_executions_pkey: ["p", ["thread_id", "run_id"]],
|
|
307
|
+
effect_agent_executions_thread_id_fkey: [
|
|
308
|
+
"f",
|
|
309
|
+
["thread_id"],
|
|
310
|
+
"effect_agent_threads",
|
|
311
|
+
["thread_id"]
|
|
312
|
+
]
|
|
313
|
+
}
|
|
314
|
+
},
|
|
315
|
+
effect_agent_journal_ranges: {
|
|
316
|
+
columns: [
|
|
317
|
+
"thread_id:text:true",
|
|
318
|
+
"first_sequence:bigint:true",
|
|
319
|
+
"last_sequence:bigint:true",
|
|
320
|
+
"record_count:bigint:true",
|
|
321
|
+
"batch_count:bigint:true",
|
|
322
|
+
"byte_count:bigint:true",
|
|
323
|
+
"state:text:true",
|
|
324
|
+
"locator:text:false"
|
|
325
|
+
],
|
|
326
|
+
constraints: {
|
|
327
|
+
effect_agent_journal_ranges_pkey: ["p", ["thread_id", "first_sequence"]],
|
|
328
|
+
effect_agent_journal_ranges_thread_id_fkey: [
|
|
329
|
+
"f",
|
|
330
|
+
["thread_id"],
|
|
331
|
+
"effect_agent_threads",
|
|
332
|
+
["thread_id"]
|
|
333
|
+
]
|
|
334
|
+
}
|
|
335
|
+
},
|
|
336
|
+
effect_agent_archive_batches: {
|
|
337
|
+
columns: [
|
|
338
|
+
"thread_id:text:true",
|
|
339
|
+
"batch_id:text:true",
|
|
340
|
+
"range_first_sequence:bigint:true",
|
|
341
|
+
"batch_json:text:true"
|
|
342
|
+
],
|
|
343
|
+
constraints: {
|
|
344
|
+
effect_agent_archive_batches_pkey: ["p", ["thread_id", "batch_id"]],
|
|
345
|
+
effect_agent_archive_batches_thread_id_batch_id_fkey: [
|
|
346
|
+
"f",
|
|
347
|
+
["thread_id", "batch_id"],
|
|
348
|
+
"effect_agent_canonical_batches",
|
|
349
|
+
["thread_id", "batch_id"]
|
|
350
|
+
],
|
|
351
|
+
effect_agent_archive_batches_range_fkey: [
|
|
352
|
+
"f",
|
|
353
|
+
["thread_id", "range_first_sequence"],
|
|
354
|
+
"effect_agent_journal_ranges",
|
|
355
|
+
["thread_id", "first_sequence"]
|
|
356
|
+
]
|
|
357
|
+
}
|
|
358
|
+
},
|
|
359
|
+
effect_agent_archive_records: {
|
|
360
|
+
columns: [
|
|
361
|
+
"thread_id:text:true",
|
|
362
|
+
"sequence:bigint:true",
|
|
363
|
+
"range_first_sequence:bigint:true",
|
|
364
|
+
"record_json:text:true"
|
|
365
|
+
],
|
|
366
|
+
constraints: {
|
|
367
|
+
effect_agent_archive_records_pkey: ["p", ["thread_id", "sequence"]],
|
|
368
|
+
effect_agent_archive_records_thread_id_sequence_fkey: [
|
|
369
|
+
"f",
|
|
370
|
+
["thread_id", "sequence"],
|
|
371
|
+
"effect_agent_canonical_records",
|
|
372
|
+
["thread_id", "sequence"]
|
|
373
|
+
],
|
|
374
|
+
effect_agent_archive_records_range_fkey: [
|
|
375
|
+
"f",
|
|
376
|
+
["thread_id", "range_first_sequence"],
|
|
377
|
+
"effect_agent_journal_ranges",
|
|
378
|
+
["thread_id", "first_sequence"]
|
|
379
|
+
]
|
|
380
|
+
}
|
|
381
|
+
},
|
|
382
|
+
effect_agent_abort_intents: {
|
|
383
|
+
columns: [
|
|
384
|
+
"submission_id:text:true",
|
|
385
|
+
"author:text:true",
|
|
386
|
+
"reason:text:true",
|
|
387
|
+
"requested_at:text:true",
|
|
388
|
+
"canonical_record_id:text:false"
|
|
389
|
+
],
|
|
390
|
+
constraints: {
|
|
391
|
+
effect_agent_abort_intents_pkey: ["p", ["submission_id"]],
|
|
392
|
+
effect_agent_abort_intents_submission_id_fkey: [
|
|
393
|
+
"f",
|
|
394
|
+
["submission_id"],
|
|
395
|
+
"effect_agent_submissions",
|
|
396
|
+
["submission_id"]
|
|
397
|
+
]
|
|
398
|
+
}
|
|
399
|
+
},
|
|
400
|
+
effect_agent_approval_decisions: {
|
|
401
|
+
columns: [
|
|
402
|
+
"submission_id:text:true",
|
|
403
|
+
"tool_call_id:text:true",
|
|
404
|
+
"decision:text:true",
|
|
405
|
+
"resolver:text:true",
|
|
406
|
+
"reason:text:true",
|
|
407
|
+
"decided_at:text:true"
|
|
408
|
+
],
|
|
409
|
+
constraints: {
|
|
410
|
+
effect_agent_approval_decisions_pkey: ["p", ["submission_id", "tool_call_id"]],
|
|
411
|
+
effect_agent_approval_decisions_submission_id_fkey: [
|
|
412
|
+
"f",
|
|
413
|
+
["submission_id"],
|
|
414
|
+
"effect_agent_submissions",
|
|
415
|
+
["submission_id"]
|
|
416
|
+
]
|
|
417
|
+
}
|
|
418
|
+
},
|
|
419
|
+
effect_agent_envelopes: {
|
|
420
|
+
columns: [
|
|
421
|
+
"envelope_key:bigint:true",
|
|
422
|
+
"content_digest:text:true",
|
|
423
|
+
"prefix:text:true",
|
|
424
|
+
"before_payload:text:true",
|
|
425
|
+
"before_record_id:text:true",
|
|
426
|
+
"suffix:text:true"
|
|
427
|
+
],
|
|
428
|
+
constraints: {
|
|
429
|
+
effect_agent_envelopes_pkey: ["p", ["envelope_key"]],
|
|
430
|
+
effect_agent_envelopes_content_digest_key: ["u", ["content_digest"]]
|
|
431
|
+
}
|
|
432
|
+
},
|
|
433
|
+
effect_agent_run_keys: {
|
|
434
|
+
columns: [
|
|
435
|
+
"run_key:bigint:true",
|
|
436
|
+
"thread_id:text:true",
|
|
437
|
+
"run_id:text:true"
|
|
438
|
+
],
|
|
439
|
+
constraints: {
|
|
440
|
+
effect_agent_run_keys_pkey: ["p", ["run_key"]],
|
|
441
|
+
effect_agent_run_keys_thread_id_run_id_key: ["u", ["thread_id", "run_id"]],
|
|
442
|
+
effect_agent_run_keys_thread_id_fkey: [
|
|
443
|
+
"f",
|
|
444
|
+
["thread_id"],
|
|
445
|
+
"effect_agent_threads",
|
|
446
|
+
["thread_id"]
|
|
447
|
+
]
|
|
448
|
+
}
|
|
449
|
+
},
|
|
450
|
+
effect_agent_definitions: {
|
|
451
|
+
columns: [
|
|
452
|
+
"definition_key:bigint:true",
|
|
453
|
+
"content_digest:text:true",
|
|
454
|
+
"value_json:text:true"
|
|
455
|
+
],
|
|
456
|
+
constraints: {
|
|
457
|
+
effect_agent_definitions_pkey: ["p", ["definition_key"]],
|
|
458
|
+
effect_agent_definitions_content_digest_key: ["u", ["content_digest"]]
|
|
459
|
+
}
|
|
460
|
+
},
|
|
461
|
+
effect_agent_canonical_batches: {
|
|
462
|
+
columns: [
|
|
463
|
+
"thread_id:text:true",
|
|
464
|
+
"batch_id:text:true",
|
|
465
|
+
"first_sequence:bigint:true",
|
|
466
|
+
"last_sequence:bigint:true",
|
|
467
|
+
"producer_id_json:text:true"
|
|
468
|
+
],
|
|
469
|
+
constraints: {
|
|
470
|
+
effect_agent_canonical_batches_pkey: ["p", ["thread_id", "batch_id"]],
|
|
471
|
+
effect_agent_canonical_batches_span: [
|
|
472
|
+
"c",
|
|
473
|
+
["first_sequence", "last_sequence"],
|
|
474
|
+
["and", [
|
|
475
|
+
[
|
|
476
|
+
"ge",
|
|
477
|
+
["column", "first_sequence"],
|
|
478
|
+
["integer", 1]
|
|
479
|
+
],
|
|
480
|
+
[
|
|
481
|
+
"ge",
|
|
482
|
+
["column", "last_sequence"],
|
|
483
|
+
["column", "first_sequence"]
|
|
484
|
+
],
|
|
485
|
+
[
|
|
486
|
+
"lt",
|
|
487
|
+
[
|
|
488
|
+
"subtract",
|
|
489
|
+
["column", "last_sequence"],
|
|
490
|
+
["column", "first_sequence"]
|
|
491
|
+
],
|
|
492
|
+
["integer", 256]
|
|
493
|
+
]
|
|
494
|
+
]]
|
|
495
|
+
],
|
|
496
|
+
effect_agent_canonical_batches_thread_id_fkey: [
|
|
497
|
+
"f",
|
|
498
|
+
["thread_id"],
|
|
499
|
+
"effect_agent_threads",
|
|
500
|
+
["thread_id"]
|
|
501
|
+
]
|
|
502
|
+
}
|
|
503
|
+
},
|
|
504
|
+
effect_agent_tool_declarations: {
|
|
505
|
+
columns: [
|
|
506
|
+
"thread_id:text:true",
|
|
507
|
+
"settlement_record_id:text:true",
|
|
508
|
+
"sequence:bigint:true"
|
|
509
|
+
],
|
|
510
|
+
constraints: {
|
|
511
|
+
effect_agent_tool_declarations_pkey: ["p", [
|
|
512
|
+
"thread_id",
|
|
513
|
+
"settlement_record_id",
|
|
514
|
+
"sequence"
|
|
515
|
+
]],
|
|
516
|
+
effect_agent_tool_declarations_thread_id_sequence_fkey: [
|
|
517
|
+
"f",
|
|
518
|
+
["thread_id", "sequence"],
|
|
519
|
+
"effect_agent_canonical_records",
|
|
520
|
+
["thread_id", "sequence"]
|
|
521
|
+
]
|
|
522
|
+
}
|
|
523
|
+
},
|
|
524
|
+
effect_agent_record_refusals: {
|
|
525
|
+
columns: [
|
|
526
|
+
"thread_id:text:true",
|
|
527
|
+
"reservation_record_id:text:true",
|
|
528
|
+
"sequence:bigint:true"
|
|
529
|
+
],
|
|
530
|
+
constraints: {
|
|
531
|
+
effect_agent_record_refusals_pkey: ["p", [
|
|
532
|
+
"thread_id",
|
|
533
|
+
"reservation_record_id",
|
|
534
|
+
"sequence"
|
|
535
|
+
]],
|
|
536
|
+
effect_agent_record_refusals_thread_id_sequence_fkey: [
|
|
537
|
+
"f",
|
|
538
|
+
["thread_id", "sequence"],
|
|
539
|
+
"effect_agent_canonical_records",
|
|
540
|
+
["thread_id", "sequence"]
|
|
541
|
+
]
|
|
542
|
+
}
|
|
543
|
+
},
|
|
544
|
+
effect_agent_live_child_reservations: {
|
|
545
|
+
columns: ["reservation_id:text:true", "thread_id:text:true"],
|
|
546
|
+
constraints: { effect_agent_live_child_reservations_pkey: ["p", ["reservation_id"]] }
|
|
547
|
+
},
|
|
548
|
+
effect_agent_record_runs: {
|
|
549
|
+
columns: [
|
|
550
|
+
"thread_id:text:true",
|
|
551
|
+
"run_id:bigint:true",
|
|
552
|
+
"sequence:bigint:true"
|
|
553
|
+
],
|
|
554
|
+
constraints: {
|
|
555
|
+
effect_agent_record_runs_pkey: ["p", [
|
|
556
|
+
"thread_id",
|
|
557
|
+
"run_id",
|
|
558
|
+
"sequence"
|
|
559
|
+
]],
|
|
560
|
+
effect_agent_record_runs_thread_id_sequence_fkey: [
|
|
561
|
+
"f",
|
|
562
|
+
["thread_id", "sequence"],
|
|
563
|
+
"effect_agent_canonical_records",
|
|
564
|
+
["thread_id", "sequence"]
|
|
565
|
+
]
|
|
566
|
+
}
|
|
567
|
+
},
|
|
568
|
+
effect_agent_canonical_records: {
|
|
569
|
+
columns: [
|
|
570
|
+
"thread_id:text:true",
|
|
571
|
+
"sequence:bigint:true",
|
|
572
|
+
"record_id:text:true",
|
|
573
|
+
"created_at_json:text:true",
|
|
574
|
+
"envelope_key:bigint:true",
|
|
575
|
+
"payload_json:text:false",
|
|
576
|
+
"record_tag:bigint:true",
|
|
577
|
+
"run_id:bigint:false",
|
|
578
|
+
"tool_call_id:text:false",
|
|
579
|
+
"input_kind:text:false",
|
|
580
|
+
"source_submission_id:text:false",
|
|
581
|
+
"message_id:text:false",
|
|
582
|
+
"submission_id:text:false",
|
|
583
|
+
"application_input:bigint:true",
|
|
584
|
+
"context_through:bigint:false",
|
|
585
|
+
"context_kind:text:false",
|
|
586
|
+
"worker_thread_id:text:false",
|
|
587
|
+
"handoff:bigint:true"
|
|
588
|
+
],
|
|
589
|
+
constraints: {
|
|
590
|
+
effect_agent_canonical_records_pkey: ["p", ["thread_id", "sequence"]],
|
|
591
|
+
effect_agent_canonical_records_thread_id_fkey: [
|
|
592
|
+
"f",
|
|
593
|
+
["thread_id"],
|
|
594
|
+
"effect_agent_threads",
|
|
595
|
+
["thread_id"]
|
|
596
|
+
],
|
|
597
|
+
effect_agent_canonical_records_thread_id_record_id_key: ["u", ["thread_id", "record_id"]]
|
|
598
|
+
}
|
|
599
|
+
},
|
|
600
|
+
effect_agent_checkpoints: {
|
|
601
|
+
columns: [
|
|
602
|
+
"thread_id:text:true",
|
|
603
|
+
"through_sequence:bigint:true",
|
|
604
|
+
"checkpoint_json:text:true"
|
|
605
|
+
],
|
|
606
|
+
constraints: {
|
|
607
|
+
effect_agent_checkpoints_pkey: ["p", ["thread_id", "through_sequence"]],
|
|
608
|
+
effect_agent_checkpoints_thread_id_fkey: [
|
|
609
|
+
"f",
|
|
610
|
+
["thread_id"],
|
|
611
|
+
"effect_agent_threads",
|
|
612
|
+
["thread_id"]
|
|
613
|
+
]
|
|
614
|
+
}
|
|
615
|
+
},
|
|
616
|
+
effect_agent_child_reservations: {
|
|
617
|
+
columns: [
|
|
618
|
+
"reservation_id:text:true",
|
|
619
|
+
"parent_submission_id:text:true",
|
|
620
|
+
"parent_tool_call_id:text:true",
|
|
621
|
+
"child_submission_id:text:false",
|
|
622
|
+
"status:text:true",
|
|
623
|
+
"allocation_json:text:true",
|
|
624
|
+
"allocation_digest:text:true",
|
|
625
|
+
"accounting_json:text:false",
|
|
626
|
+
"reserved_at:text:true",
|
|
627
|
+
"release_began_at:text:false",
|
|
628
|
+
"released_at:text:false"
|
|
629
|
+
],
|
|
630
|
+
constraints: {
|
|
631
|
+
effect_agent_child_reservatio_parent_submission_id_parent_t_key: ["u", ["parent_submission_id", "parent_tool_call_id"]],
|
|
632
|
+
effect_agent_child_reservations_parent_submission_id_fkey: [
|
|
633
|
+
"f",
|
|
634
|
+
["parent_submission_id"],
|
|
635
|
+
"effect_agent_submissions",
|
|
636
|
+
["submission_id"]
|
|
637
|
+
],
|
|
638
|
+
effect_agent_child_reservations_pkey: ["p", ["reservation_id"]]
|
|
639
|
+
}
|
|
640
|
+
},
|
|
641
|
+
effect_agent_message_deliveries: {
|
|
642
|
+
columns: [
|
|
643
|
+
"owner_thread_id:text:true",
|
|
644
|
+
"message_id:text:true",
|
|
645
|
+
"version:bigint:true",
|
|
646
|
+
"state:text:true",
|
|
647
|
+
"deadline_at_millis:bigint:false",
|
|
648
|
+
"peer_retained_until_millis:bigint:false",
|
|
649
|
+
"record_json:text:true",
|
|
650
|
+
"read_metadata:jsonb:true"
|
|
651
|
+
],
|
|
652
|
+
constraints: { effect_agent_message_deliveries_pkey: ["p", ["owner_thread_id", "message_id"]] }
|
|
653
|
+
},
|
|
654
|
+
effect_agent_schedules: {
|
|
655
|
+
columns: [
|
|
656
|
+
"tenant_id:text:true",
|
|
657
|
+
"owner_id:text:true",
|
|
658
|
+
"schedule_id:text:true",
|
|
659
|
+
"deadline_at_millis:bigint:false",
|
|
660
|
+
"record_json:text:true",
|
|
661
|
+
"uses_capacity:boolean:true"
|
|
662
|
+
],
|
|
663
|
+
constraints: { effect_agent_schedules_pkey: ["p", [
|
|
664
|
+
"tenant_id",
|
|
665
|
+
"owner_id",
|
|
666
|
+
"schedule_id"
|
|
667
|
+
]] }
|
|
668
|
+
},
|
|
669
|
+
effect_agent_storage_version: {
|
|
670
|
+
columns: ["id:boolean:true", "version:bigint:true"],
|
|
671
|
+
constraints: {
|
|
672
|
+
effect_agent_storage_version_pkey: ["p", ["id"]],
|
|
673
|
+
effect_agent_storage_version_single_row: [
|
|
674
|
+
"c",
|
|
675
|
+
["id"],
|
|
676
|
+
["column", "id"]
|
|
677
|
+
]
|
|
678
|
+
}
|
|
679
|
+
},
|
|
680
|
+
effect_agent_submissions: {
|
|
681
|
+
columns: [
|
|
682
|
+
"submission_id:text:true",
|
|
683
|
+
"thread_id:text:true",
|
|
684
|
+
"queue_sequence:bigint:true",
|
|
685
|
+
"principal:text:true",
|
|
686
|
+
"idempotency_key:text:true",
|
|
687
|
+
"agent_id:text:true",
|
|
688
|
+
"definition_key:bigint:true",
|
|
689
|
+
"deployment_id:text:true",
|
|
690
|
+
"input_json:text:true",
|
|
691
|
+
"input_digest:text:true",
|
|
692
|
+
"receipt_id:text:true",
|
|
693
|
+
"state:text:true",
|
|
694
|
+
"settled_outcome:text:false",
|
|
695
|
+
"settled_record_id:text:false",
|
|
696
|
+
"finalized_at:text:false",
|
|
697
|
+
"created_at:text:true",
|
|
698
|
+
"ready_at:text:false",
|
|
699
|
+
"input_applied_record_id:text:false",
|
|
700
|
+
"input_applied_sequence:bigint:false",
|
|
701
|
+
"joined_host_submission_id:text:false",
|
|
702
|
+
"suspended_reason_json:text:false",
|
|
703
|
+
"suspended_at:text:false",
|
|
704
|
+
"unknown_reason:text:false",
|
|
705
|
+
"unknown_tool_call_ids_json:text:false",
|
|
706
|
+
"parent_submission_id:text:false",
|
|
707
|
+
"parent_tool_call_id:text:false",
|
|
708
|
+
"admission_group:text:false",
|
|
709
|
+
"admission_fence_json:text:false",
|
|
710
|
+
"worker_admission_json:text:false",
|
|
711
|
+
"message_admission_json:text:false"
|
|
712
|
+
],
|
|
713
|
+
constraints: {
|
|
714
|
+
effect_agent_submissions_pkey: ["p", ["submission_id"]],
|
|
715
|
+
effect_agent_submissions_thread_id_principal_idempotency_ke_key: ["u", [
|
|
716
|
+
"thread_id",
|
|
717
|
+
"principal",
|
|
718
|
+
"idempotency_key"
|
|
719
|
+
]],
|
|
720
|
+
effect_agent_submissions_thread_id_queue_sequence_key: ["u", ["thread_id", "queue_sequence"]]
|
|
721
|
+
}
|
|
722
|
+
},
|
|
723
|
+
effect_agent_subscription_deliveries: {
|
|
724
|
+
columns: [
|
|
725
|
+
"tenant_id:text:true",
|
|
726
|
+
"source_address:text:true",
|
|
727
|
+
"owner_id:text:true",
|
|
728
|
+
"subscription_id:text:true",
|
|
729
|
+
"event_id:text:true",
|
|
730
|
+
"delivery_key:text:true",
|
|
731
|
+
"state:text:true",
|
|
732
|
+
"next_attempt_at_millis:bigint:true",
|
|
733
|
+
"record_json:text:true",
|
|
734
|
+
"retry_parked:boolean:true",
|
|
735
|
+
"observe_settlement:boolean:true"
|
|
736
|
+
],
|
|
737
|
+
constraints: {
|
|
738
|
+
effect_agent_subscription_del_tenant_id_source_address_deli_key: ["u", [
|
|
739
|
+
"tenant_id",
|
|
740
|
+
"source_address",
|
|
741
|
+
"delivery_key"
|
|
742
|
+
]],
|
|
743
|
+
effect_agent_subscription_deliveries_pkey: ["p", [
|
|
744
|
+
"tenant_id",
|
|
745
|
+
"source_address",
|
|
746
|
+
"owner_id",
|
|
747
|
+
"subscription_id",
|
|
748
|
+
"event_id"
|
|
749
|
+
]]
|
|
750
|
+
}
|
|
751
|
+
},
|
|
752
|
+
effect_agent_subscription_events: {
|
|
753
|
+
columns: [
|
|
754
|
+
"tenant_id:text:true",
|
|
755
|
+
"source_address:text:true",
|
|
756
|
+
"event_id:text:true",
|
|
757
|
+
"source_name:text:true",
|
|
758
|
+
"source_version:text:true",
|
|
759
|
+
"matching_key:text:true",
|
|
760
|
+
"payload_digest:text:true",
|
|
761
|
+
"cutoff:bigint:true",
|
|
762
|
+
"cursor:bigint:true",
|
|
763
|
+
"routing_complete:bigint:true",
|
|
764
|
+
"tombstone:bigint:true",
|
|
765
|
+
"next_attempt_at_millis:bigint:true",
|
|
766
|
+
"record_json:text:true"
|
|
767
|
+
],
|
|
768
|
+
constraints: { effect_agent_subscription_events_pkey: ["p", [
|
|
769
|
+
"tenant_id",
|
|
770
|
+
"source_address",
|
|
771
|
+
"event_id"
|
|
772
|
+
]] }
|
|
773
|
+
},
|
|
774
|
+
effect_agent_subscription_sequences: {
|
|
775
|
+
columns: [
|
|
776
|
+
"tenant_id:text:true",
|
|
777
|
+
"source_address:text:true",
|
|
778
|
+
"sequence:bigint:true",
|
|
779
|
+
"event_scan_cursor:text:true",
|
|
780
|
+
"delivery_scan_cursor:text:true",
|
|
781
|
+
"recovery_scan_cursor:bigint:true"
|
|
782
|
+
],
|
|
783
|
+
constraints: { effect_agent_subscription_sequences_pkey: ["p", ["tenant_id", "source_address"]] }
|
|
784
|
+
},
|
|
785
|
+
effect_agent_subscriptions: {
|
|
786
|
+
columns: [
|
|
787
|
+
"tenant_id:text:true",
|
|
788
|
+
"source_address:text:true",
|
|
789
|
+
"owner_id:text:true",
|
|
790
|
+
"subscription_id:text:true",
|
|
791
|
+
"ordinal:bigint:true",
|
|
792
|
+
"source_name:text:true",
|
|
793
|
+
"source_version:text:true",
|
|
794
|
+
"matching_key:text:true",
|
|
795
|
+
"state:text:true",
|
|
796
|
+
"expires_at_millis:bigint:false",
|
|
797
|
+
"recovery_at_millis:bigint:false",
|
|
798
|
+
"recovery_present:bigint:true",
|
|
799
|
+
"record_json:text:true"
|
|
800
|
+
],
|
|
801
|
+
constraints: {
|
|
802
|
+
effect_agent_subscriptions_pkey: ["p", [
|
|
803
|
+
"tenant_id",
|
|
804
|
+
"source_address",
|
|
805
|
+
"owner_id",
|
|
806
|
+
"subscription_id"
|
|
807
|
+
]],
|
|
808
|
+
effect_agent_subscriptions_tenant_id_source_address_ordinal_key: ["u", [
|
|
809
|
+
"tenant_id",
|
|
810
|
+
"source_address",
|
|
811
|
+
"ordinal"
|
|
812
|
+
]]
|
|
813
|
+
}
|
|
814
|
+
},
|
|
815
|
+
effect_agent_rollover_buckets: {
|
|
816
|
+
columns: [
|
|
817
|
+
"thread_id:text:true",
|
|
818
|
+
"height:bigint:true",
|
|
819
|
+
"bucket:bigint:true",
|
|
820
|
+
"sequence:bigint:true"
|
|
821
|
+
],
|
|
822
|
+
constraints: {
|
|
823
|
+
effect_agent_rollover_buckets_pkey: ["p", [
|
|
824
|
+
"thread_id",
|
|
825
|
+
"height",
|
|
826
|
+
"bucket",
|
|
827
|
+
"sequence"
|
|
828
|
+
]],
|
|
829
|
+
effect_agent_rollover_buckets_thread_id_sequence_fkey: [
|
|
830
|
+
"f",
|
|
831
|
+
["thread_id", "sequence"],
|
|
832
|
+
"effect_agent_canonical_records",
|
|
833
|
+
["thread_id", "sequence"]
|
|
834
|
+
]
|
|
835
|
+
}
|
|
836
|
+
},
|
|
837
|
+
effect_agent_threads: {
|
|
838
|
+
columns: [
|
|
839
|
+
"thread_id:text:true",
|
|
840
|
+
"created_at:text:false",
|
|
841
|
+
"tail_sequence:bigint:true",
|
|
842
|
+
"work_version:bigint:true",
|
|
843
|
+
"work_state:text:true",
|
|
844
|
+
"work_through_sequence:bigint:true",
|
|
845
|
+
"work_reporting:bigint:true",
|
|
846
|
+
"work_entry_count:bigint:true",
|
|
847
|
+
"worker_input_sequence:bigint:false",
|
|
848
|
+
"worker_settlement_sequence:bigint:false"
|
|
849
|
+
],
|
|
850
|
+
constraints: { effect_agent_threads_pkey: ["p", ["thread_id"]] }
|
|
851
|
+
},
|
|
852
|
+
effect_agent_unknown_resolutions: {
|
|
853
|
+
columns: [
|
|
854
|
+
"submission_id:text:true",
|
|
855
|
+
"tool_call_id:text:true",
|
|
856
|
+
"resolution_kind:text:true",
|
|
857
|
+
"author:text:true",
|
|
858
|
+
"reason:text:true",
|
|
859
|
+
"resolution_json:text:true",
|
|
860
|
+
"resolved_at:text:true"
|
|
861
|
+
],
|
|
862
|
+
constraints: {
|
|
863
|
+
effect_agent_unknown_resolutions_pkey: ["p", [
|
|
864
|
+
"submission_id",
|
|
865
|
+
"tool_call_id",
|
|
866
|
+
"resolution_kind"
|
|
867
|
+
]],
|
|
868
|
+
effect_agent_unknown_resolutions_submission_id_fkey: [
|
|
869
|
+
"f",
|
|
870
|
+
["submission_id"],
|
|
871
|
+
"effect_agent_submissions",
|
|
872
|
+
["submission_id"]
|
|
873
|
+
]
|
|
874
|
+
}
|
|
875
|
+
},
|
|
876
|
+
effect_agent_import_delivery_visits: {
|
|
877
|
+
columns: [
|
|
878
|
+
"thread_id:text:true",
|
|
879
|
+
"message_id:text:true",
|
|
880
|
+
"path_id:text:true",
|
|
881
|
+
"completed:bigint:true"
|
|
882
|
+
],
|
|
883
|
+
constraints: { effect_agent_import_delivery_visits_pkey: ["p", ["thread_id", "message_id"]] }
|
|
884
|
+
},
|
|
885
|
+
effect_agent_worker_stops: {
|
|
886
|
+
columns: ["thread_id:text:true", "terminal:text:false"],
|
|
887
|
+
constraints: { effect_agent_worker_stops_pkey: ["p", ["thread_id"]] }
|
|
888
|
+
}
|
|
889
|
+
};
|
|
890
|
+
const layoutIndexes = {
|
|
891
|
+
effect_agent_tool_declarations_sequence: {
|
|
892
|
+
table: "effect_agent_tool_declarations",
|
|
893
|
+
columns: ["thread_id", "sequence"]
|
|
894
|
+
},
|
|
895
|
+
effect_agent_record_refusals_sequence: {
|
|
896
|
+
table: "effect_agent_record_refusals",
|
|
897
|
+
columns: ["thread_id", "sequence"]
|
|
898
|
+
},
|
|
899
|
+
effect_agent_live_child_reservations_thread: {
|
|
900
|
+
table: "effect_agent_live_child_reservations",
|
|
901
|
+
columns: ["thread_id", "reservation_id"]
|
|
902
|
+
},
|
|
903
|
+
effect_agent_canonical_batches_last: {
|
|
904
|
+
table: "effect_agent_canonical_batches",
|
|
905
|
+
columns: ["thread_id", "last_sequence"]
|
|
906
|
+
},
|
|
907
|
+
effect_agent_canonical_batches_sequence: {
|
|
908
|
+
table: "effect_agent_canonical_batches",
|
|
909
|
+
columns: ["thread_id", "first_sequence"],
|
|
910
|
+
unique: true
|
|
911
|
+
},
|
|
912
|
+
effect_agent_import_delivery_visits_path: {
|
|
913
|
+
table: "effect_agent_import_delivery_visits",
|
|
914
|
+
columns: ["thread_id", "path_id"]
|
|
915
|
+
},
|
|
916
|
+
effect_agent_records_application_input: {
|
|
917
|
+
table: "effect_agent_canonical_records",
|
|
918
|
+
columns: ["thread_id", "sequence"],
|
|
919
|
+
predicate: [
|
|
920
|
+
"eq",
|
|
921
|
+
["column", "application_input"],
|
|
922
|
+
["integer", 1]
|
|
923
|
+
]
|
|
924
|
+
},
|
|
925
|
+
effect_agent_records_prompt: {
|
|
926
|
+
table: "effect_agent_canonical_records",
|
|
927
|
+
columns: ["thread_id", "sequence"],
|
|
928
|
+
predicate: [
|
|
929
|
+
"in",
|
|
930
|
+
["column", "record_tag"],
|
|
931
|
+
PROMPT_EVIDENCE_TAGS.map((tag) => RECORD_TAGS[tag])
|
|
932
|
+
]
|
|
933
|
+
},
|
|
934
|
+
effect_agent_records_admitted_input: {
|
|
935
|
+
table: "effect_agent_canonical_records",
|
|
936
|
+
columns: ["thread_id", "sequence"],
|
|
937
|
+
predicate: ["and", [[
|
|
938
|
+
"eq",
|
|
939
|
+
["column", "record_tag"],
|
|
940
|
+
["integer", 30]
|
|
941
|
+
], ["notNull", ["column", "submission_id"]]]]
|
|
942
|
+
},
|
|
943
|
+
effect_agent_records_worker_funding: {
|
|
944
|
+
table: "effect_agent_canonical_records",
|
|
945
|
+
columns: [
|
|
946
|
+
"thread_id",
|
|
947
|
+
"source_submission_id",
|
|
948
|
+
"sequence",
|
|
949
|
+
"message_id"
|
|
950
|
+
],
|
|
951
|
+
predicate: [
|
|
952
|
+
"eq",
|
|
953
|
+
["column", "record_tag"],
|
|
954
|
+
["integer", 34]
|
|
955
|
+
]
|
|
956
|
+
},
|
|
957
|
+
effect_agent_records_worker_completed: {
|
|
958
|
+
table: "effect_agent_canonical_records",
|
|
959
|
+
columns: [
|
|
960
|
+
"thread_id",
|
|
961
|
+
"message_id",
|
|
962
|
+
"sequence"
|
|
963
|
+
],
|
|
964
|
+
predicate: [
|
|
965
|
+
"eq",
|
|
966
|
+
["column", "record_tag"],
|
|
967
|
+
["integer", 32]
|
|
968
|
+
]
|
|
969
|
+
},
|
|
970
|
+
effect_agent_records_worker_stop: {
|
|
971
|
+
table: "effect_agent_canonical_records",
|
|
972
|
+
columns: [
|
|
973
|
+
"thread_id",
|
|
974
|
+
"worker_thread_id",
|
|
975
|
+
"sequence"
|
|
976
|
+
],
|
|
977
|
+
predicate: [
|
|
978
|
+
"eq",
|
|
979
|
+
["column", "record_tag"],
|
|
980
|
+
["integer", 38]
|
|
981
|
+
]
|
|
982
|
+
},
|
|
983
|
+
effect_agent_message_deliveries_identity: {
|
|
984
|
+
table: "effect_agent_message_deliveries",
|
|
985
|
+
columns: ["owner_thread_id", null],
|
|
986
|
+
expressions: ["messageId"]
|
|
987
|
+
},
|
|
988
|
+
effect_agent_message_deliveries_peer_retained: {
|
|
989
|
+
table: "effect_agent_message_deliveries",
|
|
990
|
+
columns: [
|
|
991
|
+
"owner_thread_id",
|
|
992
|
+
"peer_retained_until_millis",
|
|
993
|
+
"message_id"
|
|
994
|
+
],
|
|
995
|
+
predicate: ["notNull", ["column", "peer_retained_until_millis"]]
|
|
996
|
+
},
|
|
997
|
+
effect_agent_message_deliveries_peer: {
|
|
998
|
+
table: "effect_agent_message_deliveries",
|
|
999
|
+
columns: ["owner_thread_id", "message_id"],
|
|
1000
|
+
predicate: ["and", [[
|
|
1001
|
+
"notIn",
|
|
1002
|
+
["column", "state"],
|
|
1003
|
+
["processed", "refused"]
|
|
1004
|
+
], [
|
|
1005
|
+
"eq",
|
|
1006
|
+
["json", "peer"],
|
|
1007
|
+
["text", "true"]
|
|
1008
|
+
]]]
|
|
1009
|
+
},
|
|
1010
|
+
effect_agent_records_tag: {
|
|
1011
|
+
table: "effect_agent_canonical_records",
|
|
1012
|
+
columns: [
|
|
1013
|
+
"thread_id",
|
|
1014
|
+
"record_tag",
|
|
1015
|
+
"sequence"
|
|
1016
|
+
]
|
|
1017
|
+
},
|
|
1018
|
+
effect_agent_records_handoff: {
|
|
1019
|
+
table: "effect_agent_canonical_records",
|
|
1020
|
+
columns: ["thread_id", "sequence"],
|
|
1021
|
+
predicate: [
|
|
1022
|
+
"eq",
|
|
1023
|
+
["column", "handoff"],
|
|
1024
|
+
["integer", 1]
|
|
1025
|
+
]
|
|
1026
|
+
},
|
|
1027
|
+
effect_agent_message_deliveries_due: {
|
|
1028
|
+
table: "effect_agent_message_deliveries",
|
|
1029
|
+
columns: [
|
|
1030
|
+
"deadline_at_millis",
|
|
1031
|
+
"owner_thread_id",
|
|
1032
|
+
"message_id"
|
|
1033
|
+
],
|
|
1034
|
+
predicate: ["notNull", ["column", "deadline_at_millis"]]
|
|
1035
|
+
},
|
|
1036
|
+
effect_agent_message_deliveries_capacity: {
|
|
1037
|
+
table: "effect_agent_message_deliveries",
|
|
1038
|
+
columns: [
|
|
1039
|
+
"owner_thread_id",
|
|
1040
|
+
null,
|
|
1041
|
+
"message_id"
|
|
1042
|
+
],
|
|
1043
|
+
expressions: ["workerUpdate"],
|
|
1044
|
+
predicate: [
|
|
1045
|
+
"in",
|
|
1046
|
+
["column", "state"],
|
|
1047
|
+
[
|
|
1048
|
+
"pending",
|
|
1049
|
+
"accepted",
|
|
1050
|
+
"parked"
|
|
1051
|
+
]
|
|
1052
|
+
]
|
|
1053
|
+
},
|
|
1054
|
+
effect_agent_message_deliveries_pending: {
|
|
1055
|
+
table: "effect_agent_message_deliveries",
|
|
1056
|
+
columns: ["owner_thread_id", "message_id"],
|
|
1057
|
+
predicate: [
|
|
1058
|
+
"notIn",
|
|
1059
|
+
["column", "state"],
|
|
1060
|
+
["processed", "refused"]
|
|
1061
|
+
]
|
|
1062
|
+
},
|
|
1063
|
+
effect_agent_records_call: {
|
|
1064
|
+
table: "effect_agent_canonical_records",
|
|
1065
|
+
columns: [
|
|
1066
|
+
"thread_id",
|
|
1067
|
+
"record_tag",
|
|
1068
|
+
"run_id",
|
|
1069
|
+
"tool_call_id"
|
|
1070
|
+
],
|
|
1071
|
+
predicate: ["notNull", ["column", "tool_call_id"]]
|
|
1072
|
+
},
|
|
1073
|
+
effect_agent_records_run_input: {
|
|
1074
|
+
table: "effect_agent_canonical_records",
|
|
1075
|
+
columns: ["thread_id", "run_id"],
|
|
1076
|
+
predicate: ["and", [[
|
|
1077
|
+
"eq",
|
|
1078
|
+
["column", "record_tag"],
|
|
1079
|
+
["integer", 30]
|
|
1080
|
+
], [
|
|
1081
|
+
"eq",
|
|
1082
|
+
["column", "input_kind"],
|
|
1083
|
+
["text", "user"]
|
|
1084
|
+
]]]
|
|
1085
|
+
},
|
|
1086
|
+
effect_agent_records_subtree: {
|
|
1087
|
+
table: "effect_agent_canonical_records",
|
|
1088
|
+
columns: [
|
|
1089
|
+
"thread_id",
|
|
1090
|
+
"source_submission_id",
|
|
1091
|
+
"sequence"
|
|
1092
|
+
],
|
|
1093
|
+
predicate: [
|
|
1094
|
+
"eq",
|
|
1095
|
+
["column", "record_tag"],
|
|
1096
|
+
["integer", 22]
|
|
1097
|
+
]
|
|
1098
|
+
},
|
|
1099
|
+
effect_agent_records_worker_input: {
|
|
1100
|
+
table: "effect_agent_canonical_records",
|
|
1101
|
+
columns: ["thread_id", "message_id"],
|
|
1102
|
+
predicate: [
|
|
1103
|
+
"eq",
|
|
1104
|
+
["column", "record_tag"],
|
|
1105
|
+
["integer", 34]
|
|
1106
|
+
]
|
|
1107
|
+
},
|
|
1108
|
+
effect_agent_schedules_deadline: {
|
|
1109
|
+
table: "effect_agent_schedules",
|
|
1110
|
+
columns: [
|
|
1111
|
+
"deadline_at_millis",
|
|
1112
|
+
"tenant_id",
|
|
1113
|
+
"owner_id",
|
|
1114
|
+
"schedule_id"
|
|
1115
|
+
],
|
|
1116
|
+
predicate: ["notNull", ["column", "deadline_at_millis"]]
|
|
1117
|
+
},
|
|
1118
|
+
effect_agent_schedules_owner_deadline: {
|
|
1119
|
+
table: "effect_agent_schedules",
|
|
1120
|
+
columns: [
|
|
1121
|
+
"tenant_id",
|
|
1122
|
+
"owner_id",
|
|
1123
|
+
"deadline_at_millis",
|
|
1124
|
+
"schedule_id"
|
|
1125
|
+
],
|
|
1126
|
+
predicate: ["notNull", ["column", "deadline_at_millis"]]
|
|
1127
|
+
},
|
|
1128
|
+
effect_agent_submissions_group: {
|
|
1129
|
+
table: "effect_agent_submissions",
|
|
1130
|
+
columns: ["thread_id", "admission_group"],
|
|
1131
|
+
predicate: ["and", [["notNull", ["column", "admission_group"]], [
|
|
1132
|
+
"ne",
|
|
1133
|
+
["column", "state"],
|
|
1134
|
+
["text", "settled"]
|
|
1135
|
+
]]]
|
|
1136
|
+
},
|
|
1137
|
+
effect_agent_submissions_joined_host: {
|
|
1138
|
+
table: "effect_agent_submissions",
|
|
1139
|
+
columns: ["joined_host_submission_id"],
|
|
1140
|
+
predicate: ["notNull", ["column", "joined_host_submission_id"]]
|
|
1141
|
+
},
|
|
1142
|
+
effect_agent_submissions_nonterminal: {
|
|
1143
|
+
table: "effect_agent_submissions",
|
|
1144
|
+
columns: ["thread_id", "queue_sequence"],
|
|
1145
|
+
predicate: [
|
|
1146
|
+
"ne",
|
|
1147
|
+
["column", "state"],
|
|
1148
|
+
["text", "settled"]
|
|
1149
|
+
]
|
|
1150
|
+
},
|
|
1151
|
+
effect_agent_submissions_parent: {
|
|
1152
|
+
table: "effect_agent_submissions",
|
|
1153
|
+
columns: ["parent_submission_id"],
|
|
1154
|
+
predicate: ["notNull", ["column", "parent_submission_id"]]
|
|
1155
|
+
},
|
|
1156
|
+
effect_agent_subscription_deliveries_pending: {
|
|
1157
|
+
table: "effect_agent_subscription_deliveries",
|
|
1158
|
+
columns: [
|
|
1159
|
+
"tenant_id",
|
|
1160
|
+
"source_address",
|
|
1161
|
+
"state",
|
|
1162
|
+
"next_attempt_at_millis",
|
|
1163
|
+
"delivery_key"
|
|
1164
|
+
]
|
|
1165
|
+
},
|
|
1166
|
+
effect_agent_subscription_deliveries_capacity: {
|
|
1167
|
+
table: "effect_agent_subscription_deliveries",
|
|
1168
|
+
columns: [
|
|
1169
|
+
"tenant_id",
|
|
1170
|
+
"source_address",
|
|
1171
|
+
"owner_id"
|
|
1172
|
+
],
|
|
1173
|
+
predicate: [
|
|
1174
|
+
"in",
|
|
1175
|
+
["column", "state"],
|
|
1176
|
+
["selected", "prepared"]
|
|
1177
|
+
]
|
|
1178
|
+
},
|
|
1179
|
+
effect_agent_subscription_deliveries_observing: {
|
|
1180
|
+
table: "effect_agent_subscription_deliveries",
|
|
1181
|
+
columns: [
|
|
1182
|
+
"tenant_id",
|
|
1183
|
+
"source_address",
|
|
1184
|
+
"owner_id"
|
|
1185
|
+
],
|
|
1186
|
+
predicate: ["and", [[
|
|
1187
|
+
"eq",
|
|
1188
|
+
["column", "state"],
|
|
1189
|
+
["text", "delivered"]
|
|
1190
|
+
], ["column", "observe_settlement"]]]
|
|
1191
|
+
},
|
|
1192
|
+
effect_agent_subscription_deliveries_registration: {
|
|
1193
|
+
table: "effect_agent_subscription_deliveries",
|
|
1194
|
+
columns: [
|
|
1195
|
+
"tenant_id",
|
|
1196
|
+
"source_address",
|
|
1197
|
+
"owner_id",
|
|
1198
|
+
"subscription_id",
|
|
1199
|
+
"delivery_key"
|
|
1200
|
+
]
|
|
1201
|
+
},
|
|
1202
|
+
effect_agent_subscription_events_pending: {
|
|
1203
|
+
table: "effect_agent_subscription_events",
|
|
1204
|
+
columns: [
|
|
1205
|
+
"tenant_id",
|
|
1206
|
+
"source_address",
|
|
1207
|
+
"routing_complete",
|
|
1208
|
+
"next_attempt_at_millis",
|
|
1209
|
+
"event_id"
|
|
1210
|
+
]
|
|
1211
|
+
},
|
|
1212
|
+
effect_agent_subscriptions_candidates: {
|
|
1213
|
+
table: "effect_agent_subscriptions",
|
|
1214
|
+
columns: [
|
|
1215
|
+
"tenant_id",
|
|
1216
|
+
"source_address",
|
|
1217
|
+
"source_name",
|
|
1218
|
+
"source_version",
|
|
1219
|
+
"matching_key",
|
|
1220
|
+
"ordinal"
|
|
1221
|
+
]
|
|
1222
|
+
},
|
|
1223
|
+
effect_agent_subscriptions_owner: {
|
|
1224
|
+
table: "effect_agent_subscriptions",
|
|
1225
|
+
columns: [
|
|
1226
|
+
"tenant_id",
|
|
1227
|
+
"source_address",
|
|
1228
|
+
"owner_id",
|
|
1229
|
+
"ordinal"
|
|
1230
|
+
]
|
|
1231
|
+
},
|
|
1232
|
+
effect_agent_subscriptions_capacity: {
|
|
1233
|
+
table: "effect_agent_subscriptions",
|
|
1234
|
+
columns: [
|
|
1235
|
+
"tenant_id",
|
|
1236
|
+
"source_address",
|
|
1237
|
+
"expires_at_millis"
|
|
1238
|
+
],
|
|
1239
|
+
predicate: [
|
|
1240
|
+
"in",
|
|
1241
|
+
["column", "state"],
|
|
1242
|
+
["active", "paused"]
|
|
1243
|
+
]
|
|
1244
|
+
},
|
|
1245
|
+
effect_agent_subscriptions_owner_capacity: {
|
|
1246
|
+
table: "effect_agent_subscriptions",
|
|
1247
|
+
columns: [
|
|
1248
|
+
"tenant_id",
|
|
1249
|
+
"source_address",
|
|
1250
|
+
"owner_id",
|
|
1251
|
+
"expires_at_millis"
|
|
1252
|
+
],
|
|
1253
|
+
predicate: [
|
|
1254
|
+
"in",
|
|
1255
|
+
["column", "state"],
|
|
1256
|
+
["active", "paused"]
|
|
1257
|
+
]
|
|
1258
|
+
},
|
|
1259
|
+
effect_agent_subscriptions_recovery: {
|
|
1260
|
+
table: "effect_agent_subscriptions",
|
|
1261
|
+
columns: [
|
|
1262
|
+
"tenant_id",
|
|
1263
|
+
"source_address",
|
|
1264
|
+
"recovery_at_millis",
|
|
1265
|
+
"ordinal"
|
|
1266
|
+
],
|
|
1267
|
+
predicate: ["notNull", ["column", "recovery_at_millis"]]
|
|
1268
|
+
},
|
|
1269
|
+
effect_agent_worker_pending: {
|
|
1270
|
+
table: "effect_agent_message_deliveries",
|
|
1271
|
+
columns: [
|
|
1272
|
+
"owner_thread_id",
|
|
1273
|
+
null,
|
|
1274
|
+
"message_id"
|
|
1275
|
+
],
|
|
1276
|
+
expressions: ["threadId"],
|
|
1277
|
+
predicate: ["and", [[
|
|
1278
|
+
"in",
|
|
1279
|
+
["column", "state"],
|
|
1280
|
+
["pending", "parked"]
|
|
1281
|
+
], [
|
|
1282
|
+
"eq",
|
|
1283
|
+
["json", "hasReceipt"],
|
|
1284
|
+
["text", "false"]
|
|
1285
|
+
]]]
|
|
1286
|
+
},
|
|
1287
|
+
effect_agent_worker_starts: {
|
|
1288
|
+
table: "effect_agent_message_deliveries",
|
|
1289
|
+
columns: [
|
|
1290
|
+
"owner_thread_id",
|
|
1291
|
+
null,
|
|
1292
|
+
null,
|
|
1293
|
+
"message_id"
|
|
1294
|
+
],
|
|
1295
|
+
expressions: ["delegationId", "targetAgentId"],
|
|
1296
|
+
predicate: [
|
|
1297
|
+
"eq",
|
|
1298
|
+
["json", "workerStart"],
|
|
1299
|
+
["text", "true"]
|
|
1300
|
+
]
|
|
1301
|
+
}
|
|
1302
|
+
};
|
|
1303
|
+
const headerStatement = "CREATE TABLE __NAMESPACE__.effect_agent_schema (singleton BIGINT PRIMARY KEY NOT NULL CHECK (singleton = 1), layout_version BIGINT NOT NULL CHECK (layout_version > 0), record_format TEXT NOT NULL CHECK (length(record_format) > 0))";
|
|
1304
|
+
const quote = (namespace) => `"${namespace.replaceAll("\"", "\"\"")}"`;
|
|
1305
|
+
const qualify = (statement, namespace) => statement.replaceAll("__NAMESPACE__", quote(namespace));
|
|
1306
|
+
const NameRows = Schema.Array(Schema.Struct({
|
|
1307
|
+
name: Schema.String,
|
|
1308
|
+
kind: Schema.String
|
|
1309
|
+
}));
|
|
1310
|
+
const ColumnRows = Schema.Array(Schema.Struct({
|
|
1311
|
+
name: Schema.String,
|
|
1312
|
+
column_name: Schema.String,
|
|
1313
|
+
type: Schema.String,
|
|
1314
|
+
not_null: Schema.Boolean,
|
|
1315
|
+
collation: Schema.NullOr(Schema.String)
|
|
1316
|
+
}));
|
|
1317
|
+
const ConstraintRows = Schema.Array(Schema.Struct({
|
|
1318
|
+
table_name: Schema.String,
|
|
1319
|
+
name: Schema.String,
|
|
1320
|
+
kind: Schema.String,
|
|
1321
|
+
columns: Schema.Array(Schema.String),
|
|
1322
|
+
target_table: Schema.NullOr(Schema.String),
|
|
1323
|
+
target_schema: Schema.NullOr(Schema.String),
|
|
1324
|
+
target_columns: Schema.Array(Schema.String),
|
|
1325
|
+
expression: Schema.NullOr(Schema.String),
|
|
1326
|
+
enforcement_ok: Schema.Boolean
|
|
1327
|
+
}));
|
|
1328
|
+
const IndexRows = Schema.Array(Schema.Struct({
|
|
1329
|
+
name: Schema.String,
|
|
1330
|
+
table_name: Schema.String,
|
|
1331
|
+
columns: Schema.Array(Schema.NullOr(Schema.String)),
|
|
1332
|
+
is_unique: Schema.Boolean,
|
|
1333
|
+
is_primary: Schema.Boolean,
|
|
1334
|
+
expressions: Schema.NullOr(Schema.String),
|
|
1335
|
+
predicate: Schema.NullOr(Schema.String),
|
|
1336
|
+
structure_ok: Schema.Boolean
|
|
1337
|
+
}));
|
|
1338
|
+
const sameIndexColumns = Schema.toEquivalence(Schema.Array(Schema.NullOr(Schema.String)));
|
|
1339
|
+
const matchesIndex = (actual, expected) => actual !== void 0 && actual.structure_ok && actual.table_name === expected.table && actual.is_unique === (expected.unique ?? false) && actual.is_primary === (expected.primary ?? false) && sameIndexColumns(actual.columns, expected.columns) && matchesLayoutExpressions(actual.expressions, (expected.expressions ?? []).map((key) => typeof key === "string" ? ["json", key] : key)) && matchesLayoutExpressions(actual.predicate, expected.predicate === void 0 ? [] : [expected.predicate]);
|
|
1340
|
+
const sameStrings = Schema.toEquivalence(Schema.Array(Schema.String));
|
|
1341
|
+
const Header = Schema.Tuple([Schema.Struct({
|
|
1342
|
+
singleton: SqlInteger,
|
|
1343
|
+
layout_version: SqlInteger,
|
|
1344
|
+
record_format: Schema.NonEmptyString
|
|
1345
|
+
})]);
|
|
1346
|
+
const Legacy = Schema.Tuple([Schema.Struct({
|
|
1347
|
+
id: Schema.Literal(true),
|
|
1348
|
+
version: SqlInteger
|
|
1349
|
+
})]);
|
|
1350
|
+
const incompatible = (actualVersion, message) => PostgresStorageCompatibilityError.make({
|
|
1351
|
+
actualVersion,
|
|
1352
|
+
supportedVersion: 31,
|
|
1353
|
+
message: `${message} Keep the original database; the store was not changed.`
|
|
1354
|
+
});
|
|
1355
|
+
const decode = (schema, value, table) => Schema.decodeUnknownEffect(schema)(value, { onExcessProperty: "error" }).pipe(Effect.mapError(() => PostgresStorageCorruptionError.make({
|
|
1356
|
+
table,
|
|
1357
|
+
rowKey: "schema",
|
|
1358
|
+
message: "Malformed storage layout or version header."
|
|
1359
|
+
})));
|
|
1360
|
+
/** No DDL or transaction acquisition. The caller owns the snapshot/writer lock across subsequent work. */
|
|
1361
|
+
const inspectPostgresStorage = Effect.fnUntraced(function* (namespace, allowReleasedUpgrade = false) {
|
|
1362
|
+
const sql = yield* SqlClient.SqlClient;
|
|
1363
|
+
const { table, execute } = yield* makeSqlQuery(namespace);
|
|
1364
|
+
const names = yield* decode(NameRows, yield* execute(sql`
|
|
1365
|
+
SELECT c.relname AS name, c.relkind::text AS kind FROM pg_class c JOIN pg_namespace n ON n.oid=c.relnamespace
|
|
1366
|
+
WHERE n.nspname=${namespace} AND starts_with(c.relname, 'effect_agent_')
|
|
1367
|
+
`), "pg_class");
|
|
1368
|
+
const versionTable = names.find((row) => row.name === "effect_agent_storage_version");
|
|
1369
|
+
const schemaTable = names.find((row) => row.name === "effect_agent_schema");
|
|
1370
|
+
if (versionTable === void 0) {
|
|
1371
|
+
const independent = /* @__PURE__ */ new Set([
|
|
1372
|
+
"effect_agent_activity_metadata",
|
|
1373
|
+
"effect_agent_activity_metadata_pkey",
|
|
1374
|
+
"effect_agent_activity_processor_state_v2",
|
|
1375
|
+
"effect_agent_activity_processor_state_v2_pkey"
|
|
1376
|
+
]);
|
|
1377
|
+
if (names.every((row) => independent.has(row.name))) return void 0;
|
|
1378
|
+
return yield* incompatible(0, "Unversioned or incomplete Effect Agent storage.");
|
|
1379
|
+
}
|
|
1380
|
+
if (versionTable.kind !== "r") return yield* incompatible(0, "Malformed version table.");
|
|
1381
|
+
const [legacy] = yield* decode(Legacy, yield* execute(sql`SELECT * FROM ${table("effect_agent_storage_version")}`), "effect_agent_storage_version");
|
|
1382
|
+
if (legacy.version !== 31 && !(allowReleasedUpgrade && (legacy.version === 25 || legacy.version === 26 || legacy.version === 27 || legacy.version === 28 || legacy.version === 29 || legacy.version === 30))) return yield* incompatible(legacy.version, `Unsupported storage version ${legacy.version}.`);
|
|
1383
|
+
if (schemaTable?.kind !== "r") return yield* incompatible(legacy.version, "Missing or malformed singleton schema table.");
|
|
1384
|
+
const [row] = yield* decode(Header, yield* execute(sql`SELECT * FROM ${table("effect_agent_schema")}`), "effect_agent_schema");
|
|
1385
|
+
if (row.singleton !== 1 || row.layout_version !== legacy.version) return yield* incompatible(row.layout_version, "Unsupported or conflicting layout headers.");
|
|
1386
|
+
const header = {
|
|
1387
|
+
layoutVersion: row.layout_version,
|
|
1388
|
+
recordFormat: row.record_format
|
|
1389
|
+
};
|
|
1390
|
+
if (header.recordFormat !== CURRENT_RECORD_FORMAT) return yield* incompatible(header.layoutVersion, `Unsupported record format ${header.recordFormat}.`);
|
|
1391
|
+
if (legacy.version === 25 && names.some((row) => Object.hasOwn(layout26IndexStatements, row.name))) return yield* incompatible(25, "Layout 25 contains layout 26 indexes.");
|
|
1392
|
+
const columns = yield* decode(ColumnRows, yield* execute(sql`
|
|
1393
|
+
SELECT c.relname AS name, a.attname AS column_name,
|
|
1394
|
+
CASE WHEN a.atttypmod <> -1 THEN ''
|
|
1395
|
+
WHEN a.atttypid='pg_catalog.int8'::regtype THEN 'bigint'
|
|
1396
|
+
WHEN a.atttypid='pg_catalog.bool'::regtype THEN 'boolean'
|
|
1397
|
+
WHEN a.atttypid='pg_catalog.text'::regtype THEN 'text'
|
|
1398
|
+
WHEN a.atttypid='pg_catalog.jsonb'::regtype THEN 'jsonb' ELSE '' END AS type,
|
|
1399
|
+
a.attnotnull AS not_null,
|
|
1400
|
+
cn.nspname || '.' || co.collname AS collation
|
|
1401
|
+
FROM pg_class c JOIN pg_namespace n ON n.oid=c.relnamespace JOIN pg_attribute a ON a.attrelid=c.oid
|
|
1402
|
+
LEFT JOIN pg_collation co ON co.oid=a.attcollation LEFT JOIN pg_namespace cn ON cn.oid=co.collnamespace
|
|
1403
|
+
WHERE n.nspname=${namespace} AND starts_with(c.relname, 'effect_agent_') AND c.relkind='r' AND a.attnum>0 AND NOT a.attisdropped
|
|
1404
|
+
ORDER BY c.relname,a.attnum
|
|
1405
|
+
`), "pg_attribute");
|
|
1406
|
+
const constraints = yield* decode(ConstraintRows, yield* execute(sql`
|
|
1407
|
+
SELECT c.relname AS table_name, k.conname AS name, k.contype::text AS kind,
|
|
1408
|
+
to_jsonb(ARRAY(SELECT a.attname FROM unnest(k.conkey) WITH ORDINALITY v(attnum, position)
|
|
1409
|
+
JOIN pg_attribute a ON a.attrelid=k.conrelid AND a.attnum=v.attnum ORDER BY v.position)) AS columns,
|
|
1410
|
+
r.relname AS target_table, rn.nspname AS target_schema,
|
|
1411
|
+
to_jsonb(ARRAY(SELECT a.attname FROM unnest(k.confkey) WITH ORDINALITY v(attnum, position)
|
|
1412
|
+
JOIN pg_attribute a ON a.attrelid=k.confrelid AND a.attnum=v.attnum ORDER BY v.position)) AS target_columns,
|
|
1413
|
+
pg_get_expr(k.conbin, k.conrelid, false) AS expression,
|
|
1414
|
+
(k.convalidated AND NOT k.condeferrable AND NOT k.condeferred
|
|
1415
|
+
AND k.conislocal AND k.coninhcount=0 AND k.conparentid=0
|
|
1416
|
+
AND (k.contype<>'c' OR NOT k.connoinherit)
|
|
1417
|
+
AND NOT EXISTS (SELECT 1 FROM pg_depend d WHERE d.classid='pg_constraint'::regclass
|
|
1418
|
+
AND d.objid=k.oid AND d.refclassid IN ('pg_proc'::regclass, 'pg_operator'::regclass))
|
|
1419
|
+
AND (k.contype NOT IN ('p','u') OR ic.relname=k.conname)
|
|
1420
|
+
AND (k.contype<>'f' OR (k.confupdtype='a' AND k.confdeltype='r' AND k.confmatchtype='s'
|
|
1421
|
+
AND k.conpfeqop <@ ARRAY['pg_catalog.=(text,text)'::regoperator, 'pg_catalog.=(bigint,bigint)'::regoperator]::oid[]
|
|
1422
|
+
AND k.conppeqop <@ ARRAY['pg_catalog.=(text,text)'::regoperator, 'pg_catalog.=(bigint,bigint)'::regoperator]::oid[]
|
|
1423
|
+
AND k.conffeqop <@ ARRAY['pg_catalog.=(text,text)'::regoperator, 'pg_catalog.=(bigint,bigint)'::regoperator]::oid[]))) AS enforcement_ok
|
|
1424
|
+
FROM pg_constraint k JOIN pg_class c ON c.oid=k.conrelid
|
|
1425
|
+
JOIN pg_namespace n ON n.oid=c.relnamespace
|
|
1426
|
+
LEFT JOIN pg_class r ON r.oid=k.confrelid LEFT JOIN pg_namespace rn ON rn.oid=r.relnamespace
|
|
1427
|
+
LEFT JOIN pg_class ic ON ic.oid=k.conindid
|
|
1428
|
+
WHERE n.nspname=${namespace} AND k.contype IN ('p','u','f','c','x')
|
|
1429
|
+
`), "pg_constraint");
|
|
1430
|
+
const indexes = yield* decode(IndexRows, yield* execute(sql`
|
|
1431
|
+
SELECT ic.relname AS name, c.relname AS table_name,
|
|
1432
|
+
to_jsonb(ARRAY(SELECT a.attname FROM unnest(i.indkey) WITH ORDINALITY v(attnum, position)
|
|
1433
|
+
LEFT JOIN pg_attribute a ON a.attrelid=i.indrelid AND a.attnum=v.attnum ORDER BY v.position)) AS columns,
|
|
1434
|
+
i.indisunique AS is_unique, i.indisprimary AS is_primary,
|
|
1435
|
+
pg_get_expr(i.indexprs, i.indrelid, false) AS expressions,
|
|
1436
|
+
pg_get_expr(i.indpred, i.indrelid, false) AS predicate,
|
|
1437
|
+
(ic.relkind='i' AND am.amname='btree' AND i.indisvalid AND i.indisready AND i.indislive
|
|
1438
|
+
AND NOT i.indisexclusion AND i.indimmediate AND NOT i.indnullsnotdistinct
|
|
1439
|
+
AND NOT EXISTS (SELECT 1 FROM pg_depend d WHERE d.classid='pg_class'::regclass
|
|
1440
|
+
AND d.objid=i.indexrelid AND d.refclassid IN ('pg_proc'::regclass, 'pg_operator'::regclass))
|
|
1441
|
+
AND i.indnatts=i.indnkeyatts AND NOT EXISTS (
|
|
1442
|
+
SELECT 1 FROM unnest(i.indkey, i.indcollation, i.indclass, i.indoption)
|
|
1443
|
+
WITH ORDINALITY v(attnum, collation_oid, opclass, options, position)
|
|
1444
|
+
JOIN pg_attribute ia ON ia.attrelid=i.indexrelid AND ia.attnum=v.position
|
|
1445
|
+
LEFT JOIN pg_attribute a ON a.attrelid=i.indrelid AND a.attnum=v.attnum
|
|
1446
|
+
JOIN pg_opclass op ON op.oid=v.opclass
|
|
1447
|
+
WHERE v.options<>0 OR NOT op.opcdefault OR op.opcnamespace<>'pg_catalog'::regnamespace
|
|
1448
|
+
OR op.opcintype<>ia.atttypid OR op.opcmethod<>ic.relam
|
|
1449
|
+
OR v.collation_oid<>CASE WHEN v.attnum=0 THEN
|
|
1450
|
+
CASE WHEN ia.atttypid='pg_catalog.text'::regtype THEN 'pg_catalog.default'::regcollation::oid ELSE 0::oid END
|
|
1451
|
+
ELSE a.attcollation END
|
|
1452
|
+
)) AS structure_ok
|
|
1453
|
+
FROM pg_index i JOIN pg_class ic ON ic.oid=i.indexrelid JOIN pg_class c ON c.oid=i.indrelid
|
|
1454
|
+
JOIN pg_namespace n ON n.oid=c.relnamespace JOIN pg_am am ON am.oid=ic.relam
|
|
1455
|
+
WHERE n.nspname=${namespace}
|
|
1456
|
+
`), "pg_index");
|
|
1457
|
+
const expectedShape = {
|
|
1458
|
+
...layoutShape,
|
|
1459
|
+
effect_agent_schema: {
|
|
1460
|
+
columns: [
|
|
1461
|
+
"singleton:bigint:true",
|
|
1462
|
+
"layout_version:bigint:true",
|
|
1463
|
+
"record_format:text:true"
|
|
1464
|
+
],
|
|
1465
|
+
constraints: {
|
|
1466
|
+
effect_agent_schema_pkey: ["p", ["singleton"]],
|
|
1467
|
+
effect_agent_schema_singleton_check: [
|
|
1468
|
+
"c",
|
|
1469
|
+
["singleton"],
|
|
1470
|
+
[
|
|
1471
|
+
"eq",
|
|
1472
|
+
["column", "singleton"],
|
|
1473
|
+
["integer", 1]
|
|
1474
|
+
]
|
|
1475
|
+
],
|
|
1476
|
+
effect_agent_schema_layout_version_check: [
|
|
1477
|
+
"c",
|
|
1478
|
+
["layout_version"],
|
|
1479
|
+
[
|
|
1480
|
+
"gt",
|
|
1481
|
+
["column", "layout_version"],
|
|
1482
|
+
["integer", 0]
|
|
1483
|
+
]
|
|
1484
|
+
],
|
|
1485
|
+
effect_agent_schema_record_format_check: [
|
|
1486
|
+
"c",
|
|
1487
|
+
["record_format"],
|
|
1488
|
+
[
|
|
1489
|
+
"gt",
|
|
1490
|
+
["length", "record_format"],
|
|
1491
|
+
["integer", 0]
|
|
1492
|
+
]
|
|
1493
|
+
]
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
};
|
|
1497
|
+
for (const [name, expected] of Object.entries(expectedShape)) {
|
|
1498
|
+
if (name === "effect_agent_checkpoints" && legacy.version >= 29) {
|
|
1499
|
+
if (names.some((row) => row.name === name)) return yield* incompatible(legacy.version, "Current layout contains retired generic checkpoints.");
|
|
1500
|
+
continue;
|
|
1501
|
+
}
|
|
1502
|
+
const tableColumns = columns.filter((row) => row.name === name);
|
|
1503
|
+
const actualConstraints = constraints.filter((row) => row.table_name === name);
|
|
1504
|
+
const expectedColumns = legacy.version === 25 && name === "effect_agent_message_deliveries" ? expected.columns.filter((column) => column !== "peer_retained_until_millis:bigint:false") : expected.columns;
|
|
1505
|
+
if (names.find((row) => row.name === name)?.kind !== "r" || !sameStrings(tableColumns.map((row) => `${row.column_name}:${row.type}:${row.not_null}`).sort(), [...expectedColumns].sort()) || tableColumns.some((row) => row.collation !== (row.type === "text" ? name === "effect_agent_schema" ? "pg_catalog.default" : "pg_catalog.C" : null)) || actualConstraints.length !== Object.keys(expected.constraints).length) return yield* incompatible(legacy.version, `Missing or incompatible layout table ${name}.`);
|
|
1506
|
+
for (const [constraintName, constraint] of Object.entries(expected.constraints)) {
|
|
1507
|
+
const actual = actualConstraints.find((row) => row.name === constraintName);
|
|
1508
|
+
const [kind, keyColumns] = constraint;
|
|
1509
|
+
if (actual === void 0 || !actual.enforcement_ok || actual.kind !== kind || !sameStrings(actual.columns, keyColumns) || actual.target_table !== (constraint[0] === "f" ? constraint[2] : null) || actual.target_schema !== (kind === "f" ? namespace : null) || !sameStrings(actual.target_columns, constraint[0] === "f" ? constraint[3] : []) || !matchesLayoutExpressions(actual.expression, constraint[0] === "c" ? [constraint[2]] : [])) return yield* incompatible(legacy.version, `Missing or incompatible layout constraint ${constraintName}.`);
|
|
1510
|
+
if ((kind === "p" || kind === "u") && !matchesIndex(indexes.find((row) => row.name === constraintName), {
|
|
1511
|
+
table: name,
|
|
1512
|
+
columns: keyColumns,
|
|
1513
|
+
unique: true,
|
|
1514
|
+
primary: kind === "p"
|
|
1515
|
+
})) return yield* incompatible(legacy.version, `Missing or incompatible layout index ${constraintName}.`);
|
|
1516
|
+
}
|
|
1517
|
+
}
|
|
1518
|
+
for (const [name, expected] of Object.entries(layoutIndexes)) {
|
|
1519
|
+
if (legacy.version === 25 && Object.hasOwn(layout26IndexStatements, name)) continue;
|
|
1520
|
+
if (name === "effect_agent_canonical_batches_last" && legacy.version >= 28) {
|
|
1521
|
+
if (indexes.some((row) => row.name === name)) return yield* incompatible(legacy.version, `Layout ${legacy.version} contains the retired batch-end index.`);
|
|
1522
|
+
continue;
|
|
1523
|
+
}
|
|
1524
|
+
if (!matchesIndex(indexes.find((row) => row.name === name), expected)) return yield* incompatible(legacy.version, `Missing or incompatible layout index ${name}.`);
|
|
1525
|
+
}
|
|
1526
|
+
const functions = yield* decode(Schema.Array(Schema.Struct({
|
|
1527
|
+
source: Schema.String,
|
|
1528
|
+
safe: Schema.Boolean
|
|
1529
|
+
})), yield* execute(sql`
|
|
1530
|
+
SELECT p.prosrc AS source, (l.lanname='plpgsql' AND p.prorettype='pg_catalog.trigger'::regtype
|
|
1531
|
+
AND p.pronargs=0 AND NOT p.prosecdef AND p.proconfig IS NULL AND p.prokind='f') AS safe
|
|
1532
|
+
FROM pg_proc p JOIN pg_namespace n ON n.oid=p.pronamespace JOIN pg_language l ON l.oid=p.prolang
|
|
1533
|
+
WHERE n.nspname=${namespace} AND p.proname='effect_agent_live_child_mutation'`), "pg_proc");
|
|
1534
|
+
if (functions.length !== 1 || !functions[0]?.safe || functions[0].source !== qualify(liveChildFunctionBody, namespace)) return yield* incompatible(legacy.version, "Missing or incompatible live child trigger function.");
|
|
1535
|
+
const triggers = yield* decode(Schema.Array(Schema.Struct({
|
|
1536
|
+
name: Schema.String,
|
|
1537
|
+
table_name: Schema.String,
|
|
1538
|
+
type: SqlInteger,
|
|
1539
|
+
enabled: Schema.String,
|
|
1540
|
+
function_name: Schema.String,
|
|
1541
|
+
function_schema: Schema.String,
|
|
1542
|
+
arguments: Schema.String,
|
|
1543
|
+
safe: Schema.Boolean
|
|
1544
|
+
})), yield* execute(sql`
|
|
1545
|
+
SELECT t.tgname AS name, c.relname AS table_name, t.tgtype::bigint AS type,
|
|
1546
|
+
t.tgenabled::text AS enabled, p.proname AS function_name, pn.nspname AS function_schema,
|
|
1547
|
+
encode(t.tgargs, 'hex') AS arguments,
|
|
1548
|
+
(NOT t.tgisinternal AND t.tgconstraint=0 AND NOT t.tgdeferrable AND NOT t.tginitdeferred
|
|
1549
|
+
AND t.tgqual IS NULL AND t.tgnargs=0 AND t.tgattr=''::int2vector) AS safe
|
|
1550
|
+
FROM pg_trigger t JOIN pg_class c ON c.oid=t.tgrelid JOIN pg_namespace n ON n.oid=c.relnamespace
|
|
1551
|
+
JOIN pg_proc p ON p.oid=t.tgfoid JOIN pg_namespace pn ON pn.oid=p.pronamespace
|
|
1552
|
+
WHERE n.nspname=${namespace} AND (
|
|
1553
|
+
starts_with(t.tgname, 'effect_agent_live_child_') OR (
|
|
1554
|
+
${legacy.version < 31} AND NOT t.tgisinternal
|
|
1555
|
+
AND c.relname IN ${sql.in(Object.keys(expectedShape))}
|
|
1556
|
+
)
|
|
1557
|
+
)`), "pg_trigger");
|
|
1558
|
+
for (const expected of liveChildTriggers) {
|
|
1559
|
+
const actual = triggers.find((trigger) => trigger.name === expected.name);
|
|
1560
|
+
if (actual === void 0 || !actual.safe || actual.table_name !== expected.table || actual.type !== (expected.event === "INSERT" ? 5 : expected.event === "UPDATE" ? 17 : 9) || actual.enabled !== "O" || actual.function_name !== "effect_agent_live_child_mutation" || actual.function_schema !== namespace || actual.arguments !== "") return yield* incompatible(legacy.version, `Missing or incompatible live child trigger ${expected.name}.`);
|
|
1561
|
+
}
|
|
1562
|
+
if (triggers.length !== liveChildTriggers.length) return yield* incompatible(legacy.version, "Unexpected Thread storage triggers.");
|
|
1563
|
+
return header;
|
|
1564
|
+
});
|
|
1565
|
+
/** Export opening never creates a schema, runs layout steps, or initializes a journal. */
|
|
1566
|
+
const readPostgresStorageHeader = Effect.fnUntraced(function* (namespace) {
|
|
1567
|
+
const header = yield* inspectPostgresStorage(namespace);
|
|
1568
|
+
if (header === void 0) return yield* incompatible(0, "No initialized Thread storage to export.");
|
|
1569
|
+
return header;
|
|
1570
|
+
});
|
|
1571
|
+
/** The caller owns one writer transaction encompassing inspection, DDL, backfill, and both headers. */
|
|
1572
|
+
const applyPostgresLayout = Effect.fnUntraced(function* (namespace, header) {
|
|
1573
|
+
const sql = yield* SqlClient.SqlClient;
|
|
1574
|
+
const { table, execute } = yield* makeSqlQuery(namespace);
|
|
1575
|
+
if (header !== void 0) {
|
|
1576
|
+
if (header.layoutVersion === 31) return header;
|
|
1577
|
+
if (header.layoutVersion !== 25 && header.layoutVersion !== 26 && header.layoutVersion !== 27 && header.layoutVersion !== 28 && header.layoutVersion !== 29 && header.layoutVersion !== 30) return yield* incompatible(header.layoutVersion, "Unsupported layout upgrade.");
|
|
1578
|
+
if (header.layoutVersion < 30) yield* assertLegacyMessagePublicationDrained(namespace).pipe(Effect.mapError((cause) => cause.reason === "conflict" ? incompatible(header.layoutVersion, "Recover work discovery and drain peer, report and update publication using the previous library version before upgrading.") : PostgresStorageError.make({
|
|
1579
|
+
operation: cause.operation,
|
|
1580
|
+
message: cause.message,
|
|
1581
|
+
cause
|
|
1582
|
+
})));
|
|
1583
|
+
yield* upgradeSqlThreadWorkIndex(namespace).pipe(Effect.mapError((cause) => PostgresStorageError.make({
|
|
1584
|
+
operation: cause.operation,
|
|
1585
|
+
message: cause.message,
|
|
1586
|
+
cause
|
|
1587
|
+
})));
|
|
1588
|
+
if (header.layoutVersion < 27) yield* execute(sql`LOCK TABLE ${table("effect_agent_executions")} IN SHARE ROW EXCLUSIVE MODE`);
|
|
1589
|
+
if (header.layoutVersion === 25) {
|
|
1590
|
+
yield* execute(sql`ALTER TABLE ${table("effect_agent_message_deliveries")} ADD COLUMN peer_retained_until_millis BIGINT`);
|
|
1591
|
+
yield* backfillPeerMessageRetention(namespace).pipe(Effect.catchTag("MessageDeliveryError", (cause) => PostgresStorageCorruptionError.make({
|
|
1592
|
+
table: "effect_agent_message_deliveries",
|
|
1593
|
+
rowKey: "layout-25-upgrade",
|
|
1594
|
+
message: `Invalid retained delivery during layout upgrade: ${cause.operation}.`
|
|
1595
|
+
})));
|
|
1596
|
+
for (const statement of Object.values(layout26IndexStatements)) yield* sql.unsafe(qualify(statement, namespace)).withoutTransform;
|
|
1597
|
+
}
|
|
1598
|
+
if (header.layoutVersion < 27) yield* upgradeExecutionCheckpoints(namespace).pipe(Effect.catchTag("ThreadStoreError", (cause) => PostgresStorageCorruptionError.make({
|
|
1599
|
+
table: "effect_agent_executions",
|
|
1600
|
+
rowKey: "upgrade",
|
|
1601
|
+
message: `Invalid retained execution; upgrade rolled back: ${cause.message}`
|
|
1602
|
+
})));
|
|
1603
|
+
if (header.layoutVersion < 30) {
|
|
1604
|
+
if (header.layoutVersion <= 27) yield* execute(sql`DROP INDEX ${table("effect_agent_canonical_batches_last")}`);
|
|
1605
|
+
if (header.layoutVersion < 29) yield* execute(sql`DROP TABLE ${table("effect_agent_checkpoints")}`);
|
|
1606
|
+
yield* execute(sql`DROP INDEX IF EXISTS ${table("effect_agent_records_delivery_predecessor")}`);
|
|
1607
|
+
yield* execute(sql`DROP INDEX IF EXISTS ${table("effect_agent_work_entries_message")}`);
|
|
1608
|
+
yield* execute(sql`DROP INDEX IF EXISTS ${table("effect_agent_work_entries_peer")}`);
|
|
1609
|
+
yield* createSqlThreadWorkTables(namespace).pipe(Effect.mapError((cause) => PostgresStorageError.make({
|
|
1610
|
+
operation: cause.operation,
|
|
1611
|
+
message: cause.message,
|
|
1612
|
+
cause
|
|
1613
|
+
})));
|
|
1614
|
+
}
|
|
1615
|
+
yield* execute(sql`UPDATE ${table("effect_agent_storage_version")} SET version=${31} WHERE id=TRUE`);
|
|
1616
|
+
yield* execute(sql`UPDATE ${table("effect_agent_schema")} SET layout_version=${31} WHERE singleton=1`);
|
|
1617
|
+
return yield* readPostgresStorageHeader(namespace);
|
|
1618
|
+
}
|
|
1619
|
+
for (const statement of layoutStatements) yield* sql.unsafe(qualify(statement, namespace)).withoutTransform;
|
|
1620
|
+
yield* createSqlThreadWorkTables(namespace).pipe(Effect.mapError((cause) => PostgresStorageError.make({
|
|
1621
|
+
operation: cause.operation,
|
|
1622
|
+
message: cause.message,
|
|
1623
|
+
cause
|
|
1624
|
+
})));
|
|
1625
|
+
yield* sql.unsafe(qualify(headerStatement, namespace)).withoutTransform;
|
|
1626
|
+
yield* execute(sql`INSERT INTO ${table("effect_agent_storage_version")} (id, version) VALUES (TRUE, ${31})`);
|
|
1627
|
+
yield* execute(sql`INSERT INTO ${table("effect_agent_schema")} (singleton, layout_version, record_format) VALUES (1, ${31}, ${CURRENT_RECORD_FORMAT})`);
|
|
1628
|
+
return yield* readPostgresStorageHeader(namespace);
|
|
1629
|
+
});
|
|
1630
|
+
// src/internal/postgres-storage.ts
|
|
1631
|
+
const storageError = (operation) => (cause) => PostgresStorageError.make({
|
|
1632
|
+
operation,
|
|
1633
|
+
cause,
|
|
1634
|
+
message: cause.message
|
|
1635
|
+
});
|
|
1636
|
+
const classifyWriteFailure = (operation) => (cause) => cause.reason._tag === "SerializationError" || cause.reason._tag === "DeadlockError" || cause.reason._tag === "LockTimeoutError" ? PostgresWriteContention.make({
|
|
1637
|
+
operation,
|
|
1638
|
+
cause,
|
|
1639
|
+
message: `Postgres reported a transient transaction conflict; ${operation} is safe to retry.`
|
|
1640
|
+
}) : storageError(operation)(cause);
|
|
1641
|
+
/**
|
|
1642
|
+
* Applications route each Thread to one scoped owner. This local gate is shared by all ports
|
|
1643
|
+
* using the same client/namespace; it is not a distributed lock or failover protocol.
|
|
1644
|
+
*/
|
|
1645
|
+
const makeWriteTransaction = Effect.fnUntraced(function* (sql, lockTimeout, namespace) {
|
|
1646
|
+
const owner = yield* sqlStorageOwner(sql, namespace);
|
|
1647
|
+
const transaction = makeSqlTransaction(sql, {
|
|
1648
|
+
begin: "BEGIN ISOLATION LEVEL READ COMMITTED",
|
|
1649
|
+
prelude: sql`SELECT set_config('lock_timeout', ${`${lockTimeout}ms`}, true)`.withoutTransform
|
|
1650
|
+
});
|
|
1651
|
+
return (body) => owner.withMutation(transaction(body));
|
|
1652
|
+
});
|
|
1653
|
+
/** Every page of a multi-query export observes the same snapshot without creating write authority. */
|
|
1654
|
+
const withReadTransaction = (sql) => makeSqlTransaction(sql, { begin: "BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY" });
|
|
1655
|
+
/** Run while holding the owner transaction, including when this schema does not yet exist. */
|
|
1656
|
+
const ensurePostgresSchema = Effect.fnUntraced(function* (schema) {
|
|
1657
|
+
const sql = yield* SqlClient.SqlClient;
|
|
1658
|
+
const { execute } = yield* makeSqlQuery();
|
|
1659
|
+
if ((yield* execute(sql`SELECT 1 FROM pg_namespace WHERE nspname = ${schema}`)).length === 0) yield* execute(sql`CREATE SCHEMA IF NOT EXISTS ${sql(schema)}`);
|
|
1660
|
+
});
|
|
1661
|
+
const postgresStorageErrors = {
|
|
1662
|
+
storage: (fields) => PostgresStorageError.make(fields),
|
|
1663
|
+
corruption: (fields) => PostgresStorageCorruptionError.make(fields),
|
|
1664
|
+
isCorruption: Schema.is(PostgresStorageCorruptionError)
|
|
1665
|
+
};
|
|
1666
|
+
const isTransactionFailure = Schema.is(Schema.Union([PostgresStorageError, PostgresWriteContention]));
|
|
1667
|
+
/** Inspect under the owner gate before any DDL; current headers commit together. */
|
|
1668
|
+
const initializePostgresStorage = Effect.fn("PostgresStorage.initializeLayout")(function* ({ lockTimeout, schema }) {
|
|
1669
|
+
const sql = yield* SqlClient.SqlClient;
|
|
1670
|
+
yield* (yield* makeWriteTransaction(sql, lockTimeout, schema))(Effect.gen(function* () {
|
|
1671
|
+
const header = yield* inspectPostgresStorage(schema, true);
|
|
1672
|
+
if (header === void 0) yield* ensurePostgresSchema(schema);
|
|
1673
|
+
yield* applyPostgresLayout(schema, header);
|
|
1674
|
+
})).pipe(Effect.catchTag("SqlError", (error) => Effect.fail(classifyWriteFailure("initialize storage")(error))));
|
|
1675
|
+
});
|
|
1676
|
+
/** Bind shared journal operations without repeating format initialization. */
|
|
1677
|
+
const makePostgresJournal = Effect.fnUntraced(function* (lockTimeout, hitFailpoint, namespace) {
|
|
1678
|
+
const sql = yield* SqlClient.SqlClient;
|
|
1679
|
+
const write = yield* makeWriteTransaction(sql, lockTimeout, namespace);
|
|
1680
|
+
const read = withReadTransaction(sql);
|
|
1681
|
+
return yield* makeSqlJournal({
|
|
1682
|
+
namespace,
|
|
1683
|
+
errors: postgresStorageErrors,
|
|
1684
|
+
hitFailpoint,
|
|
1685
|
+
transactions: {
|
|
1686
|
+
withWriteTransaction: (operation) => (body) => write(body).pipe(Effect.mapError((error) => {
|
|
1687
|
+
if (SqlError.isSqlError(error)) return classifyWriteFailure(operation)(error);
|
|
1688
|
+
if (Schema.is(PostgresStorageError)(error) && SqlError.isSqlError(error.cause)) return classifyWriteFailure(error.operation)(error.cause);
|
|
1689
|
+
return error;
|
|
1690
|
+
})),
|
|
1691
|
+
withReadTransaction: (operation) => (body) => read(body).pipe(Effect.mapError((error) => SqlError.isSqlError(error) ? storageError(operation)(error) : error)),
|
|
1692
|
+
isTransactionFailure
|
|
1693
|
+
}
|
|
1694
|
+
});
|
|
1695
|
+
});
|
|
1696
|
+
// src/PostgresStorage.ts
|
|
1697
|
+
var PostgresStorage_exports = /* @__PURE__ */ __exportAll({
|
|
1698
|
+
CurrentPostgresStorageVersion: () => 31,
|
|
1699
|
+
activityStoreLayer: () => activityStoreLayer,
|
|
1700
|
+
exportThread: () => exportThread,
|
|
1701
|
+
layer: () => layer,
|
|
1702
|
+
layerWith: () => layerWith,
|
|
1703
|
+
messageDeliveryStoreLayer: () => messageDeliveryStoreLayer,
|
|
1704
|
+
scheduleStoreLayer: () => scheduleStoreLayer,
|
|
1705
|
+
submissionLedgerLayer: () => submissionLedgerLayer,
|
|
1706
|
+
subscriptionStoreLayer: () => subscriptionStoreLayer,
|
|
1707
|
+
threadStoreLayer: () => threadStoreLayer
|
|
1708
|
+
});
|
|
1709
|
+
const Settings = Schema.Struct({
|
|
1710
|
+
observationPollInterval: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
|
|
1711
|
+
lockTimeout: Schema.Int.check(Schema.isGreaterThan(0)),
|
|
1712
|
+
verifyOnOpen: Schema.Boolean,
|
|
1713
|
+
schema: Schema.NonEmptyString.check(Schema.isMaxLength(63), Schema.isPattern(/^[a-z_][a-z0-9_]*$/))
|
|
1714
|
+
});
|
|
1715
|
+
const settings = (options) => Schema.decodeEffect(Settings)({
|
|
1716
|
+
observationPollInterval: options.observationPollInterval ?? 25,
|
|
1717
|
+
lockTimeout: options.lockTimeout ?? 5e3,
|
|
1718
|
+
verifyOnOpen: options.verifyOnOpen ?? false,
|
|
1719
|
+
schema: options.schema ?? "public"
|
|
1720
|
+
}).pipe(Effect.mapError((cause) => PostgresStorageError.make({
|
|
1721
|
+
cause,
|
|
1722
|
+
operation: "configure Postgres storage",
|
|
1723
|
+
message: cause.message
|
|
1724
|
+
})));
|
|
1725
|
+
const initialize = Effect.fnUntraced(function* (options) {
|
|
1726
|
+
const config = yield* settings(options);
|
|
1727
|
+
yield* initializePostgresStorage(config);
|
|
1728
|
+
return config;
|
|
1729
|
+
});
|
|
1730
|
+
const openJournal = Effect.fnUntraced(function* (options) {
|
|
1731
|
+
const config = yield* initialize(options);
|
|
1732
|
+
const hitFailpoint = options.failpoint ?? (() => Effect.void);
|
|
1733
|
+
return {
|
|
1734
|
+
config,
|
|
1735
|
+
journal: yield* makePostgresJournal(config.lockTimeout, hitFailpoint, config.schema),
|
|
1736
|
+
hitFailpoint
|
|
1737
|
+
};
|
|
1738
|
+
});
|
|
1739
|
+
const makeThreadStore = ({ config, journal, hitFailpoint }) => makeSqlThreadStoreKernel(journal, {
|
|
1740
|
+
...config,
|
|
1741
|
+
namespace: config.schema,
|
|
1742
|
+
errors: postgresStorageErrors,
|
|
1743
|
+
hitFailpoint,
|
|
1744
|
+
offsetPrefix: "effect-agent-postgres@1:"
|
|
1745
|
+
}).pipe(Effect.map((services) => ({
|
|
1746
|
+
...services,
|
|
1747
|
+
owner: journal.owner
|
|
1748
|
+
})));
|
|
1749
|
+
const makeSubmissionLedger = ({ config, journal, hitFailpoint }) => makeSqlSubmissionLedgerKernel(journal, {
|
|
1750
|
+
namespace: config.schema,
|
|
1751
|
+
errors: postgresStorageErrors,
|
|
1752
|
+
hitFailpoint,
|
|
1753
|
+
offsetPrefix: "effect-agent-postgres@1:",
|
|
1754
|
+
sqlFailure: (operation) => (cause) => {
|
|
1755
|
+
const internal = classifyWriteFailure(operation)(cause);
|
|
1756
|
+
return LedgerError.make({
|
|
1757
|
+
operation,
|
|
1758
|
+
message: internal.message,
|
|
1759
|
+
cause: internal
|
|
1760
|
+
});
|
|
1761
|
+
}
|
|
1762
|
+
}).pipe(Effect.map((services) => ({
|
|
1763
|
+
...services,
|
|
1764
|
+
owner: journal.owner
|
|
1765
|
+
})));
|
|
1766
|
+
/**
|
|
1767
|
+
* Thread history and submissions over one scoped owner. The application must route each Thread
|
|
1768
|
+
* exclusively to this owner; Postgres does not provide distributed ownership or failover here.
|
|
1769
|
+
*/
|
|
1770
|
+
const layerWith = (options) => ThreadReader.layer().pipe(Layer.provideMerge(Layer.effectContext(Effect.gen(function* () {
|
|
1771
|
+
const journal = yield* openJournal(options);
|
|
1772
|
+
const threadStore = yield* makeThreadStore(journal);
|
|
1773
|
+
const submissionLedger = yield* makeSubmissionLedger(journal);
|
|
1774
|
+
return Context.make(ThreadStore, threadStore.store).pipe(Context.add(ThreadImport, threadStore.importer), Context.add(SubmissionLedger, submissionLedger.ledger), Context.add(SettlementPublisher, submissionLedger.publisher), Context.add(StorageOwner, journal.journal.owner));
|
|
1775
|
+
}))));
|
|
1776
|
+
/**
|
|
1777
|
+
* Thread history and submissions with default settings. Requires SqlClient, Digest, and SecureRandom.
|
|
1778
|
+
* SQL text parameters, including identities, must exclude NUL and unpaired UTF-16
|
|
1779
|
+
* surrogates. Canonical JSON payload strings remain intact through escaped encoding.
|
|
1780
|
+
*/
|
|
1781
|
+
const layer = layerWith({});
|
|
1782
|
+
/** Standalone thread history over the application's SqlClient and Digest. */
|
|
1783
|
+
const threadStoreLayer = (options = {}) => ThreadReader.layer().pipe(Layer.provideMerge(Layer.effectContext(Effect.flatMap(openJournal(options), makeThreadStore).pipe(Effect.map(({ store, importer, owner }) => Context.make(ThreadStore, store).pipe(Context.add(ThreadImport, importer), Context.add(StorageOwner, owner)))))));
|
|
1784
|
+
/** Export a quiesced current-layout store through a read-only snapshot; never run DDL. */
|
|
1785
|
+
const exportThread = Effect.fn("PostgresStorage.exportThread")(function* (request, options = {}) {
|
|
1786
|
+
yield* checkExportSource(request.threadId);
|
|
1787
|
+
const config = yield* settings(options);
|
|
1788
|
+
const sql = yield* SqlClient.SqlClient;
|
|
1789
|
+
return yield* Effect.scoped(Effect.gen(function* () {
|
|
1790
|
+
const owner = yield* sqlStorageOwner(sql, config.schema);
|
|
1791
|
+
return yield* owner.withMutation(makeSqlTransaction(sql, { begin: "BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY" })(Effect.gen(function* () {
|
|
1792
|
+
yield* readPostgresStorageHeader(config.schema);
|
|
1793
|
+
return yield* (yield* makeSqlThreadImport({
|
|
1794
|
+
owner,
|
|
1795
|
+
namespace: config.schema,
|
|
1796
|
+
offsetPrefix: "effect-agent-postgres@1:",
|
|
1797
|
+
read: (body) => body,
|
|
1798
|
+
write: () => Effect.die("A read-only exporter cannot import")
|
|
1799
|
+
})).export(request);
|
|
1800
|
+
})).pipe(Effect.catchTag("SqlError", (cause) => PostgresStorageError.make({
|
|
1801
|
+
operation: "export Thread snapshot",
|
|
1802
|
+
message: cause.message,
|
|
1803
|
+
cause
|
|
1804
|
+
}))));
|
|
1805
|
+
}));
|
|
1806
|
+
});
|
|
1807
|
+
/** Submissions and canonical settlement publication over the application's SqlClient, Digest, and SecureRandom. */
|
|
1808
|
+
const submissionLedgerLayer = (options = {}) => Layer.effectContext(Effect.flatMap(openJournal(options), makeSubmissionLedger).pipe(Effect.map((services) => Context.make(SubmissionLedger, services.ledger).pipe(Context.add(SettlementPublisher, services.publisher), Context.add(StorageOwner, services.owner)))));
|
|
1809
|
+
/** Schedules over the application's SqlClient. */
|
|
1810
|
+
const scheduleStoreLayer = (options = {}) => Layer.unwrap(Effect.gen(function* () {
|
|
1811
|
+
const sql = yield* SqlClient.SqlClient;
|
|
1812
|
+
const config = yield* initialize(options);
|
|
1813
|
+
const run = yield* makeWriteTransaction(sql, config.lockTimeout, config.schema);
|
|
1814
|
+
return Layer.effect(ScheduleStore, makeSqlScheduleStore(config.schema)).pipe(Layer.provide(Layer.succeed(SqlScheduleWriteTransaction, { run })));
|
|
1815
|
+
}));
|
|
1816
|
+
/** Message delivery over the application's SqlClient and Digest, with optional live capacity and payload limits. */
|
|
1817
|
+
const messageDeliveryStoreLayer = (options = {}) => Layer.effect(MessageDeliveryStore, Effect.gen(function* () {
|
|
1818
|
+
const sql = yield* SqlClient.SqlClient;
|
|
1819
|
+
const config = yield* initialize(options);
|
|
1820
|
+
const owner = yield* sqlStorageOwner(sql, config.schema);
|
|
1821
|
+
const store = yield* makeSqlMessageDeliveryStore(owner, options.limits, {
|
|
1822
|
+
namespace: config.schema,
|
|
1823
|
+
transaction: yield* makeWriteTransaction(sql, config.lockTimeout, config.schema)
|
|
1824
|
+
});
|
|
1825
|
+
yield* registerCanonicalMessageWriter(owner, store);
|
|
1826
|
+
return store;
|
|
1827
|
+
}));
|
|
1828
|
+
/** Subscriptions owned by an explicit partition. Invalid partitions fail before accessing SQL. */
|
|
1829
|
+
const subscriptionStoreLayer = (owned, options = {}) => Layer.effect(SubscriptionStore, Effect.gen(function* () {
|
|
1830
|
+
const partition = yield* Schema.decodeEffect(SourcePartition)(owned).pipe(Effect.mapError(() => SubscriptionError.make({
|
|
1831
|
+
reason: "validation",
|
|
1832
|
+
code: "partition"
|
|
1833
|
+
})));
|
|
1834
|
+
const config = yield* initialize(options);
|
|
1835
|
+
const sql = yield* SqlClient.SqlClient;
|
|
1836
|
+
const transaction = yield* makeWriteTransaction(sql, config.lockTimeout, config.schema);
|
|
1837
|
+
return yield* transaction(makeSqlSubscriptionStore(partition, {
|
|
1838
|
+
namespace: config.schema,
|
|
1839
|
+
transaction,
|
|
1840
|
+
maxStoredJsonLength: 16777216
|
|
1841
|
+
})).pipe(Effect.catchTag("SqlError", () => SubscriptionError.make({
|
|
1842
|
+
reason: "storage",
|
|
1843
|
+
code: "initialize"
|
|
1844
|
+
})));
|
|
1845
|
+
}));
|
|
1846
|
+
/**
|
|
1847
|
+
* Independent activity progress over one shared SqlClient; does not initialize the Thread journal.
|
|
1848
|
+
* Route each activity key to one process. Sessions are local and close with their owning Scope.
|
|
1849
|
+
*/
|
|
1850
|
+
const activityStoreLayer = (options = {}) => Layer.effect(ActivityProcessorStore, Effect.gen(function* () {
|
|
1851
|
+
const sql = yield* SqlClient.SqlClient;
|
|
1852
|
+
const config = yield* settings(options);
|
|
1853
|
+
return yield* makeSqlActivityStore(yield* makeWriteTransaction(sql, config.lockTimeout, config.schema), ensurePostgresSchema(config.schema).pipe(Effect.mapError(() => ActivityStoreError.make({
|
|
1854
|
+
operation: "initialize activity schema",
|
|
1855
|
+
reason: "unavailable"
|
|
1856
|
+
}))), config.schema);
|
|
1857
|
+
})).pipe(Layer.provide(options.activityFailpoint === void 0 ? ActivityMutationFailpoint.layer : Layer.succeed(ActivityMutationFailpoint)({ hit: options.activityFailpoint })));
|
|
1858
|
+
export { layerWith as a, submissionLedgerLayer as c, CurrentPostgresStorageVersion as d, layer as i, subscriptionStoreLayer as l, activityStoreLayer as n, messageDeliveryStoreLayer as o, exportThread as r, scheduleStoreLayer as s, PostgresStorage_exports as t, threadStoreLayer as u };
|
|
1859
|
+
|
|
1860
|
+
//# sourceMappingURL=PostgresStorage-D9IqvmQ2.mjs.map
|