@@ -52,6 +52,54 @@ def test_delete_and_output_containment(self):
5252 self .assertFalse (child .exists ())
5353 self .assertTrue (outside .exists ())
5454
55+ def test_prepare_workdir_requires_marker_and_repository_provenance (self ):
56+ script = str (Path (C .DEMO ) / "prepare_workdir.py" )
57+ env = dict (os .environ , DBT_BIN = shutil .which ("true" ), GIT_CONFIG_NOSYSTEM = "1" ,
58+ GIT_CONFIG_GLOBAL = os .devnull )
59+
60+ def git (dest , * args ):
61+ subprocess .run (["git" , "-C" , str (dest ), * args ], env = env ,
62+ capture_output = True , text = True , check = True )
63+
64+ def prepare (dest ):
65+ return subprocess .run ([sys .executable , script , "train-refunds" , str (dest )], env = env ,
66+ capture_output = True , text = True )
67+
68+ with tempfile .TemporaryDirectory () as d :
69+ root = Path (d )
70+ for case in ("marker-only" , "missing-origin" , "wrong-origin" , "missing-marker" , "nested" ):
71+ with self .subTest (case = case ):
72+ dest = root / case
73+ dest .mkdir ()
74+ if case != "marker-only" :
75+ git (dest , "init" , "-q" )
76+ if case in ("wrong-origin" , "missing-marker" , "nested" ):
77+ remote = "[email protected] :other/project.git" if case == "wrong-origin" else C .
REMOTE 78+ git (dest , "remote" , "add" , "origin" , remote )
79+ if case == "nested" :
80+ dest = dest / "child"
81+ dest .mkdir ()
82+ if case != "missing-marker" :
83+ (dest / ".prepared" ).write_text ("train-refunds\n " )
84+ sentinel = dest / "keep.txt"
85+ sentinel .write_text ("preserve me\n " )
86+ result = prepare (dest )
87+ self .assertNotEqual (result .returncode , 0 , result .stdout + result .stderr )
88+ self .assertIn ("refusing to overwrite" , result .stderr )
89+ self .assertIn ("git repository rooted here with origin" , result .stderr )
90+ self .assertEqual (sentinel .read_text (), "preserve me\n " )
91+
92+ dest = root / "prepared"
93+ dest .mkdir () # Empty destinations still work.
94+ first = prepare (dest )
95+ self .assertEqual (first .returncode , 0 , first .stdout + first .stderr )
96+ sentinel = dest / "discard.txt"
97+ sentinel .write_text ("previous run\n " )
98+ rebuilt = prepare (dest )
99+ self .assertEqual (rebuilt .returncode , 0 , rebuilt .stdout + rebuilt .stderr )
100+ self .assertFalse (sentinel .exists ())
101+ self .assertEqual ((dest / ".prepared" ).read_text (), "train-refunds\n " )
102+
55103 def test_explicit_fake_and_saas_opt_in (self ):
56104 with tempfile .TemporaryDirectory () as d , patch .dict (os .environ , {}, clear = True ):
57105 C .setup_users (d )
@@ -329,6 +377,67 @@ def test_shell_ids(self):
329377 capture_output = True , text = True )
330378 self .assertNotEqual (proc .returncode , 0 )
331379
380+ def test_corrections_baseline_requires_explicit_reuse (self ):
381+ # Exercise the real shell branches without running agents or remote calls.
382+ here = Path (__file__ ).resolve ().parent
383+ with tempfile .TemporaryDirectory () as d :
384+ root = Path (d )
385+ harness = root / "harness"
386+ harness .mkdir ()
387+ for name in ("run_corrections.sh" , "shell_common.sh" ):
388+ shutil .copy (here / name , harness )
389+ historical = harness / "runs/saas-v2/eval/none.jsonl"
390+ historical .parent .mkdir (parents = True )
391+ historical .write_text ('{"historical": true}\n ' )
392+ source = root / "explicit baseline.jsonl"
393+ source .write_text ('{"reused": true}\n ' )
394+ calls = root / "calls.jsonl"
395+ shim = root / "bin"
396+ shim .mkdir ()
397+ python = shim / "python3"
398+ python .write_text (f"#!{ sys .executable } \n " + '''import json, os, sys
399+ from pathlib import Path
400+ args = sys.argv[1:]
401+ with open(os.environ["CALLS_LOG"], "a") as log:
402+ log.write(json.dumps(args) + "\\ n")
403+ if args[0] == "eval.py":
404+ Path(args[args.index("--out") + 1]).write_text('{"fresh": true}\\ n')
405+ elif args[0] == "loop_corrections.py":
406+ final = Path(args[args.index("--run-dir") + 1]) / "playbooks/final.md"
407+ final.parent.mkdir(parents=True)
408+ final.write_text("test playbook")
409+ ''' )
410+ python .chmod (0o755 )
411+ for label , baseline in (("unset" , None ), ("empty" , "" ), ("explicit" , str (source )),
412+ ("missing" , str (root / "missing.jsonl" )), ("directory" , str (root ))):
413+ with self .subTest (baseline = label ):
414+ calls .write_text ("" )
415+ env = dict (os .environ , PATH = str (shim ) + os .pathsep + os .environ ["PATH" ],
416+ BACKEND = "fake" , CALLS_LOG = str (calls ))
417+ env .pop ("BASELINE_FROM" , None )
418+ if baseline is not None :
419+ env ["BASELINE_FROM" ] = baseline
420+ proc = subprocess .run (["bash" , str (harness / "run_corrections.sh" ), label ],
421+ env = env , capture_output = True , text = True )
422+ run = harness / "runs" / label
423+ invocations = C .read_jsonl (str (calls ))
424+ if label in ("missing" , "directory" ):
425+ self .assertNotEqual (proc .returncode , 0 )
426+ self .assertIn ("BASELINE_FROM file does not exist or is not a regular file" , proc .stderr )
427+ self .assertIn (baseline , proc .stderr )
428+ self .assertFalse (run .exists ())
429+ self .assertEqual (invocations , [])
430+ continue
431+ self .assertEqual (proc .returncode , 0 , proc .stdout + proc .stderr )
432+ baseline_calls = [args for args in invocations if args [0 ] == "eval.py"
433+ and args [args .index ("--arm" ) + 1 ] == "none" ]
434+ fresh = label in ("unset" , "empty" )
435+ self .assertEqual (len (baseline_calls ), 1 if fresh else 0 )
436+ self .assertEqual ((run / "eval/none.jsonl" ).read_text (),
437+ '{"fresh": true}\n ' if fresh else source .read_text ())
438+ self .assertEqual (historical .read_text (), '{"historical": true}\n ' )
439+ self .assertEqual (source .read_text (), '{"reused": true}\n ' )
440+
332441 def test_budget_and_matrix_failures_propagate (self ):
333442 # Copy runners and stub Python/sleep: this can never launch a model or wait 30s.
334443 here = Path (__file__ ).resolve ().parent
0 commit comments