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.
@@ -36,7 +36,35 @@ const ref=n=>'refs/turns/'+n;
36
36
  // BUG-0553: snapshots skip large files (videos, data dumps, archives) so a question does not copy them into
37
37
  // ~/.mlai/sessions every turn, and a folder too big to record is skipped as a whole.
38
38
  export const SNAPSHOT_FILE_LIMIT=5*1024*1024,SNAPSHOT_TOTAL_LIMIT=200*1024*1024;
39
- /** The current folder as a tree id. Files over SNAPSHOT_FILE_LIMIT are left out. */
39
+ // BUG-0617: secret files are left out too, as in the git checkpoints (worker/checkpoints.py SECRET_NAME and
40
+ // config_password): .env files, keys, credential and secrets files, and small files that hold a token or a password.
41
+ export const SECRET_FILE=/(^|\/)(\.env(\.(?!example$|sample$|template$|dist$)[^/]*)?|\.env[-_](?!example$|sample$|template$|dist$)[^/]*|\.envrc|\.dev\.vars(\.[^/]*)?|\.netrc|\.npmrc|\.pypirc|\.git-credentials|\.htpasswd|id_(rsa|dsa|ecdsa|ed25519)[^/]*|credentials(\.(json|ya?ml|toml|ini|xml))?|[^/]*secrets?\.(toml|ya?ml|json|ini|cfg|conf|properties|env)|database\.ya?ml|[^/]*\.(pem|key|p12|pfx|keystore|jks))$/i;
42
+ const SECRET_TEXT=[/-----BEGIN (?:[A-Z]+ )?PRIVATE KEY-----/,/\b(?:github_pat_[A-Za-z0-9_]{40,}|gh[pousr]_[A-Za-z0-9]{30,})\b/,/\bsk-[A-Za-z0-9_-]{20,}/,/\b(?:sk|rk)_(?:live|test)_[A-Za-z0-9]{16,}\b/,
43
+ /\bxox[abposr]-[A-Za-z0-9-]{10,}\b/,/\b(?:AKIA|ASIA)[A-Z0-9]{16}\b/,/\bAIza[A-Za-z0-9_-]{35}\b/,/\bnpm_[A-Za-z0-9]{36}\b/,/\bhf_[A-Za-z0-9]{30,}\b/,/\bglpat-[A-Za-z0-9_-]{20,}\b/,/\beyJ[A-Za-z0-9_-]{10,}\.eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{20,}/];
44
+ const SECRET_ASSIGN=/\b[A-Z][A-Z0-9_]{1,62}(?:KEY|TOKEN|SECRET|PASSWORD|PASSWD|CREDENTIALS?)\s*[:=]\s*["']?([^\s"'`]{8,})/g;
45
+ const CONFIG_FILE=/\.(toml|ya?ml|json|ini|cfg|conf|properties|xml|env|vars|plist)$|(^|\/)\.[^/.]+rc$/i;
46
+ const CONFIG_PASSWORD=/(?:^|[{,])[ \t-]*["']?[\w.-]*(?:password|passwd|pwd|passphrase|secret|token|api[_-]?key|private[_-]?key|access[_-]?key)["']?[ \t]*[:=][ \t]*["']?([^\s"'#,;}<>]{4,})/gim;
47
+ const PLACEHOLDER=/^(?:x+|\*+|<[^>]*>|\$\{?[A-Za-z_][A-Za-z0-9_]*\}?|\{\{.*|%\(.*|your[-_].*|changeme|change[-_]me|example.*|placeholder|null|none|nil|undefined|true|false|\.\.\.|env\(.*|ENV\[.*|process\.env\..*|os\.environ.*|import\.meta\.env\..*)$/i;
48
+ /** True when the text of `rel` holds a credential: a known token format, KEY=value, or a password in a config file. */
49
+ export function secretText(rel,text){
50
+ if(!text)return false;
51
+ if(SECRET_TEXT.some(rx=>rx.test(text)))return true;
52
+ for(const m of text.matchAll(SECRET_ASSIGN)){const v=m[1].replace(/[.,;)\]}]+$/,'');if(!PLACEHOLDER.test(v)&&/[0-9]/.test(v)&&/[A-Za-z]/.test(v))return true}
53
+ if(CONFIG_FILE.test(rel))for(const m of text.matchAll(CONFIG_PASSWORD))if(!PLACEHOLDER.test(m[1]))return true;
54
+ return false;
55
+ }
56
+ const SCAN_LIMIT=512*1024,scanned=new Map();// path -> {key,secret}: unchanged files are not read again every turn
57
+ async function secretFile(root,f,st){
58
+ if(SECRET_FILE.test(f))return true;
59
+ if(!st?.isFile()||!st.size||st.size>SCAN_LIMIT)return false;
60
+ const full=path.join(root,f),key=st.size+':'+st.mtimeMs,hit=scanned.get(full);
61
+ if(hit?.key===key)return hit.secret;
62
+ const buf=await readFile(full).catch(()=>null);
63
+ const secret=!!buf&&!buf.subarray(0,8000).includes(0)&&secretText(f,buf.toString('utf8'));
64
+ if(scanned.size>20000)scanned.clear();
65
+ scanned.set(full,{key,secret});return secret;
66
+ }
67
+ /** The current folder as a tree id. Files over SNAPSHOT_FILE_LIMIT and secret files are left out. */
40
68
  async function tree(session){
41
69
  const root=session.meta.root;
42
70
  const list=(await git(session,['ls-files','-z','-c','-o','--exclude-standard'])).split('\0').filter(Boolean);
@@ -44,6 +72,7 @@ async function tree(session){
44
72
  for(const f of [...new Set(list)]){
45
73
  const st=await lstat(path.join(root,f)).catch(()=>null);
46
74
  if(st&&st.isFile()&&st.size>SNAPSHOT_FILE_LIMIT){skip.push(f);continue}
75
+ if(await secretFile(root,f,st)){skip.push(f);continue}
47
76
  total+=st?.isFile()?st.size:0;keep.push(f);
48
77
  }
49
78
  if(total>SNAPSHOT_TOTAL_LIMIT)throw Object.assign(new Error('folder too large to snapshot'),{code:'TOO_LARGE'});
@@ -100,10 +100,23 @@ export class Transcript{
100
100
  }
101
101
  todos(items){
102
102
  const t=this.t;if(!Array.isArray(items)||!items.length)return [];
103
+ // Tester report (CLI 0.3.8): the same plan was printed twice in a row. A list repeated with nothing shown since, or
104
+ // identical to the last one shown, adds nothing.
105
+ const names=JSON.stringify(items.slice(0,30).map(it=>String(it?.text??'')));
106
+ const full=JSON.stringify(items.slice(0,30).map(it=>[String(it?.text??''),it?.status||'']));
107
+ const repeat=full===this.lastTodos||(names===this.lastTodoNames&&!this.shownSinceTodos);
108
+ this.lastTodos=full;this.lastTodoNames=names;
109
+ if(repeat)return [];
110
+ this.shownSinceTodos=false;
103
111
  return [this.dot(t.tool)+t.toolHead('Plan'),...items.slice(0,30).map(it=>{const s=it.status;const mark=s==='done'?t.ok('[x]'):s==='in_progress'?t.accent('[>]'):t.dim('[ ]');return ' '+mark+' '+(s==='done'?t.faint(truncate(it.text,this.cols-10)):truncate(String(it.text),this.cols-10))})];
104
112
  }
105
113
  /** Lines for one event; [] for events that stay quiet. */
106
114
  lines(e){
115
+ const out=this.eventLines(e);
116
+ if(out.length&&e?.type!=='todos')this.shownSinceTodos=true;
117
+ return out;
118
+ }
119
+ eventLines(e){
107
120
  const t=this.t;
108
121
  switch(e?.type){
109
122
  case 'assistant':return this.assistant(e.content);
@@ -133,7 +146,8 @@ export function answerRest(shown,p){
133
146
  const norm=x=>String(x).replace(/\s+/g,' ').trim(),list=(shown||[]).filter(Boolean);
134
147
  if(list.some(seen=>norm(seen)===norm(p)))return '';
135
148
  // The longest earlier reply it contains, so "A" then "A + END" then answer "A + END" adds nothing the third time.
136
- const inside=list.filter(seen=>seen.length>40&&p.includes(seen)).sort((a,b)=>b.length-a.length)[0];
149
+ // BUG-0613: so does a short reply ('CHARLIE-LINE') the answer starts with, followed by a note on its own line.
150
+ const inside=list.filter(seen=>(seen.length>40&&p.includes(seen))||(p.startsWith(seen)&&/^[ \t]*\n/.test(p.slice(seen.length)))).sort((a,b)=>b.length-a.length)[0];
137
151
  return inside?p.replace(inside,'').trim():p;
138
152
  }
139
153
 
package/src/two-words.mjs CHANGED
@@ -9,12 +9,21 @@ const ACRONYM={api:'API',apis:'APIs',css:'CSS',html:'HTML',ui:'UI',ux:'UX',ci:'C
9
9
  const INTENT=[[/\b(fix|fixe[sd]|fixing|bug|bugs|broken|error|errors|crash\w*|fail\w*|issue|debug\w*|repair)\b/i,'Fix'],[/\b(build|create|make|add|implement\w*|scaffold|generate|write|set ?up|new)\b/i,'Build'],
10
10
  [/\b(review\w*|check|audit|inspect|look|analy[sz]e)\b/i,'Review'],[/\?\s*$|^\s*(what|how|why|when|where|who|explain|tell)\b/i,'Chat']];
11
11
  const word=(t)=>{const k=t.toLowerCase();if(ACRONYM[k])return ACRONYM[k];if(/#|[A-Z]/.test(t.slice(1)))return t;const c=[...t];return c[0].toLocaleUpperCase()+c.slice(1).join('')};
12
+ // BUG-0681: a key or password typed in a task never becomes part of its automatic name (titles are listed in mlai sessions and the web app).
13
+ const KEY_PREFIX=/^(?:(?:sk|pk|rk|whsec|ghp|gho|ghu|ghs|ghr|github_pat|glpat|xox[abposr]|hf|npm|pypi)[-_](?=[\w.-]*\d)[\w.-]{6,}|(?:AKIA|ASIA)[A-Z0-9]{12,}|AIza[\w-]{20,}|ya29\.[\w.-]{10,}|eyJ[\w-]{10,})/;
14
+ const LABEL=/^(passwords?|passwd|pwd|passcode|passphrase|pin|tokens?|secrets?|keys?|apikey|api_key|access_key|secret_key|credentials?)$/i;
15
+ const secretish=(t)=>KEY_PREFIX.test(t)||t.split(/[-_.]+/).some(p=>p.length>=10&&/\p{L}/u.test(p)&&/\p{N}/u.test(p));
16
+ const valueish=(t)=>/[\p{N}_=:$!%*+-]/u.test(t)||/\p{Ll}\p{Lu}/u.test(t);
12
17
  function tokens(text){
13
18
  const s=String(text??'').replace(/```[\s\S]*?(```|$)/g,' ').replace(/`/g,' ').replace(/\b(https?:\/\/|www\.)\S+/gi,' ').replace(/['\u2019](s|t|re|ll|d|m|ve)\b/gi,'');
14
19
  const out=[];
15
20
  const prose=/\s/.test(s.trim());
21
+ let label=false; // the word after password, token, key... is not a title word when it looks like a value
16
22
  for(const raw of s.split(/\s+/)){
17
23
  if(!raw||/[\/\\]|^[~.@]|@|^\[(image|file|attachment)/i.test(raw))continue;
24
+ const bare=raw.replace(/^[^\p{L}\p{N}]+|[^\p{L}\p{N}]+$/gu,'');
25
+ if(/^[\w.-]*(pass|pwd|token|secret|key|pin)\w*\s*[=:]\s*\S/i.test(bare)||secretish(bare)||label&&valueish(bare)){label=false;continue}
26
+ if(LABEL.test(bare)){label=true}else if(bare&&!STOP.has(bare.toLowerCase()))label=false;
18
27
  // a file name counts as its base name: notes.txt is Notes, never Txt
19
28
  const t=raw.replace(/^[^\p{L}\p{N}#]+|[^\p{L}\p{N}]+$/gu,'').replace(/^([\p{L}\p{N}_-]+)\.[a-z][a-z0-9]{0,4}$/iu,'$1');if(!t)continue;
20
29
  const parts=/\p{N}|#/u.test(t)&&/\p{L}/u.test(t)||prose&&/^\p{L}{2,}-\p{L}{2,}$/u.test(t)?[t]:t.split(/[^\p{L}\p{N}]+/u); // in a sentence, long-running stays one word; slugs split
@@ -64,15 +64,15 @@ function baseUrl(){return signIn().url}
64
64
 
65
65
  // ---------- http ----------
66
66
  class ApiError extends Error{constructor(message,status,code){super(message);this.status=status;this.code=code}}
67
- async function api(path,body,{auth=true,method='POST'}={}){
67
+ async function api(path,body,{auth=true,method='POST',signal}={}){
68
68
  const cfg=signIn();const headers={'Content-Type':'application/json','User-Agent':'mlai-cli/'+VERSION};
69
69
  if(auth){if(!cfg.token)throw new ApiError('Not signed in. Run `mlai login` first.',401,'no_token');headers.Authorization='Bearer '+cfg.token}
70
- const r=await fetch(baseUrl()+path,{method,headers,body:body?JSON.stringify(body):undefined});
70
+ const r=await fetch(baseUrl()+path,{method,headers,body:body?JSON.stringify(body):undefined,signal});
71
71
  const text=await r.text();let data;try{data=JSON.parse(text)}catch{data={error:text.slice(0,200)||r.statusText}}
72
72
  if(!r.ok||data.error){if(data.code==='cli_token_invalid')throw new ApiError('Your CLI token expired or was rejected. Run `mlai login` again.',401,data.code);throw new ApiError(data.error||('HTTP '+r.status),r.status,data.code)}
73
73
  return data;
74
74
  }
75
- const ws=(token,action,extra={})=>api('/api/workspace',{action,token,...extra});
75
+ const ws=(token,action,extra={},opts={})=>api('/api/workspace',{action,token,...extra},opts);
76
76
 
77
77
  // ---------- output helpers ----------
78
78
  function out(s=''){process.stdout.write(s+'\n')}
@@ -197,25 +197,42 @@ commands.whoami=async()=>{
197
197
  const si=signIn();if(si.env&&si.token.startsWith('mlaid_')){emit({server:si.url,signIn:'mlai login'});if(!JSON_OUT)out(c.bold('Signed in')+' with mlai login'+c.dim(' · '+si.url+' · run mlai whoami for the account'));return}
198
198
  const me=await api('/api/cli',{action:'whoami'});emit(me);if(!JSON_OUT){out(c.bold('Signed in')+' as '+me.label+c.dim(' · owner '+me.owner.slice(0,8)+'… · '+me.server));out(c.dim('Token expires '+new Date(me.expires).toLocaleString()))}};
199
199
 
200
- async function listProjects(){const acct=await api('/api/account',null,{method:'GET'});if(!acct.configured)throw new ApiError('This server has no account database; projects cannot be listed.',503);return (acct.workspaces||[]).sort((a,b)=>(b.created||0)-(a.created||0))}
200
+ // BUG-0660: the account also lists the chats of local mlai sessions (backend laptop); they are not cloud projects.
201
+ // The server pages cloud projects (?scope=cloud&offset=N, 200 a page); a server without paging returns the same list each time,
202
+ // so paging stops when a page adds nothing new.
203
+ export const PROJECT_PAGE=200;
204
+ async function listProjects(){
205
+ const seen=new Map();
206
+ for(let page=0;page<10;page++){
207
+ const acct=await api('/api/account?scope=cloud&limit='+PROJECT_PAGE+'&offset='+page*PROJECT_PAGE,null,{method:'GET'});
208
+ if(!acct.configured)throw new ApiError('This server has no account database; projects cannot be listed.',503);
209
+ const rows=acct.workspaces||[];let added=0;for(const w of rows)if(w?.name&&!seen.has(w.name)){seen.set(w.name,w);added++}
210
+ if(!added||rows.filter(w=>!w.shared).length<PROJECT_PAGE)break;
211
+ }
212
+ return [...seen.values()].filter(w=>w.backend!=='laptop').sort((a,b)=>(b.created||0)-(a.created||0));
213
+ }
214
+ export const projectName=w=>w.title||w.label||w.repo||w.name;
201
215
  commands.projects=async()=>{
202
216
  const list=await listProjects();emit(list);if(JSON_OUT)return;
203
217
  if(!list.length){out('No projects yet. Create one with `mlai new` or `mlai new https://github.com/org/repo`.');return}
204
218
  const linked=linkedProject()?.token;
205
- list.forEach((w,i)=>out(`${c.dim('#'+(i+1))} ${w.token===linked?c.teal('● '):' '}${c.bold(w.label||w.repo||w.name)} ${c.dim((w.repo?w.repo+' · ':'')+'created '+timeAgo(w.created))}`));
219
+ list.forEach((w,i)=>out(`${c.dim('#'+(i+1))} ${w.token===linked?c.teal('● '):' '}${c.bold(projectName(w))} ${c.dim((w.repo?w.repo+' · ':'')+'created '+timeAgo(w.created))}`));
206
220
  out(c.dim('\n● linked to this folder. `mlai link #n` to change.'));
207
221
  };
208
222
  commands.link=async opts=>{
209
223
  const want=String(opts._[0]||'');if(!want)fail('Usage: mlai link <name|#n>');
210
- const list=await listProjects();const w=want.startsWith('#')?list[Number(want.slice(1))-1]:list.find(x=>x.name===want||x.label===want||x.repo===want||x.token===want);
211
- if(!w)fail('No project matched '+want);saveLink({token:w.token,name:w.name,label:w.label||w.repo||w.name});out(c.green('Linked ')+process.cwd()+' → '+(w.label||w.name));
224
+ const list=await listProjects();const w=want.startsWith('#')?list[Number(want.slice(1))-1]:list.find(x=>x.name===want||x.title===want||x.label===want||x.repo===want||x.token===want);
225
+ if(!w)fail('No project matched '+want);saveLink({token:w.token,name:w.name,label:projectName(w)});out(c.green('Linked ')+process.cwd()+' → '+projectName(w));
212
226
  };
213
227
  commands.new=async opts=>{
214
228
  const repo=String(opts._[0]||'');if(repo&&!/^https:\/\/github\.com\/[\w.-]+\/[\w.-]+/.test(repo))fail('Pass a GitHub repository URL, e.g. https://github.com/org/repo');
215
229
  if(!repo&&!opts.yes&&TTY){const a=await ask('Create an empty project? [Y/n] ');if(/^n/i.test(a))return}
216
230
  process.stdout.write(c.dim('Creating workspace…'));
217
231
  const w=await api('/api/workspace',{action:'create',repo,backend:opts.backend,profile:opts.profile||'large',fit:!opts.profile});
218
- process.stdout.write('\r');emit(w);
232
+ process.stdout.write('\r');
233
+ // BUG-0660: the name is stored with the project, so `mlai projects` and `mlai link <name>` find it.
234
+ if(opts.name){const named=await ws(w.token,'rename',{title:String(opts.name)}).catch(()=>null);if(named?.title)w.title=named.title}
235
+ emit(w);
219
236
  saveLink({token:w.token,name:w.name,label:opts.name||w.label||repo.split('/').slice(-2).join('/')||w.name});
220
237
  if(!JSON_OUT)out(c.green('Project ready ')+(repo||'(empty)')+c.dim(' · linked to '+process.cwd()));
221
238
  };
@@ -339,7 +356,10 @@ commands.reply=async opts=>{const t=requireProject(opts);const text=opts._.join(
339
356
 
340
357
  // ----- code -----
341
358
  commands.ls=async opts=>{const snap=await ws(requireProject(opts),'status');emit(snap.files);if(!JSON_OUT)out((snap.files||[]).join('\n'))};
342
- commands.cat=async opts=>{const f=opts._[0];if(!f)fail('Usage: mlai cat <path>');const r=await ws(requireProject(opts),'read',{path:f});process.stdout.write(r.content||'')};
359
+ commands.cat=async opts=>{const f=opts._[0];if(!f)fail('Usage: mlai cat <path>');const token=requireProject(opts);const r=await ws(token,'read',{path:f});
360
+ // BUG-0668: the workspace answers a missing file with empty content; tell it apart from an empty file.
361
+ if(!r.content){const raw=await ws(token,'read_raw',{path:f}).catch(e=>/not found/i.test(e.message)?null:{size:0});if(!raw)fail('No file '+f+' in the workspace.')}
362
+ process.stdout.write(r.content||'')};
343
363
  commands.diff=async opts=>{const r=await ws(requireProject(opts),'diff');emit(r);if(JSON_OUT)return;if(!String(r.diff||'').trim()){out(c.dim('No changes.'));return}
344
364
  if(TTY)for(const line of String(r.diff).split('\n'))out(line.startsWith('+')&&!line.startsWith('+++')?c.green(line):line.startsWith('-')&&!line.startsWith('---')?c.red(line):line.startsWith('@@')?c.teal(line):line.startsWith('diff ')?c.bold(line):line);else out(r.diff);
345
365
  if(r.skipped?.length)out(c.dim('Skipped large/untracked files: '+r.skipped.join(', ')))};
@@ -352,7 +372,18 @@ commands.pull=async opts=>{
352
372
  const apply=spawnSync('git',['apply','--3way'],{input:patch,encoding:'utf8'});if(apply.status!==0){process.stderr.write(apply.stderr);fail('git apply failed')}
353
373
  const stat=spawnSync('git',['diff','--stat'],{encoding:'utf8'}).stdout;out(c.green('Applied workspace changes to this repo.')+'\n'+stat);
354
374
  };
355
- commands.sh=async opts=>{const cmd=opts._.join(' ');if(!cmd)fail('Usage: mlai sh "<command>"');const r=await ws(requireProject(opts),'terminal',{command:cmd});process.stdout.write(r.output||'');process.exitCode=r.exitCode||0};
375
+ // BUG-0659: commands are stopped after 90 seconds in the workspace. A command that leaves a background process behind can
376
+ // keep the request open past that, so the request is given up a little later and the command (and its children) stopped.
377
+ export const SH_LIMIT_S=90;
378
+ commands.sh=async opts=>{const cmd=opts._.join(' ');if(!cmd)fail('Usage: mlai sh "<command>"');const token=requireProject(opts);
379
+ const id=Array.from(randomBytes(12),b=>'abcdefghijklmnopqrstuvwxyz0123456789'[b%36]).join('');
380
+ const ac=new AbortController();const wait=Number(process.env.MLAI_SH_WAIT_MS)||(SH_LIMIT_S+5)*1000;
381
+ const timer=setTimeout(()=>ac.abort(),wait);
382
+ let r;try{r=await ws(token,'terminal',{command:cmd,id},{signal:ac.signal})}
383
+ catch(e){if(!ac.signal.aborted)throw e;await ws(token,'terminal_stop',{id}).catch(()=>{});
384
+ process.stderr.write(ce.red('error: ')+'The command ran past the '+SH_LIMIT_S+' second limit and was stopped.\n');process.exitCode=124;return}
385
+ finally{clearTimeout(timer)}
386
+ process.stdout.write(r.output||'');process.exitCode=r.exitCode||0};
356
387
  commands.push=async opts=>{const m=opts.message||opts.mode;if(!m)fail('Usage: mlai push -m "commit message"');const r=await ws(requireProject(opts),'git_push',{confirm:true,message:m});emit(r);if(!JSON_OUT)out(c.green('Pushed')+(r.branch?' to '+r.branch:'')+(r.url?' · '+r.url:''))};
357
388
  commands.pr=async opts=>{const t=requireProject(opts);const title=opts.title;if(!title)fail('Usage: mlai pr -t "title" [-b "body"]');const plan=await ws(t,'pr_plan');if(!opts.yes&&TTY){out(c.bold('Files in this PR'));for(const f of plan.files||plan.changes||[])out(' '+(f.path||f));const a=await ask('Open a draft PR? [Y/n] ');if(/^n/i.test(a))return}const r=await ws(t,'pr_publish',{confirm:true,title,body:opts.body||'',planHash:plan.planHash});emit(r);if(!JSON_OUT)out(c.green('Draft PR ')+(r.url||JSON.stringify(r)))};
358
389
 
@@ -42,6 +42,37 @@ def as_preview(value):
42
42
  u=urlparse(value)
43
43
  return u._replace(netloc=PREVIEW_HOST+':3000').geturl() if u.hostname in ('127.0.0.1','localhost') else value
44
44
 
45
+ FOREIGN=('{who} This check opened another application, not this project, so it is not evidence for this workspace. '
46
+ '{hint}')
47
+
48
+ def workspace_server(url):
49
+ """Who serves the checked address, for this workspace (MINI_REPO / MINI_ROOT): see preview.server_owner. CLI 0.3.8: port
50
+ 3000 served another workspace's app and its checks passed as the Notes app's evidence."""
51
+ repo,state=os.getenv('MINI_REPO'),os.getenv('MINI_ROOT')
52
+ if not repo or not Path(repo).is_dir():return {}
53
+ try:
54
+ import preview
55
+ port=urlparse(url).port or 80
56
+ return preview.server_owner(port,repo,state)
57
+ except Exception:return {}
58
+
59
+ def preview_url():
60
+ """This workspace's preview address (laptop previews move off port 3000 when another app holds it)."""
61
+ try:
62
+ import preview
63
+ return preview.base_url(preview.recorded_port(os.getenv('MINI_ROOT') or '/nonexistent')) if on_laptop() else 'http://127.0.0.1:3000'
64
+ except Exception:return 'http://127.0.0.1:3000'
65
+
66
+ def foreign_result(owner,url,width,height,timeline=None):
67
+ try:
68
+ import preview
69
+ who=preview.foreign_text(owner)
70
+ except Exception:who='Port %s is served by another app.'%owner.get('port')
71
+ mine=preview_url()
72
+ hint=('Use this project\'s preview at '+mine+' (start it with preview first).') if urlparse(mine).port!=owner.get('port') else 'Start the preview of this project first (it reports the address to check).'
73
+ return {'success':False,'workspaceEvidence':False,'foreignServer':owner,'error':FOREIGN.format(who=who,hint=hint),'checkedAt':url,'url':url,'timeline':timeline or [],
74
+ 'pageErrors':[],'consoleErrors':[],'networkFailures':[],'screenshot':None,'viewport':{'width':width,'height':height}}
75
+
45
76
  HYDRATION='''() => {
46
77
  const next = !!document.querySelector('script[src*="/_next/"]');
47
78
  if (!next) return {framework: null, interactive: true};
@@ -85,9 +116,7 @@ def scenario(args,output):
85
116
  if not (local and os.getenv('PLAYWRIGHT_BROWSERS_PATH')):os.environ['PLAYWRIGHT_BROWSERS_PATH']=str(output.parent.parent/'pw-browsers')
86
117
  # Windows bug list (round 6): a missing module surfaced as a bare "No module named 'fcntl'" and the agent spent many
87
118
  # calls building its own Puppeteer harness. A runtime that cannot start now says so and what to do instead.
88
- try:from playwright.sync_api import sync_playwright
89
- except ImportError as exc:raise unavailable(exc) from exc
90
- url=args.get('url','http://127.0.0.1:3000')
119
+ url=args.get('url') or preview_url()
91
120
  if not local_url(url):raise ValueError('Browser is restricted to the local app on this computer' if local else 'Browser is restricted to the local app on port 3000')
92
121
  url=as_preview(url)
93
122
  steps=args.get('steps',[])
@@ -95,6 +124,10 @@ def scenario(args,output):
95
124
  width=args.get('width',1280);height=args.get('height',800)
96
125
  if type(width)!=int or type(height)!=int or not 320<=width<=1920 or not 400<=height<=1200:raise ValueError('Unsupported viewport')
97
126
  errors=[];console=[];noise=[];timeline=[];network=[];page=None
127
+ owner=workspace_server(url)
128
+ if owner.get('owner')=='foreign':return foreign_result(owner,url,width,height)
129
+ try:from playwright.sync_api import sync_playwright
130
+ except ImportError as exc:raise unavailable(exc) from exc
98
131
  with sync_playwright() as p:
99
132
  try:browser=p.chromium.launch(headless=True,args=browser_local.launch_args() if local else ['--no-sandbox','--host-resolver-rules=MAP '+PREVIEW_HOST+' 127.0.0.1','--unsafely-treat-insecure-origin-as-secure=http://'+PREVIEW_HOST+':3000'])
100
133
  except Exception as exc:raise unavailable(exc) from exc
@@ -128,7 +161,12 @@ def scenario(args,output):
128
161
  interactive=hydrated(page)
129
162
  if interactive is False:errors.insert(0,NOT_INTERACTIVE)
130
163
  checked=actual(page,checked,local)
131
- return {'success':not errors,'interactive':interactive,'checkedAt':checked,**({'requestedUrl':url} if checked!=url and local else {}),'timeline':timeline,'url':page.url,'title':page.title(),'text':page.locator('body').inner_text()[:16000],'pageErrors':errors[:20],'consoleErrors':console[:20],**({'ignoredExtensionNoise':noise[:5]} if noise else {}),'networkFailures':network[:30],'horizontalOverflow':page.evaluate('document.documentElement.scrollWidth>innerWidth'),'screenshot':output.name,'viewport':{'width':width,'height':height}}
164
+ ended=page.url
165
+ if urlparse(ended).port!=urlparse(url).port:
166
+ moved=workspace_server(ended)
167
+ if moved.get('owner')=='foreign':return foreign_result(moved,ended,width,height,timeline)
168
+ owner=moved or owner
169
+ return {'success':not errors,**({'workspaceEvidence':True} if owner.get('owner') in ('ours','workspace') else {}),'interactive':interactive,'checkedAt':checked,**({'requestedUrl':url} if checked!=url and local else {}),'timeline':timeline,'url':page.url,'title':page.title(),'text':page.locator('body').inner_text()[:16000],'pageErrors':errors[:20],'consoleErrors':console[:20],**({'ignoredExtensionNoise':noise[:5]} if noise else {}),'networkFailures':network[:30],'horizontalOverflow':page.evaluate('document.documentElement.scrollWidth>innerWidth'),'screenshot':output.name,'viewport':{'width':width,'height':height}}
132
170
  except Exception as exc:
133
171
  if timeline and timeline[-1]['state']=='started':timeline[-1]['state']='failed'
134
172
  captured=False
@@ -39,7 +39,20 @@ def tree_of_worktree(repo):
39
39
 
40
40
  UNTRACKED_LIMIT = 2 * 1024 * 1024
41
41
  HEAVY_DIR = re.compile(r'(^|/)(node_modules|\.venv|venv|__pycache__|\.next|\.turbo|\.cache|target|dist|build)/')
42
- SECRET_NAME = re.compile(r'(^|/)(\.env(\.(?!example$|sample$|template$|dist$)[^/]*)?|\.netrc|\.npmrc|\.pypirc|id_(rsa|dsa|ecdsa|ed25519)[^/]*|credentials(\.json)?|[^/]*\.(pem|key|p12|pfx|keystore|jks))$', re.I)
42
+ # BUG-0657: also the usual homes of plain passwords (.streamlit/secrets.toml, config/database.yml, .envrc, .dev.vars,
43
+ # .env-local), and config files whose values are plain passwords (secret_scan only knows token formats).
44
+ SECRET_NAME = re.compile(r'(^|/)(\.env(\.(?!example$|sample$|template$|dist$)[^/]*)?|\.env[-_](?!example$|sample$|template$|dist$)[^/]*|\.envrc|\.dev\.vars(\.[^/]*)?'
45
+ r'|\.netrc|\.npmrc|\.pypirc|\.git-credentials|\.htpasswd|id_(rsa|dsa|ecdsa|ed25519)[^/]*|credentials(\.(json|ya?ml|toml|ini|xml))?'
46
+ r'|[^/]*secrets?\.(toml|ya?ml|json|ini|cfg|conf|properties|env)|database\.ya?ml|[^/]*\.(pem|key|p12|pfx|keystore|jks))$', re.I)
47
+ CONFIG_FILE = re.compile(r'\.(toml|ya?ml|json|ini|cfg|conf|properties|xml|env|vars|plist)$|(^|/)\.[^/.]+rc$', re.I)
48
+ CONFIG_PASSWORD = re.compile(r'''(?im)(?:^|[{,])[ \t-]*["']?[\w.-]*(?:password|passwd|pwd|passphrase|secret|token|api[_-]?key|private[_-]?key|access[_-]?key)["']?[ \t]*[:=][ \t]*["']?([^\s"'#,;}<>]{4,})''')
49
+ SECRET_PLACEHOLDER = re.compile(r'^(?:x+|\*+|\$\{?[A-Za-z_][A-Za-z0-9_]*\}?|\{\{.*|%\(.*|your[-_].*|changeme|change[-_]me|example.*|placeholder|null|none|nil|undefined|true|false|\.\.\.|env\(.*|ENV\[.*)$', re.I)
50
+
51
+
52
+ def config_password(rel, text):
53
+ """True when a config file (TOML, YAML, JSON, INI...) sets a password, token or key to a literal value."""
54
+ if not CONFIG_FILE.search(rel): return False
55
+ return any(not SECRET_PLACEHOLDER.match(m.group(1)) for m in CONFIG_PASSWORD.finditer(text[:2_000_000]))
43
56
 
44
57
  def _has_commit(path):
45
58
  r = subprocess.run(['git', 'rev-parse', '-q', '--verify', 'HEAD'], cwd=path, capture_output=True, text=True, timeout=15,
@@ -94,8 +107,15 @@ def _safe_file(repo, rel, secret_scan):
94
107
  if secret_scan and st.st_size and os.path.isfile(full) and not os.path.islink(full):
95
108
  try:
96
109
  with open(full, 'r', encoding='utf-8') as fh:
97
- if secret_scan.scan_text(fh.read()):
98
- return False
110
+ text = fh.read()
111
+ if secret_scan.scan_text(text) or config_password(rel, text):
112
+ return False
113
+ except (UnicodeDecodeError, OSError):
114
+ pass
115
+ elif st.st_size and os.path.isfile(full) and not os.path.islink(full):
116
+ try:
117
+ with open(full, 'r', encoding='utf-8') as fh:
118
+ if config_password(rel, fh.read()): return False
99
119
  except (UnicodeDecodeError, OSError):
100
120
  pass
101
121
  return True
@@ -51,6 +51,16 @@ def git_args(args):
51
51
  return list(args[i:])
52
52
 
53
53
 
54
+ PM_CONFIG_WRITE = frozenset(('set', 'delete', 'rm', 'unset', 'edit', 'fix', 'add'))
55
+
56
+
57
+ def pm_config_change(prog, positional):
58
+ """npm/pnpm/yarn/bun config set|delete|edit, npm set/unset, pip config set|unset|edit (BUG-0714)."""
59
+ if not positional: return False
60
+ if positional[0] == 'config': return len(positional) > 1 and positional[1] in PM_CONFIG_WRITE
61
+ return prog in ('npm', 'pnpm', 'yarn') and positional[0] in ('set', 'unset') and len(positional) > 1
62
+
63
+
54
64
  GIT_WRITE = frozenset(('add', 'commit', 'checkout', 'switch', 'restore', 'merge', 'rebase', 'stash', 'cherry-pick', 'revert', 'mv', 'rm', 'init', 'apply', 'am', 'worktree', 'submodule', 'notes', 'bisect', 'update-index', 'reset'))
55
65
  GIT_NETWORK = frozenset(('push', 'fetch', 'pull', 'clone', 'ls-remote', 'remote_update', 'lfs'))
56
66
  NETWORK_PROGRAMS = frozenset(('curl', 'wget', 'ssh', 'scp', 'sftp', 'rsync_remote', 'nc', 'ncat', 'netcat', 'telnet', 'ftp', 'gh', 'glab', 'hub', 'aws', 'gcloud', 'az', 'vercel', 'netlify', 'wrangler',
@@ -390,6 +400,11 @@ def evaluate(words, redirects, flags, repo, cwd, home, body_programs=None):
390
400
  if sub == 'clean' and any(o.startswith('-') and ('f' in o or 'x' in o or 'd' in o) and not o.startswith('--') for o in rest) or (sub == 'clean' and '--force' in rest): return 'destructive', 'git clean removes untracked files', []
391
401
  if sub in ('checkout', 'restore') and (('.' in rest) or ('--' in rest and rest[-1] in ('.', '*')) or any(o in ('--source', '-s') for o in rest) and '.' in rest or (sub == 'checkout' and '--' in rest and len(rest) > rest.index('--') + 1 and rest[rest.index('--') + 1] in ('.', '*'))): return 'destructive', 'git ' + sub + ' . discards working changes', []
392
402
  if sub == 'branch' and any(o in ('-D', '--delete', '-d') for o in rest): return 'destructive', 'deletes a branch', []
403
+ # BUG-0713: overwriting or force-moving a branch loses its commits like branch -D or reset --hard.
404
+ if sub == 'branch' and any(o in ('-M', '-f', '--force', '-C') or re.fullmatch(r'-[a-zA-Z]*[MfC][a-zA-Z]*', o) and not o.startswith('--') for o in rest): return 'destructive', 'overwrites or moves a branch', []
405
+ if sub == 'branch' and any(o in ('-m', '--move', '-c', '--copy', '-u', '--set-upstream-to', '--unset-upstream', '--edit-description') or o.startswith('--set-upstream-to=') for o in rest): return 'write', 'renames or changes a branch', []
406
+ if sub in ('checkout', 'switch') and any(o in ('-B', '-C', '--force-create') for o in rest): return 'destructive', 'git ' + sub + ' ' + next(o for o in rest if o in ('-B', '-C', '--force-create')) + ' resets an existing branch', []
407
+ if sub == 'update-ref' or sub == 'symbolic-ref' and len([r for r in rest if not r.startswith('-')]) >= 2: return 'destructive', 'git ' + sub + ' moves or deletes a branch', []
393
408
  if sub == 'tag' and any(o in ('-d', '--delete') for o in rest): return 'destructive', 'deletes a tag', []
394
409
  if sub == 'stash' and rest and rest[0] in ('drop', 'clear'): return 'destructive', 'drops stashed work', []
395
410
  if sub in ('rebase', 'filter-branch', 'filter-repo', 'replace') or (sub == 'commit' and '--amend' in rest): return 'destructive', 'git ' + sub + ' rewrites history', []
@@ -456,6 +471,8 @@ def evaluate(words, redirects, flags, repo, cwd, home, body_programs=None):
456
471
  # A version or help check is not an install (BUG-0223: `npm -v` asked "Approve package install · npm install").
457
472
  if sub == '' and any(a in ('-v', '-V', '--version', '-h', '--help', 'help') for a in args): return 'read', '', []
458
473
  if sub in INSTALLERS[prog] or (sub == '' and prog in ('npm', 'pnpm', 'yarn', 'bun')): return 'install', prog + ' ' + (sub or 'install'), []
474
+ # BUG-0714: changing the registry or an auth token changes every later install on this computer.
475
+ if pm_config_change(prog, positional): return 'external', 'changes ' + prog + ' settings (registry or credentials)', []
459
476
  if sub in ('version', 'view', 'ls', 'list', 'outdated', 'why', 'explain', 'audit', 'info', 'show', 'config', 'root', 'bin', 'prefix', 'ping', 'search', 'pack', 'cache', 'doctor', 'help'): return 'read' if sub not in ('search', 'ping', 'view', 'info', 'show', 'audit', 'outdated') else 'network', '', []
460
477
  return 'exec', prog + ' ' + sub, []
461
478
  if prog in ('cargo', 'go'):
@@ -469,6 +486,7 @@ def evaluate(words, redirects, flags, repo, cwd, home, body_programs=None):
469
486
  if sub in ('pip',) and len(positional) > 1 and positional[1] in ('list', 'show', 'freeze', 'check', 'tree'): return 'read', '', []
470
487
  if prog in ('mix', 'dotnet', 'gradle', 'mvn') and sub in CHECK_SUBCOMMAND.get(prog, ()): return 'check', '', []
471
488
  if sub in INSTALLERS[prog]: return 'install', prog + ' ' + sub + ' installs packages', []
489
+ if pm_config_change(prog, positional): return 'external', 'changes ' + prog + ' settings (index or credentials)', []
472
490
  if sub in ('list', 'show', 'freeze', 'check', 'config', 'version', '--version', 'help', 'info', 'search', 'env', 'which', 'tree', 'outdated', 'audit', 'cache'): return 'read', '', []
473
491
  return 'exec', prog + ' ' + sub, []
474
492
  if prog in SHELLS:
@@ -27,13 +27,17 @@ NO_CHANGE = re.compile('|'.join((
27
27
  RUN_AND_REPORT = re.compile(r"^\s*(?:please\s+|can\s+you\s+|could\s+you\s+)?(?:just\s+)?run\s+(?:the\s+|my\s+|our\s+|all\s+(?:the\s+)?)?(?:unit\s+|existing\s+|project'?s?\s+)?(?:tests?|test\s+suite|specs?|checks?|linter|lint|type\s*check|build)\b[^.?!]{0,40}?\s+(?:and\s+)?(?:then\s+)?(?:tell|report|show|give|let\s+me\s+know|summari[sz]e)\b", re.I)
28
28
  FIX_WORDS = re.compile(r"\b(fix|fixes|fixing|repair|make\s+(?:them|it|the\s+tests?)\s+pass|change|edit|update|correct|resolve|implement|add|write|refactor)\b", re.I)
29
29
 
30
+ # BUG-0689: "Open index.html in a browser, click Sign up, and tell me whether it works or shows an error" is a check and a
31
+ # report, not a request to change the page.
32
+ BROWSER_CHECK = re.compile(r"^\s*(?:please\s+|can\s+you\s+|could\s+you\s+)?(?:just\s+)?(?:open|load|visit|view|try|test|check)\b[^?!\n]{0,80}?\b(?:in\s+(?:a|the|your)\s+)?browser\b[^?!\n]{0,160}?\b(?:tell|report|show|let\s+me\s+know)\b", re.I)
33
+
30
34
  NO_CHANGE_NOTE = 'You asked for no changes in this turn, so the agent will only inspect and report. Reply when you want it to apply fixes.'
31
35
 
32
36
  def no_change_request(text):
33
37
  """The user said this turn must not change the project (an audit, a review, "ask before fixing")."""
34
38
  text = str(text or '')
35
39
  if NO_CHANGE.search(text): return True
36
- return len(text) < 300 and bool(RUN_AND_REPORT.search(text)) and not FIX_WORDS.search(text)
40
+ return len(text) < 300 and bool(RUN_AND_REPORT.search(text) or BROWSER_CHECK.search(text)) and not FIX_WORDS.search(text)
37
41
 
38
42
  def honor_no_change(config, shell=False):
39
43
  """An explicit "do not modify anything" is never upgraded to a build; the next message ("go ahead and fix them") routes normally.
@@ -187,6 +191,13 @@ def deck_json_misnamed(repo, paths):
187
191
  if isinstance(data, dict) and isinstance(data.get('slides'), list) and data['slides']: out.append(p)
188
192
  except (OSError, ValueError): continue
189
193
  return out
194
+ # BUG-0691: a terminal session (MINI_TERMINAL=1, set by the mlai CLI) has no slide viewer: a deck must also be exported
195
+ # to a file a person can open, and the answer must name that file instead of pointing at a viewer.
196
+ TERMINAL_DECK_RULE = ('Presentations in this terminal session: there is no slide viewer here. After writing decks/<name>.deck.json and '
197
+ 'checking it, export it with deck_check {path, pdf: true} (it writes exports/<name>.pdf) and tell the user that '
198
+ 'file path. Do not say the deck opens in a viewer or that they can export it from an app.')
199
+ def terminal_deck_rule(task):
200
+ return TERMINAL_DECK_RULE if os.getenv('MINI_TERMINAL') == '1' and deliverable_request(task) == 'slides' else ''
190
201
  def deliverable_issue(task, changed, repo=None):
191
202
  """One repair note when a presentation or document was requested but no such file was made (or it was misnamed)."""
192
203
  kind = deliverable_request(task)
@@ -194,6 +205,12 @@ def deliverable_issue(task, changed, repo=None):
194
205
  misnamed = deck_json_misnamed(repo, changed) if repo else []
195
206
  if misnamed:
196
207
  return 'Rename ' + ', '.join(misnamed[:3]) + ' so the name ends in .deck.json (for example decks/pitch.deck.json). The app only opens files ending in .deck.json as slides; other .json files show as code.'
208
+ if kind == 'slides' and os.getenv('MINI_TERMINAL') == '1':
209
+ decks = [p for p in changed if isinstance(p, str) and p.lower().endswith('.deck.json')]
210
+ if decks and not any(isinstance(p, str) and p.lower().endswith(('.pdf', '.pptx')) for p in changed):
211
+ return ('The deck is saved as ' + ', '.join(decks[:3]) + ', but this is a terminal session with no slide viewer, so the user '
212
+ 'cannot open it. Export it now with deck_check {path: "' + decks[0] + '", pdf: true}, which writes exports/<name>.pdf, and '
213
+ 'tell the user that PDF path. Do not say it opens in a viewer.')
197
214
  if deliverable_files(changed): return None
198
215
  if kind == 'slides':
199
216
  return 'The user asked for a presentation, but no presentation file was created in this run. Write it now as decks/<name>.deck.json in the slide schema of the presentations skill, check it with deck_check {path} and fix every error, and do not paste the slide JSON into your reply. The app opens the deck on the right when the run ends.'
@@ -231,7 +231,8 @@ def prepare(config,model,root,emit):
231
231
  def browser_review(records,current_fingerprint):
232
232
  fresh=[r for r in records if isinstance(r,dict) and r.get('sourceFingerprint')==current_fingerprint]
233
233
  def valid(r):
234
- return r.get('success') is True and r.get('horizontalOverflow') is False and not r.get('pageErrors') and not r.get('consoleErrors') and not r.get('networkFailures') and bool(r.get('screenshot')) and any(s.get('action')=='assert_text' and s.get('state')=='passed' for s in r.get('timeline',[]) if isinstance(s,dict))
234
+ # A check against another app's server (CLI 0.3.8: port 3000 served a different workspace) is not evidence here.
235
+ return r.get('workspaceEvidence') is not False and not r.get('foreignServer') and r.get('success') is True and r.get('horizontalOverflow') is False and not r.get('pageErrors') and not r.get('consoleErrors') and not r.get('networkFailures') and bool(r.get('screenshot')) and any(s.get('action')=='assert_text' and s.get('state')=='passed' for s in r.get('timeline',[]) if isinstance(s,dict))
235
236
  good=[r for r in fresh if valid(r)]
236
237
  mobile=any(320<=r.get('viewport',{}).get('width',0)<=480 for r in good)
237
238
  desktop=any(r.get('viewport',{}).get('width',0)>=1024 for r in good)