@pattern-stack/codegen 0.13.0 → 0.14.0

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 (161) hide show
  1. package/dist/{job-orchestrator.protocol-CHOEqBDk.d.ts → job-orchestrator.protocol-CARhMLCO.d.ts} +1 -1
  2. package/dist/runtime/subsystems/analytics/analytics.module.js +6 -2
  3. package/dist/runtime/subsystems/analytics/analytics.module.js.map +1 -1
  4. package/dist/runtime/subsystems/analytics/analytics.tokens.d.ts +0 -11
  5. package/dist/runtime/subsystems/analytics/analytics.tokens.js +6 -2
  6. package/dist/runtime/subsystems/analytics/analytics.tokens.js.map +1 -1
  7. package/dist/runtime/subsystems/analytics/cube-backend.js +6 -2
  8. package/dist/runtime/subsystems/analytics/cube-backend.js.map +1 -1
  9. package/dist/runtime/subsystems/analytics/index.js +6 -2
  10. package/dist/runtime/subsystems/analytics/index.js.map +1 -1
  11. package/dist/runtime/subsystems/auth/auth.module.js +12 -6
  12. package/dist/runtime/subsystems/auth/auth.module.js.map +1 -1
  13. package/dist/runtime/subsystems/auth/auth.tokens.d.ts +0 -28
  14. package/dist/runtime/subsystems/auth/auth.tokens.js +12 -8
  15. package/dist/runtime/subsystems/auth/auth.tokens.js.map +1 -1
  16. package/dist/runtime/subsystems/auth/controllers/auth.controller.js +12 -5
  17. package/dist/runtime/subsystems/auth/controllers/auth.controller.js.map +1 -1
  18. package/dist/runtime/subsystems/auth/index.js +12 -8
  19. package/dist/runtime/subsystems/auth/index.js.map +1 -1
  20. package/dist/runtime/subsystems/auth/middleware/requester-context.js +12 -1
  21. package/dist/runtime/subsystems/auth/middleware/requester-context.js.map +1 -1
  22. package/dist/runtime/subsystems/bridge/bridge-delivery-handler.d.ts +1 -1
  23. package/dist/runtime/subsystems/bridge/bridge-delivery-handler.js +10 -2
  24. package/dist/runtime/subsystems/bridge/bridge-delivery-handler.js.map +1 -1
  25. package/dist/runtime/subsystems/bridge/bridge-outbox-drain-hook.js +10 -2
  26. package/dist/runtime/subsystems/bridge/bridge-outbox-drain-hook.js.map +1 -1
  27. package/dist/runtime/subsystems/bridge/bridge.module.d.ts +1 -1
  28. package/dist/runtime/subsystems/bridge/bridge.module.js +14 -9
  29. package/dist/runtime/subsystems/bridge/bridge.module.js.map +1 -1
  30. package/dist/runtime/subsystems/bridge/bridge.protocol.d.ts +1 -1
  31. package/dist/runtime/subsystems/bridge/event-flow.service.d.ts +1 -1
  32. package/dist/runtime/subsystems/bridge/event-flow.service.js +9 -1
  33. package/dist/runtime/subsystems/bridge/event-flow.service.js.map +1 -1
  34. package/dist/runtime/subsystems/bridge/index.d.ts +1 -1
  35. package/dist/runtime/subsystems/bridge/index.js +14 -9
  36. package/dist/runtime/subsystems/bridge/index.js.map +1 -1
  37. package/dist/runtime/subsystems/cache/cache.drizzle-backend.js +6 -1
  38. package/dist/runtime/subsystems/cache/cache.drizzle-backend.js.map +1 -1
  39. package/dist/runtime/subsystems/cache/cache.memory-backend.js +6 -1
  40. package/dist/runtime/subsystems/cache/cache.memory-backend.js.map +1 -1
  41. package/dist/runtime/subsystems/cache/cache.module.js +6 -2
  42. package/dist/runtime/subsystems/cache/cache.module.js.map +1 -1
  43. package/dist/runtime/subsystems/cache/cache.tokens.d.ts +0 -10
  44. package/dist/runtime/subsystems/cache/cache.tokens.js +6 -2
  45. package/dist/runtime/subsystems/cache/cache.tokens.js.map +1 -1
  46. package/dist/runtime/subsystems/cache/index.js +6 -2
  47. package/dist/runtime/subsystems/cache/index.js.map +1 -1
  48. package/dist/runtime/subsystems/events/event-bus.drizzle-backend.js +5 -0
  49. package/dist/runtime/subsystems/events/event-bus.drizzle-backend.js.map +1 -1
  50. package/dist/runtime/subsystems/events/event-bus.memory-backend.js +5 -0
  51. package/dist/runtime/subsystems/events/event-bus.memory-backend.js.map +1 -1
  52. package/dist/runtime/subsystems/events/event-bus.redis-backend.js +5 -1
  53. package/dist/runtime/subsystems/events/event-bus.redis-backend.js.map +1 -1
  54. package/dist/runtime/subsystems/events/events.module.js +5 -1
  55. package/dist/runtime/subsystems/events/events.module.js.map +1 -1
  56. package/dist/runtime/subsystems/events/events.tokens.d.ts +5 -11
  57. package/dist/runtime/subsystems/events/events.tokens.js +5 -1
  58. package/dist/runtime/subsystems/events/events.tokens.js.map +1 -1
  59. package/dist/runtime/subsystems/events/generated/bus.js +5 -0
  60. package/dist/runtime/subsystems/events/generated/bus.js.map +1 -1
  61. package/dist/runtime/subsystems/events/generated/index.js +5 -0
  62. package/dist/runtime/subsystems/events/generated/index.js.map +1 -1
  63. package/dist/runtime/subsystems/events/index.js +5 -1
  64. package/dist/runtime/subsystems/events/index.js.map +1 -1
  65. package/dist/runtime/subsystems/index.d.ts +3 -3
  66. package/dist/runtime/subsystems/index.js +34 -26
  67. package/dist/runtime/subsystems/index.js.map +1 -1
  68. package/dist/runtime/subsystems/integration/incremental-read.d.ts +35 -8
  69. package/dist/runtime/subsystems/integration/incremental-read.js +9 -6
  70. package/dist/runtime/subsystems/integration/incremental-read.js.map +1 -1
  71. package/dist/runtime/subsystems/integration/index.d.ts +1 -1
  72. package/dist/runtime/subsystems/integration/index.js +9 -6
  73. package/dist/runtime/subsystems/integration/index.js.map +1 -1
  74. package/dist/runtime/subsystems/jobs/bullmq.config.d.ts +0 -9
  75. package/dist/runtime/subsystems/jobs/bullmq.config.js +6 -2
  76. package/dist/runtime/subsystems/jobs/bullmq.config.js.map +1 -1
  77. package/dist/runtime/subsystems/jobs/index.d.ts +1 -1
  78. package/dist/runtime/subsystems/jobs/index.js +13 -9
  79. package/dist/runtime/subsystems/jobs/index.js.map +1 -1
  80. package/dist/runtime/subsystems/jobs/job-handler.base.d.ts +1 -1
  81. package/dist/runtime/subsystems/jobs/job-handler.base.js +5 -1
  82. package/dist/runtime/subsystems/jobs/job-handler.base.js.map +1 -1
  83. package/dist/runtime/subsystems/jobs/job-orchestrator.bullmq-backend.d.ts +1 -1
  84. package/dist/runtime/subsystems/jobs/job-orchestrator.bullmq-backend.js +10 -3
  85. package/dist/runtime/subsystems/jobs/job-orchestrator.bullmq-backend.js.map +1 -1
  86. package/dist/runtime/subsystems/jobs/job-orchestrator.drizzle-backend.d.ts +1 -1
  87. package/dist/runtime/subsystems/jobs/job-orchestrator.drizzle-backend.js +8 -1
  88. package/dist/runtime/subsystems/jobs/job-orchestrator.drizzle-backend.js.map +1 -1
  89. package/dist/runtime/subsystems/jobs/job-orchestrator.memory-backend.d.ts +1 -1
  90. package/dist/runtime/subsystems/jobs/job-orchestrator.memory-backend.js +9 -1
  91. package/dist/runtime/subsystems/jobs/job-orchestrator.memory-backend.js.map +1 -1
  92. package/dist/runtime/subsystems/jobs/job-orchestrator.protocol.d.ts +1 -1
  93. package/dist/runtime/subsystems/jobs/job-run-keyset-cursor.d.ts +1 -1
  94. package/dist/runtime/subsystems/jobs/job-run-service.drizzle-backend.d.ts +1 -1
  95. package/dist/runtime/subsystems/jobs/job-run-service.drizzle-backend.js +8 -2
  96. package/dist/runtime/subsystems/jobs/job-run-service.drizzle-backend.js.map +1 -1
  97. package/dist/runtime/subsystems/jobs/job-run-service.memory-backend.d.ts +1 -1
  98. package/dist/runtime/subsystems/jobs/job-run-service.memory-backend.js +8 -2
  99. package/dist/runtime/subsystems/jobs/job-run-service.memory-backend.js.map +1 -1
  100. package/dist/runtime/subsystems/jobs/job-run-service.protocol.d.ts +1 -1
  101. package/dist/runtime/subsystems/jobs/job-worker.bullmq-backend.d.ts +1 -1
  102. package/dist/runtime/subsystems/jobs/job-worker.bullmq-backend.js +5 -0
  103. package/dist/runtime/subsystems/jobs/job-worker.bullmq-backend.js.map +1 -1
  104. package/dist/runtime/subsystems/jobs/job-worker.d.ts +1 -1
  105. package/dist/runtime/subsystems/jobs/job-worker.js +10 -4
  106. package/dist/runtime/subsystems/jobs/job-worker.js.map +1 -1
  107. package/dist/runtime/subsystems/jobs/job-worker.module.d.ts +5 -2
  108. package/dist/runtime/subsystems/jobs/job-worker.module.js +13 -8
  109. package/dist/runtime/subsystems/jobs/job-worker.module.js.map +1 -1
  110. package/dist/runtime/subsystems/jobs/jobs-domain.module.js +11 -6
  111. package/dist/runtime/subsystems/jobs/jobs-domain.module.js.map +1 -1
  112. package/dist/runtime/subsystems/jobs/jobs-domain.tokens.d.ts +0 -11
  113. package/dist/runtime/subsystems/jobs/jobs-domain.tokens.js +8 -4
  114. package/dist/runtime/subsystems/jobs/jobs-domain.tokens.js.map +1 -1
  115. package/dist/runtime/subsystems/jobs/jobs-errors.d.ts +1 -1
  116. package/dist/runtime/subsystems/observability/index.d.ts +1 -1
  117. package/dist/runtime/subsystems/observability/index.js +9 -1
  118. package/dist/runtime/subsystems/observability/index.js.map +1 -1
  119. package/dist/runtime/subsystems/observability/observability.module.js +9 -1
  120. package/dist/runtime/subsystems/observability/observability.module.js.map +1 -1
  121. package/dist/runtime/subsystems/observability/observability.protocol.d.ts +1 -1
  122. package/dist/runtime/subsystems/observability/observability.service.d.ts +1 -1
  123. package/dist/runtime/subsystems/observability/observability.service.js +9 -1
  124. package/dist/runtime/subsystems/observability/observability.service.js.map +1 -1
  125. package/dist/runtime/subsystems/observability/reporters/bridge-metrics.reporter.d.ts +1 -1
  126. package/dist/runtime/subsystems/observability/reporters/index.d.ts +1 -1
  127. package/dist/runtime/subsystems/storage/index.js +5 -1
  128. package/dist/runtime/subsystems/storage/index.js.map +1 -1
  129. package/dist/runtime/subsystems/storage/storage.module.js +5 -1
  130. package/dist/runtime/subsystems/storage/storage.module.js.map +1 -1
  131. package/dist/runtime/subsystems/storage/storage.tokens.d.ts +0 -8
  132. package/dist/runtime/subsystems/storage/storage.tokens.js +5 -1
  133. package/dist/runtime/subsystems/storage/storage.tokens.js.map +1 -1
  134. package/dist/runtime/subsystems/token-key.d.ts +7 -0
  135. package/dist/runtime/subsystems/token-key.js +8 -0
  136. package/dist/runtime/subsystems/token-key.js.map +1 -0
  137. package/dist/src/cli/index.js +362 -233
  138. package/dist/src/cli/index.js.map +1 -1
  139. package/package.json +5 -1
  140. package/runtime/subsystems/analytics/analytics.tokens.ts +6 -2
  141. package/runtime/subsystems/auth/auth.tokens.ts +15 -8
  142. package/runtime/subsystems/cache/cache.tokens.ts +7 -2
  143. package/runtime/subsystems/events/events.tokens.ts +8 -1
  144. package/runtime/subsystems/index.ts +6 -1
  145. package/runtime/subsystems/integration/incremental-read.ts +43 -9
  146. package/runtime/subsystems/integration/index.ts +1 -0
  147. package/runtime/subsystems/jobs/bullmq.config.ts +5 -2
  148. package/runtime/subsystems/jobs/job-handler.base.ts +6 -1
  149. package/runtime/subsystems/jobs/job-worker.module.ts +5 -1
  150. package/runtime/subsystems/jobs/job-worker.ts +4 -1
  151. package/runtime/subsystems/jobs/jobs-domain.tokens.ts +10 -7
  152. package/runtime/subsystems/storage/storage.tokens.ts +6 -1
  153. package/runtime/subsystems/token-key.ts +7 -0
  154. package/src/config/runtime-mode.mjs +82 -0
  155. package/templates/entity/new/backend/modules/core/integration-source.ejs.t +3 -2
  156. package/templates/entity/new/clean-lite-ps/controller.ejs.t +1 -1
  157. package/templates/entity/new/clean-lite-ps/module.ejs.t +1 -1
  158. package/templates/entity/new/clean-lite-ps/prompt-extension.js +8 -2
  159. package/templates/entity/new/clean-lite-ps/repository.ejs.t +4 -4
  160. package/templates/entity/new/clean-lite-ps/service.ejs.t +4 -4
  161. package/templates/entity/new/prompt.js +49 -10
@@ -1,4 +1,4 @@
1
- import { IChangeSource, ChangeSource, IntegrationSubscriptionView, Change } from './integration-change-source.protocol.js';
1
+ import { IntegrationSubscriptionView, IChangeSource, ChangeSource, Change } from './integration-change-source.protocol.js';
2
2
 
3
3
  /**
4
4
  * Integration subsystem — `IncrementalRead<T, F>` + `RandomRead<T>` capability
@@ -73,6 +73,25 @@ interface ReadRequest<F = unknown> {
73
73
  readonly filter?: F;
74
74
  readonly pageSize?: number;
75
75
  }
76
+ /**
77
+ * Per-run context threaded from `listChanges` into the vendor read body (R5).
78
+ *
79
+ * Carries the `subscription` framing the run so `enumerate`/`hydrate` can resolve
80
+ * **per-connection credentials** (and raw-landing keys) from
81
+ * `subscription.externalRef` — the gap a multi-account consumer surfaced: a
82
+ * singleton change source cannot hold connection-scoped auth, and before R5 the
83
+ * base forwarded the subscription only into `filterFor`, never into the fetch.
84
+ *
85
+ * Optional throughout (the core contract): a direct `read()` / `get()` call — the
86
+ * query surface's "fill one record on click" — may omit it. An adapter that needs
87
+ * per-connection auth reads `ctx?.subscription?.externalRef` and asserts its
88
+ * presence; a provider-level-auth adapter ignores it.
89
+ */
90
+ interface ReadContext {
91
+ /** The subscription framing this run; `externalRef` is the upstream scope /
92
+ * connection id the adapter resolves credentials + raw-landing keys from. */
93
+ readonly subscription?: IntegrationSubscriptionView;
94
+ }
76
95
  /**
77
96
  * The `read()`-side envelope: canonical record + the raw vendor payload it came
78
97
  * from + the originating external id + the per-ref cursor.
@@ -94,7 +113,7 @@ interface SourcedRecord<T> {
94
113
  * hydration, and cursor emission are the providing base's concern.
95
114
  */
96
115
  interface IncrementalRead<T, F = unknown> {
97
- read(req: ReadRequest<F>): AsyncIterable<SourcedRecord<T>>;
116
+ read(req: ReadRequest<F>, ctx?: ReadContext): AsyncIterable<SourcedRecord<T>>;
98
117
  }
99
118
  /**
100
119
  * Single-record read by external id — the "fill on click" atom. Provided for
@@ -103,7 +122,7 @@ interface IncrementalRead<T, F = unknown> {
103
122
  * surface.
104
123
  */
105
124
  interface RandomRead<T> {
106
- get(id: string): Promise<T | null>;
125
+ get(id: string, ctx?: ReadContext): Promise<T | null>;
107
126
  }
108
127
  /**
109
128
  * Map `ids` through `fn` with at most `limit` concurrent in-flight calls,
@@ -163,16 +182,24 @@ declare abstract class IncrementalReadBase<T, F = unknown, M = Record<string, un
163
182
  * `pageSize` (from `ReadRequest`) is the adapter's requested vendor page size
164
183
  * — also the atomic-cursor backfill blast-radius bound (§ `cursorDivisible`).
165
184
  * Honor it as a hint; vendors that cap page size clamp it.
185
+ *
186
+ * `ctx?.subscription` (R5) carries the run's subscription, so a per-connection
187
+ * adapter resolves credentials / upstream scope from `externalRef` here; absent
188
+ * on a direct `read()` with no run subscription.
166
189
  */
167
- protected abstract enumerate(mode: ReadMode, filter?: F, pageSize?: number): AsyncIterable<Ref<M>[]>;
190
+ protected abstract enumerate(mode: ReadMode, filter?: F, pageSize?: number, ctx?: ReadContext): AsyncIterable<Ref<M>[]>;
168
191
  /**
169
192
  * Fetch raw payloads for `ids`, keyed by id. MUST be miss-tolerant: omit (or
170
193
  * map to `null`) any id that 404s mid-run rather than throwing or shifting
171
194
  * alignment. Write it over `mapConcurrent(ids, (id) => this.fetchOne(id),
172
195
  * this.hydrateConcurrency)`; override with a real `/batch` call or a
173
196
  * passthrough (full-object list) where the vendor allows.
197
+ *
198
+ * `ctx?.subscription` (R5) carries the run's subscription for per-connection
199
+ * credential resolution (the fetch is where the vendor call happens) and is the
200
+ * natural place to land raw payloads keyed by `subscription.id`.
174
201
  */
175
- protected abstract hydrate(ids: string[]): Promise<Map<string, unknown>>;
202
+ protected abstract hydrate(ids: string[], ctx?: ReadContext): Promise<Map<string, unknown>>;
176
203
  /** Provider payload → canonical record. Return `null` to drop a record. */
177
204
  protected abstract toCanonical(raw: unknown): T | null;
178
205
  /** Pre-hydrate predicate over the cheap ref (preferred — avoids hydration). */
@@ -191,13 +218,13 @@ declare abstract class IncrementalReadBase<T, F = unknown, M = Record<string, un
191
218
  * adapter cannot hydrate-then-discard. A hydrate miss (deleted mid-run) is
192
219
  * skipped, never fabricated.
193
220
  */
194
- read(req: ReadRequest<F>): AsyncIterable<SourcedRecord<T>>;
221
+ read(req: ReadRequest<F>, ctx?: ReadContext): AsyncIterable<SourcedRecord<T>>;
195
222
  /**
196
223
  * `RandomRead<T>` — single-record read, provided for free as
197
224
  * `toCanonical ∘ hydrate([id])`. Reuses the adapter's batched fetch + miss
198
225
  * tolerance; returns `null` for a missing or undecodable record.
199
226
  */
200
- get(id: string): Promise<T | null>;
227
+ get(id: string, ctx?: ReadContext): Promise<T | null>;
201
228
  /**
202
229
  * `IChangeSource<T>` adaptation. Maps the orchestrator's by-value cursor to a
203
230
  * `ReadMode` (`null` → `full` backfill, else `delta`), streams `read()`, and
@@ -218,4 +245,4 @@ declare abstract class IncrementalReadBase<T, F = unknown, M = Record<string, un
218
245
  private toChange;
219
246
  }
220
247
 
221
- export { type IncrementalRead, IncrementalReadBase, type RandomRead, type ReadMode, type ReadRequest, type Ref, type SourcedRecord, mapConcurrent };
248
+ export { type IncrementalRead, IncrementalReadBase, type RandomRead, type ReadContext, type ReadMode, type ReadRequest, type Ref, type SourcedRecord, mapConcurrent };
@@ -70,11 +70,14 @@ var IncrementalReadBase = class {
70
70
  * adapter cannot hydrate-then-discard. A hydrate miss (deleted mid-run) is
71
71
  * skipped, never fabricated.
72
72
  */
73
- async *read(req) {
74
- for await (const refPage of this.enumerate(req.mode, req.filter, req.pageSize)) {
73
+ async *read(req, ctx) {
74
+ for await (const refPage of this.enumerate(req.mode, req.filter, req.pageSize, ctx)) {
75
75
  const kept = refPage.filter((ref) => this.matchesRef(ref, req.filter));
76
76
  if (kept.length === 0) continue;
77
- const raws = await this.hydrate(kept.map((ref) => ref.externalId));
77
+ const raws = await this.hydrate(
78
+ kept.map((ref) => ref.externalId),
79
+ ctx
80
+ );
78
81
  for (const ref of kept) {
79
82
  const raw = raws.get(ref.externalId);
80
83
  if (raw === void 0 || raw === null) continue;
@@ -90,8 +93,8 @@ var IncrementalReadBase = class {
90
93
  * `toCanonical ∘ hydrate([id])`. Reuses the adapter's batched fetch + miss
91
94
  * tolerance; returns `null` for a missing or undecodable record.
92
95
  */
93
- async get(id) {
94
- const raws = await this.hydrate([id]);
96
+ async get(id, ctx) {
97
+ const raws = await this.hydrate([id], ctx);
95
98
  const raw = raws.get(id);
96
99
  if (raw === void 0 || raw === null) return null;
97
100
  return this.toCanonical(raw);
@@ -114,7 +117,7 @@ var IncrementalReadBase = class {
114
117
  async *listChanges(subscription, cursor) {
115
118
  const mode = cursor === null || cursor === void 0 ? { kind: "full" } : { kind: "delta", cursor };
116
119
  const filter = this.filterFor(subscription);
117
- const stream = this.read({ mode, filter });
120
+ const stream = this.read({ mode, filter }, { subscription });
118
121
  if (this.cursorDivisible) {
119
122
  for await (const sourced of stream) {
120
123
  yield this.toChange(sourced, sourced.cursor);
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../runtime/subsystems/integration/incremental-read.ts"],"sourcesContent":["/**\n * Integration subsystem — `IncrementalRead<T, F>` + `RandomRead<T>` capability\n * and the providing `IncrementalReadBase<T, F, M>` (RFC-0003 R1).\n *\n * The universal read primitive. Where `IChangeSource.listChanges` is the\n * *transport* contract (stream `Change<T>`, orchestrator owns cursor lifecycle),\n * this base owns *how the body that produces those changes is written* — the\n * level the bare `changeSources = {}` author-seam left unstructured.\n *\n * The read decomposes into two composable verbs the adapter supplies:\n *\n * - `enumerate(mode, filter) → AsyncIterable<Ref<M>[]>` — the cheap delta /\n * backfill walk; streams pages of lightweight refs (id + per-ref cursor +\n * filterable metadata). LAZY: pull-driven so hydrate backpressures it.\n * - `hydrate(ids) → Map<id, raw>` — the expensive fetch-by-id, batched; where\n * bounded concurrency / a vendor `/batch` endpoint lives. Keyed and\n * miss-tolerant (a mid-run 404 cannot shift alignment).\n * - `toCanonical(raw) → T | null` — provider payload → canonical record.\n *\n * The base PROVIDES the orchestration: drain enumerate, **filter before\n * hydrate** (structural — an adapter physically cannot hydrate-then-discard),\n * keyed pairing, per-ref cursor emission, and the `IChangeSource.listChanges`\n * adaptation. It also provides `RandomRead.get()` for free as\n * `toCanonical ∘ hydrate([id])` — so every incremental adapter is a\n * single-record reader (the \"list cheaply, fill on click\" query-surface need)\n * without extra code.\n *\n * The shape generalizes dealbrain's proven HubSpot `listSince` (streams, pushes\n * the filter server-side, carries a per-record cursor) to vendors whose list\n * returns id-stubs (Gmail) or nested resources (Meet). Calendar-style\n * full-object lists override `hydrate` as a passthrough.\n *\n * See RFC-0003 (Track D round-3), ADR-033 (`detection:` config), and\n * `poll-change-source.ts` (the sibling primitive this composes beside).\n */\n\nimport type {\n Change,\n ChangeSource,\n IChangeSource,\n IntegrationSubscriptionView,\n} from './integration-change-source.protocol';\n\n// ============================================================================\n// Capability shapes\n// ============================================================================\n\n/**\n * How a read walks the upstream. Modes are values, not verbs (swe-brain\n * ADR-0003: mode ≠ capability) — one `read()` verb dispatches on these.\n *\n * - `delta` — incremental walk from a persisted cursor.\n * - `full` — cursorless backfill (optionally bounded by `since`).\n * - `reconcile` — gap-repair: re-fetch a known id set the cursor skipped\n * (the repair pass for the silent-tail-skip + #414-style\n * multi-provider divergence).\n */\nexport type ReadMode =\n | { readonly kind: 'delta'; readonly cursor: unknown }\n | { readonly kind: 'full'; readonly since?: Date }\n | { readonly kind: 'reconcile'; readonly knownIds: readonly string[] };\n\n/**\n * A cheap ref from the enumerate pass: identity + per-ref cursor + metadata to\n * filter or display on. `cursor` is the position AS OF this ref — see\n * `IncrementalReadBase.cursorDivisible` (R2) for when it may be checkpointed\n * mid-walk versus withheld until a safe boundary.\n */\nexport interface Ref<M = Record<string, unknown>> {\n readonly externalId: string;\n readonly cursor: unknown;\n readonly meta: M;\n}\n\n/** A read request: the mode, an optional adapter-typed filter, and page size. */\nexport interface ReadRequest<F = unknown> {\n readonly mode: ReadMode;\n readonly filter?: F;\n readonly pageSize?: number;\n}\n\n/**\n * The `read()`-side envelope: canonical record + the raw vendor payload it came\n * from + the originating external id + the per-ref cursor.\n *\n * Distinct from the runtime's transport envelope `Change<T>`\n * (operation/externalId/cursor/source). The relationship is one-directional:\n * `listChanges()` adapts `read()` → `Change<T>` (dropping `raw`, stamping\n * `operation`). `read()` keeps `raw` and `externalId` so a query surface can\n * re-project without a second fetch.\n */\nexport interface SourcedRecord<T> {\n readonly externalId: string;\n readonly record: T;\n readonly raw: unknown;\n readonly cursor: unknown;\n}\n\n/**\n * The universal read capability — one public verb that streams. Filtering,\n * hydration, and cursor emission are the providing base's concern.\n */\nexport interface IncrementalRead<T, F = unknown> {\n read(req: ReadRequest<F>): AsyncIterable<SourcedRecord<T>>;\n}\n\n/**\n * Single-record read by external id — the \"fill on click\" atom. Provided for\n * free by `IncrementalReadBase` (composes `hydrate` + `toCanonical`); declared\n * as its own capability so consumers can depend on it without the streaming\n * surface.\n */\nexport interface RandomRead<T> {\n get(id: string): Promise<T | null>;\n}\n\n// ============================================================================\n// Bounded-parallel map helper\n// ============================================================================\n\n/**\n * Map `ids` through `fn` with at most `limit` concurrent in-flight calls,\n * collecting results keyed by id. The workhorse for writing a batched\n * `hydrate` over a single-id fetch without serial N+1 latency.\n */\nexport async function mapConcurrent<R>(\n ids: readonly string[],\n fn: (id: string) => Promise<R>,\n limit: number,\n): Promise<Map<string, R>> {\n const out = new Map<string, R>();\n if (ids.length === 0) return out;\n const width = Math.max(1, Math.min(limit, ids.length));\n let next = 0;\n const worker = async (): Promise<void> => {\n while (next < ids.length) {\n const idx = next++;\n const id = ids[idx]!;\n out.set(id, await fn(id));\n }\n };\n await Promise.all(Array.from({ length: width }, worker));\n return out;\n}\n\n// ============================================================================\n// IncrementalReadBase\n// ============================================================================\n\n/**\n * Providing base for the read capability. A subclass fills exactly three vendor\n * methods — `enumerate`, `hydrate`, `toCanonical` — and gets a streaming,\n * filter-before-hydrate, miss-tolerant `IncrementalRead<T, F>` +\n * `IChangeSource<T>` + `RandomRead<T>`.\n *\n * Type params: `T` canonical record, `F` adapter-typed filter, `M` per-ref\n * metadata (defaults to an untyped bag — surface packages supply a domain `M`).\n */\nexport abstract class IncrementalReadBase<T, F = unknown, M = Record<string, unknown>>\n implements IncrementalRead<T, F>, IChangeSource<T>, RandomRead<T>\n{\n /** Human label for run logs — e.g. `'google-mail-email'`. */\n abstract readonly label: string;\n\n /**\n * Whether the vendor takes the request predicate server-side. Declared, not\n * enforced here — surfaced into the emission manifest (R3) so the falsifier\n * suite (R4) can record which adapters filter post-hydrate. `false` is the\n * honest floor (e.g. Gmail without `q=`), handled via `matchesRecord`.\n */\n protected readonly filterPushdown: boolean = false;\n\n /** Max concurrent in-flight calls for a `mapConcurrent`-built `hydrate`. */\n protected readonly hydrateConcurrency: number = 10;\n\n /** `Change<T>.source` provenance stamped by `listChanges`. */\n protected readonly changeSource: ChangeSource = 'poll';\n\n /**\n * Whether this source's cursor strategy is divisible (RFC-0003 §3). When\n * `true` (default — sortable watermarks like `systemModstamp`/`timestamp`/\n * `replayId`), `listChanges` emits each record's per-ref cursor, so the\n * orchestrator may checkpoint mid-walk and a crash resumes from the last\n * delivered ref.\n *\n * When `false` (atomic opaque tokens — Gmail `historyId`, Calendar\n * `syncToken`), `listChanges` WITHHOLDS per-ref cursors and emits the\n * end-of-walk token only on the final record, so the orchestrator's\n * persist-last-yielded lifecycle can never persist an unresumable mid-walk\n * token. The cost is blast-radius: an interrupted atomic run resumes\n * all-or-nothing from the prior persisted token. For atomic *backfills* that\n * radius is the whole enumerate walk — bound it with `ReadRequest.pageSize`\n * (smaller pages ⇒ shorter walks per run). Per-page atomic checkpointing is a\n * future refinement; R2 gates at end-of-walk.\n *\n * Codegen (R3) sets this from the strategy kind via `isDivisibleCursor`.\n */\n protected readonly cursorDivisible: boolean = true;\n\n // ---- SUPPLIED by the adapter (the irreducible vendor seam) ----\n\n /**\n * The cheap walk. Streams pages of refs; LAZY so `hydrate` backpressures it\n * (one page hydrated before the next is pulled). Mode-dispatch lives here:\n * `delta` resumes from `mode.cursor`, `full` walks from the top, `reconcile`\n * re-fetches `mode.knownIds`.\n *\n * `pageSize` (from `ReadRequest`) is the adapter's requested vendor page size\n * — also the atomic-cursor backfill blast-radius bound (§ `cursorDivisible`).\n * Honor it as a hint; vendors that cap page size clamp it.\n */\n protected abstract enumerate(\n mode: ReadMode,\n filter?: F,\n pageSize?: number,\n ): AsyncIterable<Ref<M>[]>;\n\n /**\n * Fetch raw payloads for `ids`, keyed by id. MUST be miss-tolerant: omit (or\n * map to `null`) any id that 404s mid-run rather than throwing or shifting\n * alignment. Write it over `mapConcurrent(ids, (id) => this.fetchOne(id),\n * this.hydrateConcurrency)`; override with a real `/batch` call or a\n * passthrough (full-object list) where the vendor allows.\n */\n protected abstract hydrate(ids: string[]): Promise<Map<string, unknown>>;\n\n /** Provider payload → canonical record. Return `null` to drop a record. */\n protected abstract toCanonical(raw: unknown): T | null;\n\n // ---- Optional filter hooks — exactly one is live per `filterPushdown` ----\n\n /** Pre-hydrate predicate over the cheap ref (preferred — avoids hydration). */\n protected matchesRef(_ref: Ref<M>, _filter?: F): boolean {\n return true;\n }\n\n /** Post-hydrate predicate over the canonical record (the no-pushdown floor). */\n protected matchesRecord(_record: T, _filter?: F): boolean {\n return true;\n }\n\n /**\n * Resolve the filter for a subscription when adapting to `listChanges`\n * (which has no filter argument). Defaults to none; codegen wiring (R3)\n * overrides this to thread `DetectionConfig.filters`.\n */\n protected filterFor(_subscription: IntegrationSubscriptionView): F | undefined {\n return undefined;\n }\n\n // ---- PROVIDED by the base ----\n\n /**\n * Stream canonical records for a request. Filter is applied BEFORE hydrate\n * (structural: a kept ref is hydrated, a rejected one never is), so an\n * adapter cannot hydrate-then-discard. A hydrate miss (deleted mid-run) is\n * skipped, never fabricated.\n */\n async *read(req: ReadRequest<F>): AsyncIterable<SourcedRecord<T>> {\n for await (const refPage of this.enumerate(req.mode, req.filter, req.pageSize)) {\n const kept = refPage.filter((ref) => this.matchesRef(ref, req.filter));\n if (kept.length === 0) continue;\n const raws = await this.hydrate(kept.map((ref) => ref.externalId));\n for (const ref of kept) {\n const raw = raws.get(ref.externalId);\n if (raw === undefined || raw === null) continue; // deleted mid-run → skip\n const record = this.toCanonical(raw);\n if (record !== null && this.matchesRecord(record, req.filter)) {\n yield { externalId: ref.externalId, record, raw, cursor: ref.cursor };\n }\n }\n }\n }\n\n /**\n * `RandomRead<T>` — single-record read, provided for free as\n * `toCanonical ∘ hydrate([id])`. Reuses the adapter's batched fetch + miss\n * tolerance; returns `null` for a missing or undecodable record.\n */\n async get(id: string): Promise<T | null> {\n const raws = await this.hydrate([id]);\n const raw = raws.get(id);\n if (raw === undefined || raw === null) return null;\n return this.toCanonical(raw);\n }\n\n /**\n * `IChangeSource<T>` adaptation. Maps the orchestrator's by-value cursor to a\n * `ReadMode` (`null` → `full` backfill, else `delta`), streams `read()`, and\n * stamps each `SourcedRecord` into a `Change<T>`. All records surface as\n * `'updated'`; the orchestrator's diff stage classifies create-vs-update and\n * deletes arrive as tombstone refs (`toCanonical` may flag them).\n *\n * Cursor emission honors `cursorDivisible` (RFC-0003 §3). Divisible: each\n * record carries its own per-ref cursor. Atomic: per-ref cursors are withheld\n * (`undefined`, which the orchestrator skips persisting) and the end-of-walk\n * token rides only on the final record — so a mid-walk crash never persists\n * an unresumable token. If an atomic run yields no surviving records, no\n * cursor is persisted and the next run re-reads the same (empty) delta — a\n * bounded inefficiency, never data loss.\n */\n async *listChanges(\n subscription: IntegrationSubscriptionView,\n cursor: unknown | null,\n ): AsyncIterable<Change<T>> {\n const mode: ReadMode =\n cursor === null || cursor === undefined\n ? { kind: 'full' }\n : { kind: 'delta', cursor };\n const filter = this.filterFor(subscription);\n const stream = this.read({ mode, filter });\n\n if (this.cursorDivisible) {\n for await (const sourced of stream) {\n yield this.toChange(sourced, sourced.cursor);\n }\n return;\n }\n\n // Atomic: one-record lookahead. Emit every record but the last with a\n // withheld (`undefined`) cursor; the last record carries the end-of-walk\n // token. Contract: an atomic adapter stamps the (single, shared) end-of-walk\n // token onto its refs' `cursor` — so whichever record survives last carries\n // it. The base emits a real cursor exactly once, on that final record, so the\n // orchestrator can never persist a mid-walk value. If zero records survive,\n // nothing is persisted (next run re-reads the delta — bounded, never lossy).\n let prev: SourcedRecord<T> | null = null;\n for await (const sourced of stream) {\n if (prev !== null) yield this.toChange(prev, undefined);\n prev = sourced;\n }\n if (prev !== null) yield this.toChange(prev, prev.cursor);\n }\n\n /** Stamp a `SourcedRecord` into a `Change<T>` with an explicit emitted cursor. */\n private toChange(sourced: SourcedRecord<T>, cursor: unknown): Change<T> {\n return {\n externalId: sourced.externalId,\n operation: 'updated',\n record: sourced.record,\n cursor,\n source: this.chang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AAM,KAAK,SAAS,MAAM,KAAK,MAAM;AAAA,EAC1D;AAAA;AAAA,EAGQ,SAAS,SAA2B,QAA4B;AACtE,WAAO;AAAA,MACL,YAAY,QAAQ;AAAA,MACpB,WAAW;AAAA,MACX,QAAQ,QAAQ;AAAA,MAChB;AAAA,MACA,QAAQ,KAAK;AAAA,IACf;AAAA,EACF;AACF;","names":[]}
1
+ {"version":3,"sources":["../../../../runtime/subsystems/integration/incremental-read.ts"],"sourcesContent":["/**\n * Integration subsystem — `IncrementalRead<T, F>` + `RandomRead<T>` capability\n * and the providing `IncrementalReadBase<T, F, M>` (RFC-0003 R1).\n *\n * The universal read primitive. Where `IChangeSource.listChanges` is the\n * *transport* contract (stream `Change<T>`, orchestrator owns cursor lifecycle),\n * this base owns *how the body that produces those changes is written* — the\n * level the bare `changeSources = {}` author-seam left unstructured.\n *\n * The read decomposes into two composable verbs the adapter supplies:\n *\n * - `enumerate(mode, filter) → AsyncIterable<Ref<M>[]>` — the cheap delta /\n * backfill walk; streams pages of lightweight refs (id + per-ref cursor +\n * filterable metadata). LAZY: pull-driven so hydrate backpressures it.\n * - `hydrate(ids) → Map<id, raw>` — the expensive fetch-by-id, batched; where\n * bounded concurrency / a vendor `/batch` endpoint lives. Keyed and\n * miss-tolerant (a mid-run 404 cannot shift alignment).\n * - `toCanonical(raw) → T | null` — provider payload → canonical record.\n *\n * The base PROVIDES the orchestration: drain enumerate, **filter before\n * hydrate** (structural — an adapter physically cannot hydrate-then-discard),\n * keyed pairing, per-ref cursor emission, and the `IChangeSource.listChanges`\n * adaptation. It also provides `RandomRead.get()` for free as\n * `toCanonical ∘ hydrate([id])` — so every incremental adapter is a\n * single-record reader (the \"list cheaply, fill on click\" query-surface need)\n * without extra code.\n *\n * The shape generalizes dealbrain's proven HubSpot `listSince` (streams, pushes\n * the filter server-side, carries a per-record cursor) to vendors whose list\n * returns id-stubs (Gmail) or nested resources (Meet). Calendar-style\n * full-object lists override `hydrate` as a passthrough.\n *\n * See RFC-0003 (Track D round-3), ADR-033 (`detection:` config), and\n * `poll-change-source.ts` (the sibling primitive this composes beside).\n */\n\nimport type {\n Change,\n ChangeSource,\n IChangeSource,\n IntegrationSubscriptionView,\n} from './integration-change-source.protocol';\n\n// ============================================================================\n// Capability shapes\n// ============================================================================\n\n/**\n * How a read walks the upstream. Modes are values, not verbs (swe-brain\n * ADR-0003: mode ≠ capability) — one `read()` verb dispatches on these.\n *\n * - `delta` — incremental walk from a persisted cursor.\n * - `full` — cursorless backfill (optionally bounded by `since`).\n * - `reconcile` — gap-repair: re-fetch a known id set the cursor skipped\n * (the repair pass for the silent-tail-skip + #414-style\n * multi-provider divergence).\n */\nexport type ReadMode =\n | { readonly kind: 'delta'; readonly cursor: unknown }\n | { readonly kind: 'full'; readonly since?: Date }\n | { readonly kind: 'reconcile'; readonly knownIds: readonly string[] };\n\n/**\n * A cheap ref from the enumerate pass: identity + per-ref cursor + metadata to\n * filter or display on. `cursor` is the position AS OF this ref — see\n * `IncrementalReadBase.cursorDivisible` (R2) for when it may be checkpointed\n * mid-walk versus withheld until a safe boundary.\n */\nexport interface Ref<M = Record<string, unknown>> {\n readonly externalId: string;\n readonly cursor: unknown;\n readonly meta: M;\n}\n\n/** A read request: the mode, an optional adapter-typed filter, and page size. */\nexport interface ReadRequest<F = unknown> {\n readonly mode: ReadMode;\n readonly filter?: F;\n readonly pageSize?: number;\n}\n\n/**\n * Per-run context threaded from `listChanges` into the vendor read body (R5).\n *\n * Carries the `subscription` framing the run so `enumerate`/`hydrate` can resolve\n * **per-connection credentials** (and raw-landing keys) from\n * `subscription.externalRef` — the gap a multi-account consumer surfaced: a\n * singleton change source cannot hold connection-scoped auth, and before R5 the\n * base forwarded the subscription only into `filterFor`, never into the fetch.\n *\n * Optional throughout (the core contract): a direct `read()` / `get()` call — the\n * query surface's \"fill one record on click\" — may omit it. An adapter that needs\n * per-connection auth reads `ctx?.subscription?.externalRef` and asserts its\n * presence; a provider-level-auth adapter ignores it.\n */\nexport interface ReadContext {\n /** The subscription framing this run; `externalRef` is the upstream scope /\n * connection id the adapter resolves credentials + raw-landing keys from. */\n readonly subscription?: IntegrationSubscriptionView;\n}\n\n/**\n * The `read()`-side envelope: canonical record + the raw vendor payload it came\n * from + the originating external id + the per-ref cursor.\n *\n * Distinct from the runtime's transport envelope `Change<T>`\n * (operation/externalId/cursor/source). The relationship is one-directional:\n * `listChanges()` adapts `read()` → `Change<T>` (dropping `raw`, stamping\n * `operation`). `read()` keeps `raw` and `externalId` so a query surface can\n * re-project without a second fetch.\n */\nexport interface SourcedRecord<T> {\n readonly externalId: string;\n readonly record: T;\n readonly raw: unknown;\n readonly cursor: unknown;\n}\n\n/**\n * The universal read capability — one public verb that streams. Filtering,\n * hydration, and cursor emission are the providing base's concern.\n */\nexport interface IncrementalRead<T, F = unknown> {\n read(req: ReadRequest<F>, ctx?: ReadContext): AsyncIterable<SourcedRecord<T>>;\n}\n\n/**\n * Single-record read by external id — the \"fill on click\" atom. Provided for\n * free by `IncrementalReadBase` (composes `hydrate` + `toCanonical`); declared\n * as its own capability so consumers can depend on it without the streaming\n * surface.\n */\nexport interface RandomRead<T> {\n get(id: string, ctx?: ReadContext): Promise<T | null>;\n}\n\n// ============================================================================\n// Bounded-parallel map helper\n// ============================================================================\n\n/**\n * Map `ids` through `fn` with at most `limit` concurrent in-flight calls,\n * collecting results keyed by id. The workhorse for writing a batched\n * `hydrate` over a single-id fetch without serial N+1 latency.\n */\nexport async function mapConcurrent<R>(\n ids: readonly string[],\n fn: (id: string) => Promise<R>,\n limit: number,\n): Promise<Map<string, R>> {\n const out = new Map<string, R>();\n if (ids.length === 0) return out;\n const width = Math.max(1, Math.min(limit, ids.length));\n let next = 0;\n const worker = async (): Promise<void> => {\n while (next < ids.length) {\n const idx = next++;\n const id = ids[idx]!;\n out.set(id, await fn(id));\n }\n };\n await Promise.all(Array.from({ length: width }, worker));\n return out;\n}\n\n// ============================================================================\n// IncrementalReadBase\n// ============================================================================\n\n/**\n * Providing base for the read capability. A subclass fills exactly three vendor\n * methods — `enumerate`, `hydrate`, `toCanonical` — and gets a streaming,\n * filter-before-hydrate, miss-tolerant `IncrementalRead<T, F>` +\n * `IChangeSource<T>` + `RandomRead<T>`.\n *\n * Type params: `T` canonical record, `F` adapter-typed filter, `M` per-ref\n * metadata (defaults to an untyped bag — surface packages supply a domain `M`).\n */\nexport abstract class IncrementalReadBase<T, F = unknown, M = Record<string, unknown>>\n implements IncrementalRead<T, F>, IChangeSource<T>, RandomRead<T>\n{\n /** Human label for run logs — e.g. `'google-mail-email'`. */\n abstract readonly label: string;\n\n /**\n * Whether the vendor takes the request predicate server-side. Declared, not\n * enforced here — surfaced into the emission manifest (R3) so the falsifier\n * suite (R4) can record which adapters filter post-hydrate. `false` is the\n * honest floor (e.g. Gmail without `q=`), handled via `matchesRecord`.\n */\n protected readonly filterPushdown: boolean = false;\n\n /** Max concurrent in-flight calls for a `mapConcurrent`-built `hydrate`. */\n protected readonly hydrateConcurrency: number = 10;\n\n /** `Change<T>.source` provenance stamped by `listChanges`. */\n protected readonly changeSource: ChangeSource = 'poll';\n\n /**\n * Whether this source's cursor strategy is divisible (RFC-0003 §3). When\n * `true` (default — sortable watermarks like `systemModstamp`/`timestamp`/\n * `replayId`), `listChanges` emits each record's per-ref cursor, so the\n * orchestrator may checkpoint mid-walk and a crash resumes from the last\n * delivered ref.\n *\n * When `false` (atomic opaque tokens — Gmail `historyId`, Calendar\n * `syncToken`), `listChanges` WITHHOLDS per-ref cursors and emits the\n * end-of-walk token only on the final record, so the orchestrator's\n * persist-last-yielded lifecycle can never persist an unresumable mid-walk\n * token. The cost is blast-radius: an interrupted atomic run resumes\n * all-or-nothing from the prior persisted token. For atomic *backfills* that\n * radius is the whole enumerate walk — bound it with `ReadRequest.pageSize`\n * (smaller pages ⇒ shorter walks per run). Per-page atomic checkpointing is a\n * future refinement; R2 gates at end-of-walk.\n *\n * Codegen (R3) sets this from the strategy kind via `isDivisibleCursor`.\n */\n protected readonly cursorDivisible: boolean = true;\n\n // ---- SUPPLIED by the adapter (the irreducible vendor seam) ----\n\n /**\n * The cheap walk. Streams pages of refs; LAZY so `hydrate` backpressures it\n * (one page hydrated before the next is pulled). Mode-dispatch lives here:\n * `delta` resumes from `mode.cursor`, `full` walks from the top, `reconcile`\n * re-fetches `mode.knownIds`.\n *\n * `pageSize` (from `ReadRequest`) is the adapter's requested vendor page size\n * — also the atomic-cursor backfill blast-radius bound (§ `cursorDivisible`).\n * Honor it as a hint; vendors that cap page size clamp it.\n *\n * `ctx?.subscription` (R5) carries the run's subscription, so a per-connection\n * adapter resolves credentials / upstream scope from `externalRef` here; absent\n * on a direct `read()` with no run subscription.\n */\n protected abstract enumerate(\n mode: ReadMode,\n filter?: F,\n pageSize?: number,\n ctx?: ReadContext,\n ): AsyncIterable<Ref<M>[]>;\n\n /**\n * Fetch raw payloads for `ids`, keyed by id. MUST be miss-tolerant: omit (or\n * map to `null`) any id that 404s mid-run rather than throwing or shifting\n * alignment. Write it over `mapConcurrent(ids, (id) => this.fetchOne(id),\n * this.hydrateConcurrency)`; override with a real `/batch` call or a\n * passthrough (full-object list) where the vendor allows.\n *\n * `ctx?.subscription` (R5) carries the run's subscription for per-connection\n * credential resolution (the fetch is where the vendor call happens) and is the\n * natural place to land raw payloads keyed by `subscription.id`.\n */\n protected abstract hydrate(ids: string[], ctx?: ReadContext): Promise<Map<string, unknown>>;\n\n /** Provider payload → canonical record. Return `null` to drop a record. */\n protected abstract toCanonical(raw: unknown): T | null;\n\n // ---- Optional filter hooks — exactly one is live per `filterPushdown` ----\n\n /** Pre-hydrate predicate over the cheap ref (preferred — avoids hydration). */\n protected matchesRef(_ref: Ref<M>, _filter?: F): boolean {\n return true;\n }\n\n /** Post-hydrate predicate over the canonical record (the no-pushdown floor). */\n protected matchesRecord(_record: T, _filter?: F): boolean {\n return true;\n }\n\n /**\n * Resolve the filter for a subscription when adapting to `listChanges`\n * (which has no filter argument). Defaults to none; codegen wiring (R3)\n * overrides this to thread `DetectionConfig.filters`.\n */\n protected filterFor(_subscription: IntegrationSubscriptionView): F | undefined {\n return undefined;\n }\n\n // ---- PROVIDED by the base ----\n\n /**\n * Stream canonical records for a request. Filter is applied BEFORE hydrate\n * (structural: a kept ref is hydrated, a rejected one never is), so an\n * adapter cannot hydrate-then-discard. A hydrate miss (deleted mid-run) is\n * skipped, never fabricated.\n */\n async *read(req: ReadRequest<F>, ctx?: ReadContext): AsyncIterable<SourcedRecord<T>> {\n for await (const refPage of this.enumerate(req.mode, req.filter, req.pageSize, ctx)) {\n const kept = refPage.filter((ref) => this.matchesRef(ref, req.filter));\n if (kept.length === 0) continue;\n const raws = await this.hydrate(\n kept.map((ref) => ref.externalId),\n ctx,\n );\n for (const ref of kept) {\n const raw = raws.get(ref.externalId);\n if (raw === undefined || raw === null) continue; // deleted mid-run → skip\n const record = this.toCanonical(raw);\n if (record !== null && this.matchesRecord(record, req.filter)) {\n yield { externalId: ref.externalId, record, raw, cursor: ref.cursor };\n }\n }\n }\n }\n\n /**\n * `RandomRead<T>` — single-record read, provided for free as\n * `toCanonical ∘ hydrate([id])`. Reuses the adapter's batched fetch + miss\n * tolerance; returns `null` for a missing or undecodable record.\n */\n async get(id: string, ctx?: ReadContext): Promise<T | null> {\n const raws = await this.hydrate([id], ctx);\n const raw = raws.get(id);\n if (raw === undefined || raw === null) return null;\n return this.toCanonical(raw);\n }\n\n /**\n * `IChangeSource<T>` adaptation. Maps the orchestrator's by-value cursor to a\n * `ReadMode` (`null` → `full` backfill, else `delta`), streams `read()`, and\n * stamps each `SourcedRecord` into a `Change<T>`. All records surface as\n * `'updated'`; the orchestrator's diff stage classifies create-vs-update and\n * deletes arrive as tombstone refs (`toCanonical` may flag them).\n *\n * Cursor emission honors `cursorDivisible` (RFC-0003 §3). Divisible: each\n * record carries its own per-ref cursor. Atomic: per-ref cursors are withheld\n * (`undefined`, which the orchestrator skips persisting) and the end-of-walk\n * token rides only on the final record — so a mid-walk crash never persists\n * an unresumable token. If an atomic run yields no surviving records, no\n * cursor is persisted and the next run re-reads the same (empty) delta — a\n * bounded inefficiency, never data loss.\n */\n async *listChanges(\n subscription: IntegrationSubscriptionView,\n cursor: unknown | null,\n ): AsyncIterable<Change<T>> {\n const mode: ReadMode =\n cursor === null || cursor === undefined\n ? { kind: 'full' }\n : { kind: 'delta', cursor };\n const filter = this.filterFor(subscription);\n // R5: thread the run's subscription into the read body so `enumerate`/`hydrate`\n // can resolve per-connection credentials (and raw-landing keys) from it.\n const stream = this.read({ mode, filter }, { subscription });\n\n if (this.cursorDivisible) {\n for await (const sourced of stream) {\n yield this.toChange(sourced, sourced.cursor);\n }\n return;\n }\n\n // Atomic: one-record lookahead. Emit every record but the last with a\n // withheld (`undefined`) cursor; the last record carries the end-of-walk\n // token. Contract: an atomic adapter stamps the (single, shared) end-of-walk\n // token onto its refs' `cursor` — so whichever record survives last carries\n // it. The base emits a real cursor exactly once, on that final record, so the\n // orchestrator can never persist a mid-walk value. If zero records survive,\n // nothing is persisted (next run re-reads the delta — bounded, never lossy).\n let prev: SourcedRecord<T> | null = null;\n for await (const sourced of stream) {\n if (prev !== null) yield this.toChange(prev, undefined);\n prev = sourced;\n }\n if (prev !== null) yield this.toChange(prev, prev.cursor);\n }\n\n /** Stamp a `SourcedRecord` into a `Change<T>` with an explicit emitted cursor. */\n private toChange(sourced: SourcedRecord<T>, cursor: unknown): Change<T> {\n return {\n externalId: sourced.externalId,\n operation: 'updated',\n record: sourced.record,\n cursor,\n source: this.changeSource,\n };\n }\n}\n"],"mappings":";AAiJA,eAAsB,cACpB,KACA,IACA,OACyB;AACzB,QAAM,MAAM,oBAAI,IAAe;AAC/B,MAAI,IAAI,WAAW,EAAG,QAAO;AAC7B,QAAM,QAAQ,KAAK,IAAI,GAAG,KAAK,IAAI,OAAO,IAAI,MAAM,CAAC;AACrD,MAAI,OAAO;AACX,QAAM,SAAS,YAA2B;AACxC,WAAO,OAAO,IAAI,QAAQ;AACxB,YAAM,MAAM;AACZ,YAAM,KAA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7 @@ export { CURSOR_DIVISIBILITY, CursorStrategy, CursorStrategySchema, DetectionCon
10
10
  export { ChangeIterator, ChangeMiddleware, ComposeChangeMiddleware } from './integration-middleware.protocol.js';
11
11
  export { createLoopbackMiddleware } from './loopback.middleware.js';
12
12
  export { PollChangeSource, PollChangeSourceOptions, PollCursor, PollFetchCallback, PollFetchContext } from './poll-change-source.js';
13
- export { IncrementalRead, IncrementalReadBase, RandomRead, ReadMode, ReadRequest, Ref, SourcedRecord, mapConcurrent } from './incremental-read.js';
13
+ export { IncrementalRead, IncrementalReadBase, RandomRead, ReadContext, ReadMode, ReadRequest, Ref, SourcedRecord, mapConcurrent } from './incremental-read.js';
14
14
  export { WebhookChangeSource, WebhookChangeSourceOptions, WebhookCursor, WebhookFetchCallback, WebhookFetchContext } from './webhook-change-source.js';
15
15
  export { buildChangeSource } from './build-change-source.js';
16
16
  export { ENTITY_CHANGE_SOURCE_REGISTRY, INTEGRATION_CHANGE_SOURCE, INTEGRATION_CURSOR_STORE, INTEGRATION_FIELD_DIFFER, INTEGRATION_MODULE_OPTIONS, INTEGRATION_MULTI_TENANT, INTEGRATION_RUN_RECORDER, INTEGRATION_SINK } from './integration.tokens.js';
@@ -307,11 +307,14 @@ var IncrementalReadBase = class {
307
307
  * adapter cannot hydrate-then-discard. A hydrate miss (deleted mid-run) is
308
308
  * skipped, never fabricated.
309
309
  */
310
- async *read(req) {
311
- for await (const refPage of this.enumerate(req.mode, req.filter, req.pageSize)) {
310
+ async *read(req, ctx) {
311
+ for await (const refPage of this.enumerate(req.mode, req.filter, req.pageSize, ctx)) {
312
312
  const kept = refPage.filter((ref) => this.matchesRef(ref, req.filter));
313
313
  if (kept.length === 0) continue;
314
- const raws = await this.hydrate(kept.map((ref) => ref.externalId));
314
+ const raws = await this.hydrate(
315
+ kept.map((ref) => ref.externalId),
316
+ ctx
317
+ );
315
318
  for (const ref of kept) {
316
319
  const raw = raws.get(ref.externalId);
317
320
  if (raw === void 0 || raw === null) continue;
@@ -327,8 +330,8 @@ var IncrementalReadBase = class {
327
330
  * `toCanonical ∘ hydrate([id])`. Reuses the adapter's batched fetch + miss
328
331
  * tolerance; returns `null` for a missing or undecodable record.
329
332
  */
330
- async get(id) {
331
- const raws = await this.hydrate([id]);
333
+ async get(id, ctx) {
334
+ const raws = await this.hydrate([id], ctx);
332
335
  const raw = raws.get(id);
333
336
  if (raw === void 0 || raw === null) return null;
334
337
  return this.toCanonical(raw);
@@ -351,7 +354,7 @@ var IncrementalReadBase = class {
351
354
  async *listChanges(subscription, cursor) {
352
355
  const mode = cursor === null || cursor === void 0 ? { kind: "full" } : { kind: "delta", cursor };
353
356
  const filter = this.filterFor(subscription);
354
- const stream = this.read({ mode, filter });
357
+ const stream = this.read({ mode, filter }, { subscription });
355
358
  if (this.cursorDivisible) {
356
359
  for await (const sourced of stream) {
357
360
  yield this.toChange(sourced, sourced.cursor);