cdk-real-drift 0.4.42 → 0.4.44

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -418,9 +418,16 @@ Nothing` (see [The model](#the-model-one-verb-you-run-three-it-offers)). Each
418
418
  each write. REMOVE ops (deleting a live value not in your template) are labeled
419
419
  `(REMOVE)`. **space** toggles · **→** selects all · **←** clears all · **enter**
420
420
  confirms. `--yes` applies the full plan.
421
- - **`record`** shows a multiselect of only the **delta** from the existing baseline
422
- (new + changed values, pre-selected); already-recorded unchanged values are
423
- auto-kept. Deselect a suspicious one and it stays reported by `check`. Nested
421
+ - **`record`** shows a multiselect of only the **delta** from the existing
422
+ baseline; already-recorded unchanged values are auto-kept. **New** undeclared
423
+ values are pre-selected; a value that **changed since record** (a recorded value
424
+ altered out of band) is shown as `recorded → live` and default **UNSELECTED**,
425
+ so one Enter never silently blesses a changed value. A recorded value that
426
+ **reverted to its AWS default or was removed** since record is offered as a
427
+ separate default-unselected "drop from baseline?" row — leaving it keeps the
428
+ watch (it stays reported by `check`); a `--yes` record preserves it and echoes
429
+ what it accepted. Deselect a suspicious one and it stays reported by `check`.
430
+ Nested
424
431
  undeclared sub-keys (a value inside an object you _did_ declare) are listed in
425
432
  full alongside the top-level ones — a non-default one is a real out-of-band
426
433
  setting, so it is surfaced, not hidden. `record` writes only undeclared + added
package/dist/cli.js CHANGED
@@ -1227,17 +1227,14 @@ async function resolveStacks(a) {
1227
1227
  const app = resolveApp(a.app);
1228
1228
  if (!app) throw new Error("cdkrd needs a CDK app: run in a directory with cdk.json, pass --app \"<cmd>\" or --app <cdk.out> (a pre-synthesized assembly), or set $CDKRD_APP");
1229
1229
  const scopePatterns = !a.all && a.stackNames.length > 0 ? a.stackNames : void 0;
1230
+ if (!a.region) a.region = await resolveProfileRegion(a.profile);
1231
+ const fallbackRegion = a.region;
1230
1232
  const discovered = await discoverStacks(app, {
1231
1233
  region: a.region,
1232
1234
  profile: a.profile,
1233
1235
  context: a.context,
1234
1236
  stackPatterns: scopePatterns
1235
1237
  });
1236
- let fallbackRegion = a.region;
1237
- if (!fallbackRegion && discovered.some((s) => !s.region)) {
1238
- fallbackRegion = await resolveProfileRegion(a.profile);
1239
- a.region = fallbackRegion;
1240
- }
1241
1238
  if (a.all || a.stackNames.length === 0) return discovered.map((s) => ({
1242
1239
  stackName: s.stackName,
1243
1240
  region: s.region ?? fallbackRegion,
@@ -10693,6 +10690,64 @@ function splitRecordedByBaseline(recorded, baseline) {
10693
10690
  changed
10694
10691
  };
10695
10692
  }
10693
+ /**
10694
+ * #758: for a `changed` recorded entry (a path already in the baseline whose value differs
10695
+ * this run), return the OLD recorded value so the record picker can show a `recorded → live`
10696
+ * diff and the user can see WHAT they are blessing. Matches on the same three identity axes as
10697
+ * `splitRecordedByBaseline` (#793). Returns `undefined` for a genuinely NEW path (no baseline
10698
+ * entry) — the caller then renders a plain "new" row. A sentinel `{ hasRecorded: boolean }` is
10699
+ * used rather than a bare `undefined` return so an intentional recorded value of `undefined`/
10700
+ * `null` is still distinguishable from "no baseline entry".
10701
+ */
10702
+ function recordedValueForChanged(entry, baseline) {
10703
+ const baselineEntry = baseline?.recorded.find((a) => a.logicalId === entry.logicalId && a.path === entry.path && a.resourceType === entry.resourceType);
10704
+ return baselineEntry ? {
10705
+ hasRecorded: true,
10706
+ recordedValue: baselineEntry.value
10707
+ } : {
10708
+ hasRecorded: false,
10709
+ recordedValue: void 0
10710
+ };
10711
+ }
10712
+ /**
10713
+ * #790: baseline entries that have NO counterpart in the freshly-built recorded set for a
10714
+ * resource that was READ CLEANLY this run — i.e. a recorded value reverted to its AWS default
10715
+ * (now classified `atDefault`/`generated`, so absent from `buildRecorded`) or a recorded
10716
+ * property REMOVED out of band. `check`'s `applyBaseline` force-surfaces these as drift, but a
10717
+ * naive re-`record` full-replaces the baseline with only the newly-built set, silently DROPPING
10718
+ * these entries — accepting exactly that drift with zero output. They must instead surface in
10719
+ * the record flow as explicit "drop this recorded watch?" rows (default UNSELECTED).
10720
+ *
10721
+ * An entry is a drop-candidate ONLY when its resource was genuinely OBSERVED and the value is
10722
+ * genuinely gone — every "the recorded value is stale for a benign reason" case is EXCLUDED so
10723
+ * it is never offered for a (misleading) drop:
10724
+ * - resource `skipped`/`modelReadFailed` this run → unread, not gone (carryForwardUnreadable
10725
+ * already preserves these; excluding them here keeps them silently preserved, not offered);
10726
+ * - resource `deleted`, or absent from the current template (`allLogicalIds`) → the resource
10727
+ * itself is gone (#675), a different story than a value reverting;
10728
+ * - path since PROMOTED into the template (declared) → the "clean up your baseline" nudge
10729
+ * (#749/#1079), not an accepted drift;
10730
+ * - the entry is an `added`-resource snapshot (empty path) → reconciled resource-wise, not a
10731
+ * property revert.
10732
+ * The remaining entries are true "recorded value reverted-to-default / removed since record".
10733
+ * Mirrors the exclusion set `applyBaseline`'s removed-since-record loop already applies.
10734
+ */
10735
+ function baselineOnlyEntries(recorded, baseline, findings, opts = {}) {
10736
+ if (!baseline || baseline.recorded.length === 0) return [];
10737
+ const present = new Set(recorded.map(recordedKey));
10738
+ const skippedOrUnread = new Set(findings.filter((f) => f.tier === "skipped" || f.modelReadFailed).map((f) => f.logicalId));
10739
+ const deletedLogical = new Set(findings.filter((f) => f.tier === "deleted").map((f) => f.logicalId));
10740
+ const currentLogicalIds = opts.allLogicalIds === void 0 ? void 0 : Array.isArray(opts.allLogicalIds) ? new Set(opts.allLogicalIds) : opts.allLogicalIds;
10741
+ return baseline.recorded.filter((e) => {
10742
+ if (e.path === "") return false;
10743
+ if (present.has(recordedKey(e))) return false;
10744
+ if (skippedOrUnread.has(e.logicalId)) return false;
10745
+ if (deletedLogical.has(e.logicalId)) return false;
10746
+ if (currentLogicalIds !== void 0 && !currentLogicalIds.has(e.logicalId)) return false;
10747
+ if (declaredPathIsPromoted(opts.declaredByLogical?.get(e.logicalId), e.path)) return false;
10748
+ return true;
10749
+ });
10750
+ }
10696
10751
  /** Selective record: build the recorded set from only the findings whose key is in
10697
10752
  * `selectedKeys`. Empty set -> []; all keys -> equals buildRecorded(findings). */
10698
10753
  function selectRecorded(findings, selectedKeys) {
@@ -10798,6 +10853,7 @@ function applyBaseline(findings, baseline, opts = {}) {
10798
10853
  kept.push({
10799
10854
  ...f,
10800
10855
  tier: "undeclared",
10856
+ desired: canonicalizeForCompare(entry.value),
10801
10857
  ...delta && { arrayDelta: delta }
10802
10858
  });
10803
10859
  continue;
@@ -13582,6 +13638,53 @@ function recordSelectMessage(stackName, region, foldedCount = 0) {
13582
13638
  return `${stackLabel$1(stackName, region)}: select undeclared value(s) to record (unselected stay reported)${fold}`;
13583
13639
  }
13584
13640
  /**
13641
+ * #790: header for the record DROP multiselect — the baseline-only entries (a recorded value
13642
+ * reverted to its AWS default, or removed out of band). Selecting a row DROPS the recorded
13643
+ * watch (accept the drift); leaving it UNSELECTED preserves the entry (it keeps reporting as
13644
+ * drift). Default-unselected, so one Enter here changes nothing. Pure + exported for unit tests.
13645
+ */
13646
+ function recordDropMessage(stackName, region) {
13647
+ return `${stackLabel$1(stackName, region)}: recorded value(s) reverted-to-default / removed since record — select to DROP from the baseline (unselected stay watched & reported)`;
13648
+ }
13649
+ /**
13650
+ * #758: a compact single-line preview of a recorded/live VALUE for a record-picker label —
13651
+ * so a `changed since record` row can show `recorded → live` and the user can see WHAT they
13652
+ * are blessing (instead of a bare `Res.Path`). JSON-encoded (scalars stay bare-ish), then
13653
+ * truncated to keep the multiselect row on one line. Pure + exported for unit tests.
13654
+ */
13655
+ function previewValue(v, max = 40) {
13656
+ let s;
13657
+ try {
13658
+ s = typeof v === "string" ? v : JSON.stringify(v);
13659
+ } catch {
13660
+ s = String(v);
13661
+ }
13662
+ if (s === void 0) s = String(v);
13663
+ return s.length > max ? `${s.slice(0, max - 1)}…` : s;
13664
+ }
13665
+ /**
13666
+ * #758: the record multiselect label for a `changed` entry — a path already in the baseline
13667
+ * whose live value differs this run (a recorded value CHANGED out of band). Distinct from a
13668
+ * plain new-path row: it shows `recorded → live` (or a `(changed since record)` marker when the
13669
+ * old value could not be paired) so the user does not silently bless a possibly-attacker-changed
13670
+ * value under a bare label. Pure + exported for unit tests.
13671
+ */
13672
+ function changedRecordLabel(entry, recordedValue) {
13673
+ const id = entry.path ? `${entry.logicalId}.${entry.path}` : `${entry.logicalId} (added resource)`;
13674
+ if (!recordedValue.hasRecorded) return id;
13675
+ return `${id} (changed since record: ${previewValue(recordedValue.recordedValue)} → ${previewValue(entry.value)})`;
13676
+ }
13677
+ /**
13678
+ * #790: the record multiselect label for a `baseline-only` drop-candidate — a recorded value
13679
+ * that reverted to its AWS default (now folded away) or was REMOVED out of band, so it has no
13680
+ * current undeclared finding. Selecting the row DROPS the recorded watch (an accept-drift
13681
+ * action, so the row is default UNSELECTED); leaving it keeps the entry in the baseline. Pure +
13682
+ * exported for unit tests.
13683
+ */
13684
+ function dropRecordLabel(entry) {
13685
+ return `${entry.path ? `${entry.logicalId}.${entry.path}` : `${entry.logicalId} (added resource)`} (reverted-to-default / removed since record — drop from baseline?)`;
13686
+ }
13687
+ /**
13585
13688
  * Split the to-record delta into STANDOUT (itemized in the record multiselect) and FOLDED
13586
13689
  * (nested live-only sub-keys — the `undeclared-subkey` mass the report folds, R96). Folded
13587
13690
  * entries are auto-recorded as a summarized count instead of dozens of rows. `expandNested`
@@ -13691,8 +13794,19 @@ async function recordStack(p) {
13691
13794
  if (existing) checkBaselineAccount(existing, desired.accountId, stackName);
13692
13795
  if (!yes && existing) console.error(`note: ${stackLabel$1(stackName, region)}: will overwrite the existing baseline file on confirm (nothing written yet; it is git-tracked — review the diff afterwards). Pass --yes to silence.`);
13693
13796
  let recorded = carryForwardUnreadable(buildRecorded(findings), existing, findings);
13797
+ const dropCandidates = baselineOnlyEntries(recorded, existing, findings, {
13798
+ declaredByLogical: declaredKeysByLogical(desired.resources),
13799
+ allLogicalIds: desired.resources.map((r) => r.logicalId)
13800
+ });
13801
+ const droppedKeys = /* @__PURE__ */ new Set();
13802
+ if (yes && existing) {
13803
+ const { changed } = splitRecordedByBaseline(recorded, existing);
13804
+ const changedExisting = changed.filter((e) => recordedValueForChanged(e, existing).hasRecorded);
13805
+ if (changedExisting.length > 0) console.error(`note: ${stackName}: --yes accepted ${changedExisting.length} recorded value(s) CHANGED out of band since record (review the baseline diff): ` + changedExisting.map((e) => `${e.logicalId}.${e.path}`).join(", "));
13806
+ if (dropCandidates.length > 0) console.error(`note: ${stackName}: --yes PRESERVED ${dropCandidates.length} recorded watch(es) whose value reverted-to-default / was removed since record (they still report as drift; re-run interactively to drop them)`);
13807
+ }
13694
13808
  let refreshedOnly = false;
13695
- if (!yes && recorded.length > 0) {
13809
+ if (!yes && (recorded.length > 0 || dropCandidates.length > 0)) {
13696
13810
  if (!interactive) {
13697
13811
  console.error(`error: record needs a decision — pass --yes to record ALL undeclared values, or run interactively`);
13698
13812
  return {
@@ -13704,7 +13818,7 @@ async function recordStack(p) {
13704
13818
  if (unchanged.length > 0) console.error(`note: ${stackName}: keeping ${unchanged.length} already-recorded unchanged value(s)`);
13705
13819
  if (changed.length === 0) {
13706
13820
  recorded = unchanged;
13707
- refreshedOnly = true;
13821
+ refreshedOnly = dropCandidates.length === 0;
13708
13822
  } else {
13709
13823
  let picked;
13710
13824
  if (preselectedKeys) picked = changed.map((e) => recordedKey(e)).filter((k) => preselectedKeys.has(k));
@@ -13724,11 +13838,14 @@ async function recordStack(p) {
13724
13838
  }
13725
13839
  picked = changed.map((e) => recordedKey(e));
13726
13840
  } else {
13727
- const fromPrompt = await bulkMultiselect(recordSelectMessage(stackName, region, folded.length), standout.map((e) => ({
13728
- value: recordedKey(e),
13729
- label: e.path ? `${e.logicalId}.${e.path}` : `${e.logicalId} (added resource)`,
13730
- selected: true
13731
- })));
13841
+ const fromPrompt = await bulkMultiselect(recordSelectMessage(stackName, region, folded.length), standout.map((e) => {
13842
+ const rec = recordedValueForChanged(e, existing);
13843
+ return {
13844
+ value: recordedKey(e),
13845
+ label: changedRecordLabel(e, rec),
13846
+ selected: !rec.hasRecorded
13847
+ };
13848
+ }));
13732
13849
  if (fromPrompt === void 0) {
13733
13850
  console.error(`note: ${stackName}: record cancelled — baseline unchanged`);
13734
13851
  return {
@@ -13741,21 +13858,40 @@ async function recordStack(p) {
13741
13858
  }
13742
13859
  const selectedChanged = selectRecorded(findings, new Set(picked));
13743
13860
  recorded = [...unchanged, ...selectedChanged];
13744
- if (recorded.length === 0 && !preselectedKeys) {
13745
- const proceed = await confirm({
13746
- message: `${stackLabel$1(stackName, region)}: record nothing? This writes an EMPTY baseline — every undeclared value stays reported as unrecorded.`,
13747
- initialValue: false
13748
- });
13749
- if (isCancel(proceed) || !proceed) {
13750
- console.error(`note: ${stackName}: record cancelled — baseline unchanged`);
13751
- return {
13752
- wrote: false,
13753
- refused: false
13754
- };
13755
- }
13861
+ }
13862
+ if (dropCandidates.length > 0 && !preselectedKeys) {
13863
+ const dropPrompt = await bulkMultiselect(recordDropMessage(stackName, region), dropCandidates.map((e) => ({
13864
+ value: recordedKey(e),
13865
+ label: dropRecordLabel(e),
13866
+ selected: false
13867
+ })));
13868
+ if (dropPrompt === void 0) {
13869
+ console.error(`note: ${stackName}: record cancelled — baseline unchanged`);
13870
+ return {
13871
+ wrote: false,
13872
+ refused: false
13873
+ };
13874
+ }
13875
+ for (const key of dropPrompt) droppedKeys.add(key);
13876
+ refreshedOnly = false;
13877
+ }
13878
+ recorded = [...recorded, ...dropCandidates.filter((e) => !droppedKeys.has(recordedKey(e)))];
13879
+ if (recorded.length === 0 && !preselectedKeys) {
13880
+ const proceed = await confirm({
13881
+ message: `${stackLabel$1(stackName, region)}: record nothing? This writes an EMPTY baseline — every undeclared value stays reported as unrecorded.`,
13882
+ initialValue: false
13883
+ });
13884
+ if (isCancel(proceed) || !proceed) {
13885
+ console.error(`note: ${stackName}: record cancelled — baseline unchanged`);
13886
+ return {
13887
+ wrote: false,
13888
+ refused: false
13889
+ };
13756
13890
  }
13757
13891
  }
13758
13892
  }
13893
+ const presentKeys = new Set(recorded.map(recordedKey));
13894
+ for (const e of dropCandidates) if (!droppedKeys.has(recordedKey(e)) && !presentKeys.has(recordedKey(e))) recorded.push(e);
13759
13895
  const { path, count } = await writeBaseline(stackName, region, desired.accountId, findings, desired.rawTemplate, recorded, {
13760
13896
  allLogicalIds: desired.resources.map((r) => r.logicalId),
13761
13897
  previous: existing,