@intentius/chant 0.33.0 → 0.34.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 (56) hide show
  1. package/dist/cli/commands/onboard.d.ts.map +1 -1
  2. package/dist/cli/handlers/graph.d.ts.map +1 -1
  3. package/dist/cli/handlers/lifecycle.d.ts.map +1 -1
  4. package/dist/cli/handlers/search.d.ts +72 -0
  5. package/dist/cli/handlers/search.d.ts.map +1 -1
  6. package/dist/cli/main.d.ts.map +1 -1
  7. package/dist/cli/registry.d.ts +26 -0
  8. package/dist/cli/registry.d.ts.map +1 -1
  9. package/dist/graph-effective.d.ts.map +1 -1
  10. package/dist/graph-ir.d.ts +21 -0
  11. package/dist/graph-ir.d.ts.map +1 -1
  12. package/dist/graph-refs.d.ts +19 -0
  13. package/dist/graph-refs.d.ts.map +1 -1
  14. package/dist/lexicon.d.ts +141 -0
  15. package/dist/lexicon.d.ts.map +1 -1
  16. package/dist/lifecycle/deep-observe.d.ts +4 -0
  17. package/dist/lifecycle/deep-observe.d.ts.map +1 -1
  18. package/dist/lifecycle/live-diff.d.ts.map +1 -1
  19. package/dist/lifecycle/observe.d.ts +55 -1
  20. package/dist/lifecycle/observe.d.ts.map +1 -1
  21. package/dist/lifecycle/replay.d.ts +47 -0
  22. package/dist/lifecycle/replay.d.ts.map +1 -0
  23. package/dist/lifecycle/snapshot.d.ts +6 -0
  24. package/dist/lifecycle/snapshot.d.ts.map +1 -1
  25. package/dist/lifecycle/types.d.ts +46 -0
  26. package/dist/lifecycle/types.d.ts.map +1 -1
  27. package/dist/observation.d.ts +71 -0
  28. package/dist/observation.d.ts.map +1 -1
  29. package/package.json +1 -1
  30. package/src/cli/commands/onboard.ts +10 -25
  31. package/src/cli/handlers/graph.test.ts +74 -0
  32. package/src/cli/handlers/graph.ts +77 -36
  33. package/src/cli/handlers/lifecycle.test.ts +86 -0
  34. package/src/cli/handlers/lifecycle.ts +43 -10
  35. package/src/cli/handlers/search.test.ts +246 -4
  36. package/src/cli/handlers/search.ts +432 -27
  37. package/src/cli/main.ts +9 -0
  38. package/src/cli/registry.ts +27 -0
  39. package/src/codegen/lexicon-wiring.test.ts +53 -0
  40. package/src/codegen/release-wiring.test.ts +174 -0
  41. package/src/graph-effective.ts +7 -1
  42. package/src/graph-ir-live.test.ts +83 -0
  43. package/src/graph-ir.ts +58 -1
  44. package/src/graph-refs.test.ts +59 -0
  45. package/src/graph-refs.ts +39 -8
  46. package/src/lexicon.ts +145 -0
  47. package/src/lifecycle/deep-observe.ts +5 -0
  48. package/src/lifecycle/live-diff.test.ts +38 -0
  49. package/src/lifecycle/live-diff.ts +45 -2
  50. package/src/lifecycle/observe.ts +186 -4
  51. package/src/lifecycle/replay.ts +141 -0
  52. package/src/lifecycle/snapshot.test.ts +179 -0
  53. package/src/lifecycle/snapshot.ts +88 -3
  54. package/src/lifecycle/types.ts +47 -0
  55. package/src/observation.test.ts +135 -0
  56. package/src/observation.ts +151 -0
@@ -3,9 +3,12 @@ import { build } from "../../build";
3
3
  import { buildGraphIr, buildLiveGraphIr, sourceOverlayGraphs, type GraphIR, type IRNode } from "../../graph-ir";
4
4
  import { buildDeclaredPerStack } from "../../graph-declared";
5
5
  import { enrichEffectiveTopology } from "../../graph-effective";
6
+ import { reconstructEdges, mergeCatalogs, type ReferenceCatalog } from "../../graph-refs";
6
7
  import { discover } from "../../discovery/index";
7
8
 
8
9
  import { observeResources } from "../../lifecycle/observe";
10
+ import { replaySnapshots, hasSnapshot } from "../../lifecycle/replay";
11
+ import type { LiveObservation } from "../../graph-ir";
9
12
  import { loadChantConfig } from "../../config";
10
13
  import { loadPlugins, resolveProjectLexicons } from "../plugins";
11
14
  import { formatError, formatWarning } from "../format";
@@ -41,17 +44,42 @@ export async function runSearch(ctx: CommandContext): Promise<number> {
41
44
  console.error(formatError({ message: "chant search needs a query: chant search \"<terms>\" [--live --env <name>]" }));
42
45
  return 1;
43
46
  }
44
- const terms = parseQuery(query);
47
+ let terms: Term[];
48
+ try {
49
+ terms = parseQuery(query);
50
+ } catch (err) {
51
+ if (err instanceof QueryError) {
52
+ console.error(formatError({ message: err.message, hint: err.hint }));
53
+ return 1;
54
+ }
55
+ throw err;
56
+ }
45
57
  const show = parseShow(args);
46
58
 
47
59
  const projectPath = resolve(".");
48
60
  const { config } = await loadChantConfig(projectPath);
49
61
 
50
62
  let ir: GraphIR;
51
- if (args.live) {
63
+ let source: AnswerSource = { kind: "declared" };
64
+ // Kinds that can exist in the account without being declared (#1278). Known
65
+ // without a scan, so it costs nothing to mention.
66
+ let ambientKinds: string[] = [];
67
+ // Set only on a replay: whether the recording itself holds ambient resources.
68
+ let replayAmbient: { recordedAmbient: boolean } | undefined;
69
+ if (args.live || args.at) {
52
70
  const environment = args.env;
53
71
  if (!environment) {
54
- console.error(formatError({ message: "chant search --live needs an environment: --live --env <name>" }));
72
+ const flag = args.at ? "--at" : "--live";
73
+ console.error(formatError({ message: `chant search ${flag} needs an environment: ${flag} --env <name>` }));
74
+ return 1;
75
+ }
76
+ if (args.live && args.at) {
77
+ // Two different observations of the same estate, and no rule for which
78
+ // wins. Comparing them is a real question (#1268) but it is not this one.
79
+ console.error(formatError({
80
+ message: "chant search takes --live or --at, not both",
81
+ hint: "--live reads the estate now; --at answers from a recorded snapshot",
82
+ }));
55
83
  return 1;
56
84
  }
57
85
  if (config.environments && !config.environments.includes(environment)) {
@@ -65,24 +93,76 @@ export async function runSearch(ctx: CommandContext): Promise<number> {
65
93
  return 1;
66
94
  }
67
95
  const observing = plugins.filter((p) => p.describeResources);
96
+ ambientKinds = observing.flatMap((p) => p.ambientKinds?.() ?? []);
68
97
  const stacks = (config.stacks ?? []).map((s) => ({ name: s.name, region: s.region, src: s.src }));
69
- const { observations, errors } = await observeResources(environment, observing, buildResult, {
70
- owned: true,
71
- stacks,
72
- });
73
- for (const e of errors) console.error(formatWarning({ message: e }));
74
- let live = buildLiveGraphIr(observations);
98
+
99
+ let observations: LiveObservation[];
75
100
  const liveAttrs: Record<string, Record<string, unknown>> = {};
76
- for (const p of observing) {
77
- if (!p.enrichLiveAttrs) continue;
78
- try {
79
- const enriched = await p.enrichLiveAttrs({ environment, owned: true, stacks });
80
- for (const [id, a] of Object.entries(enriched)) liveAttrs[id] = { ...liveAttrs[id], ...a };
81
- live = { ...live, nodes: live.nodes.map((n) => (enriched[n.id] ? { ...n, attrs: { ...n.attrs, ...enriched[n.id] } } : n)) };
82
- } catch {
83
- /* enrichment is best-effort; search still works on describe attrs */
101
+ if (args.at) {
102
+ // Answer from a recorded observation (#1266). Everything downstream is
103
+ // the live path's — the point is that a snapshot replays into the same
104
+ // shape a live read produces, so one pipeline serves both and a fold
105
+ // improvement reaches an old snapshot for free.
106
+ const scoped = new Set(stacks.filter((st) => st.src).map((st) => st.name));
107
+ const replay = await replaySnapshots(environment, String(args.at), scoped);
108
+ if ("error" in replay) {
109
+ console.error(formatError({ message: replay.error, ...(replay.hint ? { hint: replay.hint } : {}) }));
110
+ return 1;
111
+ }
112
+ observations = replay.observations;
113
+ replayAmbient = {
114
+ recordedAmbient: replay.observations.some((o) =>
115
+ Object.values(o.resources).some((m) => m.ambient === true),
116
+ ),
117
+ };
118
+ source = { kind: "snapshot", commit: replay.commit, timestamp: replay.timestamp };
119
+ } else {
120
+ const observed = await observeResources(environment, observing, buildResult, {
121
+ owned: true,
122
+ stacks,
123
+ ambient: args.ambient === true,
124
+ });
125
+ for (const e of observed.errors) console.error(formatWarning({ message: e }));
126
+ observations = observed.observations;
127
+ source = { kind: "live" };
128
+ }
129
+ let live = buildLiveGraphIr(observations);
130
+ // Live-only: enrichment is a provider call, so it has no place in a replay.
131
+ // A recorded answer that quietly reached for the API would stop being one.
132
+ if (!args.at) {
133
+ for (const p of observing) {
134
+ if (!p.enrichLiveAttrs) continue;
135
+ try {
136
+ const enriched = await p.enrichLiveAttrs({ environment, owned: true, stacks });
137
+ for (const [id, a] of Object.entries(enriched)) liveAttrs[id] = { ...liveAttrs[id], ...a };
138
+ live = { ...live, nodes: live.nodes.map((n) => (enriched[n.id] ? { ...n, attrs: { ...n.attrs, ...enriched[n.id] } } : n)) };
139
+ } catch {
140
+ /* enrichment is best-effort; search still works on describe attrs */
141
+ }
84
142
  }
85
143
  }
144
+ // Reconstruct edges from live references (#778), the same way `graph --live`
145
+ // does (#1271). `buildLiveGraphIr` projects nodes only, so without this the
146
+ // live side of the graph has no relationships at all — and a fold over
147
+ // topology has nothing to traverse on anything the declared graph does not
148
+ // already model.
149
+ const catalogs = observing.map((p) => p.referenceCatalog).filter((c): c is ReferenceCatalog => !!c);
150
+ if (catalogs.length > 0) {
151
+ // Merge, never replace. A lexicon reports relationships a catalog cannot
152
+ // reconstruct (#1273) — an instance placed in a subnet it did not declare
153
+ // carries a template `Ref` in its attributes, not the physical subnet id,
154
+ // so no identity index resolves it. Overwriting here dropped exactly those
155
+ // edges and left the fold with a chain missing its first hop.
156
+ const reconstructed = reconstructEdges(live.nodes, mergeCatalogs(catalogs)).edges;
157
+ const seen = new Set((live.edges ?? []).map((e) => `${e.from}|${e.to}|${e.viaAttr ?? ""}`));
158
+ live = {
159
+ ...live,
160
+ edges: [
161
+ ...(live.edges ?? []),
162
+ ...reconstructed.filter((e) => !seen.has(`${e.from}|${e.to}|${e.viaAttr ?? ""}`)),
163
+ ],
164
+ };
165
+ }
86
166
  // Overlay live identity onto the SOURCE graph (same as `graph --overlay`):
87
167
  // the declared graph is the canvas — its edges carry the topology so ->/<-
88
168
  // resolves, while the live side supplies physical ids.
@@ -114,16 +194,211 @@ export async function runSearch(ctx: CommandContext): Promise<number> {
114
194
  const matches = ir.nodes.filter((n) => terms.every((t) => matchTerm(n, t, ir, nodeById)));
115
195
  if (matches.length === 0) {
116
196
  console.log("(no matches)");
197
+ availableAttrs(terms, ir);
117
198
  if (args.explain) explain(terms, matches, ir, nodeById, query);
118
199
  return 0;
119
200
  }
120
201
  for (const n of matches) {
121
202
  console.log(formatRow(n, show));
122
203
  }
204
+ const backed = source.kind === "declared" || matches.some((n) => n.physicalId);
205
+ // Only worth asking when the live read came back empty — that is the one case
206
+ // where a recording changes what the caller should do next.
207
+ const recorded =
208
+ source.kind === "live" && !matches.some((n) => n.physicalId) && args.env
209
+ ? (await hasSnapshot(String(args.env))) ? "yes" : undefined
210
+ : undefined;
211
+ provenance(matches, source, recorded);
212
+ ambientHint(matches, ambientKinds, args.ambient === true, replayAmbient);
213
+ showMiss(matches, show);
214
+ regionSpread(terms, matches, show);
215
+ derivedSurface(terms, matches, ir, backed);
123
216
  if (args.explain) explain(terms, matches, ir, nodeById, query);
124
217
  return 0;
125
218
  }
126
219
 
220
+
221
+ /** Where an answer's facts came from, for the provenance line (#1266). */
222
+ type AnswerSource =
223
+ | { kind: "declared" }
224
+ | { kind: "live" }
225
+ | { kind: "snapshot"; commit: string; timestamp: string };
226
+
227
+
228
+
229
+
230
+ /**
231
+ * Name the `--show` columns nothing carries (#1279).
232
+ *
233
+ * A requested column that no matched resource has simply did not appear, so the
234
+ * result looked like a resource with no such value rather than a name that was
235
+ * never going to match. Combined with case sensitivity that made `--show
236
+ * Region` an invisible no-op on an estate where every resource carries
237
+ * `region`.
238
+ */
239
+ function showMiss(matches: IRNode[], show: string[]): void {
240
+ if (show.length === 0 || matches.length === 0) return;
241
+ const present = new Set(
242
+ matches.flatMap((n) => Object.keys((n.attrs ?? {}) as object).map((k) => k.toLowerCase())),
243
+ );
244
+ const missing = show.filter((k) => !present.has(k.toLowerCase()));
245
+ if (missing.length === 0) return;
246
+ console.log(`— no matched resource carries ${missing.join(", ")}`);
247
+ }
248
+
249
+ /**
250
+ * Say when the answer spans more than one region (#1279).
251
+ *
252
+ * A result is a list of resources with no shape to it, and region is the one
253
+ * dimension of this estate that is invisible in a row unless asked for. Asked
254
+ * to list instances "in all regions", an agent printed six correct ids with no
255
+ * region against any of them — a complete answer to a question about regions
256
+ * that never mentions one, and it was judged wrong.
257
+ *
258
+ * Stated only when the matched set actually spans several and the caller has
259
+ * not already asked: a fact about the result, in the same family as the
260
+ * provenance line. It names the regions and no resource, so it cannot stand in
261
+ * for the answer — it says the answer has a dimension, not what to say about it.
262
+ */
263
+ function regionSpread(terms: Term[], matches: IRNode[], show: string[]): void {
264
+ if (show.includes("region") || terms.some((t) => t.a === "region")) return;
265
+ const regions = [
266
+ ...new Set(
267
+ matches
268
+ .map((n) => (n.attrs as Record<string, unknown>)?.region)
269
+ .filter((r): r is string => typeof r === "string" && r.length > 0),
270
+ ),
271
+ ].sort();
272
+ if (regions.length < 2) return;
273
+ console.log(`— these span ${regions.length} regions: ${regions.join(", ")} · add --show region to see which`);
274
+ }
275
+
276
+ /**
277
+ * Point out that `--ambient` is relevant to the kind just queried (#1278).
278
+ *
279
+ * A resource nothing declares and nothing references is invisible to every
280
+ * other observation path, so an answer about "my security groups" can be
281
+ * complete for the declared estate and still not be the answer the question
282
+ * wanted. The caller cannot know that from the result — it looks like the whole
283
+ * set. An agent asked which groups were unused queried the three declared ones,
284
+ * never learned three more existed, and spent twenty-five turns trying to
285
+ * reconcile the shortfall from the graph.
286
+ *
287
+ * Says only that the flag applies to this kind, which is knowable without a
288
+ * scan. It reports no count and names no resource, so it cannot stand in for
289
+ * the answer.
290
+ */
291
+ function ambientHint(
292
+ matches: IRNode[],
293
+ ambientKinds: string[],
294
+ asked: boolean,
295
+ replay?: { recordedAmbient: boolean },
296
+ ): void {
297
+ if (asked || ambientKinds.length === 0 || matches.length === 0) return;
298
+ // On a replay the flag cannot change the answer: what is ambient in a
299
+ // recording was fixed when it was recorded. Telling a caller to add
300
+ // `--ambient` to `--at` is advice that does nothing — and when the snapshot
301
+ // already holds ambient resources it is worse than nothing, because the
302
+ // answer is complete and the hint says it is not. An agent read "6 of 6
303
+ // matched" alongside it, went looking for a seventh, and hand-built a wrong
304
+ // answer from the raw graph over twelve turns.
305
+ if (replay) {
306
+ if (!replay.recordedAmbient) {
307
+ console.log(
308
+ `— this snapshot recorded no ambient resources · re-record with \`chant lifecycle snapshot <env> --ambient\` to include them`,
309
+ );
310
+ }
311
+ return;
312
+ }
313
+ const relevant = [...new Set(ambientKinds.filter((k) => matches.some((n) => n.kind === k)))];
314
+ if (relevant.length === 0) return;
315
+ const label = relevant.map((k) => k.split("::").slice(-1)[0]).join(", ");
316
+ console.log(
317
+ `— ${label} can also exist in the account without being declared or referenced; --ambient includes those`,
318
+ );
319
+ }
320
+
321
+ /**
322
+ * Say what backed this answer (#1266).
323
+ *
324
+ * Two things went wrong without it. A `--live` read that failed entirely
325
+ * returned the declared graph, exit 0, with no physical ids and nothing to say
326
+ * so — indistinguishable from a working live answer (#1263). And the derived
327
+ * surface below named folds like `internetFacing` whether or not the
328
+ * observation could support them, which is worse than saying nothing.
329
+ *
330
+ * It is also the most direct thing the tool can say to a caller deciding
331
+ * whether to re-check with a raw provider sweep: the API has already been read,
332
+ * and this many resources were bound to what it returned. A sweep repeats work
333
+ * already done. That is a fact about the query, printed for every query, and it
334
+ * encodes no expected answer.
335
+ */
336
+ function provenance(matches: IRNode[], source: AnswerSource, recorded?: string): void {
337
+ if (source.kind === "declared") {
338
+ console.log("— declared only · no observation · physical ids unavailable");
339
+ return;
340
+ }
341
+ const bound = matches.filter((n) => n.physicalId).length;
342
+ const what = source.kind === "live" ? "live read" : "snapshot";
343
+ if (bound === 0) {
344
+ // The estate was asked for and nothing came back bound. Naming it is the
345
+ // difference between "these do not exist" and "nobody could see them".
346
+ // A snapshot sitting unused is the actionable half of this. Denied network,
347
+ // agents read six declared rows as a live answer and spent their turns
348
+ // retrying `--live` — the tool knew the estate was unreachable AND that a
349
+ // recording of it was on disk, and said only the first half.
350
+ if (recorded) {
351
+ console.log(
352
+ `— ${what} returned no bound resources · a snapshot of this environment is recorded — answer from it with --at latest`,
353
+ );
354
+ return;
355
+ }
356
+ console.log(
357
+ `— ${what} returned no bound resources · answered from the declared graph · physical ids unavailable`,
358
+ );
359
+ return;
360
+ }
361
+ if (source.kind === "live") {
362
+ console.log(`— observed live · bound ${bound}/${matches.length}`);
363
+ return;
364
+ }
365
+ // Time is the whole risk of a recorded answer, so it leads. A caller can see
366
+ // how old this is and decide, rather than discovering staleness later.
367
+ const taken = source.timestamp ? ` taken ${source.timestamp}` : "";
368
+ const at = source.commit ? ` ${source.commit.slice(0, 7)}` : "";
369
+ console.log(`— observed from snapshot${at}${taken} · bound ${bound}/${matches.length}`);
370
+ }
371
+
372
+ /**
373
+ * Name the facts chant computed for the kinds in this result that the query did not use.
374
+ *
375
+ * A provider API can only return what it stores; chant additionally folds multi-hop topology
376
+ * onto a node, and a caller has no way to know that surface exists. Reporting it turns a
377
+ * one-shot query into a conversation with the graph — ask something, learn what else is
378
+ * knowable about the same resources, refine.
379
+ *
380
+ * The names come from {@link GraphIR.derivedAttrs}, recorded by whichever enrichment pass
381
+ * produced them. Nothing here knows what any attribute means or which question it answers;
382
+ * add a pass and its facts appear, remove one and they stop.
383
+ */
384
+ function derivedSurface(terms: Term[], matches: IRNode[], ir: GraphIR, backed = true): void {
385
+ const derived = ir.derivedAttrs;
386
+ // A fold over live topology has nothing to report when the observation came
387
+ // back empty (#1263). Naming the surface anyway advertises facts this answer
388
+ // could not have computed, which is worse than saying nothing at all.
389
+ if (!derived || matches.length === 0 || !backed) return;
390
+ const used = new Set(terms.filter((t) => t.kind === "attr").map((t) => t.a));
391
+ const unused = new Set<string>();
392
+ for (const n of matches) {
393
+ for (const [kind, names] of Object.entries(derived)) {
394
+ if (!n.kind?.includes(kind)) continue;
395
+ for (const name of names) if (!used.has(name)) unused.add(name);
396
+ }
397
+ }
398
+ if (unused.size === 0) return;
399
+ console.log(`— also derived for these resources: ${[...unused].sort().join(", ")}`);
400
+ }
401
+
127
402
  /**
128
403
  * `--explain` footer (#1139): a compact, model-DERIVED summary that gives a
129
404
  * small model a reason to trust the result instead of re-deriving it with a
@@ -143,11 +418,12 @@ function explain(terms: Term[], matches: IRNode[], ir: GraphIR, byId: Map<string
143
418
  // Inclusion evidence: for a derived fact a CLI can't easily re-verify
144
419
  // (internetFacing, resolved across the default VPC's routing), name WHY each
145
420
  // match qualifies, so the agent trusts the result instead of dropping it.
146
- if (terms.some((t) => t.kind === "attr" && t.a === "internetFacing")) {
421
+ for (const t of terms) {
422
+ if (t.kind !== "attr") continue;
147
423
  for (const n of matches) {
148
- const via = (n.attrs as Record<string, unknown> | undefined)?.["internetFacingVia"];
424
+ const via = (n.attrs as Record<string, unknown> | undefined)?.[`${t.a}Via`];
149
425
  const id = n.id.includes("::") ? n.id.slice(n.id.lastIndexOf("::") + 2) : n.id;
150
- if (typeof via === "string") console.log(` ✓ ${id} internet-facing via ${via}`);
426
+ if (typeof via === "string") console.log(` ✓ ${id} ${t.a} via ${via}`);
151
427
  }
152
428
  }
153
429
  const shown = excluded.slice(0, 8);
@@ -159,7 +435,53 @@ function explain(terms: Term[], matches: IRNode[], ir: GraphIR, byId: Map<string
159
435
  if (excluded.length > shown.length) console.log(` · …and ${excluded.length - shown.length} more excluded`);
160
436
  }
161
437
 
438
+ /**
439
+ * On a miss, name the attributes the queried kind actually carries. A graph knows
440
+ * its own schema, so a caller who guessed an attribute name — or did not know a
441
+ * derived one existed — can see what is queryable instead of falling back to a
442
+ * lossy CLI sweep. Read off the nodes present, so it stays a property of the
443
+ * graph rather than of any expected answer: whatever the estate holds is what
444
+ * this lists, and it says nothing about which attribute answers a question.
445
+ */
446
+ function availableAttrs(terms: Term[], ir: GraphIR): void {
447
+ const kindTerm = terms.find((t) => t.kind === "kind");
448
+ if (!kindTerm) return;
449
+ const of = ir.nodes.filter((n) => n.kind?.includes(kindTerm.a));
450
+ if (of.length === 0) return;
451
+ const names = new Set<string>();
452
+ for (const n of of) for (const k of Object.keys((n.attrs as Record<string, unknown>) ?? {})) names.add(k);
453
+ const queried = new Set(terms.filter((t) => t.kind === "attr").map((t) => t.a));
454
+ const unused = [...names].filter((k) => !queried.has(k)).sort();
455
+ if (unused.length > 0) {
456
+ console.log(` · ${of.length} ${kindTerm.a} node(s) carry: ${unused.join(", ")}`);
457
+ }
458
+
459
+ // A queried attribute that EXISTS but matched nothing is the more useful
460
+ // miss to explain, and it was the one left silent: the list above omits
461
+ // anything the caller asked about, so querying a real attribute with an
462
+ // unmatchable value taught nothing at all. A caller reaching for a wildcard —
463
+ // `attr:effectiveIngress=*tcp:22:0.0.0.0/0`, which this grammar has no
464
+ // operator for — got "(no matches)" and concluded the tool had nothing.
465
+ for (const term of terms) {
466
+ if (term.kind !== "attr" || term.b === undefined || !names.has(term.a)) continue;
467
+ const values = new Set<string>();
468
+ for (const n of of) {
469
+ const v = (n.attrs as Record<string, unknown> | undefined)?.[term.a];
470
+ for (const one of Array.isArray(v) ? v : [v]) {
471
+ if (one !== undefined && one !== null) values.add(String(one));
472
+ }
473
+ }
474
+ if (values.size === 0) continue;
475
+ const sample = [...values].sort().slice(0, 8);
476
+ const more = values.size > sample.length ? `, … ${values.size - sample.length} more` : "";
477
+ console.log(` · ${term.a} is present but no value matched "${term.b}" — values seen: ${sample.join(", ")}${more}`);
478
+ }
479
+ }
480
+
162
481
  function describeTerm(t: Term): string {
482
+ // `--explain` has to say a negated term was negated, or an exclusion reads as
483
+ // the opposite of what it is.
484
+ if (t.negated) return `!${describeTerm({ ...t, negated: false })}`;
163
485
  const leaf = (x: Term): string =>
164
486
  x.kind === "kind" ? `kind:${x.a}` : x.kind === "attr" ? `attr:${x.a}${x.b !== undefined ? "=" + x.b : ""}`
165
487
  : x.kind === "tag" ? `tag:${x.a}${x.b !== undefined ? "=" + x.b : ""}` : `"${x.a}"`;
@@ -169,6 +491,8 @@ function describeTerm(t: Term): string {
169
491
 
170
492
  interface Term {
171
493
  kind: "word" | "kind" | "tag" | "attr" | "edge";
494
+ /** `!term` — the node must NOT satisfy this (#1280). */
495
+ negated?: boolean;
172
496
  a: string;
173
497
  b?: string;
174
498
  /** For edge terms: the direction and the sub-predicate matched at the far end. */
@@ -185,17 +509,84 @@ function parseLeaf(tok: string): Term {
185
509
  if (eq >= 0) return { kind: key, a: rest.slice(0, eq), b: rest.slice(eq + 1) };
186
510
  return { kind: key, a: rest };
187
511
  }
512
+ // `name:value` with a prefix the grammar does not have. This parsed as a
513
+ // free-text word and matched nothing, which is the worst available outcome:
514
+ // an agent looking for SSH reachability wrote
515
+ // `effectiveIngress:tcp:22:0.0.0.0/0` — the right idea, the right attribute,
516
+ // the wrong spelling — got a clean empty result, concluded chant did not hold
517
+ // the fact, and rebuilt the answer by hand from security-group rows. An empty
518
+ // result must never be the reply to a question the grammar could not read.
519
+ //
520
+ // `::` and `://` are excluded so a genuine word search for `AWS::EC2::Instance`
521
+ // or a URL still works — a real prefix is one colon, not two.
522
+ const bad = /^([A-Za-z][A-Za-z0-9_]*):(?![:/])/.exec(tok);
523
+ if (bad) {
524
+ const name = bad[1];
525
+ const value = tok.slice(name.length + 1);
526
+ throw new QueryError(
527
+ `"${tok}" is not a term — there is no "${name}:" prefix`,
528
+ `for an attribute, say attr:${name}=${value || "<value>"}; the prefixes are kind:, attr:, tag:, and ->/<- for edges`,
529
+ );
530
+ }
188
531
  return { kind: "word", a: tok };
189
532
  }
190
533
 
534
+ /** A query the grammar cannot accept, carrying the correction to print. */
535
+ class QueryError extends Error {
536
+ constructor(
537
+ message: string,
538
+ readonly hint: string,
539
+ ) {
540
+ super(message);
541
+ }
542
+ }
543
+
191
544
  function parseQuery(query: string): Term[] {
192
545
  // Split on whitespace but keep quoted phrases together.
193
546
  const tokens = query.match(/"[^"]*"|\S+/g) ?? [];
194
547
  return tokens.map((raw) => {
195
- const tok = raw.replace(/^"|"$/g, "");
196
- if (tok.startsWith("->")) return { kind: "edge", a: "", dir: "out", sub: parseLeaf(tok.slice(2)) };
197
- if (tok.startsWith("<-")) return { kind: "edge", a: "", dir: "in", sub: parseLeaf(tok.slice(2)) };
198
- return parseLeaf(tok);
548
+ let tok = raw.replace(/^"|"$/g, "");
549
+ // A leading `!` negates the term (#1280). Absence is a real estate
550
+ // question — "which security groups does nothing reference", "which
551
+ // subnets hold no instances" — and the grammar could only express
552
+ // presence, so the one question a graph is uniquely good at needed a
553
+ // provider sweep and a hand-built set difference.
554
+ const negated = tok.startsWith("!");
555
+ if (negated) tok = tok.slice(1);
556
+ // An edge term needs a target. A bare `<-` used to parse to an empty leaf
557
+ // and quietly match something arbitrary — an agent wrote
558
+ // `kind:EC2::SecurityGroup !<-` meaning "referenced by nothing" and got a
559
+ // silently wrong set. Refusing it is right beyond the parse bug too:
560
+ // "referenced by nothing at all" and "referenced by no Instance" are
561
+ // different questions, and on any estate whose declared graph carries
562
+ // references they give different answers.
563
+ // A bare edge term is refused, and the refusal names the correction.
564
+ //
565
+ // It first parsed to an empty leaf and matched arbitrarily. The fix was to
566
+ // refuse it; then, because agents kept writing `!<-` for "what is unused",
567
+ // it was made to mean "no edge in this direction" — which is a coherent
568
+ // query and still the wrong one to answer that question with. It counts
569
+ // every reference in the project, and a stack output that publishes a
570
+ // resource's id is one, so `kind:EC2::SecurityGroup !<-` omits precisely
571
+ // the unattached group the question was about.
572
+ //
573
+ // Measured both ways: refusing it, agents wrote `!<-kind:EC2::Instance` and
574
+ // got the right answer 3/3; accepting it, they wrote `!<-` and got a wrong
575
+ // one 2 runs out of 3. A query whose plain reading is reliably not what the
576
+ // caller means is worth refusing, and the correction below is what makes
577
+ // the refusal useful rather than merely strict.
578
+ if (/^(->|<-)\s*$/.test(tok)) {
579
+ throw new QueryError(
580
+ `"${negated ? "!" : ""}${tok}" needs a target`,
581
+ `say what the edge reaches: ${negated ? "!" : ""}${tok}kind:EC2::Instance, or ${negated ? "!" : ""}${tok}attr:Name=web`,
582
+ );
583
+ }
584
+ const term = tok.startsWith("->")
585
+ ? { kind: "edge" as const, a: "", dir: "out" as const, sub: parseLeaf(tok.slice(2)) }
586
+ : tok.startsWith("<-")
587
+ ? { kind: "edge" as const, a: "", dir: "in" as const, sub: parseLeaf(tok.slice(2)) }
588
+ : parseLeaf(tok);
589
+ return negated ? { ...term, negated: true } : term;
199
590
  });
200
591
  }
201
592
 
@@ -213,6 +604,7 @@ function attrString(v: unknown): string {
213
604
  }
214
605
 
215
606
  function matchTerm(n: IRNode, t: Term, ir?: GraphIR, byId?: Map<string, IRNode>): boolean {
607
+ if (t.negated) return !matchTerm(n, { ...t, negated: false }, ir, byId);
216
608
  const attrs = n.attrs ?? {};
217
609
  if (t.kind === "edge") {
218
610
  if (!ir || !byId || !t.sub) return false;
@@ -253,11 +645,24 @@ function formatRow(n: IRNode, show: string[]): string {
253
645
  const physical = (n as { physicalId?: unknown }).physicalId ?? attrs["physicalId"] ?? attrs["InstanceId"] ?? attrs["Id"];
254
646
  if (physical != null && typeof physical !== "object") parts.push(String(physical));
255
647
  for (const key of show) {
256
- const v = attrs[key];
257
- if (v != null && typeof v !== "object") parts.push(`${key}=${attrString(v)}`);
648
+ // Match the name case-insensitively, and report what was actually found.
649
+ // AWS attribute names are PascalCase and chant's derived ones are not, so a
650
+ // caller mixing them is normal: `--show region` and `--show Region` are the
651
+ // same request, and one of them silently printed nothing. Seven of the
652
+ // `--show` names in one benchmark run missed on case alone.
653
+ const actual = key in attrs ? key : Object.keys(attrs).find((k) => k.toLowerCase() === key.toLowerCase());
654
+ const v = actual == null ? undefined : attrs[actual];
655
+ if (v == null) continue;
656
+ // A column the caller explicitly asked for is shown whatever shape it is.
657
+ // Skipping non-scalars silently meant `--show effectiveIngress` — the
658
+ // derived reachability fact, and the reason to reach for chant at all —
659
+ // printed a blank column, because it is a list. The agent read that as
660
+ // "chant does not have this" and hand-rolled the answer from raw
661
+ // security-group rows, which is exactly the work the fold exists to avoid.
662
+ parts.push(`${actual}=${typeof v === "object" ? JSON.stringify(v) : attrString(v)}`);
258
663
  }
259
664
  return parts.filter(Boolean).join(" ");
260
665
  }
261
666
 
262
667
  /** Internals exposed for unit tests. */
263
- export const __searchInternals = { parseQuery, matchTerm, formatRow, explain, describeTerm };
668
+ export const __searchInternals = { parseQuery, matchTerm, formatRow, explain, describeTerm, derivedSurface, availableAttrs, ambientHint, regionSpread, showMiss };
package/src/cli/main.ts CHANGED
@@ -273,6 +273,12 @@ export function parseArgs(args: string[]): ParsedArgs {
273
273
  result.updateSnapshot = true;
274
274
  } else if (arg === "--update-baseline") {
275
275
  result.updateBaseline = true;
276
+ } else if (arg === "--deep") {
277
+ result.deep = true;
278
+ } else if (arg === "--at") {
279
+ result.at = args[++i];
280
+ } else if (arg === "--ambient") {
281
+ result.ambient = true;
276
282
  } else if (arg === "--run-examples") {
277
283
  result.runExamples = true;
278
284
  } else if (arg === "--pinned-digest") {
@@ -437,6 +443,9 @@ Ops:
437
443
 
438
444
  Lifecycle (alias: lc):
439
445
  lifecycle snapshot <env> Query API, save metadata to orphan branch
446
+ --deep: also record each resource's property tree,
447
+ not just its identity — what a fold over topology
448
+ needs (costs more provider calls; #1267)
440
449
  lifecycle show <env> Show latest lifecycle snapshot
441
450
  lifecycle diff <env> Compare current build against last snapshot
442
451
  --live: query cloud now and detect drift
@@ -130,6 +130,33 @@ export interface ParsedArgs {
130
130
  * orphan branch; never touches the cloud.
131
131
  */
132
132
  updateBaseline?: boolean;
133
+ /**
134
+ * `chant lifecycle snapshot <env> --deep` (#1267) — also record each
135
+ * resource's normalized property tree, not just its identity. What a fold
136
+ * over topology needs, and what a snapshot-backed query needs to answer a
137
+ * property question at all. Costs more provider calls and a larger record,
138
+ * so it is opt-in.
139
+ */
140
+ deep?: boolean;
141
+ /**
142
+ * `chant search "<q>" --at <ref> --env <name>` (#1266) — answer from a
143
+ * recorded observation instead of reading the estate now. `latest` uses the
144
+ * most recent snapshot. Mutually exclusive with `--live`: they are two
145
+ * different observations of the same estate, and there is no rule for which
146
+ * should win.
147
+ */
148
+ at?: string;
149
+ /**
150
+ * `chant search "<q>" --ambient --live --env <name>` (#1278) — also report
151
+ * resources of a kind this estate manages that exist in the account without
152
+ * being declared or referenced. What "which of my security groups are
153
+ * unused" is asking about, and unreachable from a state file.
154
+ *
155
+ * Opt-in: it asks the provider what exists rather than resolving out from
156
+ * what is declared, which is a broader read and a different claim.
157
+ */
158
+ ambient?: boolean;
159
+
133
160
  /** `chant dev surface-diff --run-examples` — also run the example build harness */
134
161
  runExamples?: boolean;
135
162
  /** `chant dev surface-diff --pinned-digest <file>` — path to SHA-256 digest file for supply-chain verification */