@tearleads/client-sdk 0.1.253 → 0.1.255

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 (108) hide show
  1. package/client/containerContents/documentDiscovery.js +0 -2
  2. package/client/containerContents/documentDiscovery.js.map +1 -1
  3. package/data/persistence/container-contents/containerContentsPersistenceTypes.d.ts +7 -10
  4. package/data/persistence/container-contents/containerContentsPersistenceTypes.js.map +1 -1
  5. package/data/persistence/container-contents/containerDeletionPersistence.js +5 -1
  6. package/data/persistence/container-contents/containerDeletionPersistence.js.map +1 -1
  7. package/data/persistence/container-contents/containerHydrationPersistence.js +1 -1
  8. package/data/persistence/container-contents/containerHydrationPersistence.js.map +1 -1
  9. package/data/persistence/container-contents/containerMetadataMutationPersistence.js +11 -4
  10. package/data/persistence/container-contents/containerMetadataMutationPersistence.js.map +1 -1
  11. package/data/persistence/container-contents/containerRemovalMetadata.js +47 -0
  12. package/data/persistence/container-contents/containerRemovalMetadata.js.map +1 -0
  13. package/data/persistence/container-contents/containerRemovalPlacements.js +22 -0
  14. package/data/persistence/container-contents/containerRemovalPlacements.js.map +1 -0
  15. package/data/persistence/container-contents/containerSyncWatermarkPersistence.d.ts +2 -1
  16. package/data/persistence/container-contents/containerSyncWatermarkPersistence.js +10 -2
  17. package/data/persistence/container-contents/containerSyncWatermarkPersistence.js.map +1 -1
  18. package/data/persistence/documents/documentDiscoveryCandidate.d.ts +1 -0
  19. package/data/persistence/documents/documentDiscoveryCandidate.js +3 -1
  20. package/data/persistence/documents/documentDiscoveryCandidate.js.map +1 -1
  21. package/data/persistence/documents/documentDiscoveryEvidencePersistence.d.ts +1 -1
  22. package/data/persistence/documents/documentDiscoveryEvidencePersistence.js +16 -4
  23. package/data/persistence/documents/documentDiscoveryEvidencePersistence.js.map +1 -1
  24. package/data/sqlite/migrations/localSchemaMigrations.js +11 -0
  25. package/data/sqlite/migrations/localSchemaMigrations.js.map +1 -1
  26. package/data/sqlite/schema/containerSyncWatermarkSchema.js +6 -3
  27. package/data/sqlite/schema/containerSyncWatermarkSchema.js.map +1 -1
  28. package/data/sqlite/schema/index.d.ts +17 -0
  29. package/index.d.ts +1 -1
  30. package/index.js +1 -1
  31. package/index.js.map +1 -1
  32. package/internal/api-client/ApiClient.d.ts +1 -0
  33. package/internal/api-client/client/apiClientContext.d.ts +1 -0
  34. package/internal/api-client/client/containerReads.d.ts +1 -0
  35. package/internal/api-client/requestInternals.js +14 -6
  36. package/internal/api-client/requestInternals.js.map +1 -1
  37. package/internal/validators/containerSystemSlot.d.ts +6 -0
  38. package/internal/validators/containerSystemSlot.js +14 -6
  39. package/internal/validators/containerSystemSlot.js.map +1 -1
  40. package/internal/validators/operation/containerReads.d.ts +29 -2
  41. package/internal/validators/operation/containerReads.js +2 -1
  42. package/internal/validators/operation/containerReads.js.map +1 -1
  43. package/internal/validators/request/containerDocumentLanes.d.ts +7 -0
  44. package/internal/validators/request/containerDocumentLanes.js +6 -0
  45. package/internal/validators/request/containerDocumentLanes.js.map +1 -1
  46. package/internal/validators/response/container.d.ts +8 -0
  47. package/internal/validators/response/container.js +1 -0
  48. package/internal/validators/response/container.js.map +1 -1
  49. package/internal/validators/response/containerParentLanes.d.ts +2 -0
  50. package/package.json +1 -1
  51. package/stores/container-contents/operations/index.js +4 -0
  52. package/stores/container-contents/operations/index.js.map +1 -1
  53. package/workflows/container-contents/container-state/create/createChild.d.ts +30 -0
  54. package/workflows/container-contents/container-state/move/moveIntentSync.js +1 -0
  55. package/workflows/container-contents/container-state/move/moveIntentSync.js.map +1 -1
  56. package/workflows/container-contents/container-state/remote.js +1 -1
  57. package/workflows/container-contents/container-state/remote.js.map +1 -1
  58. package/workflows/container-contents/container-state/types.d.ts +141 -0
  59. package/workflows/container-contents/documentDiscovery/containerDocumentListing.js +52 -4
  60. package/workflows/container-contents/documentDiscovery/containerDocumentListing.js.map +1 -1
  61. package/workflows/container-contents/documentDiscovery/documentDiscoveryApply.js +4 -2
  62. package/workflows/container-contents/documentDiscovery/documentDiscoveryApply.js.map +1 -1
  63. package/workflows/container-contents/documentDiscovery/documentDiscoveryEvidence.js +17 -12
  64. package/workflows/container-contents/documentDiscovery/documentDiscoveryEvidence.js.map +1 -1
  65. package/workflows/container-contents/documentDiscovery/documentDiscoveryTypes.js.map +1 -1
  66. package/workflows/container-contents/documentDiscovery/publicationWatermark.js +15 -0
  67. package/workflows/container-contents/documentDiscovery/publicationWatermark.js.map +1 -0
  68. package/workflows/container-contents/index.d.ts +1 -0
  69. package/workflows/container-contents/index.js +1 -0
  70. package/workflows/container-contents/index.js.map +1 -1
  71. package/workflows/container-contents/remoteHydration/destination/destinationSystemTopology.js +16 -0
  72. package/workflows/container-contents/remoteHydration/destination/destinationSystemTopology.js.map +1 -0
  73. package/workflows/container-contents/remoteHydration/destination/verifiedDestination.js +8 -9
  74. package/workflows/container-contents/remoteHydration/destination/verifiedDestination.js.map +1 -1
  75. package/workflows/container-contents/remoteHydration/hydrateRemoteContainers.js +4 -4
  76. package/workflows/container-contents/remoteHydration/hydrateRemoteContainers.js.map +1 -1
  77. package/workflows/container-contents/remoteHydration/lanes/restorationReceipt.js +25 -0
  78. package/workflows/container-contents/remoteHydration/lanes/restorationReceipt.js.map +1 -0
  79. package/workflows/container-contents/remoteHydration/lanes/tombstoneAccess.js +107 -0
  80. package/workflows/container-contents/remoteHydration/lanes/tombstoneAccess.js.map +1 -0
  81. package/workflows/container-contents/remoteHydration/remoteContainers/remoteContainerPageHydration.js +29 -27
  82. package/workflows/container-contents/remoteHydration/remoteContainers/remoteContainerPageHydration.js.map +1 -1
  83. package/workflows/container-contents/remoteHydration/remoteContainers/remoteContainerState.js +17 -5
  84. package/workflows/container-contents/remoteHydration/remoteContainers/remoteContainerState.js.map +1 -1
  85. package/workflows/container-contents/remoteHydration/tombstoneApplication.js +67 -24
  86. package/workflows/container-contents/remoteHydration/tombstoneApplication.js.map +1 -1
  87. package/workflows/container-contents/remoteHydration/types.d.ts +3 -1
  88. package/workflows/container-contents/remoteHydration/types.js.map +1 -1
  89. package/workflows/container-contents/remoteReset/remoteResetCursorScope.js +3 -3
  90. package/workflows/container-contents/remoteReset/remoteResetCursorScope.js.map +1 -1
  91. package/workflows/container-contents/syncPause/discoveryGuard.js +6 -2
  92. package/workflows/container-contents/syncPause/discoveryGuard.js.map +1 -1
  93. package/workflows/container-contents/tombstones/documentTombstoneGate.js +6 -3
  94. package/workflows/container-contents/tombstones/documentTombstoneGate.js.map +1 -1
  95. package/workflows/registration/bootstrapRootContainer.d.ts +1 -0
  96. package/workflows/registration/bootstrapRootContainer.js +9 -0
  97. package/workflows/registration/bootstrapRootContainer.js.map +1 -1
  98. package/workflows/registration/index.d.ts +1 -1
  99. package/workflows/registration/index.js +1 -1
  100. package/workflows/registration/index.js.map +1 -1
  101. package/workflows/registration/organizationProvisioning.d.ts +3 -2
  102. package/workflows/registration/organizationProvisioning.js +1 -2
  103. package/workflows/registration/organizationProvisioning.js.map +1 -1
  104. package/workflows/registration/provisioningBootstrapBuilders.d.ts +3 -5
  105. package/workflows/registration/provisioningBootstrapBuilders.js +11 -5
  106. package/workflows/registration/provisioningBootstrapBuilders.js.map +1 -1
  107. package/workflows/registration/registerIdentity.js +2 -0
  108. package/workflows/registration/registerIdentity.js.map +1 -1
@@ -1,4 +1,5 @@
1
1
  import { createPrincipalPolicyOperation, } from "../../../principals/principalPolicyService.js";
2
+ import { changesDiscoveryOrder } from "../../documentDiscovery/publicationWatermark.js";
2
3
  import { prefetchDestinationProjections } from "../destination/destinationPrefetch.js";
3
4
  import { cachedDestinationRole } from "../destination/destinationRoleCache.js";
4
5
  import { markContainerParentLaneFetched } from "../lanes/laneFetchMarkers.js";
@@ -30,13 +31,12 @@ function matchesListedState(state, expectedContainerStates, containerId) {
30
31
  });
31
32
  }
32
33
  async function applyRemoteContainerPage(input) {
33
- const { expectedContainerStates, expectedHydrationTombstones, queueParentLane, seenContainerIds, state, } = input;
34
+ const { seenContainerObservations, state } = input;
34
35
  let hydratedCount = 0;
35
- const policies = await warmListedContainerPolicies(input.items, state.runtime.principalPolicies.createWarmer({
36
+ const { items, outcome: policyOutcome } = await warmListedContainerPolicies(input.items, state.runtime.principalPolicies.createWarmer({
36
37
  operation: input.policyOperation,
37
38
  }), input.reconnect?.recordManualError, input.isCurrent);
38
- const { items } = policies;
39
- let outcome = policies.outcome;
39
+ let outcome = policyOutcome;
40
40
  if (input.isCurrent?.() === false) {
41
41
  return { changedCount: 0, outcome: "retry" };
42
42
  }
@@ -54,9 +54,9 @@ async function applyRemoteContainerPage(input) {
54
54
  return { changedCount: 0, outcome: "retry" };
55
55
  }
56
56
  const prefetchedProjections = await prefetchDestinationProjections({
57
- containers: items.filter((item) => !seenContainerIds.has(item.id) &&
57
+ containers: items.filter((item) => !seenContainerObservations.has(item.id) &&
58
58
  !state.containersById.has(item.id) &&
59
- !expectedHydrationTombstones.get(item.id) &&
59
+ !input.expectedHydrationTombstones.get(item.id) &&
60
60
  !cachedDestinationRole(state.runtime.infra.execSql, item)),
61
61
  isCurrent: input.isCurrent,
62
62
  runtime: state.runtime,
@@ -65,11 +65,13 @@ async function applyRemoteContainerPage(input) {
65
65
  if (input.isCurrent?.() === false) {
66
66
  return { changedCount: hydratedCount, outcome: "retry" };
67
67
  }
68
- if (seenContainerIds.has(container.id)) {
69
- queueParentLane(container.id);
68
+ const observation = JSON.stringify(container);
69
+ if (seenContainerObservations.get(container.id) === observation &&
70
+ state.containersById.has(container.id)) {
71
+ input.queueParentLane(container.id);
70
72
  continue;
71
73
  }
72
- if (!matchesListedState(state, expectedContainerStates, container.id)) {
74
+ if (!matchesListedState(state, input.expectedContainerStates, container.id)) {
73
75
  outcome = "retry";
74
76
  continue;
75
77
  }
@@ -80,7 +82,7 @@ async function applyRemoteContainerPage(input) {
80
82
  containerIdsWithPendingStructuralIntents,
81
83
  host: input.host,
82
84
  isCurrent: currentUnlessPaused(input, container),
83
- expectedHydrationTombstone: expectedHydrationTombstones.get(container.id) ?? null,
85
+ expectedHydrationTombstone: input.expectedHydrationTombstones.get(container.id) ?? null,
84
86
  prefetchedProjection: prefetchedProjections.get(container.id),
85
87
  remoteContainer: container,
86
88
  state,
@@ -91,12 +93,12 @@ async function applyRemoteContainerPage(input) {
91
93
  outcome = combineOutcomes(outcome, failure);
92
94
  continue;
93
95
  }
94
- seenContainerIds.add(container.id);
96
+ seenContainerObservations.set(container.id, observation);
95
97
  hydratedCount += 1;
96
98
  if (input.isCurrent?.() === false) {
97
99
  return { changedCount: hydratedCount, outcome: "retry" };
98
100
  }
99
- queueParentLane(container.id);
101
+ input.queueParentLane(container.id);
100
102
  }
101
103
  return { changedCount: hydratedCount, outcome };
102
104
  }
@@ -149,14 +151,19 @@ async function checkpointPage(input) {
149
151
  });
150
152
  }
151
153
  async function applyContainerParentLanePage(input) {
152
- const { childIdsByParentId, fetchedPage, host, queueContinuationLane, queueParentLane, seenContainerIds, state, } = input;
154
+ const { childIdsByParentId, fetchedPage, host, state } = input;
153
155
  const { expectedContainerStates, expectedHydrationTombstones, lane, response, } = fetchedPage;
154
156
  let changedCount = 0;
155
157
  if (input.isCurrent?.() === false) {
156
158
  return { changedCount, outcome: "retry", shouldStop: true };
157
159
  }
160
+ if (changesDiscoveryOrder(lane.watermark, response.nextWatermark, !!lane.continuationCursors))
161
+ return { changedCount, outcome: "retry", shouldStop: false };
158
162
  const remoteContainerItems = getApplicableRemoteContainerItems(response);
163
+ const isPaused = await loadPausedContainerFilter(state);
159
164
  const tombstoneResult = await applyContainerTombstones({
165
+ isPaused,
166
+ parentId: lane.parentId,
160
167
  childIdsByParentId,
161
168
  expectedContainerStates,
162
169
  isCurrent: input.isCurrent,
@@ -172,15 +179,10 @@ async function applyContainerParentLanePage(input) {
172
179
  return { changedCount, outcome: "retry", shouldStop: true };
173
180
  }
174
181
  if (tombstoneResult.changedCount > 0) {
175
- // A live tombstone cascade may have orphaned documents (row 3); re-arm
176
- // document priming so their null-scoped passes run now rather than on
177
- // the next startup.
182
+ // Prime documents orphaned by this cascade during the current pass.
178
183
  host.requestDocumentPriming?.();
179
184
  }
180
- // A paused organization's containers stay as the device last saw them.
181
- // Skipping them here, after tombstones saw the full page, still lets the
182
- // lane complete and its watermark advance; their children are never queued.
183
- const isPaused = await loadPausedContainerFilter(state);
185
+ // Paused folders keep their local state while the lane advances.
184
186
  const appliedPage = await applyRemoteContainerPage({
185
187
  reconnect: input.reconnect,
186
188
  childIdsByParentId,
@@ -191,8 +193,8 @@ async function applyContainerParentLanePage(input) {
191
193
  isPaused,
192
194
  items: remoteContainerItems.filter((item) => !isPaused(item)),
193
195
  policyOperation: input.policyOperation,
194
- queueParentLane,
195
- seenContainerIds,
196
+ queueParentLane: input.queueParentLane,
197
+ seenContainerObservations: input.seenContainerObservations,
196
198
  state,
197
199
  });
198
200
  changedCount += appliedPage.changedCount;
@@ -211,7 +213,7 @@ async function applyContainerParentLanePage(input) {
211
213
  if (!skipWatermarkWrite && !(await checkpointPage(input))) {
212
214
  return { changedCount, outcome: "retry", shouldStop: true };
213
215
  }
214
- queueNextPage(fetchedPage, skipWatermarkWrite, queueContinuationLane);
216
+ queueNextPage(fetchedPage, skipWatermarkWrite, input.queueContinuationLane);
215
217
  return { changedCount, outcome, shouldStop: false };
216
218
  }
217
219
  function takeContainerParentLaneBatch(input) {
@@ -228,7 +230,7 @@ function takeContainerParentLaneBatch(input) {
228
230
  return batch;
229
231
  }
230
232
  async function applyContainerParentLaneBatch(input) {
231
- const { childIdsByParentId, fetchedPages, host, lanes, queueParentLane, seenContainerIds, state, } = input;
233
+ const { childIdsByParentId, fetchedPages, host, lanes, queueParentLane, seenContainerObservations, state, } = input;
232
234
  let changedCount = 0;
233
235
  let outcome = "complete";
234
236
  for (const fetchedPage of fetchedPages) {
@@ -248,7 +250,7 @@ async function applyContainerParentLaneBatch(input) {
248
250
  policyOperation: input.policyOperation,
249
251
  queueContinuationLane: (lane) => lanes.push(lane),
250
252
  queueParentLane,
251
- seenContainerIds,
253
+ seenContainerObservations,
252
254
  state,
253
255
  });
254
256
  changedCount += result.changedCount;
@@ -260,7 +262,7 @@ async function applyContainerParentLaneBatch(input) {
260
262
  return { changedCount, outcome, shouldStop: false };
261
263
  }
262
264
  export async function hydrateContainerParentLanes(input) {
263
- const { childIdsByParentId, host, lanes, queueParentLane, seenContainerIds, state, } = input;
265
+ const { childIdsByParentId, host, lanes, queueParentLane, seenContainerObservations, state, } = input;
264
266
  let changedCount = 0;
265
267
  let outcome = "complete";
266
268
  // One hydration pass is one policy operation: its pages share a discovery.
@@ -294,7 +296,7 @@ export async function hydrateContainerParentLanes(input) {
294
296
  lanes,
295
297
  policyOperation,
296
298
  queueParentLane,
297
- seenContainerIds,
299
+ seenContainerObservations,
298
300
  state,
299
301
  });
300
302
  changedCount += result.changedCount;
@@ -1 +1 @@
1
- {"version":3,"file":"remoteContainerPageHydration.js","sourceRoot":"","sources":["remoteContainerPageHydration.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,8BAA8B,GAE/B,MAAM,4CAA4C,CAAC;AACpD,OAAO,EAAE,8BAA8B,EAAE,MAAM,oCAAoC,CAAC;AACpF,OAAO,EAAE,qBAAqB,EAAE,MAAM,qCAAqC,CAAC;AAC5E,OAAO,EAAE,8BAA8B,EAAE,MAAM,2BAA2B,CAAC;AAC3E,OAAO,EAAE,6BAA6B,EAAE,MAAM,0BAA0B,CAAC;AACzE,OAAO,EAAE,2BAA2B,EAAE,MAAM,yBAAyB,CAAC;AACtE,OAAO,EACL,wBAAwB,EACxB,iCAAiC,GAClC,MAAM,yBAAyB,CAAC;AAajC,OAAO,EAAE,gCAAgC,EAAE,MAAM,6BAA6B,CAAC;AAC/E,OAAO,EAAE,yBAAyB,EAAE,MAAM,+BAA+B,CAAC;AAC1E,OAAO,EACL,gDAAgD,EAChD,kDAAkD,EAClD,kCAAkC,GACnC,MAAM,wBAAwB,CAAC;AAEhC,MAAM,sCAAsC,GAAG,CAAC,CAAC;AAOjD,qFAAqF;AACrF,SAAS,eAAe,CACtB,CAAmB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AAC;YACvB,SAAS;QACX,CAAC;QAED,MAAM,YAAY,GAAG,MAAM,6BAA6B,CAAC;YACvD,KAAK;YACL,YAAY,EAAE,KAAK,CAAC,SAAS,EAAE,YAAY;YAC3C,SAAS,EAAE,KAAK,CAAC,SAAS;YAC1B,KAAK;SACN,CAAC,CAAC;QACH,IAAI,CAAC,YAAY,EAAE,CAAC;YAClB,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC;QAC9D,CAAC;QACD,IAAI,KAAK,CAAC,SAAS,EAAE,EAAE,KAAK,KAAK,EAAE,CAAC;YAClC,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC;QAC9D,CAAC;QAED,MAAM,MAAM,GAAG,MAAM,6BAA6B,CAAC;YACjD,SAAS,EAAE,KAAK,CAAC,SAAS;YAC1B,kBAAkB;YAClB,YAAY;YACZ,IAAI;YACJ,SAAS,EAAE,KAAK,CAAC,SAAS;YAC1B,KAAK;YACL,eAAe;YACf,eAAe;YACf,gBAAgB;YAChB,KAAK;SACN,CAAC,CAAC;QACH,YAAY,IAAI,MAAM,CAAC,YAAY,CAAC;QACpC,OAAO,GAAG,eAAe,CAAC,OAAO,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC;QAEnD,IAAI,MAAM,CAAC,UAAU,EAAE,CAAC;YACtB,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC;QAC9D,CAAC;IACH,CAAC;IAED,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC;AACtD,CAAC","sourcesContent":["import {\n createPrincipalPolicyOperation,\n type PrincipalPolicyOperation,\n} from \"../../../principals/principalPolicyService\";\nimport { prefetchDestinationProjections } from \"../destination/destinationPrefetch\";\nimport { cachedDestinationRole } from \"../destination/destinationRoleCache\";\nimport { markContainerParentLaneFetched } from \"../lanes/laneFetchMarkers\";\nimport { fetchContainerParentLaneBatch } from \"../lanes/parentLaneFetch\";\nimport { warmListedContainerPolicies } from \"../principalPolicyCache\";\nimport {\n applyContainerTombstones,\n getApplicableRemoteContainerItems,\n} from \"../tombstoneApplication\";\nimport type {\n ContainerChildIndex,\n ContainerParentHydrationLane,\n ExpectedContainerState,\n FetchedContainerParentLanePage,\n HydrationOutcome,\n ListedRemoteContainerPageItem,\n QueueContainerParentLane,\n ReconnectPages,\n RemoteContainerHydrationHost,\n RemoteContainerHydrationState,\n} from \"../types\";\nimport { containerStateMatchesFingerprint } from \"./containerStateFingerprint\";\nimport { loadPausedContainerFilter } from \"./organizationSyncPauseFilter\";\nimport {\n listRemoteContainerIdsWithPendingMetadataUpdates,\n listRemoteContainerIdsWithPendingStructuralIntents,\n upsertIsolatedRemoteContainerState,\n} from \"./remoteContainerState\";\n\nconst CONTAINER_PARENT_HYDRATION_CONCURRENCY = 4;\n\ninterface HydrationProgress {\n changedCount: number;\n outcome: HydrationOutcome;\n shouldStop: boolean;\n}\n/** A retryable lane takes precedence over a refused proof elsewhere in the crawl. */\nfunction combineOutcomes(\n a: HydrationOutcome,\n b: HydrationOutcome,\n): HydrationOutcome {\n if (a === \"retry\" || b === \"retry\") return \"retry\";\n if (a === \"refused\" || b === \"refused\") return \"refused\";\n return \"complete\";\n}\n\n// Re-checked inside the commit: a pause from another device can land after\n// the page was filtered, and its cleanup may already have run.\nfunction currentUnlessPaused(\n input: {\n isCurrent?: (() => boolean) | undefined;\n isPaused: (container: ListedRemoteContainerPageItem) => boolean;\n },\n container: ListedRemoteContainerPageItem,\n): () => boolean {\n return () => input.isCurrent?.() !== false && !input.isPaused(container);\n}\n\n/** The local state still matches the fingerprint the listing was read at. */\nfunction matchesListedState(\n state: RemoteContainerHydrationState,\n expectedContainerStates: ReadonlyMap<string, ExpectedContainerState>,\n containerId: string,\n): boolean {\n return containerStateMatchesFingerprint({\n currentState: state.containersById.get(containerId),\n expectedFingerprint: expectedContainerStates.get(containerId)?.fingerprint,\n });\n}\n\nasync function applyRemoteContainerPage(input: {\n reconnect?: ReconnectPages | undefined;\n childIdsByParentId: ContainerChildIndex;\n expectedContainerStates: ReadonlyMap<string, ExpectedContainerState>;\n expectedHydrationTombstones: FetchedContainerParentLanePage[\"expectedHydrationTombstones\"];\n host: RemoteContainerHydrationHost;\n isCurrent?: (() => boolean) | undefined;\n /** Live pause check; a pause arriving mid-page stops the commit. */\n isPaused: (container: ListedRemoteContainerPageItem) => boolean;\n items: ReadonlyArray<ListedRemoteContainerPageItem>;\n /** The hydration pass's token: its pages share one directory discovery. */\n policyOperation: PrincipalPolicyOperation;\n queueParentLane: QueueContainerParentLane;\n seenContainerIds: Set<string>;\n state: RemoteContainerHydrationState;\n}): Promise<{ changedCount: number; outcome: HydrationOutcome }> {\n const {\n expectedContainerStates,\n expectedHydrationTombstones,\n queueParentLane,\n seenContainerIds,\n state,\n } = input;\n let hydratedCount = 0;\n const policies = await warmListedContainerPolicies(\n input.items,\n state.runtime.principalPolicies.createWarmer({\n operation: input.policyOperation,\n }),\n input.reconnect?.recordManualError,\n input.isCurrent,\n );\n const { items } = policies;\n let outcome: HydrationOutcome = policies.outcome;\n if (input.isCurrent?.() === false) {\n return { changedCount: 0, outcome: \"retry\" };\n }\n const [\n containerIdsWithPendingMetadataUpdates,\n containerIdsWithPendingStructuralIntents,\n ] = await Promise.all([\n listRemoteContainerIdsWithPendingMetadataUpdates({\n remoteContainers: items,\n state,\n }),\n listRemoteContainerIdsWithPendingStructuralIntents({\n remoteContainers: items,\n state,\n }),\n ]);\n if (input.isCurrent?.() === false) {\n return { changedCount: 0, outcome: \"retry\" };\n }\n const prefetchedProjections = await prefetchDestinationProjections({\n containers: items.filter(\n (item) =>\n !seenContainerIds.has(item.id) &&\n !state.containersById.has(item.id) &&\n !expectedHydrationTombstones.get(item.id) &&\n !cachedDestinationRole(state.runtime.infra.execSql, item),\n ),\n isCurrent: input.isCurrent,\n runtime: state.runtime,\n });\n for (const container of items) {\n if (input.isCurrent?.() === false) {\n return { changedCount: hydratedCount, outcome: \"retry\" };\n }\n if (seenContainerIds.has(container.id)) {\n queueParentLane(container.id);\n continue;\n }\n if (!matchesListedState(state, expectedContainerStates, container.id)) {\n outcome = \"retry\";\n continue;\n }\n let failure: HydrationOutcome = \"retry\";\n const upserted = await upsertIsolatedRemoteContainerState(\n {\n childIdsByParentId: input.childIdsByParentId,\n containerIdsWithPendingMetadataUpdates,\n containerIdsWithPendingStructuralIntents,\n host: input.host,\n isCurrent: currentUnlessPaused(input, container),\n expectedHydrationTombstone:\n expectedHydrationTombstones.get(container.id) ?? null,\n prefetchedProjection: prefetchedProjections.get(container.id),\n remoteContainer: container,\n state,\n },\n (outcome) => {\n failure = outcome;\n },\n input.reconnect?.recordManualError,\n );\n if (!upserted) {\n outcome = combineOutcomes(outcome, failure);\n continue;\n }\n seenContainerIds.add(container.id);\n hydratedCount += 1;\n if (input.isCurrent?.() === false) {\n return { changedCount: hydratedCount, outcome: \"retry\" };\n }\n queueParentLane(container.id);\n }\n\n return { changedCount: hydratedCount, outcome };\n}\n\nexport function canHydrateRemoteContainers(\n state: RemoteContainerHydrationState,\n): boolean {\n return (\n state.runtime.auth.isAuthenticated &&\n state.runtime.state.online &&\n state.runtime.infra.dbStatus === \"ready\"\n );\n}\n\nfunction cursorKey(\n watermark: NonNullable<ContainerParentHydrationLane[\"watermark\"]>,\n): string {\n return JSON.stringify([watermark.updatedAt, watermark.id]);\n}\n\nfunction hasUnusableContinuation({\n lane,\n response,\n}: FetchedContainerParentLanePage): boolean {\n const next = response.nextWatermark;\n if (!next) return response.hasMore;\n // Opaque positions can advance with an older business timestamp.\n // A drained lane may echo its current position, but never an earlier one.\n if (\n next.id === lane.watermark?.id &&\n next.updatedAt === lane.watermark.updatedAt\n )\n return response.hasMore;\n return lane.continuationCursors?.has(cursorKey(next)) === true;\n}\n\nfunction queueNextPage(\n { lane, response }: FetchedContainerParentLanePage,\n skipWatermarkWrite: boolean,\n queueContinuationLane: (lane: ContainerParentHydrationLane) => void,\n): void {\n if (response.hasMore && response.nextWatermark) {\n const continuationCursors = lane.continuationCursors ?? new Set<string>();\n if (lane.watermark) continuationCursors.add(cursorKey(lane.watermark));\n continuationCursors.add(cursorKey(response.nextWatermark));\n queueContinuationLane({\n parentId: lane.parentId,\n watermark: response.nextWatermark,\n skipWatermarkWrite,\n continuationCursors,\n });\n }\n}\n\nasync function checkpointPage(input: {\n reconnect?: ReconnectPages | undefined;\n fetchedPage: FetchedContainerParentLanePage;\n isCurrent?: (() => boolean) | undefined;\n state: RemoteContainerHydrationState;\n}): Promise<boolean> {\n const { lane, response, syncLane } = input.fetchedPage;\n // Only a drained chain can advance a reconnect checkpoint. Otherwise a\n // cycle can alternate the durable cursor between successive attempts.\n if (input.reconnect && response.hasMore) return true;\n const nextWatermark =\n response.nextWatermark ??\n (lane.continuationCursors ? (lane.watermark ?? null) : null);\n return markContainerParentLaneFetched({\n isCurrent: input.isCurrent,\n response: { ...response, nextWatermark },\n state: input.state,\n syncLane,\n });\n}\n\nasync function applyContainerParentLanePage(input: {\n reconnect?: ReconnectPages | undefined;\n childIdsByParentId: ContainerChildIndex;\n fetchedPage: FetchedContainerParentLanePage;\n host: RemoteContainerHydrationHost;\n isCurrent?: (() => boolean) | undefined;\n policyOperation: PrincipalPolicyOperation;\n queueContinuationLane: (lane: ContainerParentHydrationLane) => void;\n queueParentLane: QueueContainerParentLane;\n seenContainerIds: Set<string>;\n state: RemoteContainerHydrationState;\n}): Promise<HydrationProgress> {\n const {\n childIdsByParentId,\n fetchedPage,\n host,\n queueContinuationLane,\n queueParentLane,\n seenContainerIds,\n state,\n } = input;\n const {\n expectedContainerStates,\n expectedHydrationTombstones,\n lane,\n response,\n } = fetchedPage;\n let changedCount = 0;\n if (input.isCurrent?.() === false) {\n return { changedCount, outcome: \"retry\", shouldStop: true };\n }\n\n const remoteContainerItems = getApplicableRemoteContainerItems(response);\n const tombstoneResult = await applyContainerTombstones({\n childIdsByParentId,\n expectedContainerStates,\n isCurrent: input.isCurrent,\n remoteContainerItems,\n response,\n state,\n });\n if (!tombstoneResult.current) {\n return { changedCount, outcome: \"retry\", shouldStop: true };\n }\n changedCount += tombstoneResult.changedCount;\n if (input.isCurrent?.() === false) {\n return { changedCount, outcome: \"retry\", shouldStop: true };\n }\n if (tombstoneResult.changedCount > 0) {\n // A live tombstone cascade may have orphaned documents (row 3); re-arm\n // document priming so their null-scoped passes run now rather than on\n // the next startup.\n host.requestDocumentPriming?.();\n }\n\n // A paused organization's containers stay as the device last saw them.\n // Skipping them here, after tombstones saw the full page, still lets the\n // lane complete and its watermark advance; their children are never queued.\n const isPaused = await loadPausedContainerFilter(state);\n const appliedPage = await applyRemoteContainerPage({\n reconnect: input.reconnect,\n childIdsByParentId,\n expectedContainerStates,\n expectedHydrationTombstones,\n host,\n isCurrent: input.isCurrent,\n isPaused,\n items: remoteContainerItems.filter((item) => !isPaused(item)),\n policyOperation: input.policyOperation,\n queueParentLane,\n seenContainerIds,\n state,\n });\n changedCount += appliedPage.changedCount;\n const outcome = tombstoneResult.completed ? appliedPage.outcome : \"retry\";\n if (input.isCurrent?.() === false) {\n return { changedCount, outcome: \"retry\", shouldStop: true };\n }\n // A bad continuation invalidates the cursor, independently verified rows still apply.\n if (hasUnusableContinuation(fetchedPage))\n return { changedCount, outcome: \"retry\", shouldStop: false };\n // A refused item leaves a gap in this lane. Later pages can still discover\n // independent children, but none may move its durable cursor past the gap.\n if (outcome !== \"complete\" && !input.reconnect)\n return { changedCount, outcome, shouldStop: false };\n const skipWatermarkWrite = lane.skipWatermarkWrite || outcome !== \"complete\";\n if (!skipWatermarkWrite && !(await checkpointPage(input))) {\n return { changedCount, outcome: \"retry\", shouldStop: true };\n }\n\n queueNextPage(fetchedPage, skipWatermarkWrite, queueContinuationLane);\n return { changedCount, outcome, shouldStop: false };\n}\n\nfunction takeContainerParentLaneBatch(input: {\n lanes: ContainerParentHydrationLane[];\n state: RemoteContainerHydrationState;\n}): ContainerParentHydrationLane[] {\n const { lanes, state } = input;\n const batch: ContainerParentHydrationLane[] = [];\n\n while (\n lanes.length > 0 &&\n batch.length < CONTAINER_PARENT_HYDRATION_CONCURRENCY\n ) {\n const lane = lanes.shift();\n if (\n lane &&\n (lane.parentId === null || state.containersById.has(lane.parentId))\n ) {\n batch.push(lane);\n }\n }\n\n return batch;\n}\n\nasync function applyContainerParentLaneBatch(input: {\n reconnect?: ReconnectPages | undefined;\n childIdsByParentId: ContainerChildIndex;\n fetchedPages: ReadonlyArray<FetchedContainerParentLanePage>;\n host: RemoteContainerHydrationHost;\n isCurrent?: (() => boolean) | undefined;\n lanes: ContainerParentHydrationLane[];\n policyOperation: PrincipalPolicyOperation;\n queueParentLane: QueueContainerParentLane;\n seenContainerIds: Set<string>;\n state: RemoteContainerHydrationState;\n}): Promise<HydrationProgress> {\n const {\n childIdsByParentId,\n fetchedPages,\n host,\n lanes,\n queueParentLane,\n seenContainerIds,\n state,\n } = input;\n let changedCount = 0;\n let outcome: HydrationOutcome = \"complete\";\n\n for (const fetchedPage of fetchedPages) {\n if (!canHydrateRemoteContainers(state) || input.isCurrent?.() === false) {\n return { changedCount, outcome: \"retry\", shouldStop: true };\n }\n if (\n fetchedPage.lane.parentId !== null &&\n !state.containersById.has(fetchedPage.lane.parentId)\n ) {\n continue;\n }\n\n const result = await applyContainerParentLanePage({\n reconnect: input.reconnect,\n childIdsByParentId,\n fetchedPage,\n host,\n isCurrent: input.isCurrent,\n policyOperation: input.policyOperation,\n queueContinuationLane: (lane) => lanes.push(lane),\n queueParentLane,\n seenContainerIds,\n state,\n });\n changedCount += result.changedCount;\n outcome = combineOutcomes(outcome, result.outcome);\n\n if (result.shouldStop) {\n return { changedCount, outcome: \"retry\", shouldStop: true };\n }\n }\n\n return { changedCount, outcome, shouldStop: false };\n}\n\nexport async function hydrateContainerParentLanes(input: {\n reconnect?: ReconnectPages | undefined;\n childIdsByParentId: ContainerChildIndex;\n host: RemoteContainerHydrationHost;\n isCurrent?: (() => boolean) | undefined;\n lanes: ContainerParentHydrationLane[];\n queueParentLane: QueueContainerParentLane;\n seenContainerIds: Set<string>;\n state: RemoteContainerHydrationState;\n}): Promise<HydrationProgress> {\n const {\n childIdsByParentId,\n host,\n lanes,\n queueParentLane,\n seenContainerIds,\n state,\n } = input;\n let changedCount = 0;\n let outcome: HydrationOutcome = \"complete\";\n // One hydration pass is one policy operation: its pages share a discovery.\n const policyOperation = createPrincipalPolicyOperation();\n\n while (lanes.length > 0) {\n if (!canHydrateRemoteContainers(state) || input.isCurrent?.() === false) {\n return { changedCount, outcome: \"retry\", shouldStop: true };\n }\n\n const batch = takeContainerParentLaneBatch({ lanes, state });\n if (batch.length === 0) {\n continue;\n }\n\n const fetchedPages = await fetchContainerParentLaneBatch({\n batch,\n laneIdPrefix: input.reconnect?.laneIdPrefix,\n isCurrent: input.isCurrent,\n state,\n });\n if (!fetchedPages) {\n return { changedCount, outcome: \"retry\", shouldStop: true };\n }\n if (input.isCurrent?.() === false) {\n return { changedCount, outcome: \"retry\", shouldStop: true };\n }\n\n const result = await applyContainerParentLaneBatch({\n reconnect: input.reconnect,\n childIdsByParentId,\n fetchedPages,\n host,\n isCurrent: input.isCurrent,\n lanes,\n policyOperation,\n queueParentLane,\n seenContainerIds,\n state,\n });\n changedCount += result.changedCount;\n outcome = combineOutcomes(outcome, result.outcome);\n\n if (result.shouldStop) {\n return { changedCount, outcome: \"retry\", shouldStop: true };\n }\n }\n\n return { changedCount, outcome, shouldStop: false };\n}\n"]}
1
+ {"version":3,"file":"remoteContainerPageHydration.js","sourceRoot":"","sources":["remoteContainerPageHydration.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,8BAA8B,GAE/B,MAAM,4CAA4C,CAAC;AACpD,OAAO,EAAE,qBAAqB,EAAE,MAAM,8CAA8C,CAAC;AACrF,OAAO,EAAE,8BAA8B,EAAE,MAAM,oCAAoC,CAAC;AACpF,OAAO,EAAE,qBAA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ACD,IAAI,KAAK,CAAC,SAAS,EAAE,EAAE,KAAK,KAAK,EAAE,CAAC;YAClC,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC;QAC9D,CAAC;QAED,MAAM,MAAM,GAAG,MAAM,6BAA6B,CAAC;YACjD,SAAS,EAAE,KAAK,CAAC,SAAS;YAC1B,kBAAkB;YAClB,YAAY;YACZ,IAAI;YACJ,SAAS,EAAE,KAAK,CAAC,SAAS;YAC1B,KAAK;YACL,eAAe;YACf,eAAe;YACf,yBAAyB;YACzB,KAAK;SACN,CAAC,CAAC;QACH,YAAY,IAAI,MAAM,CAAC,YAAY,CAAC;QACpC,OAAO,GAAG,eAAe,CAAC,OAAO,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC;QAEnD,IAAI,MAAM,CAAC,UAAU,EAAE,CAAC;YACtB,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC;QAC9D,CAAC;IACH,CAAC;IAED,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC;AACtD,CAAC","sourcesContent":["import {\n createPrincipalPolicyOperation,\n type PrincipalPolicyOperation,\n} from \"../../../principals/principalPolicyService\";\nimport { changesDiscoveryOrder } from \"../../documentDiscovery/publicationWatermark\";\nimport { prefetchDestinationProjections } from \"../destination/destinationPrefetch\";\nimport { cachedDestinationRole } from \"../destination/destinationRoleCache\";\nimport { markContainerParentLaneFetched } from \"../lanes/laneFetchMarkers\";\nimport { fetchContainerParentLaneBatch } from \"../lanes/parentLaneFetch\";\nimport { warmListedContainerPolicies } from \"../principalPolicyCache\";\nimport {\n applyContainerTombstones,\n getApplicableRemoteContainerItems,\n} from \"../tombstoneApplication\";\nimport type {\n ContainerChildIndex,\n ContainerParentHydrationLane,\n ExpectedContainerState,\n FetchedContainerParentLanePage,\n HydrationOutcome,\n ListedRemoteContainerPageItem,\n QueueContainerParentLane,\n ReconnectPages,\n RemoteContainerHydrationHost,\n RemoteContainerHydrationState,\n} from \"../types\";\nimport { containerStateMatchesFingerprint } from \"./containerStateFingerprint\";\nimport { loadPausedContainerFilter } from \"./organizationSyncPauseFilter\";\nimport {\n listRemoteContainerIdsWithPendingMetadataUpdates,\n listRemoteContainerIdsWithPendingStructuralIntents,\n upsertIsolatedRemoteContainerState,\n} from \"./remoteContainerState\";\n\nconst CONTAINER_PARENT_HYDRATION_CONCURRENCY = 4;\n\ninterface HydrationProgress {\n changedCount: number;\n outcome: HydrationOutcome;\n shouldStop: boolean;\n}\n/** A retryable lane takes precedence over a refused proof elsewhere in the crawl. */\nfunction combineOutcomes(\n a: HydrationOutcome,\n b: HydrationOutcome,\n): HydrationOutcome {\n if (a === \"retry\" || b === \"retry\") return \"retry\";\n if (a === \"refused\" || b === \"refused\") return \"refused\";\n return \"complete\";\n}\n\n// Re-checked inside the commit: a pause from another device can land after\n// the page was filtered, and its cleanup may already have run.\nfunction currentUnlessPaused(\n input: {\n isCurrent?: (() => boolean) | undefined;\n isPaused: (container: ListedRemoteContainerPageItem) => boolean;\n },\n container: ListedRemoteContainerPageItem,\n): () => boolean {\n return () => input.isCurrent?.() !== false && !input.isPaused(container);\n}\n\n/** The local state still matches the fingerprint the listing was read at. */\nfunction matchesListedState(\n state: RemoteContainerHydrationState,\n expectedContainerStates: ReadonlyMap<string, ExpectedContainerState>,\n containerId: string,\n): boolean {\n return containerStateMatchesFingerprint({\n currentState: state.containersById.get(containerId),\n expectedFingerprint: expectedContainerStates.get(containerId)?.fingerprint,\n });\n}\n\nasync function applyRemoteContainerPage(input: {\n reconnect?: ReconnectPages | undefined;\n childIdsByParentId: ContainerChildIndex;\n expectedContainerStates: ReadonlyMap<string, ExpectedContainerState>;\n expectedHydrationTombstones: FetchedContainerParentLanePage[\"expectedHydrationTombstones\"];\n host: RemoteContainerHydrationHost;\n isCurrent?: (() => boolean) | undefined;\n isPaused: (container: ListedRemoteContainerPageItem) => boolean;\n items: ReadonlyArray<ListedRemoteContainerPageItem>;\n /** The hydration pass's token: its pages share one directory discovery. */\n policyOperation: PrincipalPolicyOperation;\n queueParentLane: QueueContainerParentLane;\n seenContainerObservations: Map<string, string>;\n state: RemoteContainerHydrationState;\n}): Promise<{ changedCount: number; outcome: HydrationOutcome }> {\n const { seenContainerObservations, state } = input;\n let hydratedCount = 0;\n const { items, outcome: policyOutcome } = await warmListedContainerPolicies(\n input.items,\n state.runtime.principalPolicies.createWarmer({\n operation: input.policyOperation,\n }),\n input.reconnect?.recordManualError,\n input.isCurrent,\n );\n let outcome: HydrationOutcome = policyOutcome;\n if (input.isCurrent?.() === false) {\n return { changedCount: 0, outcome: \"retry\" };\n }\n const [\n containerIdsWithPendingMetadataUpdates,\n containerIdsWithPendingStructuralIntents,\n ] = await Promise.all([\n listRemoteContainerIdsWithPendingMetadataUpdates({\n remoteContainers: items,\n state,\n }),\n listRemoteContainerIdsWithPendingStructuralIntents({\n remoteContainers: items,\n state,\n }),\n ]);\n if (input.isCurrent?.() === false) {\n return { changedCount: 0, outcome: \"retry\" };\n }\n const prefetchedProjections = await prefetchDestinationProjections({\n containers: items.filter(\n (item) =>\n !seenContainerObservations.has(item.id) &&\n !state.containersById.has(item.id) &&\n !input.expectedHydrationTombstones.get(item.id) &&\n !cachedDestinationRole(state.runtime.infra.execSql, item),\n ),\n isCurrent: input.isCurrent,\n runtime: state.runtime,\n });\n for (const container of items) {\n if (input.isCurrent?.() === false) {\n return { changedCount: hydratedCount, outcome: \"retry\" };\n }\n const observation = JSON.stringify(container);\n if (\n seenContainerObservations.get(container.id) === observation &&\n state.containersById.has(container.id)\n ) {\n input.queueParentLane(container.id);\n continue;\n }\n if (\n !matchesListedState(state, input.expectedContainerStates, container.id)\n ) {\n outcome = \"retry\";\n continue;\n }\n let failure: HydrationOutcome = \"retry\";\n const upserted = await upsertIsolatedRemoteContainerState(\n {\n childIdsByParentId: input.childIdsByParentId,\n containerIdsWithPendingMetadataUpdates,\n containerIdsWithPendingStructuralIntents,\n host: input.host,\n isCurrent: currentUnlessPaused(input, container),\n expectedHydrationTombstone:\n input.expectedHydrationTombstones.get(container.id) ?? null,\n prefetchedProjection: prefetchedProjections.get(container.id),\n remoteContainer: container,\n state,\n },\n (outcome) => {\n failure = outcome;\n },\n input.reconnect?.recordManualError,\n );\n if (!upserted) {\n outcome = combineOutcomes(outcome, failure);\n continue;\n }\n seenContainerObservations.set(container.id, observation);\n hydratedCount += 1;\n if (input.isCurrent?.() === false) {\n return { changedCount: hydratedCount, outcome: \"retry\" };\n }\n input.queueParentLane(container.id);\n }\n\n return { changedCount: hydratedCount, outcome };\n}\n\nexport function canHydrateRemoteContainers(\n state: RemoteContainerHydrationState,\n): boolean {\n return (\n state.runtime.auth.isAuthenticated &&\n state.runtime.state.online &&\n state.runtime.infra.dbStatus === \"ready\"\n );\n}\n\nfunction cursorKey(\n watermark: NonNullable<ContainerParentHydrationLane[\"watermark\"]>,\n): string {\n return JSON.stringify([watermark.updatedAt, watermark.id]);\n}\n\nfunction hasUnusableContinuation({\n lane,\n response,\n}: FetchedContainerParentLanePage): boolean {\n const next = response.nextWatermark;\n if (!next) return response.hasMore;\n // Opaque positions can advance with an older business timestamp.\n // A drained lane may echo its current position, but never an earlier one.\n if (\n next.id === lane.watermark?.id &&\n next.updatedAt === lane.watermark.updatedAt\n )\n return response.hasMore;\n return lane.continuationCursors?.has(cursorKey(next)) === true;\n}\n\nfunction queueNextPage(\n { lane, response }: FetchedContainerParentLanePage,\n skipWatermarkWrite: boolean,\n queueContinuationLane: (lane: ContainerParentHydrationLane) => void,\n): void {\n if (response.hasMore && response.nextWatermark) {\n const continuationCursors = lane.continuationCursors ?? new Set<string>();\n if (lane.watermark) continuationCursors.add(cursorKey(lane.watermark));\n continuationCursors.add(cursorKey(response.nextWatermark));\n queueContinuationLane({\n parentId: lane.parentId,\n watermark: response.nextWatermark,\n skipWatermarkWrite,\n continuationCursors,\n });\n }\n}\n\nasync function checkpointPage(input: {\n reconnect?: ReconnectPages | undefined;\n fetchedPage: FetchedContainerParentLanePage;\n isCurrent?: (() => boolean) | undefined;\n state: RemoteContainerHydrationState;\n}): Promise<boolean> {\n const { lane, response, syncLane } = input.fetchedPage;\n // Only a drained chain can advance a reconnect checkpoint. Otherwise a\n // cycle can alternate the durable cursor between successive attempts.\n if (input.reconnect && response.hasMore) return true;\n const nextWatermark =\n response.nextWatermark ??\n (lane.continuationCursors ? (lane.watermark ?? null) : null);\n return markContainerParentLaneFetched({\n isCurrent: input.isCurrent,\n response: { ...response, nextWatermark },\n state: input.state,\n syncLane,\n });\n}\n\nasync function applyContainerParentLanePage(input: {\n reconnect?: ReconnectPages | undefined;\n childIdsByParentId: ContainerChildIndex;\n fetchedPage: FetchedContainerParentLanePage;\n host: RemoteContainerHydrationHost;\n isCurrent?: (() => boolean) | undefined;\n policyOperation: PrincipalPolicyOperation;\n queueContinuationLane: (lane: ContainerParentHydrationLane) => void;\n queueParentLane: QueueContainerParentLane;\n seenContainerObservations: Map<string, string>;\n state: RemoteContainerHydrationState;\n}): Promise<HydrationProgress> {\n const { childIdsByParentId, fetchedPage, host, state } = input;\n const {\n expectedContainerStates,\n expectedHydrationTombstones,\n lane,\n response,\n } = fetchedPage;\n let changedCount = 0;\n if (input.isCurrent?.() === false) {\n return { changedCount, outcome: \"retry\", shouldStop: true };\n }\n\n if (\n changesDiscoveryOrder(\n lane.watermark,\n response.nextWatermark,\n !!lane.continuationCursors,\n )\n )\n return { changedCount, outcome: \"retry\", shouldStop: false };\n const remoteContainerItems = getApplicableRemoteContainerItems(response);\n const isPaused = await loadPausedContainerFilter(state);\n const tombstoneResult = await applyContainerTombstones({\n isPaused,\n parentId: lane.parentId,\n childIdsByParentId,\n expectedContainerStates,\n isCurrent: input.isCurrent,\n remoteContainerItems,\n response,\n state,\n });\n if (!tombstoneResult.current) {\n return { changedCount, outcome: \"retry\", shouldStop: true };\n }\n changedCount += tombstoneResult.changedCount;\n if (input.isCurrent?.() === false) {\n return { changedCount, outcome: \"retry\", shouldStop: true };\n }\n if (tombstoneResult.changedCount > 0) {\n // Prime documents orphaned by this cascade during the current pass.\n host.requestDocumentPriming?.();\n }\n\n // Paused folders keep their local state while the lane advances.\n const appliedPage = await applyRemoteContainerPage({\n reconnect: input.reconnect,\n childIdsByParentId,\n expectedContainerStates,\n expectedHydrationTombstones,\n host,\n isCurrent: input.isCurrent,\n isPaused,\n items: remoteContainerItems.filter((item) => !isPaused(item)),\n policyOperation: input.policyOperation,\n queueParentLane: input.queueParentLane,\n seenContainerObservations: input.seenContainerObservations,\n state,\n });\n changedCount += appliedPage.changedCount;\n const outcome = tombstoneResult.completed ? appliedPage.outcome : \"retry\";\n if (input.isCurrent?.() === false) {\n return { changedCount, outcome: \"retry\", shouldStop: true };\n }\n // A bad continuation invalidates the cursor, independently verified rows still apply.\n if (hasUnusableContinuation(fetchedPage))\n return { changedCount, outcome: \"retry\", shouldStop: false };\n // A refused item leaves a gap in this lane. Later pages can still discover\n // independent children, but none may move its durable cursor past the gap.\n if (outcome !== \"complete\" && !input.reconnect)\n return { changedCount, outcome, shouldStop: false };\n const skipWatermarkWrite = lane.skipWatermarkWrite || outcome !== \"complete\";\n if (!skipWatermarkWrite && !(await checkpointPage(input))) {\n return { changedCount, outcome: \"retry\", shouldStop: true };\n }\n\n queueNextPage(fetchedPage, skipWatermarkWrite, input.queueContinuationLane);\n return { changedCount, outcome, shouldStop: false };\n}\n\nfunction takeContainerParentLaneBatch(input: {\n lanes: ContainerParentHydrationLane[];\n state: RemoteContainerHydrationState;\n}): ContainerParentHydrationLane[] {\n const { lanes, state } = input;\n const batch: ContainerParentHydrationLane[] = [];\n\n while (\n lanes.length > 0 &&\n batch.length < CONTAINER_PARENT_HYDRATION_CONCURRENCY\n ) {\n const lane = lanes.shift();\n if (\n lane &&\n (lane.parentId === null || state.containersById.has(lane.parentId))\n ) {\n batch.push(lane);\n }\n }\n\n return batch;\n}\n\nasync function applyContainerParentLaneBatch(input: {\n reconnect?: ReconnectPages | undefined;\n childIdsByParentId: ContainerChildIndex;\n fetchedPages: ReadonlyArray<FetchedContainerParentLanePage>;\n host: RemoteContainerHydrationHost;\n isCurrent?: (() => boolean) | undefined;\n lanes: ContainerParentHydrationLane[];\n policyOperation: PrincipalPolicyOperation;\n queueParentLane: QueueContainerParentLane;\n seenContainerObservations: Map<string, string>;\n state: RemoteContainerHydrationState;\n}): Promise<HydrationProgress> {\n const {\n childIdsByParentId,\n fetchedPages,\n host,\n lanes,\n queueParentLane,\n seenContainerObservations,\n state,\n } = input;\n let changedCount = 0;\n let outcome: HydrationOutcome = \"complete\";\n\n for (const fetchedPage of fetchedPages) {\n if (!canHydrateRemoteContainers(state) || input.isCurrent?.() === false) {\n return { changedCount, outcome: \"retry\", shouldStop: true };\n }\n if (\n fetchedPage.lane.parentId !== null &&\n !state.containersById.has(fetchedPage.lane.parentId)\n ) {\n continue;\n }\n\n const result = await applyContainerParentLanePage({\n reconnect: input.reconnect,\n childIdsByParentId,\n fetchedPage,\n host,\n isCurrent: input.isCurrent,\n policyOperation: input.policyOperation,\n queueContinuationLane: (lane) => lanes.push(lane),\n queueParentLane,\n seenContainerObservations,\n state,\n });\n changedCount += result.changedCount;\n outcome = combineOutcomes(outcome, result.outcome);\n\n if (result.shouldStop) {\n return { changedCount, outcome: \"retry\", shouldStop: true };\n }\n }\n\n return { changedCount, outcome, shouldStop: false };\n}\n\nexport async function hydrateContainerParentLanes(input: {\n reconnect?: ReconnectPages | undefined;\n childIdsByParentId: ContainerChildIndex;\n host: RemoteContainerHydrationHost;\n isCurrent?: (() => boolean) | undefined;\n lanes: ContainerParentHydrationLane[];\n queueParentLane: QueueContainerParentLane;\n seenContainerObservations: Map<string, string>;\n state: RemoteContainerHydrationState;\n}): Promise<HydrationProgress> {\n const {\n childIdsByParentId,\n host,\n lanes,\n queueParentLane,\n seenContainerObservations,\n state,\n } = input;\n let changedCount = 0;\n let outcome: HydrationOutcome = \"complete\";\n // One hydration pass is one policy operation: its pages share a discovery.\n const policyOperation = createPrincipalPolicyOperation();\n\n while (lanes.length > 0) {\n if (!canHydrateRemoteContainers(state) || input.isCurrent?.() === false) {\n return { changedCount, outcome: \"retry\", shouldStop: true };\n }\n\n const batch = takeContainerParentLaneBatch({ lanes, state });\n if (batch.length === 0) {\n continue;\n }\n\n const fetchedPages = await fetchContainerParentLaneBatch({\n batch,\n laneIdPrefix: input.reconnect?.laneIdPrefix,\n isCurrent: input.isCurrent,\n state,\n });\n if (!fetchedPages) {\n return { changedCount, outcome: \"retry\", shouldStop: true };\n }\n if (input.isCurrent?.() === false) {\n return { changedCount, outcome: \"retry\", shouldStop: true };\n }\n\n const result = await applyContainerParentLaneBatch({\n reconnect: input.reconnect,\n childIdsByParentId,\n fetchedPages,\n host,\n isCurrent: input.isCurrent,\n lanes,\n policyOperation,\n queueParentLane,\n seenContainerObservations,\n state,\n });\n changedCount += result.changedCount;\n outcome = combineOutcomes(outcome, result.outcome);\n\n if (result.shouldStop) {\n return { changedCount, outcome: \"retry\", shouldStop: true };\n }\n }\n\n return { changedCount, outcome, shouldStop: false };\n}\n"]}
@@ -154,10 +154,14 @@ async function updateExistingRemoteContainerState(input) {
154
154
  systemSlot: remoteContainer.systemSlot ?? null,
155
155
  organizationId: remoteContainer.organizationId,
156
156
  ...(nextParentId !== undefined ? { parentId: nextParentId } : {}),
157
- }, false, remoteContainerHydrationSaveOptions({
158
- localUpdatedAt,
159
- remoteContainer,
160
- }), {
157
+ }, false, {
158
+ ...remoteContainerHydrationSaveOptions({
159
+ localUpdatedAt,
160
+ remoteContainer,
161
+ }),
162
+ verifiedPlacement: input.verifiedPlacement,
163
+ }, {
164
+ isCurrent: input.isCurrent,
161
165
  preserveDurableStructureWhenPending: true,
162
166
  });
163
167
  if (persistenceResult.status === "missing") {
@@ -227,17 +231,24 @@ export async function upsertRemoteContainerState(input) {
227
231
  if (input.isCurrent?.() === false)
228
232
  return null;
229
233
  invalidateListedProjectionCaches(input);
234
+ const backwardsClock = heldState?.container.serverUpdatedAt != null &&
235
+ heldState.container.serverUpdatedAt > listed.updatedAt;
230
236
  const verified = await verifyRemoteContainerDestination({
231
237
  ...input,
232
238
  heldBinding,
233
239
  supersededListing,
234
- refresh: !!input.expectedHydrationTombstone,
240
+ refresh: !!input.expectedHydrationTombstone || backwardsClock,
235
241
  onVerifiedCheckpoint: (checkpoint) => {
236
242
  expectedPlacementCheckpoint = checkpoint;
237
243
  },
238
244
  });
239
245
  if (!verified || input.isCurrent?.() === false)
240
246
  return null;
247
+ // A signed projection from a lagging replica can still repeat our old pin.
248
+ // Retry until it covers the manifest whose listing would be checkpointed.
249
+ if (backwardsClock &&
250
+ expectedPlacementCheckpoint?.manifestHash !== listed.metadataAccessStateHash)
251
+ return null;
241
252
  input = { ...input, remoteContainer: verified };
242
253
  const existingState = input.state.containersById.get(input.remoteContainer.id);
243
254
  const remoteState = await runWithSecurityIncidentReporting(input.state.runtime.util.reportSecurityIncident, {
@@ -299,6 +310,7 @@ function persistVerifiedRemoteContainer(input) {
299
310
  const { existingState, expectedPlacementCheckpoint } = input;
300
311
  return existingState
301
312
  ? updateExistingRemoteContainerState({
313
+ verifiedPlacement: expectedPlacementCheckpoint,
302
314
  childIdsByParentId: input.childIdsByParentId,
303
315
  containerIdsWithPendingMetadataUpdates: input.containerIdsWithPendingMetadataUpdates,
304
316
  containerIdsWithPendingStructuralIntents: input.containerIdsWithPendingStructuralIntents,
@@ -1 +1 @@
1
- {"version":3,"file":"remoteContainerState.js","sourceRoot":"","sources":["remoteContainerState.ts"],"names":[],"mappings":"AACA,OAAO,EACL,mBAAmB,EACnB,mBAAmB,EACnB,sBAAsB,GACvB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,sCAAsC,EAAE,MAAM,qDAAqD,CAAC;AAC7G,OAAO,EACL,oBAAoB,EACpB,iCAAiC,GAClC,MAAM,kEAAkE,CAAC;AAC1E,OAAO,EAAE,oCAAoC,EAAE,MAAM,+CAA+C,CA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CAAC,CAAC,kCAAkC,CAAC;YACjC,kBAAkB,EAAE,KAAK,CAAC,kBAAkB;YAC5C,sCAAsC,EACpC,KAAK,CAAC,sCAAsC;YAC9C,wCAAwC,EACtC,KAAK,CAAC,wCAAwC;YAChD,aAAa;YACb,IAAI,EAAE,KAAK,CAAC,IAAI;YAChB,SAAS,EAAE,KAAK,CAAC,SAAS;YAC1B,eAAe,EAAE,KAAK,CAAC,eAAe;YACtC,KAAK,EAAE,KAAK,CAAC,KAAK;SACnB,CAAC;QACJ,CAAC,CAAC,0BAA0B,CAAC;YACzB,kBAAkB,EAAE,KAAK,CAAC,kBAAkB;YAC5C,IAAI,EAAE,KAAK,CAAC,IAAI;YAChB,0BAA0B,EAAE,KAAK,CAAC,0BAA0B,IAAI,IAAI;YACpE,2BAA2B;YAC3B,SAAS,EAAE,KAAK,CAAC,SAAS;YAC1B,eAAe,EAAE,KAAK,CAAC,eAAe;YACtC,KAAK,EAAE,KAAK,CAAC,KAAK;SACnB,CAAC,CAAC;AACT,CAAC","sourcesContent":["import type { AccessManifestCheckpoint } from \"@tearleads/crypto\";\nimport {\n decodeVersionVector,\n encodeVersionVector,\n satisfiesVersionVector,\n} from \"@tearleads/loro\";\nimport { isProjectionVerificationCancelledError } from \"../../../../data/keyingProjectionVerification/types\";\nimport {\n heldContainerBinding,\n listingRepeatsHeldOrdinaryBinding,\n} from \"../../../../data/persistence/container-contents/containerBinding\";\nimport { ProjectionDependencyUnavailableError } from \"../../../../data/shared/dependencyUnavailable\";\nimport {\n isKeyingVerificationError,\n runWithSecurityIncidentReporting,\n} from \"../../../../data/shared/securityIncidents\";\nimport { invalidateContainerProjectionCaches } from \"../../container-state/projectionCache\";\nimport type { ContainerHydrationTombstone } from \"../../containerPersistence\";\nimport { installContainerMetadataRecord } from \"../../metadata/metadataPersistence\";\nimport { projectionGeneration } from \"../../projectionGeneration\";\nimport type { PrefetchedDestinationProjection } from \"../destination/destinationPrefetch\";\nimport { verifyRemoteContainerDestination } from \"../destination/verifiedDestination\";\nimport { rebindReHomedContainer } from \"../heldContainerRebind\";\nimport {\n moveIndexedContainerChild,\n removeIndexedContainerChild,\n} from \"../lanes/childIndex\";\nimport {\n reconcileLocalOnlyRootContainers,\n reconcileLocalOnlySystemContainers,\n} from \"../reconciliation\";\nimport type {\n ContainerChildIndex,\n ContainerState,\n RemoteContainer,\n RemoteContainerHydrationHost,\n RemoteContainerHydrationState,\n} from \"../types\";\nimport { insertRemoteContainerState } from \"./insertRemoteContainer\";\nimport {\n applyRemoteContainerTimestamps,\n remoteContainerHydrationSaveOptions,\n resolveRemoteContainerHydrationLocalUpdatedAt,\n} from \"./remoteContainerTimestamps\";\n\n// Container ids (restricted to the inbound page) that carry an unsynced local\n// create or move intent. Such a container's parent and local-edit timestamp are\n// owned by its structural-intent lane until that lane reconciles, so inbound\n// hydration must not revert parentId to the server value nor collapse\n// localUpdatedAt — doing so silently undoes a queued move and falsely reads\n// \"synced\". Move intents are read via listUnsyncedMoveIntents so a blocked\n// move — one whose destination parent has not synced yet, the common boot-time\n// case — is protected too. Pending *metadata*\n// updates are handled separately (listRemoteContainerIdsWithPendingMetadataUpdates);\n// structural intents live in the local outbox's container lanes, which that\n// query does not cover.\nexport async function listRemoteContainerIdsWithPendingStructuralIntents(input: {\n remoteContainers: ReadonlyArray<RemoteContainer>;\n state: RemoteContainerHydrationState;\n}): Promise<Set<string>> {\n if (input.remoteContainers.length === 0) {\n return new Set();\n }\n const remoteContainerIds = new Set(\n input.remoteContainers.map((remoteContainer) => remoteContainer.id),\n );\n\n const execSql = input.state.runtime.infra.execSql;\n const [pendingCreateIntents, unsyncedMoveIntents] = await Promise.all([\n input.state.persistence.listPendingCreateIntents(execSql),\n input.state.persistence.listUnsyncedMoveIntents(execSql),\n ]);\n const containerIdsWithPendingStructuralIntents = new Set<string>();\n for (const intent of pendingCreateIntents) {\n if (remoteContainerIds.has(intent.containerId)) {\n containerIdsWithPendingStructuralIntents.add(intent.containerId);\n }\n }\n for (const intent of unsyncedMoveIntents) {\n if (remoteContainerIds.has(intent.containerId)) {\n containerIdsWithPendingStructuralIntents.add(intent.containerId);\n }\n }\n return containerIdsWithPendingStructuralIntents;\n}\n\nexport async function listRemoteContainerIdsWithPendingMetadataUpdates(input: {\n remoteContainers: ReadonlyArray<RemoteContainer>;\n state: RemoteContainerHydrationState;\n}): Promise<Set<string>> {\n const containerIds = input.remoteContainers.flatMap((remoteContainer) => {\n const previousLocalUpdatedAt = input.state.containersById.get(\n remoteContainer.id,\n )?.container.localUpdatedAt;\n\n return previousLocalUpdatedAt &&\n previousLocalUpdatedAt.localeCompare(remoteContainer.updatedAt) > 0\n ? [remoteContainer.id]\n : [];\n });\n if (containerIds.length === 0) {\n return new Set();\n }\n\n const execSql = input.state.runtime.infra.execSql;\n return new Set(\n await input.state.persistence.listContainerIdsWithPendingUpdates(\n execSql,\n containerIds,\n ),\n );\n}\n\nfunction removeMissingHydratedContainer(input: {\n childIdsByParentId?: ContainerChildIndex | undefined;\n existingState: ContainerState;\n host: RemoteContainerHydrationHost;\n previousParentId: string | null;\n remoteContainer: RemoteContainer;\n state: RemoteContainerHydrationState;\n}): void {\n const { existingState, remoteContainer, state } = input;\n if (state.containersById.get(remoteContainer.id) !== existingState) {\n return;\n }\n\n state.containersById.delete(remoteContainer.id);\n if (input.childIdsByParentId) {\n removeIndexedContainerChild(\n input.childIdsByParentId,\n remoteContainer.id,\n input.previousParentId,\n );\n input.childIdsByParentId.delete(remoteContainer.id);\n }\n input.host.updateSnapshot();\n}\n\nfunction createUpdatedRemoteContainerState(\n existingState: ContainerState,\n remoteContainer: RemoteContainer,\n): ContainerState {\n return {\n ...existingState,\n container: applyRemoteContainerTimestamps(\n existingState.container,\n remoteContainer,\n ),\n containerWriterProjection: null,\n metadataReferencedPrincipals: remoteContainer.metadataReferencedPrincipals,\n };\n}\n\nfunction installUpdatedRemoteContainerState(\n existingState: ContainerState,\n nextState: ContainerState,\n host: RemoteContainerHydrationHost,\n): void {\n const wasLocalOnly = existingState.container.serverCreatedAt == null;\n existingState.container = nextState.container;\n existingState.containerWriterProjection = nextState.containerWriterProjection;\n existingState.metadataReferencedPrincipals =\n nextState.metadataReferencedPrincipals;\n // The snapshot copied the live generation when it was taken; a hint that\n // landed during the persist leaves the live metadata projection dropped.\n if (projectionGeneration(existingState) === projectionGeneration(nextState))\n existingState.metadataWriterProjection = nextState.metadataWriterProjection;\n installContainerMetadataRecord(existingState, nextState.record);\n // A document pass may have deferred while this root was awaiting proof.\n // The first durable remote acknowledgement makes its pending creates runnable.\n if (wasLocalOnly && existingState.container.serverCreatedAt != null)\n host.requestDocumentPriming?.();\n}\n\n/**\n * A clean metadata document is read only when a hint names it, and a hint sent\n * while this client was offline or not yet subscribed is lost. The listing\n * reports the version vector of every update the server holds for the metadata\n * document; when this client's copy does not cover it, an edit is unread. Owe\n * the read before this hydration advances the lane watermark past the row.\n */\nfunction coversMetadataVersion(\n existingState: ContainerState,\n remoteContainer: RemoteContainer,\n): boolean {\n const listed = remoteContainer.metadataVersionVector;\n try {\n // A document with no stored update is covered by any copy.\n if (decodeVersionVector(listed).toJSON().size === 0) return true;\n return satisfiesVersionVector(\n encodeVersionVector(existingState.doc),\n listed,\n );\n } catch {\n // An unreadable version proves nothing: owe the read, which pulls the\n // truth, and let the rest of the page hydrate.\n return false;\n }\n}\n\nasync function oweMetadataReadIfBehind(\n host: RemoteContainerHydrationHost,\n existingState: ContainerState,\n remoteContainer: RemoteContainer,\n): Promise<void> {\n if (!coversMetadataVersion(existingState, remoteContainer)) {\n await host.requestMetadataRevalidation?.({\n advertisedVersionVector: remoteContainer.metadataVersionVector,\n documentId: remoteContainer.metadataDocumentId,\n });\n }\n}\n\nasync function updateExistingRemoteContainerState(input: {\n childIdsByParentId?: ContainerChildIndex | undefined;\n containerIdsWithPendingMetadataUpdates: ReadonlySet<string>;\n containerIdsWithPendingStructuralIntents: ReadonlySet<string>;\n host: RemoteContainerHydrationHost;\n isCurrent?: (() => boolean) | undefined;\n existingState: ContainerState;\n remoteContainer: RemoteContainer;\n state: RemoteContainerHydrationState;\n}): Promise<ContainerState | null> {\n const { childIdsByParentId, existingState, host, remoteContainer, state } =\n input;\n const previousParentId = existingState.container.parentId;\n const previousLocalUpdatedAt = existingState.container.localUpdatedAt;\n // A queued, not-yet-synced local move/create owns this container's parent\n // until its intent lane reconciles. Omit the remote parent when the page scan\n // saw one; persistence also rechecks the intent transactionally so a move\n // created after that scan wins. Both paths preserve the durable local clock.\n const hasPendingStructuralIntent =\n input.containerIdsWithPendingStructuralIntents.has(remoteContainer.id);\n const nextParentId = hasPendingStructuralIntent\n ? undefined\n : remoteContainer.parentId;\n const localUpdatedAt = resolveRemoteContainerHydrationLocalUpdatedAt({\n containerIdsWithPendingMetadataUpdates:\n input.containerIdsWithPendingMetadataUpdates,\n hasPendingStructuralIntent,\n previousLocalUpdatedAt,\n remoteContainer,\n });\n await oweMetadataReadIfBehind(host, existingState, remoteContainer);\n const nextState = createUpdatedRemoteContainerState(\n existingState,\n remoteContainer,\n );\n const persistenceResult = await host.persistContainerState(\n nextState,\n {\n accessEpoch: remoteContainer.metadataAccessEpoch,\n accessStateHash: remoteContainer.metadataAccessStateHash,\n documentId: remoteContainer.metadataDocumentId,\n effectiveAccessLevel: remoteContainer.effectiveAccessLevel,\n metadataDocumentId: remoteContainer.metadataDocumentId,\n systemSlot: remoteContainer.systemSlot ?? null,\n organizationId: remoteContainer.organizationId,\n ...(nextParentId !== undefined ? { parentId: nextParentId } : {}),\n },\n false,\n remoteContainerHydrationSaveOptions({\n localUpdatedAt,\n remoteContainer,\n }),\n {\n preserveDurableStructureWhenPending: true,\n },\n );\n if (persistenceResult.status === \"missing\") {\n // Hydration persists a detached candidate so an in-flight response cannot\n // mutate mapped state before its guards settle. Retire the exact state this\n // hydration read because store-level reference cleanup cannot see the clone.\n removeMissingHydratedContainer({\n childIdsByParentId,\n existingState,\n host,\n previousParentId,\n remoteContainer,\n state,\n });\n return null;\n }\n if (persistenceResult.status !== \"persisted\") return null;\n const { record: nextRecord } = persistenceResult;\n installContainerMetadataRecord(nextState, nextRecord);\n if (input.isCurrent?.() === false) {\n return existingState;\n }\n nextState.container = {\n ...nextState.container,\n metadataDocumentId: remoteContainer.metadataDocumentId,\n organizationId: remoteContainer.organizationId,\n parentId: nextState.container.parentId,\n };\n\n installUpdatedRemoteContainerState(existingState, nextState, host);\n moveIndexedContainerChild(\n childIdsByParentId,\n remoteContainer.id,\n previousParentId,\n existingState.container.parentId,\n );\n await reconcileLocalOnlyRootContainers({\n childIdsByParentId,\n isCurrent: input.isCurrent,\n remoteRootState: existingState,\n requestDocumentPriming: host.requestDocumentPriming,\n state,\n });\n return existingState;\n}\n\ninterface UpsertRemoteContainerStateInput {\n childIdsByParentId?: ContainerChildIndex | undefined;\n containerIdsWithPendingMetadataUpdates: ReadonlySet<string>;\n containerIdsWithPendingStructuralIntents: ReadonlySet<string>;\n host: RemoteContainerHydrationHost;\n expectedHydrationTombstone?: ContainerHydrationTombstone | null | undefined;\n isCurrent?: (() => boolean) | undefined;\n prefetchedProjection?: PrefetchedDestinationProjection | undefined;\n remoteContainer: RemoteContainer;\n state: RemoteContainerHydrationState;\n}\n\n/** A pull can observe the same manifest change as a missed realtime hint. */\nfunction invalidateListedProjectionCaches(\n input: UpsertRemoteContainerStateInput,\n) {\n const held = input.state.containersById.get(input.remoteContainer.id);\n if (\n held &&\n (held.record.accessStateHash !==\n input.remoteContainer.metadataAccessStateHash ||\n (held.container.effectiveAccessLevel ?? \"read\") !==\n input.remoteContainer.effectiveAccessLevel)\n ) {\n // Eviction admits no remote state. Drop citations before verification can\n // advance local checkpoints past those cached container/document paths.\n invalidateContainerProjectionCaches(input.state, [\n input.remoteContainer.id,\n ]);\n }\n}\n\nexport async function upsertRemoteContainerState(\n input: UpsertRemoteContainerStateInput,\n): Promise<ContainerState | null> {\n let expectedPlacementCheckpoint: AccessManifestCheckpoint | undefined;\n const heldState = input.state.containersById.get(input.remoteContainer.id);\n const heldBinding = heldState\n ? heldContainerBinding(heldState)\n : await input.state.persistence.loadHeldContainerBinding(\n input.state.runtime.infra.execSql,\n input.remoteContainer.id,\n );\n const listed = input.remoteContainer;\n // A folder re-homed away from the listed organization never returns there,\n // whatever binding (possibly none, after a reset) the device holds now.\n const supersededListing =\n !listingRepeatsHeldOrdinaryBinding(heldBinding, listed) &&\n (await input.state.persistence.isSupersededContainerBinding(\n input.state.runtime.infra.execSql,\n { containerId: listed.id, organizationId: listed.organizationId },\n ));\n if (input.isCurrent?.() === false) return null;\n invalidateListedProjectionCaches(input);\n const verified = await verifyRemoteContainerDestination({\n ...input,\n heldBinding,\n supersededListing,\n refresh: !!input.expectedHydrationTombstone,\n onVerifiedCheckpoint: (checkpoint) => {\n expectedPlacementCheckpoint = checkpoint;\n },\n });\n if (!verified || input.isCurrent?.() === false) return null;\n input = { ...input, remoteContainer: verified };\n const existingState = input.state.containersById.get(\n input.remoteContainer.id,\n );\n const remoteState = await runWithSecurityIncidentReporting(\n input.state.runtime.util.reportSecurityIncident,\n {\n objectId: verified.id,\n objectKind: \"container\",\n operation: \"container.binding.persist\",\n organizationId: verified.organizationId,\n },\n async () =>\n (await rebindReHomedContainer({\n existingState,\n heldBinding,\n isCurrent: input.isCurrent,\n state: input.state,\n verified,\n }))\n ? persistVerifiedRemoteContainer({\n ...input,\n existingState,\n expectedPlacementCheckpoint,\n })\n : null,\n );\n if (!remoteState) {\n return null;\n }\n await reconcileLocalOnlySystemContainers({\n childIdsByParentId: input.childIdsByParentId,\n isCurrent: input.isCurrent,\n requestDocumentPriming: input.host.requestDocumentPriming,\n remoteSystemState: remoteState,\n state: input.state,\n });\n return remoteState;\n}\n\n/**\n * Upsert one listed folder, isolating a rejected proof or binding. The refusal\n * was already reported; returning no state leaves the item unapplied without\n * blocking independent folders.\n */\nexport async function upsertIsolatedRemoteContainerState(\n input: UpsertRemoteContainerStateInput,\n recordIncompleteItem?: (outcome: \"refused\" | \"retry\") => void,\n recordUnavailable?:\n | ((error: ProjectionDependencyUnavailableError) => void)\n | undefined,\n): Promise<ContainerState | null> {\n try {\n return await upsertRemoteContainerState(input);\n } catch (error) {\n if (isKeyingVerificationError(error)) {\n recordIncompleteItem?.(\"refused\");\n return null;\n }\n if (isProjectionVerificationCancelledError(error)) return null;\n if (\n error instanceof ProjectionDependencyUnavailableError &&\n recordUnavailable\n ) {\n recordUnavailable(error);\n recordIncompleteItem?.(\"retry\");\n return null;\n }\n throw error;\n }\n}\n\nfunction persistVerifiedRemoteContainer(\n input: UpsertRemoteContainerStateInput & {\n existingState: ContainerState | undefined;\n expectedPlacementCheckpoint: AccessManifestCheckpoint | undefined;\n },\n): Promise<ContainerState | null> {\n const { existingState, expectedPlacementCheckpoint } = input;\n return existingState\n ? updateExistingRemoteContainerState({\n childIdsByParentId: input.childIdsByParentId,\n containerIdsWithPendingMetadataUpdates:\n input.containerIdsWithPendingMetadataUpdates,\n containerIdsWithPendingStructuralIntents:\n input.containerIdsWithPendingStructuralIntents,\n existingState,\n host: input.host,\n isCurrent: input.isCurrent,\n remoteContainer: input.remoteContainer,\n state: input.state,\n })\n : insertRemoteContainerState({\n childIdsByParentId: input.childIdsByParentId,\n host: input.host,\n expectedHydrationTombstone: input.expectedHydrationTombstone ?? null,\n expectedPlacementCheckpoint,\n isCurrent: input.isCurrent,\n remoteContainer: input.remoteContainer,\n state: input.state,\n });\n}\n"]}
1
+ {"version":3,"file":"remoteContainerState.js","sourceRoot":"","sources":["remoteContainerState.ts"],"names":[],"mappings":"AACA,OAAO,EACL,mBAAmB,EACnB,mBAAmB,EACnB,sBAAsB,GACvB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,sCAAsC,EAAE,MAAM,qDAAqD,CAAC;AAC7G,OAAO,EACL,oBAAoB,EACpB,iCAAiC,GAClC,MAAM,kEAAkE,CAAC;AAC1E,OAAO,EAAE,oCAAoC,EAAE,MAAM,+CAA+C,CAAC;AACrG,OAAO,EACL,yBAAyB,EACzB,gCAAgC,GACjC,MAAM,2CAA2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ACrD,iBAAiB,EACjB,CAAC;YACD,iBAAiB,CAAC,KAAK,CAAC,CAAC;YACzB,oBAAoB,EAAE,CAAC,OAAO,CAAC,CAAC;YAChC,OAAO,IAAI,CAAC;QACd,CAAC;QACD,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC;AAED,SAAS,8BAA8B,CACrC,KAGC;IAED,MAAM,EAAE,aAAa,EAAE,2BAA2B,EAAE,GAAG,KAAK,CAAC;IAC7D,OAAO,aAAa;QAClB,CAAC,CAAC,kCAAkC,CAAC;YACjC,iBAAiB,EAAE,2BAA2B;YAC9C,kBAAkB,EAAE,KAAK,CAAC,kBAAkB;YAC5C,sCAAsC,EACpC,KAAK,CAAC,sCAAsC;YAC9C,wCAAwC,EACtC,KAAK,CAAC,wCAAwC;YAChD,aAAa;YACb,IAAI,EAAE,KAAK,CAAC,IAAI;YAChB,SAAS,EAAE,KAAK,CAAC,SAAS;YAC1B,eAAe,EAAE,KAAK,CAAC,eAAe;YACtC,KAAK,EAAE,KAAK,CAAC,KAAK;SACnB,CAAC;QACJ,CAAC,CAAC,0BAA0B,CAAC;YACzB,kBAAkB,EAAE,KAAK,CAAC,kBAAkB;YAC5C,IAAI,EAAE,KAAK,CAAC,IAAI;YAChB,0BAA0B,EAAE,KAAK,CAAC,0BAA0B,IAAI,IAAI;YACpE,2BAA2B;YAC3B,SAAS,EAAE,KAAK,CAAC,SAAS;YAC1B,eAAe,EAAE,KAAK,CAAC,eAAe;YACtC,KAAK,EAAE,KAAK,CAAC,KAAK;SACnB,CAAC,CAAC;AACT,CAAC","sourcesContent":["import type { AccessManifestCheckpoint } from \"@tearleads/crypto\";\nimport {\n decodeVersionVector,\n encodeVersionVector,\n satisfiesVersionVector,\n} from \"@tearleads/loro\";\nimport { isProjectionVerificationCancelledError } from \"../../../../data/keyingProjectionVerification/types\";\nimport {\n heldContainerBinding,\n listingRepeatsHeldOrdinaryBinding,\n} from \"../../../../data/persistence/container-contents/containerBinding\";\nimport { ProjectionDependencyUnavailableError } from \"../../../../data/shared/dependencyUnavailable\";\nimport {\n isKeyingVerificationError,\n runWithSecurityIncidentReporting,\n} from \"../../../../data/shared/securityIncidents\";\nimport { invalidateContainerProjectionCaches } from \"../../container-state/projectionCache\";\nimport type { ContainerHydrationTombstone } from \"../../containerPersistence\";\nimport { installContainerMetadataRecord } from \"../../metadata/metadataPersistence\";\nimport { projectionGeneration } from \"../../projectionGeneration\";\nimport type { PrefetchedDestinationProjection } from \"../destination/destinationPrefetch\";\nimport { verifyRemoteContainerDestination } from \"../destination/verifiedDestination\";\nimport { rebindReHomedContainer } from \"../heldContainerRebind\";\nimport {\n moveIndexedContainerChild,\n removeIndexedContainerChild,\n} from \"../lanes/childIndex\";\nimport {\n reconcileLocalOnlyRootContainers,\n reconcileLocalOnlySystemContainers,\n} from \"../reconciliation\";\nimport type {\n ContainerChildIndex,\n ContainerState,\n RemoteContainer,\n RemoteContainerHydrationHost,\n RemoteContainerHydrationState,\n} from \"../types\";\nimport { insertRemoteContainerState } from \"./insertRemoteContainer\";\nimport {\n applyRemoteContainerTimestamps,\n remoteContainerHydrationSaveOptions,\n resolveRemoteContainerHydrationLocalUpdatedAt,\n} from \"./remoteContainerTimestamps\";\n\n// Container ids (restricted to the inbound page) that carry an unsynced local\n// create or move intent. Such a container's parent and local-edit timestamp are\n// owned by its structural-intent lane until that lane reconciles, so inbound\n// hydration must not revert parentId to the server value nor collapse\n// localUpdatedAt — doing so silently undoes a queued move and falsely reads\n// \"synced\". Move intents are read via listUnsyncedMoveIntents so a blocked\n// move — one whose destination parent has not synced yet, the common boot-time\n// case — is protected too. Pending *metadata*\n// updates are handled separately (listRemoteContainerIdsWithPendingMetadataUpdates);\n// structural intents live in the local outbox's container lanes, which that\n// query does not cover.\nexport async function listRemoteContainerIdsWithPendingStructuralIntents(input: {\n remoteContainers: ReadonlyArray<RemoteContainer>;\n state: RemoteContainerHydrationState;\n}): Promise<Set<string>> {\n if (input.remoteContainers.length === 0) {\n return new Set();\n }\n const remoteContainerIds = new Set(\n input.remoteContainers.map((remoteContainer) => remoteContainer.id),\n );\n\n const execSql = input.state.runtime.infra.execSql;\n const [pendingCreateIntents, unsyncedMoveIntents] = await Promise.all([\n input.state.persistence.listPendingCreateIntents(execSql),\n input.state.persistence.listUnsyncedMoveIntents(execSql),\n ]);\n const containerIdsWithPendingStructuralIntents = new Set<string>();\n for (const intent of pendingCreateIntents) {\n if (remoteContainerIds.has(intent.containerId)) {\n containerIdsWithPendingStructuralIntents.add(intent.containerId);\n }\n }\n for (const intent of unsyncedMoveIntents) {\n if (remoteContainerIds.has(intent.containerId)) {\n containerIdsWithPendingStructuralIntents.add(intent.containerId);\n }\n }\n return containerIdsWithPendingStructuralIntents;\n}\n\nexport async function listRemoteContainerIdsWithPendingMetadataUpdates(input: {\n remoteContainers: ReadonlyArray<RemoteContainer>;\n state: RemoteContainerHydrationState;\n}): Promise<Set<string>> {\n const containerIds = input.remoteContainers.flatMap((remoteContainer) => {\n const previousLocalUpdatedAt = input.state.containersById.get(\n remoteContainer.id,\n )?.container.localUpdatedAt;\n\n return previousLocalUpdatedAt &&\n previousLocalUpdatedAt.localeCompare(remoteContainer.updatedAt) > 0\n ? [remoteContainer.id]\n : [];\n });\n if (containerIds.length === 0) {\n return new Set();\n }\n\n const execSql = input.state.runtime.infra.execSql;\n return new Set(\n await input.state.persistence.listContainerIdsWithPendingUpdates(\n execSql,\n containerIds,\n ),\n );\n}\n\nfunction removeMissingHydratedContainer(input: {\n childIdsByParentId?: ContainerChildIndex | undefined;\n existingState: ContainerState;\n host: RemoteContainerHydrationHost;\n previousParentId: string | null;\n remoteContainer: RemoteContainer;\n state: RemoteContainerHydrationState;\n}): void {\n const { existingState, remoteContainer, state } = input;\n if (state.containersById.get(remoteContainer.id) !== existingState) {\n return;\n }\n\n state.containersById.delete(remoteContainer.id);\n if (input.childIdsByParentId) {\n removeIndexedContainerChild(\n input.childIdsByParentId,\n remoteContainer.id,\n input.previousParentId,\n );\n input.childIdsByParentId.delete(remoteContainer.id);\n }\n input.host.updateSnapshot();\n}\n\nfunction createUpdatedRemoteContainerState(\n existingState: ContainerState,\n remoteContainer: RemoteContainer,\n): ContainerState {\n return {\n ...existingState,\n container: applyRemoteContainerTimestamps(\n existingState.container,\n remoteContainer,\n ),\n containerWriterProjection: null,\n metadataReferencedPrincipals: remoteContainer.metadataReferencedPrincipals,\n };\n}\n\nfunction installUpdatedRemoteContainerState(\n existingState: ContainerState,\n nextState: ContainerState,\n host: RemoteContainerHydrationHost,\n): void {\n const wasLocalOnly = existingState.container.serverCreatedAt == null;\n existingState.container = nextState.container;\n existingState.containerWriterProjection = nextState.containerWriterProjection;\n existingState.metadataReferencedPrincipals =\n nextState.metadataReferencedPrincipals;\n // The snapshot copied the live generation when it was taken; a hint that\n // landed during the persist leaves the live metadata projection dropped.\n if (projectionGeneration(existingState) === projectionGeneration(nextState))\n existingState.metadataWriterProjection = nextState.metadataWriterProjection;\n installContainerMetadataRecord(existingState, nextState.record);\n // A document pass may have deferred while this root was awaiting proof.\n // The first durable remote acknowledgement makes its pending creates runnable.\n if (wasLocalOnly && existingState.container.serverCreatedAt != null)\n host.requestDocumentPriming?.();\n}\n\n/**\n * A clean metadata document is read only when a hint names it, and a hint sent\n * while this client was offline or not yet subscribed is lost. The listing\n * reports the version vector of every update the server holds for the metadata\n * document; when this client's copy does not cover it, an edit is unread. Owe\n * the read before this hydration advances the lane watermark past the row.\n */\nfunction coversMetadataVersion(\n existingState: ContainerState,\n remoteContainer: RemoteContainer,\n): boolean {\n const listed = remoteContainer.metadataVersionVector;\n try {\n // A document with no stored update is covered by any copy.\n if (decodeVersionVector(listed).toJSON().size === 0) return true;\n return satisfiesVersionVector(\n encodeVersionVector(existingState.doc),\n listed,\n );\n } catch {\n // An unreadable version proves nothing: owe the read, which pulls the\n // truth, and let the rest of the page hydrate.\n return false;\n }\n}\n\nasync function oweMetadataReadIfBehind(\n host: RemoteContainerHydrationHost,\n existingState: ContainerState,\n remoteContainer: RemoteContainer,\n): Promise<void> {\n if (!coversMetadataVersion(existingState, remoteContainer)) {\n await host.requestMetadataRevalidation?.({\n advertisedVersionVector: remoteContainer.metadataVersionVector,\n documentId: remoteContainer.metadataDocumentId,\n });\n }\n}\n\nasync function updateExistingRemoteContainerState(input: {\n childIdsByParentId?: ContainerChildIndex | undefined;\n containerIdsWithPendingMetadataUpdates: ReadonlySet<string>;\n containerIdsWithPendingStructuralIntents: ReadonlySet<string>;\n host: RemoteContainerHydrationHost;\n isCurrent?: (() => boolean) | undefined;\n verifiedPlacement?: AccessManifestCheckpoint | undefined;\n existingState: ContainerState;\n remoteContainer: RemoteContainer;\n state: RemoteContainerHydrationState;\n}): Promise<ContainerState | null> {\n const { childIdsByParentId, existingState, host, remoteContainer, state } =\n input;\n const previousParentId = existingState.container.parentId;\n const previousLocalUpdatedAt = existingState.container.localUpdatedAt;\n // A queued, not-yet-synced local move/create owns this container's parent\n // until its intent lane reconciles. Omit the remote parent when the page scan\n // saw one; persistence also rechecks the intent transactionally so a move\n // created after that scan wins. Both paths preserve the durable local clock.\n const hasPendingStructuralIntent =\n input.containerIdsWithPendingStructuralIntents.has(remoteContainer.id);\n const nextParentId = hasPendingStructuralIntent\n ? undefined\n : remoteContainer.parentId;\n const localUpdatedAt = resolveRemoteContainerHydrationLocalUpdatedAt({\n containerIdsWithPendingMetadataUpdates:\n input.containerIdsWithPendingMetadataUpdates,\n hasPendingStructuralIntent,\n previousLocalUpdatedAt,\n remoteContainer,\n });\n await oweMetadataReadIfBehind(host, existingState, remoteContainer);\n const nextState = createUpdatedRemoteContainerState(\n existingState,\n remoteContainer,\n );\n const persistenceResult = await host.persistContainerState(\n nextState,\n {\n accessEpoch: remoteContainer.metadataAccessEpoch,\n accessStateHash: remoteContainer.metadataAccessStateHash,\n documentId: remoteContainer.metadataDocumentId,\n effectiveAccessLevel: remoteContainer.effectiveAccessLevel,\n metadataDocumentId: remoteContainer.metadataDocumentId,\n systemSlot: remoteContainer.systemSlot ?? null,\n organizationId: remoteContainer.organizationId,\n ...(nextParentId !== undefined ? { parentId: nextParentId } : {}),\n },\n false,\n {\n ...remoteContainerHydrationSaveOptions({\n localUpdatedAt,\n remoteContainer,\n }),\n verifiedPlacement: input.verifiedPlacement,\n },\n {\n isCurrent: input.isCurrent,\n preserveDurableStructureWhenPending: true,\n },\n );\n if (persistenceResult.status === \"missing\") {\n // Hydration persists a detached candidate so an in-flight response cannot\n // mutate mapped state before its guards settle. Retire the exact state this\n // hydration read because store-level reference cleanup cannot see the clone.\n removeMissingHydratedContainer({\n childIdsByParentId,\n existingState,\n host,\n previousParentId,\n remoteContainer,\n state,\n });\n return null;\n }\n if (persistenceResult.status !== \"persisted\") return null;\n const { record: nextRecord } = persistenceResult;\n installContainerMetadataRecord(nextState, nextRecord);\n if (input.isCurrent?.() === false) {\n return existingState;\n }\n nextState.container = {\n ...nextState.container,\n metadataDocumentId: remoteContainer.metadataDocumentId,\n organizationId: remoteContainer.organizationId,\n parentId: nextState.container.parentId,\n };\n\n installUpdatedRemoteContainerState(existingState, nextState, host);\n moveIndexedContainerChild(\n childIdsByParentId,\n remoteContainer.id,\n previousParentId,\n existingState.container.parentId,\n );\n await reconcileLocalOnlyRootContainers({\n childIdsByParentId,\n isCurrent: input.isCurrent,\n remoteRootState: existingState,\n requestDocumentPriming: host.requestDocumentPriming,\n state,\n });\n return existingState;\n}\n\ninterface UpsertRemoteContainerStateInput {\n childIdsByParentId?: ContainerChildIndex | undefined;\n containerIdsWithPendingMetadataUpdates: ReadonlySet<string>;\n containerIdsWithPendingStructuralIntents: ReadonlySet<string>;\n host: RemoteContainerHydrationHost;\n expectedHydrationTombstone?: ContainerHydrationTombstone | null | undefined;\n isCurrent?: (() => boolean) | undefined;\n prefetchedProjection?: PrefetchedDestinationProjection | undefined;\n remoteContainer: RemoteContainer;\n state: RemoteContainerHydrationState;\n}\n\n/** A pull can observe the same manifest change as a missed realtime hint. */\nfunction invalidateListedProjectionCaches(\n input: UpsertRemoteContainerStateInput,\n) {\n const held = input.state.containersById.get(input.remoteContainer.id);\n if (\n held &&\n (held.record.accessStateHash !==\n input.remoteContainer.metadataAccessStateHash ||\n (held.container.effectiveAccessLevel ?? \"read\") !==\n input.remoteContainer.effectiveAccessLevel)\n ) {\n // Eviction admits no remote state. Drop citations before verification can\n // advance local checkpoints past those cached container/document paths.\n invalidateContainerProjectionCaches(input.state, [\n input.remoteContainer.id,\n ]);\n }\n}\n\nexport async function upsertRemoteContainerState(\n input: UpsertRemoteContainerStateInput,\n): Promise<ContainerState | null> {\n let expectedPlacementCheckpoint: AccessManifestCheckpoint | undefined;\n const heldState = input.state.containersById.get(input.remoteContainer.id);\n const heldBinding = heldState\n ? heldContainerBinding(heldState)\n : await input.state.persistence.loadHeldContainerBinding(\n input.state.runtime.infra.execSql,\n input.remoteContainer.id,\n );\n const listed = input.remoteContainer;\n // A folder re-homed away from the listed organization never returns there,\n // whatever binding (possibly none, after a reset) the device holds now.\n const supersededListing =\n !listingRepeatsHeldOrdinaryBinding(heldBinding, listed) &&\n (await input.state.persistence.isSupersededContainerBinding(\n input.state.runtime.infra.execSql,\n { containerId: listed.id, organizationId: listed.organizationId },\n ));\n if (input.isCurrent?.() === false) return null;\n invalidateListedProjectionCaches(input);\n const backwardsClock =\n heldState?.container.serverUpdatedAt != null &&\n heldState.container.serverUpdatedAt > listed.updatedAt;\n const verified = await verifyRemoteContainerDestination({\n ...input,\n heldBinding,\n supersededListing,\n refresh: !!input.expectedHydrationTombstone || backwardsClock,\n onVerifiedCheckpoint: (checkpoint) => {\n expectedPlacementCheckpoint = checkpoint;\n },\n });\n if (!verified || input.isCurrent?.() === false) return null;\n // A signed projection from a lagging replica can still repeat our old pin.\n // Retry until it covers the manifest whose listing would be checkpointed.\n if (\n backwardsClock &&\n expectedPlacementCheckpoint?.manifestHash !== listed.metadataAccessStateHash\n )\n return null;\n input = { ...input, remoteContainer: verified };\n const existingState = input.state.containersById.get(\n input.remoteContainer.id,\n );\n const remoteState = await runWithSecurityIncidentReporting(\n input.state.runtime.util.reportSecurityIncident,\n {\n objectId: verified.id,\n objectKind: \"container\",\n operation: \"container.binding.persist\",\n organizationId: verified.organizationId,\n },\n async () =>\n (await rebindReHomedContainer({\n existingState,\n heldBinding,\n isCurrent: input.isCurrent,\n state: input.state,\n verified,\n }))\n ? persistVerifiedRemoteContainer({\n ...input,\n existingState,\n expectedPlacementCheckpoint,\n })\n : null,\n );\n if (!remoteState) {\n return null;\n }\n await reconcileLocalOnlySystemContainers({\n childIdsByParentId: input.childIdsByParentId,\n isCurrent: input.isCurrent,\n requestDocumentPriming: input.host.requestDocumentPriming,\n remoteSystemState: remoteState,\n state: input.state,\n });\n return remoteState;\n}\n\n/**\n * Upsert one listed folder, isolating a rejected proof or binding. The refusal\n * was already reported; returning no state leaves the item unapplied without\n * blocking independent folders.\n */\nexport async function upsertIsolatedRemoteContainerState(\n input: UpsertRemoteContainerStateInput,\n recordIncompleteItem?: (outcome: \"refused\" | \"retry\") => void,\n recordUnavailable?:\n | ((error: ProjectionDependencyUnavailableError) => void)\n | undefined,\n): Promise<ContainerState | null> {\n try {\n return await upsertRemoteContainerState(input);\n } catch (error) {\n if (isKeyingVerificationError(error)) {\n recordIncompleteItem?.(\"refused\");\n return null;\n }\n if (isProjectionVerificationCancelledError(error)) return null;\n if (\n error instanceof ProjectionDependencyUnavailableError &&\n recordUnavailable\n ) {\n recordUnavailable(error);\n recordIncompleteItem?.(\"retry\");\n return null;\n }\n throw error;\n }\n}\n\nfunction persistVerifiedRemoteContainer(\n input: UpsertRemoteContainerStateInput & {\n existingState: ContainerState | undefined;\n expectedPlacementCheckpoint: AccessManifestCheckpoint | undefined;\n },\n): Promise<ContainerState | null> {\n const { existingState, expectedPlacementCheckpoint } = input;\n return existingState\n ? updateExistingRemoteContainerState({\n verifiedPlacement: expectedPlacementCheckpoint,\n childIdsByParentId: input.childIdsByParentId,\n containerIdsWithPendingMetadataUpdates:\n input.containerIdsWithPendingMetadataUpdates,\n containerIdsWithPendingStructuralIntents:\n input.containerIdsWithPendingStructuralIntents,\n existingState,\n host: input.host,\n isCurrent: input.isCurrent,\n remoteContainer: input.remoteContainer,\n state: input.state,\n })\n : insertRemoteContainerState({\n childIdsByParentId: input.childIdsByParentId,\n host: input.host,\n expectedHydrationTombstone: input.expectedHydrationTombstone ?? null,\n expectedPlacementCheckpoint,\n isCurrent: input.isCurrent,\n remoteContainer: input.remoteContainer,\n state: input.state,\n });\n}\n"]}