@yielded/agent-storage-cloudflare 0.1.0-beta.167 → 0.1.0-beta.168
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/DoMemoryStore.mjs +1 -1
- package/dist/DoMessageDeliveryStore.d.mts +1 -1
- package/dist/DoMessageDeliveryStore.mjs +3 -2
- package/dist/DoMessageDeliveryStore.mjs.map +1 -1
- package/dist/DoScheduleStore.mjs +16 -37
- package/dist/DoScheduleStore.mjs.map +1 -1
- package/dist/DoStorageConfig.d.mts +2 -10
- package/dist/DoStorageConfig.mjs +2 -11
- package/dist/DoStorageConfig.mjs.map +1 -1
- package/dist/{DoStorageError-CGuAfSwF.d.mts → DoStorageError-CxjMnzUA.d.mts} +7 -18
- package/dist/DoStorageError.d.mts +2 -2
- package/dist/DoStorageError.mjs +14 -30
- package/dist/DoStorageError.mjs.map +1 -1
- package/dist/DoStorageFailpoint.d.mts +1 -1
- package/dist/DoStorageFailpointTesting.d.mts +1 -1
- package/dist/{DoStorageVersion-iNSyi3cJ.d.mts → DoStorageVersion-DTFoLglB.d.mts} +3 -4
- package/dist/DoStorageVersion.d.mts +1 -1
- package/dist/DoStorageVersion.mjs +2 -2
- package/dist/DoStorageVersion.mjs.map +1 -1
- package/dist/DoSubmissionLedger.d.mts +7 -5
- package/dist/DoSubmissionLedger.mjs +378 -528
- package/dist/DoSubmissionLedger.mjs.map +1 -1
- package/dist/DoSubscriptionStore.mjs +5 -24
- package/dist/DoSubscriptionStore.mjs.map +1 -1
- package/dist/{DoThreadStore-D_83OjCH.d.mts → DoThreadStore-Bd4fTns3.d.mts} +17 -18
- package/dist/DoThreadStore-CwBvTLoq.mjs +468 -0
- package/dist/DoThreadStore-CwBvTLoq.mjs.map +1 -0
- package/dist/DoThreadStore.d.mts +2 -2
- package/dist/DoThreadStore.mjs +4 -529
- package/dist/MemoryProtocol.mjs +3 -3
- package/dist/MemoryProtocol.mjs.map +1 -1
- package/dist/PortProtocol.d.mts +11727 -3410
- package/dist/PortProtocol.mjs +103 -15
- package/dist/PortProtocol.mjs.map +1 -1
- package/dist/PortRouting.d.mts +14 -9
- package/dist/PortRouting.mjs +155 -29
- package/dist/PortRouting.mjs.map +1 -1
- package/dist/do-journal-Cawnsetj.mjs +761 -0
- package/dist/do-journal-Cawnsetj.mjs.map +1 -0
- package/dist/index.d.mts +3 -3
- package/dist/index.mjs +1 -1
- package/dist/migrations-D_EP3KYf.mjs +148 -0
- package/dist/migrations-D_EP3KYf.mjs.map +1 -0
- package/dist/{owned-state-DHm2aRXx.mjs → owned-state-BHzUi0eb.mjs} +50 -19
- package/dist/owned-state-BHzUi0eb.mjs.map +1 -0
- package/package.json +1 -1
- package/src/DoMessageDeliveryStore.ts +2 -1
- package/src/DoScheduleStore.ts +19 -59
- package/src/DoStorageConfig.ts +2 -11
- package/src/DoStorageError.ts +3 -26
- package/src/DoStorageVersion.ts +1 -1
- package/src/DoSubmissionLedger.ts +1806 -2030
- package/src/DoSubscriptionStore.ts +5 -47
- package/src/DoThreadStore.ts +276 -622
- package/src/MemoryProtocol.ts +3 -3
- package/src/PortProtocol.ts +198 -12
- package/src/PortRouting.ts +527 -108
- package/src/internal/canonical-append.ts +15 -0
- package/src/internal/do-journal.ts +548 -1440
- package/src/internal/migrations.ts +184 -287
- package/src/internal/owned-state.ts +81 -30
- package/src/internal/prompt-checkpoint.ts +158 -0
- package/src/internal/storage-span.ts +2 -5
- package/dist/DoThreadStore.mjs.map +0 -1
- package/dist/do-journal-DUhjZkOf.mjs +0 -1352
- package/dist/do-journal-DUhjZkOf.mjs.map +0 -1
- package/dist/migrations-DtKz_Gxr.mjs +0 -306
- package/dist/migrations-DtKz_Gxr.mjs.map +0 -1
- package/dist/owned-state-DHm2aRXx.mjs.map +0 -1
- package/src/internal/message-delivery-schema.ts +0 -24
- package/src/internal/recovery-checkpoint-schema.ts +0 -17
|
@@ -1,304 +1,201 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { SQL_PROMPT_PREDICATE } from "@yielded/agent-storage-sql/sql-thread-native-reads";
|
|
2
|
+
import { createSqlThreadWorkTables } from "@yielded/agent-storage-sql/sql-thread-work";
|
|
3
|
+
import { makeSqliteLayoutInspection } from "@yielded/agent-storage-sql/sqlite-layout-inspection";
|
|
4
|
+
import { CURRENT_RECORD_FORMAT } from "@yielded/agent/records";
|
|
5
|
+
import { Effect, Schema } from "effect";
|
|
5
6
|
import * as SqlClient from "effect/sql/SqlClient";
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
export const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
7
|
+
import type { SqlError } from "effect/sql/SqlError";
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
DoStorageCompatibilityError,
|
|
11
|
+
DoStorageCorruptionError,
|
|
12
|
+
DoStorageError,
|
|
13
|
+
} from "../DoStorageError.ts";
|
|
14
|
+
|
|
15
|
+
export const CurrentDoStorageVersion = 25;
|
|
16
|
+
|
|
17
|
+
/** Fresh layout only. Unsupported stores are rejected before these statements execute. */
|
|
18
|
+
const layoutStatements = [
|
|
19
|
+
"CREATE TABLE effect_agent_threads ( thread_id TEXT PRIMARY KEY NOT NULL, created_at TEXT, tail_sequence INTEGER NOT NULL, work_version INTEGER NOT NULL DEFAULT 3, work_state TEXT NOT NULL DEFAULT 'ready', work_through_sequence INTEGER NOT NULL DEFAULT 0, work_reporting INTEGER NOT NULL DEFAULT 0, work_entry_count INTEGER NOT NULL DEFAULT 0, worker_input_sequence INTEGER, worker_settlement_sequence INTEGER )",
|
|
20
|
+
"CREATE TABLE effect_agent_envelopes (envelope_key INTEGER PRIMARY KEY NOT NULL, content_digest TEXT UNIQUE NOT NULL, prefix TEXT NOT NULL, before_payload TEXT NOT NULL, before_record_id TEXT NOT NULL, suffix TEXT NOT NULL)",
|
|
21
|
+
"CREATE TABLE effect_agent_run_keys (run_key INTEGER PRIMARY KEY NOT NULL, thread_id TEXT NOT NULL, run_id TEXT NOT NULL, UNIQUE (thread_id, run_id), FOREIGN KEY (thread_id) REFERENCES effect_agent_threads(thread_id) ON DELETE RESTRICT)",
|
|
22
|
+
"CREATE TABLE effect_agent_definitions (definition_key INTEGER PRIMARY KEY NOT NULL, content_digest TEXT UNIQUE NOT NULL, value_json TEXT NOT NULL)",
|
|
23
|
+
"CREATE TABLE effect_agent_executions (thread_id TEXT NOT NULL, run_id TEXT NOT NULL, input_sequence INTEGER NOT NULL, revision INTEGER NOT NULL, checkpoint_json TEXT NOT NULL, last_commit_id TEXT, last_commit_digest TEXT, PRIMARY KEY (thread_id, run_id), FOREIGN KEY (thread_id) REFERENCES effect_agent_threads(thread_id) ON DELETE RESTRICT)",
|
|
24
|
+
"CREATE TABLE effect_agent_canonical_batches ( thread_id TEXT NOT NULL, batch_id TEXT NOT NULL, first_sequence INTEGER NOT NULL, last_sequence INTEGER NOT NULL, producer_id_json TEXT 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 effect_agent_threads(thread_id) ON DELETE RESTRICT )",
|
|
25
|
+
"CREATE TABLE effect_agent_canonical_records ( thread_id TEXT NOT NULL, sequence INTEGER NOT NULL, record_id TEXT NOT NULL, created_at_json TEXT NOT NULL, envelope_key INTEGER NOT NULL, payload_json TEXT, record_tag INTEGER NOT NULL, run_id INTEGER, tool_call_id TEXT, input_kind TEXT, source_submission_id TEXT, message_id TEXT, submission_id TEXT, application_input INTEGER NOT NULL, context_through INTEGER, context_kind TEXT, worker_thread_id TEXT, handoff INTEGER NOT NULL, PRIMARY KEY (thread_id, sequence), UNIQUE (thread_id, record_id), FOREIGN KEY (thread_id) REFERENCES effect_agent_threads(thread_id) ON DELETE RESTRICT )",
|
|
26
|
+
'CREATE TABLE "effect_agent_record_runs" ( thread_id TEXT NOT NULL, run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, PRIMARY KEY (thread_id, run_id, sequence), FOREIGN KEY (thread_id, sequence) REFERENCES "effect_agent_canonical_records"(thread_id, sequence) ON DELETE RESTRICT ) WITHOUT ROWID',
|
|
27
|
+
"CREATE TABLE effect_agent_rollover_buckets (thread_id TEXT NOT NULL, height INTEGER NOT NULL, bucket INTEGER NOT NULL, sequence INTEGER NOT NULL, PRIMARY KEY (thread_id, height, bucket, sequence), FOREIGN KEY (thread_id, sequence) REFERENCES effect_agent_canonical_records(thread_id, sequence) ON DELETE RESTRICT) WITHOUT ROWID",
|
|
28
|
+
"CREATE TABLE effect_agent_tool_declarations (thread_id TEXT NOT NULL, settlement_record_id TEXT NOT NULL, sequence INTEGER NOT NULL, PRIMARY KEY (thread_id, settlement_record_id, sequence), FOREIGN KEY (thread_id, sequence) REFERENCES effect_agent_canonical_records(thread_id, sequence) ON DELETE RESTRICT)",
|
|
29
|
+
"CREATE INDEX effect_agent_tool_declarations_sequence ON effect_agent_tool_declarations(thread_id, sequence)",
|
|
30
|
+
"CREATE TABLE effect_agent_record_refusals (thread_id TEXT NOT NULL, reservation_record_id TEXT NOT NULL, sequence INTEGER NOT NULL, PRIMARY KEY (thread_id, reservation_record_id, sequence), FOREIGN KEY (thread_id, sequence) REFERENCES effect_agent_canonical_records(thread_id, sequence) ON DELETE RESTRICT)",
|
|
31
|
+
"CREATE INDEX effect_agent_record_refusals_sequence ON effect_agent_record_refusals(thread_id, sequence)",
|
|
32
|
+
"CREATE TABLE effect_agent_checkpoints ( thread_id TEXT NOT NULL, through_sequence INTEGER NOT NULL, checkpoint_json TEXT NOT NULL, PRIMARY KEY (thread_id, through_sequence), FOREIGN KEY (thread_id) REFERENCES effect_agent_threads(thread_id) ON DELETE RESTRICT )",
|
|
33
|
+
"CREATE TABLE effect_agent_submissions ( submission_id TEXT PRIMARY KEY NOT NULL, thread_id TEXT NOT NULL, queue_sequence INTEGER NOT NULL, principal TEXT NOT NULL, idempotency_key TEXT NOT NULL, agent_id TEXT NOT NULL, definition_key INTEGER NOT NULL, deployment_id TEXT NOT NULL, input_json TEXT NOT NULL, input_digest TEXT NOT NULL, receipt_id TEXT NOT NULL, state TEXT NOT NULL, settled_outcome TEXT, settled_record_id TEXT, finalized_at TEXT, created_at TEXT NOT NULL, ready_at TEXT, input_applied_record_id TEXT, input_applied_sequence INTEGER, joined_host_submission_id TEXT, suspended_reason_json TEXT, suspended_at TEXT, unknown_reason TEXT, unknown_tool_call_ids_json TEXT, parent_submission_id TEXT, parent_tool_call_id TEXT, admission_group TEXT, admission_fence_json TEXT, worker_admission_json TEXT, message_admission_json TEXT, UNIQUE (thread_id, principal, idempotency_key), UNIQUE (thread_id, queue_sequence) )",
|
|
34
|
+
"CREATE INDEX effect_agent_submissions_joined_host ON effect_agent_submissions (joined_host_submission_id) WHERE joined_host_submission_id IS NOT NULL",
|
|
35
|
+
"CREATE INDEX effect_agent_submissions_parent ON effect_agent_submissions (parent_submission_id) WHERE parent_submission_id IS NOT NULL",
|
|
36
|
+
"CREATE INDEX effect_agent_submissions_group ON effect_agent_submissions (thread_id, admission_group) WHERE admission_group IS NOT NULL AND state <> 'settled'",
|
|
37
|
+
"CREATE TABLE effect_agent_abort_intents ( submission_id TEXT PRIMARY KEY NOT NULL, author TEXT NOT NULL, reason TEXT NOT NULL, requested_at TEXT NOT NULL, canonical_record_id TEXT, FOREIGN KEY (submission_id) REFERENCES effect_agent_submissions(submission_id) ON DELETE RESTRICT )",
|
|
38
|
+
"CREATE TABLE effect_agent_approval_decisions ( submission_id TEXT NOT NULL, tool_call_id TEXT NOT NULL, decision TEXT NOT NULL, resolver TEXT NOT NULL, reason TEXT NOT NULL, decided_at TEXT NOT NULL, PRIMARY KEY (submission_id, tool_call_id), FOREIGN KEY (submission_id) REFERENCES effect_agent_submissions(submission_id) ON DELETE RESTRICT )",
|
|
39
|
+
"CREATE TABLE effect_agent_unknown_resolutions ( submission_id TEXT NOT NULL, tool_call_id TEXT NOT NULL, resolution_kind TEXT NOT NULL, author TEXT NOT NULL, reason TEXT NOT NULL, resolution_json TEXT NOT NULL, resolved_at TEXT NOT NULL, PRIMARY KEY (submission_id, tool_call_id, resolution_kind), FOREIGN KEY (submission_id) REFERENCES effect_agent_submissions(submission_id) ON DELETE RESTRICT )",
|
|
40
|
+
"CREATE TABLE effect_agent_child_reservations ( reservation_id TEXT PRIMARY KEY NOT NULL, parent_submission_id TEXT NOT NULL, parent_tool_call_id TEXT NOT NULL, child_submission_id TEXT, status TEXT NOT NULL, allocation_json TEXT NOT NULL, allocation_digest TEXT NOT NULL, accounting_json TEXT, reserved_at TEXT NOT NULL, release_began_at TEXT, released_at TEXT, UNIQUE (parent_submission_id, parent_tool_call_id), FOREIGN KEY (parent_submission_id) REFERENCES effect_agent_submissions(submission_id) ON DELETE RESTRICT )",
|
|
41
|
+
"CREATE TABLE effect_agent_child_settlements ( parent_submission_id TEXT NOT NULL, child_submission_id TEXT NOT NULL, child_outcome TEXT, recorded_at TEXT NOT NULL, PRIMARY KEY (parent_submission_id, child_submission_id) )",
|
|
42
|
+
'CREATE INDEX effect_agent_records_call ON "effect_agent_canonical_records"(thread_id, record_tag, run_id, tool_call_id) WHERE tool_call_id IS NOT NULL',
|
|
43
|
+
"CREATE INDEX effect_agent_records_run_input ON \"effect_agent_canonical_records\"(thread_id, run_id) WHERE record_tag = 30 AND input_kind = 'user'",
|
|
44
|
+
'CREATE INDEX effect_agent_records_subtree ON "effect_agent_canonical_records"(thread_id, source_submission_id, sequence) WHERE record_tag = 22',
|
|
45
|
+
'CREATE INDEX effect_agent_records_worker_input ON "effect_agent_canonical_records"(thread_id, message_id) WHERE record_tag = 34',
|
|
46
|
+
"CREATE INDEX effect_agent_submissions_nonterminal ON effect_agent_submissions (thread_id, queue_sequence) WHERE state <> 'settled'",
|
|
47
|
+
"CREATE TABLE effect_agent_message_deliveries ( owner_thread_id TEXT NOT NULL, message_id TEXT NOT NULL, version INTEGER NOT NULL, state TEXT NOT NULL, deadline_at_millis INTEGER, record_json TEXT NOT NULL, PRIMARY KEY (owner_thread_id, message_id) )",
|
|
48
|
+
"CREATE INDEX effect_agent_message_deliveries_due ON effect_agent_message_deliveries (deadline_at_millis, owner_thread_id, message_id) WHERE deadline_at_millis IS NOT NULL",
|
|
49
|
+
"CREATE INDEX effect_agent_message_deliveries_pending ON \"effect_agent_message_deliveries\"(owner_thread_id, message_id) WHERE state NOT IN ('processed', 'refused')",
|
|
50
|
+
"CREATE TABLE effect_agent_worker_stops (thread_id TEXT PRIMARY KEY NOT NULL, terminal TEXT)",
|
|
51
|
+
"CREATE INDEX effect_agent_worker_starts ON effect_agent_message_deliveries(owner_thread_id, json_extract(record_json, '$.envelope.workerAdmission.origin.worker.delegationId'), json_extract(record_json, '$.envelope.workerAdmission.origin.worker.targetAgentId'), message_id) WHERE message_id = json_extract(record_json, '$.envelope.workerAdmission.origin.firstMessageId')",
|
|
52
|
+
"CREATE INDEX effect_agent_worker_pending ON effect_agent_message_deliveries(owner_thread_id, json_extract(record_json, '$.envelope.workerAdmission.origin.worker.threadId'), message_id) WHERE state IN ('pending', 'parked') AND json_extract(record_json, '$.receipt') IS NULL",
|
|
53
|
+
"CREATE TABLE effect_agent_meta ( key TEXT PRIMARY KEY NOT NULL, value TEXT NOT NULL )",
|
|
54
|
+
'CREATE INDEX effect_agent_records_tag ON "effect_agent_canonical_records"(thread_id, record_tag, sequence)',
|
|
55
|
+
'CREATE INDEX effect_agent_records_handoff ON "effect_agent_canonical_records"(thread_id, sequence) WHERE handoff = 1',
|
|
56
|
+
"CREATE INDEX effect_agent_records_application_input ON effect_agent_canonical_records(thread_id, sequence) WHERE application_input = 1",
|
|
57
|
+
`CREATE INDEX effect_agent_records_prompt ON effect_agent_canonical_records(thread_id, sequence) WHERE ${SQL_PROMPT_PREDICATE}`,
|
|
58
|
+
"CREATE INDEX effect_agent_records_admitted_input ON effect_agent_canonical_records(thread_id, sequence) WHERE record_tag = 30 AND submission_id IS NOT NULL",
|
|
59
|
+
"CREATE INDEX effect_agent_records_worker_funding ON effect_agent_canonical_records(thread_id, source_submission_id, sequence, message_id) WHERE record_tag = 34",
|
|
60
|
+
"CREATE INDEX effect_agent_records_worker_completed ON effect_agent_canonical_records(thread_id, message_id, sequence) WHERE record_tag = 32",
|
|
61
|
+
"CREATE INDEX effect_agent_records_worker_stop ON effect_agent_canonical_records(thread_id, worker_thread_id, sequence) WHERE record_tag = 38",
|
|
62
|
+
"CREATE INDEX effect_agent_message_deliveries_identity ON effect_agent_message_deliveries(owner_thread_id, json_quote(message_id))",
|
|
63
|
+
"CREATE INDEX effect_agent_message_deliveries_peer ON effect_agent_message_deliveries(owner_thread_id, message_id) WHERE state NOT IN ('processed', 'refused') AND json_extract(record_json, '$.envelope.messageAdmission.schemaVersion') = 1",
|
|
64
|
+
"CREATE TABLE effect_agent_journal_ranges (thread_id TEXT NOT NULL, first_sequence INTEGER NOT NULL, last_sequence INTEGER NOT NULL, record_count INTEGER NOT NULL, batch_count INTEGER NOT NULL, byte_count INTEGER NOT NULL, state TEXT NOT NULL, locator TEXT, PRIMARY KEY (thread_id, first_sequence), FOREIGN KEY (thread_id) REFERENCES effect_agent_threads(thread_id) ON DELETE RESTRICT)",
|
|
65
|
+
"CREATE TABLE effect_agent_archive_batches (thread_id TEXT NOT NULL, batch_id TEXT NOT NULL, range_first_sequence INTEGER NOT NULL, batch_json TEXT NOT NULL, PRIMARY KEY (thread_id, batch_id), FOREIGN KEY (thread_id, batch_id) REFERENCES 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 effect_agent_journal_ranges(thread_id, first_sequence) ON DELETE RESTRICT)",
|
|
66
|
+
"CREATE TABLE effect_agent_archive_records (thread_id TEXT NOT NULL, sequence INTEGER NOT NULL, range_first_sequence INTEGER NOT NULL, record_json TEXT NOT NULL, PRIMARY KEY (thread_id, sequence), FOREIGN KEY (thread_id, sequence) REFERENCES 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 effect_agent_journal_ranges(thread_id, first_sequence) ON DELETE RESTRICT)",
|
|
67
|
+
"CREATE UNIQUE INDEX effect_agent_canonical_batches_sequence ON effect_agent_canonical_batches(thread_id, first_sequence)",
|
|
68
|
+
"CREATE INDEX effect_agent_canonical_batches_last ON effect_agent_canonical_batches(thread_id, last_sequence)",
|
|
69
|
+
"CREATE TABLE effect_agent_live_child_reservations (reservation_id TEXT PRIMARY KEY NOT NULL, thread_id TEXT NOT NULL)",
|
|
70
|
+
"CREATE INDEX effect_agent_live_child_reservations_thread ON effect_agent_live_child_reservations(thread_id, reservation_id)",
|
|
71
|
+
// Compact native positions keep accepted identities out of bounded export cursors.
|
|
72
|
+
// Persistent scalar FIFO derivatives; rebuild from canonical input/settlement boundaries.
|
|
73
|
+
// Writer-local import indexes; rows are removed before publication.
|
|
74
|
+
"CREATE TABLE effect_agent_import_delivery_visits (thread_id TEXT NOT NULL, message_id TEXT NOT NULL, path_id TEXT NOT NULL, completed INTEGER NOT NULL, PRIMARY KEY (thread_id, message_id))",
|
|
75
|
+
"CREATE INDEX effect_agent_import_delivery_visits_path ON effect_agent_import_delivery_visits(thread_id, path_id)",
|
|
76
|
+
"CREATE TRIGGER effect_agent_live_child_insert AFTER INSERT ON effect_agent_child_reservations FOR EACH ROW BEGIN INSERT INTO effect_agent_live_child_reservations (reservation_id, thread_id) SELECT NEW.reservation_id, thread_id FROM effect_agent_submissions WHERE submission_id=NEW.parent_submission_id AND NEW.status<>'released'; END",
|
|
77
|
+
"CREATE TRIGGER effect_agent_live_child_update AFTER UPDATE ON effect_agent_child_reservations FOR EACH ROW WHEN OLD.reservation_id IS NOT NEW.reservation_id OR OLD.parent_submission_id IS NOT NEW.parent_submission_id OR OLD.parent_tool_call_id IS NOT NEW.parent_tool_call_id OR OLD.child_submission_id IS NOT NEW.child_submission_id OR OLD.status IS NOT NEW.status OR OLD.allocation_json IS NOT NEW.allocation_json OR OLD.allocation_digest IS NOT NEW.allocation_digest OR OLD.accounting_json IS NOT NEW.accounting_json OR OLD.reserved_at IS NOT NEW.reserved_at OR OLD.release_began_at IS NOT NEW.release_began_at OR OLD.released_at IS NOT NEW.released_at BEGIN DELETE FROM effect_agent_live_child_reservations WHERE reservation_id=OLD.reservation_id; INSERT INTO effect_agent_live_child_reservations (reservation_id, thread_id) SELECT NEW.reservation_id, thread_id FROM effect_agent_submissions WHERE submission_id=NEW.parent_submission_id AND NEW.status<>'released'; END",
|
|
78
|
+
"CREATE TRIGGER effect_agent_live_child_delete AFTER DELETE ON effect_agent_child_reservations FOR EACH ROW BEGIN DELETE FROM effect_agent_live_child_reservations WHERE reservation_id=OLD.reservation_id; END",
|
|
79
|
+
"CREATE INDEX effect_agent_message_deliveries_capacity ON effect_agent_message_deliveries(owner_thread_id, (COALESCE(json_extract(record_json, '$.envelope.messageAdmission._tag'), '') = 'WorkerUpdate'), message_id) WHERE state IN ('pending', 'accepted', 'parked')",
|
|
80
|
+
] as const;
|
|
81
|
+
|
|
82
|
+
const headerStatement =
|
|
83
|
+
"CREATE TABLE effect_agent_schema (singleton INTEGER PRIMARY KEY NOT NULL CHECK (singleton = 1), layout_version INTEGER NOT NULL CHECK (layout_version > 0), record_format TEXT NOT NULL CHECK (length(record_format) > 0))";
|
|
84
|
+
|
|
85
|
+
const layoutObjects = layoutStatements.map((statement, index) => {
|
|
86
|
+
const name = /^CREATE (TABLE|(?:UNIQUE )?INDEX|TRIGGER) "?([a-z_][a-z_0-9]*)"?/.exec(statement);
|
|
87
|
+
|
|
88
|
+
if (name === null) throw new Error("Invalid fresh layout statement");
|
|
89
|
+
|
|
90
|
+
return [name[1].replace("UNIQUE ", "").toLowerCase(), name[2], index] as const;
|
|
27
91
|
});
|
|
28
92
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
93
|
+
const Legacy = Schema.Tuple([
|
|
94
|
+
Schema.Struct({
|
|
95
|
+
key: Schema.Literal("storage_version"),
|
|
96
|
+
value: Schema.String.check(Schema.isPattern(/^(0|[1-9][0-9]*)$/)).pipe(
|
|
97
|
+
Schema.decodeTo(Schema.FiniteFromString),
|
|
98
|
+
Schema.decodeTo(Schema.Int),
|
|
99
|
+
),
|
|
100
|
+
}),
|
|
101
|
+
]);
|
|
102
|
+
|
|
103
|
+
export interface DoStorageHeader {
|
|
104
|
+
readonly layoutVersion: number;
|
|
105
|
+
readonly recordFormat: string;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const incompatible = (actualVersion: number, message: string) =>
|
|
109
|
+
DoStorageCompatibilityError.make({
|
|
110
|
+
actualVersion,
|
|
111
|
+
supportedVersion: CurrentDoStorageVersion,
|
|
112
|
+
message: `${message} Keep the original store; the store was not changed.`,
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
const storageError = (cause: SqlError) =>
|
|
116
|
+
DoStorageError.make({ operation: "inspect storage layout", cause, message: cause.message });
|
|
117
|
+
|
|
118
|
+
const { decode, readObjects, readHeader } = makeSqliteLayoutInspection({
|
|
119
|
+
version: CurrentDoStorageVersion,
|
|
120
|
+
statements: layoutStatements,
|
|
121
|
+
objects: layoutObjects,
|
|
122
|
+
headerStatement,
|
|
123
|
+
incompatible,
|
|
124
|
+
storageError,
|
|
125
|
+
corruption: (table) =>
|
|
126
|
+
DoStorageCorruptionError.make({
|
|
127
|
+
table,
|
|
128
|
+
rowKey: "schema",
|
|
129
|
+
message: "Malformed storage layout or version header.",
|
|
130
|
+
}),
|
|
35
131
|
});
|
|
36
132
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
* 2. `effect_agent_child_settlements` is the durable cross-store notification marker the
|
|
44
|
-
* SubmissionLedger port contract mandates for cross-store adapters (`suspend`'s covering
|
|
45
|
-
* check and `recordChildSettled`'s wake both consult it): parent and child Threads
|
|
46
|
-
* live in different Durable Objects, so a child settlement reported before the parent's
|
|
47
|
-
* suspend commits must be observable from the PARENT's own storage.
|
|
48
|
-
*/
|
|
49
|
-
export const doMigrations = SqliteMigrator.fromRecord({
|
|
50
|
-
"1_current_cloudflare_thread_object": Effect.gen(function* () {
|
|
51
|
-
const sql = yield* SqlClient.SqlClient;
|
|
52
|
-
|
|
53
|
-
yield* sql`
|
|
54
|
-
CREATE TABLE effect_agent_threads (
|
|
55
|
-
thread_id TEXT PRIMARY KEY NOT NULL,
|
|
56
|
-
created_at TEXT NOT NULL,
|
|
57
|
-
tail_sequence INTEGER NOT NULL,
|
|
58
|
-
tail_digest TEXT NOT NULL,
|
|
59
|
-
producer_epoch INTEGER NOT NULL
|
|
60
|
-
)
|
|
61
|
-
`.withoutTransform;
|
|
62
|
-
|
|
63
|
-
yield* sql`
|
|
64
|
-
CREATE TABLE effect_agent_canonical_batches (
|
|
65
|
-
thread_id TEXT NOT NULL,
|
|
66
|
-
batch_id TEXT NOT NULL,
|
|
67
|
-
first_sequence INTEGER NOT NULL,
|
|
68
|
-
last_sequence INTEGER NOT NULL,
|
|
69
|
-
batch_digest TEXT NOT NULL,
|
|
70
|
-
tail_digest TEXT NOT NULL,
|
|
71
|
-
batch_json TEXT NOT NULL,
|
|
72
|
-
PRIMARY KEY (thread_id, batch_id),
|
|
73
|
-
FOREIGN KEY (thread_id)
|
|
74
|
-
REFERENCES effect_agent_threads(thread_id)
|
|
75
|
-
ON DELETE RESTRICT
|
|
76
|
-
)
|
|
77
|
-
`.withoutTransform;
|
|
78
|
-
|
|
79
|
-
yield* sql`
|
|
80
|
-
CREATE TABLE effect_agent_canonical_records (
|
|
81
|
-
thread_id TEXT NOT NULL,
|
|
82
|
-
sequence INTEGER NOT NULL,
|
|
83
|
-
record_id TEXT NOT NULL,
|
|
84
|
-
batch_id TEXT NOT NULL,
|
|
85
|
-
record_json TEXT NOT NULL,
|
|
86
|
-
PRIMARY KEY (thread_id, sequence),
|
|
87
|
-
UNIQUE (thread_id, record_id),
|
|
88
|
-
FOREIGN KEY (thread_id, batch_id)
|
|
89
|
-
REFERENCES effect_agent_canonical_batches(thread_id, batch_id)
|
|
90
|
-
ON DELETE RESTRICT
|
|
91
|
-
)
|
|
92
|
-
`.withoutTransform;
|
|
133
|
+
const inspectStorage = Effect.fnUntraced(function* (): Effect.fn.Return<
|
|
134
|
+
DoStorageHeader | undefined,
|
|
135
|
+
DoStorageCompatibilityError | DoStorageCorruptionError | DoStorageError,
|
|
136
|
+
SqlClient.SqlClient
|
|
137
|
+
> {
|
|
138
|
+
const sql = (yield* SqlClient.SqlClient).withoutTransforms();
|
|
93
139
|
|
|
94
|
-
|
|
95
|
-
CREATE INDEX effect_agent_canonical_records_batch
|
|
96
|
-
ON effect_agent_canonical_records (thread_id, batch_id, sequence)
|
|
97
|
-
`.withoutTransform;
|
|
140
|
+
const objects = yield* readObjects();
|
|
98
141
|
|
|
99
|
-
|
|
100
|
-
CREATE TABLE effect_agent_checkpoints (
|
|
101
|
-
thread_id TEXT NOT NULL,
|
|
102
|
-
through_sequence INTEGER NOT NULL,
|
|
103
|
-
tail_digest TEXT NOT NULL,
|
|
104
|
-
checkpoint_json TEXT NOT NULL,
|
|
105
|
-
PRIMARY KEY (thread_id, through_sequence),
|
|
106
|
-
FOREIGN KEY (thread_id)
|
|
107
|
-
REFERENCES effect_agent_threads(thread_id)
|
|
108
|
-
ON DELETE RESTRICT
|
|
109
|
-
)
|
|
110
|
-
`.withoutTransform;
|
|
142
|
+
const meta = objects.find((row) => row.name === "effect_agent_meta");
|
|
111
143
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
yield* sql`
|
|
115
|
-
CREATE TABLE effect_agent_submissions (
|
|
116
|
-
submission_id TEXT PRIMARY KEY NOT NULL,
|
|
117
|
-
thread_id TEXT NOT NULL,
|
|
118
|
-
queue_sequence INTEGER NOT NULL,
|
|
119
|
-
principal TEXT NOT NULL,
|
|
120
|
-
idempotency_key TEXT NOT NULL,
|
|
121
|
-
agent_id TEXT NOT NULL,
|
|
122
|
-
agent_digests_json TEXT NOT NULL,
|
|
123
|
-
deployment_id TEXT NOT NULL,
|
|
124
|
-
input_json TEXT NOT NULL,
|
|
125
|
-
input_digest TEXT NOT NULL,
|
|
126
|
-
receipt_id TEXT NOT NULL,
|
|
127
|
-
state TEXT NOT NULL,
|
|
128
|
-
settled_outcome TEXT,
|
|
129
|
-
created_at TEXT NOT NULL,
|
|
130
|
-
ready_at TEXT,
|
|
131
|
-
input_applied_record_id TEXT,
|
|
132
|
-
input_applied_sequence INTEGER,
|
|
133
|
-
joined_host_submission_id TEXT,
|
|
134
|
-
suspended_reason_json TEXT,
|
|
135
|
-
suspended_at TEXT,
|
|
136
|
-
unknown_reason TEXT,
|
|
137
|
-
unknown_tool_call_ids_json TEXT,
|
|
138
|
-
parent_submission_id TEXT,
|
|
139
|
-
parent_tool_call_id TEXT,
|
|
140
|
-
admission_group TEXT,
|
|
141
|
-
admission_fence_json TEXT,
|
|
142
|
-
worker_admission_json TEXT,
|
|
143
|
-
message_admission_json TEXT,
|
|
144
|
-
UNIQUE (thread_id, principal, idempotency_key),
|
|
145
|
-
UNIQUE (thread_id, queue_sequence)
|
|
146
|
-
)
|
|
147
|
-
`.withoutTransform;
|
|
144
|
+
if (meta === undefined) {
|
|
145
|
+
if (objects.length === 0) return undefined;
|
|
148
146
|
|
|
149
|
-
yield*
|
|
150
|
-
|
|
151
|
-
ON effect_agent_submissions (joined_host_submission_id)
|
|
152
|
-
`.withoutTransform;
|
|
147
|
+
return yield* incompatible(0, "Unversioned or incomplete Effect Agent storage.");
|
|
148
|
+
}
|
|
153
149
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
150
|
+
const [legacy] = yield* decode(
|
|
151
|
+
Legacy,
|
|
152
|
+
yield* sql<Record<string, unknown>>`
|
|
153
|
+
SELECT * FROM effect_agent_meta WHERE key = 'storage_version'
|
|
154
|
+
`.pipe(Effect.mapError(storageError)),
|
|
155
|
+
"effect_agent_meta",
|
|
156
|
+
);
|
|
158
157
|
|
|
159
|
-
|
|
160
|
-
CREATE INDEX effect_agent_submissions_group
|
|
161
|
-
ON effect_agent_submissions (thread_id, admission_group, state)
|
|
162
|
-
`.withoutTransform;
|
|
158
|
+
const version = legacy.value;
|
|
163
159
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
submission_id TEXT PRIMARY KEY NOT NULL,
|
|
167
|
-
attempt_id TEXT NOT NULL,
|
|
168
|
-
ownership_token TEXT NOT NULL,
|
|
169
|
-
producer_epoch INTEGER NOT NULL,
|
|
170
|
-
owner_producer_id TEXT NOT NULL,
|
|
171
|
-
lease_expires_at TEXT NOT NULL,
|
|
172
|
-
FOREIGN KEY (submission_id)
|
|
173
|
-
REFERENCES effect_agent_submissions(submission_id)
|
|
174
|
-
ON DELETE RESTRICT
|
|
175
|
-
)
|
|
176
|
-
`.withoutTransform;
|
|
177
|
-
|
|
178
|
-
yield* sql`
|
|
179
|
-
CREATE TABLE effect_agent_attempts (
|
|
180
|
-
attempt_id TEXT PRIMARY KEY NOT NULL,
|
|
181
|
-
submission_id TEXT NOT NULL,
|
|
182
|
-
thread_id TEXT NOT NULL,
|
|
183
|
-
owner_producer_id TEXT NOT NULL,
|
|
184
|
-
producer_epoch INTEGER NOT NULL,
|
|
185
|
-
claimed_at TEXT NOT NULL,
|
|
186
|
-
FOREIGN KEY (submission_id)
|
|
187
|
-
REFERENCES effect_agent_submissions(submission_id)
|
|
188
|
-
ON DELETE RESTRICT
|
|
189
|
-
)
|
|
190
|
-
`.withoutTransform;
|
|
191
|
-
|
|
192
|
-
yield* sql`
|
|
193
|
-
CREATE TABLE effect_agent_settlement_reservations (
|
|
194
|
-
submission_id TEXT PRIMARY KEY NOT NULL,
|
|
195
|
-
settlement_id TEXT NOT NULL,
|
|
196
|
-
outcome TEXT NOT NULL,
|
|
197
|
-
record_id TEXT NOT NULL,
|
|
198
|
-
record_json TEXT NOT NULL,
|
|
199
|
-
record_digest TEXT NOT NULL,
|
|
200
|
-
reserved_at TEXT NOT NULL,
|
|
201
|
-
finalized_at TEXT,
|
|
202
|
-
FOREIGN KEY (submission_id)
|
|
203
|
-
REFERENCES effect_agent_submissions(submission_id)
|
|
204
|
-
ON DELETE RESTRICT
|
|
205
|
-
)
|
|
206
|
-
`.withoutTransform;
|
|
207
|
-
|
|
208
|
-
yield* sql`
|
|
209
|
-
CREATE TABLE effect_agent_abort_intents (
|
|
210
|
-
submission_id TEXT PRIMARY KEY NOT NULL,
|
|
211
|
-
author TEXT NOT NULL,
|
|
212
|
-
reason TEXT NOT NULL,
|
|
213
|
-
requested_at TEXT NOT NULL,
|
|
214
|
-
canonical_record_id TEXT,
|
|
215
|
-
FOREIGN KEY (submission_id)
|
|
216
|
-
REFERENCES effect_agent_submissions(submission_id)
|
|
217
|
-
ON DELETE RESTRICT
|
|
218
|
-
)
|
|
219
|
-
`.withoutTransform;
|
|
220
|
-
|
|
221
|
-
yield* sql`
|
|
222
|
-
CREATE TABLE effect_agent_approval_decisions (
|
|
223
|
-
submission_id TEXT NOT NULL,
|
|
224
|
-
tool_call_id TEXT NOT NULL,
|
|
225
|
-
decision TEXT NOT NULL,
|
|
226
|
-
resolver TEXT NOT NULL,
|
|
227
|
-
reason TEXT NOT NULL,
|
|
228
|
-
decided_at TEXT NOT NULL,
|
|
229
|
-
PRIMARY KEY (submission_id, tool_call_id),
|
|
230
|
-
FOREIGN KEY (submission_id)
|
|
231
|
-
REFERENCES effect_agent_submissions(submission_id)
|
|
232
|
-
ON DELETE RESTRICT
|
|
233
|
-
)
|
|
234
|
-
`.withoutTransform;
|
|
235
|
-
|
|
236
|
-
yield* sql`
|
|
237
|
-
CREATE TABLE effect_agent_unknown_resolutions (
|
|
238
|
-
submission_id TEXT NOT NULL,
|
|
239
|
-
tool_call_id TEXT NOT NULL,
|
|
240
|
-
author TEXT NOT NULL,
|
|
241
|
-
reason TEXT NOT NULL,
|
|
242
|
-
resolution_json TEXT NOT NULL,
|
|
243
|
-
resolved_at TEXT NOT NULL,
|
|
244
|
-
PRIMARY KEY (submission_id, tool_call_id),
|
|
245
|
-
FOREIGN KEY (submission_id)
|
|
246
|
-
REFERENCES effect_agent_submissions(submission_id)
|
|
247
|
-
ON DELETE RESTRICT
|
|
248
|
-
)
|
|
249
|
-
`.withoutTransform;
|
|
160
|
+
return yield* readHeader(objects, version);
|
|
161
|
+
});
|
|
250
162
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
parent_submission_id TEXT NOT NULL,
|
|
255
|
-
parent_tool_call_id TEXT NOT NULL,
|
|
256
|
-
child_submission_id TEXT,
|
|
257
|
-
status TEXT NOT NULL,
|
|
258
|
-
allocation_json TEXT NOT NULL,
|
|
259
|
-
allocation_digest TEXT NOT NULL,
|
|
260
|
-
accounting_json TEXT,
|
|
261
|
-
reserved_at TEXT NOT NULL,
|
|
262
|
-
release_began_at TEXT,
|
|
263
|
-
released_at TEXT,
|
|
264
|
-
UNIQUE (parent_submission_id, parent_tool_call_id),
|
|
265
|
-
FOREIGN KEY (parent_submission_id)
|
|
266
|
-
REFERENCES effect_agent_submissions(submission_id)
|
|
267
|
-
ON DELETE RESTRICT
|
|
268
|
-
)
|
|
269
|
-
`.withoutTransform;
|
|
163
|
+
/** Read-only inspection. The caller owns a snapshot covering this check and its export reads. */
|
|
164
|
+
export const readDoStorageHeader = Effect.fnUntraced(function* () {
|
|
165
|
+
const header = yield* inspectStorage();
|
|
270
166
|
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
// carries identities only, and the child's canonical Settlement stays the outcome
|
|
274
|
-
// authority (DUR-015). No foreign keys: the parent row is checked by the operation, and
|
|
275
|
-
// the child row is intentionally foreign.
|
|
276
|
-
yield* sql`
|
|
277
|
-
CREATE TABLE effect_agent_child_settlements (
|
|
278
|
-
parent_submission_id TEXT NOT NULL,
|
|
279
|
-
child_submission_id TEXT NOT NULL,
|
|
280
|
-
child_outcome TEXT,
|
|
281
|
-
recorded_at TEXT NOT NULL,
|
|
282
|
-
PRIMARY KEY (parent_submission_id, child_submission_id)
|
|
283
|
-
)
|
|
284
|
-
`.withoutTransform;
|
|
167
|
+
if (header === undefined)
|
|
168
|
+
return yield* incompatible(0, "No initialized Thread storage to export.");
|
|
285
169
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
yield* createMessageDeliveryTables;
|
|
289
|
-
yield* createMessageDeliveryPendingIndex();
|
|
290
|
-
yield* createRecoveryCheckpointTable;
|
|
291
|
-
yield* createWorkerStops;
|
|
292
|
-
yield* sql`
|
|
293
|
-
CREATE TABLE effect_agent_meta (
|
|
294
|
-
key TEXT PRIMARY KEY NOT NULL,
|
|
295
|
-
value TEXT NOT NULL
|
|
296
|
-
)
|
|
297
|
-
`.withoutTransform;
|
|
170
|
+
return header;
|
|
171
|
+
});
|
|
298
172
|
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
173
|
+
/** Validate under the writer transaction before any DDL; initialize only a fresh store. */
|
|
174
|
+
export const ensureDoStorageLayout = Effect.fn("DoStorage.initializeLayout")(function* () {
|
|
175
|
+
const sql = (yield* SqlClient.SqlClient).withoutTransforms();
|
|
176
|
+
|
|
177
|
+
return yield* sql
|
|
178
|
+
.withTransaction(
|
|
179
|
+
Effect.gen(function* () {
|
|
180
|
+
const header = yield* inspectStorage();
|
|
181
|
+
|
|
182
|
+
if (header !== undefined) return header;
|
|
183
|
+
for (const statement of layoutStatements) yield* sql.unsafe(statement).withoutTransform;
|
|
184
|
+
yield* createSqlThreadWorkTables().pipe(
|
|
185
|
+
Effect.mapError((cause) =>
|
|
186
|
+
DoStorageError.make({
|
|
187
|
+
operation: "initialize work catalogue",
|
|
188
|
+
message: cause.message,
|
|
189
|
+
cause,
|
|
190
|
+
}),
|
|
191
|
+
),
|
|
192
|
+
);
|
|
193
|
+
yield* sql.unsafe(headerStatement).withoutTransform;
|
|
194
|
+
yield* sql`INSERT INTO effect_agent_schema (singleton, layout_version, record_format) VALUES (1, ${CurrentDoStorageVersion}, ${CURRENT_RECORD_FORMAT})`;
|
|
195
|
+
yield* sql`INSERT INTO effect_agent_meta (key, value) VALUES ('storage_version', ${String(CurrentDoStorageVersion)})`;
|
|
196
|
+
|
|
197
|
+
return yield* readDoStorageHeader();
|
|
198
|
+
}),
|
|
199
|
+
)
|
|
200
|
+
.pipe(Effect.catchTag("SqlError", storageError));
|
|
304
201
|
});
|