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.
@@ -30,7 +30,7 @@ def save(name, data):
30
30
  def event(event_type, **data):
31
31
  with (ROOT / 'events.jsonl').open('a') as f: f.write(json.dumps({'type': event_type, 'time': time.time(), **data}) + '\n')
32
32
 
33
- HOOK_KEYS = ('steps_used', 'step_limit_noted', 'recent', 'landed', 'stuck_warned', 'gates_done', 'planned', 'plan_reviewed', 'plan_dismissed', 'brief_ok', 'brief_rounds', 'brief_nudged', 'mode', 'nudged', 'diagnosed_for', 'scope', 'scope_ok', 'approved_kinds', 'calls', 'agents', 'budget_warned', 'touched', 'policy_noted', 'policy_ok_paths', 'token_stage', 'account_stop', 'account_stop_at', 'helpers', 'budget_next', 'budget_declined', 'plan_items', 'brief_q_nudged', 'plan_nudged', 'question_changes')
33
+ HOOK_KEYS = ('steps_used', 'step_limit_noted', 'recent', 'landed', 'stuck_warned', 'gates_done', 'planned', 'plan_reviewed', 'plan_dismissed', 'brief_ok', 'brief_rounds', 'brief_nudged', 'mode', 'nudged', 'diagnosed_for', 'scope', 'scope_ok', 'approved_kinds', 'calls', 'agents', 'budget_warned', 'touched', 'policy_noted', 'policy_ok_paths', 'token_stage', 'account_stop', 'account_stop_at', 'helpers', 'budget_next', 'budget_declined', 'plan_items', 'brief_q_nudged', 'plan_nudged', 'question_changes', 'bom_warned', 'last_tool', 'hook_feedback')
34
34
  def save_state(st):
35
35
  """The hook owns only these keys; the bridge owns kimi-state.json (static) and kimi-progress.json."""
36
36
  save('kimi-hook.json', {k: st[k] for k in HOOK_KEYS if k in st})
@@ -454,13 +454,41 @@ def remember_bom(name, args):
454
454
  if str(target) not in known: save('kimi-bom.json', (known + [str(target)])[-200:])
455
455
 
456
456
  def restore_bom():
457
- """Runs before the next tool call and at Stop (PostToolUse is only registered for project hooks), so it never races an edit."""
457
+ """Runs before the next tool call and at Stop (PostToolUse is only registered for project hooks), so it never races an edit.
458
+ Returns the files it put the mark back on."""
459
+ restored = []
458
460
  for path in load('kimi-bom.json', []):
459
461
  try:
460
462
  target = Path(path)
461
463
  data = target.read_bytes()
462
- if data and not data.startswith(BOM): target.write_bytes(BOM + data)
464
+ if data and not data.startswith(BOM): target.write_bytes(BOM + data); restored.append(path)
463
465
  except OSError: pass
466
+ return restored
467
+
468
+ BOM_NOTE = ('%s starts with a UTF-8 byte order mark (BOM) that the file had before this task. ML AI Code keeps it, so it was put back; '
469
+ 'it is not a bug and no other process or session is changing the file. Do not remove it or try again another way. If a check '
470
+ 'fails on it (JSON.parse in Node rejects a BOM), strip it in memory inside the check (text.replace(/^\\uFEFF/, "")) and leave the file as it is.')
471
+
472
+ def bom_fight(restored, last_tool, st):
473
+ """BUG-0610: the edit tools keep the mark, but the agent then removed it itself (python, printf, rm and recreate) and,
474
+ seeing it come back, fought it until the time limit. The first time a command removes it, the agent is told why it
475
+ stays. Returns the note, or None."""
476
+ if not restored or last_tool in ('Write', 'Edit'): return None
477
+ warned = set(st.get('bom_warned') or [])
478
+ fresh = [p for p in restored if p not in warned]
479
+ if not fresh: return None
480
+ st['bom_warned'] = sorted(warned | set(fresh))[-50:]; save_state(st)
481
+ shown = ', '.join(os.path.relpath(p, REPO) if str(p).startswith(str(REPO)) else str(p) for p in fresh[:3])
482
+ event('note', content='Kept the byte order mark at the start of ' + shown)
483
+ return BOM_NOTE % shown
484
+
485
+ def step_limit_where():
486
+ """BUG-0683: where the step limit is set. On the person's computer there is no Settings > Agent page: the terminal's
487
+ --max-turns or --max-steps flag, or /config maxSteps."""
488
+ if not _laptop_run(): return 'Settings > Agent > Maximum steps per task'
489
+ try: hard = json.loads((ROOT / 'request.json').read_text()).get('hardSteps') is True
490
+ except (OSError, ValueError, AttributeError): hard = False
491
+ return 'the --max-turns limit of this run' if hard else 'set with --max-steps, or /config maxSteps'
464
492
 
465
493
  def step_limit_gate(st):
466
494
  """BUG-0589: Settings > Agent > Maximum steps per task. Each command, edit or app call is one step; at the limit
@@ -473,8 +501,8 @@ def step_limit_gate(st):
473
501
  if not st.get('step_limit_noted'):
474
502
  st['step_limit_noted'] = True; save_state(st)
475
503
  event('note', content='Step limit reached (%d). Asked the agent to wrap up' % limit)
476
- block('Step limit reached: the user allows %d step%s per task (Settings > Agent > Maximum steps per task), and all are used. '
477
- 'This call was not run. Do not call more tools: reply now with what is done, what is left, and that they can raise the limit or send a follow-up.' % (limit, '' if limit == 1 else 's'))
504
+ block('Step limit reached: the user allows %d step%s per task (%s), and all are used. '
505
+ 'This call was not run. Do not call more tools: reply now with what is done, what is left, and that they can raise the limit or send a follow-up.' % (limit, '' if limit == 1 else 's', step_limit_where()))
478
506
  st['steps_used'] = used + 1; save_state(st)
479
507
 
480
508
  def question_only(st):
@@ -503,8 +531,16 @@ def pre(payload, st):
503
531
  name = str(payload.get('tool_name') or '')
504
532
  args = payload.get('tool_input') or {}
505
533
  if not isinstance(args, dict): args = {}
506
- try: restore_bom(); remember_bom(name, args)
534
+ restored = []
535
+ try: restored = restore_bom(); remember_bom(name, args)
507
536
  except Exception: pass
537
+ last_tool = st.get('last_tool'); st['last_tool'] = name; save_state(st)
538
+ fight = bom_fight(restored, last_tool, st)
539
+ if fight: block(fight + ' This call was not run; re-issue it if it is still needed.')
540
+ pending = st.pop('hook_feedback', None)
541
+ if pending: # BUG-0693: an after_edit / after_tool hook block reaches the agent even if the engine drops PostToolUse output
542
+ save_state(st)
543
+ block('A project hook reported a problem after your previous step:\n' + str(pending)[:4000] + '\nThis call was not run. Fix what the hook reports first, then re-issue it if it is still needed.')
508
544
  if (ROOT / 'cancel').exists(): block('Task stopped by user.')
509
545
  if st.get('plan_dismissed') and is_work_tool(name): # before any approval card: Not now means nothing else asks or runs
510
546
  event('plan_review', content='Plan on hold: ' + re.sub(r'^mcp__\w+?__', '', name) + ' was not run')
@@ -786,7 +822,9 @@ def readonly_stop(st):
786
822
  from evidence import EvidenceLedger
787
823
  data = EvidenceLedger(ROOT, st.get('evidence_key') or 'kimi').load()
788
824
  except Exception: return
789
- if data['sources'] and not data['claims']:
825
+ # BUG-0611: on the person's computer a plan or answer about the code that looked something up on the web is not a
826
+ # research report; asking it for a claims ledger only cost repair rounds and left talk about the ledger as the answer.
827
+ if data['sources'] and not data['claims'] and (st.get('research') or not _laptop_run()):
790
828
  st['gates_done'] = sorted(done | {'evidence'}); save_state(st)
791
829
  event('done_gate', content='Research claims not recorded yet')
792
830
  block('Before answering, record the material claims with mcp__mlai__evidence {claims:[{claim, kind, source_ids, quote, uncertainty}]}. Facts need an exact quote from a retrieved source; label inferences and assumptions. Link the source URLs in the answer.')
@@ -859,7 +897,9 @@ def stop(payload, st):
859
897
  from quality import fingerprint
860
898
  report = browser_review(load('design-browser-evidence.json', []), fingerprint(REPO, st.get('base') or 'HEAD'))
861
899
  save('design-review.json', report); event('design_review', designReview=report)
862
- if report['status'] != 'passed':
900
+ if report['status'] != 'passed' and (ROOT / 'browser-unavailable.json').exists():
901
+ done.add('design') # the browser cannot run here; the agent was told to verify with tests and build instead
902
+ elif report['status'] != 'passed':
863
903
  done.add('design'); issues.append(report['message'] + ' Start the app with mcp__mlai__preview {"action":"start"}, then run mcp__mlai__browser at width 390 and at width 1280, each with at least one click/fill/press and an assert_text. Fix overflow, console or page errors and failed requests.')
864
904
  except Exception: pass
865
905
  if 'evidence' not in done:
@@ -885,10 +925,13 @@ def main():
885
925
  st = state()
886
926
  if kind == 'PreToolUse': pre(payload, st)
887
927
  elif kind == 'PostToolUse':
888
- try: restore_bom()
928
+ fight = None
929
+ try: fight = bom_fight(restore_bom(), str(payload.get('tool_name') or ''), st)
889
930
  except Exception: pass
890
931
  feedback = user_hooks('kimi_post', payload, st) # after_tool / after_edit hooks
891
- if feedback: block(feedback)
932
+ if feedback:
933
+ st['hook_feedback'] = feedback; save_state(st)
934
+ if feedback or fight: block('\n\n'.join(t for t in (fight, feedback) if t))
892
935
  elif kind == 'Stop':
893
936
  try: restore_bom()
894
937
  except Exception: pass
@@ -80,6 +80,39 @@ def kimi_servers(config, readonly=False):
80
80
  return out
81
81
 
82
82
 
83
+ def _found(spec, cwd=None):
84
+ """Whether a stdio server's command can be started: an existing file, or a program on its PATH."""
85
+ import shutil
86
+ cmd = os.path.expanduser(spec.get('command') or '')
87
+ if os.sep in cmd or '/' in cmd: return os.path.exists(os.path.join(spec.get('cwd') or cwd or os.getcwd(), cmd))
88
+ path = (spec.get('env') or {}).get('PATH') or os.environ.get('PATH')
89
+ return shutil.which(cmd, path=path) is not None
90
+
91
+
92
+ def check(kimi, config, emit, cwd=None, readonly=False, timeout=20):
93
+ """BUG-0661: a user's MCP server that cannot start or never answers is reported, not silently left out.
94
+ Servers whose command is not found are warned about and dropped from `kimi` (Kimi's mcp.json entries) at once;
95
+ the others are tried in the background while the run starts, and a warning is sent if one does not answer."""
96
+ specs = servers(config, readonly)
97
+ for name in [n for n in kimi if n in specs and 'url' not in specs[n] and not _found(specs[n], cwd)]:
98
+ kimi.pop(name, None)
99
+ emit('MCP server ' + name + ' could not start: the command ' + specs[name]['command'][:200] + ' was not found. Its tools are unavailable.')
100
+ def probe(name, spec):
101
+ start = time.time()
102
+ try: connect(spec, cwd, timeout).close()
103
+ except Exception as exc:
104
+ msg = str(exc)[:200]
105
+ if msg.startswith('No answer') and time.time() - start >= timeout - 1:
106
+ emit('MCP server ' + name + ' did not answer within ' + str(timeout) + ' seconds. Its tools are unavailable.')
107
+ elif msg.startswith('No answer'):
108
+ emit('MCP server ' + name + ' stopped before it answered. Its tools are unavailable.')
109
+ else:
110
+ emit('MCP server ' + name + ' could not start: ' + msg + '. Its tools are unavailable.')
111
+ threads = [threading.Thread(target=probe, args=(n, specs[n]), daemon=True) for n in kimi if n in specs]
112
+ for t in threads: t.start()
113
+ return threads
114
+
115
+
83
116
  def guidance(config, readonly=False):
84
117
  names = sorted(servers(config, readonly))
85
118
  if not names: return ''
@@ -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)
@@ -37,7 +37,7 @@ WRITE_TOOLS = [IMAGE_TOOL, STOCK_TOOL, BRAND_TOOL, *deck_tools.TOOLS, MEMORY_PRO
37
37
  {'name': 'browser', 'description': 'Built-in Playwright browser checks (BUG-0202: this is the browser tool; no Playwright install is needed). Open the local app (http://127.0.0.1:3000 by default, start it with preview first; it is browsed under a preview-style *.vercel.run host, the way the user reaches it, so dev-server host and origin blocks show up here) in headless Chromium at a given viewport, run up to 12 steps and take a screenshot. Reports page errors, console errors, failed requests, horizontal overflow, visible text and whether the page became interactive (hydrated). For UI work check a phone width (<=480) and a desktop width (>=1024) with at least one click/fill/press and an assert_text. Each call starts a fresh browser profile: localStorage, cookies and sessions from an earlier call are gone, so do every step that depends on saved state (sign in, add items, then a goto step to the same URL to reload, then assert_text) inside one call.',
38
38
  'inputSchema': {'type': 'object', 'properties': {'url': {'type': 'string'}, 'width': {'type': 'integer', 'minimum': 320, 'maximum': 1920}, 'height': {'type': 'integer', 'minimum': 400, 'maximum': 1200},
39
39
  'steps': {'type': 'array', 'maxItems': 12, 'items': {'type': 'object', 'properties': {'action': {'type': 'string', 'enum': ['goto', 'click', 'fill', 'press', 'assert_text']}, 'role': {'type': 'string'}, 'name': {'type': 'string'}, 'selector': {'type': 'string'}, 'value': {'type': 'string'}, 'key': {'type': 'string'}, 'text': {'type': 'string'}, 'url': {'type': 'string'}}, 'required': ['action']}}}}},
40
- {'name': 'preview', 'description': 'Start (or check) the managed preview of this project on port 3000: Next.js and Vite dev/start scripts, or a static index.html. Installs missing packages first and runs the dev server (hot reload) during the task; after the task it is swapped for the production build the user opens and shares. Servers started with Bash in the background stop when the task ends.',
40
+ {'name': 'preview', 'description': 'Start (or check) the managed preview of this project on port 3000 (on a laptop it moves to a free port when another app already answers on 3000; always check the url it reports, and never treat another app on the port as this project): Next.js and Vite dev/start scripts, or a static index.html. Installs missing packages first and runs the dev server (hot reload) during the task; after the task it is swapped for the production build the user opens and shares. Servers started with Bash in the background stop when the task ends.',
41
41
  'inputSchema': {'type': 'object', 'properties': {'action': {'type': 'string', 'enum': ['start', 'status']}}, 'required': ['action']}},
42
42
  {'name': 'job', 'description': 'Run long work (training, benchmark sweeps, data pipelines, servers other than the preview) as a supervised background job that outlives this turn. start {name, command, resume?, restart?, keepalive_hours?}: resume is the command that continues from the last checkpoint after an unexpected stop (defaults to command); restart on-failure (default) restarts it automatically up to 3 times; keepalive_hours (max 12) keeps the workspace awake while it runs. status lists jobs with state and log tail; logs {name, lines?}; stop {name}. Make long jobs checkpoint their progress so a restart continues instead of starting over.',
43
43
  'inputSchema': {'type': 'object', 'properties': {'action': {'type': 'string', 'enum': ['start', 'status', 'logs', 'stop']}, 'name': {'type': 'string'}, 'command': {'type': 'string'}, 'resume': {'type': 'string'},
@@ -236,7 +236,22 @@ def readonly_browser():
236
236
  if os.getenv('MINI_SUBAGENT') == '1': return []
237
237
  return [t for t in WRITE_TOOLS if t.get('name') == 'browser']
238
238
 
239
+ def browser_unavailable(reason):
240
+ """Remembers for this run that browser checks cannot run on this computer, so the final gates stop asking for them
241
+ (a Windows run spent 27 minutes being told to rerun a browser check that could not start)."""
242
+ try: (ROOT / 'browser-unavailable.json').write_text(json.dumps({'reason': str(reason)[:500], 'time': time.time()}))
243
+ except OSError: pass
244
+ return reason
245
+
239
246
  def browser(args):
247
+ try: return _browser(args)
248
+ except ValueError as exc:
249
+ from browser_tool import BrowserUnavailable
250
+ if isinstance(exc, BrowserUnavailable) or re.search(r'not installed in this workspace|declined the browser download|could not be installed', str(exc)):
251
+ browser_unavailable(exc)
252
+ raise
253
+
254
+ def _browser(args):
240
255
  args = {k: v for k, v in args.items() if k in ('url', 'width', 'height', 'steps')}
241
256
  installed = False
242
257
  if not (ROOT / 'browser-ready').exists():
@@ -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(