@syncular/server 0.21.1 → 0.22.0

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.
@@ -8,6 +8,144 @@
8
8
  */
9
9
  import { sha256Hex } from './scopes.js';
10
10
  export const DEFAULT_SEGMENT_TTL_MS = 24 * 60 * 60 * 1000;
11
+ /** Canonical identity of one publication; two equal keys are one grant. */
12
+ export function publicationKey(metadata) {
13
+ return JSON.stringify([
14
+ metadata.partition,
15
+ metadata.logEpoch,
16
+ metadata.table,
17
+ metadata.schemaVersion,
18
+ metadata.mediaType,
19
+ metadata.scopeDigest,
20
+ metadata.asOfCommitSeq,
21
+ metadata.rowCursor,
22
+ metadata.nextRowCursor,
23
+ ]);
24
+ }
25
+ /**
26
+ * Materialize publications for a record written before provenance existed
27
+ * (in-flight 0.21 objects, §5.1). Only the merged top-level context is
28
+ * known, so each recorded digest is one publication under it: that is the
29
+ * old single-record read behavior preserved verbatim.
30
+ */
31
+ export function publicationsForRecord(record) {
32
+ return record.scopeDigests.map((scopeDigest) => ({
33
+ partition: record.partition,
34
+ logEpoch: record.logEpoch,
35
+ table: record.table,
36
+ schemaVersion: record.schemaVersion,
37
+ mediaType: record.mediaType,
38
+ scopeDigest,
39
+ asOfCommitSeq: record.asOfCommitSeq,
40
+ rowCount: record.rowCount,
41
+ rowCursor: record.rowCursor,
42
+ nextRowCursor: record.nextRowCursor,
43
+ byteLength: record.byteLength,
44
+ createdAtMs: record.createdAtMs,
45
+ expiresAtMs: record.expiresAtMs,
46
+ }));
47
+ }
48
+ export function selectSegmentPublication(record, context) {
49
+ for (let index = record.publications.length - 1; index >= 0; index--) {
50
+ const publication = record.publications[index];
51
+ if (publication.partition === context.partition &&
52
+ publication.logEpoch === context.logEpoch &&
53
+ publication.scopeDigest === context.scopeDigest &&
54
+ (context.table === undefined || publication.table === context.table) &&
55
+ (context.schemaVersion === undefined ||
56
+ publication.schemaVersion === context.schemaVersion) &&
57
+ (context.mediaType === undefined ||
58
+ publication.mediaType === context.mediaType) &&
59
+ (context.asOfCommitSeq === undefined ||
60
+ publication.asOfCommitSeq === context.asOfCommitSeq) &&
61
+ (context.rowCursor === undefined ||
62
+ publication.rowCursor === context.rowCursor) &&
63
+ (context.nowMs === undefined || publication.expiresAtMs > context.nowMs)) {
64
+ return publication;
65
+ }
66
+ }
67
+ return undefined;
68
+ }
69
+ /**
70
+ * Project a record onto one publication. The compatibility view is narrowed
71
+ * to that grant: `scopeDigests` and `publications` hold only the chosen
72
+ * publication, so a caller that authorizes from them cannot union a grant
73
+ * from a different partition or epoch.
74
+ */
75
+ export function publicationRecord(record, publication) {
76
+ return {
77
+ ...record,
78
+ ...publication,
79
+ scopeDigests: [publication.scopeDigest],
80
+ publications: [publication],
81
+ };
82
+ }
83
+ /** Byte equality for the content-address collision check (§5.1). */
84
+ export function segmentBytesEqual(a, b) {
85
+ if (a.length !== b.length)
86
+ return false;
87
+ for (let i = 0; i < a.length; i++) {
88
+ if (a[i] !== b[i])
89
+ return false;
90
+ }
91
+ return true;
92
+ }
93
+ /**
94
+ * Merge one publication into the stored entry for the same content address
95
+ * (§5.1). Identical bytes are one entry: the record takes the incoming
96
+ * publication's metadata (a later publisher's descriptor, scope digest, and
97
+ * cursors stay consistent with what it just published), the scope digests
98
+ * union with the incoming digest first (the record's primary `scopeDigest`),
99
+ * `createdAtMs` keeps the earliest sighting, and `expiresAtMs` takes the
100
+ * later expiry. A publication with the incoming context merges into that
101
+ * publication's own lifetime; a publication with a new context is appended,
102
+ * so no context's grant is overwritten or extended by another. Publications
103
+ * whose own TTL has elapsed at `nowMs` are pruned from the stored entry —
104
+ * SPEC §5.5 lets the cache forget expired grants, and pruning on put keeps
105
+ * the publication set bounded by live contexts instead of by every context
106
+ * ever seen. Callers MUST have proven the bytes identical first.
107
+ */
108
+ export function mergeSegmentRecord(existing, metadata, segmentId, byteLength, nowMs, ttlMs) {
109
+ // Prune grants that have already elapsed at this write (§5.5): bounded by
110
+ // live contexts, not by the unbounded set of contexts ever published.
111
+ const prior = (existing?.publications ?? []).filter((publication) => publication.expiresAtMs > nowMs);
112
+ const fresh = {
113
+ ...metadata,
114
+ byteLength,
115
+ createdAtMs: nowMs,
116
+ expiresAtMs: nowMs + ttlMs,
117
+ };
118
+ const key = publicationKey(metadata);
119
+ const at = prior.findIndex((publication) => publicationKey(publication) === key);
120
+ // Re-publication of one context merges that publication's lifetime only:
121
+ // an unrelated refresh must not extend another publication's grant (§5.1).
122
+ const publications = at < 0
123
+ ? [...prior, fresh]
124
+ : prior.map((publication, index) => index === at
125
+ ? {
126
+ ...fresh,
127
+ createdAtMs: Math.min(publication.createdAtMs, nowMs),
128
+ expiresAtMs: Math.max(publication.expiresAtMs, nowMs + ttlMs),
129
+ }
130
+ : publication);
131
+ return {
132
+ ...metadata,
133
+ segmentId,
134
+ // Compatibility view over the retained publications only, incoming
135
+ // primary first: a pruned publication's digest is gone, not just its
136
+ // descriptor.
137
+ scopeDigests: [
138
+ metadata.scopeDigest,
139
+ ...new Set(publications
140
+ .map((publication) => publication.scopeDigest)
141
+ .filter((digest) => digest !== metadata.scopeDigest)),
142
+ ],
143
+ publications,
144
+ byteLength,
145
+ createdAtMs: Math.min(existing?.createdAtMs ?? nowMs, nowMs),
146
+ expiresAtMs: Math.max(existing?.expiresAtMs ?? nowMs + ttlMs, nowMs + ttlMs),
147
+ };
148
+ }
11
149
  export async function segmentIdFor(bytes) {
12
150
  return `sha256:${await sha256Hex(bytes)}`;
13
151
  }
@@ -19,13 +157,11 @@ export class MemorySegmentStore {
19
157
  }
20
158
  async put(metadata, bytes, nowMs) {
21
159
  const segmentId = await segmentIdFor(bytes);
22
- const record = {
23
- ...metadata,
24
- segmentId,
25
- byteLength: bytes.length,
26
- createdAtMs: nowMs,
27
- expiresAtMs: nowMs + this.#ttlMs,
28
- };
160
+ const existing = this.#entries.get(segmentId);
161
+ if (existing !== undefined && !segmentBytesEqual(existing.bytes, bytes)) {
162
+ throw new Error(`MemorySegmentStore: content-address collision at ${segmentId} (§5.1)`);
163
+ }
164
+ const record = mergeSegmentRecord(existing?.record, metadata, segmentId, bytes.length, nowMs, this.#ttlMs);
29
165
  this.#entries.set(segmentId, { record, bytes });
30
166
  return record;
31
167
  }
@@ -34,16 +170,19 @@ export class MemorySegmentStore {
34
170
  }
35
171
  async find(key, nowMs) {
36
172
  for (const { record } of this.#entries.values()) {
37
- if (record.partition === key.partition &&
38
- record.logEpoch === key.logEpoch &&
39
- record.table === key.table &&
40
- record.schemaVersion === key.schemaVersion &&
41
- record.mediaType === key.mediaType &&
42
- record.scopeDigest === key.scopeDigest &&
43
- record.asOfCommitSeq === key.asOfCommitSeq &&
44
- record.rowCursor === null &&
45
- record.expiresAtMs > nowMs) {
46
- return record;
173
+ const publication = selectSegmentPublication(record, {
174
+ partition: key.partition,
175
+ logEpoch: key.logEpoch,
176
+ table: key.table,
177
+ schemaVersion: key.schemaVersion,
178
+ mediaType: key.mediaType,
179
+ scopeDigest: key.scopeDigest,
180
+ asOfCommitSeq: key.asOfCommitSeq,
181
+ rowCursor: null,
182
+ nowMs,
183
+ });
184
+ if (publication !== undefined) {
185
+ return publicationRecord(record, publication);
47
186
  }
48
187
  }
49
188
  return undefined;
@@ -129,10 +129,16 @@ export declare function signSegmentToken(key: string | Uint8Array, claims: Segme
129
129
  /**
130
130
  * Verify a segment token per §5.4. Throws `SyncError sync.forbidden` on any
131
131
  * failure (MAC, expiry, or claim mismatch).
132
+ *
133
+ * `scopeDigest` is the set of scope digests the token may authorize. Pass the
134
+ * digests of the stored record's publications that match the verifying host's
135
+ * partition: identical bytes are one content address (§5.1), but a digest
136
+ * recorded under another partition is not this host's grant (§5.4). A single
137
+ * string stays valid for a record with one publication.
132
138
  */
133
139
  export declare function verifySegmentToken(key: string | Uint8Array, token: string, expected: {
134
140
  readonly segmentId: string;
135
- readonly scopeDigest: string;
141
+ readonly scopeDigest: string | readonly string[];
136
142
  readonly audience: string;
137
143
  readonly nowMs: number;
138
144
  readonly skewSeconds?: number;
@@ -108,6 +108,12 @@ const DEFAULT_SKEW_SECONDS = 60;
108
108
  /**
109
109
  * Verify a segment token per §5.4. Throws `SyncError sync.forbidden` on any
110
110
  * failure (MAC, expiry, or claim mismatch).
111
+ *
112
+ * `scopeDigest` is the set of scope digests the token may authorize. Pass the
113
+ * digests of the stored record's publications that match the verifying host's
114
+ * partition: identical bytes are one content address (§5.1), but a digest
115
+ * recorded under another partition is not this host's grant (§5.4). A single
116
+ * string stays valid for a record with one publication.
111
117
  */
112
118
  export async function verifySegmentToken(key, token, expected) {
113
119
  const forbidden = (reason) => syncError('sync.forbidden', `segment token rejected: ${reason}`);
@@ -135,7 +141,10 @@ export async function verifySegmentToken(key, token, expected) {
135
141
  }
136
142
  if (claims.seg !== expected.segmentId)
137
143
  throw forbidden('segment mismatch');
138
- if (claims.sd !== expected.scopeDigest)
144
+ const scopeDigests = typeof expected.scopeDigest === 'string'
145
+ ? [expected.scopeDigest]
146
+ : expected.scopeDigest;
147
+ if (!scopeDigests.includes(claims.sd))
139
148
  throw forbidden('scope-digest mismatch');
140
149
  if (claims.aud !== expected.audience)
141
150
  throw forbidden('audience mismatch');
@@ -29,8 +29,14 @@ import type { StoredChange, StoredCommit, StoredPushResult, StoredRow } from './
29
29
  * range that returns already-ordered rows — the same covering-index shape
30
30
  * the Postgres storage documents (§3.1, performance-by-
31
31
  * construction).
32
+ *
33
+ * `sync_row_scopes_by_row` is the one secondary scope index: a per-row
34
+ * scope replacement predicates on `(partition, tbl, row_id)`, and `row_id`
35
+ * is the LAST PRIMARY KEY column, so that predicate cannot narrow the
36
+ * inverted index. Declared here so every SQLite-family storage (and D1,
37
+ * which applies this same DDL) keeps the same plan shape as Postgres.
32
38
  */
33
- export declare const SQLITE_DDL = "\nCREATE TABLE IF NOT EXISTS sync_partitions(\n partition TEXT PRIMARY KEY,\n max_commit_seq INTEGER NOT NULL DEFAULT 0,\n horizon_seq INTEGER NOT NULL DEFAULT 0\n);\nCREATE TABLE IF NOT EXISTS sync_partition_registry(\n partition TEXT PRIMARY KEY,\n log_epoch TEXT NOT NULL,\n epoch_required INTEGER NOT NULL DEFAULT 0,\n last_authenticated_at_ms INTEGER NOT NULL\n);\nCREATE TABLE IF NOT EXISTS sync_row_scopes(\n partition TEXT NOT NULL, tbl TEXT NOT NULL,\n var TEXT NOT NULL, value TEXT NOT NULL, row_id TEXT NOT NULL,\n PRIMARY KEY(partition, tbl, var, value, row_id)\n);\nCREATE TABLE IF NOT EXISTS sync_commits(\n partition TEXT NOT NULL, commit_seq INTEGER NOT NULL,\n client_id TEXT NOT NULL, client_commit_id TEXT NOT NULL,\n actor_id TEXT NOT NULL, created_at_ms INTEGER NOT NULL,\n PRIMARY KEY(partition, commit_seq)\n);\nCREATE INDEX IF NOT EXISTS sync_commits_by_time\n ON sync_commits(partition, created_at_ms);\nCREATE TABLE IF NOT EXISTS sync_changes(\n partition TEXT NOT NULL, commit_seq INTEGER NOT NULL, idx INTEGER NOT NULL,\n tbl TEXT NOT NULL, row_id TEXT NOT NULL, op INTEGER NOT NULL,\n row_version INTEGER, scopes TEXT NOT NULL, payload BLOB,\n PRIMARY KEY(partition, commit_seq, idx)\n);\nCREATE INDEX IF NOT EXISTS sync_changes_by_table\n ON sync_changes(partition, commit_seq, tbl, idx);\nCREATE TABLE IF NOT EXISTS sync_change_scopes(\n partition TEXT NOT NULL, tbl TEXT NOT NULL,\n var TEXT NOT NULL, value TEXT NOT NULL, commit_seq INTEGER NOT NULL,\n PRIMARY KEY(partition, tbl, var, value, commit_seq)\n);\nCREATE TABLE IF NOT EXISTS sync_push_results(\n partition TEXT NOT NULL, client_id TEXT NOT NULL,\n client_commit_id TEXT NOT NULL, result TEXT NOT NULL,\n PRIMARY KEY(partition, client_id, client_commit_id)\n);\nCREATE TABLE IF NOT EXISTS sync_reactions(\n partition TEXT NOT NULL, idempotency_key TEXT NOT NULL,\n type TEXT NOT NULL, version INTEGER NOT NULL, payload TEXT NOT NULL,\n source_client_id TEXT NOT NULL, source_client_commit_id TEXT NOT NULL,\n source_commit_seq INTEGER NOT NULL, created_at_ms INTEGER NOT NULL,\n available_at_ms INTEGER NOT NULL, status TEXT NOT NULL,\n attempts INTEGER NOT NULL, max_attempts INTEGER NOT NULL,\n lease_owner TEXT, lease_expires_at_ms INTEGER, completed_at_ms INTEGER,\n last_failure TEXT,\n PRIMARY KEY(partition, idempotency_key),\n CHECK(status IN ('pending', 'leased', 'completed', 'dead-letter'))\n);\nCREATE INDEX IF NOT EXISTS sync_reactions_due\n ON sync_reactions(partition, status, available_at_ms, created_at_ms, idempotency_key);\nCREATE INDEX IF NOT EXISTS sync_reactions_lease\n ON sync_reactions(partition, status, lease_expires_at_ms);\nCREATE INDEX IF NOT EXISTS sync_reactions_completed\n ON sync_reactions(partition, status, completed_at_ms, idempotency_key);\nCREATE INDEX IF NOT EXISTS sync_reactions_dead_letter\n ON sync_reactions(partition, status, available_at_ms, idempotency_key);\nCREATE TABLE IF NOT EXISTS sync_clients(\n partition TEXT NOT NULL, client_id TEXT NOT NULL, actor_id TEXT NOT NULL,\n wire_version INTEGER NOT NULL DEFAULT 1,\n cursor INTEGER NOT NULL, subscriptions TEXT NOT NULL,\n updated_at_ms INTEGER NOT NULL,\n PRIMARY KEY(partition, client_id)\n);\nCREATE TABLE IF NOT EXISTS sync_blob_refs(\n partition TEXT NOT NULL, tbl TEXT NOT NULL, row_id TEXT NOT NULL,\n blob_id TEXT NOT NULL,\n PRIMARY KEY(partition, tbl, row_id, blob_id)\n);\nCREATE INDEX IF NOT EXISTS sync_blob_refs_by_blob\n ON sync_blob_refs(partition, blob_id);\nCREATE TABLE IF NOT EXISTS sync_tombstones(\n partition TEXT NOT NULL, tbl TEXT NOT NULL, row_id TEXT NOT NULL,\n commit_seq INTEGER NOT NULL,\n PRIMARY KEY(partition, tbl, row_id)\n);\n";
39
+ export declare const SQLITE_DDL = "\nCREATE TABLE IF NOT EXISTS sync_partitions(\n partition TEXT PRIMARY KEY,\n max_commit_seq INTEGER NOT NULL DEFAULT 0,\n horizon_seq INTEGER NOT NULL DEFAULT 0\n);\nCREATE TABLE IF NOT EXISTS sync_partition_registry(\n partition TEXT PRIMARY KEY,\n log_epoch TEXT NOT NULL,\n epoch_required INTEGER NOT NULL DEFAULT 0,\n last_authenticated_at_ms INTEGER NOT NULL\n);\nCREATE TABLE IF NOT EXISTS sync_row_scopes(\n partition TEXT NOT NULL, tbl TEXT NOT NULL,\n var TEXT NOT NULL, value TEXT NOT NULL, row_id TEXT NOT NULL,\n PRIMARY KEY(partition, tbl, var, value, row_id)\n);\nCREATE INDEX IF NOT EXISTS sync_row_scopes_by_row\n ON sync_row_scopes(partition, tbl, row_id);\nCREATE TABLE IF NOT EXISTS sync_commits(\n partition TEXT NOT NULL, commit_seq INTEGER NOT NULL,\n client_id TEXT NOT NULL, client_commit_id TEXT NOT NULL,\n actor_id TEXT NOT NULL, created_at_ms INTEGER NOT NULL,\n PRIMARY KEY(partition, commit_seq)\n);\nCREATE INDEX IF NOT EXISTS sync_commits_by_time\n ON sync_commits(partition, created_at_ms);\nCREATE TABLE IF NOT EXISTS sync_changes(\n partition TEXT NOT NULL, commit_seq INTEGER NOT NULL, idx INTEGER NOT NULL,\n tbl TEXT NOT NULL, row_id TEXT NOT NULL, op INTEGER NOT NULL,\n row_version INTEGER, scopes TEXT NOT NULL, payload BLOB,\n PRIMARY KEY(partition, commit_seq, idx)\n);\nCREATE INDEX IF NOT EXISTS sync_changes_by_table\n ON sync_changes(partition, commit_seq, tbl, idx);\nCREATE TABLE IF NOT EXISTS sync_change_scopes(\n partition TEXT NOT NULL, tbl TEXT NOT NULL,\n var TEXT NOT NULL, value TEXT NOT NULL, commit_seq INTEGER NOT NULL,\n PRIMARY KEY(partition, tbl, var, value, commit_seq)\n);\nCREATE TABLE IF NOT EXISTS sync_push_results(\n partition TEXT NOT NULL, client_id TEXT NOT NULL,\n client_commit_id TEXT NOT NULL, result TEXT NOT NULL,\n PRIMARY KEY(partition, client_id, client_commit_id)\n);\nCREATE TABLE IF NOT EXISTS sync_reactions(\n partition TEXT NOT NULL, idempotency_key TEXT NOT NULL,\n type TEXT NOT NULL, version INTEGER NOT NULL, payload TEXT NOT NULL,\n source_client_id TEXT NOT NULL, source_client_commit_id TEXT NOT NULL,\n source_commit_seq INTEGER NOT NULL, created_at_ms INTEGER NOT NULL,\n available_at_ms INTEGER NOT NULL, status TEXT NOT NULL,\n attempts INTEGER NOT NULL, max_attempts INTEGER NOT NULL,\n lease_owner TEXT, lease_expires_at_ms INTEGER, completed_at_ms INTEGER,\n last_failure TEXT,\n PRIMARY KEY(partition, idempotency_key),\n CHECK(status IN ('pending', 'leased', 'completed', 'dead-letter'))\n);\nCREATE INDEX IF NOT EXISTS sync_reactions_due\n ON sync_reactions(partition, status, available_at_ms, created_at_ms, idempotency_key);\nCREATE INDEX IF NOT EXISTS sync_reactions_lease\n ON sync_reactions(partition, status, lease_expires_at_ms);\nCREATE INDEX IF NOT EXISTS sync_reactions_completed\n ON sync_reactions(partition, status, completed_at_ms, idempotency_key);\nCREATE INDEX IF NOT EXISTS sync_reactions_dead_letter\n ON sync_reactions(partition, status, available_at_ms, idempotency_key);\nCREATE TABLE IF NOT EXISTS sync_clients(\n partition TEXT NOT NULL, client_id TEXT NOT NULL, actor_id TEXT NOT NULL,\n wire_version INTEGER NOT NULL DEFAULT 1,\n cursor INTEGER NOT NULL, subscriptions TEXT NOT NULL,\n updated_at_ms INTEGER NOT NULL,\n PRIMARY KEY(partition, client_id)\n);\nCREATE TABLE IF NOT EXISTS sync_blob_refs(\n partition TEXT NOT NULL, tbl TEXT NOT NULL, row_id TEXT NOT NULL,\n blob_id TEXT NOT NULL,\n PRIMARY KEY(partition, tbl, row_id, blob_id)\n);\nCREATE INDEX IF NOT EXISTS sync_blob_refs_by_blob\n ON sync_blob_refs(partition, blob_id);\nCREATE TABLE IF NOT EXISTS sync_tombstones(\n partition TEXT NOT NULL, tbl TEXT NOT NULL, row_id TEXT NOT NULL,\n commit_seq INTEGER NOT NULL,\n PRIMARY KEY(partition, tbl, row_id)\n);\n";
34
40
  /** Split the DDL into individual statements (D1 applies them one by one). */
35
41
  export declare function sqliteDdlStatements(): string[];
36
42
  /** `?,?,…` for an `IN (…)` clause of `count` positional parameters. */
@@ -11,6 +11,12 @@ import { matchesEffective } from './scopes.js';
11
11
  * range that returns already-ordered rows — the same covering-index shape
12
12
  * the Postgres storage documents (§3.1, performance-by-
13
13
  * construction).
14
+ *
15
+ * `sync_row_scopes_by_row` is the one secondary scope index: a per-row
16
+ * scope replacement predicates on `(partition, tbl, row_id)`, and `row_id`
17
+ * is the LAST PRIMARY KEY column, so that predicate cannot narrow the
18
+ * inverted index. Declared here so every SQLite-family storage (and D1,
19
+ * which applies this same DDL) keeps the same plan shape as Postgres.
14
20
  */
15
21
  export const SQLITE_DDL = `
16
22
  CREATE TABLE IF NOT EXISTS sync_partitions(
@@ -29,6 +35,8 @@ CREATE TABLE IF NOT EXISTS sync_row_scopes(
29
35
  var TEXT NOT NULL, value TEXT NOT NULL, row_id TEXT NOT NULL,
30
36
  PRIMARY KEY(partition, tbl, var, value, row_id)
31
37
  );
38
+ CREATE INDEX IF NOT EXISTS sync_row_scopes_by_row
39
+ ON sync_row_scopes(partition, tbl, row_id);
32
40
  CREATE TABLE IF NOT EXISTS sync_commits(
33
41
  partition TEXT NOT NULL, commit_seq INTEGER NOT NULL,
34
42
  client_id TEXT NOT NULL, client_commit_id TEXT NOT NULL,
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * SQLite-backed segment store over the shared synchronous driver.
3
3
  */
4
- import { DEFAULT_SEGMENT_TTL_MS, segmentIdFor, } from './segment-store.js';
4
+ import { DEFAULT_SEGMENT_TTL_MS, mergeSegmentRecord, publicationKey, publicationRecord, publicationsForRecord, segmentBytesEqual, segmentIdFor, selectSegmentPublication, } from './segment-store.js';
5
5
  import { SqliteAdapterRequiredError, } from './sqlite-driver.js';
6
6
  export class SqliteSegmentStore {
7
7
  db;
@@ -23,6 +23,33 @@ export class SqliteSegmentStore {
23
23
  byte_length INTEGER NOT NULL, created_at_ms INTEGER NOT NULL,
24
24
  expires_at_ms INTEGER NOT NULL, bytes BLOB NOT NULL
25
25
  );
26
+ `);
27
+ // One row per scope digest a content address was published under (§5.1):
28
+ // identical bytes from two scopes share a `segment_id` and must both stay
29
+ // downloadable (§5.5).
30
+ this.db.exec(`
31
+ CREATE TABLE IF NOT EXISTS sync_segment_scopes(
32
+ segment_id TEXT NOT NULL, scope_digest TEXT NOT NULL,
33
+ PRIMARY KEY (segment_id, scope_digest)
34
+ );
35
+ `);
36
+ // One row per publication of a content address (§5.1): the same bytes
37
+ // under a different partition, epoch, table, or pin are distinct grants
38
+ // and must each stay downloadable. Rows written before this table existed
39
+ // materialize from the top-level record plus `sync_segment_scopes` on
40
+ // read, so an in-flight 0.21 object loses no publication.
41
+ this.db.exec(`
42
+ CREATE TABLE IF NOT EXISTS sync_segment_publications(
43
+ segment_id TEXT NOT NULL, pub_key TEXT NOT NULL,
44
+ partition TEXT NOT NULL, log_epoch TEXT NOT NULL,
45
+ tbl TEXT NOT NULL, schema_version INTEGER NOT NULL,
46
+ media_type TEXT NOT NULL, scope_digest TEXT NOT NULL,
47
+ as_of_commit_seq INTEGER NOT NULL, row_count INTEGER NOT NULL,
48
+ row_cursor TEXT, next_row_cursor TEXT,
49
+ byte_length INTEGER NOT NULL, created_at_ms INTEGER NOT NULL,
50
+ expires_at_ms INTEGER NOT NULL,
51
+ PRIMARY KEY (segment_id, pub_key)
52
+ );
26
53
  `);
27
54
  const columns = this.db
28
55
  .query('PRAGMA table_info(sync_segments)')
@@ -30,23 +57,89 @@ export class SqliteSegmentStore {
30
57
  if (!columns.some((column) => column.name === 'log_epoch')) {
31
58
  this.db.exec("ALTER TABLE sync_segments ADD COLUMN log_epoch TEXT NOT NULL DEFAULT ''");
32
59
  }
60
+ // Backfill digests recorded before the side table existed, so an
61
+ // upgraded store keeps `find`/download working for cached segments.
62
+ this.db.exec(`INSERT OR IGNORE INTO sync_segment_scopes(segment_id, scope_digest)
63
+ SELECT segment_id, scope_digest FROM sync_segments`);
64
+ }
65
+ /** Every digest recorded for a content address, primary first. */
66
+ #scopeDigestsFor(segmentId, primary) {
67
+ const rows = this.db
68
+ .query('SELECT scope_digest FROM sync_segment_scopes WHERE segment_id=?')
69
+ .all(segmentId);
70
+ const digests = rows.map((row) => row.scope_digest);
71
+ return [primary, ...digests.filter((digest) => digest !== primary)];
72
+ }
73
+ /** Stored publications for a content address, oldest first. */
74
+ #publicationsFor(segmentId) {
75
+ const rows = this.db
76
+ .query('SELECT * FROM sync_segment_publications WHERE segment_id=? ORDER BY rowid')
77
+ .all(segmentId);
78
+ return rows.map((row) => ({
79
+ partition: row.partition,
80
+ logEpoch: row.log_epoch,
81
+ table: row.tbl,
82
+ schemaVersion: row.schema_version,
83
+ mediaType: row.media_type === 'sqlite' ? 'sqlite' : 'rows',
84
+ scopeDigest: row.scope_digest,
85
+ asOfCommitSeq: row.as_of_commit_seq,
86
+ rowCount: row.row_count,
87
+ rowCursor: row.row_cursor,
88
+ nextRowCursor: row.next_row_cursor,
89
+ byteLength: row.byte_length,
90
+ createdAtMs: row.created_at_ms,
91
+ expiresAtMs: row.expires_at_ms,
92
+ }));
33
93
  }
34
94
  async put(metadata, bytes, nowMs) {
35
95
  const segmentId = await segmentIdFor(bytes);
36
- const record = {
37
- ...metadata,
38
- segmentId,
39
- byteLength: bytes.length,
40
- createdAtMs: nowMs,
41
- expiresAtMs: nowMs + this.#ttlMs,
42
- };
43
- this.db
44
- .query(`INSERT OR REPLACE INTO sync_segments(
45
- segment_id, partition, log_epoch, tbl, schema_version, media_type,
46
- scope_digest, as_of_commit_seq, row_count, row_cursor,
47
- next_row_cursor, byte_length, created_at_ms, expires_at_ms, bytes
48
- ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`)
49
- .run(record.segmentId, record.partition, record.logEpoch, record.table, record.schemaVersion, record.mediaType, record.scopeDigest, record.asOfCommitSeq, record.rowCount, record.rowCursor, record.nextRowCursor, record.byteLength, record.createdAtMs, record.expiresAtMs, bytes);
96
+ const existing = await this.get(segmentId);
97
+ if (existing !== undefined && !segmentBytesEqual(existing.bytes, bytes)) {
98
+ throw new Error(`SqliteSegmentStore: content-address collision at ${segmentId} (§5.1)`);
99
+ }
100
+ const record = mergeSegmentRecord(existing?.record, metadata, segmentId, bytes.length, nowMs, this.#ttlMs);
101
+ // The bytes are proven identical to the stored ones, so replacing the
102
+ // row with the merged record (metadata, digest set, merged times) is the
103
+ // merge: the other digests live in `sync_segment_scopes` below. The
104
+ // publication set is rewritten from the merged record in one transaction
105
+ // so a crash cannot leave provenance half-applied.
106
+ this.db.exec('BEGIN IMMEDIATE');
107
+ try {
108
+ this.db
109
+ .query(`INSERT OR REPLACE INTO sync_segments(
110
+ segment_id, partition, log_epoch, tbl, schema_version, media_type,
111
+ scope_digest, as_of_commit_seq, row_count, row_cursor,
112
+ next_row_cursor, byte_length, created_at_ms, expires_at_ms, bytes
113
+ ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`)
114
+ .run(record.segmentId, record.partition, record.logEpoch, record.table, record.schemaVersion, record.mediaType, record.scopeDigest, record.asOfCommitSeq, record.rowCount, record.rowCursor, record.nextRowCursor, record.byteLength, record.createdAtMs, record.expiresAtMs, bytes);
115
+ // Rebuild the digest side table from the merged record so a pruned
116
+ // (expired) publication's digest disappears from the compatibility
117
+ // view instead of lingering as a stale scope row.
118
+ this.db
119
+ .query('DELETE FROM sync_segment_scopes WHERE segment_id=?')
120
+ .run(record.segmentId);
121
+ const insertScope = this.db.query(`INSERT OR IGNORE INTO sync_segment_scopes(segment_id, scope_digest)
122
+ VALUES (?,?)`);
123
+ for (const digest of record.scopeDigests) {
124
+ insertScope.run(record.segmentId, digest);
125
+ }
126
+ this.db
127
+ .query('DELETE FROM sync_segment_publications WHERE segment_id=?')
128
+ .run(record.segmentId);
129
+ const insertPublication = this.db.query(`INSERT INTO sync_segment_publications(
130
+ segment_id, pub_key, partition, log_epoch, tbl, schema_version,
131
+ media_type, scope_digest, as_of_commit_seq, row_count, row_cursor,
132
+ next_row_cursor, byte_length, created_at_ms, expires_at_ms
133
+ ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`);
134
+ for (const publication of record.publications) {
135
+ insertPublication.run(record.segmentId, publicationKey(publication), publication.partition, publication.logEpoch, publication.table, publication.schemaVersion, publication.mediaType, publication.scopeDigest, publication.asOfCommitSeq, publication.rowCount, publication.rowCursor, publication.nextRowCursor, publication.byteLength, publication.createdAtMs, publication.expiresAtMs);
136
+ }
137
+ this.db.exec('COMMIT');
138
+ }
139
+ catch (error) {
140
+ this.db.exec('ROLLBACK');
141
+ throw error;
142
+ }
50
143
  return record;
51
144
  }
52
145
  async get(segmentId) {
@@ -55,40 +148,59 @@ export class SqliteSegmentStore {
55
148
  .get(segmentId);
56
149
  if (row === null)
57
150
  return undefined;
151
+ const scopeDigests = this.#scopeDigestsFor(row.segment_id, row.scope_digest);
152
+ const top = {
153
+ segmentId: row.segment_id,
154
+ partition: row.partition,
155
+ logEpoch: row.log_epoch,
156
+ table: row.tbl,
157
+ schemaVersion: row.schema_version,
158
+ mediaType: row.media_type === 'sqlite' ? 'sqlite' : 'rows',
159
+ scopeDigest: row.scope_digest,
160
+ scopeDigests,
161
+ asOfCommitSeq: row.as_of_commit_seq,
162
+ rowCount: row.row_count,
163
+ rowCursor: row.row_cursor,
164
+ nextRowCursor: row.next_row_cursor,
165
+ byteLength: row.byte_length,
166
+ createdAtMs: row.created_at_ms,
167
+ expiresAtMs: row.expires_at_ms,
168
+ };
169
+ const stored = this.#publicationsFor(row.segment_id);
58
170
  return {
59
171
  record: {
60
- segmentId: row.segment_id,
61
- partition: row.partition,
62
- logEpoch: row.log_epoch,
63
- table: row.tbl,
64
- schemaVersion: row.schema_version,
65
- mediaType: row.media_type === 'sqlite' ? 'sqlite' : 'rows',
66
- scopeDigest: row.scope_digest,
67
- asOfCommitSeq: row.as_of_commit_seq,
68
- rowCount: row.row_count,
69
- rowCursor: row.row_cursor,
70
- nextRowCursor: row.next_row_cursor,
71
- byteLength: row.byte_length,
72
- createdAtMs: row.created_at_ms,
73
- expiresAtMs: row.expires_at_ms,
172
+ ...top,
173
+ publications: stored.length > 0 ? stored : publicationsForRecord(top),
74
174
  },
75
175
  bytes: new Uint8Array(row.bytes),
76
176
  };
77
177
  }
78
178
  async find(key, nowMs) {
79
- const row = this.db
80
- .query(`SELECT segment_id, row_count, next_row_cursor, byte_length,
81
- created_at_ms, expires_at_ms
82
- FROM sync_segments
179
+ // A publication row is the provenance index; a record written before the
180
+ // table existed falls back to the legacy top-level/scope join below.
181
+ const publication = this.db
182
+ .query(`SELECT segment_id FROM sync_segment_publications
83
183
  WHERE partition=? AND log_epoch=? AND tbl=? AND schema_version=? AND media_type=?
84
184
  AND scope_digest=? AND as_of_commit_seq=? AND row_cursor IS NULL
85
185
  AND expires_at_ms > ?
86
186
  LIMIT 1`)
87
187
  .get(key.partition, key.logEpoch, key.table, key.schemaVersion, key.mediaType, key.scopeDigest, key.asOfCommitSeq, nowMs);
88
- if (row === null)
188
+ const legacy = this.db
189
+ .query(`SELECT s.segment_id
190
+ FROM sync_segments s
191
+ JOIN sync_segment_scopes sc ON sc.segment_id = s.segment_id
192
+ WHERE s.partition=? AND s.log_epoch=? AND s.tbl=? AND s.schema_version=? AND s.media_type=?
193
+ AND sc.scope_digest=? AND s.as_of_commit_seq=? AND s.row_cursor IS NULL
194
+ AND s.expires_at_ms > ?
195
+ LIMIT 1`)
196
+ .get(key.partition, key.logEpoch, key.table, key.schemaVersion, key.mediaType, key.scopeDigest, key.asOfCommitSeq, nowMs);
197
+ const segmentId = publication?.segment_id ?? legacy?.segment_id;
198
+ if (segmentId === undefined)
89
199
  return undefined;
90
- return {
91
- segmentId: row.segment_id,
200
+ const entry = await this.get(segmentId);
201
+ if (entry === undefined)
202
+ return undefined;
203
+ const match = selectSegmentPublication(entry.record, {
92
204
  partition: key.partition,
93
205
  logEpoch: key.logEpoch,
94
206
  table: key.table,
@@ -96,13 +208,12 @@ export class SqliteSegmentStore {
96
208
  mediaType: key.mediaType,
97
209
  scopeDigest: key.scopeDigest,
98
210
  asOfCommitSeq: key.asOfCommitSeq,
99
- rowCount: row.row_count,
100
211
  rowCursor: null,
101
- nextRowCursor: row.next_row_cursor,
102
- byteLength: row.byte_length,
103
- createdAtMs: row.created_at_ms,
104
- expiresAtMs: row.expires_at_ms,
105
- };
212
+ nowMs,
213
+ });
214
+ if (match === undefined)
215
+ return undefined;
216
+ return publicationRecord(entry.record, match);
106
217
  }
107
218
  async stats() {
108
219
  const row = this.db
@@ -10,7 +10,7 @@ import { StorageQueryError } from './storage-errors.js';
10
10
  */
11
11
  import { bindAuthoritativePartition, prepareAuthoritativeQuery, } from './authoritative-query.js';
12
12
  import { syncError } from './errors.js';
13
- import { commitWindowPageSql, deleteRowSql, deleteSqliteRowScopesSql, dropTableDdl, indexRowPageStatement, layoutsOf, migratePayload, parseLayouts, retiredTableNames, rewritePlan, rewriteRowSql, rewriteValues, SCHEMA_META_DDL_SQLITE, scanRowPageSql, schemaDdl, selectRowScopesSql, selectRowSql, selectRowsForRewriteSql, upsertSql, upsertValues, } from './relational-rows.js';
13
+ import { assertPhysicalColumns, commitWindowPageSql, deleteRowSql, deleteSqliteRowScopesSql, dropTableDdl, indexRowPageStatement, layoutsOf, migratePayload, parseLayouts, retiredTableNames, rewritePlan, rewriteRowSql, rewriteValues, SCHEMA_META_DDL_SQLITE, scanRowPageSql, schemaDdl, selectRowScopesSql, selectRowSql, selectRowsForRewriteSql, upsertSql, upsertValues, } from './relational-rows.js';
14
14
  import { matchesEffective } from './scopes.js';
15
15
  import { collectCommitWindowPage, deserializePushResult, SQLITE_DDL, serializePushResult, toStoredRow, } from './sqlite-dialect.js';
16
16
  import { SqliteAdapterRequiredError, } from './sqlite-driver.js';
@@ -267,6 +267,55 @@ export class SqliteServerStorage {
267
267
  if (marker !== null && marker.schema_version > schema.version) {
268
268
  throw new Error(`stored schema version ${marker.schema_version} is newer than the configured schema (${schema.version}) — refusing to run an older server against a migrated database`);
269
269
  }
270
+ if (marker !== null && marker.schema_version === schema.version) {
271
+ // Version equality is not layout equality: a marker written by another
272
+ // build at the same version describes rows the running codec cannot
273
+ // decode. Compare the stored layouts instead of trusting the number.
274
+ const storedLayouts = parseLayouts(marker.layouts);
275
+ const configuredLayouts = parseLayouts(layoutsOf(schema));
276
+ let mismatch;
277
+ for (const [tableName, columns] of Object.entries(configuredLayouts)) {
278
+ const stored = storedLayouts[tableName];
279
+ const columnCount = Math.max(columns.length, stored?.length ?? 0);
280
+ for (let index = 0; index < columnCount; index++) {
281
+ const expected = columns[index];
282
+ const actual = stored?.[index];
283
+ if (expected !== undefined &&
284
+ actual !== undefined &&
285
+ actual.name === expected.name &&
286
+ actual.type === expected.type &&
287
+ actual.nullable === expected.nullable) {
288
+ continue;
289
+ }
290
+ mismatch = `table ${JSON.stringify(tableName)} column ${JSON.stringify(expected?.name ?? actual?.name ?? '')}`;
291
+ break;
292
+ }
293
+ if (mismatch !== undefined)
294
+ break;
295
+ }
296
+ if (mismatch === undefined) {
297
+ for (const tableName of Object.keys(storedLayouts)) {
298
+ if (!(tableName in configuredLayouts)) {
299
+ mismatch = `table ${JSON.stringify(tableName)}`;
300
+ break;
301
+ }
302
+ }
303
+ }
304
+ if (mismatch !== undefined) {
305
+ throw new Error(`stored schema layouts disagree with the configured schema at version ${schema.version} (${mismatch}) — refusing to serve a database whose stored rows the running code cannot decode`);
306
+ }
307
+ // The persisted layouts describe the codec's app columns only: read the
308
+ // physical tables so a same-version database missing a
309
+ // storage-internal column is refused at startup instead of failing at
310
+ // the first write.
311
+ for (const table of schema.tables.values()) {
312
+ const escapedTableName = table.name.replaceAll('"', '""');
313
+ const columns = this.db
314
+ .query(`PRAGMA table_info("${escapedTableName}")`)
315
+ .all();
316
+ assertPhysicalColumns(table, new Set(columns.map((c) => c.name)));
317
+ }
318
+ }
270
319
  if (marker === null || marker.schema_version < schema.version) {
271
320
  // Introspect existing app tables, then apply the migration subset
272
321
  // (CREATE TABLE / ADD COLUMN / rebuild indexes) to reach `schema`, then
package/dist/storage.d.ts CHANGED
@@ -388,6 +388,19 @@ export interface ServerStorage {
388
388
  listPartitionRegistry(): Promise<PartitionRegistryEntry[]>;
389
389
  /** Read continuity without refreshing authenticated activity. */
390
390
  getPartitionLogEpoch(partition: string): Promise<string | undefined>;
391
+ /**
392
+ * Open a storage transaction. This is the raw primitive, not a command
393
+ * boundary: it acquires NO partition write lock. In-tree SQLite begins
394
+ * `IMMEDIATE`, so a hand-rolled command on SQLite happens to be serialized
395
+ * against a concurrent push; PostgreSQL's `BEGIN` takes no row lock, so the
396
+ * same command races a push on Postgres and a SQLite-only test conceals it.
397
+ *
398
+ * A trusted command must therefore run through `registerRemoteCommand`,
399
+ * which builds after `lockPartitionForPush` and the idempotency recheck and
400
+ * shares the committed transaction, or call `lockPartitionForPush` before
401
+ * its first authority, version, or replay read. SSP2 request handling does
402
+ * this for you; only host-authored commands choose.
403
+ */
391
404
  begin(partition: string): Promise<StorageTransaction>;
392
405
  getMaxCommitSeq(partition: string): Promise<number>;
393
406
  getHorizonSeq(partition: string): Promise<number>;
@@ -436,6 +449,18 @@ export interface ServerStorage {
436
449
  * Optional registered-query capability. The implementation MUST replace
437
450
  * every generated app-table relation with a partition-filtered relation and
438
451
  * return rows plus maxCommitSeq from one consistent database snapshot.
452
+ *
453
+ * The storage serializes this call behind an open transaction on a
454
+ * single-connection executor (SQLite, PGlite). A validator runs inside the
455
+ * push transaction, so on that executor a validator that calls
456
+ * `queryAuthoritative` waits on the transaction it is already inside and
457
+ * the push never completes. A pool-backed executor (`pg`, `Bun.sql`) hands
458
+ * the call a second connection instead: the validator does not deadlock,
459
+ * but it reads committed state without the candidate write. On either
460
+ * executor read candidate state through the whole-commit `CommitValidator`
461
+ * reader (§6.8). A call made from any other context waits for the open
462
+ * transaction to finish on a single-connection executor, and runs
463
+ * concurrently on a pool-backed one.
439
464
  */
440
465
  queryAuthoritative?(partition: string, query: AuthoritativeQueryRequest): Promise<AuthoritativeQueryResult>;
441
466
  /**