Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
Ratchet the env-only rule and close the unguarded step-slicer class (#3809)
Andrew Mikofalvy39994427b8cd
diff --git a/.github/scripts/bug-lane-refusal-payload.test.mjs b/.github/scripts/bug-lane-refusal-payload.test.mjsindex c1fecccf5..953ef34bb 100644--- a/.github/scripts/bug-lane-refusal-payload.test.mjs+++ b/.github/scripts/bug-lane-refusal-payload.test.mjs@@ -1,4 +1,4 @@-import { readFileSync } from 'node:fs';+import { readdirSync, readFileSync } from 'node:fs';import { dirname, join } from 'node:path';import { fileURLToPath } from 'node:url';import { describe, expect, test } from 'vitest';@@ -437,4 +437,115 @@ describe('workflow wiring', () => {expect(bugLane).not.toContain('resolve_paths');expect(bugLaneVerify).not.toContain('resolve_paths');});++ // Pins the guard itself. Every other call site names a step that exists, so+ // the throw branch is otherwise dead code — and a future edit that dropped it+ // (a merge conflict, a simplification) would return this whole describe to+ // passing vacuously with nothing going red. Verifying it by hand once proves+ // it works today; only this proves it still does.+ test('step() throws on a missing name instead of returning a degenerate slice', () => {+ expect(() => step('This step does not exist')).toThrow(/has no step named/);+ });++ /**+ * Every `run:` body in a workflow, block-scalar and inline forms both.+ *+ * Scoped to `run:` rather than matching `${{ inputs.` line-shapes anywhere,+ * because the rule is about reaching a SHELL. A `with:` parameter+ * (`ref: ${{ inputs.stable }}`) is legitimate and a line-shape filter would+ * red-flag it, teaching whoever trips it that the rule is arbitrary.+ */+ const runBodies = (yaml) => {+ const lines = yaml.split('\n');+ const bodies = [];+ for (let i = 0; i < lines.length; i++) {+ const block = /^(\s*)run: [|>]/.exec(lines[i]);+ if (block) {+ const indent = block[1].length;+ const body = [];+ for (let j = i + 1; j < lines.length; j++) {+ if (lines[j].trim() === '') {+ body.push(lines[j]);+ continue;+ }+ if (lines[j].length - lines[j].trimStart().length <= indent) break;+ body.push(lines[j]);+ }+ bodies.push(body.join('\n'));+ continue;+ }+ const inline = /^\s*run: (?![|>]\s*$)(.+)$/.exec(lines[i]);+ if (inline) bodies.push(inline[1]);+ }+ return bodies;+ };++ // THE RATCHET for the env-only convention. Without it the rule lives in a+ // comment: reverting the `Refuse an empty batch` env block to direct+ // `${{ inputs.* }}` splicing left all 1200 tests in this project green, which+ // is how that defect shipped in the first place despite two reviewers naming+ // it. `${{ }}` is substituted as raw text before the shell parses, so a+ // dispatch value carrying `$(...)` executes in a job holding GITHUB_TOKEN and+ // the Slack webhook secrets.+ //+ // Scoped to `inputs.` — the untrusted surface. `steps.*.outputs.*` splicing+ // is deliberately NOT banned: those values are produced by the workflow+ // itself, so a blanket ban would be a materially different and much churnier+ // rule than the one the convention comments actually state.+ test('dispatch inputs reach the shell only through env, never `${{ }}` splicing', () => {+ // Every workflow, not just the lane's two: the convention comment claims to+ // govern publish-linux-repo.yml and point-release.yml as well, and a rule+ // enforced only where it was written is how the next copy escapes it.+ const dir = join(REPO_ROOT, '.github', 'workflows');+ const files = readdirSync(dir).filter((f) => f.endsWith('.yml') || f.endsWith('.yaml'));+ expect(files.length, 'no workflows found to scan').toBeGreaterThan(0);++ for (const file of files) {+ const yaml = readFileSync(join(dir, file), 'utf8');+ const bodies = runBodies(yaml);+ // Catches a walker that SKIPS a block. It cannot catch one that captures+ // a block and TRUNCATES it, since `declared` reads the same `run:`+ // anchor — that half is pinned directly by the walker unit test below,+ // which is the only thing that actually proves depth.+ const declared = yaml.split('\n').filter((l) => /^\s*run: /.test(l)).length;+ expect(bodies.length, `${file}: walker missed a run: block`).toBe(declared);+ // Every spelling, not one literal. `${{inputs.x}}`, a two-space variant+ // and the `github.event.inputs.*` form are all equivalent to Actions and+ // all enable the identical injection — and that last form is a live idiom+ // in this very directory, so it is the likely alternate spelling rather+ // than a hypothetical one.+ const spliced = bodies.filter((b) => /\$\{\{\s*(github\.event\.)?inputs\./.test(b));+ expect(spliced, `${file}: inputs spliced into a run: body`).toEqual([]);+ }+ });++ // Pins runBodies() against a synthetic document, which is the only thing that+ // proves DEPTH. A count check compares the walker to the same `run:` anchor it+ // reads, so a walker truncated to its first line keeps the count intact and+ // the ratchet above goes quiet with a real splice sitting on line two. A+ // corpus sentinel does not close it either: the first line of these bodies is+ // the shell preamble, so a one-line capture still matches it.+ test('runBodies captures a whole block, neither truncated nor bled into the next step', () => {+ const doc = [+ 'jobs:',+ ' j:',+ ' steps:',+ ' - name: one',+ ' run: |',+ ' first',+ ' second',+ ' third',+ ' - name: two',+ ' run: echo inline',+ '',+ ].join('\n');+ const bodies = runBodies(doc);+ expect(bodies).toHaveLength(2);+ expect(bodies[0]).toContain('first');+ // The assertion a truncating walker fails.+ expect(bodies[0]).toContain('third');+ // The assertion an over-capturing walker fails.+ expect(bodies[0]).not.toContain('- name: two');+ expect(bodies[1]).toBe('echo inline');+ });});diff --git a/.github/scripts/build-smoke-alert-payload.test.mjs b/.github/scripts/build-smoke-alert-payload.test.mjsindex 03be070f9..57b436e92 100644--- a/.github/scripts/build-smoke-alert-payload.test.mjs+++ b/.github/scripts/build-smoke-alert-payload.test.mjs@@ -21,8 +21,16 @@ const desktopRelease = readFileSync(* The fixed-length slices this replaces (400 / 4000 / 5000 chars) could run* past the step and assert against a neighbouring step's shell.*/-const alertStep = () => {- const rest = desktopRelease.slice(desktopRelease.indexOf('- name: Alert on a blocked release'));+const alertStep = (source = desktopRelease) => {+ // Throw rather than return a degenerate slice. An unguarded `indexOf` yields+ // -1, `slice(-1)` yields the file's last character, and the tests below whose+ // only assertions are `not.toMatch` / `not.toContain` then pass against that+ // one character — so renaming the step turns its own ratchet green instead of+ // red. Measured: renaming it reddens 7 of 30 tests here but leaves `a blocked+ // release never pages Discord`, the pure-negative one, passing.+ const start = source.indexOf('- name: Alert on a blocked release');+ if (start === -1) throw new Error('desktop-release.yml has no "Alert on a blocked release" step');+ const rest = source.slice(start);const end = rest.indexOf('\n - name: ');return end === -1 ? rest : rest.slice(0, end);};@@ -233,6 +241,16 @@ describe('parseArgs', () => {});describe('workflow wiring', () => {+ // Pins the guard in alertStep(). Every other call here reaches a step that+ // exists, so the throw branch is otherwise dead code, and dropping it would+ // silently restore the vacuous pass that `a blocked release never pages+ // Discord` below depends on being impossible.+ test('alertStep() throws on a missing step instead of slicing one character', () => {+ expect(() => alertStep('name: nothing that matches\n')).toThrow(+ /has no "Alert on a blocked release" step/,+ );+ });+test('the alert job fires on a blocked release, never on success', () => {// The blocked-release predicate lives at the ALERT JOB level (a failed// packaging job skips finalize, so a failure()-step inside finalizediff --git a/.github/scripts/evaluate-smoke-alarm.test.mjs b/.github/scripts/evaluate-smoke-alarm.test.mjsindex 32c49f048..95f60aa77 100644--- a/.github/scripts/evaluate-smoke-alarm.test.mjs+++ b/.github/scripts/evaluate-smoke-alarm.test.mjs@@ -239,7 +239,11 @@ describe('buildHistory', () => {),'utf8',);- const step = wf.slice(wf.indexOf('- name: Page the release channel'));+ // Bounded and guarded: an unguarded indexOf yields -1 and slice(-1) is one+ // character, which the two negative assertions below would pass against.+ const pageStart = wf.indexOf('- name: Page the release channel');+ expect(pageStart, 'no "Page the release channel" step').toBeGreaterThan(-1);+ const step = wf.slice(pageStart);expect(step).toContain('SLACK_WEBHOOK_URL');expect(step).not.toContain('DISCORD_WEBHOOK_URL');// Scoped to a `post … Discord` leg, not to the word: the step's commentsdiff --git a/.github/scripts/retry-transient.test.mjs b/.github/scripts/retry-transient.test.mjsindex f3b4811a6..65a0dcf9c 100644--- a/.github/scripts/retry-transient.test.mjs+++ b/.github/scripts/retry-transient.test.mjs@@ -198,11 +198,23 @@ describe('parseArgs', () => {describe('workflow wiring', () => {/** The `- name:` block of a step, up to the next sibling step. */const step = (needle) => {- const rest = desktopRelease.slice(desktopRelease.indexOf(`- name: ${needle}`));+ // Throw rather than slice(-1), which returns the file's last character and+ // makes every negative assertion below pass vacuously on a renamed step.+ const start = desktopRelease.indexOf(`- name: ${needle}`);+ if (start === -1) throw new Error(`desktop-release.yml has no step named ${needle}`);+ const rest = desktopRelease.slice(start);const end = rest.indexOf('\n - name: ');return end === -1 ? rest : rest.slice(0, end);};+ // Pins the guard above. Every other call names a step that exists, so the+ // throw is otherwise dead code and a later simplification drops it silently,+ // returning every negative assertion in this describe to passing against one+ // character.+ test('step() throws on a missing name instead of returning a degenerate slice', () => {+ expect(() => step('This step does not exist')).toThrow(/has no step named/);+ });+// Every platform that packages an installer. The 0.52.2/0.52.3 incident was// Linux; the same un-retried exposure existed on Windows.const PACKAGING_STEPS = [diff --git a/.github/scripts/select-native-config-prebuild.test.mjs b/.github/scripts/select-native-config-prebuild.test.mjsindex aedc58132..6ccf3cec6 100644--- a/.github/scripts/select-native-config-prebuild.test.mjs+++ b/.github/scripts/select-native-config-prebuild.test.mjs@@ -21,7 +21,11 @@ const workflow = (name) => readFileSync(join(REPO_ROOT, '.github', 'workflows',* an assertion cannot run past it into a neighbouring step's shell.*/const stagingStep = (yaml) => {- const rest = yaml.slice(yaml.indexOf('- name: Stage native-config prebuilt binaries'));+ // Throw rather than slice(-1), which returns the file's last character and+ // makes every negative assertion below pass vacuously on a renamed step.+ const start = yaml.indexOf('- name: Stage native-config prebuilt binaries');+ if (start === -1) throw new Error('no "Stage native-config prebuilt binaries" step');+ const rest = yaml.slice(start);const end = rest.indexOf('\n - name: ');return end === -1 ? rest : rest.slice(0, end);};@@ -181,6 +185,17 @@ describe('describeSelectionFailure', () => {});});+describe('step extraction', () => {+ // Pins the guard in stagingStep(). Every other call passes a document that+ // contains the step, so the throw is otherwise dead code and dropping it+ // silently restores the degenerate one-character slice.+ test('stagingStep() throws on a document with no staging step', () => {+ expect(() => stagingStep('name: nothing that matches\n')).toThrow(+ /Stage native-config prebuilt binaries/,+ );+ });+});+describe('git boundary', () => {test('isAncestor reads a non-zero exit as "not an ancestor"', () => {const isAncestor = makeIsAncestor((_cmd, args) => ({diff --git a/.github/scripts/share-contract-reader-gates-shape.test.mjs b/.github/scripts/share-contract-reader-gates-shape.test.mjsindex c0f5c8c50..5b65d99fe 100644--- a/.github/scripts/share-contract-reader-gates-shape.test.mjs+++ b/.github/scripts/share-contract-reader-gates-shape.test.mjs@@ -150,8 +150,13 @@ describe('stable release reader attestation', () => {expect(restore).toContain('.github/composite-actions/share-contract-reader-gate/action.yml');expect(restore).toContain('.github/scripts/probe-share-contract.mjs');expect(restore).toContain('test-support/fixtures/share-url-v1-v2.json');- expect(release.indexOf('- name: Restore reader gate from workflow revision')).toBeLessThan(- release.indexOf('- name: Attest production reader before release'),- );+ // Both resolved before comparing: a missing FIRST step yields -1, and+ // -1 < anyPositiveIndex passes, so renaming it would satisfy this ordering+ // ratchet instead of breaking it.+ const restoreAt = release.indexOf('- name: Restore reader gate from workflow revision');+ const attestAt = release.indexOf('- name: Attest production reader before release');+ expect(restoreAt, 'no "Restore reader gate" step').toBeGreaterThan(-1);+ expect(attestAt, 'no "Attest production reader" step').toBeGreaterThan(-1);+ expect(restoreAt).toBeLessThan(attestAt);});});