3030 assert observed == CONFIG [key ], f"source revision drift for { arm } "
3131
3232
33- def environment (arm ):
33+ def environment (arm , namespace = None ):
3434 src = SOURCES [arm ]
35+ runtime_tmp = TMP / (namespace or arm )
36+ runtime_tmp .mkdir (exist_ok = True )
3537 return {
3638 ** os .environ ,
3739 "PATH" : str (CONFIG ["node_bin" ])
@@ -47,9 +49,9 @@ def environment(arm):
4749 + ":"
4850 + str (src ),
4951 "PYTHONHASHSEED" : "0" ,
50- "TMPDIR" : str (TMP ),
51- "TEMP" : str (TMP ),
52- "TMP" : str (TMP ),
52+ "TMPDIR" : str (runtime_tmp ),
53+ "TEMP" : str (runtime_tmp ),
54+ "TMP" : str (runtime_tmp ),
5355 "NODE_OPTIONS" : "--experimental-sqlite --import "
5456 + (ROOT / "clock-hooks/clock.mjs" ).as_uri (),
5557 "LOOPX_USAGE_PING" : "0" ,
@@ -105,28 +107,33 @@ def command(manifest, arm):
105107 ]
106108
107109
108- def run (manifest , arm , mode ):
110+ def stop_runtime (arm , namespace = None ):
111+ stopped = subprocess .run (
112+ [
113+ str (SOURCES [arm ] / ".venv/bin/python" ),
114+ "-c" ,
115+ "import json;from loopx.control_plane.effect_runtime import restart_effect_runtime; print(json.dumps(restart_effect_runtime(timeout=5,wait_for_exit=True)))" ,
116+ ],
117+ capture_output = True ,
118+ text = True ,
119+ env = environment (arm , namespace ),
120+ timeout = 20 ,
121+ )
122+ assert stopped .returncode == 0 , stopped .stderr
123+ assert json .loads (stopped .stdout )["status" ] in ("stopped" , "not_running" ), (
124+ stopped .stdout
125+ )
126+
127+
128+ def run (manifest , arm , mode , namespace = None ):
109129 if mode == "cold-daemon" :
110- stop = subprocess .run (
111- [
112- str (SOURCES [arm ] / ".venv/bin/python" ),
113- "-c" ,
114- "import json;from loopx.control_plane.effect_runtime import restart_effect_runtime; print(json.dumps(restart_effect_runtime(timeout=5,wait_for_exit=True)))" ,
115- ],
116- capture_output = True ,
117- text = True ,
118- env = environment (arm ),
119- timeout = 20 ,
120- )
121- assert stop .returncode == 0 , stop .stderr
122- assert json .loads (stop .stdout )["status" ] in ("stopped" , "not_running" ), (
123- stop .stdout
124- )
130+ stop_runtime (arm , namespace )
131+ env = environment (arm , namespace )
125132 started = time .perf_counter_ns ()
126133 p = subprocess .run (
127134 command (manifest , arm ),
128135 cwd = manifest ["project" ],
129- env = environment ( arm ) ,
136+ env = env ,
130137 capture_output = True ,
131138 timeout = 120 ,
132139 )
@@ -139,8 +146,18 @@ def run(manifest, arm, mode):
139146 == "todo_tail_owned"
140147 )
141148 assert payload ["agent_todo_summary" ]["done_count" ] == manifest ["history" ]
149+ runtime_infos = [
150+ json .loads (path .read_text ())
151+ for path in (TMP / (namespace or arm )).glob ("*/runtime-*.json" )
152+ ]
153+ runtime_pids = {
154+ info ["pid" ] for info in runtime_infos if isinstance (info .get ("pid" ), int )
155+ }
156+ assert len (runtime_pids ) == 1 , runtime_pids
142157 return {
143158 "arm" : arm ,
159+ "runtime_namespace" : namespace or arm ,
160+ "runtime_pid" : runtime_pids .pop (),
144161 "ms" : elapsed ,
145162 "stdout_sha256" : hashlib .sha256 (p .stdout ).hexdigest (),
146163 }, p .stdout
@@ -187,43 +204,36 @@ def benchmark():
187204 case = path .parent .name
188205 for mode in CONFIG ["modes" ]:
189206 for arm in ["base" , "head" ]:
190- run ( manifest , arm , "warm" )
207+ stop_runtime ( arm )
191208 for phase , n in [("aa" , CONFIG ["aa_pairs" ]), ("ab" , CONFIG ["ab_pairs" ])]:
192- # Reset process history between controls and treatment, then warm both equally.
193- for arm in ["base" , "head" ]:
194- stopped = subprocess .run (
195- [
196- str (SOURCES [arm ] / ".venv/bin/python" ),
197- "-c" ,
198- "import json;from loopx.control_plane.effect_runtime import restart_effect_runtime; print(json.dumps(restart_effect_runtime(timeout=5,wait_for_exit=True)))" ,
199- ],
200- capture_output = True ,
201- text = True ,
202- env = environment (arm ),
203- timeout = 20 ,
204- )
205- assert stopped .returncode == 0 , stopped .stderr
206- assert json .loads (stopped .stdout )["status" ] in (
207- "stopped" ,
208- "not_running" ,
209- ), stopped .stdout
209+ targets = {
210+ label : ("base" if phase == "aa" else label , f"{ phase } -{ label } " )
211+ for label in ["base" , "head" ]
212+ }
213+ for arm , namespace in targets .values ():
214+ stop_runtime (arm , namespace )
210215 for warmup in range (CONFIG .get ("warmups_per_arm_per_phase" , 3 )):
211- for arm in [ "base" , "head" ] :
212- run (manifest , arm , "warm" )
216+ for arm , namespace in targets . values () :
217+ run (manifest , arm , "warm" , namespace )
213218 collected = {"base" : [], "head" : []}
214219 for pair in range (n ):
215220 order = ["base" , "head" ] if pair % 2 == 0 else ["head" , "base" ]
216221 outputs = []
222+ runtime_pids = set ()
217223 for label in order :
218- arm = "base" if phase == "aa" else label
219- row , out = run (manifest , arm , mode )
224+ arm , namespace = targets [label ]
225+ row , out = run (manifest , arm , mode , namespace )
226+ runtime_pids .add (row ["runtime_pid" ])
220227 outputs .append (out )
221228 row .update (
222229 case = case , mode = mode , phase = phase , pair = pair , label = label
223230 )
224231 with ledger .open ("a" ) as file :
225232 file .write (json .dumps (row ) + "\n " )
226233 collected [label ].append (row ["ms" ])
234+ assert len (runtime_pids ) == 2 , (
235+ "controls and treatment require separate daemons"
236+ )
227237 assert outputs [0 ] == outputs [1 ], (
228238 f"raw output changed at { case } /{ mode } /{ phase } /{ pair } "
229239 )
@@ -236,6 +246,8 @@ def benchmark():
236246 for arm , xs in collected .items ()
237247 }
238248 print ("PERFORMANCE" , case , mode , phase , json .dumps (stats ), flush = True )
249+ for arm , namespace in targets .values ():
250+ stop_runtime (arm , namespace )
239251
240252
241253if __name__ == "__main__" :
0 commit comments