Repository navigation
feat(runs): runs baseline save|list|rm with a compare-and-swap --expect-previous - #1184
vlad-perevezentsev wants to merge 26 commits into
Conversation
…vious A baseline is a named pointer to a run, like a git tag (intel#94 §2.3, R3): the file .baselines/<NAME> under the runs root holds the run_id, with '/' in a namespaced name (skill/a@main) written %2F. RUN is a run_id some artifact under the root carries, found by the scan as runs show finds it. The pointer is replaced through write_text_atomic (intel#697). Save and rm hold .baselines/.lock (flock, msvcrt on Windows), so --expect-previous RUN_ID is a compare-and-swap: a baseline that does not point to RUN_ID exits 4 (invalid, a stale baseline, §2.6) and is left as it was, and of two nightlies that read the same pointer one wins. A name Windows reserves for a device, or one that differs from a saved one only in case, is refused. runs gc --keep-baselines now keeps the runs these pointers hold as well as those of the baseline save copies, and holds the same lock while it removes. runs list does not walk .baselines/. The old baseline save store under baselines/eval/ is left as it is. Refs intel#94 Signed-off-by: Vladislav Perevezentsev <vladislav.perevezentsev@intel.com>
scan.BASELINES_DIR sat beside commands/baseline.BASELINES_DIR (the old copies under baselines/eval/) in commands/runs.py, and runs/baseline.py aliased it as STORE. It is now scan.BASELINE_POINTERS, used directly. Refs intel#94 Signed-off-by: Vladislav Perevezentsev <vladislav.perevezentsev@intel.com>
save refuses a name that differs from a saved one only in case, since Windows and macOS open them as one file, but rm read and unlinked the path as given: there, rm MAIN removed main. rm now looks for the exact file name in the store's listing under the lock and exits 2 otherwise, naming the other spelling. Refs intel#94 Signed-off-by: Vladislav Perevezentsev <vladislav.perevezentsev@intel.com>
gc took the baselines' lock only when .baselines/ existed, so a first runs baseline save made alongside it could point at a run gc was removing. It now takes the lock whenever it keeps baselines and removes (not with --dry-run), making .baselines/.lock if missing; the docs drop the exception. The rm/gc tests' file listing leaves the store out. Refs intel#94 Signed-off-by: Vladislav Perevezentsev <vladislav.perevezentsev@intel.com>
…ails When the saves never waited on the lock, wait_until failed and the Popen objects were never waited, so pytest blamed the next test for a ResourceWarning. The processes now run in a context manager that kills and waits any still running; the lock wait is a helper of its own. Refs intel#94 Signed-off-by: Vladislav Perevezentsev <vladislav.perevezentsev@intel.com>
…ines' lock docs/runs.md says gc holds the lock while it removes; with it replaced by a null context every test still passed. A gc process now waits on the lock the test holds and removes nothing until it is released, and gc with --dry-run or without --keep-baselines takes no lock. Refs intel#94 Signed-off-by: Vladislav Perevezentsev <vladislav.perevezentsev@intel.com>
The gc row named only a manifest or copy that cannot be read; a pointer that cannot be read, or a .baselines/ that cannot be listed, exits 2 too. runs baseline list exits 2 for a .baselines/ it cannot list (a file, no permission), and 0 for an entry that is not a pointer. Refs intel#94 Signed-off-by: Vladislav Perevezentsev <vladislav.perevezentsev@intel.com>
Since gc --keep-baselines takes the lock even before the first save,
locked() ran its mkdir and open with no handling: a file at .baselines
or a read-only store without .lock printed a traceback and exited 5,
where docs/runs.md says exit 2. locked() now raises InputError
('.baselines/: cannot lock: not a directory'), for gc, save and rm.
Refs intel#94
Signed-off-by: Vladislav Perevezentsev <vladislav.perevezentsev@intel.com>
CLI interface change detectedThis PR changes dokimi's user-facing CLI surface (commands and/or options): + added command `dokimi runs baseline`
+ option `--help/-h`
+ added command `dokimi runs baseline list`
+ option `--help/-h`
+ option `--json`
+ added command `dokimi runs baseline rm`
+ argument `name`
+ option `--help/-h`
+ option `--json`
+ added command `dokimi runs baseline save`
+ argument `name`
+ argument `run`
+ option `--expect-previous`
+ option `--help/-h`
+ option `--json`Updated on every push; the |
|
🔍 OpenCodeReview found 1 issue(s) in this PR. CI at By label: 1 should fix
Test notes (2)
Review details
|
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1184 +/- ##
==========================================
- Coverage 96.15% 96.11% -0.04%
==========================================
Files 498 500 +2
Lines 65973 66306 +333
Branches 11456 11491 +35
==========================================
+ Hits 63436 63732 +296
- Misses 1569 1595 +26
- Partials 968 979 +11
🚀 New features to boost your workflow:
|
Signed-off-by: Vladislav Perevezentsev <vladislav.perevezentsev@intel.com>
_read() opened every store entry in blocking mode, so a FIFO named like a pointer hung runs baseline list, rm and save, and gc --keep-baselines while it held the lock. It now opens with O_NONBLOCK and refuses what fstat says is not a regular file, so list names it unreadable, gc and save exit 2, and rm removes it. Refs intel#94 Signed-off-by: Vladislav Perevezentsev <vladislav.perevezentsev@intel.com>
The case rule reads the store's names, and a listing that failed (a store the user may write but not read) was taken as no other spelling: save Main went through beside main, and rm exited 5. Both now exit 2, '.baselines/: cannot list: <reason>', through one listing helper that list uses too. Refs intel#94 Signed-off-by: Vladislav Perevezentsev <vladislav.perevezentsev@intel.com>
Both tests started the cli.py processes and only then took the lock, so a process that got it first saved (or removed) and exited, and the wait for two waiters timed out on correct code. The test now holds the lock before Popen, waits for the waiters, then releases it; _cli() still kills what is left. Refs intel#94 Signed-off-by: Vladislav Perevezentsev <vladislav.perevezentsev@intel.com>
1a2b63c kept a FIFO pointer from blocking, but gc --keep-baselines then reads baselines/eval/manifest.json and each copy it lists in blocking mode, under the baselines' lock: a FIFO there hung gc and every runs baseline save or rm behind it. Both are now looked up first and refused (exit 2) when they are not a regular file. Refs intel#94 Signed-off-by: Vladislav Perevezentsev <vladislav.perevezentsev@intel.com>
runs rm PATH removed any file under the root, .baselines/.lock too: removed while a save or gc held it, the next save locked a new inode and ran beside them, and a pointer removed this way skipped the lock and the case rule. A path that resolves into .baselines/ now exits 2 and names runs baseline rm. Refs intel#94 Signed-off-by: Vladislav Perevezentsev <vladislav.perevezentsev@intel.com>
…st bytes _read() took the first 256 bytes and stripped them, so a run id padded with spaces past byte 256 and followed by anything else read as that run id: list and gc trusted it, and --expect-previous matched it. A pointer longer than that now cannot be read: list names it, gc and save exit 2. Refs intel#94 Signed-off-by: Vladislav Perevezentsev <vladislav.perevezentsev@intel.com>
A dangling symlink in .baselines/ is listed as unreadable and stops gc --keep-baselines, but rm read it, got nothing (its target is missing) and said there was no such baseline. The name is in the store's listing, so rm now removes the link like any pointer it cannot read. Refs intel#94 Signed-off-by: Vladislav Perevezentsev <vladislav.perevezentsev@intel.com>
75432c9 compared the first part of the resolved path with '.baselines', but contained() resolves directories: through a .baselines that links to a directory under the root, or spelled in another case on a case-insensitive file system, the lock resolved under another name and was removed. A file whose directory is the store by device and inode is now refused too. Refs intel#94 Signed-off-by: Vladislav Perevezentsev <vladislav.perevezentsev@intel.com>
ed03185 dropped the 'missing' branch to let rm unlink a link to nothing, but _read() also returns None for a pointer a process that does not lock removed after the listing: unlink then raised FileNotFoundError and rm exited 5 as an infra error. unlink's FileNotFoundError is now 'no baseline NAME' (exit 2); a link to nothing still goes, since unlink does not follow it. Refs intel#94 Signed-off-by: Vladislav Perevezentsev <vladislav.perevezentsev@intel.com>
Since ed03185 and 1a2b63c, rm is how an entry that list names as unreadable, and that stops gc --keep-baselines, goes: a link to nothing, a FIFO, a file over 256 bytes, text that is not UTF-8. The rm bullet now says so, On disk states the 256-byte limit of 06296a2, and the test cites the line it checks. Refs intel#94 Signed-off-by: Vladislav Perevezentsev <vladislav.perevezentsev@intel.com>
napetrov
left a comment
There was a problem hiding this comment.
Change class: CONTRACT (head 6af90e1)
- Contract: changed. errors dokimi/dokimi/commands/runs.py:367; errors dokimi/dokimi/commands/runs_baseline.py:26
- Responsibility: candidate. delegate cmd_runs_gc in dokimi/dokimi/commands/runs.py
- Dependency: none
- Review depth: check the changed surface: callers, docs, tests pin new behaviour
- Coverage gaps: none
The baseline, removal, scan and show suites passed on Linux (303 passed, one skipped), including the two-process compare-and-swap test. Please address the baseline store boundary before merge. I also found a small consistency issue in the timestamp returned by list.
Signed-off-by: Vladislav Perevezentsev <vladislav.perevezentsev@intel.com>
store() returned <root>/.baselines as spelled, so a symlink out of the root sent save, rm and gc --keep-baselines's lock there, and list read pointers from there, against the root boundary in docs/runs.md. store() now refuses a store whose resolved path is outside the root (exit 2) before anything reads or writes it; a link to a directory under the root is still used. Refs intel#94 Signed-off-by: Vladislav Perevezentsev <vladislav.perevezentsev@intel.com>
_entry() read the pointer, then looked up its mtime by path: a save in between gave a row with the old run id and the new save's time. The mtime now comes from fstat of the file descriptor the run id was read from. Refs intel#94 Signed-off-by: Vladislav Perevezentsev <vladislav.perevezentsev@intel.com>
860e1a9 checked the store directory only. A pointer that links out of the root was read (list showed it, gc --keep-baselines kept its run), and a .lock that links out was created and locked there. Pointers and the lock are now opened with O_NOFOLLOW after an islink check: a linked pointer is unreadable (rm removes the link), a linked lock exits 2. Refs intel#94 Signed-off-by: Vladislav Perevezentsev <vladislav.perevezentsev@intel.com>
860e1a9 refused a store that resolves outside the root but took one linked to the root or to a run's directory: there rm NAME removed a run's file (rm eval.json through .baselines -> a) and, linked to the root, every run directory read as a bad pointer, so gc --keep-baselines always exited 2. dokimi makes the store as a plain directory, so any link (a symlink or a junction) is now refused with exit 2. Refs intel#94 Signed-off-by: Vladislav Perevezentsev <vladislav.perevezentsev@intel.com>
napetrov
left a comment
There was a problem hiding this comment.
Change class: CONTRACT (head 8aeed81)
- Contract: changed. errors dokimi/dokimi/commands/runs.py:367; errors dokimi/dokimi/commands/runs_baseline.py:26
- Responsibility: candidate. delegate cmd_runs_gc in dokimi/dokimi/commands/runs.py
- Dependency: none
- Review depth: check the changed surface: callers, docs, tests pin new behaviour
- Coverage gaps: none
The pointer ID and saved timestamp now come from one open file descriptor. The new baseline-store check covers symlinks, but one supported Windows version still lacks junction detection at that call site. I reviewed the path checks statically and checked CI.
vchamarthi
left a comment
There was a problem hiding this comment.
@vlad-perevezentsev, could you fix the small thing I've marked fix before merge (in the line comments) and fix what napetrov's thread on dokimi/dokimi/runs/baseline.py:70 still points out? I'll approve once that's done.
I'll approve once that's done.
What I checked on 8aeed81:
- When I broke the changed code on purpose, the tests missed 9 of 12 breakages.
- The 130 changed tests pass, and fail on main as expected.
- The full test suite passes with main merged in (31,941 tests), apart from failures main has too.
- It merges cleanly onto main.
Signed-off-by: Vladislav Perevezentsev <vladislav.perevezentsev@intel.com>
store() tested a junction with os.path.isjunction, which Python 3.11 lacks, so there the fallback never refused one, against docs/runs.md. It now uses the scan's own link check, made public as scan.is_link, which reads st_reparse_tag on every version. Refs intel#94 Signed-off-by: Vladislav Perevezentsev <vladislav.perevezentsev@intel.com>
| def _read_with_mtime(path: Path) -> tuple[str, float] | None: | ||
| """:func:`_read`, and the mtime of the file it read: one open file, so both are of one save.""" | ||
| try: # O_NONBLOCK: opening a FIFO with no writer would wait for one | ||
| fd = _open_unlinked(path, os.O_RDONLY | getattr(os, "O_NONBLOCK", 0)) |
There was a problem hiding this comment.
Impact: a valid concurrent baseline save or removal can fail with exit 5 on Windows merely because runs baseline list is reading the pointer.
Trigger: pause a listing after this open, then save over (or remove) the same baseline.
Evidence: _open_unlinked() uses ordinary os.open, whose Windows CRT handle does not share delete access; listing takes no shared lock and keeps that handle open through fstat and reading. write_text_atomic() calls os.replace without retrying sharing violations, and remove() similarly calls unlink, so the mutation lock does not prevent this reader/writer conflict.
Fix: Use a Windows reader that permits delete sharing, or otherwise coordinate readers and mutations while preserving list's read-only contract; add a Windows regression test holding the reader open during replacement/removal.
Workaround: serialize listing with saves/removals externally, or retry the failed mutation after listing finishes.
Should fix: A run shows it; it was not judged to be met in ordinary use.
This proof replaces part of the code with stand-ins (mock): it shows the code path, not the real dependency or platform.
Shown by running: exit 1 (at the head) · exit 1 (at the merge base, which cannot run it) · exit 0 (at the head with the fix) · exit 1 (proof 2, at the head) · exit 1 (proof 2, at the merge base, which cannot run it) · exit 0 (proof 2, at the head with the fix)
python proof_c1.py save
What the proof does: Calls the real listing and save handler in a fixed interleaving, using stand-ins for Windows handle sharing and locking, and checks that saving succeeds while the listing reads the original pointer.
The problem shows as the output ASSERTION: listing must not block a baseline mutation.
Script: dokimi/proof_c1.py (125 lines).
Error: baseline main: cannot write: simulated Windows sharing violation
Traceback (most recent call last):
File "/tmp/tmp.gWaDXvT7Ll/dokimi/proof_c1.py", line 118, in <module>
assert status == [0], 'ASSERTION: listing must not block a baseline mutation'
^^^^^^^^^^^^^
AssertionError: ASSERTION: listing must not block a baseline mutation
Summary
Adds the baselines of #94 R3 (§2.3: "A baseline is a named pointer to a run, like a git tag …
--expect-previous RUN_IDmakes the save a compare-and-swap, so two nightlies cannot race on the pointer"), over the runs root thatruns listreads (results/, orDOKIMI_RUNS_DIR).runs baseline save NAME RUNpointsNAMEatRUN, arun_idthat some artifact under the root carries. The run is found by the scan, the same wayruns show RUN_IDfinds it. Arun_idno artifact carries exits 2. A baseline that already exists is moved, and the output line says what it held before (before:).--expect-previous RUN_IDsaves only whileNAMEpoints toRUN_ID. If it points elsewhere, or the baseline does not exist, the save exits 4 and the pointer stays as it was. Exit 4 isinvalid, which §2.6 lists for "a stale baseline". With--jsonthe document hasstatus: failed,verdict: invalidandreason: stale_baseline, plusexpectedandfoundunderdata. Of two nightlies that read the same pointer, one wins and the other exits 4.skill/a@main): segments of letters, digits,.,_,@or-, joined by/, 1-128 characters. Two kinds of name are refused, because Windows and macOS would read them as something else: a name Windows reserves for a device (con,nul.txt), and a name that differs from a saved one only in case (Mainbesidemain).rm MAINdoes not removemain.runs baseline listprints each baseline with its run and when it was saved.runs baseline rm NAMEremoves the pointer and keeps the run. All three commands take--json(cli_output.v1).<root>/.baselines/<NAME>. A/in the name is written%2F, and the file holds one line: therun_id.runs/kinds.py.., so.baselines/never meets a run directory (feat(runs): run directories: run.json, and a result.v1 when a run is stopped #1150).write_text_atomic(fix(io): publish pins, locks, atoms, probes, cache entries and task.json through one atomic write #697).saveandrmhold.baselines/.lock(flock, ormsvcrt.lockingon Windows), so the compare and the swap happen as one step.runs listdoes not walk.baselines/.runs gc --keep-baselinesnow also keeps the runs these pointers hold, as well as the runs of thebaseline savecopies it already kept. While it removes, it holds the same lock, so asavewaits and then finds the run gone instead of pointing at it..baselines/.lockis made if missing.--dry-runtakes no lock..baselines/that cannot be listed or locked, such as a file at that path.runs baselinedoes not read or change the copies thatbaseline save --from-evalwrites underbaselines/eval/. Thebaseline:NAMEreaders move in R-10.--expect-previousis a canonical flag of thebaselineconcept incli_flags.py.existing_root()moves fromcommands/runs.pytoruns/scan.py, so both command modules use it.Refs #94 R3
Changed output
These are new commands.
runs gc --keep-baselineskeeps more runs than before: the runs a pointer holds. Nothing else changes output.Exit codes, also in the
docs/runs.mdtable:RUNor--expect-previous; a run no artifact carries; no such baseline; no root; a.baselines/that cannot be listed or locked--expect-previousconflictEvidence
tests/test_runs_baseline.py: 50 tests on a tmp runs root, againstdocs/runs.md"Baselines" and its exit-code rows. Therunssuites together (baseline, rm/gc, scan, show): 288 passed, 1 skipped (the Windows junction test). The tests cover:list --json;--jsonfields);cli.pyprocesses. The test holds the lock and waits until/proc/locksshows both processes waiting on it, then releases. One process exits 0, the other 4, and the pointer holds the winner's run. This test is Linux only;../x,a//b,.hidden,a.,a:b,a\b,"", 129 characters,con,NUL.txt,com1…) and 7 accepted ones (128 characters, 43 segments,console);saveand forrm;rmof a missing baseline, and no root;runs listdoes not walk.baselines/;gc --keep-baselines: it keeps a pointer's run, removes nothing when a pointer cannot be read, waits on the lock a test holds before removing anything (real process, Linux), takes no lock with--dry-runor without--keep-baselines, and exits 2 when.baselinesis a file or a read-only directory with no lock.flock;.baselines/;save, and none inrm;--dry-run.make ci-local --offlineon the first commit (5c347a2): 0 failed.range(2)→range(3)for thelistcolumn widths: the third width is never used;_HEAD256→257: a pointer is at most 129 bytes;range(10)→range(11)forCOM/LPT: it would also refusecom10, which no test names;check_namereturningNone: no caller uses its return value;required=Trueon thebaselinesubparsers flipped toFalseis a gap. A bareruns baselinewould then crash withAttributeError: 'Namespace' object has no attribute 'func', and no test runs the group without a subcommand. Today it exits 2 with argparse's usage error.slot ci --quick(lint, mypy, CLI snapshot, the whole suite) 0 failed, 27576 passed. The fullmake ci-local --offlineon it runs before publishing.msvcrt.locking, the case-insensitive file system), macOS, a network file system'sflock, and the other Python versions of the matrix.Not in this PR
runs rm RUN_IDandruns gcwithout--keep-baselinesstill remove a run a baseline points to, which leaves the pointer dangling.runs baseline listdoes not check that the run still exists.RUNgiven as a run name (--name, once feat(runs): run directories: run.json, and a result.v1 when a run is stopped #1150 lands), and--from RUN(R-8).baseline:NAMEincompare, and the move of thebaseline save/comparereaders (R-10).runs export|import(the rest of R3).baseline save --from-evalcopies are not listed byruns baseline list.Open questions
reason: stale_baselinefor a CAS conflict is my reading of §2.6, whereinvalidlists "a stale baseline". Is that the code we want, or should a lost race have its own code?--expect-previousmoves an existing baseline, the way a plain pointer write would. Should it instead refuse, asgit tagdoes without-f?--expect-previous ""is refused as not a run id (exit 2), so there is no "create only if absent" form. The first nightly cannot race-proof its first save. Should there be such a form?Checklist
pytestpasses:tests/test_runs_baseline.py;slot ci --quick0 failedruff,mypy, and schema checks pass:ci-localstatic and typecheck jobs passdocs/runs.md(new "Baselines" section,--keep-baselines, exit-code rows),docs/quickstart.md(exit 4),docs/documentation-index.md,docs/reference/cli-interface.jsonregeneratedAGENTS.md: mutations above; bad inputs (../x,con.x, a run that does not exist, a secondrm,.baselinesas a file and read-only)raise/exit and test names the doc, contract or issue it traces to:docs/runs.md"Baselines" and its exit-code table, CLI API: the evaluation loop at the top level, subject and eval groups below, one enforced flag vocabulary (proposal v2.1) #94 §2.3/§2.6runs/baseline.py,commands/runs_baseline.py,tests/test_runs_baseline.py, the fragment