mlai-code 0.3.4 → 0.3.6

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.
package/worker/runner.py CHANGED
@@ -11,7 +11,7 @@ if sys.platform=='win32': # laptop engine on Windows only; cloud workers never
11
11
  from design_director import restore_choice, prepare as prepare_design, browser_review, DESIGN_BUILD_GUIDANCE, DESIGN_BRIEF_GUIDANCE
12
12
  import ask_first
13
13
  import computer_use
14
- from conversation import enabled as conversation_enabled, readonly as conversation_readonly, route as route_conversation, answer_text, recent_history, READONLY_SYSTEM, READ_TOOLS, WORKFLOW_GUIDANCE, research_required, honor_no_change, NO_CHANGE_NOTE, supersede
14
+ from conversation import enabled as conversation_enabled, readonly as conversation_readonly, route as route_conversation, answer_text, recent_history, READONLY_SYSTEM, READ_TOOLS, WORKFLOW_GUIDANCE, research_required, honor_no_change, NO_CHANGE_NOTE, supersede, quick as quick_question
15
15
  from lifecycle import boot_id, identity, nested_guidance, load
16
16
  from context_manager import ContextManager, read_archive
17
17
  import obs_mask
@@ -371,6 +371,16 @@ def bare_read_action(text):
371
371
  if not isinstance(action,dict) or set(action)-{'connection','name','arguments'} or action.get('connection') not in (None,'workspace') or not isinstance(action.get('name'),str) or action.get('name') not in READ_TOOLS or not isinstance(action.get('arguments'),dict):return None
372
372
  return {**action,'connection':'workspace'}
373
373
 
374
+ # A read-only reply in plain prose that announces a next step ("Let me check the forecast.") is not an answer.
375
+ NARRATION=re.compile(r"^\s*(?:ok(?:ay)?,?\s+|sure,?\s+|alright,?\s+)?(?:let me(?!\s+know)|let's|i'll|i will|i'm going to|i am going to|i need to|now i|next,? i|first,? i)\b|\b(?:let me(?!\s+know)|i'll|i will|i'm going to)\b[^.!?\n]{0,160}[.:]?\s*$",re.I)
376
+ def prose_answer(text):
377
+ """A read-only turn's reply written as plain prose with no tool call is the answer, so the model is not asked again
378
+ for the same text (which also printed the answer twice). Fenced, JSON, tool-markup or narration replies are not."""
379
+ t=str(text or '').strip()
380
+ if not t or '```' in t or t.startswith(('{','[','<')) or '<tool_call>' in t or '<arg_key>' in t or 'COMPLETE_TASK_AND_SUBMIT' in t:return None
381
+ if t.endswith(':') or NARRATION.search(t[-400:]) or NARRATION.match(t):return None
382
+ return t
383
+
374
384
  def bare_conversation_answer(text):
375
385
  """Accept a read-only reply written as bare JSON: {"text":...} or {"name":"finish_response","arguments":{"text":...}}, optionally in one json fence."""
376
386
  bare=str(text or '').strip()
@@ -611,6 +621,7 @@ class Model:
611
621
  json.loads(whole.group(1));whole_tool=whole.group(1);blocks=[]
612
622
  except ValueError:whole_tool=None
613
623
  bare_answer=bare_conversation_answer(text) if conversation_readonly(self.config) else None
624
+ if bare_answer is None and conversation_readonly(self.config) and not self.config.get('subagent'):bare_answer=prose_answer(text)
614
625
  if bare_answer is not None:
615
626
  # The model wrote the finish_response arguments as plain JSON text; accept the reply instead of showing raw JSON.
616
627
  return {'role':'assistant','content':bare_answer,'extra':{'actions':[{'kind':'answer','text':bare_answer}]}}
@@ -669,6 +680,17 @@ class Model:
669
680
  common+='Finish the review with a tool fence containing {"name":"finish_review","arguments":{"report":"Evidence-backed findings and limitations"}}.'
670
681
  return common
671
682
  return common+'Use a bash fence for one shell command, or a tool fence with connection workspace, name verify and arguments {label,command} to record an actual project check. To submit, use a bash fence containing echo COMPLETE_TASK_AND_SUBMIT_FINAL_OUTPUT; the normal completion review and verification gates still apply.'
683
+ def unknown_tool(self,name):
684
+ """Tester report on 0.3.4: a call to a tool that is not offered (Edit in a read-only turn) only got a generic
685
+ format hint back, and the model kept trying. Say plainly that it does not exist and list what does."""
686
+ name=str(name or '')[:100]
687
+ offered=[n for n in getattr(self,'offered_functions',None) or [] if n!=name]
688
+ names=sorted(getattr(self,'workspace_tool_names',None) or [])
689
+ line='There is no tool named "'+name+'" in this turn; do not call it again.'
690
+ if offered:line+=' Available tools: '+', '.join(offered)+'.'
691
+ if names and 'workspace_tool' in offered:line+=' workspace_tool names: '+', '.join(names)+'.'
692
+ if conversation_readonly(self.config):line+=' This turn is read-only: files cannot be changed, so answer the user with finish_response instead of trying to edit.'
693
+ return line
672
694
  def reject_native(self,reason):
673
695
  self.native_tool_failures+=1
674
696
  event('retry',content=reason+' Nothing was executed.')
@@ -678,6 +700,7 @@ class Model:
678
700
  return reason+' Nothing was executed. '+self.native_recovery_guidance()
679
701
  FAILURE_SIGNS=re.compile(r'(Traceback|\bError\b|error:|failed|FAILED|returncode\W+[1-9]|exit code [1-9]|not found|rejected)',re.I)
680
702
  def needs_depth(self,clean):
703
+ if quick_question(self.config):return False # a quick question gets fast reasoning on every call
681
704
  if self.calls<2 or self.config.get('conversation_route') in ('plan','research') or self.config.get('mode')=='plan':return True
682
705
  last=next((m for m in reversed(clean) if m.get('role') in ('user','tool')),None)
683
706
  text=str((last or {}).get('content') or '')[-3000:]
@@ -690,7 +713,7 @@ class Model:
690
713
  # only the reasoning controls are specific to the hosted route.
691
714
  if self.config.get('provider') in ('isoquant','openai-compatible'):
692
715
  if self.config.get('provider')=='isoquant':
693
- default_effort='high' if conversation_enabled(self.config) and self.config.get('conversation_route') in ('build','research','plan') else 'low'
716
+ default_effort='high' if conversation_enabled(self.config) and self.config.get('conversation_route') in ('build','research','plan') and not quick_question(self.config) else 'low'
694
717
  effort=self.config.get('_effective_reasoning_effort',self.config.get('reasoningEffort',default_effort))
695
718
  # This GLM endpoint accepts low/high/max only. Medium is adaptive: deep reasoning for the opening plan,
696
719
  # plan/research routes and recovery after a failed step; fast reasoning for routine steps.
@@ -719,10 +742,15 @@ class Model:
719
742
  tools.append({'type':'function','function':{'name':'finish_response','description':'Return the final conversational answer without executing commands.','parameters':{'type':'object','properties':{'text':{'type':'string','minLength':1,'maxLength':20000}},'required':['text'],'additionalProperties':False}}})
720
743
  if self.config.get('focused_tools') and not self.config.get('subagent'):
721
744
  tools[0]['function']['parameters']['properties']['name']['enum']=tool_names(self.config)
745
+ # Web search is a first-class native function in every read-only main-agent turn (chat included), so the model
746
+ # sees it on its first call instead of finding it later through tool_catalog. Calls route through workspace_tool.
747
+ if conversation_readonly(self.config) and not self.config.get('subagent') and 'web_search' in tools[0]['function']['parameters']['properties']['name']['enum'] and not any(t['function']['name']=='web_search' for t in tools):
748
+ tools.append({'type':'function','function':{'name':'web_search','description':'Search the public web for current facts (weather, news, prices, scores, releases, documentation). Use it instead of saying you have no web or real-time access.','parameters':{'type':'object','properties':{'query':{'type':'string','minLength':1,'maxLength':400},'objective':{'type':'string','maxLength':400},'max_results':{'type':'integer','minimum':1,'maximum':8}},'required':['query'],'additionalProperties':False}}})
749
+ tools.append({'type':'function','function':{'name':'web_fetch','description':'Read up to three public https pages, usually from web_search results.','parameters':{'type':'object','properties':{'urls':{'type':'array','minItems':1,'maxItems':3,'items':{'type':'string'}}},'required':['urls'],'additionalProperties':False}}})
722
750
  # Computer use (Mac app, local runs, turned on in Settings): offered only when the app passed its broker in the env.
723
751
  if computer_use.available() and self.config.get('mode')!='plan' and not self.config.get('subagent') and not conversation_readonly(self.config):tools[0]['function']['parameters']['properties']['name']['enum'].append('computer')
724
752
  if self.config.get('tools') and (not conversation_readonly(self.config) or read_only_tools(self.config.get('tools'))):tools.append({'type':'function','function':{'name':'connected_tool','description':'Request an explicitly enabled integration tool (in a read-only turn, only its reading tools); requires user approval.','parameters':{'type':'object','properties':{'connection':{'type':'string'},'name':{'type':'string'},'arguments':{'type':'object','additionalProperties':True}},'required':['connection','name','arguments'],'additionalProperties':False}}})
725
- if allow_tools and self.native_tool_failures<2:payload['tools']=tools
753
+ if allow_tools and self.native_tool_failures<2:payload['tools']=tools;self.offered_functions=[t['function']['name'] for t in tools]
726
754
  if allow_tools:self.workspace_tool_names=set(next((t['function']['parameters']['properties']['name'].get('enum',[]) for t in tools if t['function']['name']=='workspace_tool'),[]))-{'edit_file','verify'}
727
755
  key = os.environ.get(self.config.get('_key_env') or 'MODEL_API_KEY','') # the side model has its own key
728
756
  headers={'Authorization': 'Bearer ' + key, 'Content-Type':'application/json'}
@@ -896,7 +924,7 @@ class Model:
896
924
  event('retry',content='Recovered a malformed native call into a validated action.')
897
925
  text+=('\n```bash\n'+value+'\n```') if kind=='bash' else ('\n```tool\n'+json.dumps(value)+'\n```')
898
926
  else:
899
- return self.reject_native('Unsupported native tool '+str(function.get('name',''))[:100]+'.'),usage
927
+ return self.reject_native(self.unknown_tool(function.get('name'))),usage
900
928
  if why:remember_step(function.get('name'),arguments,why)
901
929
  finish=result.get('choices',[{}])[0].get('finish_reason') if result.get('choices') else result.get('stop_reason')
902
930
  if finish in ('length','max_tokens') or not text.strip():
@@ -1317,8 +1345,9 @@ class Environment(LocalEnvironment):
1317
1345
  if outcome.get('screenshot'):
1318
1346
  vp=outcome.get('viewport') or {}
1319
1347
  self.attach_vision({'path':str(directory/outcome['screenshot']),'name':outcome['screenshot'],'note':str(outcome.get('url',''))+' at '+str(vp.get('width','?'))+'x'+str(vp.get('height','?'))+('; scenario passed' if outcome.get('success') else '; scenario reported problems')})
1320
- record={'label':'Browser scenario','command':str(args.get('url','http://127.0.0.1:3000')),'returncode':0 if outcome.get('success') else 1,'executed':True,'output':result[-4000:],'time':time.time()}
1321
- write('checks.json',(load(ROOT,'checks.json',[])+[record])[-20:])
1348
+ from runtime_control import browser_check
1349
+ record=browser_check(outcome) # names the page actually checked, not the default address (CLI 0.3.4)
1350
+ write('checks.json',[c for c in load(ROOT,'checks.json',[]) if c.get('command')!=record['command']][-19:]+[record])
1322
1351
  if not outcome.get('success'):
1323
1352
  self.plan_failure('browser','Browser scenario',result)
1324
1353
  return {'returncode':1,'output':result}
@@ -1378,6 +1407,10 @@ class Environment(LocalEnvironment):
1378
1407
  scope=verification_scope(command,REPO)
1379
1408
  before_check=fingerprint(REPO,self.review_base or 'HEAD')
1380
1409
  outcome=self.execute({'command':command,'verification':True})
1410
+ if outcome.get('executed') is not True:
1411
+ # Refused, blocked or not run (plan mode, offline wait): not a check result, and never a replacement for
1412
+ # the last real result of this command (CLI 0.3.4).
1413
+ return {**outcome,'output':str(outcome.get('output',''))+'\n[This check did not run, so no result was recorded.]'}
1381
1414
  if scope=='inspection-only':outcome={**outcome,'output':outcome.get('output','')+'\n[Inspection only: displaying a log or value does not verify the source. Run an actual test or executable assertion through verify; this command cannot satisfy completion.]'}
1382
1415
  elif scope!='repository':outcome={**outcome,'output':outcome.get('output','')+'\n[Auxiliary reproduction only: this check changes outside the repository or has an unresolved working directory. Run passing checks against the actual repository after review to complete.]'}
1383
1416
  record={'label':label,'command':command,'scope':scope,'returncode':outcome.get('returncode',1),'executed':outcome.get('executed',False),'output':outcome.get('output','')[-4000:],'time':time.time(),'sourceFingerprint':fingerprint(REPO,self.review_base or 'HEAD')}
@@ -1632,11 +1665,15 @@ class Environment(LocalEnvironment):
1632
1665
  self.plan_completion_nudged=True
1633
1666
  return {'returncode':1,'output':'Update your investigation plan before finishing: mark completed work done, leave blocked work pending, and explain any concrete blocker in the final answer. Do not mark proposed future implementation as completed.'}
1634
1667
  if self.web_sources and not any(url.rstrip('.,);') in answer for url in self.web_sources) and not self.citation_nudged:
1635
- self.citation_nudged=True
1636
- return {'returncode':1,'output':'You retrieved external evidence. Link the relevant retrieved source URLs in your final answer so the user can verify your recommendation. Do not invent URLs.'}
1668
+ if quick_question(self.agent_config):
1669
+ # A quick question is not sent back to rewrite its answer (which showed it twice): the sources are appended.
1670
+ answer+='\n\nSources: '+', '.join(sorted(self.web_sources)[:3])
1671
+ else:
1672
+ self.citation_nudged=True
1673
+ return {'returncode':1,'output':'You retrieved external evidence. Link the relevant retrieved source URLs in your final answer so the user can verify your recommendation. Do not invent URLs.'}
1637
1674
  if research_required(self.agent_config) and self.web_attempts and not self.web_sources:
1638
1675
  answer+='\n\nWeb verification was unavailable or returned no usable source URLs; current external claims remain unverified.'
1639
- issue=self.evidence.completion_issue(answer)
1676
+ issue=None if quick_question(self.agent_config) else self.evidence.completion_issue(answer) # a quick answer links its sources instead
1640
1677
  if issue and research_required(self.agent_config):
1641
1678
  self.evidence_waits+=1
1642
1679
  if self.evidence_waits<=2:return {'returncode':1,'output':issue}
@@ -1811,9 +1848,12 @@ def simple_readonly(config):
1811
1848
  """BUG-0522/0559: a short Ask or Plan request ("What does a.txt contain?", "Plan a test file for even.js") is answered
1812
1849
  directly: no task contract, no completion reviews of that contract. Long or research requests keep the full workflow."""
1813
1850
  task=str(config.get('task') or '')
1814
- return conversation_readonly(config) and len(task)<=800 and not research_required(config)
1851
+ return conversation_readonly(config) and len(task)<=800 and (not research_required(config) or quick_question(config))
1815
1852
  SIMPLE_READONLY_NOTE=('This is a short read-only request. Do not create a task_contract or todo list. Read only the files you need, each '
1816
1853
  'once (request several together), then reply with finish_response. Do not read a file again that you already have.')
1854
+ QUICK_LIVE_NOTE=('This is a quick question about current public facts. Call web_search once (web_fetch one result only if the '
1855
+ 'snippets lack the answer), then answer in a few sentences with finish_response and link the source URL. No task_contract, '
1856
+ 'todo list or evidence claims are needed.')
1817
1857
  DENIED=('The user denied this command, so it did not run. Stop this line of work: do not run it another way and do not start '
1818
1858
  'other work they did not ask for. Say plainly what was not done and ask what they want next.')
1819
1859
  mlai_hooks.install_runner(globals(), Environment)
@@ -1868,10 +1908,43 @@ def run_usage(model,environment):
1868
1908
  'research':int(getattr(environment,'web_attempts',0) or 0),'connectors':int(counts.get('connectors',0)),'tools':int(counts.get('tools',0)),
1869
1909
  **{k:int((getattr(model,'metrics',None) or {}).get(src,0) or 0) for k,src in (('prompt','prompt_tokens'),('cached','cached_tokens'),('completion','completion_tokens'),('masked','masked_chars'))}}
1870
1910
 
1911
+ def direct_turn(config,model,history=(),status_extra=None,usage_extra=None,metrics=True):
1912
+ """A greeting, thanks or question about the agent (conversation.direct_reply): one model call with no tools, then the
1913
+ run ends. Nothing is read, scanned, edited or checked, so there is no changed-files or checks line either."""
1914
+ from conversation import direct_messages,direct_answer
1915
+ text=''
1916
+ try:text,_=model.request(direct_messages(config,REPO,history),max_tokens=400,allow_tools=False)
1917
+ except (KeyboardInterrupt,SystemExit):raise
1918
+ except Exception as exc:
1919
+ if (ROOT/'cancel').exists():raise
1920
+ event('retry',content='The model call for this reply failed ('+str(exc).replace(os.environ.get('MODEL_API_KEY','__absent__'),'[redacted]')[:160]+'); sent a short reply instead.')
1921
+ answer=direct_answer(text,config.get('task'),REPO)
1922
+ event('answer',content=answer)
1923
+ report={'changedFiles':[],'changedFilesTotal':0,'checksPassed':0,'checksFailed':0,'checksNotRun':0,'checksInconclusive':0,'checksStale':0,
1924
+ 'externalChecksPassed':0,'externalChecksPending':0,'externalChecksFailed':0,'unfinishedTasks':[],'verification':'','verificationStatus':'none','created':time.time()}
1925
+ write('completion.json',report);event('completion',**report)
1926
+ write('status.json',{'state':'completed','result':{'exit_status':'Submitted','submission':answer},'tokens':model.tokens+image_billed(),'calls':model.calls,
1927
+ **(status_extra or {}),'endReason':'completed','usage':{**run_usage(model,None),**(usage_extra or {}),'endReason':'completed','direct':1}})
1928
+ event('finished',state='completed')
1929
+ if not metrics:return answer # the Kimi bridge reports run_metrics in its own finally
1930
+ try:
1931
+ m=model.metrics;wall=time.monotonic()-m['started']
1932
+ event('run_metrics',calls=model.calls,wall_seconds=round(wall,2),inference_seconds=round(m['inference_seconds'],2),tool_seconds=round(max(0.0,wall-m['inference_seconds']),2),prompt_tokens=m['prompt_tokens'],cached_tokens=m['cached_tokens'],completion_tokens=m['completion_tokens'],cache_hit_rate=round(m['cached_tokens']/m['prompt_tokens'],3) if m['prompt_tokens'] else 0)
1933
+ except Exception:pass
1934
+ return answer
1935
+
1871
1936
  def main():
1872
1937
  config = json.loads((ROOT / 'request.json').read_text())
1873
1938
  write('status.json', {'state':'running','started':time.time(),'boot':boot_id(),'process':identity(os.getpid())})
1874
1939
  if not config.get('resuming') and not config.get('subagent'):(ROOT/'kimi-approve-all').unlink(missing_ok=True) # a new request asks afresh
1940
+ # What was already changed or untracked before this turn is the person's own work: the completion report ("N files
1941
+ # changed") subtracts it (runtime_control.completion_report). A resumed run keeps the snapshot from its start.
1942
+ if not config.get('resuming') or not (ROOT/'start-dirty.json').exists():
1943
+ (ROOT/'start-dirty.json').unlink(missing_ok=True)
1944
+ try:
1945
+ from runtime_control import save_start_dirty
1946
+ save_start_dirty(ROOT,REPO,start_base(REPO))
1947
+ except Exception:pass
1875
1948
  from build_planner import finished_phase_reply
1876
1949
  nothing_to_do=finished_phase_reply(REPO,config.get('task'))
1877
1950
  if nothing_to_do and not config.get('subagent'):
@@ -1915,6 +1988,16 @@ def main():
1915
1988
  event('error',content=message)
1916
1989
  (ROOT/'pending.json').unlink(missing_ok=True);(ROOT/'run.lock').unlink(missing_ok=True)
1917
1990
  return
1991
+ from conversation import direct_reply
1992
+ if direct_reply(config):
1993
+ try:direct_turn(config,model,config.get('conversation_history') or ())
1994
+ except Exception as exc:
1995
+ message=str(exc).replace(os.environ.get('MODEL_API_KEY','__absent__'),'[redacted]')
1996
+ write('status.json',{'state':'stopped' if (ROOT/'cancel').exists() else 'error','error':message,'tokens':model.tokens+image_billed(),'calls':model.calls})
1997
+ event('error',content=message)
1998
+ finally:
1999
+ for name in ('pending.json','run.lock'):(ROOT/name).unlink(missing_ok=True)
2000
+ return
1918
2001
  resolve_role(config)
1919
2002
  system = SYSTEM
1920
2003
  from reply_language import rule as reply_language_rule
@@ -1958,6 +2041,7 @@ def main():
1958
2041
  if config.get('tools'):system+=' Enabled integration catalog (the app runs each call; Auto mode needs no approval): {{tool_catalog}}'
1959
2042
  if conversation_enabled(config):system+='\n'+WORKFLOW_GUIDANCE
1960
2043
  if simple_readonly(config):system+='\n'+SIMPLE_READONLY_NOTE # BUG-0522/0559
2044
+ if quick_question(config) and config.get('quick_question')=='live':system+='\n'+QUICK_LIVE_NOTE
1961
2045
  if config.get('selected_design') and not conversation_readonly(config):
1962
2046
  system+='\n'+DESIGN_BUILD_GUIDANCE
1963
2047
  briefing=bool(config.get('brief')) or (config.get('briefFirst') is True and config.get('mode','approve') in ('approve','auto') and not config.get('subagent'))
@@ -2081,7 +2165,7 @@ def main():
2081
2165
  tidied=summary(tidy(REPO,since=RUN_STARTED))
2082
2166
  if tidied:event('note',content=tidied)
2083
2167
  except Exception:pass
2084
- report=completion_report(ROOT,REPO,getattr(environment,'review_base',None))
2168
+ report=completion_report(ROOT,REPO,getattr(environment,'review_base',None),since=RUN_STARTED)
2085
2169
  if getattr(environment,'reply_questions',None):event('questions',questions=environment.reply_questions)
2086
2170
  write('completion.json',report)
2087
2171
  event('completion',**report)
@@ -35,6 +35,11 @@ def latest_checks(checks):
35
35
  items = list(latest.items())
36
36
  out = []
37
37
  for n, (key, check) in enumerate(items):
38
+ # A failed browser check followed by a passing one at the same width is history: the agent fixed the page or found
39
+ # the right address (CLI 0.3.4: the first check opened another app on port 3000, the next the notes app on its port).
40
+ if check.get('kind') == 'browser' and check_status(check) == 'failed' and any(
41
+ k[0] == key[0] and c.get('kind') == 'browser' and c.get('width') == check.get('width') and check_status(c) == 'passed'
42
+ for k, c in items[n + 1:]): continue
38
43
  broader = [c for k, c in items[n + 1:] if k[0] == key[0] and isinstance(k[1], str) and isinstance(key[1], str)
39
44
  and key[1].startswith(k[1] + ' ') and NARROWING.search(key[1][len(k[1]):]) and check_status(c) == 'passed']
40
45
  if broader and check_status(check) == 'failed': continue
@@ -89,7 +94,7 @@ def check_status(check):
89
94
  return 'passed'
90
95
 
91
96
  def check_summary(checks):
92
- result = dict(passed=0, failed=0, notRun=0, inconclusive=0, stale=0, externalPending=0, externalFailed=0, externalPassed=0, supporting=0)
97
+ result = dict(passed=0, failed=0, notRun=0, inconclusive=0, stale=0, externalPending=0, externalFailed=0, externalPassed=0, supporting=0, supportingPassed=0, supportingFailed=0)
93
98
  for check in latest_checks(checks):
94
99
  scope = check.get('scope') or 'repository'
95
100
  status = check_status(check)
@@ -97,6 +102,7 @@ def check_summary(checks):
97
102
  result[{'passed': 'externalPassed', 'failed': 'externalFailed'}.get(status, 'externalPending')] += 1
98
103
  elif scope != 'repository':
99
104
  result['supporting'] += 1
105
+ if status in ('passed', 'failed'): result['supporting' + status.title()] += 1
100
106
  else:
101
107
  result[{'not_run': 'notRun'}.get(status, status)] += 1
102
108
  result['needsAttention'] = bool(result['failed'] or result['notRun'] or result['inconclusive'] or result['stale'] or result['externalPending'] or result['externalFailed'])
@@ -144,25 +150,36 @@ def completion_paths(repo,base=None,since=None):
144
150
  return [p for p in dict.fromkeys(change_path(l) for l in lines) if p and not is_noise(p)]
145
151
 
146
152
 
147
- def completion_report(root,repo,base=None):
153
+ def completion_report(root,repo,base=None,since=None):
148
154
  import subprocess
149
155
  root=Path(root)
150
156
  def read(name,fallback):
151
157
  try:return json.loads((root/name).read_text())
152
158
  except (OSError,ValueError):return fallback
153
159
  checks=read('checks.json',[]);todos=read('todos.json',[])
154
- changes=completion_paths(repo,base,read('status.json',{}).get('started') if isinstance(read('status.json',{}),dict) else None)
160
+ if since is None:since=read('status.json',{}).get('started') if isinstance(read('status.json',{}),dict) else None
161
+ files=read(START_FILES,None)
162
+ # A folder without git: what the turn wrote is what differs from the file snapshot taken when it started (CLI 0.3.4:
163
+ # index.html and styles.css were written, yet the record listed no files).
164
+ changes=snapshot_changes(repo,files) if isinstance(files,dict) and not is_git_tree(repo) else completion_paths(repo,base,since)
165
+ # Only what this turn changed: paths already changed or untracked when the turn started (save_start_dirty) are the
166
+ # person's own work. Subtracted before the list is clipped, so 600 old files cannot hide the one this turn made.
167
+ snapshot=read(START_DIRTY,None)
168
+ if isinstance(snapshot,dict):changes=changed_during_turn(repo,changes,snapshot)
155
169
  summary = check_summary(checks)
156
170
  unfinished = [t.get('text','') for t in todos if isinstance(t, dict) and t.get('status') not in ('done','cancelled')]
157
171
  note = ('Local checks: %(passed)d passed, %(failed)d failed, %(notRun)d not run, %(inconclusive)d inconclusive.' % summary) if checks else 'No verification checks recorded.'
158
172
  if summary['stale']: note += ' %d passed before the last code edit and must be rerun.' % summary['stale']
159
173
  if summary['externalPending'] or summary['externalFailed'] or summary['externalPassed']:
160
174
  note += ' Remote/CI: %(externalPassed)d passed, %(externalFailed)d failed, %(externalPending)d pending or unverified.' % summary
161
- if summary['supporting']: note += ' %d supporting evidence checks are not repository verification.' % summary['supporting']
175
+ if summary['supporting']: note += ' %d supporting evidence checks are not repository verification (%d passed, %d failed).' % (summary['supporting'], summary['supportingPassed'], summary['supportingFailed'])
162
176
  if unfinished: note += ' %d plan items remain unfinished.' % len(unfinished)
163
177
  status = 'remote_pending' if summary['remotePendingOnly'] and not unfinished else 'needs_attention' if summary['needsAttention'] or unfinished else 'passed' if summary['passed'] else 'unverified'
164
178
  if status == 'remote_pending': note += ' Local checks passed; the remote result has not been observed, so this is not yet confirmed end to end.'
165
- return {'changedFiles':changes[:100], 'checksPassed':summary['passed'], 'checksFailed':summary['failed'],
179
+ # checksPassed stays repository verification only (the gates rely on it). The evidence checks an inspect-only turn ran are
180
+ # counted next to it, so the Done line says "1 check passed" instead of "0 checks passed" (CLI 0.3.4).
181
+ return {'changedFiles':changes[:100], 'changedFilesTotal':len(changes), 'checksPassed':summary['passed'], 'checksFailed':summary['failed'],
182
+ 'supportingChecksPassed':summary['supportingPassed'], 'supportingChecksFailed':summary['supportingFailed'],
166
183
  'checksNotRun':summary['notRun'], 'checksInconclusive':summary['inconclusive'], 'checksStale':summary['stale'], 'externalChecksPassed':summary['externalPassed'],
167
184
  'externalChecksPending':summary['externalPending'], 'externalChecksFailed':summary['externalFailed'],
168
185
  'unfinishedTasks':unfinished, 'verification':note, 'verificationStatus':status, 'created':time.time()}
@@ -184,24 +201,91 @@ def _fingerprint(repo,path):
184
201
  return 'missing'
185
202
 
186
203
 
187
- def dirty_snapshot(repo,base=None,limit=400):
188
- """Paths that were already changed when the turn started, with a fingerprint of each."""
204
+ def _stat_fingerprint(repo,path):
205
+ """A cheap fingerprint (size and modification time) for snapshots too large to hash file by file."""
206
+ try:
207
+ st=(Path(repo)/path).lstat()
208
+ return 'stat:%d:%d'%(st.st_size,st.st_mtime_ns)
209
+ except OSError:return 'stat:missing'
210
+
211
+
212
+ def dirty_snapshot(repo,base=None,limit=None,hash_limit=2000,hash_bytes=2_000_000):
213
+ """Paths that were already changed when the turn started, with a fingerprint of each. Every path is recorded (a folder
214
+ with 600 untracked files must not report the 401st as this turn's change); the first hash_limit small files get a
215
+ content hash, the rest a size/mtime fingerprint. limit is kept for callers that want a bounded sample."""
189
216
  try:paths=completion_paths(repo,base)
190
217
  except Exception:return {}
191
- return {p:_fingerprint(repo,p) for p in paths[:limit]}
218
+ if limit is not None:paths=paths[:limit]
219
+ snap={}
220
+ for i,p in enumerate(paths):
221
+ target=Path(repo)/p
222
+ try:small=i<hash_limit and (target.is_dir() or not target.is_file() or target.stat().st_size<=hash_bytes)
223
+ except OSError:small=True
224
+ snap[p]=_fingerprint(repo,p) if small else _stat_fingerprint(repo,p)
225
+ return snap
192
226
 
193
227
 
194
228
  START_DIRTY='start-dirty.json'
229
+ START_FILES='start-files.json'
195
230
 
196
231
  def save_start_dirty(root,repo,base=None):
197
232
  """BUG-0495/0497: remember what was already changed or untracked when the turn started, so the checker and the
198
- "N files changed" summary only count what this turn did. Best effort: a failure keeps the old behavior."""
233
+ "N files changed" summary only count what this turn did. A folder without git gets a file snapshot instead
234
+ (snapshot_changes). Best effort: a failure keeps the old behavior."""
235
+ try:
236
+ files=Path(root)/START_FILES
237
+ if is_git_tree(repo):files.unlink(missing_ok=True)
238
+ else:files.write_text(json.dumps(file_snapshot(repo)))
239
+ except Exception:pass
199
240
  try:
200
241
  snap=dirty_snapshot(repo,base)
201
242
  (Path(root)/START_DIRTY).write_text(json.dumps(snap))
202
243
  return snap
203
244
  except Exception:return None
204
245
 
246
+
247
+ def is_git_tree(repo):
248
+ import subprocess
249
+ try:return subprocess.run(['git','rev-parse','--is-inside-work-tree'],cwd=repo,capture_output=True,text=True,timeout=10).stdout.strip()=='true'
250
+ except (OSError,subprocess.SubprocessError):return False
251
+
252
+
253
+ # Folders that are never the turn's own work: dependencies, caches, VCS and engine state.
254
+ SNAPSHOT_SKIP=frozenset(('.git','node_modules','.mlai','.mini','.kimi-code','__pycache__','.pytest_cache','.mypy_cache','.ruff_cache','.venv','venv','.next','.turbo','.vercel','.cache','.parcel-cache','.svelte-kit'))
255
+
256
+ def file_snapshot(repo,limit=20000,seconds=3.0):
257
+ """Size and modification time of every file under repo, sorted, without noise folders. Bounded by file count and time;
258
+ 'complete' says whether the whole folder was seen."""
259
+ from repo_hygiene import is_noise
260
+ files,complete,deadline={},True,time.monotonic()+seconds
261
+ root=str(repo)
262
+ for top,dirs,names in os.walk(root):
263
+ dirs[:]=sorted(d for d in dirs if d not in SNAPSHOT_SKIP and not os.path.islink(os.path.join(top,d)))
264
+ rel=os.path.relpath(top,root);rel='' if rel=='.' else rel.replace(os.sep,'/')+'/'
265
+ for name in sorted(names):
266
+ path=rel+name
267
+ if is_noise(path):continue
268
+ try:st=os.lstat(os.path.join(top,name))
269
+ except OSError:continue
270
+ files[path]='%d:%d'%(st.st_size,st.st_mtime_ns)
271
+ if len(files)>=limit:break
272
+ if len(files)>=limit or time.monotonic()>deadline:complete=False;break
273
+ return {'complete':complete,'time':time.time(),'files':files}
274
+
275
+ def snapshot_changes(repo,start):
276
+ """Paths created, changed or deleted since the file_snapshot start. When the start snapshot was cut short, a path it
277
+ never saw counts only if it was modified after the snapshot."""
278
+ old=start.get('files') if isinstance(start.get('files'),dict) else {}
279
+ now=file_snapshot(repo)
280
+ began=float(start.get('time') or 0)
281
+ out=[]
282
+ for path,fp in now['files'].items():
283
+ if path in old:
284
+ if old[path]!=fp:out.append(path)
285
+ elif start.get('complete',True) or int(fp.split(':')[1])/1e9>=began-1:out.append(path)
286
+ if now['complete']:out+=[p for p in old if p not in now['files']]
287
+ return out
288
+
205
289
  def turn_changes(root,repo,changed):
206
290
  """changed minus the paths that were already like this when the turn started (see save_start_dirty)."""
207
291
  try:snap=json.loads((Path(root)/START_DIRTY).read_text())
@@ -211,7 +295,10 @@ def turn_changes(root,repo,changed):
211
295
  def changed_during_turn(repo,changed,snapshot):
212
296
  """Of the paths changed now, the ones the turn itself changed: new, or different from the start snapshot."""
213
297
  if not isinstance(snapshot,dict):return list(changed or [])
214
- return [p for p in changed or [] if p not in snapshot or snapshot[p]!=_fingerprint(repo,p)]
298
+ def same(p):
299
+ old=snapshot[p]
300
+ return old==(_stat_fingerprint(repo,p) if str(old).startswith('stat:') else _fingerprint(repo,p))
301
+ return [p for p in changed or [] if p not in snapshot or not same(p)]
215
302
 
216
303
 
217
304
  SAFE_PROGRAMS=frozenset(('ls','cat','head','tail','wc','grep','rg','pwd','echo','tree','stat','file','du','df','which','whoami','date','uname','jq','sort','uniq','cut','tr','basename','dirname','realpath','diff','true','nl','column'))
@@ -260,6 +347,18 @@ def approve_operation(root,mode,description,cancelled,emit):
260
347
  decision.unlink(missing_ok=True);(root/'pending.json').unlink(missing_ok=True)
261
348
 
262
349
 
350
+ def browser_check(outcome, scope='repository'):
351
+ """A browser scenario as a check record. It names the address the browser actually checked, not the one first asked
352
+ for or the default (CLI 0.3.4: a passing check of the notes app on :8741 was saved as http://127.0.0.1:3000)."""
353
+ outcome = outcome if isinstance(outcome, dict) else {}
354
+ url = str(outcome.get('checkedAt') or outcome.get('url') or outcome.get('requestedUrl') or '')[:1000]
355
+ vp = outcome.get('viewport') if isinstance(outcome.get('viewport'), dict) else {}
356
+ width = ' at %spx' % vp['width'] if vp.get('width') else ''
357
+ return {'label': clip('Browser check ' + url + width, 160), 'command': 'browser ' + url + width, 'kind': 'browser', 'url': url, 'width': vp.get('width'),
358
+ 'returncode': 0 if outcome.get('success') else 1, 'executed': True, 'scope': scope,
359
+ 'output': json.dumps({k: v for k, v in outcome.items() if k != 'text'})[-4000:], 'time': time.time()}
360
+
361
+
263
362
  def clip(text, limit):
264
363
  """Shorten for display with a visible ellipsis, so a shortened command is never read (or rerun) as a complete one."""
265
364
  text = ' '.join(str(text or '').split())
@@ -1,5 +1,6 @@
1
1
  """Task-scoped tool disclosure; execution permissions remain runner-owned."""
2
2
  import os
3
+ from conversation import web_available
3
4
  GROUPS={
4
5
  'repository':{'tools':['read_file','read_files','parallel_reads','list_files','search_text','repo_map','symbols','context_read','impact','skill_load','tool_search'],'help':'read_file {path,start?,end?}; read_files {files:[{path,start?,end?}]}; search_text {query}; list_files {}; repo_map {}; symbols {query?,path?}; context_read {archive,start?,end?}. Prefer focused ranges and batch independent reads: parallel_reads {calls:[{name,arguments}]} runs up to eight read_file/search_text/list_files/repo_map/symbols/context_read/command_output calls at once, or send several read-only calls in one reply. skill_load {id,file?} loads a skill from the catalog (stack how-tos: setup, Vault variable names, verification). tool_search {query} returns full schemas of matching connected integration tools when the catalog lists names only.'},
5
6
  'planning':{'tools':['todo','task_contract','evidence','tool_catalog','schedule_wakeup','cannot_complete'],'help':'cannot_complete {reason} reports that the request cannot be done as asked (call once, then answer). schedule_wakeup {in_minutes|at,note} resumes this session later; {cancel:true} removes it. task_contract {} reads the current objective, requirements and revision. Use the task-contract/recovery protocol in workflow guidance. evidence {} reads sources and recorded claims. tool_catalog {groups:[name]} loads focused tool references; {} lists groups.'},
@@ -22,7 +23,9 @@ def groups(config):
22
23
  selected=set(config.get('_tool_groups',[]))|{'repository','planning'}
23
24
  # The Mac app's iOS Simulator pane is there when the app gave this run its address (worker/ios_simulator.py).
24
25
  if os.environ.get('MINI_SIM_URL') and config.get('mode')!='plan' and not config.get('subagent'):selected.add('simulator')
25
- if config.get('conversation_route')=='research':selected.add('research')
26
+ # Web search is part of every main-agent turn (a chat question about the weather needs it as much as a research
27
+ # turn), so the model knows from its first call that it can look things up instead of saying it has no web access.
28
+ if config.get('conversation_route')=='research' or web_available(config):selected.add('research')
26
29
  if config.get('mode')!='plan':selected|={'editing','decks'}
27
30
  # The Mac app's browser pane is there when the app gave this run its address (worker/browser_pane.py).
28
31
  if os.environ.get('MINI_PANE_URL') and config.get('mode')!='plan' and not config.get('subagent'):selected.add('pane')
@@ -132,10 +132,23 @@ def changed_paths(repo, base):
132
132
  return list(dict.fromkeys(changed)), deleted
133
133
 
134
134
 
135
- def diff_checks(repo, base, task=''):
135
+ def own_paths(repo, base, root=None):
136
+ """changed_paths, minus what was already changed or untracked when the turn started (runtime_control.save_start_dirty).
137
+ Tester report on 0.3.4: "Hi" in a folder with an untracked service-account key file was told by the done gate to remove
138
+ the "added" private key, so a greeting turned into repair rounds about a file the user had not asked about."""
139
+ changed, deleted = changed_paths(repo, base)
140
+ if root is None: return changed, deleted
141
+ try:
142
+ from runtime_control import turn_changes
143
+ mine = set(turn_changes(root, repo, list(changed) + list(deleted)))
144
+ except Exception: return changed, deleted
145
+ return [p for p in changed if p in mine], [p for p in deleted if p in mine]
146
+
147
+
148
+ def diff_checks(repo, base, task='', paths=None):
136
149
  """Reward-hacking signals in the working tree versus base: skipped/disabled tests, deleted test files, removed assertions, hard-coded outputs."""
137
150
  findings = []
138
- changed, deleted = changed_paths(repo, base)
151
+ changed, deleted = paths if paths is not None else changed_paths(repo, base)
139
152
  if not changed and not deleted: return findings
140
153
  tests_changed = [p for p in changed if TEST_PATH.search(p)]
141
154
  tests_deleted = [p for p in deleted if TEST_PATH.search(p)]
@@ -678,9 +691,13 @@ def notice(report):
678
691
  # 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.
679
692
  SOFT = frozenset(('uncited_behaviour', 'unacknowledged_change', 'contradicted_denial', 'remote_pending_claim'))
680
693
 
681
- def report(repo, base, final_text, commands, checks, task='', last_edit=None, edits=0, scope=None, record=None, earlier=None, asked=()):
682
- """Everything the gates need in one dict: findings, verdict, and the texts to show."""
694
+ def report(repo, base, final_text, commands, checks, task='', last_edit=None, edits=0, scope=None, record=None, earlier=None, asked=(), root=None):
695
+ """Everything the gates need in one dict: findings, verdict, and the texts to show. With root (the run folder), the
696
+ diff checks look only at what this turn changed, not at files that were already changed or untracked at its start."""
683
697
  findings = []
698
+ paths = None
699
+ try: paths = own_paths(repo, base, root)
700
+ except Exception: pass
684
701
  stale = []
685
702
  try: stale = stale_checks(checks, last_edit)
686
703
  except Exception: pass
@@ -696,10 +713,10 @@ def report(repo, base, final_text, commands, checks, task='', last_edit=None, ed
696
713
  except Exception: pass
697
714
  try: findings += empty_runs(fresh)
698
715
  except Exception: pass
699
- try: findings += diff_checks(repo, base, task)
716
+ try: findings += diff_checks(repo, base, task, paths)
700
717
  except Exception: pass
701
718
  files = None
702
- try: files = file_diffs(repo, base)
719
+ try: files = file_diffs(repo, base, *paths) if paths is not None else file_diffs(repo, base)
703
720
  except Exception: pass
704
721
  if files is not None:
705
722
  for check in (lambda: line_rules(files, task), lambda: check_weakening(files, task), lambda: invalid_json(repo, files), lambda: out_of_scope(files, scope), lambda: empty_change(edits, files, task) if in_git(repo) else [], lambda: forbidden_test_script(files, task)):
@@ -708,7 +725,7 @@ def report(repo, base, final_text, commands, checks, task='', last_edit=None, ed
708
725
  # Build integrity: navigation into pages that were never built, and a message that admits an open error yet says done.
709
726
  try:
710
727
  import site_check
711
- changed = changed_paths(repo, base)[0] if base else []
728
+ changed = (paths[0] if paths is not None else changed_paths(repo, base)[0]) if base else []
712
729
  if changed: findings += site_check.dead_links(repo, changed)
713
730
  findings += site_check.open_error_claim(final_text)
714
731
  except Exception: pass