@tensorix/metahub 0.1.11 → 0.2.1

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 (67) hide show
  1. package/dist/cli.js +23711 -17899
  2. package/dist/cli.js.map +84 -59
  3. package/dist/core/blobs-core.d.ts +106 -0
  4. package/dist/core/blobs-core.d.ts.map +1 -0
  5. package/dist/core/blobs.d.ts +110 -0
  6. package/dist/core/blobs.d.ts.map +1 -0
  7. package/dist/core/cache.d.ts +23 -1
  8. package/dist/core/cache.d.ts.map +1 -1
  9. package/dist/core/compact.d.ts +4 -3
  10. package/dist/core/compact.d.ts.map +1 -1
  11. package/dist/core/config.d.ts +7 -0
  12. package/dist/core/config.d.ts.map +1 -1
  13. package/dist/core/crdt.d.ts +32 -2
  14. package/dist/core/crdt.d.ts.map +1 -1
  15. package/dist/core/schema-init.d.ts +62 -2
  16. package/dist/core/schema-init.d.ts.map +1 -1
  17. package/dist/core/schema.d.ts +1 -1
  18. package/dist/core/schema.d.ts.map +1 -1
  19. package/dist/core/shares.d.ts +60 -0
  20. package/dist/core/shares.d.ts.map +1 -0
  21. package/dist/core/sites-core.d.ts +35 -0
  22. package/dist/core/sites-core.d.ts.map +1 -1
  23. package/dist/core/sites.d.ts +4 -13
  24. package/dist/core/sites.d.ts.map +1 -1
  25. package/dist/core/sync/auth.d.ts +15 -2
  26. package/dist/core/sync/auth.d.ts.map +1 -1
  27. package/dist/core/sync/blob-routes.d.ts +12 -0
  28. package/dist/core/sync/blob-routes.d.ts.map +1 -0
  29. package/dist/core/sync/client.d.ts +7 -0
  30. package/dist/core/sync/client.d.ts.map +1 -1
  31. package/dist/core/sync/e2ee.d.ts +31 -0
  32. package/dist/core/sync/e2ee.d.ts.map +1 -0
  33. package/dist/core/sync/enroll.d.ts +21 -0
  34. package/dist/core/sync/enroll.d.ts.map +1 -0
  35. package/dist/core/sync/peers-routes.d.ts.map +1 -1
  36. package/dist/core/sync/peers.d.ts +74 -2
  37. package/dist/core/sync/peers.d.ts.map +1 -1
  38. package/dist/core/sync/publisher-lease.d.ts +8 -0
  39. package/dist/core/sync/publisher-lease.d.ts.map +1 -0
  40. package/dist/core/sync/routes.d.ts.map +1 -1
  41. package/dist/core/sync/server.d.ts +1 -0
  42. package/dist/core/sync/server.d.ts.map +1 -1
  43. package/dist/core/sync/share-actions.d.ts +69 -0
  44. package/dist/core/sync/share-actions.d.ts.map +1 -0
  45. package/dist/core/sync/share-export.d.ts +72 -0
  46. package/dist/core/sync/share-export.d.ts.map +1 -0
  47. package/dist/core/sync/share-render.d.ts +10 -0
  48. package/dist/core/sync/share-render.d.ts.map +1 -0
  49. package/dist/core/sync/share-serve.d.ts +3 -0
  50. package/dist/core/sync/share-serve.d.ts.map +1 -0
  51. package/dist/core/sync/sites-serve.d.ts.map +1 -1
  52. package/dist/core/sync/storage-s3-bun.d.ts +35 -0
  53. package/dist/core/sync/storage-s3-bun.d.ts.map +1 -0
  54. package/dist/core/sync/storage-url.d.ts +18 -0
  55. package/dist/core/sync/storage-url.d.ts.map +1 -0
  56. package/dist/core/sync/storage.d.ts +170 -0
  57. package/dist/core/sync/storage.d.ts.map +1 -0
  58. package/dist/db-worker.js +212 -66
  59. package/dist/index.js +13546 -8960
  60. package/dist/index.js.map +46 -27
  61. package/dist/sw.js +1 -1
  62. package/dist/webui/server/assets.d.ts.map +1 -1
  63. package/dist/webui/server/routes.d.ts.map +1 -1
  64. package/dist/webui/server/share-routes.d.ts +3 -0
  65. package/dist/webui/server/share-routes.d.ts.map +1 -0
  66. package/dist/webui.js +93 -61
  67. package/package.json +5 -1
@@ -0,0 +1,106 @@
1
+ import type { DbDriver } from "./driver.ts";
2
+ export declare const POLICY_ID = "default";
3
+ export type Redundancy = "all" | "any";
4
+ export interface BlobPolicy {
5
+ fullNodes: string[];
6
+ redundancy: Redundancy;
7
+ }
8
+ export interface CachedBlob {
9
+ hash: string;
10
+ size: number;
11
+ content_type: string | null;
12
+ last_access: number | null;
13
+ /** Node-local pin: 1 = never auto-evicted / cleared (does not sync). */
14
+ pinned: number;
15
+ /** 1 = bytes produced here, not yet flushed to a durable anchor — protected. */
16
+ pending: number;
17
+ /** Node-local: 1 = the last presence verify confirmed a designated anchor holds
18
+ * this blob (per redundancy). Drives isClearable; reset to 0 on policy change. */
19
+ anchored: number;
20
+ }
21
+ export interface CacheStats {
22
+ /** Total bytes currently held in the node-local cache. */
23
+ totalBytes: number;
24
+ /** Bytes safe to clear (durable on the required full set). */
25
+ clearableBytes: number;
26
+ /** Bytes that cannot be cleared (sole copy / no anchor / this is a full node). */
27
+ retainedBytes: number;
28
+ count: number;
29
+ clearableCount: number;
30
+ }
31
+ export declare function readPolicy(db: DbDriver): BlobPolicy;
32
+ /** Designate the full-blob anchors (synced workspace policy). Each entry is
33
+ * either a node id (a device that keeps everything) OR a bucket url `s3://…`
34
+ * (object storage as a durable full library). Bucket urls never match a node id,
35
+ * so isFullBlobNode()/isClearable() are unaffected — they're a visible guardrail. */
36
+ export declare const setFullNodes: (db: DbDriver, nodeIds: string[]) => void;
37
+ export declare const setRedundancy: (db: DbDriver, redundancy: Redundancy) => void;
38
+ /** Is `node` (default: this device) a designated full-blob library? */
39
+ export declare function isFullBlobNode(db: DbDriver, node?: string): boolean;
40
+ /**
41
+ * Whether this device may safely drop `hash`'s local bytes — a purely LOCAL,
42
+ * offline decision read from local flags. Clearable requires ALL of:
43
+ * 1. This node is not itself the full-blob library (a full node keeps everything).
44
+ * 2. The blob is not a `pending` production (bytes produced here, not yet flushed
45
+ * to an anchor) — the only locally-unique copy.
46
+ * 3. `anchored == 1`: the last presence verify confirmed a designated anchor holds
47
+ * this blob, per redundancy(any/all). Until a verify runs (or after a policy
48
+ * change resets it), anchored is 0 → nothing is clearable (conservative). This
49
+ * replaces the earlier coarse "an anchor is designated" floor with a per-blob,
50
+ * freshly-verified fact. See verifyAnchorPresence (blobs.ts) and
51
+ * docs/impl-context/22-blob-sync (D4, on-demand presence verify).
52
+ */
53
+ export declare function isClearable(db: DbDriver, hash: string): boolean;
54
+ /** Record/clear the per-blob "a designated anchor verifiably holds this" flag. */
55
+ export declare function setAnchored(db: DbDriver, hash: string, anchored: boolean): void;
56
+ export declare function isAnchored(db: DbDriver, hash: string): boolean;
57
+ /** Drop every verified verdict (after a policy change) — forces a re-verify before
58
+ * anything is clearable again. Also clears the last-verified stamp. */
59
+ export declare function invalidateAnchored(db: DbDriver): void;
60
+ /** Epoch-ms of the last successful presence verify, or null if never / invalidated. */
61
+ export declare function readBlobVerifiedAt(db: DbDriver): number | null;
62
+ export declare function writeBlobVerifiedAt(db: DbDriver, ts: number): void;
63
+ /** Blob hashes referenced by a `/blob/<hash>` URL inside a markdown/text value. */
64
+ export declare function blobRefsIn(text: string | null): string[];
65
+ /**
66
+ * Every blob hash a live document or site still points at. Union of:
67
+ * - site_files rows stored as a blob (content = hash), and
68
+ * - `/blob/<hash>` references inside live doc_blocks markdown (doc images).
69
+ * Drives GC (orphan = not in this set) and a full node's byte acquisition.
70
+ */
71
+ export declare function referencedHashes(db: DbDriver): Set<string>;
72
+ /** Record/refresh a blob's ledger row after its bytes land in the cache. */
73
+ /** Record/refresh a cached blob. `pending`: 1 for bytes PRODUCED here (must flush
74
+ * to an anchor before they're clearable), 0 for an ACQUIRED cache copy (already
75
+ * durable at its source). */
76
+ export declare function recordBlob(db: DbDriver, hash: string, size: number, contentType?: string | null, pending?: number): void;
77
+ /** Mark a blob flushed (0) or pending (1). Flush is set after the bytes are
78
+ * confirmed at the durable anchor (see blobs.ts blobMaintenance). */
79
+ export declare function setPending(db: DbDriver, hash: string, pending: boolean): void;
80
+ /** Bump last_access on a cache hit (LRU signal for stats/future eviction). */
81
+ export declare function touchBlob(db: DbDriver, hash: string): void;
82
+ export declare function forgetBlob(db: DbDriver, hash: string): void;
83
+ export declare function cachedBlobs(db: DbDriver): CachedBlob[];
84
+ /** Blobs produced here that are not yet flushed to a durable anchor (the upload
85
+ * worklist; steady state is empty). */
86
+ export declare function pendingBlobs(db: DbDriver): {
87
+ hash: string;
88
+ size: number;
89
+ }[];
90
+ /** Pin/unpin a cached blob (node-local; never synced). Returns true when a row
91
+ * was updated (false when the hash isn't in the ledger). */
92
+ export declare function setPinned(db: DbDriver, hash: string, pinned: boolean): boolean;
93
+ /** Is a cached blob pinned? (false when not in the ledger). */
94
+ export declare function isPinned(db: DbDriver, hash: string): boolean;
95
+ /** Recorded content type for a cached blob (null when unknown / not cached). */
96
+ export declare function blobContentType(db: DbDriver, hash: string): string | null;
97
+ export declare function cacheStats(db: DbDriver): CacheStats;
98
+ export interface KnownNode {
99
+ nodeId: string;
100
+ label: string | null;
101
+ self: boolean;
102
+ }
103
+ /** Distinct node ids this device knows about (self + outbound peers + grantees),
104
+ * for the "pick a full-blob device" selector in Settings / CLI. */
105
+ export declare function knownNodes(db: DbDriver): KnownNode[];
106
+ //# sourceMappingURL=blobs-core.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"blobs-core.d.ts","sourceRoot":"","sources":["../../src/core/blobs-core.ts"],"names":[],"mappings":"AAeA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAI5C,eAAO,MAAM,SAAS,YAAY,CAAC;AAEnC,MAAM,MAAM,UAAU,GAAG,KAAK,GAAG,KAAK,CAAC;AAEvC,MAAM,WAAW,UAAU;IACzB,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,UAAU,EAAE,UAAU,CAAC;CACxB;AAED,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,wEAAwE;IACxE,MAAM,EAAE,MAAM,CAAC;IACf,gFAAgF;IAChF,OAAO,EAAE,MAAM,CAAC;IAChB;uFACmF;IACnF,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,UAAU;IACz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G,UAAU,CAmBnD;AAID,MAAM,WAAW,SAAS;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,IAAI,EAAE,OAAO,CAAC;CACf;AAED;oEACoE;AACpE,wBAAgB,UAAU,CAAC,EAAE,EAAE,QAAQ,GAAG,SAAS,EAAE,CAapD"}
@@ -0,0 +1,110 @@
1
+ import type { DbDriver } from "./driver.ts";
2
+ export * from "./blobs-core.ts";
3
+ export interface KnownBucket {
4
+ url: string;
5
+ label: string | null;
6
+ bucket: string | null;
7
+ }
8
+ /** Attached object-storage buckets (s3 peers), for the "pick a full-blob anchor"
9
+ * selector in Settings. A bucket is a durable full-blob library but is NOT a node
10
+ * (1:N store-and-forward, no single node id), so it lives alongside knownNodes()
11
+ * rather than inside it — designated by its synthetic url, not a node_id. */
12
+ export declare function knownBuckets(db: DbDriver): KnownBucket[];
13
+ /**
14
+ * Resolve a blob's bytes by content hash: local cache → each enabled HTTP peer's
15
+ * `GET /blob/<hash>?local=1` (local=1 keeps the peer from re-resolving, so there
16
+ * are no fetch loops) → each attached bucket's `blobs/<hash>`. Content-addressed,
17
+ * so every source is verified by re-hashing and any source is interchangeable.
18
+ * The first verified hit is cached locally and returned; null when unreachable.
19
+ */
20
+ export declare function resolveBlob(db: DbDriver, hash: string): Promise<Uint8Array | null>;
21
+ export interface VerifyResult {
22
+ /** Blobs confirmed durable on the anchor set (per redundancy) → clearable. */
23
+ anchoredCount: number;
24
+ anchoredBytes: number;
25
+ /** Designated anchors (bucket urls / device node ids) we could not reach to
26
+ * check. Under `all`, any unreachable anchor makes everything un-clearable. */
27
+ unreachable: string[];
28
+ /** Epoch-ms this verify ran. */
29
+ at: number;
30
+ }
31
+ /**
32
+ * Verify, right now, which locally-cached blobs a designated anchor durably holds,
33
+ * and record the per-blob `anchored` flag that `isClearable` reads. Buckets are
34
+ * checked with one paginated LIST (listBucketBlobHashes); device anchors with a
35
+ * `POST /api/blobs/has`. An anchor we can't reach (offline / no stored url+token /
36
+ * bad creds) goes into `unreachable` and is treated as NOT holding anything —
37
+ * conservative, so a transient outage defers clearing rather than risking loss.
38
+ *
39
+ * Redundancy: `any` → present on ≥1 reachable anchor; `all` → every designated
40
+ * anchor reachable AND holding it. Run on demand only (panel open / refresh /
41
+ * pre-eviction when over quota), never in the steady-state maintenance loop.
42
+ */
43
+ export declare function verifyAnchorPresence(db: DbDriver): Promise<VerifyResult>;
44
+ export interface BlobMaintenanceResult {
45
+ /** Referenced blobs a full library pulled in this round. */
46
+ acquired: number;
47
+ /** Pending productions confirmed flushed to the bucket(s) this round. */
48
+ flushed: number;
49
+ /** Blobs auto-evicted to stay under the configured cache quota. */
50
+ evicted: number;
51
+ }
52
+ /**
53
+ * Background blob upkeep, run from the sync tick (throttled). Cheap in steady
54
+ * state — it only touches work that actually exists:
55
+ * 1. A full blob library pulls every referenced blob it is still MISSING
56
+ * (resolveBlob); self-limiting, a no-op once caught up.
57
+ * 2. This node flushes its own PENDING productions to each attached bucket, then
58
+ * marks them flushed (→ clearable). It iterates ONLY pending blobs (empty in
59
+ * steady state), so — unlike the old design — it does NOT re-read/encrypt/HEAD
60
+ * every already-uploaded blob each round.
61
+ * 3. Evicts to stay under the cache quota.
62
+ */
63
+ export declare function blobMaintenance(db: DbDriver): Promise<BlobMaintenanceResult>;
64
+ export interface EvictResult {
65
+ evicted: number;
66
+ freedBytes: number;
67
+ }
68
+ /**
69
+ * Auto-evict clearable, unpinned blobs by least-recently-accessed order until the
70
+ * local cache total is at/under the low-water mark. No-op when quotaBytes <= 0
71
+ * (disabled) or the cache is already under quota. Only touches blobs `isClearable`
72
+ * marks safe (durable on the full set) and never a pinned blob — bytes stay
73
+ * re-fetchable, so this is loss-free.
74
+ */
75
+ export declare function evictToQuota(db: DbDriver, quotaBytes: number): Promise<EvictResult>;
76
+ export interface ClearResult {
77
+ /** Blobs whose bytes were dropped. */
78
+ cleared: number;
79
+ /** Actual bytes freed on disk. */
80
+ freedBytes: number;
81
+ /** Blobs left in place (sole copy / no anchor / this is a full node). */
82
+ skipped: number;
83
+ }
84
+ export interface GcResult {
85
+ removed: number;
86
+ freedBytes: number;
87
+ }
88
+ /**
89
+ * Fold on-disk cache files into the blob_cache ledger so stats/clear/gc also see
90
+ * blobs written before the ledger existed. content_type is unknown for these
91
+ * (left null — serve falls back to the URL suffix).
92
+ */
93
+ export declare function reconcileCache(db: DbDriver): void;
94
+ /** Drop the bytes of every locally-cached blob that is safe to clear (durable on
95
+ * the designated full set). Reference + hash stay in the oplog, re-fetchable. */
96
+ export declare function clearCache(db: DbDriver): Promise<ClearResult>;
97
+ /** Drop bytes for blobs no live site_files / doc image still references (true
98
+ * garbage, distinct from clearable cache). */
99
+ export declare function gcOrphans(db: DbDriver): Promise<GcResult>;
100
+ /** Clear a CHOSEN subset of cached blobs — the per-blob counterpart of
101
+ * clearCache, for the Settings blob manager. Same safety floor: only a blob that
102
+ * is unpinned AND isClearable (durable on the designated full set) has its bytes
103
+ * dropped; everything else is left in place and counted as skipped. */
104
+ export declare function clearBlobs(db: DbDriver, hashes: string[]): Promise<ClearResult>;
105
+ /** Delete a CHOSEN subset of ORPHAN blobs — the per-blob counterpart of gcOrphans.
106
+ * A hash is removed only when no live site_files / doc image still references it;
107
+ * any still-referenced (or unknown) hash is skipped, so a blob a document points
108
+ * at can never be deleted out from under it. */
109
+ export declare function deleteOrphanBlobs(db: DbDriver, hashes: string[]): Promise<GcResult>;
110
+ //# sourceMappingURL=blobs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"blobs.d.ts","sourceRoot":"","sources":["../../src/core/blobs.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAsB5C,cAAc,iBAAiB,CAAC;AAEhC,MAAM,WAAW,WAAW;IAC1B,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,MAAM,EAAE,MAAM,G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;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;CACpB;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,EAAE,EAAE,QAAQ,GAAG,IAAI,CAiBjD;AAED;kFACkF;AAClF,wBAAsB,UAAU,CAAC,EAAE,EAAE,QAAQ,GAAG,OAAO,CAAC,WAAW,CAAC,CAgBnE;AAED;+CAC+C;AAC/C,wBAAsB,SAAS,CAAC,EAAE,EAAE,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC,CAY/D;AAED;;;wEAGwE;AACxE,wBAAsB,UAAU,CAAC,EAAE,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,WAAW,CAAC,CAiBrF;AAED;;;iDAGiD;AACjD,wBAAsB,iBAAiB,CAAC,EAAE,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,QAAQ,CAAC,CAazF"}
@@ -4,7 +4,29 @@ export interface BlobInfo {
4
4
  size: number;
5
5
  }
6
6
  export declare function blobPath(hash: string): string;
7
- /** Store bytes content-addressed (sha256) under cache/. Returns its hash + path. */
7
+ /** Content key length: sha256 truncated to 128 bits (32 hex). Shorter keys keep
8
+ * doc markdown (`![](/blob/<hash>.png)`) readable; collision is negligible.
9
+ * Addressing stays length-agnostic, so legacy full 64-hex refs still resolve. */
10
+ export declare const BLOB_HASH_HEX = 32;
11
+ /** Full sha256 hex of the bytes. */
12
+ export declare function sha256Hex(bytes: Uint8Array): string;
13
+ /** Canonical content hash for new blobs: sha256 of the bytes, truncated. */
14
+ export declare function blobHash(bytes: Uint8Array): string;
15
+ /** Verify fetched bytes match a content reference of either length (canonical
16
+ * 32-hex or legacy 64-hex) — the ref is always a prefix of the full sha256. */
17
+ export declare function verifyBlobBytes(bytes: Uint8Array, hash: string): boolean;
18
+ /** Store bytes content-addressed under cache/. Returns its hash + path. */
8
19
  export declare function putBlob(data: ArrayBuffer | Uint8Array | string): Promise<BlobInfo>;
9
20
  export declare function getBlob(hash: string): Promise<Uint8Array | null>;
21
+ /** Whether this node currently holds a blob's bytes on disk — a cheap existence
22
+ * check (no read). Use over the ledger when correctness depends on the bytes
23
+ * actually being present (e.g. answering a peer's "do you hold X" before it
24
+ * drops its own copy), since the ledger can lag a vanished file. */
25
+ export declare function blobExists(hash: string): Promise<boolean>;
26
+ /** Store bytes under a GIVEN content hash (not recomputed) — for caching a blob
27
+ * fetched from a peer/bucket by its reference, which may be a legacy 64-hex hash
28
+ * that putBlob's truncation would not reproduce. Returns the byte length. */
29
+ export declare function putBlobAt(hash: string, bytes: Uint8Array): Promise<number>;
30
+ /** Remove a blob's bytes from the cache. Returns its size, or 0 if absent. */
31
+ export declare function deleteBlob(hash: string): Promise<number>;
10
32
  //# sourceMappingURL=cache.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../../src/core/cache.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,QAAQ;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACd;AAED,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE7C;AAED,oFAAoF;AACpF,wBAAsB,OAAO,CAAC,IAAI,EAAE,WAAW,GAAG,UAAU,GAAG,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC,CAaxF;AAED,wBAAsB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC,CAItE"}
1
+ {"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../../src/core/cache.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,QAAQ;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACd;AAED,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE7C;AAED;;kFAEkF;AAClF,eAAO,MAAM,aAAa,KAAK,CAAC;AAEhC,oCAAoC;AACpC,wBAAgB,SAAS,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAInD;AAED,4EAA4E;AAC5E,wBAAgB,QAAQ,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAElD;AAED;gFACgF;AAChF,wBAAgB,eAAe,CAAC,KAAK,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAExE;AAED,2EAA2E;AAC3E,wBAAsB,OAAO,CAAC,IAAI,EAAE,WAAW,GAAG,UAAU,GAAG,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC,CAWxF;AAED,wBAAsB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC,CAItE;AAED;;;qEAGqE;AACrE,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAEzD;AAED;;8EAE8E;AAC9E,wBAAsB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC,CAIhF;AAED,8EAA8E;AAC9E,wBAAsB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAM9D"}
@@ -22,9 +22,10 @@ export interface CompactOptions {
22
22
  }
23
23
  export declare function compactOplog(db: Database, opts: CompactOptions): CompactResult;
24
24
  /**
25
- * Delete cache blobs no longer referenced by any remaining oplog value or
26
- * materialized site file. Conservative: only files named like a sha256 hash
27
- * are candidates; anything else in the cache dir is left alone.
25
+ * Delete cache blobs no longer referenced by any remaining oplog value, a
26
+ * materialized site file, or a `/blob/<hash>` doc image reference. Conservative:
27
+ * only files named like a content hash (canonical 32-hex or legacy 64-hex) are
28
+ * candidates; anything else in the cache dir is left alone.
28
29
  */
29
30
  export declare function gcBlobs(db: Database, opts?: {
30
31
  dryRun?: boolean;
@@ -1 +1 @@
1
- {"version":3,"file":"compact.d.ts","sourceRoot":"","sources":["../../src/core/compact.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAsB3C,MAAM,WAAW,aAAa;IAC5B,oFAAoF;IACpF,MAAM,EAAE,MAAM,CAAC;IACf,eAAe,EAAE,MAAM,CAAC;IACxB,YAAY,EAAE,MAAM,CAAC;IACrB,aAAa,EAAE,MAAM,CAAC;IACtB,gBAAgB,EAAE,MAAM,CAAC;IACzB,eAAe,EAAE,MAAM,CAAC;IACxB,cAAc,EAAE,MAAM,CAAC;IACvB,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,MAAM,WAAW,cAAc;IAC7B,yEAAyE;IACzE,QAAQ,EAAE,MAAM,CAAC;IACjB,8DAA8D;IAC9D,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,yEAAyE;IACzE,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,wCAAwC;IACxC,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAqBD,wBAAgB,YAAY,CAAC,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,cAAc,GAAG,aAAa,CAkD9E;AAED;;;;GAIG;AACH,wBAAgB,OAAO,CACrB,EAAE,EAAE,QAAQ,EACZ,IAAI,GAAE;IAAE,MAAM,CAAC,EAAE,OAAO,CAAA;CAAO,GAC9B;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CA0CpC;AAED;0CAC0C;AAC1C,wBAAgB,eAAe,CAC7B,EAAE,EAAE,QAAQ,EACZ,QAAQ,EAAE,MAAM,EAChB,GAAG,SAAa,GACf;IAAE,aAAa,EAAE,MAAM,CAAC;IAAC,mBAAmB,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,CAa1E"}
1
+ {"version":3,"file":"compact.d.ts","sourceRoot":"","sources":["../../src/core/compact.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AA0B3C,MAAM,WAAW,aAAa;IAC5B,oFAAoF;IACpF,MAAM,EAAE,MAAM,CAAC;IACf,eAAe,EAAE,MAAM,CAAC;IACxB,YAAY,EAAE,MAAM,CAAC;IACrB,aAAa,EAAE,MAAM,CAAC;IACtB,gBAAgB,EAAE,MAAM,CAAC;IACzB,eAAe,EAAE,MAAM,CAAC;IACxB,cAAc,EAAE,MAAM,CAAC;IACvB,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,MAAM,WAAW,cAAc;IAC7B,yEAAyE;IACzE,QAAQ,EAAE,MAAM,CAAC;IACjB,8DAA8D;IAC9D,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,yEAAyE;IACzE,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,wCAAwC;IACxC,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAqBD,wBAAgB,YAAY,CAAC,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,cAAc,GAAG,aAAa,CAkD9E;AAED;;;;;GAKG;AACH,wBAAgB,OAAO,CACrB,EAAE,EAAE,QAAQ,EACZ,IAAI,GAAE;IAAE,MAAM,CAAC,EAAE,OAAO,CAAA;CAAO,GAC9B;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CA2DpC;AAED;0CAC0C;AAC1C,wBAAgB,eAAe,CAC7B,EAAE,EAAE,QAAQ,EACZ,QAAQ,EAAE,MAAM,EAChB,GAAG,SAAa,GACf;IAAE,aAAa,EAAE,MAAM,CAAC;IAAC,mBAAmB,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,CAa1E"}
@@ -1,10 +1,17 @@
1
1
  import type { DbDriver } from "./driver.ts";
2
+ /** Authoritative upper bound for a single POST /api/blob upload (bytes). The
3
+ * WebUI applies friendlier per-kind caps (image 25MB / av 100MB / file 100MB)
4
+ * client-side; this is the hard server ceiling. Override with METAHUB_MAX_BLOB_UPLOAD. */
5
+ export declare const MAX_BLOB_UPLOAD_BYTES: number;
2
6
  export interface ServerConfig {
3
7
  host: string;
4
8
  port: number;
5
9
  /** Auto-sync poll interval in ms; <= 0 disables the timer. */
6
10
  syncIntervalMs: number;
7
11
  autoSync: boolean;
12
+ /** Local blob cache quota in bytes. When the cache exceeds it, the sync tick
13
+ * auto-evicts clearable, unpinned blobs (LRU). <= 0 disables eviction. */
14
+ blobCacheQuotaBytes: number;
8
15
  }
9
16
  export declare const DEFAULT_CONFIG: ServerConfig;
10
17
  /** Persisted server config, falling back to DEFAULT_CONFIG per field. */
@@ -1 +1 @@
1
- {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/core/config.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAQ5C,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,8DAA8D;IAC9D,cAAc,EAAE,MAAM,CAAC;IACvB,QAAQ,EAAE,OAAO,CAAC;CACnB;AAED,eAAO,MAAM,cAAc,EAAE,YAK5B,CAAC;AAeF,yEAAyE;AACzE,wBAAgB,eAAe,CAAC,EAAE,EAAE,QAAQ,GAAG,YAAY,CAW1D;AAED,yEAAyE;AACzE,wBAAgB,eAAe,CAAC,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,OAAO,CAAC,YAAY,CAAC,GAAG,YAAY,CAO1F"}
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/core/config.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAG5C;;2FAE2F;AAC3F,eAAO,MAAM,qBAAqB,QAAmE,CAAC;AAQtG,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,8DAA8D;IAC9D,cAAc,EAAE,MAAM,CAAC;IACvB,QAAQ,EAAE,OAAO,CAAC;IAClB;+EAC2E;IAC3E,mBAAmB,EAAE,MAAM,CAAC;CAC7B;AAED,eAAO,MAAM,cAAc,EAAE,YAM5B,CAAC;AAeF,yEAAyE;AACzE,wBAAgB,eAAe,CAAC,EAAE,EAAE,QAAQ,GAAG,YAAY,CAc1D;AAED,yEAAyE;AACzE,wBAAgB,eAAe,CAAC,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,OAAO,CAAC,YAAY,CAAC,GAAG,YAAY,CAS1F"}
@@ -11,6 +11,13 @@ export interface Change {
11
11
  /** Oplog SELECT list, locked to the Change interface (also reused by history). */
12
12
  export declare const CHANGE_COLS: readonly ["hlc", "node_id", "dataset", "row_id", "col", "value", "txn"];
13
13
  export declare const CHANGE_SELECT: string;
14
+ /**
15
+ * Run `fn` with every emit stamped with `node` as its node_id (and HLC node
16
+ * segment) instead of the host node. INVARIANT: relies on core mutators being
17
+ * synchronous (set/clear around one synchronous call) — do not make the wrapped
18
+ * mutators async or the slot can leak across requests.
19
+ */
20
+ export declare function withNodeId<T>(node: string | null, fn: () => T): T;
14
21
  /**
15
22
  * Run `fn` with all emits stamped with one shared txn id. Nested calls keep the
16
23
  * outermost group (a revert that calls updateDocument is ONE revision). `label`
@@ -19,18 +26,33 @@ export declare const CHANGE_SELECT: string;
19
26
  export declare function withChangeGroup<T>(label: string | null, fn: () => T): T;
20
27
  /** Wrap a mutator so its body runs inside one change group. */
21
28
  export declare function grouped<A extends unknown[], R>(fn: (...args: A) => R, label?: string | null): (...args: A) => R;
29
+ /** Outcome of applying one change to the oplog. `inserted` reports whether the
30
+ * oplog actually gained a NEW row (false when we already had this exact change —
31
+ * the INSERT OR IGNORE was a no-op); it is the honest "did we receive new data"
32
+ * signal, distinct from `winner` (whether this change now wins its register and
33
+ * was materialized). Re-applying a change we already hold still returns
34
+ * `winner:true` if it's the current max-HLC, so callers that want progress (not
35
+ * re-materialization) must key off `inserted`. */
36
+ export interface ApplyResult {
37
+ inserted: boolean;
38
+ winner: boolean;
39
+ }
22
40
  /**
23
41
  * Record a change in the oplog (idempotent) and, if it is the latest write for
24
42
  * its register, materialize it. Order-independent: convergence holds regardless
25
43
  * of the order changes are applied, because the winner is recomputed as the max
26
44
  * HLC over the full oplog for that register.
27
45
  */
28
- export declare function applyChange(db: DbDriver, c: Change): boolean;
46
+ export declare function applyChange(db: DbDriver, c: Change): ApplyResult;
29
47
  /** Apply a local write: assign a fresh HLC, append to oplog, materialize. */
30
48
  export declare function emit(db: DbDriver, dataset: string, rowId: string, col: string, value: unknown): Change;
31
49
  /** Apply multiple local field writes to the same row. */
32
50
  export declare function emitFields(db: DbDriver, dataset: string, rowId: string, fields: Record<string, unknown>): Change[];
33
- /** Apply remote changes from a sync peer (advances clock, then merges). */
51
+ /** Apply remote changes from a sync peer (advances clock, then merges). Returns
52
+ * the number of changes that were NEW to our oplog (rows actually inserted) —
53
+ * the honest "received" count. Re-ingesting data we already hold returns 0, so
54
+ * this is a reliable progress signal for the auto-sync backoff (a round that
55
+ * only re-reads known data must not count as activity). */
34
56
  export declare function ingest(db: DbDriver, changes: Change[]): number;
35
57
  /** All oplog changes with HLC strictly greater than `since` (test/debug helper). */
36
58
  export declare function changesSince(db: DbDriver, since: string): Change[];
@@ -43,6 +65,14 @@ export interface ChangesAfterOpts {
43
65
  limit?: number;
44
66
  /** Datasets to omit (partial replicas, e.g. a phone skipping site_files). */
45
67
  excludeDatasets?: string[];
68
+ /**
69
+ * Return only changes this node produced itself. Storage-sync (see
70
+ * sync/storage.ts) publishes each node's own ops under its own bucket prefix,
71
+ * so it must not re-upload ops it merely ingested from peers. The cursor still
72
+ * advances over the filtered-out rows (high-water on exhaustion), so ingested
73
+ * ops are skipped once, not rescanned every round.
74
+ */
75
+ onlyNode?: string;
46
76
  }
47
77
  /**
48
78
  * Changes inserted after `seq` (a local rowid), in ins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EAAE,CAIlE;AAED,MAAM,WAAW,WAAW;IAC1B,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,gBAAgB;IAC/B,2EAA2E;IAC3E,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,6EAA6E;IAC7E,eAAe,CAAC,EAAE,MAAM,EAAE,CAAC;IAC3B;;;;;;OAMG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED;;;;;;;;;GASG;AACH,wBAAgB,eAAe,CAC7B,EAAE,EAAE,QAAQ,EACZ,GAAG,EAAE,MAAM,EACX,IAAI,GAAE,gBAAqB,GAC1B,WAAW,CAkCb"}
@@ -9,9 +9,12 @@ export declare function ftsAvailable(db: DbDriver): boolean;
9
9
  export declare function migrateRecords(db: DbDriver): void;
10
10
  /**
11
11
  * Add the peer-pairing columns to a legacy `peers` table (token/label/node_id/
12
- * enabled/last_sync_at/last_status/last_error). Idempotent — guarded per column,
13
- * never drops the table so existing replication cursors survive.
12
+ * enabled/last_sync_at/last_success_at/last_status/last_error). Idempotent —
13
+ * guarded per column, never drops the table so existing replication cursors survive.
14
14
  */
15
+ /** Add `served_base` to a `shares` table created before the column existed
16
+ * (idempotent). The vestigial s3_* columns are left as-is. */
17
+ export declare function migrateShares(db: DbDriver): void;
15
18
  export declare function migratePeers(db: DbDriver): void;
16
19
  /**
17
20
  * Add the `blank_after` column to a legacy `doc_blocks` table. Idempotent —
@@ -19,6 +22,18 @@ export declare function migratePeers(db: DbDriver): void;
19
22
  * separators), so the migration never changes an existing document's body.
20
23
  */
21
24
  export declare function migrateDocBlocks(db: DbDriver): void;
25
+ /**
26
+ * Bring a legacy `blob_cache` to the current shape and retire the old synced
27
+ * `blob_presence` table. Idempotent (guarded by hasColumn / IF EXISTS).
28
+ * - `pinned`: node-local, never auto-evicted/cleared.
29
+ * - `pending`: bytes produced here, not yet flushed to a durable anchor. Existing
30
+ * rows can't be classified produced-vs-acquired, so default to 1 (protected);
31
+ * the next online flush sets already-in-bucket blobs back to 0, so it self-heals
32
+ * and is loss-free. See blobs.ts.
33
+ * - `blob_presence` (was synced) is dropped: clearing is now decided locally from
34
+ * `pending`. Leftover presence oplog changes are ignored (forward-compat).
35
+ */
36
+ export declare function migrateBlobCache(db: DbDriver): void;
22
37
  /**
23
38
  * Add the `order_key` column to a legacy `documents` table and backfill it.
24
39
  * Idempotent — guarded by hasColumn; backfill only touches rows with a NULL key,
@@ -32,6 +47,51 @@ export declare function migrateDocuments(db: DbDriver): void;
32
47
  * back to time-gap clustering for them).
33
48
  */
34
49
  export declare function migrateOplog(db: DbDriver): void;
50
+ /**
51
+ * Rebuild a legacy `crdt_changes` (composite PRIMARY KEY, implicit rowid) into
52
+ * the current shape with an explicit `seq INTEGER PRIMARY KEY AUTOINCREMENT`.
53
+ *
54
+ * Why: replication push/pull cursors are crdt_changes rowids. The legacy table
55
+ * has no declared INTEGER PRIMARY KEY, so a `VACUUM` (run by `mh` compaction)
56
+ * renumbers its rowids 1..N; a peer's stored cursor then sits above the new
57
+ * MAX(rowid) and `changesAfterSeq`'s "never regress" floor pins it there,
58
+ * silently never pushing/pulling the writes below it. The declared `seq` is
59
+ * stable across VACUUM and never reused, closing the hole.
60
+ *
61
+ * Idempotent — guarded by the `seq` column. Old rowids are copied verbatim into
62
+ * seq so any *uncorrupted* cursor stays meaningful; then push/pull cursors are
63
+ * reset to 0 because we cannot tell which were already stranded by a past
64
+ * VACUUM, and a from-scratch re-sync is safe (INSERT OR IGNORE / ingest dedup)
65
+ * — a one-time catch-up on first sync after upgrade. storage_cursors are
66
+ * object-key based, never rowids, so they are left intact.
67
+ */
68
+ export declare function migrateCrdtChangesSeq(db: DbDriver): void;
69
+ /**
70
+ * Migrate legacy storage-peer keys `s3://<bucket>/<prefix>` to the
71
+ * endpoint-qualified `s3://<host>/<bucket>/<prefix>` so two endpoints sharing a
72
+ * bucket name (R2/MinIO/COS each have their own namespace) no longer collide and
73
+ * silently overwrite each other's peer row (and stale cursor). Rewrites all three
74
+ * places the key is referenced — the peers row, its storage_cursors, and any
75
+ * blob_policy.fullNodes anchor pointing at the bucket.
76
+ *
77
+ * Key derivation goes through the shared `storageUrl` (storage-url.ts) so every
78
+ * producer — CLI/server, the WebUI worker, and this migration — agrees byte for
79
+ * byte; otherwise a producer still minting the legacy shape turns the rename into
80
+ * a permanent whack-a-mole.
81
+ *
82
+ * Idempotent — a row already at its endpoint-qualified key produces no rename.
83
+ * Keyed off config.endpoint (authoritative), so re-running is a no-op.
84
+ *
85
+ * Cross-version note: rewriting blob_policy.fullNodes is a CRDT write, so it
86
+ * replicates. A device still on the old code keeps a legacy-format storage peer
87
+ * and matches anchors by exact string, so it won't recognize the new-format
88
+ * anchor until it upgrades — during that window a bucket-anchored device pauses
89
+ * cache eviction (loss-free: bytes stay, nothing is dropped) and self-heals once
90
+ * the fleet is upgraded. Only affects setups that designate a bucket as a
91
+ * full-blob anchor. (Accepted over a tolerant-matcher rewrite, which is wider and
92
+ * still can't fix already-shipped old clients.)
93
+ */
94
+ export declare function migrateStoragePeerUrls(db: DbDriver): void;
35
95
  /** Bring a freshly opened (or legacy) database to the current schema. */
36
96
  export declare function initSchema(db: DbDriver): void;
37
97
  //# sourceMappingURL=schema-init.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"schema-init.d.ts","sourceRoot":"","sources":["../../src/core/schema-init.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAK5C,wBAAgB,SAAS,CAAC,EAAE,EAAE,QAAQ,GAAG,IAAI,CAO5C;AAED,wBAAgB,YAAY,CAAC,EAAE,EAAE,QAAQ,GAAG,OAAO,CAKlD;AAeD;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,EAAE,EAAE,QAAQ,GAAG,IAAI,CAuBjD;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,EAAE,EAAE,QAAQ,GAAG,IAAI,CAa/C;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IAAI,CA4BxD;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,sBAAsB,CAAC,EAAE,EAAE,QAAQ,GAAG,IAAI,CAuDzD;AAED,yEAAyE;AACzE,wBAAgB,UAAU,CAAC,EAAE,EAAE,QAAQ,GAAG,IAAI,CAW7C"}
@@ -1,3 +1,3 @@
1
- export declare const CORE_SCHEMA = "\nCREATE TABLE IF NOT EXISTS meta (\n key TEXT PRIMARY KEY,\n value TEXT\n);\n\n-- CRDT oplog: one row per field assignment. Source of truth for sync.\n-- txn groups the changes of one logical mutation for history rendering\n-- (nullable: legacy rows and changes from pre-txn peers have none).\nCREATE TABLE IF NOT EXISTS crdt_changes (\n hlc TEXT NOT NULL,\n node_id TEXT NOT NULL,\n dataset TEXT NOT NULL,\n row_id TEXT NOT NULL,\n col TEXT NOT NULL,\n value TEXT,\n txn TEXT,\n PRIMARY KEY (dataset, row_id, col, hlc)\n);\nCREATE INDEX IF NOT EXISTS idx_changes_hlc ON crdt_changes(hlc);\n-- Serves history's \"every block ever attached to this doc\" lookup. Partial so\n-- it never indexes the large values other datasets carry (e.g. site file bodies).\nCREATE INDEX IF NOT EXISTS idx_changes_docref ON crdt_changes(value)\n WHERE dataset = 'doc_blocks' AND col = 'doc_id';\n\n-- Per-peer replication cursors (SQLite rowids: monotonic in insertion order,\n-- so no change is ever skipped regardless of HLC/clock skew). Outbound side of\n-- a pairing: \"token\" is the credential the *remote* issued to us (sent as a\n-- Bearer header when we sync to them). See migratePeers in db.ts for the columns\n-- added to legacy databases.\nCREATE TABLE IF NOT EXISTS peers (\n url TEXT PRIMARY KEY,\n pull_cursor INTEGER NOT NULL DEFAULT 0,\n push_cursor INTEGER NOT NULL DEFAULT 0,\n token TEXT,\n label TEXT,\n node_id TEXT,\n enabled INTEGER NOT NULL DEFAULT 1,\n last_sync_at INTEGER,\n last_status TEXT,\n last_error TEXT\n);\n\n-- Inbound side of a pairing: credentials *we* issued to remote devices. A peer\n-- presents one of these on /sync; acceptsSyncToken() checks it alongside the\n-- managed master token. Revoke by deleting the row.\nCREATE TABLE IF NOT EXISTS peer_grants (\n token TEXT PRIMARY KEY,\n peer_url TEXT,\n node_id TEXT,\n created_at INTEGER\n);\n\n-- One-time pairing codes minted by \"mh config peer code\" / POST /api/pair/new.\n-- Short-lived and single-use: redeemed during the pairing handshake, then the\n-- two devices exchange durable peer_grants. used/exp gate redemption.\nCREATE TABLE IF NOT EXISTS pairing_codes (\n code TEXT PRIMARY KEY,\n exp INTEGER NOT NULL,\n used INTEGER NOT NULL DEFAULT 0,\n created_at INTEGER\n);\n\nCREATE TABLE IF NOT EXISTS databases (\n id TEXT PRIMARY KEY,\n name TEXT,\n icon TEXT,\n created_hlc TEXT,\n __deleted INTEGER NOT NULL DEFAULT 0\n);\n\nCREATE TABLE IF NOT EXISTS properties (\n id TEXT PRIMARY KEY,\n database_id TEXT,\n name TEXT,\n type TEXT,\n config TEXT,\n position REAL,\n __deleted INTEGER NOT NULL DEFAULT 0\n);\nCREATE INDEX IF NOT EXISTS idx_properties_db ON properties(database_id);\n\n-- One row per record. Field cells live in the data JSON object, keyed by\n-- property id (rename-safe). Each (record,property) is still an independent\n-- CRDT register in the oplog; materialize() folds the winner into data via\n-- json_set/json_remove. Hot fields get expression indexes on data->>'propid'\n-- (see indexing.ts) so filter/sort/limit push down to SQL.\nCREATE TABLE IF NOT EXISTS records (\n id TEXT PRIMARY KEY,\n database_id TEXT,\n created_hlc TEXT,\n order_key TEXT,\n data TEXT NOT NULL DEFAULT '{}',\n __deleted INTEGER NOT NULL DEFAULT 0\n);\nCREATE INDEX IF NOT EXISTS idx_records_db_hlc ON records(database_id, created_hlc);\n\nCREATE TABLE IF NOT EXISTS documents (\n id TEXT PRIMARY KEY,\n title TEXT,\n body TEXT, -- materialized cache: serialized from doc_blocks\n database_id TEXT,\n parent_id TEXT,\n created_hlc TEXT,\n order_key TEXT, -- fractional index among siblings (same parent_id); ties broken by created_hlc,id\n __deleted INTEGER NOT NULL DEFAULT 0\n);\n\n-- Document body as an ordered list of blocks; each (block,field) is an\n-- independent CRDT register so concurrent edits to different blocks merge.\n-- order_key is a fractional index (ties broken by id) giving display order.\nCREATE TABLE IF NOT EXISTS doc_blocks (\n id TEXT PRIMARY KEY,\n doc_id TEXT,\n text TEXT,\n order_key TEXT,\n blank_after INTEGER NOT NULL DEFAULT 0, -- extra blank lines kept after this block (user spacing)\n __deleted INTEGER NOT NULL DEFAULT 0\n);\nCREATE INDEX IF NOT EXISTS idx_doc_blocks_doc ON doc_blocks(doc_id);\n\n-- A named static site (Supabase-Storage-style bucket). Served at /sites/<name>/.\nCREATE TABLE IF NOT EXISTS sites (\n id TEXT PRIMARY KEY,\n name TEXT, -- URL slug; resolved by getSiteByName\n title TEXT,\n created_hlc TEXT,\n __deleted INTEGER NOT NULL DEFAULT 0\n);\n\n-- One row per file in a site. content holds inline utf8/base64 text, or a blob\n-- hash (see cache.ts) when encoding = 'blob'. (site_id, path) maps to a stable\n-- id so re-uploads merge as a CRDT register instead of duplicating.\nCREATE TABLE IF NOT EXISTS site_files (\n id TEXT PRIMARY KEY,\n site_id TEXT,\n path TEXT,\n content_type TEXT,\n encoding TEXT, -- 'utf8' | 'base64' | 'blob'\n content TEXT,\n created_hlc TEXT,\n __deleted INTEGER NOT NULL DEFAULT 0\n);\nCREATE INDEX IF NOT EXISTS idx_site_files_site ON site_files(site_id);\n";
1
+ export declare const CORE_SCHEMA = "\nCREATE TABLE IF NOT EXISTS meta (\n key TEXT PRIMARY KEY,\n value TEXT\n);\n\n-- CRDT oplog: one row per field assignment. Source of truth for sync.\n-- txn groups the changes of one logical mutation for history rendering\n-- (nullable: legacy rows and changes from pre-txn peers have none).\n--\n-- seq is an explicit AUTOINCREMENT id used as the replication cursor. It must\n-- be a declared INTEGER PRIMARY KEY (not the implicit rowid): VACUUM renumbers\n-- the rowids of tables that lack one, which would shift them below a peer's\n-- stored push/pull cursor and silently strand every write in the gap.\n-- AUTOINCREMENT additionally guarantees ids are never reused. The\n-- (dataset,row_id,col,hlc) tuple stays UNIQUE so applyChange's INSERT OR IGNORE\n-- dedup is unchanged. Legacy databases are rebuilt into this shape by\n-- migrateCrdtChangesSeq (schema-init.ts).\nCREATE TABLE IF NOT EXISTS crdt_changes (\n seq INTEGER PRIMARY KEY AUTOINCREMENT,\n hlc TEXT NOT NULL,\n node_id TEXT NOT NULL,\n dataset TEXT NOT NULL,\n row_id TEXT NOT NULL,\n col TEXT NOT NULL,\n value TEXT,\n txn TEXT,\n UNIQUE (dataset, row_id, col, hlc)\n);\nCREATE INDEX IF NOT EXISTS idx_changes_hlc ON crdt_changes(hlc);\n-- Serves history's \"every block ever attached to this doc\" lookup. Partial so\n-- it never indexes the large values other datasets carry (e.g. site file bodies).\nCREATE INDEX IF NOT EXISTS idx_changes_docref ON crdt_changes(value)\n WHERE dataset = 'doc_blocks' AND col = 'doc_id';\n\n-- Per-peer replication cursors (crdt_changes.seq: AUTOINCREMENT, monotonic in\n-- insertion order and stable across VACUUM, so no change is ever skipped\n-- regardless of HLC/clock skew). Outbound side of\n-- a pairing: \"token\" is the credential the *remote* issued to us (sent as a\n-- Bearer header when we sync to them). See migratePeers in db.ts for the columns\n-- added to legacy databases.\n--\n-- kind selects the transport: 'http' (POST /sync to a peer server, the classic\n-- path) or 's3' (an S3-compatible bucket used as dumb store-and-forward \u2014 see\n-- sync/storage.ts). For 's3' peers url is a synthetic key (s3://<bucket>/<prefix>)\n-- and config holds the bucket settings + credentials + master key as JSON.\n-- Like the rest of peers, config is local-only and never enters the CRDT oplog.\nCREATE TABLE IF NOT EXISTS peers (\n url TEXT PRIMARY KEY,\n pull_cursor INTEGER NOT NULL DEFAULT 0,\n push_cursor INTEGER NOT NULL DEFAULT 0,\n token TEXT,\n label TEXT,\n node_id TEXT,\n enabled INTEGER NOT NULL DEFAULT 1,\n last_sync_at INTEGER,\n last_success_at INTEGER,\n last_status TEXT,\n last_error TEXT,\n kind TEXT NOT NULL DEFAULT 'http',\n config TEXT\n);\n\n-- Per-(storage-peer, remote-node) pull progress: the key of the last oplog\n-- segment we consumed from that node's bucket prefix. HTTP peers use the single\n-- pull_cursor above; storage peers need one cursor per remote node because each\n-- node publishes its own segment stream under its own prefix. last_key is a\n-- bucket object key (lexicographically ordered = chronological), so LIST\n-- start-after resumes exactly where we left off.\nCREATE TABLE IF NOT EXISTS storage_cursors (\n peer_url TEXT NOT NULL,\n node_id TEXT NOT NULL,\n last_key TEXT,\n PRIMARY KEY (peer_url, node_id)\n);\n\n-- Inbound side of a pairing: credentials *we* issued to remote devices. A peer\n-- presents one of these on /sync; acceptsSyncToken() checks it alongside the\n-- managed master token. Revoke by deleting the row.\nCREATE TABLE IF NOT EXISTS peer_grants (\n token TEXT PRIMARY KEY,\n peer_url TEXT,\n node_id TEXT,\n created_at INTEGER\n);\n\n-- One-time pairing codes minted by \"mh config peer code\" / POST /api/pair/new.\n-- Short-lived and single-use: redeemed during the pairing handshake, then the\n-- two devices exchange durable peer_grants. used/exp gate redemption.\nCREATE TABLE IF NOT EXISTS pairing_codes (\n code TEXT PRIMARY KEY,\n exp INTEGER NOT NULL,\n used INTEGER NOT NULL DEFAULT 0,\n created_at INTEGER\n);\n\n-- Public capability links to a doc / database / site. Like peers and blob_cache\n-- this is node-local and deliberately ABSENT from crdt.ts's DOMAIN map, so it\n-- never enters the oplog and never syncs \u2014 a share is a property of the access\n-- point that minted it, not of the workspace. slug is the unguessable capability\n-- in the URL (/share/<slug>). transport selects how it's served: 'server' (the\n-- token-exempt /share endpoint on this node) or 's3' (a presigned static export\n-- under <prefix>/shares/<slug> in a bucket). permission 'edit' is server-only:\n-- a presigned static object can't accept writes. guest_node_id attributes every\n-- edit made through an edit-share to one synthetic node (see crdt.ts withNodeId).\n-- pw_hash/pw_salt: server path stores a PBKDF2 verifier; the s3 path is E2EE so\n-- only pw_salt travels (in the link) and decryption happens in the viewer.\nCREATE TABLE IF NOT EXISTS shares (\n slug TEXT PRIMARY KEY,\n kind TEXT NOT NULL, -- 'doc' | 'database' | 'site'\n target_id TEXT NOT NULL, -- documents.id / databases.id / sites.id\n permission TEXT NOT NULL, -- 'view' | 'edit'\n transport TEXT NOT NULL, -- 'server' | 's3'\n pw_salt TEXT, -- base64; null = no password\n pw_hash TEXT, -- base64 PBKDF2 verifier (server path only)\n expires_at INTEGER, -- epoch ms; null = never\n guest_node_id TEXT, -- synthetic node id for edit shares (null for view)\n served_base TEXT, -- reachable base URL chosen at creation (link / source label)\n s3_peer_url TEXT, -- vestigial (s3 shares live in the bucket, not here)\n s3_object_prefix TEXT, -- vestigial\n s3_presign_exp INTEGER, -- vestigial\n s3_key_b64 TEXT, -- vestigial\n created_at INTEGER NOT NULL\n);\nCREATE INDEX IF NOT EXISTS idx_shares_target ON shares(target_id);\n\nCREATE TABLE IF NOT EXISTS databases (\n id TEXT PRIMARY KEY,\n name TEXT,\n icon TEXT,\n created_hlc TEXT,\n __deleted INTEGER NOT NULL DEFAULT 0\n);\n\nCREATE TABLE IF NOT EXISTS properties (\n id TEXT PRIMARY KEY,\n database_id TEXT,\n name TEXT,\n type TEXT,\n config TEXT,\n position REAL,\n __deleted INTEGER NOT NULL DEFAULT 0\n);\nCREATE INDEX IF NOT EXISTS idx_properties_db ON properties(database_id);\n\n-- One row per record. Field cells live in the data JSON object, keyed by\n-- property id (rename-safe). Each (record,property) is still an independent\n-- CRDT register in the oplog; materialize() folds the winner into data via\n-- json_set/json_remove. Hot fields get expression indexes on data->>'propid'\n-- (see indexing.ts) so filter/sort/limit push down to SQL.\nCREATE TABLE IF NOT EXISTS records (\n id TEXT PRIMARY KEY,\n database_id TEXT,\n created_hlc TEXT,\n order_key TEXT,\n data TEXT NOT NULL DEFAULT '{}',\n __deleted INTEGER NOT NULL DEFAULT 0\n);\nCREATE INDEX IF NOT EXISTS idx_records_db_hlc ON records(database_id, created_hlc);\n\nCREATE TABLE IF NOT EXISTS documents (\n id TEXT PRIMARY KEY,\n title TEXT,\n body TEXT, -- materialized cache: serialized from doc_blocks\n database_id TEXT,\n parent_id TEXT,\n created_hlc TEXT,\n order_key TEXT, -- fractional index among siblings (same parent_id); ties broken by created_hlc,id\n __deleted INTEGER NOT NULL DEFAULT 0\n);\n\n-- Document body as an ordered list of blocks; each (block,field) is an\n-- independent CRDT register so concurrent edits to different blocks merge.\n-- order_key is a fractional index (ties broken by id) giving display order.\nCREATE TABLE IF NOT EXISTS doc_blocks (\n id TEXT PRIMARY KEY,\n doc_id TEXT,\n text TEXT,\n order_key TEXT,\n blank_after INTEGER NOT NULL DEFAULT 0, -- extra blank lines kept after this block (user spacing)\n __deleted INTEGER NOT NULL DEFAULT 0\n);\nCREATE INDEX IF NOT EXISTS idx_doc_blocks_doc ON doc_blocks(doc_id);\n\n-- A named static site (Supabase-Storage-style bucket). Served at /sites/<name>/.\nCREATE TABLE IF NOT EXISTS sites (\n id TEXT PRIMARY KEY,\n name TEXT, -- URL slug; resolved by getSiteByName\n title TEXT,\n created_hlc TEXT,\n __deleted INTEGER NOT NULL DEFAULT 0\n);\n\n-- One row per file in a site. content holds inline utf8/base64 text, or a blob\n-- hash (see cache.ts) when encoding = 'blob'. (site_id, path) maps to a stable\n-- id so re-uploads merge as a CRDT register instead of duplicating.\nCREATE TABLE IF NOT EXISTS site_files (\n id TEXT PRIMARY KEY,\n site_id TEXT,\n path TEXT,\n content_type TEXT,\n encoding TEXT, -- 'utf8' | 'base64' | 'blob'\n content TEXT,\n created_hlc TEXT,\n __deleted INTEGER NOT NULL DEFAULT 0\n);\nCREATE INDEX IF NOT EXISTS idx_site_files_site ON site_files(site_id);\n\n-- Node-local blob cache ledger (NOT synced, like peers/storage_cursors): one row\n-- per blob byte-file held in cache.ts's content-addressed store (~/.metahub/cache).\n-- Gives the bare hash-named files the metadata clearing/stats need. hash is the\n-- canonical content key (sha256 truncated to 32 hex; legacy 64-hex coexist).\n-- pending = bytes produced HERE not yet confirmed flushed to a durable anchor\n-- (bucket) \u2014 the only blobs a device must protect; never auto-evicted/cleared.\n-- pending=0 means a flushed production OR an acquired cache, both safely\n-- clearable (re-fetchable). This local fact replaces the old SYNCED blob_presence\n-- table \u2014 clearing is now purely local/offline. See blobs.ts / 22-blob-sync.\nCREATE TABLE IF NOT EXISTS blob_cache (\n hash TEXT PRIMARY KEY,\n size INTEGER NOT NULL,\n content_type TEXT,\n last_access INTEGER,\n pinned INTEGER NOT NULL DEFAULT 0, -- node-local: never auto-evicted / cleared\n pending INTEGER NOT NULL DEFAULT 1, -- produced here, not yet flushed to anchor\n anchored INTEGER NOT NULL DEFAULT 0 -- node-local: last verify confirmed a designated anchor holds it (per redundancy). Drives isClearable; reset on policy change\n);\n\n-- Synced workspace policy (single row \"default\"): which nodes are designated\n-- full-blob libraries \u2014 the durable anchor in no-bucket topologies (they pull\n-- everything and never clear). full_nodes is a JSON array of node_id; redundancy\n-- is retained but unused since clearing moved to the local pending model.\nCREATE TABLE IF NOT EXISTS blob_policy (\n id TEXT PRIMARY KEY, -- always \"default\"\n full_nodes TEXT, -- JSON array of node_id\n redundancy TEXT, -- 'all' | 'any'\n __deleted INTEGER NOT NULL DEFAULT 0\n);\n";
2
2
  export declare const FTS_SCHEMA = "\nCREATE VIRTUAL TABLE IF NOT EXISTS search_fts USING fts5(\n kind UNINDEXED,\n id UNINDEXED,\n database_id UNINDEXED,\n title,\n body\n);\n";
3
3
  //# sourceMappingURL=schema.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/core/schema.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,WAAW,kzKAgJvB,CAAC;AAGF,eAAO,MAAM,UAAU,sJAQtB,CAAC"}
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/core/schema.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,WAAW,kxVA6OvB,CAAC;AAGF,eAAO,MAAM,UAAU,sJAQtB,CAAC"}
@@ -0,0 +1,60 @@
1
+ import type { DbDriver } from "./driver.ts";
2
+ export type ShareKind = "doc" | "database" | "site";
3
+ export type SharePermission = "view" | "edit";
4
+ export type ShareTransport = "server" | "s3";
5
+ export interface ShareRow {
6
+ slug: string;
7
+ kind: ShareKind;
8
+ target_id: string;
9
+ permission: SharePermission;
10
+ transport: ShareTransport;
11
+ /** base64 salt; null = no password. */
12
+ pw_salt: string | null;
13
+ /** base64 PBKDF2 verifier — server path only (s3 password is client-side E2EE). */
14
+ pw_hash: string | null;
15
+ /** epoch ms; null = never. */
16
+ expires_at: number | null;
17
+ /** synthetic node id every edit through this share is attributed to (edit only). */
18
+ guest_node_id: string | null;
19
+ /** Reachable base URL chosen at creation (for the link / source label). */
20
+ served_base: string | null;
21
+ s3_peer_url: string | null;
22
+ s3_object_prefix: string | null;
23
+ s3_presign_exp: number | null;
24
+ s3_key_b64: string | null;
25
+ created_at: number;
26
+ }
27
+ export interface CreateShareInput {
28
+ kind: ShareKind;
29
+ target_id: string;
30
+ permission?: SharePermission;
31
+ /** base64 salt + verifier (server path); produce with hashSharePassword. */
32
+ pwSalt?: string | null;
33
+ pwHash?: string | null;
34
+ expiresAt?: number | null;
35
+ /** Reachable base URL recorded for the link / source label. */
36
+ servedBase?: string | null;
37
+ }
38
+ /**
39
+ * Mint a server-transport share row. (Object-storage shares are NOT stored here —
40
+ * they live in the bucket; see sync/share-export.ts.) Validates enums + the
41
+ * `edit ⇒ server` rule and allocates an unguessable slug (+ guest node for edit).
42
+ * The caller verifies that `target_id` exists.
43
+ */
44
+ export declare function createShare(db: DbDriver, input: CreateShareInput): ShareRow;
45
+ export declare function getShare(db: DbDriver, slug: string): ShareRow | null;
46
+ export declare function listShares(db: DbDriver): ShareRow[];
47
+ /** Shares pointing at one target — used to surface existing links in the UI. */
48
+ export declare function listSharesForTarget(db: DbDriver, targetId: string): ShareRow[];
49
+ export declare function deleteShare(db: DbDriver, slug: string): boolean;
50
+ /** True once a share is past its expiry (null expiry = never expires). */
51
+ export declare function shareExpired(row: ShareRow, now?: number): boolean;
52
+ /** Derive a fresh {salt, hash} verifier for the server-path password. */
53
+ export declare function hashSharePassword(password: string): Promise<{
54
+ salt: string;
55
+ hash: string;
56
+ }>;
57
+ /** Constant-time check of `password` against a share's stored verifier. A share
58
+ * with no password always returns true (nothing to verify). */
59
+ export declare function verifySharePassword(row: ShareRow, password: string): Promise<boolean>;
60
+ //# sourceMappingURL=shares.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shares.d.ts","sourceRoot":"","sources":["../../src/core/shares.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAK5C,MAAM,MAAM,SAAS,GAAG,KAAK,GAAG,UAAU,GAAG,MAAM,CAAC;AACpD,MAAM,MAAM,eAAe,GAAG,MAAM,GAAG,MAAM,CAAC;AAC9C,MAAM,MAAM,cAAc,GAAG,QAAQ,GAAG,IAAI,CAAC;AAI7C,MAAM,WAAW,QAAQ;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,SAAS,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,eAAe,CAAC;IAC5B,SAAS,EAAE,cAAc,CAAC;IAC1B,uCAAuC;IACvC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,mFAAmF;IACnF,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,8BAA8B;IAC9B,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,oFAAoF;IACpF,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,2EAA2E;IAC3E,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IAG3B,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,gBAAgB,EAAE,MAAM,GAAG,IAAI,CAAC;IAChC,cAAc,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,UAAU,EAAE,MAAM,CAAC;CACpB;AAKD,MAAM,WAAW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