Skip to content

Commit d0eed4f

Browse files
committed
fix(runs): prune a converted job's gone trials only once its config reserves their names
1 parent a8226ba commit d0eed4f

4 files changed

Lines changed: 76 additions & 39 deletions

File tree

‎app/nullius/importers/atif.py‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -345,13 +345,6 @@ def _write_job(docs: list, root: str, out_root: str, job_name: str, command: str
345345

346346
summaries = [convert_document(path, doc, name, job_dir)
347347
for (path, doc), name in zip(docs, names)]
348-
# Derived output this importer wholly owns: a trial the tree no longer
349-
# holds is stale and would keep being counted.
350-
produced = {s["trial_dir"] for s in summaries}
351-
for name in os.listdir(job_dir):
352-
sub = os.path.join(job_dir, name)
353-
if os.path.isdir(sub) and name not in produced:
354-
shutil.rmtree(sub)
355348

356349
# ordered as instants, not as text: one stamp with a zone and one without sort apart as strings
357350
starts = sorted((s["started_at"] for s in summaries if s["started_at"]), key=_instant)
@@ -381,6 +374,14 @@ def _write_job(docs: list, root: str, out_root: str, job_name: str, command: str
381374
"trial_documents": importers.reservations(
382375
job_dir, ((name, os.path.realpath(path)) for (path, _doc), name in zip(docs, names)), *existing),
383376
})
377+
# Derived output this importer wholly owns: a trial the tree no longer
378+
# holds is stale and would keep being counted. Pruned once
379+
# config.json reserves its name: a refresh cut short before then keeps the folder.
380+
produced = {s["trial_dir"] for s in summaries}
381+
for name in os.listdir(job_dir):
382+
sub = os.path.join(job_dir, name)
383+
if os.path.isdir(sub) and name not in produced:
384+
shutil.rmtree(sub)
384385
return job_dir
385386

386387

‎app/nullius/importers/otel.py‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,6 @@ def _write_job(documents: list, out_root: str, job_name: str, command: str,
232232
os.makedirs(job_dir, exist_ok=True)
233233
summaries = [write_trial(doc, job_dir, name) for doc, name in zip(documents, names)]
234234

235-
# Derived output this importer wholly owns: a trial the export no longer
236-
# produces is stale and would keep being counted.
237-
produced = {s["trial_dir"] for s in summaries}
238-
for name in os.listdir(job_dir):
239-
sub = os.path.join(job_dir, name)
240-
if os.path.isdir(sub) and name not in produced:
241-
shutil.rmtree(sub)
242-
243235
starts = sorted(s["started_at"] for s in summaries if s["started_at"])
244236
ends = sorted(s["finished_at"] for s in summaries if s["finished_at"])
245237
_dump(os.path.join(job_dir, "result.json"), {
@@ -263,6 +255,14 @@ def _write_job(documents: list, out_root: str, job_name: str, command: str,
263255
"skipped": skipped},
264256
"trial_documents": importers.reservations(job_dir, zip(names, ids), *existing),
265257
})
258+
# Derived output this importer wholly owns: a trial the export no longer
259+
# produces is stale and would keep being counted. Pruned once
260+
# config.json reserves its name: a refresh cut short before then keeps the folder.
261+
produced = {s["trial_dir"] for s in summaries}
262+
for name in os.listdir(job_dir):
263+
sub = os.path.join(job_dir, name)
264+
if os.path.isdir(sub) and name not in produced:
265+
shutil.rmtree(sub)
266266
return job_dir
267267

268268

‎app/tests/harnesses/test_atif.py‎

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import tempfile
1717
import unittest
1818
from pathlib import Path
19+
from unittest import mock
1920

2021
APP = Path(__file__).resolve().parents[2]
2122
sys.path.insert(0, str(APP))
@@ -82,6 +83,15 @@ def test_a_broken_file_that_says_it_is_atif_is_claimed_so_it_can_be_refused(self
8283
self.assertFalse(atif.detect(other))
8384

8485

86+
def cut_at_config(dump):
87+
"""*dump*, failing on the job's ``config.json``: a refresh cut short before it publishes."""
88+
def cut(path, data):
89+
if os.path.basename(path) == "config.json":
90+
raise OSError("cut short")
91+
return dump(path, data)
92+
return cut
93+
94+
8595
class Conversion(unittest.TestCase):
8696
def setUp(self):
8797
self.out = tempfile.mkdtemp()
@@ -149,18 +159,23 @@ def test_one_tree_through_a_link_or_with_a_folder_deleted_by_hand_keeps_its_tria
149159
self.assertEqual(held["a__b"], "s-first")
150160

151161
def test_a_gone_document_keeps_its_name_from_a_job_without_reservations_and_from_a_longer_name(self):
152-
src = tempfile.mkdtemp()
153-
write_doc(src, "a/b.json", minimal(session_id="s-gone"))
154-
write_doc(src, "c.json", minimal())
155-
job_dir, _ = self.convert(src)
156-
config = json.loads(Path(job_dir, "config.json").read_text())
157-
del config["trial_documents"] # as a conversion before them wrote it
158-
Path(job_dir, "config.json").write_text(json.dumps(config))
159-
os.remove(os.path.join(src, "a", "b.json"))
160-
self.convert(src) # its folder is pruned, its name kept
161-
write_doc(src, "a__b.json", minimal(session_id="s-new"))
162-
_, job = self.convert(src)
163-
self.assertNotIn("a__b", {t.trial_name for t in job.trials})
162+
for cut in (False, True):
163+
with self.subTest(cut_before_config=cut):
164+
src = tempfile.mkdtemp()
165+
write_doc(src, "a/b.json", minimal(session_id="s-gone"))
166+
write_doc(src, "c.json", minimal())
167+
job_dir, _ = self.convert(src, job_name=f"cut-{cut}")
168+
config = json.loads(Path(job_dir, "config.json").read_text())
169+
del config["trial_documents"] # as a conversion before them wrote it
170+
Path(job_dir, "config.json").write_text(json.dumps(config))
171+
os.remove(os.path.join(src, "a", "b.json"))
172+
if cut: # a first refresh cut short before its config
173+
with mock.patch.object(atif, "_dump", cut_at_config(atif._dump)), self.assertRaises(OSError):
174+
self.convert(src, job_name=f"cut-{cut}")
175+
self.convert(src, job_name=f"cut-{cut}") # its folder is pruned, its name kept
176+
write_doc(src, "a__b.json", minimal(session_id="s-new"))
177+
_, job = self.convert(src, job_name=f"cut-{cut}")
178+
self.assertNotIn("a__b", {t.trial_name for t in job.trials})
164179

165180
src = tempfile.mkdtemp() # a gone file named like another's longer name
166181
longer = "a__b__" + hashlib.sha256(b"a/b.json").hexdigest()[:8]

‎app/tests/harnesses/test_otel_import.py‎

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,23 @@
1010
import tempfile
1111
import unittest
1212
from pathlib import Path
13+
from unittest import mock
1314

1415
APP = Path(__file__).resolve().parents[2]
1516
sys.path.insert(0, str(APP))
1617

1718
from nullius.importers import otel as importer
1819

1920

21+
def cut_at_config(dump):
22+
"""*dump*, failing on the job's ``config.json``: a refresh cut short before it publishes."""
23+
def cut(path, data):
24+
if os.path.basename(path) == "config.json":
25+
raise OSError("cut short")
26+
return dump(path, data)
27+
return cut
28+
29+
2030
class Detect(unittest.TestCase):
2131

2232
def test_a_directory_of_only_refused_files_is_not_claimed(self):
@@ -108,18 +118,29 @@ def export(name, tid, span="agent run"):
108118
def trials():
109119
return sorted(n for n in os.listdir(job_dir) if os.path.isdir(os.path.join(job_dir, n)))
110120

111-
export("a.json", gone)
112-
export("x.json", other, "other")
113-
job_dir = importer.convert(src, self.out)[0]
114-
os.remove(os.path.join(src, "a.json"))
115-
importer.convert(src, self.out) # its folder is pruned, its name kept
116-
self.assertEqual(trials(), ["other__1234567"])
117-
export("b.json", newcomer)
118-
importer.convert(src, self.out)
119-
self.assertEqual(trials(), ["agent_run__" + newcomer, "other__1234567"])
120-
export("a.json", gone) # back: its own name again
121-
importer.convert(src, self.out)
122-
self.assertEqual(trials(), ["agent_run__abcdef1", "agent_run__" + newcomer, "other__1234567"])
121+
for cut in (False, True):
122+
with self.subTest(cut_before_config=cut):
123+
for name in os.listdir(src):
124+
os.remove(os.path.join(src, name))
125+
export("a.json", gone)
126+
export("x.json", other, "other")
127+
job_dir = importer.convert(src, self.out, job_name=f"cut-{cut}")[0]
128+
os.remove(os.path.join(src, "a.json"))
129+
if cut: # a job without reservations, its first refresh cut short
130+
config = json.loads(Path(job_dir, "config.json").read_text())
131+
del config["trial_documents"]
132+
Path(job_dir, "config.json").write_text(json.dumps(config))
133+
with mock.patch.object(importer, "_dump", cut_at_config(importer._dump)), \
134+
self.assertRaises(OSError):
135+
importer.convert(src, self.out, job_name=f"cut-{cut}")
136+
importer.convert(src, self.out, job_name=f"cut-{cut}") # its folder is pruned, its name kept
137+
self.assertEqual(trials(), ["other__1234567"])
138+
export("b.json", newcomer)
139+
importer.convert(src, self.out, job_name=f"cut-{cut}")
140+
self.assertEqual(trials(), ["agent_run__" + newcomer, "other__1234567"])
141+
export("a.json", gone) # back: its own name again
142+
importer.convert(src, self.out, job_name=f"cut-{cut}")
143+
self.assertEqual(trials(), ["agent_run__abcdef1", "agent_run__" + newcomer, "other__1234567"])
123144

124145
def test_a_refused_file_deeper_down_is_named_by_its_path_under_the_root_once(self):
125146
src = tempfile.mkdtemp()

0 commit comments

Comments
 (0)