Repository navigation
test(e2e): laisser un modèle de décision choisir les parcours hors zone dans la porte PR #640
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8616886
test(e2e): laisser un modèle de décision choisir les parcours hors zo…
claude cbcbe7e
test(e2e): borner le modèle de décision à 2 minutes et garder un éche…
claude 7cefceb
test(e2e): passer le filtre de décision sur OpenAI et ne pas reprendr…
claude e6a6db8
Merge origin/master (TesterArmy e2e 0.22.0) into claude/project-threa…
claude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import type { TestFilter, TestFilterCandidate, TestFilterContext } from 'e2e'; | ||
|
|
||
| export const FAILURE_TTL_MS: number; | ||
|
|
||
| export function verdictKey(input: { | ||
| model: string; | ||
| base: string; | ||
| head: string; | ||
| files: readonly string[]; | ||
| tests: readonly string[]; | ||
| }): string; | ||
|
|
||
| export function decisionVerdict( | ||
| asked: readonly TestFilterCandidate[], | ||
| context: TestFilterContext, | ||
| options?: { | ||
| load?: () => Promise<TestFilter>; | ||
| git?: (file: string, args: readonly string[], options: { cwd: string; encoding: 'utf8' }) => string; | ||
| now?: () => number; | ||
| }, | ||
| ): Promise<{ exclude: Record<string, string>; note?: string }>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /** | ||
| * The decision model's verdict on the parcours the zones leave out, through | ||
| * the TesterArmy Filters API (`@e2e-dev/decision`, OpenAI's Decisions API). | ||
| * run.mjs calls the filters twice, `e2e list` then `e2e run`, and | ||
| * checks that the run selected exactly what the list did, so the first answer | ||
| * for a commit and a test set is kept in the clone's git directory and reused. | ||
| * A rerun of verify:pr on the same commit asks nothing either. A failure is | ||
| * kept for a few minutes only: long enough for the run to fall back to the | ||
| * zones as its list did, short enough for a later verify:pr to ask again. | ||
| */ | ||
| import { createHash } from 'node:crypto'; | ||
| import { execFileSync } from 'node:child_process'; | ||
| import { mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs'; | ||
| import { dirname, join } from 'node:path'; | ||
| import { DECISION_MODEL } from './zones.mjs'; | ||
|
|
||
| /** @param {{ model: string, base: string, head: string, files: readonly string[], tests: readonly string[] }} input */ | ||
| export function verdictKey({ model, base, head, files, tests }) { | ||
| return createHash('sha256').update(JSON.stringify({ model, base, head, files: [...files].sort(), tests: [...tests].sort() })).digest('hex'); | ||
| } | ||
|
|
||
| function gitRead(git, root, args) { | ||
| return git('git', args, { cwd: root, encoding: 'utf8' }).trim(); | ||
| } | ||
|
|
||
| /** How long a failed answer stands for the same question. */ | ||
| export const FAILURE_TTL_MS = 10 * 60_000; | ||
|
|
||
| function readVerdict(file, now) { | ||
| try { | ||
| const kept = JSON.parse(readFileSync(file, 'utf8')); | ||
| if (kept && typeof kept.failed === 'string' && Number.isFinite(kept.at)) | ||
| return now - kept.at < FAILURE_TTL_MS ? kept : null; | ||
| if (kept && typeof kept.exclude === 'object' && kept.exclude !== null && !Array.isArray(kept.exclude) | ||
| && Object.values(kept.exclude).every(reason => typeof reason === 'string')) return kept; | ||
| } catch {} | ||
| return null; | ||
| } | ||
|
|
||
| function keep(file, value) { | ||
| mkdirSync(dirname(file), { recursive: true }); | ||
| writeFileSync(`${file}.${process.pid}`, JSON.stringify(value)); | ||
| renameSync(`${file}.${process.pid}`, file); | ||
| } | ||
|
|
||
| async function loadDecisionFilter() { | ||
| const [{ decisionFilter }, { openai }] = await Promise.all([import('@e2e-dev/decision'), import('@ai-sdk/openai')]); | ||
| // Reads OPENAI_API_KEY. The diff and the journey files go to OpenAI's API. | ||
| return decisionFilter({ model: openai.decisionModel(DECISION_MODEL) }); | ||
| } | ||
|
|
||
| /** | ||
| * Asks the decision filter about `asked`, or reuses its answer for the same | ||
| * commit, base, changed files and tests. Throws when the model cannot answer | ||
| * at all (no route, every call failed, past the caller's deadline); the caller | ||
| * then keeps the zones alone. An interrupted run keeps no failure. | ||
| * @param {readonly import('e2e').TestFilterCandidate[]} asked | ||
| * @param {import('e2e').TestFilterContext} context | ||
| */ | ||
| export async function decisionVerdict(asked, context, { load = loadDecisionFilter, git = execFileSync, now = Date.now } = {}) { | ||
| const root = context.changes.root; | ||
| const key = verdictKey({ | ||
| model: DECISION_MODEL, base: context.changes.base, head: gitRead(git, root, ['rev-parse', 'HEAD']), | ||
| files: context.changes.files, tests: asked.map(test => test.id), | ||
| }); | ||
| const file = join(gitRead(git, root, ['rev-parse', '--path-format=absolute', '--git-common-dir']), 'verify-cache', 'e2e-decision', `${key}.json`); | ||
| const cached = readVerdict(file, now()); | ||
| if (cached?.failed !== undefined) throw new Error(`${cached.failed} (echec repris)`); | ||
| if (cached) return { exclude: cached.exclude, note: ['verdict repris', cached.note].filter(Boolean).join(', ') }; | ||
| let verdict; | ||
| try { | ||
| verdict = await (await load()).filter(asked, context); | ||
| } catch (error) { | ||
| const interrupted = context.signal.aborted && context.signal.reason?.name !== 'TimeoutError'; | ||
| if (!interrupted) keep(file, { failed: (error instanceof Error ? error.message : String(error)).split('\n')[0], at: now() }); | ||
| throw error; | ||
| } | ||
| const kept = { exclude: { ...verdict.exclude }, ...(verdict.note ? { note: verdict.note } : {}) }; | ||
| keep(file, kept); | ||
| return kept; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { execFileSync } from 'node:child_process'; | ||
| import { existsSync, mkdirSync, mkdtempSync, readdirSync, realpathSync, rmSync, writeFileSync } from 'node:fs'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { join } from 'node:path'; | ||
| import { test } from 'node:test'; | ||
| import { decisionFilter } from '@e2e-dev/decision'; | ||
| import { FAILURE_TTL_MS, decisionVerdict, verdictKey } from './decision-verdict.mjs'; | ||
|
|
||
| const git = (cwd, ...args) => execFileSync('git', args, { cwd, encoding: 'utf8' }).trim(); | ||
|
|
||
| /** A repository whose last commit changes one app file the two journeys may or may not reach. */ | ||
| function repository() { | ||
| const root = realpathSync(mkdtempSync(join(tmpdir(), 'decision-verdict-'))); | ||
| git(root, 'init', '-q', '-b', 'master'); | ||
| git(root, 'config', 'user.email', 'test@example.com'); | ||
| git(root, 'config', 'user.name', 'test'); | ||
| mkdirSync(join(root, 'tests')); | ||
| mkdirSync(join(root, 'components')); | ||
| writeFileSync(join(root, 'tests/today.web.e2e.ts'), "test('today shows the moon', async () => {});\n"); | ||
| writeFileSync(join(root, 'tests/billing.web.e2e.ts'), "test('billing opens the paywall', async () => {});\n"); | ||
| writeFileSync(join(root, 'components/MoonCard.tsx'), 'export const phase = 1;\n'); | ||
| git(root, 'add', '.'); | ||
| git(root, 'commit', '-q', '-m', 'base'); | ||
| const base = git(root, 'rev-parse', 'HEAD'); | ||
| writeFileSync(join(root, 'components/MoonCard.tsx'), 'export const phase = 2;\n'); | ||
| git(root, 'commit', '-qam', 'change'); | ||
| return { root, base }; | ||
| } | ||
|
|
||
| const tests = [ | ||
| { id: 'tests/today.web.e2e.ts::today shows the moon', file: 'tests/today.web.e2e.ts', titlePath: ['today shows the moon'], tags: [] }, | ||
| { id: 'tests/billing.web.e2e.ts::billing opens the paywall', file: 'tests/billing.web.e2e.ts', titlePath: ['billing opens the paywall'], tags: [] }, | ||
| ]; | ||
|
|
||
| function context(root, base) { | ||
| return { | ||
| projectRoot: root, changes: { base, root, files: ['components/MoonCard.tsx'] }, | ||
| signal: new AbortController().signal, log() {}, redact: text => text, | ||
| }; | ||
| } | ||
|
|
||
| /** An offline decision model: the moon journey is likely affected, the billing one is not. */ | ||
| function fakeModel(calls, fail = false) { | ||
| return { | ||
| specificationVersion: 'v4', provider: 'fake', modelId: 'fake-decisions', supportedQuestionTypes: ['boolean'], | ||
| async doDecide({ questions }) { | ||
| calls.push(Object.keys(questions).length); | ||
| if (fail) throw new Error('401 invalid key'); | ||
| return { | ||
| answers: Object.fromEntries(Object.entries(questions).map(([id, question]) => | ||
| [id, { type: 'boolean', probability: String(question.instructions).includes('billing') ? 0.05 : 0.9 }])), | ||
| usage: { inputTokens: 100, outputTokens: 0 }, warnings: [], | ||
| }; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| test('the real decision filter leaves out the journey the change cannot reach, and the verdict is kept for the run', async () => { | ||
| const { root, base } = repository(); | ||
| try { | ||
| const calls = []; | ||
| let loads = 0; | ||
| const load = async () => { loads += 1; return decisionFilter({ model: fakeModel(calls) }); }; | ||
| const first = await decisionVerdict(tests, context(root, base), { load }); | ||
| assert.deepEqual(Object.keys(first.exclude), ['tests/billing.web.e2e.ts::billing opens the paywall']); | ||
| assert.match(first.exclude['tests/billing.web.e2e.ts::billing opens the paywall'], /5% likely affected/); | ||
| assert.equal(loads, 1); | ||
| assert.equal(calls.length, 1); | ||
| // `e2e run` after `e2e list`: same commit, change and tests, so the same answer and no call. | ||
| const second = await decisionVerdict(tests, context(root, base), { load }); | ||
| assert.deepEqual(second.exclude, first.exclude); | ||
| assert.match(second.note, /^verdict repris/); | ||
| assert.equal(loads, 1); | ||
| // Another test set is another question. | ||
| await decisionVerdict(tests.slice(0, 1), context(root, base), { load }); | ||
| assert.equal(loads, 2); | ||
| assert.equal(readdirSync(join(root, '.git/verify-cache/e2e-decision')).length, 2); | ||
| } finally { rmSync(root, { recursive: true, force: true }); } | ||
| }); | ||
|
|
||
| test('a model that cannot answer throws, and the run falls back as its list did until the failure expires', async () => { | ||
| const { root, base } = repository(); | ||
| try { | ||
| const calls = []; | ||
| let clock = 1_000_000; | ||
| const now = () => clock; | ||
| const load = async () => decisionFilter({ model: fakeModel(calls, true) }); | ||
| await assert.rejects(decisionVerdict(tests, context(root, base), { load, now }), /401 invalid key/); | ||
| assert.equal(calls.length, 1); | ||
| // `e2e run` right after: the same failure, without asking again. | ||
| clock += FAILURE_TTL_MS - 1; | ||
| await assert.rejects(decisionVerdict(tests, context(root, base), { load, now }), /401 invalid key \(echec repris\)/); | ||
| assert.equal(calls.length, 1); | ||
| // A later verify:pr asks again. | ||
| clock += 1; | ||
| await assert.rejects(decisionVerdict(tests, context(root, base), { load, now }), /^Error: 401 invalid key$/); | ||
| assert.equal(calls.length, 2); | ||
| } finally { rmSync(root, { recursive: true, force: true }); } | ||
| }); | ||
|
|
||
| test('an interrupted run keeps no failure', async () => { | ||
| const { root, base } = repository(); | ||
| try { | ||
| const interrupt = new AbortController(); | ||
| interrupt.abort(new Error('SIGINT')); | ||
| const load = async () => ({ name: 'decision', filter: async () => { throw new Error('aborted'); } }); | ||
| await assert.rejects(decisionVerdict(tests, { ...context(root, base), signal: interrupt.signal }, { load }), /aborted/); | ||
| assert.equal(existsSync(join(root, '.git/verify-cache/e2e-decision')), false); | ||
| } finally { rmSync(root, { recursive: true, force: true }); } | ||
| }); | ||
|
|
||
| test('a new commit, base, change or model is a new verdict', () => { | ||
| const input = { model: 'gpt-6-luna', base: 'a', head: 'b', files: ['x', 'y'], tests: ['t1', 't2'] }; | ||
| const key = verdictKey(input); | ||
| assert.equal(verdictKey({ ...input, files: ['y', 'x'], tests: ['t2', 't1'] }), key); | ||
| for (const change of [{ head: 'c' }, { base: 'c' }, { files: ['x'] }, { tests: ['t1'] }, { model: 'gpt-6-sol' }]) | ||
| assert.notEqual(verdictKey({ ...input, ...change }), key, JSON.stringify(change)); | ||
| }); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On a dirty checkout, editing an already-modified file leaves HEAD, the changed-path list, and the test IDs unchanged, so a local rerun reuses the previous verdict even though the model's input diff has changed. For example, after an unrelated edit causes a billing journey to be excluded, a subsequent billing-breaking edit to that same file still excludes it without consulting the model. The local runner explicitly supports working-tree changes (
run.mjs,changedFilesSince). Hash the actual diff and relevant journey contents, or bypass persistent verdict reuse for dirty inputs.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Traced, and I don't think this reproduces, so no change here.
The model's input is fixed by what the key already holds.
@e2e-dev/decision(changeDiffindist/filter.js) diffsbase..HEADonly, so the committed diff is fixed byhead. A file that only the working tree changes reaches the model by name alone ("not committed, contents not read"). Those names arechanges.files, which the key also covers.The journey files the model reads are the asked tests' files. A test whose file or setup file is in
changes.files(uncommitted edits included) runs without asking (touchedBy), so it is never inasked. Every asked file therefore matches itsHEADcontents, whichheadcovers.In the example given, a second edit to an already-modified billing journey leaves the file in
changes.files. The billing tests then run without asking, and the cached verdict is not used for them. The same holds for a second edit to an uncommitted app file: the model would only see that file's name again.Generated by Claude Code