Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 68 additions & 5 deletions packages/core/src/lifecycle/status.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,18 @@ describe("status", () => {
],
};
const evidence = liveEvidenceFromChangeSet(cs);
expect(evidence.get("search-service")).toEqual({ live: true, action: "noop", ownership: "owned" });
expect(evidence.get("orphan-thing")).toEqual({ live: true, action: "adopt", ownership: "foreign" });
expect(evidence.get("search-service")).toEqual({
live: true,
action: "noop",
ownership: "owned",
rollup: { total: 1, present: 1, absent: 0, unobserved: 0 },
});
expect(evidence.get("orphan-thing")).toEqual({
live: true,
action: "adopt",
ownership: "foreign",
rollup: { total: 1, present: 1, absent: 0, unobserved: 0 },
});
});

// #598: a component's name need not equal the live entity/resource name
Expand All @@ -54,7 +64,15 @@ describe("status", () => {
};
const mapping: LiveNameMapping = new Map([["search-svc", ["search-service-v2"]]]);
const evidence = liveEvidenceFromChangeSet(cs, mapping);
expect(evidence.get("search-svc")).toEqual({ live: true, action: "noop", ownership: "owned" });
// The merged verdict, plus the per-resource counts it collapsed
// (behold#98) — a consumer with no deploy object to read paints from
// those rather than from a CloudFormation stack.
expect(evidence.get("search-svc")).toEqual({
live: true,
action: "noop",
ownership: "owned",
rollup: { total: 1, present: 1, absent: 0, unobserved: 0 },
});
// The live entity's own name is no longer a separate top-level key once
// it's claimed by an explicit mapping's component.
});
Expand All @@ -68,7 +86,14 @@ describe("status", () => {
};
const mapping: LiveNameMapping = new Map([["some-other-component", ["renamed-thing"]]]);
const evidence = liveEvidenceFromChangeSet(cs, mapping);
expect(evidence.get("search-service")).toEqual({ live: true, action: "noop", ownership: "owned" });
// A rollup of one: the identity join still reports the shape, so a
// consumer never branches on whether a mapping was configured.
expect(evidence.get("search-service")).toEqual({
live: true,
action: "noop",
ownership: "owned",
rollup: { total: 1, present: 1, absent: 0, unobserved: 0 },
});
});

test("aggregates evidence across several live names owned by one component", () => {
Expand All @@ -82,7 +107,12 @@ describe("status", () => {
const mapping: LiveNameMapping = new Map([["neo4j-cluster", ["cluster-node-1", "cluster-node-2"]]]);
const evidence = liveEvidenceFromChangeSet(cs, mapping);
// Drift on any owned entity surfaces as drift for the component.
expect(evidence.get("neo4j-cluster")).toEqual({ live: true, action: "update", ownership: "owned" });
expect(evidence.get("neo4j-cluster")).toEqual({
live: true,
action: "update",
ownership: "owned",
rollup: { total: 2, present: 2, absent: 0, unobserved: 0 },
});
});

test("a mapped component with none of its live names observed has no evidence entry", () => {
Expand Down Expand Up @@ -135,6 +165,39 @@ describe("status", () => {
expect(rows[0].reconciliation).toBe("drifted");
});

// behold#98 — floci-az and floci-gcp have no deploy object, so `stack` is
// absent and a renderer has nothing provider-native to colour from. The
// rollup is the substrate-neutral source for the same job.
test("surfaces a resource rollup for a component with no deploy object", () => {
const liveEvidence = new Map<string, LiveComponentEvidence>([
[
"search-service",
{ live: true, ownership: "owned", rollup: { total: 4, present: 3, absent: 0, unobserved: 1 } },
],
]);
const rows = reconcileStatus("prod", [record()], { liveEvidence });
expect(rows[0].resources).toEqual({ total: 4, present: 3, absent: 0, unobserved: 1 });
// No CloudFormation stack to enrich from, and the row is still paintable.
expect(rows[0].stack).toBeUndefined();
});

test("a rollup and a stack coexist — the stack stays the richer enrichment where it exists", () => {
const liveEvidence = new Map<string, LiveComponentEvidence>([
[
"search-service",
{
live: true,
ownership: "owned",
stack: { name: "app-prod-search", status: "CREATE_COMPLETE", healthy: true },
rollup: { total: 2, present: 2, absent: 0, unobserved: 0 },
},
],
]);
const rows = reconcileStatus("prod", [record()], { liveEvidence });
expect(rows[0].resources).toEqual({ total: 2, present: 2, absent: 0, unobserved: 0 });
expect(rows[0].stack?.healthy).toBe(true);
});

test("surfaces machine-readable live + stack status when observed (#57 hardening)", () => {
const liveEvidence = new Map<string, LiveComponentEvidence>([
["search-service", { live: true, ownership: "owned", stack: { name: "app-prod-search", status: "CREATE_COMPLETE", healthy: true } }],
Expand Down
74 changes: 72 additions & 2 deletions packages/core/src/lifecycle/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ export interface ComponentStatusRow {
* present-but-not-healthy amber (mid-deploy) / red (rollback/failed).
*/
stack?: LiveStackInfo;
/**
* How this component's own resources answered (behold#98). Present whenever
* `--live` gathered evidence across a live-name mapping.
*
* `stack` above only exists where the substrate has a deploy object to read,
* which is AWS and nowhere else — floci-az and floci-gcp have none, so a
* consumer painting component status off `stack` has nothing to paint from
* there. These counts are the substrate-neutral source for the same job:
* they aggregate observations, which every lexicon produces, rather than a
* provider-specific grouping object. `stack` stays as the richer enrichment
* where it exists.
*/
resources?: ComponentResourceRollup;
}

/** A component's owning deploy unit and its provider-native status. */
Expand Down Expand Up @@ -129,6 +142,14 @@ export interface LiveComponentEvidence {
/** The owning deploy unit's raw status, when observed (AWS: the component's own
* CFN stack). Surfaced onto `ComponentStatusRow.stack` for a richer palette. */
stack?: LiveStackInfo;
/**
* How the component's own resources answered, before any merge collapsed
* them (behold#98). Always present when evidence exists — a component that
* maps to a single entity by identity gets a rollup of one, so a consumer
* never has to branch on whether a live-name mapping happened to be
* configured.
*/
rollup?: ComponentResourceRollup;
}

/**
Expand Down Expand Up @@ -180,17 +201,57 @@ export function resolveLiveNames(component: string, mapping?: LiveNameMapping):
return mapped && mapped.length > 0 ? mapped : [component];
}

/**
* How a component's own resources answered, one count per tri-state verdict.
*
* The merged verdict above is deliberately lossy — it answers "is this
* component deployed" and nothing else. A consumer painting component status
* without a deploy object to read (behold#98: floci-az and floci-gcp have no
* CloudFormation stack, so `stack` below is absent and there is nothing to
* colour from) needs the shape underneath: how many of the component's
* resources were seen, how many were confirmed gone, how many nobody could
* look at. Substrate-neutral by construction — it counts observations, not
* provider objects.
*/
export interface ComponentResourceRollup {
/** Resources this component owns, per the live-name mapping. */
total: number;
/** Observed present. */
present: number;
/** Looked for, reported missing. Never includes a resource nobody could read. */
absent: number;
/** NOT-OBSERVED (#1089) — a hole, never counted as absence. */
unobserved: number;
}

/** Count a component's entity verdicts without collapsing them (behold#98). */
function rollUp(entries: LiveComponentEvidence[]): ComponentResourceRollup {
let present = 0;
let absent = 0;
let unobserved = 0;
for (const e of entries) {
if (e.unobserved) unobserved += 1;
else if (e.live) present += 1;
else absent += 1;
}
return { total: entries.length, present, absent, unobserved };
}

/**
* Merge live evidence for several entity/resource names owned by one
* component into a single verdict. `live` and `ownership` favor the
* most-actionable signal (present/owned) over "nothing here"; `action`
* favors `update` so that drift on *any* owned entity surfaces as drift for
* the component as a whole, matching `reconcileStatus`'s single check for
* `action === "update"`.
*
* The per-entity counts survive as {@link LiveComponentEvidence.rollup}, so a
* consumer that needs the shape rather than the verdict is not forced to redo
* this join on the far side of a CLI boundary.
*/
function mergeEvidence(entries: LiveComponentEvidence[]): LiveComponentEvidence | undefined {
if (entries.length === 0) return undefined;
if (entries.length === 1) return entries[0];
if (entries.length === 1) return { ...entries[0], rollup: rollUp(entries) };

const live = entries.some((e) => e.live);
const ownership = entries.some((e) => e.ownership === "owned")
Expand All @@ -206,7 +267,7 @@ function mergeEvidence(entries: LiveComponentEvidence[]): LiveComponentEvidence
// actually seen live, which already answers "is this deployed".
const unobserved = live ? undefined : entries.find((e) => e.unobserved)?.unobserved;

return { live, ownership, action, ...(unobserved ? { unobserved } : {}) };
return { live, ownership, action, ...(unobserved ? { unobserved } : {}), rollup: rollUp(entries) };
}

/**
Expand All @@ -228,6 +289,14 @@ export function liveEvidenceFromChangeSet(
const evidenceByName = new Map<string, LiveComponentEvidence>();
for (const entry of cs.entries) {
evidenceByName.set(entry.name, {
rollup: rollUp([
{
live: entry.evidence.live,
...(entry.action === "unobserved" && entry.unobservedReason
? { unobserved: { reason: entry.unobservedReason } }
: {}),
},
]),
live: entry.evidence.live,
action: entry.action,
ownership: entry.ownership,
Expand Down Expand Up @@ -352,6 +421,7 @@ export function reconcileStatus(
...(liveEvidence && !evidence?.unobserved ? { live: !!evidence?.live } : {}),
...(evidence?.unobserved ? { unobserved: evidence.unobserved } : {}),
...(evidence?.stack ? { stack: evidence.stack } : {}),
...(evidence?.rollup ? { resources: evidence.rollup } : {}),
});
}

Expand Down