mlai-code 0.3.2 → 0.3.3
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/README.md +1 -1
- package/package.json +1 -1
- package/src/cli.mjs +27 -7
- package/src/engine.mjs +1 -1
- package/src/home.mjs +1 -1
- package/src/parity.mjs +3 -1
- package/src/render.mjs +3 -1
- package/src/runtime.mjs +21 -1
- package/src/sandbox.mjs +19 -3
- package/src/session.mjs +22 -2
- package/src/shared-data.mjs +1 -1
- package/src/tui/app.mjs +22 -4
- package/src/tui/chat.mjs +8 -6
- package/src/tui/cmd-core.mjs +2 -2
- package/src/tui/cmd-extensibility.mjs +8 -2
- package/src/tui/commands.mjs +2 -1
- package/src/tui/statusline.mjs +2 -1
- package/src/wakeups.mjs +2 -2
- package/vendor/client-sdk/index.cjs +3 -3
- package/vendor/cloud-cli/mlai.mjs +169 -25
- package/worker/build_planner.py +75 -7
- package/worker/cloud_pty.py +91 -2
- package/worker/command_safety.py +25 -1
- package/worker/compaction_brief.py +186 -0
- package/worker/context_manager.py +6 -0
- package/worker/conversation.py +58 -1
- package/worker/deck_render.js +11 -0
- package/worker/deck_tools.py +825 -0
- package/worker/git_sync.py +38 -13
- package/worker/goals.py +4 -0
- package/worker/image_generation.py +102 -23
- package/worker/kimi_bridge.py +340 -32
- package/worker/kimi_gates.py +49 -0
- package/worker/kimi_hook.py +65 -12
- package/worker/media_tools.py +179 -0
- package/worker/memory.py +2 -2
- package/worker/mlai_hooks.py +93 -3
- package/worker/mlai_mcp.py +23 -5
- package/worker/model_stream.py +21 -7
- package/worker/offline_guard.py +1 -1
- package/worker/preview.py +47 -4
- package/worker/product_facts.py +18 -0
- package/worker/project_context.py +33 -7
- package/worker/reply_language.py +92 -10
- package/worker/roles.json +199 -37
- package/worker/roles.py +243 -8
- package/worker/runner.py +241 -36
- package/worker/site_check.py +279 -0
- package/worker/skill_library.py +15 -3
- package/worker/skill_pack.py +170 -16
- package/worker/tool_catalog.py +3 -2
- package/worker/verification.py +43 -11
- package/worker/wakeups.py +56 -4
|
@@ -14,14 +14,25 @@ import readline from 'node:readline';
|
|
|
14
14
|
import process from 'node:process';
|
|
15
15
|
|
|
16
16
|
export const VERSION='0.1.0';
|
|
17
|
-
const DEFAULT_URL=process.env.MLAI_URL||'https://
|
|
17
|
+
export const DEFAULT_URL=process.env.MLAI_URL||'https://code.ml.ai';
|
|
18
18
|
const CONFIG_DIR=process.env.MLAI_CONFIG_DIR||join(process.env.XDG_CONFIG_HOME||join(homedir(),'.config'),'mlai');
|
|
19
19
|
const CONFIG=join(CONFIG_DIR,'config.json');
|
|
20
20
|
const LINKS=join(CONFIG_DIR,'projects.json');
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
// TTY: a person is at the terminal (prompts, inline approvals). Colour is separate: NO_COLOR and TERM=dumb turn it off,
|
|
22
|
+
// FORCE_COLOR turns it on (or off with 0) even when piped; stderr gets colour only when stderr itself is a terminal.
|
|
23
|
+
const TTY=Boolean(process.stdout.isTTY);
|
|
24
|
+
export function colorOn(stream,env=process.env){
|
|
25
|
+
const f=env.FORCE_COLOR;if(f!==undefined&&f!=='')return !['0','false'].includes(String(f).toLowerCase());
|
|
26
|
+
return Boolean(stream?.isTTY)&&!env.NO_COLOR&&env.TERM!=='dumb';
|
|
27
|
+
}
|
|
28
|
+
const palette=on=>{const k=(code,s)=>on?`\x1b[${code}m${s}\x1b[0m`:s;return {dim:s=>k(2,s),bold:s=>k(1,s),teal:s=>k(36,s),green:s=>k(32,s),yellow:s=>k(33,s),red:s=>k(31,s),gray:s=>k(90,s)}};
|
|
29
|
+
const c=palette(colorOn(process.stdout)),ce=palette(colorOn(process.stderr));
|
|
30
|
+
/** Exit codes scripts can rely on: 0 done, 1 failed, 2 usage (bad command or flag, nothing linked), 130 stopped or interrupted. */
|
|
31
|
+
export const EXIT={ok:0,failed:1,usage:2,stopped:130};
|
|
23
32
|
const MODES={plan:'plan',brief:'brief',guided:'approve',approve:'approve',auto:'auto'};
|
|
24
33
|
let JSON_OUT=false;
|
|
34
|
+
// `mlai completion bash | head` closes the pipe early; that is not an error.
|
|
35
|
+
process.stdout.on('error',e=>{if(e?.code==='EPIPE')process.exit(0);throw e});
|
|
25
36
|
|
|
26
37
|
// ---------- config ----------
|
|
27
38
|
function readJson(p,d){try{return JSON.parse(readFileSync(p,'utf8'))}catch{return d}}
|
|
@@ -31,12 +42,20 @@ function saveConfig(v){writeJson(CONFIG,v)}
|
|
|
31
42
|
function links(){return readJson(LINKS,{})}
|
|
32
43
|
function linkedProject(dir=process.cwd()){const all=links();let d=resolve(dir);for(;;){if(all[d])return all[d];const up=resolve(d,'..');if(up===d)return null;d=up}}
|
|
33
44
|
function saveLink(project,dir=process.cwd()){const all=links();all[resolve(dir)]=project;writeJson(LINKS,all)}
|
|
34
|
-
|
|
45
|
+
/**
|
|
46
|
+
* The sign-in to use. MLAI_TOKEN (set by `mlai cloud`, from the `mlai login` sign-in, together with MLAI_URL) wins over
|
|
47
|
+
* the saved config, so both commands use the same account and server; otherwise the token saved by this CLI's login.
|
|
48
|
+
*/
|
|
49
|
+
export function signIn(cfg=config(),env=process.env){
|
|
50
|
+
if(env.MLAI_TOKEN)return {url:(env.MLAI_URL||cfg.url||DEFAULT_URL).replace(/\/$/,''),token:String(env.MLAI_TOKEN),label:cfg.token===env.MLAI_TOKEN?cfg.label||'':'mlai login',env:true};
|
|
51
|
+
return {url:(cfg.url||DEFAULT_URL).replace(/\/$/,''),token:cfg.token||'',label:cfg.label||'',env:false};
|
|
52
|
+
}
|
|
53
|
+
function baseUrl(){return signIn().url}
|
|
35
54
|
|
|
36
55
|
// ---------- http ----------
|
|
37
56
|
class ApiError extends Error{constructor(message,status,code){super(message);this.status=status;this.code=code}}
|
|
38
57
|
async function api(path,body,{auth=true,method='POST'}={}){
|
|
39
|
-
const cfg=
|
|
58
|
+
const cfg=signIn();const headers={'Content-Type':'application/json','User-Agent':'mlai-cli/'+VERSION};
|
|
40
59
|
if(auth){if(!cfg.token)throw new ApiError('Not signed in. Run `mlai login` first.',401,'no_token');headers.Authorization='Bearer '+cfg.token}
|
|
41
60
|
const r=await fetch(baseUrl()+path,{method,headers,body:body?JSON.stringify(body):undefined});
|
|
42
61
|
const text=await r.text();let data;try{data=JSON.parse(text)}catch{data={error:text.slice(0,200)||r.statusText}}
|
|
@@ -48,9 +67,18 @@ const ws=(token,action,extra={})=>api('/api/workspace',{action,token,...extra});
|
|
|
48
67
|
// ---------- output helpers ----------
|
|
49
68
|
function out(s=''){process.stdout.write(s+'\n')}
|
|
50
69
|
function emit(obj){if(JSON_OUT)out(JSON.stringify(obj))}
|
|
51
|
-
function fail(msg,code=1){process.stderr.write(
|
|
70
|
+
function fail(msg,code=1){closeInput();process.stderr.write(ce.red('error: ')+msg+'\n');process.exit(code)}
|
|
52
71
|
function wrap(text,indent=' ',width=Math.min(process.stdout.columns||100,110)){const lines=[];for(const para of String(text).split('\n')){let line='';for(const word of para.split(/\s+/)){if((line+' '+word).trim().length>width-indent.length){lines.push(indent+line.trim());line=word}else line+=' '+word}lines.push(indent+line.trim())}return lines.join('\n')}
|
|
53
|
-
|
|
72
|
+
// One readline for the whole process, so the up arrow brings back earlier tasks and answers in the interactive session.
|
|
73
|
+
let rl=null;
|
|
74
|
+
function input(){
|
|
75
|
+
if(rl)return rl;
|
|
76
|
+
rl=readline.createInterface({input:process.stdin,output:process.stdout,terminal:Boolean(process.stdin.isTTY&&process.stdout.isTTY),historySize:200,removeHistoryDuplicates:true});
|
|
77
|
+
rl.on('SIGINT',()=>process.emit('SIGINT'));rl.on('close',()=>{rl=null});
|
|
78
|
+
return rl;
|
|
79
|
+
}
|
|
80
|
+
function closeInput(){try{rl?.close()}catch{}rl=null}
|
|
81
|
+
function ask(question){return new Promise(res=>{const r=input();let done=false;const end=()=>{if(!done){done=true;res('')}};r.once('close',end);r.question(question,a=>{done=true;r.off('close',end);res(a.trim())})})}
|
|
54
82
|
function openBrowser(url){const cmd=platform()==='darwin'?'open':platform()==='win32'?'cmd':'xdg-open';const args=platform()==='win32'?['/c','start','',url]:[url];try{const p=spawn(cmd,args,{stdio:'ignore',detached:true});p.on('error',()=>{});p.unref();return true}catch{return false}}
|
|
55
83
|
function requireProject(opts){const token=opts.project||process.env.MLAI_PROJECT||linkedProject()?.token;if(!token)fail('No project linked to this folder. Run `mlai projects` then `mlai link <name>` or `mlai new`.',2);return token}
|
|
56
84
|
function timeAgo(ms){const s=Math.max(0,(Date.now()-ms)/1000);if(s<60)return 'just now';if(s<3600)return Math.floor(s/60)+'m ago';if(s<86400)return Math.floor(s/3600)+'h ago';return Math.floor(s/86400)+'d ago'}
|
|
@@ -140,12 +168,22 @@ ${c.bold('Deploys')} (connect a host once in the web app: Deploys)
|
|
|
140
168
|
${c.bold('Catalog')}
|
|
141
169
|
mlai roles [query] | skills [query] | modes
|
|
142
170
|
|
|
143
|
-
|
|
144
|
-
|
|
171
|
+
${c.bold('More')}
|
|
172
|
+
mlai doctor check the server, sign-in and linked project
|
|
173
|
+
mlai completion bash|zsh|fish print a shell completion script
|
|
174
|
+
mlai help <command> help for one command (or mlai <command> --help)
|
|
175
|
+
|
|
176
|
+
Global: --json (machine output), -p <project-token>, MLAI_URL, MLAI_PROJECT, NO_COLOR, FORCE_COLOR
|
|
177
|
+
Exit codes: 0 done, 1 failed, 2 usage error, 130 stopped or interrupted`;
|
|
178
|
+
commands.help=async opts=>{const name=opts?._?.[0];if(!name)return out(help);const h=commandHelp(name);if(!h)fail(unknownCommand(name),EXIT.usage);out(h)};
|
|
145
179
|
commands.version=async()=>out(VERSION);
|
|
146
180
|
commands.login=login;
|
|
147
|
-
|
|
148
|
-
commands.
|
|
181
|
+
// The server has no token revocation endpoint (CLI tokens are signed and expire on their own), so logout removes it here.
|
|
182
|
+
commands.logout=async()=>{const cfg=config();const had=Boolean(cfg.token);delete cfg.token;delete cfg.owner;delete cfg.label;saveConfig(cfg);out(had?'Signed out. The token was removed from this computer. It cannot be used from here again and expires on its own.':'Already signed out on this computer.')};
|
|
183
|
+
commands.whoami=async()=>{
|
|
184
|
+
// The device sign-in from `mlai login` is checked by `mlai whoami`; this CLI's whoami route takes only its own tokens.
|
|
185
|
+
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}
|
|
186
|
+
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()))}};
|
|
149
187
|
|
|
150
188
|
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))}
|
|
151
189
|
commands.projects=async()=>{
|
|
@@ -169,7 +207,7 @@ commands.new=async opts=>{
|
|
|
169
207
|
saveLink({token:w.token,name:w.name,label:opts.name||w.label||repo.split('/').slice(-2).join('/')||w.name});
|
|
170
208
|
if(!JSON_OUT)out(c.green('Project ready ')+(repo||'(empty)')+c.dim(' · linked to '+process.cwd()));
|
|
171
209
|
};
|
|
172
|
-
commands.open=async
|
|
210
|
+
commands.open=async opts=>{const name=opts.project?'':linkedProject()?.name;const url=baseUrl()+'/'+(name?'?chat='+encodeURIComponent(name):'');out(url);openBrowser(url)};
|
|
173
211
|
|
|
174
212
|
// ----- run / watch -----
|
|
175
213
|
function renderEvent(e,state){
|
|
@@ -211,7 +249,18 @@ async function answerPending(token,pending,interactive){
|
|
|
211
249
|
if(/^n/i.test(a)){await ws(token,'approve',{id:pending.id,approved:false});return true}
|
|
212
250
|
return false;
|
|
213
251
|
}
|
|
214
|
-
|
|
252
|
+
/** While a run is followed, Ctrl+C leaves it running in the cloud and says how to get back to it. */
|
|
253
|
+
let following=null;
|
|
254
|
+
process.on('SIGINT',()=>{
|
|
255
|
+
closeInput();
|
|
256
|
+
if(following)process.stderr.write('\n'+ce.yellow('Still running.')+' The task keeps going in the cloud. Follow it again with `mlai watch`, or stop it with `mlai stop`.\n');
|
|
257
|
+
else process.stderr.write('\n');
|
|
258
|
+
process.exit(EXIT.stopped);
|
|
259
|
+
});
|
|
260
|
+
/** How a finished run maps to the exit code: completed 0, stopped 130, anything else 1. */
|
|
261
|
+
export function exitFor(state){return state==='completed'||state==='done'||state==='idle'?EXIT.ok:state==='stopped'||state==='cancelled'?EXIT.stopped:EXIT.failed}
|
|
262
|
+
async function watch(token,opts={}){following=token;try{return await follow(token,opts)}finally{following=null}}
|
|
263
|
+
async function follow(token,{interactive=TTY,since=0,quiet=false}={}){
|
|
215
264
|
const state={finished:null};let lastTime=since;let lastPendingId=null;let idlePolls=0;
|
|
216
265
|
for(;;){
|
|
217
266
|
const snap=await ws(token,'status');
|
|
@@ -221,7 +270,8 @@ async function watch(token,{interactive=TTY,since=0,quiet=false}={}){
|
|
|
221
270
|
if(snap.pending&&snap.pending.id!==lastPendingId){lastPendingId=snap.pending.id;if(!JSON_OUT){const answered=await answerPending(token,snap.pending,interactive);if(!answered&&!interactive)return snap}else out(JSON.stringify({type:'pending',...snap.pending}))}
|
|
222
271
|
if(!running){
|
|
223
272
|
const st=snap.status?.state||state.finished||'idle';
|
|
224
|
-
|
|
273
|
+
process.exitCode=exitFor(st);
|
|
274
|
+
if(!JSON_OUT&&!quiet){const comp=snap.completion;out('');out((st==='completed'?c.green('✓ '):st==='stopped'?c.yellow('■ '):c.red('✕ '))+c.bold(st)+(snap.status?.tokens?c.dim(` · ${(()=>{const v=snap.status.tokens/1e6;return v<0.01?'under 0.01':String(Math.round(v*100)/100)})()} credits · ${snap.status.calls||0} calls`):'')+(comp?c.dim(` · checks ${comp.checksPassed||0} passed, ${comp.checksFailed||0} failed`):''))}
|
|
225
275
|
return snap;
|
|
226
276
|
}
|
|
227
277
|
idlePolls=events.length?0:idlePolls+1;
|
|
@@ -229,7 +279,7 @@ async function watch(token,{interactive=TTY,since=0,quiet=false}={}){
|
|
|
229
279
|
}
|
|
230
280
|
}
|
|
231
281
|
async function startRun(token,task,opts){
|
|
232
|
-
const mode=MODES[String(opts.mode||'guided').toLowerCase()];if(!mode)fail('Mode must be plan, brief, guided or auto');
|
|
282
|
+
const mode=MODES[String(opts.mode||'guided').toLowerCase()];if(!mode)fail('Mode must be plan, brief, guided or auto.',EXIT.usage);
|
|
233
283
|
const body={action:'run',token,task,buildIntake:true,requestId:randomUUID(),mode,effort:['low','medium','high'].includes(opts.effort)?opts.effort:'medium',steps:Math.min(160,Math.max(1,Number(opts.steps)||120)),role:opts.role||'auto',planApproval:mode==='auto'?'off':'guided',tools:[]};
|
|
234
284
|
if(opts['build-brief'])body.buildAnswers=JSON.parse(readFileSync(String(opts['build-brief']),'utf8'));
|
|
235
285
|
let r=await api('/api/workspace',body);
|
|
@@ -267,6 +317,7 @@ commands.status=async opts=>{
|
|
|
267
317
|
if(snap.history?.length){out(c.bold('Recent'));for(const h of snap.history.slice(0,5))out(c.dim(' '+h.state.padEnd(9))+' '+h.task.slice(0,90))}
|
|
268
318
|
};
|
|
269
319
|
commands.stop=async opts=>{await ws(requireProject(opts),'stop');out('Stop requested.')};
|
|
320
|
+
commands.completion=async opts=>{const sh=String(opts._[0]||'');const script=completionScript(sh);if(!script)fail('Usage: mlai completion bash|zsh|fish',EXIT.usage);process.stdout.write(script)};
|
|
270
321
|
commands.resume=async opts=>{const t=requireProject(opts);await ws(t,'resume',{requestId:randomUUID()});out(c.dim('Continuing the previous task…'));await watch(t)};
|
|
271
322
|
commands.steer=async opts=>{const msg=opts._.join(' ');if(!msg)fail('Usage: mlai steer "<message>"');await ws(requireProject(opts),'steer',{message:msg});out('Instruction delivered.')};
|
|
272
323
|
async function pendingOrFail(token){const snap=await ws(token,'status');if(!snap.pending)fail('Nothing is waiting for approval.');return snap.pending}
|
|
@@ -290,7 +341,7 @@ commands.pull=async opts=>{
|
|
|
290
341
|
const stat=spawnSync('git',['diff','--stat'],{encoding:'utf8'}).stdout;out(c.green('Applied workspace changes to this repo.')+'\n'+stat);
|
|
291
342
|
};
|
|
292
343
|
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};
|
|
293
|
-
commands.push=async opts=>{const m=opts.
|
|
344
|
+
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:''))};
|
|
294
345
|
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)))};
|
|
295
346
|
|
|
296
347
|
// ----- deploys -----
|
|
@@ -359,7 +410,7 @@ commands.roles=async opts=>{const q=opts._.join(' ').toLowerCase();const cat=awa
|
|
|
359
410
|
let group='';for(const r of roles){if(r.group!==group){group=r.group;out(c.bold('\n'+group))}out(' '+c.teal(r.id.padEnd(12))+r.label.padEnd(20)+c.dim(r.tagline.slice(0,70)))}
|
|
360
411
|
out(c.dim('\n'+roles.length+' roles · `mlai roles <id>` for tools and decisions · `mlai run -r <id> "…"`'))};
|
|
361
412
|
commands.skills=async opts=>{const q=opts._.join(' ').toLowerCase();const cat=await catalog();const skills=cat.skills.filter(s=>!q||(s.id+' '+s.name+' '+s.description).toLowerCase().includes(q));emit(skills);if(JSON_OUT)return;for(const s of skills)out(' '+c.teal(s.id.padEnd(28))+c.dim(s.description.slice(0,80)));out(c.dim('\n'+skills.length+' skills'))};
|
|
362
|
-
commands.doctor=async()=>{const cfg=
|
|
413
|
+
commands.doctor=async()=>{const cfg=signIn();out('version '+VERSION);out('server '+cfg.url);out('token '+(cfg.token?'present ('+(cfg.label||'')+')':'missing — run mlai login'));out('project '+(linkedProject()?.label||'none linked here'));out('node '+process.version);try{const h=await fetch(baseUrl()+'/api/health').then(r=>r.status);out('health HTTP '+h)}catch(e){out(c.red('health '+(networkMessage(e,baseUrl())||'unreachable: '+e.message)));process.exitCode=EXIT.failed}};
|
|
363
414
|
|
|
364
415
|
// ----- interactive session -----
|
|
365
416
|
async function session(opts){
|
|
@@ -369,7 +420,7 @@ async function session(opts){
|
|
|
369
420
|
const snap=await ws(token,'status');if(snap.status?.state==='running'){out(c.dim('A task is already running; attaching…'));await watch(token)}
|
|
370
421
|
for(;;){
|
|
371
422
|
const line=await ask(c.teal('› '));if(!line)continue;
|
|
372
|
-
if(line==='/quit'||line==='/exit'||line==='/q')return
|
|
423
|
+
if(line==='/quit'||line==='/exit'||line==='/q'){process.exitCode=0;return}
|
|
373
424
|
if(line.startsWith('/mode ')){const m=line.slice(6).trim();if(!MODES[m]){out(c.red('Modes: plan, brief, guided, auto'));continue}mode=m;out(c.dim('mode '+mode));continue}
|
|
374
425
|
if(line.startsWith('/role ')){role=line.slice(6).trim()||'auto';out(c.dim('role '+role));continue}
|
|
375
426
|
if(line==='/status'){await commands.status({_:[],project:token});continue}
|
|
@@ -381,14 +432,107 @@ async function session(opts){
|
|
|
381
432
|
}
|
|
382
433
|
}
|
|
383
434
|
|
|
435
|
+
// ---------- per-command help, typos, flags, completions ----------
|
|
436
|
+
/** usage, what it does, and the flags it takes (besides the global --json, -p/--project, --help). */
|
|
437
|
+
export const COMMANDS={
|
|
438
|
+
login:{usage:'mlai login [--manual] [--url URL] [--token TOKEN]',about:'Approve this computer in your browser. --manual prints a link and asks for the token; --url signs in to another server.',flags:['manual','url','token','browser']},
|
|
439
|
+
logout:{usage:'mlai logout',about:'Remove the token from this computer.'},
|
|
440
|
+
whoami:{usage:'mlai whoami',about:'Show the account this computer is signed in to.'},
|
|
441
|
+
projects:{usage:'mlai projects',about:'List your projects. The one linked to this folder is marked.'},
|
|
442
|
+
new:{usage:'mlai new [github-url] [-n name] [--profile small|medium|large] [-y]',about:'Create a project, empty or from a GitHub repository, and link this folder to it.',flags:['name','backend','profile','yes']},
|
|
443
|
+
link:{usage:'mlai link <name|#n>',about:'Link this folder to an existing project (see mlai projects).'},
|
|
444
|
+
open:{usage:'mlai open',about:'Open the linked chat in the web app.'},
|
|
445
|
+
run:{usage:'mlai run "<task>" [-m plan|brief|guided|auto] [-r role] [-e low|medium|high] [-s steps] [--no-watch] [--build-brief answers.json]',about:'Start a task in the linked project and follow it. Ctrl+C stops following; the task keeps running. Exits 0 when it completes, 1 when it fails, 130 when it was stopped.',flags:['mode','role','effort','steps','watch','build-brief']},
|
|
446
|
+
watch:{usage:'mlai watch',about:'Follow the current run and answer approvals inline.'},
|
|
447
|
+
status:{usage:'mlai status',about:'Show the current state, task list, approval and recent runs.'},
|
|
448
|
+
stop:{usage:'mlai stop',about:'Stop the current run.'},
|
|
449
|
+
resume:{usage:'mlai resume',about:'Continue the previous task.'},
|
|
450
|
+
steer:{usage:'mlai steer "<message>"',about:'Add an instruction while the agent is working.'},
|
|
451
|
+
approve:{usage:'mlai approve [--all]',about:'Approve what is waiting. --all approves the rest of this run.',flags:['all']},
|
|
452
|
+
reject:{usage:'mlai reject',about:'Reject what is waiting.'},
|
|
453
|
+
reply:{usage:'mlai reply "<feedback>"',about:'Brief mode: send feedback on the task list.'},
|
|
454
|
+
ls:{usage:'mlai ls',about:'List the files in the workspace.'},
|
|
455
|
+
cat:{usage:'mlai cat <file>',about:'Print a file from the workspace.'},
|
|
456
|
+
diff:{usage:'mlai diff',about:'Show the uncommitted changes.'},
|
|
457
|
+
patch:{usage:'mlai patch',about:'Print the uncommitted changes as a patch.'},
|
|
458
|
+
pull:{usage:'mlai pull [-y]',about:'Apply the workspace changes to this git repository (git apply --3way).',flags:['yes']},
|
|
459
|
+
sh:{usage:'mlai sh "<command>"',about:'Run a shell command in the workspace (90 second limit). Exits with the command\'s exit code.'},
|
|
460
|
+
push:{usage:'mlai push -m "message"',about:'Commit and push to the linked GitHub repository.',flags:['message','mode']},
|
|
461
|
+
pr:{usage:'mlai pr -t "title" [-b body] [-y]',about:'Open a draft pull request.',flags:['title','body','yes']},
|
|
462
|
+
deploys:{usage:'mlai deploys',about:'Production, the latest preview and recent deployments.'},
|
|
463
|
+
preview:{usage:'mlai preview [--no-wait] [-y]',about:'Deploy the workspace as a preview and print the URL.',flags:['wait','yes']},
|
|
464
|
+
promote:{usage:'mlai promote [deployment] [-y]',about:'Review checks and promote a preview to production.',flags:['yes']},
|
|
465
|
+
rollback:{usage:'mlai rollback [deployment] [-y]',about:'Roll production back (default: the build before the last change).',flags:['yes']},
|
|
466
|
+
roles:{usage:'mlai roles [query]',about:'List agent roles, or show one.'},
|
|
467
|
+
skills:{usage:'mlai skills [query]',about:'List skills.'},
|
|
468
|
+
modes:{usage:'mlai modes',about:'List run modes.'},
|
|
469
|
+
doctor:{usage:'mlai doctor',about:'Check the server address, sign-in, linked project, Node version and that the server answers.'},
|
|
470
|
+
completion:{usage:'mlai completion bash|zsh|fish',about:'Print a completion script. Add `source <(mlai completion bash)` to ~/.bashrc, `source <(mlai completion zsh)` to ~/.zshrc, or run `mlai completion fish > ~/.config/fish/completions/mlai.fish`.'},
|
|
471
|
+
help:{usage:'mlai help [command]',about:'Show help.'},
|
|
472
|
+
version:{usage:'mlai version',about:'Print the version.'},
|
|
473
|
+
};
|
|
474
|
+
const GLOBAL_FLAGS=['json','project','help','version'];
|
|
475
|
+
const SESSION_FLAGS=['mode','role'];
|
|
476
|
+
export function commandHelp(name){const h=COMMANDS[name];if(!h)return '';const f=(h.flags||[]).map(x=>'--'+x);return `${c.bold('Usage')} ${h.usage}\n\n${wrap(h.about,' ')}${f.length?'\n\n'+c.bold('Options')+' '+f.join(' '):''}\n${c.dim('Global: --json, -p <project>, --help')}`}
|
|
477
|
+
/** Edit distance, for "did you mean". */
|
|
478
|
+
function distance(a,b){const d=Array.from({length:a.length+1},(_,i)=>[i]);for(let j=1;j<=b.length;j++)d[0][j]=j;for(let i=1;i<=a.length;i++)for(let j=1;j<=b.length;j++)d[i][j]=Math.min(d[i-1][j]+1,d[i][j-1]+1,d[i-1][j-1]+(a[i-1]===b[j-1]?0:1),i>1&&j>1&&a[i-1]===b[j-2]&&a[i-2]===b[j-1]?d[i-2][j-2]+1:Infinity);return d[a.length][b.length]}
|
|
479
|
+
export function didYouMean(word,options){
|
|
480
|
+
const w=String(word||'').toLowerCase();let best='',score=Infinity;
|
|
481
|
+
for(const o of options){const d=o.startsWith(w)&&w.length>=2?0.5:distance(w,o);if(d<score){score=d;best=o}}
|
|
482
|
+
return score<=Math.max(1,Math.floor(w.length/3))?best:'';
|
|
483
|
+
}
|
|
484
|
+
function unknownCommand(name){const m=didYouMean(name,Object.keys(COMMANDS));return `Unknown command "${name}".`+(m?` Did you mean \`mlai ${m}\`?`:'')+' Run `mlai help` for the list.'}
|
|
485
|
+
/** Flags a command does not take, and values that cannot work. Returns an error message or ''. */
|
|
486
|
+
export function checkFlags(name,opts){
|
|
487
|
+
const allowed=new Set([...GLOBAL_FLAGS,...(name?COMMANDS[name]?.flags||[]:SESSION_FLAGS)]);
|
|
488
|
+
for(const k of Object.keys(opts)){
|
|
489
|
+
if(k==='_'||allowed.has(k))continue;
|
|
490
|
+
const flag=k.length===1?'-'+k:'--'+k;const m=didYouMean(k,[...allowed]);
|
|
491
|
+
return `Unknown option ${flag}${name?' for mlai '+name:''}.`+(m?` Did you mean --${m}?`:'')+` See \`mlai help${name?' '+name:''}\`.`;
|
|
492
|
+
}
|
|
493
|
+
if(name!=='push'&&opts.mode!==undefined&&!MODES[String(opts.mode).toLowerCase()])return 'Mode must be plan, brief, guided or auto.';
|
|
494
|
+
if(opts.effort!==undefined&&!['low','medium','high'].includes(opts.effort))return 'Effort must be low, medium or high.';
|
|
495
|
+
if(opts.steps!==undefined&&!(/^\d+$/.test(String(opts.steps))&&Number(opts.steps)>=1&&Number(opts.steps)<=160))return 'Steps must be a whole number from 1 to 160.';
|
|
496
|
+
if(opts.profile!==undefined&&!['small','medium','large'].includes(opts.profile))return 'Profile must be small, medium or large.';
|
|
497
|
+
for(const k of ['project','role','title','body','name','message','url','token','build-brief'])if(opts[k]===true&&allowed.has(k)&&!(name==='login'&&k==='token'))return `--${k} needs a value.`;
|
|
498
|
+
return '';
|
|
499
|
+
}
|
|
500
|
+
export function completionScript(sh){
|
|
501
|
+
const cmds=Object.keys(COMMANDS).join(' ');
|
|
502
|
+
if(sh==='bash')return `# mlai completion for bash\n_mlai(){ local cur=\${COMP_WORDS[COMP_CWORD]}; if [ \$COMP_CWORD -eq 1 ]; then COMPREPLY=( \$(compgen -W "${cmds}" -- "\$cur") ); elif [[ "\$cur" == -* ]]; then COMPREPLY=( \$(compgen -W "--json --project --help --mode --role --effort --steps --no-watch --all --yes" -- "\$cur") ); fi; }\ncomplete -F _mlai mlai\n`;
|
|
503
|
+
if(sh==='zsh')return `#compdef mlai\n# mlai completion for zsh\n_mlai(){ local -a cmds; cmds=(${Object.entries(COMMANDS).map(([k,v])=>`'${k}:${v.about.split('.')[0].replace(/['\:]/g,'')}'`).join(' ')}); if (( CURRENT == 2 )); then _describe 'command' cmds; else _arguments '--json[machine output]' '--project[project token]:token' '--help[help]' '--mode[run mode]:mode:(plan brief guided auto)' '--effort[effort]:effort:(low medium high)' '--all' '--yes'; fi }\ncompdef _mlai mlai\n`;
|
|
504
|
+
if(sh==='fish')return `# mlai completion for fish\ncomplete -c mlai -f\n${Object.entries(COMMANDS).map(([k,v])=>`complete -c mlai -n '__fish_use_subcommand' -a ${k} -d '${v.about.split('.')[0].replace(/'/g,'')}'`).join('\n')}\ncomplete -c mlai -l json -d 'Machine output'\ncomplete -c mlai -s p -l project -r -d 'Project token'\ncomplete -c mlai -s m -l mode -xa 'plan brief guided auto'\ncomplete -c mlai -s e -l effort -xa 'low medium high'\n`;
|
|
505
|
+
return '';
|
|
506
|
+
}
|
|
507
|
+
/** fetch failures as plain sentences instead of "fetch failed". */
|
|
508
|
+
export function networkMessage(e,url){
|
|
509
|
+
const code=e?.cause?.code||e?.cause?.errors?.[0]?.code||e?.code||'';const host=(()=>{try{return new URL(url).host}catch{return url}})();
|
|
510
|
+
if(!code&&!(e instanceof TypeError&&/fetch failed/i.test(e.message)))return '';
|
|
511
|
+
if(['ENOTFOUND','EAI_AGAIN'].includes(code))return `Could not find ${host}. Check your internet connection, or the server address (mlai login --url, MLAI_URL).`;
|
|
512
|
+
if(code==='ECONNREFUSED')return `${host} refused the connection. The server may be down or the address wrong (mlai login --url, MLAI_URL).`;
|
|
513
|
+
if(['ETIMEDOUT','UND_ERR_CONNECT_TIMEOUT','UND_ERR_HEADERS_TIMEOUT'].includes(code))return `${host} did not answer in time. Check your connection and try again.`;
|
|
514
|
+
if(['ECONNRESET','UND_ERR_SOCKET','EPIPE'].includes(code))return `The connection to ${host} dropped. Try again.`;
|
|
515
|
+
if(/CERT|SSL|TLS/i.test(code))return `Could not make a secure connection to ${host} (${code}). A proxy or firewall may be in the way.`;
|
|
516
|
+
if(['ENETUNREACH','EHOSTUNREACH'].includes(code))return `No network route to ${host}. Check your internet connection.`;
|
|
517
|
+
return `Could not reach ${host}${code?' ('+code+')':''}. Check your internet connection.`;
|
|
518
|
+
}
|
|
519
|
+
|
|
384
520
|
// ---------- main ----------
|
|
385
521
|
export async function main(argv=process.argv.slice(2)){
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
522
|
+
try{
|
|
523
|
+
const opts=parseArgs(argv);JSON_OUT=opts.json===true;
|
|
524
|
+
if(opts.version&&!opts._.length)return await commands.version();
|
|
525
|
+
const name=opts._.shift();
|
|
526
|
+
if(name==='help')return await commands.help(opts);
|
|
527
|
+
if(name&&!commands[name])fail(unknownCommand(name),EXIT.usage);
|
|
528
|
+
if(opts.help){if(name)return out(commandHelp(name));return await commands.help({_:[]})}
|
|
529
|
+
const bad=checkFlags(name,opts);if(bad)fail(bad,EXIT.usage);
|
|
530
|
+
if(!name)return await session(opts);
|
|
531
|
+
await commands[name](opts);
|
|
532
|
+
}catch(e){
|
|
533
|
+
if(e instanceof ApiError){if(e.code==='workspace_missing')fail('This project no longer exists. `mlai projects` to pick another.');fail(e.message)}
|
|
534
|
+
const net=networkMessage(e,baseUrl());if(net)fail(net);
|
|
535
|
+
fail(e?.message||String(e));
|
|
536
|
+
}finally{closeInput()}
|
|
393
537
|
}
|
|
394
538
|
if(import.meta.url===`file://${process.argv[1]}`||process.argv[1]?.endsWith('/mlai')||process.argv[1]?.endsWith('mlai.mjs'))main();
|
package/worker/build_planner.py
CHANGED
|
@@ -75,17 +75,33 @@ def read_plan(repo):
|
|
|
75
75
|
_PHASE = re.compile(r'^#{2,3}\s*Phase\s+(\d+)\s*(?:[:.\u2014\u2013-]\s*)?(.*)$', re.I | re.M)
|
|
76
76
|
_ITEM = re.compile(r'^\s*[-*]\s*\[( |x|X)\]\s*(.+)$', re.M)
|
|
77
77
|
|
|
78
|
+
# "(done)", "[complete]", "- delivered", a bare "delivered" or a check mark after the phase number mark the phase finished.
|
|
79
|
+
_DONE_MARK = re.compile(r'(?:^|\(|\[|\s[\u2014\u2013-]\s)\s*(?:delivered|done|complete|completed|shipped|finished)\s*[)\]]?\s*$|[\u2705\u2714\u2611]', re.I)
|
|
80
|
+
_STATUS_SUFFIX = re.compile(r'\s*(?:[(\[](?:done|complete|completed|delivered|shipped|finished|in progress|next|current)[)\]]|\s[\u2014\u2013-]\s*(?:done|complete|completed|delivered|shipped|finished)|[\u2705\u2714\u2611])\s*$', re.I)
|
|
81
|
+
|
|
78
82
|
def parse_phases(plan):
|
|
79
|
-
"""Phases from PLAN.md headings (## Phase N: title) with their checkbox deliverables.
|
|
80
|
-
|
|
83
|
+
"""Phases from PLAN.md headings (## Phase N: title) with their checkbox deliverables.
|
|
84
|
+
A second heading with the same number ("### Phase 4 delivered", a summary the agent writes after finishing) belongs to
|
|
85
|
+
that phase; it used to become a new, unfinished Phase 4 with no items, so Start Phase 4 kept reappearing and each press
|
|
86
|
+
started a paid run that had nothing to do."""
|
|
87
|
+
phases, by_number = [], {}
|
|
81
88
|
matches = list(_PHASE.finditer(plan or ''))
|
|
82
89
|
for i, m in enumerate(matches):
|
|
83
90
|
body = plan[m.end():matches[i + 1].start() if i + 1 < len(matches) else len(plan)]
|
|
84
91
|
items = [{'text': t.strip()[:200], 'done': c.lower() == 'x'} for c, t in _ITEM.findall(body)][:30]
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
92
|
+
number, raw = int(m.group(1)), m.group(2).strip()
|
|
93
|
+
marked = bool(_DONE_MARK.search(raw))
|
|
94
|
+
if number in by_number:
|
|
95
|
+
p = by_number[number]
|
|
96
|
+
p['items'] = (p['items'] + items)[:30]; p['_marked'] = p['_marked'] or marked
|
|
97
|
+
continue
|
|
98
|
+
title = _STATUS_SUFFIX.sub('', raw)[:120]
|
|
99
|
+
if _DONE_MARK.fullmatch(title.strip() or 'x'): title = ''
|
|
100
|
+
p = {'number': number, 'title': title or 'Phase ' + m.group(1), 'items': items, '_marked': marked}
|
|
101
|
+
phases.append(p); by_number[number] = p
|
|
102
|
+
for p in phases:
|
|
103
|
+
p['done'] = bool(p['items']) and all(x['done'] for x in p['items']) or p.pop('_marked')
|
|
104
|
+
p.pop('_marked', None)
|
|
89
105
|
current = next((p for p in phases if not p['done']), None)
|
|
90
106
|
for p in phases:
|
|
91
107
|
p['status'] = 'done' if p['done'] else ('current' if p is current else 'planned')
|
|
@@ -106,6 +122,8 @@ ARCHITECT_GUIDANCE = '''Build planning (this is a request to build a product): w
|
|
|
106
122
|
3. Create the task plan from Phase 1 deliverables only, each item prefixed "Phase 1:". Implement only Phase 1 in this run.
|
|
107
123
|
4. Verify Phase 1 actually works (build, tests, and the running preview where possible), then tick its boxes in PLAN.md.
|
|
108
124
|
5. Finish with a short summary: what Phase 1 delivered, how to open it, and what Phase 2 will add. Do not start Phase 2 until the user asks.
|
|
125
|
+
6. Never link to a page that does not exist. Navigation, buttons and footer links may only point at routes built in this run; add links to later pages when their phase builds them.
|
|
126
|
+
7. If anything is still broken at the end (a page that errors, a failing check), fix it before finishing. If you cannot, say plainly that it is still open; never call broken work complete or verified.
|
|
109
127
|
'''
|
|
110
128
|
|
|
111
129
|
_PREFIX = re.compile(r'^\s*phase\s*(\d+)\s*[:.\u2014\u2013-]\s*', re.I)
|
|
@@ -163,8 +181,37 @@ def continuation_guidance(phase):
|
|
|
163
181
|
'Keep earlier phases working. Update PLAN.md if the architecture changes, verify the phase works end to end, tick its boxes, and finish with what it delivered and what the next phase adds.\n'
|
|
164
182
|
'Remaining deliverables:\n' + (items or '- (see PLAN.md)') + '\n\n')
|
|
165
183
|
|
|
184
|
+
# Resume wraps the person's request (lib/resume-task.ts): "Continue the previous task: <request>" plus notes for the agent.
|
|
185
|
+
_RESUME_PREFIX = 'Continue the previous task:'
|
|
186
|
+
_RESUME_NOTES = ('The previous attempt stopped. Inspect the current files', 'The person saw your last reply only up to here:')
|
|
187
|
+
|
|
188
|
+
def original_task(task):
|
|
189
|
+
"""The person's own request inside any number of Resume wrappers. Resume says "continue", which is not a request to
|
|
190
|
+
continue the plan: a resumed edit must stay that edit, not restart the first unticked phase of PLAN.md."""
|
|
191
|
+
t = str(task or '').strip()
|
|
192
|
+
for _ in range(50):
|
|
193
|
+
if not t.startswith(_RESUME_PREFIX): break
|
|
194
|
+
t = t[len(_RESUME_PREFIX):].strip()
|
|
195
|
+
cut = [i for i in (t.find(n) for n in _RESUME_NOTES) if i >= 0]
|
|
196
|
+
return (t[:min(cut)] if cut else t).strip()
|
|
197
|
+
|
|
166
198
|
def planning_guidance(task, repo, mode='approve', phase1=None):
|
|
167
|
-
"""Prompt prefix for build requests and phase continuations; empty for everything else.
|
|
199
|
+
"""Prompt prefix for build requests and phase continuations; empty for everything else. A resumed task is judged by
|
|
200
|
+
the request it resumes, so Resume never turns an ordinary follow-up into "start the next unfinished phase".
|
|
201
|
+
An explicit size ("a marketing website with 10 pages") binds this run: all of it is built now, never split into
|
|
202
|
+
phases on the agent's own initiative (bug report Nov 16 2026: 2 of 10 pages built, 7 nav links left 404ing)."""
|
|
203
|
+
task = original_task(task)
|
|
204
|
+
guidance = _planning_guidance(task, repo, mode, phase1)
|
|
205
|
+
try:
|
|
206
|
+
from site_check import requested_scope, scope_rule
|
|
207
|
+
rule = scope_rule(requested_scope(task)) if (guidance or is_build_request(task, source_files(repo)) or re.search(_BUILD_VERB, (task or '').lower())) else ''
|
|
208
|
+
except Exception:
|
|
209
|
+
rule = ''
|
|
210
|
+
if not rule: return guidance
|
|
211
|
+
if mode == 'plan': return guidance + rule + '\n\n'
|
|
212
|
+
return (guidance.rstrip('\n') + '\n' if guidance else '') + rule + '\n\n'
|
|
213
|
+
|
|
214
|
+
def _planning_guidance(task, repo, mode='approve', phase1=None):
|
|
168
215
|
plan = read_plan(repo)
|
|
169
216
|
phases = parse_phases(plan) if plan else []
|
|
170
217
|
current = next((p for p in phases if p['status'] == 'current'), None)
|
|
@@ -185,3 +232,24 @@ def planning_guidance(task, repo, mode='approve', phase1=None):
|
|
|
185
232
|
+ (phase1.strip() + ' ' if phase1 else 'Phase 1 must be a thin working slice the user can open; ') + 'Each phase lists deliverables and an acceptance check.\n\n')
|
|
186
233
|
rule = (phase1.strip() + ' Every later phase also ends with something working. Order phases by user value and risk.') if phase1 else DEFAULT_PHASE1
|
|
187
234
|
return ARCHITECT_GUIDANCE.replace('{phase1}', rule) + '\n'
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
PHASE_CHIP = re.compile(r'^\s*Continue with Phase (\d+):.*from PLAN\.md\.?\s*$', re.I | re.S)
|
|
238
|
+
|
|
239
|
+
def finished_phase_reply(repo, task):
|
|
240
|
+
"""The Start Phase button sends "Continue with Phase N: ... from PLAN.md.". When PLAN.md already shows that phase as
|
|
241
|
+
finished, the run answers at once without calling the model, so a repeated press costs nothing (one user ran
|
|
242
|
+
the same finished phase six times). Returns '' when the run should go ahead."""
|
|
243
|
+
m = PHASE_CHIP.match(str(task or ''))
|
|
244
|
+
if not m: return ''
|
|
245
|
+
try: plan = (Path(repo) / 'PLAN.md').read_text(errors='replace')
|
|
246
|
+
except OSError: return ''
|
|
247
|
+
phases = parse_phases(plan)
|
|
248
|
+
number = int(m.group(1))
|
|
249
|
+
phase = next((p for p in phases if p['number'] == number), None)
|
|
250
|
+
if not phase or not phase['done']: return ''
|
|
251
|
+
upcoming = next((p for p in phases if not p['done']), None)
|
|
252
|
+
head = 'Phase %d (%s) is already finished in PLAN.md, so there was nothing new to build and no credits were used.' % (number, phase['title'])
|
|
253
|
+
if upcoming:
|
|
254
|
+
return head + ' The next one is Phase %d: %s. Start it when you are ready, or tell me what to change first.' % (upcoming['number'], upcoming['title'])
|
|
255
|
+
return head + ' All phases in the plan are complete. Tell me what you would like to add or improve next.'
|
package/worker/cloud_pty.py
CHANGED
|
@@ -10,9 +10,12 @@ when it is closed, or after IDLE seconds with nobody reading it.
|
|
|
10
10
|
cloud_pty.py rename <id> <title>
|
|
11
11
|
cloud_pty.py close <id>
|
|
12
12
|
cloud_pty.py list
|
|
13
|
-
|
|
13
|
+
cloud_pty.py attach <id> <offset>
|
|
14
|
+
Every command prints one JSON object, except attach: it runs on the terminal the browser holds over a WebSocket
|
|
15
|
+
(Sandbox.openInteractive, lib/cloud-pty.ts ptySocketStart) and copies keys into the tab and its output back, byte for
|
|
16
|
+
byte, until the shell ends or the socket closes. The tab itself stays with its daemon, so it survives a reload.
|
|
14
17
|
"""
|
|
15
|
-
import base64, errno, fcntl, json, os, pty, re, select, shutil, signal, struct, sys, termios, time
|
|
18
|
+
import base64, errno, fcntl, json, os, pty, re, select, shutil, signal, struct, sys, termios, time, tty
|
|
16
19
|
|
|
17
20
|
BASE = os.environ.get('MLAI_PTY_DIR') or os.path.join(os.path.dirname(os.path.abspath(__file__)), 'pty')
|
|
18
21
|
ID = re.compile(r'^t[a-z0-9]{6,24}$')
|
|
@@ -289,6 +292,90 @@ def cmd_io(i, offset, wait_ms, data_b64='', cols=None, rows=None):
|
|
|
289
292
|
'exited': caught_up, 'code': int(code) if caught_up and code and code.lstrip('-').isdigit() else None}
|
|
290
293
|
|
|
291
294
|
|
|
295
|
+
def cmd_attach(i, offset):
|
|
296
|
+
"""Stream one tab over this process's own terminal: stdin to the tab's fifo, its output file to stdout.
|
|
297
|
+
|
|
298
|
+
Exit status: 0 when the shell ended and everything it printed was sent, 3 when the tab is gone."""
|
|
299
|
+
d = path(i)
|
|
300
|
+
if not os.path.isdir(d) or not (live(i) or os.path.exists(path(i, 'exit'))):
|
|
301
|
+
return 3
|
|
302
|
+
term = os.isatty(0)
|
|
303
|
+
if term:
|
|
304
|
+
tty.setraw(0, termios.TCSANOW) # keys and output pass through untouched; the tab's own pty does the line editing
|
|
305
|
+
|
|
306
|
+
def push_size(*_):
|
|
307
|
+
try:
|
|
308
|
+
rows, cols = struct.unpack('HHHH', fcntl.ioctl(0, termios.TIOCGWINSZ, b'\0' * 8))[:2]
|
|
309
|
+
if cols and rows:
|
|
310
|
+
write(path(i, 'size'), '%d %d' % (clamp(cols, 10, 500, 100), clamp(rows, 4, 200, 24)))
|
|
311
|
+
except OSError:
|
|
312
|
+
pass
|
|
313
|
+
if term:
|
|
314
|
+
signal.signal(signal.SIGWINCH, push_size)
|
|
315
|
+
push_size()
|
|
316
|
+
out_file, seen_file = path(i, 'out'), path(i, 'seen')
|
|
317
|
+
pos, mark, seen_at, busy_at = max(0, int(offset)), None, 0.0, time.time()
|
|
318
|
+
pending = b''
|
|
319
|
+
while True:
|
|
320
|
+
now = time.time()
|
|
321
|
+
if now - seen_at > 20:
|
|
322
|
+
seen_at = now
|
|
323
|
+
try:
|
|
324
|
+
with open(seen_file, 'a'):
|
|
325
|
+
os.utime(seen_file, None)
|
|
326
|
+
except OSError:
|
|
327
|
+
return 3
|
|
328
|
+
# Short waits while someone is typing or output is flowing, longer ones when the tab is quiet.
|
|
329
|
+
timeout = 0.004 if now - busy_at < 2 else 0.04
|
|
330
|
+
try:
|
|
331
|
+
r = select.select([0], [], [], timeout)[0]
|
|
332
|
+
except InterruptedError:
|
|
333
|
+
r = []
|
|
334
|
+
if r:
|
|
335
|
+
try:
|
|
336
|
+
data = os.read(0, 65536)
|
|
337
|
+
except OSError:
|
|
338
|
+
data = b''
|
|
339
|
+
if not data:
|
|
340
|
+
return 0 # the socket closed; the tab keeps running
|
|
341
|
+
pending += data
|
|
342
|
+
busy_at = now
|
|
343
|
+
if pending and live(i):
|
|
344
|
+
try:
|
|
345
|
+
fd = os.open(path(i, 'in'), os.O_WRONLY | os.O_NONBLOCK)
|
|
346
|
+
try:
|
|
347
|
+
n = os.write(fd, pending)
|
|
348
|
+
pending = pending[n:]
|
|
349
|
+
finally:
|
|
350
|
+
os.close(fd)
|
|
351
|
+
except BlockingIOError:
|
|
352
|
+
pass
|
|
353
|
+
except OSError:
|
|
354
|
+
pending = b''
|
|
355
|
+
try:
|
|
356
|
+
st = os.stat(out_file)
|
|
357
|
+
key = (st.st_size, st.st_mtime_ns)
|
|
358
|
+
except OSError:
|
|
359
|
+
return 3
|
|
360
|
+
exited = os.path.exists(path(i, 'exit'))
|
|
361
|
+
if key != mark or exited:
|
|
362
|
+
start, end, data, _ = snapshot(i, pos)
|
|
363
|
+
if data:
|
|
364
|
+
view = memoryview(data)
|
|
365
|
+
while view:
|
|
366
|
+
try:
|
|
367
|
+
view = view[os.write(1, view):]
|
|
368
|
+
except InterruptedError:
|
|
369
|
+
pass
|
|
370
|
+
pos = end
|
|
371
|
+
busy_at = time.time()
|
|
372
|
+
if len(data) >= READ_MAX:
|
|
373
|
+
continue # more to send; read again before checking the file
|
|
374
|
+
mark = key
|
|
375
|
+
if exited and not snapshot(i, pos)[2]:
|
|
376
|
+
return 0
|
|
377
|
+
|
|
378
|
+
|
|
292
379
|
def cmd_close(i):
|
|
293
380
|
d, c = pids(i)
|
|
294
381
|
for p, sig in ((c, signal.SIGHUP), (d, signal.SIGTERM)):
|
|
@@ -340,6 +427,8 @@ def main(argv):
|
|
|
340
427
|
r = cmd_close(argv[1])
|
|
341
428
|
elif op == 'list':
|
|
342
429
|
r = cmd_list()
|
|
430
|
+
elif op == 'attach':
|
|
431
|
+
sys.exit(cmd_attach(argv[1], int(argv[2]) if len(argv) > 2 else 0))
|
|
343
432
|
else:
|
|
344
433
|
raise ValueError('Unknown terminal operation')
|
|
345
434
|
except (ValueError, IndexError, OSError) as e:
|
package/worker/command_safety.py
CHANGED
|
@@ -257,6 +257,22 @@ def _classify_paths(paths, repo, cwd, home):
|
|
|
257
257
|
REGENERABLE = {'__pycache__', '.pytest_cache', '.mypy_cache', '.ruff_cache', '.tox', '.nox', '.cache', 'node_modules', '.next', '.nuxt', '.svelte-kit', '.turbo', '.parcel-cache', 'dist', 'build', 'out', 'target', 'coverage', '.coverage', 'htmlcov', '.eggs', 'tmp', '.tmp', '.venv', 'venv', '.gradle', 'bin', 'obj', 'storybook-static', '.angular', '.vite'}
|
|
258
258
|
REGENERABLE_SUFFIX = ('.pyc', '.pyo', '.log', '.tsbuildinfo', '.egg-info', '.o', '.class')
|
|
259
259
|
|
|
260
|
+
def _kept_files(targets, repo, cwd):
|
|
261
|
+
"""Existing workspace files the project keeps (tracked by git, or any existing file when there is no git repo).
|
|
262
|
+
Deleting them is a real delete that Auto must ask about (BUG-0101); files the agent just made and never committed are not."""
|
|
263
|
+
import subprocess
|
|
264
|
+
base = cwd or repo
|
|
265
|
+
found = [t for t in targets if os.path.lexists(t if os.path.isabs(t) else os.path.join(base, t))]
|
|
266
|
+
if not found or not repo: return []
|
|
267
|
+
if not os.path.exists(os.path.join(repo, '.git')): return found
|
|
268
|
+
try:
|
|
269
|
+
out = subprocess.run(['git', '-C', base, 'ls-files', '--', *found], capture_output=True, text=True, timeout=5).stdout
|
|
270
|
+
except (OSError, subprocess.SubprocessError): return found
|
|
271
|
+
tracked = set(out.split('\n'))
|
|
272
|
+
rel = lambda t: os.path.relpath(t if os.path.isabs(t) else os.path.join(base, t), base)
|
|
273
|
+
return [t for t in found if rel(t) in tracked or any(x.startswith(rel(t).rstrip('/') + '/') for x in tracked)]
|
|
274
|
+
|
|
275
|
+
|
|
260
276
|
def _regenerable(target):
|
|
261
277
|
"""A build or cache artefact the toolchain recreates: deleting it is routine, not destructive."""
|
|
262
278
|
t = target.rstrip('/')
|
|
@@ -307,6 +323,8 @@ def evaluate(words, redirects, flags, repo, cwd, home, body_programs=None):
|
|
|
307
323
|
if all(c == 'tmp' for c in classes) and 'xargs' not in flags: return 'write', 'removes scratch files', targets
|
|
308
324
|
if 'xargs' not in flags and prog == 'rm' and all(_regenerable(t) for t in targets): return 'write', 'removes build or cache output (' + ', '.join(targets[:3]) + ')', targets
|
|
309
325
|
if recursive or 'xargs' in flags or any('*' in t for t in targets) or prog == 'shred': return 'destructive', 'recursive or wildcard delete inside the workspace (' + ', '.join(targets[:3]) + ')', targets
|
|
326
|
+
kept = _kept_files(targets, repo, cwd)
|
|
327
|
+
if kept: return 'destructive', 'deletes project files (' + ', '.join(kept[:3]) + ')', targets
|
|
310
328
|
return 'write', '', targets
|
|
311
329
|
if prog in ('mkfs', 'fdisk', 'parted', 'wipefs', 'shutdown', 'reboot', 'halt', 'poweroff', 'init', 'telinit', 'swapoff', 'mkswap') or prog.startswith('mkfs.'): return 'catastrophic', prog + ' changes disks or the machine state', []
|
|
312
330
|
if prog == 'dd':
|
|
@@ -348,6 +366,9 @@ def evaluate(words, redirects, flags, repo, cwd, home, body_programs=None):
|
|
|
348
366
|
if sub in GIT_READ or sub == 'stash' and rest[:1] == ['list'] or sub == 'remote' and not rest[:1] or (sub == 'remote' and rest[:1] in (['-v'], ['show'], ['get-url'])) or (sub == 'config' and not any(r for r in rest if not r.startswith('-')) or (sub == 'config' and (rest[:1] in (['--get'], ['--list'], ['-l'], ['--get-regexp']) or (len([r for r in rest if not r.startswith('-')]) == 1)))):
|
|
349
367
|
if sub == 'branch' and rest and not rest[0].startswith('-'): return 'write', 'creates a branch', []
|
|
350
368
|
return 'read', '', []
|
|
369
|
+
if sub == 'rm' and '--cached' not in rest:
|
|
370
|
+
files = [r for r in rest if not r.startswith('-')]
|
|
371
|
+
return 'destructive', 'git rm deletes project files (' + ', '.join(files[:3]) + ')', files
|
|
351
372
|
if sub in GIT_WRITE or sub in ('remote', 'config', 'tag', 'branch', 'stash'): return 'write', 'git ' + sub, []
|
|
352
373
|
return 'exec', 'git ' + sub, []
|
|
353
374
|
if prog in NETWORK_PROGRAMS:
|
|
@@ -476,7 +497,10 @@ def evaluate(words, redirects, flags, repo, cwd, home, body_programs=None):
|
|
|
476
497
|
if inline or scripts:
|
|
477
498
|
code = joined if inline else ''
|
|
478
499
|
if body_programs: code += '\n' + body_programs
|
|
479
|
-
if re.search(r'(shutil\.rmtree|os\.remove|os\.unlink|rimraf|fs\.rm\w*Sync?\(|fs\.unlink|rm\s+-rf|
|
|
500
|
+
if re.search(r'(shutil\.rmtree|os\.remove|os\.unlink|rimraf|fs\.rm\w*Sync?\(|fs\.unlink|rm\s+-rf|FileUtils\.rm_rf)', code): return 'destructive', 'inline script deletes files', scripts
|
|
501
|
+
# Shelling out alone runs commands like the shell would; it only asks when the script's own text deletes or rewrites history.
|
|
502
|
+
# (Every subprocess-using script used to ask, even in Auto and after Approve all.)
|
|
503
|
+
if re.search(r'(subprocess|child_process|execSync|os\.system)', code) and re.search(r'(\brm\s+-\w*[rf]|\brmdir\b|\bgit\s+(reset\s+--hard|clean\s+-\w*f|push\s+(-f|--force)|rebase|filter-branch|branch\s+-D)|\bdropdb\b|DROP\s+(TABLE|DATABASE))', code, re.I): return 'destructive', 'inline script shells out to delete or rewrite history', scripts
|
|
480
504
|
if re.search(r'(requests\.|urllib|httpx|aiohttp|fetch\(|axios|http\.request|socket\.|net\.connect|smtplib|paramiko)', code): return 'network', 'inline script uses the network', scripts
|
|
481
505
|
if re.search(r'\b(assert|expect\(|unittest|pytest|console\.assert|test\(|describe\()', code) and not re.search(r'(open\([^)]*[\'"]w|writeFile|\.write\(|mkdir|rename|copy)', code): return 'check', '', scripts
|
|
482
506
|
if scripts and re.search(r'(^|/)(test_\w+|\w+_test|\w+\.test|\w+\.spec)\.(py|[cm]?js|ts|tsx)$', scripts[0]): return 'check', '', scripts
|