Repository navigation
feat(signing): add opt-in isolated-worktree exact-tree typecheck verification (S3b Part 2b) #503
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
Closed
Closed
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3f96225
feat(signing): add opt-in isolated-worktree exact-tree typecheck veri…
qnbs feb8504
fix(signing): replace live node_modules reuse with real pnpm material…
qnbs 3f952e5
fix(signing): harden exact-tree install against scripts, corepack, an…
qnbs b09591f
test(signing): add bare-call regressions for the exact-tree lifecycle…
qnbs 41816ad
fix: refuse tracked node_modules and disable hooks in exact-tree veri…
qnbs 7a173fd
fix: match node_modules case-insensitively in tracked-tree refusal
qnbs c19e801
fix: gate compiler trust and pnpm output paths, propagate interruption
qnbs f796bf4
fix: neutralize checkout filters, escaping symlinks, and prune interr…
qnbs b3d8ab5
fix: close compiler-configuration weaponization and replace-ref gaps
qnbs 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
Some comments aren't visible on the classic Files Changed page.
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,70 @@ | ||
| import type { BoundedResult } from './hooks/shared.d.mts'; | ||
| import type { GitOptions, GitResult } from './signing/signing-core.d.mts'; | ||
|
|
||
| // QNBS-v3: absolute correctness check, not a comparison -- distinct vocabulary from WorkingTreeState. | ||
| export type ExactTreeState = 'PASS' | 'FAIL' | 'NOT_APPLICABLE' | 'UNKNOWN'; | ||
|
|
||
| export interface VerifyExactTreeDependencies { | ||
| runBounded?: ( | ||
| command: string, | ||
| args: string[], | ||
| options?: { | ||
| timeoutMs?: number; | ||
| cwd?: string; | ||
| env?: NodeJS.ProcessEnv; | ||
| input?: string; | ||
| shell?: boolean; | ||
| root?: string; | ||
| detached?: boolean; | ||
| }, | ||
| ) => Promise<BoundedResult>; | ||
| runLocalBinaryDetailed?: ( | ||
| binary: string, | ||
| args: string[], | ||
| options?: { root?: string; cwd?: string; timeoutMs?: number }, | ||
| ) => Promise<BoundedResult>; | ||
| // QNBS-v3: a distinct, pre-existing (#494) synchronous/output-capturing wrapper -- not BoundedResult. | ||
| runGit?: (args: string[], options?: GitOptions) => GitResult; | ||
| mkdtempFn?: () => Promise<string>; | ||
| rmFn?: (path: string) => Promise<void>; | ||
| installTimeoutMs?: number; | ||
| tsgoArgs?: string[]; | ||
| repoRoot?: string; | ||
| } | ||
|
|
||
| export function createIsolatedWorktree( | ||
| sha: string, | ||
| repoRoot: string, | ||
| dependencies?: VerifyExactTreeDependencies, | ||
|
qnbs marked this conversation as resolved.
|
||
| ): Promise<{ ok: boolean; path: string | undefined }>; | ||
|
|
||
| export function removeIsolatedWorktree( | ||
| worktreePath: string | undefined, | ||
| repoRoot: string, | ||
| dependencies?: VerifyExactTreeDependencies, | ||
| ): Promise<void>; | ||
|
|
||
| export function installDependencies( | ||
| worktreePath: string, | ||
| dependencies?: VerifyExactTreeDependencies, | ||
| ): Promise<boolean>; | ||
|
|
||
| export function verifyExactTreeTypecheck( | ||
| sha: string, | ||
| repoRoot?: string, | ||
| dependencies?: VerifyExactTreeDependencies, | ||
| ): Promise<ExactTreeState>; | ||
|
|
||
| export function verifyExactTreeForShas( | ||
| shas: string[], | ||
| repoRoot?: string, | ||
| dependencies?: VerifyExactTreeDependencies, | ||
| ): Promise<ExactTreeState>; | ||
|
|
||
| export function resolveRef( | ||
| ref: string, | ||
| repoRoot: string, | ||
| dependencies?: VerifyExactTreeDependencies, | ||
| ): string | null; | ||
|
|
||
| export function main(argv?: string[], dependencies?: VerifyExactTreeDependencies): Promise<void>; | ||
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,171 @@ | ||
| import { mkdtemp as mkdtempAsync, rm as rmAsync } from 'node:fs/promises'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { join, resolve } from 'node:path'; | ||
| import process from 'node:process'; | ||
| import { isMainModule } from './ci-prepush-range-resolver.mjs'; | ||
| import { runBounded, runLocalBinaryDetailed } from './hooks/shared.mjs'; | ||
| import { runGit as defaultRunGit } from './signing/signing-core.mjs'; | ||
|
|
||
| const DEFAULT_TSGO_ARGS = ['--project', 'tsconfig.tsgo.json', '--noEmit', '--checkers', '1']; | ||
|
qnbs marked this conversation as resolved.
Outdated
|
||
| // QNBS-v3: measured ~2m20s for the full project on this hardware with a warm store; 5min gives margin. | ||
| const DEFAULT_INSTALL_TIMEOUT_MS = 300_000; | ||
|
|
||
| // QNBS-v3: lifecycle commands (worktree/install) -- any non-zero or unreadable exit fails outright. | ||
| function boundedCommandFailed(result) { | ||
| return Boolean( | ||
| result.error || result.timedOut || result.interrupted || result.signal || result.status !== 0, | ||
| ); | ||
| } | ||
|
|
||
| // QNBS-v3: tsgo only -- a genuine numeric non-zero status is a real FAIL; a signal/timeout/OOM is UNKNOWN. | ||
| function tsgoResultUnknown(result) { | ||
| return Boolean( | ||
| result.error || result.timedOut || result.interrupted || result.signal || typeof result.status !== 'number', | ||
| ); | ||
| } | ||
|
|
||
| // QNBS-v3: sweeps entries orphaned by a prior crashed/killed run before creating a new one. | ||
| async function pruneStaleWorktrees(repoRoot, dependencies) { | ||
| const runGit = dependencies.runBounded ?? runBounded; | ||
| await runGit('git', ['worktree', 'prune'], { cwd: repoRoot }); | ||
|
qnbs marked this conversation as resolved.
Outdated
|
||
| } | ||
|
qnbs marked this conversation as resolved.
|
||
|
|
||
| export async function createIsolatedWorktree(sha, repoRoot, dependencies) { | ||
| const runGit = dependencies.runBounded ?? runBounded; | ||
|
qnbs marked this conversation as resolved.
Outdated
|
||
| const makeTempDir = | ||
| dependencies.mkdtempFn ?? (() => mkdtempAsync(join(tmpdir(), 'worldscript-exact-tree-'))); | ||
| await pruneStaleWorktrees(repoRoot, dependencies); | ||
| let path; | ||
| try { | ||
| path = await makeTempDir(); | ||
| } catch { | ||
| return { ok: false, path: undefined }; | ||
| } | ||
| const result = await runGit('git', ['worktree', 'add', '--detach', path, sha], { cwd: repoRoot }); | ||
|
qnbs marked this conversation as resolved.
Outdated
|
||
| if (boundedCommandFailed(result)) return { ok: false, path }; | ||
| return { ok: true, path }; | ||
| } | ||
|
|
||
| // QNBS-v3: fail-closed -- git's own removal failing falls back to a raw sweep plus a metadata prune. | ||
| export async function removeIsolatedWorktree(worktreePath, repoRoot, dependencies) { | ||
| if (!worktreePath) return; | ||
| const runGit = dependencies.runBounded ?? runBounded; | ||
| const removeDir = dependencies.rmFn ?? ((path) => rmAsync(path, { recursive: true, force: true })); | ||
| const result = await runGit('git', ['worktree', 'remove', '--force', worktreePath], { | ||
| cwd: repoRoot, | ||
| }); | ||
| if (boundedCommandFailed(result)) { | ||
| try { | ||
| await removeDir(worktreePath); | ||
| } catch { | ||
|
qnbs marked this conversation as resolved.
|
||
| // Best-effort: nothing more can be done from here; the directory is under os.tmpdir(). | ||
| } | ||
| await runGit('git', ['worktree', 'prune'], { cwd: repoRoot }); | ||
| return; | ||
| } | ||
| // QNBS-v3: git leaves the now-empty mkdtemp-created dir in place after remove -- sweep it too. | ||
| try { | ||
| await removeDir(worktreePath); | ||
| } catch { | ||
| // Best-effort: nothing more can be done from here; the directory is under os.tmpdir(). | ||
| } | ||
| } | ||
|
|
||
| // QNBS-v3: real pnpm install, not a hand-reconstructed symlink graph -- offline, fails to UNKNOWN below. | ||
| export async function installDependencies(worktreePath, dependencies) { | ||
| const runPnpm = dependencies.runBounded ?? runBounded; | ||
| const timeoutMs = dependencies.installTimeoutMs ?? DEFAULT_INSTALL_TIMEOUT_MS; | ||
| const result = await runPnpm('pnpm', ['install', '--frozen-lockfile', '--offline'], { | ||
| cwd: worktreePath, | ||
| timeoutMs, | ||
|
qnbs marked this conversation as resolved.
Outdated
qnbs marked this conversation as resolved.
Outdated
qnbs marked this conversation as resolved.
Outdated
|
||
| }); | ||
| return !boundedCommandFailed(result); | ||
| } | ||
|
|
||
| // QNBS-v3: precedence shape mirrors aggregateDiagnosticState, kept separate -- different vocabulary. | ||
| function aggregateExactTreeState(states) { | ||
| const relevant = states.filter((state) => state !== 'NOT_APPLICABLE'); | ||
| if (relevant.length === 0) return 'NOT_APPLICABLE'; | ||
| if (relevant.includes('FAIL')) return 'FAIL'; | ||
| if (relevant.includes('UNKNOWN')) return 'UNKNOWN'; | ||
| return 'PASS'; | ||
| } | ||
|
|
||
| // QNBS-v3: never throws -- an opt-in diagnostic tool must fail closed to UNKNOWN, not crash the caller. | ||
| export async function verifyExactTreeTypecheck(sha, repoRoot = process.cwd(), dependencies = {}) { | ||
| // QNBS-v3: a relative repoRoot would produce ambiguous git-cwd and install-cwd semantics. | ||
| const absoluteRepoRoot = resolve(repoRoot); | ||
| try { | ||
| const created = await createIsolatedWorktree(sha, absoluteRepoRoot, dependencies); | ||
| if (!created.ok) { | ||
| await removeIsolatedWorktree(created.path, absoluteRepoRoot, dependencies); | ||
| return 'UNKNOWN'; | ||
| } | ||
|
|
||
| try { | ||
| if (!(await installDependencies(created.path, dependencies))) return 'UNKNOWN'; | ||
|
|
||
| const runDetailed = dependencies.runLocalBinaryDetailed ?? runLocalBinaryDetailed; | ||
| const tsgoArgs = dependencies.tsgoArgs ?? DEFAULT_TSGO_ARGS; | ||
| const result = await runDetailed('tsgo', tsgoArgs, { | ||
|
qnbs marked this conversation as resolved.
|
||
| root: created.path, | ||
| cwd: created.path, | ||
| }); | ||
|
qnbs marked this conversation as resolved.
|
||
| // QNBS-v3: a signal (including an external OOM kill) must yield UNKNOWN, never a false FAIL. | ||
| if (tsgoResultUnknown(result)) return 'UNKNOWN'; | ||
| return result.status === 0 ? 'PASS' : 'FAIL'; | ||
| } finally { | ||
| await removeIsolatedWorktree(created.path, absoluteRepoRoot, dependencies); | ||
| } | ||
| } catch { | ||
| return 'UNKNOWN'; | ||
| } | ||
| } | ||
|
|
||
| export async function verifyExactTreeForShas(shas, repoRoot = process.cwd(), dependencies = {}) { | ||
| const unique = [...new Set(shas)]; | ||
| if (unique.length === 0) return 'NOT_APPLICABLE'; | ||
| const states = []; | ||
| for (const sha of unique) { | ||
| // QNBS-v3: sequential, never parallel -- bounded resource use on constrained developer hardware. | ||
| states.push(await verifyExactTreeTypecheck(sha, repoRoot, dependencies)); | ||
|
qnbs marked this conversation as resolved.
|
||
| } | ||
| return aggregateExactTreeState(states); | ||
| } | ||
|
|
||
| // QNBS-v3: reuses signing-core's bounded, output-capturing runGit -- runBounded can't capture stdout. | ||
| export function resolveRef(ref, repoRoot, dependencies = {}) { | ||
| const runGit = dependencies.runGit ?? defaultRunGit; | ||
| const result = runGit(['rev-parse', '--verify', `${ref}^{commit}`], { cwd: repoRoot }); | ||
| if (result.error || result.status !== 0) return null; | ||
| return result.stdout.trim(); | ||
| } | ||
|
|
||
| export async function main(argv = process.argv.slice(2), dependencies = {}) { | ||
| const repoRoot = resolve(dependencies.repoRoot ?? process.cwd()); | ||
| const refs = argv.length > 0 ? argv : ['HEAD']; | ||
| const shas = []; | ||
| for (const ref of refs) { | ||
| const sha = resolveRef(ref, repoRoot, dependencies); | ||
| if (!sha) { | ||
| console.error(`[verify-exact-tree] could not resolve ref: ${ref}`); | ||
| process.exitCode = 1; | ||
| return; | ||
| } | ||
| shas.push(sha); | ||
| } | ||
| console.log(`[verify-exact-tree] verifying ${shas.length} commit(s) in isolated worktree(s)...`); | ||
| const state = await verifyExactTreeForShas(shas, repoRoot, dependencies); | ||
| console.log(`[verify-exact-tree] result: ${state}`); | ||
| if (state === 'UNKNOWN') { | ||
| console.log( | ||
| '[verify-exact-tree] could not be established (offline dependency materialization failed, or a worktree/tsgo step failed); required CI remains authoritative.', | ||
| ); | ||
| } else if (state === 'FAIL') { | ||
| console.log('[verify-exact-tree] the exact committed tree does not typecheck in isolation.'); | ||
| process.exitCode = 1; | ||
| } | ||
| } | ||
|
|
||
| // QNBS-v3: guard execution so this module can be imported for testing without running the CLI. | ||
| if (isMainModule(process.argv[1], import.meta.url)) await main(); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.