@intentius/chant 0.28.0 → 0.30.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 (106) hide show
  1. package/dist/cli/handlers/components.d.ts.map +1 -1
  2. package/dist/cli/handlers/graph.d.ts.map +1 -1
  3. package/dist/cli/handlers/lifecycle.d.ts +5 -3
  4. package/dist/cli/handlers/lifecycle.d.ts.map +1 -1
  5. package/dist/cli/main.d.ts.map +1 -1
  6. package/dist/cli/registry.d.ts +14 -0
  7. package/dist/cli/registry.d.ts.map +1 -1
  8. package/dist/config.d.ts +46 -4
  9. package/dist/config.d.ts.map +1 -1
  10. package/dist/deep-observation.d.ts +257 -0
  11. package/dist/deep-observation.d.ts.map +1 -0
  12. package/dist/discovery/fold-import.d.ts +153 -17
  13. package/dist/discovery/fold-import.d.ts.map +1 -1
  14. package/dist/discovery/sandbox/config-wire.d.ts +3 -2
  15. package/dist/discovery/sandbox/config-wire.d.ts.map +1 -1
  16. package/dist/env.d.ts +5 -2
  17. package/dist/env.d.ts.map +1 -1
  18. package/dist/fold/fold.d.ts +35 -3
  19. package/dist/fold/fold.d.ts.map +1 -1
  20. package/dist/fold/subset.d.ts +9 -0
  21. package/dist/fold/subset.d.ts.map +1 -1
  22. package/dist/graph-ir.d.ts +73 -4
  23. package/dist/graph-ir.d.ts.map +1 -1
  24. package/dist/index.d.ts +2 -0
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/kubectl-context.d.ts +27 -0
  27. package/dist/kubectl-context.d.ts.map +1 -1
  28. package/dist/lexicon.d.ts +78 -6
  29. package/dist/lexicon.d.ts.map +1 -1
  30. package/dist/lifecycle/change-set.d.ts +26 -5
  31. package/dist/lifecycle/change-set.d.ts.map +1 -1
  32. package/dist/lifecycle/deep-diff.d.ts +103 -0
  33. package/dist/lifecycle/deep-diff.d.ts.map +1 -0
  34. package/dist/lifecycle/deep-observe.d.ts +62 -0
  35. package/dist/lifecycle/deep-observe.d.ts.map +1 -0
  36. package/dist/lifecycle/index.d.ts +3 -0
  37. package/dist/lifecycle/index.d.ts.map +1 -1
  38. package/dist/lifecycle/live-diff.d.ts +25 -1
  39. package/dist/lifecycle/live-diff.d.ts.map +1 -1
  40. package/dist/lifecycle/observation-baseline.d.ts +118 -0
  41. package/dist/lifecycle/observation-baseline.d.ts.map +1 -0
  42. package/dist/lifecycle/observe.d.ts +4 -2
  43. package/dist/lifecycle/observe.d.ts.map +1 -1
  44. package/dist/lifecycle/snapshot.d.ts.map +1 -1
  45. package/dist/lifecycle/status.d.ts +26 -1
  46. package/dist/lifecycle/status.d.ts.map +1 -1
  47. package/dist/lifecycle/types.d.ts +8 -0
  48. package/dist/lifecycle/types.d.ts.map +1 -1
  49. package/dist/live-endpoint.d.ts +92 -0
  50. package/dist/live-endpoint.d.ts.map +1 -0
  51. package/dist/observation.d.ts +123 -0
  52. package/dist/observation.d.ts.map +1 -0
  53. package/dist/stack-output.d.ts.map +1 -1
  54. package/package.json +1 -1
  55. package/src/cli/handlers/components.test.ts +63 -4
  56. package/src/cli/handlers/components.ts +78 -35
  57. package/src/cli/handlers/graph.test.ts +155 -6
  58. package/src/cli/handlers/graph.ts +124 -29
  59. package/src/cli/handlers/lifecycle.test.ts +410 -6
  60. package/src/cli/handlers/lifecycle.ts +475 -182
  61. package/src/cli/main.test.ts +6 -0
  62. package/src/cli/main.ts +12 -0
  63. package/src/cli/registry.ts +14 -0
  64. package/src/config.test.ts +75 -0
  65. package/src/config.ts +61 -3
  66. package/src/deep-observation.test.ts +234 -0
  67. package/src/deep-observation.ts +489 -0
  68. package/src/discovery/fold-composite.test.ts +594 -0
  69. package/src/discovery/fold-import.test.ts +372 -1
  70. package/src/discovery/fold-import.ts +1216 -116
  71. package/src/discovery/sandbox/config-wire.ts +3 -2
  72. package/src/env.test.ts +12 -0
  73. package/src/env.ts +12 -4
  74. package/src/fold/fold.test.ts +105 -0
  75. package/src/fold/fold.ts +100 -20
  76. package/src/fold/subset.test.ts +38 -7
  77. package/src/fold/subset.ts +9 -0
  78. package/src/graph-ir-live.test.ts +28 -1
  79. package/src/graph-ir.ts +115 -12
  80. package/src/index.ts +2 -0
  81. package/src/kubectl-context.ts +81 -0
  82. package/src/lexicon.ts +100 -6
  83. package/src/lifecycle/change-set.test.ts +93 -1
  84. package/src/lifecycle/change-set.ts +65 -13
  85. package/src/lifecycle/deep-diff.test.ts +157 -0
  86. package/src/lifecycle/deep-diff.ts +213 -0
  87. package/src/lifecycle/deep-observe.test.ts +174 -0
  88. package/src/lifecycle/deep-observe.ts +173 -0
  89. package/src/lifecycle/index.ts +3 -0
  90. package/src/lifecycle/live-diff.test.ts +39 -0
  91. package/src/lifecycle/live-diff.ts +51 -5
  92. package/src/lifecycle/observation-baseline.test.ts +99 -0
  93. package/src/lifecycle/observation-baseline.ts +217 -0
  94. package/src/lifecycle/observe.test.ts +74 -3
  95. package/src/lifecycle/observe.ts +82 -22
  96. package/src/lifecycle/snapshot.test.ts +39 -1
  97. package/src/lifecycle/snapshot.ts +40 -20
  98. package/src/lifecycle/status.test.ts +89 -8
  99. package/src/lifecycle/status.ts +53 -3
  100. package/src/lifecycle/types.ts +8 -0
  101. package/src/live-endpoint.test.ts +115 -0
  102. package/src/live-endpoint.ts +148 -0
  103. package/src/observation.test.ts +96 -0
  104. package/src/observation.ts +213 -0
  105. package/src/stack-output.test.ts +55 -0
  106. package/src/stack-output.ts +41 -20
@@ -0,0 +1,148 @@
1
+ /**
2
+ * chant #1166 — let a declared `environment` carry its own endpoint so
3
+ * `--live --env <name>` is self-sufficient.
4
+ *
5
+ * The bug this closes: `chant graph --live --env floci` (and `lifecycle
6
+ * diff`/`plan`) observe a stack by shelling out through each lexicon's
7
+ * `describeResources()`. For AWS that shell-out honors the ambient
8
+ * `AWS_ENDPOINT_URL` env var — when a project's `floci` environment is a local
9
+ * emulator (`http://localhost:4566`) but the invoking shell never exported
10
+ * that var, the AWS CLI silently targets real AWS instead. The stack named
11
+ * after the environment doesn't exist there, so `describeResources` hits its
12
+ * `stackDoesNotExist` branch and returns an empty, unremarkable "nothing is
13
+ * deployed" — indistinguishable from the truthful answer. This cost real
14
+ * debugging time validating #1162's live overlay: the observation code was
15
+ * right, the manual repro just never set the var.
16
+ *
17
+ * The fix: `environments` in `chant.config.ts` can name an endpoint per
18
+ * environment (`config.ts`'s `EnvironmentDeclaration`), and {@link
19
+ * applyLiveEndpoint} injects it into the ambient env var each observing
20
+ * lexicon's CLI shell-out actually reads — but only for a var that isn't
21
+ * already set. Ambient always wins: a shell that already exports
22
+ * `AWS_ENDPOINT_URL` sees no change in behavior.
23
+ *
24
+ * Audited (#1166) which lexicons have an ambient-env-var endpoint knob at
25
+ * all, since that's the specific footgun — a lexicon whose environment
26
+ * binding is resolved from `chant.config` itself (not an ambient var) has
27
+ * nothing to inject here:
28
+ *
29
+ * - **aws** — `AWS_ENDPOINT_URL`, read directly by
30
+ * `lexicons/aws/src/components/cloud-executor.ts` / `plugin.ts` before
31
+ * every `aws …` shell-out (`applyAwsEndpoint`/`applyAwsEndpointArgv`).
32
+ * - **fly** — `FLY_FLAPS_BASE_URL`, read by `resolveEndpoint()` in
33
+ * `lexicons/fly/src/op/activities/fly-apply.ts`, the same seam
34
+ * `describeResources` (`../describe-resources.ts`) calls through.
35
+ * - **gcp**, **k8s** — resolve their live target from `chant.config` itself
36
+ * (`k8s.profiles.<env>.context` via `resolveClusterTarget`,
37
+ * `packages/core/src/kubectl-context.ts`), not an ambient var. Nothing to
38
+ * inject: the config *is* the binding already.
39
+ * - **azure** — resolves via the `az` CLI's own logged-in
40
+ * subscription/session context; no ambient endpoint var exists to miss.
41
+ * - **temporal** — resolves its connection from `temporal.profiles.<env>`
42
+ * (`resolveProfile`, `lexicons/temporal/src/describe-resources.ts`), the
43
+ * same "config is the binding" shape as k8s/gcp.
44
+ */
45
+
46
+ import { environmentEndpoint, type EnvironmentDeclaration } from "./config";
47
+
48
+ /**
49
+ * Per-lexicon ambient env var a `--live` read honors for its endpoint. Only
50
+ * lexicons with a genuine ambient-var footgun are listed — see the module doc
51
+ * for the full audit (gcp/k8s/azure/temporal resolve their target from
52
+ * `chant.config` instead, so they have nothing to inject).
53
+ */
54
+ export const LEXICON_ENDPOINT_ENV_VAR: Record<string, string> = {
55
+ aws: "AWS_ENDPOINT_URL",
56
+ fly: "FLY_FLAPS_BASE_URL",
57
+ };
58
+
59
+ /** Result of {@link applyLiveEndpoint} — always call `restore()`, even when nothing was applied (it is then a no-op). */
60
+ export interface AppliedEndpoint {
61
+ /**
62
+ * One line describing what happened, or `undefined` when the environment
63
+ * declares no endpoint at all (nothing to say). Present whether the
64
+ * declared endpoint was applied OR an ambient var already won — #1166's
65
+ * "no silent anything" stance: an operator should never have to guess which
66
+ * target a `--live` read actually used.
67
+ */
68
+ notice?: string;
69
+ /** Undo whatever ambient env vars this call set. Always safe to call. */
70
+ restore: () => void;
71
+ }
72
+
73
+ /**
74
+ * Resolve `environment`'s declared endpoint (if any) from `config.environments`
75
+ * and apply it to the ambient env var of every lexicon in `lexicons` that has
76
+ * one ({@link LEXICON_ENDPOINT_ENV_VAR}) — but only when that var isn't
77
+ * already set. Ambient always wins (#1166): behavior for a shell that already
78
+ * exports `AWS_ENDPOINT_URL` is unchanged.
79
+ *
80
+ * Call before a `--live` describe/enrich pass; `restore()` in a `finally` so
81
+ * the injected value never leaks into a later invocation in the same process
82
+ * (tests, or a long-lived host like the MCP server).
83
+ */
84
+ export function applyLiveEndpoint(
85
+ environments: EnvironmentDeclaration[] | undefined,
86
+ environment: string,
87
+ lexicons: readonly string[],
88
+ env: NodeJS.ProcessEnv = process.env,
89
+ ): AppliedEndpoint {
90
+ const endpoint = environmentEndpoint(environments, environment);
91
+ if (!endpoint) return { restore: () => {} };
92
+
93
+ const applied: string[] = [];
94
+ const overridden: string[] = [];
95
+ const seen = new Set<string>(); // a var shared by two lexicons is only reported once
96
+ for (const lexicon of lexicons) {
97
+ const varName = LEXICON_ENDPOINT_ENV_VAR[lexicon];
98
+ if (!varName || seen.has(varName)) continue;
99
+ seen.add(varName);
100
+ if (env[varName]) {
101
+ overridden.push(varName);
102
+ continue;
103
+ }
104
+ env[varName] = endpoint;
105
+ applied.push(varName);
106
+ }
107
+
108
+ const notices: string[] = [];
109
+ if (applied.length > 0) {
110
+ notices.push(
111
+ `environment "${environment}" declares endpoint ${endpoint} — applied to ${applied.join(", ")} for this read`,
112
+ );
113
+ }
114
+ if (overridden.length > 0) {
115
+ notices.push(
116
+ `ambient ${overridden.join(", ")} already set — keeping it over environment "${environment}"'s declared endpoint (${endpoint})`,
117
+ );
118
+ }
119
+
120
+ return {
121
+ notice: notices.length > 0 ? notices.join("; ") : undefined,
122
+ restore: () => {
123
+ for (const varName of applied) delete env[varName];
124
+ },
125
+ };
126
+ }
127
+
128
+ /**
129
+ * #1166 acceptance: when a `--live` describe comes back with zero resources
130
+ * for a lexicon that had declared entities to look for, and nothing was
131
+ * already reported NOT-OBSERVED (#1089) either, that is either "genuinely
132
+ * nothing is deployed yet" or a misconfigured endpoint/credentials — the two
133
+ * are visually identical, so a caller must say so rather than stay quiet.
134
+ * Returns `undefined` when there is nothing to declare (no declared entities
135
+ * to have asked about, or the emptiness is already explained by #1089's
136
+ * `unobserved`).
137
+ */
138
+ export function zeroResourcesWarning(
139
+ lexicon: string,
140
+ environment: string,
141
+ declaredCount: number,
142
+ observed: { resources: Record<string, unknown>; unobserved: Record<string, unknown> },
143
+ ): string | undefined {
144
+ if (declaredCount === 0) return undefined;
145
+ if (Object.keys(observed.resources).length > 0) return undefined;
146
+ if (Object.keys(observed.unobserved).length > 0) return undefined;
147
+ return `${lexicon}: 0 live resources for env "${environment}" (${declaredCount} declared) — check the endpoint/credentials`;
148
+ }
@@ -0,0 +1,96 @@
1
+ /**
2
+ * The observation contract itself (#1089) — the normalizer, the envelope
3
+ * discriminant, and the multi-stack merge.
4
+ */
5
+ import { describe, test, expect } from "vitest";
6
+ import {
7
+ UNOBSERVED_REASONS,
8
+ formatUnobserved,
9
+ isObservationResult,
10
+ isUnobservedReason,
11
+ mergeObservations,
12
+ normalizeObservation,
13
+ observation,
14
+ unobservedAll,
15
+ unobservedReasonText,
16
+ } from "./observation";
17
+ import type { ResourceMetadata } from "./lexicon";
18
+
19
+ const meta = (over: Partial<ResourceMetadata> = {}): ResourceMetadata => ({
20
+ type: "Fake::Resource",
21
+ status: "OK",
22
+ ...over,
23
+ });
24
+
25
+ describe("normalizeObservation", () => {
26
+ test("a bare map means 'I looked at everything'", () => {
27
+ expect(normalizeObservation({ a: meta() })).toEqual({ resources: { a: meta() }, unobserved: {} });
28
+ });
29
+
30
+ test("the envelope carries both halves", () => {
31
+ const value = observation({ a: meta() }, { b: { reason: "read-failed" } });
32
+ expect(normalizeObservation(value)).toEqual({
33
+ resources: { a: meta() },
34
+ unobserved: { b: { reason: "read-failed" } },
35
+ });
36
+ });
37
+
38
+ test("undefined normalizes to two empty maps", () => {
39
+ expect(normalizeObservation(undefined)).toEqual({ resources: {}, unobserved: {} });
40
+ });
41
+
42
+ test("an entity literally named `observation` cannot be mistaken for the envelope", () => {
43
+ const bare = { observation: meta({ type: "Odd::Name" }) };
44
+ expect(isObservationResult(bare)).toBe(false);
45
+ expect(normalizeObservation(bare).resources.observation.type).toBe("Odd::Name");
46
+ });
47
+
48
+ test("the envelope omits an empty unobserved map", () => {
49
+ expect(observation({ a: meta() }, {})).toEqual({ observation: "v1", resources: { a: meta() } });
50
+ });
51
+ });
52
+
53
+ describe("unobservedAll", () => {
54
+ test("marks every named entity with one reason, carrying declared types", () => {
55
+ const entities = new Map([["a", { entityType: "AWS::S3::Bucket" }]]);
56
+ expect(unobservedAll(["a", "b"], "no-credentials", "token expired", entities)).toEqual({
57
+ a: { reason: "no-credentials", type: "AWS::S3::Bucket", detail: "token expired" },
58
+ b: { reason: "no-credentials", detail: "token expired" },
59
+ });
60
+ });
61
+ });
62
+
63
+ describe("mergeObservations (multi-stack)", () => {
64
+ test("present beats not-observed beats absent", () => {
65
+ const merged = mergeObservations([
66
+ { resources: {}, unobserved: { a: { reason: "read-failed" }, b: { reason: "no-binding" } } },
67
+ { resources: { a: meta() }, unobserved: {} },
68
+ ]);
69
+ expect(Object.keys(merged.resources)).toEqual(["a"]);
70
+ expect(Object.keys(merged.unobserved)).toEqual(["b"]);
71
+ });
72
+
73
+ test("an entity nobody looked for in any stack stays absent", () => {
74
+ const merged = mergeObservations([
75
+ { resources: { a: meta() }, unobserved: {} },
76
+ { resources: { b: meta() }, unobserved: {} },
77
+ ]);
78
+ expect(merged.unobserved).toEqual({});
79
+ });
80
+ });
81
+
82
+ describe("reason totality", () => {
83
+ test("every reason has human text and passes the guard", () => {
84
+ for (const reason of UNOBSERVED_REASONS) {
85
+ expect(isUnobservedReason(reason)).toBe(true);
86
+ expect(unobservedReasonText(reason).length).toBeGreaterThan(0);
87
+ }
88
+ expect(isUnobservedReason("made-up")).toBe(false);
89
+ });
90
+
91
+ test("formatUnobserved names the entity, the type and the detail", () => {
92
+ expect(
93
+ formatUnobserved("widget", { type: "K8s::X::Widget", reason: "unsupported-kind", detail: "no mapping" }),
94
+ ).toBe("widget (K8s::X::Widget) — no reader for this resource kind: no mapping");
95
+ });
96
+ });
@@ -0,0 +1,213 @@
1
+ /**
2
+ * The observation contract (#1089) — what a lexicon's `describeResources()`
3
+ * is allowed to mean.
4
+ *
5
+ * Before this module there were two ways for an observation to return nothing
6
+ * for a declared entity and no way to tell them apart:
7
+ *
8
+ * 1. The provider was asked and said the resource does not exist.
9
+ * 2. The lexicon never looked — no reader for that kind, the read errored,
10
+ * no credentials, no cluster/subscription binding.
11
+ *
12
+ * Both arrived at the change set as "absent", and absent + declared classifies
13
+ * as `create`. So `chant lifecycle plan` proposed creating a Kubernetes CRD
14
+ * that already existed in the cluster, with nothing in the plan to say the tool
15
+ * had simply not looked (a warn on stderr is not a signal in a change set).
16
+ *
17
+ * The contract is a tri-state, per declared entity:
18
+ *
19
+ * - **OBSERVED-PRESENT** — a key in `resources`. The provider returned it.
20
+ * - **OBSERVED-ABSENT** — in neither map. The lexicon looked and the provider
21
+ * reported it missing. This is the only shape that may become a `create`.
22
+ * - **NOT-OBSERVED** — a key in `unobserved`, carrying a total
23
+ * {@link UnobservedReason}. Never a `create`, never a `delete`; consumers
24
+ * surface it and stop.
25
+ *
26
+ * Compatibility: `describeResources()` may still return the bare
27
+ * `name → ResourceMetadata` map it always did — that means "everything I was
28
+ * asked about, I looked at" and normalizes to an empty `unobserved`. Reporting
29
+ * NOT-OBSERVED requires the explicitly versioned {@link ObservationResult}
30
+ * envelope, which is discriminated by its literal `observation: "v1"` field (a
31
+ * bare map's value at any key is a `ResourceMetadata` object, never that
32
+ * string, so the two shapes can never be confused).
33
+ */
34
+
35
+ import type { ResourceMetadata } from "./lexicon";
36
+
37
+ /**
38
+ * Why a declared entity was not observed. Total: a lexicon that cannot observe
39
+ * an entity must pick one of these, and consumers may switch exhaustively.
40
+ *
41
+ * - `read-failed` — the provider was reached and the read errored (a non-zero
42
+ * CLI exit, a 5xx, an unparseable response).
43
+ * - `no-credentials` — no usable credentials/authorization for the target.
44
+ * - `no-binding` — the environment resolves to no concrete target (no kubectl
45
+ * context, no subscription, no stack, no endpoint).
46
+ * - `unsupported-kind` — the lexicon has no reader for this entity type (the
47
+ * K8s CRD case, Azure's nested ARM types). The resource may well exist.
48
+ * - `filtered` — the entity was reached but withheld by a caller-requested
49
+ * filter (`owned: true` against a resource carrying no chant marker). The
50
+ * result deliberately says nothing about it, which is still not absence: a
51
+ * declared resource that exists but is foreign must not classify as `create`.
52
+ */
53
+ export type UnobservedReason =
54
+ | "read-failed"
55
+ | "no-credentials"
56
+ | "no-binding"
57
+ | "unsupported-kind"
58
+ | "filtered";
59
+
60
+ /** Every legal {@link UnobservedReason}, for validation and conformance checks. */
61
+ export const UNOBSERVED_REASONS: readonly UnobservedReason[] = [
62
+ "read-failed",
63
+ "no-credentials",
64
+ "no-binding",
65
+ "unsupported-kind",
66
+ "filtered",
67
+ ];
68
+
69
+ /** True when `value` is a legal {@link UnobservedReason}. */
70
+ export function isUnobservedReason(value: unknown): value is UnobservedReason {
71
+ return typeof value === "string" && (UNOBSERVED_REASONS as readonly string[]).includes(value);
72
+ }
73
+
74
+ /** One declared entity the lexicon could not observe, and why. */
75
+ export interface UnobservedEntity {
76
+ /** Declared entity type, when the lexicon knows it (it usually does — the entity is declared). */
77
+ type?: string;
78
+ /** Total verdict. */
79
+ reason: UnobservedReason;
80
+ /** Human-readable detail: the command that failed, the missing binding key, the unsupported kind. */
81
+ detail?: string;
82
+ }
83
+
84
+ /**
85
+ * The observation envelope — a `describeResources()` return value that can say
86
+ * "I did not look at this one". Explicitly versioned: `observation: "v1"`.
87
+ */
88
+ export interface ObservationResult {
89
+ /** Discriminant + wire version. Distinguishes the envelope from the bare `name → ResourceMetadata` map. */
90
+ readonly observation: "v1";
91
+ /** OBSERVED-PRESENT, keyed by chant entity name. */
92
+ resources: Record<string, ResourceMetadata>;
93
+ /** NOT-OBSERVED, keyed by chant entity name. Omit or leave empty when everything asked about was looked at. */
94
+ unobserved?: Record<string, UnobservedEntity>;
95
+ }
96
+
97
+ /**
98
+ * What `describeResources()` may return: the bare map (pre-#1089, still valid —
99
+ * "I looked at everything") or the {@link ObservationResult} envelope.
100
+ */
101
+ export type DescribeResourcesResult = Record<string, ResourceMetadata> | ObservationResult;
102
+
103
+ /** Normalized form every consumer works with. Both maps always present. */
104
+ export interface NormalizedObservation {
105
+ resources: Record<string, ResourceMetadata>;
106
+ unobserved: Record<string, UnobservedEntity>;
107
+ }
108
+
109
+ /** True when `value` is the versioned {@link ObservationResult} envelope. */
110
+ export function isObservationResult(value: unknown): value is ObservationResult {
111
+ return (
112
+ typeof value === "object" &&
113
+ value !== null &&
114
+ (value as { observation?: unknown }).observation === "v1"
115
+ );
116
+ }
117
+
118
+ /**
119
+ * Build an {@link ObservationResult}. Lexicons use this rather than writing the
120
+ * discriminant by hand.
121
+ */
122
+ export function observation(
123
+ resources: Record<string, ResourceMetadata>,
124
+ unobserved?: Record<string, UnobservedEntity>,
125
+ ): ObservationResult {
126
+ return {
127
+ observation: "v1",
128
+ resources,
129
+ ...(unobserved && Object.keys(unobserved).length > 0 ? { unobserved } : {}),
130
+ };
131
+ }
132
+
133
+ /**
134
+ * Normalize either accepted return shape. `undefined` (a lexicon that returned
135
+ * nothing at all) normalizes to two empty maps — which reads as "everything was
136
+ * observed absent", so callers that mean "the read failed" must say so with
137
+ * {@link unobservedAll} rather than returning nothing.
138
+ */
139
+ export function normalizeObservation(value: DescribeResourcesResult | undefined): NormalizedObservation {
140
+ if (!value) return { resources: {}, unobserved: {} };
141
+ if (isObservationResult(value)) {
142
+ return { resources: value.resources ?? {}, unobserved: value.unobserved ?? {} };
143
+ }
144
+ return { resources: value, unobserved: {} };
145
+ }
146
+
147
+ /**
148
+ * Mark every named entity NOT-OBSERVED with one reason — the whole-lexicon
149
+ * failure case (the provider CLI is missing, the cluster binding refused, the
150
+ * credentials are gone). Core applies this when `describeResources()` throws,
151
+ * so a thrown read degrades to an honest "did not look" for each declared
152
+ * entity instead of an empty map that classifies as N creates.
153
+ */
154
+ export function unobservedAll(
155
+ names: Iterable<string>,
156
+ reason: UnobservedReason,
157
+ detail?: string,
158
+ types?: Map<string, { entityType: string }> | Record<string, string>,
159
+ ): Record<string, UnobservedEntity> {
160
+ const typeOf = (name: string): string | undefined => {
161
+ if (!types) return undefined;
162
+ if (types instanceof Map) return types.get(name)?.entityType;
163
+ return types[name];
164
+ };
165
+ const out: Record<string, UnobservedEntity> = {};
166
+ for (const name of names) {
167
+ const type = typeOf(name);
168
+ out[name] = { reason, ...(type ? { type } : {}), ...(detail ? { detail } : {}) };
169
+ }
170
+ return out;
171
+ }
172
+
173
+ /**
174
+ * Union several observations of the same lexicon (the multi-stack read, where
175
+ * `describeResources` runs once per stack). Precedence is
176
+ * present > not-observed > absent: a resource found in any stack is present; an
177
+ * entity nobody could look at stays not-observed; an entity every reader looked
178
+ * for and did not find is absent.
179
+ */
180
+ export function mergeObservations(parts: Iterable<NormalizedObservation>): NormalizedObservation {
181
+ const resources: Record<string, ResourceMetadata> = {};
182
+ const unobserved: Record<string, UnobservedEntity> = {};
183
+ for (const part of parts) {
184
+ Object.assign(resources, part.resources);
185
+ Object.assign(unobserved, part.unobserved);
186
+ }
187
+ // Present wins: a stack that could not be read does not un-observe a resource
188
+ // another stack returned.
189
+ for (const name of Object.keys(resources)) delete unobserved[name];
190
+ return { resources, unobserved };
191
+ }
192
+
193
+ /** One-line human phrasing of a reason, for CLI output. */
194
+ export function unobservedReasonText(reason: UnobservedReason): string {
195
+ switch (reason) {
196
+ case "read-failed":
197
+ return "read failed";
198
+ case "no-credentials":
199
+ return "no credentials";
200
+ case "no-binding":
201
+ return "no binding for this environment";
202
+ case "unsupported-kind":
203
+ return "no reader for this resource kind";
204
+ case "filtered":
205
+ return "withheld by the --owned filter";
206
+ }
207
+ }
208
+
209
+ /** `name — reason (detail)`, the shared rendering for CLI and plan output. */
210
+ export function formatUnobserved(name: string, entry: UnobservedEntity): string {
211
+ const base = `${name}${entry.type ? ` (${entry.type})` : ""} — ${unobservedReasonText(entry.reason)}`;
212
+ return entry.detail ? `${base}: ${entry.detail}` : base;
213
+ }
@@ -97,6 +97,61 @@ describe("stackOutput", () => {
97
97
  expect(out.lexicon).toBe("aws");
98
98
  vi.resetModules();
99
99
  });
100
+
101
+ // chant #1152 — a CloudFormation Parameter carries no attributes at all, so
102
+ // `Ref(param)` (an intrinsic wrapping the Parameter Declarable directly, not
103
+ // one of its attributes) could never contain a nested AttrRef for the old
104
+ // AttrRef-only anchor search to find. Before the fix that meant `firstAttrRef`
105
+ // always came back empty for this shape and the output's `lexicon` silently
106
+ // became `"unknown"` — not a thrown error (a Parameter-wrapping `Ref` already
107
+ // carries the `Intrinsic` marker, so the input-validation guard always
108
+ // accepted it), but `"unknown"` isn't any real lexicon's partition, so the
109
+ // output was silently dropped from every serializer's `Outputs` section.
110
+ const param: Declarable = {
111
+ lexicon: "aws",
112
+ entityType: "AWS::CloudFormation::Parameter",
113
+ [DECLARABLE_MARKER]: true,
114
+ };
115
+
116
+ test("derives lexicon from a Declarable referenced directly by a wrapping intrinsic (Ref(param))", () => {
117
+ const ref = {
118
+ [INTRINSIC_MARKER]: true as const,
119
+ target: param,
120
+ toJSON: () => ({ Ref: "environment" }),
121
+ };
122
+
123
+ const out = stackOutput(ref, { exportName: "EnvironmentName" });
124
+ expect(out.lexicon).toBe("aws");
125
+ expect(out.sourceRef).toBe(ref);
126
+ expect(out.exportName).toBe("EnvironmentName");
127
+ });
128
+
129
+ test("derives lexicon from a Declarable referenced directly, nested inside a further intrinsic (Sub interpolating Ref(param))", () => {
130
+ const ref = {
131
+ [INTRINSIC_MARKER]: true as const,
132
+ target: param,
133
+ toJSON: () => ({ Ref: "environment" }),
134
+ };
135
+ const sub = {
136
+ [INTRINSIC_MARKER]: true as const,
137
+ values: [ref],
138
+ toJSON: () => ({ "Fn::Sub": "env-${environment}" }),
139
+ };
140
+
141
+ const out = stackOutput(sub);
142
+ expect(out.lexicon).toBe("aws");
143
+ });
144
+
145
+ test("an explicit options.lexicon still wins over a Declarable-derived anchor", () => {
146
+ const ref = {
147
+ [INTRINSIC_MARKER]: true as const,
148
+ target: param,
149
+ toJSON: () => ({ Ref: "environment" }),
150
+ };
151
+
152
+ const out = stackOutput(ref, { lexicon: "gcp" });
153
+ expect(out.lexicon).toBe("gcp");
154
+ });
100
155
  });
101
156
 
102
157
  describe("isStackOutput", () => {
@@ -6,7 +6,7 @@
6
6
  * it via `nestedStack().outputs.name`.
7
7
  */
8
8
 
9
- import { DECLARABLE_MARKER, type Declarable } from "./declarable";
9
+ import { DECLARABLE_MARKER, isDeclarable, type Declarable } from "./declarable";
10
10
  import { AttrRef } from "./attrref";
11
11
  import { isIntrinsic, type Intrinsic } from "./intrinsic";
12
12
  import { isAttrRefLike } from "./utils";
@@ -35,25 +35,47 @@ export interface StackOutput extends Declarable {
35
35
  readonly exportName?: string;
36
36
  }
37
37
 
38
- /** Find the first AttrRef anywhere inside a value (walking intrinsics/objects/
39
- * arrays), so a wrapping intrinsic can still borrow its parent's lexicon.
40
- * Duck-type, not `instanceof` (chant #1137): a lexicon built against a
41
- * separate copy of `@intentius/chant` produces AttrRefs that fail
42
- * `instanceof AttrRef` here but carry the same shape — missing one would
43
- * make the walk recurse into the AttrRef's own (unhelpful) fields instead
44
- * of stopping on it, silently failing to find the anchor. */
45
- function firstAttrRef(value: unknown, seen = new Set<unknown>()): AttrRef | undefined {
38
+ /** What a wrapping intrinsic's lexicon gets derived from: either a nested
39
+ * AttrRef (borrow its parent's `.lexicon`) or a Declarable referenced
40
+ * directly (use its own `.lexicon`). Two shapes, not one, because
41
+ * `Ref(someEntity)` (chant #1152) wraps the entity itself — a
42
+ * CloudFormation Parameter carries no attributes at all, so `Ref(param)`
43
+ * can never contain a nested AttrRef for a single-shape search to find. */
44
+ type Anchor = { readonly kind: "attrRef"; readonly ref: AttrRef } | { readonly kind: "declarable"; readonly entity: Declarable };
45
+
46
+ /** Find the first AttrRef or directly-referenced Declarable anywhere inside a
47
+ * value (walking intrinsics/objects/arrays), so a wrapping intrinsic can
48
+ * still borrow its source's lexicon. Duck-type, not `instanceof` (chant
49
+ * #1137): a lexicon built against a separate copy of `@intentius/chant`
50
+ * produces AttrRefs/Declarables that fail `instanceof` here but carry the
51
+ * same shape (`isDeclarable`'s `Symbol.for` marker is already dual-copy-safe
52
+ * on its own) — missing one would make the walk recurse into its own
53
+ * (unhelpful) fields instead of stopping on it, silently failing to find the
54
+ * anchor. */
55
+ function findAnchor(value: unknown, seen = new Set<unknown>()): Anchor | undefined {
46
56
  if (value === null || typeof value !== "object" || seen.has(value)) return undefined;
47
- if (isAttrRefLike(value)) return value;
57
+ if (isAttrRefLike(value)) return { kind: "attrRef", ref: value };
58
+ if (isDeclarable(value)) return { kind: "declarable", entity: value };
48
59
  seen.add(value);
49
60
  const children = Array.isArray(value) ? value : Object.values(value as Record<string, unknown>);
50
61
  for (const child of children) {
51
- const found = firstAttrRef(child, seen);
62
+ const found = findAnchor(child, seen);
52
63
  if (found) return found;
53
64
  }
54
65
  return undefined;
55
66
  }
56
67
 
68
+ /** Derive the lexicon an anchor belongs to: an AttrRef borrows its (deref'd)
69
+ * parent's `.lexicon`; a directly-referenced Declarable reports its own. */
70
+ function anchorLexicon(anchor: Anchor | undefined): string {
71
+ if (!anchor) return "unknown";
72
+ if (anchor.kind === "declarable") return anchor.entity.lexicon;
73
+ const parent = anchor.ref.parent.deref();
74
+ return parent && typeof (parent as Record<string, unknown>).lexicon === "string"
75
+ ? ((parent as Record<string, unknown>).lexicon as string)
76
+ : "unknown";
77
+ }
78
+
57
79
  /**
58
80
  * Type guard for StackOutput.
59
81
  */
@@ -106,16 +128,15 @@ export function stackOutput(
106
128
  );
107
129
  }
108
130
  // Derive lexicon from the referenced entity — for a bare AttrRef, its parent;
109
- // for an intrinsic (Join etc.), the first AttrRef nested inside it. A
110
- // foreign-copy AttrRef failing raw `instanceof` here would fall to
111
- // `firstAttrRef`, which (before its own #1137 fix) would also miss it —
131
+ // for an intrinsic (Join etc.), the first AttrRef or directly-referenced
132
+ // Declarable nested inside it (chant #1152: `Ref(param)`/`Ref(resource)`
133
+ // wrap the entity itself, not one of its attributes). A foreign-copy
134
+ // AttrRef/Declarable failing raw `instanceof` here would fall to
135
+ // `findAnchor`, which (before its own #1137 fix) would also miss it —
112
136
  // silently anchoring on nothing and recording `lexicon: "unknown"`.
113
- const anchor = typeof ref === "string" ? undefined : isAttrRefLike(ref) ? ref : firstAttrRef(ref);
114
- const parent = anchor?.parent.deref();
115
- const derived = parent && typeof (parent as Record<string, unknown>).lexicon === "string"
116
- ? (parent as Record<string, unknown>).lexicon as string
117
- : "unknown";
118
- const lexicon = options?.lexicon ?? derived;
137
+ const anchor: Anchor | undefined =
138
+ typeof ref === "string" ? undefined : isAttrRefLike(ref) ? { kind: "attrRef", ref } : findAnchor(ref);
139
+ const lexicon = options?.lexicon ?? anchorLexicon(anchor);
119
140
 
120
141
  const output: StackOutput = {
121
142
  [STACK_OUTPUT_MARKER]: true,