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 1 commit
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| 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?: GitOptions & { timeoutMs?: number }, | ||
| ) => Promise<GitResult & { timedOut: boolean; interrupted: boolean }>; | ||
|
qnbs marked this conversation as resolved.
Resolved
|
||
| runLocalBinaryDetailed?: ( | ||
| binary: string, | ||
| args: string[], | ||
| options?: { root?: string; cwd?: string; timeoutMs?: number }, | ||
| ) => Promise<GitResult & { timedOut: boolean; interrupted: boolean }>; | ||
| mkdtempFn?: () => Promise<string>; | ||
| rmFn?: (path: string) => Promise<void>; | ||
| symlinkFn?: (target: string, linkPath: string) => void; | ||
| dependencyStateForRef?: (sha: string) => string; | ||
| nodeModulesSource?: string; | ||
| tsgoArgs?: string[]; | ||
| runGitSync?: (args: string[]) => { status: number | null; stdout: string }; | ||
| } | ||
|
|
||
| export function verifyExactTreeTypecheck( | ||
| sha: string, | ||
| repoRoot?: string, | ||
| dependencies?: VerifyExactTreeDependencies, | ||
| ): Promise<ExactTreeState>; | ||
|
|
||
| export function verifyExactTreeForShas( | ||
| shas: string[], | ||
| repoRoot?: string, | ||
| dependencies?: VerifyExactTreeDependencies, | ||
| ): Promise<ExactTreeState>; | ||
|
|
||
| export function main(argv?: string[]): 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,156 @@ | ||
| import { spawnSync } from 'node:child_process'; | ||
|
sourcery-ai[bot] marked this conversation as resolved.
Resolved
|
||
| import { symlinkSync } from 'node:fs'; | ||
| import { mkdtemp as mkdtempAsync, rm as rmAsync } from 'node:fs/promises'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { join } from 'node:path'; | ||
| import process from 'node:process'; | ||
| import { isMainModule } from './ci-prepush-range-resolver.mjs'; | ||
| import { computeDependencyState } from './dependency-state.mjs'; | ||
| import { runBounded, runLocalBinaryDetailed } from './hooks/shared.mjs'; | ||
|
|
||
| const DEFAULT_TSGO_ARGS = ['--project', 'tsconfig.tsgo.json', '--noEmit', '--checkers', '1']; | ||
|
qnbs marked this conversation as resolved.
Resolved
|
||
|
|
||
| // 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.
Resolved
|
||
| } | ||
|
|
||
| async function createIsolatedWorktree(sha, repoRoot, dependencies) { | ||
| const runGit = dependencies.runBounded ?? runBounded; | ||
| 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.
Resolved
|
||
| if (result.error || result.timedOut || result.interrupted || result.status !== 0) { | ||
| 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. | ||
| 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 (result.error || result.timedOut || result.interrupted || result.status !== 0) { | ||
| try { | ||
| await removeDir(worktreePath); | ||
| } catch { | ||
|
qnbs marked this conversation as resolved.
Resolved
|
||
| // Best-effort: nothing more can be done from here; the directory is under os.tmpdir(). | ||
| } | ||
| await runGit('git', ['worktree', 'prune'], { cwd: repoRoot }); | ||
| } | ||
| } | ||
|
|
||
| // QNBS-v3: real node_modules install per verification is exactly the unbounded cost this avoids. | ||
| function linkNodeModules(worktreePath, source, dependencies) { | ||
| const symlinkFn = | ||
| dependencies.symlinkFn ?? | ||
| ((target, linkPath) => symlinkSync(target, linkPath, process.platform === 'win32' ? 'junction' : 'dir')); | ||
| try { | ||
| symlinkFn(source, join(worktreePath, 'node_modules')); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| return true; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| // 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 = {}) { | ||
| try { | ||
| const dependencyStateForRef = | ||
| dependencies.dependencyStateForRef ?? ((ref) => computeDependencyState(ref, repoRoot)); | ||
| // QNBS-v3: precondition for a trustworthy symlink, not a skip gate -- the check always runs. | ||
| if (dependencyStateForRef(sha) !== 'MATCHES') return 'UNKNOWN'; | ||
|
|
||
| const created = await createIsolatedWorktree(sha, repoRoot, dependencies); | ||
| if (!created.ok) { | ||
| await removeIsolatedWorktree(created.path, repoRoot, dependencies); | ||
| return 'UNKNOWN'; | ||
| } | ||
|
|
||
| try { | ||
| const nodeModulesSource = dependencies.nodeModulesSource ?? join(repoRoot, 'node_modules'); | ||
| if (!linkNodeModules(created.path, nodeModulesSource, dependencies)) return 'UNKNOWN'; | ||
|
qnbs marked this conversation as resolved.
Resolved
qnbs marked this conversation as resolved.
Resolved
|
||
|
|
||
| const runDetailed = dependencies.runLocalBinaryDetailed ?? runLocalBinaryDetailed; | ||
| const tsgoArgs = dependencies.tsgoArgs ?? DEFAULT_TSGO_ARGS; | ||
| const result = await runDetailed('tsgo', tsgoArgs, { | ||
|
qnbs marked this conversation as resolved.
Resolved
|
||
| root: created.path, | ||
| cwd: created.path, | ||
| }); | ||
|
qnbs marked this conversation as resolved.
Resolved
|
||
| if (result.error || result.timedOut || result.interrupted) return 'UNKNOWN'; | ||
| return result.status === 0 ? 'PASS' : 'FAIL'; | ||
|
qnbs marked this conversation as resolved.
Resolved
qnbs marked this conversation as resolved.
Resolved
|
||
| } finally { | ||
| await removeIsolatedWorktree(created.path, repoRoot, 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.
Resolved
|
||
| } | ||
| return aggregateExactTreeState(states); | ||
| } | ||
|
|
||
| function resolveRef(ref, dependencies) { | ||
| const runGitSync = | ||
| dependencies.runGitSync ?? ((args) => spawnSync('git', args, { encoding: 'utf8' })); | ||
| const result = runGitSync(['rev-parse', '--verify', `${ref}^{commit}`]); | ||
| if (result.status !== 0) return null; | ||
| return result.stdout.trim(); | ||
| } | ||
|
qnbs marked this conversation as resolved.
Resolved
|
||
|
|
||
| export async function main(argv = process.argv.slice(2)) { | ||
| const refs = argv.length > 0 ? argv : ['HEAD']; | ||
| const shas = []; | ||
| for (const ref of refs) { | ||
| const sha = resolveRef(ref); | ||
|
qnbs marked this conversation as resolved.
Resolved
|
||
| 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); | ||
| console.log(`[verify-exact-tree] result: ${state}`); | ||
| if (state === 'UNKNOWN') { | ||
| console.log( | ||
| '[verify-exact-tree] could not be established (dependencies not reconciled/matched locally, 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.