@intentius/chant 0.41.20 → 0.44.1

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 (144) hide show
  1. package/dist/build.d.ts +15 -1
  2. package/dist/build.d.ts.map +1 -1
  3. package/dist/cli/commands/build.d.ts.map +1 -1
  4. package/dist/cli/commands/carve-apply.d.ts +9 -0
  5. package/dist/cli/commands/carve-apply.d.ts.map +1 -1
  6. package/dist/cli/commands/carve-bridge.d.ts +7 -0
  7. package/dist/cli/commands/carve-bridge.d.ts.map +1 -1
  8. package/dist/cli/commands/carve-emit.d.ts +7 -0
  9. package/dist/cli/commands/carve-emit.d.ts.map +1 -1
  10. package/dist/cli/commands/init.d.ts +2 -0
  11. package/dist/cli/commands/init.d.ts.map +1 -1
  12. package/dist/cli/handlers/carve-apply.d.ts.map +1 -1
  13. package/dist/cli/handlers/components.d.ts.map +1 -1
  14. package/dist/cli/handlers/explain.d.ts +11 -0
  15. package/dist/cli/handlers/explain.d.ts.map +1 -0
  16. package/dist/cli/handlers/lifecycle.d.ts.map +1 -1
  17. package/dist/cli/main.d.ts.map +1 -1
  18. package/dist/cli/mcp/tools/explain.d.ts.map +1 -1
  19. package/dist/cli/plugins.d.ts +8 -0
  20. package/dist/cli/plugins.d.ts.map +1 -1
  21. package/dist/cli/registry.d.ts +2 -0
  22. package/dist/cli/registry.d.ts.map +1 -1
  23. package/dist/codegen/docs-rule-scanning.d.ts +11 -0
  24. package/dist/codegen/docs-rule-scanning.d.ts.map +1 -1
  25. package/dist/codegen/okf-lexicon.d.ts +36 -0
  26. package/dist/codegen/okf-lexicon.d.ts.map +1 -0
  27. package/dist/codegen/package.d.ts +2 -1
  28. package/dist/codegen/package.d.ts.map +1 -1
  29. package/dist/components/deploy-units.d.ts.map +1 -1
  30. package/dist/components/verbs/sbom.d.ts +1 -1
  31. package/dist/components/verbs/sbom.d.ts.map +1 -1
  32. package/dist/components/verbs/vuln-gate.d.ts +15 -4
  33. package/dist/components/verbs/vuln-gate.d.ts.map +1 -1
  34. package/dist/components/verbs/vuln-scan.d.ts +18 -6
  35. package/dist/components/verbs/vuln-scan.d.ts.map +1 -1
  36. package/dist/config.d.ts +12 -0
  37. package/dist/config.d.ts.map +1 -1
  38. package/dist/deep-observation.d.ts +13 -0
  39. package/dist/deep-observation.d.ts.map +1 -1
  40. package/dist/lexicon.d.ts +47 -0
  41. package/dist/lexicon.d.ts.map +1 -1
  42. package/dist/lifecycle/observe.d.ts.map +1 -1
  43. package/dist/lifecycle/snapshot.d.ts.map +1 -1
  44. package/dist/okf.d.ts +57 -0
  45. package/dist/okf.d.ts.map +1 -0
  46. package/dist/terraform/__fixtures__/build-graph.d.ts +13 -0
  47. package/dist/terraform/__fixtures__/build-graph.d.ts.map +1 -0
  48. package/dist/terraform/adopt-state.d.ts +33 -1
  49. package/dist/terraform/adopt-state.d.ts.map +1 -1
  50. package/dist/terraform/aws-resources.d.ts.map +1 -1
  51. package/dist/terraform/bridge.d.ts +7 -1
  52. package/dist/terraform/bridge.d.ts.map +1 -1
  53. package/dist/terraform/carve.d.ts +12 -0
  54. package/dist/terraform/carve.d.ts.map +1 -1
  55. package/dist/terraform/excise.d.ts +31 -0
  56. package/dist/terraform/excise.d.ts.map +1 -0
  57. package/dist/terraform/graduate.d.ts +12 -0
  58. package/dist/terraform/graduate.d.ts.map +1 -1
  59. package/dist/terraform/graph.d.ts +35 -5
  60. package/dist/terraform/graph.d.ts.map +1 -1
  61. package/dist/terraform/manifest.d.ts +89 -0
  62. package/dist/terraform/manifest.d.ts.map +1 -0
  63. package/dist/terraform/parse.d.ts +9 -4
  64. package/dist/terraform/parse.d.ts.map +1 -1
  65. package/dist/terraform/tier-map.d.ts +4 -3
  66. package/dist/terraform/tier-map.d.ts.map +1 -1
  67. package/dist/terraform/types.d.ts +7 -0
  68. package/dist/terraform/types.d.ts.map +1 -1
  69. package/dist/terraform/unified-diff.d.ts +11 -0
  70. package/dist/terraform/unified-diff.d.ts.map +1 -0
  71. package/package.json +1 -1
  72. package/src/__snapshots__/okf.test.ts.snap +381 -0
  73. package/src/audit/rules-doc.ts +1 -1
  74. package/src/build.test.ts +95 -0
  75. package/src/build.ts +46 -1
  76. package/src/cli/commands/build.ts +9 -1
  77. package/src/cli/commands/carve-apply.test.ts +94 -2
  78. package/src/cli/commands/carve-apply.ts +62 -11
  79. package/src/cli/commands/carve-bridge.test.ts +82 -4
  80. package/src/cli/commands/carve-bridge.ts +79 -11
  81. package/src/cli/commands/carve-emit-state.test.ts +110 -1
  82. package/src/cli/commands/carve-emit.ts +184 -8
  83. package/src/cli/commands/init.ts +1 -1
  84. package/src/cli/handlers/carve-apply.ts +1 -0
  85. package/src/cli/handlers/components.ts +4 -2
  86. package/src/cli/handlers/explain.test.ts +93 -0
  87. package/src/cli/handlers/explain.ts +60 -0
  88. package/src/cli/handlers/lifecycle.ts +16 -9
  89. package/src/cli/main.ts +21 -5
  90. package/src/cli/mcp/server.test.ts +17 -0
  91. package/src/cli/mcp/tools/explain.ts +15 -3
  92. package/src/cli/plugins.ts +17 -0
  93. package/src/cli/registry.ts +2 -0
  94. package/src/codegen/__snapshots__/okf-lexicon.test.ts.snap +170 -0
  95. package/src/codegen/docs-rule-scanning.ts +35 -14
  96. package/src/codegen/okf-lexicon.test.ts +248 -0
  97. package/src/codegen/okf-lexicon.ts +303 -0
  98. package/src/codegen/package.ts +29 -4
  99. package/src/components/config-defaults.test.ts +30 -0
  100. package/src/components/deploy-units.test.ts +13 -0
  101. package/src/components/deploy-units.ts +5 -0
  102. package/src/components/verbs/__fixtures__/grype-with-kev-epss.json +1 -0
  103. package/src/components/verbs/__fixtures__/trivy-with-kev-epss.json +631 -0
  104. package/src/components/verbs/exploitability-roundtrip.test.ts +190 -0
  105. package/src/components/verbs/sbom.ts +1 -1
  106. package/src/components/verbs/vuln-gate.test.ts +144 -1
  107. package/src/components/verbs/vuln-gate.ts +61 -10
  108. package/src/components/verbs/vuln-scan.test.ts +94 -0
  109. package/src/components/verbs/vuln-scan.ts +44 -9
  110. package/src/config.test.ts +21 -0
  111. package/src/config.ts +18 -0
  112. package/src/deep-observation.test.ts +19 -0
  113. package/src/deep-observation.ts +32 -13
  114. package/src/lexicon.ts +50 -0
  115. package/src/lifecycle/observe.test.ts +18 -0
  116. package/src/lifecycle/observe.ts +10 -3
  117. package/src/lifecycle/snapshot.ts +5 -4
  118. package/src/okf.test.ts +169 -0
  119. package/src/okf.ts +308 -0
  120. package/src/terraform/__fixtures__/build-graph.ts +25 -0
  121. package/src/terraform/adopt-state.test.ts +34 -1
  122. package/src/terraform/adopt-state.ts +69 -2
  123. package/src/terraform/aws-resources.test.ts +52 -2
  124. package/src/terraform/aws-resources.ts +95 -4
  125. package/src/terraform/bridge.test.ts +38 -14
  126. package/src/terraform/bridge.ts +48 -14
  127. package/src/terraform/carve.test.ts +9 -7
  128. package/src/terraform/carve.ts +0 -0
  129. package/src/terraform/excise.test.ts +92 -0
  130. package/src/terraform/excise.ts +139 -0
  131. package/src/terraform/graduate.test.ts +73 -6
  132. package/src/terraform/graduate.ts +49 -0
  133. package/src/terraform/graph.test.ts +73 -10
  134. package/src/terraform/graph.ts +124 -47
  135. package/src/terraform/manifest.test.ts +105 -0
  136. package/src/terraform/manifest.ts +162 -0
  137. package/src/terraform/parse.test.ts +39 -1
  138. package/src/terraform/parse.ts +36 -9
  139. package/src/terraform/score.test.ts +8 -8
  140. package/src/terraform/state.test.ts +4 -4
  141. package/src/terraform/tier-map.ts +60 -8
  142. package/src/terraform/types.ts +7 -0
  143. package/src/terraform/unified-diff.test.ts +77 -0
  144. package/src/terraform/unified-diff.ts +146 -0
@@ -3,7 +3,7 @@ import { loadChantConfigUpward, resolveOwnershipMarker, resolveFoldEnabled, reso
3
3
  import { resolveCliBuildParams } from "../build-params-cli";
4
4
  import type { Serializer, SerializerResult } from "../../serializer";
5
5
  import type { LexiconPlugin } from "../../lexicon";
6
- import { resolveLexiconVersions } from "../plugins";
6
+ import { resolveLexiconVersions, collectBuildRootContributors } from "../plugins";
7
7
  import { runPostSynthChecks } from "../../lint/post-synth";
8
8
  import { applyConfiguredSeverity } from "../../lint/config";
9
9
  import { loadPolicyChecks } from "../../lint/policy";
@@ -261,6 +261,14 @@ export async function buildCommand(options: BuildOptions): Promise<BuildResult>
261
261
  lexicons,
262
262
  lexiconVersions,
263
263
  buildParams: paramsResolution.provenance,
264
+ // #1548 piece 3 — config-declared build roots (kustomize dirs), rendered
265
+ // into entities by the owning lexicon's hook. Rooted at the config dir,
266
+ // not the (possibly sourceDir-scoped) infra path.
267
+ buildRoots: collectBuildRootContributors(
268
+ options.plugins,
269
+ config as unknown as Record<string, unknown>,
270
+ configDir,
271
+ ),
264
272
  });
265
273
 
266
274
  // #1022 — report per-file fold vs run so it's visible what still runs.
@@ -4,6 +4,7 @@ import { tmpdir } from "os";
4
4
  import { join } from "path";
5
5
  import { carveApply, formatCarveApply } from "./carve-apply";
6
6
  import { loadHcl2json } from "../../terraform/parse";
7
+ import { readCarveManifest, writeCarveManifest, type CarveManifest } from "../../terraform/manifest";
7
8
 
8
9
  let parserAvailable = false;
9
10
  try {
@@ -30,10 +31,101 @@ async function withEstate<T>(fn: (dir: string) => Promise<T>): Promise<T> {
30
31
  }
31
32
  }
32
33
 
34
+ function manifestFor(target: string, dir: string): CarveManifest {
35
+ return {
36
+ version: 1,
37
+ target,
38
+ from: dir,
39
+ boundary: {
40
+ target,
41
+ carveSet: [{ address: target, type: target.split(".")[0] }],
42
+ peelability: 90,
43
+ inbound: [],
44
+ outbound: [],
45
+ reversible: true,
46
+ diagnostics: [],
47
+ },
48
+ };
49
+ }
50
+
33
51
  describe("carveApply", () => {
34
- test("requires --from and --select", async () => {
52
+ test("requires --from; without --select it needs a carve manifest", async () => {
35
53
  expect((await carveApply({})).error).toContain("--from");
36
- expect((await carveApply({ from: "/x" })).error).toContain("--select");
54
+ await withEstate(async (dir) => {
55
+ const res = await carveApply({ from: dir });
56
+ expect(res.ok).toBe(false);
57
+ expect(res.error).toContain("--select");
58
+ });
59
+ });
60
+
61
+ test("composes with the carve manifest: target resolved without --select, graduation recorded", async () => {
62
+ if (!parserAvailable) return;
63
+ await withEstate(async (dir) => {
64
+ const out = join(dir, "carveout");
65
+ writeCarveManifest(out, manifestFor("aws_s3_bucket.assets", dir));
66
+
67
+ const res = await carveApply({ from: dir, output: out, env: "prod", stack: "assets" });
68
+ expect(res.ok).toBe(true);
69
+ expect(res.selectFromManifest).toBe(true);
70
+ expect(formatCarveApply(res)).toContain("target from the carve manifest");
71
+
72
+ const m = readCarveManifest(res.manifestPath!)!;
73
+ expect(m.apply!.marker).toEqual({ stack: "assets", env: "prod" });
74
+ expect(m.apply!.ownershipTags["chant:managed-by"]).toBe("chant");
75
+ });
76
+ });
77
+
78
+ test("--write-source stamps the ownership marker into the emitted source and records it", async () => {
79
+ if (!parserAvailable) return;
80
+ await withEstate(async (dir) => {
81
+ const out = join(dir, "carveout");
82
+ const emitted = join(out, "assets.ts");
83
+ const manifest = manifestFor("aws_s3_bucket.assets", dir);
84
+ manifest.emit = { source: "tfstate", files: [emitted], at: "t" };
85
+ writeCarveManifest(out, manifest);
86
+ writeFileSync(emitted, 'export const assets = new Bucket({\n BucketName: "myapp-assets-prod",\n});\n');
87
+
88
+ const res = await carveApply({ from: dir, output: out, env: "prod", stack: "assets", writeSource: true });
89
+ expect(res.ok).toBe(true);
90
+ expect(res.stamped).toEqual([emitted]);
91
+
92
+ const stamped = readFileSync(emitted, "utf-8");
93
+ expect(stamped).toContain('{"Key":"chant:managed-by","Value":"chant"}');
94
+ expect(stamped).toContain('{"Key":"chant:stack","Value":"assets"}');
95
+ expect(stamped).toContain('{"Key":"chant:env","Value":"prod"}');
96
+
97
+ const m = readCarveManifest(res.manifestPath!)!;
98
+ expect(m.apply!.stampedFiles).toEqual([emitted]);
99
+ expect(formatCarveApply(res)).toContain("Stamped the ownership marker into");
100
+
101
+ // Idempotent: a second graduation leaves the source stable.
102
+ const again = await carveApply({ from: dir, output: out, env: "prod", stack: "assets", writeSource: true });
103
+ expect(again.ok).toBe(true);
104
+ expect(readFileSync(emitted, "utf-8")).toBe(stamped);
105
+ });
106
+ });
107
+
108
+ test("--write-source without an emit record in the manifest is a clear error", async () => {
109
+ if (!parserAvailable) return;
110
+ await withEstate(async (dir) => {
111
+ const out = join(dir, "carveout");
112
+ writeCarveManifest(out, manifestFor("aws_s3_bucket.assets", dir));
113
+ const res = await carveApply({ from: dir, output: out, env: "prod", writeSource: true });
114
+ expect(res.ok).toBe(false);
115
+ expect(res.error).toContain("carve emit");
116
+ });
117
+ });
118
+
119
+ test("falls back to the manifest's persisted boundary when the block is already excised", async () => {
120
+ if (!parserAvailable) return;
121
+ await withEstate(async (dir) => {
122
+ const out = join(dir, "carveout");
123
+ writeCarveManifest(out, manifestFor("aws_s3_bucket.gone", dir));
124
+
125
+ const res = await carveApply({ from: dir, output: out, env: "prod" });
126
+ expect(res.ok).toBe(true);
127
+ expect(res.plan!.target).toBe("aws_s3_bucket.gone");
128
+ });
37
129
  });
38
130
 
39
131
  test("plans graduation with ownership marker; writes nothing by default", async () => {
@@ -9,14 +9,16 @@
9
9
  * lifecycle you brought.
10
10
  */
11
11
 
12
- import { existsSync, statSync, mkdirSync, writeFileSync } from "fs";
12
+ import { existsSync, statSync, mkdirSync, readFileSync, writeFileSync } from "fs";
13
13
  import { join } from "path";
14
14
  import { parseTerraformDir, Hcl2JsonNotInstalled } from "../../terraform/parse";
15
15
  import { boundaryReport } from "../../terraform/carve";
16
- import { graduationPlan, type GraduationPlan } from "../../terraform/graduate";
16
+ import { graduationPlan, stampOwnershipIntoSource, type GraduationPlan } from "../../terraform/graduate";
17
+ import { resolveCarveManifest, updateCarveManifest } from "../../terraform/manifest";
17
18
 
18
19
  export interface CarveApplyOptions {
19
20
  from?: string;
21
+ /** Optional when the output dir holds a single carve manifest from `carve emit`. */
20
22
  select?: string;
21
23
  env?: string;
22
24
  stack?: string;
@@ -24,6 +26,8 @@ export interface CarveApplyOptions {
24
26
  output?: string;
25
27
  /** Write the graduation doc to the output dir. */
26
28
  write?: boolean;
29
+ /** Stamp the ownership marker into the emitted chant source. */
30
+ writeSource?: boolean;
27
31
  }
28
32
 
29
33
  export interface CarveApplyResult {
@@ -31,36 +35,77 @@ export interface CarveApplyResult {
31
35
  error?: string;
32
36
  plan?: GraduationPlan;
33
37
  written?: string;
38
+ /** Emitted source files the ownership marker was stamped into. */
39
+ stamped?: string[];
40
+ /** The carve state manifest this graduation was recorded into. */
41
+ manifestPath?: string;
42
+ /** True when the target came from the manifest, not --select. */
43
+ selectFromManifest?: boolean;
34
44
  }
35
45
 
36
46
  export async function carveApply(opts: CarveApplyOptions): Promise<CarveApplyResult> {
37
47
  if (!opts.from) return { ok: false, error: "chant carve apply requires --from <terraform-dir>" };
38
- if (!opts.select) return { ok: false, error: "chant carve apply requires --select <tf-address>" };
39
48
  if (!existsSync(opts.from) || !statSync(opts.from).isDirectory()) {
40
49
  return { ok: false, error: `Not a directory: ${opts.from}` };
41
50
  }
42
51
 
52
+ // Compose with the manifest emit/bridge persisted: it supplies the target
53
+ // (and tfstate) when --select is absent, and its persisted boundary stands in
54
+ // when the carved block is already excised from the estate.
55
+ const outDir = opts.output ?? join(opts.from, "carveout");
56
+ const resolved = resolveCarveManifest(outDir, opts.select);
57
+ if (!opts.select && resolved.error) return { ok: false, error: resolved.error };
58
+ const select = opts.select ?? resolved.manifest!.target;
59
+ const statePath = opts.statePath ?? resolved.manifest?.statePath;
60
+
43
61
  let plan: GraduationPlan;
44
62
  try {
45
- const graph = await parseTerraformDir(opts.from, { statePath: opts.statePath });
46
- const report = boundaryReport(graph, opts.select);
47
- if (!report) return { ok: false, error: `${opts.select} not found in ${opts.from}` };
63
+ const graph = await parseTerraformDir(opts.from, { statePath });
64
+ const report = boundaryReport(graph, select) ?? resolved.manifest?.boundary ?? null;
65
+ if (!report) return { ok: false, error: `${select} not found in ${opts.from}` };
48
66
  plan = graduationPlan(report, { stack: opts.stack, env: opts.env });
49
67
  } catch (err) {
50
68
  if (err instanceof Hcl2JsonNotInstalled) return { ok: false, error: err.message };
51
69
  return { ok: false, error: `Failed to plan graduation: ${err instanceof Error ? err.message : String(err)}` };
52
70
  }
53
71
 
72
+ // Stamp the marker into the emitted source (`--write-source`) — the emitted
73
+ // files are known from the manifest, so the tags the plan promises are the
74
+ // tags the source actually carries into the apply.
75
+ let stamped: string[] | undefined;
76
+ if (opts.writeSource) {
77
+ const files = resolved.manifest?.emit?.files ?? [];
78
+ if (files.length === 0) {
79
+ return {
80
+ ok: false,
81
+ error: "--write-source needs the emitted source recorded in the carve manifest — run `chant carve emit` first.",
82
+ };
83
+ }
84
+ stamped = [];
85
+ for (const file of files) {
86
+ if (!existsSync(file)) return { ok: false, error: `Emitted source not found: ${file}` };
87
+ const result = stampOwnershipIntoSource(readFileSync(file, "utf-8"), plan.ownershipTags);
88
+ if (!result) return { ok: false, error: `No constructor to stamp in ${file}.` };
89
+ if (result.changed) writeFileSync(file, result.content);
90
+ stamped.push(file);
91
+ }
92
+ }
93
+
54
94
  let written: string | undefined;
55
95
  if (opts.write) {
56
- const outDir = opts.output ?? join(opts.from, "carveout");
57
96
  mkdirSync(outDir, { recursive: true });
58
- const slug = opts.select.replace(/[^A-Za-z0-9_]+/g, "-");
97
+ const slug = select.replace(/[^A-Za-z0-9_]+/g, "-");
59
98
  written = join(outDir, `${slug}-graduation.md`);
60
99
  writeFileSync(written, renderGraduationDoc(plan) + "\n");
61
100
  }
62
101
 
63
- return { ok: true, plan, written };
102
+ // Record the graduation into an existing manifest; never create one here —
103
+ // plan-only stays plan-only unless a carve flow is already in progress.
104
+ const manifestPath = updateCarveManifest(outDir, select, {
105
+ apply: { marker: plan.marker, ownershipTags: plan.ownershipTags, stampedFiles: stamped, at: new Date().toISOString() },
106
+ });
107
+
108
+ return { ok: true, plan, written, stamped, manifestPath, selectFromManifest: !opts.select };
64
109
  }
65
110
 
66
111
  function renderGraduationDoc(plan: GraduationPlan): string {
@@ -86,7 +131,7 @@ export function formatCarveApply(result: CarveApplyResult): string {
86
131
  if (!result.ok || !result.plan) return result.error ?? "carve apply failed";
87
132
  const p = result.plan;
88
133
  const L: string[] = [];
89
- L.push(`Apply graduation for ${p.target} (dial-turn: observe → apply).`);
134
+ L.push(`Apply graduation for ${p.target} (dial-turn: observe → apply)${result.selectFromManifest ? " — target from the carve manifest" : ""}.`);
90
135
  L.push(` Ownership marker: stack=${p.marker.stack}${p.marker.env ? `, env=${p.marker.env}` : ""}`);
91
136
  L.push(" Ownership tags (stamped on apply):");
92
137
  for (const [k, v] of Object.entries(p.ownershipTags)) L.push(` ${k} = ${v}`);
@@ -97,10 +142,16 @@ export function formatCarveApply(result: CarveApplyResult): string {
97
142
  for (const w of p.warnings) L.push(` ! ${w}`);
98
143
  }
99
144
  L.push("");
145
+ if (result.stamped?.length) {
146
+ L.push(`Stamped the ownership marker into: ${result.stamped.join(", ")}`);
147
+ }
100
148
  if (result.written) {
101
149
  L.push(`Wrote graduation doc: ${result.written}`);
102
- } else {
150
+ } else if (!result.stamped?.length) {
103
151
  L.push("Plan only — no cloud call, nothing written. The apply is your lifecycle (BYOL). Add --write to save this as a doc.");
104
152
  }
153
+ if (result.manifestPath) {
154
+ L.push(`Recorded in the carve manifest: ${result.manifestPath}`);
155
+ }
105
156
  return L.join("\n");
106
157
  }
@@ -1,9 +1,11 @@
1
1
  import { describe, test, expect } from "vitest";
2
+ import { execFileSync } from "child_process";
2
3
  import { mkdtempSync, writeFileSync, rmSync, readFileSync, existsSync } from "fs";
3
4
  import { tmpdir } from "os";
4
5
  import { join } from "path";
5
6
  import { carveBridge, formatCarveBridge } from "./carve-bridge";
6
7
  import { loadHcl2json } from "../../terraform/parse";
8
+ import { readCarveManifest, writeCarveManifest, type CarveManifest } from "../../terraform/manifest";
7
9
 
8
10
  let parserAvailable = false;
9
11
  try {
@@ -39,10 +41,52 @@ async function withEstate<T>(fn: (dir: string) => Promise<T>): Promise<T> {
39
41
  }
40
42
  }
41
43
 
44
+ function manifestFor(target: string, dir: string): CarveManifest {
45
+ return {
46
+ version: 1,
47
+ target,
48
+ from: dir,
49
+ boundary: {
50
+ target,
51
+ carveSet: [{ address: target, type: target.split(".")[0] }],
52
+ peelability: 90,
53
+ inbound: [],
54
+ outbound: [],
55
+ reversible: true,
56
+ diagnostics: [],
57
+ },
58
+ };
59
+ }
60
+
42
61
  describe("carveBridge", () => {
43
- test("requires --from and --select", async () => {
62
+ test("requires --from; without --select it needs a carve manifest", async () => {
44
63
  expect((await carveBridge({})).error).toContain("--from");
45
- expect((await carveBridge({ from: "/x" })).error).toContain("--select");
64
+ await withEstate(async (dir) => {
65
+ const res = await carveBridge({ from: dir });
66
+ expect(res.ok).toBe(false);
67
+ expect(res.error).toContain("--select");
68
+ expect(res.error).toContain("carve emit");
69
+ });
70
+ });
71
+
72
+ test("composes with the carve manifest: target resolved without --select, bridge recorded", async () => {
73
+ if (!parserAvailable) return;
74
+ await withEstate(async (dir) => {
75
+ const out = join(dir, "carveout");
76
+ writeCarveManifest(out, manifestFor("aws_s3_bucket.assets", dir));
77
+
78
+ const res = await carveBridge({ from: dir, output: out });
79
+ expect(res.ok).toBe(true);
80
+ expect(res.selectFromManifest).toBe(true);
81
+ expect(res.plan!.target).toBe("aws_s3_bucket.assets");
82
+ expect(formatCarveBridge(res)).toContain("target from the carve manifest");
83
+
84
+ const m = readCarveManifest(res.manifestPath!)!;
85
+ expect(m.bridge!.written.length).toBeGreaterThan(0);
86
+ expect(m.bridge!.appliedInPlace).toBe(false);
87
+ // The boundary is refreshed from the estate, not left as the stub.
88
+ expect(m.boundary.inbound.map((e) => e.survivor)).toEqual(["aws_lambda_function.api"]);
89
+ });
46
90
  });
47
91
 
48
92
  test("dry-run: writes runbook + data sources + proposed survivor, touches no .tf in place", async () => {
@@ -65,6 +109,39 @@ describe("carveBridge", () => {
65
109
  });
66
110
  });
67
111
 
112
+ test("emits one git-applyable patch carrying the whole survivor edit", async () => {
113
+ if (!parserAvailable) return;
114
+ await withEstate(async (dir) => {
115
+ const out = join(dir, "carveout");
116
+ const res = await carveBridge({ from: dir, select: "aws_s3_bucket.assets", output: out });
117
+ expect(res.ok).toBe(true);
118
+ expect(res.patchPath).toBe(join(out, "aws_s3_bucket-assets-bridge.patch"));
119
+ expect(res.written).toContain(res.patchPath);
120
+
121
+ const patch = readFileSync(res.patchPath!, "utf-8");
122
+ expect(patch).toContain("diff --git a/aws_s3_bucket-assets-datasources.tf b/aws_s3_bucket-assets-datasources.tf");
123
+ expect(patch).toContain("new file mode 100644");
124
+ expect(patch).toContain("diff --git a/api.tf b/api.tf");
125
+ expect(patch).toContain("- ASSETS_BUCKET = aws_s3_bucket.assets.bucket");
126
+ expect(patch).toContain("+ ASSETS_BUCKET = data.aws_s3_bucket.assets.bucket");
127
+
128
+ const m = readCarveManifest(res.manifestPath!)!;
129
+ expect(m.bridge!.patch).toBe(res.patchPath);
130
+ expect(formatCarveBridge(res)).toContain("git apply");
131
+
132
+ // The patch really applies: `git apply` from the estate reproduces the
133
+ // rewired survivor and the new data-source file.
134
+ try {
135
+ execFileSync("git", ["apply", res.patchPath!], { cwd: dir });
136
+ } catch (err) {
137
+ if ((err as NodeJS.ErrnoException).code === "ENOENT") return; // no git on this machine
138
+ throw err;
139
+ }
140
+ expect(readFileSync(join(dir, "api.tf"), "utf-8")).toContain("data.aws_s3_bucket.assets.bucket");
141
+ expect(readFileSync(join(dir, "aws_s3_bucket-assets-datasources.tf"), "utf-8")).toContain('data "aws_s3_bucket" "assets"');
142
+ });
143
+ });
144
+
68
145
  test("--apply-rewrites edits the survivor .tf in place", async () => {
69
146
  if (!parserAvailable) return;
70
147
  await withEstate(async (dir) => {
@@ -77,10 +154,11 @@ describe("carveBridge", () => {
77
154
  expect(res.ok).toBe(true);
78
155
  expect(res.appliedInPlace).toBe(true);
79
156
 
80
- // api.tf rewritten in place; bucket.tf (the carved declaration) untouched.
157
+ // api.tf rewritten in place; bucket.tf's carved declaration excised
158
+ // (#998 — after `terraform state rm`, the block would re-create it).
81
159
  const api = readFileSync(join(dir, "api.tf"), "utf-8");
82
160
  expect(api).toContain("data.aws_s3_bucket.assets.bucket");
83
- expect(readFileSync(join(dir, "bucket.tf"), "utf-8")).toBe(BUCKET_TF);
161
+ expect(readFileSync(join(dir, "bucket.tf"), "utf-8")).not.toContain('resource "aws_s3_bucket" "assets"');
84
162
  });
85
163
  });
86
164
 
@@ -10,14 +10,17 @@
10
10
  */
11
11
 
12
12
  import { existsSync, statSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from "fs";
13
- import { join, basename } from "path";
13
+ import { join, basename, relative, resolve } from "path";
14
14
  import { parseTerraformDir, Hcl2JsonNotInstalled } from "../../terraform/parse";
15
- import { boundaryReport } from "../../terraform/carve";
15
+ import { boundaryReport, type CarveReport } from "../../terraform/carve";
16
16
  import { generateBridge, type BridgePlan, type CarvedIdentity } from "../../terraform/bridge";
17
17
  import { IDENTITY_ATTR } from "../../terraform/tier-map";
18
+ import { resolveCarveManifest, writeCarveManifest, type CarveManifest } from "../../terraform/manifest";
19
+ import { newFileDiff, unifiedDiff } from "../../terraform/unified-diff";
18
20
 
19
21
  export interface CarveBridgeOptions {
20
22
  from?: string;
23
+ /** Optional when the output dir holds a single carve manifest from `carve emit`. */
21
24
  select?: string;
22
25
  statePath?: string;
23
26
  /** Output directory for proposed files (default `<from>/carveout`). */
@@ -34,6 +37,12 @@ export interface CarveBridgeResult {
34
37
  written?: string[];
35
38
  /** True if survivor files were edited in place. */
36
39
  appliedInPlace?: boolean;
40
+ /** The git-applyable `.patch` carrying the whole survivor edit. */
41
+ patchPath?: string;
42
+ /** The carve state manifest this bridge composed with / recorded into. */
43
+ manifestPath?: string;
44
+ /** True when the target came from the manifest, not --select. */
45
+ selectFromManifest?: boolean;
37
46
  }
38
47
 
39
48
  function listTfFiles(dir: string): string[] {
@@ -45,16 +54,25 @@ function listTfFiles(dir: string): string[] {
45
54
 
46
55
  export async function carveBridge(opts: CarveBridgeOptions): Promise<CarveBridgeResult> {
47
56
  if (!opts.from) return { ok: false, error: "chant carve bridge requires --from <terraform-dir>" };
48
- if (!opts.select) return { ok: false, error: "chant carve bridge requires --select <tf-address>" };
49
57
  if (!existsSync(opts.from) || !statSync(opts.from).isDirectory()) {
50
58
  return { ok: false, error: `Not a directory: ${opts.from}` };
51
59
  }
52
60
 
61
+ // Compose with the carve state manifest `carve emit` persisted: without
62
+ // --select it supplies the target (and tfstate), with it it is updated below.
63
+ const outDir = opts.output ?? join(opts.from, "carveout");
64
+ const resolved = resolveCarveManifest(outDir, opts.select);
65
+ if (!opts.select && resolved.error) return { ok: false, error: resolved.error };
66
+ const select = opts.select ?? resolved.manifest!.target;
67
+ const statePath = opts.statePath ?? resolved.manifest?.statePath;
68
+
53
69
  let plan: BridgePlan;
70
+ let report: CarveReport;
54
71
  try {
55
- const graph = await parseTerraformDir(opts.from, { statePath: opts.statePath });
56
- const report = boundaryReport(graph, opts.select);
57
- if (!report) return { ok: false, error: `${opts.select} not found in ${opts.from}` };
72
+ const graph = await parseTerraformDir(opts.from, { statePath });
73
+ const found = boundaryReport(graph, select);
74
+ if (!found) return { ok: false, error: `${select} not found in ${opts.from}` };
75
+ report = found;
58
76
 
59
77
  // Physical identities for the carved resources, for the data sources.
60
78
  const identities = new Map<string, CarvedIdentity>();
@@ -71,8 +89,7 @@ export async function carveBridge(opts: CarveBridgeOptions): Promise<CarveBridge
71
89
  return { ok: false, error: `Failed to build the bridge: ${err instanceof Error ? err.message : String(err)}` };
72
90
  }
73
91
 
74
- const slug = opts.select.replace(/[^A-Za-z0-9_]+/g, "-");
75
- const outDir = opts.output ?? join(opts.from, "carveout");
92
+ const slug = select.replace(/[^A-Za-z0-9_]+/g, "-");
76
93
  mkdirSync(outDir, { recursive: true });
77
94
  const written: string[] = [];
78
95
 
@@ -101,19 +118,66 @@ export async function carveBridge(opts: CarveBridgeOptions): Promise<CarveBridge
101
118
  }
102
119
  }
103
120
 
104
- return { ok: true, plan, written, appliedInPlace: opts.applyRewrites && changed.length > 0 };
121
+ // The whole survivor edit as one git-applyable patch: the data-source file as
122
+ // a new file, plus the rewired references — `cd <from> && git apply <patch>`.
123
+ let patchPath: string | undefined;
124
+ if (plan.dataSources.length || changed.length) {
125
+ const chunks: string[] = [];
126
+ if (plan.dataSources.length) {
127
+ chunks.push(newFileDiff(`${slug}-datasources.tf`, plan.dataSources.map((d) => d.hcl).join("\n\n") + "\n"));
128
+ }
129
+ for (const r of changed) {
130
+ chunks.push(unifiedDiff(relative(opts.from, r.path), r.original, r.rewritten));
131
+ }
132
+ patchPath = join(outDir, `${slug}-bridge.patch`);
133
+ writeFileSync(patchPath, chunks.join(""));
134
+ written.push(patchPath);
135
+ }
136
+
137
+ // Record the bridge into the manifest (create one on a standalone bridge),
138
+ // so `carve apply` composes with this step too.
139
+ const applied = Boolean(opts.applyRewrites) && changed.length > 0;
140
+ const manifest: CarveManifest = resolved.manifest ?? {
141
+ version: 1,
142
+ target: select,
143
+ from: resolve(opts.from),
144
+ statePath: statePath ? resolve(statePath) : undefined,
145
+ boundary: report,
146
+ };
147
+ manifest.boundary = report;
148
+ manifest.bridge = {
149
+ written: written.map((w) => resolve(w)),
150
+ appliedInPlace: applied,
151
+ patch: patchPath ? resolve(patchPath) : undefined,
152
+ excised: plan.excised.length ? plan.excised : undefined,
153
+ at: new Date().toISOString(),
154
+ };
155
+ const manifestPath = writeCarveManifest(outDir, manifest);
156
+
157
+ return {
158
+ ok: true,
159
+ plan,
160
+ written,
161
+ appliedInPlace: applied,
162
+ patchPath,
163
+ manifestPath,
164
+ selectFromManifest: !opts.select,
165
+ };
105
166
  }
106
167
 
107
168
  export function formatCarveBridge(result: CarveBridgeResult): string {
108
169
  if (!result.ok || !result.plan) return result.error ?? "carve bridge failed";
109
170
  const p = result.plan;
110
171
  const L: string[] = [];
111
- L.push(`Boundary bridge for ${p.target}:`);
172
+ L.push(`Boundary bridge for ${p.target}${result.selectFromManifest ? " (target from the carve manifest)" : ""}:`);
112
173
  if (p.dataSources.length) {
113
174
  L.push(` ${p.dataSources.length} data source(s) for survivors to read:`);
114
175
  for (const d of p.dataSources) L.push(` data.${d.type}.${d.name} (was ${d.address})`);
115
176
  } else {
116
- L.push(" no inbound edges — no survivor patch needed.");
177
+ L.push(" no inbound edges — nothing reads the carved resource, no data source needed.");
178
+ }
179
+ if (p.excised.length) {
180
+ L.push(` carved block(s) excised from the survivor source: ${p.excised.join(", ")}`);
117
181
  }
118
182
  const changed = p.rewrites.filter((r) => r.changed);
119
183
  if (changed.length) {
@@ -124,6 +188,10 @@ export function formatCarveBridge(result: CarveBridgeResult): string {
124
188
  for (const d of p.deferredInputs) L.push(` - ${d.note}`);
125
189
  }
126
190
  L.push("");
191
+ if (result.patchPath && !result.appliedInPlace) {
192
+ L.push(`One git-applyable patch carries the whole edit: git apply --directory=<terraform-dir> ${resolve(result.patchPath)}`);
193
+ L.push(" (--directory is the Terraform dir relative to your repo root; plain `git apply` from the dir works outside a repo.)");
194
+ }
127
195
  if (result.appliedInPlace) {
128
196
  L.push("Rewritten survivor Terraform in place. Review with `git diff`, then `terraform plan`.");
129
197
  } else {