diff --git a/lexicons/github/surface.snapshot.json b/lexicons/github/surface.snapshot.json new file mode 100644 index 0000000000..3dc9f06d61 --- /dev/null +++ b/lexicons/github/surface.snapshot.json @@ -0,0 +1,134 @@ +{ + "schemaVersion": 1, + "generatedAt": "2026-07-01T23:01:14.865Z", + "entries": { + "Concurrency": { + "kind": "property", + "resourceType": "GitHub::Actions::Concurrency" + }, + "Container": { + "kind": "property", + "resourceType": "GitHub::Actions::Container" + }, + "Defaults": { + "kind": "property", + "resourceType": "GitHub::Actions::Defaults" + }, + "Environment": { + "kind": "property", + "resourceType": "GitHub::Actions::Environment" + }, + "Job": { + "kind": "resource", + "resourceType": "GitHub::Actions::Job", + "props": [ + "concurrency:false", + "container:false", + "continue-on-error:false", + "defaults:false", + "env:false", + "environment:false", + "if:false", + "name:false", + "needs:false", + "outputs:false", + "permissions:false", + "runs-on:true", + "services:false", + "snapshot:false", + "steps:false", + "strategy:false", + "timeout-minutes:false" + ] + }, + "Permissions": { + "kind": "property", + "resourceType": "GitHub::Actions::Permissions" + }, + "PullRequestTargetTrigger": { + "kind": "property", + "resourceType": "GitHub::Actions::PullRequestTargetTrigger" + }, + "PullRequestTrigger": { + "kind": "property", + "resourceType": "GitHub::Actions::PullRequestTrigger" + }, + "PushTrigger": { + "kind": "property", + "resourceType": "GitHub::Actions::PushTrigger" + }, + "RepositoryDispatchTrigger": { + "kind": "property", + "resourceType": "GitHub::Actions::RepositoryDispatchTrigger" + }, + "ReusableWorkflowCallJob": { + "kind": "resource", + "resourceType": "GitHub::Actions::ReusableWorkflowCallJob", + "props": [ + "concurrency:false", + "if:false", + "name:false", + "needs:false", + "permissions:false", + "secrets:false", + "strategy:false", + "uses:true", + "with:false" + ] + }, + "ScheduleTrigger": { + "kind": "property", + "resourceType": "GitHub::Actions::ScheduleTrigger" + }, + "Service": { + "kind": "property", + "resourceType": "GitHub::Actions::Service" + }, + "Step": { + "kind": "property", + "resourceType": "GitHub::Actions::Step" + }, + "Strategy": { + "kind": "property", + "resourceType": "GitHub::Actions::Strategy" + }, + "Workflow": { + "kind": "resource", + "resourceType": "GitHub::Actions::Workflow", + "props": [ + "concurrency:false", + "defaults:false", + "env:false", + "jobs:true", + "name:false", + "on:true", + "permissions:false", + "run-name:false" + ] + }, + "WorkflowCallTrigger": { + "kind": "property", + "resourceType": "GitHub::Actions::WorkflowCallTrigger" + }, + "WorkflowDispatchTrigger": { + "kind": "property", + "resourceType": "GitHub::Actions::WorkflowDispatchTrigger" + }, + "WorkflowInput": { + "kind": "property", + "resourceType": "GitHub::Actions::WorkflowInput" + }, + "WorkflowOutput": { + "kind": "property", + "resourceType": "GitHub::Actions::WorkflowOutput" + }, + "WorkflowRunTrigger": { + "kind": "property", + "resourceType": "GitHub::Actions::WorkflowRunTrigger" + }, + "WorkflowSecret": { + "kind": "property", + "resourceType": "GitHub::Actions::WorkflowSecret" + } + } +} diff --git a/packages/core/src/cli/commands/lexicon-rolling-upgrade.ts b/packages/core/src/cli/commands/lexicon-rolling-upgrade.ts new file mode 100644 index 0000000000..c3e1e4b3bc --- /dev/null +++ b/packages/core/src/cli/commands/lexicon-rolling-upgrade.ts @@ -0,0 +1,198 @@ +/** + * `chant dev rolling-upgrade ` command implementation. + * + * Detects rolling-spec drift for the aws, azure, and github lexicons. These + * lexicons pin no version constant — "latest" is fetched on every regen. This + * command regenerates from current-latest, diffs the produced API surface + * against the committed baseline (surface.snapshot.json), and REPORTS the + * result. It is a dry run: it never opens a PR, updates a branch, or writes the + * baseline. PR automation and scheduling live in #527. + * + * The PR label an eventual automation would apply is derived from severity: + * additive → `minor`, breaking → `major`. This command only prints it. + */ + +import { existsSync } from "fs"; +import { resolve } from "path"; +import { + checkRollingUpgrade, + type RollingUpgradeResult, +} from "../../codegen/rolling-upgrade"; + +// ── Types ───────────────────────────────────────────────────────────── + +export interface RollingUpgradeCliOptions { + /** Resolved path to the lexicon directory. */ + lexiconDir: string; + /** Force re-fetch of the upstream spec (bypass cache). */ + force?: boolean; + /** Print subprocess output while running. */ + verbose?: boolean; +} + +// ── Main entry ──────────────────────────────────────────────────────── + +/** + * Run the rolling-upgrade check and return the result. + * Returns a synthetic failed result when the directory is missing. + */ +export async function runRollingUpgrade( + opts: RollingUpgradeCliOptions, +): Promise { + const dir = resolve(opts.lexiconDir); + + if (!existsSync(dir)) { + return { + lexicon: "aws", + hasUpgrade: false, + severity: "none", + delta: { added: [], changed: [], removed: [], severity: "none" }, + deltaText: "", + validationOk: false, + failures: [{ step: "setup", output: `Lexicon directory not found: ${dir}` }], + apiVersionDelta: [], + freshSnapshot: null, + }; + } + + return checkRollingUpgrade({ + lexiconDir: dir, + force: opts.force, + verbose: opts.verbose, + }); +} + +/** + * Map surface severity to the semver PR label an upgrade would carry. + * additive → minor, breaking → major, none → none. + */ +export function severityToLabel(severity: RollingUpgradeResult["severity"]): string { + if (severity === "breaking") return "major"; + if (severity === "additive") return "minor"; + return "none"; +} + +// ── Human-readable output ───────────────────────────────────────────── + +const COLORS = { + green: "\x1b[32m", + yellow: "\x1b[33m", + red: "\x1b[31m", + gray: "\x1b[90m", + bold: "\x1b[1m", + reset: "\x1b[0m", +}; + +function useColors(): boolean { + return !process.env.NO_COLOR && process.stdout.isTTY !== false; +} + +function c(text: string, code: string): string { + return useColors() ? `${code}${text}${COLORS.reset}` : text; +} + +/** + * Print the rolling-upgrade result to stdout. + */ +export function printRollingUpgradeResult(result: RollingUpgradeResult, json: boolean): void { + if (json) { + console.log( + JSON.stringify( + { + lexicon: result.lexicon, + hasUpgrade: result.hasUpgrade, + severity: result.severity, + label: severityToLabel(result.severity), + validationOk: result.validationOk, + delta: result.delta, + apiVersionDelta: result.apiVersionDelta, + failures: result.failures, + }, + null, + 2, + ), + ); + return; + } + + console.log(c(`Rolling upgrade check: ${result.lexicon}`, COLORS.bold)); + console.log(""); + + // Surface delta — the review payload + if (result.deltaText) { + console.log(result.deltaText); + } else { + console.log(c("No surface changes.", COLORS.green)); + } + + // azure API-version delta + if (result.apiVersionDelta.length > 0) { + console.log(""); + console.log(c(`API version changes (${result.apiVersionDelta.length}):`, COLORS.bold)); + for (const v of result.apiVersionDelta) { + const arrow = + v.kind === "added" + ? `+ ${v.provider} (${v.after})` + : v.kind === "removed" + ? `- ${v.provider} (was ${v.before})` + : `~ ${v.provider}: ${v.before} -> ${v.after}`; + console.log(` ${arrow}`); + } + } + + // Failures + if (result.failures.length > 0) { + console.log(""); + console.log(c(`${result.failures.length} step(s) failed:`, COLORS.bold)); + for (const f of result.failures) { + console.log( + c( + ` FAIL [${f.step}]${f.exitCode !== undefined ? ` (exit ${f.exitCode})` : ""}`, + COLORS.red, + ), + ); + if (f.output) { + const indented = f.output + .split("\n") + .map((l) => ` ${l}`) + .join("\n"); + console.log(c(indented, COLORS.gray)); + } + } + } + + console.log(""); + + // Decision line + const severityColor = + result.severity === "breaking" + ? COLORS.red + : result.severity === "additive" + ? COLORS.yellow + : COLORS.green; + + const label = severityToLabel(result.severity); + const validationLabel = result.validationOk + ? c("ok", COLORS.green) + : c("FAILED", COLORS.red); + + console.log( + `Upgrade: ${result.hasUpgrade} Severity: ${c(result.severity, severityColor)} ` + + `Label: ${label} Validation: ${validationLabel}`, + ); + + if (result.hasUpgrade && result.validationOk) { + console.log( + c( + `A PR would be opened: chore(${result.lexicon}): regen from latest spec [${label}]`, + COLORS.gray, + ), + ); + } else if (result.hasUpgrade && !result.validationOk) { + console.log( + c("Surface moved but validation failed — no PR; surfaces as a report/issue (#527).", COLORS.yellow), + ); + } else { + console.log(c("No surface change — no PR.", COLORS.gray)); + } +} diff --git a/packages/core/src/cli/handlers/dev.ts b/packages/core/src/cli/handlers/dev.ts index 5a2e16bbc3..1309aa6386 100644 --- a/packages/core/src/cli/handlers/dev.ts +++ b/packages/core/src/cli/handlers/dev.ts @@ -76,10 +76,22 @@ export async function runDevPinnedUpgrade(ctx: CommandContext): Promise return 0; } +export async function runDevRollingUpgrade(ctx: CommandContext): Promise { + const dir = ctx.args.extraPositional ?? "."; + const { runRollingUpgrade, printRollingUpgradeResult } = await import("../commands/lexicon-rolling-upgrade"); + const result = await runRollingUpgrade({ + lexiconDir: resolve(dir), + force: ctx.args.force, + verbose: ctx.args.verbose, + }); + printRollingUpgradeResult(result, ctx.args.format === "json"); + return result.validationOk ? 0 : 1; +} + export async function runDevUnknown(ctx: CommandContext): Promise { console.error(formatError({ message: `Unknown dev subcommand: ${ctx.args.path}`, - hint: "Available: chant dev generate, chant dev publish, chant dev onboard, chant dev check-lexicon, chant dev surface-diff, chant dev pinned-upgrade", + hint: "Available: chant dev generate, chant dev publish, chant dev onboard, chant dev check-lexicon, chant dev surface-diff, chant dev pinned-upgrade, chant dev rolling-upgrade", })); return 1; } diff --git a/packages/core/src/cli/main.ts b/packages/core/src/cli/main.ts index 9cc57cefd2..c717545353 100755 --- a/packages/core/src/cli/main.ts +++ b/packages/core/src/cli/main.ts @@ -9,7 +9,7 @@ import { ENV_VAR, unknownEnvError } from "../env"; import { initRuntime } from "../runtime-adapter"; import { runBuild } from "./handlers/build"; import { runLint } from "./handlers/lint"; -import { runDevGenerate, runDevPublish, runDevOnboard, runDevCheckLexicon, runDevSurfaceDiff, runDevPinnedUpgrade, runDevUnknown } from "./handlers/dev"; +import { runDevGenerate, runDevPublish, runDevOnboard, runDevCheckLexicon, runDevSurfaceDiff, runDevPinnedUpgrade, runDevRollingUpgrade, runDevUnknown } from "./handlers/dev"; import { runServeLsp, runServeMcp, runServeUnknown } from "./handlers/serve"; import { runInit, runInitLexicon } from "./handlers/init"; import { runList, runDescribe, runImport, runAudit, runUpdate, runDoctor } from "./handlers/misc"; @@ -240,6 +240,9 @@ Lexicon development: dev pinned-upgrade Report if a pinned lexicon (k8s|gcp|docker|gitlab) has a newer upstream release; dry-run bump + regen + surface-diff, then revert (reports only; --force bypasses the spec cache, -f json for JSON) + dev rolling-upgrade Report rolling-spec drift (aws, azure, github): regen from + latest, diff surface vs committed baseline, print delta + PR + label (dry run; --force bypasses the spec cache, --format json) Servers: serve lsp Start the LSP server (stdio) @@ -347,6 +350,7 @@ const registry: CommandDef[] = [ { name: "dev check-lexicon", handler: runDevCheckLexicon }, { name: "dev surface-diff", handler: runDevSurfaceDiff }, { name: "dev pinned-upgrade", handler: runDevPinnedUpgrade }, + { name: "dev rolling-upgrade", handler: runDevRollingUpgrade }, // Op / run subcommands { name: "run list", handler: runOpList }, diff --git a/packages/core/src/codegen/rolling-upgrade.test.ts b/packages/core/src/codegen/rolling-upgrade.test.ts new file mode 100644 index 0000000000..74cbfd4ee8 --- /dev/null +++ b/packages/core/src/codegen/rolling-upgrade.test.ts @@ -0,0 +1,333 @@ +import { describe, test, expect } from "vitest"; +import { mkdtempSync, rmSync, writeFileSync, mkdirSync } from "fs"; +import { join } from "path"; +import { tmpdir } from "os"; +import { + checkRollingUpgrade, + classifyDelta, + diffRollingSurface, + computeAzureApiVersionDelta, + extractAzureProvider, + diffAzureApiVersions, +} from "./rolling-upgrade"; +import type { SurfaceSnapshot } from "./surface-snapshot"; +import type { RegenResult, RegenOptions } from "./lexicon-regen"; +import { diffSurface } from "./surface-snapshot"; + +// ── Fixture snapshots ───────────────────────────────────────────────── + +function baselineAws(): SurfaceSnapshot { + return { + schemaVersion: 1, + generatedAt: "2026-01-01T00:00:00.000Z", + entries: { + Bucket: { + kind: "resource", + resourceType: "AWS::S3::Bucket", + attrs: ["Arn"], + props: ["BucketName:false", "Tags:false"], + taggable: true, + }, + Bucket_Tag: { kind: "property", resourceType: "AWS::S3::Bucket.Tag" }, + }, + }; +} + +/** Adds a new resource → additive upgrade. */ +function freshAwsAdditive(): SurfaceSnapshot { + const s = baselineAws(); + s.entries.Table = { + kind: "resource", + resourceType: "AWS::DynamoDB::Table", + attrs: ["Arn"], + props: ["TableName:false"], + taggable: true, + }; + return s; +} + +/** Removes a resource → breaking upgrade. */ +function freshAwsBreaking(): SurfaceSnapshot { + const s = baselineAws(); + delete s.entries.Bucket_Tag; + return s; +} + +function azureBaseline(): SurfaceSnapshot { + return { + schemaVersion: 1, + generatedAt: "2026-01-01T00:00:00.000Z", + entries: { + StorageAccount: { + kind: "resource", + resourceType: "Microsoft.Storage/storageAccounts", + props: ["name:true"], + }, + VirtualMachine: { + kind: "resource", + resourceType: "Microsoft.Compute/virtualMachines", + props: ["name:true"], + }, + }, + }; +} + +/** Adds a resource from a brand-new provider. */ +function azureFreshNewProvider(): SurfaceSnapshot { + const s = azureBaseline(); + s.entries.Vault = { + kind: "resource", + resourceType: "Microsoft.KeyVault/vaults", + props: ["name:true"], + }; + return s; +} + +// ── Mock regen ──────────────────────────────────────────────────────── + +function makeRegen( + fresh: SurfaceSnapshot | null, + baseline: SurfaceSnapshot, + ok = true, +): (opts: RegenOptions) => Promise { + return async () => { + const delta = fresh + ? diffSurface(baseline, fresh) + : { added: [], changed: [], removed: [], severity: "none" as const }; + const changed = + delta.added.length > 0 || delta.changed.length > 0 || delta.removed.length > 0; + return { + ok, + changed, + severity: delta.severity, + delta, + deltaText: changed ? "delta" : "", + failures: ok ? [] : [{ step: "generate", output: "boom" }], + freshSnapshot: fresh, + }; + }; +} + +// ── Test dir helper ─────────────────────────────────────────────────── + +/** Create a fake lexicon dir with a package.json and committed baseline snapshot. */ +function makeLexiconDir(name: string, baseline: SurfaceSnapshot): string { + const dir = mkdtempSync(join(tmpdir(), "chant-rolling-")); + writeFileSync( + join(dir, "package.json"), + JSON.stringify({ name: `@intentius/chant-lexicon-${name}` }), + ); + writeFileSync(join(dir, "surface.snapshot.json"), JSON.stringify(baseline, null, 2)); + mkdirSync(join(dir, "src", "generated"), { recursive: true }); + return dir; +} + +// ── classifyDelta (pure) ────────────────────────────────────────────── + +describe("classifyDelta", () => { + test("unchanged surface → no upgrade, none severity", () => { + const delta = diffRollingSurface(baselineAws(), baselineAws()); + expect(classifyDelta(delta)).toEqual({ hasUpgrade: false, severity: "none" }); + }); + + test("added resource → upgrade, additive severity", () => { + const delta = diffRollingSurface(baselineAws(), freshAwsAdditive()); + expect(classifyDelta(delta)).toEqual({ hasUpgrade: true, severity: "additive" }); + }); + + test("removed resource → upgrade, breaking severity", () => { + const delta = diffRollingSurface(baselineAws(), freshAwsBreaking()); + const c = classifyDelta(delta); + expect(c.hasUpgrade).toBe(true); + expect(c.severity).toBe("breaking"); + }); +}); + +// ── checkRollingUpgrade (mocked regen) ──────────────────────────────── + +describe("checkRollingUpgrade — surface changed vs unchanged", () => { + test("unchanged surface → hasUpgrade false", async () => { + const dir = makeLexiconDir("aws", baselineAws()); + try { + const res = await checkRollingUpgrade({ + lexiconDir: dir, + _regenFn: makeRegen(baselineAws(), baselineAws()), + }); + expect(res.lexicon).toBe("aws"); + expect(res.hasUpgrade).toBe(false); + expect(res.severity).toBe("none"); + expect(res.validationOk).toBe(true); + expect(res.deltaText).toBe(""); + } finally { + rmSync(dir, { recursive: true, force: true }); + } + }); + + test("additive surface change → hasUpgrade true, additive", async () => { + const dir = makeLexiconDir("aws", baselineAws()); + try { + const res = await checkRollingUpgrade({ + lexiconDir: dir, + _regenFn: makeRegen(freshAwsAdditive(), baselineAws()), + }); + expect(res.hasUpgrade).toBe(true); + expect(res.severity).toBe("additive"); + expect(res.delta.added.map((a) => a.name)).toContain("Table"); + expect(res.deltaText).not.toBe(""); + } finally { + rmSync(dir, { recursive: true, force: true }); + } + }); + + test("breaking surface change → hasUpgrade true, breaking", async () => { + const dir = makeLexiconDir("aws", baselineAws()); + try { + const res = await checkRollingUpgrade({ + lexiconDir: dir, + _regenFn: makeRegen(freshAwsBreaking(), baselineAws()), + }); + expect(res.hasUpgrade).toBe(true); + expect(res.severity).toBe("breaking"); + expect(res.delta.removed.map((r) => r.name)).toContain("Bucket_Tag"); + } finally { + rmSync(dir, { recursive: true, force: true }); + } + }); + + test("failed regen surfaces validationOk=false and failures", async () => { + const dir = makeLexiconDir("aws", baselineAws()); + try { + const res = await checkRollingUpgrade({ + lexiconDir: dir, + _regenFn: makeRegen(freshAwsAdditive(), baselineAws(), false), + }); + expect(res.validationOk).toBe(false); + expect(res.failures.length).toBeGreaterThan(0); + // Still reports the delta so the failure carries the diff + expect(res.hasUpgrade).toBe(true); + } finally { + rmSync(dir, { recursive: true, force: true }); + } + }); +}); + +// ── azure lexicon detection + apiVersionDelta ───────────────────────── + +describe("checkRollingUpgrade — azure", () => { + test("detects azure lexicon by package name", async () => { + const dir = makeLexiconDir("azure", azureBaseline()); + try { + const res = await checkRollingUpgrade({ + lexiconDir: dir, + _regenFn: makeRegen(azureBaseline(), azureBaseline()), + }); + expect(res.lexicon).toBe("azure"); + } finally { + rmSync(dir, { recursive: true, force: true }); + } + }); + + test("new provider shows up in apiVersionDelta", async () => { + const dir = makeLexiconDir("azure", azureBaseline()); + try { + const res = await checkRollingUpgrade({ + lexiconDir: dir, + _regenFn: makeRegen(azureFreshNewProvider(), azureBaseline()), + }); + expect(res.hasUpgrade).toBe(true); + const kv = res.apiVersionDelta.find((c) => c.provider === "Microsoft.KeyVault"); + expect(kv).toBeDefined(); + expect(kv!.kind).toBe("added"); + } finally { + rmSync(dir, { recursive: true, force: true }); + } + }); + + test("aws has empty apiVersionDelta", async () => { + const dir = makeLexiconDir("aws", baselineAws()); + try { + const res = await checkRollingUpgrade({ + lexiconDir: dir, + _regenFn: makeRegen(freshAwsAdditive(), baselineAws()), + }); + expect(res.apiVersionDelta).toEqual([]); + } finally { + rmSync(dir, { recursive: true, force: true }); + } + }); +}); + +// ── extractAzureProvider (pure) ─────────────────────────────────────── + +describe("extractAzureProvider", () => { + test("extracts provider from resourceType", () => { + expect(extractAzureProvider("Microsoft.Storage/storageAccounts")).toBe("Microsoft.Storage"); + expect(extractAzureProvider("Microsoft.Compute/virtualMachines/extensions")).toBe( + "Microsoft.Compute", + ); + }); + + test("returns null for non-ARM types", () => { + expect(extractAzureProvider("AWS::S3::Bucket")).toBeNull(); + expect(extractAzureProvider("")).toBeNull(); + expect(extractAzureProvider("Microsoft.Storage")).toBeNull(); + }); +}); + +// ── computeAzureApiVersionDelta (pure) ──────────────────────────────── + +describe("computeAzureApiVersionDelta", () => { + test("reports added and removed providers", () => { + const delta = computeAzureApiVersionDelta(azureBaseline(), azureFreshNewProvider()); + expect(delta).toHaveLength(1); + expect(delta[0]).toMatchObject({ provider: "Microsoft.KeyVault", kind: "added" }); + }); + + test("no change when providers identical", () => { + expect(computeAzureApiVersionDelta(azureBaseline(), azureBaseline())).toEqual([]); + }); + + test("reports removed provider", () => { + const shrunk: SurfaceSnapshot = { + ...azureBaseline(), + entries: { StorageAccount: azureBaseline().entries.StorageAccount }, + }; + const delta = computeAzureApiVersionDelta(azureBaseline(), shrunk); + expect(delta).toEqual([ + { provider: "Microsoft.Compute", before: "latest", after: null, kind: "removed" }, + ]); + }); +}); + +// ── diffAzureApiVersions (pure) ─────────────────────────────────────── + +describe("diffAzureApiVersions", () => { + test("detects version bump as updated", () => { + const base = new Map([["Microsoft.Compute", "2022-03-01"]]); + const fresh = new Map([["Microsoft.Compute", "2023-07-01"]]); + const delta = diffAzureApiVersions(base, fresh); + expect(delta).toEqual([ + { + provider: "Microsoft.Compute", + before: "2022-03-01", + after: "2023-07-01", + kind: "updated", + }, + ]); + }); + + test("detects added and removed providers", () => { + const base = new Map([["Microsoft.Old", "2020-01-01"]]); + const fresh = new Map([["Microsoft.New", "2024-01-01"]]); + const delta = diffAzureApiVersions(base, fresh); + expect(delta).toEqual([ + { provider: "Microsoft.New", before: null, after: "2024-01-01", kind: "added" }, + { provider: "Microsoft.Old", before: "2020-01-01", after: null, kind: "removed" }, + ]); + }); + + test("no change when versions identical", () => { + const m = new Map([["Microsoft.Storage", "2023-01-01"]]); + expect(diffAzureApiVersions(m, new Map(m))).toEqual([]); + }); +}); diff --git a/packages/core/src/codegen/rolling-upgrade.ts b/packages/core/src/codegen/rolling-upgrade.ts new file mode 100644 index 0000000000..5d62b04ef8 --- /dev/null +++ b/packages/core/src/codegen/rolling-upgrade.ts @@ -0,0 +1,320 @@ +/** + * Rolling-spec drift detection for aws, azure, and github lexicons. + * + * These lexicons have no pinned version constant — "latest" is fetched on every + * regen. An upgrade means regenerating from current-latest and diffing the + * produced API surface against the committed baseline (surface.snapshot.json). + * If the surface changed and validation passed, a PR can be opened with the + * delta as the review payload. + * + * This module contains only pure business logic (detecting drift, classifying + * it, formatting the report). It calls regenLexicon from #524 for the actual + * regen/validate work, and never runs a live regen itself. + * + * azure extra delta: + * The Azure regen depends on `latestVersionPerProvider`, which picks one API + * version per ARM provider. When new providers appear or existing ones get a + * newer GA/preview version, that shows up in the surface diff. In addition, + * this module computes an `apiVersionDelta` to surface those version changes + * separately, so reviewers can see "Microsoft.Compute moved 2022-03-01 → + * 2023-07-01" without reading the raw surface diff. + */ + +import { existsSync, readFileSync } from "fs"; +import { join } from "path"; +import { + diffSurface, + formatDelta, + parseSnapshot, + type SurfaceDelta, + type ChangeSeverity, + type SurfaceSnapshot, +} from "./surface-snapshot"; +import { regenLexicon, SNAPSHOT_FILENAME, type RegenOptions, type RegenResult } from "./lexicon-regen"; + +// ── Public types ────────────────────────────────────────────────────── + +export type RollingLexicon = "aws" | "azure" | "github"; + +/** + * An API-version change for one Azure ARM provider. + * Only populated for the azure lexicon. + */ +export interface AzureApiVersionChange { + provider: string; + before: string | null; + after: string | null; + /** "added" when provider is new; "updated" when version bumped; "removed" when gone. */ + kind: "added" | "updated" | "removed"; +} + +/** + * The result of a rolling-upgrade check for one lexicon. + */ +export interface RollingUpgradeResult { + /** Lexicon identifier. */ + lexicon: RollingLexicon; + /** True when the surface changed versus the committed baseline. */ + hasUpgrade: boolean; + /** Severity of the surface delta. "none" when the surface is unchanged. */ + severity: ChangeSeverity; + /** Structured surface delta. */ + delta: SurfaceDelta; + /** Human-readable delta text. Empty when no changes. */ + deltaText: string; + /** Whether regen+validation passed. False means the delta is unreliable. */ + validationOk: boolean; + /** Failures captured during regen. */ + failures: RegenResult["failures"]; + /** azure-only: per-provider API version changes. Empty for aws/github. */ + apiVersionDelta: AzureApiVersionChange[]; + /** The fresh snapshot produced by the regen. Null when regen failed. */ + freshSnapshot: SurfaceSnapshot | null; +} + +/** + * Options for checkRollingUpgrade. + */ +export interface CheckRollingUpgradeOptions { + /** Root of the lexicon package (must contain package.json and surface.snapshot.json). */ + lexiconDir: string; + /** Force re-fetch of the upstream spec (bypass cache). Default: false. */ + force?: boolean; + /** Print subprocess output while running. Default: false. */ + verbose?: boolean; + /** + * Override for the regen function — inject a mock in tests. + * Defaults to the production regenLexicon. + */ + _regenFn?: (opts: RegenOptions) => Promise; +} + +// ── Main entry ──────────────────────────────────────────────────────── + +/** + * Check whether the rolling spec has drifted since the committed baseline. + * + * Calls regenLexicon (from #524) to regenerate from current-latest, then diffs + * the resulting surface against the committed surface.snapshot.json. Returns + * hasUpgrade=true when the surface changed and validation passed. + * + * For azure, also computes an apiVersionDelta showing which ARM providers gained + * a new latest API version since the baseline was snapshotted. + * + * Never throws — all failures are captured in the result. + */ +export async function checkRollingUpgrade( + opts: CheckRollingUpgradeOptions, +): Promise { + const { + lexiconDir, + force = false, + verbose = false, + _regenFn = regenLexicon, + } = opts; + + const lexicon = detectLexicon(lexiconDir); + + const regenResult = await _regenFn({ + lexiconDir, + force, + verbose, + skipBundle: false, + skipBuild: false, + skipLint: false, + skipExamples: true, + }); + + const apiVersionDelta: AzureApiVersionChange[] = []; + + if (lexicon === "azure" && regenResult.freshSnapshot) { + const baselinePath = join(lexiconDir, SNAPSHOT_FILENAME); + if (existsSync(baselinePath)) { + try { + const baselineJSON = readFileSync(baselinePath, "utf-8"); + const baseline = parseSnapshot(baselineJSON); + apiVersionDelta.push(...computeAzureApiVersionDelta(baseline, regenResult.freshSnapshot)); + } catch { + // Ignore — apiVersionDelta stays empty; the surface diff is still valid + } + } + } + + return buildResult(lexicon, regenResult, apiVersionDelta); +} + +// ── Lexicon detection ───────────────────────────────────────────────── + +/** + * Detect which rolling lexicon a directory belongs to by its package name. + * Falls back to directory-name matching. + */ +function detectLexicon(lexiconDir: string): RollingLexicon { + try { + const pkgPath = join(lexiconDir, "package.json"); + if (existsSync(pkgPath)) { + const pkg = JSON.parse(readFileSync(pkgPath, "utf-8")) as { name?: string }; + const name = pkg.name ?? ""; + if (name.includes("azure")) return "azure"; + if (name.includes("github")) return "github"; + } + } catch { + // Fall through to directory-based detection + } + + const normalized = lexiconDir.replace(/\\/g, "/"); + if (/\/azure(\/|$)/.test(normalized)) return "azure"; + if (/\/github(\/|$)/.test(normalized)) return "github"; + return "aws"; +} + +// ── Azure API-version delta ─────────────────────────────────────────── + +/** + * Compute provider-level additions and removals between two azure snapshots. + * + * The snapshot resourceType values for azure look like + * "Microsoft.Storage/storageAccounts". This function extracts the provider + * name (the part before the first "/") and reports which providers appeared, + * disappeared, or (when version strings are supplied via diffAzureApiVersions) + * bumped their API version. + * + * Use diffAzureApiVersions when you have access to actual API version strings + * (e.g. from the lexicon's latestVersionPerProvider output in a CI run). + */ +export function computeAzureApiVersionDelta( + baseline: SurfaceSnapshot, + fresh: SurfaceSnapshot, +): AzureApiVersionChange[] { + const baseProviders = new Set(); + const freshProviders = new Set(); + + for (const entry of Object.values(baseline.entries)) { + const p = extractAzureProvider(entry.resourceType); + if (p) baseProviders.add(p); + } + + for (const entry of Object.values(fresh.entries)) { + const p = extractAzureProvider(entry.resourceType); + if (p) freshProviders.add(p); + } + + const changes: AzureApiVersionChange[] = []; + + for (const p of freshProviders) { + if (!baseProviders.has(p)) { + changes.push({ provider: p, before: null, after: "latest", kind: "added" }); + } + } + + for (const p of baseProviders) { + if (!freshProviders.has(p)) { + changes.push({ provider: p, before: "latest", after: null, kind: "removed" }); + } + } + + return changes.sort((a, b) => a.provider.localeCompare(b.provider)); +} + +/** + * Extract the Azure ARM provider name from a resourceType string. + * + * Input: "Microsoft.Storage/storageAccounts" + * Output: "Microsoft.Storage" + * + * Returns null for non-ARM resource types (e.g. empty string or non-Microsoft prefix). + */ +export function extractAzureProvider(resourceType: string): string | null { + if (!resourceType.startsWith("Microsoft.")) return null; + const slash = resourceType.indexOf("/"); + if (slash === -1) return null; + return resourceType.slice(0, slash); +} + +/** + * Diff actual API version strings per provider between two regen runs. + * + * Use this when the caller has access to the azure lexicon's + * latestVersionPerProvider output. Produces a richer delta that includes + * version bumps ("updated"), not just provider additions/removals. + * + * @param baseVersions Map from the previous regen. + * @param freshVersions Map from the current regen. + */ +export function diffAzureApiVersions( + baseVersions: Map, + freshVersions: Map, +): AzureApiVersionChange[] { + const changes: AzureApiVersionChange[] = []; + + for (const [provider, freshVersion] of freshVersions) { + const baseVersion = baseVersions.get(provider) ?? null; + if (baseVersion === null) { + changes.push({ provider, before: null, after: freshVersion, kind: "added" }); + } else if (baseVersion !== freshVersion) { + changes.push({ provider, before: baseVersion, after: freshVersion, kind: "updated" }); + } + } + + for (const [provider, baseVersion] of baseVersions) { + if (!freshVersions.has(provider)) { + changes.push({ provider, before: baseVersion, after: null, kind: "removed" }); + } + } + + return changes.sort((a, b) => a.provider.localeCompare(b.provider)); +} + +// ── Pure decision functions ─────────────────────────────────────────── + +/** + * Diff two surface snapshots. + * + * Pure function re-exported for callers that import rolling-upgrade and want to + * compute the delta without importing surface-snapshot directly. + */ +export function diffRollingSurface( + baseline: SurfaceSnapshot, + fresh: SurfaceSnapshot, +): SurfaceDelta { + return diffSurface(baseline, fresh); +} + +/** + * Classify a delta as an upgrade and determine its severity. + * + * Pure function — no I/O. Unit-testable with fixture snapshots. + * Used by the CLI and the checkRollingUpgrade entry point. + */ +export function classifyDelta(delta: SurfaceDelta): { + hasUpgrade: boolean; + severity: ChangeSeverity; +} { + const hasUpgrade = + delta.added.length > 0 || delta.changed.length > 0 || delta.removed.length > 0; + return { hasUpgrade, severity: hasUpgrade ? delta.severity : "none" }; +} + +// ── Result builder ──────────────────────────────────────────────────── + +function buildResult( + lexicon: RollingLexicon, + regenResult: RegenResult, + apiVersionDelta: AzureApiVersionChange[], +): RollingUpgradeResult { + const delta = regenResult.delta; + const { hasUpgrade, severity } = classifyDelta(delta); + const deltaText = hasUpgrade ? formatDelta(delta) : ""; + + return { + lexicon, + hasUpgrade, + severity, + delta, + deltaText, + validationOk: regenResult.ok, + failures: regenResult.failures, + apiVersionDelta, + freshSnapshot: regenResult.freshSnapshot, + }; +} diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index c4a2682b95..6c7454036a 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -69,6 +69,18 @@ export { type RegenResult, type RegenFailure, } from "./codegen/lexicon-regen"; +export { + checkRollingUpgrade, + classifyDelta, + diffRollingSurface, + computeAzureApiVersionDelta, + diffAzureApiVersions, + extractAzureProvider, + type RollingLexicon, + type RollingUpgradeResult, + type CheckRollingUpgradeOptions, + type AzureApiVersionChange, +} from "./codegen/rolling-upgrade"; export * from "./runtime"; export * from "./runtime-adapter"; export * from "./resource-attributes";