@indigoai-us/hq-cloud 6.16.5 → 6.16.6

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/watcher.js CHANGED
@@ -1812,8 +1812,16 @@ export function resolvePublishMinIntervalMs(env = process.env) {
1812
1812
  }
1813
1813
  const PUBLISHED_CONTENT_HASH_RECORD_SET = 0x5031;
1814
1814
  const PUBLISHED_CONTENT_HASH_RECORD_DELETE = 0x5032;
1815
+ const PUBLISHED_CONTENT_HASH_RECORD_BATCH = 0x5033;
1816
+ /** Limit one WAL frame to a modest size while collapsing a reconcile pass. */
1817
+ export const DURABLE_PUBLISHED_HASH_BATCH_MAX_ENTRIES = 4_096;
1818
+ /** A crash can lose at most this small durable-memo window under light load. */
1819
+ export const DURABLE_PUBLISHED_HASH_BATCH_FLUSH_MS = 25;
1815
1820
  const DURABLE_PUBLISHED_HASH_RETRY_INITIAL_MS = 250;
1816
1821
  const DURABLE_PUBLISHED_HASH_RETRY_MAX_MS = 10_000;
1822
+ // Match HttpPushTransport's existing per-request abort deadline. If a custom
1823
+ // transport ignores it, disposal returns without closing the durable memo.
1824
+ const PUSH_EVENT_EMITTER_DISPOSE_DRAIN_MS = 60_000;
1817
1825
  /**
1818
1826
  * JSON snapshots deliberately retain the long-lived `{ hashes, order }` wire
1819
1827
  * shape. At runtime, keep the order as a Map so replay and live writes can
@@ -1828,9 +1836,21 @@ const durablePublishedContentHashOrders = new WeakMap();
1828
1836
  * journal row proves the object upload completed, while the event publish that
1829
1837
  * follows can still fail or time out.
1830
1838
  */
1831
- class DurablePublishedContentHashes {
1839
+ export class DurablePublishedContentHashes {
1832
1840
  store;
1833
- constructor(stateDir, scopeId, maxPaths, initialHashes) {
1841
+ clock;
1842
+ onBatchLockFailure;
1843
+ pending = [];
1844
+ batchRetries = new Set();
1845
+ flushTimer;
1846
+ flushPromise;
1847
+ flushRequested = false;
1848
+ microtaskFlushScheduled = false;
1849
+ closed = false;
1850
+ nextSequence = 0;
1851
+ constructor(stateDir, scopeId, maxPaths, initialHashes, clock, onBatchLockFailure = () => undefined) {
1852
+ this.clock = clock;
1853
+ this.onBatchLockFailure = onBatchLockFailure;
1834
1854
  this.store = StateStore.open({
1835
1855
  rootDir: stateDir,
1836
1856
  scopeId: `event-push-published-content:${scopeId}`,
@@ -1843,25 +1863,258 @@ class DurablePublishedContentHashes {
1843
1863
  get(relativePath) {
1844
1864
  return durablePublishedContentHashValue(this.store.getState().hashes, relativePath);
1845
1865
  }
1846
- async set(relativePath, contentHash, maxPaths) {
1847
- await this.store.appendAsync(PUBLISHED_CONTENT_HASH_RECORD_SET, {
1866
+ get pendingRetryCount() {
1867
+ return this.batchRetries.size;
1868
+ }
1869
+ async set(relativePath, contentHash, maxPaths, flushImmediately) {
1870
+ await this.enqueue({
1871
+ kind: "set",
1848
1872
  relativePath,
1849
1873
  contentHash,
1850
1874
  maxPaths,
1851
- });
1875
+ }, undefined, flushImmediately);
1852
1876
  }
1853
1877
  async setIfCurrent(relativePath, contentHash, maxPaths, shouldPersist) {
1854
- return (await this.store.appendIfAsync(PUBLISHED_CONTENT_HASH_RECORD_SET, {
1878
+ return this.enqueue({
1879
+ kind: "set",
1855
1880
  relativePath,
1856
1881
  contentHash,
1857
1882
  maxPaths,
1858
- }, shouldPersist)) !== undefined;
1883
+ }, shouldPersist, true);
1859
1884
  }
1860
- async delete(relativePath) {
1861
- await this.store.appendAsync(PUBLISHED_CONTENT_HASH_RECORD_DELETE, { relativePath });
1885
+ async delete(relativePath, flushImmediately) {
1886
+ await this.enqueue({ kind: "delete", relativePath }, undefined, flushImmediately);
1862
1887
  }
1863
1888
  async deleteIfCurrent(relativePath, shouldPersist) {
1864
- return (await this.store.appendIfAsync(PUBLISHED_CONTENT_HASH_RECORD_DELETE, { relativePath }, shouldPersist)) !== undefined;
1889
+ return this.enqueue({ kind: "delete", relativePath }, shouldPersist, true);
1890
+ }
1891
+ async dispose() {
1892
+ this.closed = true;
1893
+ this.clearFlushTimer();
1894
+ this.clearBatchRetries();
1895
+ let failure;
1896
+ while (this.flushPromise || this.pending.length > 0) {
1897
+ try {
1898
+ if (this.flushPromise) {
1899
+ await this.flushPromise;
1900
+ }
1901
+ else {
1902
+ await this.requestFlush();
1903
+ }
1904
+ }
1905
+ catch (error) {
1906
+ failure ??= error;
1907
+ }
1908
+ }
1909
+ if (failure !== undefined)
1910
+ throw failure;
1911
+ }
1912
+ enqueue(mutation, shouldPersist, flushImmediately) {
1913
+ if (this.closed)
1914
+ return Promise.resolve(false);
1915
+ this.supersedeBatchRetries(mutation.relativePath);
1916
+ let resolve;
1917
+ let reject;
1918
+ const result = new Promise((resolveResult, rejectResult) => {
1919
+ resolve = resolveResult;
1920
+ reject = rejectResult;
1921
+ });
1922
+ this.pending.push({
1923
+ mutation,
1924
+ sequence: ++this.nextSequence,
1925
+ shouldPersist,
1926
+ resolve,
1927
+ reject,
1928
+ settled: false,
1929
+ });
1930
+ if (flushImmediately) {
1931
+ this.requestMicrotaskFlush();
1932
+ }
1933
+ else if (this.pending.length >= DURABLE_PUBLISHED_HASH_BATCH_MAX_ENTRIES) {
1934
+ void this.requestFlush().catch(() => undefined);
1935
+ }
1936
+ else {
1937
+ this.armFlushTimer();
1938
+ }
1939
+ return result;
1940
+ }
1941
+ requestMicrotaskFlush() {
1942
+ if (this.microtaskFlushScheduled)
1943
+ return;
1944
+ this.microtaskFlushScheduled = true;
1945
+ queueMicrotask(() => {
1946
+ this.microtaskFlushScheduled = false;
1947
+ void this.requestFlush().catch(() => undefined);
1948
+ });
1949
+ }
1950
+ armFlushTimer() {
1951
+ if (this.flushTimer !== undefined || this.closed)
1952
+ return;
1953
+ this.flushTimer = this.clock.setTimeout(() => {
1954
+ this.flushTimer = undefined;
1955
+ void this.requestFlush().catch(() => undefined);
1956
+ }, DURABLE_PUBLISHED_HASH_BATCH_FLUSH_MS);
1957
+ }
1958
+ clearFlushTimer() {
1959
+ if (this.flushTimer === undefined)
1960
+ return;
1961
+ this.clock.clearTimeout(this.flushTimer);
1962
+ this.flushTimer = undefined;
1963
+ }
1964
+ requestFlush() {
1965
+ this.flushRequested = true;
1966
+ if (this.flushPromise)
1967
+ return this.flushPromise;
1968
+ if (this.pending.length === 0)
1969
+ return Promise.resolve();
1970
+ this.clearFlushTimer();
1971
+ this.flushRequested = false;
1972
+ const batch = this.pending.splice(0, DURABLE_PUBLISHED_HASH_BATCH_MAX_ENTRIES);
1973
+ return this.startFlush(batch);
1974
+ }
1975
+ startFlush(batch, retry) {
1976
+ const flush = this.flushBatch(batch, retry);
1977
+ const tracked = flush.finally(() => {
1978
+ if (this.flushPromise !== tracked)
1979
+ return;
1980
+ this.flushPromise = undefined;
1981
+ if (this.pending.length === 0)
1982
+ return;
1983
+ if (this.closed || this.flushRequested || this.pending.length >= DURABLE_PUBLISHED_HASH_BATCH_MAX_ENTRIES) {
1984
+ void this.requestFlush().catch(() => undefined);
1985
+ }
1986
+ else {
1987
+ this.armFlushTimer();
1988
+ }
1989
+ });
1990
+ this.flushPromise = tracked;
1991
+ return tracked;
1992
+ }
1993
+ async flushBatch(batch, retry) {
1994
+ const included = [];
1995
+ let prepared = false;
1996
+ try {
1997
+ const appended = await this.store.appendGeneratedIfAsync(PUBLISHED_CONTENT_HASH_RECORD_BATCH, () => {
1998
+ prepared = true;
1999
+ const lastIncludedByPath = new Map();
2000
+ for (const entry of batch) {
2001
+ // This runs under append.lock, immediately before the mutation is
2002
+ // selected for the durable batch. Retain older candidates until
2003
+ // now: a stale later guard must not erase an earlier valid mark.
2004
+ if (entry.shouldPersist && !entry.shouldPersist()) {
2005
+ this.settle(entry, false);
2006
+ continue;
2007
+ }
2008
+ lastIncludedByPath.set(entry.mutation.relativePath, entry);
2009
+ }
2010
+ included.push(...lastIncludedByPath.values());
2011
+ included.sort((left, right) => left.sequence - right.sequence);
2012
+ for (const entry of batch) {
2013
+ if (!entry.settled && !included.includes(entry))
2014
+ this.settle(entry, false);
2015
+ }
2016
+ if (included.length === 0)
2017
+ return undefined;
2018
+ return {
2019
+ entries: included.map((entry) => entry.mutation),
2020
+ };
2021
+ });
2022
+ if (appended === undefined) {
2023
+ if (retry)
2024
+ this.batchRetries.delete(retry);
2025
+ return;
2026
+ }
2027
+ for (const entry of included)
2028
+ this.settle(entry, true);
2029
+ if (retry)
2030
+ this.batchRetries.delete(retry);
2031
+ }
2032
+ catch (error) {
2033
+ if (error instanceof StateStoreLockError) {
2034
+ const affected = prepared
2035
+ ? included
2036
+ : retry
2037
+ ? batch
2038
+ : batch.filter((entry) => !entry.settled);
2039
+ this.handleBatchLockFailure(error, affected, retry);
2040
+ return;
2041
+ }
2042
+ for (const entry of batch) {
2043
+ if (!entry.settled) {
2044
+ entry.settled = true;
2045
+ entry.reject(error);
2046
+ }
2047
+ }
2048
+ throw error;
2049
+ }
2050
+ }
2051
+ settle(entry, persisted) {
2052
+ if (entry.settled)
2053
+ return;
2054
+ entry.settled = true;
2055
+ entry.resolve(persisted);
2056
+ }
2057
+ handleBatchLockFailure(error, entries, existingRetry) {
2058
+ if (entries.length === 0) {
2059
+ if (existingRetry)
2060
+ this.batchRetries.delete(existingRetry);
2061
+ return;
2062
+ }
2063
+ for (const entry of entries)
2064
+ this.settle(entry, false);
2065
+ this.onBatchLockFailure(error, entries.length, entries.length === 1 ? entries[0]?.mutation.relativePath : undefined);
2066
+ if (this.closed) {
2067
+ if (existingRetry)
2068
+ this.batchRetries.delete(existingRetry);
2069
+ return;
2070
+ }
2071
+ const retry = existingRetry ?? { entries, attempts: 0 };
2072
+ if (existingRetry) {
2073
+ existingRetry.entries.splice(0, existingRetry.entries.length, ...entries);
2074
+ }
2075
+ else {
2076
+ this.batchRetries.add(retry);
2077
+ }
2078
+ retry.attempts += 1;
2079
+ const delay = Math.min(DURABLE_PUBLISHED_HASH_RETRY_INITIAL_MS * 2 ** Math.min(retry.attempts - 1, 6), DURABLE_PUBLISHED_HASH_RETRY_MAX_MS);
2080
+ retry.timer = this.clock.setTimeout(() => {
2081
+ retry.timer = undefined;
2082
+ void this.retryBatch(retry).catch(() => undefined);
2083
+ }, delay);
2084
+ }
2085
+ async retryBatch(retry) {
2086
+ if (this.closed || !this.batchRetries.has(retry))
2087
+ return;
2088
+ if (this.flushPromise)
2089
+ await this.flushPromise;
2090
+ if (this.closed || !this.batchRetries.has(retry))
2091
+ return;
2092
+ if (retry.entries.length === 0) {
2093
+ this.batchRetries.delete(retry);
2094
+ return;
2095
+ }
2096
+ await this.startFlush(retry.entries, retry);
2097
+ }
2098
+ supersedeBatchRetries(relativePath) {
2099
+ for (const retry of this.batchRetries) {
2100
+ for (let index = retry.entries.length - 1; index >= 0; index -= 1) {
2101
+ if (retry.entries[index]?.mutation.relativePath === relativePath) {
2102
+ retry.entries.splice(index, 1);
2103
+ }
2104
+ }
2105
+ if (retry.entries.length > 0)
2106
+ continue;
2107
+ if (retry.timer !== undefined)
2108
+ this.clock.clearTimeout(retry.timer);
2109
+ this.batchRetries.delete(retry);
2110
+ }
2111
+ }
2112
+ clearBatchRetries() {
2113
+ for (const retry of this.batchRetries) {
2114
+ if (retry.timer !== undefined)
2115
+ this.clock.clearTimeout(retry.timer);
2116
+ }
2117
+ this.batchRetries.clear();
1865
2118
  }
1866
2119
  }
1867
2120
  function boundedDurablePublishedContentHashState(hashes, maxPaths) {
@@ -1873,58 +2126,31 @@ function boundedDurablePublishedContentHashState(hashes, maxPaths) {
1873
2126
  }
1874
2127
  function reduceDurablePublishedContentHashState(state, record) {
1875
2128
  if (record.type === PUBLISHED_CONTENT_HASH_RECORD_SET) {
1876
- const payload = durablePublishedContentHashSetRecord(record);
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 };
2129
+ return reduceDurablePublishedContentHashMutation(state, { kind: "set", ...durablePublishedContentHashSetRecord(record) });
1887
2130
  }
1888
2131
  if (record.type === PUBLISHED_CONTENT_HASH_RECORD_DELETE) {
1889
- const payload = durablePublishedContentHashDeleteRecord(record);
1890
- if (!(payload.relativePath in state.hashes))
1891
- return state;
1892
- const hashes = { ...state.hashes };
1893
- delete hashes[payload.relativePath];
1894
- return {
1895
- hashes,
1896
- order: state.order.filter((path) => path !== payload.relativePath),
1897
- };
2132
+ return reduceDurablePublishedContentHashMutation(state, { kind: "delete", ...durablePublishedContentHashDeleteRecord(record) });
2133
+ }
2134
+ if (record.type === PUBLISHED_CONTENT_HASH_RECORD_BATCH) {
2135
+ let next = state;
2136
+ for (const mutation of durablePublishedContentHashBatchRecord(record).entries) {
2137
+ next = reduceDurablePublishedContentHashMutation(next, mutation);
2138
+ }
2139
+ return next;
1898
2140
  }
1899
2141
  throw new Error("published-content-hashes: unknown record type");
1900
2142
  }
1901
2143
  function reduceDurablePublishedContentHashStateInPlace(state, record) {
1902
2144
  if (record.type === PUBLISHED_CONTENT_HASH_RECORD_SET) {
1903
- const payload = durablePublishedContentHashSetRecord(record);
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;
2145
+ return reduceDurablePublishedContentHashMutationInPlace(state, { kind: "set", ...durablePublishedContentHashSetRecord(record) });
1921
2146
  }
1922
2147
  if (record.type === PUBLISHED_CONTENT_HASH_RECORD_DELETE) {
1923
- const payload = durablePublishedContentHashDeleteRecord(record);
1924
- if (!Object.hasOwn(state.hashes, payload.relativePath))
1925
- return state;
1926
- durablePublishedContentHashOrder(state).delete(payload.relativePath);
1927
- delete state.hashes[payload.relativePath];
2148
+ return reduceDurablePublishedContentHashMutationInPlace(state, { kind: "delete", ...durablePublishedContentHashDeleteRecord(record) });
2149
+ }
2150
+ if (record.type === PUBLISHED_CONTENT_HASH_RECORD_BATCH) {
2151
+ for (const mutation of durablePublishedContentHashBatchRecord(record).entries) {
2152
+ reduceDurablePublishedContentHashMutationInPlace(state, mutation);
2153
+ }
1928
2154
  return state;
1929
2155
  }
1930
2156
  throw new Error("published-content-hashes: unknown record type");
@@ -1938,8 +2164,59 @@ function validateDurablePublishedContentHashRecord(record) {
1938
2164
  durablePublishedContentHashDeleteRecord(record);
1939
2165
  return;
1940
2166
  }
2167
+ if (record.type === PUBLISHED_CONTENT_HASH_RECORD_BATCH) {
2168
+ durablePublishedContentHashBatchRecord(record);
2169
+ return;
2170
+ }
1941
2171
  throw new Error("published-content-hashes: unknown record type");
1942
2172
  }
2173
+ function reduceDurablePublishedContentHashMutation(state, mutation) {
2174
+ if (mutation.kind === "delete") {
2175
+ if (!(mutation.relativePath in state.hashes))
2176
+ return state;
2177
+ const hashes = { ...state.hashes };
2178
+ delete hashes[mutation.relativePath];
2179
+ return {
2180
+ hashes,
2181
+ order: state.order.filter((path) => path !== mutation.relativePath),
2182
+ };
2183
+ }
2184
+ const hashes = { ...state.hashes, [mutation.relativePath]: mutation.contentHash };
2185
+ const order = state.order.filter((path) => path !== mutation.relativePath);
2186
+ order.push(mutation.relativePath);
2187
+ while (order.length > mutation.maxPaths) {
2188
+ const oldest = order.shift();
2189
+ if (oldest !== undefined)
2190
+ delete hashes[oldest];
2191
+ }
2192
+ return { hashes, order };
2193
+ }
2194
+ function reduceDurablePublishedContentHashMutationInPlace(state, mutation) {
2195
+ if (mutation.kind === "delete") {
2196
+ if (!Object.hasOwn(state.hashes, mutation.relativePath))
2197
+ return state;
2198
+ durablePublishedContentHashOrder(state).delete(mutation.relativePath);
2199
+ delete state.hashes[mutation.relativePath];
2200
+ return state;
2201
+ }
2202
+ const order = durablePublishedContentHashOrder(state);
2203
+ order.delete(mutation.relativePath);
2204
+ order.set(mutation.relativePath, mutation.contentHash);
2205
+ Object.defineProperty(state.hashes, mutation.relativePath, {
2206
+ configurable: true,
2207
+ enumerable: true,
2208
+ value: mutation.contentHash,
2209
+ writable: true,
2210
+ });
2211
+ while (order.size > mutation.maxPaths) {
2212
+ const oldest = order.keys().next().value;
2213
+ if (oldest === undefined)
2214
+ break;
2215
+ order.delete(oldest);
2216
+ delete state.hashes[oldest];
2217
+ }
2218
+ return state;
2219
+ }
1943
2220
  function durablePublishedContentHashSetRecord(record) {
1944
2221
  const payload = record.payload;
1945
2222
  if (typeof payload.relativePath !== "string" ||
@@ -1960,6 +2237,33 @@ function durablePublishedContentHashDeleteRecord(record) {
1960
2237
  }
1961
2238
  return { relativePath: payload.relativePath };
1962
2239
  }
2240
+ function durablePublishedContentHashBatchRecord(record) {
2241
+ const payload = record.payload;
2242
+ if (!Array.isArray(payload.entries) || payload.entries.length === 0) {
2243
+ throw new Error("published-content-hashes: invalid batch record");
2244
+ }
2245
+ return {
2246
+ entries: payload.entries.map((entry) => {
2247
+ if (typeof entry !== "object" || entry === null) {
2248
+ throw new Error("published-content-hashes: invalid batch record");
2249
+ }
2250
+ const mutation = entry;
2251
+ if (mutation.kind === "set") {
2252
+ return {
2253
+ kind: "set",
2254
+ ...durablePublishedContentHashSetRecord({ ...record, payload: entry }),
2255
+ };
2256
+ }
2257
+ if (mutation.kind === "delete") {
2258
+ return {
2259
+ kind: "delete",
2260
+ ...durablePublishedContentHashDeleteRecord({ ...record, payload: entry }),
2261
+ };
2262
+ }
2263
+ throw new Error("published-content-hashes: invalid batch record");
2264
+ }),
2265
+ };
2266
+ }
1963
2267
  function durablePublishedContentHashOrder(state) {
1964
2268
  const existing = durablePublishedContentHashOrders.get(state);
1965
2269
  if (existing)
@@ -2182,8 +2486,8 @@ export class PushEventEmitter {
2182
2486
  */
2183
2487
  deferredPublishes = new Map();
2184
2488
  deferredPublishTimers = new Map();
2185
- durablePublishedHashRetries = new Map();
2186
2489
  disposed = false;
2490
+ disposePromise = null;
2187
2491
  _realtimeUnavailable = false;
2188
2492
  internalSeq = 0;
2189
2493
  nextSeq;
@@ -2283,7 +2587,7 @@ export class PushEventEmitter {
2283
2587
  // A pulled revision was never locally announced, so it must not begin the
2284
2588
  // per-path announcement quiet period for a subsequent local edit.
2285
2589
  this.lastPublishedContentHashes.set(relativePath, contentHash);
2286
- void this.setDurablePublishedContentHash(relativePath, contentHash);
2590
+ void this.setDurablePublishedContentHash(relativePath, contentHash, undefined, false);
2287
2591
  }
2288
2592
  async emitEntries(entries) {
2289
2593
  const attempts = [];
@@ -2452,7 +2756,10 @@ export class PushEventEmitter {
2452
2756
  }
2453
2757
  openDurablePublishedContentHashes(stateDir, initialHashes) {
2454
2758
  try {
2455
- return new DurablePublishedContentHashes(stateDir, `${this.originTenantId}:${this.originDeviceId}`, this.lastPublishedContentHashMaxPaths, initialHashes);
2759
+ return new DurablePublishedContentHashes(stateDir, `${this.originTenantId}:${this.originDeviceId}`, this.lastPublishedContentHashMaxPaths, initialHashes, this.clock, (error, mutationCount, relativePath) => {
2760
+ error.message += `; durable published-content-hash batch affected ${mutationCount} mutation${mutationCount === 1 ? "" : "s"}`;
2761
+ this.onError(error, relativePath === undefined ? {} : { relativePath });
2762
+ });
2456
2763
  }
2457
2764
  catch (err) {
2458
2765
  this.onError(err instanceof Error ? err : new Error(String(err)), {});
@@ -2483,30 +2790,32 @@ export class PushEventEmitter {
2483
2790
  return false;
2484
2791
  }
2485
2792
  }
2486
- async setDurablePublishedContentHash(relativePath, contentHash, generation) {
2487
- const operation = { kind: "set", contentHash, generation };
2793
+ async setDurablePublishedContentHash(relativePath, contentHash, generation, flushImmediately = true) {
2488
2794
  try {
2489
- const persisted = await this.persistDurablePublishedContentHash(relativePath, operation, generation === undefined
2490
- ? undefined
2491
- : () => this.isPublishedContentHashGenerationCurrent(relativePath, generation));
2492
- if (persisted)
2493
- this.clearDurablePublishedContentHashRetry(relativePath);
2795
+ if (!this.durablePublishedContentHashes)
2796
+ return;
2797
+ if (generation === undefined) {
2798
+ await this.durablePublishedContentHashes.set(relativePath, contentHash, this.lastPublishedContentHashMaxPaths, flushImmediately);
2799
+ return;
2800
+ }
2801
+ await this.durablePublishedContentHashes.setIfCurrent(relativePath, contentHash, this.lastPublishedContentHashMaxPaths, () => this.isPublishedContentHashGenerationCurrent(relativePath, generation));
2494
2802
  }
2495
2803
  catch (err) {
2496
- this.handleDurablePublishedContentHashFailure(relativePath, operation, err);
2804
+ this.onError(err instanceof Error ? err : new Error(String(err)), { relativePath });
2497
2805
  }
2498
2806
  }
2499
- async deleteDurablePublishedContentHash(relativePath, generation) {
2500
- const operation = { kind: "delete", generation };
2807
+ async deleteDurablePublishedContentHash(relativePath, generation, flushImmediately = true) {
2501
2808
  try {
2502
- const persisted = await this.persistDurablePublishedContentHash(relativePath, operation, generation === undefined
2503
- ? undefined
2504
- : () => this.isPublishedContentHashGenerationCurrent(relativePath, generation));
2505
- if (persisted)
2506
- this.clearDurablePublishedContentHashRetry(relativePath);
2809
+ if (!this.durablePublishedContentHashes)
2810
+ return;
2811
+ if (generation === undefined) {
2812
+ await this.durablePublishedContentHashes.delete(relativePath, flushImmediately);
2813
+ return;
2814
+ }
2815
+ await this.durablePublishedContentHashes.deleteIfCurrent(relativePath, () => this.isPublishedContentHashGenerationCurrent(relativePath, generation));
2507
2816
  }
2508
2817
  catch (err) {
2509
- this.handleDurablePublishedContentHashFailure(relativePath, operation, err);
2818
+ this.onError(err instanceof Error ? err : new Error(String(err)), { relativePath });
2510
2819
  }
2511
2820
  }
2512
2821
  /**
@@ -2530,79 +2839,6 @@ export class PushEventEmitter {
2530
2839
  return true;
2531
2840
  return this.publishedContentHashUnchangedSince(relativePath, generationCounterAtStart);
2532
2841
  }
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
2842
  /** Remember one latest-wins uploaded revision until its publish interval ends. */
2607
2843
  deferUpsert(event, lastPublishedAt) {
2608
2844
  const relativePath = event.relativePath;
@@ -2643,21 +2879,46 @@ export class PushEventEmitter {
2643
2879
  this.clock.clearTimeout(timer);
2644
2880
  this.deferredPublishTimers.delete(relativePath);
2645
2881
  }
2646
- /** Cancel every trailing publish and make future batch delivery inert. */
2882
+ /** Flush durable publication state, cancel trailing publishes, and become inert. */
2647
2883
  dispose() {
2648
- if (this.disposed)
2649
- return;
2884
+ if (this.disposePromise !== null)
2885
+ return this.disposePromise;
2650
2886
  this.disposed = true;
2651
- for (const timer of this.deferredPublishTimers.values()) {
2652
- this.clock.clearTimeout(timer);
2887
+ this.disposePromise = (async () => {
2888
+ for (const timer of this.deferredPublishTimers.values()) {
2889
+ this.clock.clearTimeout(timer);
2890
+ }
2891
+ this.deferredPublishTimers.clear();
2892
+ this.deferredPublishes.clear();
2893
+ if (await this.waitForPublishDrain()) {
2894
+ await this.disposeDurablePublishedContentHashes();
2895
+ return;
2896
+ }
2897
+ 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`), {});
2898
+ void this.publishTail.then(() => this.disposeDurablePublishedContentHashes());
2899
+ })();
2900
+ return this.disposePromise;
2901
+ }
2902
+ async waitForPublishDrain() {
2903
+ let timeout;
2904
+ const deadline = new Promise((resolve) => {
2905
+ timeout = this.clock.setTimeout(() => resolve(false), PUSH_EVENT_EMITTER_DISPOSE_DRAIN_MS);
2906
+ });
2907
+ const drained = await Promise.race([
2908
+ this.publishTail.then(() => true, () => true),
2909
+ deadline,
2910
+ ]);
2911
+ if (timeout !== undefined)
2912
+ this.clock.clearTimeout(timeout);
2913
+ return drained;
2914
+ }
2915
+ async disposeDurablePublishedContentHashes() {
2916
+ try {
2917
+ await this.durablePublishedContentHashes?.dispose();
2653
2918
  }
2654
- this.deferredPublishTimers.clear();
2655
- this.deferredPublishes.clear();
2656
- for (const retry of this.durablePublishedHashRetries.values()) {
2657
- if (retry.timer !== undefined)
2658
- this.clock.clearTimeout(retry.timer);
2919
+ catch (err) {
2920
+ this.onError(err instanceof Error ? err : new Error(String(err)), {});
2659
2921
  }
2660
- this.durablePublishedHashRetries.clear();
2661
2922
  // Release the per-path maps so a disposed emitter retains nothing.
2662
2923
  this.publishedContentHashGenerations.clear();
2663
2924
  }
@@ -2670,7 +2931,7 @@ export class PushEventEmitter {
2670
2931
  lastPublishedContentHashes: this.lastPublishedContentHashes.size,
2671
2932
  publishedContentHashGenerations: this.publishedContentHashGenerations.size,
2672
2933
  deferredPublishes: this.deferredPublishes.size,
2673
- durablePublishedHashRetries: this.durablePublishedHashRetries.size,
2934
+ durablePublishedHashRetries: this.durablePublishedContentHashes?.pendingRetryCount ?? 0,
2674
2935
  // Runner callers no longer pass the session-wide object. Keep a fixed
2675
2936
  // zero-valued token so heap telemetry explicitly records its absence.
2676
2937
  initialPublishedContentHashes: 0,