@intentius/chant 0.124.44 → 0.124.46
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/capabilities/aws.paths +148 -0
- package/dist/capabilities/base-mappings.json +122 -0
- package/dist/capabilities/catalog.d.ts +89 -0
- package/dist/capabilities/catalog.d.ts.map +1 -0
- package/dist/capabilities/catalog.js +204 -0
- package/dist/capabilities/language.d.ts +103 -0
- package/dist/capabilities/language.d.ts.map +1 -0
- package/dist/capabilities/language.js +335 -0
- package/dist/capabilities/matcher.d.ts +65 -0
- package/dist/capabilities/matcher.d.ts.map +1 -0
- package/dist/capabilities/matcher.js +299 -0
- package/dist/capabilities/view.d.ts +207 -0
- package/dist/capabilities/view.d.ts.map +1 -0
- package/dist/capabilities/view.js +484 -0
- package/dist/ci-identity.d.ts +183 -12
- package/dist/ci-identity.d.ts.map +1 -1
- package/dist/ci-identity.js +362 -53
- package/dist/cli/commands/lint.d.ts.map +1 -1
- package/dist/cli/commands/lint.js +1 -0
- package/dist/cli/handlers/build.d.ts.map +1 -1
- package/dist/cli/handlers/build.js +17 -1
- package/dist/cli/handlers/declared-graph.d.ts +44 -0
- package/dist/cli/handlers/declared-graph.d.ts.map +1 -0
- package/dist/cli/handlers/declared-graph.js +87 -0
- package/dist/cli/handlers/graph.d.ts +25 -0
- package/dist/cli/handlers/graph.d.ts.map +1 -1
- package/dist/cli/handlers/graph.js +93 -87
- package/dist/cli/handlers/pr-env.d.ts +8 -2
- package/dist/cli/handlers/pr-env.d.ts.map +1 -1
- package/dist/cli/handlers/pr-env.js +127 -225
- package/dist/cli/handlers/pr.d.ts +3 -0
- package/dist/cli/handlers/pr.d.ts.map +1 -1
- package/dist/cli/handlers/pr.js +1 -0
- package/dist/cli/handlers/search-count.d.ts +89 -0
- package/dist/cli/handlers/search-count.d.ts.map +1 -0
- package/dist/cli/handlers/search-count.js +132 -0
- package/dist/cli/handlers/search.d.ts +358 -6
- package/dist/cli/handlers/search.d.ts.map +1 -1
- package/dist/cli/handlers/search.js +1065 -155
- package/dist/cli/main.d.ts.map +1 -1
- package/dist/cli/main.js +23 -3
- package/dist/cli/mcp/server.d.ts.map +1 -1
- package/dist/cli/mcp/server.js +3 -0
- package/dist/cli/mcp/tools/capabilities.d.ts +7 -0
- package/dist/cli/mcp/tools/capabilities.d.ts.map +1 -0
- package/dist/cli/mcp/tools/capabilities.js +27 -0
- package/dist/cli/mcp/workspace-tools.d.ts +3 -1
- package/dist/cli/mcp/workspace-tools.d.ts.map +1 -1
- package/dist/cli/mcp/workspace-tools.js +47 -2
- package/dist/cli/parse-args.d.ts.map +1 -1
- package/dist/cli/parse-args.js +64 -17
- package/dist/cli/registry.d.ts +22 -0
- package/dist/cli/registry.d.ts.map +1 -1
- package/dist/components/cli-support.d.ts +2 -0
- package/dist/components/cli-support.d.ts.map +1 -1
- package/dist/components/cli-support.js +4 -2
- package/dist/components/fan-out-support.d.ts +3 -0
- package/dist/components/fan-out-support.d.ts.map +1 -1
- package/dist/components/fan-out-support.js +1 -1
- package/dist/components/pr-env-ports.d.ts +29 -0
- package/dist/components/pr-env-ports.d.ts.map +1 -0
- package/dist/components/pr-env-ports.js +77 -0
- package/dist/components/pr-env-run.d.ts +254 -0
- package/dist/components/pr-env-run.d.ts.map +1 -0
- package/dist/components/pr-env-run.js +376 -0
- package/dist/components/pr-environment.d.ts +91 -15
- package/dist/components/pr-environment.d.ts.map +1 -1
- package/dist/components/pr-environment.js +95 -9
- package/dist/components/pr-pipeline.d.ts +10 -1
- package/dist/components/pr-pipeline.d.ts.map +1 -1
- package/dist/config-environments.d.ts +92 -0
- package/dist/config-environments.d.ts.map +1 -0
- package/dist/config-environments.js +102 -0
- package/dist/config.d.ts +24 -84
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +9 -96
- package/dist/graph-detail.d.ts.map +1 -1
- package/dist/graph-detail.js +15 -3
- package/dist/graph-effective.d.ts +33 -2
- package/dist/graph-effective.d.ts.map +1 -1
- package/dist/graph-effective.js +215 -68
- package/dist/graph-ir.d.ts +23 -0
- package/dist/graph-ir.d.ts.map +1 -1
- package/dist/graph-ir.js +130 -0
- package/dist/graph-sub-template.d.ts +11 -0
- package/dist/graph-sub-template.d.ts.map +1 -0
- package/dist/graph-sub-template.js +19 -0
- package/dist/lexicon.d.ts +9 -0
- package/dist/lexicon.d.ts.map +1 -1
- package/dist/lifecycle/observe.d.ts +28 -0
- package/dist/lifecycle/observe.d.ts.map +1 -1
- package/dist/lifecycle/observe.js +23 -1
- package/dist/lint/rule.d.ts +6 -0
- package/dist/lint/rule.d.ts.map +1 -1
- package/dist/live-endpoint.d.ts +1 -1
- package/dist/live-endpoint.d.ts.map +1 -1
- package/dist/live-endpoint.js +1 -1
- package/dist/observation.d.ts +48 -1
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +21 -5
- package/dist/op/activities/activity-contracts.d.ts +4 -4
- package/dist/op/op-waves.d.ts +17 -0
- package/dist/op/op-waves.d.ts.map +1 -1
- package/dist/op/op-waves.js +23 -1
- package/dist/pr-forge.d.ts +15 -1
- package/dist/pr-forge.d.ts.map +1 -1
- package/dist/pr-forge.js +28 -16
- package/dist/pr-requires.d.ts +5 -1
- package/dist/pr-requires.d.ts.map +1 -1
- package/dist/wave-runtime.mjs +251 -250
- package/dist/workspace/capabilities-contract.d.ts +66 -0
- package/dist/workspace/capabilities-contract.d.ts.map +1 -0
- package/dist/workspace/capabilities-contract.js +107 -0
- package/dist/workspace/capabilities.schema.json +229 -0
- package/dist/workspace/ci-cloud-identity.d.ts +3 -1
- package/dist/workspace/ci-cloud-identity.d.ts.map +1 -1
- package/dist/workspace/ci-cloud-identity.js +16 -1
- package/dist/workspace/ci-run.d.ts +35 -8
- package/dist/workspace/ci-run.d.ts.map +1 -1
- package/dist/workspace/ci-run.js +95 -21
- package/dist/workspace/ci-secrets.d.ts.map +1 -1
- package/dist/workspace/ci-secrets.js +4 -3
- package/dist/workspace/compose-graph.d.ts +9 -0
- package/dist/workspace/compose-graph.d.ts.map +1 -1
- package/dist/workspace/compose-graph.js +3 -1
- package/dist/workspace/conformance/index.d.ts +1 -1
- package/dist/workspace/conformance/index.d.ts.map +1 -1
- package/dist/workspace/conformance/index.js +39 -2
- package/dist/workspace/declaration.d.ts +10 -0
- package/dist/workspace/declaration.d.ts.map +1 -1
- package/dist/workspace/declaration.js +17 -1
- package/dist/workspace/declaration.schema.json +36 -1
- package/dist/workspace/graph-cache.d.ts +8 -0
- package/dist/workspace/graph-cache.d.ts.map +1 -1
- package/dist/workspace/graph-cache.js +27 -3
- package/dist/workspace/graph-cli.d.ts +39 -2
- package/dist/workspace/graph-cli.d.ts.map +1 -1
- package/dist/workspace/graph-cli.js +66 -24
- package/dist/workspace/graph.schema.json +148 -0
- package/dist/workspace/kind-readers.d.ts +3 -0
- package/dist/workspace/kind-readers.d.ts.map +1 -1
- package/dist/workspace/kind-readers.js +7 -1
- package/dist/workspace/member-commands.d.ts +10 -1
- package/dist/workspace/member-commands.d.ts.map +1 -1
- package/dist/workspace/member-commands.js +17 -1
- package/dist/workspace/nested-graph.d.ts +2 -0
- package/dist/workspace/nested-graph.d.ts.map +1 -1
- package/dist/workspace/nested-graph.js +32 -1
- package/dist/workspace/reason-codes.d.ts +10 -0
- package/dist/workspace/reason-codes.d.ts.map +1 -1
- package/dist/workspace/reason-codes.js +12 -0
- package/dist/workspace/search-cli.d.ts +204 -0
- package/dist/workspace/search-cli.d.ts.map +1 -0
- package/dist/workspace/search-cli.js +466 -0
- package/dist/workspace/search-contract.d.ts +56 -0
- package/dist/workspace/search-contract.d.ts.map +1 -0
- package/dist/workspace/search-contract.js +70 -0
- package/dist/workspace/search.schema.json +822 -0
- package/package.json +1 -1
- package/src/__snapshots__/okf.test.ts.snap +10 -0
- package/src/capabilities/aws.paths +148 -0
- package/src/capabilities/base-mappings.json +122 -0
- package/src/capabilities/catalog.ts +230 -0
- package/src/capabilities/language.test.ts +191 -0
- package/src/capabilities/language.ts +373 -0
- package/src/capabilities/matcher.ts +315 -0
- package/src/capabilities/view-compose.test.ts +76 -0
- package/src/capabilities/view.ts +631 -0
- package/src/ci-identity-targets.test.ts +275 -0
- package/src/ci-identity.ts +394 -61
- package/src/cli/commands/lint.ts +1 -0
- package/src/cli/handlers/build.ts +15 -1
- package/src/cli/handlers/declared-graph.ts +99 -0
- package/src/cli/handlers/graph.test.ts +94 -3
- package/src/cli/handlers/graph.ts +123 -96
- package/src/cli/handlers/pr-env.ts +135 -227
- package/src/cli/handlers/pr.ts +4 -0
- package/src/cli/handlers/search-build-roots.test.ts +144 -0
- package/src/cli/handlers/search-count.test.ts +191 -0
- package/src/cli/handlers/search-count.ts +165 -0
- package/src/cli/handlers/search-json.test.ts +333 -0
- package/src/cli/handlers/search-live.test.ts +387 -0
- package/src/cli/handlers/search-paths.test.ts +235 -0
- package/src/cli/handlers/search.ts +1312 -166
- package/src/cli/main.test.ts +28 -3
- package/src/cli/main.ts +23 -3
- package/src/cli/mcp/server.test.ts +3 -3
- package/src/cli/mcp/server.ts +3 -0
- package/src/cli/mcp/tools/capabilities.ts +32 -0
- package/src/cli/mcp/workspace-tools.e2e.test.ts +21 -1
- package/src/cli/mcp/workspace-tools.ts +47 -2
- package/src/cli/parse-args.ts +52 -13
- package/src/cli/registry.ts +22 -0
- package/src/components/cli-support.ts +6 -1
- package/src/components/fan-out-support.ts +4 -1
- package/src/components/pr-env-ports.ts +89 -0
- package/src/components/pr-env-run-bundle.test.ts +54 -0
- package/src/components/pr-env-run.test.ts +290 -0
- package/src/components/pr-env-run.ts +563 -0
- package/src/components/pr-environment.ts +161 -13
- package/src/components/pr-pipeline.ts +10 -1
- package/src/config-environments.ts +133 -0
- package/src/config.ts +37 -124
- package/src/graph-detail.ts +15 -3
- package/src/graph-effective.ts +226 -75
- package/src/graph-ir.test.ts +163 -0
- package/src/graph-ir.ts +147 -0
- package/src/graph-sub-template.ts +22 -0
- package/src/lexicon.ts +10 -0
- package/src/lifecycle/observe.test.ts +27 -1
- package/src/lifecycle/observe.ts +43 -1
- package/src/lint/rule.ts +6 -0
- package/src/live-endpoint.ts +1 -1
- package/src/observation.test.ts +25 -0
- package/src/observation.ts +69 -5
- package/src/op/op-waves.ts +38 -1
- package/src/pr-forge-bundle.test.ts +1 -1
- package/src/pr-forge.ts +44 -17
- package/src/pr-requires.test.ts +43 -6
- package/src/pr-requires.ts +5 -1
- package/src/workspace/capabilities-contract.test.ts +88 -0
- package/src/workspace/capabilities-contract.ts +132 -0
- package/src/workspace/capabilities.schema.json +229 -0
- package/src/workspace/ci-cloud-identity.test.ts +20 -0
- package/src/workspace/ci-cloud-identity.ts +14 -1
- package/src/workspace/ci-run.test.ts +74 -1
- package/src/workspace/ci-run.ts +122 -29
- package/src/workspace/ci-secrets.test.ts +1 -1
- package/src/workspace/ci-secrets.ts +4 -3
- package/src/workspace/compose-graph.ts +10 -1
- package/src/workspace/conformance/conformance.e2e.test.ts +14 -1
- package/src/workspace/conformance/index.ts +31 -2
- package/src/workspace/declaration.schema.json +36 -1
- package/src/workspace/declaration.test.ts +7 -0
- package/src/workspace/declaration.ts +24 -2
- package/src/workspace/graph-cache.ts +27 -3
- package/src/workspace/graph-cli.ts +96 -24
- package/src/workspace/graph.schema.json +148 -0
- package/src/workspace/kind-readers.e2e.test.ts +28 -0
- package/src/workspace/kind-readers.test.ts +1 -1
- package/src/workspace/kind-readers.ts +7 -1
- package/src/workspace/member-commands.ts +22 -1
- package/src/workspace/nested-graph.ts +32 -1
- package/src/workspace/reason-codes.test.ts +7 -0
- package/src/workspace/reason-codes.ts +12 -0
- package/src/workspace/search-cli.e2e.test.ts +305 -0
- package/src/workspace/search-cli.ts +591 -0
- package/src/workspace/search-contract.ts +95 -0
- package/src/workspace/search.schema.json +822 -0
|
@@ -2,21 +2,28 @@ import { resolve } from "node:path";
|
|
|
2
2
|
import { build } from "../../build";
|
|
3
3
|
import { buildGraphIr, buildLiveGraphIr, collectUnobserved, sourceOverlayGraphs, type GraphIR, type IRNode, type IREdge } from "../../graph-ir";
|
|
4
4
|
import { buildDeclaredPerStack } from "../../graph-declared";
|
|
5
|
-
import { enrichEffectiveTopology } from "../../graph-effective";
|
|
5
|
+
import { enrichEffectiveTopology, terraformResourceType, terraformType } from "../../graph-effective";
|
|
6
|
+
import { attachCapabilities, attachView, capabilityEngine, parseCapabilityCall } from "../../capabilities/catalog";
|
|
7
|
+
import { CapabilityError, type CapabilityEngine, type Evidence } from "../../capabilities/matcher";
|
|
8
|
+
import type { CapabilityPack, View } from "../../capabilities/view";
|
|
6
9
|
import { reconstructEdges, mergeCatalogs, type ReferenceCatalog } from "../../graph-refs";
|
|
7
10
|
import { discover } from "../../discovery/index";
|
|
8
11
|
|
|
9
|
-
import { observeResources } from "../../lifecycle/observe";
|
|
12
|
+
import { carryAnnotations, observeResources } from "../../lifecycle/observe";
|
|
10
13
|
import { replaySnapshots, hasSnapshot } from "../../lifecycle/replay";
|
|
11
14
|
import { diffLive, type LiveDiffResult } from "../../lifecycle/live-diff";
|
|
12
15
|
import type { LiveObservation } from "../../graph-ir";
|
|
13
|
-
import type { ResourceMetadata } from "../../lexicon";
|
|
16
|
+
import type { LexiconPlugin, ResourceMetadata } from "../../lexicon";
|
|
14
17
|
import type { ObservationDepth } from "../../lifecycle/types";
|
|
15
|
-
import { formatUnobserved } from "../../observation";
|
|
18
|
+
import { formatUnobserved, type RootRead } from "../../observation";
|
|
19
|
+
import { readChantPackageVersion } from "../../package-version";
|
|
16
20
|
import { loadChantConfig, matchesDeclaredEnvironment } from "../../config";
|
|
17
|
-
import { loadPlugins, resolveProjectLexicons } from "../plugins";
|
|
21
|
+
import { collectBuildRootContributors, loadPlugins, resolveProjectLexicons } from "../plugins";
|
|
22
|
+
import { declaredBuildRootContributors, mergeDeclaredBuildRoots, withLexiconEdges } from "./declared-graph";
|
|
23
|
+
import { parseCountBy, countGroups, countsJson, formatCounts, groupMembers, hasManyTerraformRoots, type CountBy } from "./search-count";
|
|
18
24
|
import { formatError, formatWarning } from "../format";
|
|
19
25
|
import type { CommandContext } from "../registry";
|
|
26
|
+
import { SEARCH_CONTRACT_VERSION, SEARCH_OUTPUT_SCHEMA_ID, type SearchErrorCode, type SearchFailure, type SearchHead } from "../../workspace/search-contract";
|
|
20
27
|
|
|
21
28
|
/**
|
|
22
29
|
* `chant search <query> [--live --env <name>]` — answer an estate question with
|
|
@@ -26,11 +33,14 @@ import type { CommandContext } from "../registry";
|
|
|
26
33
|
*
|
|
27
34
|
* Query grammar (space-separated terms, all must match — AND):
|
|
28
35
|
* bare word case-insensitive substring over id, kind, and attrs
|
|
29
|
-
* kind:<substr> node kind contains <substr> (e.g. kind:EC2::Instance)
|
|
36
|
+
* kind:<substr> node kind contains <substr> (e.g. kind:EC2::Instance);
|
|
37
|
+
* on a terraform resource, its type (kind:aws_instance, kind:aws_iam_)
|
|
30
38
|
* tag:<key>=<val> a Tags entry with Key=key and Value containing val
|
|
31
39
|
* attr:<name>=<val> attribute <name> equals/contains <val>
|
|
32
40
|
* ->kind:X / ->attr:.. this node has an edge TO a node matching the right side
|
|
33
41
|
* <-kind:X / <-attr:.. this node has an edge FROM a node matching the right side
|
|
42
|
+
* ->*X, ->{n}X, ->{m,n}X the far end is any depth / n / m..n hops away (#4359)
|
|
43
|
+
* ->[via=a]X, ->[to=a]X only edges through consumer attr a / reading producer attr a
|
|
34
44
|
*
|
|
35
45
|
* The edge operators are the point of "edge-aware" search (#1139): a small
|
|
36
46
|
* model shouldn't hand-join instance→subnet→public across many results — one
|
|
@@ -44,47 +54,34 @@ import type { CommandContext } from "../registry";
|
|
|
44
54
|
export async function runSearch(ctx: CommandContext): Promise<number> {
|
|
45
55
|
const { args } = ctx;
|
|
46
56
|
const query = (args.path ?? "").trim();
|
|
47
|
-
|
|
48
|
-
|
|
57
|
+
// A refusal (#4367): the error on stderr as always, and under --json the
|
|
58
|
+
// same message and hint as the one document on stdout, so a program reading
|
|
59
|
+
// stdout sees why there are no rows. Exit 1 either way. The code is from the
|
|
60
|
+
// read contract's closed list (#4374).
|
|
61
|
+
const refuse = (error: { code: SearchErrorCode; message: string; hint?: string }): number => {
|
|
62
|
+
console.error(formatError(error));
|
|
63
|
+
if (args.json) console.log(JSON.stringify(searchErrorJson(query, error)));
|
|
49
64
|
return 1;
|
|
65
|
+
};
|
|
66
|
+
// The capability catalog (#4375): the `capabilities` document, not an answer.
|
|
67
|
+
if (args.capabilities) return printCapabilities(args, query, await resolveProjectLexicons(resolve(".")).catch(() => []));
|
|
68
|
+
// The snapshot to replay (#4374): `--snapshot <ref>`, or `--at <ref>`, which
|
|
69
|
+
// plain search keeps for compatibility. Everywhere else in the read contract
|
|
70
|
+
// `--at` is a git revision, so the workspace form takes only `--snapshot`.
|
|
71
|
+
if (args.snapshot !== undefined && args.at !== undefined && String(args.snapshot) !== String(args.at)) {
|
|
72
|
+
return refuse({
|
|
73
|
+
code: "usage-invalid",
|
|
74
|
+
message: `chant search was given --snapshot ${args.snapshot} and --at ${args.at}`,
|
|
75
|
+
hint: "they name the same thing, the recorded snapshot to answer from; give one",
|
|
76
|
+
});
|
|
50
77
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
console.error(formatError({ message: err.message, hint: err.hint }));
|
|
57
|
-
return 1;
|
|
58
|
-
}
|
|
59
|
-
throw err;
|
|
60
|
-
}
|
|
78
|
+
const snapshotRef: string | undefined = args.snapshot ?? (args.at !== undefined ? String(args.at) : undefined);
|
|
79
|
+
const snapshotFlag = args.snapshot !== undefined ? "--snapshot" : "--at";
|
|
80
|
+
const checked = checkSearchArgs(args, query, { ref: snapshotRef, flag: snapshotFlag });
|
|
81
|
+
if ("error" in checked) return refuse(checked.error);
|
|
82
|
+
const { terms, countBy } = checked;
|
|
61
83
|
const show = parseShow(args);
|
|
62
84
|
|
|
63
|
-
// Query-scoped drift (#1268): --check-live checks a snapshot answer against
|
|
64
|
-
// a live read, --check-snapshot checks a live answer against the recorded
|
|
65
|
-
// snapshot — the direction each needs is the source it does NOT already have.
|
|
66
|
-
if (args.checkLive && !args.at) {
|
|
67
|
-
console.error(formatError({
|
|
68
|
-
message: "chant search --check-live needs --at <ref>",
|
|
69
|
-
hint: "answer from a snapshot and check the matched rows against a live read: --at latest --check-live --env <name>",
|
|
70
|
-
}));
|
|
71
|
-
return 1;
|
|
72
|
-
}
|
|
73
|
-
if (args.checkSnapshot && !args.live) {
|
|
74
|
-
console.error(formatError({
|
|
75
|
-
message: "chant search --check-snapshot needs --live",
|
|
76
|
-
hint: "answer live and check the matched rows against the recorded snapshot: --live --check-snapshot --env <name>",
|
|
77
|
-
}));
|
|
78
|
-
return 1;
|
|
79
|
-
}
|
|
80
|
-
if (args.failOnDrift && !args.checkLive && !args.checkSnapshot) {
|
|
81
|
-
console.error(formatError({
|
|
82
|
-
message: "chant search --fail-on-drift needs --check-live or --check-snapshot",
|
|
83
|
-
hint: "it fails the check those flags run, so it is meaningless without one",
|
|
84
|
-
}));
|
|
85
|
-
return 1;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
85
|
const projectPath = resolve(".");
|
|
89
86
|
const { config } = await loadChantConfig(projectPath);
|
|
90
87
|
|
|
@@ -97,6 +94,9 @@ export async function runSearch(ctx: CommandContext): Promise<number> {
|
|
|
97
94
|
// on this path" — printed with the provenance footer, after the rows, so the
|
|
98
95
|
// answer is not preceded by what qualifies it.
|
|
99
96
|
let liveNotes: string[] = [];
|
|
97
|
+
// Which read answered each entity (#2267), from a live read.
|
|
98
|
+
let readSources: Record<string, string> = {};
|
|
99
|
+
let rootReads: Record<string, RootRead> = {};
|
|
100
100
|
// Kinds that can exist in the account without being declared (#1278). Known
|
|
101
101
|
// without a scan, so it costs nothing to mention.
|
|
102
102
|
let ambientKinds: string[] = [];
|
|
@@ -110,53 +110,46 @@ export async function runSearch(ctx: CommandContext): Promise<number> {
|
|
|
110
110
|
let declaredForDrift: GraphIR | undefined;
|
|
111
111
|
let snapshotDepth: ObservationDepth | undefined;
|
|
112
112
|
let driftFailure = false;
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
return 1;
|
|
119
|
-
}
|
|
120
|
-
if (args.live && args.at) {
|
|
121
|
-
// Two different observations of the same estate, and no rule for which
|
|
122
|
-
// wins. Comparing them is a real question (#1268) but it is not this one.
|
|
123
|
-
console.error(formatError({
|
|
124
|
-
message: "chant search takes --live or --at, not both",
|
|
125
|
-
hint: "--live reads the estate now; --at answers from a recorded snapshot",
|
|
126
|
-
}));
|
|
127
|
-
return 1;
|
|
128
|
-
}
|
|
113
|
+
// Why --check-snapshot had nothing to check against, for --json (#4367).
|
|
114
|
+
let driftUnavailable: string | undefined;
|
|
115
|
+
if (args.live || snapshotRef) {
|
|
116
|
+
// checkSearchArgs refused a live or snapshot read without one.
|
|
117
|
+
const environment = String(args.env);
|
|
129
118
|
// Membership via matchesDeclaredEnvironment (#1221): handles `{ name,
|
|
130
119
|
// endpoint }` entries (#1166) — a plain `.includes` never matched those —
|
|
131
120
|
// and glob-pattern entries like `"pr-*"`.
|
|
132
121
|
if (config.environments && config.environments.length > 0 && !matchesDeclaredEnvironment(config.environments, environment)) {
|
|
133
|
-
|
|
134
|
-
return 1;
|
|
122
|
+
return refuse({ code: "environment-unknown", message: `Unknown environment "${environment}"` });
|
|
135
123
|
}
|
|
136
124
|
const plugins = ctx.plugins.length > 0 ? ctx.plugins : await loadPlugins(await resolveProjectLexicons(projectPath));
|
|
125
|
+
// Build roots too (#4358), as `chant graph --live` does: a terraform
|
|
126
|
+
// root's blocks are entities only once its root is rendered, and an
|
|
127
|
+
// entity the build never produced is never observed.
|
|
128
|
+
const buildRoots = collectBuildRootContributors(plugins, config as unknown as Record<string, unknown>, projectPath);
|
|
137
129
|
const buildResult = await build(resolve(args.src ?? config.sourceDir ?? "."), plugins.map((p) => p.serializer), undefined, {
|
|
130
|
+
buildRoots,
|
|
138
131
|
config: config as unknown as Record<string, unknown>,
|
|
139
132
|
});
|
|
140
133
|
if (buildResult.errors.length > 0) {
|
|
141
|
-
|
|
142
|
-
return 1;
|
|
134
|
+
return refuse({ code: "build-failed", message: "Build failed — fix errors before searching live state" });
|
|
143
135
|
}
|
|
144
136
|
const observing = plugins.filter((p) => p.describeResources);
|
|
145
137
|
ambientKinds = observing.flatMap((p) => p.ambientKinds?.() ?? []);
|
|
146
138
|
const stacks = (config.stacks ?? []).map((s) => ({ name: s.name, region: s.region, src: s.src }));
|
|
147
139
|
|
|
148
140
|
let observations: LiveObservation[];
|
|
141
|
+
// Facts about absent entities (#4372): a planned create's plan attributes.
|
|
142
|
+
let annotations: Record<string, Record<string, unknown>> | undefined;
|
|
149
143
|
const liveAttrs: Record<string, Record<string, unknown>> = {};
|
|
150
|
-
if (
|
|
144
|
+
if (snapshotRef) {
|
|
151
145
|
// Answer from a recorded observation (#1266). Everything downstream is
|
|
152
146
|
// the live path's — the point is that a snapshot replays into the same
|
|
153
147
|
// shape a live read produces, so one pipeline serves both and a fold
|
|
154
148
|
// improvement reaches an old snapshot for free.
|
|
155
149
|
const scoped = new Set(stacks.filter((st) => st.src).map((st) => st.name));
|
|
156
|
-
const replay = await replaySnapshots(environment,
|
|
150
|
+
const replay = await replaySnapshots(environment, snapshotRef, scoped);
|
|
157
151
|
if ("error" in replay) {
|
|
158
|
-
|
|
159
|
-
return 1;
|
|
152
|
+
return refuse({ code: "snapshot-unavailable", message: replay.error, ...(replay.hint ? { hint: replay.hint } : {}) });
|
|
160
153
|
}
|
|
161
154
|
observations = replay.observations;
|
|
162
155
|
replayAmbient = {
|
|
@@ -164,7 +157,7 @@ export async function runSearch(ctx: CommandContext): Promise<number> {
|
|
|
164
157
|
Object.values(o.resources).some((m) => m.ambient === true),
|
|
165
158
|
),
|
|
166
159
|
};
|
|
167
|
-
source = { kind: "snapshot", commit: replay.commit, timestamp: replay.timestamp };
|
|
160
|
+
source = { kind: "snapshot", snapshot: snapshotRef, commit: replay.commit, timestamp: replay.timestamp };
|
|
168
161
|
snapshotDepth = replay.depth;
|
|
169
162
|
if (args.checkLive) {
|
|
170
163
|
// A fresh live read, additional to the snapshot the answer itself came
|
|
@@ -194,6 +187,9 @@ export async function runSearch(ctx: CommandContext): Promise<number> {
|
|
|
194
187
|
console.error(formatError({ message: `live read failed — ${e}` }));
|
|
195
188
|
}
|
|
196
189
|
observations = observed.observations;
|
|
190
|
+
annotations = observed.annotations;
|
|
191
|
+
readSources = observed.sources ?? {};
|
|
192
|
+
rootReads = observed.rootReads ?? {};
|
|
197
193
|
liveNotes = observed.notes ?? [];
|
|
198
194
|
source = { kind: "live" };
|
|
199
195
|
if (args.checkSnapshot) {
|
|
@@ -205,6 +201,7 @@ export async function runSearch(ctx: CommandContext): Promise<number> {
|
|
|
205
201
|
const checkReplay = await replaySnapshots(environment, "latest", scoped);
|
|
206
202
|
if ("error" in checkReplay) {
|
|
207
203
|
console.error(formatWarning({ message: `--check-snapshot: ${checkReplay.error}` }));
|
|
204
|
+
driftUnavailable = checkReplay.error;
|
|
208
205
|
} else {
|
|
209
206
|
checkObservations = checkReplay.observations;
|
|
210
207
|
snapshotDepth = checkReplay.depth;
|
|
@@ -217,7 +214,7 @@ export async function runSearch(ctx: CommandContext): Promise<number> {
|
|
|
217
214
|
let containmentEdges: IREdge[] = [];
|
|
218
215
|
// Live-only: enrichment is a provider call, so it has no place in a replay.
|
|
219
216
|
// A recorded answer that quietly reached for the API would stop being one.
|
|
220
|
-
if (!
|
|
217
|
+
if (!snapshotRef) {
|
|
221
218
|
for (const p of observing) {
|
|
222
219
|
if (!p.enrichLiveAttrs) continue;
|
|
223
220
|
try {
|
|
@@ -271,10 +268,16 @@ export async function runSearch(ctx: CommandContext): Promise<number> {
|
|
|
271
268
|
// `${stack}::${id}` — matching how observation qualifies. A flat whole-
|
|
272
269
|
// project discovery would disambiguate colliding names by module path
|
|
273
270
|
// (UsEast1Src…), which never matches the observed bare LogicalResourceIds.
|
|
271
|
+
//
|
|
272
|
+
// The flat canvas is the declared path's graph (#4365, #4358): discovery
|
|
273
|
+
// plus the build-root contributions (a terraform root's blocks, a rendered
|
|
274
|
+
// kustomize root) plus the lexicon edges, so a live answer over a Terraform
|
|
275
|
+
// root overlays onto a canvas that has the root's resources on it.
|
|
274
276
|
const declared =
|
|
275
277
|
stacks.length > 0
|
|
276
|
-
? await buildDeclaredPerStack(stacks, projectPath)
|
|
277
|
-
:
|
|
278
|
+
? await buildDeclaredPerStack(stacks, projectPath, (g, entities) => withLexiconEdges(g, plugins, entities))
|
|
279
|
+
: await declaredGraph(ctx, projectPath, resolve(args.src ?? config.sourceDir ?? "."), plugins);
|
|
280
|
+
if (!declared) return refuse(BUILD_ROOT_FAILED);
|
|
278
281
|
declaredForDrift = declared;
|
|
279
282
|
// Carry the NOT-OBSERVED half of the tri-state (#1089) onto the rows, so a
|
|
280
283
|
// declared entity nobody could read is painted `_unobserved` and a row can
|
|
@@ -315,28 +318,109 @@ export async function runSearch(ctx: CommandContext): Promise<number> {
|
|
|
315
318
|
// account's default VPC, whose route table chant does not model). The
|
|
316
319
|
// overlay copies physical identity but not attrs, so merge them here.
|
|
317
320
|
ir = { ...ir, nodes: ir.nodes.map((n) => (liveAttrs[n.id] ? { ...n, attrs: { ...n.attrs, ...liveAttrs[n.id] } } : n)) };
|
|
321
|
+
// And what the read learned about an entity it found absent (#4372), so
|
|
322
|
+
// `attr:planAction=create` matches a block the plan will create. The node
|
|
323
|
+
// stays pending: only its attributes change.
|
|
324
|
+
ir = { ...ir, nodes: carryAnnotations(ir.nodes, annotations) };
|
|
318
325
|
} else {
|
|
319
|
-
const
|
|
320
|
-
|
|
326
|
+
const declared = await declaredGraph(ctx, projectPath, resolve(args.src ?? config.sourceDir ?? "."));
|
|
327
|
+
if (!declared) return refuse(BUILD_ROOT_FAILED);
|
|
328
|
+
ir = declared;
|
|
321
329
|
}
|
|
322
330
|
|
|
323
331
|
// Fold derived reachability facts (effectiveIngress, internetFacing) onto
|
|
324
332
|
// instance nodes so multi-hop/launch-template joins are one node predicate (#1139).
|
|
325
333
|
ir = enrichEffectiveTopology(ir);
|
|
334
|
+
// Capability terms (#4375) read the graph through the lexicons' mappings.
|
|
335
|
+
if (terms.some(usesCapability)) {
|
|
336
|
+
const refused = await bindCapabilities(ctx, projectPath, ir, terms);
|
|
337
|
+
if (refused) return refuse({ code: "query-invalid", ...refused });
|
|
338
|
+
}
|
|
326
339
|
// Resolve `kind:` terms against what is actually in this graph, before any
|
|
327
340
|
// node is tested against them.
|
|
328
341
|
resolveKindTerms(terms, ir.nodes);
|
|
329
342
|
const nodeById = new Map(ir.nodes.map((n) => [n.id, n]));
|
|
330
343
|
const matches = ir.nodes.filter((n) => terms.every((t) => matchTerm(n, t, ir, nodeById)));
|
|
344
|
+
// Query-scoped drift (#1268): the matched rows, and only the matched rows,
|
|
345
|
+
// checked against the observation the primary answer did NOT use. Scoping
|
|
346
|
+
// both sides of diffLive() to the matched ids is what keeps a query-scoped-
|
|
347
|
+
// out resource from reading as `missing` or `unobserved` — it was never
|
|
348
|
+
// asked about, which is a third thing from both.
|
|
349
|
+
let drift: LiveDiffResult | undefined;
|
|
350
|
+
if (checkObservations && declaredForDrift && primaryObservations) {
|
|
351
|
+
const declaredIds = new Set(declaredForDrift.nodes.map((n) => n.id));
|
|
352
|
+
const matchedIds = new Set(matches.map((n) => n.id));
|
|
353
|
+
const nowObservations = args.checkLive ? checkObservations : primaryObservations;
|
|
354
|
+
const thenObservations = args.checkLive ? primaryObservations : checkObservations;
|
|
355
|
+
drift = diffLive({
|
|
356
|
+
declared: new Set([...matchedIds].filter((id) => declaredIds.has(id))),
|
|
357
|
+
observedNow: pickResources(mergeResources(nowObservations), matchedIds),
|
|
358
|
+
observedThen: pickResources(mergeResources(thenObservations), matchedIds),
|
|
359
|
+
unobserved: pickResources(collectUnobserved(nowObservations), matchedIds),
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
// Name the plan read when it answered (#4384).
|
|
363
|
+
if (source.kind === "live") source = withReadDepth(source, matches, readSources, rootReads);
|
|
364
|
+
const driftAgainst: "live" | "snapshot" = args.checkLive ? "live" : "snapshot";
|
|
365
|
+
// Machine-readable answer (#4359, #4367): the rows, their paths, and every
|
|
366
|
+
// footer the text prints as prose, as one document for a caller that draws
|
|
367
|
+
// it (behold#913). Same exit codes as the text path.
|
|
368
|
+
if (args.json) {
|
|
369
|
+
const recorded =
|
|
370
|
+
source.kind === "live" && !matches.some((n) => n.physicalId) && args.env
|
|
371
|
+
? (await hasSnapshot(String(args.env))) ? "yes" : undefined
|
|
372
|
+
: undefined;
|
|
373
|
+
const env = args.env && source.kind !== "declared" ? String(args.env) : undefined;
|
|
374
|
+
const provenanceText = provenanceLine(matches, source, recorded, liveFailures, [...derivedUsed(terms, matches, ir), ...capabilitiesUsed(terms)]).replace(/^— /, "");
|
|
375
|
+
const capNotes = capabilityNotes(terms, ir, source);
|
|
376
|
+
const plannedCreates = source.kind !== "declared" && liveFailures.length === 0 ? plannedCreateCount(matches) : 0;
|
|
377
|
+
const driftJson = drift
|
|
378
|
+
? searchDriftJson(drift, matches.length, driftAgainst, snapshotDepth)
|
|
379
|
+
: driftUnavailable !== undefined
|
|
380
|
+
? { against: driftAgainst, unavailable: driftUnavailable }
|
|
381
|
+
: undefined;
|
|
382
|
+
// With --count-by (#4360) the object carries the groups and the total in
|
|
383
|
+
// place of the matches, the same answer the grouped lines give, with the
|
|
384
|
+
// provenance line beside it (#4367).
|
|
385
|
+
const head = { ...searchHead(), ...(await workspaceIdentity(projectPath)) };
|
|
386
|
+
const payload = countBy
|
|
387
|
+
? {
|
|
388
|
+
...head,
|
|
389
|
+
...countsJson(query, env ? { ...source, env } : source, groupMembers(matches, countBy, hasManyTerraformRoots(ir)), countBy, liveFailures),
|
|
390
|
+
provenance: provenanceText,
|
|
391
|
+
...(plannedCreates > 0 ? { plannedCreates } : {}),
|
|
392
|
+
...(driftJson ? { drift: driftJson } : {}),
|
|
393
|
+
}
|
|
394
|
+
: {
|
|
395
|
+
...head,
|
|
396
|
+
...searchJson(query, source, matches, terms, ir, nodeById, liveFailures, {
|
|
397
|
+
...(env ? { env } : {}),
|
|
398
|
+
show,
|
|
399
|
+
provenance: provenanceText,
|
|
400
|
+
...(plannedCreates > 0 ? { plannedCreates } : {}),
|
|
401
|
+
...(capNotes.length > 0 ? { capabilities: capNotes } : {}),
|
|
402
|
+
...(args.explain ? { explain: explainData(terms, matches, ir, nodeById) } : {}),
|
|
403
|
+
...(driftJson ? { drift: driftJson } : {}),
|
|
404
|
+
}),
|
|
405
|
+
};
|
|
406
|
+
console.log(JSON.stringify(payload));
|
|
407
|
+
for (const n of liveNotes) console.error(formatWarning({ message: n }));
|
|
408
|
+
if (liveFailures.length > 0) return 1;
|
|
409
|
+
return args.failOnDrift && drift && driftCountOf(drift) > 0 ? 1 : 0;
|
|
410
|
+
}
|
|
331
411
|
if (matches.length === 0) {
|
|
332
|
-
console.log("(no matches)");
|
|
412
|
+
console.log(countBy ? "(no matches) · 0 total" : "(no matches)");
|
|
333
413
|
availableAttrs(terms, ir);
|
|
334
414
|
if (args.explain) explain(terms, matches, ir, nodeById, query);
|
|
335
415
|
for (const n of liveNotes) console.error(formatWarning({ message: n }));
|
|
336
416
|
return 0;
|
|
337
417
|
}
|
|
338
|
-
|
|
339
|
-
|
|
418
|
+
if (countBy) {
|
|
419
|
+
for (const line of formatCounts(countGroups(matches, countBy, hasManyTerraformRoots(ir)), countBy)) console.log(line);
|
|
420
|
+
} else {
|
|
421
|
+
for (const n of matches) {
|
|
422
|
+
console.log(formatRow(n, show));
|
|
423
|
+
}
|
|
340
424
|
}
|
|
341
425
|
const backed = source.kind === "declared" || matches.some((n) => n.physicalId);
|
|
342
426
|
// Only worth asking when the live read came back empty — that is the one case
|
|
@@ -345,36 +429,23 @@ export async function runSearch(ctx: CommandContext): Promise<number> {
|
|
|
345
429
|
source.kind === "live" && !matches.some((n) => n.physicalId) && args.env
|
|
346
430
|
? (await hasSnapshot(String(args.env))) ? "yes" : undefined
|
|
347
431
|
: undefined;
|
|
348
|
-
provenance(matches, source, recorded, liveFailures);
|
|
432
|
+
provenance(matches, source, recorded, liveFailures, [...derivedUsed(terms, matches, ir), ...capabilitiesUsed(terms)]);
|
|
433
|
+
for (const c of capabilityNotes(terms, ir, source)) {
|
|
434
|
+
if (c.notEvaluated.length > 0) console.log(`— ${c.name} does not evaluate: ${c.notEvaluated.join("; ")}`);
|
|
435
|
+
}
|
|
349
436
|
// Qualifies the provenance line, so it sits with it: one line per distinct
|
|
350
437
|
// note for the whole run, not one per stack, and after the rows (#1265).
|
|
351
438
|
for (const n of liveNotes) console.error(formatWarning({ message: n }));
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
// both sides of diffLive() to the matched ids is what keeps a query-scoped-
|
|
355
|
-
// out resource from reading as `missing` or `unobserved` — it was never
|
|
356
|
-
// asked about, which is a third thing from both.
|
|
357
|
-
if (checkObservations && declaredForDrift && primaryObservations) {
|
|
358
|
-
const declaredIds = new Set(declaredForDrift.nodes.map((n) => n.id));
|
|
359
|
-
const matchedIds = new Set(matches.map((n) => n.id));
|
|
360
|
-
const nowObservations = args.checkLive ? checkObservations : primaryObservations;
|
|
361
|
-
const thenObservations = args.checkLive ? primaryObservations : checkObservations;
|
|
362
|
-
const driftCount = renderDrift(
|
|
363
|
-
diffLive({
|
|
364
|
-
declared: new Set([...matchedIds].filter((id) => declaredIds.has(id))),
|
|
365
|
-
observedNow: pickResources(mergeResources(nowObservations), matchedIds),
|
|
366
|
-
observedThen: pickResources(mergeResources(thenObservations), matchedIds),
|
|
367
|
-
unobserved: pickResources(collectUnobserved(nowObservations), matchedIds),
|
|
368
|
-
}),
|
|
369
|
-
matches.length,
|
|
370
|
-
args.checkLive ? "live" : "snapshot",
|
|
371
|
-
snapshotDepth,
|
|
372
|
-
);
|
|
439
|
+
if (drift) {
|
|
440
|
+
const driftCount = renderDrift(drift, matches.length, driftAgainst, snapshotDepth);
|
|
373
441
|
if (args.failOnDrift && driftCount > 0) driftFailure = true;
|
|
374
442
|
}
|
|
375
443
|
ambientHint(matches, ambientKinds, args.ambient === true, replayAmbient);
|
|
376
|
-
|
|
377
|
-
|
|
444
|
+
// Both point at `--show`, which a grouped count does not print (#4360).
|
|
445
|
+
if (!countBy) {
|
|
446
|
+
showMiss(matches, show);
|
|
447
|
+
regionSpread(terms, matches, show);
|
|
448
|
+
}
|
|
378
449
|
derivedSurface(terms, matches, ir, backed);
|
|
379
450
|
if (args.explain) explain(terms, matches, ir, nodeById, query);
|
|
380
451
|
// The caller asked for a live answer and at least one lexicon could not be
|
|
@@ -395,11 +466,359 @@ export async function runSearch(ctx: CommandContext): Promise<number> {
|
|
|
395
466
|
}
|
|
396
467
|
|
|
397
468
|
|
|
469
|
+
/** A search's arguments refused before anything is read, or its parsed terms and grouping. */
|
|
470
|
+
export type SearchArgsCheck =
|
|
471
|
+
| { error: { code: SearchErrorCode; message: string; hint?: string } }
|
|
472
|
+
| { terms: Term[]; countBy?: CountBy };
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* Check a search's query and flags before anything is read (#4374): the
|
|
476
|
+
* grammar, `--count-by`, and the flags that need or exclude each other. The
|
|
477
|
+
* rules `chant search` and `chant workspace search` share, so the workspace
|
|
478
|
+
* form refuses once, with the same code and message, instead of once per
|
|
479
|
+
* member. `snapshot` is the snapshot asked for and the flag that asked:
|
|
480
|
+
* `--snapshot`, or plain search's `--at`.
|
|
481
|
+
*/
|
|
482
|
+
export function checkSearchArgs(
|
|
483
|
+
args: { countBy?: string; checkLive?: boolean; checkSnapshot?: boolean; failOnDrift?: boolean; live?: boolean; env?: string },
|
|
484
|
+
query: string,
|
|
485
|
+
snapshot: { ref?: string; flag: string },
|
|
486
|
+
): SearchArgsCheck {
|
|
487
|
+
const flag = snapshot.flag;
|
|
488
|
+
// `--count-by` groups the matched set (#4360). It takes any query, and no
|
|
489
|
+
// query at all counts the whole graph: "which kinds does this estate hold".
|
|
490
|
+
let countBy: CountBy | undefined;
|
|
491
|
+
if (args.countBy !== undefined) {
|
|
492
|
+
const parsed = parseCountBy(args.countBy);
|
|
493
|
+
if ("error" in parsed) return { error: { code: "query-invalid", message: parsed.error, hint: parsed.hint } };
|
|
494
|
+
countBy = parsed;
|
|
495
|
+
}
|
|
496
|
+
if (!query && !countBy) {
|
|
497
|
+
return { error: { code: "query-invalid", message: "chant search needs a query: chant search \"<terms>\" [--live --env <name>]" } };
|
|
498
|
+
}
|
|
499
|
+
let terms: Term[];
|
|
500
|
+
try {
|
|
501
|
+
terms = parseQuery(query);
|
|
502
|
+
} catch (err) {
|
|
503
|
+
if (err instanceof QueryError) return { error: { code: "query-invalid", message: err.message, hint: err.hint } };
|
|
504
|
+
throw err;
|
|
505
|
+
}
|
|
506
|
+
// Query-scoped drift (#1268): --check-live checks a snapshot answer against
|
|
507
|
+
// a live read, --check-snapshot checks a live answer against the recorded
|
|
508
|
+
// snapshot — the direction each needs is the source it does NOT already have.
|
|
509
|
+
if (args.checkLive && !snapshot.ref) {
|
|
510
|
+
return {
|
|
511
|
+
error: {
|
|
512
|
+
code: "usage-invalid",
|
|
513
|
+
message: `chant search --check-live needs ${flag} <ref>`,
|
|
514
|
+
hint: `answer from a snapshot and check the matched rows against a live read: ${flag} latest --check-live --env <name>`,
|
|
515
|
+
},
|
|
516
|
+
};
|
|
517
|
+
}
|
|
518
|
+
if (args.checkSnapshot && !args.live) {
|
|
519
|
+
return {
|
|
520
|
+
error: {
|
|
521
|
+
code: "usage-invalid",
|
|
522
|
+
message: "chant search --check-snapshot needs --live",
|
|
523
|
+
hint: "answer live and check the matched rows against the recorded snapshot: --live --check-snapshot --env <name>",
|
|
524
|
+
},
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
if (args.failOnDrift && !args.checkLive && !args.checkSnapshot) {
|
|
528
|
+
return {
|
|
529
|
+
error: {
|
|
530
|
+
code: "usage-invalid",
|
|
531
|
+
message: "chant search --fail-on-drift needs --check-live or --check-snapshot",
|
|
532
|
+
hint: "it fails the check those flags run, so it is meaningless without one",
|
|
533
|
+
},
|
|
534
|
+
};
|
|
535
|
+
}
|
|
536
|
+
if ((args.live || snapshot.ref) && !args.env) {
|
|
537
|
+
const needs = snapshot.ref ? flag : "--live";
|
|
538
|
+
return { error: { code: "usage-invalid", message: `chant search ${needs} needs an environment: ${needs} --env <name>` } };
|
|
539
|
+
}
|
|
540
|
+
if (args.live && snapshot.ref) {
|
|
541
|
+
// Two different observations of the same estate, and no rule for which
|
|
542
|
+
// wins. Comparing them is a real question (#1268) but it is not this one.
|
|
543
|
+
return {
|
|
544
|
+
error: {
|
|
545
|
+
code: "usage-invalid",
|
|
546
|
+
message: `chant search takes --live or ${flag}, not both`,
|
|
547
|
+
hint: `--live reads the estate now; ${flag} answers from a recorded snapshot`,
|
|
548
|
+
},
|
|
549
|
+
};
|
|
550
|
+
}
|
|
551
|
+
return { terms, ...(countBy ? { countBy } : {}) };
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
/** Why a search over a project whose build root failed refuses (#4365), as the refusal prints it. */
|
|
555
|
+
const BUILD_ROOT_FAILED = {
|
|
556
|
+
code: "build-failed",
|
|
557
|
+
message: "chant search: a build root failed to render, so the graph it would answer over is incomplete",
|
|
558
|
+
hint: "fix the error above; `chant graph . --format ir` reports the same one",
|
|
559
|
+
} as const;
|
|
560
|
+
|
|
561
|
+
/**
|
|
562
|
+
* The declared graph a search answers over (#4362, #4365, #4368): discovery,
|
|
563
|
+
* plus the build-root entities (a terraform root's blocks, a rendered
|
|
564
|
+
* kustomize root), plus the edges the lexicons' `graphEdges` hooks add. The
|
|
565
|
+
* same join and the same edges `chant graph --format ir` draws, through the
|
|
566
|
+
* same helpers (`./declared-graph.ts`), so a node's id here is its id there.
|
|
567
|
+
*
|
|
568
|
+
* Returns undefined, having printed the build root's own errors, when a build
|
|
569
|
+
* root failed to render or collided with a discovered name; the caller refuses
|
|
570
|
+
* with {@link BUILD_ROOT_FAILED}. A search over the rest would answer
|
|
571
|
+
* `(no matches)` for a root it never read, which is a wrong answer stated as
|
|
572
|
+
* a fact.
|
|
573
|
+
*/
|
|
574
|
+
async function declaredGraph(
|
|
575
|
+
ctx: CommandContext,
|
|
576
|
+
projectPath: string,
|
|
577
|
+
srcDir: string,
|
|
578
|
+
loaded?: readonly LexiconPlugin[],
|
|
579
|
+
): Promise<GraphIR | undefined> {
|
|
580
|
+
const discovered = await discover(srcDir);
|
|
581
|
+
if (!(await mergeDeclaredBuildRoots(discovered.entities, await declaredBuildRootContributors(ctx, projectPath)))) return undefined;
|
|
582
|
+
let plugins = loaded ?? ctx.plugins;
|
|
583
|
+
if (plugins.length === 0) {
|
|
584
|
+
try {
|
|
585
|
+
plugins = await loadPlugins(await resolveProjectLexicons(projectPath));
|
|
586
|
+
} catch {
|
|
587
|
+
plugins = [];
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
return withLexiconEdges(buildGraphIr(discovered.entities, projectPath), plugins, discovered.entities);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* Print the capability catalog (#4375): the `capabilities` document under
|
|
595
|
+
* `--json`, a line per capability otherwise. It takes no query and no other
|
|
596
|
+
* search flag, since it answers nothing about the estate. Shared by `chant
|
|
597
|
+
* search` and `chant workspace search`, which differ only in the lexicons
|
|
598
|
+
* they report on.
|
|
599
|
+
*/
|
|
600
|
+
export async function printCapabilities(
|
|
601
|
+
args: { json?: boolean; live?: boolean; env?: string; snapshot?: string; at?: string; countBy?: string; explain?: boolean; show?: string },
|
|
602
|
+
query: string,
|
|
603
|
+
lexicons: string[],
|
|
604
|
+
): Promise<number> {
|
|
605
|
+
const { capabilitiesDocument, capabilitiesFailure, formatCapabilities } = await import("../../workspace/capabilities-contract");
|
|
606
|
+
const extra = [
|
|
607
|
+
// `args.path` is "." when no query was given.
|
|
608
|
+
...(query && query !== "." ? [`the query "${query}"`] : []),
|
|
609
|
+
...(["live", "env", "snapshot", "at", "countBy", "explain", "show"] as const)
|
|
610
|
+
.filter((k) => args[k] !== undefined && args[k] !== false)
|
|
611
|
+
.map((k) => `--${k.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`)}`),
|
|
612
|
+
];
|
|
613
|
+
if (extra.length > 0) {
|
|
614
|
+
const error = {
|
|
615
|
+
code: "usage-invalid" as const,
|
|
616
|
+
message: `--capabilities prints the catalog, so it takes no query and no other search flag; it was given ${extra.join(", ")}`,
|
|
617
|
+
hint: "ask with a capability instead: chant search \"cap:internet-reachable(22)\"",
|
|
618
|
+
};
|
|
619
|
+
console.error(formatError(error));
|
|
620
|
+
if (args.json) console.log(JSON.stringify(await capabilitiesFailure(error)));
|
|
621
|
+
return 1;
|
|
622
|
+
}
|
|
623
|
+
const doc = await capabilitiesDocument(lexicons);
|
|
624
|
+
if (args.json) console.log(JSON.stringify(doc));
|
|
625
|
+
else for (const line of formatCapabilities(doc)) console.log(line);
|
|
626
|
+
return 0;
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
/** The capabilities a query names, in query order, for the provenance line (#4375). */
|
|
630
|
+
function capabilitiesUsed(terms: Term[]): string[] {
|
|
631
|
+
const out: string[] = [];
|
|
632
|
+
const walk = (t: Term): void => {
|
|
633
|
+
if (t.negated) return;
|
|
634
|
+
const call = t.kind === "cap" ? t.a : t.capability;
|
|
635
|
+
const name = call !== undefined ? parseCapabilityCall(call)?.name : undefined;
|
|
636
|
+
if (name && !out.includes(name)) out.push(name);
|
|
637
|
+
if (t.sub) walk(t.sub);
|
|
638
|
+
};
|
|
639
|
+
terms.forEach(walk);
|
|
640
|
+
return out;
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
/**
|
|
644
|
+
* What each capability the query used could not evaluate (#4375), with a
|
|
645
|
+
* count where this graph holds the thing left out: an answer computed from
|
|
646
|
+
* declared references says what it skipped, so a caller knows the rows are a
|
|
647
|
+
* floor, not the whole of it.
|
|
648
|
+
*/
|
|
649
|
+
function capabilityNotes(terms: Term[], ir: GraphIR, source: AnswerSource): { name: string; source: AnswerSource["kind"]; notEvaluated: string[] }[] {
|
|
650
|
+
const names = capabilitiesUsed(terms);
|
|
651
|
+
if (names.length === 0) return [];
|
|
652
|
+
const engine = capabilityEngine(ir);
|
|
653
|
+
const statements = [...engine.view.nodes.values()].filter((n) => n.label === "Statement" && n.attrs.effect === "Allow");
|
|
654
|
+
const counts: Record<string, number> = {
|
|
655
|
+
'Resource "*"': statements.filter((n) => n.attrs.anyResource).length,
|
|
656
|
+
"IAM Condition keys": statements.filter((n) => n.attrs.condition).length,
|
|
657
|
+
"resources the graph holds but can't resolve, such as an expression naming nothing declared": statements.filter((n) => n.attrs.unresolved).length,
|
|
658
|
+
};
|
|
659
|
+
return names.map((name) => ({
|
|
660
|
+
name,
|
|
661
|
+
source: source.kind,
|
|
662
|
+
notEvaluated: (engine.rule(name)?.meta["not-evaluated"] ?? "")
|
|
663
|
+
.split(/;|\n/)
|
|
664
|
+
.map((x) => x.trim())
|
|
665
|
+
.filter(Boolean)
|
|
666
|
+
.map((x) => (counts[x] ? `${x} (${counts[x]} statement${counts[x] === 1 ? "" : "s"} here)` : x)),
|
|
667
|
+
}));
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
/**
|
|
671
|
+
* Answer capability terms over a graph that is not this project's own (#4375):
|
|
672
|
+
* `chant workspace search` composes its members' graphs and views and asks
|
|
673
|
+
* here, so the rules run once over the whole of it. Given packs, the view is
|
|
674
|
+
* built here; given a view, it is the one the members' own chants mapped
|
|
675
|
+
* (#4378). Returns the rows
|
|
676
|
+
* as `--json` carries them, or the refusal for a capability the query names
|
|
677
|
+
* wrongly.
|
|
678
|
+
*/
|
|
679
|
+
export function answerOverGraph(
|
|
680
|
+
ir: GraphIR,
|
|
681
|
+
terms: Term[],
|
|
682
|
+
query: string,
|
|
683
|
+
packsOrView: CapabilityPack[] | View,
|
|
684
|
+
): { matches: SearchJson["matches"]; capabilities: NonNullable<SearchJson["capabilities"]> } | { error: { message: string; hint?: string } } {
|
|
685
|
+
// A workspace hands over the view its members' chants mapped (#4378).
|
|
686
|
+
const engine = Array.isArray(packsOrView) ? attachCapabilities(ir, packsOrView) : attachView(ir, packsOrView);
|
|
687
|
+
const refused = checkCapabilityTerms(engine, terms);
|
|
688
|
+
if (refused) return { error: refused };
|
|
689
|
+
resolveKindTerms(terms, ir.nodes);
|
|
690
|
+
const byId = new Map(ir.nodes.map((n) => [n.id, n]));
|
|
691
|
+
const matches = ir.nodes.filter((n) => terms.every((t) => matchTerm(n, t, ir, byId)));
|
|
692
|
+
const doc = searchJson(query, { kind: "declared" }, matches, terms, ir, byId);
|
|
693
|
+
return { matches: doc.matches, capabilities: capabilityNotes(terms, ir, { kind: "declared" }) };
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
/** Whether a term, or the leaf under it, names a capability (#4375). */
|
|
697
|
+
export function usesCapability(t: Term): boolean {
|
|
698
|
+
return t.kind === "cap" || t.capability !== undefined || (t.sub !== undefined && usesCapability(t.sub));
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
/**
|
|
702
|
+
* Bind the graph to its lexicons' capability packs and check every capability
|
|
703
|
+
* the query names (#4375): an unknown name, a set used as an edge or the
|
|
704
|
+
* wrong arguments is a refusal, never an empty answer.
|
|
705
|
+
*/
|
|
706
|
+
async function bindCapabilities(
|
|
707
|
+
ctx: CommandContext,
|
|
708
|
+
projectPath: string,
|
|
709
|
+
ir: GraphIR,
|
|
710
|
+
terms: Term[],
|
|
711
|
+
): Promise<{ message: string; hint?: string } | undefined> {
|
|
712
|
+
let plugins: readonly LexiconPlugin[] = ctx.plugins;
|
|
713
|
+
if (plugins.length === 0) {
|
|
714
|
+
try {
|
|
715
|
+
plugins = await loadPlugins(await resolveProjectLexicons(projectPath));
|
|
716
|
+
} catch {
|
|
717
|
+
plugins = [];
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
const engine = attachCapabilities(ir, plugins.map((p) => p.capabilityPack).filter((p): p is CapabilityPack => !!p));
|
|
721
|
+
return checkCapabilityTerms(engine, terms);
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
/** Refuse a capability the query names that doesn't exist, a set used as an edge, or the wrong arguments. */
|
|
725
|
+
function checkCapabilityTerms(engine: CapabilityEngine, terms: Term[]): { message: string; hint?: string } | undefined {
|
|
726
|
+
const known = [...new Set(engine.publicNames())].join(", ");
|
|
727
|
+
const check = (t: Term): { message: string; hint?: string } | undefined => {
|
|
728
|
+
if (t.sub) {
|
|
729
|
+
const inner = check(t.sub);
|
|
730
|
+
if (inner) return inner;
|
|
731
|
+
}
|
|
732
|
+
const call = t.kind === "cap" ? t.a : t.capability;
|
|
733
|
+
if (call === undefined) return undefined;
|
|
734
|
+
const parsed = parseCapabilityCall(call)!;
|
|
735
|
+
if (!engine.has(parsed.name)) {
|
|
736
|
+
return { message: `no capability "${parsed.name}"`, hint: `the capabilities are ${known}; chant search --capabilities describes each` };
|
|
737
|
+
}
|
|
738
|
+
const shape = engine.rule(parsed.name)!.vars.length === 1 ? "set" : "edge";
|
|
739
|
+
if (t.capability !== undefined && shape === "set") {
|
|
740
|
+
return { message: `"${parsed.name}" is a set, not an edge`, hint: `test it as a term: cap:${call}` };
|
|
741
|
+
}
|
|
742
|
+
try {
|
|
743
|
+
engine.bind(parsed.name, parsed.args);
|
|
744
|
+
} catch (err) {
|
|
745
|
+
if (err instanceof CapabilityError) return { message: `cap:${call}: ${err.message}`, hint: "chant search --capabilities lists each capability's parameters" };
|
|
746
|
+
throw err;
|
|
747
|
+
}
|
|
748
|
+
return undefined;
|
|
749
|
+
};
|
|
750
|
+
for (const t of terms) {
|
|
751
|
+
const r = check(t);
|
|
752
|
+
if (r) return r;
|
|
753
|
+
}
|
|
754
|
+
return undefined;
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
/**
|
|
758
|
+
* Which read answered a live search's matches, when the lexicon reports it
|
|
759
|
+
* (#2267) and at least one answer came from the plan (#4384). Absent for a
|
|
760
|
+
* plain live or state answer, so those documents are unchanged.
|
|
761
|
+
*/
|
|
762
|
+
export interface ReadDepth {
|
|
763
|
+
/** `plan` or `live+plan` when every read match came that way, else `mixed`. */
|
|
764
|
+
depth?: "plan" | "live+plan" | "mixed";
|
|
765
|
+
/** The distinct reads that answered the matches, sorted. */
|
|
766
|
+
reads?: string[];
|
|
767
|
+
/** The plan read was asked for and failed: the read that answered instead. */
|
|
768
|
+
planFellBackTo?: "state" | "live" | "declared";
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
/**
|
|
772
|
+
* The read a plan-depth root fell back to, from the lexicon's structured
|
|
773
|
+
* per-root report. Never from note text. "state" wins over "declared" when
|
|
774
|
+
* roots disagree, because some read still answered.
|
|
775
|
+
*/
|
|
776
|
+
function planFallback(rootReads: Record<string, RootRead>): "state" | "live" | "declared" | undefined {
|
|
777
|
+
const falls = Object.values(rootReads)
|
|
778
|
+
.filter((r) => (r.depth === "plan" || r.depth === "live+plan") && r.fellBackTo)
|
|
779
|
+
.map((r) => r.fellBackTo);
|
|
780
|
+
if (falls.includes("state")) return "state";
|
|
781
|
+
if (falls.includes("live")) return "live";
|
|
782
|
+
if (falls.includes("declared")) return "declared";
|
|
783
|
+
return undefined;
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
/**
|
|
787
|
+
* Add the read depth to a live source (#4384). Only bound matches count: a
|
|
788
|
+
* row nobody read has no read to name. Nothing is added when no match was
|
|
789
|
+
* answered by the plan and the plan did not fall back.
|
|
790
|
+
*/
|
|
791
|
+
function withReadDepth(
|
|
792
|
+
source: AnswerSource & { kind: "live" },
|
|
793
|
+
matches: IRNode[],
|
|
794
|
+
sources: Record<string, string>,
|
|
795
|
+
rootReads: Record<string, RootRead>,
|
|
796
|
+
): AnswerSource {
|
|
797
|
+
const reads = [...new Set(matches.filter((n) => n.physicalId).map((n) => sources[n.id]).filter((r): r is string => !!r))].sort();
|
|
798
|
+
const fellBack = planFallback(rootReads);
|
|
799
|
+
const planned = reads.filter((r) => r === "plan" || r === "live+plan");
|
|
800
|
+
if (planned.length === 0 && !fellBack) return source;
|
|
801
|
+
return {
|
|
802
|
+
...source,
|
|
803
|
+
...(planned.length > 0 ? { depth: reads.length === 1 ? (reads[0] as "plan" | "live+plan") : ("mixed" as const) } : {}),
|
|
804
|
+
...(reads.length > 0 ? { reads } : {}),
|
|
805
|
+
...(fellBack ? { planFellBackTo: fellBack } : {}),
|
|
806
|
+
};
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
const READ_PHRASE: Record<string, string> = {
|
|
810
|
+
plan: "at the plan depth (terraform plan -lock=false)",
|
|
811
|
+
"live+plan": "live and at the plan depth (live-plan -json, terraform plan -lock=false)",
|
|
812
|
+
state: "from state",
|
|
813
|
+
live: "live",
|
|
814
|
+
};
|
|
815
|
+
|
|
398
816
|
/** Where an answer's facts came from, for the provenance line (#1266). */
|
|
399
|
-
type AnswerSource =
|
|
817
|
+
export type AnswerSource =
|
|
400
818
|
| { kind: "declared" }
|
|
401
|
-
| { kind: "live" }
|
|
402
|
-
|
|
819
|
+
| ({ kind: "live" } & ReadDepth)
|
|
820
|
+
/** `snapshot` is the ref asked for (`latest`), `commit` the snapshot commit it named (#4374). */
|
|
821
|
+
| { kind: "snapshot"; snapshot?: string; commit: string; timestamp: string };
|
|
403
822
|
|
|
404
823
|
|
|
405
824
|
|
|
@@ -510,10 +929,38 @@ function ambientHint(
|
|
|
510
929
|
* already done. That is a fact about the query, printed for every query, and it
|
|
511
930
|
* encodes no expected answer.
|
|
512
931
|
*/
|
|
513
|
-
function
|
|
932
|
+
function provenanceLine(
|
|
933
|
+
matches: IRNode[],
|
|
934
|
+
source: AnswerSource,
|
|
935
|
+
recorded?: string,
|
|
936
|
+
liveFailures: string[] = [],
|
|
937
|
+
derived: string[] = [],
|
|
938
|
+
): string {
|
|
939
|
+
const line = provenanceBase(matches, source, recorded, liveFailures, derived);
|
|
940
|
+
// The plan read fell back (#4384): say to what, on whichever line this is.
|
|
941
|
+
return source.kind === "live" ? line + fellBackText(source) : line;
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
/** ` · the plan read fell back to state`, or nothing. */
|
|
945
|
+
function fellBackText(source: ReadDepth): string {
|
|
946
|
+
if (!source.planFellBackTo) return "";
|
|
947
|
+
// A live root has no state read to fall to: live-plan answered alone.
|
|
948
|
+
const to = source.planFellBackTo === "live" ? "live-plan alone" : source.planFellBackTo;
|
|
949
|
+
return ` · the plan read fell back to ${to}`;
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
function provenanceBase(
|
|
953
|
+
matches: IRNode[],
|
|
954
|
+
source: AnswerSource,
|
|
955
|
+
recorded?: string,
|
|
956
|
+
liveFailures: string[] = [],
|
|
957
|
+
derived: string[] = [],
|
|
958
|
+
): string {
|
|
514
959
|
if (source.kind === "declared") {
|
|
515
|
-
|
|
516
|
-
|
|
960
|
+
// A derived fact the query relied on (#4362) was folded from declared
|
|
961
|
+
// references alone, so it describes the source, not the account.
|
|
962
|
+
const from = derived.length > 0 ? ` · ${derived.join(", ")} derived from declared references` : "";
|
|
963
|
+
return `— declared only · no observation · physical ids unavailable${from}`;
|
|
517
964
|
}
|
|
518
965
|
const bound = matches.filter((n) => n.physicalId).length;
|
|
519
966
|
const what = source.kind === "live" ? "live read" : "snapshot";
|
|
@@ -525,10 +972,15 @@ function provenance(matches: IRNode[], source: AnswerSource, recorded?: string,
|
|
|
525
972
|
const rest = recorded
|
|
526
973
|
? "a snapshot of this environment is recorded — answer from it with --at latest"
|
|
527
974
|
: "answered from the declared graph · physical ids unavailable";
|
|
528
|
-
|
|
529
|
-
return;
|
|
975
|
+
return `— live read failed (${lexicons}) · ${unobserved}/${matches.length} rows unobserved · ${rest}`;
|
|
530
976
|
}
|
|
531
977
|
if (bound === 0) {
|
|
978
|
+
// Every match a planned create (#4372): absent on purpose, so "returned no
|
|
979
|
+
// bound resources" reads as a failed read when the read worked.
|
|
980
|
+
const planned = plannedCreateCount(matches);
|
|
981
|
+
if (planned > 0) {
|
|
982
|
+
return `— ${what}: ${planned} planned ${planned === 1 ? "create" : "creates"}, none bound yet · the plan has not been applied`;
|
|
983
|
+
}
|
|
532
984
|
// The estate was asked for and nothing came back bound. Naming it is the
|
|
533
985
|
// difference between "these do not exist" and "nobody could see them".
|
|
534
986
|
// A snapshot sitting unused is the actionable half of this. Denied network,
|
|
@@ -536,25 +988,50 @@ function provenance(matches: IRNode[], source: AnswerSource, recorded?: string,
|
|
|
536
988
|
// retrying `--live` — the tool knew the estate was unreachable AND that a
|
|
537
989
|
// recording of it was on disk, and said only the first half.
|
|
538
990
|
if (recorded) {
|
|
539
|
-
|
|
540
|
-
`— ${what} returned no bound resources · a snapshot of this environment is recorded — answer from it with --at latest
|
|
541
|
-
|
|
542
|
-
return;
|
|
991
|
+
return (
|
|
992
|
+
`— ${what} returned no bound resources · a snapshot of this environment is recorded — answer from it with --at latest`
|
|
993
|
+
);
|
|
543
994
|
}
|
|
544
|
-
|
|
545
|
-
`— ${what} returned no bound resources · answered from the declared graph · physical ids unavailable
|
|
995
|
+
return (
|
|
996
|
+
`— ${what} returned no bound resources · answered from the declared graph · physical ids unavailable`
|
|
546
997
|
);
|
|
547
|
-
return;
|
|
548
998
|
}
|
|
549
999
|
if (source.kind === "live") {
|
|
550
|
-
|
|
551
|
-
|
|
1000
|
+
// The plan answered some or all of it (#4384): say so, and name each read.
|
|
1001
|
+
if (source.depth && source.reads) {
|
|
1002
|
+
return `— observed ${source.reads.map((r) => READ_PHRASE[r] ?? r).join(" and ")} · bound ${bound}/${matches.length}`;
|
|
1003
|
+
}
|
|
1004
|
+
return `— observed live · bound ${bound}/${matches.length}`;
|
|
552
1005
|
}
|
|
553
1006
|
// Time is the whole risk of a recorded answer, so it leads. A caller can see
|
|
554
1007
|
// how old this is and decide, rather than discovering staleness later.
|
|
555
1008
|
const taken = source.timestamp ? ` taken ${source.timestamp}` : "";
|
|
556
1009
|
const at = source.commit ? ` ${source.commit.slice(0, 7)}` : "";
|
|
557
|
-
|
|
1010
|
+
return `— observed from snapshot${at}${taken} · bound ${bound}/${matches.length}`;
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
/**
|
|
1014
|
+
* How many matches there are when every one is a planned create (#4372): an
|
|
1015
|
+
* absent entity (no physical id) carrying `planAction=create`. Zero for no
|
|
1016
|
+
* matches or any other mix, so a real empty read keeps its wording.
|
|
1017
|
+
*/
|
|
1018
|
+
function plannedCreateCount(matches: IRNode[]): number {
|
|
1019
|
+
if (matches.length === 0) return 0;
|
|
1020
|
+
const all = matches.every(
|
|
1021
|
+
(n) => !n.physicalId && (n.attrs as Record<string, unknown> | undefined)?.planAction === "create",
|
|
1022
|
+
);
|
|
1023
|
+
return all ? matches.length : 0;
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
/** Print {@link provenanceLine}: the text footer. */
|
|
1027
|
+
function provenance(
|
|
1028
|
+
matches: IRNode[],
|
|
1029
|
+
source: AnswerSource,
|
|
1030
|
+
recorded?: string,
|
|
1031
|
+
liveFailures: string[] = [],
|
|
1032
|
+
derived: string[] = [],
|
|
1033
|
+
): void {
|
|
1034
|
+
console.log(provenanceLine(matches, source, recorded, liveFailures, derived));
|
|
558
1035
|
}
|
|
559
1036
|
|
|
560
1037
|
/** Union `LiveObservation.resources` across lexicons into one name-keyed map. */
|
|
@@ -598,7 +1075,7 @@ function renderDrift(
|
|
|
598
1075
|
for (const name of diff.newlyObserved) console.log(` · ${name} — newly observed since the recorded snapshot`);
|
|
599
1076
|
for (const r of diff.runtimeChildren) console.log(` · ${r.name} (${r.type}) — runtime, owned by ${r.owner}`);
|
|
600
1077
|
for (const u of diff.unobserved) console.log(` ? ${formatUnobserved(u.name, u)}`);
|
|
601
|
-
const driftCount = diff
|
|
1078
|
+
const driftCount = driftCountOf(diff);
|
|
602
1079
|
const label = direction === "live" ? "a live read" : "the recorded snapshot";
|
|
603
1080
|
const depthNote = depth === "deep" ? " · snapshot recorded at deep depth, compared here at identity" : "";
|
|
604
1081
|
console.log(
|
|
@@ -607,6 +1084,59 @@ function renderDrift(
|
|
|
607
1084
|
return driftCount;
|
|
608
1085
|
}
|
|
609
1086
|
|
|
1087
|
+
/** What counts as drift: the set `lifecycle diff` totals (#1268). */
|
|
1088
|
+
function driftCountOf(diff: LiveDiffResult): number {
|
|
1089
|
+
return diff.missing.length + diff.orphan.length + diff.disappeared.length + diff.driftedSinceSnapshot.length;
|
|
1090
|
+
}
|
|
1091
|
+
|
|
1092
|
+
/** The query-scoped drift check as `--json` carries it (#4367). See {@link SearchJson}. */
|
|
1093
|
+
export interface SearchDriftJson {
|
|
1094
|
+
against: "live" | "snapshot";
|
|
1095
|
+
/** Matched rows checked. */
|
|
1096
|
+
checked?: number;
|
|
1097
|
+
/** Rows that drifted: missing + orphan + disappeared + drifted, as the text summary counts. */
|
|
1098
|
+
count?: number;
|
|
1099
|
+
missing?: string[];
|
|
1100
|
+
orphan?: string[];
|
|
1101
|
+
disappeared?: string[];
|
|
1102
|
+
drifted?: { id: string; path: string; old?: unknown; new?: unknown }[];
|
|
1103
|
+
newlyObserved?: string[];
|
|
1104
|
+
runtimeChildren?: { id: string; type: string; owner: string }[];
|
|
1105
|
+
unobserved?: { id: string; type?: string; reason: string; detail?: string }[];
|
|
1106
|
+
/** The snapshot was recorded deeper than the identity-level comparison made here. */
|
|
1107
|
+
depth?: ObservationDepth;
|
|
1108
|
+
/** --check-snapshot found no snapshot to check against: the reason, and no other field. */
|
|
1109
|
+
unavailable?: string;
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
function searchDriftJson(
|
|
1113
|
+
diff: LiveDiffResult,
|
|
1114
|
+
checked: number,
|
|
1115
|
+
against: "live" | "snapshot",
|
|
1116
|
+
depth?: ObservationDepth,
|
|
1117
|
+
): SearchDriftJson {
|
|
1118
|
+
return {
|
|
1119
|
+
against,
|
|
1120
|
+
checked,
|
|
1121
|
+
count: driftCountOf(diff),
|
|
1122
|
+
missing: diff.missing,
|
|
1123
|
+
orphan: diff.orphan,
|
|
1124
|
+
disappeared: diff.disappeared,
|
|
1125
|
+
drifted: diff.driftedSinceSnapshot.flatMap((d) =>
|
|
1126
|
+
d.changes.map((c) => ({ id: d.name, path: c.path, old: c.oldValue, new: c.newValue })),
|
|
1127
|
+
),
|
|
1128
|
+
newlyObserved: diff.newlyObserved,
|
|
1129
|
+
runtimeChildren: diff.runtimeChildren.map((r) => ({ id: r.name, type: r.type, owner: r.owner })),
|
|
1130
|
+
unobserved: diff.unobserved.map((u) => ({
|
|
1131
|
+
id: u.name,
|
|
1132
|
+
...(u.type ? { type: u.type } : {}),
|
|
1133
|
+
reason: u.reason,
|
|
1134
|
+
...(u.detail ? { detail: u.detail } : {}),
|
|
1135
|
+
})),
|
|
1136
|
+
...(depth ? { depth } : {}),
|
|
1137
|
+
};
|
|
1138
|
+
}
|
|
1139
|
+
|
|
610
1140
|
function formatValue(v: unknown): string {
|
|
611
1141
|
if (v === undefined) return "<unset>";
|
|
612
1142
|
if (typeof v === "string") return v.length > 60 ? v.slice(0, 57) + "..." : v;
|
|
@@ -634,16 +1164,27 @@ function derivedSurface(terms: Term[], matches: IRNode[], ir: GraphIR, backed =
|
|
|
634
1164
|
if (!derived || matches.length === 0 || !backed) return;
|
|
635
1165
|
const used = new Set(terms.filter((t) => t.kind === "attr").map((t) => t.a));
|
|
636
1166
|
const unused = new Set<string>();
|
|
637
|
-
for (const n of matches)
|
|
638
|
-
for (const [kind, names] of Object.entries(derived)) {
|
|
639
|
-
if (!n.kind?.includes(kind)) continue;
|
|
640
|
-
for (const name of names) if (!used.has(name)) unused.add(name);
|
|
641
|
-
}
|
|
642
|
-
}
|
|
1167
|
+
for (const n of matches) for (const name of derivedNames(n, ir)) if (!used.has(name)) unused.add(name);
|
|
643
1168
|
if (unused.size === 0) return;
|
|
644
1169
|
console.log(`— also derived for these resources: ${[...unused].sort().join(", ")}`);
|
|
645
1170
|
}
|
|
646
1171
|
|
|
1172
|
+
/** The derived attributes recorded for a node's kind (or its terraform resource type, #4362). */
|
|
1173
|
+
function derivedNames(n: IRNode, ir: GraphIR): string[] {
|
|
1174
|
+
const tfType = terraformResourceType(n);
|
|
1175
|
+
return Object.entries(ir.derivedAttrs ?? {})
|
|
1176
|
+
.filter(([kind]) => n.kind?.includes(kind) || kind === tfType)
|
|
1177
|
+
.flatMap(([, names]) => names);
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1180
|
+
/** The derived attributes the query's `attr:` terms relied on, for the provenance line. */
|
|
1181
|
+
function derivedUsed(terms: Term[], matches: IRNode[], ir: GraphIR): string[] {
|
|
1182
|
+
const queried = new Set(terms.filter((t) => t.kind === "attr" && !t.negated).map((t) => t.a));
|
|
1183
|
+
const out = new Set<string>();
|
|
1184
|
+
for (const n of matches) for (const name of derivedNames(n, ir)) if (queried.has(name)) out.add(name);
|
|
1185
|
+
return [...out].sort();
|
|
1186
|
+
}
|
|
1187
|
+
|
|
647
1188
|
/**
|
|
648
1189
|
* `--explain` footer (#1139): a compact, model-DERIVED summary that gives a
|
|
649
1190
|
* small model a reason to trust the result instead of re-deriving it with a
|
|
@@ -654,30 +1195,75 @@ function derivedSurface(terms: Term[], matches: IRNode[], ir: GraphIR, backed =
|
|
|
654
1195
|
* any expected answer, so it stays a fair, question-agnostic capability.
|
|
655
1196
|
*/
|
|
656
1197
|
function explain(terms: Term[], matches: IRNode[], ir: GraphIR, byId: Map<string, IRNode>, query: string): void {
|
|
657
|
-
const
|
|
658
|
-
const
|
|
659
|
-
|
|
660
|
-
const excluded = universe.filter((n) => !matched.has(n.id));
|
|
661
|
-
const kindLabel = kinds.size > 0 ? [...kinds].join("/") : "nodes";
|
|
662
|
-
console.log(`— ${matches.length} of ${universe.length} ${kindLabel} matched (query: ${query})`);
|
|
1198
|
+
const data = explainData(terms, matches, ir, byId);
|
|
1199
|
+
const kindLabel = data.universe.kinds.length > 0 ? data.universe.kinds.join("/") : "nodes";
|
|
1200
|
+
console.log(`— ${data.universe.matched} of ${data.universe.of} ${kindLabel} matched (query: ${query})`);
|
|
663
1201
|
// Inclusion evidence: for a derived fact a CLI can't easily re-verify
|
|
664
1202
|
// (internetFacing, resolved across the default VPC's routing), name WHY each
|
|
665
1203
|
// match qualifies, so the agent trusts the result instead of dropping it.
|
|
1204
|
+
for (const e of data.evidence) console.log(` ✓ ${bareId(e.id)} ${e.attr} via ${e.via}`);
|
|
1205
|
+
// The chain each match was reached by (#4359), so a caller can quote the
|
|
1206
|
+
// path instead of re-deriving it with one search per hop.
|
|
1207
|
+
for (const n of matches) {
|
|
1208
|
+
for (const p of matchPaths(n, terms, ir, byId)) {
|
|
1209
|
+
// A capability's path (#4375) turns as the rule does, so each hop says which way it went.
|
|
1210
|
+
if (p.term.kind === "cap" || p.term.capability !== undefined) {
|
|
1211
|
+
// A set a node is in by its own attributes (stores-data) has no path to print.
|
|
1212
|
+
if (p.hops.length === 0 && !p.because) continue;
|
|
1213
|
+
console.log(` path ${bareId(n.id)} ${termText({ ...p.term, negated: false })}: ${formatHops(p.hops, n.id) || "(the node itself)"}`);
|
|
1214
|
+
for (const b of p.because ?? []) {
|
|
1215
|
+
console.log(` ${b.capability} ${bareId(b.id)}: ${formatHops(b.hops, b.id) || "(an attribute of the node)"}`);
|
|
1216
|
+
}
|
|
1217
|
+
} else console.log(` path ${formatPath(p.hops, p.term.dir!)}`);
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
for (const m of data.nearMisses) {
|
|
1221
|
+
console.log(` · excluded ${bareId(m.id)} — fails ${m.term ? describeTerm(m.term) : "(query)"}`);
|
|
1222
|
+
}
|
|
1223
|
+
if (data.moreExcluded > 0) console.log(` · …and ${data.moreExcluded} more excluded`);
|
|
1224
|
+
}
|
|
1225
|
+
|
|
1226
|
+
/** How many near-misses `--explain` names before summarising the rest. */
|
|
1227
|
+
const NEAR_MISS_LIMIT = 8;
|
|
1228
|
+
|
|
1229
|
+
/**
|
|
1230
|
+
* The facts behind the `--explain` footer, shared by the text and `--json`
|
|
1231
|
+
* (#4367). The universe is every node of the matched kinds (every node when
|
|
1232
|
+
* nothing matched); a near-miss is a node of that universe the query
|
|
1233
|
+
* excluded, with the first term it fails.
|
|
1234
|
+
*/
|
|
1235
|
+
function explainData(
|
|
1236
|
+
terms: Term[],
|
|
1237
|
+
matches: IRNode[],
|
|
1238
|
+
ir: GraphIR,
|
|
1239
|
+
byId: Map<string, IRNode>,
|
|
1240
|
+
): {
|
|
1241
|
+
universe: { matched: number; of: number; kinds: string[] };
|
|
1242
|
+
evidence: { id: string; attr: string; via: string }[];
|
|
1243
|
+
nearMisses: { id: string; term?: Term }[];
|
|
1244
|
+
moreExcluded: number;
|
|
1245
|
+
} {
|
|
1246
|
+
// Counted against a terraform block's type, not `Terraform::Resource` (#4366):
|
|
1247
|
+
// "2 of 2 aws_instance", not "2 of 185 Terraform::Resource".
|
|
1248
|
+
const kinds = new Set(matches.map(displayKind).filter((k): k is string => !!k));
|
|
1249
|
+
const universe = kinds.size > 0 ? ir.nodes.filter((n) => kinds.has(displayKind(n) ?? "")) : ir.nodes;
|
|
1250
|
+
const matched = new Set(matches.map((n) => n.id));
|
|
1251
|
+
const excluded = universe.filter((n) => !matched.has(n.id));
|
|
1252
|
+
const evidence: { id: string; attr: string; via: string }[] = [];
|
|
666
1253
|
for (const t of terms) {
|
|
667
1254
|
if (t.kind !== "attr") continue;
|
|
668
1255
|
for (const n of matches) {
|
|
669
1256
|
const via = (n.attrs as Record<string, unknown> | undefined)?.[`${t.a}Via`];
|
|
670
|
-
|
|
671
|
-
if (typeof via === "string") console.log(` ✓ ${id} ${t.a} via ${via}`);
|
|
1257
|
+
if (typeof via === "string") evidence.push({ id: n.id, attr: t.a, via });
|
|
672
1258
|
}
|
|
673
1259
|
}
|
|
674
|
-
const shown = excluded.slice(0,
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
1260
|
+
const shown = excluded.slice(0, NEAR_MISS_LIMIT);
|
|
1261
|
+
return {
|
|
1262
|
+
universe: { matched: matches.length, of: universe.length, kinds: [...kinds] },
|
|
1263
|
+
evidence,
|
|
1264
|
+
nearMisses: shown.map((n) => ({ id: n.id, term: terms.find((t) => !matchTerm(n, t, ir, byId)) })),
|
|
1265
|
+
moreExcluded: excluded.length - shown.length,
|
|
1266
|
+
};
|
|
681
1267
|
}
|
|
682
1268
|
|
|
683
1269
|
/**
|
|
@@ -691,7 +1277,7 @@ function explain(terms: Term[], matches: IRNode[], ir: GraphIR, byId: Map<string
|
|
|
691
1277
|
function availableAttrs(terms: Term[], ir: GraphIR): void {
|
|
692
1278
|
const kindTerm = terms.find((t) => t.kind === "kind");
|
|
693
1279
|
if (!kindTerm) return;
|
|
694
|
-
const of = ir.nodes.filter((n) => n
|
|
1280
|
+
const of = ir.nodes.filter((n) => matchTerm(n, { ...kindTerm, negated: false }));
|
|
695
1281
|
if (of.length === 0) return;
|
|
696
1282
|
const names = new Set<string>();
|
|
697
1283
|
for (const n of of) for (const k of Object.keys((n.attrs as Record<string, unknown>) ?? {})) names.add(k);
|
|
@@ -727,15 +1313,189 @@ function describeTerm(t: Term): string {
|
|
|
727
1313
|
// `--explain` has to say a negated term was negated, or an exclusion reads as
|
|
728
1314
|
// the opposite of what it is.
|
|
729
1315
|
if (t.negated) return `!${describeTerm({ ...t, negated: false })}`;
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
1316
|
+
if (t.kind === "edge") return `${edgeOperator(t, { out: "→", in: "←" })}${t.sub ? leafText(t.sub) : ""} (no such edge)`;
|
|
1317
|
+
return leafText(t);
|
|
1318
|
+
}
|
|
1319
|
+
|
|
1320
|
+
function leafText(x: Term): string {
|
|
1321
|
+
if (x.kind === "cap") return `cap:${x.a}`;
|
|
1322
|
+
return x.kind === "kind" ? `kind:${x.a}` : x.kind === "attr" ? `attr:${x.a}${x.b !== undefined ? "=" + x.b : ""}`
|
|
1323
|
+
: x.kind === "tag" ? `tag:${x.a}${x.b !== undefined ? "=" + x.b : ""}` : `"${x.a}"`;
|
|
1324
|
+
}
|
|
1325
|
+
|
|
1326
|
+
/**
|
|
1327
|
+
* `chant search --json` (#4359, #4367): the whole answer as one document on
|
|
1328
|
+
* stdout, read by behold (behold#913). Warnings and errors still go to stderr;
|
|
1329
|
+
* the exit code is the text path's. It is the `search` document of the read
|
|
1330
|
+
* contract (#4374, `workspace/search.schema.json`): every document opens with
|
|
1331
|
+
* `$schema`, `contract` and `chant`, then `workspace` when the project belongs
|
|
1332
|
+
* to one, and `at: null`, since plain search reads the working tree.
|
|
1333
|
+
*
|
|
1334
|
+
* {
|
|
1335
|
+
* "$schema": "https://intentius.io/chant/schemas/workspace/search/v1/search.schema.json",
|
|
1336
|
+
* "contract": 1, "chant": "0.124.46", "workspace"?: { "name", "root" }, "at": null,
|
|
1337
|
+
* "query": "kind:aws_instance ->*kind:aws_vpc",
|
|
1338
|
+
* "source": { "kind": "declared" | "live" | "snapshot", "env"?, "snapshot"?, "commit"?, "timestamp"? },
|
|
1339
|
+
* "matches": [
|
|
1340
|
+
* { "id": "east::web", "displayId": "web", "kind": "Terraform::Resource", "physicalId"?: "i-1",
|
|
1341
|
+
* "unobserved"?: "read-failed", "show"?: { "InstanceType": "t3.micro" },
|
|
1342
|
+
* "paths": [ { "term": "->*kind:aws_vpc", "dir": "out",
|
|
1343
|
+
* "hops": [ { "from": "web", "fromAttr": "subnet_id", "to": "a", "toAttr": "id" },
|
|
1344
|
+
* { "from": "a", "fromAttr": "vpc_id", "to": "main", "toAttr": "id" } ] } ] }
|
|
1345
|
+
* ],
|
|
1346
|
+
* "provenance": "observed live · bound 2/2",
|
|
1347
|
+
* "liveFailures"?: ["aws: ..."],
|
|
1348
|
+
* "explain"?: { "universe": { "matched": 2, "of": 6, "kinds": ["aws_instance"] },
|
|
1349
|
+
* "evidence": [ { "id": "web", "attr": "internetFacing", "via": "..." } ],
|
|
1350
|
+
* "nearMisses": [ { "id": "db", "fails": "attr:internetFacing=true" } ],
|
|
1351
|
+
* "moreExcluded": 0 },
|
|
1352
|
+
* "drift"?: { "against": "live" | "snapshot", ... see SearchDriftJson }
|
|
1353
|
+
* }
|
|
1354
|
+
*
|
|
1355
|
+
* A refused query or invocation prints the head, `query` and
|
|
1356
|
+
* `error: { code, message, hint? }` instead, and exits 1. The code is from the
|
|
1357
|
+
* read contract's closed list (`SEARCH_ERROR_CODES`).
|
|
1358
|
+
*
|
|
1359
|
+
* `kind` is the IR kind, so a Terraform block's is `Terraform::Resource`;
|
|
1360
|
+
* `kind:aws_instance` matches it by its resource type (#4366).
|
|
1361
|
+
*
|
|
1362
|
+
* `id` is the graph's full id, stack-qualified (`stack::name`) where the graph
|
|
1363
|
+
* qualifies it; `displayId` is the bare name the text row prints. `hops` is in
|
|
1364
|
+
* walk order from the match; each hop keeps the edge's own direction (see
|
|
1365
|
+
* {@link PathHop}). `paths` is empty when the query has no positive edge
|
|
1366
|
+
* term. `show` is present when `--show` was given, holding the requested
|
|
1367
|
+
* attributes a row carries, under the name the graph spells them with.
|
|
1368
|
+
* `explain` is present with `--explain`; `drift` with `--check-live` or
|
|
1369
|
+
* `--check-snapshot`.
|
|
1370
|
+
*/
|
|
1371
|
+
export interface SearchJson {
|
|
1372
|
+
query: string;
|
|
1373
|
+
source: AnswerSource & { env?: string };
|
|
1374
|
+
matches: {
|
|
1375
|
+
id: string;
|
|
1376
|
+
displayId: string;
|
|
1377
|
+
kind?: string;
|
|
1378
|
+
physicalId?: string;
|
|
1379
|
+
unobserved?: string;
|
|
1380
|
+
show?: Record<string, unknown>;
|
|
1381
|
+
paths: { term: string; dir: "out" | "in"; hops: PathHop[]; because?: { capability: string; id: string; hops: PathHop[] }[] }[];
|
|
1382
|
+
}[];
|
|
1383
|
+
provenance?: string;
|
|
1384
|
+
/** Present when every match is a planned create (#4372): how many. See the schema. */
|
|
1385
|
+
plannedCreates?: number;
|
|
1386
|
+
liveFailures?: string[];
|
|
1387
|
+
explain?: {
|
|
1388
|
+
universe: { matched: number; of: number; kinds: string[] };
|
|
1389
|
+
evidence: { id: string; attr: string; via: string }[];
|
|
1390
|
+
nearMisses: { id: string; fails?: string }[];
|
|
1391
|
+
moreExcluded: number;
|
|
1392
|
+
};
|
|
1393
|
+
drift?: SearchDriftJson;
|
|
1394
|
+
/**
|
|
1395
|
+
* Present when the query names a capability (#4375): each one, the
|
|
1396
|
+
* observation its facts were derived from, and what it does not evaluate.
|
|
1397
|
+
*/
|
|
1398
|
+
capabilities?: { name: string; source: AnswerSource["kind"]; notEvaluated: string[] }[];
|
|
1399
|
+
}
|
|
1400
|
+
|
|
1401
|
+
/**
|
|
1402
|
+
* A refusal under `--json` (#4367): the same message and hint stderr carries,
|
|
1403
|
+
* with its code from the read contract's closed list (#4374).
|
|
1404
|
+
*/
|
|
1405
|
+
export type SearchErrorJson = SearchFailure;
|
|
1406
|
+
|
|
1407
|
+
function searchErrorJson(query: string, error: { code: SearchErrorCode; message: string; hint?: string }): SearchErrorJson {
|
|
1408
|
+
return { ...searchHead(), query, error: { code: error.code, message: error.message, ...(error.hint ? { hint: error.hint } : {}) } };
|
|
1409
|
+
}
|
|
1410
|
+
|
|
1411
|
+
/** The head of every `search` document (#4374): the contract, its schema, and the chant that wrote it. */
|
|
1412
|
+
function searchHead(): SearchHead {
|
|
1413
|
+
return { $schema: SEARCH_OUTPUT_SCHEMA_ID, contract: SEARCH_CONTRACT_VERSION, chant: readChantPackageVersion() };
|
|
1414
|
+
}
|
|
1415
|
+
|
|
1416
|
+
/**
|
|
1417
|
+
* The workspace a project belongs to, when it belongs to one (#4374, ws-016):
|
|
1418
|
+
* its declared name and its root relative to the git root, with `at: null`,
|
|
1419
|
+
* since plain search reads the working tree. Outside a workspace, or when its
|
|
1420
|
+
* declaration can't be read, only `at`.
|
|
1421
|
+
*/
|
|
1422
|
+
async function workspaceIdentity(cwd: string): Promise<{ workspace?: { name: string; root: string }; at: null }> {
|
|
1423
|
+
try {
|
|
1424
|
+
const [{ locateWorkspace }, { readDeclaration }] = await Promise.all([import("../../workspace/which-chant"), import("../../workspace/declaration")]);
|
|
1425
|
+
const located = locateWorkspace(cwd);
|
|
1426
|
+
return { workspace: { name: readDeclaration(located.tree, "").name, root: located.root }, at: null };
|
|
1427
|
+
} catch {
|
|
1428
|
+
return { at: null };
|
|
1429
|
+
}
|
|
1430
|
+
}
|
|
1431
|
+
|
|
1432
|
+
function searchJson(
|
|
1433
|
+
query: string,
|
|
1434
|
+
source: AnswerSource,
|
|
1435
|
+
matches: IRNode[],
|
|
1436
|
+
terms: Term[],
|
|
1437
|
+
ir: GraphIR,
|
|
1438
|
+
byId: Map<string, IRNode>,
|
|
1439
|
+
liveFailures: string[] = [],
|
|
1440
|
+
extra: {
|
|
1441
|
+
env?: string;
|
|
1442
|
+
show?: string[];
|
|
1443
|
+
provenance?: string;
|
|
1444
|
+
plannedCreates?: number;
|
|
1445
|
+
capabilities?: SearchJson["capabilities"];
|
|
1446
|
+
explain?: ReturnType<typeof explainData>;
|
|
1447
|
+
drift?: SearchDriftJson;
|
|
1448
|
+
} = {},
|
|
1449
|
+
): SearchJson {
|
|
1450
|
+
const show = extra.show ?? [];
|
|
1451
|
+
return {
|
|
1452
|
+
query,
|
|
1453
|
+
source: extra.env ? { ...source, env: extra.env } : source,
|
|
1454
|
+
matches: matches.map((n) => {
|
|
1455
|
+
const attrs = (n.attrs ?? {}) as Record<string, unknown>;
|
|
1456
|
+
const physical = (n as { physicalId?: unknown }).physicalId ?? attrs["physicalId"] ?? attrs["InstanceId"] ?? attrs["Id"];
|
|
1457
|
+
return {
|
|
1458
|
+
id: n.id,
|
|
1459
|
+
displayId: bareId(n.id),
|
|
1460
|
+
...(n.kind ? { kind: n.kind } : {}),
|
|
1461
|
+
...(physical != null && typeof physical !== "object" ? { physicalId: String(physical) } : {}),
|
|
1462
|
+
...(typeof attrs["_unobserved"] === "string" ? { unobserved: attrs["_unobserved"] } : {}),
|
|
1463
|
+
...(show.length > 0 ? { show: Object.fromEntries(showValues(attrs, show)) } : {}),
|
|
1464
|
+
paths: matchPaths(n, terms, ir, byId).map((p) => ({
|
|
1465
|
+
term: termText({ ...p.term, negated: false }),
|
|
1466
|
+
dir: p.term.dir ?? ("out" as const),
|
|
1467
|
+
hops: p.hops,
|
|
1468
|
+
...(p.because ? { because: p.because } : {}),
|
|
1469
|
+
})),
|
|
1470
|
+
};
|
|
1471
|
+
}),
|
|
1472
|
+
...(extra.provenance !== undefined ? { provenance: extra.provenance } : {}),
|
|
1473
|
+
...(extra.plannedCreates ? { plannedCreates: extra.plannedCreates } : {}),
|
|
1474
|
+
...(extra.capabilities ? { capabilities: extra.capabilities } : {}),
|
|
1475
|
+
...(liveFailures.length > 0 ? { liveFailures } : {}),
|
|
1476
|
+
...(extra.explain
|
|
1477
|
+
? {
|
|
1478
|
+
explain: {
|
|
1479
|
+
universe: extra.explain.universe,
|
|
1480
|
+
evidence: extra.explain.evidence,
|
|
1481
|
+
nearMisses: extra.explain.nearMisses.map((m) => ({ id: m.id, ...(m.term ? { fails: termText(m.term) } : {}) })),
|
|
1482
|
+
moreExcluded: extra.explain.moreExcluded,
|
|
1483
|
+
},
|
|
1484
|
+
}
|
|
1485
|
+
: {}),
|
|
1486
|
+
...(extra.drift ? { drift: extra.drift } : {}),
|
|
1487
|
+
};
|
|
1488
|
+
}
|
|
1489
|
+
|
|
1490
|
+
/** A term as the query grammar spells it, `!` and ASCII arrows included (#4367). */
|
|
1491
|
+
function termText(t: Term): string {
|
|
1492
|
+
if (t.negated) return `!${termText({ ...t, negated: false })}`;
|
|
1493
|
+
if (t.kind === "edge") return `${edgeOperator(t, { out: "->", in: "<-" })}${t.sub ? leafText(t.sub) : ""}`;
|
|
1494
|
+
return leafText(t);
|
|
735
1495
|
}
|
|
736
1496
|
|
|
737
1497
|
export interface Term {
|
|
738
|
-
kind: "word" | "kind" | "tag" | "attr" | "edge";
|
|
1498
|
+
kind: "word" | "kind" | "tag" | "attr" | "edge" | "cap";
|
|
739
1499
|
/** `!term` — the node must NOT satisfy this (#1280). */
|
|
740
1500
|
negated?: boolean;
|
|
741
1501
|
a: string;
|
|
@@ -743,12 +1503,33 @@ export interface Term {
|
|
|
743
1503
|
/** For edge terms: the direction and the sub-predicate matched at the far end. */
|
|
744
1504
|
dir?: "out" | "in";
|
|
745
1505
|
sub?: Term;
|
|
1506
|
+
/**
|
|
1507
|
+
* For edge terms (#4359): how many hops the far end may be away. Absent on a
|
|
1508
|
+
* plain `->`/`<-`, which is exactly one hop. `max` is `Infinity` for `*`.
|
|
1509
|
+
*/
|
|
1510
|
+
depth?: { min: number; max: number };
|
|
1511
|
+
/** For edge terms (#4359): every edge on the path goes through this consumer attribute (`IREdge.viaAttr`). */
|
|
1512
|
+
viaAttr?: string;
|
|
1513
|
+
/** For edge terms (#4359): every edge on the path reads this producer attribute (`IREdge.toAttr`). */
|
|
1514
|
+
toAttr?: string;
|
|
1515
|
+
/**
|
|
1516
|
+
* For edge terms (#4375): walk this edge capability (`reaches`,
|
|
1517
|
+
* `grants(s3:GetObject)`) instead of the graph's own edges. A `cap:` leaf
|
|
1518
|
+
* keeps its call in `a`.
|
|
1519
|
+
*/
|
|
1520
|
+
capability?: string;
|
|
746
1521
|
/**
|
|
747
1522
|
* For kind terms: the kinds this term actually means, resolved against the
|
|
748
1523
|
* graph (see {@link resolveKindTerms}). Absent when nothing resolved it, in
|
|
749
1524
|
* which case the substring rule applies unchanged.
|
|
750
1525
|
*/
|
|
751
1526
|
kinds?: Set<string>;
|
|
1527
|
+
/**
|
|
1528
|
+
* For kind terms (#4366): the terraform resource types this term means,
|
|
1529
|
+
* resolved against the graph. A terraform resource matches the term when its
|
|
1530
|
+
* type is in here, whatever `kinds` says.
|
|
1531
|
+
*/
|
|
1532
|
+
tfTypes?: Set<string>;
|
|
752
1533
|
}
|
|
753
1534
|
|
|
754
1535
|
/**
|
|
@@ -772,12 +1553,25 @@ export interface Term {
|
|
|
772
1553
|
* Substring is kept as the fallback, so `kind:Gateway` still finds both
|
|
773
1554
|
* `InternetGateway` and `VPCGatewayAttachment` — a genuine substring search
|
|
774
1555
|
* that no segment rule would serve. Most specific wins; nothing else changes.
|
|
1556
|
+
*
|
|
1557
|
+
* A terraform resource is `Terraform::Resource` whatever it is, so the term is
|
|
1558
|
+
* also read against each resource's type (#4366), with the same boundary rule
|
|
1559
|
+
* on `_`: `kind:aws_iam_role` means that type exactly and never
|
|
1560
|
+
* `aws_iam_role_policy`, and a term ending in `_` (`kind:aws_iam_`) or a bare
|
|
1561
|
+
* provider name (`kind:aws`) means every type it begins. A data source's type
|
|
1562
|
+
* keeps its `data.` prefix (`kind:data.aws_ami`), the same type `--count-by kind`
|
|
1563
|
+
* groups it under, so `kind:aws_ami` is not a data source. A term that names a
|
|
1564
|
+
* type turns off the substring fallback for the IR kind, so `kind:aws_instance`
|
|
1565
|
+
* is the instances and nothing found by accident.
|
|
775
1566
|
*/
|
|
776
1567
|
export function resolveKindTerms(terms: Term[], nodes: IRNode[]): void {
|
|
777
1568
|
const present = [...new Set(nodes.map((n) => n.kind).filter((k): k is string => !!k))];
|
|
1569
|
+
const tfPresent = [...new Set(nodes.map(terraformType).filter((k): k is string => !!k))];
|
|
778
1570
|
const walk = (t: Term): void => {
|
|
779
1571
|
if (t.sub) walk(t.sub);
|
|
780
1572
|
if (t.kind !== "kind") return;
|
|
1573
|
+
const types = resolveTerraformTypes(t.a, tfPresent);
|
|
1574
|
+
if (types.length > 0) t.tfTypes = new Set(types);
|
|
781
1575
|
const want = t.a.toLowerCase().split("::").filter(Boolean);
|
|
782
1576
|
if (want.length === 0) return;
|
|
783
1577
|
const onBoundary = present.filter((k) => {
|
|
@@ -792,7 +1586,32 @@ export function resolveKindTerms(terms: Term[], nodes: IRNode[]): void {
|
|
|
792
1586
|
terms.forEach(walk);
|
|
793
1587
|
}
|
|
794
1588
|
|
|
1589
|
+
/** The terraform resource types a `kind:` term names, by the rule in {@link resolveKindTerms}. */
|
|
1590
|
+
function resolveTerraformTypes(term: string, present: string[]): string[] {
|
|
1591
|
+
const want = term.toLowerCase();
|
|
1592
|
+
if (want.length === 0 || want.includes("::")) return [];
|
|
1593
|
+
const exact = present.filter((p) => p === want);
|
|
1594
|
+
if (exact.length > 0) return exact;
|
|
1595
|
+
const prefix = want.endsWith("_") || want.endsWith(".") ? want : /[_.]/.test(want) ? undefined : `${want}_`;
|
|
1596
|
+
return prefix ? present.filter((p) => p.startsWith(prefix)) : [];
|
|
1597
|
+
}
|
|
1598
|
+
|
|
1599
|
+
/** What a row and `--explain` call a node's kind: a terraform block's type (#4366), else its IR kind. Same as `--count-by kind`. */
|
|
1600
|
+
function displayKind(n: IRNode): string | undefined {
|
|
1601
|
+
return terraformType(n) ?? n.kind;
|
|
1602
|
+
}
|
|
1603
|
+
|
|
795
1604
|
function parseLeaf(tok: string): Term {
|
|
1605
|
+
const cap = /^cap:(.*)$/i.exec(tok);
|
|
1606
|
+
if (cap) {
|
|
1607
|
+
if (!parseCapabilityCall(cap[1])) {
|
|
1608
|
+
throw new QueryError(
|
|
1609
|
+
`"${tok}" is not a capability term`,
|
|
1610
|
+
"name a capability, with arguments in parentheses when it takes any: cap:internet-reachable, cap:open-ingress(22); chant search --capabilities lists them",
|
|
1611
|
+
);
|
|
1612
|
+
}
|
|
1613
|
+
return { kind: "cap", a: cap[1] };
|
|
1614
|
+
}
|
|
796
1615
|
const m = /^(kind|tag|attr):(.*)$/i.exec(tok);
|
|
797
1616
|
if (m) {
|
|
798
1617
|
const key = m[1].toLowerCase() as Term["kind"];
|
|
@@ -833,6 +1652,76 @@ class QueryError extends Error {
|
|
|
833
1652
|
}
|
|
834
1653
|
}
|
|
835
1654
|
|
|
1655
|
+
/**
|
|
1656
|
+
* Parse an edge term (#4359): `->` or `<-`, then an optional depth, then an
|
|
1657
|
+
* optional attribute qualifier, then the target leaf.
|
|
1658
|
+
*
|
|
1659
|
+
* ->kind:X one hop, as it always was
|
|
1660
|
+
* ->*kind:X any number of hops, one or more
|
|
1661
|
+
* ->{3}kind:X exactly three hops
|
|
1662
|
+
* ->{1,3}kind:X one to three hops ({2,} is two or more)
|
|
1663
|
+
* <-[via=vpc_id]kind:X only edges through the consumer attribute vpc_id
|
|
1664
|
+
* ->[to=id]kind:X only edges that read the producer attribute id
|
|
1665
|
+
* <-*[via=subnet_id] a qualifier may stand without a target: any node
|
|
1666
|
+
*
|
|
1667
|
+
* A qualifier constrains every edge on the path, not only the first one.
|
|
1668
|
+
*/
|
|
1669
|
+
function parseEdge(tok: string, negated: boolean): Term {
|
|
1670
|
+
const dir = tok.startsWith("->") ? ("out" as const) : ("in" as const);
|
|
1671
|
+
const arrow = tok.slice(0, 2);
|
|
1672
|
+
let rest = tok.slice(2);
|
|
1673
|
+
const term: Term = { kind: "edge", a: "", dir };
|
|
1674
|
+
if (rest.startsWith("*")) {
|
|
1675
|
+
term.depth = { min: 1, max: Infinity };
|
|
1676
|
+
rest = rest.slice(1);
|
|
1677
|
+
} else if (rest.startsWith("{")) {
|
|
1678
|
+
const m = /^\{(\d+)(,(\d*))?\}/.exec(rest);
|
|
1679
|
+
const min = m ? Number(m[1]) : NaN;
|
|
1680
|
+
const max = !m ? NaN : m[2] === undefined ? min : m[3] === "" ? Infinity : Number(m[3]);
|
|
1681
|
+
if (!m || min < 1 || max < min) {
|
|
1682
|
+
throw new QueryError(
|
|
1683
|
+
`"${tok}" has a depth this grammar cannot read`,
|
|
1684
|
+
`a depth is {n}, {min,max} or {min,} with 1 <= min <= max, e.g. ${arrow}{1,3}kind:aws_vpc; ${arrow}* is any depth`,
|
|
1685
|
+
);
|
|
1686
|
+
}
|
|
1687
|
+
term.depth = { min, max };
|
|
1688
|
+
rest = rest.slice(m[0].length);
|
|
1689
|
+
}
|
|
1690
|
+
if (rest.startsWith("[")) {
|
|
1691
|
+
const close = rest.indexOf("]");
|
|
1692
|
+
// Split on commas outside parentheses, so `[cap=open-ingress(22,tcp)]` is one part.
|
|
1693
|
+
const parts = close < 0 ? [""] : (rest.slice(1, close).match(/(?:[^,(]|\([^)]*\))+/g) ?? [""]).map((p) => p.trim());
|
|
1694
|
+
for (const part of parts) {
|
|
1695
|
+
const c = /^cap=(.+)$/.exec(part);
|
|
1696
|
+
if (c && parseCapabilityCall(c[1])) {
|
|
1697
|
+
term.capability = c[1];
|
|
1698
|
+
continue;
|
|
1699
|
+
}
|
|
1700
|
+
const q = /^(via|to)=(.+)$/.exec(part);
|
|
1701
|
+
if (!q) {
|
|
1702
|
+
throw new QueryError(
|
|
1703
|
+
`"${tok}" has an edge qualifier this grammar cannot read`,
|
|
1704
|
+
`qualify an edge by attribute with [via=<consumer attr>] or [to=<producer attr>], e.g. <-[via=vpc_id]kind:aws_subnet or ->[via=subnet_id,to=id], or walk a capability with [cap=<name>], e.g. ->[cap=reaches]cap:stores-data`,
|
|
1705
|
+
);
|
|
1706
|
+
}
|
|
1707
|
+
if (q[1] === "via") term.viaAttr = q[2];
|
|
1708
|
+
else term.toAttr = q[2];
|
|
1709
|
+
}
|
|
1710
|
+
rest = rest.slice(close + 1);
|
|
1711
|
+
}
|
|
1712
|
+
if (rest.trim() === "") {
|
|
1713
|
+
if (term.viaAttr === undefined && term.toAttr === undefined && term.capability === undefined) {
|
|
1714
|
+
const bang = negated ? "!" : "";
|
|
1715
|
+
throw new QueryError(
|
|
1716
|
+
`"${bang}${tok}" needs a target`,
|
|
1717
|
+
`say what the edge reaches: ${bang}${tok}kind:EC2::Instance, or qualify it by attribute: ${bang}${tok}[via=vpc_id]`,
|
|
1718
|
+
);
|
|
1719
|
+
}
|
|
1720
|
+
return term;
|
|
1721
|
+
}
|
|
1722
|
+
return { ...term, sub: parseLeaf(rest) };
|
|
1723
|
+
}
|
|
1724
|
+
|
|
836
1725
|
function parseQuery(query: string): Term[] {
|
|
837
1726
|
// Split on whitespace but keep quoted phrases together.
|
|
838
1727
|
const tokens = query.match(/"[^"]*"|\S+/g) ?? [];
|
|
@@ -873,11 +1762,7 @@ function parseQuery(query: string): Term[] {
|
|
|
873
1762
|
`say what the edge reaches: ${negated ? "!" : ""}${tok}kind:EC2::Instance, or ${negated ? "!" : ""}${tok}attr:Name=web`,
|
|
874
1763
|
);
|
|
875
1764
|
}
|
|
876
|
-
const term = tok.startsWith("->")
|
|
877
|
-
? { kind: "edge" as const, a: "", dir: "out" as const, sub: parseLeaf(tok.slice(2)) }
|
|
878
|
-
: tok.startsWith("<-")
|
|
879
|
-
? { kind: "edge" as const, a: "", dir: "in" as const, sub: parseLeaf(tok.slice(2)) }
|
|
880
|
-
: parseLeaf(tok);
|
|
1765
|
+
const term: Term = tok.startsWith("->") || tok.startsWith("<-") ? parseEdge(tok, negated) : parseLeaf(tok);
|
|
881
1766
|
return negated ? { ...term, negated: true } : term;
|
|
882
1767
|
});
|
|
883
1768
|
}
|
|
@@ -895,11 +1780,28 @@ function attrString(v: unknown): string {
|
|
|
895
1780
|
return String(v);
|
|
896
1781
|
}
|
|
897
1782
|
|
|
1783
|
+
/**
|
|
1784
|
+
* Attributes a bare word does not search, by lexicon (#4368). A terraform
|
|
1785
|
+
* node's `source` is the whole `.tf` file its block came from (the lint
|
|
1786
|
+
* checks need the raw text), so a bare `aws_security_group` matched every
|
|
1787
|
+
* node declared in a file that mentions one: 21 nodes on behold's example
|
|
1788
|
+
* estate, where three are security groups. The block itself is still
|
|
1789
|
+
* searched, through `address` and `body`, and `attr:source=<text>` still
|
|
1790
|
+
* reads the file when that is what the caller means.
|
|
1791
|
+
*/
|
|
1792
|
+
const BARE_WORD_SKIPS: Record<string, ReadonlySet<string>> = {
|
|
1793
|
+
terraform: new Set(["source"]),
|
|
1794
|
+
};
|
|
1795
|
+
|
|
898
1796
|
function matchTerm(n: IRNode, t: Term, ir?: GraphIR, byId?: Map<string, IRNode>): boolean {
|
|
899
1797
|
if (t.negated) return !matchTerm(n, { ...t, negated: false }, ir, byId);
|
|
900
1798
|
const attrs = n.attrs ?? {};
|
|
901
1799
|
if (t.kind === "edge") {
|
|
902
|
-
if (!ir || !byId
|
|
1800
|
+
if (!ir || !byId) return false;
|
|
1801
|
+
// Depth and attribute qualifiers (#4359) go through the path search. A
|
|
1802
|
+
// plain one-hop term keeps the original code below, unchanged.
|
|
1803
|
+
if (t.depth || t.viaAttr !== undefined || t.toAttr !== undefined || t.capability !== undefined) return findPath(n, t, ir, byId) !== null;
|
|
1804
|
+
if (!t.sub) return false;
|
|
903
1805
|
// A node matches if it has an edge (out or in) to a node satisfying `sub`.
|
|
904
1806
|
const edges = ir.edges ?? [];
|
|
905
1807
|
const neighbors = edges
|
|
@@ -908,10 +1810,14 @@ function matchTerm(n: IRNode, t: Term, ir?: GraphIR, byId?: Map<string, IRNode>)
|
|
|
908
1810
|
.filter((x): x is IRNode => !!x);
|
|
909
1811
|
return neighbors.some((m) => matchTerm(m, t.sub!, ir, byId));
|
|
910
1812
|
}
|
|
1813
|
+
if (t.kind === "cap") return !!ir && capabilityEvidence(n, t, ir) !== undefined;
|
|
911
1814
|
if (t.kind === "kind") {
|
|
912
1815
|
// Resolved kinds when the term lined up with `::` boundaries; the
|
|
913
|
-
// documented substring otherwise.
|
|
1816
|
+
// documented substring otherwise. A term that named a terraform type
|
|
1817
|
+
// (#4366) matches resources of that type as well, and drops the substring.
|
|
1818
|
+
if (t.tfTypes?.has(terraformType(n) ?? "")) return true;
|
|
914
1819
|
if (t.kinds) return t.kinds.has(n.kind ?? "");
|
|
1820
|
+
if (t.tfTypes) return false;
|
|
915
1821
|
return (n.kind ?? "").toLowerCase().includes(t.a.toLowerCase());
|
|
916
1822
|
}
|
|
917
1823
|
if (t.kind === "attr") {
|
|
@@ -927,16 +1833,244 @@ function matchTerm(n: IRNode, t: Term, ir?: GraphIR, byId?: Map<string, IRNode>)
|
|
|
927
1833
|
return key.toLowerCase() === t.a.toLowerCase() && (t.b === undefined || val.toLowerCase().includes(t.b.toLowerCase()));
|
|
928
1834
|
});
|
|
929
1835
|
}
|
|
930
|
-
// bare word: substring over id, kind, and all attr values
|
|
931
|
-
|
|
1836
|
+
// bare word: substring over id, kind, and all attr values, less the ones
|
|
1837
|
+
// that hold text about other nodes (see BARE_WORD_SKIPS).
|
|
1838
|
+
const skip = BARE_WORD_SKIPS[n.lexicon];
|
|
1839
|
+
const values = Object.entries(attrs)
|
|
1840
|
+
.filter(([k]) => !skip?.has(k))
|
|
1841
|
+
.map(([, v]) => attrString(v));
|
|
1842
|
+
const hay = [n.id, n.kind, ...values].join(" ").toLowerCase();
|
|
932
1843
|
return hay.includes(t.a.toLowerCase());
|
|
933
1844
|
}
|
|
934
1845
|
|
|
1846
|
+
/**
|
|
1847
|
+
* One edge on the path a match was reached by (#4359), in walk order from the
|
|
1848
|
+
* match outward. `from` and `to` keep the edge's own direction (`from` holds
|
|
1849
|
+
* the reference, `to` is what it names), whichever way the term walked, so a
|
|
1850
|
+
* reader can find the edge in the graph by its endpoints. Node ids are the
|
|
1851
|
+
* graph's full ids, stack qualification included.
|
|
1852
|
+
*/
|
|
1853
|
+
export interface PathHop {
|
|
1854
|
+
from: string;
|
|
1855
|
+
/** The consumer attribute the reference flows through (`IREdge.viaAttr`), when known. */
|
|
1856
|
+
fromAttr?: string;
|
|
1857
|
+
to: string;
|
|
1858
|
+
/** The producer attribute read (`IREdge.toAttr`), when known. */
|
|
1859
|
+
toAttr?: string;
|
|
1860
|
+
/**
|
|
1861
|
+
* On a capability's path (#4375): the name the capability rules give this
|
|
1862
|
+
* edge (`subnet`, `statement`, `resource`). Its `from` may be a node inside
|
|
1863
|
+
* another (a policy statement, `<holder>#policy[0].Statement[1]`).
|
|
1864
|
+
*/
|
|
1865
|
+
edge?: string;
|
|
1866
|
+
}
|
|
1867
|
+
|
|
1868
|
+
const adjacency = new WeakMap<GraphIR, { out: Map<string, IREdge[]>; in: Map<string, IREdge[]> }>();
|
|
1869
|
+
|
|
1870
|
+
function edgeIndex(ir: GraphIR): { out: Map<string, IREdge[]>; in: Map<string, IREdge[]> } {
|
|
1871
|
+
let idx = adjacency.get(ir);
|
|
1872
|
+
if (!idx) {
|
|
1873
|
+
idx = { out: new Map(), in: new Map() };
|
|
1874
|
+
for (const e of ir.edges ?? []) {
|
|
1875
|
+
if (!idx.out.has(e.from)) idx.out.set(e.from, []);
|
|
1876
|
+
idx.out.get(e.from)!.push(e);
|
|
1877
|
+
if (!idx.in.has(e.to)) idx.in.set(e.to, []);
|
|
1878
|
+
idx.in.get(e.to)!.push(e);
|
|
1879
|
+
}
|
|
1880
|
+
adjacency.set(ir, idx);
|
|
1881
|
+
}
|
|
1882
|
+
return idx;
|
|
1883
|
+
}
|
|
1884
|
+
|
|
1885
|
+
function sameName(a: string | undefined, b: string): boolean {
|
|
1886
|
+
return a !== undefined && a.toLowerCase() === b.toLowerCase();
|
|
1887
|
+
}
|
|
1888
|
+
|
|
1889
|
+
/**
|
|
1890
|
+
* The shortest path by which an edge term reaches `n` (#4359), or null.
|
|
1891
|
+
*
|
|
1892
|
+
* Breadth-first over the edges in the term's direction, keeping only edges
|
|
1893
|
+
* whose attributes satisfy its qualifier, and accepting a node that satisfies
|
|
1894
|
+
* the target leaf (any node when the term has none) at a depth inside the
|
|
1895
|
+
* term's range. A plain `->`/`<-` is the range 1..1.
|
|
1896
|
+
*
|
|
1897
|
+
* Termination on a cycle: a state is (node, depth capped at the range's
|
|
1898
|
+
* minimum). Past the minimum, arriving at a node again by a longer walk can
|
|
1899
|
+
* reach nothing the first arrival could not, so it is not queued twice. The
|
|
1900
|
+
* state space is finite and the walk always ends.
|
|
1901
|
+
*/
|
|
1902
|
+
function findPath(n: IRNode, t: Term, ir: GraphIR, byId: Map<string, IRNode>): PathHop[] | null {
|
|
1903
|
+
if (t.kind !== "edge") return null;
|
|
1904
|
+
const { min, max } = t.depth ?? { min: 1, max: 1 };
|
|
1905
|
+
const idx = edgeIndex(ir);
|
|
1906
|
+
const out = t.dir === "out";
|
|
1907
|
+
type State = { id: string; depth: number; hops: PathHop[] };
|
|
1908
|
+
// A capability edge (#4375) stands for the hops that proved it.
|
|
1909
|
+
const steps = (id: string): { far: string; hops: PathHop[] }[] => {
|
|
1910
|
+
if (t.capability !== undefined) return capabilitySteps(id, t.capability, out, ir);
|
|
1911
|
+
return (((out ? idx.out.get(id) : idx.in.get(id)) ?? [])
|
|
1912
|
+
.filter((e) => (t.viaAttr === undefined || sameName(e.viaAttr, t.viaAttr)) && (t.toAttr === undefined || sameName(e.toAttr, t.toAttr)))
|
|
1913
|
+
.map((e) => ({
|
|
1914
|
+
far: out ? e.to : e.from,
|
|
1915
|
+
hops: [{ from: e.from, ...(e.viaAttr ? { fromAttr: e.viaAttr } : {}), to: e.to, ...(e.toAttr ? { toAttr: e.toAttr } : {}) }],
|
|
1916
|
+
})));
|
|
1917
|
+
};
|
|
1918
|
+
const seen = new Set<string>([`${n.id}|0`]);
|
|
1919
|
+
let frontier: State[] = [{ id: n.id, depth: 0, hops: [] }];
|
|
1920
|
+
while (frontier.length > 0) {
|
|
1921
|
+
const next: State[] = [];
|
|
1922
|
+
for (const s of frontier) {
|
|
1923
|
+
if (s.depth >= max) continue;
|
|
1924
|
+
for (const step of steps(s.id)) {
|
|
1925
|
+
const farId = step.far;
|
|
1926
|
+
const far = byId.get(farId);
|
|
1927
|
+
if (!far) continue;
|
|
1928
|
+
const depth = s.depth + 1;
|
|
1929
|
+
const hops = [...s.hops, ...step.hops];
|
|
1930
|
+
if (depth >= min && (!t.sub || matchTerm(far, t.sub, ir, byId))) return hops;
|
|
1931
|
+
const key = `${farId}|${Math.min(depth, min)}`;
|
|
1932
|
+
if (seen.has(key)) continue;
|
|
1933
|
+
seen.add(key);
|
|
1934
|
+
next.push({ id: farId, depth, hops });
|
|
1935
|
+
}
|
|
1936
|
+
}
|
|
1937
|
+
frontier = next;
|
|
1938
|
+
}
|
|
1939
|
+
return null;
|
|
1940
|
+
}
|
|
1941
|
+
|
|
1942
|
+
function bareId(id: string): string {
|
|
1943
|
+
return id.includes("::") ? id.slice(id.lastIndexOf("::") + 2) : id;
|
|
1944
|
+
}
|
|
1945
|
+
|
|
1946
|
+
/**
|
|
1947
|
+
* A path as one line of `id.attr` steps, read from the match outward (#4359).
|
|
1948
|
+
* Walking `->`, each hop reads `consumer.via -> producer.attr`; walking `<-`,
|
|
1949
|
+
* `producer.attr <- consumer.via`. Hops are joined with `; `.
|
|
1950
|
+
*/
|
|
1951
|
+
function formatPath(hops: PathHop[], dir: "out" | "in"): string {
|
|
1952
|
+
const end = (id: string, attr?: string): string => (attr ? `${bareId(id)}.${attr}` : bareId(id));
|
|
1953
|
+
return hops
|
|
1954
|
+
.map((h) =>
|
|
1955
|
+
dir === "out"
|
|
1956
|
+
? `${end(h.from, h.fromAttr)} -> ${end(h.to, h.toAttr)}`
|
|
1957
|
+
: `${end(h.to, h.toAttr)} <- ${end(h.from, h.fromAttr)}`,
|
|
1958
|
+
)
|
|
1959
|
+
.join("; ");
|
|
1960
|
+
}
|
|
1961
|
+
|
|
1962
|
+
/** The edge operator as written: arrow, depth and qualifier, no target. */
|
|
1963
|
+
function edgeOperator(t: Term, arrows: { out: string; in: string }): string {
|
|
1964
|
+
const arrow = t.dir === "out" ? arrows.out : arrows.in;
|
|
1965
|
+
const depth = !t.depth
|
|
1966
|
+
? ""
|
|
1967
|
+
: t.depth.min === 1 && t.depth.max === Infinity
|
|
1968
|
+
? "*"
|
|
1969
|
+
: t.depth.min === t.depth.max
|
|
1970
|
+
? `{${t.depth.min}}`
|
|
1971
|
+
: `{${t.depth.min},${t.depth.max === Infinity ? "" : t.depth.max}}`;
|
|
1972
|
+
const quals = [
|
|
1973
|
+
...(t.viaAttr !== undefined ? [`via=${t.viaAttr}`] : []),
|
|
1974
|
+
...(t.toAttr !== undefined ? [`to=${t.toAttr}`] : []),
|
|
1975
|
+
...(t.capability !== undefined ? [`cap=${t.capability}`] : []),
|
|
1976
|
+
];
|
|
1977
|
+
return `${arrow}${depth}${quals.length > 0 ? `[${quals.join(",")}]` : ""}`;
|
|
1978
|
+
}
|
|
1979
|
+
|
|
1980
|
+
/** Each positive edge term's path to `n`, in query order (#4359). */
|
|
1981
|
+
function matchPaths(n: IRNode, terms: Term[], ir: GraphIR, byId: Map<string, IRNode>): MatchPath[] {
|
|
1982
|
+
const out: MatchPath[] = [];
|
|
1983
|
+
for (const t of terms) {
|
|
1984
|
+
if (t.negated) continue;
|
|
1985
|
+
if (t.kind === "cap") {
|
|
1986
|
+
const ev = capabilityEvidence(n, t, ir);
|
|
1987
|
+
if (ev) out.push({ term: t, hops: ev.hops.map(pathHop), because: becauseOf(ev) });
|
|
1988
|
+
continue;
|
|
1989
|
+
}
|
|
1990
|
+
if (t.kind !== "edge") continue;
|
|
1991
|
+
const hops = findPath(n, t, ir, byId);
|
|
1992
|
+
if (hops) out.push({ term: t, hops });
|
|
1993
|
+
}
|
|
1994
|
+
return out;
|
|
1995
|
+
}
|
|
1996
|
+
|
|
1997
|
+
/** A term's path to a match: one per positive edge or `cap:` term. */
|
|
1998
|
+
interface MatchPath {
|
|
1999
|
+
term: Term;
|
|
2000
|
+
hops: PathHop[];
|
|
2001
|
+
/** For a `cap:` term (#4375): the node sets the match relied on, each with its own path. */
|
|
2002
|
+
because?: { capability: string; id: string; hops: PathHop[] }[];
|
|
2003
|
+
}
|
|
2004
|
+
|
|
2005
|
+
/**
|
|
2006
|
+
* The evidence for a `cap:` term on a node (#4375): its path when the node
|
|
2007
|
+
* is in the set, or the source of an edge capability, else undefined. A
|
|
2008
|
+
* capability or argument the catalog can't take throws a CapabilityError,
|
|
2009
|
+
* which `runSearch` turns into a refusal before any node is tested.
|
|
2010
|
+
*/
|
|
2011
|
+
function capabilityEvidence(n: IRNode, t: Term, ir: GraphIR): Evidence | undefined {
|
|
2012
|
+
const call = parseCapabilityCall(t.a);
|
|
2013
|
+
if (!call) return undefined;
|
|
2014
|
+
const engine = capabilityEngine(ir);
|
|
2015
|
+
const args = engine.bind(call.name, call.args);
|
|
2016
|
+
if (engine.rule(call.name)!.vars.length === 1) return engine.set(call.name, args).get(n.id);
|
|
2017
|
+
const targets = engine.relation(call.name, args).out.get(n.id);
|
|
2018
|
+
return targets ? targets.values().next().value : undefined;
|
|
2019
|
+
}
|
|
2020
|
+
|
|
2021
|
+
/** One step along an edge capability from `id` (#4375), each with the hops that proved it, in walk order. */
|
|
2022
|
+
function capabilitySteps(id: string, call: string, out: boolean, ir: GraphIR): { far: string; hops: PathHop[] }[] {
|
|
2023
|
+
const parsed = parseCapabilityCall(call);
|
|
2024
|
+
if (!parsed) return [];
|
|
2025
|
+
const engine = capabilityEngine(ir);
|
|
2026
|
+
const rel = engine.relation(parsed.name, engine.bind(parsed.name, parsed.args));
|
|
2027
|
+
const m = (out ? rel.out : rel.in).get(id);
|
|
2028
|
+
if (!m) return [];
|
|
2029
|
+
return [...m].map(([far, ev]) => ({ far, hops: (out ? ev.hops : [...ev.hops].reverse()).map(pathHop) }));
|
|
2030
|
+
}
|
|
2031
|
+
|
|
2032
|
+
function pathHop(h: { from: string; fromAttr?: string; to: string; toAttr?: string; edge: string }): PathHop {
|
|
2033
|
+
return { from: h.from, ...(h.fromAttr ? { fromAttr: h.fromAttr } : {}), to: h.to, ...(h.toAttr ? { toAttr: h.toAttr } : {}), edge: h.edge };
|
|
2034
|
+
}
|
|
2035
|
+
|
|
2036
|
+
function becauseOf(ev: Evidence): MatchPath["because"] {
|
|
2037
|
+
const out: NonNullable<MatchPath["because"]> = [];
|
|
2038
|
+
const walk = (e: Evidence): void => {
|
|
2039
|
+
for (const b of e.because) {
|
|
2040
|
+
out.push({ capability: b.capability, id: b.id, hops: b.evidence.hops.map(pathHop) });
|
|
2041
|
+
walk(b.evidence);
|
|
2042
|
+
}
|
|
2043
|
+
};
|
|
2044
|
+
walk(ev);
|
|
2045
|
+
return out.length > 0 ? out : undefined;
|
|
2046
|
+
}
|
|
2047
|
+
|
|
2048
|
+
/**
|
|
2049
|
+
* A capability's path as one line (#4375), in #4359's hop format, read from
|
|
2050
|
+
* `start`: each hop is `consumer.via -> producer.attr` when the walk follows
|
|
2051
|
+
* the reference, `producer.attr <- consumer.via` when it goes against it. A
|
|
2052
|
+
* hop with no IR attribute names the capability edge instead (`-statement->`).
|
|
2053
|
+
*/
|
|
2054
|
+
function formatHops(hops: PathHop[], start: string): string {
|
|
2055
|
+
const end = (id: string, attr?: string): string => (attr ? `${bareId(id)}.${attr}` : bareId(id));
|
|
2056
|
+
let at = start;
|
|
2057
|
+
return hops
|
|
2058
|
+
.map((h) => {
|
|
2059
|
+
const forward = h.from === at || h.to !== at;
|
|
2060
|
+
at = forward ? h.to : h.from;
|
|
2061
|
+
return forward
|
|
2062
|
+
? `${end(h.from, h.fromAttr)} -> ${end(h.to, h.toAttr)}`
|
|
2063
|
+
: `${end(h.to, h.toAttr)} <- ${end(h.from, h.fromAttr)}`;
|
|
2064
|
+
})
|
|
2065
|
+
.join("; ");
|
|
2066
|
+
}
|
|
2067
|
+
|
|
935
2068
|
function formatRow(n: IRNode, show: string[]): string {
|
|
936
2069
|
const attrs = (n.attrs ?? {}) as Record<string, unknown>;
|
|
937
2070
|
// Display the bare logical id, not the `${stack}::` qualification (#1162).
|
|
938
2071
|
const displayId = n.id.includes("::") ? n.id.slice(n.id.lastIndexOf("::") + 2) : n.id;
|
|
939
|
-
|
|
2072
|
+
// A terraform resource shows its type (#4366); its IR kind is `Terraform::Resource` for all of them.
|
|
2073
|
+
const parts: string[] = [displayId, displayKind(n) ?? ""];
|
|
940
2074
|
// Prefer the node-level live physicalId (set by the overlay), then attrs;
|
|
941
2075
|
// skip source-mode AttrRef placeholders (objects).
|
|
942
2076
|
const physical = (n as { physicalId?: unknown }).physicalId ?? attrs["physicalId"] ?? attrs["InstanceId"] ?? attrs["Id"];
|
|
@@ -945,6 +2079,24 @@ function formatRow(n: IRNode, show: string[]): string {
|
|
|
945
2079
|
// blank there reads as "declared, not provisioned", which the read never
|
|
946
2080
|
// established; the reason is the tri-state's (#1089).
|
|
947
2081
|
else if (typeof attrs["_unobserved"] === "string") parts.push(`(unobserved: ${attrs["_unobserved"]})`);
|
|
2082
|
+
for (const [actual, v] of showValues(attrs, show)) {
|
|
2083
|
+
// A column the caller explicitly asked for is shown whatever shape it is.
|
|
2084
|
+
// Skipping non-scalars silently meant `--show effectiveIngress` — the
|
|
2085
|
+
// derived reachability fact, and the reason to reach for chant at all —
|
|
2086
|
+
// printed a blank column, because it is a list. The agent read that as
|
|
2087
|
+
// "chant does not have this" and hand-rolled the answer from raw
|
|
2088
|
+
// security-group rows, which is exactly the work the fold exists to avoid.
|
|
2089
|
+
parts.push(`${actual}=${typeof v === "object" ? JSON.stringify(v) : attrString(v)}`);
|
|
2090
|
+
}
|
|
2091
|
+
return parts.filter(Boolean).join(" ");
|
|
2092
|
+
}
|
|
2093
|
+
|
|
2094
|
+
/**
|
|
2095
|
+
* The `--show` columns a row carries, as `[name as the graph spells it, value]`,
|
|
2096
|
+
* in the order asked. Shared by the text row and `--json` (#4367).
|
|
2097
|
+
*/
|
|
2098
|
+
function showValues(attrs: Record<string, unknown>, show: string[]): [string, unknown][] {
|
|
2099
|
+
const out: [string, unknown][] = [];
|
|
948
2100
|
for (const key of show) {
|
|
949
2101
|
// Match the name case-insensitively, and report what was actually found.
|
|
950
2102
|
// AWS attribute names are PascalCase and chant's derived ones are not, so a
|
|
@@ -954,16 +2106,10 @@ function formatRow(n: IRNode, show: string[]): string {
|
|
|
954
2106
|
const actual = key in attrs ? key : Object.keys(attrs).find((k) => k.toLowerCase() === key.toLowerCase());
|
|
955
2107
|
const v = actual == null ? undefined : attrs[actual];
|
|
956
2108
|
if (v == null) continue;
|
|
957
|
-
|
|
958
|
-
// Skipping non-scalars silently meant `--show effectiveIngress` — the
|
|
959
|
-
// derived reachability fact, and the reason to reach for chant at all —
|
|
960
|
-
// printed a blank column, because it is a list. The agent read that as
|
|
961
|
-
// "chant does not have this" and hand-rolled the answer from raw
|
|
962
|
-
// security-group rows, which is exactly the work the fold exists to avoid.
|
|
963
|
-
parts.push(`${actual}=${typeof v === "object" ? JSON.stringify(v) : attrString(v)}`);
|
|
2109
|
+
out.push([actual!, v]);
|
|
964
2110
|
}
|
|
965
|
-
return
|
|
2111
|
+
return out;
|
|
966
2112
|
}
|
|
967
2113
|
|
|
968
2114
|
/** Internals exposed for unit tests. */
|
|
969
|
-
export const __searchInternals = { parseQuery, matchTerm, formatRow, explain, describeTerm, derivedSurface, availableAttrs, ambientHint, regionSpread, showMiss, provenance, renderDrift, mergeResources, pickResources };
|
|
2115
|
+
export const __searchInternals = { parseQuery, matchTerm, findPath, formatPath, searchJson, searchErrorJson, explainData, termText, formatRow, explain, describeTerm, derivedSurface, availableAttrs, ambientHint, regionSpread, showMiss, provenance, provenanceLine, renderDrift, searchDriftJson, mergeResources, pickResources };
|