Skip to content

Commit 6eac1cb

Browse files
authored
Merge pull request #1198 from cipherstash/ci/impact-gate-corrupt-cache
fix(ci): a corrupt ImpactGate cache for a commit is replaced, not kept
2 parents 141054f + bf87972 commit 6eac1cb

3 files changed

Lines changed: 91 additions & 10 deletions

File tree

‎.github/workflows/impact.yml‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,32 +61,42 @@ jobs:
6161
6262
# The fallback stays within one tool/policy AND target branch. Never use a
6363
# repository-wide fallback: another release branch has different history.
64+
# Saves append the run to the commit's key (see "Save baselines"), so the
65+
# first restore key finds this commit's newest pair before any ancestor's.
66+
# No entry is ever saved under the primary key itself, so it never matches:
67+
# it is kept only to supply cache-primary-key to REFRESH and the save key.
6468
- name: Restore baselines
6569
id: restore
6670
uses: actions/cache/restore@0057852bfaa89a56745cba8c7296529d2fc39830 # v4
6771
with:
6872
path: ${{ env.BASELINE_DIR }}
6973
key: impact-${{ hashFiles('.github/impact-gate/**', 'scripts/impact-gate.py', '.github/workflows/impact.yml') }}-${{ steps.target.outputs.key }}-${{ github.event.pull_request.base.sha || github.sha }}
70-
restore-keys: impact-${{ hashFiles('.github/impact-gate/**', 'scripts/impact-gate.py', '.github/workflows/impact.yml') }}-${{ steps.target.outputs.key }}-
74+
restore-keys: |
75+
impact-${{ hashFiles('.github/impact-gate/**', 'scripts/impact-gate.py', '.github/workflows/impact.yml') }}-${{ steps.target.outputs.key }}-${{ github.event.pull_request.base.sha || github.sha }}-
76+
impact-${{ hashFiles('.github/impact-gate/**', 'scripts/impact-gate.py', '.github/workflows/impact.yml') }}-${{ steps.target.outputs.key }}-
7177
7278
- name: Validate or build baselines
7379
id: prepare
7480
env:
75-
REFRESH: ${{ github.event_name == 'push' && steps.restore.outputs.cache-hit != 'true' }}
81+
# Main refreshes unless it restored a pair saved for this very commit.
82+
REFRESH: ${{ github.event_name == 'push' && !startsWith(steps.restore.outputs.cache-matched-key, format('{0}-', steps.restore.outputs.cache-primary-key)) }}
7683
run: |
7784
args=()
7885
if [[ "$REFRESH" == 'true' ]]; then args+=(--refresh); fi
7986
python scripts/impact-gate.py prepare --base "$BASE_REF" --cache-dir "$BASELINE_DIR" "${args[@]}"
8087
8188
# Any rebuilt pair is saved, including on pull requests: a PR's cache is
8289
# scoped to its own ref, so it cannot replace main's, and later pushes to
83-
# the PR reuse it instead of rebuilding both scopes again.
90+
# the PR reuse it instead of rebuilding both scopes again. Cache keys are
91+
# immutable, so the key names this run: when the pair restored for this
92+
# commit fails validation, its replacement saves beside it rather than
93+
# being refused, and the next run restores the newer one.
8494
- name: Save baselines
8595
if: steps.prepare.outputs.rebuilt == 'true'
8696
uses: actions/cache/save@0057852bfaa89a56745cba8c7296529d2fc39830 # v4
8797
with:
8898
path: ${{ env.BASELINE_DIR }}
89-
key: ${{ steps.restore.outputs.cache-primary-key }}
99+
key: ${{ steps.restore.outputs.cache-primary-key }}-${{ github.run_id }}-${{ github.run_attempt }}
90100

91101
- name: Report pull request impact
92102
if: github.event_name == 'pull_request'

‎docs/plans/2026-10-08-impact-gate-rollout.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ The upstream baseline serializes `_meta` (`tool`, `n`, `head`, `base_ref`) plus
3636

3737
## Actions verification still required
3838

39-
Local calibration and tests cannot verify GitHub cache scoping, cache saves, later PR restores, or the rendered job summary. Any run that rebuilds the pair saves it, pull requests included; a PR's cache is scoped to its own ref, so it cannot replace main's, and it spares later pushes to that PR a full rebuild. Main pushes run in per-SHA concurrency groups so a quick second merge cannot cancel the first before it saves. After the workflow is available in Actions, record links to a cold run, a main run saving the pair, a PR run restoring it, and a PR run reusing its own saved pair. Verify both report sections, policy/version cache invalidation, malformed-pair rebuilding, and explicit seed-only disclosure. Do not mark these checks complete based on local cache simulation.
39+
Local calibration and tests cannot verify GitHub cache scoping, cache saves, later PR restores, or the rendered job summary. Any run that rebuilds the pair saves it, pull requests included; a PR's cache is scoped to its own ref, so it cannot replace main's, and it spares later pushes to that PR a full rebuild. Each save's key ends with its run ID and attempt, because cache keys are immutable: a pair rebuilt after the one restored for the same commit failed validation saves beside it instead of being refused, and the next run restores the newer one. Main pushes run in per-SHA concurrency groups so a quick second merge cannot cancel the first before it saves. After the workflow is available in Actions, record links to a cold run, a main run saving the pair, a PR run restoring it, and a PR run reusing its own saved pair. Verify both report sections, policy/version cache invalidation, malformed-pair rebuilding, and explicit seed-only disclosure. Do not mark these checks complete based on local cache simulation.
4040

4141
## Imported history and rename checks
4242

‎scripts/__tests__/impact-workflow.test.mjs‎

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,20 @@ import { readWorkflow } from './lib/workflows.mjs'
66

77
const workflowPath = '.github/workflows/impact.yml'
88

9+
// The Actions functions these expressions call. hashFiles stands in for the
10+
// policy digest, which is constant within one run of these tests.
11+
const functions = {
12+
startsWith: (value, prefix) =>
13+
String(value).toLowerCase().startsWith(String(prefix).toLowerCase()),
14+
format: (template, ...args) =>
15+
String(template).replace(/\{(\d+)\}/g, (_, index) => String(args[index])),
16+
hashFiles: () => 'policy',
17+
}
18+
919
// Evaluates the small subset of the Actions expression language these guards
10-
// need (context paths, string literals, ==, !=, &&, ||, !), so a test asserts
11-
// what an expression DOES for an event rather than how it is spelled.
20+
// need (context paths, string literals, ==, !=, &&, ||, !, and the functions
21+
// above), so a test asserts what an expression DOES for an event rather than
22+
// how it is spelled.
1223
function evaluate(expression, context) {
1324
const body = String(expression)
1425
.replace(/^\s*\$\{\{\s*/, '')
@@ -24,7 +35,9 @@ function evaluate(expression, context) {
2435
)
2536
.replace(/==/g, '===')
2637
.replace(/!===/g, '!==')
27-
return new Function(`return (${js})`)()
38+
return new Function(...Object.keys(functions), `return (${js})`)(
39+
...Object.values(functions),
40+
)
2841
}
2942

3043
function interpolate(template, context) {
@@ -80,13 +93,16 @@ describe('advisory change impact', () => {
8093
expect(restore.with.key).toContain(
8194
'github.event.pull_request.base.sha || github.sha',
8295
)
83-
expect(restore.with['restore-keys']).toBe(
96+
// This commit's run-suffixed saves first, then the target branch's
97+
// ancestors — never a broader prefix.
98+
expect(restore.with['restore-keys'].trim().split('\n')).toEqual([
99+
`${restore.with.key}-`,
84100
restore.with.key.replace(
85101
// biome-ignore lint/suspicious/noTemplateCurlyInString: literal Actions expression
86102
'-${{ github.event.pull_request.base.sha || github.sha }}',
87103
'-',
88104
),
89-
)
105+
])
90106
const save = job.steps.find((step) =>
91107
step.uses?.startsWith('actions/cache/save@'),
92108
)
@@ -109,6 +125,61 @@ describe('advisory change impact', () => {
109125
expect(save.with.path).toBe(restore.with.path)
110126
})
111127

128+
it('replaces a corrupt same-commit cache instead of colliding with its key', () => {
129+
// A cache key is immutable. When the entry saved for a commit fails
130+
// validation, prepare rebuilds; saving under that same key is refused
131+
// ("Unable to reserve cache"), so the bad entry stayed and every later run
132+
// for the commit rebuilt again. Each save now takes a run-unique key that
133+
// the next run for the commit restores ahead of any ancestor's.
134+
const job = readWorkflow(workflowPath).jobs.impact
135+
const restore = job.steps.find((step) =>
136+
step.uses?.startsWith('actions/cache/restore@'),
137+
)
138+
const prepare = job.steps.find((step) => step.id === 'prepare')
139+
const save = job.steps.find((step) =>
140+
step.uses?.startsWith('actions/cache/save@'),
141+
)
142+
const run = (event, runId, attempt, restored) => {
143+
const context = {
144+
github: { ...event.github, run_id: runId, run_attempt: attempt },
145+
steps: { target: { outputs: { key: 'main' } } },
146+
}
147+
const primary = interpolate(restore.with.key, context)
148+
context.steps.restore = {
149+
outputs: {
150+
'cache-primary-key': primary,
151+
'cache-matched-key': restored ?? '',
152+
'cache-hit': String(restored === primary),
153+
},
154+
}
155+
return {
156+
primary,
157+
restoreKeys: interpolate(restore.with['restore-keys'], context)
158+
.split('\n')
159+
.map((line) => line.trim())
160+
.filter(Boolean),
161+
refresh: String(interpolate(prepare.env.REFRESH, context)),
162+
saved: interpolate(save.with.key, context),
163+
}
164+
}
165+
166+
// A rerun of the same commit's push, restoring the first attempt's entry.
167+
const first = run(pushEvent('c0ffee'), '41', '1')
168+
const rerun = run(pushEvent('c0ffee'), '41', '2', first.saved)
169+
expect(rerun.saved).not.toBe(first.saved)
170+
// A later run for the commit finds its newest save before any ancestor's.
171+
expect(first.saved.startsWith(rerun.restoreKeys[0])).toBe(true)
172+
expect(rerun.restoreKeys.at(-1)).not.toBe(rerun.restoreKeys[0])
173+
// A same-commit entry is reused without a forced rebuild; an ancestor's
174+
// fallback, or nothing, still refreshes main.
175+
expect(rerun.refresh).toBe('false')
176+
const ancestor = run(pushEvent('ancest'), '40', '1').saved
177+
expect(run(pushEvent('c0ffee'), '42', '1', ancestor).refresh).toBe('true')
178+
expect(run(pushEvent('c0ffee'), '42', '1').refresh).toBe('true')
179+
// Pull requests never force a rebuild.
180+
expect(run(pullRequestEvent('feed'), '43', '1').refresh).toBe('false')
181+
})
182+
112183
it('never lets one main push cancel another before its baselines are saved', () => {
113184
// F3: a shared `impact-refs/heads/main` group with cancel-in-progress
114185
// cancelled a merge's run before "Save main baselines", so that SHA never

0 commit comments

Comments
 (0)