lab-kit-cli 1.10.1__tar.gz → 1.11.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/CHANGELOG.md +21 -0
  2. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/PKG-INFO +1 -1
  3. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/docs/model.md +22 -4
  4. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/pyproject.toml +1 -1
  5. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/skills/lab-work/SKILL.md +5 -0
  6. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/src/lab_kit/__init__.py +1 -1
  7. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/src/lab_kit/checks.py +27 -1
  8. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/src/lab_kit/cli.py +6 -1
  9. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/src/lab_kit/record.py +22 -4
  10. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/src/lab_kit/runs.py +94 -3
  11. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/src/lab_kit/views.py +1 -1
  12. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/tests/test_integrity.py +54 -0
  13. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/.github/workflows/ci.yml +0 -0
  14. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/.github/workflows/policy.yml +0 -0
  15. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/.github/workflows/release.yml +0 -0
  16. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/.gitignore +0 -0
  17. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/AGENTS.md +0 -0
  18. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/CLAUDE.md +0 -0
  19. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/CONTRIBUTING.md +0 -0
  20. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/LICENSE +0 -0
  21. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/README.md +0 -0
  22. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/agents/reviewer.md +0 -0
  23. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/method/METHOD.md +0 -0
  24. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/skills/lab-mission/SKILL.md +0 -0
  25. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/skills/lab-review/SKILL.md +0 -0
  26. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/skills/lab-set-up/SKILL.md +0 -0
  27. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/skills/lab-talk/SKILL.md +0 -0
  28. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/src/lab_kit/history.py +0 -0
  29. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/src/lab_kit/site.py +0 -0
  30. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/src/lab_kit/supervise.py +0 -0
  31. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/src/lab_kit/write.py +0 -0
  32. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/tests/test_drafts.py +0 -0
  33. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/tests/test_flow.py +0 -0
  34. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/tests/test_held.py +0 -0
  35. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/tests/test_mission.py +0 -0
  36. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/tests/test_needs.py +0 -0
  37. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/tests/test_order.py +0 -0
  38. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/tests/test_parked.py +0 -0
  39. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/tests/test_refusals.py +0 -0
  40. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/tests/test_site.py +0 -0
  41. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/tests/test_states.py +0 -0
  42. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/tests/test_vocabulary.py +0 -0
  43. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/tests/test_wording.py +0 -0
  44. {lab_kit_cli-1.10.1 → lab_kit_cli-1.11.1}/tools/import_run.py +0 -0
@@ -1,5 +1,26 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.11.1
4
+
5
+ - **A run in progress in another checkout no longer fails `lab check`.** A run stays in the `.lab/running` of the
6
+ checkout that started it until it ends, so every other checkout of the same lab saw a run listed as started and
7
+ not in the store, and `lab check` failed there with "a run is never removed": with several sessions running
8
+ experiments in their own worktrees, the gate was red everywhere but in each run's own checkout. A run listed as
9
+ started and not ended, whose folder is in the `.lab/running` of another git worktree of the lab on this machine,
10
+ is now a note. A run listed as started whose folder is nowhere is still a problem, the prompt to declare it lost.
11
+
12
+ ## 1.11.0
13
+
14
+ - **An ended run that did not record what it used can be amended.** A run stopped before its machines' files came
15
+ back seals with no amount for a resource it ran on, and `lab check` says it "did not record what it used"; `lab
16
+ runs close` refused it, since it had ended. `lab runs close <run> "<why>" --used <resource>=<amount>...
17
+ --evidence <folder>` now writes a sealed amendment, `<run>.amended.json`, beside the run's folder in the store,
18
+ never inside it: the run's seal, the amounts, why, and every file of the evidence with its hash. `runs.tsv`
19
+ lists it on an `amended` row with its seal. It covers only resources the run ran on and did not record, once.
20
+ `lab check` counts the amounts, and checks the amendment's seal, its row, the run's seal and the evidence's
21
+ hashes.
22
+ - The skill `lab-work` changed: run `lab init` in a lab after upgrading.
23
+
3
24
  ## 1.10.1
4
25
 
5
26
  - **A fold, once committed, ends what can lock the folded decision.** `lab check` refused a draft folded into another
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: lab-kit-cli
3
- Version: 1.10.1
3
+ Version: 1.11.1
4
4
  Summary: A research lab's record, its method, and the command that keeps both.
5
5
  Project-URL: Homepage, https://github.com/pierg/lab-kit
6
6
  Project-URL: Issues, https://github.com/pierg/lab-kit/issues
@@ -173,7 +173,10 @@ Each arrow is a command that refuses when the step before it is missing. You see
173
173
  git: one row when a run or a trial starts, and one when it ends, with how it ended and its seal.
174
174
  Rows are only added. `lab run` reads that list as well as the store, so a clone without the store
175
175
  still knows what ran. `lab check` asks that every listed run is in the store with the seal that
176
- was listed, and that every run in the store is listed. Where the store is not mounted the check
176
+ was listed, and that every run in the store is listed. A run in progress is in the `.lab/running`
177
+ of the checkout that started it until it ends: one listed as started and not ended whose folder is
178
+ there in another git worktree of the lab on this machine is running, and the check notes it
179
+ instead of asking for it in the store. Where the store is not mounted the check
177
180
  says so and checks no run.
178
181
  **A run that is lost.** A run can start and never report back: the machine dies with its folder on
179
182
  it. Its `started` row stays. `lab runs lost <run> "<why>"` adds a row that says so, with the reason
@@ -389,7 +392,8 @@ that resource, under `about:`.
389
392
  the store (`lab runs publish`) and copies one from a store this machine does not mount
390
393
  (`lab runs fetch`). Closing a run stops whatever its command left running, reads what it said it
391
394
  used, and takes `--used <resource>=<amount>` for a run that said nothing; that amount is recorded
392
- as stated by hand. Where the system gives no way to tell that a process is the run's, nothing is
395
+ as stated by hand. On a run that has already ended, `lab runs close` with `--used` and
396
+ `--evidence <folder>` writes an amendment instead (below, under `runs.tsv`). Where the system gives no way to tell that a process is the run's, nothing is
393
397
  stopped and the command says so. `lab runs lost` records a run that started and whose folder is
394
398
  gone for good.
395
399
  - **Once:** `lab init`. Run again, it restores the method, the skills and the reviewer role.
@@ -835,11 +839,25 @@ import it in place of a script, and no hash covers it. `lab lock` and `lab run`
835
839
  `lab check` reports it. Under a lock that 1.0.0 made, whose hash did count such a file, it stays.
836
840
 
837
841
  **`runs.tsv`**, beside the lock and in git, is written by `lab run` and by the run as it ends: `run`
838
- (its id), `kind` (run, trial), `what` (started, ended, lost), `at`, on the ended row `exit` and `seal`,
839
- and on a lost row `used` (`{"<resource>": <amount>}`, or empty) and `note` (why it is lost). When a
842
+ (its id), `kind` (run, trial), `what` (started, ended, lost, amended), `at`, on the ended row `exit` and `seal`,
843
+ on a lost row `used` (`{"<resource>": <amount>}`, or empty) and `note` (why it is lost), and on an
844
+ amended row the amendment's `seal`, `used` and `note` (why). When a
840
845
  run ends on a machine that cannot write the lab, `lab runs publish`, `lab runs fetch` or
841
846
  `lab runs close` adds its ended row.
842
847
 
848
+ **An amendment** records what an ended run used of a resource it ran on (one in its `cap`) and did not
849
+ record itself, for example because it was stopped before its machines' files came back:
850
+ `lab runs close <run> "<why>" --used <resource>=<amount>... --evidence <folder>`. The run's folder is
851
+ sealed, every file in it hashed, so nothing is added to it: the amendment is `<run>.amended.json`
852
+ beside the folder in the store, read-only, holding the run's seal, the amounts, why, and the
853
+ evidence the amounts were read from, a folder outside the run's (its path, relative to the store
854
+ when it is in it, and every file in it with its hash), with one seal over it all. Its seal goes into
855
+ `runs.tsv` on an amended row. A run is amended at most once, and what it recorded itself stands.
856
+ `lab check` counts the amended amounts as what the run used, and asks that the amendment matches
857
+ its seal, names this run with its seal, is the one `runs.tsv` lists, and that every file of its
858
+ evidence is still there with its hash; and that a run `runs.tsv` lists as amended has its amendment
859
+ beside it.
860
+
843
861
  **A run** writes into the folder `LAB_RUN` names. If it uses a metered resource it writes
844
862
  `used.json` there: `{"<resource>": <amount>}`, each amount a number, finite and not negative;
845
863
  anything else is refused when the run is sealed and reported by `lab check`. Its record is `run.json`.
@@ -5,7 +5,7 @@ build-backend = "hatchling.build"
5
5
  [project]
6
6
  dynamic = ["readme"]
7
7
  name = "lab-kit-cli"
8
- version = "1.10.1"
8
+ version = "1.11.1"
9
9
  description = "A research lab's record, its method, and the command that keeps both."
10
10
  license = "MIT"
11
11
  authors = [{ name = "Piergiuseppe Mallozzi" }]
@@ -263,6 +263,11 @@ what should come next: open a decision.
263
263
  - **A run's process died.** `lab runs close <run> "<why>"`. It stops whatever the run left going and
264
264
  reads the run's `used.json`. If the run used a metered resource and wrote none, say what it used:
265
265
  `--used <resource>=<amount>`.
266
+ - **A run ended without recording what it used of a resource it ran on** (it was stopped before its
267
+ machines' files came back, and `lab check` says it "did not record what it used"). Copy the files
268
+ the amounts can be read from to a folder outside the run's, read the amounts there, then
269
+ `lab runs close <run> "<why>" --used <resource>=<amount>... --evidence <folder>`, and commit
270
+ `runs.tsv`. The amendment is sealed beside the run's folder; the folder is never touched.
266
271
  - **A run started and its folder is gone for good** (the machine was deleted with it). `lab runs lost
267
272
  <run> "<why>"`, with `--used <resource>=<amount>` for what it spent, then commit. It counts as a
268
273
  failed run, and another may follow.
@@ -1,2 +1,2 @@
1
1
  """lab-kit: a research lab's record, its method, and the command that keeps both."""
2
- __version__ = "1.10.1"
2
+ __version__ = "1.11.1"
@@ -32,6 +32,22 @@ def run(lab, rederive=True):
32
32
  if lab.held.get("error"): # what locks a decision is read from git; without it a lock could be edited away unseen
33
33
  bad("history", "git", f"what locks a decision could not be read from the lab's history: {lab.held['error']}")
34
34
 
35
+ elsewhere, others = [], [] # runs runs.tsv lists as started and not ended whose folder is in another checkout's .lab/running
36
+
37
+ def running_elsewhere():
38
+ """The runs whose folder is in the .lab/running of another git worktree of this lab, on this machine."""
39
+ if not others:
40
+ top = history.git(root, "rev-parse", "--show-toplevel")
41
+ sub = os.path.relpath(root, top.strip()) if top else "."
42
+ trees = [l[len("worktree "):] for l in (history.git(root, "worktree", "list", "--porcelain") or "").splitlines() if l.startswith("worktree ")]
43
+ found = set()
44
+ for t in trees:
45
+ folder = os.path.join(t, sub, ".lab", "running")
46
+ if os.path.realpath(os.path.join(t, sub)) != os.path.realpath(root) and os.path.isdir(folder):
47
+ found.update(os.listdir(folder))
48
+ others.append(found)
49
+ return others[0]
50
+
35
51
  absent = {} # the libraries the charter names whose folder is not on this machine
36
52
 
37
53
  def see(where, page, who=""):
@@ -243,7 +259,9 @@ def run(lab, rederive=True):
243
259
  where = f"{r['dir']}"
244
260
  listed = x["listed"].get(r["id"])
245
261
  if r["missing"]: # runs.tsv is in git and the store is not: a run the ledger lists stays where it was sealed
246
- if here and not r["lost"]:
262
+ if here and not r["lost"] and r["state"] == "running" and r["id"] in running_elsewhere(): # it runs in another checkout here
263
+ elsewhere.append(r["id"])
264
+ elif here and not r["lost"]:
247
265
  bad("runs", where, f"is listed in {x['dir']}/runs.tsv and is not in the store: a run is never removed. If it started and its folder is gone for good, `lab runs lost {r['id']} \"<why>\"` records that")
248
266
  continue
249
267
  if listed and listed["lost"]:
@@ -271,6 +289,12 @@ def run(lab, rederive=True):
271
289
  why = runs_.verify(os.path.join(root, r["dir"]))
272
290
  if why:
273
291
  bad("runs", where, why)
292
+ if r.get("amendment") is not None:
293
+ why = runs_.check_amendment(lab, r, listed)
294
+ if why:
295
+ bad("runs", where, why)
296
+ elif listed and listed.get("amended") and not r["local"]:
297
+ bad("runs", where, f"runs.tsv lists an amendment of it, and {r['id']}.amended.json is not beside it in the store")
274
298
  for name, cap in (r.get("cap") or {}).items():
275
299
  used = (r.get("used") or {}).get(name)
276
300
  if used is None:
@@ -419,6 +443,8 @@ def run(lab, rederive=True):
419
443
  waiting = [r for r in lab.runs if r["local"] and r["state"] != "running"]
420
444
  if waiting:
421
445
  notes.append(f"{len(waiting)} ended run(s) are still on this machine, not in the store: `lab runs publish`")
446
+ if elsewhere: # its own checkout checks it; it is held here once runs.tsv says it ended
447
+ notes.append(f"{len(elsewhere)} run(s) still running in another checkout on this machine, checked once they end: {', '.join(elsewhere)}")
422
448
  found, said = history.check(lab)
423
449
  said += history.uncommitted(lab) # not held by git yet: a note, and a failure when the check is strict
424
450
  problems += found
@@ -32,6 +32,7 @@ For evidence
32
32
  lab run <experiment> [--trial] [--wait] [--changed "<what changed since the last run, and why>"] -- <command>
33
33
  lab runs every run; also: lab runs publish [--keep] | fetch <run>
34
34
  lab runs close <run> "<why>" [--used <resource>=<amount>]... a run whose process died; --used for one that wrote no used.json
35
+ lab runs close <run> "<why>" --used <resource>=<amount>... --evidence <folder> an ended run that did not record what it used of a resource it ran on
35
36
  lab runs lost <run> "<why>" [--used <resource>=<amount>]... a run that started and whose folder is gone for good
36
37
  lab review <experiment> plan|results pass|fail "<note>" [--by <who>]
37
38
  lab check [--no-rederive] [--strict] runs every command in results.tsv: only on a lab you trust. --strict fails when
@@ -163,13 +164,17 @@ def main(argv=None):
163
164
  used[name] = float(text) if "." in text or "e" in text.lower() else int(text)
164
165
  except ValueError:
165
166
  raise SystemExit(f"--used {pair}: <resource>=<amount>")
167
+ evidence = flag("--evidence")
166
168
  if len(rest) != 3:
167
169
  raise SystemExit(USAGE)
170
+ if evidence and rest[0] == "lost":
171
+ raise SystemExit("--evidence is for `lab runs close` on a run that has ended")
168
172
  if rest[0] == "lost":
169
173
  with runs.exclusive(lab.root):
170
174
  print(f"recorded as lost in {runs.lost(Lab(lab.root), rest[1], rest[2], used)}; commit it")
171
175
  else:
172
- print(f"closed: {runs.close(lab, rest[1], rest[2], used)}")
176
+ done = runs.close(lab, rest[1], rest[2], used, evidence)
177
+ print(f"amended: {done[0]}, listed in {done[1]}; commit it" if isinstance(done, tuple) else f"closed: {done}")
173
178
  elif rest[0] == "publish":
174
179
  keep = flag("--keep", switch=True)
175
180
  for r in lab.runs: # a run that ended where the lab could not be written gets its seal listed here
@@ -594,11 +594,22 @@ class Lab:
594
594
  for r in rows[1:]:
595
595
  row = dict(zip(LEDGER, r + [""] * (len(LEDGER) - len(r))))
596
596
  run, what = row["run"], row["what"]
597
- if len(r) < 6 or len(r) > len(LEDGER) or not RUN_ID.match(run) or row["kind"] not in ("run", "trial") or what not in ("started", "ended", "lost") or not moment(row["at"]):
598
- self.problems.append(f"runs: {where}: the row for {run[:40]!r} is not `run, kind, started|ended|lost, time, exit, seal, used, note`")
597
+ if len(r) < 6 or len(r) > len(LEDGER) or not RUN_ID.match(run) or row["kind"] not in ("run", "trial") or what not in ("started", "ended", "lost", "amended") or not moment(row["at"]):
598
+ self.problems.append(f"runs: {where}: the row for {run[:40]!r} is not `run, kind, started|ended|lost|amended, time, exit, seal, used, note`")
599
599
  continue
600
- e = out.setdefault(run, {"id": run, "kind": row["kind"], "started": None, "ended": None, "exit": None, "seal": None, "lost": "", "used": None})
601
- if (what == "started" and e["started"]) or (what != "started" and e["ended"]) or e["kind"] != row["kind"]:
600
+ e = out.setdefault(run, {"id": run, "kind": row["kind"], "started": None, "ended": None, "exit": None, "seal": None, "lost": "", "used": None, "amended": None})
601
+ if what == "amended":
602
+ try:
603
+ used = json.loads(row["used"]) if row["used"] else {}
604
+ except ValueError:
605
+ used = None
606
+ if e["amended"] or not e["ended"] or e["lost"] or e["kind"] != row["kind"]:
607
+ self.problems.append(f"runs: {where}: {run} is amended twice, or before it ended, or after it was declared lost")
608
+ elif not flat(row["note"]) or not re.fullmatch(r"[0-9a-f]{64}", row["seal"]) or not isinstance(used, dict) or not used or not all(amount(v) for v in used.values()):
609
+ self.problems.append(f"runs: {where}: {run} is amended with no reason, no seal, or a use that is not `{{\"<resource>\": <amount>}}`")
610
+ else:
611
+ e["amended"] = {"at": row["at"], "seal": row["seal"], "used": used, "why": flat(row["note"])}
612
+ elif (what == "started" and e["started"]) or (what != "started" and e["ended"]) or e["kind"] != row["kind"]:
602
613
  self.problems.append(f"runs: {where}: {run} is listed twice as {'started' if what == 'started' else 'ended'}")
603
614
  elif what == "started":
604
615
  e["started"] = row["at"]
@@ -795,6 +806,13 @@ class Lab:
795
806
  r["orphan"] = r["state"] == "running" and r.get("host") == os.uname().nodename and not alive(r.get("pid"))
796
807
  note = d + ".unpublished"
797
808
  r["unpublished"] = read(note).strip() if os.path.exists(note) else ""
809
+ r["own_used"] = dict(r.get("used") or {})
810
+ amended = d + ".amended.json" # what the run used and did not record, stated after it ended: beside its folder, not in it
811
+ r["amendment"] = None if r["local"] or not os.path.lexists(amended) else (self._json(amended) or {})
812
+ a = r["amendment"]
813
+ if isinstance(a, dict) and isinstance(a.get("used"), dict):
814
+ more = {k: v for k, v in a["used"].items() if k not in r["own_used"]}
815
+ r["used"], r["used_amended"] = {**r["own_used"], **more}, sorted(more)
798
816
  out.append(r)
799
817
  here = {r["id"] for r in out}
800
818
  for x in self.experiments: # a run the ledger lists and the store does not hold: known to have run, with nothing to read
@@ -374,16 +374,16 @@ def lost(lab, run_id, why, used=None):
374
374
  stop(f"no run {run_id} is listed as started")
375
375
 
376
376
 
377
- def close(lab, run_id, why, used=None):
377
+ def close(lab, run_id, why, used=None, evidence=""):
378
378
  """Close a run whose process died: stop what it left running, record that it stopped and what it used, seal what it
379
- left, move it into the store."""
379
+ left, move it into the store. On a run that has ended, record what it used and did not record (`amend`)."""
380
380
  run_id_or_stop(run_id)
381
381
  if not R.flat(why):
382
382
  stop("say why the run is closed")
383
383
  for r in lab.runs:
384
384
  if r["id"] == run_id:
385
385
  if r["state"] != "running":
386
- stop(f"{run_id} has already ended")
386
+ return amend(lab, run_id, why, used, evidence)
387
387
  if r["missing"]:
388
388
  stop(f"{run_id} is listed in runs.tsv and its folder is not on this machine: close it where it ran, or if it is gone for good, `lab runs lost {run_id} \"<why>\"`")
389
389
  if not r["orphan"]:
@@ -407,6 +407,97 @@ def close(lab, run_id, why, used=None):
407
407
  stop(f"no run {run_id}")
408
408
 
409
409
 
410
+ def amendment(lab, run_id):
411
+ """Where the amendment of an ended run is kept: beside its folder in the store, never inside it, since the seal
412
+ covers every file in the folder."""
413
+ return os.path.join(lab.store, run_id + ".amended.json")
414
+
415
+
416
+ def evidence_files(folder):
417
+ """Every file under the evidence, with its hash."""
418
+ if os.path.isfile(folder):
419
+ return {os.path.basename(folder): R.sha(folder)}
420
+ return {os.path.relpath(os.path.join(top, f), folder): R.sha(os.path.join(top, f)) for top, _, fs in os.walk(folder) for f in fs}
421
+
422
+
423
+ def evidence_at(lab, where):
424
+ return where if os.path.isabs(where) else os.path.join(lab.store_root, where)
425
+
426
+
427
+ def check_amendment(lab, r, listed):
428
+ """Why the amendment beside an ended run does not stand, or None."""
429
+ a = r.get("amendment")
430
+ if not isinstance(a, dict) or not isinstance(a.get("used"), dict) or not isinstance(a.get("evidence"), dict) or not isinstance(a["evidence"].get("files"), dict):
431
+ return "its amendment cannot be read as one `lab runs close` wrote"
432
+ if R.seal_of(a) != a.get("seal"):
433
+ return "its amendment does not match its seal"
434
+ if a.get("run") != r["id"] or a.get("run_seal") != r.get("seal"):
435
+ return "its amendment is of another run, or of this run with another seal"
436
+ if not listed or (listed.get("amended") or {}).get("seal") != a["seal"]:
437
+ return f"its amendment is not the one runs.tsv lists: `lab runs close {r['id']}` lists it when it writes it"
438
+ for k, v in a["used"].items():
439
+ if not R.amount(v):
440
+ return f"its amendment records {v!r} as what it used of {k}: an amount is a number, finite and not negative"
441
+ if k in (r.get("own_used") or {}):
442
+ return f"its amendment states what it used of {k}, which the run recorded itself"
443
+ where = evidence_at(lab, str(a["evidence"].get("path") or ""))
444
+ if not a["evidence"]["files"] or not os.path.exists(where):
445
+ return f"the evidence of its amendment, {a['evidence'].get('path')}, is not there"
446
+ for p, h in a["evidence"]["files"].items():
447
+ f = where if os.path.isfile(where) else os.path.join(where, p)
448
+ if not os.path.isfile(f) or R.sha(f) != h:
449
+ return f"the evidence of its amendment changed: {p} does not match its hash"
450
+ return None
451
+
452
+
453
+ def amend(lab, run_id, why, used, evidence):
454
+ """Record what an ended run used and did not record: a sealed amendment beside its folder in the store, naming
455
+ the files the amounts were read from with their hashes, and listed in runs.tsv. What the run recorded stands."""
456
+ r = next((x for x in lab.runs if x["id"] == run_id), None)
457
+ if r["missing"] or r["local"]:
458
+ stop(f"{run_id} is not in the store here: an amendment is written beside a run in the store" + (", so `lab runs publish` it first" if r["local"] else ""))
459
+ if not used:
460
+ stop("--used <resource>=<amount>: what the run used and did not record")
461
+ if not evidence:
462
+ stop("--evidence <folder>: the files the amounts were read from, kept outside the run's folder")
463
+ used = stated(lab, used)
464
+ own = r.get("own_used") or {}
465
+ for k in used:
466
+ if k in own:
467
+ stop(f"{run_id} recorded what it used of {k} itself, and that stands")
468
+ if k not in (r.get("cap") or {}):
469
+ stop(f"{run_id} did not run on {k}")
470
+ rdir = os.path.join(lab.root, r["dir"])
471
+ why_not = verify(rdir)
472
+ if why_not:
473
+ stop(f"{run_id} does not match its seal: {why_not}")
474
+ path = amendment(lab, run_id)
475
+ if os.path.lexists(path) or (lab.nodes[r["experiment"]]["listed"].get(run_id) or {}).get("amended"):
476
+ stop(f"{run_id} has already been amended: an amendment is never rewritten")
477
+ ev = os.path.abspath(evidence)
478
+ if not os.path.exists(ev):
479
+ stop(f"--evidence {evidence}: not there")
480
+ if ev == os.path.abspath(rdir) or ev.startswith(os.path.realpath(rdir) + os.sep) or ev.startswith(os.path.abspath(rdir) + os.sep):
481
+ stop("--evidence is outside the run's folder: the run's own files are sealed as they are")
482
+ files = evidence_files(ev)
483
+ if not files:
484
+ stop(f"--evidence {evidence}: holds no files")
485
+ store = os.path.realpath(lab.store_root)
486
+ real = os.path.realpath(ev)
487
+ where = os.path.relpath(real, store) if real.startswith(store + os.sep) else real
488
+ a = {"run": run_id, "run_seal": r["seal"], "at": now(), "why": " ".join(why.split()), "used": used,
489
+ "evidence": {"path": where, "files": dict(sorted(files.items()))}}
490
+ a["seal"] = R.seal_of(a)
491
+ tmp = path + ".tmp"
492
+ with open(tmp, "w") as f:
493
+ json.dump(a, f, indent=1, sort_keys=True)
494
+ f.write("\n")
495
+ os.chmod(tmp, 0o444)
496
+ os.rename(tmp, path)
497
+ ledger(lab, lab.nodes[r["experiment"]], run_id, r["kind"], "amended", a["at"], seal=a["seal"], used=json.dumps(used, sort_keys=True), note=why)
498
+ return lab.rel(path), os.path.join(lab.nodes[r["experiment"]]["dir"], "runs.tsv")
499
+
500
+
410
501
  def listing(lab):
411
502
  out = []
412
503
  for r in lab.runs:
@@ -264,7 +264,7 @@ def _show(lab, ident):
264
264
  if x["results"]:
265
265
  out += ["", "Results" + ("" if x["reviewed"] else " (not reviewed: not to be used on a card yet)")] + [f"- {r['name']} = {r['value']}" + (f", shown as {R.shown(r)}" if R.shown(r) != r["value"] else "") + f" ({r.get('label') or 'no label'}; against: {r.get('versus', '')})" for r in x["results"]]
266
266
  for r in x["trials"] + x["runs"]:
267
- used = "".join(f", used {num(v)} {lab.resources.get(k, {}).get('unit', k)}" + (" (stated by hand when it was closed)" if k in (r.get("used_by_hand") or []) else "") for k, v in (r.get("used") or {}).items() if R.amount(v))
267
+ used = "".join(f", used {num(v)} {lab.resources.get(k, {}).get('unit', k)}" + (" (stated by hand when it was closed)" if k in (r.get("used_by_hand") or []) else " (stated after it ended, from the evidence its amendment names)" if k in (r.get("used_amended") or []) else "") for k, v in (r.get("used") or {}).items() if R.amount(v))
268
268
  out += ["", f"{'Trial run' if r['kind'] == 'trial' else 'Run'} {r['id']}: {r['state']}" + (f" (exit {r['exit']})" if r["state"] == "failed" else "") + f"{used}, {r['dir']}"]
269
269
  out += [f" declared lost: {r['lost']}"] if r["lost"] else [" listed in runs.tsv; its folder is not in the store here"] if r["missing"] else []
270
270
  out += [f" closed by hand: {r['closed']}"] if r.get("closed") and not r["lost"] else []
@@ -313,6 +313,52 @@ def test_what_a_run_says_it_used_is_an_amount(tmp_path):
313
313
  assert "gpu: 0 of 10 hours used" in lab(root, "now") and "nan" not in lab(root, "now").lower()
314
314
 
315
315
 
316
+ def test_an_ended_run_that_did_not_record_what_it_used_is_amended_beside_its_folder(tmp_path):
317
+ root = str(tmp_path)
318
+ open_question(root)
319
+ ident, d, command = experiment(root, uses="[gpu]", command=WRITES % "0.5") # it writes no used.json: stopped before it could
320
+ lab(root, "review", ident, "plan", "pass", "One thing varies; the refuting line can hold.")
321
+ lab(root, "lock", ident)
322
+ assert "finished" in lab(root, "run", ident, "--wait", "--", *command)
323
+ run_id = open(f"{d}/runs.tsv").read().splitlines()[1].split("\t")[0]
324
+ assert "did not record what it used" in lab(root, "check", "--no-rederive", ok=False)
325
+ os.makedirs(f"{root}/recovered/helper-1")
326
+ open(f"{root}/recovered/helper-1/used.json", "w").write('{"gpu": 0.75}\n')
327
+
328
+ # it names its evidence, outside the run's folder, and only what the run ran on and did not record
329
+ assert "--evidence <folder>" in lab(root, "runs", "close", run_id, "stopped", "--used", "gpu=0.75", ok=False)
330
+ assert "outside the run's folder" in lab(root, "runs", "close", run_id, "stopped", "--used", "gpu=0.75", "--evidence", f"runs/{run_id}", ok=False)
331
+ assert "a metered resource" in lab(root, "runs", "close", run_id, "stopped", "--used", "laptop=1", "--evidence", "recovered", ok=False)
332
+ assert "--evidence is for" in lab(root, "runs", "lost", run_id, "stopped", "--evidence", "recovered", ok=False)
333
+ out = lab(root, "runs", "close", run_id, "Stopped before its members' files came back; read from their used.json.", "--used", "gpu=0.75", "--evidence", "recovered")
334
+ assert f"amended: runs/{run_id}.amended.json" in out
335
+ seal = json.load(open(f"{root}/runs/{run_id}/run.json"))["seal"]
336
+ a = json.load(open(f"{root}/runs/{run_id}.amended.json"))
337
+ assert a["run_seal"] == seal and a["used"] == {"gpu": 0.75} and a["evidence"] == {"path": "recovered", "files": {"helper-1/used.json": a["evidence"]["files"]["helper-1/used.json"]}}
338
+ assert [r.split("\t")[2] for r in open(f"{d}/runs.tsv").read().splitlines()[1:]] == ["started", "ended", "amended"]
339
+ assert "the record is sound" in lab(root, "check", "--no-rederive")
340
+ assert "used 0.75 hours (stated after it ended" in lab(root, "show", ident) and "gpu: 0.75 of 10 hours used" in lab(root, "now")
341
+ assert "never rewritten" in lab(root, "runs", "close", run_id, "again", "--used", "gpu=1", "--evidence", "recovered", ok=False)
342
+
343
+ # the evidence, the amendment and its row in runs.tsv hold one another
344
+ open(f"{root}/recovered/helper-1/used.json", "w").write('{"gpu": 0.5}\n')
345
+ assert "the evidence of its amendment changed" in lab(root, "check", "--no-rederive", ok=False)
346
+ open(f"{root}/recovered/helper-1/used.json", "w").write('{"gpu": 0.75}\n')
347
+ ledger = open(f"{d}/runs.tsv").read()
348
+ open(f"{d}/runs.tsv", "w").write("\n".join(ledger.splitlines()[:-1]) + "\n")
349
+ assert "is not the one runs.tsv lists" in lab(root, "check", "--no-rederive", ok=False)
350
+ open(f"{d}/runs.tsv", "w").write(ledger)
351
+ path = f"{root}/runs/{run_id}.amended.json"
352
+ text = open(path).read()
353
+ os.chmod(path, 0o644)
354
+ open(path, "w").write(text.replace('"gpu": 0.75', '"gpu": 0.25'))
355
+ assert "does not match its seal" in lab(root, "check", "--no-rederive", ok=False)
356
+ os.remove(path)
357
+ assert "is not beside it in the store" in lab(root, "check", "--no-rederive", ok=False)
358
+ open(path, "w").write(text)
359
+ assert "the record is sound" in lab(root, "check", "--no-rederive")
360
+
361
+
316
362
  def test_a_run_may_write_any_file_and_a_run_is_named_by_its_id(tmp_path):
317
363
  root = str(tmp_path)
318
364
  open_question(root)
@@ -470,6 +516,14 @@ def test_a_run_that_never_reports_back_is_declared_lost(tmp_path):
470
516
  assert f"lab runs lost {gone}" in lab(root, "run", ident, "--wait", "--", *command, ok=False)
471
517
  assert f"lab runs lost {gone}" in lab(root, "runs", "close", gone, "the machine is gone", ok=False)
472
518
  assert f"lab runs lost {gone}" in lab(root, "check", "--no-rederive", ok=False)
519
+ # while its folder is in another checkout's .lab/running on this machine, it is running there: a note, not a problem
520
+ other = str(tmp_path) + "-other"
521
+ subprocess.run(["git", "-C", root, "worktree", "add", "-q", "--detach", other], check=True, capture_output=True)
522
+ os.makedirs(f"{other}/.lab/running/{gone}")
523
+ out = lab(root, "check", "--no-rederive")
524
+ assert "the record is sound" in out and f"still running in another checkout on this machine, checked once they end: {gone}" in out
525
+ shutil.rmtree(f"{other}/.lab/running/{gone}")
526
+ assert f"lab runs lost {gone}" in lab(root, "check", "--no-rederive", ok=False)
473
527
  assert "no run 20260102-0931_G1.Q1.1_zzzz is listed" in lab(root, "runs", "lost", "20260102-0931_G1.Q1.1_zzzz", "gone", ok=False)
474
528
  assert "say why" in lab(root, "runs", "lost", gone, " ", ok=False)
475
529
  assert "--used laptop=1" in lab(root, "runs", "lost", gone, "The machine was deleted.", "--used", "laptop=1", ok=False)
File without changes
File without changes
File without changes
File without changes
File without changes