@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 669 lines 25 KB
Raw
1 import * as pipes from "@distilled.cloud/aws/pipes";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import { createPhysicalName } from "alchemy/PhysicalName";
5 import * as Provider from "alchemy/Provider";
6 import { Resource } from "alchemy/Resource";
7 import { createInternalTags, diffTags, hasAlchemyTags } from "alchemy/Tags";
8 import * as Data from "effect/Data";
9 import * as Effect from "effect/Effect";
10 import * as Redacted from "effect/Redacted";
11 import * as Schedule from "effect/Schedule";
12 import * as Stream from "effect/Stream";
13 import type { Providers } from "../Providers.ts";
14
15 /**
16 * The state you want the pipe to be in after reconciliation.
17 *
18 * - `RUNNING` — the pipe polls the source and delivers to the target.
19 * - `STOPPED` — the pipe exists but does not poll the source.
20 */
21 export type PipeDesiredState = "RUNNING" | "STOPPED";
22
23 export interface PipeProps {
24 /**
25 * Name of the pipe (1-64 characters). If omitted, a deterministic
26 * physical name is generated from the app, stage, and logical ID.
27 */
28 pipeName?: string;
29 /**
30 * A description of the pipe.
31 */
32 description?: string;
33 /**
34 * The state the pipe should be in.
35 * @default "RUNNING"
36 */
37 desiredState?: PipeDesiredState;
38 /**
39 * The ARN of the source resource (SQS queue, Kinesis stream, DynamoDB
40 * stream, etc.). Changing the source triggers a **replacement** — the
41 * EventBridge Pipes API does not allow updating a pipe's source.
42 */
43 source: string;
44 /**
45 * Source-specific parameters (batching, starting position, filter
46 * criteria). `FilterCriteria` holds EventBridge event-pattern strings
47 * that select which source events reach the enrichment/target.
48 * Changing a stream source's `StartingPosition` triggers a replacement.
49 */
50 sourceParameters?: pipes.PipeSourceParameters;
51 /**
52 * The ARN of the enrichment resource (Lambda function, Step Functions
53 * state machine, or API destination) invoked between source and target.
54 */
55 enrichment?: string;
56 /**
57 * Parameters for the enrichment step (input template, HTTP parameters).
58 */
59 enrichmentParameters?: pipes.PipeEnrichmentParameters;
60 /**
61 * The ARN of the target resource (Lambda function, SQS queue, Kinesis
62 * stream, EventBridge bus, CloudWatch Logs group, ECS task, etc.).
63 */
64 target: string;
65 /**
66 * Target-specific invocation parameters (input template, invocation
67 * type, partition key, message group, etc.).
68 */
69 targetParameters?: pipes.PipeTargetParameters;
70 /**
71 * The ARN of the IAM role that EventBridge Pipes assumes to read from
72 * the source and deliver to the target (and invoke the enrichment).
73 * Must trust the `pipes.amazonaws.com` service principal. Use
74 * `Pipes.from(source).toLambda(fn)` to have the role synthesized
75 * automatically.
76 */
77 roleArn: string;
78 /**
79 * Pipe execution log configuration (CloudWatch Logs, Firehose, or S3).
80 */
81 logConfiguration?: pipes.PipeLogConfigurationParameters;
82 /**
83 * The identifier of the KMS customer managed key used to encrypt pipe
84 * data at rest.
85 */
86 kmsKeyIdentifier?: string;
87 /**
88 * Tags to apply to the pipe. Merged with internal Alchemy tags.
89 */
90 tags?: Record<string, string>;
91 }
92
93 export interface Pipe extends Resource<
94 "AWS.Pipes.Pipe",
95 PipeProps,
96 {
97 /**
98 * Name of the pipe.
99 */
100 pipeName: string;
101 /**
102 * ARN of the pipe.
103 */
104 pipeArn: string;
105 /**
106 * Observed state of the pipe (e.g. `RUNNING`, `STOPPED`).
107 */
108 currentState: string | undefined;
109 },
110 never,
111 Providers
112 > {}
113
114 /**
115 * An Amazon EventBridge Pipe — point-to-point source→(filter)→(enrich)→target
116 * plumbing between AWS services without glue code.
117 *
118 * `Pipe` owns the lifecycle of an EventBridge Pipe. Reconcile waits (bounded)
119 * for the pipe to leave its `CREATING`/`UPDATING` transitional states, and a
120 * pipe that lands in a `*_FAILED` state surfaces as a typed {@link PipeFailed}
121 * error rather than hanging. Prefer the {@link from} builder for the common
122 * pairs — it synthesizes the `pipes.amazonaws.com` execution role with
123 * source-read and target-invoke policies for you.
124 * ### Creating Pipes
125 * **Example:** SQS to Lambda (builder — role synthesized automatically)
126 * ```typescript
127 * import * as AWS from "@alchemy.run/aws";
128 *
129 * const queue = yield* AWS.SQS.Queue("OrdersQueue");
130 * const pipe = yield* AWS.Pipes.from(queue, { batchSize: 1 }).toLambda(fn);
131 * ```
132 *
133 * **Example:** SQS to SQS (canonical resource with an explicit role)
134 * ```typescript
135 * const pipe = yield* AWS.Pipes.Pipe("OrdersPipe", {
136 * source: source.queueArn,
137 * target: target.queueArn,
138 * roleArn: role.roleArn,
139 * sourceParameters: {
140 * SqsQueueParameters: { BatchSize: 1 },
141 * },
142 * });
143 * ```
144 *
145 * ### Filtering
146 * **Example:** Only deliver matching events
147 * ```typescript
148 * const pipe = yield* AWS.Pipes.from(queue)
149 * .filter(JSON.stringify({ body: { type: ["order.created"] } }))
150 * .toLambda(fn);
151 * ```
152 *
153 * ### Enrichment
154 * **Example:** Enrich events with a Lambda function before delivery
155 * ```typescript
156 * const pipe = yield* AWS.Pipes.from(queue)
157 * .enrich(enricherFn)
158 * .toQueue(target);
159 * ```
160 *
161 * ### Stream Sources
162 * **Example:** Kinesis stream source
163 * ```typescript
164 * const pipe = yield* AWS.Pipes.from(stream, {
165 * startingPosition: "TRIM_HORIZON",
166 * batchSize: 10,
167 * }).toLambda(fn);
168 * ```
169 *
170 * **Example:** Stop a pipe without deleting it
171 * ```typescript
172 * const pipe = yield* AWS.Pipes.Pipe("OrdersPipe", {
173 * source: source.queueArn,
174 * target: target.queueArn,
175 * roleArn: role.roleArn,
176 * desiredState: "STOPPED",
177 * });
178 * ```
179 *
180 * @resource
181 */
182 export const Pipe = Resource<Pipe>("AWS.Pipes.Pipe");
183
184 /**
185 * Raised when a pipe lands in a terminal failed state
186 * (`CREATE_FAILED`, `UPDATE_FAILED`, `START_FAILED`, `STOP_FAILED`,
187 * `DELETE_FAILED`, ...). Carries the pipe's `StateReason` so the
188 * misconfiguration (usually IAM) is visible in the failure.
189 */
190 export class PipeFailed extends Data.TaggedError("PipeFailed")<{
191 pipeName: string;
192 state: string;
193 stateReason: string | undefined;
194 }> {}
195
196 /**
197 * Raised when a pipe does not settle out of a transitional state
198 * (`CREATING`, `UPDATING`, `STARTING`, `STOPPING`, `DELETING`) within the
199 * bounded wait (~60s). Surfaces the
200 * last observed state instead of hanging the deploy.
201 */
202 export class PipeStateTimeout extends Data.TaggedError("PipeStateTimeout")<{
203 pipeName: string;
204 state: string | undefined;
205 message: string;
206 }> {}
207
208 /** Internal poll signal: the pipe is still in a transitional state. */
209 class PipeStillTransitioning extends Data.TaggedError("PipeStillTransitioning")<{
210 pipeName: string;
211 state: string;
212 }> {}
213
214 /** Internal poll signal: the pipe still exists after a delete. */
215 class PipeStillPresent extends Data.TaggedError("PipeStillPresent")<{
216 pipeName: string;
217 state: string | undefined;
218 }> {}
219
220 const TRANSITIONAL_STATES: ReadonlySet<string> = new Set([
221 "CREATING",
222 "UPDATING",
223 "STARTING",
224 "STOPPING",
225 "DELETING",
226 ]);
227
228 const isFailedState = (state: string) => state.includes("FAILED");
229
230 const unwrap = (value: string | Redacted.Redacted<string> | undefined): string | undefined =>
231 value === undefined ? undefined : Redacted.isRedacted(value) ? Redacted.value(value) : value;
232
233 const unwrapTags = (
234 tags: { [key: string]: string | Redacted.Redacted<string> | undefined } | undefined,
235 ): Record<string, string> => {
236 const out: Record<string, string> = {};
237 for (const [key, value] of Object.entries(tags ?? {})) {
238 const plain = unwrap(value);
239 if (plain !== undefined) out[key] = plain;
240 }
241 return out;
242 };
243
244 /**
245 * Bounded retry helpers with EXPLICIT `Effect.Effect<A, E, R>` annotations.
246 * Inlining `Effect.retry` with options in provider lifecycle code leaves
247 * `Retry.Return`'s conditional type unresolved in the provider's inferred
248 * layer type, which declaration emit widens to an `unknown` R — poisoning
249 * `AWS.providers()` for every downstream consumer (see
250 * `retryThroughDeletionWindow` in AWS/SecretsManager/Secret.ts).
251 */
252 const retryWhileTransitioning = <A, E extends { _tag: string }, R>(
253 self: Effect.Effect<A, E, R>,
254 ): Effect.Effect<A, E, R> =>
255 Effect.retry(self, {
256 while: (e) => e._tag === "PipeStillTransitioning",
257 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(30)]),
258 });
259
260 const retryWhilePresent = <A, E extends { _tag: string }, R>(
261 self: Effect.Effect<A, E, R>,
262 ): Effect.Effect<A, E, R> =>
263 Effect.retry(self, {
264 while: (e) => e._tag === "PipeStillPresent",
265 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(30)]),
266 });
267
268 /**
269 * EventBridge Pipes validates that the execution role is assumable by
270 * `pipes.amazonaws.com` at create/update time. A freshly-created IAM role
271 * can take a minute to propagate, surfacing as a `ValidationException`
272 * mentioning role assumption. Retry (bounded) until propagation completes.
273 */
274 const retryUntilRoleAssumable = <A, E extends { _tag: string }, R>(
275 self: Effect.Effect<A, E, R>,
276 ): Effect.Effect<A, E, R> =>
277 Effect.retry(self, {
278 while: (e) => {
279 if (e._tag !== "ValidationException") return false;
280 const message = (e as { message?: unknown }).message;
281 return (
282 typeof message === "string" && (message.includes("assume") || message.includes("trust"))
283 );
284 },
285 schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(20)]),
286 });
287
288 /**
289 * Pipe mutations racing an in-flight transition surface as
290 * `ConflictException`. Retry on a bounded schedule so a delete/update
291 * issued while the pipe is still `CREATING`/`UPDATING` converges.
292 */
293 const retryWhileConflicting = <A, E extends { _tag: string }, R>(
294 self: Effect.Effect<A, E, R>,
295 ): Effect.Effect<A, E, R> =>
296 Effect.retry(self, {
297 while: (e) => e._tag === "ConflictException",
298 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(20)]),
299 });
300
301 /**
302 * `true` when every DEFINED field of `desired` deep-equals the observed
303 * value (Redacted values are unwrapped on both sides). Undefined desired
304 * fields are skipped — AWS fills unspecified parameter fields with
305 * defaults, so only user-specified values participate in drift detection.
306 */
307 const subsetMatches = (desired: unknown, observed: unknown): boolean => {
308 if (desired === undefined) return true;
309 const want = Redacted.isRedacted(desired) ? Redacted.value(desired) : desired;
310 const have = Redacted.isRedacted(observed) ? Redacted.value(observed) : observed;
311 if (Array.isArray(want)) {
312 if (!Array.isArray(have) || have.length !== want.length) return false;
313 return want.every((item, i) => subsetMatches(item, have[i]));
314 }
315 if (typeof want === "object" && want !== null) {
316 if (typeof have !== "object" || have === null) return false;
317 return Object.entries(want).every(([key, value]) =>
318 subsetMatches(value, (have as Record<string, unknown>)[key]),
319 );
320 }
321 return want === have;
322 };
323
324 const filterPatternsOf = (criteria: pipes.FilterCriteria | undefined): string[] =>
325 (criteria?.Filters ?? [])
326 .map((filter) => unwrap(filter.Pattern))
327 .filter((pattern): pattern is string => pattern !== undefined);
328
329 const sameFilters = (a: string[], b: string[]) =>
330 a.length === b.length && a.every((pattern, i) => pattern === b[i]);
331
332 const startingPositionOf = (params: pipes.PipeSourceParameters | undefined) =>
333 params?.KinesisStreamParameters?.StartingPosition ??
334 params?.DynamoDBStreamParameters?.StartingPosition;
335
336 /**
337 * Convert create-shape source parameters into the update shape. Stream
338 * starting positions are immutable (replacement — see `diff`), so they are
339 * stripped. `FilterCriteria` is always sent (defaulting to no filters) so
340 * removing a filter from props converges: EventBridge resets unspecified
341 * fields inside `SourceParameters` to their defaults on update.
342 */
343 const toUpdateSourceParameters = (
344 params: pipes.PipeSourceParameters | undefined,
345 ): pipes.UpdatePipeSourceParameters => ({
346 FilterCriteria: params?.FilterCriteria ?? { Filters: [] },
347 SqsQueueParameters: params?.SqsQueueParameters,
348 KinesisStreamParameters: params?.KinesisStreamParameters
349 ? {
350 BatchSize: params.KinesisStreamParameters.BatchSize,
351 DeadLetterConfig: params.KinesisStreamParameters.DeadLetterConfig,
352 OnPartialBatchItemFailure: params.KinesisStreamParameters.OnPartialBatchItemFailure,
353 MaximumBatchingWindowInSeconds:
354 params.KinesisStreamParameters.MaximumBatchingWindowInSeconds,
355 MaximumRecordAgeInSeconds: params.KinesisStreamParameters.MaximumRecordAgeInSeconds,
356 MaximumRetryAttempts: params.KinesisStreamParameters.MaximumRetryAttempts,
357 ParallelizationFactor: params.KinesisStreamParameters.ParallelizationFactor,
358 }
359 : undefined,
360 DynamoDBStreamParameters: params?.DynamoDBStreamParameters
361 ? {
362 BatchSize: params.DynamoDBStreamParameters.BatchSize,
363 DeadLetterConfig: params.DynamoDBStreamParameters.DeadLetterConfig,
364 OnPartialBatchItemFailure: params.DynamoDBStreamParameters.OnPartialBatchItemFailure,
365 MaximumBatchingWindowInSeconds:
366 params.DynamoDBStreamParameters.MaximumBatchingWindowInSeconds,
367 MaximumRecordAgeInSeconds: params.DynamoDBStreamParameters.MaximumRecordAgeInSeconds,
368 MaximumRetryAttempts: params.DynamoDBStreamParameters.MaximumRetryAttempts,
369 ParallelizationFactor: params.DynamoDBStreamParameters.ParallelizationFactor,
370 }
371 : undefined,
372 });
373
374 /**
375 * Source parameters with `FilterCriteria` removed — filters get dedicated
376 * removal-sensitive comparison, everything else uses `subsetMatches`.
377 */
378 const sourceParametersSansFilters = (
379 params: pipes.PipeSourceParameters | undefined,
380 ): Record<string, unknown> | undefined => {
381 if (params === undefined) return undefined;
382 const { FilterCriteria: _filters, ...rest } = params;
383 return rest;
384 };
385
386 export const PipeProvider = () =>
387 Provider.effect(
388 Pipe,
389 Effect.gen(function* () {
390 const createName = Effect.fn(function* (
391 id: string,
392 props: { pipeName?: string | undefined },
393 ) {
394 return props.pipeName ?? (yield* createPhysicalName({ id, maxLength: 64 }));
395 });
396
397 const describeOrUndefined = (pipeName: string) =>
398 pipes
399 .describePipe({ Name: pipeName })
400 .pipe(Effect.catchTag("NotFoundException", () => Effect.undefined));
401
402 /**
403 * Poll (bounded, ~60s) until the pipe leaves its transitional state.
404 * A `*_FAILED` terminal state surfaces as a typed `PipeFailed`; a pipe
405 * still transitioning after the bounded wait surfaces as a typed
406 * `PipeStateTimeout` — never a hang.
407 */
408 const awaitSettled = Effect.fn(function* (pipeName: string) {
409 const described = yield* describeOrUndefined(pipeName).pipe(
410 Effect.flatMap((d) =>
411 d?.CurrentState !== undefined && TRANSITIONAL_STATES.has(d.CurrentState)
412 ? Effect.fail(
413 new PipeStillTransitioning({
414 pipeName,
415 state: d.CurrentState,
416 }),
417 )
418 : Effect.succeed(d),
419 ),
420 retryWhileTransitioning,
421 Effect.catchTag("PipeStillTransitioning", (e) =>
422 Effect.fail(
423 new PipeStateTimeout({
424 pipeName,
425 state: e.state,
426 message: `Pipe '${pipeName}' did not settle within the bounded wait (last state: ${e.state})`,
427 }),
428 ),
429 ),
430 );
431 if (described?.CurrentState !== undefined && isFailedState(described.CurrentState)) {
432 return yield* new PipeFailed({
433 pipeName,
434 state: described.CurrentState,
435 stateReason: described.StateReason,
436 });
437 }
438 return described;
439 });
440
441 /** Poll (bounded, ~60s) until `describePipe` returns NotFound. */
442 const awaitGone = Effect.fn(function* (pipeName: string) {
443 yield* pipes.describePipe({ Name: pipeName }).pipe(
444 Effect.flatMap((d) =>
445 Effect.fail(new PipeStillPresent({ pipeName, state: d.CurrentState })),
446 ),
447 Effect.catchTag("NotFoundException", () => Effect.void),
448 retryWhilePresent,
449 Effect.catchTag("PipeStillPresent", (e) =>
450 Effect.fail(
451 new PipeStateTimeout({
452 pipeName,
453 state: e.state,
454 message: `Pipe '${pipeName}' did not finish deleting within the bounded wait (last state: ${e.state})`,
455 }),
456 ),
457 ),
458 );
459 });
460
461 return Pipe.Provider.of({
462 stables: ["pipeName", "pipeArn"],
463
464 list: () =>
465 Effect.gen(function* () {
466 const summaries = yield* pipes.listPipes.pages({}).pipe(
467 Stream.runCollect,
468 Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.Pipes ?? [])),
469 );
470 return summaries
471 .filter(
472 (p): p is pipes.Pipe & { Name: string; Arn: string } =>
473 p.Name !== undefined && p.Arn !== undefined,
474 )
475 .map((p) => ({
476 pipeName: p.Name,
477 pipeArn: p.Arn,
478 currentState: p.CurrentState,
479 }));
480 }),
481
482 read: Effect.fn(function* ({ id, olds, output }) {
483 const pipeName = output?.pipeName ?? (yield* createName(id, olds ?? {}));
484 const described = yield* describeOrUndefined(pipeName);
485 if (!described?.Arn) return undefined;
486 const attrs = {
487 pipeName,
488 pipeArn: described.Arn,
489 currentState: described.CurrentState,
490 };
491 const tags = unwrapTags(described.Tags);
492 return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs);
493 }),
494
495 diff: Effect.fn(function* ({ id, news, olds }) {
496 if (!isResolved(news)) return undefined;
497 const oldName = yield* createName(id, olds);
498 const newName = yield* createName(id, news);
499 if (oldName !== newName) {
500 return { action: "replace" } as const;
501 }
502 // The Pipes API has no way to change a pipe's source.
503 if (olds.source !== undefined && olds.source !== news.source) {
504 return { action: "replace" } as const;
505 }
506 // Stream starting positions are create-only.
507 if (
508 startingPositionOf(olds.sourceParameters) !== startingPositionOf(news.sourceParameters)
509 ) {
510 return { action: "replace" } as const;
511 }
512 // fall through: undefined → default update path
513 }),
514
515 reconcile: Effect.fn(function* ({ id, news, output, session }) {
516 const pipeName = output?.pipeName ?? (yield* createName(id, news));
517 const internalTags = yield* createInternalTags(id);
518 const desiredTags: Record<string, string> = {
519 ...news.tags,
520 ...internalTags,
521 };
522 const desiredState = news.desiredState ?? "RUNNING";
523
524 // 1. OBSERVE — settle any in-flight transition first; cloud state
525 // is authoritative, `output` is only a name cache. A pipe stuck
526 // in a `*_FAILED` state can't be repaired in place (the failure
527 // is usually IAM propagation from a crashed prior run), so it
528 // is deleted and recreated below.
529 let observed = yield* awaitSettled(pipeName).pipe(
530 Effect.catchTag("PipeFailed", (e) =>
531 Effect.gen(function* () {
532 yield* session.note(
533 `Pipe '${pipeName}' is in failed state ${e.state} (${e.stateReason ?? "no reason"}); deleting and recreating`,
534 );
535 yield* pipes.deletePipe({ Name: pipeName }).pipe(
536 retryWhileConflicting,
537 Effect.catchTag("NotFoundException", () => Effect.void),
538 );
539 yield* awaitGone(pipeName);
540 return undefined;
541 }),
542 ),
543 );
544
545 // 2. ENSURE — create if missing; tolerate the concurrent-create
546 // race and retry while a freshly-created IAM role propagates.
547 if (observed?.Arn === undefined) {
548 yield* pipes
549 .createPipe({
550 Name: pipeName,
551 Description: news.description,
552 DesiredState: desiredState,
553 Source: news.source,
554 SourceParameters: news.sourceParameters,
555 Enrichment: news.enrichment,
556 EnrichmentParameters: news.enrichmentParameters,
557 Target: news.target,
558 TargetParameters: news.targetParameters,
559 RoleArn: news.roleArn,
560 Tags: desiredTags,
561 LogConfiguration: news.logConfiguration,
562 KmsKeyIdentifier: news.kmsKeyIdentifier,
563 })
564 .pipe(
565 retryUntilRoleAssumable,
566 Effect.catchTag("ConflictException", () => Effect.void),
567 );
568 observed = yield* awaitSettled(pipeName);
569 }
570
571 if (observed?.Arn === undefined) {
572 return yield* new PipeFailed({
573 pipeName,
574 state: "MISSING",
575 stateReason: `describePipe returned no pipe after createPipe('${pipeName}') succeeded`,
576 });
577 }
578
579 // 3. SYNC — diff OBSERVED cloud state against desired and update
580 // only on drift. Filters/desired-state/target/enrichment are
581 // removal-sensitive; parameter objects compare user-specified
582 // fields only (AWS fills the rest with defaults).
583 const needsUpdate =
584 (news.description !== undefined && unwrap(observed.Description) !== news.description) ||
585 (observed.DesiredState ?? "RUNNING") !== desiredState ||
586 observed.Target !== news.target ||
587 (observed.Enrichment !== undefined && observed.Enrichment !== ""
588 ? observed.Enrichment
589 : undefined) !== news.enrichment ||
590 observed.RoleArn !== news.roleArn ||
591 (news.kmsKeyIdentifier !== undefined &&
592 observed.KmsKeyIdentifier !== news.kmsKeyIdentifier) ||
593 !sameFilters(
594 filterPatternsOf(observed.SourceParameters?.FilterCriteria),
595 filterPatternsOf(news.sourceParameters?.FilterCriteria),
596 ) ||
597 !subsetMatches(
598 sourceParametersSansFilters(news.sourceParameters),
599 observed.SourceParameters,
600 ) ||
601 !subsetMatches(news.targetParameters, observed.TargetParameters) ||
602 !subsetMatches(news.enrichmentParameters, observed.EnrichmentParameters);
603
604 if (needsUpdate) {
605 yield* pipes
606 .updatePipe({
607 Name: pipeName,
608 Description: news.description,
609 DesiredState: desiredState,
610 SourceParameters: toUpdateSourceParameters(news.sourceParameters),
611 // "" clears a previously-configured enrichment.
612 Enrichment: news.enrichment ?? "",
613 EnrichmentParameters: news.enrichmentParameters,
614 Target: news.target,
615 TargetParameters: news.targetParameters,
616 RoleArn: news.roleArn,
617 LogConfiguration: news.logConfiguration,
618 KmsKeyIdentifier: news.kmsKeyIdentifier,
619 })
620 .pipe(retryUntilRoleAssumable, retryWhileConflicting);
621 observed = yield* awaitSettled(pipeName);
622 }
623
624 if (observed?.Arn === undefined) {
625 return yield* new PipeFailed({
626 pipeName,
627 state: "MISSING",
628 stateReason: `Pipe '${pipeName}' disappeared during reconciliation`,
629 });
630 }
631
632 // 3b. SYNC TAGS — diff against OBSERVED cloud tags so adoption
633 // converges (never against olds/output).
634 const observedTags = unwrapTags(observed.Tags);
635 const { upsert, removed } = diffTags(observedTags, desiredTags);
636 if (upsert.length > 0) {
637 yield* pipes.tagResource({
638 resourceArn: observed.Arn,
639 tags: Object.fromEntries(upsert.map((t) => [t.Key, t.Value])),
640 });
641 }
642 if (removed.length > 0) {
643 yield* pipes.untagResource({
644 resourceArn: observed.Arn,
645 tagKeys: removed,
646 });
647 }
648
649 // 4. RETURN fresh Attributes.
650 yield* session.note(observed.Arn);
651 return {
652 pipeName,
653 pipeArn: observed.Arn,
654 currentState: observed.CurrentState,
655 };
656 }),
657
658 delete: Effect.fn(function* ({ output }) {
659 yield* pipes.deletePipe({ Name: output.pipeName }).pipe(
660 // A delete racing an in-flight CREATE/UPDATE transition
661 // returns ConflictException — retry until the pipe settles.
662 retryWhileConflicting,
663 Effect.catchTag("NotFoundException", () => Effect.void),
664 );
665 yield* awaitGone(output.pipeName);
666 }),
667 });
668 }),
669 );