mlai-code 0.3.8 → 0.3.10

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.
@@ -9,7 +9,7 @@ pre_existing(repo, base, command) -> True when the same command also fails on t
9
9
  Findings are dicts {'kind', 'message', 'severity': 'block'|'note'}; the hook and bridge turn `block` findings into
10
10
  repair rounds and every finding into a `verification_report` event for the activity feed.
11
11
  """
12
- import os, re, subprocess, tempfile, shutil
12
+ import os, re, signal, subprocess, sys, tempfile, time, shutil
13
13
  from pathlib import Path
14
14
  from runtime_control import latest_checks, check_status, clip, EXTERNAL_SCOPES
15
15
 
@@ -255,8 +255,10 @@ def pre_existing(repo, base, command, timeout=300):
255
255
  try: os.symlink(src, Path(tmp) / link)
256
256
  except OSError: pass
257
257
  env = {k: v for k, v in os.environ.items() if k not in ('MODEL_API_KEY', 'MINI_KIMI_PROXY_TOKEN', 'VERCEL_OIDC_TOKEN')}
258
- run = subprocess.run(['bash', '-lc', command], cwd=tmp, capture_output=True, text=True, timeout=timeout, env=env)
259
- return run.returncode != 0
258
+ code, _out, why = bounded_run( # bounded on Windows too: a leftover child holding the output pipe cannot block it
259
+ ['bash', '-lc', command], cwd=tmp, env=env, timeout=timeout)
260
+ if why: return None
261
+ return code != 0
260
262
  except (OSError, subprocess.SubprocessError): return None
261
263
  finally:
262
264
  subprocess.run(['git', 'worktree', 'remove', '--force', tmp], cwd=repo, capture_output=True, timeout=60)
@@ -979,3 +981,67 @@ def program_run_check(command, repo):
979
981
  if target.is_file() and Path(repo).resolve() in target.parents: return m.group(1)
980
982
  except OSError: pass
981
983
  return ''
984
+
985
+
986
+ # Bounded shell commands for checks the harness replays on its own (CLI r7).
987
+ # Bounded shell commands for the harness (checks it replays on its own).
988
+ #
989
+ # subprocess.run(..., capture_output=True, timeout=N) is not bounded on Windows: after the timeout it kills only the shell
990
+ # and then reads the pipes with no timeout, so a child that is still alive (a headless browser left by an e2e script) keeps
991
+ # the pipe open and the harness waits forever. One tester's run sat at "Working" for 45 minutes after its final answer.
992
+ #
993
+ # run() sends output to a temporary file instead of a pipe, starts the command in its own process group, and stops the
994
+ # whole tree on timeout or when stop() returns True. It always returns within about the timeout.
995
+ WINDOWS = os.name == 'nt'
996
+
997
+
998
+ def _kill_tree(proc):
999
+ """Stops a started command and everything it launched. Never raises."""
1000
+ try:
1001
+ if proc.poll() is not None and not WINDOWS:
1002
+ # The shell is gone; its children may still be in the group.
1003
+ os.killpg(proc.pid, signal.SIGKILL); return
1004
+ except (OSError, ProcessLookupError): return
1005
+ try:
1006
+ if WINDOWS:
1007
+ import winproc
1008
+ winproc.kill_tree(proc.pid)
1009
+ else:
1010
+ os.killpg(proc.pid, signal.SIGKILL)
1011
+ except (OSError, ProcessLookupError, subprocess.SubprocessError):
1012
+ try: proc.kill()
1013
+ except OSError: pass
1014
+
1015
+
1016
+ def bounded_run(argv, cwd=None, env=None, timeout=150, stop=None, tail=4000, poll=0.25):
1017
+ """Returns (returncode, output, reason). returncode is None when the command was stopped; reason is '' when it
1018
+ finished, 'timeout' or 'stopped' otherwise. output is the last `tail` characters of stdout and stderr together."""
1019
+ out = tempfile.TemporaryFile()
1020
+ kwargs = {'cwd': cwd, 'env': env, 'stdin': subprocess.DEVNULL, 'stdout': out, 'stderr': subprocess.STDOUT}
1021
+ if WINDOWS: kwargs['creationflags'] = getattr(subprocess, 'CREATE_NEW_PROCESS_GROUP', 0) | getattr(subprocess, 'CREATE_NO_WINDOW', 0)
1022
+ else: kwargs['start_new_session'] = True
1023
+ try:
1024
+ try: proc = subprocess.Popen(argv, **kwargs)
1025
+ except OSError as exc: return None, str(exc)[-tail:], 'error'
1026
+ deadline = time.time() + max(1, timeout)
1027
+ reason = ''
1028
+ while proc.poll() is None:
1029
+ if stop and stop(): reason = 'stopped'; break
1030
+ if time.time() >= deadline: reason = 'timeout'; break
1031
+ time.sleep(poll)
1032
+ code = proc.returncode
1033
+ if reason:
1034
+ _kill_tree(proc)
1035
+ try: proc.wait(timeout=10)
1036
+ except subprocess.TimeoutExpired: pass
1037
+ code = None
1038
+ elif not WINDOWS:
1039
+ # A background child left by the command must not outlive the check.
1040
+ try: os.killpg(proc.pid, signal.SIGKILL)
1041
+ except (OSError, ProcessLookupError): pass
1042
+ out.seek(0)
1043
+ text = out.read()[-tail * 4:].decode('utf-8', 'replace')[-tail:]
1044
+ if reason == 'timeout': text = (text + '\n' if text else '') + 'Stopped after %ds (timeout).' % timeout
1045
+ return code, text, reason
1046
+ finally:
1047
+ out.close()