mlai-code 0.3.9 → 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.
@@ -5,7 +5,7 @@ block message when it denies. Denied actions are recorded in ROOT/auto-review/de
5
5
  the fingerprint to approved.json and the next identical action runs once without review (the grant is consumed).
6
6
  Settings come from request.json extensions.autoReview: {enabled: bool, model: str}. Off unless enabled is true.
7
7
  """
8
- import hashlib, json, os, re, time, urllib.request
8
+ import hashlib, json, os, re, time, urllib.error, urllib.request
9
9
  from pathlib import Path
10
10
 
11
11
  DIR = 'auto-review'
@@ -20,6 +20,7 @@ COMMANDS = [
20
20
  (r'\bgit\s+clean\s+-\w*f', 'deletes untracked files'),
21
21
  (r'\bgit\s+(checkout|restore)\s+(--\s+)?\.(\s|$)', 'discards uncommitted changes'),
22
22
  (r'\bgit\s+(branch\s+-D|stash\s+(drop|clear)|rebase|filter-branch|update-ref\s+-d)\b', 'rewrites or drops git history'),
23
+ (r'\bgit\s+(?-i:branch\s+(.*\s)?(-[a-zA-Z]*[MfC][a-zA-Z]*|--force)\b|checkout\s+(.*\s)?-B\b|switch\s+(.*\s)?(-C|--force-create)\b|update-ref\b)', 'overwrites or moves a branch'), # BUG-0713
23
24
  (r'\bsudo\b|\bdoas\b', 'runs as administrator'),
24
25
  (r'\b(curl|wget)\b[^|;&]*\|\s*(sudo\s+)?(ba|z|da)?sh\b', 'pipes a download into a shell'),
25
26
  (r'\b(curl|wget|http)\b.*\s(-X\s*(POST|PUT|PATCH|DELETE)|--data|-d\s|-F\s|--upload-file|-T\s)', 'sends data to a server'),
@@ -109,24 +110,44 @@ def _parse(text):
109
110
  return None, 'The reviewer gave no clear decision.'
110
111
 
111
112
 
113
+ def engine_proxy(root):
114
+ """(base_url, token) of the main engine's per-run model proxy, read from its private config (ROOT/kimi/config.toml)."""
115
+ try: text = (Path(root) / 'kimi' / 'config.toml').read_text()[:200000]
116
+ except OSError: return None
117
+ block = text.split('[providers.mlai]', 1)
118
+ if len(block) < 2: return None
119
+ body = block[1].split('\n[', 1)[0]
120
+ url = re.search(r'^base_url\s*=\s*"(http://127\.0\.0\.1:\d+/v1)"', body, re.M); token = re.search(r'^api_key\s*=\s*"([A-Za-z0-9_-]{16,200})"', body, re.M)
121
+ return (url.group(1), token.group(1)) if url and token else None
122
+
123
+
112
124
  def ask_model(action, why, task, root, model=None, opener=None):
113
125
  """One reviewer call through the run's endpoint and token. Returns (decision, reason); decision None on failure."""
114
126
  req = _read(Path(root) / 'request.json', {})
115
127
  endpoint = str(req.get('endpoint') or '').rstrip('/'); key = os.environ.get('MODEL_API_KEY', '')
116
- if not endpoint: return None, 'No model endpoint for the reviewer.'
117
128
  model = model or req.get('model') or 'mlai-code'; provider = req.get('provider')
129
+ if not key:
130
+ # BUG-0715: the main engine's hooks never get the model key (BUG-0517 keeps it out of every command's
131
+ # environment), so the reviewer always failed with HTTP 401. It goes through the run's own model proxy instead.
132
+ local = engine_proxy(root)
133
+ if local:
134
+ endpoint, key, provider = local[0], local[1], 'proxy'
135
+ opener = opener or urllib.request.build_opener(urllib.request.ProxyHandler({})).open # loopback: never via HTTP_PROXY
136
+ if not endpoint: return None, 'No model endpoint for the reviewer.'
118
137
  user = 'Task: ' + str(task or req.get('task') or '')[:3000] + '\n\nAction: ' + action + '\nWhy it was flagged: ' + why
119
138
  if provider == 'anthropic':
120
139
  payload = {'model': model, 'max_tokens': 200, 'system': PROMPT, 'messages': [{'role': 'user', 'content': user}]}
121
140
  headers = {'x-api-key': key, 'anthropic-version': '2023-06-01', 'Content-Type': 'application/json'}; suffix = '/messages'
122
141
  else:
123
142
  payload = {'model': model, 'messages': [{'role': 'system', 'content': PROMPT}, {'role': 'user', 'content': user}], 'temperature': 0}
124
- payload['max_completion_tokens' if provider == 'openai' else 'max_tokens'] = 200
143
+ # Through the proxy the run's model may reason first: room for that, so the JSON answer is not cut off.
144
+ payload['max_completion_tokens' if provider == 'openai' else 'max_tokens'] = 1000 if provider == 'proxy' else 200
125
145
  headers = {'Authorization': 'Bearer ' + key, 'Content-Type': 'application/json'}; suffix = '/chat/completions'
126
146
  try:
127
147
  r = (opener or urllib.request.urlopen)(urllib.request.Request(endpoint + suffix, data=json.dumps(payload).encode(), headers=headers), timeout=TIMEOUT)
128
148
  body = json.loads(r.read().decode())
129
- except Exception as exc: return None, 'The reviewer could not be reached (' + type(exc).__name__ + ').'
149
+ except urllib.error.HTTPError as exc: return None, 'The reviewer could not be reached (the review service answered HTTP ' + str(exc.code) + '), so the action was not run.'
150
+ except Exception as exc: return None, 'The reviewer could not be reached (' + type(exc).__name__ + '; the review service is unavailable), so the action was not run.'
130
151
  if provider == 'anthropic': text = ''.join(b.get('text', '') for b in body.get('content') or [] if isinstance(b, dict))
131
152
  else: text = ((body.get('choices') or [{}])[0].get('message') or {}).get('content') or ''
132
153
  return _parse(text)
@@ -213,21 +213,50 @@ def _under(full, roots):
213
213
  return any(full == r or full.startswith(r.rstrip(os.sep) + os.sep) for r in roots)
214
214
 
215
215
 
216
+ _LISTS_CWD = frozenset(('ls', 'tree', 'find', 'du', 'fd', 'rg', 'ag', 'ack')) # read the current folder when given no path
217
+
218
+
216
219
  def _command_read_paths(command):
217
- """Path-looking arguments of read programs in a shell command (absolute, ~, $HOME or ../ paths only)."""
220
+ """Path-looking arguments of read programs in a shell command (absolute, ~, $HOME or ../ paths, and any path after a
221
+ cd to such a folder). BUG-0656: `cd ~/other && cat notes.txt` and `git -C ~/other show HEAD:notes.txt` read outside
222
+ the project too."""
218
223
  out = []
219
224
  try:
220
225
  import command_safety as cs
226
+ cwd = '' # '' is the project folder; otherwise the folder earlier cd/pushd statements moved to
227
+ def at(w):
228
+ w = w.replace('$HOME', '~').replace('${HOME}', '~')
229
+ if w.startswith(('/', '~')): return w
230
+ return os.path.join(cwd, w) if cwd else w
231
+ def outside_like(w): return w.startswith(('/', '~', '../')) or w == '..'
221
232
  for st in cs.split_statements(str(command or ''))[:40]:
222
233
  try: words, redirects = cs._words(st); words, _f = cs._strip_wrappers(words)
223
234
  except Exception: continue
235
+ prog = words[0].rsplit('/', 1)[-1] if words else ''
236
+ if prog in ('cd', 'pushd'):
237
+ args = [w for w in words[1:] if not w.startswith('-')]
238
+ if not args and '-' in words[1:]: continue
239
+ cwd = at(args[0]) if args else '~'
240
+ continue
224
241
  targets = [t for kind, t in redirects if kind == 'read']
225
- if words and words[0].rsplit('/', 1)[-1] in OUTSIDE_READ_PROGRAMS: targets += [w for w in words[1:] if not w.startswith('-')]
242
+ if prog in OUTSIDE_READ_PROGRAMS:
243
+ given = [w for w in words[1:] if not w.startswith('-')]
244
+ targets += given
245
+ if not given and prog in _LISTS_CWD and cwd: out.append(cwd)
246
+ if prog == 'git':
247
+ # git -C <dir> (and --git-dir / --work-tree) reads that repository; a git command after cd reads the folder.
248
+ i, gdirs = 1, []
249
+ while i < len(words) and words[i].startswith('-') and words[i] != '--':
250
+ w = words[i]
251
+ if w in ('-C', '--git-dir', '--work-tree') and i + 1 < len(words): gdirs.append(at(words[i + 1]))
252
+ elif w.startswith(('--git-dir=', '--work-tree=')): gdirs.append(at(w.split('=', 1)[1]))
253
+ i += 2 if w in cs.GIT_GLOBAL_WITH_VALUE else 1
254
+ out += gdirs or ([cwd] if cwd else [])
226
255
  for w in targets:
227
- w = w.replace('$HOME', '~').replace('${HOME}', '~')
228
- if w.startswith(('/', '~', '../')) or w == '..': out.append(w)
256
+ full = at(w)
257
+ if outside_like(full) or cwd: out.append(full)
229
258
  except Exception: return []
230
- return out[:20]
259
+ return list(dict.fromkeys(out))[:20]
231
260
 
232
261
 
233
262
  def outside_read_paths(tool, command, path, cfg, root=None, repo=None):
@@ -407,7 +436,7 @@ def permission(tool, args, command=None, path=None, cfg=None, root=None, ask=Tru
407
436
  emit('policy', root, rule=str(rule)[:200], decision='deny', content=reason[:600], permission=True, command=clip(command or mp.describe(subs), 300))
408
437
  return reason
409
438
  if verdict == 'ask' and ask and not perms.get('bypass'):
410
- if not mp.ask_user(mp.describe(subs), rule, root or ROOT):
439
+ if not mp.ask_user(mp.describe(subs), rule, root or ROOT, user_rule=True):
411
440
  return 'The user did not approve this call (permission rule "' + rule + '" asks first). Choose another approach or explain what you need.'
412
441
  _PERM.update(grant='asked', rule=rule)
413
442
  elif verdict == 'allow':
@@ -1006,7 +1035,9 @@ def before(tool, args, run=None, command=None, path=None, cfg=None, root=None):
1006
1035
  if r['decision'] == 'block': return block_text(r, 'before')
1007
1036
  if command and wants('before_command', cfg):
1008
1037
  r = dispatch('before_command', payload('before_command', run, tool={'name': tool, 'canonical': name}, command=command), command, (), cfg, root)
1009
- if r['decision'] == 'block': return block_text(r, 'before')
1038
+ if r['decision'] == 'block':
1039
+ _note_command_block(root, command, r)
1040
+ return block_text(r, 'before')
1010
1041
  blocked = permission(tool, args, command, path, cfg, root, mode=(run or {}).get('mode'))
1011
1042
  if not blocked and _PERM.get('grant') not in ('allow', 'asked', 'bypass'): blocked = outside_read_gate(tool, args, command, path, cfg, root)
1012
1043
  if not blocked and _PERM.get('grant') not in ('allow', 'asked', 'bypass'): blocked = scope_gate(tool, command, path, cfg, run, root)
@@ -1028,10 +1059,85 @@ def after(tool, args, result, run=None, command=None, path=None, cfg=None, root=
1028
1059
  for p in ([path] if isinstance(path, str) else list(path or []))[:10]:
1029
1060
  if not p: continue
1030
1061
  r = dispatch('after_edit', payload('after_edit', run, tool={'name': tool, 'canonical': name}, path=p), p, (), cfg, root)
1062
+ _note_after_edit(root, p, r)
1031
1063
  if r['decision'] == 'block': notes.append(block_text(r, 'after'))
1032
1064
  return '\n'.join(notes) or None
1033
1065
 
1034
1066
 
1067
+ COMMAND_BLOCKS = 'hooks-command-blocks.json'
1068
+
1069
+ def _note_command_block(root, command, r):
1070
+ """BUG-0695: commands a before_command hook blocked in this run, for the run's outcome."""
1071
+ f = Path(root or ROOT) / COMMAND_BLOCKS
1072
+ try: items = json.loads(f.read_text())
1073
+ except (OSError, ValueError): items = []
1074
+ if not isinstance(items, list): items = []
1075
+ items.append({'command': clip(command, 400), 'hook': (r.get('hook') or {}).get('name', 'hook'), 'reason': clip(r.get('reason'), 400), 'time': time.time()})
1076
+ try: f.write_text(json.dumps(items[-20:]))
1077
+ except OSError: pass
1078
+
1079
+
1080
+ def _command_key(command):
1081
+ words = re.findall(r'[A-Za-z][\w.-]*', str(command or ''))
1082
+ while words and words[0] in ('sudo', 'env', 'command', 'cd'): words = words[1:]
1083
+ return [w.lower() for w in words[:2]]
1084
+
1085
+
1086
+ def blocked_request(task, commands, root=None, since=0):
1087
+ """BUG-0695: when a before_command hook blocked the very action the user asked for ("commit" -> git commit) and no
1088
+ later run of that command succeeded, the request was not done. Returns a cannot-complete reason, or None."""
1089
+ f = Path(root or ROOT) / COMMAND_BLOCKS
1090
+ try: items = json.loads(f.read_text())
1091
+ except (OSError, ValueError): return None
1092
+ if not isinstance(items, list): return None
1093
+ words = set(re.findall(r'[a-z][\w-]*', str(task or '').lower()))
1094
+ for b in reversed(items):
1095
+ if not isinstance(b, dict) or float(b.get('time') or 0) < since: continue
1096
+ key = _command_key(b.get('command'))
1097
+ if not key: continue
1098
+ verb = key[1] if len(key) > 1 and key[0] in ('git', 'npm', 'pnpm', 'yarn', 'gh', 'docker', 'cargo', 'make') else key[0]
1099
+ if verb not in words and verb.rstrip('e') + 'ing' not in words: continue
1100
+ done = any(isinstance(c, dict) and not c.get('returncode') and not c.get('blocked') and float(c.get('time') or 0) > float(b.get('time') or 0)
1101
+ and _command_key(c.get('command')) == key for c in (commands or []))
1102
+ if done: continue
1103
+ return ('The project hook "' + str(b.get('hook')) + '" blocked `' + ' '.join(key) + '`, so this was not done: ' + str(b.get('reason') or ''))[:600]
1104
+ return None
1105
+
1106
+
1107
+ AFTER_EDIT_OPEN = 'hooks-after-edit-open.json'
1108
+
1109
+ def _note_after_edit(root, path, r):
1110
+ """BUG-0693: which edited files a blocking after_edit hook still rejects (cleared when the hook passes again)."""
1111
+ f = Path(root or ROOT) / AFTER_EDIT_OPEN
1112
+ try: items = json.loads(f.read_text())
1113
+ except (OSError, ValueError): items = {}
1114
+ if not isinstance(items, dict): items = {}
1115
+ if r.get('decision') == 'block': items[str(path)] = {'hook': (r.get('hook') or {}).get('name', 'hook'), 'reason': clip(r.get('reason'), 600)}
1116
+ elif str(path) in items: del items[str(path)]
1117
+ else: return
1118
+ try: f.write_text(json.dumps(dict(list(items.items())[-20:])))
1119
+ except OSError: pass
1120
+
1121
+ def open_after_edit(run=None, cfg=None, root=None):
1122
+ """BUG-0693: before a run ends, each file a blocking after_edit hook rejected is checked again (the agent may have
1123
+ fixed it with a command, which runs no after_edit hook). Returns messages for the ones it still rejects."""
1124
+ f = Path(root or ROOT) / AFTER_EDIT_OPEN
1125
+ try: items = json.loads(f.read_text())
1126
+ except (OSError, ValueError): return []
1127
+ if not isinstance(items, dict) or not items: return []
1128
+ cfg = cfg or settings(root)
1129
+ if not wants('after_edit', cfg): return []
1130
+ out = []
1131
+ for p in list(items)[:10]:
1132
+ r = dispatch('after_edit', payload('after_edit', run, tool={'name': 'Edit', 'canonical': 'edit'}, path=p), p, (), cfg, root)
1133
+ _note_after_edit(root, p, r)
1134
+ if r['decision'] == 'block':
1135
+ h = r.get('hook') or {}
1136
+ out.append('The project hook "' + str(h.get('name', 'hook')) + '" still rejects ' + str(p) + ': ' + clip(r['reason'], 600) +
1137
+ ' Fix the file so the hook passes. If you cannot, say so plainly in your answer and name the hook; never report the change as done.')
1138
+ return out
1139
+
1140
+
1035
1141
  def run_end(run=None, summary='', cfg=None, root=None):
1036
1142
  """run_end hooks when the agent is about to finish. Returns a message that keeps it working, or None."""
1037
1143
  cfg = cfg or settings(root); root = Path(root or ROOT)
@@ -241,11 +241,13 @@ def deny_reason(rule): return 'Blocked by your permission rule "' + rule + '" (d
241
241
 
242
242
 
243
243
  # ------------------------------------------------------------------ asking (ask rules)
244
- def ask_user(description, rule, root=None, timeout=600, reason=None):
245
- """Ask the user directly (approval card kind "permission"). True when approved."""
244
+ def ask_user(description, rule, root=None, timeout=600, reason=None, user_rule=False):
245
+ """Ask the user directly (approval card kind "permission"). True when approved.
246
+ user_rule: the user's own ask rule asked (BUG-0692): --yes (-y) does not answer these; a person has to."""
246
247
  root = Path(root or ROOT); ident = os.urandom(8).hex()
247
248
  pending = {'id': ident, 'kind': 'command', 'effect': 'permission', 'rule': rule[:200],
248
- 'reason': reason or ('Your permission rule "' + rule[:200] + '" asks before this runs'), 'command': description[:20000]}
249
+ 'reason': reason or ('Your permission rule "' + rule[:200] + '" asks before this runs'), 'command': description[:20000],
250
+ **({'userRule': True} if user_rule else {})}
249
251
  tmp = root / 'pending.json.tmp'; tmp.write_text(json.dumps(pending)); tmp.replace(root / 'pending.json')
250
252
  try:
251
253
  with (root / 'events.jsonl').open('a') as f: f.write(json.dumps({'type': 'approval', 'time': time.time(), **pending}) + '\n')
@@ -130,7 +130,9 @@ async def run(request: dict[str, Any]) -> int:
130
130
  paths=tuple(str(p) for p in request.get("paths") or ()),
131
131
  )
132
132
  out = Path(str(request.get("out") or ""))
133
- publisher = EventPublisher(log, root, prior=_prior_logs(request.get("prior"), out))
133
+ publisher = EventPublisher(
134
+ log, root, prior=_prior_logs(request.get("prior"), out), repeat_open=request.get("repeatOpen") is True
135
+ )
134
136
  dismissed = read_dismissals(
135
137
  Path(request["dismissed"]) if request.get("dismissed") else None
136
138
  )
@@ -69,6 +69,16 @@ class PublishingSink(FindingSink):
69
69
  self.offered.append(finding)
70
70
  seen = self._seen(finding)
71
71
  if seen is not None:
72
+ # BUG-0658: a local review asked to keep open findings in its result (mlai review in a terminal or CI)
73
+ # reports an earlier review's finding that is still there, so a re-run cannot turn the gate green.
74
+ repeat = getattr(self._publisher, "repeat_finding", None)
75
+ if (
76
+ seen is Offer.REPEATED
77
+ and repeat is not None
78
+ and fingerprint(finding) not in self._commented
79
+ and finding.severity.rank >= self._min_rank
80
+ ):
81
+ await repeat(self._target, finding)
72
82
  return seen
73
83
  if finding.severity.rank < self._min_rank:
74
84
  return Offer.HELD
@@ -5,24 +5,31 @@ those whose lines have changed and are no longer reported "look resolved"."""
5
5
  from __future__ import annotations
6
6
 
7
7
  import json
8
+ from dataclasses import replace
8
9
  from pathlib import Path
9
10
 
10
11
  from mlai_review.application.ports import ReviewPublisher
11
12
  from mlai_review.contracts.review import ReviewPullRequestInput
12
13
  from mlai_review.domain.feedback import Dismissal
13
- from mlai_review.domain.findings import Finding
14
+ from mlai_review.domain.findings import Finding, fingerprint
14
15
  from mlai_review.domain.lifecycle import PriorFinding
15
- from mlai_review.domain.report import ReviewReport
16
+ from mlai_review.domain.report import ReviewReport, Verdict
16
17
  from mlai_review.infrastructure.local.events import EventLog, finding_json, report_json
17
18
 
18
19
  MAX_PRIOR_FILES = 20
19
20
 
20
21
 
21
22
  class EventPublisher(ReviewPublisher):
22
- def __init__(self, log: EventLog, root: Path, *, prior: tuple[Path, ...] = ()) -> None:
23
+ def __init__(
24
+ self, log: EventLog, root: Path, *, prior: tuple[Path, ...] = (), repeat_open: bool = False
25
+ ) -> None:
23
26
  self._log = log
24
27
  self._root = root
25
28
  self._prior_files = prior
29
+ # BUG-0658: with repeat_open (the terminal's mlai review) an earlier finding that is still reported is part
30
+ # of this result too; otherwise (a chat) it is not shown again.
31
+ self._repeat_open = repeat_open
32
+ self._repeated: list[Finding] = []
26
33
  self._posted: list[PriorFinding] = [] # by this run: a retried step must not repeat them
27
34
  self.report: ReviewReport | None = None
28
35
 
@@ -42,9 +49,24 @@ class EventPublisher(ReviewPublisher):
42
49
  )
43
50
  )
44
51
 
52
+ async def repeat_finding(self, target: ReviewPullRequestInput, finding: Finding) -> None:
53
+ if not self._repeat_open:
54
+ return
55
+ if any(fingerprint(f) == fingerprint(finding) for f in self._repeated):
56
+ return
57
+ self._repeated.append(finding)
58
+ self._log.emit("finding", finding={**finding_json(finding, line_text=self._line(finding)), "repeated": True})
59
+
45
60
  async def complete(
46
61
  self, target: ReviewPullRequestInput, publication_id: int, report: ReviewReport
47
62
  ) -> None:
63
+ if self._repeated:
64
+ report = replace(
65
+ report,
66
+ findings=(*report.findings, *self._repeated),
67
+ verdict=Verdict.ISSUES_FOUND,
68
+ repeated=max(0, report.repeated - len(self._repeated)),
69
+ )
48
70
  self.report = report
49
71
  for prior in report.resolved:
50
72
  self._log.emit(
package/worker/preview.py CHANGED
@@ -11,6 +11,15 @@ if sys.platform == 'win32':
11
11
  import winproc; winproc.install() # os.killpg / os.getpgid for the dev server it stops
12
12
 
13
13
 
14
+ LOOPBACK = '127.0.0.1'
15
+
16
+
17
+ def listen_host():
18
+ """BUG-0687: on a laptop the preview listens on this computer only (127.0.0.1); bound to 0.0.0.0 it served the
19
+ project's files to everyone on the same Wi-Fi. The cloud workspace keeps 0.0.0.0, which its preview address needs."""
20
+ return LOOPBACK if os.getenv('MINI_LAPTOP') == '1' or os.getenv('MINI_PREVIEW_HOST') == LOOPBACK else '0.0.0.0'
21
+
22
+
14
23
  def plan(repo):
15
24
  repo = pathlib.Path(repo).resolve()
16
25
  package = repo / 'package.json'
@@ -27,9 +36,9 @@ def plan(repo):
27
36
  if not isinstance(script, str): continue
28
37
  # Only standard framework entrypoints; compound scripts need agent review.
29
38
  if 'next' in deps and re.fullmatch(r'next (dev|start)(?: --(?:hostname|port) [a-zA-Z0-9.:-]+)*', script.strip()):
30
- return {'command': ['npm', 'run', name, '--', '--hostname', '0.0.0.0', '--port', str(PORT)], 'label': 'Next.js', 'cwd': str(repo)}
39
+ return {'command': ['npm', 'run', name, '--', '--hostname', listen_host(), '--port', str(PORT)], 'label': 'Next.js', 'cwd': str(repo)}
31
40
  if 'vite' in deps and re.fullmatch(r'vite(?: --host)?', script.strip()):
32
- return {'command': ['npm', 'run', name, '--', '--host', '0.0.0.0', '--port', str(PORT), '--strictPort'], 'label': 'Vite', 'cwd': str(repo)}
41
+ return {'command': ['npm', 'run', name, '--', '--host', listen_host(), '--port', str(PORT), '--strictPort'], 'label': 'Vite', 'cwd': str(repo)}
33
42
  return None
34
43
  index = repo / 'index.html'
35
44
  if index.is_file() and not index.is_symlink():
@@ -336,6 +345,43 @@ def started_pids(state):
336
345
  return pids
337
346
 
338
347
 
348
+ def _preview_process(pid, repo):
349
+ """True when pid still is a preview process of this project (not a PID the system gave to something else since)."""
350
+ if sys.platform.startswith('linux'): return True # started_pids already compared the start time
351
+ command, cwd = process_command(pid), process_cwd(pid)
352
+ if cwd and inside(cwd, repo): return True
353
+ if command: return 'preview.py' in command or str(repo) in command
354
+ return not cwd and sys.platform == 'win32'
355
+
356
+
357
+ def stop_session(repo, state):
358
+ """BUG-0687: stop the preview servers this session started (and anything they started), when the CLI session that
359
+ started them ends. They used to keep running, orphaned, after mlai exited. Other apps are never touched."""
360
+ import signal, time
361
+ repo, state = pathlib.Path(repo).resolve(), pathlib.Path(state)
362
+ roots = {pid for pid in started_pids(state) if _preview_process(pid, repo)}
363
+ if not roots: return {'stopped': []}
364
+ table = processes()
365
+ tree = process_tree(roots, table) if table else set(roots)
366
+ for pid in sorted(tree | roots):
367
+ try: os.killpg(pid, signal.SIGTERM) if pid in roots and os.name != 'nt' else os.kill(pid, signal.SIGTERM)
368
+ except OSError:
369
+ try: os.kill(pid, signal.SIGTERM)
370
+ except OSError: pass
371
+ def alive(pid):
372
+ if sys.platform.startswith('linux'): return identity(pid) is not None
373
+ try: os.kill(pid, 0); return True
374
+ except OSError: return False
375
+ until = time.time() + 3
376
+ while time.time() < until and any(alive(p) for p in tree | roots): time.sleep(0.1)
377
+ for pid in tree | roots:
378
+ if alive(pid):
379
+ try: os.kill(pid, getattr(signal, 'SIGKILL', signal.SIGTERM))
380
+ except OSError: pass
381
+ (state / 'preview-process.json').unlink(missing_ok=True)
382
+ return {'stopped': sorted(roots), 'port': PORT}
383
+
384
+
339
385
  def process_cwd(pid):
340
386
  if sys.platform.startswith('linux'):
341
387
  try: return os.readlink('/proc/%d/cwd' % pid)
@@ -564,7 +610,7 @@ NETWORK_ENV = ('HTTP_PROXY', 'HTTPS_PROXY', 'NO_PROXY', 'http_proxy', 'https_pro
564
610
  def preview_env():
565
611
  # Only what installs and servers need to reach the registry; never model keys, Vault secrets or registry tokens.
566
612
  env = {k: v for k, v in os.environ.items() if k in SAFE_ENV or k in NETWORK_ENV}
567
- env.update({'PORT': str(PORT), 'HOST': '0.0.0.0', 'BROWSER': 'none', '__VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS': '.vercel.run', 'NEXT_TELEMETRY_DISABLED': '1', 'npm_config_update_notifier': 'false'})
613
+ env.update({'PORT': str(PORT), 'HOST': listen_host(), **({'MINI_PREVIEW_HOST': LOOPBACK} if listen_host() == LOOPBACK else {}), 'BROWSER': 'none', '__VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS': '.vercel.run', 'NEXT_TELEMETRY_DISABLED': '1', 'npm_config_update_notifier': 'false'})
568
614
  return env
569
615
 
570
616
 
@@ -618,13 +664,13 @@ def prod_plan(repo):
618
664
  if selected['label'] == 'Next.js':
619
665
  direct = [str(bin_dir / 'next'), 'build']
620
666
  build = ['npm', 'run', 'build'] if NEXT_BUILD.fullmatch(script.strip()) else direct
621
- serve = me[:-1] + ['--port', str(PORT), '--root', str(repo / 'out')] if static_export(repo) else [str(bin_dir / 'next'), 'start', '--hostname', '0.0.0.0', '--port', str(PORT)]
667
+ serve = me[:-1] + ['--port', str(PORT), '--root', str(repo / 'out')] if static_export(repo) else [str(bin_dir / 'next'), 'start', '--hostname', listen_host(), '--port', str(PORT)]
622
668
  # Next.js 16+ builds into .next while dev uses .next/dev, so the dev preview keeps serving during the build.
623
669
  return {'label': 'Next.js', 'build': build, 'fallback': direct if build != direct else None, 'serve': serve, 'concurrent': next_major(repo) >= 16, 'artifact': 'out' if static_export(repo) else '.next/BUILD_ID'}
624
670
  direct = [str(bin_dir / 'vite'), 'build']
625
671
  build = ['npm', 'run', 'build'] if VITE_BUILD.fullmatch(script.strip()) else direct
626
672
  server = node_app_server(repo)
627
- serve = ['node', server] if server else [str(bin_dir / 'vite'), 'preview', '--host', '0.0.0.0', '--port', str(PORT), '--strictPort']
673
+ serve = ['node', server] if server else [str(bin_dir / 'vite'), 'preview', '--host', listen_host(), '--port', str(PORT), '--strictPort']
628
674
  return {'label': 'Vite', 'build': build, 'fallback': direct if build != direct else None, 'serve': serve, 'concurrent': True, 'artifact': 'dist/index.html'}
629
675
 
630
676
 
@@ -1165,18 +1211,19 @@ def serve(repo, port=None):
1165
1211
  return super().send_head()
1166
1212
  def list_directory(self, path):
1167
1213
  self.send_error(403);return None
1168
- ThreadingHTTPServer(('0.0.0.0', int(port or os.getenv('PORT') or PORT)), Handler).serve_forever()
1214
+ ThreadingHTTPServer((listen_host(), int(port or os.getenv('PORT') or PORT)), Handler).serve_forever()
1169
1215
 
1170
1216
 
1171
1217
  PATHS = {} # explicit repo/state (laptop runs keep the session state apart from the user's folder)
1172
1218
 
1173
1219
  def main():
1174
- parser = argparse.ArgumentParser();parser.add_argument('action', choices=['status', 'start', 'serve', 'supervise', 'finalize']);parser.add_argument('--mode', choices=['dev', 'prod']);parser.add_argument('--root', default='/vercel/sandbox');parser.add_argument('--repo');parser.add_argument('--state');parser.add_argument('--confirmed', action='store_true');parser.add_argument('--agent', action='store_true');parser.add_argument('--revive', action='store_true');parser.add_argument('--port', type=int);args = parser.parse_args()
1220
+ parser = argparse.ArgumentParser();parser.add_argument('action', choices=['status', 'start', 'serve', 'supervise', 'finalize', 'stop']);parser.add_argument('--mode', choices=['dev', 'prod']);parser.add_argument('--root', default='/vercel/sandbox');parser.add_argument('--repo');parser.add_argument('--state');parser.add_argument('--confirmed', action='store_true');parser.add_argument('--agent', action='store_true');parser.add_argument('--revive', action='store_true');parser.add_argument('--port', type=int);args = parser.parse_args()
1175
1221
  root = pathlib.Path(args.root);state = pathlib.Path(args.state) if args.state else root / '.mini'
1176
1222
  if args.action == 'serve': return serve(root, args.port)
1177
1223
  repo = pathlib.Path(args.repo) if args.repo else root / 'repo'
1178
1224
  if args.repo: PATHS['repo'] = repo
1179
1225
  bind(repo, state)
1226
+ if args.action == 'stop': print(json.dumps(stop_session(repo, state))); return
1180
1227
  if args.action in ('supervise', 'finalize'):
1181
1228
  result = supervise(repo, state, args.mode or 'prod') if args.action == 'supervise' else finalize(repo, state)
1182
1229
  print(json.dumps(result)); return
package/worker/runner.py CHANGED
@@ -479,6 +479,47 @@ def start_base(repo):
479
479
  except (OSError,subprocess.SubprocessError):pass
480
480
  return EMPTY_TREE
481
481
 
482
+ # Opus 5.5R: when this many steps in a row end in a failed command or tool, the next step runs on the backup model.
483
+ ESCALATE_AFTER=int(os.getenv('MINI_ESCALATE_AFTER','2') or 2)
484
+ STEP_FAILED=re.compile(r'returncode\W+[1-9]|Command failed with exit code: [1-9]|^(?:Error|error):|<system>ERROR|Tool execution failed',re.M)
485
+ def _content_text(content):
486
+ if isinstance(content,str):return content
487
+ if isinstance(content,list):return ' '.join(str(p.get('text') or '') for p in content if isinstance(p,dict))
488
+ return ''
489
+ EDIT_TOOL_NAMES={'edit_file','write_file','Edit','Write','MultiEdit','StrReplaceFile','WriteFile','apply_patch'}
490
+ SHELL_EDIT=re.compile(r"\bsed\s+-i|\bpatch\b|\bgit\s+apply\b|\btee\b|(?<![0-9&>])>>?\s*[\w./-]+\.\w+|open\([^)]*['\"][wa]")
491
+ def _is_edit(call):
492
+ fn=(call or {}).get('function') or {}
493
+ name=fn.get('name') or '';args=str(fn.get('arguments') or '')
494
+ if name in EDIT_TOOL_NAMES:return True
495
+ if name=='workspace_tool' and re.search(r'"name"\s*:\s*"(edit_file|write_file)"',args):return True
496
+ return bool(SHELL_EDIT.search(args.replace('2>&1','').replace('>/dev/null','').replace('> /dev/null','')))
497
+ def first_edit_index(messages):
498
+ for i,m in enumerate(messages or []):
499
+ if not isinstance(m,dict) or m.get('role')!='assistant':continue
500
+ calls=m.get('tool_calls') or []
501
+ if any(_is_edit(c) for c in calls) or (not calls and SHELL_EDIT.search(_content_text(m.get('content')).replace('2>&1','').replace('/dev/null',''))):return i
502
+ return None
503
+ def failing_streak(messages,rx=STEP_FAILED):
504
+ """Steps in a row, newest first, whose results (tool or observation messages after an assistant turn) show a failure.
505
+ Only steps after the model's first file change count: reproducing a known failure before any fix is expected, not a failed attempt."""
506
+ at=first_edit_index(messages)
507
+ if at is None:return 0
508
+ messages=messages[at:]
509
+ streak=0;group=[]
510
+ for m in reversed(messages or []):
511
+ role=m.get('role') if isinstance(m,dict) else None
512
+ if role in ('tool','user'):group.append(_content_text(m.get('content'))[-6000:]);continue
513
+ if role=='assistant':
514
+ if group and any(rx.search(t) for t in group):streak+=1;group=[];continue
515
+ break
516
+ if role=='system':continue
517
+ break
518
+ return streak
519
+
520
+ class StepsFailing(Exception):
521
+ """Opus 5.5R: Pixis failed ESCALATE_AFTER steps in a row, so this step goes to the backup."""
522
+
482
523
  class RunClosed(RuntimeError):
483
524
  """The server closed this run (409 run_finished); str() is the plain message, without "HTTP Error 409"."""
484
525
  RUN_CLOSED_MESSAGE='This run was already closed on the server, so the agent stopped. Press Resume to carry on in a fresh run; your files are kept.'
@@ -705,7 +746,34 @@ class Model:
705
746
  last=next((m for m in reversed(clean) if m.get('role') in ('user','tool')),None)
706
747
  text=str((last or {}).get('content') or '')[-3000:]
707
748
  return bool(self.FAILURE_SIGNS.search(text))
749
+ FALLBACK_ERRORS=(urllib.error.URLError,TimeoutError,ConnectionError,json.JSONDecodeError,KeyError,IndexError)
708
750
  def request(self,clean,max_tokens=16384,retried=False,allow_tools=True,recovered=False):
751
+ """Opus 5.5R: a Pixis call that still fails after its own retries is sent once to the backup model
752
+ (config.fallback, key FALLBACK_MODEL_API_KEY), billed like any Pixis call. Other models call _request_once only."""
753
+ fb=self.config.get('fallback')
754
+ if not isinstance(fb,dict) or not fb.get('endpoint') or not fb.get('model') or self.config.get('_key_env') or not os.environ.get('FALLBACK_MODEL_API_KEY'):
755
+ return self._request_once(clean,max_tokens,retried,allow_tools,recovered)
756
+ try:
757
+ if allow_tools and failing_streak(clean)>=ESCALATE_AFTER:raise StepsFailing()
758
+ return self._request_once(clean,max_tokens,retried,allow_tools,recovered)
759
+ except RunClosed:raise
760
+ except (StepsFailing,*self.FALLBACK_ERRORS) as exc:
761
+ try:cap=max(0,int(fb.get('maxCalls',40)))
762
+ except (TypeError,ValueError):cap=40
763
+ used=getattr(self,'fallback_calls',0)
764
+ if (ROOT/'cancel').exists() or used>=cap:raise
765
+ self.fallback_calls=used+1
766
+ keys=('provider','model','endpoint','_key_env','_effective_reasoning_effort')
767
+ saved={k:self.config[k] for k in keys if k in self.config}
768
+ self.config.update(provider='openai-compatible',model=str(fb['model']),endpoint=str(fb['endpoint']).rstrip('/'),_key_env='FALLBACK_MODEL_API_KEY')
769
+ reason='steps_failing' if isinstance(exc,StepsFailing) else 'HTTP '+str(exc.code) if isinstance(exc,urllib.error.HTTPError) else type(exc).__name__
770
+ label=str(fb.get('label') or fb['model'])
771
+ event('model_fallback',content=('Pixis could not get the last steps working, so '+label+' took this step.') if reason=='steps_failing' else 'Pixis did not answer this step, so it ran on '+label+'.',model=str(fb['model']),reason=reason,calls=self.fallback_calls)
772
+ try:return self._request_once(clean,max_tokens,retried,allow_tools,recovered)
773
+ finally:
774
+ for k in keys:self.config.pop(k,None)
775
+ self.config.update(saved)
776
+ def _request_once(self,clean,max_tokens=16384,retried=False,allow_tools=True,recovered=False):
709
777
  if self.config.get('focused_tools') and not self.config.get('subagent') and allow_tools:
710
778
  clean=[*clean,{'role':'system','content':'Available tool references (permissions unchanged):\n'+tool_reference(self.config)}]
711
779
  payload = {'model': self.config['model'], 'messages': clean, 'max_tokens': max_tokens}
@@ -1793,7 +1861,7 @@ class Environment(LocalEnvironment):
1793
1861
  why=step_intent('cmd',None,command)
1794
1862
  event('command', command=command,**({'why':why} if why else {}))
1795
1863
  # Never pass model credentials into generated shell commands.
1796
- env = {k:v for k,v in os.environ.items() if k not in ('MODEL_API_KEY','SIDE_MODEL_API_KEY','VERCEL_OIDC_TOKEN','EXA_API_KEY','FIRECRAWL_API_KEY','OPENROUTER_API_KEY','PIXABAY_API_KEY','BRANDFETCH_API_KEY','MINI_IMAGE_URL','MINI_IMAGE_TOKEN','MINI_RESEARCH_TOKEN')}
1864
+ env = {k:v for k,v in os.environ.items() if k not in ('MODEL_API_KEY','SIDE_MODEL_API_KEY','FALLBACK_MODEL_API_KEY','VERCEL_OIDC_TOKEN','EXA_API_KEY','FIRECRAWL_API_KEY','OPENROUTER_API_KEY','PIXABAY_API_KEY','BRANDFETCH_API_KEY','MINI_IMAGE_URL','MINI_IMAGE_TOKEN','MINI_RESEARCH_TOKEN')}
1797
1865
  env.update(VAULT)
1798
1866
  # The preview iframe reaches dev servers through the sandbox's public *.vercel.run host; Vite rejects unknown hosts otherwise.
1799
1867
  env.setdefault('__VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS','.vercel.run')
@@ -2051,6 +2119,9 @@ def main():
2051
2119
  try:
2052
2120
  if mlai_hooks.active():system+='\n'+mlai_hooks.guidance()
2053
2121
  if (config.get('laptop') or os.getenv('MINI_LAPTOP')=='1') and not config.get('subagent'):system+='\n'+mlai_hooks.LAPTOP_RULES # BUG-0523/0529/0531
2122
+ if not config.get('subagent'):
2123
+ from conversation import terminal_deck_rule
2124
+ if terminal_deck_rule(config.get('task','')):system+='\n'+terminal_deck_rule(config.get('task','')) # BUG-0691
2054
2125
  try:
2055
2126
  import mcp_local
2056
2127
  mcp_note=mcp_local.guidance(config,readonly=config.get('mode')=='plan' or conversation_readonly(config))