-
Notifications
You must be signed in to change notification settings - Fork 1.1k
test(ci-workflows): add file-size ratchet gate #4636
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
13 commits
Select commit
Hold shift + click to select a range
45fca0a
test(ci-workflows): add file-size ratchet gate
lidge-jun 913e0d0
refactor(responses,codex): split state and shim behind facades
lidge-jun c63e9ea
refactor(codex): split inject and catalog sync behind facades
lidge-jun 3596985
refactor(codex,providers): split routing and quota behind facades
lidge-jun ce51b3e
fix(responses): restore imports dropped by the state split
lidge-jun a6eb03b
Merge branch 'codex/m2k-l3-state-shim' into codex/m2k-l4-inject-sync
lidge-jun e874436
fix(codex): point split leaves at the real definition modules
lidge-jun a6c6e29
Merge branch 'codex/m2k-l4-inject-sync' into codex/m2k-l5-routing-quota
lidge-jun 48abcfb
fix(codex,providers): restore bindings dropped by the routing and quo…
lidge-jun e443f58
fix(codex): correct the config import depth in routing/active-account
lidge-jun cf10995
Merge pull request #4645 from lidge-jun/codex/m2k-l5-routing-quota
lidge-jun 60d935f
Merge pull request #4643 from lidge-jun/codex/m2k-l4-inject-sync
lidge-jun ccb7454
Merge pull request #4642 from lidge-jun/codex/m2k-l3-state-shim
lidge-jun 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| import { existsSync, readFileSync, writeFileSync } from "node:fs"; | ||
| import { extname, join, resolve } from "node:path"; | ||
|
|
||
| export const THRESHOLD = 2000; | ||
| export const BASELINE_REL = "tests/fixtures/file-size-baseline.json"; | ||
|
|
||
| export const SCAN_EXTENSIONS = new Set([ | ||
| ".ts", | ||
| ".tsx", | ||
| ".js", | ||
| ".cjs", | ||
| ".mjs", | ||
| ".json", | ||
| ".css", | ||
| ".md", | ||
| ".yml", | ||
| ".yaml", | ||
| ".sh", | ||
| ]); | ||
|
|
||
| export const EXCLUDED_PREFIXES = [ | ||
| "devlog/", | ||
| "assets/", | ||
| "docs-site/public/", | ||
| "docs-site/src/assets/", | ||
| "gui/dist/", | ||
| ] as const; | ||
|
|
||
| export const EXCLUDED_EXACT = new Set(["bun.lock", "gui/dist"]); | ||
|
|
||
| export const GENERATED_PATHS = [ | ||
| "scripts/model-metadata.source.json", | ||
| "src/adapters/cursor/gen/agent_pb.ts", | ||
| "gui/src/i18n/de.ts", | ||
| "gui/src/i18n/en.ts", | ||
| "gui/src/i18n/fr.ts", | ||
| "gui/src/i18n/ja.ts", | ||
| "gui/src/i18n/ko.ts", | ||
| "gui/src/i18n/ru.ts", | ||
| "gui/src/i18n/tr.ts", | ||
| "gui/src/i18n/zh.ts", | ||
| "gui/src/i18n/zh-TW.ts", | ||
| "docs-site/src/data/frontier-benchmarks.json", | ||
| ] as const; | ||
|
|
||
| export type Verdict = | ||
| | "NEW_OVERSIZED" | ||
| | "GREW" | ||
| | "SHRANK" | ||
| | "GENERATED" | ||
| | "UNCHANGED" | ||
| | "NEW_OK"; | ||
|
|
||
| export type Baseline = { | ||
| generated: string[]; | ||
| files: Record<string, number>; | ||
| }; | ||
|
|
||
| export type FileSize = { | ||
| path: string; | ||
| lines: number; | ||
| }; | ||
|
|
||
| export type Evaluation = FileSize & { | ||
| verdict: Verdict; | ||
| }; | ||
|
|
||
| export function countLines(text: string): number { | ||
| return text.split("\n").length - (text.endsWith("\n") ? 1 : 0); | ||
| } | ||
|
|
||
| export function isScannedPath(path: string): boolean { | ||
| if (EXCLUDED_EXACT.has(path)) return false; | ||
| if (EXCLUDED_PREFIXES.some((prefix) => path.startsWith(prefix))) return false; | ||
| return SCAN_EXTENSIONS.has(extname(path)); | ||
| } | ||
|
|
||
| export function evaluate(files: FileSize[], baseline: Baseline): Evaluation[] { | ||
| const generated = new Set(baseline.generated); | ||
| return files.map((file) => { | ||
| if (generated.has(file.path)) return { ...file, verdict: "GENERATED" }; | ||
| const cap = baseline.files[file.path]; | ||
| if (cap === undefined) { | ||
| return { ...file, verdict: file.lines >= THRESHOLD ? "NEW_OVERSIZED" : "NEW_OK" }; | ||
| } | ||
| if (file.lines > cap) return { ...file, verdict: "GREW" }; | ||
| if (file.lines < cap) return { ...file, verdict: "SHRANK" }; | ||
| return { ...file, verdict: "UNCHANGED" }; | ||
| }); | ||
| } | ||
|
|
||
| export function isOffender(row: Evaluation): boolean { | ||
| return row.verdict === "NEW_OVERSIZED" || row.verdict === "GREW"; | ||
| } | ||
|
|
||
| export function gitLsFiles(repoRoot: string): string[] { | ||
| const result = Bun.spawnSync(["git", "ls-files"], { cwd: repoRoot }); | ||
| if (result.exitCode !== 0) { | ||
| throw new Error(`git ls-files failed: ${new TextDecoder().decode(result.stderr)}`); | ||
| } | ||
| return new TextDecoder() | ||
| .decode(result.stdout) | ||
| .split("\n") | ||
| .map((line) => line.trim()) | ||
| .filter(Boolean); | ||
| } | ||
|
|
||
| export function scanRepo(repoRoot: string): FileSize[] { | ||
| const out: FileSize[] = []; | ||
| for (const path of gitLsFiles(repoRoot)) { | ||
| if (!isScannedPath(path)) continue; | ||
| out.push({ path, lines: countLines(readFileSync(join(repoRoot, path), "utf8")) }); | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| export function loadBaseline(text: string): Baseline { | ||
| const parsed = JSON.parse(text) as Baseline; | ||
| if ( | ||
| !parsed | ||
| || typeof parsed !== "object" | ||
| || !Array.isArray(parsed.generated) | ||
| || typeof parsed.files !== "object" | ||
| || parsed.files === null | ||
| || Array.isArray(parsed.files) | ||
| ) { | ||
| throw new Error("invalid file-size baseline"); | ||
| } | ||
| return parsed; | ||
| } | ||
|
|
||
| function sortRecord(input: Record<string, number>): Record<string, number> { | ||
| return Object.fromEntries( | ||
| Object.entries(input).sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0)), | ||
| ); | ||
| } | ||
|
|
||
| export function updateBaseline(current: FileSize[], baseline: Baseline, seed: boolean): Baseline { | ||
| const now = new Map(current.map((file) => [file.path, file.lines] as const)); | ||
| const files: Record<string, number> = {}; | ||
| for (const [path, cap] of Object.entries(baseline.files)) { | ||
| const lines = now.get(path); | ||
| if (lines === undefined) continue; | ||
| files[path] = Math.min(cap, lines); | ||
| } | ||
| if (seed) { | ||
| const generated = new Set(baseline.generated); | ||
| for (const [path, lines] of now) { | ||
| if (generated.has(path) || lines < THRESHOLD || files[path] !== undefined) continue; | ||
| files[path] = lines; | ||
| } | ||
| } | ||
| return { generated: [...baseline.generated], files: sortRecord(files) }; | ||
| } | ||
|
|
||
| export function formatOffenders(rows: Evaluation[]): string { | ||
| return rows | ||
| .filter(isOffender) | ||
| .map((row) => `${row.verdict} ${row.path} ${row.lines}`) | ||
| .join("\n"); | ||
| } | ||
|
|
||
| if (import.meta.main) { | ||
| const repoRoot = resolve(import.meta.dir, ".."); | ||
| const baselinePath = join(repoRoot, BASELINE_REL); | ||
| const existed = existsSync(baselinePath); | ||
| const baseline: Baseline = existed | ||
| ? loadBaseline(readFileSync(baselinePath, "utf8")) | ||
| : { generated: [...GENERATED_PATHS], files: {} }; | ||
| const current = scanRepo(repoRoot); | ||
| if (process.argv.includes("--update")) { | ||
| const next = updateBaseline(current, baseline, !existed); | ||
| writeFileSync(baselinePath, `${JSON.stringify(next, null, 2)}\n`); | ||
| console.log(`wrote ${BASELINE_REL} (${Object.keys(next.files).length} caps)`); | ||
| process.exit(0); | ||
| } | ||
| const offenders = evaluate(current, baseline).filter(isOffender); | ||
| if (offenders.length > 0) { | ||
| console.error("file-size ratchet failed:"); | ||
| console.error(formatOffenders(offenders)); | ||
| process.exit(1); | ||
| } | ||
| console.log("file-size ratchet passed"); | ||
| } | ||
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.
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.
If
ratchet:updateis interrupted or the write fails after truncation, this direct overwrite can leave the committed baseline as partial JSON, after which both the test and subsequent update attempts fail inloadBaseline()and require manual recovery. Write a temporary file in the same directory and rename it over the baseline only after the complete write succeeds.AGENTS.md reference: scripts/AGENTS.md:L16-L16
Useful? React with 👍 / 👎.