mlai-code 0.3.6 → 0.3.8

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.
@@ -74,6 +74,25 @@ def claims(text):
74
74
  return found
75
75
 
76
76
 
77
+ FAIL_WORD = re.compile(r"\b(?:fail(?:s|ed|ing|ure|ures)?|still (?:red|broken)|broken|not (?:fixed|passing)|does(?: not|n'?t) pass|errors? out)\b", re.I)
78
+ _FAILED_NAMES = re.compile(r"(?:^FAILED\s+\S*?::|^\s*[\u2716\u00d7\u2717]\s+|^not ok \d+ - |^\s*\u25cf\s+)([\w .:/-]{3,80}?)(?:\s+\(|\s+-\s|\s*$)", re.M)
79
+
80
+ def admits_failure(text, check):
81
+ """True when a sentence of the final message names this failing check (its test file, a failing test, or the runner)
82
+ together with a failure word."""
83
+ command = str(check.get('command') or check.get('label') or '')
84
+ names = set(re.findall(r'[\w./-]*\w\.(?:py|[cm]?[jt]sx?)\b|\btest_\w+', command))
85
+ names |= {n.strip().split('::')[-1] for n in _FAILED_NAMES.findall(str(check.get('output') or '')) if len(n.strip()) >= 3}
86
+ m = re.search(r'\b(pytest|npm (?:run )?test|node --test|vitest|jest|unittest|go test|cargo test)\b', command)
87
+ if m: names.add(m.group(1))
88
+ names = {n.lower() for n in names if n and n.lower() not in ('test.py', 'test.js')}
89
+ if not names: return False
90
+ for sentence in re.split(r'(?<=[.!?])\s+|\n+', str(text or '')):
91
+ low = sentence.lower()
92
+ if FAIL_WORD.search(sentence) and any(n in low for n in names): return True
93
+ return False
94
+
95
+
77
96
  def claim_check(final_text, commands, checks=None):
78
97
  """Findings when the final message claims verification that no command of this turn performed."""
79
98
  found = claims(final_text)
@@ -88,6 +107,9 @@ def claim_check(final_text, commands, checks=None):
88
107
  if re.search(r'lint|ruff|eslint|flake8|biome', command): kinds.add('lint')
89
108
  return not kinds or any(f['kind'] in kinds or f['kind'] == 'verified' or re.search(r'everything|all checks|checks (pass|are)', f['text'], re.I) for f in found)
90
109
  failed = [c for c in current if (c.get('scope') or 'repository') == 'repository' and check_status(c) == 'failed' and contradicts(c)]
110
+ # "My checks pass; `pytest test_strutil.py` still fails its test_rev" is an honest partial report: each failing check
111
+ # is named next to a failure word somewhere in the message (CLI r5: it was sent back twice and ended Not verified).
112
+ if failed and all(admits_failure(final_text, c) for c in failed): failed = []
91
113
  if failed:
92
114
  return [{'kind': 'contradicted_claim', 'severity': 'block',
93
115
  'message': 'The final message claims successful verification, but a current repository check failed: ' + clip(failed[0].get('command') or failed[0].get('label') or 'unnamed check', 240) + '. Fix and rerun it, or report the failure honestly.'}]
@@ -691,6 +713,50 @@ def notice(report):
691
713
  # in the harness's own words instead of the run being held open. A citation that does not resolve is a fact, so it stays blocking.
692
714
  SOFT = frozenset(('uncited_behaviour', 'unacknowledged_change', 'contradicted_denial', 'remote_pending_claim'))
693
715
 
716
+ # ---------------------------------------------------------------- what kind of task this is (CLI r5, coding)
717
+ # A commit, a cleanup or a question is not a code change. Holding it to the code-change gates ("code changed after the
718
+ # last passing check", "verification still fails", "edits were undone", "open error but says done") sent "Commit changes"
719
+ # into repair rounds and ended it as Not verified when the user's own tests were red. Each clause of the request must
720
+ # start with one of these; anything else (fix, add, rename, refactor, write tests...) is code work.
721
+ _LEAD = r'(?:(?:please|pls|ok(?:ay)?|now|then|also|and|just|can you|could you|would you|will you|i want you to|go ahead and|let\'?s)\s+)*'
722
+ _NON_CODE_CLAUSE = re.compile(r'^' + _LEAD + r'(?:'
723
+ r'(?:git\s+)?(?:commit|stage|unstage|stash|amend|squash|tag|checkout|switch(?:\s+to)?|rebase|cherry[- ]pick|push|pull|fetch)\b'
724
+ r'|(?:create|make|open|start|delete|remove|rename)\s+(?:a\s+|the\s+|new\s+|this\s+|that\s+|my\s+)*(?:local\s+)?(?:git\s+)?branch\b'
725
+ r'|(?:revert|undo)\s+(?:the\s+|my\s+|that\s+|this\s+|your\s+)?(?:last\s+|previous\s+|latest\s+)?(?:commit|change|changes|edit|edits)\b'
726
+ r'|(?:clean\s*up|tidy(?:\s+up)?)\b(?!.*\b(?:code|function|class|module|logic|component|implementation)s?\b)'
727
+ 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
+ r'|(?:run|rerun)\s+(?:the\s+|all\s+|my\s+)*(?:tests?|test\s+suite|specs?|checks?|lint(?:er)?|linting|type\s*check|build)\b'
729
+ 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'
730
+ r'|(?:report|share|paste|give\s+me|send\s+me)\b(?!.*\b(?:fix|implement|add|change|update|rewrite|refactor)\b)'
731
+ 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'
732
+ 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)'
733
+ r')', re.I)
734
+ _CLAUSE_SPLIT = re.compile(r'\s*(?:[.;!?\n]+(?=\s|$)|,\s*(?:and\s+|then\s+)?|\band then\b|\bthen\b|\band\b(?=\s+(?:please\s+)?[a-z]+\b))\s*', re.I)
735
+ _NO_TEST = re.compile(r"\b(?:do\s*n[o']?t|don'?t|no\s+need\s+to|without|skip(?:ping)?|never)\s+(?:run(?:ning)?\s+|bother\s+(?:running\s+)?)?(?:any\s+|the\s+)?(?:tests?|test\s+suite|checks?|commands?)\b|\bi(?:'ll| will)\s+test\b|\bno\s+tests?\b(?!\s+(?:for|of|exist|file))", re.I)
736
+
737
+ def _request(task):
738
+ return re.sub(r'\s+', ' ', str(task or '').split('User attachments (untrusted data', 1)[0]).strip()[:600]
739
+
740
+ # CLI r6: the command a user quotes ("run this command: npm test | tail; echo ...") is not a clause of the request.
741
+ # A quoted command runs to the next "then"/"and then" clause, so "Run this command: rm -rf build; then fix the import"
742
+ # keeps its code clause.
743
+ _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)
744
+ _BACKTICKED = re.compile(r'`[^`]*`')
745
+
746
+ def non_code_task(task):
747
+ """True for a request that asks for a git step, a cleanup or an answer, and nothing that changes code."""
748
+ text = _QUOTED_COMMAND.sub(lambda m: m.group(1), _BACKTICKED.sub('this command', _request(task))).strip()
749
+ if not text: return False
750
+ clauses = [c.strip(' "\'`') for c in _CLAUSE_SPLIT.split(text) if c and c.strip(' "\'`')]
751
+ return bool(clauses) and all(_NON_CODE_CLAUSE.search(c) for c in clauses)
752
+
753
+ def no_tests_requested(task):
754
+ """The user said not to run tests or commands (\"I will test it myself\"). The work is then done, not tested, by request."""
755
+ return bool(_NO_TEST.search(_request(task)))
756
+
757
+ # Findings that only make sense for a code change. A non-code task keeps them as notes.
758
+ CODE_CHANGE_KINDS = frozenset(('empty_change', 'open_error_claim', 'zero_tests', 'dead_link'))
759
+
694
760
  def report(repo, base, final_text, commands, checks, task='', last_edit=None, edits=0, scope=None, record=None, earlier=None, asked=(), root=None):
695
761
  """Everything the gates need in one dict: findings, verdict, and the texts to show. With root (the run folder), the
696
762
  diff checks look only at what this turn changed, not at files that were already changed or untracked at its start."""
@@ -733,6 +799,7 @@ def report(repo, base, final_text, commands, checks, task='', last_edit=None, ed
733
799
  try: findings += check()
734
800
  except Exception: pass
735
801
  findings = [({**f, 'severity': 'note'} if f.get('kind') in SOFT and f.get('kind') in (asked or ()) else f) for f in findings]
802
+ if non_code_task(task): findings = [({**f, 'severity': 'note'} if f.get('kind') in CODE_CHANGE_KINDS else f) for f in findings]
736
803
  v = verdict(fresh, findings)
737
804
  if stale and v['status'] == 'passed' and not v['passed']: v['status'] = 'inconclusive'
738
805
  if stale: v['reasons'] = v['reasons'] + ['%d passing check%s ran before the last code edit' % (len(stale), '' if len(stale) == 1 else 's')]
@@ -749,6 +816,23 @@ INLINE_ASSERT = re.compile(r'\bassert\b|console\.assert|\bexpect\(|\bassert\.\w+
749
816
  # An inline script that rewrites files is an edit, not a check, even when it asserts that its search text was found
750
817
  # (Windows bug list, item 7: python3 heredoc edits to .tsx files showed up as failed checks).
751
818
  EDIT_SCRIPT = re.compile(r'\.write_text\(|\.write_bytes\(|writeFileSync\(|open\([^)]*,\s*[\'"][wa]\+?[\'"]|\bsed\s+-i')
819
+ # Round 6 (tester report, 15-minute run): the agent verified the app with its own script (`node .e2e-tmp/e2e.mjs`,
820
+ # "21 checks passed"). That is not a test runner and has no assert in the command, so nothing was recorded and the
821
+ # monitor said "No verification checks recorded" next to a summary claiming 21 passing checks. A script run counts as a
822
+ # check when its file is named like a check (e2e, smoke, verify, test, check) or its output reports pass/fail counts.
823
+ 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)
824
+ RESULT_SUMMARY = re.compile(r'\b\d+\s*/\s*\d+\s+(?:(?:checks?|tests?|assertions?|cases?|scenarios?)\s+)?(?:passed|passing|ok|succeeded|failed)\b'
825
+ r'|\b\d+\s+(?:checks?|tests?|assertions?|scenarios?|cases?)\s+(?:passed|failed|ok|succeeded)\b'
826
+ r'|\ball (?:\d+ )?(?:checks?|tests?|assertions?) (?:passed|ok)\b|\b[1-9]\d*\s+(?:passed|passing|failed|failing)\b', re.I)
827
+
828
+ def script_check(command, output=''):
829
+ """A run of the agent's own verification script: by its name, or by the pass/fail counts it printed."""
830
+ c = str(command or '')
831
+ if not INLINE_RUN.search(c) or EDIT_SCRIPT.search(c): return False
832
+ if SCRIPT_NAME.search(c): return True
833
+ clean = re.sub(r'\x1b\[[0-9;]*m', '', str(output or ''))[-6000:]
834
+ return bool(RESULT_SUMMARY.search(clean))
835
+
752
836
  def looks_like_check(command):
753
837
  if TEST_COMMAND.search(command): return True
754
838
  return bool(INLINE_RUN.search(command) and INLINE_ASSERT.search(command) and not EDIT_SCRIPT.search(command))
@@ -794,19 +878,34 @@ def output_failure(output):
794
878
  m = FAILURE_SUMMARY.search(clean)
795
879
  return m.group(0).strip()[:120] if m else ''
796
880
 
881
+ # `npm test 2>&1 | tail -20; echo "exit=${PIPESTATUS[0]}"` exits 0 (the echo ran last) while it prints exit=254.
882
+ _ECHOED_STATUS = re.compile(r'(?:;|&&|\|\||\n)\s*(?:echo|printf)\b[^;&|\n]*\$\{?(?:\?|PIPESTATUS)', re.I)
883
+
797
884
  def check_exit(command, code, output, is_check):
798
885
  """Reconcile the shell exit status with what the runner printed.
799
886
  Returns (returncode, note). returncode None means the result is inconclusive."""
800
887
  if code: return code, ''
888
+ # Round 6 (tester report): the row said "Check passed" for exit=254 because only the echo's 0 was looked at.
889
+ from runtime_control import masked_exit
890
+ printed = masked_exit(command, output)
891
+ if printed: return printed, 'The command printed exit=%d for the runner although the shell exited 0 (a later echo reported it)' % printed
801
892
  failure = output_failure(output)
802
893
  if failure: return 1, 'Runner output reports failures (' + failure + ') although the command exited 0'
803
894
  if masked_pipe(command, is_check):
804
895
  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'
896
+ # 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.
897
+ if _ECHOED_STATUS.search(str(command or '')) and not re.search(r'\bexit\s+\$\{?(?:\?|PIPESTATUS)', str(command or '')) and not _printed_zero(command, output):
898
+ 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)'
805
899
  return 0, ''
806
900
 
901
+ def _printed_zero(command, output):
902
+ from runtime_control import printed_exit
903
+ return printed_exit(command, output) == 0
904
+
807
905
 
808
906
  def unverified_banner(report):
809
907
  """Short header put above a final answer whose checks did not hold up."""
908
+ given = isinstance(report, dict) and bool(report)
810
909
  report = report or {}
811
910
  parts = []
812
911
  for key, label in (('checksFailed', 'failed'), ('checksStale', 'ran before the last code change'), ('checksNotRun', 'did not run'), ('checksInconclusive', 'were inconclusive')):
@@ -814,6 +913,9 @@ def unverified_banner(report):
814
913
  if n: parts.append('%d %s' % (n, label))
815
914
  unfinished = len(report.get('unfinishedTasks') or [])
816
915
  if unfinished: parts.append('%d planned step%s unfinished' % (unfinished, '' if unfinished == 1 else 's'))
916
+ # Round 6: the reply claimed "all 21 checks passed" while this turn recorded none; say so in the banner itself.
917
+ 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')):
918
+ parts.insert(0, 'no test, build or check was recorded in this turn')
817
919
  detail = ('; '.join(parts) + '. ') if parts else ''
818
920
  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.') + \
819
921
  ' Any "verified" or "working" claims below are unconfirmed until the checks are rerun and pass.\n\n'
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)