@indigoai-us/hq-cloud 6.16.5 → 6.16.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/sync/event-sync.js +2 -2
- package/dist/sync/event-sync.js.map +1 -1
- package/dist/sync/index.d.ts +2 -2
- package/dist/sync/index.d.ts.map +1 -1
- package/dist/sync/index.js +1 -1
- package/dist/sync/index.js.map +1 -1
- package/dist/sync/push-event.d.ts +16 -0
- package/dist/sync/push-event.d.ts.map +1 -1
- package/dist/sync/push-event.js +46 -1
- package/dist/sync/push-event.js.map +1 -1
- package/dist/sync/push-event.test.js +38 -1
- package/dist/sync/push-event.test.js.map +1 -1
- package/dist/sync/state-store.d.ts +8 -0
- package/dist/sync/state-store.d.ts.map +1 -1
- package/dist/sync/state-store.js +37 -0
- package/dist/sync/state-store.js.map +1 -1
- package/dist/watcher.d.ts +49 -9
- package/dist/watcher.d.ts.map +1 -1
- package/dist/watcher.js +431 -160
- package/dist/watcher.js.map +1 -1
- package/dist/watcher.test.js +330 -38
- package/dist/watcher.test.js.map +1 -1
- package/package.json +1 -1
package/dist/watcher.js
CHANGED
|
@@ -17,6 +17,7 @@ import { isPersonalVaultExcluded } from "./personal-vault-exclusions.js";
|
|
|
17
17
|
import { StateStore, StateStoreLockError } from "./sync/state-store.js";
|
|
18
18
|
import { AGENCY_SYNC_REL, computePersonalCompanySubdirs, CONTINUITY_POINTER_REL, isCurrentContinuityThreadPath, SESSION_LOGS_SYNC_REL, PERSONAL_VAULT_EXCLUDED_TOP_LEVEL, } from "./personal-vault.js";
|
|
19
19
|
import { toPosixKey } from "./s3.js";
|
|
20
|
+
import { nativeSeparatorsToPosix, relativePathRejectionReason, } from "./sync/push-event.js";
|
|
20
21
|
import { getPublishFailureOutcome, getPublishSuccessOutcome, isRealtimeUnavailableError, serializePublishError, } from "./sync/push-transport.js";
|
|
21
22
|
import { emitCloudTelemetry, } from "./telemetry-events.js";
|
|
22
23
|
const DEBOUNCE_MS = 2000;
|
|
@@ -1812,8 +1813,16 @@ export function resolvePublishMinIntervalMs(env = process.env) {
|
|
|
1812
1813
|
}
|
|
1813
1814
|
const PUBLISHED_CONTENT_HASH_RECORD_SET = 0x5031;
|
|
1814
1815
|
const PUBLISHED_CONTENT_HASH_RECORD_DELETE = 0x5032;
|
|
1816
|
+
const PUBLISHED_CONTENT_HASH_RECORD_BATCH = 0x5033;
|
|
1817
|
+
/** Limit one WAL frame to a modest size while collapsing a reconcile pass. */
|
|
1818
|
+
export const DURABLE_PUBLISHED_HASH_BATCH_MAX_ENTRIES = 4_096;
|
|
1819
|
+
/** A crash can lose at most this small durable-memo window under light load. */
|
|
1820
|
+
export const DURABLE_PUBLISHED_HASH_BATCH_FLUSH_MS = 25;
|
|
1815
1821
|
const DURABLE_PUBLISHED_HASH_RETRY_INITIAL_MS = 250;
|
|
1816
1822
|
const DURABLE_PUBLISHED_HASH_RETRY_MAX_MS = 10_000;
|
|
1823
|
+
// Match HttpPushTransport's existing per-request abort deadline. If a custom
|
|
1824
|
+
// transport ignores it, disposal returns without closing the durable memo.
|
|
1825
|
+
const PUSH_EVENT_EMITTER_DISPOSE_DRAIN_MS = 60_000;
|
|
1817
1826
|
/**
|
|
1818
1827
|
* JSON snapshots deliberately retain the long-lived `{ hashes, order }` wire
|
|
1819
1828
|
* shape. At runtime, keep the order as a Map so replay and live writes can
|
|
@@ -1828,9 +1837,21 @@ const durablePublishedContentHashOrders = new WeakMap();
|
|
|
1828
1837
|
* journal row proves the object upload completed, while the event publish that
|
|
1829
1838
|
* follows can still fail or time out.
|
|
1830
1839
|
*/
|
|
1831
|
-
class DurablePublishedContentHashes {
|
|
1840
|
+
export class DurablePublishedContentHashes {
|
|
1832
1841
|
store;
|
|
1833
|
-
|
|
1842
|
+
clock;
|
|
1843
|
+
onBatchLockFailure;
|
|
1844
|
+
pending = [];
|
|
1845
|
+
batchRetries = new Set();
|
|
1846
|
+
flushTimer;
|
|
1847
|
+
flushPromise;
|
|
1848
|
+
flushRequested = false;
|
|
1849
|
+
microtaskFlushScheduled = false;
|
|
1850
|
+
closed = false;
|
|
1851
|
+
nextSequence = 0;
|
|
1852
|
+
constructor(stateDir, scopeId, maxPaths, initialHashes, clock, onBatchLockFailure = () => undefined) {
|
|
1853
|
+
this.clock = clock;
|
|
1854
|
+
this.onBatchLockFailure = onBatchLockFailure;
|
|
1834
1855
|
this.store = StateStore.open({
|
|
1835
1856
|
rootDir: stateDir,
|
|
1836
1857
|
scopeId: `event-push-published-content:${scopeId}`,
|
|
@@ -1843,25 +1864,258 @@ class DurablePublishedContentHashes {
|
|
|
1843
1864
|
get(relativePath) {
|
|
1844
1865
|
return durablePublishedContentHashValue(this.store.getState().hashes, relativePath);
|
|
1845
1866
|
}
|
|
1846
|
-
|
|
1847
|
-
|
|
1867
|
+
get pendingRetryCount() {
|
|
1868
|
+
return this.batchRetries.size;
|
|
1869
|
+
}
|
|
1870
|
+
async set(relativePath, contentHash, maxPaths, flushImmediately) {
|
|
1871
|
+
await this.enqueue({
|
|
1872
|
+
kind: "set",
|
|
1848
1873
|
relativePath,
|
|
1849
1874
|
contentHash,
|
|
1850
1875
|
maxPaths,
|
|
1851
|
-
});
|
|
1876
|
+
}, undefined, flushImmediately);
|
|
1852
1877
|
}
|
|
1853
1878
|
async setIfCurrent(relativePath, contentHash, maxPaths, shouldPersist) {
|
|
1854
|
-
return
|
|
1879
|
+
return this.enqueue({
|
|
1880
|
+
kind: "set",
|
|
1855
1881
|
relativePath,
|
|
1856
1882
|
contentHash,
|
|
1857
1883
|
maxPaths,
|
|
1858
|
-
}, shouldPersist)
|
|
1884
|
+
}, shouldPersist, true);
|
|
1859
1885
|
}
|
|
1860
|
-
async delete(relativePath) {
|
|
1861
|
-
await this.
|
|
1886
|
+
async delete(relativePath, flushImmediately) {
|
|
1887
|
+
await this.enqueue({ kind: "delete", relativePath }, undefined, flushImmediately);
|
|
1862
1888
|
}
|
|
1863
1889
|
async deleteIfCurrent(relativePath, shouldPersist) {
|
|
1864
|
-
return
|
|
1890
|
+
return this.enqueue({ kind: "delete", relativePath }, shouldPersist, true);
|
|
1891
|
+
}
|
|
1892
|
+
async dispose() {
|
|
1893
|
+
this.closed = true;
|
|
1894
|
+
this.clearFlushTimer();
|
|
1895
|
+
this.clearBatchRetries();
|
|
1896
|
+
let failure;
|
|
1897
|
+
while (this.flushPromise || this.pending.length > 0) {
|
|
1898
|
+
try {
|
|
1899
|
+
if (this.flushPromise) {
|
|
1900
|
+
await this.flushPromise;
|
|
1901
|
+
}
|
|
1902
|
+
else {
|
|
1903
|
+
await this.requestFlush();
|
|
1904
|
+
}
|
|
1905
|
+
}
|
|
1906
|
+
catch (error) {
|
|
1907
|
+
failure ??= error;
|
|
1908
|
+
}
|
|
1909
|
+
}
|
|
1910
|
+
if (failure !== undefined)
|
|
1911
|
+
throw failure;
|
|
1912
|
+
}
|
|
1913
|
+
enqueue(mutation, shouldPersist, flushImmediately) {
|
|
1914
|
+
if (this.closed)
|
|
1915
|
+
return Promise.resolve(false);
|
|
1916
|
+
this.supersedeBatchRetries(mutation.relativePath);
|
|
1917
|
+
let resolve;
|
|
1918
|
+
let reject;
|
|
1919
|
+
const result = new Promise((resolveResult, rejectResult) => {
|
|
1920
|
+
resolve = resolveResult;
|
|
1921
|
+
reject = rejectResult;
|
|
1922
|
+
});
|
|
1923
|
+
this.pending.push({
|
|
1924
|
+
mutation,
|
|
1925
|
+
sequence: ++this.nextSequence,
|
|
1926
|
+
shouldPersist,
|
|
1927
|
+
resolve,
|
|
1928
|
+
reject,
|
|
1929
|
+
settled: false,
|
|
1930
|
+
});
|
|
1931
|
+
if (flushImmediately) {
|
|
1932
|
+
this.requestMicrotaskFlush();
|
|
1933
|
+
}
|
|
1934
|
+
else if (this.pending.length >= DURABLE_PUBLISHED_HASH_BATCH_MAX_ENTRIES) {
|
|
1935
|
+
void this.requestFlush().catch(() => undefined);
|
|
1936
|
+
}
|
|
1937
|
+
else {
|
|
1938
|
+
this.armFlushTimer();
|
|
1939
|
+
}
|
|
1940
|
+
return result;
|
|
1941
|
+
}
|
|
1942
|
+
requestMicrotaskFlush() {
|
|
1943
|
+
if (this.microtaskFlushScheduled)
|
|
1944
|
+
return;
|
|
1945
|
+
this.microtaskFlushScheduled = true;
|
|
1946
|
+
queueMicrotask(() => {
|
|
1947
|
+
this.microtaskFlushScheduled = false;
|
|
1948
|
+
void this.requestFlush().catch(() => undefined);
|
|
1949
|
+
});
|
|
1950
|
+
}
|
|
1951
|
+
armFlushTimer() {
|
|
1952
|
+
if (this.flushTimer !== undefined || this.closed)
|
|
1953
|
+
return;
|
|
1954
|
+
this.flushTimer = this.clock.setTimeout(() => {
|
|
1955
|
+
this.flushTimer = undefined;
|
|
1956
|
+
void this.requestFlush().catch(() => undefined);
|
|
1957
|
+
}, DURABLE_PUBLISHED_HASH_BATCH_FLUSH_MS);
|
|
1958
|
+
}
|
|
1959
|
+
clearFlushTimer() {
|
|
1960
|
+
if (this.flushTimer === undefined)
|
|
1961
|
+
return;
|
|
1962
|
+
this.clock.clearTimeout(this.flushTimer);
|
|
1963
|
+
this.flushTimer = undefined;
|
|
1964
|
+
}
|
|
1965
|
+
requestFlush() {
|
|
1966
|
+
this.flushRequested = true;
|
|
1967
|
+
if (this.flushPromise)
|
|
1968
|
+
return this.flushPromise;
|
|
1969
|
+
if (this.pending.length === 0)
|
|
1970
|
+
return Promise.resolve();
|
|
1971
|
+
this.clearFlushTimer();
|
|
1972
|
+
this.flushRequested = false;
|
|
1973
|
+
const batch = this.pending.splice(0, DURABLE_PUBLISHED_HASH_BATCH_MAX_ENTRIES);
|
|
1974
|
+
return this.startFlush(batch);
|
|
1975
|
+
}
|
|
1976
|
+
startFlush(batch, retry) {
|
|
1977
|
+
const flush = this.flushBatch(batch, retry);
|
|
1978
|
+
const tracked = flush.finally(() => {
|
|
1979
|
+
if (this.flushPromise !== tracked)
|
|
1980
|
+
return;
|
|
1981
|
+
this.flushPromise = undefined;
|
|
1982
|
+
if (this.pending.length === 0)
|
|
1983
|
+
return;
|
|
1984
|
+
if (this.closed || this.flushRequested || this.pending.length >= DURABLE_PUBLISHED_HASH_BATCH_MAX_ENTRIES) {
|
|
1985
|
+
void this.requestFlush().catch(() => undefined);
|
|
1986
|
+
}
|
|
1987
|
+
else {
|
|
1988
|
+
this.armFlushTimer();
|
|
1989
|
+
}
|
|
1990
|
+
});
|
|
1991
|
+
this.flushPromise = tracked;
|
|
1992
|
+
return tracked;
|
|
1993
|
+
}
|
|
1994
|
+
async flushBatch(batch, retry) {
|
|
1995
|
+
const included = [];
|
|
1996
|
+
let prepared = false;
|
|
1997
|
+
try {
|
|
1998
|
+
const appended = await this.store.appendGeneratedIfAsync(PUBLISHED_CONTENT_HASH_RECORD_BATCH, () => {
|
|
1999
|
+
prepared = true;
|
|
2000
|
+
const lastIncludedByPath = new Map();
|
|
2001
|
+
for (const entry of batch) {
|
|
2002
|
+
// This runs under append.lock, immediately before the mutation is
|
|
2003
|
+
// selected for the durable batch. Retain older candidates until
|
|
2004
|
+
// now: a stale later guard must not erase an earlier valid mark.
|
|
2005
|
+
if (entry.shouldPersist && !entry.shouldPersist()) {
|
|
2006
|
+
this.settle(entry, false);
|
|
2007
|
+
continue;
|
|
2008
|
+
}
|
|
2009
|
+
lastIncludedByPath.set(entry.mutation.relativePath, entry);
|
|
2010
|
+
}
|
|
2011
|
+
included.push(...lastIncludedByPath.values());
|
|
2012
|
+
included.sort((left, right) => left.sequence - right.sequence);
|
|
2013
|
+
for (const entry of batch) {
|
|
2014
|
+
if (!entry.settled && !included.includes(entry))
|
|
2015
|
+
this.settle(entry, false);
|
|
2016
|
+
}
|
|
2017
|
+
if (included.length === 0)
|
|
2018
|
+
return undefined;
|
|
2019
|
+
return {
|
|
2020
|
+
entries: included.map((entry) => entry.mutation),
|
|
2021
|
+
};
|
|
2022
|
+
});
|
|
2023
|
+
if (appended === undefined) {
|
|
2024
|
+
if (retry)
|
|
2025
|
+
this.batchRetries.delete(retry);
|
|
2026
|
+
return;
|
|
2027
|
+
}
|
|
2028
|
+
for (const entry of included)
|
|
2029
|
+
this.settle(entry, true);
|
|
2030
|
+
if (retry)
|
|
2031
|
+
this.batchRetries.delete(retry);
|
|
2032
|
+
}
|
|
2033
|
+
catch (error) {
|
|
2034
|
+
if (error instanceof StateStoreLockError) {
|
|
2035
|
+
const affected = prepared
|
|
2036
|
+
? included
|
|
2037
|
+
: retry
|
|
2038
|
+
? batch
|
|
2039
|
+
: batch.filter((entry) => !entry.settled);
|
|
2040
|
+
this.handleBatchLockFailure(error, affected, retry);
|
|
2041
|
+
return;
|
|
2042
|
+
}
|
|
2043
|
+
for (const entry of batch) {
|
|
2044
|
+
if (!entry.settled) {
|
|
2045
|
+
entry.settled = true;
|
|
2046
|
+
entry.reject(error);
|
|
2047
|
+
}
|
|
2048
|
+
}
|
|
2049
|
+
throw error;
|
|
2050
|
+
}
|
|
2051
|
+
}
|
|
2052
|
+
settle(entry, persisted) {
|
|
2053
|
+
if (entry.settled)
|
|
2054
|
+
return;
|
|
2055
|
+
entry.settled = true;
|
|
2056
|
+
entry.resolve(persisted);
|
|
2057
|
+
}
|
|
2058
|
+
handleBatchLockFailure(error, entries, existingRetry) {
|
|
2059
|
+
if (entries.length === 0) {
|
|
2060
|
+
if (existingRetry)
|
|
2061
|
+
this.batchRetries.delete(existingRetry);
|
|
2062
|
+
return;
|
|
2063
|
+
}
|
|
2064
|
+
for (const entry of entries)
|
|
2065
|
+
this.settle(entry, false);
|
|
2066
|
+
this.onBatchLockFailure(error, entries.length, entries.length === 1 ? entries[0]?.mutation.relativePath : undefined);
|
|
2067
|
+
if (this.closed) {
|
|
2068
|
+
if (existingRetry)
|
|
2069
|
+
this.batchRetries.delete(existingRetry);
|
|
2070
|
+
return;
|
|
2071
|
+
}
|
|
2072
|
+
const retry = existingRetry ?? { entries, attempts: 0 };
|
|
2073
|
+
if (existingRetry) {
|
|
2074
|
+
existingRetry.entries.splice(0, existingRetry.entries.length, ...entries);
|
|
2075
|
+
}
|
|
2076
|
+
else {
|
|
2077
|
+
this.batchRetries.add(retry);
|
|
2078
|
+
}
|
|
2079
|
+
retry.attempts += 1;
|
|
2080
|
+
const delay = Math.min(DURABLE_PUBLISHED_HASH_RETRY_INITIAL_MS * 2 ** Math.min(retry.attempts - 1, 6), DURABLE_PUBLISHED_HASH_RETRY_MAX_MS);
|
|
2081
|
+
retry.timer = this.clock.setTimeout(() => {
|
|
2082
|
+
retry.timer = undefined;
|
|
2083
|
+
void this.retryBatch(retry).catch(() => undefined);
|
|
2084
|
+
}, delay);
|
|
2085
|
+
}
|
|
2086
|
+
async retryBatch(retry) {
|
|
2087
|
+
if (this.closed || !this.batchRetries.has(retry))
|
|
2088
|
+
return;
|
|
2089
|
+
if (this.flushPromise)
|
|
2090
|
+
await this.flushPromise;
|
|
2091
|
+
if (this.closed || !this.batchRetries.has(retry))
|
|
2092
|
+
return;
|
|
2093
|
+
if (retry.entries.length === 0) {
|
|
2094
|
+
this.batchRetries.delete(retry);
|
|
2095
|
+
return;
|
|
2096
|
+
}
|
|
2097
|
+
await this.startFlush(retry.entries, retry);
|
|
2098
|
+
}
|
|
2099
|
+
supersedeBatchRetries(relativePath) {
|
|
2100
|
+
for (const retry of this.batchRetries) {
|
|
2101
|
+
for (let index = retry.entries.length - 1; index >= 0; index -= 1) {
|
|
2102
|
+
if (retry.entries[index]?.mutation.relativePath === relativePath) {
|
|
2103
|
+
retry.entries.splice(index, 1);
|
|
2104
|
+
}
|
|
2105
|
+
}
|
|
2106
|
+
if (retry.entries.length > 0)
|
|
2107
|
+
continue;
|
|
2108
|
+
if (retry.timer !== undefined)
|
|
2109
|
+
this.clock.clearTimeout(retry.timer);
|
|
2110
|
+
this.batchRetries.delete(retry);
|
|
2111
|
+
}
|
|
2112
|
+
}
|
|
2113
|
+
clearBatchRetries() {
|
|
2114
|
+
for (const retry of this.batchRetries) {
|
|
2115
|
+
if (retry.timer !== undefined)
|
|
2116
|
+
this.clock.clearTimeout(retry.timer);
|
|
2117
|
+
}
|
|
2118
|
+
this.batchRetries.clear();
|
|
1865
2119
|
}
|
|
1866
2120
|
}
|
|
1867
2121
|
function boundedDurablePublishedContentHashState(hashes, maxPaths) {
|
|
@@ -1873,58 +2127,31 @@ function boundedDurablePublishedContentHashState(hashes, maxPaths) {
|
|
|
1873
2127
|
}
|
|
1874
2128
|
function reduceDurablePublishedContentHashState(state, record) {
|
|
1875
2129
|
if (record.type === PUBLISHED_CONTENT_HASH_RECORD_SET) {
|
|
1876
|
-
|
|
1877
|
-
const maxPaths = payload.maxPaths;
|
|
1878
|
-
const hashes = { ...state.hashes, [payload.relativePath]: payload.contentHash };
|
|
1879
|
-
const order = state.order.filter((path) => path !== payload.relativePath);
|
|
1880
|
-
order.push(payload.relativePath);
|
|
1881
|
-
while (order.length > maxPaths) {
|
|
1882
|
-
const oldest = order.shift();
|
|
1883
|
-
if (oldest !== undefined)
|
|
1884
|
-
delete hashes[oldest];
|
|
1885
|
-
}
|
|
1886
|
-
return { hashes, order };
|
|
2130
|
+
return reduceDurablePublishedContentHashMutation(state, { kind: "set", ...durablePublishedContentHashSetRecord(record) });
|
|
1887
2131
|
}
|
|
1888
2132
|
if (record.type === PUBLISHED_CONTENT_HASH_RECORD_DELETE) {
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
};
|
|
2133
|
+
return reduceDurablePublishedContentHashMutation(state, { kind: "delete", ...durablePublishedContentHashDeleteRecord(record) });
|
|
2134
|
+
}
|
|
2135
|
+
if (record.type === PUBLISHED_CONTENT_HASH_RECORD_BATCH) {
|
|
2136
|
+
let next = state;
|
|
2137
|
+
for (const mutation of durablePublishedContentHashBatchRecord(record).entries) {
|
|
2138
|
+
next = reduceDurablePublishedContentHashMutation(next, mutation);
|
|
2139
|
+
}
|
|
2140
|
+
return next;
|
|
1898
2141
|
}
|
|
1899
2142
|
throw new Error("published-content-hashes: unknown record type");
|
|
1900
2143
|
}
|
|
1901
2144
|
function reduceDurablePublishedContentHashStateInPlace(state, record) {
|
|
1902
2145
|
if (record.type === PUBLISHED_CONTENT_HASH_RECORD_SET) {
|
|
1903
|
-
|
|
1904
|
-
const order = durablePublishedContentHashOrder(state);
|
|
1905
|
-
order.delete(payload.relativePath);
|
|
1906
|
-
order.set(payload.relativePath, payload.contentHash);
|
|
1907
|
-
Object.defineProperty(state.hashes, payload.relativePath, {
|
|
1908
|
-
configurable: true,
|
|
1909
|
-
enumerable: true,
|
|
1910
|
-
value: payload.contentHash,
|
|
1911
|
-
writable: true,
|
|
1912
|
-
});
|
|
1913
|
-
while (order.size > payload.maxPaths) {
|
|
1914
|
-
const oldest = order.keys().next().value;
|
|
1915
|
-
if (oldest === undefined)
|
|
1916
|
-
break;
|
|
1917
|
-
order.delete(oldest);
|
|
1918
|
-
delete state.hashes[oldest];
|
|
1919
|
-
}
|
|
1920
|
-
return state;
|
|
2146
|
+
return reduceDurablePublishedContentHashMutationInPlace(state, { kind: "set", ...durablePublishedContentHashSetRecord(record) });
|
|
1921
2147
|
}
|
|
1922
2148
|
if (record.type === PUBLISHED_CONTENT_HASH_RECORD_DELETE) {
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
2149
|
+
return reduceDurablePublishedContentHashMutationInPlace(state, { kind: "delete", ...durablePublishedContentHashDeleteRecord(record) });
|
|
2150
|
+
}
|
|
2151
|
+
if (record.type === PUBLISHED_CONTENT_HASH_RECORD_BATCH) {
|
|
2152
|
+
for (const mutation of durablePublishedContentHashBatchRecord(record).entries) {
|
|
2153
|
+
reduceDurablePublishedContentHashMutationInPlace(state, mutation);
|
|
2154
|
+
}
|
|
1928
2155
|
return state;
|
|
1929
2156
|
}
|
|
1930
2157
|
throw new Error("published-content-hashes: unknown record type");
|
|
@@ -1938,8 +2165,59 @@ function validateDurablePublishedContentHashRecord(record) {
|
|
|
1938
2165
|
durablePublishedContentHashDeleteRecord(record);
|
|
1939
2166
|
return;
|
|
1940
2167
|
}
|
|
2168
|
+
if (record.type === PUBLISHED_CONTENT_HASH_RECORD_BATCH) {
|
|
2169
|
+
durablePublishedContentHashBatchRecord(record);
|
|
2170
|
+
return;
|
|
2171
|
+
}
|
|
1941
2172
|
throw new Error("published-content-hashes: unknown record type");
|
|
1942
2173
|
}
|
|
2174
|
+
function reduceDurablePublishedContentHashMutation(state, mutation) {
|
|
2175
|
+
if (mutation.kind === "delete") {
|
|
2176
|
+
if (!(mutation.relativePath in state.hashes))
|
|
2177
|
+
return state;
|
|
2178
|
+
const hashes = { ...state.hashes };
|
|
2179
|
+
delete hashes[mutation.relativePath];
|
|
2180
|
+
return {
|
|
2181
|
+
hashes,
|
|
2182
|
+
order: state.order.filter((path) => path !== mutation.relativePath),
|
|
2183
|
+
};
|
|
2184
|
+
}
|
|
2185
|
+
const hashes = { ...state.hashes, [mutation.relativePath]: mutation.contentHash };
|
|
2186
|
+
const order = state.order.filter((path) => path !== mutation.relativePath);
|
|
2187
|
+
order.push(mutation.relativePath);
|
|
2188
|
+
while (order.length > mutation.maxPaths) {
|
|
2189
|
+
const oldest = order.shift();
|
|
2190
|
+
if (oldest !== undefined)
|
|
2191
|
+
delete hashes[oldest];
|
|
2192
|
+
}
|
|
2193
|
+
return { hashes, order };
|
|
2194
|
+
}
|
|
2195
|
+
function reduceDurablePublishedContentHashMutationInPlace(state, mutation) {
|
|
2196
|
+
if (mutation.kind === "delete") {
|
|
2197
|
+
if (!Object.hasOwn(state.hashes, mutation.relativePath))
|
|
2198
|
+
return state;
|
|
2199
|
+
durablePublishedContentHashOrder(state).delete(mutation.relativePath);
|
|
2200
|
+
delete state.hashes[mutation.relativePath];
|
|
2201
|
+
return state;
|
|
2202
|
+
}
|
|
2203
|
+
const order = durablePublishedContentHashOrder(state);
|
|
2204
|
+
order.delete(mutation.relativePath);
|
|
2205
|
+
order.set(mutation.relativePath, mutation.contentHash);
|
|
2206
|
+
Object.defineProperty(state.hashes, mutation.relativePath, {
|
|
2207
|
+
configurable: true,
|
|
2208
|
+
enumerable: true,
|
|
2209
|
+
value: mutation.contentHash,
|
|
2210
|
+
writable: true,
|
|
2211
|
+
});
|
|
2212
|
+
while (order.size > mutation.maxPaths) {
|
|
2213
|
+
const oldest = order.keys().next().value;
|
|
2214
|
+
if (oldest === undefined)
|
|
2215
|
+
break;
|
|
2216
|
+
order.delete(oldest);
|
|
2217
|
+
delete state.hashes[oldest];
|
|
2218
|
+
}
|
|
2219
|
+
return state;
|
|
2220
|
+
}
|
|
1943
2221
|
function durablePublishedContentHashSetRecord(record) {
|
|
1944
2222
|
const payload = record.payload;
|
|
1945
2223
|
if (typeof payload.relativePath !== "string" ||
|
|
@@ -1960,6 +2238,33 @@ function durablePublishedContentHashDeleteRecord(record) {
|
|
|
1960
2238
|
}
|
|
1961
2239
|
return { relativePath: payload.relativePath };
|
|
1962
2240
|
}
|
|
2241
|
+
function durablePublishedContentHashBatchRecord(record) {
|
|
2242
|
+
const payload = record.payload;
|
|
2243
|
+
if (!Array.isArray(payload.entries) || payload.entries.length === 0) {
|
|
2244
|
+
throw new Error("published-content-hashes: invalid batch record");
|
|
2245
|
+
}
|
|
2246
|
+
return {
|
|
2247
|
+
entries: payload.entries.map((entry) => {
|
|
2248
|
+
if (typeof entry !== "object" || entry === null) {
|
|
2249
|
+
throw new Error("published-content-hashes: invalid batch record");
|
|
2250
|
+
}
|
|
2251
|
+
const mutation = entry;
|
|
2252
|
+
if (mutation.kind === "set") {
|
|
2253
|
+
return {
|
|
2254
|
+
kind: "set",
|
|
2255
|
+
...durablePublishedContentHashSetRecord({ ...record, payload: entry }),
|
|
2256
|
+
};
|
|
2257
|
+
}
|
|
2258
|
+
if (mutation.kind === "delete") {
|
|
2259
|
+
return {
|
|
2260
|
+
kind: "delete",
|
|
2261
|
+
...durablePublishedContentHashDeleteRecord({ ...record, payload: entry }),
|
|
2262
|
+
};
|
|
2263
|
+
}
|
|
2264
|
+
throw new Error("published-content-hashes: invalid batch record");
|
|
2265
|
+
}),
|
|
2266
|
+
};
|
|
2267
|
+
}
|
|
1963
2268
|
function durablePublishedContentHashOrder(state) {
|
|
1964
2269
|
const existing = durablePublishedContentHashOrders.get(state);
|
|
1965
2270
|
if (existing)
|
|
@@ -2182,8 +2487,8 @@ export class PushEventEmitter {
|
|
|
2182
2487
|
*/
|
|
2183
2488
|
deferredPublishes = new Map();
|
|
2184
2489
|
deferredPublishTimers = new Map();
|
|
2185
|
-
durablePublishedHashRetries = new Map();
|
|
2186
2490
|
disposed = false;
|
|
2491
|
+
disposePromise = null;
|
|
2187
2492
|
_realtimeUnavailable = false;
|
|
2188
2493
|
internalSeq = 0;
|
|
2189
2494
|
nextSeq;
|
|
@@ -2283,7 +2588,7 @@ export class PushEventEmitter {
|
|
|
2283
2588
|
// A pulled revision was never locally announced, so it must not begin the
|
|
2284
2589
|
// per-path announcement quiet period for a subsequent local edit.
|
|
2285
2590
|
this.lastPublishedContentHashes.set(relativePath, contentHash);
|
|
2286
|
-
void this.setDurablePublishedContentHash(relativePath, contentHash);
|
|
2591
|
+
void this.setDurablePublishedContentHash(relativePath, contentHash, undefined, false);
|
|
2287
2592
|
}
|
|
2288
2593
|
async emitEntries(entries) {
|
|
2289
2594
|
const attempts = [];
|
|
@@ -2308,9 +2613,18 @@ export class PushEventEmitter {
|
|
|
2308
2613
|
}
|
|
2309
2614
|
async emitOne(absolutePath, relativePath) {
|
|
2310
2615
|
// TreeChangeBatch has multiple producers. Treat its relative path as
|
|
2311
|
-
// untrusted until this wire boundary
|
|
2312
|
-
//
|
|
2313
|
-
|
|
2616
|
+
// untrusted until this wire boundary. Rewrite only the native separator
|
|
2617
|
+
// (`path.sep`): on win32 that is `\\` and converting it is always safe.
|
|
2618
|
+
// On POSIX `\\` is a legal filename byte — unconditional toPosixKey
|
|
2619
|
+
// would remap that file onto a different vault path. Leftover `\\`
|
|
2620
|
+
// segments fail PushEventSchema (`backslash_segment`) instead.
|
|
2621
|
+
relativePath = nativeSeparatorsToPosix(relativePath, path.sep);
|
|
2622
|
+
const rejection = relativePathRejectionReason(relativePath);
|
|
2623
|
+
if (rejection) {
|
|
2624
|
+
// Closed-vocabulary only — do not put the path on the error or ctx.
|
|
2625
|
+
this.onError(new Error(`push relativePath rejected: ${rejection}`), {});
|
|
2626
|
+
return undefined;
|
|
2627
|
+
}
|
|
2314
2628
|
let event;
|
|
2315
2629
|
try {
|
|
2316
2630
|
const st = await stat(absolutePath);
|
|
@@ -2452,7 +2766,10 @@ export class PushEventEmitter {
|
|
|
2452
2766
|
}
|
|
2453
2767
|
openDurablePublishedContentHashes(stateDir, initialHashes) {
|
|
2454
2768
|
try {
|
|
2455
|
-
return new DurablePublishedContentHashes(stateDir, `${this.originTenantId}:${this.originDeviceId}`, this.lastPublishedContentHashMaxPaths, initialHashes)
|
|
2769
|
+
return new DurablePublishedContentHashes(stateDir, `${this.originTenantId}:${this.originDeviceId}`, this.lastPublishedContentHashMaxPaths, initialHashes, this.clock, (error, mutationCount, relativePath) => {
|
|
2770
|
+
error.message += `; durable published-content-hash batch affected ${mutationCount} mutation${mutationCount === 1 ? "" : "s"}`;
|
|
2771
|
+
this.onError(error, relativePath === undefined ? {} : { relativePath });
|
|
2772
|
+
});
|
|
2456
2773
|
}
|
|
2457
2774
|
catch (err) {
|
|
2458
2775
|
this.onError(err instanceof Error ? err : new Error(String(err)), {});
|
|
@@ -2483,30 +2800,32 @@ export class PushEventEmitter {
|
|
|
2483
2800
|
return false;
|
|
2484
2801
|
}
|
|
2485
2802
|
}
|
|
2486
|
-
async setDurablePublishedContentHash(relativePath, contentHash, generation) {
|
|
2487
|
-
const operation = { kind: "set", contentHash, generation };
|
|
2803
|
+
async setDurablePublishedContentHash(relativePath, contentHash, generation, flushImmediately = true) {
|
|
2488
2804
|
try {
|
|
2489
|
-
|
|
2490
|
-
|
|
2491
|
-
|
|
2492
|
-
|
|
2493
|
-
|
|
2805
|
+
if (!this.durablePublishedContentHashes)
|
|
2806
|
+
return;
|
|
2807
|
+
if (generation === undefined) {
|
|
2808
|
+
await this.durablePublishedContentHashes.set(relativePath, contentHash, this.lastPublishedContentHashMaxPaths, flushImmediately);
|
|
2809
|
+
return;
|
|
2810
|
+
}
|
|
2811
|
+
await this.durablePublishedContentHashes.setIfCurrent(relativePath, contentHash, this.lastPublishedContentHashMaxPaths, () => this.isPublishedContentHashGenerationCurrent(relativePath, generation));
|
|
2494
2812
|
}
|
|
2495
2813
|
catch (err) {
|
|
2496
|
-
this.
|
|
2814
|
+
this.onError(err instanceof Error ? err : new Error(String(err)), { relativePath });
|
|
2497
2815
|
}
|
|
2498
2816
|
}
|
|
2499
|
-
async deleteDurablePublishedContentHash(relativePath, generation) {
|
|
2500
|
-
const operation = { kind: "delete", generation };
|
|
2817
|
+
async deleteDurablePublishedContentHash(relativePath, generation, flushImmediately = true) {
|
|
2501
2818
|
try {
|
|
2502
|
-
|
|
2503
|
-
|
|
2504
|
-
|
|
2505
|
-
|
|
2506
|
-
|
|
2819
|
+
if (!this.durablePublishedContentHashes)
|
|
2820
|
+
return;
|
|
2821
|
+
if (generation === undefined) {
|
|
2822
|
+
await this.durablePublishedContentHashes.delete(relativePath, flushImmediately);
|
|
2823
|
+
return;
|
|
2824
|
+
}
|
|
2825
|
+
await this.durablePublishedContentHashes.deleteIfCurrent(relativePath, () => this.isPublishedContentHashGenerationCurrent(relativePath, generation));
|
|
2507
2826
|
}
|
|
2508
2827
|
catch (err) {
|
|
2509
|
-
this.
|
|
2828
|
+
this.onError(err instanceof Error ? err : new Error(String(err)), { relativePath });
|
|
2510
2829
|
}
|
|
2511
2830
|
}
|
|
2512
2831
|
/**
|
|
@@ -2530,79 +2849,6 @@ export class PushEventEmitter {
|
|
|
2530
2849
|
return true;
|
|
2531
2850
|
return this.publishedContentHashUnchangedSince(relativePath, generationCounterAtStart);
|
|
2532
2851
|
}
|
|
2533
|
-
async persistDurablePublishedContentHash(relativePath, operation, shouldPersist) {
|
|
2534
|
-
if (!this.durablePublishedContentHashes)
|
|
2535
|
-
return true;
|
|
2536
|
-
if (operation.kind === "set") {
|
|
2537
|
-
if (shouldPersist) {
|
|
2538
|
-
return this.durablePublishedContentHashes.setIfCurrent(relativePath, operation.contentHash, this.lastPublishedContentHashMaxPaths, shouldPersist);
|
|
2539
|
-
}
|
|
2540
|
-
await this.durablePublishedContentHashes.set(relativePath, operation.contentHash, this.lastPublishedContentHashMaxPaths);
|
|
2541
|
-
}
|
|
2542
|
-
else {
|
|
2543
|
-
if (shouldPersist) {
|
|
2544
|
-
return this.durablePublishedContentHashes.deleteIfCurrent(relativePath, shouldPersist);
|
|
2545
|
-
}
|
|
2546
|
-
await this.durablePublishedContentHashes.delete(relativePath);
|
|
2547
|
-
}
|
|
2548
|
-
return true;
|
|
2549
|
-
}
|
|
2550
|
-
handleDurablePublishedContentHashFailure(relativePath, operation, err) {
|
|
2551
|
-
const error = err instanceof Error ? err : new Error(String(err));
|
|
2552
|
-
this.onError(error, { relativePath });
|
|
2553
|
-
if (error instanceof StateStoreLockError) {
|
|
2554
|
-
this.scheduleDurablePublishedContentHashRetry(relativePath, operation);
|
|
2555
|
-
}
|
|
2556
|
-
}
|
|
2557
|
-
clearDurablePublishedContentHashRetry(relativePath) {
|
|
2558
|
-
const retry = this.durablePublishedHashRetries.get(relativePath);
|
|
2559
|
-
if (!retry)
|
|
2560
|
-
return;
|
|
2561
|
-
if (retry.timer !== undefined)
|
|
2562
|
-
this.clock.clearTimeout(retry.timer);
|
|
2563
|
-
this.durablePublishedHashRetries.delete(relativePath);
|
|
2564
|
-
}
|
|
2565
|
-
scheduleDurablePublishedContentHashRetry(relativePath, operation) {
|
|
2566
|
-
if (this.disposed || !this.durablePublishedContentHashes)
|
|
2567
|
-
return;
|
|
2568
|
-
const existing = this.durablePublishedHashRetries.get(relativePath);
|
|
2569
|
-
if (existing?.timer !== undefined) {
|
|
2570
|
-
existing.operation = operation;
|
|
2571
|
-
return;
|
|
2572
|
-
}
|
|
2573
|
-
const retry = {
|
|
2574
|
-
operation,
|
|
2575
|
-
attempts: (existing?.attempts ?? 0) + 1,
|
|
2576
|
-
};
|
|
2577
|
-
const delay = Math.min(DURABLE_PUBLISHED_HASH_RETRY_INITIAL_MS * 2 ** Math.min(retry.attempts - 1, 6), DURABLE_PUBLISHED_HASH_RETRY_MAX_MS);
|
|
2578
|
-
retry.timer = this.clock.setTimeout(() => {
|
|
2579
|
-
retry.timer = undefined;
|
|
2580
|
-
void this.retryDurablePublishedContentHash(relativePath);
|
|
2581
|
-
}, delay);
|
|
2582
|
-
this.durablePublishedHashRetries.set(relativePath, retry);
|
|
2583
|
-
}
|
|
2584
|
-
async retryDurablePublishedContentHash(relativePath) {
|
|
2585
|
-
if (this.disposed)
|
|
2586
|
-
return;
|
|
2587
|
-
const retry = this.durablePublishedHashRetries.get(relativePath);
|
|
2588
|
-
if (!retry)
|
|
2589
|
-
return;
|
|
2590
|
-
try {
|
|
2591
|
-
const persisted = await this.persistDurablePublishedContentHash(relativePath, retry.operation, () => this.durablePublishedHashRetries.get(relativePath) === retry &&
|
|
2592
|
-
this.isPublishedContentHashGenerationCurrent(relativePath, retry.operation.generation));
|
|
2593
|
-
if (this.durablePublishedHashRetries.get(relativePath) === retry && !persisted) {
|
|
2594
|
-
this.durablePublishedHashRetries.delete(relativePath);
|
|
2595
|
-
}
|
|
2596
|
-
else if (persisted && this.durablePublishedHashRetries.get(relativePath) === retry) {
|
|
2597
|
-
this.durablePublishedHashRetries.delete(relativePath);
|
|
2598
|
-
}
|
|
2599
|
-
}
|
|
2600
|
-
catch (err) {
|
|
2601
|
-
if (this.durablePublishedHashRetries.get(relativePath) === retry) {
|
|
2602
|
-
this.handleDurablePublishedContentHashFailure(relativePath, retry.operation, err);
|
|
2603
|
-
}
|
|
2604
|
-
}
|
|
2605
|
-
}
|
|
2606
2852
|
/** Remember one latest-wins uploaded revision until its publish interval ends. */
|
|
2607
2853
|
deferUpsert(event, lastPublishedAt) {
|
|
2608
2854
|
const relativePath = event.relativePath;
|
|
@@ -2643,21 +2889,46 @@ export class PushEventEmitter {
|
|
|
2643
2889
|
this.clock.clearTimeout(timer);
|
|
2644
2890
|
this.deferredPublishTimers.delete(relativePath);
|
|
2645
2891
|
}
|
|
2646
|
-
/**
|
|
2892
|
+
/** Flush durable publication state, cancel trailing publishes, and become inert. */
|
|
2647
2893
|
dispose() {
|
|
2648
|
-
if (this.
|
|
2649
|
-
return;
|
|
2894
|
+
if (this.disposePromise !== null)
|
|
2895
|
+
return this.disposePromise;
|
|
2650
2896
|
this.disposed = true;
|
|
2651
|
-
|
|
2652
|
-
this.
|
|
2897
|
+
this.disposePromise = (async () => {
|
|
2898
|
+
for (const timer of this.deferredPublishTimers.values()) {
|
|
2899
|
+
this.clock.clearTimeout(timer);
|
|
2900
|
+
}
|
|
2901
|
+
this.deferredPublishTimers.clear();
|
|
2902
|
+
this.deferredPublishes.clear();
|
|
2903
|
+
if (await this.waitForPublishDrain()) {
|
|
2904
|
+
await this.disposeDurablePublishedContentHashes();
|
|
2905
|
+
return;
|
|
2906
|
+
}
|
|
2907
|
+
this.onError(new Error(`PushEventEmitter: timed out draining active publish after ${PUSH_EVENT_EMITTER_DISPOSE_DRAIN_MS}ms; durable memo remains open until it settles`), {});
|
|
2908
|
+
void this.publishTail.then(() => this.disposeDurablePublishedContentHashes());
|
|
2909
|
+
})();
|
|
2910
|
+
return this.disposePromise;
|
|
2911
|
+
}
|
|
2912
|
+
async waitForPublishDrain() {
|
|
2913
|
+
let timeout;
|
|
2914
|
+
const deadline = new Promise((resolve) => {
|
|
2915
|
+
timeout = this.clock.setTimeout(() => resolve(false), PUSH_EVENT_EMITTER_DISPOSE_DRAIN_MS);
|
|
2916
|
+
});
|
|
2917
|
+
const drained = await Promise.race([
|
|
2918
|
+
this.publishTail.then(() => true, () => true),
|
|
2919
|
+
deadline,
|
|
2920
|
+
]);
|
|
2921
|
+
if (timeout !== undefined)
|
|
2922
|
+
this.clock.clearTimeout(timeout);
|
|
2923
|
+
return drained;
|
|
2924
|
+
}
|
|
2925
|
+
async disposeDurablePublishedContentHashes() {
|
|
2926
|
+
try {
|
|
2927
|
+
await this.durablePublishedContentHashes?.dispose();
|
|
2653
2928
|
}
|
|
2654
|
-
|
|
2655
|
-
|
|
2656
|
-
for (const retry of this.durablePublishedHashRetries.values()) {
|
|
2657
|
-
if (retry.timer !== undefined)
|
|
2658
|
-
this.clock.clearTimeout(retry.timer);
|
|
2929
|
+
catch (err) {
|
|
2930
|
+
this.onError(err instanceof Error ? err : new Error(String(err)), {});
|
|
2659
2931
|
}
|
|
2660
|
-
this.durablePublishedHashRetries.clear();
|
|
2661
2932
|
// Release the per-path maps so a disposed emitter retains nothing.
|
|
2662
2933
|
this.publishedContentHashGenerations.clear();
|
|
2663
2934
|
}
|
|
@@ -2670,7 +2941,7 @@ export class PushEventEmitter {
|
|
|
2670
2941
|
lastPublishedContentHashes: this.lastPublishedContentHashes.size,
|
|
2671
2942
|
publishedContentHashGenerations: this.publishedContentHashGenerations.size,
|
|
2672
2943
|
deferredPublishes: this.deferredPublishes.size,
|
|
2673
|
-
durablePublishedHashRetries: this.
|
|
2944
|
+
durablePublishedHashRetries: this.durablePublishedContentHashes?.pendingRetryCount ?? 0,
|
|
2674
2945
|
// Runner callers no longer pass the session-wide object. Keep a fixed
|
|
2675
2946
|
// zero-valued token so heap telemetry explicitly records its absence.
|
|
2676
2947
|
initialPublishedContentHashes: 0,
|