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
36 changes: 22 additions & 14 deletions pm-stack/aggregator/README.md

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions pm-stack/aggregator/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ export interface Config {
projector: {
baseUrl: string | null;
};
/** Probe targets for the /health page (#1147): the stack services the
* aggregator does not otherwise read. Kaneo/Grist/projector targets
* already live in their own sections above. */
pad: {
baseUrl: string;
};
readyForAgent: {
baseUrl: string;
};
}

export function loadConfig(env: Record<string, string | undefined> = process.env): Config {
Expand Down Expand Up @@ -92,5 +101,11 @@ export function loadConfig(env: Record<string, string | undefined> = process.env
? null
: s(env.PM_PROJECTOR_URL, "http://127.0.0.1:7801"),
},
pad: {
baseUrl: s(env.PM_AGG_PAD_URL, "http://127.0.0.1:7777"),
},
readyForAgent: {
baseUrl: s(env.PM_AGG_RFA_URL, "http://127.0.0.1:6056"),
},
};
}
341 changes: 341 additions & 0 deletions pm-stack/aggregator/src/health.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,341 @@
// Health assembly for /health + /api/health (#1147): per-service
// probe state, per-section feed ages, job runs, and this process's
// own page-render history — assembled from the same readers the rest
// of the dashboard trusts, so the page and the API can never disagree.
//
// Honesty rules (the #1110/#1139 lessons, applied to health itself):
// - Probes are real HTTP reads of the loopback targets in
// src/topology.ts (from config where config has them), cached 60 s
// so a human refreshing — or a watchdog polling — never fans out to
// five services per hit. A failed probe renders as a failed probe,
// with its error and its age; nothing here invents a state.
// - The projector (off by config) is never probed and never red.
// - Page health is an in-process render registry (server.ts records
// each successful page render), never self-HTTP: a route nobody has
// opened is labelled "not rendered since boot", not fake-green.
// - In fixtures mode there are no live probes to make: every service
// reports "fixtures", which is also the degraded-state test fixture.
import type { Config } from "./config.ts";
import { ageMsText, type FleetFeed, type SectionDiagnosis } from "./feed.ts";
import type { ProvenanceDetail } from "./model.ts";
import { PAGES } from "./pages.ts";
import { buildServices, type ServiceDef } from "./topology.ts";

/** Same staleness threshold live.ts applies to feed-backed facets. */
const FEED_STALE_MS = 15 * 60_000;
/** Orchestration cadence: hourly; next run is an expectation, not a promise. */
const ORCHESTRATION_CADENCE_MS = 3_600_000;

export const PROBE_CACHE_MS = 60_000;
export const PROBE_TIMEOUT_MS = 1_500;

/** The minimal fetch shape probes need — injectable for tests. */
export type ProbeFetch = (
url: string,
init: { signal: AbortSignal; redirect: "manual" },
) => Promise<{ status: number }>;

const defaultProbeFetch: ProbeFetch = async (url, init) => {
const res = await fetch(url, init);
return { status: res.status };
};

export interface ProbeOutcome {
state: ServiceState;
ok: boolean | null;
detail: string;
statusCode?: number;
/** When the probe actually ran (a cached outcome keeps its own time). */
probedAtMs?: number;
}

export type ServiceState = "up" | "down" | "stale" | "absent" | "invalid" | "unknown" | "off" | "self" | "fixtures";

export interface ProbeCache {
entries: Map<string, { at: number; outcome: ProbeOutcome }>;
}

export function createProbeCache(): ProbeCache {
return { entries: new Map() };
}

const sharedProbeCache = createProbeCache();

async function probeHttp(def: ServiceDef, fetcher: ProbeFetch, cache: ProbeCache, now: number): Promise<ProbeOutcome> {
const hit = cache.entries.get(def.id);
if (hit && now - hit.at < PROBE_CACHE_MS) return hit.outcome;
const url = `${def.loopbackUrl}${def.healthPath ?? "/"}`;
let outcome: ProbeOutcome;
try {
const res = await fetcher(url, { signal: AbortSignal.timeout(PROBE_TIMEOUT_MS), redirect: "manual" });
const expected = def.expectStatuses ?? [200];
outcome = expected.includes(res.status)
? { state: "up", ok: true, detail: `HTTP ${res.status}`, statusCode: res.status, probedAtMs: now }
: {
state: "down",
ok: false,
detail: `HTTP ${res.status} — expected ${expected.join(" or ")}`,
statusCode: res.status,
probedAtMs: now,
};
} catch (err) {
const name = err instanceof Error ? err.name : "";
const detail =
name === "TimeoutError" || name === "AbortError"
? `probe timed out after ${PROBE_TIMEOUT_MS / 1000}s`
: `probe failed: ${err instanceof Error ? err.message : String(err)}`;
outcome = { state: "down", ok: false, detail, probedAtMs: now };
}
cache.entries.set(def.id, { at: now, outcome });
return outcome;
}

export interface ServiceHealth {
def: ServiceDef;
state: ServiceState;
ok: boolean | null;
detail: string;
statusCode?: number;
probedAt?: string;
}

export interface FeedSectionHealth {
key: string;
present: boolean;
/** The timestamp the age is measured from (the section's own when it has one). */
generatedAt?: string;
ageMs?: number;
note?: string;
}

export interface PageHealth {
route: string;
title: string;
lastRenderedAt: string | null;
ok: boolean | null;
ageMs: number | null;
}

export interface HealthView {
/** The aggregator process itself is up if it assembled this view. */
status: "ok";
mode: "live" | "fixtures";
generatedAt: string;
bootAt: string;
degraded: boolean;
services: ServiceHealth[];
feed: {
generatedAt: string | null;
fileMtime: string | null;
ageMs: number | null;
sections: FeedSectionHealth[];
};
jobs: {
orchestration: {
state: SectionDiagnosis["state"] | "unknown";
runId?: string;
generatedAt?: string;
ageMs?: number;
nextExpectedAt?: string;
failedChecks?: string[];
};
collector: { generatedAt: string | null; fileMtime: string | null; ageMs: number | null };
};
pages: PageHealth[];
facets: ProvenanceDetail | null;
}

// --- Render registry -------------------------------------------------------
// server.ts records every successful page render here. Module scope
// ≈ process scope: BOOT_AT is when this process started serving.

const BOOT_AT = Date.now();
const renderLog = new Map<string, { at: number; ok: boolean }>();

export function recordRender(route: string, ok: boolean, at = Date.now()): void {
renderLog.set(route, { at, ok });
}

export function renderRegistrySize(): number {
return renderLog.size;
}

function pageHealth(now: number): PageHealth[] {
return PAGES.map((p) => {
const mark = renderLog.get(p.route);
if (!mark) return { route: p.route, title: p.title, lastRenderedAt: null, ok: null, ageMs: null };
return {
route: p.route,
title: p.title,
lastRenderedAt: new Date(mark.at).toISOString(),
ok: mark.ok,
ageMs: Math.max(0, now - mark.at),
};
});
}

// --- Feed sections ---------------------------------------------------------

function feedSections(feed: FleetFeed | null, now: number): FeedSectionHealth[] {
if (!feed) return [];
const from = (generatedAt: string | undefined, present: boolean, note?: string): Partial<FeedSectionHealth> => {
const ts = generatedAt ?? feed.generatedAt;
return { present, generatedAt: ts, ageMs: Math.max(0, now - Date.parse(ts)), ...(note ? { note } : {}) };
};
const aa = feed.aa;
const aaNote =
aa && "status" in aa && aa.status === "absent"
? `absent form: ${"reason" in aa ? String(aa.reason) : "no reason given"}`
: undefined;
return [
{ key: "quota", ...from(undefined, true) },
{ key: "attention", ...from(undefined, true) },
{ key: "orchestration", ...from(feed.orchestration?.generatedAt, feed.orchestration !== undefined) },
{ key: "dns", ...from(feed.dns?.generatedAt, feed.dns !== undefined) },
{ key: "registry", ...from(undefined, feed.registry !== undefined) },
{ key: "projects", ...from(undefined, feed.projects !== undefined) },
{ key: "saas", ...from(undefined, feed.saas !== undefined) },
{ key: "payg", ...from(undefined, feed.payg !== undefined) },
{ key: "charges", ...from(undefined, feed.charges !== undefined) },
{ key: "subscriptions", ...from(undefined, feed.subscriptions !== undefined) },
{ key: "snapshots", ...from(undefined, feed.snapshots !== undefined) },
{ key: "aa", ...from(aa?.generatedAt, aa !== undefined, aaNote) },
] as FeedSectionHealth[];
}

// --- Assembly ---------------------------------------------------------------

export interface HealthInputs {
mode: "live" | "fixtures";
cfg: Config;
feed: FleetFeed | null;
feedFileMtimeMs: number | null;
diagnosis?: SectionDiagnosis;
detail?: ProvenanceDetail;
}

export interface HealthDeps {
fetcher?: ProbeFetch;
cache?: ProbeCache;
now?: number;
}

export async function buildHealth(inputs: HealthInputs, deps: HealthDeps = {}): Promise<HealthView> {
const now = deps.now ?? Date.now();
const fetcher = deps.fetcher ?? defaultProbeFetch;
const cache = deps.cache ?? sharedProbeCache;
const { cfg, feed } = inputs;
const feedAgeMs = feed ? Math.max(0, now - Date.parse(feed.generatedAt)) : null;
const diagnosis = inputs.diagnosis;

const services: ServiceHealth[] = [];
const outcomes = new Map<string, ProbeOutcome>();
for (const def of buildServices(cfg)) {
let outcome: ProbeOutcome;
if (inputs.mode === "fixtures") {
outcome = { state: "fixtures", ok: null, detail: "fixtures mode — no live probes are made" };
} else if (def.probe === "off") {
outcome = { state: "off", ok: null, detail: "off by config — never probed, not an error" };
} else if (def.probe === "self") {
outcome = { state: "self", ok: true, detail: "this process — it assembled this view" };
} else if (def.probe === "http") {
outcome = await probeHttp(def, fetcher, cache, now);
} else if (def.probe === "feed") {
outcome =
feed === null
? { state: "down", ok: false, detail: "no readable fleet feed" }
: feedAgeMs !== null && feedAgeMs > FEED_STALE_MS
? { state: "stale", ok: false, detail: `feed ${ageMsText(feedAgeMs)} — past the 15m freshness threshold` }
: { state: "up", ok: true, detail: `feed ${feedAgeMs !== null ? ageMsText(feedAgeMs) : ""}`.trim() };
} else if (def.probe === "section") {
if (!diagnosis) {
outcome = { state: "unknown", ok: null, detail: "this source reports no section diagnosis" };
} else if (diagnosis.state === "live") {
outcome = {
state: "up",
ok: true,
detail:
`section ${diagnosis.runId ?? ""} · ${diagnosis.ageMs !== undefined ? ageMsText(diagnosis.ageMs) : ""}`.trim(),
};
} else if (diagnosis.state === "stale") {
outcome = {
state: "stale",
ok: false,
detail: `section ${diagnosis.runId ?? ""} is ${diagnosis.ageMs !== undefined ? ageMsText(diagnosis.ageMs) : "old"} — past the 90m stale threshold`,
};
} else if (diagnosis.state === "invalid") {
outcome = {
state: "invalid",
ok: false,
detail: `section present but rejected — failed checks: ${(diagnosis.failedChecks ?? []).join(", ") || "unknown"}`,
};
} else {
outcome = {
state: "absent",
ok: false,
detail: "no orchestration section in the feed the aggregator received",
};
}
} else {
// probe === "inferred": follows the named service's outcome.
const parent = def.inferredFrom ? outcomes.get(def.inferredFrom) : undefined;
outcome =
parent === undefined
? { state: "unknown", ok: null, detail: "the service this is inferred from has no probe outcome" }
: parent.state === "up"
? { state: "up", ok: true, detail: `inferred from ${def.inferredFrom} — container-internal, no host probe` }
: {
state: "unknown",
ok: null,
detail: `cannot infer — ${def.inferredFrom} is ${parent.state}, which proves nothing about this service`,
};
}
outcomes.set(def.id, outcome);
services.push({
def,
state: outcome.state,
ok: outcome.ok,
detail: outcome.detail,
...(outcome.statusCode !== undefined ? { statusCode: outcome.statusCode } : {}),
...(outcome.probedAtMs !== undefined ? { probedAt: new Date(outcome.probedAtMs).toISOString() } : {}),
});
}

const degraded =
inputs.mode === "live" &&
(services.some((s) => s.ok === false) || (feedAgeMs !== null && feedAgeMs > FEED_STALE_MS));

return {
status: "ok",
mode: inputs.mode,
generatedAt: new Date(now).toISOString(),
bootAt: new Date(BOOT_AT).toISOString(),
degraded,
services,
feed: {
generatedAt: feed?.generatedAt ?? null,
fileMtime: inputs.feedFileMtimeMs !== null ? new Date(inputs.feedFileMtimeMs).toISOString() : null,
ageMs: feedAgeMs,
sections: feedSections(feed, now),
},
jobs: {
orchestration: {
state: diagnosis?.state ?? "unknown",
...(diagnosis?.runId ? { runId: diagnosis.runId } : {}),
...(diagnosis?.generatedAt ? { generatedAt: diagnosis.generatedAt } : {}),
...(diagnosis?.ageMs !== undefined ? { ageMs: diagnosis.ageMs } : {}),
...(diagnosis?.generatedAt
? { nextExpectedAt: new Date(Date.parse(diagnosis.generatedAt) + ORCHESTRATION_CADENCE_MS).toISOString() }
: {}),
...(diagnosis?.failedChecks ? { failedChecks: diagnosis.failedChecks } : {}),
},
collector: {
generatedAt: feed?.generatedAt ?? null,
fileMtime: inputs.feedFileMtimeMs !== null ? new Date(inputs.feedFileMtimeMs).toISOString() : null,
ageMs: feedAgeMs,
},
},
pages: pageHealth(now),
facets: inputs.detail ?? null,
};
}
8 changes: 8 additions & 0 deletions pm-stack/aggregator/src/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ export const PAGES: PageDef[] = [
blurb:
"The projects hub: every project's status, roadmap, and tracked work, one page per project under /program/<id> — fed from the project registry, the tracked issues, and the Project pool.",
},
{
id: "health",
title: "Health",
route: "/health",
api: "/api/health",
blurb:
"Service & page health (#1147): live probes for every stack service, feed section ages, last job runs, this dashboard's render history — and the services & ports topology (#1137).",
},
{
id: "pages",
title: "All pages",
Expand Down
Loading
Loading