From 1b530f9010cdc4e7ab9d8c4aeaff8b7bfc15da7b Mon Sep 17 00:00:00 2001 From: konojunya Date: Sun, 6 Sep 2026 02:24:01 +0900 Subject: [PATCH 1/2] Audit generated guidance freshness across consumers and deployment --- .github/workflows/consumer-freshness.yaml | 24 ++++++++ README.md | 15 +++++ package.json | 1 + scripts/consumer-freshness.mjs | 71 +++++++++++++++++++++++ scripts/consumer-freshness.test.mjs | 47 +++++++++++++++ 5 files changed, 158 insertions(+) create mode 100644 .github/workflows/consumer-freshness.yaml create mode 100644 scripts/consumer-freshness.mjs create mode 100644 scripts/consumer-freshness.test.mjs diff --git a/.github/workflows/consumer-freshness.yaml b/.github/workflows/consumer-freshness.yaml new file mode 100644 index 0000000..f02f3be --- /dev/null +++ b/.github/workflows/consumer-freshness.yaml @@ -0,0 +1,24 @@ +name: Consumer freshness + +on: + push: + branches: [main] + schedule: + - cron: '43 2 * * *' + workflow_dispatch: + +permissions: + contents: read + +jobs: + freshness: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v7 + with: + persist-credentials: false + - uses: actions/setup-node@v7 + with: + node-version: 22.14.0 + - run: npm run check + - run: npm run consumers:check diff --git a/README.md b/README.md index b111386..8751ff6 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,21 @@ control failure notifications. Web additionally audits the release it deploys. Site shell examples receive a generated `$ ` prompt; the installable skill keeps executable command lines without prompts. The canonical source remains shared. +## Consumer freshness + +`npm run consumers:check` compares the current generated content with the CLI and +Web main-branch pins and the website's public `/docs-source.json` deployment +provenance. It verifies every pinned manifest hash and the actual CLI skill bytes. +Equivalent content at an older commit is accepted; a website-only edit does not +require a pointless CLI skill update. Website production provenance identifies the +deployed source; it is not a substitute for checking rendered pages after release. + +The read-only Consumer freshness workflow runs after Docs main changes, daily, +and on manual dispatch. A source change may intentionally fail this audit until +the required consumer PRs and Web deployment finish. Update the affected pins, +run consumer tests, merge and deploy, then rerun the audit. It never grants write +permissions or silently auto-merges consumer updates. + ## Migration status Shared workflow and multilingual source generation are available here. CLI and Web diff --git a/package.json b/package.json index dcdf919..2b46131 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "type": "module", "license": "Apache-2.0", "scripts": { + "consumers:check": "node scripts/consumer-freshness.mjs", "release:check": "node scripts/cli-release.mjs", "release:sync": "node scripts/cli-release.mjs --sync", "generate": "node scripts/generate.mjs", diff --git a/scripts/consumer-freshness.mjs b/scripts/consumer-freshness.mjs new file mode 100644 index 0000000..41bf956 --- /dev/null +++ b/scripts/consumer-freshness.mjs @@ -0,0 +1,71 @@ +import assert from 'node:assert/strict'; +import { createHash } from 'node:crypto'; +import { readFile } from 'node:fs/promises'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const digest = bytes => createHash('sha256').update(bytes).digest('hex'); +const skillPath = 'skills/stack-diagrams/SKILL.md'; + +export function compareConsumer(expected, actual, kind) { + assert.ok(['cli', 'web', 'production'].includes(kind)); + assert.equal(expected.schemaVersion, '1.0'); + assert.equal(actual.schemaVersion, '1.0', 'Unsupported consumer manifest'); + assert.deepEqual(actual.cli, expected.cli, `${kind}: published CLI identity is stale`); + const select = manifest => { + assert.ok(Array.isArray(manifest.files)); + const files = manifest.files.filter(file => kind === 'cli' ? file.path === skillPath : file.path.startsWith('site/') || file.path === skillPath); + assert.ok(files.length > 0); + const sorted = files.map(file => { + assert.match(file.sha256, /^[a-f0-9]{64}$/); + return [file.path, file.sha256]; + }).sort(([a], [b]) => a.localeCompare(b)); + assert.equal(new Set(sorted.map(([file]) => file)).size, sorted.length, 'Duplicate manifest entry'); + return sorted; + }; + assert.deepEqual(select(actual), select(expected), `${kind}: generated Docs content is stale; update the consumer pin`); +} + +async function get(url) { + const response = await fetch(url, { signal: AbortSignal.timeout(15_000), cache: 'no-store' }); + assert.ok(response.ok, `Freshness resource unavailable: ${url} (HTTP ${response.status})`); + return Buffer.from(await response.arrayBuffer()); +} + +export async function readPinnedManifest(lock, fetchBytes = get) { + assert.equal(lock.repository, 'stack-sh/docs'); + assert.match(lock.revision, /^[a-f0-9]{40}$/); + assert.match(lock.manifestSha256, /^[a-f0-9]{64}$/); + const bytes = await fetchBytes(`https://raw.githubusercontent.com/stack-sh/docs/${lock.revision}/generated/manifest.json`); + assert.equal(digest(bytes), lock.manifestSha256, 'Consumer manifest integrity mismatch'); + return JSON.parse(bytes.toString('utf8')); +} + +export async function auditConsumers(expected, fetchBytes = get) { + const targets = [ + ['cli', 'https://raw.githubusercontent.com/stack-sh/cli/main/skills/docs-source.json'], + ['web', 'https://raw.githubusercontent.com/stack-sh/web/main/scripts/docs-source.json'], + ['production', 'https://stack-diagram.com/docs-source.json'], + ]; + const errors = []; + for (const [kind, url] of targets) { + try { + const lock = JSON.parse((await fetchBytes(url)).toString('utf8')); + const actual = await readPinnedManifest(lock, fetchBytes); + compareConsumer(expected, actual, kind); + if (kind === 'cli') { + const skill = await fetchBytes('https://raw.githubusercontent.com/stack-sh/cli/main/skills/stack-diagrams/SKILL.md'); + assert.equal(digest(skill), expected.files.find(file => file.path === skillPath).sha256, 'CLI skill differs from the canonical generated bytes'); + } + console.log(`${kind}: canonical generated content is current (${lock.revision}).`); + } catch (error) { + errors.push(`${kind}: ${error.message}`); + } + } + if (errors.length) throw new Error(errors.join('\n')); +} + +if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { + const expected = JSON.parse(await readFile(new URL('../generated/manifest.json', import.meta.url), 'utf8')); + await auditConsumers(expected); +} diff --git a/scripts/consumer-freshness.test.mjs b/scripts/consumer-freshness.test.mjs new file mode 100644 index 0000000..e815370 --- /dev/null +++ b/scripts/consumer-freshness.test.mjs @@ -0,0 +1,47 @@ +import assert from 'node:assert/strict'; +import { createHash } from 'node:crypto'; +import test from 'node:test'; +import { auditConsumers, compareConsumer, readPinnedManifest } from './consumer-freshness.mjs'; + +const skill = 'generated skill'; +const hash = value => createHash('sha256').update(value).digest('hex'); +const manifest = { schemaVersion: '1.0', cli: { version: '0.4.0', revision: 'a'.repeat(40) }, files: [ + { path: 'skills/stack-diagrams/SKILL.md', sha256: hash(skill) }, + { path: 'site/index.md', sha256: 'b'.repeat(64) }, +] }; + +test('compares relevant content rather than requiring equal source commits', () => { + compareConsumer(manifest, structuredClone(manifest), 'web'); + const pageChange = structuredClone(manifest); + pageChange.files[1].sha256 = 'c'.repeat(64); + compareConsumer(manifest, pageChange, 'cli'); + for (const kind of ['web', 'production']) assert.throws(() => compareConsumer(manifest, pageChange, kind), /stale/); +}); + +test('rejects missing resources, stale CLI identity, and unsupported schema', () => { + for (const changed of [ + { ...manifest, files: manifest.files.slice(0, 1) }, + { ...manifest, cli: { ...manifest.cli, version: '0.3.0' } }, + { ...manifest, schemaVersion: '2.0' }, + ]) assert.throws(() => compareConsumer(manifest, changed, 'web')); +}); + +test('verifies pinned manifest integrity and rejects mutable revisions', async () => { + const bytes = Buffer.from(JSON.stringify(manifest)); + const lock = { repository: 'stack-sh/docs', revision: 'a'.repeat(40), manifestSha256: hash(bytes) }; + assert.deepEqual(await readPinnedManifest(lock, async () => bytes), manifest); + await assert.rejects(readPinnedManifest({ ...lock, revision: 'main' }, async () => bytes)); + await assert.rejects(readPinnedManifest(lock, async () => Buffer.from('altered')), /integrity/); +}); + +test('checks actual installed skill bytes and reports stale production separately', async () => { + const bytes = Buffer.from(JSON.stringify(manifest)); + const lock = Buffer.from(JSON.stringify({ repository: 'stack-sh/docs', revision: 'a'.repeat(40), manifestSha256: hash(bytes) })); + const fetchBytes = async url => url.endsWith('/generated/manifest.json') ? bytes : url.endsWith('/SKILL.md') ? Buffer.from(skill) : lock; + await auditConsumers(manifest, fetchBytes); + await assert.rejects(auditConsumers(manifest, async url => url.endsWith('/SKILL.md') ? Buffer.from('hand edited') : fetchBytes(url)), /CLI skill differs/); + await assert.rejects(auditConsumers(manifest, async url => { + if (url === 'https://stack-diagram.com/docs-source.json') throw new Error('deployment missing'); + return fetchBytes(url); + }), /production: deployment missing/); +}); From ec07ee3ff85c2ba7f7062d05e6f00b7add1b52b8 Mon Sep 17 00:00:00 2001 From: konojunya Date: Sun, 6 Sep 2026 02:25:44 +0900 Subject: [PATCH 2/2] Resolve consumer branch tips before reading immutable audit inputs --- scripts/consumer-freshness.mjs | 16 ++++++++++++---- scripts/consumer-freshness.test.mjs | 2 +- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/scripts/consumer-freshness.mjs b/scripts/consumer-freshness.mjs index 41bf956..ea0b95c 100644 --- a/scripts/consumer-freshness.mjs +++ b/scripts/consumer-freshness.mjs @@ -43,18 +43,26 @@ export async function readPinnedManifest(lock, fetchBytes = get) { export async function auditConsumers(expected, fetchBytes = get) { const targets = [ - ['cli', 'https://raw.githubusercontent.com/stack-sh/cli/main/skills/docs-source.json'], - ['web', 'https://raw.githubusercontent.com/stack-sh/web/main/scripts/docs-source.json'], + ['cli', 'skills/docs-source.json'], + ['web', 'scripts/docs-source.json'], ['production', 'https://stack-diagram.com/docs-source.json'], ]; const errors = []; - for (const [kind, url] of targets) { + for (const [kind, resource] of targets) { try { + let base; + if (kind !== 'production') { + const ref = JSON.parse((await fetchBytes(`https://api.github.com/repos/stack-sh/${kind}/git/ref/heads/main`)).toString('utf8')); + assert.equal(ref.object.type, 'commit'); + assert.match(ref.object.sha, /^[a-f0-9]{40}$/); + base = `https://raw.githubusercontent.com/stack-sh/${kind}/${ref.object.sha}/`; + } + const url = kind === 'production' ? resource : base + resource; const lock = JSON.parse((await fetchBytes(url)).toString('utf8')); const actual = await readPinnedManifest(lock, fetchBytes); compareConsumer(expected, actual, kind); if (kind === 'cli') { - const skill = await fetchBytes('https://raw.githubusercontent.com/stack-sh/cli/main/skills/stack-diagrams/SKILL.md'); + const skill = await fetchBytes(base + 'skills/stack-diagrams/SKILL.md'); assert.equal(digest(skill), expected.files.find(file => file.path === skillPath).sha256, 'CLI skill differs from the canonical generated bytes'); } console.log(`${kind}: canonical generated content is current (${lock.revision}).`); diff --git a/scripts/consumer-freshness.test.mjs b/scripts/consumer-freshness.test.mjs index e815370..75b9073 100644 --- a/scripts/consumer-freshness.test.mjs +++ b/scripts/consumer-freshness.test.mjs @@ -37,7 +37,7 @@ test('verifies pinned manifest integrity and rejects mutable revisions', async ( test('checks actual installed skill bytes and reports stale production separately', async () => { const bytes = Buffer.from(JSON.stringify(manifest)); const lock = Buffer.from(JSON.stringify({ repository: 'stack-sh/docs', revision: 'a'.repeat(40), manifestSha256: hash(bytes) })); - const fetchBytes = async url => url.endsWith('/generated/manifest.json') ? bytes : url.endsWith('/SKILL.md') ? Buffer.from(skill) : lock; + const fetchBytes = async url => url.includes('/git/ref/heads/main') ? Buffer.from(JSON.stringify({ object: { type: 'commit', sha: 'b'.repeat(40) } })) : url.endsWith('/generated/manifest.json') ? bytes : url.endsWith('/SKILL.md') ? Buffer.from(skill) : lock; await auditConsumers(manifest, fetchBytes); await assert.rejects(auditConsumers(manifest, async url => url.endsWith('/SKILL.md') ? Buffer.from('hand edited') : fetchBytes(url)), /CLI skill differs/); await assert.rejects(auditConsumers(manifest, async url => {