Upload folder using huggingface_hub
Browse filesFiles changed (4)
- README.md+95-4
- lgpython/cli.py+3-2
- tests/test_cli.pyAdded+170
- tools/publish_dataset.py+7-7
34 unmodified lines3536373839404181 unmodified lines12312412512612712812 unmodified lines14114214314414514614714814934 unmodified linesGATES: CONFIRMED (9 gates: 9 CONFIRMED, 0 REJECTED, 0 ABSTAIN)```The gate runner is where this bundle states its numbers out loud: 9 gates, 64 tests across 8 files,18 files under `nodes/`, and a kernel it does not carry. `tools/publish_dataset.py` re-derives everyone of those numbers from the tree it is standing in and stops the upload when the card and the codebeside it disagree.81 unmodified linesthe family, the intent tags, the edits, the verdict the run must reach, and for a refusal the reasoncode. `patterns/` is a generated folder, so edit `tools/build_patterns.py` and let `--check` say so.## RunningFrom the folder of this dataset, with `LGCODEX_ROOT` pointing at the folder of the kernel dataset:12 unmodified lines`patterns apply` without `--dry-run` commits, and only on a verdict the kernel accepts: it commitswhen the assurance reaches the higher of the session floor and the card's own floor, and reports arefusal with the reason code of the rule that stopped it. `fixtures/pattern_lab/` is a copycandidate: the commands above run against it, and the tests copy it to a temporary folder first, sothe folder as published stays the shape the coverage numbers were counted on.## What this bundle needs from the codex kernel34 unmodified lines3536373839404181 unmodified lines12312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821912 unmodified lines23223323423523623723823924034 unmodified linesGATES: CONFIRMED (9 gates: 9 CONFIRMED, 0 REJECTED, 0 ABSTAIN)```The gate runner is where this bundle states its numbers out loud: 9 gates, 75 tests across 9 files,18 files under `nodes/`, and a kernel it does not carry. `tools/publish_dataset.py` re-derives everyone of those numbers from the tree it is standing in and stops the upload when the card and the codebeside it disagree.81 unmodified linesthe family, the intent tags, the edits, the verdict the run must reach, and for a refusal the reasoncode. `patterns/` is a generated folder, so edit `tools/build_patterns.py` and let `--check` say so.## Working the fixture from the command lineThe commands below were run against `fixtures/pattern_lab/` as published, and what follows is whatcame back. The first one answers before anything is asked to change:```$ python -B -m lgpython symbols --workspace fixtures/pattern_lab --limit 5files=10 symbols=29 edges=290.0942 src/registry.py::render0.0838 src/tokens.py::count_words0.0669 src/guards.py::open_ticket0.0572 src/registry.py::format_short0.0481 src/registry.py::format_json```The ranking is PageRank over the call and import graph, and the three highest ranked functions here arethree of the five the catalog claims: the dispatcher, the annotatable default, the bare guard. To knowwhich tests a change is answerable to:```$ python -B -m lgpython select-tests --workspace fixtures/pattern_lab --files src/registry.pytests/test_registry.py```A card states its terms before you use it. This transcript is abridged to the lines a decision turnson, and the two long lines are wrapped here for width:```$ python -B -m lgpython patterns show --pattern open-to-context-manageropen-to-context-managerfamily resourcesshape a module level function that opens a file into a name and closes that name itselfwhy a close that is not tied to the scope is skipped by the one path that raises on the wayout, and the reader has to trace every exit to believe the handle is closed.needs- the name is used only inside that function and never stored or returnedlock the source opens inside a with block and never calls close, and a file written in atemporary directory is read back through the rewritten functionrefuses ALREADY_SCOPED, NO_BARE_OPEN, HANDLE_ESCAPE, NOT_A_MODULE_FUNCTIONneeds assurance >= 3 to commit```To find out whether a workspace really holds the shape, rather than guessing:```$ python -B -m lgpython patterns scan --workspace fixtures/pattern_lab --pattern chain-to-registry```This prints one hit, `src/registry.py::render`, with the two edits it would apply: the chain of `if`and `elif` that becomes a mapping of handlers, and the lock test appended to `tests/test_registry.py`.`render_pair`, in the same file, is not listed: its chain is not in the shape the card claims, and acard that does not claim a shape does not touch it.The pair, verified and not committed:```$ python -B -m lgpython patterns apply --workspace fixtures/pattern_lab \--pattern chain-to-registry --target src/registry.py::render --dry-runVERIFIED (assurance 3, committed=False) OK_DUAL_CONFIRMATION: confirmed by adversary, gate_checker```The same command without `--dry-run` writes both files in one transaction. Ask for the function whoseshape is wrong and the answer is a refusal with a reason, an exit code of 1, and a workspace that didnot move by a byte:```$ python -B -m lgpython patterns apply --workspace fixtures/pattern_lab \--pattern chain-to-registry --target src/registry.py::render_pair --dry-runREFUSED (assurance 0, committed=False) BRANCH_SHAPE_MISMATCH: the chain in 'render_pair' tests morethan one name against text literals, its branches do not take the same arguments, or its last branchdoes not raisenothing was written; the card named this reason before any node ran```A change of your own goes through the same six nodes, as a search and replace you wrote:```bashpython -B -m lgpython edit --workspace repo --file src/x.py \--search "def load(path):" --replace "def load(path: Path) -> dict:" --dry-run```and so do `create --file ... --content-file ...`, `delete`, and `verify --file ...`, which reports averdict for a file without asking for a change. With `--strict-tools` the run demands `mypy` and `ruff`and answers `ABSTAIN` when they are not installed, instead of reporting a check that never happened.`check-adapter` says whether the Python adapter holds the contract of the kernel, and exits non-zerowhen it does not.`tests/test_cli.py` runs the documented commands as separate processes and asserts their exit codes andtheir answers, because a documented command that crashes breaks a promise even when every library callbeside it is correct.## RunningFrom the folder of this dataset, with `LGCODEX_ROOT` pointing at the folder of the kernel dataset:12 unmodified lines`patterns apply` without `--dry-run` commits, and only on a verdict the kernel accepts: it commitswhen the assurance reaches the higher of the session floor and the card's own floor, and reports arefusal with the reason code of the rule that stopped it. `fixtures/pattern_lab/` is a copy candidate:the commands above either read it or verify against a copy, and the tests copy it to a temporary folderbefore writing, so the folder as published keeps the exact shape the coverage numbers were counted on.## What this bundle needs from the codex kernel
188 unmodified lines1891901911921931943 unmodified lines198199200201202203204205206207208209188 unmodified linesprint("this action needs --pattern", file=sys.stderr)return 2if args.action == "list":rows = [{"pattern_id": spec.pattern_id,3 unmodified lines"rule": spec.rule,"companions": list(spec.companions),}for spec in PATTERNS]if args.json:print(json.dumps({"patterns": rows}, indent=2, sort_keys=True))else:for spec in PATTERNS:print("%-28s %-10s %s"% (spec.pattern_id, spec.family, ", ".join(spec.intent_tags))188 unmodified lines1891901911921931941953 unmodified lines199200201202203204205206207208209210188 unmodified linesprint("this action needs --pattern", file=sys.stderr)return 2if args.action == "list":specs = [PATTERNS[name] for name in sorted(PATTERNS)]rows = [{"pattern_id": spec.pattern_id,3 unmodified lines"rule": spec.rule,"companions": list(spec.companions),}for spec in specs]if args.json:print(json.dumps({"patterns": rows}, indent=2, sort_keys=True))else:for spec in specs:print("%-28s %-10s %s"% (spec.pattern_id, spec.family, ", ".join(spec.intent_tags))
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170"""The command line is a published interface, so it is run as a process here.
Nothing in this file reaches into the CLI module. Each test starts the same process areader of the card would start, and asserts on the exit code and on the text that comesback, because a documented command that crashes is a broken promise even when everylibrary call beside it is correct."""
from __future__ import annotations
import hashlibimport jsonimport subprocessimport sysfrom pathlib import Pathfrom typing import Any, Dict
from lgpython.patterns import PATTERNS
from lgpython.rules import RULE_IDS
ROOT = Path(__file__).resolve().parents[1]LAB = ROOT / "fixtures" / "pattern_lab"SAMPLE = ROOT / "fixtures" / "sample_pkg"
def run_cli(*args: str) -> subprocess.CompletedProcess: return subprocess.run( [sys.executable, "-B", "-m", "lgpython", *args], cwd=str(ROOT), capture_output=True, text=True, )
def _tree_state(root: Path) -> Dict[str, str]: state = {} for path in sorted(root.rglob("*")): if path.is_file(): digest = hashlib.sha256(path.read_bytes()).hexdigest() state[str(path.relative_to(root))] = digest return state
def test_patterns_list_prints_every_card_once_in_a_stable_order() -> None: proc = run_cli("patterns", "list") assert proc.returncode == 0, proc.stdout + proc.stderr lines = [line for line in proc.stdout.splitlines() if line.strip()] assert len(lines) == len(PATTERNS) assert [line.split()[0] for line in lines] == sorted(PATTERNS) for line in lines: assert line.split()[1] in {"contracts", "mutation", "resources", "shape", "typing"}
def test_patterns_list_json_carries_the_fields_a_reader_needs() -> None: proc = run_cli("patterns", "list", "--json") assert proc.returncode == 0, proc.stdout + proc.stderr rows = json.loads(proc.stdout)["patterns"] assert [row["pattern_id"] for row in rows] == sorted(PATTERNS) for row in rows: assert set(row) == { "pattern_id", "family", "intent_tags", "min_assurance", "rule", "companions", } assert row["rule"] in RULE_IDS assert row["companions"] assert row["min_assurance"] >= 2
def test_patterns_show_answers_for_one_card_in_text_and_in_json() -> None: proc = run_cli("patterns", "show", "--pattern", "chain-to-registry") assert proc.returncode == 0, proc.stdout + proc.stderr for part in ("shape", "why", "needs", "lock", "refuses"): assert part in proc.stdout assert "BRANCH_SHAPE_MISMATCH" in proc.stdout
proc = run_cli("patterns", "show", "--pattern", "chain-to-registry", "--json") assert proc.returncode == 0, proc.stdout + proc.stderr payload: Dict[str, Any] = json.loads(proc.stdout) assert payload["pattern_id"] == "chain-to-registry" assert payload["lock_claims"]
def test_an_unknown_card_and_a_missing_flag_answer_before_any_work_starts() -> None: proc = run_cli("patterns", "show", "--pattern", "no-such-card") assert proc.returncode == 2 assert "unknown pattern" in proc.stderr
proc = run_cli("patterns", "scan", "--workspace", str(LAB)) assert proc.returncode == 2 assert "--pattern" in proc.stderr
proc = run_cli("patterns", "apply", "--pattern", "chain-to-registry") assert proc.returncode == 2 assert "--workspace" in proc.stderr
def test_patterns_scan_names_the_functions_in_the_shape_of_the_card() -> None: proc = run_cli("patterns", "scan", "--workspace", str(LAB), "--pattern", "chain-to-registry") assert proc.returncode == 0, proc.stdout + proc.stderr assert "src/registry.py::render" in proc.stdout assert "render_pair" not in proc.stdout
def test_patterns_scan_says_so_when_nothing_in_the_workspace_qualifies() -> None: proc = run_cli("patterns", "scan", "--workspace", str(SAMPLE), "--pattern", "chain-to-registry") assert proc.returncode == 0, proc.stdout + proc.stderr assert "no function in this workspace has the shape of this card" in proc.stdout
def test_apply_dry_run_verifies_the_pair_without_writing_it(pattern_lab: Path) -> None: before = _tree_state(pattern_lab) proc = run_cli( "patterns", "apply", "--workspace", str(pattern_lab), "--pattern", "chain-to-registry", "--target", "src/registry.py::render", "--dry-run", ) assert proc.returncode == 0, proc.stdout + proc.stderr assert "VERIFIED" in proc.stdout assert "committed=False" in proc.stdout assert _tree_state(pattern_lab) == before
def test_a_shape_the_card_does_not_claim_exits_non_zero_and_writes_nothing(pattern_lab: Path) -> None: before = _tree_state(pattern_lab) proc = run_cli( "patterns", "apply", "--workspace", str(pattern_lab), "--pattern", "chain-to-registry", "--target", "src/registry.py::render_pair", "--dry-run", ) assert proc.returncode == 1 assert "REFUSED" in proc.stdout assert "BRANCH_SHAPE_MISMATCH" in proc.stdout assert "nothing was written" in proc.stdout assert _tree_state(pattern_lab) == before
def test_symbols_names_the_functions_of_a_workspace_and_ranks_them() -> None: proc = run_cli("symbols", "--workspace", str(LAB), "--limit", "200") assert proc.returncode == 0, proc.stdout + proc.stderr assert "files=" in proc.stdout assert "src/registry.py::render" in proc.stdout
def test_select_tests_answers_with_the_tests_that_import_the_changed_file() -> None: proc = run_cli("select-tests", "--workspace", str(LAB), "--files", "src/registry.py") assert proc.returncode == 0, proc.stdout + proc.stderr assert "tests/test_registry.py" in proc.stdout.split()
def test_the_adapter_answers_the_contract_of_the_kernel() -> None: proc = run_cli("check-adapter", "--workspace", str(SAMPLE)) assert proc.returncode == 0, proc.stdout + proc.stderr assert "ADAPTER OK" in proc.stdout13 unmodified lines1415161718192018 unmodified lines394041424344454647484939 unmodified lines89909192939495969713 unmodified linesThis bundle carries no kernel. It carries one corpus of its own, the ten pattern cases under`patterns/`, generated from the rules and the fixture beside them, and the card states theirdenominator rather than letting ten rows look like a benchmark. Every number the card states (9 gates,64 tests, 18 node files, the staged file count) is re-derived from this tree before a single object isuploaded. A card that contradicts the code beside it stops the run instead of publishing.The folder README is the body of the dataset card. A short list of wording pairs turns the sentences18 unmodified linesROOT = Path(__file__).resolve().parents[1]REPO = "tailot/codex-python"EXPECTED_FILES = 65 # the staged tree: 65 files of this folder, the card replacing README.mdEXPECTED_HUB_OBJECTS = 66 # those 65 plus the .gitattributes the hub addsEXPECTED_GATES = 9EXPECTED_TESTS = 64EXPECTED_TEST_FILES = 8EXPECTED_NODE_FILES = 18EXPECTED_GATE_LINE = "GATES: CONFIRMED (9 gates: 9 CONFIRMED, 0 REJECTED, 0 ABSTAIN)"ROLES = ("planner", "retriever", "executor", "gate_checker", "adversary", "arbiter")39 unmodified lines"\"What this bundle needs from the codex kernel\" below says how the two find each other.",),("The gate runner is where this bundle states its numbers out loud: 9 gates, 64 tests across 8 files,\n""18 files under `nodes/`, and a kernel it does not carry.","The gate runner is where this bundle states its numbers out loud: 9 gates, 64 tests across 8 files,\n""18 files under `nodes/`, and a kernel it does not carry. `tools/publish_dataset.py` re-derives every\n""one of those numbers from the tree it is standing in and stops the upload when the card and the code\n""beside it disagree.",13 unmodified lines1415161718192018 unmodified lines394041424344454647484939 unmodified lines89909192939495969713 unmodified linesThis bundle carries no kernel. It carries one corpus of its own, the ten pattern cases under`patterns/`, generated from the rules and the fixture beside them, and the card states theirdenominator rather than letting ten rows look like a benchmark. Every number the card states (9 gates,75 tests, 18 node files, the staged file count) is re-derived from this tree before a single object isuploaded. A card that contradicts the code beside it stops the run instead of publishing.The folder README is the body of the dataset card. A short list of wording pairs turns the sentences18 unmodified linesROOT = Path(__file__).resolve().parents[1]REPO = "tailot/codex-python"EXPECTED_FILES = 66 # the staged tree: 66 files of this folder, the card replacing README.mdEXPECTED_HUB_OBJECTS = 67 # those 66 plus the .gitattributes the hub addsEXPECTED_GATES = 9EXPECTED_TESTS = 75EXPECTED_TEST_FILES = 9EXPECTED_NODE_FILES = 18EXPECTED_GATE_LINE = "GATES: CONFIRMED (9 gates: 9 CONFIRMED, 0 REJECTED, 0 ABSTAIN)"ROLES = ("planner", "retriever", "executor", "gate_checker", "adversary", "arbiter")39 unmodified lines"\"What this bundle needs from the codex kernel\" below says how the two find each other.",),("The gate runner is where this bundle states its numbers out loud: 9 gates, 75 tests across 9 files,\n""18 files under `nodes/`, and a kernel it does not carry.","The gate runner is where this bundle states its numbers out loud: 9 gates, 75 tests across 9 files,\n""18 files under `nodes/`, and a kernel it does not carry. `tools/publish_dataset.py` re-derives every\n""one of those numbers from the tree it is standing in and stops the upload when the card and the code\n""beside it disagree.",
README.md+95-4
lgpython/cli.py+3-2
tests/test_cli.pyAdded+170
tools/publish_dataset.py+7-7