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
24 changes: 24 additions & 0 deletions .github/workflows/consumer-freshness.yaml
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
71 changes: 71 additions & 0 deletions scripts/consumer-freshness.mjs
Original file line number Diff line number Diff line change
@@ -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);
}
47 changes: 47 additions & 0 deletions scripts/consumer-freshness.test.mjs
Original file line number Diff line number Diff line change
@@ -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/);
});