@indigoai-us/hq-cloud 6.16.33 → 6.16.35

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 (64) hide show
  1. package/dist/bin/sync-runner-watch-loop.d.ts.map +1 -1
  2. package/dist/bin/sync-runner-watch-loop.js +20 -8
  3. package/dist/bin/sync-runner-watch-loop.js.map +1 -1
  4. package/dist/bin/sync-runner.test.js +15 -0
  5. package/dist/bin/sync-runner.test.js.map +1 -1
  6. package/dist/cli/share.d.ts.map +1 -1
  7. package/dist/cli/share.js +18 -7
  8. package/dist/cli/share.js.map +1 -1
  9. package/dist/cli/share.test.js +30 -3
  10. package/dist/cli/share.test.js.map +1 -1
  11. package/dist/cli/sync.d.ts.map +1 -1
  12. package/dist/cli/sync.js +3 -2
  13. package/dist/cli/sync.js.map +1 -1
  14. package/dist/cli/sync.test.js +6 -6
  15. package/dist/cli/sync.test.js.map +1 -1
  16. package/dist/journal-baseline.test.js +18 -15
  17. package/dist/journal-baseline.test.js.map +1 -1
  18. package/dist/journal-delta-writes.test.js +2 -2
  19. package/dist/journal-delta-writes.test.js.map +1 -1
  20. package/dist/journal-public-boundary.test.d.ts +2 -0
  21. package/dist/journal-public-boundary.test.d.ts.map +1 -0
  22. package/dist/journal-public-boundary.test.js +216 -0
  23. package/dist/journal-public-boundary.test.js.map +1 -0
  24. package/dist/journal-record-proxy.d.ts +11 -0
  25. package/dist/journal-record-proxy.d.ts.map +1 -0
  26. package/dist/journal-record-proxy.js +71 -0
  27. package/dist/journal-record-proxy.js.map +1 -0
  28. package/dist/journal-row-store.d.ts +152 -0
  29. package/dist/journal-row-store.d.ts.map +1 -0
  30. package/dist/journal-row-store.js +820 -0
  31. package/dist/journal-row-store.js.map +1 -0
  32. package/dist/journal-row-store.test.d.ts +2 -0
  33. package/dist/journal-row-store.test.d.ts.map +1 -0
  34. package/dist/journal-row-store.test.js +526 -0
  35. package/dist/journal-row-store.test.js.map +1 -0
  36. package/dist/journal-store.test.js +179 -16
  37. package/dist/journal-store.test.js.map +1 -1
  38. package/dist/journal.d.ts +6 -6
  39. package/dist/journal.d.ts.map +1 -1
  40. package/dist/journal.js +124 -49
  41. package/dist/journal.js.map +1 -1
  42. package/dist/journal.test.js +1 -1
  43. package/dist/journal.test.js.map +1 -1
  44. package/dist/remote-pull.js +6 -4
  45. package/dist/remote-pull.js.map +1 -1
  46. package/dist/remote-pull.test.js +72 -0
  47. package/dist/remote-pull.test.js.map +1 -1
  48. package/dist/session-log-retention.test.js +3 -3
  49. package/dist/session-log-retention.test.js.map +1 -1
  50. package/dist/sync/push-transport.d.ts +17 -0
  51. package/dist/sync/push-transport.d.ts.map +1 -1
  52. package/dist/sync/push-transport.js +42 -6
  53. package/dist/sync/push-transport.js.map +1 -1
  54. package/dist/sync/push-transport.test.d.ts +2 -0
  55. package/dist/sync/push-transport.test.d.ts.map +1 -0
  56. package/dist/sync/push-transport.test.js +57 -0
  57. package/dist/sync/push-transport.test.js.map +1 -0
  58. package/dist/watcher.d.ts +3 -0
  59. package/dist/watcher.d.ts.map +1 -1
  60. package/dist/watcher.js +26 -4
  61. package/dist/watcher.js.map +1 -1
  62. package/dist/watcher.test.js +72 -1
  63. package/dist/watcher.test.js.map +1 -1
  64. package/package.json +1 -1
@@ -0,0 +1,820 @@
1
+ /** The storage decision for each journal field; `satisfies` makes schema drift a type error. */
2
+ export const JOURNAL_ENTRY_STORAGE = {
3
+ hash: "column",
4
+ size: "column",
5
+ syncedAt: "column",
6
+ direction: "column",
7
+ kind: "column",
8
+ createdBySub: "column",
9
+ remoteEtag: "column",
10
+ mtimeMs: "column",
11
+ ctimeMs: "column",
12
+ removedAt: "side-table",
13
+ removedReason: "side-table",
14
+ localDeleteIntent: "side-table",
15
+ outOfScopeProtected: "column",
16
+ skillMetadataPending: "column",
17
+ localDiverges: "column",
18
+ };
19
+ const HASH_BYTES = 32;
20
+ const ETAG_BYTES = 16;
21
+ const INITIAL_ROW_CAPACITY = 16;
22
+ const INITIAL_LEAF_CAPACITY = 1_024;
23
+ const INITIAL_BUCKET_CAPACITY = 32;
24
+ const DIRECTION_DOWN = 1 << 0;
25
+ const KIND_PRESENT = 1 << 1;
26
+ const KIND_SYMLINK = 1 << 2;
27
+ const REMOTE_ETAG_PRESENT = 1 << 3;
28
+ const MTIME_PRESENT = 1 << 4;
29
+ const CTIME_PRESENT = 1 << 5;
30
+ const CREATED_BY_SUB_PRESENT = 1 << 6;
31
+ const OUT_OF_SCOPE_PROTECTED_PRESENT = 1 << 7;
32
+ const OUT_OF_SCOPE_PROTECTED_VALUE = 1 << 8;
33
+ const SKILL_METADATA_PENDING_PRESENT = 1 << 9;
34
+ const SKILL_METADATA_PENDING_VALUE = 1 << 10;
35
+ const LOCAL_DIVERGES_PRESENT = 1 << 11;
36
+ const LOCAL_DIVERGES_VALUE = 1 << 12;
37
+ const HASH_FALLBACK_PRESENT = 1 << 13;
38
+ const ETAG_FALLBACK_PRESENT = 1 << 14;
39
+ const SYNCED_AT_FALLBACK_PRESENT = 1 << 15;
40
+ const textDecoder = new TextDecoder();
41
+ const textEncoder = new TextEncoder();
42
+ const lowerHex = /^[0-9a-f]+$/;
43
+ const CANONICAL_ENTRY_KEY_ORDER = [
44
+ "createdBySub",
45
+ "ctimeMs",
46
+ "direction",
47
+ "hash",
48
+ "kind",
49
+ "localDeleteIntent",
50
+ "localDiverges",
51
+ "mtimeMs",
52
+ "outOfScopeProtected",
53
+ "remoteEtag",
54
+ "removedAt",
55
+ "removedReason",
56
+ "size",
57
+ "skillMetadataPending",
58
+ "syncedAt",
59
+ ];
60
+ function splitPath(path) {
61
+ const slash = path.lastIndexOf("/");
62
+ return slash === -1
63
+ ? { directory: "", leaf: path }
64
+ : { directory: path.slice(0, slash), leaf: path.slice(slash + 1) };
65
+ }
66
+ function isCanonicalHex(value, length) {
67
+ return value.length === length && lowerHex.test(value);
68
+ }
69
+ function decodeHex(target, offset, value) {
70
+ for (let index = 0; index < value.length / 2; index++) {
71
+ target[offset + index] = Number.parseInt(value.slice(index * 2, index * 2 + 2), 16);
72
+ }
73
+ }
74
+ function encodeHex(source, offset, length) {
75
+ let value = "";
76
+ for (let index = 0; index < length; index++)
77
+ value += source[offset + index].toString(16).padStart(2, "0");
78
+ return value;
79
+ }
80
+ function canonicalEpoch(value) {
81
+ const epoch = new Date(value).getTime();
82
+ if (!Number.isFinite(epoch))
83
+ return undefined;
84
+ try {
85
+ return new Date(epoch).toISOString() === value ? epoch : undefined;
86
+ }
87
+ catch {
88
+ return undefined;
89
+ }
90
+ }
91
+ function cloneIntent(intent) {
92
+ return {
93
+ version: 1,
94
+ remoteEtag: intent.remoteEtag,
95
+ localHash: intent.localHash,
96
+ localKind: intent.localKind,
97
+ };
98
+ }
99
+ function bytesEqual(left, right) {
100
+ if (left.length !== right.length)
101
+ return false;
102
+ for (let index = 0; index < left.length; index++) {
103
+ if (left[index] !== right[index])
104
+ return false;
105
+ }
106
+ return true;
107
+ }
108
+ function freezeEntry(entry) {
109
+ if (entry.localDeleteIntent !== undefined)
110
+ Object.freeze(entry.localDeleteIntent);
111
+ return Object.freeze(entry);
112
+ }
113
+ /** Compact before-image used by the dirty log to avoid retaining row objects. */
114
+ export function journalEntryFingerprint(entry) {
115
+ const record = entry;
116
+ const keys = Object.keys(record).sort();
117
+ let text = "";
118
+ for (const key of keys) {
119
+ const value = record[key];
120
+ text += key;
121
+ text += "\u0001";
122
+ if (value === undefined)
123
+ text += "u";
124
+ else if (value === null)
125
+ text += "n";
126
+ else if (typeof value === "object")
127
+ text += "o" + JSON.stringify(value);
128
+ else if (typeof value === "string")
129
+ text += "s" + JSON.stringify(value);
130
+ else
131
+ text += typeof value + String(value);
132
+ text += "\u0000";
133
+ }
134
+ let h1 = 0xdeadbeef;
135
+ let h2 = 0x41c6ce57;
136
+ for (let index = 0; index < text.length; index++) {
137
+ const character = text.charCodeAt(index);
138
+ h1 = Math.imul(h1 ^ character, 2654435761);
139
+ h2 = Math.imul(h2 ^ character, 1597334677);
140
+ }
141
+ h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507);
142
+ h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909);
143
+ h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507);
144
+ h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909);
145
+ return 4294967296 * (2097151 & h2) + (h1 >>> 0);
146
+ }
147
+ /**
148
+ * The record-like view used by internal callers. It has no object-indexing
149
+ * semantics: paths and rows are materialised only at the operation boundary.
150
+ */
151
+ export class JournalRows {
152
+ store;
153
+ constructor(store) {
154
+ this.store = store;
155
+ }
156
+ get size() {
157
+ return this.store.size;
158
+ }
159
+ get(path) {
160
+ return this.store.get(path);
161
+ }
162
+ has(path) {
163
+ return this.store.has(path);
164
+ }
165
+ set(path, entry) {
166
+ this.store.set(path, entry);
167
+ }
168
+ delete(path) {
169
+ return this.store.delete(path);
170
+ }
171
+ entries() {
172
+ return this.store.entries();
173
+ }
174
+ keys() {
175
+ return this.store.keys();
176
+ }
177
+ values() {
178
+ return this.store.values();
179
+ }
180
+ toRecord() {
181
+ return this.store.toRecord();
182
+ }
183
+ }
184
+ /**
185
+ * A struct-of-arrays store for resident journal rows. It deliberately owns no
186
+ * file format or proxy boundary; those arrive in later journal phases.
187
+ */
188
+ export class JournalRowStore {
189
+ hashes;
190
+ remoteEtags;
191
+ sizes;
192
+ mtimes;
193
+ ctimes;
194
+ syncedAts;
195
+ flags;
196
+ createdBySubs;
197
+ dirIds;
198
+ leaves;
199
+ propertyOrderIds;
200
+ iterationNext;
201
+ iterationPrevious;
202
+ leafBlob;
203
+ leafLength = 0;
204
+ buckets;
205
+ directories = [""];
206
+ directoryIds = new Map([["", 0]]);
207
+ authors = [""];
208
+ authorIds = new Map();
209
+ freeRows = [];
210
+ freeRowSet = new Set();
211
+ hashFallbacks = new Map();
212
+ etagFallbacks = new Map();
213
+ syncedAtFallbacks = new Map();
214
+ leafFallbacks = new Map();
215
+ removedAts = new Map();
216
+ removedReasons = new Map();
217
+ localDeleteIntents = new Map();
218
+ propertyOrders = [[...CANONICAL_ENTRY_KEY_ORDER]];
219
+ propertyOrderIdsByLayout = new Map([
220
+ [CANONICAL_ENTRY_KEY_ORDER.join("\u0000"), 0],
221
+ ]);
222
+ dirtyLog = [];
223
+ dirtyPaths = new Set();
224
+ trackChanges;
225
+ nextRowId = 0;
226
+ liveRows = 0;
227
+ firstIterationRowId = -1;
228
+ lastIterationRowId = -1;
229
+ rows = new JournalRows(this);
230
+ constructor(initialCapacityOrOptions = INITIAL_ROW_CAPACITY, options = {}) {
231
+ const initialCapacity = typeof initialCapacityOrOptions === "number"
232
+ ? initialCapacityOrOptions
233
+ : INITIAL_ROW_CAPACITY;
234
+ const selectedOptions = typeof initialCapacityOrOptions === "number"
235
+ ? options
236
+ : initialCapacityOrOptions;
237
+ this.trackChanges = selectedOptions.trackChanges ?? true;
238
+ const capacity = Math.max(INITIAL_ROW_CAPACITY, initialCapacity);
239
+ this.hashes = new Uint8Array(HASH_BYTES * capacity);
240
+ this.remoteEtags = new Uint8Array(ETAG_BYTES * capacity);
241
+ this.sizes = new Float64Array(capacity);
242
+ this.mtimes = new Float64Array(capacity);
243
+ this.ctimes = new Float64Array(capacity);
244
+ this.syncedAts = new Float64Array(capacity);
245
+ this.flags = new Uint16Array(capacity);
246
+ this.createdBySubs = new Uint32Array(capacity);
247
+ this.dirIds = new Uint32Array(capacity);
248
+ this.leaves = new Uint32Array(capacity);
249
+ this.propertyOrderIds = new Uint16Array(capacity);
250
+ this.iterationNext = new Int32Array(capacity).fill(-1);
251
+ this.iterationPrevious = new Int32Array(capacity).fill(-1);
252
+ this.leafBlob = new Uint8Array(Math.max(INITIAL_LEAF_CAPACITY, capacity * 32));
253
+ this.buckets = new Uint32Array(Math.max(INITIAL_BUCKET_CAPACITY, bucketCapacityFor(capacity)));
254
+ }
255
+ get size() {
256
+ return this.liveRows;
257
+ }
258
+ /** Exposed for diagnostics and path-identity tests; id zero is the root directory. */
259
+ get directoryCount() {
260
+ return this.directories.length;
261
+ }
262
+ get(path) {
263
+ const rowId = this.findRowId(path);
264
+ return rowId === undefined ? undefined : this.materialize(rowId);
265
+ }
266
+ has(path) {
267
+ return this.findRowId(path) !== undefined;
268
+ }
269
+ /** Remove all rows and pending changes so the store can be reused cleanly. */
270
+ clear() {
271
+ this.reset(INITIAL_ROW_CAPACITY);
272
+ this.dirtyLog = [];
273
+ this.dirtyPaths.clear();
274
+ }
275
+ set(path, entry) {
276
+ const { directory, leaf } = splitPath(path);
277
+ const existingRowId = this.findRowId(path);
278
+ if (existingRowId !== undefined) {
279
+ if (this.trackChanges)
280
+ this.recordDirty(existingRowId, path, true, journalEntryFingerprint(this.materialize(existingRowId)));
281
+ this.writeEntry(existingRowId, entry);
282
+ this.storeLeafFallback(existingRowId, leaf, this.leafBytes(existingRowId));
283
+ return;
284
+ }
285
+ const directoryId = this.internDirectory(directory);
286
+ const leafBytes = textEncoder.encode(leaf);
287
+ this.ensureBucketCapacity();
288
+ const rowId = this.allocateRow();
289
+ if (this.trackChanges)
290
+ this.recordDirty(rowId, path, false, undefined);
291
+ this.dirIds[rowId] = directoryId;
292
+ this.leaves[rowId] = this.appendLeaf(leafBytes);
293
+ this.writeEntry(rowId, entry);
294
+ this.storeLeafFallback(rowId, leaf, leafBytes);
295
+ this.insertIndex(rowId);
296
+ this.appendIterationRow(rowId);
297
+ this.liveRows++;
298
+ }
299
+ delete(path) {
300
+ const rowId = this.findRowId(path);
301
+ if (rowId === undefined)
302
+ return false;
303
+ if (this.trackChanges)
304
+ this.recordDirty(rowId, path, true, journalEntryFingerprint(this.materialize(rowId)));
305
+ this.deleteIndex(rowId);
306
+ this.removeIterationRow(rowId);
307
+ this.clearRow(rowId);
308
+ this.freeRows.push(rowId);
309
+ this.freeRowSet.add(rowId);
310
+ this.liveRows--;
311
+ return true;
312
+ }
313
+ *entries() {
314
+ for (let rowId = this.firstIterationRowId; rowId !== -1; rowId = this.iterationNext[rowId]) {
315
+ yield [this.pathForRow(rowId), this.materialize(rowId)];
316
+ }
317
+ }
318
+ *keys() {
319
+ for (let rowId = this.firstIterationRowId; rowId !== -1; rowId = this.iterationNext[rowId]) {
320
+ yield this.pathForRow(rowId);
321
+ }
322
+ }
323
+ *entriesForPrefix(prefix) {
324
+ const normalizedPrefix = prefix.replace(/\/+$/, "");
325
+ if (normalizedPrefix === "") {
326
+ yield* this.entries();
327
+ return;
328
+ }
329
+ const exactRowId = this.findRowId(normalizedPrefix);
330
+ const descendantPrefix = `${normalizedPrefix}/`;
331
+ for (let rowId = this.firstIterationRowId; rowId !== -1; rowId = this.iterationNext[rowId]) {
332
+ const directory = this.directories[this.dirIds[rowId]];
333
+ if (rowId === exactRowId ||
334
+ directory === normalizedPrefix ||
335
+ directory.startsWith(descendantPrefix)) {
336
+ yield [this.pathForRow(rowId), this.materialize(rowId)];
337
+ }
338
+ }
339
+ }
340
+ *values() {
341
+ for (const [, entry] of this.entries())
342
+ yield entry;
343
+ }
344
+ toRecord() {
345
+ const record = {};
346
+ for (const [path, entry] of this.entries()) {
347
+ Object.defineProperty(record, path, {
348
+ value: entry,
349
+ enumerable: true,
350
+ configurable: true,
351
+ writable: true,
352
+ });
353
+ }
354
+ return record;
355
+ }
356
+ /**
357
+ * Returns and clears changes since the prior checkpoint.
358
+ *
359
+ * A non-tracking store has no change stream; asking it to drain is a caller
360
+ * error rather than an empty checkpoint.
361
+ */
362
+ drainDirtyLog() {
363
+ if (!this.trackChanges)
364
+ throw new Error("Change tracking is disabled for this journal row store");
365
+ const drained = this.dirtyLog;
366
+ this.dirtyLog = [];
367
+ this.dirtyPaths.clear();
368
+ return drained;
369
+ }
370
+ /** Read pending changes without acknowledging them; writeJournal snapshots before append. */
371
+ dirtyLogSnapshot() {
372
+ if (!this.trackChanges)
373
+ throw new Error("Change tracking is disabled for this journal row store");
374
+ return this.dirtyLog;
375
+ }
376
+ /** Alias for callers that treat the dirty log as a change stream. */
377
+ drainChanges() {
378
+ return this.drainDirtyLog();
379
+ }
380
+ /**
381
+ * Rewrites active rows densely. Checkpoints drain the dirty log first, so
382
+ * row ids in already-emitted records remain meaningful.
383
+ */
384
+ compact() {
385
+ if (this.trackChanges && this.dirtyLog.length !== 0)
386
+ throw new Error("Drain the journal dirty log before compaction");
387
+ const entries = [...this.entries()];
388
+ this.reset(Math.max(INITIAL_ROW_CAPACITY, entries.length));
389
+ for (const [path, entry] of entries)
390
+ this.insertWithoutDirtyLog(path, entry);
391
+ }
392
+ reset(rowCapacity) {
393
+ this.hashes = new Uint8Array(HASH_BYTES * rowCapacity);
394
+ this.remoteEtags = new Uint8Array(ETAG_BYTES * rowCapacity);
395
+ this.sizes = new Float64Array(rowCapacity);
396
+ this.mtimes = new Float64Array(rowCapacity);
397
+ this.ctimes = new Float64Array(rowCapacity);
398
+ this.syncedAts = new Float64Array(rowCapacity);
399
+ this.flags = new Uint16Array(rowCapacity);
400
+ this.createdBySubs = new Uint32Array(rowCapacity);
401
+ this.dirIds = new Uint32Array(rowCapacity);
402
+ this.leaves = new Uint32Array(rowCapacity);
403
+ this.propertyOrderIds = new Uint16Array(rowCapacity);
404
+ this.iterationNext = new Int32Array(rowCapacity).fill(-1);
405
+ this.iterationPrevious = new Int32Array(rowCapacity).fill(-1);
406
+ this.leafBlob = new Uint8Array(Math.max(INITIAL_LEAF_CAPACITY, rowCapacity * 8));
407
+ this.leafLength = 0;
408
+ this.buckets = new Uint32Array(bucketCapacityFor(rowCapacity));
409
+ this.directories.length = 1;
410
+ this.directoryIds.clear();
411
+ this.directoryIds.set("", 0);
412
+ this.authors.length = 1;
413
+ this.authorIds.clear();
414
+ this.freeRows.length = 0;
415
+ this.freeRowSet.clear();
416
+ this.hashFallbacks.clear();
417
+ this.etagFallbacks.clear();
418
+ this.syncedAtFallbacks.clear();
419
+ this.leafFallbacks.clear();
420
+ this.removedAts.clear();
421
+ this.removedReasons.clear();
422
+ this.localDeleteIntents.clear();
423
+ this.nextRowId = 0;
424
+ this.liveRows = 0;
425
+ this.firstIterationRowId = -1;
426
+ this.lastIterationRowId = -1;
427
+ }
428
+ insertWithoutDirtyLog(path, entry) {
429
+ const { directory, leaf } = splitPath(path);
430
+ const leafBytes = textEncoder.encode(leaf);
431
+ this.ensureBucketCapacity();
432
+ const rowId = this.allocateRow();
433
+ this.dirIds[rowId] = this.internDirectory(directory);
434
+ this.leaves[rowId] = this.appendLeaf(leafBytes);
435
+ this.writeEntry(rowId, entry);
436
+ this.storeLeafFallback(rowId, leaf, leafBytes);
437
+ this.insertIndex(rowId);
438
+ this.appendIterationRow(rowId);
439
+ this.liveRows++;
440
+ }
441
+ recordDirty(rowId, path, hadPrevious, previousFingerprint) {
442
+ if (this.dirtyPaths.has(path))
443
+ return;
444
+ this.dirtyPaths.add(path);
445
+ this.dirtyLog.push(Object.freeze({ rowId, path, hadPrevious, previousFingerprint }));
446
+ }
447
+ allocateRow() {
448
+ const reused = this.freeRows.pop();
449
+ if (reused !== undefined) {
450
+ this.freeRowSet.delete(reused);
451
+ return reused;
452
+ }
453
+ const rowId = this.nextRowId++;
454
+ this.ensureRowCapacity(this.nextRowId);
455
+ return rowId;
456
+ }
457
+ ensureRowCapacity(required) {
458
+ if (required <= this.flags.length)
459
+ return;
460
+ const previousCapacity = this.flags.length;
461
+ const capacity = Math.max(required, this.flags.length * 2);
462
+ this.hashes = growUint8(this.hashes, capacity * HASH_BYTES);
463
+ this.remoteEtags = growUint8(this.remoteEtags, capacity * ETAG_BYTES);
464
+ this.sizes = growFloat64(this.sizes, capacity);
465
+ this.mtimes = growFloat64(this.mtimes, capacity);
466
+ this.ctimes = growFloat64(this.ctimes, capacity);
467
+ this.syncedAts = growFloat64(this.syncedAts, capacity);
468
+ this.flags = growUint16(this.flags, capacity);
469
+ this.createdBySubs = growUint32(this.createdBySubs, capacity);
470
+ this.dirIds = growUint32(this.dirIds, capacity);
471
+ this.leaves = growUint32(this.leaves, capacity);
472
+ this.propertyOrderIds = growUint16(this.propertyOrderIds, capacity);
473
+ this.iterationNext = growInt32(this.iterationNext, capacity);
474
+ this.iterationPrevious = growInt32(this.iterationPrevious, capacity);
475
+ this.iterationNext.fill(-1, previousCapacity);
476
+ this.iterationPrevious.fill(-1, previousCapacity);
477
+ }
478
+ writeEntry(rowId, entry) {
479
+ const directoryId = this.dirIds[rowId];
480
+ const leafOffset = this.leaves[rowId];
481
+ const nextIterationRowId = this.iterationNext[rowId];
482
+ const previousIterationRowId = this.iterationPrevious[rowId];
483
+ const propertyOrderId = this.propertyOrderIdFor(entry);
484
+ this.clearRow(rowId);
485
+ this.dirIds[rowId] = directoryId;
486
+ this.leaves[rowId] = leafOffset;
487
+ this.iterationNext[rowId] = nextIterationRowId;
488
+ this.iterationPrevious[rowId] = previousIterationRowId;
489
+ this.propertyOrderIds[rowId] = propertyOrderId;
490
+ let flags = entry.direction === "down" ? DIRECTION_DOWN : 0;
491
+ if (isCanonicalHex(entry.hash, HASH_BYTES * 2)) {
492
+ decodeHex(this.hashes, rowId * HASH_BYTES, entry.hash);
493
+ }
494
+ else {
495
+ flags |= HASH_FALLBACK_PRESENT;
496
+ this.hashFallbacks.set(rowId, entry.hash);
497
+ }
498
+ this.sizes[rowId] = entry.size;
499
+ const epoch = canonicalEpoch(entry.syncedAt);
500
+ if (epoch === undefined) {
501
+ flags |= SYNCED_AT_FALLBACK_PRESENT;
502
+ this.syncedAtFallbacks.set(rowId, entry.syncedAt);
503
+ }
504
+ else {
505
+ this.syncedAts[rowId] = epoch;
506
+ }
507
+ if (entry.kind !== undefined) {
508
+ flags |= KIND_PRESENT;
509
+ if (entry.kind === "symlink")
510
+ flags |= KIND_SYMLINK;
511
+ }
512
+ if (entry.createdBySub !== undefined) {
513
+ flags |= CREATED_BY_SUB_PRESENT;
514
+ this.createdBySubs[rowId] = this.internAuthor(entry.createdBySub);
515
+ }
516
+ if (entry.remoteEtag !== undefined) {
517
+ flags |= REMOTE_ETAG_PRESENT;
518
+ if (isCanonicalHex(entry.remoteEtag, ETAG_BYTES * 2)) {
519
+ decodeHex(this.remoteEtags, rowId * ETAG_BYTES, entry.remoteEtag);
520
+ }
521
+ else {
522
+ flags |= ETAG_FALLBACK_PRESENT;
523
+ this.etagFallbacks.set(rowId, entry.remoteEtag);
524
+ }
525
+ }
526
+ if (entry.mtimeMs !== undefined) {
527
+ flags |= MTIME_PRESENT;
528
+ this.mtimes[rowId] = entry.mtimeMs;
529
+ }
530
+ if (entry.ctimeMs !== undefined) {
531
+ flags |= CTIME_PRESENT;
532
+ this.ctimes[rowId] = entry.ctimeMs;
533
+ }
534
+ if (entry.removedAt !== undefined)
535
+ this.removedAts.set(rowId, entry.removedAt);
536
+ if (entry.removedReason !== undefined)
537
+ this.removedReasons.set(rowId, entry.removedReason);
538
+ if (entry.localDeleteIntent !== undefined)
539
+ this.localDeleteIntents.set(rowId, cloneIntent(entry.localDeleteIntent));
540
+ flags = setOptionalBoolean(flags, OUT_OF_SCOPE_PROTECTED_PRESENT, OUT_OF_SCOPE_PROTECTED_VALUE, entry.outOfScopeProtected);
541
+ flags = setOptionalBoolean(flags, SKILL_METADATA_PENDING_PRESENT, SKILL_METADATA_PENDING_VALUE, entry.skillMetadataPending);
542
+ flags = setOptionalBoolean(flags, LOCAL_DIVERGES_PRESENT, LOCAL_DIVERGES_VALUE, entry.localDiverges);
543
+ this.flags[rowId] = flags;
544
+ }
545
+ clearRow(rowId) {
546
+ this.hashes.fill(0, rowId * HASH_BYTES, (rowId + 1) * HASH_BYTES);
547
+ this.remoteEtags.fill(0, rowId * ETAG_BYTES, (rowId + 1) * ETAG_BYTES);
548
+ this.sizes[rowId] = 0;
549
+ this.mtimes[rowId] = 0;
550
+ this.ctimes[rowId] = 0;
551
+ this.syncedAts[rowId] = 0;
552
+ this.flags[rowId] = 0;
553
+ this.createdBySubs[rowId] = 0;
554
+ this.dirIds[rowId] = 0;
555
+ this.leaves[rowId] = 0;
556
+ this.propertyOrderIds[rowId] = 0;
557
+ this.iterationNext[rowId] = -1;
558
+ this.iterationPrevious[rowId] = -1;
559
+ this.hashFallbacks.delete(rowId);
560
+ this.etagFallbacks.delete(rowId);
561
+ this.syncedAtFallbacks.delete(rowId);
562
+ this.leafFallbacks.delete(rowId);
563
+ this.removedAts.delete(rowId);
564
+ this.removedReasons.delete(rowId);
565
+ this.localDeleteIntents.delete(rowId);
566
+ }
567
+ propertyOrderIdFor(entry) {
568
+ const keys = Object.keys(entry);
569
+ const layout = keys.join("\u0000");
570
+ const known = this.propertyOrderIdsByLayout.get(layout);
571
+ if (known !== undefined)
572
+ return known;
573
+ const id = this.propertyOrders.length;
574
+ if (id > 0xffff)
575
+ throw new Error("Too many journal entry property layouts");
576
+ this.propertyOrders.push(keys);
577
+ this.propertyOrderIdsByLayout.set(layout, id);
578
+ return id;
579
+ }
580
+ appendIterationRow(rowId) {
581
+ const previous = this.lastIterationRowId;
582
+ this.iterationPrevious[rowId] = previous;
583
+ this.iterationNext[rowId] = -1;
584
+ if (previous === -1)
585
+ this.firstIterationRowId = rowId;
586
+ else
587
+ this.iterationNext[previous] = rowId;
588
+ this.lastIterationRowId = rowId;
589
+ }
590
+ removeIterationRow(rowId) {
591
+ const previous = this.iterationPrevious[rowId];
592
+ const next = this.iterationNext[rowId];
593
+ if (previous === -1)
594
+ this.firstIterationRowId = next;
595
+ else
596
+ this.iterationNext[previous] = next;
597
+ if (next === -1)
598
+ this.lastIterationRowId = previous;
599
+ else
600
+ this.iterationPrevious[next] = previous;
601
+ this.iterationNext[rowId] = -1;
602
+ this.iterationPrevious[rowId] = -1;
603
+ }
604
+ materialize(rowId) {
605
+ const flags = this.flags[rowId];
606
+ const values = {};
607
+ if (flags & CREATED_BY_SUB_PRESENT)
608
+ values.createdBySub = this.authors[this.createdBySubs[rowId]];
609
+ if (flags & CTIME_PRESENT)
610
+ values.ctimeMs = this.ctimes[rowId];
611
+ values.direction = flags & DIRECTION_DOWN ? "down" : "up";
612
+ values.hash = flags & HASH_FALLBACK_PRESENT
613
+ ? this.hashFallbacks.get(rowId)
614
+ : encodeHex(this.hashes, rowId * HASH_BYTES, HASH_BYTES);
615
+ if (flags & KIND_PRESENT)
616
+ values.kind = flags & KIND_SYMLINK ? "symlink" : "file";
617
+ const localDeleteIntent = this.localDeleteIntents.get(rowId);
618
+ if (localDeleteIntent !== undefined)
619
+ values.localDeleteIntent = cloneIntent(localDeleteIntent);
620
+ readOptionalBoolean(values, "localDiverges", flags, LOCAL_DIVERGES_PRESENT, LOCAL_DIVERGES_VALUE);
621
+ if (flags & MTIME_PRESENT)
622
+ values.mtimeMs = this.mtimes[rowId];
623
+ readOptionalBoolean(values, "outOfScopeProtected", flags, OUT_OF_SCOPE_PROTECTED_PRESENT, OUT_OF_SCOPE_PROTECTED_VALUE);
624
+ if (flags & REMOTE_ETAG_PRESENT) {
625
+ values.remoteEtag = flags & ETAG_FALLBACK_PRESENT
626
+ ? this.etagFallbacks.get(rowId)
627
+ : encodeHex(this.remoteEtags, rowId * ETAG_BYTES, ETAG_BYTES);
628
+ }
629
+ const removedAt = this.removedAts.get(rowId);
630
+ if (removedAt !== undefined)
631
+ values.removedAt = removedAt;
632
+ const removedReason = this.removedReasons.get(rowId);
633
+ if (removedReason !== undefined)
634
+ values.removedReason = removedReason;
635
+ values.size = this.sizes[rowId];
636
+ readOptionalBoolean(values, "skillMetadataPending", flags, SKILL_METADATA_PENDING_PRESENT, SKILL_METADATA_PENDING_VALUE);
637
+ values.syncedAt = flags & SYNCED_AT_FALLBACK_PRESENT
638
+ ? this.syncedAtFallbacks.get(rowId)
639
+ : new Date(this.syncedAts[rowId]).toISOString();
640
+ const entry = {};
641
+ const source = values;
642
+ const target = entry;
643
+ for (const key of this.propertyOrders[this.propertyOrderIds[rowId]]) {
644
+ if (Object.hasOwn(source, key))
645
+ target[key] = source[key];
646
+ }
647
+ return freezeEntry(entry);
648
+ }
649
+ internDirectory(directory) {
650
+ const existing = this.directoryIds.get(directory);
651
+ if (existing !== undefined)
652
+ return existing;
653
+ const id = this.directories.length;
654
+ this.directories.push(directory);
655
+ this.directoryIds.set(directory, id);
656
+ return id;
657
+ }
658
+ internAuthor(author) {
659
+ const existing = this.authorIds.get(author);
660
+ if (existing !== undefined)
661
+ return existing;
662
+ const id = this.authors.length;
663
+ this.authors.push(author);
664
+ this.authorIds.set(author, id);
665
+ return id;
666
+ }
667
+ appendLeaf(leaf) {
668
+ const length = leaf.length;
669
+ const needed = this.leafLength + 4 + length;
670
+ if (needed > this.leafBlob.length) {
671
+ let capacity = this.leafBlob.length;
672
+ while (capacity < needed)
673
+ capacity *= 2;
674
+ this.leafBlob = growUint8(this.leafBlob, capacity);
675
+ }
676
+ const offset = this.leafLength;
677
+ writeUint32(this.leafBlob, offset, length);
678
+ this.leafBlob.set(leaf, offset + 4);
679
+ this.leafLength = needed;
680
+ return offset;
681
+ }
682
+ storeLeafFallback(rowId, leaf, leafBytes) {
683
+ if (textDecoder.decode(leafBytes) !== leaf)
684
+ this.leafFallbacks.set(rowId, leaf);
685
+ }
686
+ leafBytes(rowId) {
687
+ const offset = this.leaves[rowId];
688
+ const length = readUint32(this.leafBlob, offset);
689
+ return this.leafBlob.subarray(offset + 4, offset + 4 + length);
690
+ }
691
+ pathForRow(rowId) {
692
+ const directory = this.directories[this.dirIds[rowId]];
693
+ const leaf = this.leafFallbacks.has(rowId)
694
+ ? this.leafFallbacks.get(rowId)
695
+ : textDecoder.decode(this.leafBytes(rowId));
696
+ return directory === "" ? leaf : directory + "/" + leaf;
697
+ }
698
+ findRowId(path) {
699
+ const { directory, leaf } = splitPath(path);
700
+ const directoryId = this.directoryIds.get(directory);
701
+ if (directoryId === undefined)
702
+ return undefined;
703
+ return this.findIndexedRow(directoryId, leaf);
704
+ }
705
+ findIndexedRow(directoryId, leaf) {
706
+ const leafBytes = textEncoder.encode(leaf);
707
+ const mask = this.buckets.length - 1;
708
+ let bucket = this.hashBytes(directoryId, leafBytes) & mask;
709
+ while (true) {
710
+ const stored = this.buckets[bucket];
711
+ if (stored === 0)
712
+ return undefined;
713
+ const rowId = stored - 1;
714
+ if (this.dirIds[rowId] === directoryId && bytesEqual(this.leafBytes(rowId), leafBytes)) {
715
+ const candidateLeafFallback = this.leafFallbacks.get(rowId);
716
+ if (candidateLeafFallback === undefined) {
717
+ if (textDecoder.decode(leafBytes) === leaf)
718
+ return rowId;
719
+ }
720
+ else if (candidateLeafFallback === leaf) {
721
+ return rowId;
722
+ }
723
+ }
724
+ bucket = (bucket + 1) & mask;
725
+ }
726
+ }
727
+ ensureBucketCapacity() {
728
+ if ((this.liveRows + 1) * 3 <= this.buckets.length * 2)
729
+ return;
730
+ this.rehash(this.buckets.length * 2);
731
+ }
732
+ rehash(capacity) {
733
+ this.buckets = new Uint32Array(capacity);
734
+ for (let rowId = 0; rowId < this.nextRowId; rowId++) {
735
+ if (!this.freeRowSet.has(rowId))
736
+ this.insertIndex(rowId);
737
+ }
738
+ }
739
+ insertIndex(rowId) {
740
+ const leaf = this.leafBytes(rowId);
741
+ const mask = this.buckets.length - 1;
742
+ let bucket = this.hashBytes(this.dirIds[rowId], leaf) & mask;
743
+ while (this.buckets[bucket] !== 0)
744
+ bucket = (bucket + 1) & mask;
745
+ this.buckets[bucket] = rowId + 1;
746
+ }
747
+ deleteIndex(rowId) {
748
+ const leaf = this.leafBytes(rowId);
749
+ const mask = this.buckets.length - 1;
750
+ let bucket = this.hashBytes(this.dirIds[rowId], leaf) & mask;
751
+ while (this.buckets[bucket] !== rowId + 1)
752
+ bucket = (bucket + 1) & mask;
753
+ this.buckets[bucket] = 0;
754
+ bucket = (bucket + 1) & mask;
755
+ while (this.buckets[bucket] !== 0) {
756
+ const displaced = this.buckets[bucket] - 1;
757
+ this.buckets[bucket] = 0;
758
+ this.insertIndex(displaced);
759
+ bucket = (bucket + 1) & mask;
760
+ }
761
+ }
762
+ hashBytes(directoryId, leaf) {
763
+ let hash = (2_166_136_261 ^ directoryId) >>> 0;
764
+ for (const byte of leaf)
765
+ hash = Math.imul(hash ^ byte, 16_777_619) >>> 0;
766
+ return hash;
767
+ }
768
+ }
769
+ /** A descriptive alias for call sites that prefer the design document's name. */
770
+ export { JournalRowStore as PackedJournalRowStore };
771
+ function bucketCapacityFor(rows) {
772
+ let capacity = INITIAL_BUCKET_CAPACITY;
773
+ while (rows * 3 > capacity * 2)
774
+ capacity *= 2;
775
+ return capacity;
776
+ }
777
+ function growUint8(source, length) {
778
+ const next = new Uint8Array(length);
779
+ next.set(source);
780
+ return next;
781
+ }
782
+ function growUint16(source, length) {
783
+ const next = new Uint16Array(length);
784
+ next.set(source);
785
+ return next;
786
+ }
787
+ function growUint32(source, length) {
788
+ const next = new Uint32Array(length);
789
+ next.set(source);
790
+ return next;
791
+ }
792
+ function growInt32(source, length) {
793
+ const next = new Int32Array(length);
794
+ next.set(source);
795
+ return next;
796
+ }
797
+ function growFloat64(source, length) {
798
+ const next = new Float64Array(length);
799
+ next.set(source);
800
+ return next;
801
+ }
802
+ function writeUint32(target, offset, value) {
803
+ target[offset] = value & 0xff;
804
+ target[offset + 1] = (value >>> 8) & 0xff;
805
+ target[offset + 2] = (value >>> 16) & 0xff;
806
+ target[offset + 3] = (value >>> 24) & 0xff;
807
+ }
808
+ function readUint32(source, offset) {
809
+ return (source[offset] | (source[offset + 1] << 8) | (source[offset + 2] << 16) | (source[offset + 3] << 24)) >>> 0;
810
+ }
811
+ function setOptionalBoolean(flags, presentBit, valueBit, value) {
812
+ if (value === undefined)
813
+ return flags;
814
+ return flags | presentBit | (value ? valueBit : 0);
815
+ }
816
+ function readOptionalBoolean(entry, key, flags, presentBit, valueBit) {
817
+ if (flags & presentBit)
818
+ entry[key] = Boolean(flags & valueBit);
819
+ }
820
+ //# sourceMappingURL=journal-row-store.js.map