@telorun/analyzer 0.59.0 → 0.60.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/dist/analyzer.d.ts +9 -2
  2. package/dist/analyzer.d.ts.map +1 -1
  3. package/dist/analyzer.js +38 -10
  4. package/dist/builtins.d.ts.map +1 -1
  5. package/dist/builtins.js +16 -42
  6. package/dist/cel-environment.d.ts.map +1 -1
  7. package/dist/cel-environment.js +35 -0
  8. package/dist/flatten-for-analyzer.d.ts +4 -0
  9. package/dist/flatten-for-analyzer.d.ts.map +1 -1
  10. package/dist/flatten-for-analyzer.js +8 -0
  11. package/dist/index.d.ts +3 -0
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +6 -0
  14. package/dist/kernel-globals.d.ts +1 -1
  15. package/dist/kernel-globals.d.ts.map +1 -1
  16. package/dist/kernel-globals.js +15 -1
  17. package/dist/manifest-schemas.d.ts +407 -0
  18. package/dist/manifest-schemas.d.ts.map +1 -0
  19. package/dist/manifest-schemas.js +396 -0
  20. package/dist/module-file-claims.d.ts +6 -0
  21. package/dist/module-file-claims.d.ts.map +1 -1
  22. package/dist/module-file-claims.js +4 -0
  23. package/dist/module-metadata-scope.d.ts +44 -0
  24. package/dist/module-metadata-scope.d.ts.map +1 -0
  25. package/dist/module-metadata-scope.js +82 -0
  26. package/dist/parse-loaded-file.d.ts.map +1 -1
  27. package/dist/parse-loaded-file.js +16 -0
  28. package/dist/release/bump-level.d.ts +46 -0
  29. package/dist/release/bump-level.d.ts.map +1 -0
  30. package/dist/release/bump-level.js +80 -0
  31. package/dist/release/changelog.d.ts +36 -0
  32. package/dist/release/changelog.d.ts.map +1 -0
  33. package/dist/release/changelog.js +53 -0
  34. package/dist/release/fragment.d.ts +48 -0
  35. package/dist/release/fragment.d.ts.map +1 -0
  36. package/dist/release/fragment.js +84 -0
  37. package/dist/release/index.d.ts +26 -0
  38. package/dist/release/index.d.ts.map +1 -0
  39. package/dist/release/index.js +18 -0
  40. package/dist/release/ledger.d.ts +53 -0
  41. package/dist/release/ledger.d.ts.map +1 -0
  42. package/dist/release/ledger.js +109 -0
  43. package/dist/release/payload-digest.d.ts +63 -0
  44. package/dist/release/payload-digest.d.ts.map +1 -0
  45. package/dist/release/payload-digest.js +65 -0
  46. package/dist/release/release-plan.d.ts +128 -0
  47. package/dist/release/release-plan.d.ts.map +1 -0
  48. package/dist/release/release-plan.js +277 -0
  49. package/dist/release/version-stamp.d.ts +50 -0
  50. package/dist/release/version-stamp.d.ts.map +1 -0
  51. package/dist/release/version-stamp.js +119 -0
  52. package/dist/release/workspace-config.d.ts +41 -0
  53. package/dist/release/workspace-config.d.ts.map +1 -0
  54. package/dist/release/workspace-config.js +60 -0
  55. package/dist/schema-compat.js +2 -1
  56. package/dist/validate-step-inputs.d.ts +1 -1
  57. package/dist/validate-step-inputs.d.ts.map +1 -1
  58. package/dist/validate-step-inputs.js +99 -8
  59. package/package.json +3 -3
  60. package/src/analyzer.ts +41 -10
  61. package/src/builtins.ts +16 -42
  62. package/src/cel-environment.ts +37 -0
  63. package/src/flatten-for-analyzer.ts +12 -0
  64. package/src/index.ts +9 -1
  65. package/src/kernel-globals.ts +23 -1
  66. package/src/manifest-schemas.ts +408 -0
  67. package/src/module-file-claims.ts +10 -0
  68. package/src/module-metadata-scope.ts +88 -0
  69. package/src/parse-loaded-file.ts +16 -0
  70. package/src/release/bump-level.ts +95 -0
  71. package/src/release/changelog.ts +62 -0
  72. package/src/release/fragment.ts +130 -0
  73. package/src/release/index.ts +66 -0
  74. package/src/release/ledger.ts +142 -0
  75. package/src/release/payload-digest.ts +83 -0
  76. package/src/release/release-plan.ts +392 -0
  77. package/src/release/version-stamp.ts +142 -0
  78. package/src/release/workspace-config.ts +81 -0
  79. package/src/schema-compat.ts +1 -1
  80. package/src/validate-step-inputs.ts +109 -9
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Rendering a module's `CHANGELOG.md` entry, and splicing it into the file.
3
+ *
4
+ * The output shape is the one changie was configured to emit — `## <version> -
5
+ * <date>`, `### <Kind>`, `* <body>` — so a module's history stays one document
6
+ * across the handover rather than changing format mid-file.
7
+ *
8
+ * Prepending rather than rewriting: a changelog is append-only history, and the
9
+ * only edit ever made to it is inserting the newest release above the previous
10
+ * one. Everything already written is untouched bytes.
11
+ */
12
+
13
+ import { FRAGMENT_KIND_ORDER, type FragmentKind } from "./bump-level.js";
14
+
15
+ export const CHANGELOG_HEADER = "# Changelog";
16
+
17
+ export interface ChangelogRelease {
18
+ readonly version: string;
19
+ /** `YYYY-MM-DD`. Passed in rather than read from a clock, so a plan renders
20
+ * identically whenever it is rendered and a test needs no clock control. */
21
+ readonly date: string;
22
+ readonly entries: readonly { readonly kind: FragmentKind; readonly body: string }[];
23
+ }
24
+
25
+ /** One release block. Entries group under their kind, in the vocabulary's own
26
+ * order, so two releases never disagree about where `Fixed` sits. */
27
+ export function renderChangelogRelease(release: ChangelogRelease): string {
28
+ const byKind = new Map<FragmentKind, string[]>();
29
+ for (const entry of release.entries) {
30
+ const list = byKind.get(entry.kind);
31
+ if (list) list.push(entry.body);
32
+ else byKind.set(entry.kind, [entry.body]);
33
+ }
34
+
35
+ const lines = [`## ${release.version} - ${release.date}`];
36
+ for (const kind of FRAGMENT_KIND_ORDER) {
37
+ const bodies = byKind.get(kind);
38
+ if (!bodies) continue;
39
+ lines.push(`### ${kind}`);
40
+ for (const body of bodies) lines.push(`* ${body}`);
41
+ }
42
+ return lines.join("\n") + "\n";
43
+ }
44
+
45
+ /**
46
+ * Insert a release block below the file's `# Changelog` header, creating the
47
+ * file's skeleton when it has none.
48
+ *
49
+ * A module with no changelog yet is the ordinary case for a new module, so this
50
+ * writes the header rather than failing — the alternative is a release that
51
+ * stops to ask for an empty file to be created by hand.
52
+ */
53
+ export function prependChangelogRelease(existing: string | undefined, block: string): string {
54
+ const text = existing ?? "";
55
+ const header = text.match(/^#\s+Changelog[^\n]*\n/);
56
+ if (!header) {
57
+ const rest = text.trim();
58
+ return `${CHANGELOG_HEADER}\n\n${block}${rest ? `\n${rest}\n` : ""}`;
59
+ }
60
+ const after = text.slice(header[0].length).replace(/^\n+/, "");
61
+ return `${header[0]}\n${block}${after ? `\n${after}` : ""}`;
62
+ }
@@ -0,0 +1,130 @@
1
+ /**
2
+ * A release fragment — one pending change, written by an author.
3
+ *
4
+ * The shape is changesets' (one file, several modules, one body) carrying
5
+ * changie's kind vocabulary, which is what lets one cross-cutting change be one
6
+ * file while the kind still drives both the level and the changelog section a
7
+ * line lands under. Changie's own file is the other arrangement — one module per
8
+ * file — so a change touching five modules meant five files repeating one
9
+ * sentence, and its key was the module's bare directory NAME, which two
10
+ * directories in different subtrees can share.
11
+ *
12
+ * A fragment names modules by **workspace-relative path**, the same key the
13
+ * ledger and every diagnostic use.
14
+ *
15
+ * They are plain YAML validated here, not a `Telo.*` kind: build-time repo state
16
+ * with no controller and no capability, which the runtime never loads.
17
+ */
18
+
19
+ import { Document, parseDocument } from "yaml";
20
+ import { isFragmentKind, type FragmentKind } from "./bump-level.js";
21
+
22
+ /** A module's workspace-relative directory path — `modules/sql`, `apps/hub`. */
23
+ export type ModuleKey = string;
24
+
25
+ export interface ReleaseFragment {
26
+ /**
27
+ * Where this fragment was read from, workspace-relative. Carried so a
28
+ * diagnostic can name the file to fix and `apply` can delete the file it
29
+ * consumed.
30
+ */
31
+ readonly source: string;
32
+ /** The directly-changed modules this fragment declares, each with its kind. */
33
+ readonly modules: ReadonlyMap<ModuleKey, FragmentKind>;
34
+ /** The changelog line. */
35
+ readonly body: string;
36
+ }
37
+
38
+ export class FragmentError extends Error {
39
+ constructor(
40
+ readonly source: string,
41
+ message: string,
42
+ ) {
43
+ super(message);
44
+ }
45
+ }
46
+
47
+ export function parseFragment(text: string, source: string): ReleaseFragment {
48
+ let value: unknown;
49
+ try {
50
+ value = parseDocument(text).toJSON();
51
+ } catch (err) {
52
+ throw new FragmentError(
53
+ source,
54
+ `${source} is not valid YAML: ${err instanceof Error ? err.message : String(err)}`,
55
+ );
56
+ }
57
+ if (value === null || typeof value !== "object" || Array.isArray(value)) {
58
+ throw new FragmentError(source, `${source} must be a YAML mapping.`);
59
+ }
60
+ const record = value as Record<string, unknown>;
61
+
62
+ for (const key of Object.keys(record)) {
63
+ if (key !== "modules" && key !== "body") {
64
+ throw new FragmentError(
65
+ source,
66
+ `${source}: unknown field '${key}'. A fragment carries 'modules:' and 'body:'.`,
67
+ );
68
+ }
69
+ }
70
+
71
+ const rawModules = record.modules;
72
+ if (rawModules === null || typeof rawModules !== "object" || Array.isArray(rawModules)) {
73
+ throw new FragmentError(
74
+ source,
75
+ `${source}: 'modules' must be a mapping of module path to kind, ` +
76
+ `e.g. { modules/sql: Fixed }.`,
77
+ );
78
+ }
79
+ const modules = new Map<ModuleKey, FragmentKind>();
80
+ for (const [key, kind] of Object.entries(rawModules as Record<string, unknown>)) {
81
+ if (!isFragmentKind(kind)) {
82
+ throw new FragmentError(
83
+ source,
84
+ `${source}: '${key}' declares kind '${String(kind)}', which is not a release kind. ` +
85
+ `Use Added, Changed, Deprecated, Removed, Fixed or Security.`,
86
+ );
87
+ }
88
+ modules.set(normalizeModuleKey(key), kind);
89
+ }
90
+ if (modules.size === 0) {
91
+ throw new FragmentError(
92
+ source,
93
+ `${source}: 'modules' is empty, so this fragment releases nothing. ` +
94
+ `Name at least one module by its workspace-relative path.`,
95
+ );
96
+ }
97
+
98
+ const body = record.body;
99
+ if (typeof body !== "string" || body.trim() === "") {
100
+ throw new FragmentError(
101
+ source,
102
+ `${source}: 'body' must be the changelog line for this change.`,
103
+ );
104
+ }
105
+
106
+ return { source, modules, body: body.trim() };
107
+ }
108
+
109
+ /**
110
+ * Render a fragment, for `telo release add`.
111
+ *
112
+ * Serialized rather than templated so a long body folds the way the YAML writer
113
+ * folds it, and so a module path needing quotes gets them.
114
+ */
115
+ export function serializeFragment(
116
+ modules: ReadonlyMap<ModuleKey, FragmentKind>,
117
+ body: string,
118
+ ): string {
119
+ const doc = new Document({
120
+ modules: Object.fromEntries([...modules].map(([key, kind]) => [key, kind])),
121
+ body,
122
+ });
123
+ return doc.toString({ lineWidth: 88 });
124
+ }
125
+
126
+ /** Module keys are POSIX-separated and carry no leading `./` or trailing slash,
127
+ * so the same directory written three ways is one key. */
128
+ export function normalizeModuleKey(key: string): ModuleKey {
129
+ return key.replace(/\\/g, "/").replace(/^\.\//, "").replace(/\/+$/, "");
130
+ }
@@ -0,0 +1,66 @@
1
+ /**
2
+ * The release model: module identity, fragments, the ledger, the edge graph,
3
+ * level propagation and version planning.
4
+ *
5
+ * Browser-safe by construction — pure data in, plan out — so the telo editor can
6
+ * answer "what does changing this library bump?" from the same model the CLI
7
+ * releases from. Everything Node-shaped (finding the workspace, running the
8
+ * controller builder, building payloads, reading git, writing files) is
9
+ * `cli/nodejs/src/release/`.
10
+ */
11
+
12
+ export {
13
+ FRAGMENT_KINDS,
14
+ FRAGMENT_KIND_ORDER,
15
+ applyBump,
16
+ compareVersions,
17
+ isFragmentKind,
18
+ isReleaseVersion,
19
+ levelOfKind,
20
+ maxLevel,
21
+ } from "./bump-level.js";
22
+ export type { BumpLevel, FragmentKind } from "./bump-level.js";
23
+
24
+ export {
25
+ CHANGELOG_HEADER,
26
+ prependChangelogRelease,
27
+ renderChangelogRelease,
28
+ } from "./changelog.js";
29
+ export type { ChangelogRelease } from "./changelog.js";
30
+
31
+ export { FragmentError, normalizeModuleKey, parseFragment, serializeFragment } from "./fragment.js";
32
+ export type { ModuleKey, ReleaseFragment } from "./fragment.js";
33
+
34
+ export { EMPTY_LEDGER, LEDGER_PATH, LedgerError, parseLedger, serializeLedger } from "./ledger.js";
35
+ export type { Ledger, LedgerEntry } from "./ledger.js";
36
+
37
+ export {
38
+ LOCALLY_DERIVED_LAYERS,
39
+ MANIFEST_LAYER,
40
+ diffLayerDigests,
41
+ layerDigestKey,
42
+ } from "./payload-digest.js";
43
+ export type { LayerChange, LayerDigests } from "./payload-digest.js";
44
+
45
+ export { orderByImports, planRelease } from "./release-plan.js";
46
+ export type {
47
+ ArtifactKind,
48
+ BumpReason,
49
+ ChangelogEntry,
50
+ ModuleEvidence,
51
+ PlannedModule,
52
+ ReleaseDiagnostic,
53
+ ReleaseEvidence,
54
+ ReleasePlan,
55
+ } from "./release-plan.js";
56
+
57
+ export {
58
+ VersionStampError,
59
+ readManifestVersion,
60
+ stampCrateVersion,
61
+ stampManifestVersion,
62
+ stampPackageVersion,
63
+ } from "./version-stamp.js";
64
+
65
+ export { WORKSPACE_FILENAME, WorkspaceConfigError, parseWorkspaceConfig } from "./workspace-config.js";
66
+ export type { WorkspaceConfig } from "./workspace-config.js";
@@ -0,0 +1,142 @@
1
+ /**
2
+ * `.changes/ledger.yaml` — what each module looks like **as published**.
3
+ *
4
+ * It exists so a gate can answer "does my payload differ from what is published
5
+ * at my current version?" with no network call, no merge base and no
6
+ * credentials: a fork's PR runs the identical computation the release job runs.
7
+ * That is the load-bearing property — the PR gate and the publish gate compute
8
+ * the same number.
9
+ *
10
+ * **It is a cache, never the authority.** A committed digest can disagree with
11
+ * the registry — a hand edit, an `apply` whose publish then failed, a push made
12
+ * outside the pipeline — so `telo publish` still reads the registry, and
13
+ * `telo release verify` reconciles the two on demand. A *missing* entry is not
14
+ * drift: it means nothing is published, which is the correct reading for a
15
+ * module that has never shipped.
16
+ *
17
+ * **It records the registry base**, because canonicalizing a relative `imports:`
18
+ * source writes the destination into the manifest layer — so the digests below
19
+ * are digests *against that base*, and comparing them to digests taken against
20
+ * another one would be comparing two different artifacts.
21
+ */
22
+
23
+ import { Document, parseDocument } from "yaml";
24
+ import { isReleaseVersion } from "./bump-level.js";
25
+ import { normalizeModuleKey, type ModuleKey } from "./fragment.js";
26
+ import type { LayerDigests } from "./payload-digest.js";
27
+
28
+ export const LEDGER_PATH = ".changes/ledger.yaml";
29
+
30
+ export interface LedgerEntry {
31
+ /** The version these digests were taken at — the tag the artifact published
32
+ * under. */
33
+ readonly version: string;
34
+ readonly layers: LayerDigests;
35
+ }
36
+
37
+ export interface Ledger {
38
+ /**
39
+ * The publish destination base the digests were taken against
40
+ * (`oci://ghcr.io/telorun`). Absent in a workspace that has published nothing
41
+ * yet, which is why it is optional rather than required — but a `check` that
42
+ * has entries and no base cannot reproduce them, and says so.
43
+ */
44
+ readonly registry?: string;
45
+ readonly modules: ReadonlyMap<ModuleKey, LedgerEntry>;
46
+ }
47
+
48
+ export class LedgerError extends Error {}
49
+
50
+ export const EMPTY_LEDGER: Ledger = { modules: new Map() };
51
+
52
+ export function parseLedger(text: string, where: string): Ledger {
53
+ let value: unknown;
54
+ try {
55
+ value = parseDocument(text).toJSON();
56
+ } catch (err) {
57
+ throw new LedgerError(
58
+ `${where} is not valid YAML: ${err instanceof Error ? err.message : String(err)}`,
59
+ );
60
+ }
61
+ if (value === null) return EMPTY_LEDGER;
62
+ if (typeof value !== "object" || Array.isArray(value)) {
63
+ throw new LedgerError(`${where} must be a YAML mapping.`);
64
+ }
65
+ const record = value as Record<string, unknown>;
66
+
67
+ for (const key of Object.keys(record)) {
68
+ if (key !== "registry" && key !== "modules") {
69
+ throw new LedgerError(
70
+ `${where}: unknown field '${key}'. The ledger carries 'registry:' and 'modules:'.`,
71
+ );
72
+ }
73
+ }
74
+
75
+ const registry = record.registry;
76
+ if (registry !== undefined && typeof registry !== "string") {
77
+ throw new LedgerError(`${where}: 'registry' must be the publish destination base, as a string.`);
78
+ }
79
+
80
+ const rawModules = record.modules;
81
+ if (rawModules === undefined || rawModules === null) return { ...(registry ? { registry } : {}), modules: new Map() };
82
+ if (typeof rawModules !== "object" || Array.isArray(rawModules)) {
83
+ throw new LedgerError(`${where}: 'modules' must be a mapping of module path to entry.`);
84
+ }
85
+
86
+ const modules = new Map<ModuleKey, LedgerEntry>();
87
+ for (const [key, raw] of Object.entries(rawModules as Record<string, unknown>)) {
88
+ if (raw === null || typeof raw !== "object" || Array.isArray(raw)) {
89
+ throw new LedgerError(`${where}: entry '${key}' must be a mapping.`);
90
+ }
91
+ const entry = raw as Record<string, unknown>;
92
+ if (!isReleaseVersion(entry.version)) {
93
+ throw new LedgerError(
94
+ `${where}: entry '${key}' has no major.minor.patch 'version'. The ledger records the ` +
95
+ `version each digest was taken at; without it the digest says nothing.`,
96
+ );
97
+ }
98
+ const layers: Record<string, string> = {};
99
+ const rawLayers = entry.layers;
100
+ if (rawLayers !== undefined && rawLayers !== null) {
101
+ if (typeof rawLayers !== "object" || Array.isArray(rawLayers)) {
102
+ throw new LedgerError(`${where}: entry '${key}' has a non-mapping 'layers'.`);
103
+ }
104
+ for (const [layer, digest] of Object.entries(rawLayers as Record<string, unknown>)) {
105
+ if (typeof digest !== "string") {
106
+ throw new LedgerError(`${where}: entry '${key}' layer '${layer}' is not a digest string.`);
107
+ }
108
+ layers[layer] = digest;
109
+ }
110
+ }
111
+ modules.set(normalizeModuleKey(key), { version: entry.version, layers });
112
+ }
113
+
114
+ return { ...(registry ? { registry } : {}), modules };
115
+ }
116
+
117
+ /**
118
+ * Render the ledger.
119
+ *
120
+ * Keys are sorted so a release's ledger diff shows only the modules that moved
121
+ * — the file is committed and reviewed, and a map whose order followed insertion
122
+ * would reorder wholesale on every run.
123
+ */
124
+ export function serializeLedger(ledger: Ledger): string {
125
+ const modules: Record<string, unknown> = {};
126
+ for (const key of [...ledger.modules.keys()].sort()) {
127
+ const entry = ledger.modules.get(key)!;
128
+ const layers: Record<string, string> = {};
129
+ for (const layer of Object.keys(entry.layers).sort()) layers[layer] = entry.layers[layer];
130
+ modules[key] = { version: entry.version, layers };
131
+ }
132
+ const doc = new Document({
133
+ ...(ledger.registry ? { registry: ledger.registry } : {}),
134
+ modules,
135
+ });
136
+ return (
137
+ "# Generated by `telo release apply` — what each module looks like as published.\n" +
138
+ "# A cache of the registry's answer, so a PR gate needs no credentials.\n" +
139
+ "# `telo release verify` reconciles it against the registry.\n" +
140
+ doc.toString({ lineWidth: 0 })
141
+ );
142
+ }
@@ -0,0 +1,83 @@
1
+ /**
2
+ * A module's payload, reduced to one digest per layer — the exact answer to
3
+ * "did this module's artifact change?".
4
+ *
5
+ * It is exact because it is taken from the bytes: it fires for an inlined
6
+ * sibling's edit, for a shared-library fix, and for a transitive bump the
7
+ * lockfile alone moved, none of which touch a file under the module's own
8
+ * directory. No path-scoped rule and no declared-dependency graph can see any of
9
+ * them.
10
+ *
11
+ * The digest itself is `computeFilesIntegrity`, the same framing-independent
12
+ * content digest the published `layers:` index carries — which is what makes the
13
+ * ledger's recorded number and the registry's the same number, comparable
14
+ * without fetching a blob.
15
+ */
16
+
17
+ import { PLATFORM_AXES, type ArtifactSelector, type LayerRole } from "../artifact-selector.js";
18
+
19
+ /** Per-layer integrity, keyed by `layerDigestKey`. */
20
+ export type LayerDigests = Readonly<Record<string, string>>;
21
+
22
+ /** The ledger key for the module's `telo.yaml` layer. */
23
+ export const MANIFEST_LAYER = "manifest";
24
+
25
+ /**
26
+ * Ledger keys the registry cannot answer for, and so the ones `telo release
27
+ * verify` reconciles nothing about and must preserve.
28
+ *
29
+ * Exactly one today, and for a structural reason rather than an omission: the
30
+ * published `layers:` index lives *inside* `telo.yaml`, so it cannot carry that
31
+ * file's own digest — the entry would have to be computed over bytes containing
32
+ * it. Nor can the digest be recovered by hashing what the registry serves, since
33
+ * the transport injects the index at push time and the published text is
34
+ * therefore not the text the payload builder produced.
35
+ *
36
+ * It stays in the ledger regardless, because it is the only thing that sees a
37
+ * **manifest-only change**: a schema edit, a new kind, a description, a
38
+ * dependency's version moving into a pin. None of those touch a controller byte,
39
+ * so without this key such a module would report no drift and its fix would ship
40
+ * to nobody — the exact failure the digest exists to prevent.
41
+ */
42
+ export const LOCALLY_DERIVED_LAYERS: ReadonlySet<string> = new Set([MANIFEST_LAYER]);
43
+
44
+ /**
45
+ * A layer's identity within an artifact, as a stable, readable, YAML-plain key.
46
+ *
47
+ * A role alone is not the identity — there is one controller layer per selector
48
+ * — so a selector's axes join the key. Rendered as `controller/js` and
49
+ * `controller/napi+linux+amd64+gnu` rather than through `selectorKey`'s
50
+ * `axis=value;…` form, because this string is read by a human in a ledger diff
51
+ * and written into a plain YAML key; the axis order is `PLATFORM_AXES`, so the
52
+ * rendering is deterministic rather than dependent on how the selector was
53
+ * built.
54
+ */
55
+ export function layerDigestKey(role: LayerRole | string, selector?: ArtifactSelector): string {
56
+ if (!selector) return role;
57
+ const parts = [selector.format, ...PLATFORM_AXES.map((axis) => selector[axis])].filter(
58
+ (value): value is string => value !== undefined,
59
+ );
60
+ return `${role}/${parts.join("+")}`;
61
+ }
62
+
63
+ /** A layer whose digest differs between two readings, or that exists in only
64
+ * one of them. `before` / `after` are absent when the layer is absent there —
65
+ * a payload that gained or lost a layer changed just as much as one whose
66
+ * bytes moved. */
67
+ export interface LayerChange {
68
+ readonly layer: string;
69
+ readonly before?: string;
70
+ readonly after?: string;
71
+ }
72
+
73
+ /** Every layer on which two readings disagree, in key order. Empty means the
74
+ * payloads are byte-identical. */
75
+ export function diffLayerDigests(before: LayerDigests, after: LayerDigests): LayerChange[] {
76
+ const changes: LayerChange[] = [];
77
+ for (const layer of [...new Set([...Object.keys(before), ...Object.keys(after)])].sort()) {
78
+ const a = before[layer];
79
+ const b = after[layer];
80
+ if (a !== b) changes.push({ layer, ...(a ? { before: a } : {}), ...(b ? { after: b } : {}) });
81
+ }
82
+ return changes;
83
+ }