mlai-code 0.3.7 → 0.3.9

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)
@@ -726,6 +728,8 @@ _NON_CODE_CLAUSE = re.compile(r'^' + _LEAD + r'(?:'
726
728
  r'|(?:clean\s*up|tidy(?:\s+up)?)\b(?!.*\b(?:code|function|class|module|logic|component|implementation)s?\b)'
727
729
  r'|(?:delete|remove)\s+(?:the\s+|all\s+|these\s+|those\s+)*(?:untracked|temp(?:orary)?|stray|generated|build|cache[ds]?|log|backup|\.orig|\.bak)\b'
728
730
  r'|(?:run|rerun)\s+(?:the\s+|all\s+|my\s+)*(?:tests?|test\s+suite|specs?|checks?|lint(?:er)?|linting|type\s*check|build)\b'
731
+ r'|(?:run|rerun|execute)\s+(?:exactly\s+|just\s+|only\s+)?(?:this|that|these|those|the\s+following|my)\s+(?:shell\s+|terminal\s+)?commands?\b'
732
+ r'|(?:report|share|paste|give\s+me|send\s+me)\b(?!.*\b(?:fix|implement|add|change|update|rewrite|refactor)\b)'
729
733
  r'|(?:explain|describe|summari[sz]e|tell\s+me|show(?:\s+me)?|list|walk\s+me\s+through|review|look\s+at|find\s+out|investigate|check\s+(?:if|whether|why|what|how))\b'
730
734
  r'|(?:what|why|how|where|which|who|when|is|are|does|do|did|was|were|should\s+i|can\s+i|could\s+i)\b(?!\s+(?:you|we|u)\b)(?!.*\b(?:fix|implement|add|change|update|rewrite|refactor)\s+it\b)'
731
735
  r')', re.I)
@@ -735,9 +739,15 @@ _NO_TEST = re.compile(r"\b(?:do\s*n[o']?t|don'?t|no\s+need\s+to|without|skip(?:p
735
739
  def _request(task):
736
740
  return re.sub(r'\s+', ' ', str(task or '').split('User attachments (untrusted data', 1)[0]).strip()[:600]
737
741
 
742
+ # CLI r6: the command a user quotes ("run this command: npm test | tail; echo ...") is not a clause of the request.
743
+ # A quoted command runs to the next "then"/"and then" clause, so "Run this command: rm -rf build; then fix the import"
744
+ # keeps its code clause.
745
+ _QUOTED_COMMAND = re.compile(r'(\bcommands?\b[^:`]{0,80}?|\bresults?\b|\boutput\b)\s*:\s*\S.*?(?=[,.;]?\s*\b(?:and\s+)?then\b|$)', re.I | re.S)
746
+ _BACKTICKED = re.compile(r'`[^`]*`')
747
+
738
748
  def non_code_task(task):
739
749
  """True for a request that asks for a git step, a cleanup or an answer, and nothing that changes code."""
740
- text = _request(task)
750
+ text = _QUOTED_COMMAND.sub(lambda m: m.group(1), _BACKTICKED.sub('this command', _request(task))).strip()
741
751
  if not text: return False
742
752
  clauses = [c.strip(' "\'`') for c in _CLAUSE_SPLIT.split(text) if c and c.strip(' "\'`')]
743
753
  return bool(clauses) and all(_NON_CODE_CLAUSE.search(c) for c in clauses)
@@ -808,6 +818,23 @@ INLINE_ASSERT = re.compile(r'\bassert\b|console\.assert|\bexpect\(|\bassert\.\w+
808
818
  # An inline script that rewrites files is an edit, not a check, even when it asserts that its search text was found
809
819
  # (Windows bug list, item 7: python3 heredoc edits to .tsx files showed up as failed checks).
810
820
  EDIT_SCRIPT = re.compile(r'\.write_text\(|\.write_bytes\(|writeFileSync\(|open\([^)]*,\s*[\'"][wa]\+?[\'"]|\bsed\s+-i')
821
+ # Round 6 (tester report, 15-minute run): the agent verified the app with its own script (`node .e2e-tmp/e2e.mjs`,
822
+ # "21 checks passed"). That is not a test runner and has no assert in the command, so nothing was recorded and the
823
+ # monitor said "No verification checks recorded" next to a summary claiming 21 passing checks. A script run counts as a
824
+ # check when its file is named like a check (e2e, smoke, verify, test, check) or its output reports pass/fail counts.
825
+ SCRIPT_NAME = re.compile(r'(?:^|[\s/])\.?(?:e2e|smoke|verify|verification|selftest|self-test|sanity|qa|checks?|tests?|specs?)(?:[._-][\w.-]*)?(?:/|\.(?:py|[cm]?js|ts)\b)', re.I)
826
+ RESULT_SUMMARY = re.compile(r'\b\d+\s*/\s*\d+\s+(?:(?:checks?|tests?|assertions?|cases?|scenarios?)\s+)?(?:passed|passing|ok|succeeded|failed)\b'
827
+ r'|\b\d+\s+(?:checks?|tests?|assertions?|scenarios?|cases?)\s+(?:passed|failed|ok|succeeded)\b'
828
+ r'|\ball (?:\d+ )?(?:checks?|tests?|assertions?) (?:passed|ok)\b|\b[1-9]\d*\s+(?:passed|passing|failed|failing)\b', re.I)
829
+
830
+ def script_check(command, output=''):
831
+ """A run of the agent's own verification script: by its name, or by the pass/fail counts it printed."""
832
+ c = str(command or '')
833
+ if not INLINE_RUN.search(c) or EDIT_SCRIPT.search(c): return False
834
+ if SCRIPT_NAME.search(c): return True
835
+ clean = re.sub(r'\x1b\[[0-9;]*m', '', str(output or ''))[-6000:]
836
+ return bool(RESULT_SUMMARY.search(clean))
837
+
811
838
  def looks_like_check(command):
812
839
  if TEST_COMMAND.search(command): return True
813
840
  return bool(INLINE_RUN.search(command) and INLINE_ASSERT.search(command) and not EDIT_SCRIPT.search(command))
@@ -853,19 +880,34 @@ def output_failure(output):
853
880
  m = FAILURE_SUMMARY.search(clean)
854
881
  return m.group(0).strip()[:120] if m else ''
855
882
 
883
+ # `npm test 2>&1 | tail -20; echo "exit=${PIPESTATUS[0]}"` exits 0 (the echo ran last) while it prints exit=254.
884
+ _ECHOED_STATUS = re.compile(r'(?:;|&&|\|\||\n)\s*(?:echo|printf)\b[^;&|\n]*\$\{?(?:\?|PIPESTATUS)', re.I)
885
+
856
886
  def check_exit(command, code, output, is_check):
857
887
  """Reconcile the shell exit status with what the runner printed.
858
888
  Returns (returncode, note). returncode None means the result is inconclusive."""
859
889
  if code: return code, ''
890
+ # Round 6 (tester report): the row said "Check passed" for exit=254 because only the echo's 0 was looked at.
891
+ from runtime_control import masked_exit
892
+ printed = masked_exit(command, output)
893
+ if printed: return printed, 'The command printed exit=%d for the runner although the shell exited 0 (a later echo reported it)' % printed
860
894
  failure = output_failure(output)
861
895
  if failure: return 1, 'Runner output reports failures (' + failure + ') although the command exited 0'
862
896
  if masked_pipe(command, is_check):
863
897
  return None, 'Exit status came from a later pipe stage or command, not the test runner. Rerun with set -o pipefail and without || or ; after the runner'
898
+ # The status was echoed, but no exit=N for it can be read back from the output: not a pass on the echo's 0 alone.
899
+ if _ECHOED_STATUS.search(str(command or '')) and not re.search(r'\bexit\s+\$\{?(?:\?|PIPESTATUS)', str(command or '')) and not _printed_zero(command, output):
900
+ return None, 'The command echoed an exit status instead of exiting with it, and no exit=N could be read from the output. Rerun the runner on its own (set -o pipefail if you pipe it)'
864
901
  return 0, ''
865
902
 
903
+ def _printed_zero(command, output):
904
+ from runtime_control import printed_exit
905
+ return printed_exit(command, output) == 0
906
+
866
907
 
867
908
  def unverified_banner(report):
868
909
  """Short header put above a final answer whose checks did not hold up."""
910
+ given = isinstance(report, dict) and bool(report)
869
911
  report = report or {}
870
912
  parts = []
871
913
  for key, label in (('checksFailed', 'failed'), ('checksStale', 'ran before the last code change'), ('checksNotRun', 'did not run'), ('checksInconclusive', 'were inconclusive')):
@@ -873,6 +915,9 @@ def unverified_banner(report):
873
915
  if n: parts.append('%d %s' % (n, label))
874
916
  unfinished = len(report.get('unfinishedTasks') or [])
875
917
  if unfinished: parts.append('%d planned step%s unfinished' % (unfinished, '' if unfinished == 1 else 's'))
918
+ # Round 6: the reply claimed "all 21 checks passed" while this turn recorded none; say so in the banner itself.
919
+ if given and not int(report.get('checksPassed') or 0) and not any(int(report.get(k) or 0) for k in ('checksFailed', 'checksStale', 'checksNotRun', 'checksInconclusive')):
920
+ parts.insert(0, 'no test, build or check was recorded in this turn')
876
921
  detail = ('; '.join(parts) + '. ') if parts else ''
877
922
  return ('> **Not verified.** The final checks did not confirm this work (' + detail.rstrip('. ') + ').' if parts else '> **Not verified.** The final checks did not confirm this work.') + \
878
923
  ' Any "verified" or "working" claims below are unconfirmed until the checks are rerun and pass.\n\n'
@@ -936,3 +981,67 @@ def program_run_check(command, repo):
936
981
  if target.is_file() and Path(repo).resolve() in target.parents: return m.group(1)
937
982
  except OSError: pass
938
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()
package/worker/winproc.py CHANGED
@@ -133,3 +133,26 @@ def git_bash(env=None):
133
133
  if not found or 'system32' in found.lower() or 'windowsapps' in found.lower():
134
134
  raise FileNotFoundError('Git Bash was not found. Install Git for Windows (https://git-scm.com/download/win).')
135
135
  return found
136
+
137
+
138
+ def lock_file(f, blocking=True):
139
+ """Exclusive lock on an open file, released when the file is closed (fcntl.flock on macOS and Linux).
140
+
141
+ Windows has no fcntl (the browser check crashed with "No module named 'fcntl'"), so there the first byte is locked
142
+ with msvcrt. A non-blocking attempt raises OSError when another process holds the lock, like LOCK_NB."""
143
+ try:
144
+ import fcntl
145
+ except ImportError:
146
+ fcntl = None
147
+ if fcntl is not None:
148
+ fcntl.flock(f, fcntl.LOCK_EX | (0 if blocking else fcntl.LOCK_NB))
149
+ return
150
+ import msvcrt, time
151
+ f.seek(0)
152
+ while True:
153
+ try:
154
+ msvcrt.locking(f.fileno(), msvcrt.LK_NBLCK, 1)
155
+ return
156
+ except OSError:
157
+ if not blocking: raise
158
+ time.sleep(0.05)