or3-provider-sqlite 0.0.30 → 0.0.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. package/README.md +83 -3
  2. package/dist/module.json +1 -1
  3. package/dist/runtime/server/admin/adapters/sync-sqlite.js +9 -0
  4. package/dist/runtime/server/background-jobs/sqlite-provider.js +18 -1
  5. package/dist/runtime/server/db/kysely.js +11 -2
  6. package/dist/runtime/server/db/migrate.js +25 -2
  7. package/dist/runtime/server/db/migration-schema.d.ts +2 -0
  8. package/dist/runtime/server/db/migration-schema.js +14 -0
  9. package/dist/runtime/server/db/migrations/006_sync_snapshots.js +10 -21
  10. package/dist/runtime/server/db/migrations/007_device_cursor_ownership.js +2 -1
  11. package/dist/runtime/server/db/migrations/019_background_generation_history.js +5 -16
  12. package/dist/runtime/server/db/migrations/021_background_request_usage.js +2 -1
  13. package/dist/runtime/server/db/migrations/033_tool_result_operation_ids.js +8 -2
  14. package/dist/runtime/server/db/migrations/034_sync_intent_fingerprint.d.ts +3 -0
  15. package/dist/runtime/server/db/migrations/034_sync_intent_fingerprint.js +8 -0
  16. package/dist/runtime/server/db/migrations/035_sync_unavailable_records.d.ts +3 -0
  17. package/dist/runtime/server/db/migrations/035_sync_unavailable_records.js +24 -0
  18. package/dist/runtime/server/db/migrations/036_sync_history_incarnation.d.ts +3 -0
  19. package/dist/runtime/server/db/migrations/036_sync_history_incarnation.js +28 -0
  20. package/dist/runtime/server/db/schema.d.ts +5 -0
  21. package/dist/runtime/server/sync/canonical-project-ownership.d.ts +1 -1
  22. package/dist/runtime/server/sync/canonical-project-ownership.js +29 -12
  23. package/dist/runtime/server/sync/evidence-maintenance.d.ts +20 -0
  24. package/dist/runtime/server/sync/evidence-maintenance.js +130 -0
  25. package/dist/runtime/server/sync/history-incarnation.d.ts +8 -0
  26. package/dist/runtime/server/sync/history-incarnation.js +54 -0
  27. package/dist/runtime/server/sync/sqlite-sync-gateway-adapter.d.ts +28 -4
  28. package/dist/runtime/server/sync/sqlite-sync-gateway-adapter.js +802 -356
  29. package/dist/runtime/server/sync/unavailable-records.d.ts +30 -0
  30. package/dist/runtime/server/sync/unavailable-records.js +55 -0
  31. package/package.json +1 -1
@@ -0,0 +1,30 @@
1
+ import { type D1SqlStatement } from '../db/d1.js';
2
+ export declare const SYNC_ROW_BYTES: number;
3
+ export declare const SYNC_METADATA_BYTES: number;
4
+ export declare function syncMetadataBytes(...columns: string[]): string;
5
+ /** Legacy snapshot revisions have no stamp-length limit. Bound their transfer
6
+ * at the page ceiling, preserving originals SQL-to-SQL for larger records. */
7
+ export declare function boundedSyncMetadata(column: string, bytes?: string): string;
8
+ /** SQL CASE, rather than WHERE order, guards both payload transfer and JSON evaluation. */
9
+ export declare function boundedSyncPayload(column: string): string;
10
+ export declare function notificationOwnerSql(column: string): string;
11
+ export declare function syncRows<T>(query: string, ...parameters: unknown[]): Promise<T[]>;
12
+ export type EvidenceSource = {
13
+ historyIncarnation: string;
14
+ workspaceId: string;
15
+ source: 'pull' | 'snapshot' | 'canonical';
16
+ locator: string;
17
+ revisionSql: string;
18
+ table: string;
19
+ where: string;
20
+ parameters: unknown[];
21
+ payload: string;
22
+ metadata: string;
23
+ };
24
+ /** Original bytes travel only database-to-database, including oversized records. */
25
+ export declare function retainSqlEvidence(source: EvidenceSource, reason: string): Promise<string>;
26
+ /** Shared SQL retention for synchronous native push transactions and D1 batches. */
27
+ export declare function evidenceInsert(source: EvidenceSource, reason: string): D1SqlStatement;
28
+ export declare function evidenceLookup(source: EvidenceSource): D1SqlStatement;
29
+ /** Capture unknown notification owners only into privileged evidence, never a workspace marker. */
30
+ export declare function unknownNotificationEvidence(workspaceId: string): D1SqlStatement;
@@ -0,0 +1,55 @@
1
+ import { historyAssertion, historyBatch } from "./history-incarnation.js";
2
+ import { randomUUID } from "node:crypto";
3
+ import { getRawDb, isD1Driver } from "../db/kysely.js";
4
+ import { d1All } from "../db/d1.js";
5
+ export const SYNC_ROW_BYTES = 256 * 1024;
6
+ export const SYNC_METADATA_BYTES = 1024 * 1024;
7
+ export function syncMetadataBytes(...columns) {
8
+ return columns.map((column) => `coalesce(length(cast(${column} AS blob)),0)`).join(" + ");
9
+ }
10
+ export function boundedSyncMetadata(column, bytes = syncMetadataBytes(column)) {
11
+ return `CASE WHEN (${bytes}) <= ${SYNC_METADATA_BYTES} THEN ${column} ELSE NULL END`;
12
+ }
13
+ export function boundedSyncPayload(column) {
14
+ return `CASE WHEN coalesce(length(cast(${column} AS blob)),0) <= ${SYNC_ROW_BYTES} THEN ${column} ELSE NULL END`;
15
+ }
16
+ export function notificationOwnerSql(column) {
17
+ return `CASE WHEN json_valid(${column}) THEN CASE WHEN json_type(${column}, '$.user_id') = 'text'
18
+ THEN CASE WHEN length(cast(json_extract(${column}, '$.user_id') AS blob)) BETWEEN 1 AND 1024 THEN json_extract(${column}, '$.user_id') END END END`;
19
+ }
20
+ export async function syncRows(query, ...parameters) {
21
+ return isD1Driver() ? d1All(query, ...parameters) : getRawDb().prepare(query).all(...parameters);
22
+ }
23
+ export async function retainSqlEvidence(source, reason) {
24
+ await historyBatch([historyAssertion(source.historyIncarnation), evidenceInsert(source, reason)], source.historyIncarnation);
25
+ const lookup = evidenceLookup(source);
26
+ const [retained] = await syncRows(lookup.sql, ...lookup.parameters);
27
+ if (!retained) throw new Error("Sync evidence source disappeared; coverage was not acknowledged");
28
+ return retained.id;
29
+ }
30
+ export function evidenceInsert(source, reason) {
31
+ return {
32
+ sql: `INSERT OR IGNORE INTO sync_unavailable_records
33
+ (id, workspace_id, source, locator, revision, payload_json, metadata_json, reason, status, created_at)
34
+ SELECT ?, ?, ?, ?, json_array(?, ${source.revisionSql}), ${source.payload}, json_set(${source.metadata}, '$.historyIncarnation', ?), ?, 'unresolved', unixepoch()
35
+ FROM ${source.table} WHERE ${source.where}`,
36
+ parameters: [randomUUID(), source.workspaceId, source.source, source.locator, source.historyIncarnation, source.historyIncarnation, reason, ...source.parameters]
37
+ };
38
+ }
39
+ export function evidenceLookup(source) {
40
+ return {
41
+ sql: `SELECT id FROM sync_unavailable_records
42
+ WHERE workspace_id = ? AND source = ? AND locator = ? AND revision = (SELECT json_array(?, ${source.revisionSql}) FROM ${source.table} WHERE ${source.where})`,
43
+ parameters: [source.workspaceId, source.source, source.locator, source.historyIncarnation, ...source.parameters]
44
+ };
45
+ }
46
+ export function unknownNotificationEvidence(workspaceId) {
47
+ return { sql: `INSERT OR IGNORE INTO sync_unavailable_records
48
+ (id, workspace_id, source, locator, revision, payload_json, metadata_json, reason, status, created_at)
49
+ SELECT lower(hex(randomblob(16))), workspace_id, 'canonical', json_array('notifications', id),
50
+ json_array((SELECT incarnation FROM sync_history WHERE id=1), json_array(clock, hlc, op_id, deleted)), data_json,
51
+ json_object('historyIncarnation',(SELECT incarnation FROM sync_history WHERE id=1),'table_name','notifications','pk',id,'clock',clock,'hlc',hlc,'op_id',op_id,'deleted',deleted),
52
+ 'notification_owner_unavailable', 'unresolved', unixepoch()
53
+ FROM s_notifications WHERE workspace_id = ? AND
54
+ coalesce(length(cast(${notificationOwnerSql("data_json")} AS blob)),0) = 0`, parameters: [workspaceId] };
55
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "or3-provider-sqlite",
3
- "version": "0.0.30",
3
+ "version": "0.0.31",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/Saluana/or3-provider-sqlite.git"