mirror of
https://github.com/stablyai/orca.git
synced 2026-09-28 16:02:45 +00:00
* fix(skills): advance the release ledger at the cut so shipped revisions freeze #10340 made the released-skill registry a function of the committed ledger instead of a git tag walk, and #10460 reverted the cut step that advances that ledger because it violated the #9119 contract (a version-only cut must not regenerate or stage the content-addressed skill artifacts). Both were right; the result is a ledger that never advances. generate-skill-bundle-manifest.mjs:390 derives releasedCount solely from release-mapping.json and :461 assigns a changed skill releaseRevision = releasedCount + 1, while :518 protects only committedReleasedCounts[name] — so index releasedCount is unprotected. A tag ships that tail revision, nothing records it, and the next skill change rebuilds the same revision number over different bytes. Installs carrying the shipped digest then match no snapshot and degrade to unrecognized, which cannot be updated. Restore the advance in a form the #9119 contract can keep enforcing: --release now verifies that current-manifest.json and snapshot-registry.json already match the ref being tagged, appends the mapping row, and writes only release-mapping.json. The cut stages just that file, so it still cannot move a content-addressed artifact — the failure #9119 guarded against — and now fails loudly instead of recording a revision the tag does not ship. The contract test is narrowed to match: it asserts the cut runs --release (never --write) and stages exactly package.json and release-mapping.json. * test(release-cut): close the staging bypasses the narrowed gate left open The narrowed contract test anchored its `git add` scan to line start and only inspected staged paths, so three ways to reintroduce #9119 stayed green: a `git add` chained after `&&`, a write that never calls `git add` at all, and `pnpm run generate:skill-bundle-manifest` — the package.json alias for `--write`, which the hyphenated ban never matched. That last one also passed the pre-#10460 assertions, so it was never covered. Drop the anchor, require every `resources/skills` mention in the step to be exactly what is staged, and ban the alias and `commit -a`. Comments are stripped first so prose cannot trip a ban. Verified each bypass fails and the real workflow passes. * fix(release-cut): make the new provenance failure actionable to an operator Verifying the content-addressed artifacts is the only new way the cut can block, and it fails inside a step named "Bump package.json and tag" with a lint-shaped message. That names the files and the command but not the two things the operator needs: the regeneration has to land on main, and the cut is safe to re-run afterwards. Say so. Also pin down why assertReleasedHistoryPreserved takes the pre-append mapping. It pairs with artifacts.releasedSnapshotCounts, which seeding fixed before the row existed; handing it the post-append mapping makes every cut throw "Released snapshot history is incomplete", which points at tag fetching rather than the real cause. Nothing enforces the pairing. * test(release-cut): gate the whole cut job, not just the bump step Round-2 review defeated the previous gate twice, both proved by running the full contract file green with #9119 reintroduced. Every step in the cut job shares one workspace and one index, but the contract test only inspected `Bump package.json and tag`. A step inserted earlier could run --write and `git add resources/skills`, and the bump step's own commit swept it into the version commit and the tag. Assert job-wide instead: only the bump step may name the directory, and no step may regenerate under either the flag or its package.json alias. That lives in the generator suite because the contract file is at its max-lines cap. Two regexes were also evadable. The mention scan required a trailing slash, so a path held in a variable was invisible; it now matches the directory itself. The `commit -a` ban matched nothing at all — `commit\s` ate the only separator, so `-a`, `-am`, and `--all` all survived while only a trailing `-a` was caught. `--allow-empty` stays allowed. * fix(release-cut): assert the index, not the workflow text, before committing Round-3 review defeated the job-wide grep three ways, each proved by running both test files green with #9119 reintroduced into the tagged commit: an `env:` block holding `--write` and `resources/skills`, a composite action whose steps the workflow never spells out, and plain shell concatenation (`root=resources; leaf=skills`). Grepping shell source for path literals is inherently evadable, and the previous fix only relocated round-2's variable-indirection hole one step over. Move the invariant to where it cannot be dodged: immediately before committing, the cut diffs its own index and refuses anything that is not package.json or the release-mapping row. That does not care which step staged what, or how the path was spelled. The workflow grep stays as a cheap tripwire for literal spellings, now paired with a positive assertion that the index guard exists and precedes the commit — indirection cannot hide a missing guard. Mention matching dedupes and trims quotes, since the guard names the row a second time. * fix(release-cut): match the staged-path allowlist literally `grep -vx` treats its patterns as regexes, so the `.` in `package.json` matched any character: a staged `packageXjson` or a `resources/skills/release-mappingXjson` was silently accepted by the index guard. Verified both slip through `-vx` and are caught by `-vxF`. Exercised the guard against a legitimate cut, an empty index, a staged content-addressed artifact, paths containing a space and a non-ASCII character (git quotes the latter, so it fails closed), and a staged deletion. Only the two allowed paths pass. * test(release-cut): assert the index guard aborts, not just that it exists The positive assertion pinned the guard's shape and its position before the commit, but not its effect: replacing `exit 1` with `:` left both test files green while the cut logged the error and shipped the artifact anyway. That is the same failure this whole gate keeps having — asserting the shape of a defense rather than what it does. Pin the abort too. Verified the neutered guard now fails the suite. * test(release-cut): scope the abort check and catch clustered commit flags Two holes in the guards this PR added, both in the same shape-not-effect class the previous commit was meant to close. The abort assertion's lazy match was not scoped to the guard's own block, so it could borrow an `exit 1` from any later `if ... fi` in the step. Degrading the guard to a warning while adding a plausible HEAD precondition left every test green. Stop the match at the guard's `fi`. The `commit -a` ban only matched when `a` led the flag cluster, so `-vam`, `-va`, `-qam` and `-sam` all survived. That matters more than it looks: `commit -a` stages at commit time, after the index guard has already inspected a clean index, so it is the one way to defeat that guard. Match `a` anywhere in a short-flag cluster; `--allow-empty` and `--amend` stay allowed. Verified both mutants now fail. * fix(release-cut): validate the commit, not the index, before tagging The index guard asserted the wrong thing. `git commit` has a family of forms that commit the working tree rather than the index — `-a`, `-i`, `--only`, and a bare pathspec — so a rogue earlier step could leave regenerated artifacts unstaged and any of those forms would carry them into the tagged commit while the guard saw a clean index and passed. Reproduced end to end: `git commit -i resources` put current-manifest.json and snapshot-registry.json in the tag with all gates green, and `--only resources` additionally dropped package.json from the tag. Banning those flags one by one is the same enumeration game the earlier rounds kept losing. Assert the outcome instead: after committing and before tagging, diff-tree HEAD and refuse anything that is not package.json or the release-mapping row. That is indifferent to which step staged what and to how the commit was spelled. Verified the whole family is now blocked (-i, --only, -a, -am, -vam, pathspec, and an alias expanding to `commit -i`), that a stock commit and an --allow-empty re-cut still pass, and that deleting, neutering, un-anchoring, or relocating the guard each fails the suite. * fix(release-cut): make the commit guard fail closed on a merge commit Plain `git diff-tree` prints nothing for a merge commit, so the guard would have passed silently instead of failing closed — the one direction that matters on a release path. `-m --first-parent` reports the diff against the first parent; verified byte-identical output for an ordinary commit and still empty for the `--allow-empty` re-cut, so nothing else changes. Not reachable today (nothing in the cut job creates a merge, and npm version has no lifecycle hooks defined), but the failure mode is a guard that looks like it ran. Pin the flags in the assertion too, so neither dropping -m nor slipping in a `--diff-filter` can weaken it without failing the suite.
690 lines
26 KiB
JavaScript
690 lines
26 KiB
JavaScript
import { createHash } from 'node:crypto'
|
|
import { execFileSync } from 'node:child_process'
|
|
import { constants } from 'node:fs'
|
|
import { access, lstat, mkdir, readFile, readdir, writeFile } from 'node:fs/promises'
|
|
import path from 'node:path'
|
|
import process from 'node:process'
|
|
import { isDeepStrictEqual } from 'node:util'
|
|
|
|
// Why: the three artifacts version independently — bumping one shape must not
|
|
// rewrite the others or bypass the registry's schema-gated append-only guard.
|
|
const CURRENT_MANIFEST_SCHEMA_VERSION = 2
|
|
const SNAPSHOT_REGISTRY_SCHEMA_VERSION = 1
|
|
const RELEASE_MAPPING_SCHEMA_VERSION = 1
|
|
const SCRIPT_DIR = import.meta.dirname
|
|
const REPO_ROOT = path.resolve(SCRIPT_DIR, '..', '..')
|
|
const SKILLS_ROOT = path.join(REPO_ROOT, 'skills')
|
|
const OUTPUT_ROOT = path.join(REPO_ROOT, 'resources', 'skills')
|
|
const CURRENT_MANIFEST_PATH = path.join(OUTPUT_ROOT, 'current-manifest.json')
|
|
const SNAPSHOT_REGISTRY_PATH = path.join(OUTPUT_ROOT, 'snapshot-registry.json')
|
|
const RELEASE_MAPPING_PATH = path.join(OUTPUT_ROOT, 'release-mapping.json')
|
|
// Why: the manifest and registry are content-addressed — they describe skill
|
|
// bytes. The mapping is provenance: which already-committed revision a tag
|
|
// shipped. A release cut may append the second without regenerating the first.
|
|
const CONTENT_ADDRESSED_PATHS = [CURRENT_MANIFEST_PATH, SNAPSHOT_REGISTRY_PATH]
|
|
const ALL_ARTIFACT_PATHS = [...CONTENT_ADDRESSED_PATHS, RELEASE_MAPPING_PATH]
|
|
|
|
function sha256(bytes) {
|
|
return createHash('sha256').update(bytes).digest('hex')
|
|
}
|
|
|
|
function compareCodeUnits(left, right) {
|
|
return left === right ? 0 : left < right ? -1 : 1
|
|
}
|
|
|
|
function gitObjectSha(kind, bytes) {
|
|
return createHash('sha1').update(`${kind} ${bytes.length}\0`).update(bytes).digest()
|
|
}
|
|
|
|
function normalizeText(bytes) {
|
|
const text = new TextDecoder('utf-8', { fatal: true }).decode(bytes)
|
|
return Buffer.from(text.replace(/\r\n/g, '\n').replace(/\r/g, '\n'), 'utf8')
|
|
}
|
|
|
|
function classifyFile(bytes) {
|
|
if (bytes.includes(0)) {
|
|
return 'binary'
|
|
}
|
|
try {
|
|
normalizeText(bytes)
|
|
return 'text'
|
|
} catch {
|
|
return 'binary'
|
|
}
|
|
}
|
|
|
|
function assertSafeRelativePath(relativePath) {
|
|
if (
|
|
path.isAbsolute(relativePath) ||
|
|
relativePath === '..' ||
|
|
relativePath.startsWith(`..${path.sep}`)
|
|
) {
|
|
throw new Error(`Unsafe skill package path: ${relativePath}`)
|
|
}
|
|
}
|
|
|
|
function describeFile(manifestPath, bytes, executable) {
|
|
const classification = classifyFile(bytes)
|
|
const exactSha256 = sha256(bytes)
|
|
const textNormalizedSha256 = classification === 'text' ? sha256(normalizeText(bytes)) : null
|
|
return {
|
|
path: manifestPath,
|
|
size: bytes.length,
|
|
executable,
|
|
classification,
|
|
exactSha256,
|
|
textNormalizedSha256,
|
|
identitySha256: classification === 'text' && !executable ? textNormalizedSha256 : exactSha256,
|
|
gitBlobSha: gitObjectSha('blob', bytes).toString('hex')
|
|
}
|
|
}
|
|
|
|
function gitTreeSha(entries) {
|
|
const root = { directories: new Map(), files: [] }
|
|
for (const entry of entries) {
|
|
const parts = entry.path.split('/')
|
|
const filename = parts.pop()
|
|
let directory = root
|
|
for (const part of parts) {
|
|
let child = directory.directories.get(part)
|
|
if (!child) {
|
|
child = { directories: new Map(), files: [] }
|
|
directory.directories.set(part, child)
|
|
}
|
|
directory = child
|
|
}
|
|
directory.files.push({ filename, ...entry })
|
|
}
|
|
|
|
function hashDirectory(directory) {
|
|
const children = [
|
|
...[...directory.directories].map(([name, child]) => ({
|
|
mode: '40000',
|
|
name,
|
|
hash: hashDirectory(child)
|
|
})),
|
|
...directory.files.map((file) => ({
|
|
mode: file.executable ? '100755' : '100644',
|
|
name: file.filename,
|
|
hash: Buffer.from(file.gitBlobSha, 'hex')
|
|
}))
|
|
].sort((left, right) => {
|
|
const leftName = left.mode === '40000' ? `${left.name}/` : left.name
|
|
const rightName = right.mode === '40000' ? `${right.name}/` : right.name
|
|
return Buffer.from(leftName).compare(Buffer.from(rightName))
|
|
})
|
|
const body = Buffer.concat(
|
|
children.map(({ mode, name, hash }) =>
|
|
Buffer.concat([Buffer.from(`${mode} ${name}\0`, 'utf8'), hash])
|
|
)
|
|
)
|
|
return gitObjectSha('tree', body)
|
|
}
|
|
|
|
return hashDirectory(root).toString('hex')
|
|
}
|
|
|
|
async function collectPackageFiles(packageRoot) {
|
|
const files = []
|
|
const caseFoldedPaths = new Map()
|
|
|
|
async function visit(directory) {
|
|
const entries = await readdir(directory, { withFileTypes: true })
|
|
// Why: build-time Node and packaged Electron may ship different ICU data;
|
|
// package identity order must use the same locale-independent comparison.
|
|
entries.sort((left, right) => compareCodeUnits(left.name, right.name))
|
|
for (const entry of entries) {
|
|
const absolutePath = path.join(directory, entry.name)
|
|
const relativePath = path.relative(packageRoot, absolutePath)
|
|
assertSafeRelativePath(relativePath)
|
|
const manifestPath = relativePath.split(path.sep).join('/')
|
|
const foldedPath = manifestPath.toLocaleLowerCase('en-US')
|
|
const collision = caseFoldedPaths.get(foldedPath)
|
|
if (collision && collision !== manifestPath) {
|
|
throw new Error(`Case-colliding skill paths: ${collision} and ${manifestPath}`)
|
|
}
|
|
caseFoldedPaths.set(foldedPath, manifestPath)
|
|
const fileStat = await lstat(absolutePath)
|
|
if (fileStat.isSymbolicLink()) {
|
|
throw new Error(`Symlink is not allowed in a shipped skill: ${manifestPath}`)
|
|
}
|
|
if (fileStat.isDirectory()) {
|
|
await visit(absolutePath)
|
|
continue
|
|
}
|
|
if (!fileStat.isFile()) {
|
|
throw new Error(`Special file is not allowed in a shipped skill: ${manifestPath}`)
|
|
}
|
|
// Why: Windows observation cannot see execute bits, so an executable file in
|
|
// a shipped skill would misclassify every pristine Windows install as unrecognized.
|
|
if ((fileStat.mode & 0o111) !== 0) {
|
|
throw new Error(`Executable file is not allowed in a shipped skill: ${manifestPath}`)
|
|
}
|
|
files.push(describeFile(manifestPath, await readFile(absolutePath), false))
|
|
}
|
|
}
|
|
|
|
await visit(packageRoot)
|
|
return sortManifestFiles(files)
|
|
}
|
|
|
|
function collectGitSkillTreeEntries(treeSha) {
|
|
const output = execFileSync('git', ['ls-tree', '-r', '-z', treeSha])
|
|
.toString('utf8')
|
|
.split('\0')
|
|
.filter(Boolean)
|
|
const packages = new Map()
|
|
for (const line of output) {
|
|
const match = /^(\d+) (\w+) ([a-f0-9]+)\t(.+)$/.exec(line)
|
|
if (!match) {
|
|
throw new Error(`Unexpected git tree entry in ${treeSha}: ${line}`)
|
|
}
|
|
const [, mode, type, objectSha, sourcePath] = match
|
|
const separator = sourcePath.indexOf('/')
|
|
if (separator <= 0 || separator === sourcePath.length - 1) {
|
|
throw new Error(`Unsupported shipped skill path in ${treeSha}: ${sourcePath}`)
|
|
}
|
|
const name = sourcePath.slice(0, separator)
|
|
const manifestPath = sourcePath.slice(separator + 1)
|
|
const entries = packages.get(name) ?? []
|
|
entries.push({ mode, type, objectSha, manifestPath })
|
|
packages.set(name, entries)
|
|
}
|
|
return packages
|
|
}
|
|
|
|
function readGitBlobs(objectShas) {
|
|
const uniqueShas = [...new Set(objectShas)]
|
|
if (uniqueShas.length === 0) {
|
|
return new Map()
|
|
}
|
|
// Why: released history spans hundreds of tags. Batch mode avoids a Git
|
|
// subprocess per historical file while remaining available on Git 2.25.
|
|
const output = execFileSync('git', ['cat-file', '--batch'], {
|
|
input: `${uniqueShas.join('\n')}\n`,
|
|
maxBuffer: 64 * 1024 * 1024
|
|
})
|
|
const blobs = new Map()
|
|
let offset = 0
|
|
for (const requestedSha of uniqueShas) {
|
|
const headerEnd = output.indexOf(10, offset)
|
|
if (headerEnd < 0) {
|
|
throw new Error(`Missing git cat-file header for ${requestedSha}`)
|
|
}
|
|
const header = output.subarray(offset, headerEnd).toString('utf8')
|
|
const match = /^([a-f0-9]+) blob (\d+)$/.exec(header)
|
|
if (!match || match[1] !== requestedSha) {
|
|
throw new Error(`Unexpected git cat-file header for ${requestedSha}: ${header}`)
|
|
}
|
|
const size = Number(match[2])
|
|
const contentStart = headerEnd + 1
|
|
const contentEnd = contentStart + size
|
|
if (contentEnd >= output.length || output[contentEnd] !== 10) {
|
|
throw new Error(`Truncated git blob for ${requestedSha}`)
|
|
}
|
|
blobs.set(requestedSha, Buffer.from(output.subarray(contentStart, contentEnd)))
|
|
offset = contentEnd + 1
|
|
}
|
|
return blobs
|
|
}
|
|
|
|
function collectGitPackageFiles(treeSha, name, entries, blobs) {
|
|
const caseFoldedPaths = new Map()
|
|
const files = entries.map(({ mode, type, objectSha, manifestPath }) => {
|
|
if (type !== 'blob' || (mode !== '100644' && mode !== '100755')) {
|
|
throw new Error(`Unsupported shipped skill entry in ${treeSha}: ${name}/${manifestPath}`)
|
|
}
|
|
assertSafeRelativePath(manifestPath)
|
|
const foldedPath = manifestPath.toLocaleLowerCase('en-US')
|
|
const collision = caseFoldedPaths.get(foldedPath)
|
|
if (collision && collision !== manifestPath) {
|
|
throw new Error(`Case-colliding skill paths in ${treeSha}: ${collision} and ${manifestPath}`)
|
|
}
|
|
caseFoldedPaths.set(foldedPath, manifestPath)
|
|
const bytes = blobs.get(objectSha)
|
|
if (!bytes) {
|
|
throw new Error(`Missing git blob ${objectSha} for ${name}/${manifestPath}`)
|
|
}
|
|
return describeFile(manifestPath, bytes, mode === '100755')
|
|
})
|
|
// Why: git ls-tree emits git byte-order, not the canonical walk order.
|
|
return sortManifestFiles(files)
|
|
}
|
|
|
|
// Why: snapshot matching compares files by array index, so every producer —
|
|
// working-tree walk, git history, and runtime observation — must emit one
|
|
// canonical order. This mirrors the sorted depth-first filesystem walk.
|
|
function compareManifestPaths(left, right) {
|
|
const leftParts = left.split('/')
|
|
const rightParts = right.split('/')
|
|
const shared = Math.min(leftParts.length, rightParts.length)
|
|
for (let index = 0; index < shared; index += 1) {
|
|
const order = compareCodeUnits(leftParts[index], rightParts[index])
|
|
if (order !== 0) {
|
|
return order
|
|
}
|
|
}
|
|
return leftParts.length - rightParts.length
|
|
}
|
|
|
|
function sortManifestFiles(files) {
|
|
return [...files].sort((left, right) => compareManifestPaths(left.path, right.path))
|
|
}
|
|
|
|
function packageDigest(files) {
|
|
return sha256(
|
|
Buffer.from(
|
|
JSON.stringify(
|
|
files.map((file) => ({
|
|
path: file.path,
|
|
executable: file.executable,
|
|
classification: file.classification,
|
|
identitySha256: file.identitySha256
|
|
}))
|
|
),
|
|
'utf8'
|
|
)
|
|
)
|
|
}
|
|
|
|
function releaseTags() {
|
|
return execFileSync(
|
|
'git',
|
|
['for-each-ref', '--sort=creatordate', '--format=%(refname:short)', 'refs/tags/v*'],
|
|
{ encoding: 'utf8' }
|
|
)
|
|
.split('\n')
|
|
.filter((tag) => /^v\d+\.\d+\.\d+(?:[-.][0-9A-Za-z.-]+)?$/.test(tag))
|
|
}
|
|
|
|
function skillsTreeShasAtRefs(refs) {
|
|
if (refs.length === 0) {
|
|
return []
|
|
}
|
|
const output = execFileSync('git', ['cat-file', '--batch-check=%(objectname) %(objecttype)'], {
|
|
input: `${refs.map((ref) => `${ref}:skills`).join('\n')}\n`,
|
|
encoding: 'utf8'
|
|
})
|
|
const lines = output.trimEnd().split('\n')
|
|
if (lines.length !== refs.length) {
|
|
throw new Error(`Expected ${refs.length} skills tree identities, received ${lines.length}`)
|
|
}
|
|
return lines.map((line, index) => {
|
|
if (line.endsWith(' missing')) {
|
|
return null
|
|
}
|
|
const match = /^([a-f0-9]+) tree$/.exec(line)
|
|
if (!match) {
|
|
throw new Error(`Unexpected skills tree identity at ${refs[index]}: ${line}`)
|
|
}
|
|
return match[1]
|
|
})
|
|
}
|
|
|
|
function buildReleasedHistory() {
|
|
const registry = { schemaVersion: SNAPSHOT_REGISTRY_SCHEMA_VERSION, skills: {} }
|
|
const mapping = { schemaVersion: RELEASE_MAPPING_SCHEMA_VERSION, releases: [] }
|
|
const tags = releaseTags()
|
|
const treeShas = skillsTreeShasAtRefs(tags)
|
|
const distinctTreeShas = [...new Set(treeShas.filter(Boolean))]
|
|
const packagesByTree = new Map(
|
|
distinctTreeShas.map((treeSha) => [treeSha, collectGitSkillTreeEntries(treeSha)])
|
|
)
|
|
const blobs = readGitBlobs(
|
|
[...packagesByTree.values()].flatMap((packages) =>
|
|
[...packages.values()].flatMap((entries) => entries.map((entry) => entry.objectSha))
|
|
)
|
|
)
|
|
let previousSkillsTreeSha = null
|
|
for (const [index, tag] of tags.entries()) {
|
|
const skillsTreeSha = treeShas[index]
|
|
if (!skillsTreeSha || skillsTreeSha === previousSkillsTreeSha) {
|
|
continue
|
|
}
|
|
previousSkillsTreeSha = skillsTreeSha
|
|
const revisions = {}
|
|
const packages = packagesByTree.get(skillsTreeSha)
|
|
if (!packages) {
|
|
throw new Error(`Missing released skill tree ${skillsTreeSha} at ${tag}`)
|
|
}
|
|
for (const name of [...packages.keys()].sort(compareCodeUnits)) {
|
|
const entries = packages.get(name)
|
|
const filesWithGitHashes = collectGitPackageFiles(skillsTreeSha, name, entries, blobs)
|
|
if (!filesWithGitHashes.some((file) => file.path === 'SKILL.md')) {
|
|
continue
|
|
}
|
|
const digest = packageDigest(filesWithGitHashes)
|
|
const snapshots = registry.skills[name] ?? []
|
|
const latest = snapshots.at(-1)
|
|
if (!latest || latest.packageDigest !== digest) {
|
|
const files = filesWithGitHashes.map(({ gitBlobSha: _gitBlobSha, ...file }) => file)
|
|
snapshots.push({
|
|
releaseRevision: (latest?.releaseRevision ?? 0) + 1,
|
|
packageDigest: digest,
|
|
gitTreeSha: gitTreeSha(filesWithGitHashes),
|
|
files
|
|
})
|
|
registry.skills[name] = snapshots
|
|
}
|
|
revisions[name] = snapshots.at(-1).releaseRevision
|
|
}
|
|
if (Object.keys(revisions).length > 0) {
|
|
mapping.releases.push({ appVersion: tag.slice(1), skills: revisions })
|
|
}
|
|
}
|
|
return { registry, mapping }
|
|
}
|
|
|
|
// Why: released history is authoritative committed data, advanced only at
|
|
// release cut. Seeding generation from the committed registry + mapping makes
|
|
// ordinary verify/regeneration a pure function of working-tree bytes, so it
|
|
// never walks git tags — the root cause of recurring lint drift when a clone
|
|
// holds stray, deleted, or fork tags the committed artifacts predate.
|
|
function releasedHistoryFromCommitted(committedRegistry, committedMapping) {
|
|
const registry = { schemaVersion: SNAPSHOT_REGISTRY_SCHEMA_VERSION, skills: {} }
|
|
const releasedSnapshotCounts = {}
|
|
const mapping =
|
|
committedMapping && committedMapping.schemaVersion === RELEASE_MAPPING_SCHEMA_VERSION
|
|
? structuredClone(committedMapping)
|
|
: { schemaVersion: RELEASE_MAPPING_SCHEMA_VERSION, releases: [] }
|
|
if (committedRegistry && committedRegistry.schemaVersion === SNAPSHOT_REGISTRY_SCHEMA_VERSION) {
|
|
const mappedCounts = releasedSnapshotCountsFromMapping(mapping)
|
|
for (const [name, snapshots] of Object.entries(committedRegistry.skills ?? {})) {
|
|
// The committed registry carries at most one unreleased tail beyond the
|
|
// revisions named by the mapping; drop it and recompute it from bytes.
|
|
const releasedCount = mappedCounts?.[name] ?? Math.max(0, snapshots.length - 1)
|
|
registry.skills[name] = snapshots.slice(0, releasedCount)
|
|
releasedSnapshotCounts[name] = releasedCount
|
|
}
|
|
}
|
|
return { registry, mapping, releasedSnapshotCounts }
|
|
}
|
|
|
|
// Why: disaster recovery only. Reconstruct released history from the immutable
|
|
// release tags when the committed ledger must be rebuilt from scratch. Kept off
|
|
// the verify/regenerate path — walking tags there is what coupled lint to the
|
|
// executing clone's tag state and broke it on version bumps, new tags, and
|
|
// stray/deleted local tags.
|
|
function releasedHistoryFromTags() {
|
|
const { registry, mapping } = buildReleasedHistory()
|
|
const releasedSnapshotCounts = Object.fromEntries(
|
|
Object.entries(registry.skills).map(([name, snapshots]) => [name, snapshots.length])
|
|
)
|
|
return { registry, mapping, releasedSnapshotCounts }
|
|
}
|
|
|
|
// Why: release cut is the single authoritative point where working-tree bytes
|
|
// become an immutable released revision. Append one mapping row for the version,
|
|
// mirroring the historical dedupe where consecutive identical skill trees share
|
|
// the earliest release's row.
|
|
function appendReleaseRow(artifacts, version) {
|
|
const appVersion = version.startsWith('v') ? version.slice(1) : version
|
|
const currentRevisions = {}
|
|
for (const skill of artifacts.currentManifest.skills) {
|
|
currentRevisions[skill.name] = skill.releaseRevision
|
|
}
|
|
const releases = artifacts.releaseMapping.releases
|
|
const last = releases.at(-1)
|
|
if (last && isDeepStrictEqual(last.skills, currentRevisions)) {
|
|
return
|
|
}
|
|
// Why: a cut that pushed the version bump to main but died before pushing the
|
|
// tag is re-cut at the same version. If skills changed in between, appending
|
|
// would leave two rows claiming this version and the stale one would name
|
|
// revisions that tag never ships — overwrite, since the tag ships these bytes.
|
|
if (last?.appVersion === appVersion) {
|
|
releases[releases.length - 1] = { appVersion, skills: currentRevisions }
|
|
return
|
|
}
|
|
// Why: an earlier row means re-cutting an already-shipped version, which the
|
|
// cut workflow refuses upstream. Fail rather than corrupt shipped provenance.
|
|
if (releases.some((release) => release.appVersion === appVersion)) {
|
|
throw new Error(`Release mapping already has a row for ${appVersion}.`)
|
|
}
|
|
releases.push({ appVersion, skills: currentRevisions })
|
|
}
|
|
|
|
async function buildArtifacts(releasedHistory) {
|
|
const { registry, mapping, releasedSnapshotCounts } = releasedHistory
|
|
const skillDirectories = (await readdir(SKILLS_ROOT, { withFileTypes: true }))
|
|
.filter((entry) => entry.isDirectory())
|
|
.map((entry) => entry.name)
|
|
.sort(compareCodeUnits)
|
|
const currentSkills = []
|
|
for (const name of skillDirectories) {
|
|
const filesWithGitHashes = await collectPackageFiles(path.join(SKILLS_ROOT, name))
|
|
if (!filesWithGitHashes.some((file) => file.path === 'SKILL.md')) {
|
|
throw new Error(`Skill package ${name} has no top-level SKILL.md`)
|
|
}
|
|
const digest = packageDigest(filesWithGitHashes)
|
|
const snapshots = registry.skills[name] ?? []
|
|
const latest = snapshots.at(-1)
|
|
let snapshot = latest
|
|
if (!latest || latest.packageDigest !== digest) {
|
|
const files = filesWithGitHashes.map(({ gitBlobSha: _gitBlobSha, ...file }) => file)
|
|
snapshot = {
|
|
releaseRevision: (latest?.releaseRevision ?? 0) + 1,
|
|
packageDigest: digest,
|
|
gitTreeSha: gitTreeSha(filesWithGitHashes),
|
|
files
|
|
}
|
|
snapshots.push(snapshot)
|
|
registry.skills[name] = snapshots
|
|
}
|
|
currentSkills.push({
|
|
name,
|
|
sourcePath: `skills/${name}`,
|
|
...snapshot
|
|
})
|
|
}
|
|
return {
|
|
currentManifest: {
|
|
schemaVersion: CURRENT_MANIFEST_SCHEMA_VERSION,
|
|
skills: currentSkills
|
|
},
|
|
snapshotRegistry: registry,
|
|
releaseMapping: mapping,
|
|
releasedSnapshotCounts
|
|
}
|
|
}
|
|
|
|
// Why: released snapshots are the detection ground truth for existing installs,
|
|
// so a generation-logic change must not rewrite them silently. Only the one
|
|
// unreleased working-tree append per skill may change between runs.
|
|
function releasedSnapshotCountsFromMapping(releaseMapping) {
|
|
if (!releaseMapping || releaseMapping.schemaVersion !== RELEASE_MAPPING_SCHEMA_VERSION) {
|
|
return null
|
|
}
|
|
const counts = {}
|
|
for (const release of releaseMapping.releases ?? []) {
|
|
for (const [name, revision] of Object.entries(release.skills ?? {})) {
|
|
counts[name] = Math.max(counts[name] ?? 0, revision)
|
|
}
|
|
}
|
|
return counts
|
|
}
|
|
|
|
function assertReleasedHistoryPreserved(committedRegistry, artifacts, committedReleaseMapping) {
|
|
if (!committedRegistry || committedRegistry.schemaVersion !== SNAPSHOT_REGISTRY_SCHEMA_VERSION) {
|
|
return
|
|
}
|
|
const committedReleasedCounts = releasedSnapshotCountsFromMapping(committedReleaseMapping)
|
|
for (const [name, committedSnapshots] of Object.entries(committedRegistry.skills ?? {})) {
|
|
const releasedCount = artifacts.releasedSnapshotCounts[name] ?? 0
|
|
const regenerated = artifacts.snapshotRegistry.skills[name] ?? []
|
|
const minimumReleasedCount =
|
|
committedReleasedCounts?.[name] ?? Math.max(0, committedSnapshots.length - 1)
|
|
if (releasedCount < minimumReleasedCount) {
|
|
throw new Error(
|
|
`Released snapshot history is incomplete for ${name}. ` +
|
|
'Fetch all release tags before regenerating skill artifacts.'
|
|
)
|
|
}
|
|
const protectedCount = committedReleasedCounts
|
|
? (committedReleasedCounts[name] ?? 0)
|
|
: Math.min(committedSnapshots.length, releasedCount)
|
|
for (let index = 0; index < protectedCount; index += 1) {
|
|
const committed = committedSnapshots[index]
|
|
const rebuilt = regenerated[index]
|
|
if (!rebuilt || !isDeepStrictEqual(rebuilt, committed)) {
|
|
throw new Error(
|
|
`Released snapshot history changed for ${name} at revision ${committed.releaseRevision}. ` +
|
|
'Released snapshots are append-only; a deliberate identity migration must update this check.'
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
async function readCommittedRegistry() {
|
|
try {
|
|
return JSON.parse(await readFile(SNAPSHOT_REGISTRY_PATH, 'utf8'))
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|
|
|
|
async function readCommittedReleaseMapping() {
|
|
try {
|
|
return JSON.parse(await readFile(RELEASE_MAPPING_PATH, 'utf8'))
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|
|
|
|
function serialized(value) {
|
|
return `${JSON.stringify(value, null, 2)}\n`
|
|
}
|
|
|
|
async function writeArtifacts(artifacts, paths = ALL_ARTIFACT_PATHS) {
|
|
const values = new Map([
|
|
[CURRENT_MANIFEST_PATH, artifacts.currentManifest],
|
|
[SNAPSHOT_REGISTRY_PATH, artifacts.snapshotRegistry],
|
|
[RELEASE_MAPPING_PATH, artifacts.releaseMapping]
|
|
])
|
|
await mkdir(OUTPUT_ROOT, { recursive: true })
|
|
await Promise.all(paths.map((filePath) => writeFile(filePath, serialized(values.get(filePath)))))
|
|
}
|
|
|
|
// Why: cutting a release tag adds a trailing mapping row on every checkout at
|
|
// once, before any branch can regenerate. A trailing row whose revisions all
|
|
// equal the current manifest is provably redundant until the next real
|
|
// regeneration (installs of those bytes classify as current and are labeled by
|
|
// the running build), so verify must not fail branches over it.
|
|
function isToleratedReleaseMappingPrefix(committedText, artifacts) {
|
|
let committed
|
|
try {
|
|
committed = JSON.parse(committedText)
|
|
} catch {
|
|
return false
|
|
}
|
|
const derived = artifacts.releaseMapping
|
|
const committedCount = Array.isArray(committed?.releases) ? committed.releases.length : -1
|
|
if (committedCount < 0 || committedCount >= derived.releases.length) {
|
|
return false
|
|
}
|
|
const prefix = {
|
|
schemaVersion: derived.schemaVersion,
|
|
releases: derived.releases.slice(0, committedCount)
|
|
}
|
|
if (committedText !== serialized(prefix)) {
|
|
return false
|
|
}
|
|
const currentRevisions = Object.fromEntries(
|
|
artifacts.currentManifest.skills.map((skill) => [skill.name, skill.releaseRevision])
|
|
)
|
|
return derived.releases
|
|
.slice(committedCount)
|
|
.every((release) => isDeepStrictEqual(release.skills, currentRevisions))
|
|
}
|
|
|
|
async function verifyArtifacts(artifacts, paths = ALL_ARTIFACT_PATHS) {
|
|
const expected = [
|
|
[CURRENT_MANIFEST_PATH, artifacts.currentManifest, null],
|
|
[SNAPSHOT_REGISTRY_PATH, artifacts.snapshotRegistry, null],
|
|
[RELEASE_MAPPING_PATH, artifacts.releaseMapping, isToleratedReleaseMappingPrefix]
|
|
].filter(([filePath]) => paths.includes(filePath))
|
|
const stale = []
|
|
for (const [filePath, value, tolerated] of expected) {
|
|
try {
|
|
await access(filePath, constants.R_OK)
|
|
const committedText = await readFile(filePath, 'utf8')
|
|
if (committedText !== serialized(value) && !tolerated?.(committedText, artifacts)) {
|
|
stale.push(filePath)
|
|
}
|
|
} catch {
|
|
stale.push(filePath)
|
|
}
|
|
}
|
|
if (stale.length > 0) {
|
|
throw new Error(
|
|
`Generated skill artifacts are stale:\n${stale
|
|
.map((filePath) => path.relative(REPO_ROOT, filePath))
|
|
.join('\n')}\nRun pnpm generate:skill-bundle-manifest.`
|
|
)
|
|
}
|
|
}
|
|
|
|
async function main() {
|
|
const argv = process.argv.slice(2)
|
|
const rebuildFromTags = argv.includes('--rebuild-from-tags')
|
|
const releaseIndex = argv.indexOf('--release')
|
|
const releaseVersion = releaseIndex >= 0 ? argv[releaseIndex + 1] : null
|
|
if (releaseIndex >= 0 && !releaseVersion) {
|
|
throw new Error('--release requires a version argument, e.g. --release 1.4.160')
|
|
}
|
|
|
|
const committedRegistry = await readCommittedRegistry()
|
|
const committedMapping = await readCommittedReleaseMapping()
|
|
const releasedHistory = rebuildFromTags
|
|
? releasedHistoryFromTags()
|
|
: releasedHistoryFromCommitted(committedRegistry, committedMapping)
|
|
const artifacts = await buildArtifacts(releasedHistory)
|
|
|
|
if (releaseVersion) {
|
|
// Why: the row names revisions the committed registry must already contain,
|
|
// so proving those artifacts match this ref is what lets the cut record
|
|
// provenance without regenerating them. If regeneration disagrees with what
|
|
// is committed, the row would name a revision this tag does not ship.
|
|
await verifyArtifacts(artifacts, CONTENT_ADDRESSED_PATHS)
|
|
appendReleaseRow(artifacts, releaseVersion)
|
|
}
|
|
|
|
// Why: released snapshots are append-only. The committed registry/mapping are
|
|
// read fresh here so the artifacts (which may have appended a release row) can
|
|
// never alias what we validate against. This mapping must stay the PRE-append
|
|
// one to match artifacts.releasedSnapshotCounts, which seeding fixed before the
|
|
// row existed; the post-append mapping names one more revision than the counts
|
|
// do, which reads as incomplete history and would throw on every cut.
|
|
assertReleasedHistoryPreserved(committedRegistry, artifacts, committedMapping)
|
|
|
|
const shouldWrite = releaseVersion !== null || argv.includes('--write')
|
|
const writePaths = releaseVersion ? [RELEASE_MAPPING_PATH] : ALL_ARTIFACT_PATHS
|
|
await (shouldWrite ? writeArtifacts(artifacts, writePaths) : verifyArtifacts(artifacts))
|
|
}
|
|
|
|
if (process.argv[1] && path.resolve(process.argv[1]) === import.meta.filename) {
|
|
main().catch((error) => {
|
|
console.error(error instanceof Error ? error.message : error)
|
|
process.exitCode = 1
|
|
})
|
|
}
|
|
|
|
export {
|
|
appendReleaseRow,
|
|
assertReleasedHistoryPreserved,
|
|
buildArtifacts,
|
|
buildReleasedHistory,
|
|
classifyFile,
|
|
collectPackageFiles,
|
|
describeFile,
|
|
gitTreeSha,
|
|
isToleratedReleaseMappingPrefix,
|
|
normalizeText,
|
|
packageDigest,
|
|
releasedHistoryFromCommitted,
|
|
sortManifestFiles,
|
|
verifyArtifacts,
|
|
writeArtifacts
|
|
}
|