diff --git a/research/pr6334-posix-path-audit/README.md b/research/pr6334-posix-path-audit/README.md new file mode 100644 index 0000000000..6cb7a0b7b3 --- /dev/null +++ b/research/pr6334-posix-path-audit/README.md @@ -0,0 +1,162 @@ +# Independent POSIX audit of PR6334 Git paths + +This package takes the explicit independent POSIX check requested by +[PR6334](https://github.com/CrocSwap/integer-mult-bounds/pull/6334) at +`e579ddc0e0b4614184b869f8d20cffdcc26380a6`. That contribution repairs line-ending +normalization which can collapse distinct CR, LF and CRLF Git paths and falsely +report complete local overlap coverage. The repair and its Windows evidence +belong to the predecessor; this package supplies independent POSIX fixtures. + +The original `portable-replay` card is retained verbatim in [TASK.json](TASK.json). +The actual scope changes from its compressed-artifact example to this named +path-identity handoff. Canonical base is +`e54523517d0f03e58352e647c403a5af3e525a57`, priority revision 8. No selected +result, shared helper, frozen predecessor package or mathematical certificate +is changed. + +## Scope and evidence + +The bounded question is whether the retained candidate preserves distinct +valid UTF-8 Git paths on POSIX and reports the correct local coverage when +line-ending names and symbolic links are present. A separately constructed +Git fixture compares the candidate against the retained canonical-main helper. +Meaningful controls must reject missing or incorrectly reported coverage; +checking that a process merely exits successfully is insufficient. + +On macOS 26.5 arm64, CPython 3.12.16, Apple Git 2.50.1, all **83 tests** +from the predecessor's nine documented modules passed, with **zero skips**. +The two path suites also passed under optimized drivers (13 executions). +All five symlink controls skipped in the original Windows run executed here. +Exact commands, timings and log hashes are in +[evidence/predecessor-replay.json](evidence/predecessor-replay.json). +These 96 executions took about 20.5 seconds; this is a local timing, not a +performance claim or a hosted-CI result. + +The new independent verifier runs **seven materialized filesystem states** +against both pinned CLI implementations (14 CLI executions per mode). Its +oracle is the explicitly constructed fixture, not either helper's parsed tree. +It preserves an executable note, excludes a committed symlink and a nested +vendor note, and uses `--limit 1` while checking total results and warnings. +The five incomplete states cover removed CR/CRLF files, leaf aliases, parent +aliases, dangling aliases and an unrelated untracked note. In all five, main +falsely reports complete coverage; PR6334 reports the exact two missing names. +Both real-note positive states pass. The cache remains byte-identical. + +Normal and optimized receipts are in +[evidence/independent-normal.json](evidence/independent-normal.json) and +[evidence/independent-optimized.json](evidence/independent-optimized.json). +Each also reruns the unchanged 13-test path suites from a disposable export. +Every independent CLI child receives `-O` in optimized mode. The unchanged +predecessor local-suite driver does not propagate `-O` to its own CLI child; +its optimized-driver result is intentionally distinguished from the new probe. + +An initial audit-harness check incorrectly searched verbose test output for +any occurrence of `skipped`, including a successful test's name. It was fixed +to require the exact unittest success summary; the receipts above are from +completed reruns. No predecessor functional failure was found. + +The separate [controls.py](controls.py) rejects five byte-corrupted retained +sources and eight forged report fields, and accepts two positive controls. +Both normal and optimized runs passed; their receipts are in `evidence/controls-*.json`. +A copy of this package outside the project Git checkout also passed optimized +replay (seven differential cases plus 13 retained tests, zero skips). This +confirms that the direct replay does not rely on cached unmerged Git objects. + +Both helper versions and their shared dependency are retained under this +package. [SOURCE-VERSIONS.json](SOURCE-VERSIONS.json) records their original +commits, paths, Git blobs and SHA256 digests; `verify.py` checks the retained +bytes before execution. The predecessor's unmerged Git commit is not needed +for a cold replay. The origin receipt records extraction provenance. Hard-coded SHA256 pins bind +the executed bytes; this is not a cryptographic commit-membership proof or +acceptance of every predecessor claim. + +[SOURCES.json](SOURCES.json) binds only the base catalogue and selected record +as historical provenance. There are no live external inputs in +[VERIFY.json](VERIFY.json): all executed helper bytes are package-local. +The registered metadata check needs the canonical base Git history; the direct +verifier creates disposable fixture repositories and does not require project +Git history or a network connection. + +## Reproduce + +From the repository root, with Python 3.12 and Git available: + +```sh +python3.12 -B scripts/research_package.py --package research/pr6334-posix-path-audit --check-only +python3.12 -B scripts/research_package.py --package research/pr6334-posix-path-audit +python3.12 -I -B research/pr6334-posix-path-audit/verify.py +python3.12 -I -O -B research/pr6334-posix-path-audit/verify.py +``` + +The explicit normal and optimized commands exercise the same contract; the +audit must not depend on Python `assert` statements. Each main replay has a 180-second timeout; the two rejection-control commands +have 30-second limits. No Python dependencies, solver, Lean or Docker build +is requested. The POSIX audit does not extend the predecessor's Windows +coverage or certify arbitrary non-UTF-8 filenames, concurrent worktree edits, +scientific source bindings or the complete multiplication theorem. + +After committing, publication checks are: + +```sh +python3.12 -B scripts/check_research_commit.py --package research/pr6334-posix-path-audit --check-source-history +``` + +The unrelated six-test `tests/test_research_current_status.py` module was also +run on the unchanged main files: five passed and +`CurrentCiNavigationTests.test_current_ci_section_matches_selected_record` +failed because CI documentation still names frontier6030 while the selected +record names frontier7000. This is a reproduced baseline failure, not a pass. + +Hosted CI results are separate from the local scoped replay. In particular, +the unrelated selected-record/CI-navigation failure reported against canonical +main is already addressed by open +[PR7276](https://github.com/CrocSwap/integer-mult-bounds/pull/7276); this audit +does not duplicate that repair. + +## Task selection and overlap + +Read the current `AGENTS.md`, `autoresearch.md`, `CONTRIBUTING.md`, and +[coordination guide](https://github.com/CrocSwap/integer-mult-bounds/discussions/1163) +before choosing this source-specific handoff. At the selection check, PR6334 +had no comments or reviews and explicitly asked for POSIX execution of both +path suites, including materialized LF names and symbolic links. + +Targeted open-PR search for `6334` returned only PR6334, and `overlap POSIX` +returned no result. The Discussion search for `6334` returned +[discussion6356](https://github.com/CrocSwap/integer-mult-bounds/discussions/6356), +which distinguishes PR6357's requested-repository pin reader from PR6334's +overlap index. The latest 500 top-level handoff comments, dated November 3–14, +2026, and up to 5–10 recent replies per comment did not surface a POSIX audit +of PR6334. The repository overlap helper additionally completed a refresh of +901 open PR bodies and 7,512 Discussion records (including comments/replies) at +2026-11-14 13:17–13:20 UTC. The literal `6334` search had unrelated hash/numeric +hits and the separate #6356 work; no additional matching POSIX audit was found. +Local sparse coverage omitted 10,177 of 10,178 tracked notes; closed PR bodies +were not fully fetched. These are bounded search results, not a novelty claim. +The [advance claim](https://github.com/CrocSwap/integer-mult-bounds/discussions/1166#discussioncomment-19612921) +names the exact scope. A pre-submission canonical-main fetch retained the same +`e54523517` base. + +## Open next questions + +Does the eventual integration of PR6334 preserve this behavior alongside later +main changes? The first step is to pin that integrated helper, inspect its delta, +and rerun these fixture expectations plus the current path suites. This audit +covers the exact pending head, not GitHub's merge result. Main contains later +inventory parsing and test changes, so passing the pending head is not sufficient +integration evidence. The blocker is an identified integration commit; no extra +search over scientific constructions is justified by this tooling result. + +## Credit + +Credit josecarlosgonzalezroque-design's PR6334 for the path-byte repair, its +counterexample and Windows evidence; lee-phys's PR4419 for the coverage +contract; Ali Romman's PR5725 for path checks; Nick Petalas's PR6161 and the +retained overlap contributors. Douglas Colkitt and the retained contributors +receive credit for the research-package runner and source-binding machinery. +Original notices remain with retained sources. + +Prepared for FanPUYen with substantial OpenAI Codex assistance and separately +scoped agents for handoff/overlap review, package scaffolding and independent +code review. New work is Apache-2.0. This is an agent-assisted tooling audit; +independent human and formal-kernel acceptance are not claimed. diff --git a/research/pr6334-posix-path-audit/SOURCE-VERSIONS.json b/research/pr6334-posix-path-audit/SOURCE-VERSIONS.json new file mode 100644 index 0000000000..be521a65fa --- /dev/null +++ b/research/pr6334-posix-path-audit/SOURCE-VERSIONS.json @@ -0,0 +1,40 @@ +{ + "schema_version": 1, + "files": [ + { + "path": "retained/main/scripts/find_overlap.py", + "source_path": "scripts/find_overlap.py", + "commit": "e54523517d0f03e58352e647c403a5af3e525a57", + "git_blob": "7e6290f273935ad1fdab6696b0fdf9e0ffc400a2", + "sha256": "41a2796868b5122f568b126726cd04d98f9b5fea78d399c449c1d2e2a69aa0f4" + }, + { + "path": "retained/pr6334/scripts/find_overlap.py", + "source_path": "scripts/find_overlap.py", + "commit": "e579ddc0e0b4614184b869f8d20cffdcc26380a6", + "git_blob": "a424af9b6b9dd0ba4202e837c81181ad074ec107", + "sha256": "cff8509bf54b19cfb174ba0b3878ed5ddf350cd7f18e40bf359d9cbbf67f8b29" + }, + { + "path": "retained/pr6334/scripts/research_package.py", + "source_path": "scripts/research_package.py", + "commit": "e579ddc0e0b4614184b869f8d20cffdcc26380a6", + "git_blob": "091b4c8f58bac9a9580d76025b887dc5c873d05f", + "sha256": "e1256016e7f51827d04fa2179255989f639ae1d97afaa9c16265f85a0c6abd70" + }, + { + "path": "retained/pr6334/tests/test_research_overlap_git_paths.py", + "source_path": "tests/test_research_overlap_git_paths.py", + "commit": "e579ddc0e0b4614184b869f8d20cffdcc26380a6", + "git_blob": "8e66d9a047c5b7e1e789c8783844d9a4194067a7", + "sha256": "85c99372b916a2cce45e4513252e9d64b24aeab293a7ab5d829d91e7c15749e1" + }, + { + "path": "retained/pr6334/tests/test_research_overlap_local.py", + "source_path": "tests/test_research_overlap_local.py", + "commit": "e579ddc0e0b4614184b869f8d20cffdcc26380a6", + "git_blob": "cc9943d78d9e2cdee01516d86b5f8e96ce7062d9", + "sha256": "51360d84b55792a55a1ff9a34ec02cf87086e43a0eb5144cf0e630a982d5d7b5" + } + ] +} diff --git a/research/pr6334-posix-path-audit/SOURCES.json b/research/pr6334-posix-path-audit/SOURCES.json new file mode 100644 index 0000000000..5335fa97d9 --- /dev/null +++ b/research/pr6334-posix-path-audit/SOURCES.json @@ -0,0 +1,18 @@ +{ + "schema_version": 1, + "base_commit": "e54523517d0f03e58352e647c403a5af3e525a57", + "files": [ + { + "path": "certificates/selected-result.json", + "usage": "provenance", + "git_blob": "75d77b328ea02ded535e7f23aee5b1e55d0bb1a8", + "sha256": "154c3d30455d778fd3343b6b4851f0fa4665f0f7625b0adb3675e2bafbbf6bf1" + }, + { + "path": "docs/research/agent-task-catalog.json", + "usage": "provenance", + "git_blob": "3404fbdd5e7529a0674eef26114b5071e4586cec", + "sha256": "ead6cdc9d0af6d58e8a49744d8ec8c7c15e13d2ba93c276c434bcf9b51b93a81" + } + ] +} diff --git a/research/pr6334-posix-path-audit/TASK.json b/research/pr6334-posix-path-audit/TASK.json new file mode 100644 index 0000000000..a803a80f17 --- /dev/null +++ b/research/pr6334-posix-path-audit/TASK.json @@ -0,0 +1,60 @@ +{ + "schema_version": 1, + "task_id": "portable-replay", + "priority_revision": 8, + "base_commit": "e54523517d0f03e58352e647c403a5af3e525a57", + "original_card": { + "id": "portable-replay", + "question": "Can one reproducible environment or artifact-comparison failure be repaired?", + "starting_points": [ + "research/gzip-container-reproducibility/README.md", + "docs/ci-verification.md" + ], + "methods": [ + "Reproduce one portability failure and add a minimal cross-environment fix.", + "Audit one canonical-payload comparison for omitted proof-relevant fields." + ], + "acceptance_gate": "Preserve immutable source hashes and proof-relevant content; a different compressed byte stream alone is not a mathematical mismatch.", + "first_step": { + "command": [ + "python3", + "-B", + "scripts/research_selected_baseline.py" + ], + "expected": "PASS_RESEARCH_BASELINE_BINDINGS_ONLY", + "inspect": "Resolve the current selected review, separate complex/ordinary BIT/admitted BIT/outer budgets, and compare the card against live handoffs before starting. Historical ceilings require exact source/model equivalence.", + "scope": "Source-binding orientation only; not a scientific replay or all-size proof. Materialize selected inputs if this checkout is sparse." + }, + "first_experiment": "Reproduce one specific mismatch or audit one canonical comparison for omitted proof-relevant fields.", + "bottleneck_link": "Portable exact checks prevent environment failures from blocking valid contributions.", + "allowed_variations": [ + "One interpreter/version mismatch or one payload field comparison." + ], + "scope_change_triggers": [ + "Removing immutable-source pins because generated compressed bytes differ." + ], + "overlap_terms": [ + "gzip container", + "reproducibility", + "canonical payload" + ] + }, + "scope_status": "changed", + "actual_scope": "Independently audit PR6334 exact Git-path preservation on POSIX, including materialized CR/LF/CRLF names, symlinks and complete-versus-partial overlap coverage, against the retained canonical-main helper.", + "scope_change_reason": "The portable-replay card starts from compressed artifact/environment mismatches. PR6334 explicitly requests independent POSIX coverage after Windows-only validation. This task takes that concrete handoff and audits exact path identity and coverage reporting rather than compressed-payload equivalence.", + "bottleneck_link": "Reliable overlap inventory prevents a false complete-coverage report from hiding prior research. This is contributor-tool verification and does not improve or audit the selected multiplication construction.", + "budget_minutes": 60, + "stop_rule": "Stop after the bounded POSIX source-bound differential fixtures and meaningful rejecting controls have an explicit result, or report the first unresolved failure. Do not expand into a mathematical replay, shared-helper rewrite or full historical suite.", + "outcome": "completed", + "evidence": "The package retains both exact helper versions and the shared dependency with origin hashes in SOURCE-VERSIONS.json. verify.py supplies bounded independent POSIX fixtures and emits source-bound results. Final measured replay evidence is recorded in README.md and the package evidence files; no scientific replay is claimed.", + "author": "Hao Yan (@FanPUYen)", + "ai_assistance": "Prepared for FanPUYen with substantial OpenAI Codex assistance, including separately scoped agents for handoff/overlap review, metadata scaffolding and independent code review.", + "open_questions": [ + { + "question": "Does the eventual integrated PR6334 helper preserve these path and coverage guarantees alongside later main changes?", + "why_it_matters": "This audit binds the exact pending head, not the eventual merge result. Current main includes later inventory parsing and test changes.", + "first_step": "Pin the integration commit, inspect its helper delta and rerun the seven declared filesystem states plus current path suites.", + "blocked_by": "An identified integration commit is required; pending-head success cannot establish merge-result behavior." + } + ] +} diff --git a/research/pr6334-posix-path-audit/VERIFY.json b/research/pr6334-posix-path-audit/VERIFY.json new file mode 100644 index 0000000000..0f183de834 --- /dev/null +++ b/research/pr6334-posix-path-audit/VERIFY.json @@ -0,0 +1,59 @@ +{ + "schema_version": 1, + "status": "ready", + "python": "3.12", + "requirements": [], + "inputs": [], + "commands": [ + { + "argv": [ + "{python}", + "-I", + "-W", + "error", + "-B", + "{package}/verify.py" + ], + "timeout_seconds": 180, + "checks": "Authenticate the retained helper bytes; run independent POSIX path-identity and coverage-report fixtures with a canonical-main differential control and rejecting controls. Git is required for disposable fixture repositories; no network or predecessor Git objects are required." + }, + { + "argv": [ + "{python}", + "-I", + "-W", + "error", + "-O", + "-B", + "{package}/verify.py" + ], + "timeout_seconds": 180, + "checks": "Repeat the same source-bound POSIX fixtures and rejecting controls with Python assertions disabled; coverage must remain enforced." + }, + { + "argv": [ + "{python}", + "-I", + "-B", + "-W", + "error", + "{package}/controls.py" + ], + "timeout_seconds": 30, + "checks": "Reject five altered retained sources and eight corrupt candidate reports; positive export/report controls must pass." + }, + { + "argv": [ + "{python}", + "-I", + "-B", + "-W", + "error", + "-O", + "{package}/controls.py" + ], + "timeout_seconds": 30, + "checks": "Reject five altered retained sources and eight corrupt candidate reports; positive export/report controls must pass." + } + ] +} diff --git a/research/pr6334-posix-path-audit/controls.py b/research/pr6334-posix-path-audit/controls.py new file mode 100644 index 0000000000..b1a19991c4 --- /dev/null +++ b/research/pr6334-posix-path-audit/controls.py @@ -0,0 +1,103 @@ +#!/usr/bin/env python3 +"""Independent rejection controls for the scoped PR6334 POSIX audit. + +Hao Yan (@FanPUYen), with substantial OpenAI Codex assistance. Apache-2.0. +These controls test the audit's acceptance boundary, not the theorem. +""" +import copy +from hashlib import sha256 +import importlib.util +import json +from pathlib import Path +import sys +import tempfile + +HERE = Path(__file__).resolve().parent + + +def require(condition, message): + if not condition: + raise ValueError(message) + + +def reject(call, message): + try: + call() + except ValueError as exc: + require(str(exc) == message, + 'control failed for an unrelated reason: ' + str(exc)) + else: + raise ValueError('invalid evidence was accepted: ' + message) + + +def main(): + path = HERE / 'verify.py' + spec = importlib.util.spec_from_file_location('pr6334_audit_under_test', path) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + files = module.source_bytes() + source_controls = [] + with tempfile.TemporaryDirectory(prefix='pr6334-rejection-') as directory: + root = Path(directory) + for name, raw in files.items(): + target = root / name + target.parent.mkdir(parents=True, exist_ok=True) + target.write_bytes(raw) + require(module.source_bytes(root) == files, 'valid source export rejected') + for name, raw in files.items(): + target = root / name + target.write_bytes(raw + b'\n') + reject(lambda: module.source_bytes(root), 'source hash mismatch: ' + name) + source_controls.append(name) + target.write_bytes(raw) + + # This report oracle is declared here, independently of the producer. + head = 'a' * 40 + missing = ['research/line\r\nbreak/README.md', + 'research/line\rbreak/README.md'] + report = {'coverage': {'local': { + 'head': head, 'head_records': 5, 'records': 3, + 'unsearched_head_paths': missing, 'complete_head_inventory': False}}, + 'matches_total': 3, 'matches': [{}], + 'warnings': ['local: 2 HEAD note paths were not searched']} + module.check_report(report, head, missing, 3, 'pr6334') + mutations = [ + ('head', 'b' * 40, 'HEAD binding changed'), + ('head_records', 3, 'unexpected HEAD path count'), + ('unsearched_head_paths', [], 'wrong missing paths'), + ('complete_head_inventory', True, 'incorrect completeness flag'), + ('records', 4, 'wrong searched record count'), + ] + report_controls = [] + for field, value, message in mutations: + bad = copy.deepcopy(report) + bad['coverage']['local'][field] = value + reject(lambda: module.check_report(bad, head, missing, 3, 'pr6334'), message) + report_controls.append(field) + for field, value, message in [ + ('matches_total', 2, 'search lost a materialized note'), + ('matches', [], '--limit did not limit displayed results'), + ('warnings', [], 'missing coverage warning'), + ]: + bad = copy.deepcopy(report) + bad[field] = value + reject(lambda: module.check_report(bad, head, missing, 3, 'pr6334'), message) + report_controls.append(field) + print(json.dumps({ + 'status': 'PASS_PR6334_AUDIT_REJECTION_CONTROLS', + 'optimized': bool(sys.flags.optimize), + 'verifier_sha256': sha256(path.read_bytes()).hexdigest(), + 'positive_controls': ['valid_source_export', 'valid_candidate_report'], + 'source_corruptions_rejected': source_controls, + 'report_corruptions_rejected': report_controls, + 'scope': 'Audit source-byte and report acceptance only; no mathematical proof.' + }, indent=2)) + + +if __name__ == '__main__': + try: + main() + except (OSError, ValueError) as exc: + print(json.dumps({'status': 'FAIL_PR6334_AUDIT_REJECTION_CONTROLS', + 'error': str(exc)}), file=sys.stderr) + raise SystemExit(1) diff --git a/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_git_root_binding-normal.log b/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_git_root_binding-normal.log new file mode 100644 index 0000000000..f7b6dd943f --- /dev/null +++ b/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_git_root_binding-normal.log @@ -0,0 +1,13 @@ +test_dirty_publication_cannot_pass_against_foreign_head (test_research_git_root_binding.GitRootBindingTests.test_dirty_publication_cannot_pass_against_foreign_head) ... ok +test_every_reader_and_scaffold_uses_local_objects (test_research_git_root_binding.GitRootBindingTests.test_every_reader_and_scaffold_uses_local_objects) ... ok +test_export_and_history_stay_in_requested_repository (test_research_git_root_binding.GitRootBindingTests.test_export_and_history_stay_in_requested_repository) ... ok +test_foreign_live_pins_do_not_validate (test_research_git_root_binding.GitRootBindingTests.test_foreign_live_pins_do_not_validate) ... ok +test_head_inventory_planning_and_overlap_cannot_use_foreign_notes (test_research_git_root_binding.GitRootBindingTests.test_head_inventory_planning_and_overlap_cannot_use_foreign_notes) ... ok +test_other_repository_selection_channels_are_not_inherited (test_research_git_root_binding.GitRootBindingTests.test_other_repository_selection_channels_are_not_inherited) ... ok +test_parent_and_verifier_environments_are_unchanged (test_research_git_root_binding.GitRootBindingTests.test_parent_and_verifier_environments_are_unchanged) ... ok +test_redirecting_environment_does_not_break_linked_worktree (test_research_git_root_binding.GitRootBindingTests.test_redirecting_environment_does_not_break_linked_worktree) ... ok + +---------------------------------------------------------------------- +Ran 8 tests in 2.636s + +OK diff --git a/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_local_aliases-normal.log b/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_local_aliases-normal.log new file mode 100644 index 0000000000..94768b7563 --- /dev/null +++ b/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_local_aliases-normal.log @@ -0,0 +1,9 @@ +test_checkout_root_alias_is_allowed_but_ledger_parent_alias_is_not (test_research_local_aliases.LocalAliasTests.test_checkout_root_alias_is_allowed_but_ledger_parent_alias_is_not) ... ok +test_internal_alias_and_directory_note_are_not_counted_twice (test_research_local_aliases.LocalAliasTests.test_internal_alias_and_directory_note_are_not_counted_twice) ... ok +test_parent_and_leaf_aliases_are_not_local_records (test_research_local_aliases.LocalAliasTests.test_parent_and_leaf_aliases_are_not_local_records) ... ok +test_replaced_tracked_parent_leaves_an_explicit_coverage_gap (test_research_local_aliases.LocalAliasTests.test_replaced_tracked_parent_leaves_an_explicit_coverage_gap) ... ok + +---------------------------------------------------------------------- +Ran 4 tests in 0.889s + +OK diff --git a/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_onboarding-normal.log b/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_onboarding-normal.log new file mode 100644 index 0000000000..23e95842b8 --- /dev/null +++ b/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_onboarding-normal.log @@ -0,0 +1,41 @@ +CHECK Scoped test fixture +PASS_REGISTERED_RESEARCH_PACKAGE research/fixture outcome=partial +CHECK Scoped test fixture +test_binding_and_target_are_derived (test_research_onboarding.BaselineTests.test_binding_and_target_are_derived) ... ok +test_fetch_pr_files_paginates_and_flags_shortfall (test_research_onboarding.OverlapPathIndexTests.test_fetch_pr_files_paginates_and_flags_shortfall) ... ok +test_fetch_pr_files_retries_transient_page_failures (test_research_onboarding.OverlapPathIndexTests.test_fetch_pr_files_retries_transient_page_failures) ... ok +test_fetch_pr_files_skips_unavailable_file_lists (test_research_onboarding.OverlapPathIndexTests.test_fetch_pr_files_skips_unavailable_file_lists) ... ok +test_path_report_exact_glob_substring_and_rejection (test_research_onboarding.OverlapPathIndexTests.test_path_report_exact_glob_substring_and_rejection) ... ok +test_pr_files_fetched_only_when_requested_and_cached (test_research_onboarding.OverlapPathIndexTests.test_pr_files_fetched_only_when_requested_and_cached) ... ok +test_pr_files_refresh_failure_keeps_old_records (test_research_onboarding.OverlapPathIndexTests.test_pr_files_refresh_failure_keeps_old_records) ... ok +test_body_only_matches_and_no_clean_certificate (test_research_onboarding.OverlapTests.test_body_only_matches_and_no_clean_certificate) ... ok +test_cache_fresh_offline_and_partial_failure (test_research_onboarding.OverlapTests.test_cache_fresh_offline_and_partial_failure) ... ok +test_discussion_comment_and_reply_pagination (test_research_onboarding.OverlapTests.test_discussion_comment_and_reply_pagination) ... ok +test_local_readme_and_ledger (test_research_onboarding.OverlapTests.test_local_readme_and_ledger) ... ok +test_pr_pagination_normalization (test_research_onboarding.OverlapTests.test_pr_pagination_normalization) ... ok +test_report_survives_a_cp1252_stdout (test_research_onboarding.OverlapTests.test_report_survives_a_cp1252_stdout) ... ok +test_cli_replay_cannot_accept_assertion_failure_from_environment (test_research_onboarding.PackageTests.test_cli_replay_cannot_accept_assertion_failure_from_environment) ... ok +test_cli_unicode_coverage_does_not_relax_source_pins (test_research_onboarding.PackageTests.test_cli_unicode_coverage_does_not_relax_source_pins) ... ok +test_cli_unicode_coverage_survives_legacy_stdout (test_research_onboarding.PackageTests.test_cli_unicode_coverage_survives_legacy_stdout) ... ok +test_dependency_plan_and_runtime_failure (test_research_onboarding.PackageTests.test_dependency_plan_and_runtime_failure) ... ok +test_inherited_optimization_cannot_disable_child_checks (test_research_onboarding.PackageTests.test_inherited_optimization_cannot_disable_child_checks) ... Traceback (most recent call last): + File "", line 1, in +AssertionError: invalid fixture +Traceback (most recent call last): + File "", line 1, in +AssertionError: invalid fixture +ok +test_inherited_pythonpath_cannot_shadow_child_imports (test_research_onboarding.PackageTests.test_inherited_pythonpath_cannot_shadow_child_imports) ... ok +test_live_input_drift_but_provenance_survives_priority_change (test_research_onboarding.PackageTests.test_live_input_drift_but_provenance_survives_priority_change) ... ok +test_nested_interpreter_and_explicit_options (test_research_onboarding.PackageTests.test_nested_interpreter_and_explicit_options) ... ok +test_original_contract_and_timeout_limits (test_research_onboarding.PackageTests.test_original_contract_and_timeout_limits) ... ok +test_ready_package_replays_and_mutation_fails (test_research_onboarding.PackageTests.test_ready_package_replays_and_mutation_fails) ... ok +test_scaffold_pins_and_refuses_overwrite (test_research_onboarding.PackageTests.test_scaffold_pins_and_refuses_overwrite) ... ok +test_scope_drift_requires_reason (test_research_onboarding.PackageTests.test_scope_drift_requires_reason) ... ok +test_utf8_metadata_validates_under_any_locale (test_research_onboarding.PackageTests.test_utf8_metadata_validates_under_any_locale) ... ok +test_onboarding_text_io_does_not_depend_on_host_locale (test_research_onboarding.TextEncodingTests.test_onboarding_text_io_does_not_depend_on_host_locale) ... ok + +---------------------------------------------------------------------- +Ran 27 tests in 5.411s + +OK diff --git a/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_overlap_cache-normal.log b/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_overlap_cache-normal.log new file mode 100644 index 0000000000..cdf6ab449e --- /dev/null +++ b/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_overlap_cache-normal.log @@ -0,0 +1,10 @@ +test_bad_root_is_unknown_coverage_and_does_not_rewrite_cache (test_research_overlap_cache.CacheShapeTests.test_bad_root_is_unknown_coverage_and_does_not_rewrite_cache) ... ok +test_bad_source_keeps_other_usable_evidence (test_research_overlap_cache.CacheShapeTests.test_bad_source_keeps_other_usable_evidence) ... ok +test_invalid_path_records_are_not_searched (test_research_overlap_cache.CacheShapeTests.test_invalid_path_records_are_not_searched) ... ok +test_legacy_optional_text_fields_remain_optional (test_research_overlap_cache.CacheShapeTests.test_legacy_optional_text_fields_remain_optional) ... ok +test_online_refresh_replaces_invalid_source (test_research_overlap_cache.CacheShapeTests.test_online_refresh_replaces_invalid_source) ... ok + +---------------------------------------------------------------------- +Ran 5 tests in 0.008s + +OK diff --git a/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_overlap_closed-normal.log b/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_overlap_closed-normal.log new file mode 100644 index 0000000000..8a6c06b9c1 --- /dev/null +++ b/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_overlap_closed-normal.log @@ -0,0 +1,13 @@ +test_closed_path_request_fails_before_network (test_research_overlap_closed.ClosedOverlapTests.test_closed_path_request_fails_before_network) ... ok +test_default_does_not_fetch_or_search_cached_closed_bodies (test_research_overlap_closed.ClosedOverlapTests.test_default_does_not_fetch_or_search_cached_closed_bodies) ... ok +test_empty_closed_repository_is_complete (test_research_overlap_closed.ClosedOverlapTests.test_empty_closed_repository_is_complete) ... ok +test_failed_first_closed_fetch_cannot_claim_complete_coverage (test_research_overlap_closed.ClosedOverlapTests.test_failed_first_closed_fetch_cannot_claim_complete_coverage) ... ok +test_failed_later_page_preserves_complete_closed_cache (test_research_overlap_closed.ClosedOverlapTests.test_failed_later_page_preserves_complete_closed_cache) ... ok +test_later_closed_page_is_searched_with_explicit_coverage (test_research_overlap_closed.ClosedOverlapTests.test_later_closed_page_is_searched_with_explicit_coverage) ... ok +test_offline_opt_in_reports_missing_closed_snapshot (test_research_overlap_closed.ClosedOverlapTests.test_offline_opt_in_reports_missing_closed_snapshot) ... ok +test_state_change_keeps_one_newest_head_and_open_tie (test_research_overlap_closed.ClosedOverlapTests.test_state_change_keeps_one_newest_head_and_open_tie) ... ok + +---------------------------------------------------------------------- +Ran 8 tests in 0.015s + +OK diff --git a/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_overlap_git_paths-normal.log b/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_overlap_git_paths-normal.log new file mode 100644 index 0000000000..d6e518e54f --- /dev/null +++ b/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_overlap_git_paths-normal.log @@ -0,0 +1,12 @@ +test_cli_json_preserves_paths_and_search_results (__main__.GitPathCoverageTests.test_cli_json_preserves_paths_and_search_results) ... ok +test_cr_lf_and_crlf_paths_remain_distinct (__main__.GitPathCoverageTests.test_cr_lf_and_crlf_paths_remain_distinct) ... ok +test_exact_records_complete_the_inventory (__main__.GitPathCoverageTests.test_exact_records_complete_the_inventory) ... ok +test_lf_record_cannot_hide_unsearched_cr_paths (__main__.GitPathCoverageTests.test_lf_record_cannot_hide_unsearched_cr_paths) ... ok +test_missing_git_keeps_coverage_unknown (__main__.GitPathCoverageTests.test_missing_git_keeps_coverage_unknown) ... ok +test_tabs_unicode_and_non_utf8_bytes_are_preserved (__main__.GitPathCoverageTests.test_tabs_unicode_and_non_utf8_bytes_are_preserved) ... ok +test_unborn_head_keeps_coverage_unknown (__main__.GitPathCoverageTests.test_unborn_head_keeps_coverage_unknown) ... ok + +---------------------------------------------------------------------- +Ran 7 tests in 0.760s + +OK diff --git a/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_overlap_git_paths-optimized.log b/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_overlap_git_paths-optimized.log new file mode 100644 index 0000000000..2b2d42b6c7 --- /dev/null +++ b/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_overlap_git_paths-optimized.log @@ -0,0 +1,12 @@ +test_cli_json_preserves_paths_and_search_results (__main__.GitPathCoverageTests.test_cli_json_preserves_paths_and_search_results) ... ok +test_cr_lf_and_crlf_paths_remain_distinct (__main__.GitPathCoverageTests.test_cr_lf_and_crlf_paths_remain_distinct) ... ok +test_exact_records_complete_the_inventory (__main__.GitPathCoverageTests.test_exact_records_complete_the_inventory) ... ok +test_lf_record_cannot_hide_unsearched_cr_paths (__main__.GitPathCoverageTests.test_lf_record_cannot_hide_unsearched_cr_paths) ... ok +test_missing_git_keeps_coverage_unknown (__main__.GitPathCoverageTests.test_missing_git_keeps_coverage_unknown) ... ok +test_tabs_unicode_and_non_utf8_bytes_are_preserved (__main__.GitPathCoverageTests.test_tabs_unicode_and_non_utf8_bytes_are_preserved) ... ok +test_unborn_head_keeps_coverage_unknown (__main__.GitPathCoverageTests.test_unborn_head_keeps_coverage_unknown) ... ok + +---------------------------------------------------------------------- +Ran 7 tests in 0.731s + +OK diff --git a/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_overlap_local-normal.log b/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_overlap_local-normal.log new file mode 100644 index 0000000000..0ad9ddcbb2 --- /dev/null +++ b/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_overlap_local-normal.log @@ -0,0 +1,11 @@ +test_empty_head_note_inventory_is_complete (test_research_overlap_local.LocalOverlapCoverageTests.test_empty_head_note_inventory_is_complete) ... ok +test_full_checkout_uses_working_edits_and_untracked_notes (test_research_overlap_local.LocalOverlapCoverageTests.test_full_checkout_uses_working_edits_and_untracked_notes) ... ok +test_missing_git_or_repository_keeps_search_but_marks_coverage_unknown (test_research_overlap_local.LocalOverlapCoverageTests.test_missing_git_or_repository_keeps_search_but_marks_coverage_unknown) ... ok +test_ordinary_deleted_note_is_also_unsearched (test_research_overlap_local.LocalOverlapCoverageTests.test_ordinary_deleted_note_is_also_unsearched) ... ok +test_skipped_symlink_cannot_count_as_a_searched_head_note (test_research_overlap_local.LocalOverlapCoverageTests.test_skipped_symlink_cannot_count_as_a_searched_head_note) ... ok +test_sparse_gaps_survive_no_hits_and_result_limits (test_research_overlap_local.LocalOverlapCoverageTests.test_sparse_gaps_survive_no_hits_and_result_limits) ... ok + +---------------------------------------------------------------------- +Ran 6 tests in 2.982s + +OK diff --git a/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_overlap_local-optimized.log b/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_overlap_local-optimized.log new file mode 100644 index 0000000000..8070b56acd --- /dev/null +++ b/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_overlap_local-optimized.log @@ -0,0 +1,11 @@ +test_empty_head_note_inventory_is_complete (__main__.LocalOverlapCoverageTests.test_empty_head_note_inventory_is_complete) ... ok +test_full_checkout_uses_working_edits_and_untracked_notes (__main__.LocalOverlapCoverageTests.test_full_checkout_uses_working_edits_and_untracked_notes) ... ok +test_missing_git_or_repository_keeps_search_but_marks_coverage_unknown (__main__.LocalOverlapCoverageTests.test_missing_git_or_repository_keeps_search_but_marks_coverage_unknown) ... ok +test_ordinary_deleted_note_is_also_unsearched (__main__.LocalOverlapCoverageTests.test_ordinary_deleted_note_is_also_unsearched) ... ok +test_skipped_symlink_cannot_count_as_a_searched_head_note (__main__.LocalOverlapCoverageTests.test_skipped_symlink_cannot_count_as_a_searched_head_note) ... ok +test_sparse_gaps_survive_no_hits_and_result_limits (__main__.LocalOverlapCoverageTests.test_sparse_gaps_survive_no_hits_and_result_limits) ... ok + +---------------------------------------------------------------------- +Ran 6 tests in 2.965s + +OK diff --git a/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_overlap_pagination-normal.log b/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_overlap_pagination-normal.log new file mode 100644 index 0000000000..583f55850e --- /dev/null +++ b/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_overlap_pagination-normal.log @@ -0,0 +1,12 @@ +test_cached_multi_page_snapshot_needs_no_network (test_research_overlap_pagination.OverlapPaginationTests.test_cached_multi_page_snapshot_needs_no_network) ... ok +test_cli_finds_later_page_draft_without_slurp (test_research_overlap_pagination.OverlapPaginationTests.test_cli_finds_later_page_draft_without_slurp) ... ok +test_empty_repository_is_complete_not_missing (test_research_overlap_pagination.OverlapPaginationTests.test_empty_repository_is_complete_not_missing) ... ok +test_failed_first_fetch_does_not_claim_complete_coverage (test_research_overlap_pagination.OverlapPaginationTests.test_failed_first_fetch_does_not_claim_complete_coverage) ... ok +test_invalid_or_failed_refresh_preserves_previous_complete_pages (test_research_overlap_pagination.OverlapPaginationTests.test_invalid_or_failed_refresh_preserves_previous_complete_pages) ... ok +test_legacy_slurp_arguments_keep_all_pages (test_research_overlap_pagination.OverlapPaginationTests.test_legacy_slurp_arguments_keep_all_pages) ... ok +test_single_response_and_graphql_errors_keep_their_contract (test_research_overlap_pagination.OverlapPaginationTests.test_single_response_and_graphql_errors_keep_their_contract) ... ok + +---------------------------------------------------------------------- +Ran 7 tests in 0.010s + +OK diff --git a/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_overlap_terms-normal.log b/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_overlap_terms-normal.log new file mode 100644 index 0000000000..91dc651d40 --- /dev/null +++ b/research/pr6334-posix-path-audit/evidence/6334-replay-test_research_overlap_terms-normal.log @@ -0,0 +1,16 @@ +test_catalogue_terms_and_literal_package_names_are_not_stopwords (test_research_overlap_terms.OverlapTaskTermsTests.test_catalogue_terms_and_literal_package_names_are_not_stopwords) ... ok +test_exclusively_nested_sources_find_their_packages (test_research_overlap_terms.OverlapTaskTermsTests.test_exclusively_nested_sources_find_their_packages) ... ok +test_explicit_generic_terms_and_all_terms_are_preserved (test_research_overlap_terms.OverlapTaskTermsTests.test_explicit_generic_terms_and_all_terms_are_preserved) ... ok +test_explicit_numeric_query_overrides_card_reference_matching (test_research_overlap_terms.OverlapTaskTermsTests.test_explicit_numeric_query_overrides_card_reference_matching) ... ok +test_leading_zero_word_terms_and_zero_remain_literal (test_research_overlap_terms.OverlapTaskTermsTests.test_leading_zero_word_terms_and_zero_remain_literal) ... ok +test_nested_evidence_does_not_hide_a_lead_below_the_default_limit (test_research_overlap_terms.OverlapTaskTermsTests.test_nested_evidence_does_not_hide_a_lead_below_the_default_limit) ... ok +test_numeric_card_reference_forms_and_boundaries (test_research_overlap_terms.OverlapTaskTermsTests.test_numeric_card_reference_forms_and_boundaries) ... ok +test_numeric_card_reference_is_not_hidden_by_decimal_or_hash_hits (test_research_overlap_terms.OverlapTaskTermsTests.test_numeric_card_reference_is_not_hidden_by_decimal_or_hash_hits) ... ok +test_reference_excerpt_and_title_score_use_the_actual_match (test_research_overlap_terms.OverlapTaskTermsTests.test_reference_excerpt_and_title_score_use_the_actual_match) ... ok +test_reference_matching_preserves_all_terms (test_research_overlap_terms.OverlapTaskTermsTests.test_reference_matching_preserves_all_terms) ... ok +test_reference_prefilter_keeps_the_actual_regex_offset (test_research_overlap_terms.OverlapTaskTermsTests.test_reference_prefilter_keeps_the_actual_regex_offset) ... ok + +---------------------------------------------------------------------- +Ran 11 tests in 1.977s + +OK diff --git a/research/pr6334-posix-path-audit/evidence/controls-normal.json b/research/pr6334-posix-path-audit/evidence/controls-normal.json new file mode 100644 index 0000000000..b573c36e6c --- /dev/null +++ b/research/pr6334-posix-path-audit/evidence/controls-normal.json @@ -0,0 +1,27 @@ +{ + "status": "PASS_PR6334_AUDIT_REJECTION_CONTROLS", + "optimized": false, + "verifier_sha256": "1d75b7fd4098bcb748b9f57a306f48431abfa59347da96c447f8694a04e6d836", + "positive_controls": [ + "valid_source_export", + "valid_candidate_report" + ], + "source_corruptions_rejected": [ + "retained/main/scripts/find_overlap.py", + "retained/pr6334/scripts/find_overlap.py", + "retained/pr6334/scripts/research_package.py", + "retained/pr6334/tests/test_research_overlap_git_paths.py", + "retained/pr6334/tests/test_research_overlap_local.py" + ], + "report_corruptions_rejected": [ + "head", + "head_records", + "unsearched_head_paths", + "complete_head_inventory", + "records", + "matches_total", + "matches", + "warnings" + ], + "scope": "Audit source-byte and report acceptance only; no mathematical proof." +} diff --git a/research/pr6334-posix-path-audit/evidence/controls-optimized.json b/research/pr6334-posix-path-audit/evidence/controls-optimized.json new file mode 100644 index 0000000000..cf27f2391f --- /dev/null +++ b/research/pr6334-posix-path-audit/evidence/controls-optimized.json @@ -0,0 +1,27 @@ +{ + "status": "PASS_PR6334_AUDIT_REJECTION_CONTROLS", + "optimized": true, + "verifier_sha256": "1d75b7fd4098bcb748b9f57a306f48431abfa59347da96c447f8694a04e6d836", + "positive_controls": [ + "valid_source_export", + "valid_candidate_report" + ], + "source_corruptions_rejected": [ + "retained/main/scripts/find_overlap.py", + "retained/pr6334/scripts/find_overlap.py", + "retained/pr6334/scripts/research_package.py", + "retained/pr6334/tests/test_research_overlap_git_paths.py", + "retained/pr6334/tests/test_research_overlap_local.py" + ], + "report_corruptions_rejected": [ + "head", + "head_records", + "unsearched_head_paths", + "complete_head_inventory", + "records", + "matches_total", + "matches", + "warnings" + ], + "scope": "Audit source-byte and report acceptance only; no mathematical proof." +} diff --git a/research/pr6334-posix-path-audit/evidence/independent-normal.json b/research/pr6334-posix-path-audit/evidence/independent-normal.json new file mode 100644 index 0000000000..2da2d81d4b --- /dev/null +++ b/research/pr6334-posix-path-audit/evidence/independent-normal.json @@ -0,0 +1,148 @@ +{ + "status": "PASS_PR6334_POSIX_PATH_AUDIT", + "python": "3.12.16", + "platform": "macOS-26.5-arm64-arm-64bit", + "optimized_cli_children": false, + "differential_cases": [ + { + "case": "all_materialized_executable_and_tracked_symlink", + "main": { + "records": 5, + "head_records": 3, + "complete_head_inventory": true, + "unsearched_head_paths": [] + }, + "pr6334": { + "records": 5, + "head_records": 5, + "complete_head_inventory": true, + "unsearched_head_paths": [] + } + }, + { + "case": "only_lf_materialized", + "main": { + "records": 3, + "head_records": 3, + "complete_head_inventory": true, + "unsearched_head_paths": [] + }, + "pr6334": { + "records": 3, + "head_records": 5, + "complete_head_inventory": false, + "unsearched_head_paths": [ + "research/line\r\nbreak/README.md", + "research/line\rbreak/README.md" + ] + } + }, + { + "case": "leaf_symlinks_to_lf_cannot_fill_gap", + "main": { + "records": 3, + "head_records": 3, + "complete_head_inventory": true, + "unsearched_head_paths": [] + }, + "pr6334": { + "records": 3, + "head_records": 5, + "complete_head_inventory": false, + "unsearched_head_paths": [ + "research/line\r\nbreak/README.md", + "research/line\rbreak/README.md" + ] + } + }, + { + "case": "parent_symlinks_to_lf_cannot_fill_gap", + "main": { + "records": 3, + "head_records": 3, + "complete_head_inventory": true, + "unsearched_head_paths": [] + }, + "pr6334": { + "records": 3, + "head_records": 5, + "complete_head_inventory": false, + "unsearched_head_paths": [ + "research/line\r\nbreak/README.md", + "research/line\rbreak/README.md" + ] + } + }, + { + "case": "dangling_symlinks_cannot_fill_gap", + "main": { + "records": 3, + "head_records": 3, + "complete_head_inventory": true, + "unsearched_head_paths": [] + }, + "pr6334": { + "records": 3, + "head_records": 5, + "complete_head_inventory": false, + "unsearched_head_paths": [ + "research/line\r\nbreak/README.md", + "research/line\rbreak/README.md" + ] + } + }, + { + "case": "untracked_note_cannot_fill_gap", + "main": { + "records": 4, + "head_records": 3, + "complete_head_inventory": true, + "unsearched_head_paths": [] + }, + "pr6334": { + "records": 4, + "head_records": 5, + "complete_head_inventory": false, + "unsearched_head_paths": [ + "research/line\r\nbreak/README.md", + "research/line\rbreak/README.md" + ] + } + }, + { + "case": "restored_real_notes_complete_inventory", + "main": { + "records": 6, + "head_records": 3, + "complete_head_inventory": true, + "unsearched_head_paths": [] + }, + "pr6334": { + "records": 6, + "head_records": 5, + "complete_head_inventory": true, + "unsearched_head_paths": [] + } + } + ], + "retained_suites": [ + { + "module": "test_research_overlap_git_paths.py", + "tests": 7, + "skips": 0 + }, + { + "module": "test_research_overlap_local.py", + "tests": 6, + "skips": 0 + } + ], + "source_sha256": { + "retained/main/scripts/find_overlap.py": "41a2796868b5122f568b126726cd04d98f9b5fea78d399c449c1d2e2a69aa0f4", + "retained/pr6334/scripts/find_overlap.py": "cff8509bf54b19cfb174ba0b3878ed5ddf350cd7f18e40bf359d9cbbf67f8b29", + "retained/pr6334/scripts/research_package.py": "e1256016e7f51827d04fa2179255989f639ae1d97afaa9c16265f85a0c6abd70", + "retained/pr6334/tests/test_research_overlap_git_paths.py": "85c99372b916a2cce45e4513252e9d64b24aeab293a7ab5d829d91e7c15749e1", + "retained/pr6334/tests/test_research_overlap_local.py": "51360d84b55792a55a1ff9a34ec02cf87086e43a0eb5144cf0e630a982d5d7b5" + }, + "scope": "Pinned local path/coverage behavior only; no network, current-main repair, supplier replay or theorem proof." +} diff --git a/research/pr6334-posix-path-audit/evidence/independent-optimized.json b/research/pr6334-posix-path-audit/evidence/independent-optimized.json new file mode 100644 index 0000000000..0aecdda9a1 --- /dev/null +++ b/research/pr6334-posix-path-audit/evidence/independent-optimized.json @@ -0,0 +1,148 @@ +{ + "status": "PASS_PR6334_POSIX_PATH_AUDIT", + "python": "3.12.16", + "platform": "macOS-26.5-arm64-arm-64bit", + "optimized_cli_children": true, + "differential_cases": [ + { + "case": "all_materialized_executable_and_tracked_symlink", + "main": { + "records": 5, + "head_records": 3, + "complete_head_inventory": true, + "unsearched_head_paths": [] + }, + "pr6334": { + "records": 5, + "head_records": 5, + "complete_head_inventory": true, + "unsearched_head_paths": [] + } + }, + { + "case": "only_lf_materialized", + "main": { + "records": 3, + "head_records": 3, + "complete_head_inventory": true, + "unsearched_head_paths": [] + }, + "pr6334": { + "records": 3, + "head_records": 5, + "complete_head_inventory": false, + "unsearched_head_paths": [ + "research/line\r\nbreak/README.md", + "research/line\rbreak/README.md" + ] + } + }, + { + "case": "leaf_symlinks_to_lf_cannot_fill_gap", + "main": { + "records": 3, + "head_records": 3, + "complete_head_inventory": true, + "unsearched_head_paths": [] + }, + "pr6334": { + "records": 3, + "head_records": 5, + "complete_head_inventory": false, + "unsearched_head_paths": [ + "research/line\r\nbreak/README.md", + "research/line\rbreak/README.md" + ] + } + }, + { + "case": "parent_symlinks_to_lf_cannot_fill_gap", + "main": { + "records": 3, + "head_records": 3, + "complete_head_inventory": true, + "unsearched_head_paths": [] + }, + "pr6334": { + "records": 3, + "head_records": 5, + "complete_head_inventory": false, + "unsearched_head_paths": [ + "research/line\r\nbreak/README.md", + "research/line\rbreak/README.md" + ] + } + }, + { + "case": "dangling_symlinks_cannot_fill_gap", + "main": { + "records": 3, + "head_records": 3, + "complete_head_inventory": true, + "unsearched_head_paths": [] + }, + "pr6334": { + "records": 3, + "head_records": 5, + "complete_head_inventory": false, + "unsearched_head_paths": [ + "research/line\r\nbreak/README.md", + "research/line\rbreak/README.md" + ] + } + }, + { + "case": "untracked_note_cannot_fill_gap", + "main": { + "records": 4, + "head_records": 3, + "complete_head_inventory": true, + "unsearched_head_paths": [] + }, + "pr6334": { + "records": 4, + "head_records": 5, + "complete_head_inventory": false, + "unsearched_head_paths": [ + "research/line\r\nbreak/README.md", + "research/line\rbreak/README.md" + ] + } + }, + { + "case": "restored_real_notes_complete_inventory", + "main": { + "records": 6, + "head_records": 3, + "complete_head_inventory": true, + "unsearched_head_paths": [] + }, + "pr6334": { + "records": 6, + "head_records": 5, + "complete_head_inventory": true, + "unsearched_head_paths": [] + } + } + ], + "retained_suites": [ + { + "module": "test_research_overlap_git_paths.py", + "tests": 7, + "skips": 0 + }, + { + "module": "test_research_overlap_local.py", + "tests": 6, + "skips": 0 + } + ], + "source_sha256": { + "retained/main/scripts/find_overlap.py": "41a2796868b5122f568b126726cd04d98f9b5fea78d399c449c1d2e2a69aa0f4", + "retained/pr6334/scripts/find_overlap.py": "cff8509bf54b19cfb174ba0b3878ed5ddf350cd7f18e40bf359d9cbbf67f8b29", + "retained/pr6334/scripts/research_package.py": "e1256016e7f51827d04fa2179255989f639ae1d97afaa9c16265f85a0c6abd70", + "retained/pr6334/tests/test_research_overlap_git_paths.py": "85c99372b916a2cce45e4513252e9d64b24aeab293a7ab5d829d91e7c15749e1", + "retained/pr6334/tests/test_research_overlap_local.py": "51360d84b55792a55a1ff9a34ec02cf87086e43a0eb5144cf0e630a982d5d7b5" + }, + "scope": "Pinned local path/coverage behavior only; no network, current-main repair, supplier replay or theorem proof." +} diff --git a/research/pr6334-posix-path-audit/evidence/predecessor-replay.json b/research/pr6334-posix-path-audit/evidence/predecessor-replay.json new file mode 100644 index 0000000000..af306caae1 --- /dev/null +++ b/research/pr6334-posix-path-audit/evidence/predecessor-replay.json @@ -0,0 +1,297 @@ +{ + "python": "Python 3.12.16", + "platform": "macOS-26.5-arm64-arm-64bit", + "git": "git version 2.50.1 (Apple Git-155)", + "head": "e579ddc0e0b4614184b869f8d20cffdcc26380a6", + "runs": [ + { + "module": "test_research_overlap_git_paths.py", + "mode": "normal", + "command": [ + "python3.12", + "-W", + "error", + "-B", + "tests/test_research_overlap_git_paths.py", + "-v" + ], + "exit_code": 0, + "seconds": 1.0058876250004687, + "log": "evidence/6334-replay-test_research_overlap_git_paths-normal.log", + "tests": 7, + "unittest_seconds": 0.76, + "skips": 0, + "log_sha256": "52847a3ec1c96e10c3a996d95f4d10ef80b443d9e870542965b484fdd8ea4700", + "result": "OK" + }, + { + "module": "test_research_overlap_git_paths.py", + "mode": "optimized", + "command": [ + "python3.12", + "-I", + "-O", + "-W", + "error", + "-B", + "tests/test_research_overlap_git_paths.py", + "-v" + ], + "exit_code": 0, + "seconds": 0.9459793330024695, + "log": "evidence/6334-replay-test_research_overlap_git_paths-optimized.log", + "tests": 7, + "unittest_seconds": 0.731, + "skips": 0, + "log_sha256": "9a1648e562f83a592e4dd59ed7bface0c08589196ff90a49e08d31a30d2a2604", + "result": "OK" + }, + { + "module": "test_research_overlap_local.py", + "mode": "normal", + "command": [ + "python3.12", + "-W", + "error", + "-B", + "-m", + "unittest", + "discover", + "-s", + "tests", + "-p", + "test_research_overlap_local.py", + "-v" + ], + "exit_code": 0, + "seconds": 3.106524500002706, + "log": "evidence/6334-replay-test_research_overlap_local-normal.log", + "tests": 6, + "unittest_seconds": 2.982, + "skips": 0, + "log_sha256": "727849ac7f0ac7409e99a79ce08bd67c0ae88dd52e2e2654aaa2d232ef3ac132", + "result": "OK" + }, + { + "module": "test_research_overlap_local.py", + "mode": "optimized", + "command": [ + "python3.12", + "-I", + "-O", + "-W", + "error", + "-B", + "tests/test_research_overlap_local.py", + "-v" + ], + "exit_code": 0, + "seconds": 3.0889652079968073, + "log": "evidence/6334-replay-test_research_overlap_local-optimized.log", + "tests": 6, + "unittest_seconds": 2.965, + "skips": 0, + "log_sha256": "465e93003a63472e6cfef48e7ae68b39c4b35fe41d1cbc98bcce13ac719f2363", + "result": "OK" + }, + { + "module": "test_research_overlap_terms.py", + "mode": "normal", + "command": [ + "python3.12", + "-W", + "error", + "-B", + "-m", + "unittest", + "discover", + "-s", + "tests", + "-p", + "test_research_overlap_terms.py", + "-v" + ], + "exit_code": 0, + "seconds": 2.106979667001724, + "log": "evidence/6334-replay-test_research_overlap_terms-normal.log", + "tests": 11, + "unittest_seconds": 1.977, + "skips": 0, + "log_sha256": "267c87346d45b1c58009e2cc8fc0227b927971ab01d0e7e1bf434396659437be", + "result": "OK" + }, + { + "module": "test_research_git_root_binding.py", + "mode": "normal", + "command": [ + "python3.12", + "-W", + "error", + "-B", + "-m", + "unittest", + "discover", + "-s", + "tests", + "-p", + "test_research_git_root_binding.py", + "-v" + ], + "exit_code": 0, + "seconds": 2.8619152499995835, + "log": "evidence/6334-replay-test_research_git_root_binding-normal.log", + "tests": 8, + "unittest_seconds": 2.636, + "skips": 0, + "log_sha256": "21f11b7c23ca58cb7ee53e7d7ab304aeb928b370d7da6f7b234b34d29250547a", + "result": "OK" + }, + { + "module": "test_research_local_aliases.py", + "mode": "normal", + "command": [ + "python3.12", + "-W", + "error", + "-B", + "-m", + "unittest", + "discover", + "-s", + "tests", + "-p", + "test_research_local_aliases.py", + "-v" + ], + "exit_code": 0, + "seconds": 1.0581881670004805, + "log": "evidence/6334-replay-test_research_local_aliases-normal.log", + "tests": 4, + "unittest_seconds": 0.889, + "skips": 0, + "log_sha256": "577277a0a80c8b08ee3b46e72d0f775b288fac75a230d24b0641786855bb36dc", + "result": "OK" + }, + { + "module": "test_research_overlap_cache.py", + "mode": "normal", + "command": [ + "python3.12", + "-W", + "error", + "-B", + "-m", + "unittest", + "discover", + "-s", + "tests", + "-p", + "test_research_overlap_cache.py", + "-v" + ], + "exit_code": 0, + "seconds": 0.2227003749976575, + "log": "evidence/6334-replay-test_research_overlap_cache-normal.log", + "tests": 5, + "unittest_seconds": 0.008, + "skips": 0, + "log_sha256": "a33e4513782ba8630f566ff494b1bb9a8aca63d629ca206932cf913f723cb813", + "result": "OK" + }, + { + "module": "test_research_overlap_closed.py", + "mode": "normal", + "command": [ + "python3.12", + "-W", + "error", + "-B", + "-m", + "unittest", + "discover", + "-s", + "tests", + "-p", + "test_research_overlap_closed.py", + "-v" + ], + "exit_code": 0, + "seconds": 0.23532095900009153, + "log": "evidence/6334-replay-test_research_overlap_closed-normal.log", + "tests": 8, + "unittest_seconds": 0.015, + "skips": 0, + "log_sha256": "72be4e2e374f602db2333ac96d39dee0a18c1fca6a3df8a5c0edc4c522a0420e", + "result": "OK" + }, + { + "module": "test_research_overlap_pagination.py", + "mode": "normal", + "command": [ + "python3.12", + "-W", + "error", + "-B", + "-m", + "unittest", + "discover", + "-s", + "tests", + "-p", + "test_research_overlap_pagination.py", + "-v" + ], + "exit_code": 0, + "seconds": 0.2352762080008688, + "log": "evidence/6334-replay-test_research_overlap_pagination-normal.log", + "tests": 7, + "unittest_seconds": 0.01, + "skips": 0, + "log_sha256": "f483481ea99bcae4b9756879639c802686638b83f9ef2d1386bc332acf28019b", + "result": "OK" + }, + { + "module": "test_research_onboarding.py", + "mode": "normal", + "command": [ + "python3.12", + "-W", + "error", + "-B", + "-m", + "unittest", + "discover", + "-s", + "tests", + "-p", + "test_research_onboarding.py", + "-v" + ], + "exit_code": 0, + "seconds": 5.64452162499947, + "log": "evidence/6334-replay-test_research_onboarding-normal.log", + "tests": 27, + "unittest_seconds": 5.411, + "skips": 0, + "log_sha256": "e377681eaaa30189ed27e02f3fba92c2d4552e2902a3cf10b84b7e18b4ac0830", + "result": "OK" + } + ], + "normal_test_count": 83, + "optimized_driver_test_count": 13, + "total_wall_seconds": 20.512258917002328, + "source_sha256": { + "scripts/find_overlap.py": "cff8509bf54b19cfb174ba0b3878ed5ddf350cd7f18e40bf359d9cbbf67f8b29", + "scripts/research_package.py": "e1256016e7f51827d04fa2179255989f639ae1d97afaa9c16265f85a0c6abd70", + "tests/test_research_git_root_binding.py": "a5deb0eef274d56ec7e1da40ddb36d454c12d20eb2d08fcb710a3d84c1b5e4ba", + "tests/test_research_local_aliases.py": "0aaec0929a663c3901002399310f78c5371f3fbdedf204ebde5482a3a811f905", + "tests/test_research_onboarding.py": "5e3993fd7192f900d146df5db1a04d2793377a4c935c60bfcf1a48fcb4b56b9b", + "tests/test_research_overlap_cache.py": "e49cf1c1707a16c0181f642ab8d969eb8e8689262277b42dba343d0a13295bad", + "tests/test_research_overlap_closed.py": "2a5d28e13fe57191702d86630763fdfd33842696077837d225c5677aea819b03", + "tests/test_research_overlap_git_paths.py": "85c99372b916a2cce45e4513252e9d64b24aeab293a7ab5d829d91e7c15749e1", + "tests/test_research_overlap_local.py": "51360d84b55792a55a1ff9a34ec02cf87086e43a0eb5144cf0e630a982d5d7b5", + "tests/test_research_overlap_pagination.py": "b83e490c45f76b70eb394dd9706ff0b2f627211efe975909f9e1b40d8995f99c", + "tests/test_research_overlap_terms.py": "7dd29a1b44ab0c80b8b58a6a1eb7cff810dda942019754029b3bc38c9ff3e601" + }, + "optimization_scope": "The git-path suite propagates -O into its CLI subprocess; the local overlap suite uses -I -B child commands without -O even under its optimized driver." +} diff --git a/research/pr6334-posix-path-audit/retained/main/scripts/find_overlap.py b/research/pr6334-posix-path-audit/retained/main/scripts/find_overlap.py new file mode 100644 index 0000000000..7e6290f273 --- /dev/null +++ b/research/pr6334-posix-path-audit/retained/main/scripts/find_overlap.py @@ -0,0 +1,643 @@ +#!/usr/bin/env python3 +"""Search PR bodies, discussion claims and local package READMEs for possible overlap. + +Standard library + optional authenticated gh CLI. Remote snapshots live outside +the checkout. Results are leads, not a novelty certificate. Douglas Colkitt, +with OpenAI Codex assistance. Apache-2.0. +""" +import argparse +from datetime import datetime, timezone +import fnmatch +import hashlib +import json +import os +from pathlib import Path, PurePosixPath +import re +import subprocess +import sys +import tempfile +import time + +# Retain direct script execution under -I; the shared helper uses stdlib only. +sys.path.insert(0, str(Path(__file__).resolve().parent)) +try: + from research_package import git_source_env, relative_file +finally: + sys.path.pop(0) + +ROOT = Path(__file__).resolve().parents[1] +REPO = "CrocSwap/integer-mult-bounds" + + +def now(): + return datetime.now(timezone.utc).isoformat() + + +def gh_json(args, payload=None): + # --slurp is absent from older gh releases. Normalize it here so callers + # keep their nested-page contract. @json emits one compact array per page + # and escapes embedded newlines in PR titles and bodies. + paginated = "--paginate" in args + if paginated: + args = [arg for arg in args if arg != "--slurp"] + ["--jq", "@json"] + result = subprocess.run( + ["gh", "api", *args], input=None if payload is None else json.dumps(payload), + text=True, encoding="utf-8", capture_output=True, timeout=90, check=True, + ) + if paginated: + pages = [json.loads(line) for line in result.stdout.split("\n") if line] + if not pages or not all(isinstance(page, list) for page in pages): + raise ValueError("expected one JSON array per REST page") + return pages + value = json.loads(result.stdout) + if isinstance(value, dict) and value.get("errors"): + raise ValueError("GitHub GraphQL errors: " + json.dumps(value["errors"])) + return value + + +def graphql(query, variables): + return gh_json(["graphql", "--input", "-"], {"query": query, "variables": variables})["data"] + + +def pull_records(pages): + return [{"kind": "pr", "number": pr["number"], "title": pr["title"], + "body": pr.get("body") or "", "url": pr["html_url"], + "head": pr["head"]["sha"], "base": pr.get("base", {}).get("sha"), + "updated_at": pr["updated_at"], + "draft": pr.get("draft", False), "state": pr["state"]} + for page in pages for pr in page] + + +def pull_file_record(pull): + """Collect every available path, bound to both endpoints and advertised count.""" + if not pull.get("headRefOid") or not pull.get("baseRefOid"): + raise ValueError("PR file inventory is missing a head or base OID") + connection = pull.get("files") + paths, cursors = set(), set() + while connection is not None: + paths.update(node["path"] for node in connection["nodes"] if node) + if not connection["pageInfo"]["hasNextPage"]: + break + cursor = connection["pageInfo"].get("endCursor") + if not cursor or cursor in cursors: + raise ValueError("PR file pagination did not advance") + cursors.add(cursor) + page = graphql('''query($number:Int!, $cursor:String!) { + repository(owner:"CrocSwap",name:"integer-mult-bounds") { + pullRequest(number:$number) { + headRefOid baseRefOid changedFiles files(first:100,after:$cursor) { + pageInfo {hasNextPage endCursor} nodes {path} + } + } + } + }''', {"number": pull["number"], "cursor": cursor})["repository"]["pullRequest"] + if ((page.get("headRefOid"), page.get("baseRefOid"), page["changedFiles"]) + != (pull["headRefOid"], pull["baseRefOid"], pull["changedFiles"])): + raise ValueError("PR changed during file pagination") + connection = page["files"] + if connection is None: + raise ValueError("PR file continuation unavailable") + return {"number": pull["number"], "head": pull["headRefOid"], "base": pull["baseRefOid"], + "paths": sorted(paths), + "truncated": connection is None or len(paths) != pull["changedFiles"]} + + +def next_cursor(page_info, seen, label): + """Require progress within one GraphQL connection.""" + cursor = page_info.get("endCursor") + if not isinstance(cursor, str) or not cursor or cursor in seen: + raise ValueError(label + " pagination did not advance") + seen.add(cursor) + return cursor + + +def fetch_pr_files(): + """Changed file paths per open PR, so overlap can be searched by path. + + Follow 100-file pages; missing lists or count shortfalls remain explicitly + truncated. This is a file inventory, not a search of diff contents. + """ + rows = [] + cursor = None + cursors = set() + while True: + # ponytail: 10 PRs per page (25x100-node pages exceed GitHub's ~10 s + # gateway limit and 502; 10x100 stays under it). A full index refresh is + # ~74 pages and only path queries pay it. Each page gets four attempts. + result = None + for attempt in range(4): + try: + result = graphql('''query($cursor:String) { + repository(owner:"CrocSwap",name:"integer-mult-bounds") { + pullRequests(first:10,states:OPEN,after:$cursor) { + pageInfo {hasNextPage endCursor} + nodes {number headRefOid baseRefOid changedFiles files(first:100) { + pageInfo {hasNextPage endCursor} + nodes {path} + }} + } + } + }''', {"cursor": cursor})["repository"]["pullRequests"] + break + except subprocess.SubprocessError: + if attempt == 3: + raise + time.sleep(1 + attempt) + for pull in result["nodes"]: + # PR 376 (and similar) report no file list; record it as empty and + # flagged so path search never silently trusts missing data. + rows.append(pull_file_record(pull)) + if not result["pageInfo"]["hasNextPage"]: + return sorted(rows, key=lambda row: row["number"]) + cursor = next_cursor(result["pageInfo"], cursors, "connection") + + +def path_report(pr_records, file_rows, patterns): + """Open-PR entries whose changed files match any pattern (fnmatch or substring).""" + pulls = {record["number"]: record for record in pr_records if record.get("kind") == "pr"} + matches = [] + for row in sorted(file_rows, key=lambda item: -item["number"]): + matched = [path for path in row["paths"] + if any(fnmatch.fnmatchcase(path, pattern) or pattern in path for pattern in patterns)] + if not matched: + continue + pull = pulls.get(row["number"], {}) + matches.append({"kind": "pr", "number": row["number"], "title": pull.get("title", ""), + "url": pull.get("url") or f"https://github.com/{REPO}/pull/{row['number']}", + "head": row.get("head"), "body_head": pull.get("head"), + "head_matches": bool(row.get("head") and row["head"] == pull.get("head")), + "base": row.get("base"), "body_base": pull.get("base"), + "base_matches": bool(row.get("base") and row["base"] == pull.get("base")), + "updated_at": pull.get("updated_at"), "draft": pull.get("draft", False), + "matched_paths": matched, "changed_paths": len(row["paths"]), + "truncated": row.get("truncated", False)}) + return matches + + +def fetch_discussions(): + records = [] + cursor = None + cursors = set() + while True: + result = graphql('''query($cursor:String) { + repository(owner:"CrocSwap",name:"integer-mult-bounds") { + discussions(first:10,after:$cursor) { + pageInfo {hasNextPage endCursor} + nodes {id number title body url updatedAt comments(first:25) { + pageInfo {hasNextPage endCursor} + nodes {id body url updatedAt replies(first:10) { + pageInfo {hasNextPage endCursor} nodes {body url updatedAt} + }} + }} + } + } + }''', {"cursor": cursor})["repository"]["discussions"] + for discussion in result["nodes"]: + common = {"number": discussion["number"], "title": discussion["title"]} + records.append({**common, "kind": "discussion", "body": discussion["body"], + "url": discussion["url"], "updated_at": discussion["updatedAt"]}) + # Batch small first pages without a process/API round trip for + # every discussion. The worst-case nested node count is only + # 10 + 10*25 + 10*25*10 = 2760; busy threads still page fully. + connection = discussion["comments"] + comment_cursors = set() + while True: + for comment in connection["nodes"]: + records.append({**common, "kind": "discussion-comment", "body": comment["body"], + "url": comment["url"], "updated_at": comment["updatedAt"]}) + replies = comment["replies"] + reply_cursors = set() + while True: + for reply in replies["nodes"]: + records.append({**common, "kind": "discussion-reply", "body": reply["body"], + "url": reply["url"], "updated_at": reply["updatedAt"]}) + if not replies["pageInfo"]["hasNextPage"]: + break + replies = graphql('''query($id:ID!, $cursor:String) { + node(id:$id) { ... on DiscussionComment { + replies(first:100,after:$cursor) { + pageInfo {hasNextPage endCursor} nodes {body url updatedAt} + } + }} + }''', {"id": comment["id"], "cursor": next_cursor(replies["pageInfo"], reply_cursors, "replies")})["node"]["replies"] + if not connection["pageInfo"]["hasNextPage"]: + break + connection = graphql('''query($id:ID!, $cursor:String) { + node(id:$id) { ... on Discussion { + comments(first:100,after:$cursor) { + pageInfo {hasNextPage endCursor} + nodes {id body url updatedAt replies(first:100) { + pageInfo {hasNextPage endCursor} nodes {body url updatedAt} + }} + } + }} + }''', {"id": discussion["id"], + "cursor": next_cursor(connection["pageInfo"], comment_cursors, "comments")})["node"]["comments"] + if not result["pageInfo"]["hasNextPage"]: + return records + cursor = next_cursor(result["pageInfo"], cursors, "connection") + + +def atomic_json(path, data): + path.parent.mkdir(parents=True, exist_ok=True) + tmp = None + try: + with tempfile.NamedTemporaryFile(mode="w", encoding="utf-8", dir=path.parent, delete=False) as out: + tmp = Path(out.name) + json.dump(data, out) + out.write("\n") + os.replace(tmp, path) + finally: + if tmp is not None: + tmp.unlink(missing_ok=True) + + +def error_summary(exc): + if isinstance(exc, subprocess.CalledProcessError): + return (exc.stderr or str(exc))[:700] + return str(exc)[:700] + + +def validate_cached_source(source, name): + """Check cached containers before search code uses their fields.""" + if not isinstance(source, dict) or not isinstance(source.get("records"), list): + raise ValueError("source must contain a records list") + if type(source.get("complete_pagination")) is not bool: + raise ValueError("source must declare pagination coverage") + stamp = datetime.fromisoformat(source["fetched_at"]) + if stamp.tzinfo is None: + raise ValueError("source timestamp needs a UTC offset") + for row in source["records"]: + if not isinstance(row, dict) or type(row.get("number")) is not int: + raise ValueError("record needs an integer number") + if name == "pr_files": + if not isinstance(row.get("paths"), list) or not all(isinstance(p, str) for p in row["paths"]): + raise ValueError("file record needs a list of paths") + elif (not all(isinstance(row.get(key), str) for key in ("kind", "title")) + or not all(isinstance(row[key], str) for key in ("body", "url", "updated_at", "state") if key in row)): + raise ValueError("record text fields must be strings") + return stamp + + +def remote_snapshot(cache, offline=False, refresh=False, max_age=300, need_files=False, + include_closed=False): + warnings = [] + try: + snapshot = json.loads(cache.read_text(encoding="utf-8")) + if (not isinstance(snapshot, dict) or snapshot.get("schema_version") != 1 + or snapshot.get("repository") != REPO or not isinstance(snapshot.get("sources"), dict)): + raise ValueError("cache schema or repository mismatch") + except FileNotFoundError: + snapshot = {"schema_version": 1, "repository": REPO, "sources": {}} + except (OSError, ValueError) as exc: + warnings.append("cache: unusable snapshot: " + error_summary(exc)) + snapshot = {"schema_version": 1, "repository": REPO, "sources": {}} + sources = snapshot.setdefault("sources", {}) + fetches = [("open_prs", lambda: pull_records(gh_json([ + "--paginate", "--slurp", f"repos/{REPO}/pulls?state=open&per_page=100&sort=updated&direction=desc"]))), + ("discussions", fetch_discussions)] + if include_closed: + # Separate coverage and cache identity: an open-only snapshot cannot + # satisfy a request for closed (including merged) PR bodies. + fetches.append(("closed_prs", lambda: pull_records(gh_json([ + "--paginate", "--slurp", f"repos/{REPO}/pulls?state=closed&per_page=100&sort=updated&direction=desc"])))) + if need_files: + # Only path queries pay for the changed-file index; body searches stay as they were. + fetches.append(("pr_files", fetch_pr_files)) + for name, fetch in fetches: + cached = sources.get(name) + age = None + if cached is not None: + try: + stamp = validate_cached_source(cached, name) + age = (datetime.now(timezone.utc) - stamp).total_seconds() + except (KeyError, ValueError, TypeError) as exc: + warnings.append(f"{name}: unusable cached source: {error_summary(exc)}") + sources.pop(name, None) + cached = None + fresh = cached is not None and age is not None and 0 <= age <= max_age + if need_files and name in ("pr_files", "open_prs") and cached: + # Old schema-1 caches omitted endpoints. Refresh only affected sources; + # offline/failed refreshes retain the evidence with explicit warnings. + fresh = fresh and all(row.get("head") and row.get("base") for row in cached["records"]) + if offline: + warnings.append(f"{name}: offline; cached data may miss current changes") + elif refresh or not fresh: + try: + # Replace each source only after every page succeeds. + records = fetch() + replacement = {"fetched_at": now(), "records": records, "complete_pagination": True} + pending = {**snapshot, "sources": {**sources, name: replacement}} + atomic_json(cache, pending) + sources[name] = replacement + cached = replacement + except (OSError, ValueError, KeyError, TypeError, subprocess.SubprocessError) as exc: + warnings.append(f"{name}: refresh failed; using old data if available: {error_summary(exc)}") + if not cached: + warnings.append(f"{name}: no usable remote snapshot") + sources.pop(name, None) + return snapshot, warnings + + +def pull_corpus(snapshot, include_closed=False): + """One body per PR; a state change between snapshots must not double it.""" + pulls = {} + names = ("open_prs", "closed_prs") if include_closed else ("open_prs",) + for name in names: + for row in snapshot["sources"].get(name, {}).get("records", []): + old = pulls.get(row["number"]) + # GitHub updated_at uses one UTC spelling. Prefer the newer record; + # an exact tie keeps the open state. Coverage timestamps remain + # separate: this is not an atomic server snapshot. + key = lambda item: (item["updated_at"], item["state"] == "open") + if old is None or key(row) > key(old): + pulls[row["number"]] = row + return list(pulls.values()) + + +def local_records(root): + records = [] + # Deliberately avoid vendored trees and large binary artifacts. + paths = sorted([*(root / "research").glob("*/README.md"), *(root / "research").glob("*/TASK.json")]) + ledger = root / "docs/research/pr-ledger.md" + if ledger.is_file(): + paths.append(ledger) + for path in paths: + try: + path = relative_file(root, path.relative_to(root).as_posix()) + except ValueError: + # Aliases and non-files do not count as searched local notes. + continue + data = path.read_bytes() + body = data.decode("utf-8", errors="replace") + name = path.relative_to(root).as_posix() + kind = {"README.md": "local-readme", "TASK.json": "local-task"}.get(path.name, "local-ledger") + records.append({"kind": kind, + "title": name, "path": name, "body": body, + "sha256": hashlib.sha256(data).hexdigest()}) + return records + + +def local_coverage(root, records): + """Compare searched working-tree notes with the regular note paths at HEAD.""" + coverage = {"head": None, "records": len(records), "head_records": None, + "complete_head_inventory": None, "unsearched_head_paths": None} + try: + def git(*args): + return subprocess.run( + ["git", "--no-replace-objects", *args], cwd=root, check=True, + capture_output=True, encoding="utf-8", errors="surrogateescape", timeout=15, + env={**git_source_env(), "GIT_NO_LAZY_FETCH": "1"}, + ).stdout + + # Pin the tree once; only metadata is read, never omitted note blobs. + coverage["head"] = git("rev-parse", "--verify", "HEAD^{commit}").strip() + tree = git("ls-tree", "-r", "-z", "--full-tree", coverage["head"], "--", + "research", "docs/research/pr-ledger.md") + expected = set() + for entry in tree.split("\0"): + if not entry: + continue + metadata, path = entry.split("\t", 1) + # Git tree paths already have canonical slash-separated components. + # Filter before parsing metadata; most entries are nested vendor or + # evidence files, outside this deliberately shallow note inventory. + parts = path.split("/") + in_scope = (path == "docs/research/pr-ledger.md" + or (len(parts) == 3 and parts[0] == "research" + and parts[2] in ("README.md", "TASK.json"))) + if not in_scope: + continue + mode, kind, _ = metadata.split() + if kind == "blob" and mode in ("100644", "100755"): + expected.add(path) + except (OSError, subprocess.SubprocessError, ValueError) as exc: + return coverage, ["local: HEAD note inventory unavailable; local coverage is unknown. " + + error_summary(exc)] + missing = sorted(expected - {record["path"] for record in records}) + coverage.update(head_records=len(expected), complete_head_inventory=not missing, + unsearched_head_paths=missing) + warnings = ([f"local: {len(missing)} HEAD note paths were not searched; " + "inspect coverage.local.unsearched_head_paths in --json output and materialize relevant notes " + "before treating a lack of matches as evidence."] if missing else []) + return coverage, warnings + + +def match_position(text, term, pattern): + if pattern is None: + return text.find(term) + # Numeric-reference patterns contain the complete literal term. Avoid a + # boundary-aware regex scan when that mandatory substring is absent. + if term not in text: + return -1 + match = pattern.search(text) + return match.start() if match else -1 + + +def original_offset(text, lowered_offset): + """Map a lowercase search offset back to the unchanged source text.""" + if text.isascii(): + return lowered_offset + for index, char in enumerate(text): + width = len(char.lower()) + if lowered_offset < width: + return index + lowered_offset -= width + return len(text) + + +def search(records, terms, all_terms=False, reference_terms=()): + terms = list(dict.fromkeys(t.strip().lower() for t in terms if t.strip())) + if not terms: + raise ValueError("provide at least one nonempty search term") + # Numeric card references name predecessors, rather than digits inside a kappa + # or a source hash. Explicit user terms retain literal substring semantics. + patterns = {term: re.compile(r"(?= 0 or body_positions[t] >= 0] + if not found or (all_terms and len(found) != len(terms)): + continue + positions = [body_positions[t] for t in found if body_positions[t] >= 0] + start = max(0, original_offset(record.get("body", ""), min(positions)) - 100) if positions else 0 + matches.append({**{k: v for k, v in record.items() if k != "body"}, + "matched_terms": found, + "score": sum(3 if title_positions[t] >= 0 else 1 for t in found), + "excerpt": record.get("body", "")[start:start + 500]}) + return sorted(matches, key=lambda r: (-r["score"], r["kind"], r["title"], r.get("url", ""))) + + +def main(): + ap = argparse.ArgumentParser(description=__doc__) + ap.add_argument("terms", nargs="*", help="literal case-insensitive terms; quote multiword phrases") + ap.add_argument("--task", help="add the named catalogue card's overlap terms") + ap.add_argument("--path", action="append", default=[], metavar="PATTERN", + help="match open PRs by case-sensitive Git path glob or substring; spaces are literal; repeatable") + ap.add_argument("--offline", action="store_true") + ap.add_argument("--refresh", action="store_true") + ap.add_argument("--include-closed", action="store_true", + help="also search all paginated closed/merged PR titles and bodies (extra fetch/cache)") + ap.add_argument("--max-age", type=int, default=300, help="remote cache lifetime in seconds") + ap.add_argument("--limit", type=int, default=20) + ap.add_argument("--all-terms", action="store_true") + ap.add_argument("--json", action="store_true", dest="json_output") + ap.add_argument("--cache", type=Path, default=Path(os.environ.get("XDG_CACHE_HOME", str(Path.home() / ".cache"))) / "integer-mult-bounds/overlap.json") + args = ap.parse_args() + if args.max_age < 0 or args.limit < 1 or (args.offline and args.refresh): + ap.error("use nonnegative max-age, positive limit, and either offline or refresh") + if args.include_closed and args.path: + ap.error("--include-closed searches titles/bodies only; omit --path (its index covers open PRs)") + if args.cache.resolve().is_relative_to(ROOT.resolve()): + ap.error("keep the cache outside the repository") + terms = list(args.terms) + explicit_terms = {term.strip().lower() for term in args.terms} + reference_terms = [] + if args.task: + catalog = json.loads((ROOT / "docs/research/agent-task-catalog.json").read_text(encoding="utf-8")) + cards = [t for tr in catalog["tracks"] for t in tr["tasks"] if t["id"] == args.task] + if not cards: + ap.error("unknown task") + terms.extend([args.task, *cards[0].get("overlap_terms", [])]) + # A leading-zero digit string can be a word, e.g. census witness + # 01021323. Restrict reference matching to canonical positive integers. + reference_terms = list(dict.fromkeys( + t.strip() for t in cards[0].get("overlap_terms", []) + if t.strip().isascii() and t.strip().isdecimal() and not t.strip().startswith("0") + and t.strip() not in explicit_terms)) + # Catalogue paths are repository-relative POSIX paths. Nested sources + # identify their package, not generic directories such as evidence/code. + for point in cards[0]["starting_points"]: + parts = PurePosixPath(point).parts + if len(parts) >= 3 and parts[0] == "research": + terms.append(parts[1]) + if not any(t.strip() for t in terms) and not args.path: + ap.error("provide terms, --task or --path") + patterns = list(args.path) + if any(not p for p in patterns): + ap.error("--path must not be empty") + local = local_records(ROOT) + local_status, local_warnings = local_coverage(ROOT, local) + snapshot, warnings = remote_snapshot(args.cache, args.offline, args.refresh, args.max_age, + need_files=bool(patterns), include_closed=args.include_closed) + warnings = local_warnings + warnings + records = list(local) + pull_corpus(snapshot, args.include_closed) + coverage = {"local": local_status} + corpus_names = ("open_prs", "discussions", "closed_prs") if args.include_closed else ("open_prs", "discussions") + for name in corpus_names: + source = snapshot["sources"].get(name) + coverage[name] = {k: source[k] for k in ("fetched_at", "complete_pagination")} if source else None + if source: + if name == "discussions": + records.extend(source["records"]) + coverage[name]["records"] = len(source["records"]) + path_matches = [] + if patterns: + source = snapshot["sources"].get("pr_files") + coverage["pr_files"] = {k: source[k] for k in ("fetched_at", "complete_pagination")} if source else None + if source: + coverage["pr_files"]["records"] = len(source["records"]) + truncated = sorted({row["number"] for row in source["records"] if row.get("truncated", False)}) + coverage["pr_files"]["truncated_prs"] = truncated + if truncated: + warnings.append(f"pr_files: incomplete file inventories for {len(truncated)} PRs; " + "paths may be missing. Check truncated_prs in coverage, even when no path matches.") + pulls = snapshot["sources"].get("open_prs", {"records": []})["records"] + heads = {pull["number"]: pull.get("head") for pull in pulls} + file_heads = {row["number"]: row.get("head") for row in source["records"]} + unverified = sorted(number for number in heads.keys() | file_heads.keys() + if not file_heads.get(number) or file_heads[number] != heads.get(number)) + coverage["pr_files"]["unverified_heads"] = unverified + if unverified: + warnings.append(f"pr_files: file-head binding unverified for {len(unverified)} PRs; " + "paths may be stale. Refresh and recheck the heads listed in coverage.") + bases = {pull["number"]: pull.get("base") for pull in pulls} + file_bases = {row["number"]: row.get("base") for row in source["records"]} + unverified_bases = sorted(number for number in bases.keys() | file_bases.keys() + if not file_bases.get(number) or file_bases[number] != bases.get(number)) + coverage["pr_files"]["unverified_bases"] = unverified_bases + if unverified_bases: + warnings.append(f"pr_files: file-base binding unverified for {len(unverified_bases)} PRs; " + "paths may be stale. Refresh and recheck the bases listed in coverage.") + path_matches = path_report(pulls, source["records"], patterns) + matches = search(records, terms, args.all_terms, reference_terms) if any(t.strip() for t in terms) else [] + if patterns and matches: + # With both axes given, a record must match at least one term and one path. + hits = {m["number"]: m for m in path_matches} + matches = [dict(m, **{key: hits[m["number"]][key] + for key in ("matched_paths", "head", "body_head", "head_matches", + "base", "body_base", "base_matches", "truncated")}) for m in matches + if m.get("kind") == "pr" and m.get("number") in hits] + if patterns and not any(t.strip() for t in terms): + matches = [] + report = {"status": "POSSIBLE_OVERLAP_NOT_A_NOVELTY_CERTIFICATE", "searched_at": now(), + "terms": terms, "coverage": coverage, "local_records": len(local), "warnings": warnings, + "reference_terms": reference_terms, + "limits": (("Remote PR scope includes open and closed/merged titles and bodies, with separate coverage and timestamps. " + "State changes are deduplicated by latest PR update; snapshots are not atomic. " + if args.include_closed else + "Remote PR scope is open PRs including drafts. Closed work is searched through local READMEs, TASK metadata and ledger only. ") + + "Local coverage compares searched working-tree notes with regular note paths at coverage.local.head; " + "it does not certify HEAD contents or historical coverage. " + "No code diffs or arbitrary package files are searched. Recheck relevant heads before submission."), + "matches_total": len(matches), "matches": matches[:args.limit]} + if patterns: + report["path_patterns"] = patterns + report["path_limits"] = ("Path matching covers the changed file paths GitHub reports for open PRs, " + "not diff contents; all available file pages are followed. Missing lists or " + "advertised-count shortfalls are flagged truncated and listed in coverage.pr_files.truncated_prs " + "independently of matches or --limit. complete_pagination describes list paging, " + "not completeness of each PR's file inventory. Head/base identify the file " + "snapshot; body_head/body_base identify PR metadata. Both endpoints must match. " + "This is not an atomic server snapshot. Unverified joins are flagged, " + "including inventories with no matching path.") + report["path_matches_total"] = len(path_matches) + report["path_matches"] = path_matches[:args.limit] + if args.json_output: + print(json.dumps(report, indent=2)) + else: + # Excerpts quote arbitrary UTF-8; a cp1252 pipe must escape them, not crash. + if hasattr(sys.stdout, "reconfigure"): + sys.stdout.reconfigure(errors="backslashreplace") + print(report["status"]) + # A sparse checkout can omit thousands of notes. Keep the complete + # path list in JSON; the text report surfaces the count and warning. + local_summary = {k: v for k, v in local_status.items() if k != "unsearched_head_paths"} + omitted = local_status.get("unsearched_head_paths") + local_summary["unsearched_head_paths_count"] = None if omitted is None else len(omitted) + print("Coverage: " + json.dumps({**coverage, "local": local_summary})) + if reference_terms: + print("Task references (identifier matching): " + ", ".join(reference_terms)) + print(report["limits"]) + if patterns: + print(report["path_limits"]) + for warning in warnings: + print("WARNING: " + warning) + print(f"{len(matches)} possible matches; showing {min(args.limit, len(matches))}") + for match in matches[:args.limit]: + flags = " (file list truncated)" if match.get("truncated") else "" + print(f"\n[{match['kind']}] {match['title']}{flags}\n{match.get('url', match.get('path'))}") + if match.get("head"): + print("Head: " + match["head"]) + if match.get("head_matches") is False: + print("File-head binding unverified; body head: " + str(match.get("body_head"))) + print(match["excerpt"]) + if patterns: + print(f"\n{len(path_matches)} open PRs match paths {patterns}; " + f"showing {min(args.limit, len(path_matches))}") + for match in path_matches[:args.limit]: + flags = " (file list truncated)" if match["truncated"] else "" + print(f"\n[pr {match['number']}] {match['title']}{flags}\n{match['url']}") + if match.get("head"): + print("Head: " + match["head"]) + if not match["head_matches"]: + print("File-head binding unverified; body head: " + str(match["body_head"])) + print("Matched paths: " + ", ".join(match["matched_paths"])) + + +if __name__ == "__main__": + main() diff --git a/research/pr6334-posix-path-audit/retained/pr6334/scripts/find_overlap.py b/research/pr6334-posix-path-audit/retained/pr6334/scripts/find_overlap.py new file mode 100644 index 0000000000..a424af9b6b --- /dev/null +++ b/research/pr6334-posix-path-audit/retained/pr6334/scripts/find_overlap.py @@ -0,0 +1,645 @@ +#!/usr/bin/env python3 +"""Search PR bodies, discussion claims and local package READMEs for possible overlap. + +Standard library + optional authenticated gh CLI. Remote snapshots live outside +the checkout. Results are leads, not a novelty certificate. Douglas Colkitt, +with OpenAI Codex assistance. Apache-2.0. +""" +import argparse +from datetime import datetime, timezone +import fnmatch +import hashlib +import json +import os +from pathlib import Path, PurePosixPath +import re +import subprocess +import sys +import tempfile +import time + +# Retain direct script execution under -I; the shared helper uses stdlib only. +sys.path.insert(0, str(Path(__file__).resolve().parent)) +try: + from research_package import git_source_env, relative_file +finally: + sys.path.pop(0) + +ROOT = Path(__file__).resolve().parents[1] +REPO = "CrocSwap/integer-mult-bounds" + + +def now(): + return datetime.now(timezone.utc).isoformat() + + +def gh_json(args, payload=None): + # --slurp is absent from older gh releases. Normalize it here so callers + # keep their nested-page contract. @json emits one compact array per page + # and escapes embedded newlines in PR titles and bodies. + paginated = "--paginate" in args + if paginated: + args = [arg for arg in args if arg != "--slurp"] + ["--jq", "@json"] + result = subprocess.run( + ["gh", "api", *args], input=None if payload is None else json.dumps(payload), + text=True, encoding="utf-8", capture_output=True, timeout=90, check=True, + ) + if paginated: + pages = [json.loads(line) for line in result.stdout.split("\n") if line] + if not pages or not all(isinstance(page, list) for page in pages): + raise ValueError("expected one JSON array per REST page") + return pages + value = json.loads(result.stdout) + if isinstance(value, dict) and value.get("errors"): + raise ValueError("GitHub GraphQL errors: " + json.dumps(value["errors"])) + return value + + +def graphql(query, variables): + return gh_json(["graphql", "--input", "-"], {"query": query, "variables": variables})["data"] + + +def pull_records(pages): + return [{"kind": "pr", "number": pr["number"], "title": pr["title"], + "body": pr.get("body") or "", "url": pr["html_url"], + "head": pr["head"]["sha"], "base": pr.get("base", {}).get("sha"), + "updated_at": pr["updated_at"], + "draft": pr.get("draft", False), "state": pr["state"]} + for page in pages for pr in page] + + +def pull_file_record(pull): + """Collect every available path, bound to both endpoints and advertised count.""" + if not pull.get("headRefOid") or not pull.get("baseRefOid"): + raise ValueError("PR file inventory is missing a head or base OID") + connection = pull.get("files") + paths, cursors = set(), set() + while connection is not None: + paths.update(node["path"] for node in connection["nodes"] if node) + if not connection["pageInfo"]["hasNextPage"]: + break + cursor = connection["pageInfo"].get("endCursor") + if not cursor or cursor in cursors: + raise ValueError("PR file pagination did not advance") + cursors.add(cursor) + page = graphql('''query($number:Int!, $cursor:String!) { + repository(owner:"CrocSwap",name:"integer-mult-bounds") { + pullRequest(number:$number) { + headRefOid baseRefOid changedFiles files(first:100,after:$cursor) { + pageInfo {hasNextPage endCursor} nodes {path} + } + } + } + }''', {"number": pull["number"], "cursor": cursor})["repository"]["pullRequest"] + if ((page.get("headRefOid"), page.get("baseRefOid"), page["changedFiles"]) + != (pull["headRefOid"], pull["baseRefOid"], pull["changedFiles"])): + raise ValueError("PR changed during file pagination") + connection = page["files"] + if connection is None: + raise ValueError("PR file continuation unavailable") + return {"number": pull["number"], "head": pull["headRefOid"], "base": pull["baseRefOid"], + "paths": sorted(paths), + "truncated": connection is None or len(paths) != pull["changedFiles"]} + + +def next_cursor(page_info, seen, label): + """Require progress within one GraphQL connection.""" + cursor = page_info.get("endCursor") + if not isinstance(cursor, str) or not cursor or cursor in seen: + raise ValueError(label + " pagination did not advance") + seen.add(cursor) + return cursor + + +def fetch_pr_files(): + """Changed file paths per open PR, so overlap can be searched by path. + + Follow 100-file pages; missing lists or count shortfalls remain explicitly + truncated. This is a file inventory, not a search of diff contents. + """ + rows = [] + cursor = None + cursors = set() + while True: + # ponytail: 10 PRs per page (25x100-node pages exceed GitHub's ~10 s + # gateway limit and 502; 10x100 stays under it). A full index refresh is + # ~74 pages and only path queries pay it. Each page gets four attempts. + result = None + for attempt in range(4): + try: + result = graphql('''query($cursor:String) { + repository(owner:"CrocSwap",name:"integer-mult-bounds") { + pullRequests(first:10,states:OPEN,after:$cursor) { + pageInfo {hasNextPage endCursor} + nodes {number headRefOid baseRefOid changedFiles files(first:100) { + pageInfo {hasNextPage endCursor} + nodes {path} + }} + } + } + }''', {"cursor": cursor})["repository"]["pullRequests"] + break + except subprocess.SubprocessError: + if attempt == 3: + raise + time.sleep(1 + attempt) + for pull in result["nodes"]: + # PR 376 (and similar) report no file list; record it as empty and + # flagged so path search never silently trusts missing data. + rows.append(pull_file_record(pull)) + if not result["pageInfo"]["hasNextPage"]: + return sorted(rows, key=lambda row: row["number"]) + cursor = next_cursor(result["pageInfo"], cursors, "connection") + + +def path_report(pr_records, file_rows, patterns): + """Open-PR entries whose changed files match any pattern (fnmatch or substring).""" + pulls = {record["number"]: record for record in pr_records if record.get("kind") == "pr"} + matches = [] + for row in sorted(file_rows, key=lambda item: -item["number"]): + matched = [path for path in row["paths"] + if any(fnmatch.fnmatchcase(path, pattern) or pattern in path for pattern in patterns)] + if not matched: + continue + pull = pulls.get(row["number"], {}) + matches.append({"kind": "pr", "number": row["number"], "title": pull.get("title", ""), + "url": pull.get("url") or f"https://github.com/{REPO}/pull/{row['number']}", + "head": row.get("head"), "body_head": pull.get("head"), + "head_matches": bool(row.get("head") and row["head"] == pull.get("head")), + "base": row.get("base"), "body_base": pull.get("base"), + "base_matches": bool(row.get("base") and row["base"] == pull.get("base")), + "updated_at": pull.get("updated_at"), "draft": pull.get("draft", False), + "matched_paths": matched, "changed_paths": len(row["paths"]), + "truncated": row.get("truncated", False)}) + return matches + + +def fetch_discussions(): + records = [] + cursor = None + cursors = set() + while True: + result = graphql('''query($cursor:String) { + repository(owner:"CrocSwap",name:"integer-mult-bounds") { + discussions(first:10,after:$cursor) { + pageInfo {hasNextPage endCursor} + nodes {id number title body url updatedAt comments(first:25) { + pageInfo {hasNextPage endCursor} + nodes {id body url updatedAt replies(first:10) { + pageInfo {hasNextPage endCursor} nodes {body url updatedAt} + }} + }} + } + } + }''', {"cursor": cursor})["repository"]["discussions"] + for discussion in result["nodes"]: + common = {"number": discussion["number"], "title": discussion["title"]} + records.append({**common, "kind": "discussion", "body": discussion["body"], + "url": discussion["url"], "updated_at": discussion["updatedAt"]}) + # Batch small first pages without a process/API round trip for + # every discussion. The worst-case nested node count is only + # 10 + 10*25 + 10*25*10 = 2760; busy threads still page fully. + connection = discussion["comments"] + comment_cursors = set() + while True: + for comment in connection["nodes"]: + records.append({**common, "kind": "discussion-comment", "body": comment["body"], + "url": comment["url"], "updated_at": comment["updatedAt"]}) + replies = comment["replies"] + reply_cursors = set() + while True: + for reply in replies["nodes"]: + records.append({**common, "kind": "discussion-reply", "body": reply["body"], + "url": reply["url"], "updated_at": reply["updatedAt"]}) + if not replies["pageInfo"]["hasNextPage"]: + break + replies = graphql('''query($id:ID!, $cursor:String) { + node(id:$id) { ... on DiscussionComment { + replies(first:100,after:$cursor) { + pageInfo {hasNextPage endCursor} nodes {body url updatedAt} + } + }} + }''', {"id": comment["id"], "cursor": next_cursor(replies["pageInfo"], reply_cursors, "replies")})["node"]["replies"] + if not connection["pageInfo"]["hasNextPage"]: + break + connection = graphql('''query($id:ID!, $cursor:String) { + node(id:$id) { ... on Discussion { + comments(first:100,after:$cursor) { + pageInfo {hasNextPage endCursor} + nodes {id body url updatedAt replies(first:100) { + pageInfo {hasNextPage endCursor} nodes {body url updatedAt} + }} + } + }} + }''', {"id": discussion["id"], + "cursor": next_cursor(connection["pageInfo"], comment_cursors, "comments")})["node"]["comments"] + if not result["pageInfo"]["hasNextPage"]: + return records + cursor = next_cursor(result["pageInfo"], cursors, "connection") + + +def atomic_json(path, data): + path.parent.mkdir(parents=True, exist_ok=True) + tmp = None + try: + with tempfile.NamedTemporaryFile(mode="w", encoding="utf-8", dir=path.parent, delete=False) as out: + tmp = Path(out.name) + json.dump(data, out) + out.write("\n") + os.replace(tmp, path) + finally: + if tmp is not None: + tmp.unlink(missing_ok=True) + + +def error_summary(exc): + if isinstance(exc, subprocess.CalledProcessError): + return (exc.stderr or str(exc))[:700] + return str(exc)[:700] + + +def validate_cached_source(source, name): + """Check cached containers before search code uses their fields.""" + if not isinstance(source, dict) or not isinstance(source.get("records"), list): + raise ValueError("source must contain a records list") + if type(source.get("complete_pagination")) is not bool: + raise ValueError("source must declare pagination coverage") + stamp = datetime.fromisoformat(source["fetched_at"]) + if stamp.tzinfo is None: + raise ValueError("source timestamp needs a UTC offset") + for row in source["records"]: + if not isinstance(row, dict) or type(row.get("number")) is not int: + raise ValueError("record needs an integer number") + if name == "pr_files": + if not isinstance(row.get("paths"), list) or not all(isinstance(p, str) for p in row["paths"]): + raise ValueError("file record needs a list of paths") + elif (not all(isinstance(row.get(key), str) for key in ("kind", "title")) + or not all(isinstance(row[key], str) for key in ("body", "url", "updated_at", "state") if key in row)): + raise ValueError("record text fields must be strings") + return stamp + + +def remote_snapshot(cache, offline=False, refresh=False, max_age=300, need_files=False, + include_closed=False): + warnings = [] + try: + snapshot = json.loads(cache.read_text(encoding="utf-8")) + if (not isinstance(snapshot, dict) or snapshot.get("schema_version") != 1 + or snapshot.get("repository") != REPO or not isinstance(snapshot.get("sources"), dict)): + raise ValueError("cache schema or repository mismatch") + except FileNotFoundError: + snapshot = {"schema_version": 1, "repository": REPO, "sources": {}} + except (OSError, ValueError) as exc: + warnings.append("cache: unusable snapshot: " + error_summary(exc)) + snapshot = {"schema_version": 1, "repository": REPO, "sources": {}} + sources = snapshot.setdefault("sources", {}) + fetches = [("open_prs", lambda: pull_records(gh_json([ + "--paginate", "--slurp", f"repos/{REPO}/pulls?state=open&per_page=100&sort=updated&direction=desc"]))), + ("discussions", fetch_discussions)] + if include_closed: + # Separate coverage and cache identity: an open-only snapshot cannot + # satisfy a request for closed (including merged) PR bodies. + fetches.append(("closed_prs", lambda: pull_records(gh_json([ + "--paginate", "--slurp", f"repos/{REPO}/pulls?state=closed&per_page=100&sort=updated&direction=desc"])))) + if need_files: + # Only path queries pay for the changed-file index; body searches stay as they were. + fetches.append(("pr_files", fetch_pr_files)) + for name, fetch in fetches: + cached = sources.get(name) + age = None + if cached is not None: + try: + stamp = validate_cached_source(cached, name) + age = (datetime.now(timezone.utc) - stamp).total_seconds() + except (KeyError, ValueError, TypeError) as exc: + warnings.append(f"{name}: unusable cached source: {error_summary(exc)}") + sources.pop(name, None) + cached = None + fresh = cached is not None and age is not None and 0 <= age <= max_age + if need_files and name in ("pr_files", "open_prs") and cached: + # Old schema-1 caches omitted endpoints. Refresh only affected sources; + # offline/failed refreshes retain the evidence with explicit warnings. + fresh = fresh and all(row.get("head") and row.get("base") for row in cached["records"]) + if offline: + warnings.append(f"{name}: offline; cached data may miss current changes") + elif refresh or not fresh: + try: + # Replace each source only after every page succeeds. + records = fetch() + replacement = {"fetched_at": now(), "records": records, "complete_pagination": True} + pending = {**snapshot, "sources": {**sources, name: replacement}} + atomic_json(cache, pending) + sources[name] = replacement + cached = replacement + except (OSError, ValueError, KeyError, TypeError, subprocess.SubprocessError) as exc: + warnings.append(f"{name}: refresh failed; using old data if available: {error_summary(exc)}") + if not cached: + warnings.append(f"{name}: no usable remote snapshot") + sources.pop(name, None) + return snapshot, warnings + + +def pull_corpus(snapshot, include_closed=False): + """One body per PR; a state change between snapshots must not double it.""" + pulls = {} + names = ("open_prs", "closed_prs") if include_closed else ("open_prs",) + for name in names: + for row in snapshot["sources"].get(name, {}).get("records", []): + old = pulls.get(row["number"]) + # GitHub updated_at uses one UTC spelling. Prefer the newer record; + # an exact tie keeps the open state. Coverage timestamps remain + # separate: this is not an atomic server snapshot. + key = lambda item: (item["updated_at"], item["state"] == "open") + if old is None or key(row) > key(old): + pulls[row["number"]] = row + return list(pulls.values()) + + +def local_records(root): + records = [] + # Deliberately avoid vendored trees and large binary artifacts. + paths = sorted([*(root / "research").glob("*/README.md"), *(root / "research").glob("*/TASK.json")]) + ledger = root / "docs/research/pr-ledger.md" + if ledger.is_file(): + paths.append(ledger) + for path in paths: + try: + path = relative_file(root, path.relative_to(root).as_posix()) + except ValueError: + # Aliases and non-files do not count as searched local notes. + continue + data = path.read_bytes() + body = data.decode("utf-8", errors="replace") + name = path.relative_to(root).as_posix() + kind = {"README.md": "local-readme", "TASK.json": "local-task"}.get(path.name, "local-ledger") + records.append({"kind": kind, + "title": name, "path": name, "body": body, + "sha256": hashlib.sha256(data).hexdigest()}) + return records + + +def local_coverage(root, records): + """Compare searched working-tree notes with the regular note paths at HEAD.""" + coverage = {"head": None, "records": len(records), "head_records": None, + "complete_head_inventory": None, "unsearched_head_paths": None} + try: + def git(*args): + result = subprocess.run( + ["git", "--no-replace-objects", *args], cwd=root, + capture_output=True, timeout=15, + env={**git_source_env(), "GIT_NO_LAZY_FETCH": "1"}, + ) + # Text-mode pipes normalize CR and CRLF even in `ls-tree -z` + # paths. Decode bytes explicitly so distinct Git names stay distinct. + # Keep failure diagnostics textual for the existing warning path. + result.stdout = result.stdout.decode("utf-8", errors="surrogateescape") + result.stderr = result.stderr.decode("utf-8", errors="surrogateescape") + result.check_returncode() + return result.stdout + + # Pin the tree once; only metadata is read, never omitted note blobs. + coverage["head"] = git("rev-parse", "--verify", "HEAD^{commit}").strip() + tree = git("ls-tree", "-r", "-z", "--full-tree", coverage["head"], "--", + "research", "docs/research/pr-ledger.md") + expected = set() + for entry in tree.split("\0"): + if not entry: + continue + metadata, path = entry.split("\t", 1) + mode, kind, _ = metadata.split() + parts = PurePosixPath(path).parts + in_scope = (path == "docs/research/pr-ledger.md" + or (len(parts) == 3 and parts[0] == "research" + and parts[2] in ("README.md", "TASK.json"))) + if in_scope and kind == "blob" and mode in ("100644", "100755"): + expected.add(path) + except (OSError, subprocess.SubprocessError, ValueError) as exc: + return coverage, ["local: HEAD note inventory unavailable; local coverage is unknown. " + + error_summary(exc)] + missing = sorted(expected - {record["path"] for record in records}) + coverage.update(head_records=len(expected), complete_head_inventory=not missing, + unsearched_head_paths=missing) + warnings = ([f"local: {len(missing)} HEAD note paths were not searched; " + "inspect coverage.local.unsearched_head_paths in --json output and materialize relevant notes " + "before treating a lack of matches as evidence."] if missing else []) + return coverage, warnings + + +def match_position(text, term, pattern): + if pattern is None: + return text.find(term) + # Numeric-reference patterns contain the complete literal term. Avoid a + # boundary-aware regex scan when that mandatory substring is absent. + if term not in text: + return -1 + match = pattern.search(text) + return match.start() if match else -1 + + +def original_offset(text, lowered_offset): + """Map a lowercase search offset back to the unchanged source text.""" + if text.isascii(): + return lowered_offset + for index, char in enumerate(text): + width = len(char.lower()) + if lowered_offset < width: + return index + lowered_offset -= width + return len(text) + + +def search(records, terms, all_terms=False, reference_terms=()): + terms = list(dict.fromkeys(t.strip().lower() for t in terms if t.strip())) + if not terms: + raise ValueError("provide at least one nonempty search term") + # Numeric card references name predecessors, rather than digits inside a kappa + # or a source hash. Explicit user terms retain literal substring semantics. + patterns = {term: re.compile(r"(?= 0 or body_positions[t] >= 0] + if not found or (all_terms and len(found) != len(terms)): + continue + positions = [body_positions[t] for t in found if body_positions[t] >= 0] + start = max(0, original_offset(record.get("body", ""), min(positions)) - 100) if positions else 0 + matches.append({**{k: v for k, v in record.items() if k != "body"}, + "matched_terms": found, + "score": sum(3 if title_positions[t] >= 0 else 1 for t in found), + "excerpt": record.get("body", "")[start:start + 500]}) + return sorted(matches, key=lambda r: (-r["score"], r["kind"], r["title"], r.get("url", ""))) + + +def main(): + ap = argparse.ArgumentParser(description=__doc__) + ap.add_argument("terms", nargs="*", help="literal case-insensitive terms; quote multiword phrases") + ap.add_argument("--task", help="add the named catalogue card's overlap terms") + ap.add_argument("--path", action="append", default=[], metavar="PATTERN", + help="match open PRs by case-sensitive Git path glob or substring; spaces are literal; repeatable") + ap.add_argument("--offline", action="store_true") + ap.add_argument("--refresh", action="store_true") + ap.add_argument("--include-closed", action="store_true", + help="also search all paginated closed/merged PR titles and bodies (extra fetch/cache)") + ap.add_argument("--max-age", type=int, default=300, help="remote cache lifetime in seconds") + ap.add_argument("--limit", type=int, default=20) + ap.add_argument("--all-terms", action="store_true") + ap.add_argument("--json", action="store_true", dest="json_output") + ap.add_argument("--cache", type=Path, default=Path(os.environ.get("XDG_CACHE_HOME", str(Path.home() / ".cache"))) / "integer-mult-bounds/overlap.json") + args = ap.parse_args() + if args.max_age < 0 or args.limit < 1 or (args.offline and args.refresh): + ap.error("use nonnegative max-age, positive limit, and either offline or refresh") + if args.include_closed and args.path: + ap.error("--include-closed searches titles/bodies only; omit --path (its index covers open PRs)") + if args.cache.resolve().is_relative_to(ROOT.resolve()): + ap.error("keep the cache outside the repository") + terms = list(args.terms) + explicit_terms = {term.strip().lower() for term in args.terms} + reference_terms = [] + if args.task: + catalog = json.loads((ROOT / "docs/research/agent-task-catalog.json").read_text(encoding="utf-8")) + cards = [t for tr in catalog["tracks"] for t in tr["tasks"] if t["id"] == args.task] + if not cards: + ap.error("unknown task") + terms.extend([args.task, *cards[0].get("overlap_terms", [])]) + # A leading-zero digit string can be a word, e.g. census witness + # 01021323. Restrict reference matching to canonical positive integers. + reference_terms = list(dict.fromkeys( + t.strip() for t in cards[0].get("overlap_terms", []) + if t.strip().isascii() and t.strip().isdecimal() and not t.strip().startswith("0") + and t.strip() not in explicit_terms)) + # Catalogue paths are repository-relative POSIX paths. Nested sources + # identify their package, not generic directories such as evidence/code. + for point in cards[0]["starting_points"]: + parts = PurePosixPath(point).parts + if len(parts) >= 3 and parts[0] == "research": + terms.append(parts[1]) + if not any(t.strip() for t in terms) and not args.path: + ap.error("provide terms, --task or --path") + patterns = list(args.path) + if any(not p for p in patterns): + ap.error("--path must not be empty") + local = local_records(ROOT) + local_status, local_warnings = local_coverage(ROOT, local) + snapshot, warnings = remote_snapshot(args.cache, args.offline, args.refresh, args.max_age, + need_files=bool(patterns), include_closed=args.include_closed) + warnings = local_warnings + warnings + records = list(local) + pull_corpus(snapshot, args.include_closed) + coverage = {"local": local_status} + corpus_names = ("open_prs", "discussions", "closed_prs") if args.include_closed else ("open_prs", "discussions") + for name in corpus_names: + source = snapshot["sources"].get(name) + coverage[name] = {k: source[k] for k in ("fetched_at", "complete_pagination")} if source else None + if source: + if name == "discussions": + records.extend(source["records"]) + coverage[name]["records"] = len(source["records"]) + path_matches = [] + if patterns: + source = snapshot["sources"].get("pr_files") + coverage["pr_files"] = {k: source[k] for k in ("fetched_at", "complete_pagination")} if source else None + if source: + coverage["pr_files"]["records"] = len(source["records"]) + truncated = sorted({row["number"] for row in source["records"] if row.get("truncated", False)}) + coverage["pr_files"]["truncated_prs"] = truncated + if truncated: + warnings.append(f"pr_files: incomplete file inventories for {len(truncated)} PRs; " + "paths may be missing. Check truncated_prs in coverage, even when no path matches.") + pulls = snapshot["sources"].get("open_prs", {"records": []})["records"] + heads = {pull["number"]: pull.get("head") for pull in pulls} + file_heads = {row["number"]: row.get("head") for row in source["records"]} + unverified = sorted(number for number in heads.keys() | file_heads.keys() + if not file_heads.get(number) or file_heads[number] != heads.get(number)) + coverage["pr_files"]["unverified_heads"] = unverified + if unverified: + warnings.append(f"pr_files: file-head binding unverified for {len(unverified)} PRs; " + "paths may be stale. Refresh and recheck the heads listed in coverage.") + bases = {pull["number"]: pull.get("base") for pull in pulls} + file_bases = {row["number"]: row.get("base") for row in source["records"]} + unverified_bases = sorted(number for number in bases.keys() | file_bases.keys() + if not file_bases.get(number) or file_bases[number] != bases.get(number)) + coverage["pr_files"]["unverified_bases"] = unverified_bases + if unverified_bases: + warnings.append(f"pr_files: file-base binding unverified for {len(unverified_bases)} PRs; " + "paths may be stale. Refresh and recheck the bases listed in coverage.") + path_matches = path_report(pulls, source["records"], patterns) + matches = search(records, terms, args.all_terms, reference_terms) if any(t.strip() for t in terms) else [] + if patterns and matches: + # With both axes given, a record must match at least one term and one path. + hits = {m["number"]: m for m in path_matches} + matches = [dict(m, **{key: hits[m["number"]][key] + for key in ("matched_paths", "head", "body_head", "head_matches", + "base", "body_base", "base_matches", "truncated")}) for m in matches + if m.get("kind") == "pr" and m.get("number") in hits] + if patterns and not any(t.strip() for t in terms): + matches = [] + report = {"status": "POSSIBLE_OVERLAP_NOT_A_NOVELTY_CERTIFICATE", "searched_at": now(), + "terms": terms, "coverage": coverage, "local_records": len(local), "warnings": warnings, + "reference_terms": reference_terms, + "limits": (("Remote PR scope includes open and closed/merged titles and bodies, with separate coverage and timestamps. " + "State changes are deduplicated by latest PR update; snapshots are not atomic. " + if args.include_closed else + "Remote PR scope is open PRs including drafts. Closed work is searched through local READMEs, TASK metadata and ledger only. ") + + "Local coverage compares searched working-tree notes with regular note paths at coverage.local.head; " + "it does not certify HEAD contents or historical coverage. " + "No code diffs or arbitrary package files are searched. Recheck relevant heads before submission."), + "matches_total": len(matches), "matches": matches[:args.limit]} + if patterns: + report["path_patterns"] = patterns + report["path_limits"] = ("Path matching covers the changed file paths GitHub reports for open PRs, " + "not diff contents; all available file pages are followed. Missing lists or " + "advertised-count shortfalls are flagged truncated and listed in coverage.pr_files.truncated_prs " + "independently of matches or --limit. complete_pagination describes list paging, " + "not completeness of each PR's file inventory. Head/base identify the file " + "snapshot; body_head/body_base identify PR metadata. Both endpoints must match. " + "This is not an atomic server snapshot. Unverified joins are flagged, " + "including inventories with no matching path.") + report["path_matches_total"] = len(path_matches) + report["path_matches"] = path_matches[:args.limit] + if args.json_output: + print(json.dumps(report, indent=2)) + else: + # Excerpts quote arbitrary UTF-8; a cp1252 pipe must escape them, not crash. + if hasattr(sys.stdout, "reconfigure"): + sys.stdout.reconfigure(errors="backslashreplace") + print(report["status"]) + # A sparse checkout can omit thousands of notes. Keep the complete + # path list in JSON; the text report surfaces the count and warning. + local_summary = {k: v for k, v in local_status.items() if k != "unsearched_head_paths"} + omitted = local_status.get("unsearched_head_paths") + local_summary["unsearched_head_paths_count"] = None if omitted is None else len(omitted) + print("Coverage: " + json.dumps({**coverage, "local": local_summary})) + if reference_terms: + print("Task references (identifier matching): " + ", ".join(reference_terms)) + print(report["limits"]) + if patterns: + print(report["path_limits"]) + for warning in warnings: + print("WARNING: " + warning) + print(f"{len(matches)} possible matches; showing {min(args.limit, len(matches))}") + for match in matches[:args.limit]: + flags = " (file list truncated)" if match.get("truncated") else "" + print(f"\n[{match['kind']}] {match['title']}{flags}\n{match.get('url', match.get('path'))}") + if match.get("head"): + print("Head: " + match["head"]) + if match.get("head_matches") is False: + print("File-head binding unverified; body head: " + str(match.get("body_head"))) + print(match["excerpt"]) + if patterns: + print(f"\n{len(path_matches)} open PRs match paths {patterns}; " + f"showing {min(args.limit, len(path_matches))}") + for match in path_matches[:args.limit]: + flags = " (file list truncated)" if match["truncated"] else "" + print(f"\n[pr {match['number']}] {match['title']}{flags}\n{match['url']}") + if match.get("head"): + print("Head: " + match["head"]) + if not match["head_matches"]: + print("File-head binding unverified; body head: " + str(match["body_head"])) + print("Matched paths: " + ", ".join(match["matched_paths"])) + + +if __name__ == "__main__": + main() diff --git a/research/pr6334-posix-path-audit/retained/pr6334/scripts/research_package.py b/research/pr6334-posix-path-audit/retained/pr6334/scripts/research_package.py new file mode 100644 index 0000000000..091b4c8f58 --- /dev/null +++ b/research/pr6334-posix-path-audit/retained/pr6334/scripts/research_package.py @@ -0,0 +1,1053 @@ +#!/usr/bin/env python3 +"""Validate and replay explicitly registered research packages, never every verify.py. + +Douglas Colkitt, with OpenAI Codex assistance. Apache-2.0. +""" +import argparse +import errno +from contextlib import closing, contextmanager +from itertools import chain +import hashlib +import importlib.metadata +import json +import os +from pathlib import Path, PurePosixPath +import re +import signal +import stat +import subprocess +import sys +import tempfile +import threading + +ROOT = Path(__file__).resolve().parents[1] +GIT_BLOB_RESPONSE_TIMEOUT = 90 + + +def require(ok, message): + if not ok: + raise ValueError(message) + + +def canonical_relative_path(name): + # Git's changed paths use one repository-relative POSIX spelling. Accepting + # aliases here would let a declared input evade the planner's exact match. + require(isinstance(name, str), "canonical relative path must be a string") + path = PurePosixPath(name) + require(bool(path.parts) and not path.is_absolute() and ".." not in path.parts + and "\\" not in name and path.as_posix() == name, + "use a canonical repository-relative POSIX path: " + name) + return path + + +def symlink_free_relative_path(root, name): + """Reject symlink/junction components without requiring a deleted input to exist. + + Git reports changes under the target's path, not a symlink alias declared + by its consumer. Inspect existing components below the repository root; + the checkout must remain stable during planning, validation and replay. + """ + parts = canonical_relative_path(name).parts + current = root + for part in parts: + current = current / part + try: + info = current.lstat() + except (FileNotFoundError, NotADirectoryError): + break + require(not stat.S_ISLNK(info.st_mode), "symlink in repository-relative path: " + name) + # Windows junctions have directory mode, not S_IFLNK. Inspect the same + # lstat result so even a dangling junction is rejected before traversal. + require(getattr(info, "st_reparse_tag", 0) != getattr(stat, "IO_REPARSE_TAG_MOUNT_POINT", None), + "junction in repository-relative path: " + name) + return root / name + + +def relative_file(root, name): + result = symlink_free_relative_path(root, name) + require(result.resolve().is_relative_to(root.resolve()) and result.is_file(), + "missing or external file: " + name) + return result + + +# Adapted from the package-local policy in +# research/t74-2530-source-partner-audit/audit_common.py (Apache-2.0). +# Keep repository configuration and linked-worktree .git discovery intact; +# inherited shell overrides must not choose a different repository or index. +_GIT_REDIRECT_VARS = frozenset(( + "GIT_DIR", "GIT_WORK_TREE", "GIT_INDEX_FILE", "GIT_OBJECT_DIRECTORY", + "GIT_ALTERNATE_OBJECT_DIRECTORIES", "GIT_NAMESPACE", "GIT_CEILING_DIRECTORIES", + "GIT_COMMON_DIR", "GIT_CONFIG_PARAMETERS", "GIT_CONFIG_COUNT", +)) +_GIT_CONFIG_PREFIXES = ("GIT_CONFIG_KEY_", "GIT_CONFIG_VALUE_") + + +def git_source_env(): + """Use cwd's repository and stored objects, without changing the parent. + + Git can ignore cwd when inherited selection variables point elsewhere. + Config-injection variables can also redirect worktree/index operations. + This policy is only for Git metadata/source subprocesses, not verifiers. + """ + env = {name: value for name, value in os.environ.items() + if name not in _GIT_REDIRECT_VARS and not name.startswith(_GIT_CONFIG_PREFIXES)} + # Replacement refs can report an original ID while returning other bytes. + env["GIT_NO_REPLACE_OBJECTS"] = "1" + return env + + +def git(root, *args): + return subprocess.run(["git", *args], cwd=root, env=git_source_env(), + capture_output=True, check=True).stdout + + +def tracked_input_paths(root): + """Use HEAD spelling, never case folding, filesystem resolution or the index. + + Staged/untracked files are not replay inputs until committed. NUL framing + preserves Git filenames containing whitespace, including CR and LF. + """ + raw = subprocess.run( + ["git", "ls-tree", "-r", "--name-only", "-z", "HEAD"], cwd=root, env=git_source_env(), + capture_output=True, check=True, timeout=30).stdout + return frozenset(name.decode("utf-8") for name in raw.split(b"\0") if name) + + +def require_tracked_input(name, paths): + canonical_relative_path(name) + require(name in paths, "use an exact Git-tracked input path from HEAD: " + name) + + +def git_source_rows(root, base, names): + """Read one pinned object at a time through one Git process; no persistent cache. + + Each request is consumed before the next is sent. This avoids pipe deadlocks + and retains the old one-blob memory bound, even for large source archives. + Newline paths and non-blob objects retain the legacy Git-show semantics. + The caller must close this generator if source validation fails mid-stream. + """ + with subprocess.Popen(["git", "cat-file", "--batch"], cwd=root, env=git_source_env(), + stdin=subprocess.PIPE, stdout=subprocess.PIPE, + stderr=subprocess.PIPE) as process: + try: + for name in names: + expression = base + ":" + name + if "\n" in expression or "\r" in expression: + yield git(root, "rev-parse", expression).decode().strip(), git(root, "show", expression) + continue + process.stdin.write(expression.encode("utf-8") + b"\n") + process.stdin.flush() + header = process.stdout.readline().rstrip(b"\n").split(b" ") + require(len(header) == 3 and re.fullmatch(rb"[0-9a-f]{40,64}", header[0]) + and header[2].isdigit(), "missing or invalid Git source: " + name) + size = int(header[2]) + payload = process.stdout.read(size) + require(len(payload) == size and process.stdout.read(1) == b"\n", + "truncated Git source: " + name) + if header[1] != b"blob": + payload = git(root, "show", expression) + yield header[0].decode("ascii"), payload + process.stdin.close() + code = process.wait(timeout=90) + require(code == 0, "Git source reader failed: " + process.stderr.read().decode("utf-8", errors="replace")) + finally: + if not process.stdin.closed: + process.stdin.close() + if process.poll() is None: + try: + process.wait(timeout=5) + except subprocess.TimeoutExpired: + process.kill() + process.wait() + + +class _UnsupportedNulBatch(Exception): + """Git rejected the initial NUL-mode request before yielding any source.""" + + +def _closed_stdin_error(error): + # Windows can report EINVAL instead of EPIPE when a child closes stdin. + # This classifies the pipe error only; the retry still requires exit 129. + return (isinstance(error, BrokenPipeError) + or (os.name == "nt" and error.errno == errno.EINVAL)) + + +def _stop_git_blob_process(process): + """Stop this reader's owned POSIX group even after its leader exits.""" + try: + if os.name == "posix": + # Reap an exited leader first: signaling a group containing only + # its unreaped zombie can report EPERM on macOS. A surviving + # descendant keeps the group alive after the leader is reaped. + process.poll() + os.killpg(process.pid, signal.SIGKILL) + else: + process.kill() + except ProcessLookupError: + pass + + +@contextmanager +def _git_blob_response_deadline(stop): + """Bound one response, excluding time spent by its consumer. + + On POSIX the reader owns a session: ordinary descendants retaining a pipe + are stopped with the leader. Escaped sessions and separate git() calls are + outside this contract; non-POSIX retains the direct-child deadline. + """ + expired = threading.Event() + + def terminate(): + expired.set() + stop() + + timer = threading.Timer(GIT_BLOB_RESPONSE_TIMEOUT, terminate) + timer.start() + try: + yield + finally: + timer.cancel() + timer.join() + if expired.is_set(): + raise subprocess.TimeoutExpired("git cat-file blob response", GIT_BLOB_RESPONSE_TIMEOUT) + + +def _git_blob_batch(root, specs, nul): + argv = ["git", "cat-file", "--batch"] + (["-z"] if nul else []) + # Usage diagnostics from the unsupported-option probe are expected. Avoid + # a second pipe that could block while we consume the framed stdout reply. + with subprocess.Popen(argv, cwd=root, env=git_source_env(), stdin=subprocess.PIPE, + stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, + start_new_session=(os.name == "posix")) as process: + stopped = False + + def stop(): + nonlocal stopped + if not stopped: + _stop_git_blob_process(process) + stopped = True + + try: + for index, spec in enumerate(specs): + require("\0" not in spec, "invalid Git object request") + request = spec + if not nul and ("\n" in spec or "\r" in spec): + # Resolve one literal argument, never split a path into requests. + request = git(root, "rev-parse", "--verify", "--end-of-options", spec).decode("ascii").strip() + require(re.fullmatch(r"[0-9a-f]{40,64}", request), "invalid Git object ID") + with _git_blob_response_deadline(stop): + try: + process.stdin.write(request.encode("utf-8") + (b"\0" if nul else b"\n")) + process.stdin.flush() + except OSError as exc: + if not _closed_stdin_error(exc): + raise + header = b"" + else: + header = process.stdout.readline() + if nul and index == 0 and not header and process.wait(timeout=90) == 129: + raise _UnsupportedNulBatch(spec) + fields = header.rstrip(b"\n").split(b" ") + require(len(fields) == 3 and re.fullmatch(rb"[0-9a-f]{40,64}", fields[0]) + and fields[1] == b"blob" and fields[2].isdigit(), + "missing or non-blob Git source: " + spec) + size = int(fields[2]) + pinned = process.stdout.read(size) + require(len(pinned) == size and process.stdout.read(1) == b"\n", + "incomplete Git blob response: " + spec) + yield fields[0].decode("ascii"), pinned + process.stdin.close() + require(process.wait(timeout=90) == 0, "Git source reader failed") + finally: + # Terminate ordinary descendants before closing buffered pipes. This + # also covers successful leader exit and an early generator close. + if os.name == "posix": + stop() + # Option rejection can close stdin before our first write/flush. + try: + process.stdin.close() + except OSError as exc: + if not _closed_stdin_error(exc): + raise + process.stdout.close() + if process.poll() is None: + try: + process.wait(timeout=5) + except subprocess.TimeoutExpired: + process.kill() + process.wait() + + +def git_blobs(root, specs): + """Stream exact blobs, retaining the one-process NUL protocol on newer Git. + + Git without -z rejects it with usage status 129. Retry only before any + response, replaying the consumed first request through the line protocol. + The fallback resolves CR/LF paths to object IDs before requesting content. + The caller must close this iterator if validation stops before exhaustion. + """ + specs = iter(specs) + sentinel = object() + first = next(specs, sentinel) + if first is sentinel: + return + with closing(_git_blob_batch(root, chain((first,), specs), nul=True)) as rows: + try: + yield from rows + except _UnsupportedNulBatch as exc: + with closing(_git_blob_batch(root, chain((exc.args[0],), specs), nul=False)) as legacy: + yield from legacy + + +def meaningful(value): + return isinstance(value, str) and bool(value.strip()) and not re.search(r"\bTODO\b", value, re.I) + + +def brief(value): + text = json.dumps(value, ensure_ascii=True, sort_keys=True) + return text if len(text) <= 120 else text[:117] + "..." + + +def require_key(mapping, key, where, shape=None): + """Name the missing key and its expected shape instead of a bare KeyError.""" + require(isinstance(mapping, dict), + where + " must be a JSON object; found " + type(mapping).__name__ + ": " + brief(mapping)) + require(key in mapping, where + "." + key + " is missing" + + ("; expected " + shape if shape else "")) + return mapping[key] + + +def require_text(mapping, key, where, shape="a non-empty string without TODO"): + """Name the key and distinguish missing, wrong-shaped and weak values.""" + require(isinstance(mapping, dict), + where + " must be a JSON object; found " + type(mapping).__name__ + ": " + brief(mapping)) + require(key in mapping, where + "." + key + " is missing; expected " + shape) + value = mapping[key] + require(isinstance(value, str) and meaningful(value), + where + "." + key + " must be " + shape + "; found " + type(value).__name__ + ": " + brief(value)) + + +def card_differences(own, pinned): + """Name every key by which a TASK.json original_card differs from its pin.""" + if not isinstance(own, dict): + return "TASK.json original_card is " + type(own).__name__ + ", expected a JSON object" + details = [] + missing = [key for key in pinned if key not in own] + extra = [key for key in own if key not in pinned] + if missing: + details.append("missing keys: " + ", ".join(missing)) + if extra: + details.append("unexpected keys: " + ", ".join(extra)) + for key in (key for key in pinned if key in own and own[key] != pinned[key]): + details.append(key + " differs (TASK.json: " + brief(own[key]) + "; pinned: " + brief(pinned[key]) + ")") + return "; ".join(details) or "cards compare unequal without a key difference" + + +def source_pin(row, base): + """Resolve one SOURCES.json row to its exact Git object expression. + + Rows without an explicit pin keep the historical base_commit: form. + A file vendored from a pull-request head is pinned exactly with + head:<40-hex sha>:, so its bytes resolve at that commit, not at the + package base. An explicit but unparseable pin fails closed. + """ + name = row["path"] + pin = row.get("pin") + if pin is None: + return base + ":" + name + if pin == "base_commit:" + name: + return base + ":" + name + # Git filenames can contain LF; it is part of the exact path, not a delimiter. + match = re.fullmatch(r"head:([0-9a-f]{40}):(.+)", pin, re.DOTALL) if isinstance(pin, str) else None + if match is not None and match.group(2) == name: + return match.group(1) + ":" + name + raise ValueError("SOURCES.json pin for " + name + " is not a supported pin form: " + brief(pin) + + "; accepted pin forms are base_commit: and head:<40-hex sha>:") + + +def file_equals_bytes(path, expected): + """Compare a stable regular file exactly, with at most a 1 MiB buffer. + + Read the whole initial extent even after a mismatch, propagating I/O errors. + The caller retains responsibility for path validation and checkout stability. + """ + with path.open("rb") as stream: + size = os.fstat(stream.fileno()).st_size + capacity = min(1024 * 1024, max(1, size)) + buffer = bytearray(capacity) + actual = memoryview(buffer) + wanted = memoryview(expected) + matches = size == len(expected) + for offset in range(0, size, capacity): + count = stream.readinto(buffer) + assert 0 <= count <= capacity + if count != min(capacity, size - offset) or actual[:count] != wanted[offset:offset + count]: + matches = False + tail = stream.read(1) + return matches and not tail + + +def validate(root, package, check_pins=True): + parts = PurePosixPath(package).parts + require(len(parts) == 2 and parts[0] == "research" and re.fullmatch(r"[a-z0-9][a-z0-9-]*", parts[1]), + "package must be research/") + data = {} + for name in ("TASK.json", "SOURCES.json", "VERIFY.json"): + data[name] = json.loads(relative_file(root, package + "/" + name).read_text(encoding="utf-8")) + version = require_key(data[name], "schema_version", name) + require(version == 1, name + ".schema_version must be 1; found " + brief(version)) + task, sources, manifest = (data[n] for n in ("TASK.json", "SOURCES.json", "VERIFY.json")) + scope_status = require_key(task, "scope_status", "TASK.json", "unchanged, refined or changed") + require(scope_status in ("unchanged", "refined", "changed"), + "TASK.json.scope_status must be unchanged, refined or changed; found " + brief(scope_status)) + for field in ("actual_scope", "bottleneck_link", "stop_rule", "evidence", "author", "ai_assistance"): + require_text(task, field, "TASK.json") + if scope_status == "unchanged": + question = require_key(require_key(task, "original_card", "TASK.json", + "the pinned catalogue card object"), + "question", "TASK.json.original_card") + require(task["actual_scope"] == question, + "different wording/scope requires refined or changed plus a reason" + + " (TASK.json.actual_scope " + brief(task["actual_scope"]) + + " differs from TASK.json.original_card.question " + brief(question) + ")") + else: + require_text(task, "scope_change_reason", "TASK.json", + "a non-empty string without TODO explaining the scope refinement/change") + outcome = require_key(task, "outcome", "TASK.json", + "partial, completed, blocked or negative-result") + require(outcome in ("partial", "completed", "blocked", "negative-result"), + "TASK.json.outcome must be partial, completed, blocked or negative-result; found " + brief(outcome)) + questions = task.get("open_questions") + require(isinstance(questions, list), + "TASK.json.open_questions must be a list; found " + type(questions).__name__ + ": " + brief(questions)) + if not questions: + require_text(task, "no_open_questions_reason", "TASK.json", + "a non-empty string without TODO explaining an empty next-question list") + for position, question in enumerate(questions): + for field in ("question", "why_it_matters", "first_step", "blocked_by"): + require_text(question, field, "TASK.json.open_questions[" + str(position) + "]") + readme = relative_file(root, package + "/README.md").read_text(encoding="utf-8") + require("## Open next questions" in readme, "README.md needs an \"## Open next questions\" section") + status = require_key(manifest, "status", "VERIFY.json") + require(status == "ready", + "VERIFY.json.status must be \"ready\"; complete the draft before requesting replay; found " + brief(status)) + python = require_key(manifest, "python", "VERIFY.json") + require(python == "3.12", + "VERIFY.json.python must be \"3.12\" (generic replay currently uses Python 3.12); found " + brief(python)) + requirements = manifest.get("requirements", []) + require(isinstance(requirements, list), + "VERIFY.json.requirements must be a list of exact \"name==version\" pins; found " + + type(requirements).__name__ + ": " + brief(requirements)) + require(len(requirements) == len(set(requirements)), "VERIFY.json.requirements must not repeat a pin") + for item in requirements: + require(isinstance(item, str) + and re.fullmatch(r"(?:numpy|sympy|scipy)==[0-9]+(?:\.[0-9]+){1,2}", item), + "generic CI supports exact numpy/sympy/scipy pins; use a dedicated workflow for other environments" + + "; offending entry: " + brief(item)) + names = [item.split("==")[0] for item in requirements] + require(len(names) == len(set(names)), "declare only one version per dependency") + # Validation already treats an omitted list as empty; execution must receive + # that same default without rewriting the contributor's manifest on disk. + manifest["requirements"] = requirements + commands = manifest.get("commands") + require(isinstance(commands, list) and 1 <= len(commands) <= 8, + "VERIFY.json.commands must be a list of 1..8 explicit verification commands; found " + + type(commands).__name__ + ": " + brief(commands)) + total = 0 + for position, command in enumerate(commands): + path = "VERIFY.json.commands[" + str(position) + "]" + require(isinstance(command, dict), + path + " must be a JSON object; found " + type(command).__name__ + ": " + brief(command)) + argv = command.get("argv") + require(isinstance(argv, list) and len(argv) >= 2 and all(isinstance(a, str) for a in argv), + path + ".argv must be a list of at least 2 strings; found " + brief(argv)) + require(argv[0] == "{python}", path + ".argv must explicitly use {python}; no implicit shell") + require_text(command, "checks", path, "a non-empty string without TODO describing command coverage") + seconds = command.get("timeout_seconds") + require(type(seconds) is int, + path + ".timeout_seconds must be an integer number of seconds; found " + + type(seconds).__name__ + ": " + brief(seconds)) + require(1 <= seconds <= 600, + path + ".timeout_seconds " + str(seconds) + + (" exceeds the 600 s per-command cap" if seconds > 600 + else " is below the 1 s per-command minimum")) + total += seconds + require(total <= 1200, "declared replay budget " + str(total) + + " s (sum of timeout_seconds) exceeds the 1200 s total cap" + + "; use a dedicated job for larger checks") + inputs = manifest.get("inputs") + require(isinstance(inputs, list), + "VERIFY.json.inputs must be a list of canonical repository-relative paths; found " + + type(inputs).__name__ + ": " + brief(inputs)) + tracked = tracked_input_paths(root) + for name in inputs: + symlink_free_relative_path(root, name) + require_tracked_input(name, tracked) + relative_file(root, name) + files = require_key(sources, "files", "SOURCES.json") + require(files, "SOURCES.json.files is empty; declare the source inventory") + base = require_key(sources, "base_commit", "SOURCES.json", "a 40-64 hex string pinning an exact base commit") + require(isinstance(base, str) and re.fullmatch(r"[0-9a-f]{40,64}", base), + "SOURCES.json.base_commit must be a 40-64 hex string pinning an exact base commit; found " + brief(base)) + task_base = require_key(task, "base_commit", "TASK.json", + "a 40-64 hex string equal to SOURCES.json.base_commit") + require(task_base == base, "TASK.json.base_commit " + brief(task_base) + + " must equal SOURCES.json.base_commit " + brief(base)) + for row in files: + require(isinstance(row, dict) and isinstance(row.get("path"), str), + "SOURCES.json.files rows must be JSON objects with a string path; offending row: " + brief(row)) + require(row.get("usage") in ("input", "provenance"), + "SOURCES.json.files row " + row["path"] + " must declare usage \"input\" or \"provenance\"; found " + + brief(row.get("usage"))) + undeclared = sorted({r["path"] for r in sources["files"] if r["usage"] == "input"} - set(inputs)) + require(not undeclared, + "every SOURCES.json files row with usage \"input\" must also be declared in VERIFY.json inputs" + + " (they drive CI invalidation); undeclared: " + ", ".join(undeclared)) + if check_pins: + catalog_name = "docs/research/agent-task-catalog.json" + require(catalog_name in {r["path"] for r in sources["files"]}, + "SOURCES.json.files must pin the originating task catalogue " + catalog_name + + " (a provenance row pinned at base_commit)") + names = [r["path"] for r in sources["files"]] + for name in names: + p = PurePosixPath(name) + require(not p.is_absolute() and ".." not in p.parts, "invalid source path") + specs = [source_pin(row, sources["base_commit"]) for row in sources["files"]] + require(all(spec == sources["base_commit"] + ":" + catalog_name + for name, spec in zip(names, specs) if name == catalog_name), + "originating task catalogue must be pinned at base_commit") + with closing(git_blobs(root, specs)) as objects: + for index, (obj, pinned) in enumerate(objects): + row = sources["files"][index] + name = row["path"] + require(obj == row["git_blob"], "source blob provenance mismatch: " + name) + require(hashlib.sha256(pinned).hexdigest() == row["sha256"], "source SHA256 provenance mismatch: " + name) + if row["usage"] == "input": + require(file_equals_bytes(relative_file(root, name), pinned), "working source drift: " + name) + if name == catalog_name: + catalog = json.loads(pinned) + cards = [t for tr in catalog["tracks"] for t in tr["tasks"] if t["id"] == task["task_id"]] + require(len(cards) == 1, + "TASK.json task_id " + brief(task["task_id"]) + " matches " + str(len(cards)) + + " cards in pinned " + catalog_name + " at base_commit " + sources["base_commit"] + + "; need exactly one") + original_card = require_key(task, "original_card", "TASK.json", + "the pinned catalogue card object (copy --print-card output verbatim)") + if original_card != cards[0]: + raise ValueError("original card in TASK.json must equal the pinned catalogue card object exactly; " + "copy it verbatim from " + catalog_name + " at base_commit " + sources["base_commit"] + + ": " + card_differences(original_card, cards[0])) + return task, manifest + + +def catalogue_card(root, package, task_id): + """The exact original_card object a hand-authored TASK.json must copy. + + Reads the catalogue blob pinned at the package base commit, never the + working tree, because validate() compares original_card against that pin. + """ + sources = json.loads(relative_file(root, package + "/SOURCES.json").read_text(encoding="utf-8")) + name = "docs/research/agent-task-catalog.json" + rows = require_key(sources, "files", "SOURCES.json") + row = next((r for r in rows if isinstance(r, dict) and r.get("path") == name), None) + require(row is not None, "SOURCES.json.files must pin the originating task catalogue " + name) + base = require_key(sources, "base_commit", "SOURCES.json", "a 40-64 hex string pinning an exact base commit") + spec = source_pin(row, base) + require(spec == base + ":" + name, "originating task catalogue must be pinned at base_commit") + catalog = json.loads(git(root, "show", spec)) + cards = [t for tr in catalog["tracks"] for t in tr["tasks"] if t["id"] == task_id] + require(len(cards) == 1, + "task_id " + brief(task_id) + " matches " + str(len(cards)) + + " cards in pinned " + name + " at base_commit " + base + "; need exactly one") + return cards[0] + + +_WINDOWS_GATE = r""" +import ctypes +from ctypes import wintypes +import json +import subprocess +import sys +api = ctypes.WinDLL('kernel32', use_last_error=True) +api.OpenEventW.argtypes = [wintypes.DWORD, wintypes.BOOL, wintypes.LPCWSTR] +api.OpenEventW.restype = wintypes.HANDLE +api.WaitForSingleObject.argtypes = [wintypes.HANDLE, wintypes.DWORD] +api.WaitForSingleObject.restype = wintypes.DWORD +api.CloseHandle.argtypes = [wintypes.HANDLE] +api.CloseHandle.restype = wintypes.BOOL +event = api.OpenEventW(0x00100000, False, sys.argv[1]) +if not event: + raise ctypes.WinError(ctypes.get_last_error()) +try: + if api.WaitForSingleObject(event, 30000) != 0: + raise RuntimeError('replay ownership gate was not released') +finally: + api.CloseHandle(event) +# No verification command or descendant starts until the parent owns this job. +sys.exit(subprocess.call(json.loads(sys.argv[2]))) +""" + +def run_windows_command(argv, cwd, timeout, env): + """Own ordinary verification descendants even after their leader exits. + + The gated launcher joins a kill-on-close Job Object before it may spawn + the requested command. A late taskkill cannot recover an exited leader's + tree. Job handles are not inherited and breakaway is not enabled. + """ + import ctypes + from ctypes import wintypes + import time + import uuid + + class BasicLimits(ctypes.Structure): + _fields_ = [("ProcessTime", ctypes.c_int64), ("JobTime", ctypes.c_int64), + ("Flags", wintypes.DWORD), ("MinWorkingSet", ctypes.c_size_t), + ("MaxWorkingSet", ctypes.c_size_t), ("ActiveProcessLimit", wintypes.DWORD), + ("Affinity", ctypes.c_size_t), ("Priority", wintypes.DWORD), + ("Scheduling", wintypes.DWORD)] + + class IOCounts(ctypes.Structure): + _fields_ = [(name, ctypes.c_uint64) for name in + ("ReadOps", "WriteOps", "OtherOps", "ReadBytes", "WriteBytes", "OtherBytes")] + + class ExtendedLimits(ctypes.Structure): + _fields_ = [("Basic", BasicLimits), ("IO", IOCounts), + ("ProcessMemory", ctypes.c_size_t), ("JobMemory", ctypes.c_size_t), + ("PeakProcessMemory", ctypes.c_size_t), ("PeakJobMemory", ctypes.c_size_t)] + + class Accounting(ctypes.Structure): + _fields_ = [(name, ctypes.c_int64) for name in + ("UserTime", "KernelTime", "PeriodUserTime", "PeriodKernelTime")] + [ + (name, wintypes.DWORD) for name in + ("PageFaults", "TotalProcesses", "ActiveProcesses", "TerminatedProcesses")] + + api = ctypes.WinDLL("kernel32", use_last_error=True) + + def native(name, result, *arguments): + fn = getattr(api, name) + fn.restype, fn.argtypes = result, arguments + return fn + + handle, dword, boolean = wintypes.HANDLE, wintypes.DWORD, wintypes.BOOL + create_job = native("CreateJobObjectW", handle, ctypes.c_void_p, wintypes.LPCWSTR) + set_info = native("SetInformationJobObject", boolean, handle, ctypes.c_int, ctypes.c_void_p, dword) + assign = native("AssignProcessToJobObject", boolean, handle, handle) + terminate = native("TerminateJobObject", boolean, handle, wintypes.UINT) + query = native("QueryInformationJobObject", boolean, handle, ctypes.c_int, + ctypes.c_void_p, dword, ctypes.c_void_p) + create_event = native("CreateEventW", handle, ctypes.c_void_p, boolean, boolean, wintypes.LPCWSTR) + set_event = native("SetEvent", boolean, handle) + close = native("CloseHandle", boolean, handle) + + def checked(ok): + if not ok: + raise ctypes.WinError(ctypes.get_last_error()) + return ok + + job = checked(create_job(None, None)) + event, process, assigned = None, None, False + try: + limits = ExtendedLimits() + limits.Basic.Flags = 0x2000 # JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE + checked(set_info(job, 9, ctypes.byref(limits), ctypes.sizeof(limits))) + name = "Local\\research-replay-" + uuid.uuid4().hex + event = checked(create_event(None, True, False, name)) + # A name collision must not attach to an already-signaled event. + if ctypes.get_last_error() == 183: + raise ValueError("replay ownership gate name already exists") + process = subprocess.Popen( + # The gate itself must not run sitecustomize/PYTHONPATH startup + # code before ownership. The requested command retains env/argv. + # Windows accepts Unicode argv directly. ASCII JSON escaping can + # expand an otherwise valid command past its UTF-16 launch limit. + [sys.executable, "-I", "-S", "-c", _WINDOWS_GATE, name, + json.dumps(argv, ensure_ascii=False)], + cwd=cwd, env=env, start_new_session=True) + checked(assign(job, int(process._handle))) + assigned = True + checked(set_event(event)) + try: + return process.wait(timeout=timeout) + except subprocess.TimeoutExpired: + raise ValueError("verification timed out") from None + finally: + try: + if process is not None: + if assigned: + checked(terminate(job, 1)) + # Termination is asynchronous. Require the job to report + # no active processes before releasing its ownership handle. + deadline = time.monotonic() + 10 + while True: + state = Accounting() + checked(query(job, 1, ctypes.byref(state), ctypes.sizeof(state), None)) + if not state.ActiveProcesses: + break + if time.monotonic() >= deadline: + raise ValueError("verification process cleanup timed out") + time.sleep(0.01) + elif process.poll() is None: + # Assignment failed before the gate opened: no command child + # exists, so only this known gated launcher needs stopping. + process.kill() + process.wait(timeout=10) + finally: + try: + if event: + checked(close(event)) + finally: + checked(close(job)) + +def run_command(argv, cwd, timeout): + # Assertion checks must not disappear because of the caller's environment. + # Keep explicit manifest options (including deliberate -O controls) intact. + # PYTHONPATH precedes the standard library on a child's sys.path, so a stray + # same-named module could end a verifier with exit code 0 before any check. + env = os.environ.copy() + env.pop("PYTHONOPTIMIZE", None) + # Ambient inspection can turn a verifier exception into a successful REPL + # exit at terminal EOF. Keep explicit -i and the caller's stdin unchanged. + env.pop("PYTHONINSPECT", None) + env.pop("PYTHONPATH", None) + if os.name == "nt": + code = run_windows_command(argv, cwd, timeout, env) + require(code == 0, f"verification command exited {code}") + return + previous_handler = None + terminating = False + waiting = False + process = None + + def terminate(signum, frame): + nonlocal terminating + if not terminating: + terminating = True + # Only interrupt the protected wait. Launch/assignment and cleanup + # defer delivery; repeated signals cannot interrupt group cleanup. + if waiting: + raise SystemExit(128 + signum) + + # Own a group for POSIX cleanup of this command and its descendants. + try: + if (threading.current_thread() is threading.main_thread() + and signal.getsignal(signal.SIGTERM) == signal.SIG_DFL): + previous_handler = signal.SIG_DFL + signal.signal(signal.SIGTERM, terminate) + if terminating: + raise SystemExit(143) + process = subprocess.Popen(argv, cwd=cwd, env=env, start_new_session=True) + try: + try: + waiting = True + if terminating: + raise SystemExit(143) + code = process.wait(timeout=timeout) + finally: + waiting = False + except subprocess.TimeoutExpired: + raise ValueError("verification timed out") from None + require(code == 0, f"verification command exited {code}") + finally: + try: + if process is not None: + if hasattr(os, "killpg"): + # The leader may have exited while descendants hold its group. + try: + os.killpg(process.pid, signal.SIGKILL) + except ProcessLookupError: + pass + elif process.poll() is None: + subprocess.run(["taskkill", "/F", "/T", "/PID", str(process.pid)], capture_output=True) + process.wait() + finally: + if previous_handler is not None: + signal.signal(signal.SIGTERM, previous_handler) + if terminating: + raise SystemExit(143) + + +def isolated_python_argv(argv, cache_directory): + """Place our cache option after interpreter flags, before the entry point. + + -B prevents writes, not reads of existing .pyc files. A fresh namespace + keeps preexisting source-adjacent and externally configured caches out of + this registered interpreter. It is not inherited by child interpreters. + """ + require(argv and argv[0] == sys.executable, "cache isolation requires the registered Python interpreter") + index = 1 + options = [argv[0]] + while index < len(argv): + option = argv[index] + if option in ("-", "--") or not option.startswith("-"): + break + if option == "--check-hash-based-pycs": + require(index + 1 < len(argv), "missing Python hash-cache option argument") + options.extend(argv[index:index + 2]) + index += 2 + continue + if option.startswith("--"): + options.append(option) + index += 1 + continue + # Python permits combined short flags, attached -c/-m payloads, and + # attached or separate -W/-X arguments. Stop scanning an option once + # its argument begins: letters inside that argument are not flags. + entry = False + consumed = 1 + replace_cache = False + for offset, flag in enumerate(option[1:], start=1): + if flag in "cm": + entry = True + break + if flag in "WX": + if offset == len(option) - 1: + require(index + 1 < len(argv), "missing Python interpreter option argument") + consumed = 2 + value = argv[index + 1] if consumed == 2 else option[offset + 1:] + if flag == "X" and value.split("=", 1)[0] == "pycache_prefix": + replace_cache = True + if offset > 1: + options.append(option[:offset]) + break + if entry: + break + if not replace_cache: + options.extend(argv[index:index + consumed]) + index += consumed + # CPython's effective prefix can use the first duplicate -X value even + # when sys._xoptions shows the last. Remove only earlier cache-prefix + # settings; script arguments remain untouched, even when spelled -X. + return [*options, "-X", "pycache_prefix=" + str(cache_directory), *argv[index:]] + + +def run_python_replay(argv, cwd, timeout): + with tempfile.TemporaryDirectory(prefix="research-replay-pycache-") as cache: + run_command(isolated_python_argv(argv, cache), cwd, timeout) + + +def run_package(root, package, install=False): + task, manifest = validate(root, package) + require(sys.version_info[:2] == (3, 12), "run replay with Python 3.12 (metadata check is version-independent)") + if install and manifest["requirements"]: + run_command([sys.executable, "-m", "pip", "install", "--disable-pip-version-check", *manifest["requirements"]], root, 300) + for requirement in manifest["requirements"]: + name, version = requirement.split("==") + require(importlib.metadata.version(name) == version, "dependency version mismatch: " + requirement) + # Coverage labels are UTF-8 metadata but a redirected Windows stdout may + # use cp1252. Escape unrepresentable display characters instead of aborting + # before replay. This changes only our stream, not child encoding or env. + if hasattr(sys.stdout, "reconfigure"): + sys.stdout.reconfigure(errors="backslashreplace") + for command in manifest["commands"]: + argv = [sys.executable if item == "{python}" else item.replace("{package}", package) for item in command["argv"]] + print("CHECK " + command["checks"], flush=True) + run_python_replay(argv, root, command["timeout_seconds"]) + print("PASS_REGISTERED_RESEARCH_PACKAGE " + package + " outcome=" + task["outcome"]) + + +def planning_manifests(root, tracked): + """Include deliberate sparse omissions without disguising deletions or drift. + + The fallback follows the chooser's PR3102 contract: one stage-zero regular + skip-worktree index entry must match the exact HEAD mode and blob. This is + declaration discovery only, not permission to replay absent package code. + """ + committed = {name for name in tracked + if name.startswith("research/") and name.count("/") == 2 + and name.endswith("/VERIFY.json")} + # Explicit scandir retains error detection; only an absent directory is OK. + try: + with os.scandir(root / "research") as entries: + local = {Path(entry.path) / "VERIFY.json" for entry in entries} + except FileNotFoundError: + local = set() + present, missing = {}, set() + for path in sorted(local | {root / name for name in committed}): + name = path.relative_to(root).as_posix() + symlink_free_relative_path(root, name) + try: + mode = path.lstat().st_mode + except (FileNotFoundError, NotADirectoryError): + if name in committed: + missing.add(name) + continue + require(stat.S_ISREG(mode), "non-regular replay manifest: " + name) + present[name] = path + fallback = {} + if missing: + indexed, objects = {}, {} + for record in git(root, "ls-files", "-t", "--stage", "-z", "--", "research").split(b"\0"): + if not record: + continue + header, raw_name = record.split(b"\t", 1) + name = raw_name.decode("utf-8") + if name in missing: + indexed.setdefault(name, []).append(header.split()) + for record in git(root, "ls-tree", "-r", "-z", "HEAD", "--", "research").split(b"\0"): + if not record: + continue + header, raw_name = record.split(b"\t", 1) + name = raw_name.decode("utf-8") + if name in missing: + objects[name] = header.split() + for name in sorted(missing): + metadata = objects.get(name, []) + require(len(metadata) == 3 and metadata[0] in (b"100644", b"100755") + and metadata[1] == b"blob" + and indexed.get(name) == [[b"S", metadata[0], metadata[2], b"0"]], + "missing replay manifest is not an unchanged sparse HEAD input: " + name + + "; materialize it or commit its intended deletion") + # A regular file occupying the package directory is not an omission. + require(not (root / name).parent.exists() or (root / name).parent.is_dir(), + "non-directory replay package: " + name) + names = sorted(missing) + with closing(git_blobs(root, [objects[n][2].decode("ascii") for n in names])) as rows: + for (oid, raw), name in zip(rows, names): + require(oid.encode("ascii") == objects[name][2], "sparse manifest blob mismatch: " + name) + fallback[name] = raw + # Preserve Path's component ordering: package "a" precedes "a-extra". + for name in sorted(present.keys() | fallback.keys(), key=lambda name: PurePosixPath(name).parts): + yield root / name, fallback[name] if name in fallback else present[name].read_bytes(), name in fallback + + +def plan_details(root, changed): + """One selection pass with exact causes; no replay or acceptance decision.""" + changed = set(changed) + # A removed input is absent from HEAD, including an old case-only rename + # spelling on case-insensitive filesystems. Git's exact changed paths remain + # eligible so its consumer is selected, rather than rejected or skipped. + head_tracked = tracked_input_paths(root) + tracked = head_tracked | changed + replay_changed = sorted(changed.intersection({ + "scripts/research_package.py", ".github/workflows/research-packages.yml", + ".github/workflows/research-package-batch.yml"})) + # Group literal Git paths once. Keep removed paths and exact spellings. + changed_packages = {} + for name in sorted(changed): + parts = name.split("/", 2) + if len(parts) == 3 and parts[0] == "research": + changed_packages.setdefault("/".join(parts[:2]), []).append(name) + packages = [] + sparse_count = 0 + # Planning requires a stable checkout; reuse only within this invocation. + validated_inputs = set() + for path, raw, sparse in planning_manifests(root, head_tracked): + sparse_count += sparse + package = path.parent.relative_to(root).as_posix() + manifest = json.loads(raw.decode("utf-8")) + # Parse only for selection; validate fully in the isolated replay job. + inputs = manifest.get("inputs", []) + require(isinstance(inputs, list), "declare input paths for CI invalidation") + for name in inputs: + if isinstance(name, str) and name in validated_inputs: + continue + # Do not require existence: a deleted input must still trigger replay. + symlink_free_relative_path(root, name) + require_tracked_input(name, tracked) + validated_inputs.add(name) + causes = { + "declared_inputs": sorted(changed.intersection(inputs)), + "package_files": changed_packages.get(package, []), + "shared_controls": replay_changed, + } + if any(causes.values()): + packages.append({"package": package, "causes": causes, + "manifest_sha256": hashlib.sha256(raw).hexdigest()}) + if sparse_count: + print(f"Planning includes {sparse_count} unchanged sparse manifests from HEAD; " + "materialize packages and inputs before replay.", file=sys.stderr) + return packages + + +def plan(root, changed): + return [row["package"] for row in plan_details(root, changed)] + + +def write_plan_explanation(path, base, head, changed, details): + """Publish a complete sidecar before stdout/GitHub matrix publication.""" + matched = {name for row in details for names in row["causes"].values() for name in names} + report = { + "schema_version": 1, "base": base, "head": head, + "mode": "all-tracked" if base is None else "commit-diff", + "scope": "Selection from working or authenticated sparse HEAD manifests, not replay or mathematical acceptance. " + "Manifest hashes bind the declarations read; checkout must remain stable.", + "changed_paths": sorted(set(changed) - {""}), "selected": details, + "unmatched_paths": sorted(set(changed) - matched - {""}), + } + fd, temporary = tempfile.mkstemp(prefix=".replay-plan-", dir=path.parent) + try: + with os.fdopen(fd, "w", encoding="utf-8") as stream: + json.dump(report, stream, indent=2) + stream.write("\n") + os.replace(temporary, path) + finally: + Path(temporary).unlink(missing_ok=True) + + +def replay_matrix(packages, batch_size=None): + """Retain the flat CLI format, or group complete coverage into bounded matrices.""" + if batch_size is None: + return {"include": [{"package": p} for p in packages]} + require(type(batch_size) is int and 1 <= batch_size <= 256, + "matrix batch size must be 1..256") + batches = [packages[i:i + batch_size] for i in range(0, len(packages), batch_size)] + require(len(batches) <= 256, + "more than 256 matrix batches required; no packages were truncated") + return {"include": [{"packages": batch} for batch in batches]} + + +def main(): + ap = argparse.ArgumentParser(description=__doc__) + ap.add_argument("--package") + ap.add_argument("--check-only", action="store_true") + ap.add_argument("--print-card", metavar="TASK_ID", + help="print the exact pinned catalogue card object to copy into TASK.json original_card") + ap.add_argument("--install-deps", action="store_true", help="explicit opt-in to install manifest's allowed pinned dependencies") + ap.add_argument("--changed-since", help="plan affected opt-in packages relative to this Git commit") + ap.add_argument("--github-output", type=Path) + ap.add_argument("--explain-plan", type=Path, + help="write exact replay-selection causes to JSON; matrix output stays unchanged") + ap.add_argument("--matrix-batch-size", type=int, + help="group planned packages for bounded reusable-workflow matrices (1..256)") + args = ap.parse_args() + if args.explain_plan and (not args.changed_since or args.package or args.print_card): + ap.error("--explain-plan requires --changed-since without --package or --print-card") + if args.explain_plan and args.github_output and args.explain_plan.resolve() == args.github_output.resolve(): + ap.error("--explain-plan and --github-output must use different files") + try: + if args.print_card: + require(args.package, + "--print-card needs --package research/ to resolve its pinned catalogue") + sys.stdout.write(json.dumps(catalogue_card(ROOT, args.package, args.print_card), indent=2) + "\n") + elif args.changed_since: + if set(args.changed_since) == {"0"}: + base = None + changed = git(ROOT, "ls-files", "-z").decode().split("\0") + else: + base = git(ROOT, "rev-parse", "--verify", args.changed_since + "^{commit}").decode().strip() + # A rename must invalidate both the removed input and its new path. + changed = git(ROOT, "diff", "--no-renames", "--name-only", "-z", base, "HEAD").decode().split("\0") + details = plan_details(ROOT, changed) + packages = [row["package"] for row in details] + matrix = json.dumps(replay_matrix(packages, args.matrix_batch_size)) + if args.explain_plan: + head = git(ROOT, "rev-parse", "--verify", "HEAD^{commit}").decode().strip() + write_plan_explanation(args.explain_plan, base, head, changed, details) + print(matrix) + if args.github_output: + with args.github_output.open("a", encoding="utf-8") as out: + out.write(f"matrix={matrix}\ncount={len(packages)}\n") + elif args.package: + if args.check_only: + validate(ROOT, args.package) + print("PASS_PACKAGE_METADATA_AND_PINS (not a mathematical acceptance check)") + else: + run_package(ROOT, args.package, args.install_deps) + else: + ap.error("use --package, --print-card or --changed-since") + except (OSError, ValueError, KeyError, TypeError, subprocess.SubprocessError, importlib.metadata.PackageNotFoundError) as exc: + ap.exit(1, str(exc) + "\n") + + +if __name__ == "__main__": + main() diff --git a/research/pr6334-posix-path-audit/retained/pr6334/tests/test_research_overlap_git_paths.py b/research/pr6334-posix-path-audit/retained/pr6334/tests/test_research_overlap_git_paths.py new file mode 100644 index 0000000000..8e66d9a047 --- /dev/null +++ b/research/pr6334-posix-path-audit/retained/pr6334/tests/test_research_overlap_git_paths.py @@ -0,0 +1,134 @@ +"""Git path bytes must survive the local overlap coverage inventory. + +Object-only fixtures also exercise names that Windows cannot materialize. +Prepared for josecarlosgonzalezroque-design with OpenAI Codex assistance. +Retains the local coverage contract introduced by PR4419. Apache-2.0. +""" +import json +from pathlib import Path +import shutil +import subprocess +import sys +import tempfile +import unittest +from unittest.mock import patch + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / "scripts")) +import find_overlap as overlap +sys.path.pop(0) + + +@unittest.skipUnless(shutil.which("git"), "Git is needed for object fixtures") +class GitPathCoverageTests(unittest.TestCase): + def setUp(self): + temporary = tempfile.TemporaryDirectory() + self.addCleanup(temporary.cleanup) + self.root = Path(temporary.name) / "repo" + self.root.mkdir() + self.git("init", "-q") + self.git("config", "user.name", "Fixture") + self.git("config", "user.email", "fixture@example.invalid") + + def git(self, *args, data=None): + return subprocess.run(["git", *args], cwd=self.root, input=data, + capture_output=True, check=True, timeout=15).stdout + + def inventory(self, package_names): + # Construct trees directly: no checkout or platform filename support + # is needed to inspect the repository's NUL-delimited path inventory. + blob = self.git("hash-object", "-w", "--stdin", data=b"needle\n").strip() + note_tree = self.git("mktree", "-z", data=b"100644 blob " + blob + b"\tREADME.md\0").strip() + entries = [b"040000 tree " + note_tree + b"\t" + name + b"\0" + for name in package_names] + research = self.git("mktree", "-z", data=b"".join(entries)).strip() + tree = self.git("mktree", "-z", data=b"040000 tree " + research + b"\tresearch\0").strip() + head = self.git("-c", "commit.gpgsign=false", "commit-tree", tree.decode("ascii"), + data=b"path fixture\n").strip().decode("ascii") + self.git("update-ref", "HEAD", head) + return head, sorted((b"research/" + name + b"/README.md").decode("utf-8", "surrogateescape") + for name in package_names) + + def test_cr_lf_and_crlf_paths_remain_distinct(self): + head, expected = self.inventory([b"line\rbreak", b"line\nbreak", b"line\r\nbreak"]) + coverage, warnings = overlap.local_coverage(self.root, []) + self.assertEqual(coverage["head"], head) + self.assertEqual(coverage["head_records"], 3) + self.assertEqual(coverage["unsearched_head_paths"], expected) + self.assertFalse(coverage["complete_head_inventory"]) + self.assertEqual(len(warnings), 1) + self.assertIn("3 HEAD note paths were not searched", warnings[0]) + + def test_lf_record_cannot_hide_unsearched_cr_paths(self): + _, expected = self.inventory([b"line\rbreak", b"line\nbreak", b"line\r\nbreak"]) + # The input to local_coverage is the already searched note inventory. + # A distinct LF note must not count as either omitted CR variant. + searched = "research/line\nbreak/README.md" + coverage, warnings = overlap.local_coverage(self.root, [{"path": searched}]) + self.assertFalse(coverage["complete_head_inventory"]) + self.assertEqual(coverage["head_records"], 3) + self.assertEqual(coverage["unsearched_head_paths"], [p for p in expected if p != searched]) + self.assertIn("2 HEAD note paths were not searched", warnings[0]) + + def test_exact_records_complete_the_inventory(self): + _, expected = self.inventory([b"line\rbreak", b"line\nbreak", b"line\r\nbreak"]) + coverage, warnings = overlap.local_coverage(self.root, [{"path": p} for p in expected]) + self.assertEqual(coverage["head_records"], 3) + self.assertTrue(coverage["complete_head_inventory"]) + self.assertEqual(coverage["unsearched_head_paths"], []) + self.assertEqual(warnings, []) + + def test_tabs_unicode_and_non_utf8_bytes_are_preserved(self): + _, expected = self.inventory([b"tab\tname", "unicode-κ".encode("utf-8"), b"raw-\xff"]) + coverage, warnings = overlap.local_coverage(self.root, []) + self.assertEqual(coverage["unsearched_head_paths"], expected) + self.assertEqual(coverage["head_records"], 3) + self.assertEqual(json.loads(json.dumps(coverage)), coverage) + self.assertEqual(len(warnings), 1) + + def test_cli_json_preserves_paths_and_search_results(self): + head, expected = self.inventory([b"line\rbreak", b"line\nbreak", b"line\r\nbreak"]) + scripts = self.root / "scripts" + scripts.mkdir() + for name in ("find_overlap.py", "research_package.py"): + shutil.copyfile(ROOT / "scripts" / name, scripts / name) + note = self.root / "research/kept/README.md" + note.parent.mkdir(parents=True) + note.write_bytes(b"needle from the working tree\n") + cache = self.root.parent / "cache.json" + cache.write_text(json.dumps({"schema_version": 1, "repository": overlap.REPO, + "sources": {}}), encoding="utf-8") + original = cache.read_bytes() + result = subprocess.run( + [sys.executable, *(["-O"] if sys.flags.optimize else []), "-I", "-B", + str(scripts / "find_overlap.py"), "needle", "--offline", "--json", + "--limit", "1", "--cache", str(cache)], cwd=self.root, + capture_output=True, check=True, timeout=15) + report = json.loads(result.stdout) + self.assertEqual(report["coverage"]["local"]["head"], head) + self.assertEqual(report["coverage"]["local"]["head_records"], 3) + self.assertEqual(report["coverage"]["local"]["unsearched_head_paths"], expected) + self.assertFalse(report["coverage"]["local"]["complete_head_inventory"]) + self.assertEqual(report["matches_total"], 1) + self.assertIn("needle from the working tree", report["matches"][0]["excerpt"]) + self.assertEqual(cache.read_bytes(), original) + + def test_unborn_head_keeps_coverage_unknown(self): + coverage, warnings = overlap.local_coverage(self.root, []) + self.assertIsNone(coverage["head"]) + self.assertIsNone(coverage["head_records"]) + self.assertIsNone(coverage["complete_head_inventory"]) + self.assertIsNone(coverage["unsearched_head_paths"]) + self.assertEqual(len(warnings), 1) + self.assertIn("HEAD note inventory unavailable", warnings[0]) + + def test_missing_git_keeps_coverage_unknown(self): + with patch.object(overlap.subprocess, "run", side_effect=FileNotFoundError("missing git")): + coverage, warnings = overlap.local_coverage(self.root, []) + self.assertIsNone(coverage["complete_head_inventory"]) + self.assertEqual(len(warnings), 1) + self.assertIn("missing git", warnings[0]) + + +if __name__ == "__main__": + unittest.main() diff --git a/research/pr6334-posix-path-audit/retained/pr6334/tests/test_research_overlap_local.py b/research/pr6334-posix-path-audit/retained/pr6334/tests/test_research_overlap_local.py new file mode 100644 index 0000000000..cc9943d78d --- /dev/null +++ b/research/pr6334-posix-path-audit/retained/pr6334/tests/test_research_overlap_local.py @@ -0,0 +1,171 @@ +"""Local overlap coverage must expose notes omitted from a Git checkout.""" +from datetime import datetime, timezone +import json +import os +from pathlib import Path +import shutil +import subprocess +import sys +import tempfile +import unittest + + +ROOT = Path(__file__).resolve().parents[1] + + +@unittest.skipUnless(shutil.which("git"), "Git is needed for checkout fixtures") +class LocalOverlapCoverageTests(unittest.TestCase): + def setUp(self): + temporary = tempfile.TemporaryDirectory() + self.addCleanup(temporary.cleanup) + self.root = Path(temporary.name) / "repo" + self.root.mkdir() + self.cache = Path(temporary.name) / "cache.json" + sources = {name: {"fetched_at": datetime.now(timezone.utc).isoformat(), + "complete_pagination": True, "records": []} + for name in ("open_prs", "discussions")} + self.cache.write_text(json.dumps({"schema_version": 1, + "repository": "CrocSwap/integer-mult-bounds", + "sources": sources}), encoding="utf-8") + # Windows cannot create newline filenames. Git-object fixtures in + # test_research_overlap_git_paths cover LF/CR/CRLF on every platform; + # this fixture also needs to materialize the note after sparse restore. + self.omitted = "research/omitted κ" + (" " if os.name == "nt" else "\n") + "package" + for path, body in { + "scripts/find_overlap.py": (ROOT / "scripts/find_overlap.py").read_text(encoding="utf-8"), + "scripts/research_package.py": (ROOT / "scripts/research_package.py").read_text(encoding="utf-8"), + "research/kept/README.md": "needle local note", + "research/kept/TASK.json": '{"scope": "needle local task"}', + f"{self.omitted}/README.md": "only-omitted needle", + f"{self.omitted}/TASK.json": '{"scope": "only-omitted needle"}', + "docs/research/pr-ledger.md": "ledger needle", + "research/kept/vendor/README.md": "out-of-scope nested note", + "research/kept/NOTES.md": "out-of-scope arbitrary note", + }.items(): + self.write(path, body) + self.git("init", "-q") + self.git("add", ".") + self.git("-c", "user.name=Fixture", "-c", "user.email=fixture@example.invalid", + "-c", "core.hooksPath=/dev/null", "-c", "commit.gpgsign=false", + "commit", "-qm", "overlap fixture") + self.head = self.git("rev-parse", "HEAD").strip() + + def write(self, name, body): + path = self.root / name + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(body, encoding="utf-8") + + def git(self, *args): + return subprocess.run(["git", *args], cwd=self.root, check=True, + capture_output=True, encoding="utf-8").stdout + + def report(self, *args, text=False, env=None): + original = self.cache.read_bytes() + result = subprocess.run( + [sys.executable, "-I", "-B", str(self.root / "scripts/find_overlap.py"), + "--cache", str(self.cache), "--max-age", "3600", + *([] if text else ["--json"]), *args], + cwd=self.root, capture_output=True, encoding="utf-8", timeout=15, env=env) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(self.cache.read_bytes(), original) + return result.stdout if text else json.loads(result.stdout) + + def sparse(self, cone=False): + if cone: + self.git("sparse-checkout", "init", "--cone", "--sparse-index") + self.git("sparse-checkout", "set", "scripts", "research/kept") + else: + self.git("sparse-checkout", "init", "--no-cone") + self.git("sparse-checkout", "set", "/scripts/", "/research/kept/") + + def test_full_checkout_uses_working_edits_and_untracked_notes(self): + self.write("research/kept/README.md", "edited needle") + self.write("research/untracked/README.md", "new needle") + report = self.report("needle") + self.assertEqual(report["local_records"], 6) + self.assertEqual(report["coverage"]["local"], { + "head": self.head, "records": 6, "head_records": 5, + "complete_head_inventory": True, "unsearched_head_paths": []}) + self.assertEqual(report["warnings"], []) + self.assertEqual({m["excerpt"] for m in report["matches"] if "README.md" in m["path"]}, + {"edited needle", "new needle", "only-omitted needle"}) + + def test_sparse_gaps_survive_no_hits_and_result_limits(self): + expected = sorted(["docs/research/pr-ledger.md", f"{self.omitted}/README.md", + f"{self.omitted}/TASK.json"]) + for cone in (False, True): + with self.subTest(cone=cone): + self.sparse(cone) + report = self.report("only-omitted", "--offline", "--limit", "1") + self.assertEqual(report["matches_total"], 0) + self.assertEqual(report["coverage"]["local"], { + "head": self.head, "records": 2, "head_records": 5, + "complete_head_inventory": False, "unsearched_head_paths": expected}) + self.assertTrue(any("local: 3 HEAD note paths were not searched" in warning + for warning in report["warnings"])) + limited = self.report("needle", "--limit", "1") + self.assertEqual(limited["matches_total"], 2) + self.assertEqual(len(limited["matches"]), 1) + self.assertEqual(limited["coverage"]["local"], report["coverage"]["local"]) + output = self.report("only-omitted", text=True) + self.assertIn('"complete_head_inventory": false', output) + self.assertIn('"unsearched_head_paths_count": 3', output) + self.assertNotIn('"unsearched_head_paths":', output) + self.assertIn("WARNING: local: 3 HEAD note paths were not searched", output) + self.assertFalse((self.root / self.omitted).exists()) + self.git("sparse-checkout", "disable") + restored = self.report("only-omitted") + self.assertTrue(restored["coverage"]["local"]["complete_head_inventory"]) + self.assertEqual(restored["matches_total"], 2) + + def test_ordinary_deleted_note_is_also_unsearched(self): + (self.root / "research/kept/README.md").unlink() + report = self.report("needle") + self.assertEqual(report["coverage"]["local"]["unsearched_head_paths"], + ["research/kept/README.md"]) + self.assertFalse(report["coverage"]["local"]["complete_head_inventory"]) + + def test_skipped_symlink_cannot_count_as_a_searched_head_note(self): + note = self.root / "research/kept/README.md" + note.unlink() + try: + note.symlink_to("TASK.json") + except OSError: + self.skipTest("symlinks unavailable") + report = self.report("needle") + self.assertEqual(report["coverage"]["local"]["unsearched_head_paths"], + ["research/kept/README.md"]) + self.assertEqual(report["local_records"], 4) + + def test_empty_head_note_inventory_is_complete(self): + self.git("rm", "-qr", "research", "docs") + self.git("-c", "user.name=Fixture", "-c", "user.email=fixture@example.invalid", + "-c", "core.hooksPath=/dev/null", "-c", "commit.gpgsign=false", + "commit", "-qm", "no notes") + report = self.report("needle") + self.assertEqual(report["coverage"]["local"]["head_records"], 0) + self.assertTrue(report["coverage"]["local"]["complete_head_inventory"]) + self.assertEqual(report["warnings"], []) + + def test_missing_git_or_repository_keeps_search_but_marks_coverage_unknown(self): + for mode in ("missing-git", "not-a-repository", "unborn-head"): + with self.subTest(mode=mode): + if mode == "not-a-repository": + # Keep read-only Git objects inside the owned temporary + # directory; its cleanup handles Windows file attributes. + (self.root / ".git").rename(self.root.parent / "saved-git") + elif mode == "unborn-head": + self.git("init", "-q") + env = {**os.environ, "PATH": ""} if mode == "missing-git" else None + report = self.report("needle", env=env) + coverage = report["coverage"]["local"] + self.assertIsNone(coverage["complete_head_inventory"]) + self.assertIsNone(coverage["head_records"]) + self.assertIsNone(coverage["unsearched_head_paths"]) + self.assertEqual(report["matches_total"], 5) + self.assertTrue(any(w.startswith("local: HEAD note inventory unavailable;") + for w in report["warnings"])) + + +if __name__ == "__main__": + unittest.main() diff --git a/research/pr6334-posix-path-audit/verify.py b/research/pr6334-posix-path-audit/verify.py new file mode 100644 index 0000000000..1b68f9053a --- /dev/null +++ b/research/pr6334-posix-path-audit/verify.py @@ -0,0 +1,214 @@ +#!/usr/bin/env python3 +"""Independent materialized-path differential audit of PR6334 on POSIX. + +Hao Yan (@FanPUYen), with substantial OpenAI Codex assistance. Apache-2.0. +The repair and retained tests are josecarlosgonzalezroque-design's work; +the original tools retain Douglas Colkitt and their other contributors' credit. +""" +from hashlib import sha256 +import json +import os +from pathlib import Path +import platform +import shutil +import subprocess +import sys +import tempfile + +HERE = Path(__file__).resolve().parent +PINS = { + 'retained/main/scripts/find_overlap.py': '41a2796868b5122f568b126726cd04d98f9b5fea78d399c449c1d2e2a69aa0f4', + 'retained/pr6334/scripts/find_overlap.py': 'cff8509bf54b19cfb174ba0b3878ed5ddf350cd7f18e40bf359d9cbbf67f8b29', + 'retained/pr6334/scripts/research_package.py': 'e1256016e7f51827d04fa2179255989f639ae1d97afaa9c16265f85a0c6abd70', + 'retained/pr6334/tests/test_research_overlap_git_paths.py': '85c99372b916a2cce45e4513252e9d64b24aeab293a7ab5d829d91e7c15749e1', + 'retained/pr6334/tests/test_research_overlap_local.py': '51360d84b55792a55a1ff9a34ec02cf87086e43a0eb5144cf0e630a982d5d7b5', +} +NAMES = ['research/line\rbreak/README.md', 'research/line\nbreak/README.md', + 'research/line\r\nbreak/README.md'] +EXTRA = ['research/plain/TASK.json', 'docs/research/pr-ledger.md'] +EXPECTED = sorted(NAMES + EXTRA) + + +def require(condition, message): + if not condition: + raise ValueError(message) + + +def source_bytes(base=HERE): + result = {} + for name, digest in PINS.items(): + raw = (base / name).read_bytes() + require(sha256(raw).hexdigest() == digest, 'source hash mismatch: ' + name) + result[name] = raw + return result + + +def environment(): + env = {k: v for k, v in os.environ.items() if not k.startswith('GIT_')} + env.update(GIT_CONFIG_NOSYSTEM='1', GIT_CONFIG_GLOBAL=os.devnull, + GIT_AUTHOR_NAME='Path fixture', GIT_COMMITTER_NAME='Path fixture', + GIT_AUTHOR_EMAIL='fixture@example.invalid', + GIT_COMMITTER_EMAIL='fixture@example.invalid') + return env + + +def run(args, root, env): + return subprocess.run(args, cwd=root, env=env, capture_output=True, + check=True, timeout=30) + + +def git(root, env, *args): + return run(['git', '-c', 'core.hooksPath=' + os.devnull, + '-c', 'commit.gpgsign=false', *args], root, env).stdout + + +def write(root, name, raw=b'needle\n'): + target = root / name + target.parent.mkdir(parents=True, exist_ok=True) + target.write_bytes(raw) + return target + + +def fixture(root, env): + root.mkdir() + for name in EXPECTED: + write(root, name) + (root / EXTRA[0]).chmod(0o755) + # Deliberately outside the inventory of regular top-level notes. + write(root, 'research/plain/vendor/README.md') + link = root / 'research/tracked-link/README.md' + link.parent.mkdir() + link.symlink_to('../line\nbreak/README.md') + git(root, env, 'init', '-q') + git(root, env, 'add', '.') + git(root, env, 'commit', '-qm', 'materialized POSIX path fixture') + return git(root, env, 'rev-parse', 'HEAD').decode('ascii').strip() + + +def cli(root, env, cache, files, variant): + write(root, 'scripts/find_overlap.py', files[f'retained/{variant}/scripts/find_overlap.py']) + write(root, 'scripts/research_package.py', files['retained/pr6334/scripts/research_package.py']) + flags = ['-I', '-B', '-W', 'error'] + if sys.flags.optimize: + flags += ['-O'] # Propagate optimization into every independent CLI child. + args = [sys.executable, *flags, str(root / 'scripts/find_overlap.py'), + 'needle', '--offline', '--json', '--limit', '1', '--cache', str(cache)] + before = cache.read_bytes() + report = json.loads(run(args, root, env).stdout) + require(cache.read_bytes() == before, 'offline command changed cache') + return report + + +def check_report(report, head, missing, records, variant): + coverage = report['coverage']['local'] + # The oracle comes from declared fixture paths, not the tool/Git decoder. + # The baseline's normalization is an expected negative control. + expected_missing = sorted(missing) if variant == 'pr6334' else [] + require(coverage['head'] == head, 'HEAD binding changed') + require(coverage['head_records'] == (5 if variant == 'pr6334' else 3), + 'unexpected HEAD path count') + require(coverage['unsearched_head_paths'] == expected_missing, 'wrong missing paths') + require(coverage['complete_head_inventory'] == (not expected_missing), + 'incorrect completeness flag') + require(coverage['records'] == records, 'wrong searched record count') + require(report['matches_total'] == records, 'search lost a materialized note') + require(len(report['matches']) == 1, '--limit did not limit displayed results') + if expected_missing: + require(any(f'{len(expected_missing)} HEAD note paths were not searched' in w + for w in report['warnings']), 'missing coverage warning') + return {k: coverage[k] for k in ('records', 'head_records', + 'complete_head_inventory', 'unsearched_head_paths')} + + +def differential(files): + results = [] + env = environment() + with tempfile.TemporaryDirectory(prefix='pr6334-posix-') as directory: + base = Path(directory) + root = base / 'repo' + head = fixture(root, env) + cache = base / 'overlap.json' + cache.write_text(json.dumps({'schema_version': 1, + 'repository': 'CrocSwap/integer-mult-bounds', + 'sources': {}}), encoding='utf-8') + omitted = [NAMES[0], NAMES[2]] + + def probe(name, missing, records): + row = {'case': name} + for variant in ('main', 'pr6334'): + row[variant] = check_report(cli(root, env, cache, files, variant), + head, missing, records, variant) + results.append(row) + + probe('all_materialized_executable_and_tracked_symlink', [], 5) + for name in omitted: + (root / name).unlink() + probe('only_lf_materialized', omitted, 3) + for name in omitted: + (root / name).symlink_to(root / NAMES[1]) + probe('leaf_symlinks_to_lf_cannot_fill_gap', omitted, 3) + for name in omitted: + (root / name).unlink() + (root / name).parent.rmdir() + (root / name).parent.symlink_to((root / NAMES[1]).parent, + target_is_directory=True) + probe('parent_symlinks_to_lf_cannot_fill_gap', omitted, 3) + for name in omitted: + (root / name).parent.unlink() + (root / name).parent.mkdir() + (root / name).symlink_to(base / 'nonexistent') + probe('dangling_symlinks_cannot_fill_gap', omitted, 3) + write(root, 'research/untracked/README.md') + probe('untracked_note_cannot_fill_gap', omitted, 4) + for name in omitted: + (root / name).unlink() + write(root, name) + probe('restored_real_notes_complete_inventory', [], 6) + return results + + +def retained_suites(files): + """Run the two unchanged predecessor suites in an offline source export.""" + results = [] + with tempfile.TemporaryDirectory(prefix='pr6334-tests-') as directory: + root = Path(directory) + prefix = 'retained/pr6334/' + for name, raw in files.items(): + if name.startswith(prefix): + write(root, name[len(prefix):], raw) + for test, count in [('test_research_overlap_git_paths.py', 7), + ('test_research_overlap_local.py', 6)]: + flags = ['-I', '-B', '-W', 'error'] + (['-O'] if sys.flags.optimize else []) + result = run([sys.executable, *flags, str(root / 'tests' / test), '-v'], + root, environment()) + log = result.stderr.decode('utf-8') + require(f'Ran {count} tests' in log and '\nOK\n' in log, + 'unexpected retained suite outcome: ' + test) + # A skipped suite ends in "OK (skipped=N)", not the exact OK + # checked above. Test names themselves may contain "skipped". + results.append({'module': test, 'tests': count, 'skips': 0}) + return results + + +def main(): + require(os.name == 'posix', 'this audit requires a POSIX filesystem and symlinks') + require(shutil.which('git') is not None, 'Git is required') + files = source_bytes() + results = differential(files) + suites = retained_suites(files) + print(json.dumps({'status': 'PASS_PR6334_POSIX_PATH_AUDIT', + 'python': platform.python_version(), 'platform': platform.platform(), + 'optimized_cli_children': bool(sys.flags.optimize), + 'differential_cases': results, 'retained_suites': suites, + 'source_sha256': PINS, + 'scope': 'Pinned local path/coverage behavior only; no network, ' + 'current-main repair, supplier replay or theorem proof.'}, indent=2)) + + +if __name__ == '__main__': + try: + main() + except (OSError, ValueError, subprocess.SubprocessError) as exc: + print(json.dumps({'status': 'FAIL_PR6334_POSIX_PATH_AUDIT', 'error': str(exc)}), + file=sys.stderr) + raise SystemExit(1)