mlai-code 0.0.0-stage → 0.3.0

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.
Files changed (182) hide show
  1. package/README.md +12 -2
  2. package/bin/mlai.mjs +3 -0
  3. package/package.json +18 -3
  4. package/src/browser-setup.mjs +77 -0
  5. package/src/cli-ext.mjs +146 -0
  6. package/src/cli.mjs +534 -0
  7. package/src/cloud.mjs +29 -0
  8. package/src/completion.mjs +159 -0
  9. package/src/credentials.mjs +54 -0
  10. package/src/deploy.mjs +134 -0
  11. package/src/distribution.mjs +373 -0
  12. package/src/engine-key.mjs +4 -0
  13. package/src/engine.mjs +167 -0
  14. package/src/headless.mjs +196 -0
  15. package/src/home.mjs +22 -0
  16. package/src/host.mjs +123 -0
  17. package/src/local-models.mjs +64 -0
  18. package/src/modes.mjs +38 -0
  19. package/src/netbox.mjs +97 -0
  20. package/src/netjail.py +59 -0
  21. package/src/offline.mjs +98 -0
  22. package/src/parity.mjs +86 -0
  23. package/src/project.mjs +68 -0
  24. package/src/relay.mjs +140 -0
  25. package/src/render.mjs +73 -0
  26. package/src/restricted.mjs +47 -0
  27. package/src/review.mjs +70 -0
  28. package/src/runner.mjs +127 -0
  29. package/src/runtime.mjs +75 -0
  30. package/src/sandbox.mjs +135 -0
  31. package/src/sdk.mjs +11 -0
  32. package/src/server.mjs +110 -0
  33. package/src/session.mjs +290 -0
  34. package/src/settings.mjs +135 -0
  35. package/src/shared-data.mjs +3 -0
  36. package/src/tui/acp.mjs +145 -0
  37. package/src/tui/agents-cli.mjs +114 -0
  38. package/src/tui/agents-local.mjs +65 -0
  39. package/src/tui/ansi.mjs +123 -0
  40. package/src/tui/app.mjs +710 -0
  41. package/src/tui/approval.mjs +88 -0
  42. package/src/tui/bg.mjs +172 -0
  43. package/src/tui/bgterm.mjs +79 -0
  44. package/src/tui/chat.mjs +230 -0
  45. package/src/tui/clipboard.mjs +74 -0
  46. package/src/tui/cloud-cli.mjs +57 -0
  47. package/src/tui/cloud-run.mjs +123 -0
  48. package/src/tui/cmd-agents.mjs +130 -0
  49. package/src/tui/cmd-auto-review.mjs +94 -0
  50. package/src/tui/cmd-cloud.mjs +17 -0
  51. package/src/tui/cmd-config.mjs +104 -0
  52. package/src/tui/cmd-core.mjs +160 -0
  53. package/src/tui/cmd-distribution.mjs +15 -0
  54. package/src/tui/cmd-ecosystem.mjs +169 -0
  55. package/src/tui/cmd-extensibility.mjs +333 -0
  56. package/src/tui/cmd-remote.mjs +18 -0
  57. package/src/tui/cmd-review.mjs +58 -0
  58. package/src/tui/cmd-session-extra.mjs +152 -0
  59. package/src/tui/cmd-tui.mjs +200 -0
  60. package/src/tui/commands.mjs +160 -0
  61. package/src/tui/custom-commands.mjs +98 -0
  62. package/src/tui/diff.mjs +60 -0
  63. package/src/tui/editor.mjs +190 -0
  64. package/src/tui/ext-state.mjs +16 -0
  65. package/src/tui/extensions-local.mjs +140 -0
  66. package/src/tui/files.mjs +72 -0
  67. package/src/tui/handoff.mjs +110 -0
  68. package/src/tui/import-setups.mjs +151 -0
  69. package/src/tui/init.mjs +104 -0
  70. package/src/tui/keymap.mjs +70 -0
  71. package/src/tui/keys.mjs +101 -0
  72. package/src/tui/loop.mjs +75 -0
  73. package/src/tui/markdown.mjs +118 -0
  74. package/src/tui/mcp-config.mjs +225 -0
  75. package/src/tui/mcp-oauth.mjs +162 -0
  76. package/src/tui/permissions.mjs +189 -0
  77. package/src/tui/plain.mjs +32 -0
  78. package/src/tui/plugins-local.mjs +103 -0
  79. package/src/tui/remote.mjs +125 -0
  80. package/src/tui/review-cli.mjs +332 -0
  81. package/src/tui/review-github.mjs +59 -0
  82. package/src/tui/screen.mjs +60 -0
  83. package/src/tui/session-ops.mjs +104 -0
  84. package/src/tui/skills-local.mjs +92 -0
  85. package/src/tui/snapshots.mjs +87 -0
  86. package/src/tui/statusline.mjs +98 -0
  87. package/src/tui/transcript.mjs +116 -0
  88. package/src/two-words.mjs +36 -0
  89. package/src/wakeups.mjs +53 -0
  90. package/src/winenv.mjs +94 -0
  91. package/src/winsandbox.py +351 -0
  92. package/src/workspace-api.mjs +621 -0
  93. package/src/worktree.mjs +128 -0
  94. package/vendor/client-sdk/controller.cjs +115 -0
  95. package/vendor/client-sdk/files.cjs +38 -0
  96. package/vendor/client-sdk/index.cjs +133 -0
  97. package/vendor/client-sdk/login.cjs +52 -0
  98. package/vendor/client-sdk/updates.cjs +41 -0
  99. package/vendor/cloud-cli/mlai.mjs +394 -0
  100. package/worker/architecture_parse.cjs +23 -0
  101. package/worker/architecture_worker.py +394 -0
  102. package/worker/ask_first.py +96 -0
  103. package/worker/attachment_reader.py +66 -0
  104. package/worker/background_jobs.py +111 -0
  105. package/worker/brief_questions.py +56 -0
  106. package/worker/browser_local.py +49 -0
  107. package/worker/browser_pane.py +89 -0
  108. package/worker/browser_tool.py +101 -0
  109. package/worker/build_planner.py +187 -0
  110. package/worker/chat_fork.py +277 -0
  111. package/worker/checkpoints.py +133 -0
  112. package/worker/cloud_pty.py +351 -0
  113. package/worker/command_policy.py +39 -0
  114. package/worker/command_safety.py +717 -0
  115. package/worker/computer_card.sh +23 -0
  116. package/worker/computer_setup.sh +86 -0
  117. package/worker/computer_use.py +221 -0
  118. package/worker/connector_search.py +43 -0
  119. package/worker/context_manager.py +60 -0
  120. package/worker/conversation.py +144 -0
  121. package/worker/deploy_pack.py +116 -0
  122. package/worker/design_director.py +240 -0
  123. package/worker/dev_jobs.py +166 -0
  124. package/worker/edit_guard.py +99 -0
  125. package/worker/env_setup.py +129 -0
  126. package/worker/evidence.py +109 -0
  127. package/worker/fast_search.py +24 -0
  128. package/worker/git_sync.py +333 -0
  129. package/worker/goals.py +156 -0
  130. package/worker/handover.py +98 -0
  131. package/worker/harness_memory.py +194 -0
  132. package/worker/image_generation.py +173 -0
  133. package/worker/intelligence.py +149 -0
  134. package/worker/intent_model.py +78 -0
  135. package/worker/intent_weights.json +1 -0
  136. package/worker/ios_simulator.py +87 -0
  137. package/worker/kimi_bridge.py +997 -0
  138. package/worker/kimi_compact.py +93 -0
  139. package/worker/kimi_gates.py +304 -0
  140. package/worker/kimi_hook.py +617 -0
  141. package/worker/lifecycle.py +95 -0
  142. package/worker/lsp.py +77 -0
  143. package/worker/mcp_local.py +293 -0
  144. package/worker/memory.py +99 -0
  145. package/worker/mlai_auto_review.py +177 -0
  146. package/worker/mlai_hooks.py +966 -0
  147. package/worker/mlai_mcp.py +441 -0
  148. package/worker/mlai_permissions.py +283 -0
  149. package/worker/model_stream.py +75 -0
  150. package/worker/nested_repos.py +76 -0
  151. package/worker/offline_guard.py +17 -0
  152. package/worker/preview.py +806 -0
  153. package/worker/proc_watch.py +124 -0
  154. package/worker/project_context.py +72 -0
  155. package/worker/project_rules.py +99 -0
  156. package/worker/quality.py +180 -0
  157. package/worker/reply_language.py +56 -0
  158. package/worker/repo_hygiene.py +132 -0
  159. package/worker/repo_memory.py +101 -0
  160. package/worker/repo_policy.py +269 -0
  161. package/worker/roles.json +3173 -0
  162. package/worker/roles.py +303 -0
  163. package/worker/runner.py +1795 -0
  164. package/worker/runtime_control.py +205 -0
  165. package/worker/secret_scan.py +168 -0
  166. package/worker/session_hub.py +80 -0
  167. package/worker/skill_library.py +129 -0
  168. package/worker/skill_pack.py +533 -0
  169. package/worker/subagents.py +91 -0
  170. package/worker/task_controller.py +139 -0
  171. package/worker/task_skills.py +159 -0
  172. package/worker/team_checkpoint.py +33 -0
  173. package/worker/token_count.py +32 -0
  174. package/worker/tool_catalog.py +38 -0
  175. package/worker/upload_storage.py +531 -0
  176. package/worker/verification.py +725 -0
  177. package/worker/vision.py +84 -0
  178. package/worker/wakeups.py +63 -0
  179. package/worker/web_tools.py +203 -0
  180. package/worker/winproc.py +135 -0
  181. package/worker/work_ledger.py +117 -0
  182. package/worker/worktrees.py +79 -0
package/src/runner.mjs ADDED
@@ -0,0 +1,127 @@
1
+ // mlai runner: the engine on a remote machine (an SSH machine set up by the server, or a self-hosted runner container).
2
+ // It signs in with its runner token (MLAI_RUNNER_TOKEN, lib/machines.ts on the server), keeps a short-lived device
3
+ // credential, reports a heartbeat, and starts chats when the account asks. Chats then run exactly like on This Mac:
4
+ // the local workspace API, the server-built run request and the model gateway. When the relay is available
5
+ // (src/relay.mjs), the account's other devices drive those chats live.
6
+ import {execFile} from 'node:child_process';
7
+ import {mkdir,readFile,writeFile,stat,chmod} from 'node:fs/promises';
8
+ import os from 'node:os';
9
+ import path from 'node:path';
10
+ import {VERSION,home} from './home.mjs';
11
+ import {Cloud} from './cloud.mjs';
12
+
13
+ export const RUNNER_LIMITS={heartbeatMs:30000,pollMs:20000,refreshBeforeMs:86400000,maxBackoffMs:60000,cloneMs:600000};
14
+ const sleep=(ms,signal)=>new Promise(r=>{const t=setTimeout(r,ms);t.unref?.();signal?.addEventListener('abort',()=>{clearTimeout(t);r()},{once:true})});
15
+ const loopback=u=>/^http:\/\/(127\.0\.0\.1|localhost|\[::1\])(:\d+)?$/.test(u);
16
+ export class RunnerError extends Error{constructor(message,status=0,code=''){super(message);this.name='RunnerError';this.status=status;this.code=code}}
17
+
18
+ export function runnerConfig(env=process.env){
19
+ const server=String(env.MLAI_SERVER||'').replace(/\/$/,''),token=String(env.MLAI_RUNNER_TOKEN||'').trim();
20
+ if(!token)throw new RunnerError('Set MLAI_RUNNER_TOKEN to the runner token from Settings, Machines.');
21
+ if(!/^mlrr_[A-Za-z0-9_-]+\.[A-Za-z0-9_-]{20,}$/.test(token))throw new RunnerError('MLAI_RUNNER_TOKEN is not a runner token. Copy it again from Settings, Machines.');
22
+ if(!/^https:\/\/[^/\s]+$/.test(server)&&!loopback(server))throw new RunnerError('Set MLAI_SERVER to the ML AI Code address, for example https://code.ml.ai');
23
+ const root=expand(String(env.MLAI_WORK_ROOT||'~/mlai-work'));
24
+ return {server,token,workRoot:root,label:String(env.MLAI_RUNNER_LABEL||os.hostname()).slice(0,60)};
25
+ }
26
+ export const expand=p=>p==='~'?os.homedir():p.startsWith('~/')?path.join(os.homedir(),p.slice(2)):path.resolve(p);
27
+ /** A new chat's folder must be inside the work root (the server picks it; the machine checks it again). */
28
+ export function insideRoot(root,p){const r=path.resolve(root),q=path.resolve(expand(String(p||'')));return q===r||q.startsWith(r+path.sep)?q:null}
29
+
30
+ async function call(server,token,body,{fetchImpl=globalThis.fetch,signal,timeoutMs=45000}={}){
31
+ const r=await fetchImpl(server+'/api/machines',{method:'POST',headers:{'Content-Type':'application/json',Authorization:'Bearer '+token},body:JSON.stringify(body),signal:signal?AbortSignal.any([signal,AbortSignal.timeout(timeoutMs)]):AbortSignal.timeout(timeoutMs)});
32
+ let data={};try{data=await r.json()}catch{}
33
+ if(!r.ok)throw new RunnerError(data.error||'Server answered '+r.status,r.status,data.code||'');
34
+ return data;
35
+ }
36
+
37
+ const credFile=()=>path.join(home(),'runner-credential.json');
38
+ async function loadCred(server){try{const c=JSON.parse(await readFile(credFile(),'utf8'));return c.server===server&&c.token?c:null}catch{return null}}
39
+ async function saveCred(c){await writeFile(credFile(),JSON.stringify(c),{mode:0o600});await chmod(credFile(),0o600)}
40
+
41
+ function cloneRepo(repo,root,{timeoutMs=RUNNER_LIMITS.cloneMs}={}){
42
+ if(!/^https:\/\/[^\s'"]+$/.test(repo))throw new RunnerError('Use an https:// repository address');
43
+ return new Promise((resolve,reject)=>execFile('git',['clone','--quiet','--',repo,root],{timeout:timeoutMs,env:{...process.env,GIT_TERMINAL_PROMPT:'0'}},(e,_o,err)=>e?reject(new RunnerError('Could not clone the repository: '+String(err||e.message).trim().split('\n').pop().slice(0,300))):resolve()));
44
+ }
45
+
46
+ /**
47
+ * Start the runner. Returns {stop, done}. Everything is injectable for tests: env, fetchImpl, workspaces (the local
48
+ * workspace API), relay (a startRelay function or null), clone and now.
49
+ */
50
+ export async function startRunner({env=process.env,fetchImpl=globalThis.fetch,workspaces=null,relay,clone=cloneRepo,warm=null,limits={},log=m=>process.stderr.write(m+'\n'),now=Date.now}={}){
51
+ const L={...RUNNER_LIMITS,...limits},cfg=runnerConfig(env),ctl=new AbortController();
52
+ await mkdir(cfg.workRoot,{recursive:true});
53
+ let cred=await loadCred(cfg.server),cloud=null,enrolling=null;
54
+ const enroll=async()=>{
55
+ for(let i=0;;i++){
56
+ try{const r=await call(cfg.server,cfg.token,{action:'enroll'},{fetchImpl,signal:ctl.signal});cred={server:cfg.server,token:r.token,expires:r.expires,deviceId:r.deviceId,label:r.label,machine:r.machine};await saveCred(cred);cloud=null;log('Signed in as '+(r.label||'runner'));return cred}
57
+ catch(e){if(e.status===401||ctl.signal.aborted)throw e;if(e.status!==429&&i>4)throw e;await sleep(Math.min(L.maxBackoffMs,(e.status===429?16000:2000*(i+1))),ctl.signal)}
58
+ }
59
+ };
60
+ const ensureCred=async(force=false)=>{
61
+ if(!force&&cred&&cred.expires-now()>L.refreshBeforeMs)return cred;
62
+ if(!enrolling)enrolling=enroll().finally(()=>{enrolling=null});
63
+ return await enrolling;
64
+ };
65
+ const getCloud=async()=>{const c=await ensureCred();if(!cloud||cloud.token!==c.token)cloud=new Cloud({server:cfg.server,token:c.token,fetchImpl});return cloud};
66
+ if(!workspaces){const {createLocalWorkspaces}=await import('./workspace-api.mjs');workspaces=createLocalWorkspaces({cloud:getCloud,device:cfg.label})}
67
+
68
+ const create=async({root,folder,title,repo}={})=>{
69
+ const dir=insideRoot(cfg.workRoot,root||(folder?path.join(cfg.workRoot,folder):''));
70
+ if(!dir||dir===path.resolve(cfg.workRoot))throw new RunnerError('New chats go in a folder inside '+cfg.workRoot,400,'machine_folder');
71
+ let exists=false;try{exists=(await stat(dir)).isDirectory()}catch{}
72
+ if(repo&&!exists)await clone(repo,dir);else await mkdir(dir,{recursive:true});
73
+ return await workspaces.create({root:dir,title:title||path.basename(dir)});
74
+ };
75
+ await ensureCred();
76
+ let relayHandle=null;
77
+ if(relay===undefined)relay=await import('./relay.mjs').then(m=>m.startRelay).catch(()=>null);
78
+ if(typeof relay==='function'){try{relayHandle=relay({workspaces,getCloud,create});log('Remote control is on')}catch(e){log('Remote control is unavailable: '+e.message)}}
79
+ if(warm)Promise.resolve().then(warm).catch(e=>log('Engine runtime is not ready yet: '+e.message));
80
+
81
+ const results=[],done=new Set();
82
+ const info=async()=>{
83
+ let sessions=0;try{sessions=(await workspaces.list()).length}catch{}
84
+ return {version:VERSION,os:os.platform()+' '+os.release(),arch:os.arch(),node:process.version,cpus:os.cpus().length,memGB:Math.round(os.totalmem()/1e8)/10,load:os.loadavg()[0],sessions,relay:Boolean(relayHandle),workRoot:cfg.workRoot};
85
+ };
86
+ const heartbeat=async()=>{
87
+ const sent=results.splice(0);
88
+ try{const r=await call(cfg.server,cfg.token,{action:'heartbeat',info:await info(),results:sent},{fetchImpl,signal:ctl.signal});if(r.refresh)await ensureCred(true)}
89
+ catch(e){results.unshift(...sent);throw e}
90
+ };
91
+ const handle=async c=>{
92
+ done.add(c.id);
93
+ if(c.op!=='create'){results.push({id:c.id,ok:false,error:'This runner does not know that command. Update it.'});return}
94
+ try{const out=await create(c.body||{});results.push({id:c.id,ok:true,data:{token:out.token,name:out.name,title:out.title,root:out.root,folder:out.folder}})}
95
+ catch(e){results.push({id:c.id,ok:false,error:String(e.message||e).slice(0,400)})}
96
+ await heartbeat().catch(()=>{});
97
+ };
98
+ const loop=async()=>{
99
+ let backoff=0,lastBeat=0;
100
+ while(!ctl.signal.aborted){
101
+ try{
102
+ if(now()-lastBeat>=L.heartbeatMs){await heartbeat();lastBeat=now()}
103
+ const r=await call(cfg.server,cfg.token,{action:'poll',done:[...done].slice(-50),wait:L.pollMs},{fetchImpl,signal:ctl.signal,timeoutMs:L.pollMs+15000});
104
+ for(const c of r.commands||[])if(!done.has(c.id))await handle(c);
105
+ backoff=0;
106
+ }catch(e){
107
+ if(ctl.signal.aborted)break;
108
+ if(e.status===401&&e.code==='runner_token_invalid'){log(e.message);ctl.abort();throw e}
109
+ backoff=Math.min(L.maxBackoffMs,backoff?backoff*2:2000);log('Connection problem, retrying in '+Math.round(backoff/1000)+' s: '+e.message);await sleep(backoff,ctl.signal);
110
+ }
111
+ }
112
+ };
113
+ const finished=loop();
114
+ return {config:cfg,create,heartbeat,stop:async()=>{ctl.abort();relayHandle?.stop?.();await finished.catch(()=>{});await workspaces.close?.()},done:finished};
115
+ }
116
+
117
+ /** `mlai runner`: run until stopped. Exit code 2 when the token is invalid, so the service manager logs it plainly. */
118
+ export async function runnerMain(){
119
+ const {ensureRuntime}=await import('./runtime.mjs');
120
+ const r=await startRunner({warm:()=>ensureRuntime()});
121
+ process.stderr.write('ML AI Code runner '+VERSION+' is running. Work folder: '+r.config.workRoot+'\n');
122
+ const stopped=new Promise(res=>{process.once('SIGINT',res);process.once('SIGTERM',res)});
123
+ const outcome=await Promise.race([stopped.then(()=>'stop'),r.done.then(()=>'done',e=>e)]);
124
+ await r.stop();
125
+ if(outcome instanceof Error){process.stderr.write(outcome.message+'\n');return 2}
126
+ return 0;
127
+ }
@@ -0,0 +1,75 @@
1
+ import {execFile,spawn} from 'node:child_process';
2
+ import {promisify} from 'node:util';
3
+ import {existsSync} from 'node:fs';
4
+ import {rm,writeFile,readFile} from 'node:fs/promises';
5
+ import path from 'node:path';
6
+ import {sub} from './home.mjs';
7
+ const run=promisify(execFile);
8
+ /** Pinned engine dependencies. Same version as cloud computers, so behavior matches. */
9
+ export const PY_PACKAGES=['mini-swe-agent==2.4.6'];
10
+ export const PY_VERSION='3.12';
11
+ const key='py'+PY_VERSION+'-'+PY_PACKAGES.join('_').replace(/[^\w.]+/g,'-');
12
+ const bin=dir=>process.platform==='win32'?path.join(dir,'Scripts','python.exe'):path.join(dir,'bin','python');
13
+
14
+ async function which(name){
15
+ const own=process.env.MLAI_UV&&name==='uv'?process.env.MLAI_UV:null;if(own&&existsSync(own))return own;
16
+ try{const {stdout}=await run(process.platform==='win32'?'where':'which',[name]);return stdout.split(/\r?\n/)[0].trim()||null}catch{return null}
17
+ }
18
+ /** "3.9.6" for a Python executable, or null. */
19
+ export async function pythonVersion(python){
20
+ if(!python)return null;
21
+ try{const {stdout}=await run(python,['-c','import sys;print("%d.%d.%d"%sys.version_info[:3])'],{timeout:20000});return stdout.trim()||null}catch{return null}
22
+ }
23
+ const recent=v=>{const [a,b]=String(v||'').split('.').map(Number);return a>3||(a===3&&b>=10)};
24
+ /**
25
+ * Without uv, the newest Python 3.10+ on this computer. A stock Mac's python3 is 3.9, which cannot install the
26
+ * engine, so versioned names and the usual Homebrew and python.org locations are tried too (apps opened from
27
+ * Finder do not see the shell's PATH).
28
+ */
29
+ export async function systemPython({candidates}={}){
30
+ const names=['python3.13','python3.12','python3.11','python3.10','python3'];
31
+ const list=candidates||[
32
+ ...(await Promise.all(names.map(which))),
33
+ ...(process.platform==='win32'?[]:['/opt/homebrew/bin','/usr/local/bin','/Library/Frameworks/Python.framework/Versions/Current/bin'].flatMap(d=>names.map(n=>path.join(d,n))).filter(p=>existsSync(p)))
34
+ ].filter(Boolean);
35
+ for(const p of [...new Set(list)])if(recent(await pythonVersion(p)))return p;
36
+ return null;
37
+ }
38
+ async function works(python){
39
+ try{await run(python,['-c','import minisweagent,sys;assert sys.version_info>=(3,10)'],{timeout:60000});return true}catch{return false}
40
+ }
41
+ /**
42
+ * uv uses the operating system's certificate store, so company proxies with their own CA work as they do in the browser.
43
+ * A private Python for the harness in ~/.mlai/runtime. Uses the bundled or installed uv, which also downloads
44
+ * a standalone Python, so the developer installs nothing. Falls back to python3 -m venv. Never touches system Python.
45
+ */
46
+ let inflight=null;
47
+ /** Single flight: the app warms the runtime when a folder opens, and the first task may ask for it at the same time. */
48
+ export function ensureRuntime(opts={}){
49
+ if(process.env.MLAI_PYTHON)return Promise.resolve(process.env.MLAI_PYTHON);
50
+ if(!inflight)inflight=prepareRuntime(opts).finally(()=>{inflight=null});
51
+ else if(opts.onProgress)opts.onProgress('Preparing the coding engine');
52
+ return inflight;
53
+ }
54
+ async function prepareRuntime({onProgress=()=>{},force=false}={}){
55
+ const dir=path.join(sub('runtime'),key),python=bin(dir),marker=path.join(dir,'.mlai-ready');
56
+ if(!force&&existsSync(marker)&&await works(python))return python;
57
+ await rm(dir,{recursive:true,force:true});
58
+ const uv=await which('uv');
59
+ const step=async(label,cmd,args)=>{onProgress(label);await new Promise((resolve,reject)=>{const c=spawn(cmd,args,{stdio:['ignore','pipe','pipe'],env:{UV_SYSTEM_CERTS:'1',UV_NATIVE_TLS:'1',...process.env,UV_PYTHON_PREFERENCE:'managed',PIP_DISABLE_PIP_VERSION_CHECK:'1'}});let err='';c.stderr.on('data',d=>{err=(err+d).slice(-4000)});c.on('error',reject);c.on('exit',code=>code===0?resolve():reject(new Error(label+' failed: '+err.trim().split('\n').slice(-3).join(' '))))})};
60
+ if(uv){
61
+ await step('Preparing Python '+PY_VERSION,uv,['venv','--quiet','--python',PY_VERSION,dir]);
62
+ await step('Installing the coding engine',uv,['pip','install','--quiet','--python',python,...PY_PACKAGES]);
63
+ }else{
64
+ const sys=await systemPython();if(!sys)throw new Error('The coding engine needs Python 3.10 or newer the first time, and this computer only has '+(await pythonVersion(await which('python3'))||'no Python')+'. Install uv (https://docs.astral.sh/uv/) or Python 3.12, then choose Prepare now.');
65
+ await step('Preparing Python',sys,['-m','venv',dir]);
66
+ await step('Installing the coding engine',python,['-m','pip','install','--quiet',...PY_PACKAGES]);
67
+ }
68
+ if(!await works(python))throw new Error('The coding engine did not install correctly. Run `mlai doctor`.');
69
+ await writeFile(marker,new Date().toISOString());
70
+ return python;
71
+ }
72
+ export async function runtimeInfo(){
73
+ const dir=path.join(sub('runtime'),key);
74
+ return {dir,ready:existsSync(path.join(dir,'.mlai-ready')),uv:await which('uv'),python3:await which('python3')};
75
+ }
@@ -0,0 +1,135 @@
1
+ import {existsSync,realpathSync,statSync,readFileSync,writeFileSync,mkdirSync} from 'node:fs';
2
+ import {randomInt} from 'node:crypto';
3
+ import {fileURLToPath} from 'node:url';
4
+ import {execFileSync} from 'node:child_process';
5
+ import os from 'node:os';
6
+ import path from 'node:path';
7
+
8
+ /** Credentials that commands never need to read, even in Ask mode's sandboxed runs. */
9
+ export const PRIVATE_DIRS=['.ssh','.aws','.gnupg','.config/gcloud','.kube','.docker','.azure','.netrc','.npmrc','.pypirc','.git-credentials','.config/gh','Library/Keychains','.vault-token','.config/op','.config/doctl','.oci','.gem/credentials','.terraform.d'];
10
+ /**
11
+ * Hard-deny paths from Settings (request.extensions.hardDeny.paths) that are absolute or under ~: the fixed part before
12
+ * the first wildcard becomes an OS-level deny, so even a command the hook check misses cannot read or write there.
13
+ * Repository-relative patterns stay with the worker's check. Roots that would cover home, / or the project are ignored.
14
+ */
15
+ export function denyRoots(patterns,{home=os.homedir(),repo}={}){
16
+ const out=new Set();
17
+ for(const raw of Array.isArray(patterns)?patterns.slice(0,100):[]){
18
+ if(typeof raw!=='string')continue;
19
+ let p=raw.trim();if(p==='~'||p.startsWith('~/'))p=path.join(home,p.slice(1));
20
+ if(!path.isAbsolute(p))continue;
21
+ const fixed=p.split('/').filter((seg,i,all)=>!all.slice(0,i+1).some(x=>/[*?[\]{}]/.test(x))).join('/')||'/';
22
+ const root=path.resolve(fixed);
23
+ if(root.split('/').filter(Boolean).length<2||root===path.resolve(home)||(repo&&(path.resolve(repo)===root||path.resolve(repo).startsWith(root+'/'))))continue;
24
+ out.add(root);
25
+ }
26
+ return [...out].slice(0,50);
27
+ }
28
+ /** Package caches and toolchain homes that builds and installs legitimately write to. */
29
+ export const CACHE_DIRS=['.npm','.cache','.cargo','.rustup','.bun','.pnpm-store','.local/share/pnpm','.yarn','go','.gradle','.m2','.nuget','Library/Caches','.deno','.pyenv','.nvm','.volta','.local/share/uv'];
30
+
31
+ const real=p=>{try{return realpathSync(p)}catch{return path.resolve(p)}};
32
+ function has(cmd){try{execFileSync(process.platform==='win32'?'where':'which',[cmd],{stdio:'ignore'});return true}catch{return false}}
33
+
34
+ /** Sandboxes that enforce the write limits; anything else means commands run with the user's full permissions. */
35
+ export const SANDBOXES=['seatbelt','bwrap','windows'];
36
+ /** Which OS sandbox this machine can use for Auto mode. */
37
+ export function sandboxKind(){
38
+ if(process.env.MLAI_SANDBOX==='off')return 'off';
39
+ if(process.platform==='darwin'&&existsSync('/usr/bin/sandbox-exec'))return 'seatbelt';
40
+ if(process.platform==='linux'&&has('bwrap'))return 'bwrap';
41
+ if(process.platform==='win32')return 'windows';
42
+ return 'none';
43
+ }
44
+ /**
45
+ * macOS Seatbelt profile: everything runs normally, but writes are limited to the project, the session's state,
46
+ * temp folders and package caches, and credential folders cannot be read. Network stays on (the model, package installs)
47
+ * unless network is false (see NETWORK_OFF).
48
+ */
49
+ export function seatbeltProfile({home=os.homedir(),extraRead=[],extraDeny=[],network=true}={}){
50
+ // Seatbelt matches resolved paths (/var is /private/var on macOS), so rules use the real home folder. macOS paths
51
+ // only: on another host (the profile tests) path.resolve would turn /Users/dev into D:\Users\dev.
52
+ home=process.platform==='win32'?home:real(home);
53
+ const sp=s=>JSON.stringify(s);
54
+ const caches=CACHE_DIRS.map(d=>`(subpath ${sp(path.posix.join(home,d))})`).join(' ');
55
+ const secrets=PRIVATE_DIRS.map(d=>`(subpath ${sp(path.posix.join(home,d))})`).join(' ');
56
+ return `(version 1)
57
+ (allow default)
58
+ (deny file-write*)
59
+ (allow file-write*
60
+ (subpath (param "REPO")) (subpath (param "STATE")) (subpath (param "TMP"))
61
+ (subpath "/private/tmp") (subpath "/private/var/folders") (subpath "/tmp")
62
+ (literal "/dev/null") (literal "/dev/zero") (literal "/dev/dtracehelper") (regex #"^/dev/tty") (regex #"^/dev/fd/")
63
+ ${caches})
64
+ (deny file-read* ${secrets} (subpath (param "MLAI_HOME")))
65
+ (allow file-read* (subpath (param "STATE")) (subpath (param "RUNTIME")) (subpath (param "WORKER")))
66
+ ${extraReadRules(extraRead)}${extraDenyRules(extraDeny)}${network===false?NETWORK_OFF:''}`;
67
+ }
68
+ /**
69
+ * Network off for Seatbelt (src/netbox.mjs): no network at all except loopback (the model relay, dev servers on
70
+ * localhost) and Unix sockets. Remote hosts and DNS lookups through the network fail with EPERM.
71
+ */
72
+ export const NETWORK_OFF=`(deny network*)
73
+ (allow network-bind network-inbound (local ip "localhost:*"))
74
+ (allow network-outbound (remote ip "localhost:*"))
75
+ (allow network-outbound (remote unix-socket))
76
+ `;
77
+ /** Seatbelt profile that changes nothing but the network (guided mode, where commands are approved one by one). */
78
+ export function seatbeltNetworkProfile(){return '(version 1)\n(allow default)\n'+NETWORK_OFF}
79
+ /** Hard-deny roots come last so they win over every allow above. */
80
+ function extraDenyRules(list){return list.length?'(deny file-read* file-write* '+list.map(p=>'(subpath '+JSON.stringify(real(p))+')').join(' ')+')\n':''}
81
+ /** Read-only folders inside ~/.mlai the agent's processes need (the test browser in ~/.mlai/runtime/pw-browsers). Last rule wins, so these follow the MLAI_HOME deny. */
82
+ function extraReadRules(list){return list.length?'(allow file-read* '+list.map(p=>'(subpath '+JSON.stringify(real(p))+')').join(' ')+')\n':''}
83
+ /**
84
+ * Returns the command that runs `cmd args` inside the sandbox, or null when none is available.
85
+ * network: a relay box from src/netbox.mjs ({port, socket, dir, python?}) switches the network off at the OS level and leaves
86
+ * only the relay; omitted, the network is on. netOnly: limit nothing but the network (guided mode with network off).
87
+ */
88
+ export function wrap(cmd,args,{repo,state,runtime,worker,mlaiHome,home=os.homedir(),tmp=os.tmpdir(),busybox,extraRead=[],extraDeny=[],network=null,netOnly=false}){
89
+ const kind=sandboxKind();
90
+ const R=real(repo),S=real(state),RT=real(runtime),W=real(worker),M=real(mlaiHome),T=real(tmp);
91
+ if(kind==='seatbelt'){
92
+ if(netOnly&&network)return {kind,network:'os',cmd:'/usr/bin/sandbox-exec',args:['-p',seatbeltNetworkProfile(),cmd,...args]};
93
+ return {kind,...(network?{network:'os'}:{}),cmd:'/usr/bin/sandbox-exec',args:['-p',seatbeltProfile({home,extraRead,extraDeny,network:!network}),'-D','REPO='+R,'-D','STATE='+S,'-D','TMP='+T,'-D','RUNTIME='+RT,'-D','WORKER='+W,'-D','MLAI_HOME='+M,cmd,...args]};
94
+ }
95
+ if(kind==='bwrap'){
96
+ // Network off: an empty network namespace, the relay's socket folder bound in, and netjail.py (run by the same
97
+ // Python as the harness) forwarding 127.0.0.1:<port> to it.
98
+ const jail=real(NET_JAIL);
99
+ // The launcher is bound explicitly: an installed CLI lives under ~/.mlai, which the sandbox hides.
100
+ const net=network?{args:['--unshare-net','--ro-bind',jail,jail,...(network.dir?['--bind',real(network.dir),real(network.dir)]:[])],launch:[network.python||cmd,jail,'--port',String(network.port),'--unix',network.dir?path.join(real(network.dir),path.basename(network.socket)):network.socket,'--']}:{args:[],launch:[]};
101
+ if(netOnly&&network)return {kind,network:'os',cmd:'bwrap',args:['--die-with-parent','--bind','/','/','--dev','/dev','--proc','/proc',...net.args,'--chdir',R,...net.launch,cmd,...args]};
102
+ const b=['--die-with-parent','--ro-bind','/','/','--dev','/dev','--proc','/proc','--tmpfs','/tmp'];
103
+ for(const d of CACHE_DIRS){const p=path.join(home,d);if(existsSync(p))b.push('--bind',p,p)}
104
+ for(const d of PRIVATE_DIRS){const p=path.join(home,d);if(existsSync(p))b.push(...(isDir(p)?['--tmpfs',p]:['--ro-bind','/dev/null',p]))}
105
+ b.push('--tmpfs',M,'--ro-bind',RT,RT,'--ro-bind',W,W,'--bind',S,S,'--bind',R,R);
106
+ for(const p of extraRead.map(real))if(existsSync(p))b.push('--ro-bind',p,p);
107
+ if(T!=='/tmp'&&!T.startsWith('/tmp/'))b.push('--bind',T,T);
108
+ for(const p of extraDeny.map(real))if(existsSync(p))b.push(...(isDir(p)?['--tmpfs',p]:['--ro-bind','/dev/null',p]));
109
+ return {kind,...(network?{network:'os'}:{}),cmd:'bwrap',args:[...b,...net.args,'--chdir',R,...net.launch,cmd,...args]};
110
+ }
111
+ if(netOnly)return null; // Windows: no OS-level network off without administrator rights (src/netbox.mjs)
112
+ if(kind==='windows'){
113
+ const tmpDir=path.join(S,'tmp');mkdirSync(tmpDir,{recursive:true});
114
+ const writes=[R,S,...WIN_CACHE_DIRS.map(d=>winCache(d,home)).filter(p=>p&&existsSync(p))];
115
+ const l=[WIN_LAUNCHER,'--sid',windowsSandboxSid(M)];for(const w of writes)l.push('--write',w);
116
+ // The launcher runs in the harness's own Python (cmd is that Python); it starts `cmd args` under the restricted token.
117
+ return {kind,cmd,args:[...l,'--',cmd,...args],env:{TEMP:tmpDir,TMP:tmpDir,...(busybox?{MLAI_BUSYBOX:busybox}:{})}};
118
+ }
119
+ return null;
120
+ }
121
+ /** Package caches on Windows that builds and installs legitimately write to (only those that exist are allowed). */
122
+ export const WIN_CACHE_DIRS=['%LOCALAPPDATA%\\npm-cache','%APPDATA%\\npm-cache','%LOCALAPPDATA%\\pip\\Cache','%LOCALAPPDATA%\\uv\\cache','%LOCALAPPDATA%\\pnpm','%LOCALAPPDATA%\\Yarn','~\\.cargo','~\\.rustup','~\\.nuget\\packages','~\\go','~\\.gradle','~\\.m2','~\\.bun','~\\.cache'];
123
+ function winCache(d,home){
124
+ return d.replace(/^~/,home).replace(/%(\w+)%/g,(_,k)=>process.env[k]||'\0');
125
+ }
126
+ const NET_JAIL=path.join(path.dirname(fileURLToPath(import.meta.url)),'netjail.py');
127
+ export const WIN_LAUNCHER=path.join(path.dirname(fileURLToPath(import.meta.url)),'winsandbox.py');
128
+ /** A machine-local SID that only the sandbox's token carries; folders the agent may write grant it access. */
129
+ export function windowsSandboxSid(mlaiHome){
130
+ const f=path.join(mlaiHome,'sandbox-sid');
131
+ try{const v=readFileSync(f,'utf8').trim();if(/^S-1-5-21-\d+-\d+-\d+-\d+$/.test(v))return v}catch{}
132
+ const v='S-1-5-21-'+[0,0,0].map(()=>randomInt(1e8,4e9)).join('-')+'-'+randomInt(1000,1e6);
133
+ writeFileSync(f,v);return v;
134
+ }
135
+ function isDir(p){try{return statSync(p).isDirectory()}catch{return false}}
package/src/sdk.mjs ADDED
@@ -0,0 +1,11 @@
1
+ import {createRequire} from 'node:module';
2
+ import path from 'node:path';
3
+ import {fileURLToPath} from 'node:url';
4
+ import {existsSync} from 'node:fs';
5
+ /** The shared client SDK (device sign-in, HTTP client). Bundled as vendor/client-sdk in releases. */
6
+ const here=path.dirname(fileURLToPath(import.meta.url));
7
+ const dir=[path.join(here,'..','vendor','client-sdk'),path.join(here,'..','..','..','packages','client-sdk')].find(d=>existsSync(path.join(d,'index.cjs')));
8
+ if(!dir)throw new Error('The ML AI Code client library is missing from this install. Reinstall ML AI Code.');
9
+ const require=createRequire(import.meta.url);
10
+ export const {Client,ClientError,serverOrigin}=require(path.join(dir,'index.cjs'));
11
+ export const {login:deviceLogin}=require(path.join(dir,'login.cjs'));
package/src/server.mjs ADDED
@@ -0,0 +1,110 @@
1
+ // mlai serve: the engine host as a small HTTP API on an owner-only Unix socket, for editors and other tools.
2
+ // Clients read events as Server-Sent Events and send approvals, steering and cancel.
3
+ import {createServer,request} from 'node:http';
4
+ import {rm,chmod} from 'node:fs/promises';
5
+ import {existsSync,readFileSync,writeFileSync} from 'node:fs';
6
+ import {randomBytes,createHash,timingSafeEqual} from 'node:crypto';
7
+ import path from 'node:path';
8
+ import {home,DEFAULT_SERVER} from './home.mjs';
9
+ import {Cloud,friendly} from './cloud.mjs';
10
+ import {loadLogin} from './credentials.mjs';
11
+ import {EngineHost,HostError} from './host.mjs';
12
+
13
+ const WIN=process.platform==='win32';
14
+ /** Unix socket in ~/.mlai (owner-only), or on Windows a named pipe per user and data folder. */
15
+ export const socketPath=()=>process.env.MLAI_SOCKET||(WIN?'\\\\.\\pipe\\mlaid-'+createHash('sha256').update(home().toLowerCase()).digest('hex').slice(0,16):path.join(home(),'mlaid.sock'));
16
+ /**
17
+ * Named pipes have no owner-only file mode, so on Windows every request also carries a secret kept in ~/.mlai
18
+ * (which only this user can read). Clients send it as `Authorization: Bearer <token>`.
19
+ */
20
+ export function engineToken(){
21
+ const f=path.join(home(),'mlaid.token');
22
+ try{const t=readFileSync(f,'utf8').trim();if(t.length>=32)return t}catch{}
23
+ const t=randomBytes(24).toString('hex');writeFileSync(f,t,{mode:0o600});return t;
24
+ }
25
+ export const authHeaders=()=>WIN?{Authorization:'Bearer '+engineToken()}:{};
26
+ function authorized(req,token){
27
+ if(!token)return true;
28
+ const got=Buffer.from(String(req.headers.authorization||'')),want=Buffer.from('Bearer '+token);
29
+ return got.length===want.length&&timingSafeEqual(got,want);
30
+ }
31
+ const MAX_BODY=64000;
32
+
33
+ async function body(req){
34
+ let raw='';for await(const c of req){raw+=c;if(raw.length>MAX_BODY)throw new HostError('Request too large',413)}
35
+ if(!raw)return {};
36
+ try{const v=JSON.parse(raw);if(!v||typeof v!=='object'||Array.isArray(v))throw 0;return v}catch{throw new HostError('Invalid JSON')}
37
+ }
38
+ const send=(res,status,data)=>{res.writeHead(status,{'Content-Type':'application/json','Cache-Control':'no-store'});res.end(JSON.stringify(data))};
39
+ const sse=(res,type,data)=>res.write('event: '+type+'\ndata: '+JSON.stringify(data)+'\n\n');
40
+
41
+ export async function terminalCloud(){
42
+ const l=await loadLogin();if(!l?.token)throw new Error('Not signed in. Run `mlai login`.');
43
+ return new Cloud({server:(process.env.MLAI_SERVER||l.server||DEFAULT_SERVER).replace(/\/$/,''),token:l.token});
44
+ }
45
+
46
+ export function createEngineServer({cloud=terminalCloud}={}){
47
+ const host=new EngineHost({cloud});
48
+ const subscribers=new Map(); // session id -> Set<res>
49
+ host.on('session',({id,type,data})=>{for(const r of subscribers.get(id)||[])sse(r,type,data)});
50
+ const token=WIN?engineToken():null;
51
+ const server=createServer(async(req,res)=>{
52
+ try{
53
+ if(!authorized(req,token))throw new HostError('Missing or wrong engine token',401);
54
+ const url=new URL(req.url,'http://mlaid');const [v,a,id,b]=url.pathname.split('/').filter(Boolean);
55
+ if(v!=='v1')throw new HostError('Not found',404);
56
+ if(req.method==='GET'&&a==='health')return send(res,200,host.health());
57
+ if(req.method==='GET'&&a==='doctor')return send(res,200,await host.doctor());
58
+ if(req.method==='GET'&&a==='account'){
59
+ try{const me=await (await cloud()).whoami();return send(res,200,{signedIn:true,...me})}catch(e){return send(res,200,{signedIn:false,error:friendly(e)})}
60
+ }
61
+ if(a!=='sessions')throw new HostError('Not found',404);
62
+ if(!id){
63
+ if(req.method==='GET')return send(res,200,await host.sessions({root:url.searchParams.get('root')||undefined}));
64
+ if(req.method==='POST')return send(res,201,await host.create(await body(req)));
65
+ throw new HostError('Method not allowed',405);
66
+ }
67
+ if(req.method==='GET'&&!b)return send(res,200,await host.get(id));
68
+ if(req.method==='GET'&&b==='transcript')return send(res,200,await host.transcript(id));
69
+ if(req.method==='GET'&&b==='events'){
70
+ const info=await host.get(id);
71
+ res.writeHead(200,{'Content-Type':'text/event-stream','Cache-Control':'no-store',Connection:'keep-alive'});
72
+ for(const ev of host.events(id,Number(url.searchParams.get('after'))||0))sse(res,'harness',ev);
73
+ sse(res,'state',{running:info.running,result:info.result});
74
+ if(!subscribers.has(id))subscribers.set(id,new Set());subscribers.get(id).add(res);
75
+ const ping=setInterval(()=>res.write(': ping\n\n'),15000);
76
+ req.on('close',()=>{clearInterval(ping);subscribers.get(id)?.delete(res)});return;
77
+ }
78
+ if(req.method!=='POST')throw new HostError('Method not allowed',405);
79
+ const d=await body(req);
80
+ if(b==='messages')return send(res,202,await host.start(id,d));
81
+ if(b==='decisions')return send(res,200,await host.decide(id,d));
82
+ if(b==='steer')return send(res,200,await host.steer(id,d));
83
+ if(b==='cancel')return send(res,200,await host.cancel(id));
84
+ if(b==='mode')return send(res,200,await host.mode(id,d));
85
+ throw new HostError('Not found',404);
86
+ }catch(err){
87
+ if(res.headersSent)return res.end();
88
+ send(res,err instanceof HostError?err.status:500,{error:err instanceof HostError?err.message:friendly(err),...(err.code?{code:err.code}:{})});
89
+ }
90
+ });
91
+ server.host=host;
92
+ return server;
93
+ }
94
+
95
+ function alive(socket){
96
+ return new Promise(r=>{const q=request({socketPath:socket,path:'/v1/health',timeout:2000,headers:authHeaders()},res=>{res.resume();r(res.statusCode===200)});q.on('error',()=>r(false));q.on('timeout',()=>{q.destroy();r(false)});q.end()});
97
+ }
98
+ /** Listens on the owner-only socket. Refuses to start if another engine is already serving it. */
99
+ export async function serve({socket=socketPath(),cloud}={}){
100
+ if(WIN?await alive(socket):existsSync(socket)){
101
+ if(await alive(socket))throw new Error('The ML AI Code engine is already running.');
102
+ await rm(socket,{force:true});
103
+ }
104
+ const server=createEngineServer({cloud});
105
+ const old=WIN?null:process.umask(0o177);
106
+ try{await new Promise((resolve,reject)=>{server.once('error',e=>reject(e.code==='EADDRINUSE'?new Error('The ML AI Code engine is already running.'):e));server.listen(socket,resolve)})}finally{if(!WIN)process.umask(old)}
107
+ if(!WIN)await chmod(socket,0o600);
108
+ const close=async()=>{await server.host.close();server.closeAllConnections?.();server.close();if(!WIN)await rm(socket,{force:true})};
109
+ return {server,socket,close};
110
+ }