mlai-code 0.0.0-stage → 0.3.1
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 +12 -2
- package/bin/mlai.mjs +3 -0
- package/package.json +18 -3
- package/src/browser-setup.mjs +77 -0
- package/src/cli-ext.mjs +146 -0
- package/src/cli.mjs +534 -0
- package/src/cloud.mjs +29 -0
- package/src/completion.mjs +159 -0
- package/src/credentials.mjs +54 -0
- package/src/deploy.mjs +134 -0
- package/src/distribution.mjs +373 -0
- package/src/engine-key.mjs +4 -0
- package/src/engine.mjs +167 -0
- package/src/headless.mjs +196 -0
- package/src/home.mjs +22 -0
- package/src/host.mjs +123 -0
- package/src/local-models.mjs +64 -0
- package/src/modes.mjs +38 -0
- package/src/netbox.mjs +97 -0
- package/src/netjail.py +59 -0
- package/src/offline.mjs +98 -0
- package/src/parity.mjs +86 -0
- package/src/project.mjs +68 -0
- package/src/relay.mjs +140 -0
- package/src/render.mjs +73 -0
- package/src/restricted.mjs +47 -0
- package/src/review.mjs +70 -0
- package/src/runner.mjs +127 -0
- package/src/runtime.mjs +75 -0
- package/src/sandbox.mjs +135 -0
- package/src/sdk.mjs +11 -0
- package/src/server.mjs +110 -0
- package/src/session.mjs +290 -0
- package/src/settings.mjs +135 -0
- package/src/shared-data.mjs +3 -0
- package/src/tui/acp.mjs +145 -0
- package/src/tui/agents-cli.mjs +114 -0
- package/src/tui/agents-local.mjs +65 -0
- package/src/tui/ansi.mjs +123 -0
- package/src/tui/app.mjs +710 -0
- package/src/tui/approval.mjs +88 -0
- package/src/tui/bg.mjs +172 -0
- package/src/tui/bgterm.mjs +79 -0
- package/src/tui/chat.mjs +230 -0
- package/src/tui/clipboard.mjs +74 -0
- package/src/tui/cloud-cli.mjs +57 -0
- package/src/tui/cloud-run.mjs +123 -0
- package/src/tui/cmd-agents.mjs +130 -0
- package/src/tui/cmd-auto-review.mjs +94 -0
- package/src/tui/cmd-cloud.mjs +17 -0
- package/src/tui/cmd-config.mjs +104 -0
- package/src/tui/cmd-core.mjs +160 -0
- package/src/tui/cmd-distribution.mjs +15 -0
- package/src/tui/cmd-ecosystem.mjs +169 -0
- package/src/tui/cmd-extensibility.mjs +333 -0
- package/src/tui/cmd-remote.mjs +18 -0
- package/src/tui/cmd-review.mjs +58 -0
- package/src/tui/cmd-session-extra.mjs +152 -0
- package/src/tui/cmd-tui.mjs +200 -0
- package/src/tui/commands.mjs +160 -0
- package/src/tui/custom-commands.mjs +98 -0
- package/src/tui/diff.mjs +60 -0
- package/src/tui/editor.mjs +190 -0
- package/src/tui/ext-state.mjs +16 -0
- package/src/tui/extensions-local.mjs +140 -0
- package/src/tui/files.mjs +72 -0
- package/src/tui/handoff.mjs +110 -0
- package/src/tui/import-setups.mjs +151 -0
- package/src/tui/init.mjs +104 -0
- package/src/tui/keymap.mjs +70 -0
- package/src/tui/keys.mjs +101 -0
- package/src/tui/loop.mjs +75 -0
- package/src/tui/markdown.mjs +118 -0
- package/src/tui/mcp-config.mjs +225 -0
- package/src/tui/mcp-oauth.mjs +162 -0
- package/src/tui/permissions.mjs +189 -0
- package/src/tui/plain.mjs +32 -0
- package/src/tui/plugins-local.mjs +103 -0
- package/src/tui/remote.mjs +125 -0
- package/src/tui/review-cli.mjs +332 -0
- package/src/tui/review-github.mjs +59 -0
- package/src/tui/screen.mjs +60 -0
- package/src/tui/session-ops.mjs +104 -0
- package/src/tui/skills-local.mjs +92 -0
- package/src/tui/snapshots.mjs +87 -0
- package/src/tui/statusline.mjs +98 -0
- package/src/tui/transcript.mjs +116 -0
- package/src/two-words.mjs +36 -0
- package/src/wakeups.mjs +53 -0
- package/src/winenv.mjs +94 -0
- package/src/winsandbox.py +351 -0
- package/src/workspace-api.mjs +621 -0
- package/src/worktree.mjs +128 -0
- package/vendor/client-sdk/controller.cjs +115 -0
- package/vendor/client-sdk/files.cjs +38 -0
- package/vendor/client-sdk/index.cjs +133 -0
- package/vendor/client-sdk/login.cjs +52 -0
- package/vendor/client-sdk/updates.cjs +41 -0
- package/vendor/cloud-cli/mlai.mjs +394 -0
- package/worker/architecture_parse.cjs +23 -0
- package/worker/architecture_worker.py +394 -0
- package/worker/ask_first.py +96 -0
- package/worker/attachment_reader.py +66 -0
- package/worker/background_jobs.py +111 -0
- package/worker/brief_questions.py +56 -0
- package/worker/browser_local.py +49 -0
- package/worker/browser_pane.py +89 -0
- package/worker/browser_tool.py +101 -0
- package/worker/build_planner.py +187 -0
- package/worker/chat_fork.py +277 -0
- package/worker/checkpoints.py +133 -0
- package/worker/cloud_pty.py +351 -0
- package/worker/command_policy.py +39 -0
- package/worker/command_safety.py +717 -0
- package/worker/computer_card.sh +23 -0
- package/worker/computer_setup.sh +86 -0
- package/worker/computer_use.py +221 -0
- package/worker/connector_search.py +43 -0
- package/worker/context_manager.py +60 -0
- package/worker/conversation.py +144 -0
- package/worker/deploy_pack.py +116 -0
- package/worker/design_director.py +240 -0
- package/worker/dev_jobs.py +166 -0
- package/worker/edit_guard.py +99 -0
- package/worker/env_setup.py +129 -0
- package/worker/evidence.py +109 -0
- package/worker/fast_search.py +24 -0
- package/worker/git_sync.py +333 -0
- package/worker/goals.py +156 -0
- package/worker/handover.py +98 -0
- package/worker/harness_memory.py +194 -0
- package/worker/image_generation.py +173 -0
- package/worker/intelligence.py +149 -0
- package/worker/intent_model.py +78 -0
- package/worker/intent_weights.json +1 -0
- package/worker/ios_simulator.py +87 -0
- package/worker/kimi_bridge.py +997 -0
- package/worker/kimi_compact.py +93 -0
- package/worker/kimi_gates.py +304 -0
- package/worker/kimi_hook.py +617 -0
- package/worker/lifecycle.py +95 -0
- package/worker/lsp.py +77 -0
- package/worker/mcp_local.py +293 -0
- package/worker/memory.py +99 -0
- package/worker/mlai_auto_review.py +177 -0
- package/worker/mlai_hooks.py +966 -0
- package/worker/mlai_mcp.py +441 -0
- package/worker/mlai_permissions.py +283 -0
- package/worker/model_stream.py +75 -0
- package/worker/nested_repos.py +76 -0
- package/worker/offline_guard.py +17 -0
- package/worker/preview.py +806 -0
- package/worker/proc_watch.py +124 -0
- package/worker/project_context.py +72 -0
- package/worker/project_rules.py +99 -0
- package/worker/quality.py +180 -0
- package/worker/reply_language.py +56 -0
- package/worker/repo_hygiene.py +132 -0
- package/worker/repo_memory.py +101 -0
- package/worker/repo_policy.py +269 -0
- package/worker/roles.json +3173 -0
- package/worker/roles.py +303 -0
- package/worker/runner.py +1795 -0
- package/worker/runtime_control.py +205 -0
- package/worker/secret_scan.py +168 -0
- package/worker/session_hub.py +80 -0
- package/worker/skill_library.py +129 -0
- package/worker/skill_pack.py +533 -0
- package/worker/subagents.py +91 -0
- package/worker/task_controller.py +139 -0
- package/worker/task_skills.py +159 -0
- package/worker/team_checkpoint.py +33 -0
- package/worker/token_count.py +32 -0
- package/worker/tool_catalog.py +38 -0
- package/worker/upload_storage.py +531 -0
- package/worker/verification.py +725 -0
- package/worker/vision.py +84 -0
- package/worker/wakeups.py +63 -0
- package/worker/web_tools.py +203 -0
- package/worker/winproc.py +135 -0
- package/worker/work_ledger.py +117 -0
- package/worker/worktrees.py +79 -0
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How the terminal CLI is installed and kept current (CLI v1, owner 6).
|
|
3
|
+
*
|
|
4
|
+
* Managed installs (from /cli/install.sh or /cli/install.ps1) live in ~/.mlai/cli (MLAI_HOME moves it):
|
|
5
|
+
* cli/versions/<id>/ one extracted build each (<id> = <version>-<sha prefix>, see lib/cli-release.ts)
|
|
6
|
+
* cli/current.json {id, version, sha256, previous, installedAt}: which build `mlai` runs
|
|
7
|
+
* cli/launcher.mjs reads current.json and runs that build, falling back to the previous one if it is broken
|
|
8
|
+
* cli/state.json last background check and a notice to show on the next start
|
|
9
|
+
* bin/mlai (bin/mlai.cmd) the shim on PATH; it runs the launcher, so an update is a single atomic current.json write
|
|
10
|
+
*
|
|
11
|
+
* `mlai update` downloads the build named by the server manifest (/api/cli/release), checks its size and SHA-256 (and its
|
|
12
|
+
* Ed25519 signature when this copy pins a key in engine-key.mjs), extracts it next to the running one and switches
|
|
13
|
+
* current.json. npm and Homebrew installs are updated with their own package manager instead.
|
|
14
|
+
*
|
|
15
|
+
* Background check: an interactive session started on a terminal checks at most once a day, after a short delay. A
|
|
16
|
+
* managed install stages the new build so it starts next time; others print a one-line notice next time. It never
|
|
17
|
+
* runs for -p/--print, --json, non-TTY or CI, and is off with settings autoUpdate=false or MLAI_AUTO_UPDATE=0.
|
|
18
|
+
*/
|
|
19
|
+
import {createHash,createPublicKey,verify as edVerify} from 'node:crypto';
|
|
20
|
+
import {gunzipSync} from 'node:zlib';
|
|
21
|
+
import fs from 'node:fs';
|
|
22
|
+
import fsp from 'node:fs/promises';
|
|
23
|
+
import path from 'node:path';
|
|
24
|
+
import {spawn} from 'node:child_process';
|
|
25
|
+
import {fileURLToPath} from 'node:url';
|
|
26
|
+
import {VERSION,DEFAULT_SERVER,home} from './home.mjs';
|
|
27
|
+
import {ENGINE_PUBLIC_KEY} from './engine-key.mjs';
|
|
28
|
+
|
|
29
|
+
export const PROTOCOL=1;
|
|
30
|
+
export const NPM_PACKAGE='mlai-code';
|
|
31
|
+
export const BREW_FORMULA='mlai';
|
|
32
|
+
export const CHECK_EVERY_MS=24*3600_000;
|
|
33
|
+
const KEEP_VERSIONS=3;
|
|
34
|
+
const ID=/^[0-9A-Za-z.+-]{1,64}$/;
|
|
35
|
+
const SHA=/^[0-9a-f]{64}$/;
|
|
36
|
+
|
|
37
|
+
export const cliDir=()=>path.join(home(),'cli');
|
|
38
|
+
export const binDir=()=>path.join(home(),'bin');
|
|
39
|
+
const versionsDir=dir=>path.join(dir,'versions');
|
|
40
|
+
const readJson=(f,d=null)=>{try{return JSON.parse(fs.readFileSync(f,'utf8'))}catch{return d}};
|
|
41
|
+
async function writeJsonAtomic(file,v){
|
|
42
|
+
await fsp.mkdir(path.dirname(file),{recursive:true});
|
|
43
|
+
const tmp=file+'.'+process.pid+'.'+Date.now().toString(36)+'.tmp';
|
|
44
|
+
await fsp.writeFile(tmp,JSON.stringify(v,null,1)+'\n');await fsp.rename(tmp,file);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/* ---------- versions ---------- */
|
|
48
|
+
|
|
49
|
+
/** -1 / 0 / 1 for dotted numeric versions; a pre-release (1.2.0-beta) sorts before its release. */
|
|
50
|
+
export function compareVersions(a,b){
|
|
51
|
+
const p=v=>{const [main,pre]=String(v||'0').replace(/^v/,'').split('-',2);return {n:main.split('.').map(x=>parseInt(x,10)||0),pre:pre||null}};
|
|
52
|
+
const x=p(a),y=p(b);
|
|
53
|
+
for(let i=0;i<Math.max(x.n.length,y.n.length,3);i++){const d=(x.n[i]||0)-(y.n[i]||0);if(d)return d<0?-1:1}
|
|
54
|
+
if(x.pre===y.pre)return 0;if(!x.pre)return 1;if(!y.pre)return -1;return x.pre<y.pre?-1:1;
|
|
55
|
+
}
|
|
56
|
+
/** An update is the server's build when it is a different build of the same or a later version (never a downgrade). */
|
|
57
|
+
export function isUpdate(current,manifest){
|
|
58
|
+
if(!manifest?.id||!manifest?.version)return false;
|
|
59
|
+
if(current?.id&¤t.id===manifest.id)return false;
|
|
60
|
+
const c=compareVersions(manifest.version,current?.version||VERSION);
|
|
61
|
+
return c>0||(c===0&&!!current?.id&¤t.sha256!==manifest.sha256);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/* ---------- manifest + bytes ---------- */
|
|
65
|
+
|
|
66
|
+
export function canonical(m){return ['mlai-cli',m.protocol,m.version,m.id,m.sha256,m.size].join('\n')}
|
|
67
|
+
export const sha256=bytes=>createHash('sha256').update(bytes).digest('hex');
|
|
68
|
+
|
|
69
|
+
/** Throws unless the manifest is well formed and (when this copy pins a key) signed with that key. */
|
|
70
|
+
export function checkManifest(m,{publicKey=ENGINE_PUBLIC_KEY}={}){
|
|
71
|
+
if(!m||typeof m!=='object')throw new Error('The update server sent no manifest.');
|
|
72
|
+
if(m.protocol!==PROTOCOL)throw new Error('This CLI is too old to read the update manifest. Reinstall: '+installCommand());
|
|
73
|
+
if(!ID.test(String(m.id))||!SHA.test(String(m.sha256))||!Number.isInteger(m.size)||m.size<=0||!m.version)throw new Error('The update manifest is malformed.');
|
|
74
|
+
if(publicKey){
|
|
75
|
+
if(!m.signature)throw new Error('The update is not signed. Refusing to install it.');
|
|
76
|
+
let ok=false;try{ok=edVerify(null,Buffer.from(canonical(m)),createPublicKey({key:Buffer.from(publicKey,'base64'),format:'der',type:'spki'}),Buffer.from(m.signature,'base64'))}catch{}
|
|
77
|
+
if(!ok)throw new Error('The update signature does not match. Refusing to install it.');
|
|
78
|
+
}
|
|
79
|
+
return m;
|
|
80
|
+
}
|
|
81
|
+
export function verifyBytes(bytes,m){
|
|
82
|
+
if(bytes.length!==m.size)throw new Error(`The download is ${bytes.length} bytes, expected ${m.size}. Try again.`);
|
|
83
|
+
const got=sha256(bytes);
|
|
84
|
+
if(got!==m.sha256)throw new Error('The download does not match its SHA-256 checksum. Nothing was installed.');
|
|
85
|
+
return got;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/* ---------- tar.gz ---------- */
|
|
89
|
+
|
|
90
|
+
/** Rejects absolute paths, drive letters, backslashes and any `..` segment. */
|
|
91
|
+
export function safeEntry(name){
|
|
92
|
+
const n=String(name).replace(/^\.\//,'');
|
|
93
|
+
if(!n||n.includes('\\')||n.includes('\0')||n.startsWith('/')||/^[A-Za-z]:/.test(n))return null;
|
|
94
|
+
const parts=n.split('/').filter(Boolean);
|
|
95
|
+
if(!parts.length||parts.some(p=>p==='..'||p==='.'))return null;
|
|
96
|
+
return parts.join('/');
|
|
97
|
+
}
|
|
98
|
+
/** Minimal ustar reader: regular files and directories only (links and devices are refused). */
|
|
99
|
+
export function untar(gz){
|
|
100
|
+
const buf=gunzipSync(gz);const out=[];let longName=null;
|
|
101
|
+
const str=(o,l)=>{const b=buf.subarray(o,o+l);const z=b.indexOf(0);return b.subarray(0,z<0?l:z).toString('utf8')};
|
|
102
|
+
const num=(o,l)=>parseInt(str(o,l).trim()||'0',8);
|
|
103
|
+
for(let off=0;off+512<=buf.length;){
|
|
104
|
+
const h=buf.subarray(off,off+512);if(h.every(b=>b===0))break;
|
|
105
|
+
const size=num(off+124,12),mode=num(off+100,8)&0o777,type=String.fromCharCode(h[156]||48);
|
|
106
|
+
let name=str(off,100);const prefix=str(off+345,155);if(prefix&&buf.subarray(off+257,off+262).toString()==='ustar')name=prefix+'/'+name;
|
|
107
|
+
if(longName!==null){name=longName;longName=null}
|
|
108
|
+
const body=buf.subarray(off+512,off+512+size);off+=512+Math.ceil(size/512)*512;
|
|
109
|
+
if(type==='L'){longName=body.toString('utf8').replace(/\0+$/,'');continue}
|
|
110
|
+
if(type==='x'||type==='g'){const m=/\d+ path=([^\n]*)\n/.exec(body.toString('utf8'));if(m&&type==='x')longName=m[1];continue}
|
|
111
|
+
const safe=safeEntry(name);if(!safe)throw new Error('Refusing an unsafe path in the download: '+JSON.stringify(name));
|
|
112
|
+
if(type==='5'){out.push({name:safe,dir:true});continue}
|
|
113
|
+
if(type!=='0'&&type!=='\0'&&type!=='7')throw new Error('Refusing a link or special file in the download: '+safe);
|
|
114
|
+
out.push({name:safe,data:Buffer.from(body),mode});
|
|
115
|
+
}
|
|
116
|
+
return out;
|
|
117
|
+
}
|
|
118
|
+
async function extract(bytes,dest){
|
|
119
|
+
const entries=untar(bytes);
|
|
120
|
+
if(!entries.some(e=>e.name==='bin/mlai.mjs'))throw new Error('The download has no bin/mlai.mjs. Nothing was installed.');
|
|
121
|
+
for(const e of entries){
|
|
122
|
+
const p=path.join(dest,...e.name.split('/'));
|
|
123
|
+
if(!p.startsWith(dest+path.sep))throw new Error('Refusing an unsafe path in the download.');
|
|
124
|
+
if(e.dir){await fsp.mkdir(p,{recursive:true});continue}
|
|
125
|
+
await fsp.mkdir(path.dirname(p),{recursive:true});await fsp.writeFile(p,e.data,{mode:e.mode&0o111?0o755:0o644});
|
|
126
|
+
}
|
|
127
|
+
return entries.length;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/* ---------- install layout ---------- */
|
|
131
|
+
|
|
132
|
+
export function launcherSource(){
|
|
133
|
+
return `#!/usr/bin/env node
|
|
134
|
+
// ML AI Code CLI launcher (written by the installer and mlai update). Runs the build named in current.json.
|
|
135
|
+
import fs from 'node:fs';import path from 'node:path';import {fileURLToPath,pathToFileURL} from 'node:url';
|
|
136
|
+
const dir=path.dirname(fileURLToPath(import.meta.url));
|
|
137
|
+
let cur={};try{cur=JSON.parse(fs.readFileSync(path.join(dir,'current.json'),'utf8'))}catch{}
|
|
138
|
+
const ok=id=>typeof id==='string'&&/^[0-9A-Za-z.+-]{1,64}$/.test(id)&&fs.existsSync(path.join(dir,'versions',id,'bin','mlai.mjs'));
|
|
139
|
+
const id=[cur.id,cur.previous].find(ok);
|
|
140
|
+
if(!id){process.stderr.write('ML AI Code CLI is not installed correctly. Reinstall: curl -fsSL ${DEFAULT_SERVER}/cli/install.sh | sh\\n');process.exit(1)}
|
|
141
|
+
process.env.MLAI_CLI_BUILD=id;
|
|
142
|
+
await import(pathToFileURL(path.join(dir,'versions',id,'bin','mlai.mjs')).href);
|
|
143
|
+
`;
|
|
144
|
+
}
|
|
145
|
+
export function shimSources(dir=cliDir()){
|
|
146
|
+
const launcher=path.join(dir,'launcher.mjs');
|
|
147
|
+
return {
|
|
148
|
+
sh:`#!/bin/sh\n# ML AI Code CLI (managed install). Update with: mlai update\nexec node "${launcher}" "$@"\n`,
|
|
149
|
+
cmd:`@echo off\r\nrem ML AI Code CLI (managed install). Update with: mlai update\r\nnode "${launcher}" %*\r\n`,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
async function writeLayout(dir,bin,platform){
|
|
153
|
+
await fsp.writeFile(path.join(dir,'launcher.mjs'),launcherSource(),{mode:0o755});
|
|
154
|
+
await fsp.mkdir(bin,{recursive:true});
|
|
155
|
+
const s=shimSources(dir);
|
|
156
|
+
if(platform==='win32')await fsp.writeFile(path.join(bin,'mlai.cmd'),s.cmd);
|
|
157
|
+
else{await fsp.writeFile(path.join(bin,'mlai'),s.sh,{mode:0o755});await fsp.chmod(path.join(bin,'mlai'),0o755)}
|
|
158
|
+
// The previous installer put the cloud CLI here as a single file; `mlai cloud` replaces it.
|
|
159
|
+
await fsp.rm(path.join(bin,'mlai.mjs'),{force:true});
|
|
160
|
+
}
|
|
161
|
+
async function prune(dir,keep){
|
|
162
|
+
const names=await fsp.readdir(versionsDir(dir)).catch(()=>[]);
|
|
163
|
+
const stale=names.filter(n=>n.includes('.tmp-'));
|
|
164
|
+
const builds=[];
|
|
165
|
+
for(const n of names.filter(n=>!n.includes('.tmp-')&&!keep.includes(n))){const st=await fsp.stat(path.join(versionsDir(dir),n)).catch(()=>null);if(st)builds.push({n,t:st.mtimeMs})}
|
|
166
|
+
builds.sort((a,b)=>b.t-a.t);
|
|
167
|
+
for(const n of [...stale,...builds.slice(Math.max(0,KEEP_VERSIONS-keep.length)).map(b=>b.n)])await fsp.rm(path.join(versionsDir(dir),n),{recursive:true,force:true});
|
|
168
|
+
}
|
|
169
|
+
/** Points current.json at an extracted build and rewrites the launcher and shims. Used by both installers and update. */
|
|
170
|
+
export async function activate({id,version,sha256:sha,dir=cliDir(),bin=binDir(),platform=process.platform}){
|
|
171
|
+
if(!ID.test(String(id)))throw new Error('Bad build id.');
|
|
172
|
+
if(!fs.existsSync(path.join(versionsDir(dir),id,'bin','mlai.mjs')))throw new Error('Build '+id+' is not extracted in '+versionsDir(dir)+'.');
|
|
173
|
+
const before=readJson(path.join(dir,'current.json'),{});
|
|
174
|
+
const previous=before.id&&before.id!==id?before.id:before.previous||null;
|
|
175
|
+
await writeLayout(dir,bin,platform);
|
|
176
|
+
await writeJsonAtomic(path.join(dir,'current.json'),{id,version:String(version||''),sha256:sha||null,previous,installedAt:new Date().toISOString()});
|
|
177
|
+
await prune(dir,[id,previous].filter(Boolean));
|
|
178
|
+
return {id,previous,bin};
|
|
179
|
+
}
|
|
180
|
+
/** Verifies and extracts a downloaded build, then activates it. */
|
|
181
|
+
export async function installBuild({manifest,bytes,dir=cliDir(),bin=binDir(),platform=process.platform}){
|
|
182
|
+
verifyBytes(bytes,manifest);
|
|
183
|
+
const target=path.join(versionsDir(dir),manifest.id);
|
|
184
|
+
if(!fs.existsSync(path.join(target,'bin','mlai.mjs'))){
|
|
185
|
+
const tmp=target+'.tmp-'+process.pid+'-'+Date.now().toString(36);
|
|
186
|
+
await fsp.mkdir(tmp,{recursive:true});
|
|
187
|
+
try{await extract(bytes,tmp);await fsp.rm(target,{recursive:true,force:true});await fsp.rename(tmp,target)}
|
|
188
|
+
catch(e){await fsp.rm(tmp,{recursive:true,force:true});throw e}
|
|
189
|
+
}
|
|
190
|
+
return activate({id:manifest.id,version:manifest.version,sha256:manifest.sha256,dir,bin,platform});
|
|
191
|
+
}
|
|
192
|
+
export const currentBuild=(dir=cliDir())=>readJson(path.join(dir,'current.json'),null);
|
|
193
|
+
|
|
194
|
+
/** One updater at a time per install: a lock file with a 10 minute lease. */
|
|
195
|
+
async function withLock(dir,fn){
|
|
196
|
+
await fsp.mkdir(dir,{recursive:true});const lock=path.join(dir,'update.lock');
|
|
197
|
+
try{const st=await fsp.stat(lock);if(Date.now()-st.mtimeMs<600_000)throw Object.assign(new Error('Another update is running.'),{code:'locked'});await fsp.rm(lock,{force:true})}catch(e){if(e.code==='locked')throw e}
|
|
198
|
+
let h;try{h=await fsp.open(lock,'wx')}catch{throw Object.assign(new Error('Another update is running.'),{code:'locked'})}
|
|
199
|
+
try{await h.writeFile(String(process.pid));await h.close();return await fn()}finally{await fsp.rm(lock,{force:true})}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/* ---------- install kind ---------- */
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* How this copy was installed, from where its files are:
|
|
206
|
+
* managed ~/.mlai/cli/versions/<id> (install.sh / install.ps1) -> mlai update replaces it
|
|
207
|
+
* npm .../node_modules/mlai-code -> npm install -g mlai-code@latest
|
|
208
|
+
* brew .../Cellar/mlai/... -> brew upgrade mlai
|
|
209
|
+
* app inside the Mac app bundle -> updates with the app
|
|
210
|
+
* source a repository checkout -> git pull
|
|
211
|
+
*/
|
|
212
|
+
export function installKind(file=fileURLToPath(import.meta.url),dir=cliDir()){
|
|
213
|
+
const f=path.resolve(file),v=path.resolve(versionsDir(dir))+path.sep;
|
|
214
|
+
if(f.startsWith(v))return 'managed';
|
|
215
|
+
const n=f.split(path.sep).join('/');
|
|
216
|
+
if(/\/Cellar\/mlai\//.test(n)||/\/homebrew\/.*\/mlai\//i.test(n)&&!/node_modules/.test(n))return 'brew';
|
|
217
|
+
if(/\/node_modules\/mlai-code\//.test(n))return 'npm';
|
|
218
|
+
if(/\.app\/Contents\/Resources\//.test(n))return 'app';
|
|
219
|
+
return 'source';
|
|
220
|
+
}
|
|
221
|
+
export function installCommand(platform=process.platform,server=DEFAULT_SERVER){
|
|
222
|
+
return platform==='win32'?`irm ${server}/cli/install.ps1 | iex`:`curl -fsSL ${server}/cli/install.sh | sh`;
|
|
223
|
+
}
|
|
224
|
+
export function manualCommand(kind){
|
|
225
|
+
if(kind==='npm')return 'npm install -g '+NPM_PACKAGE+'@latest';
|
|
226
|
+
if(kind==='brew')return 'brew upgrade '+BREW_FORMULA;
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/* ---------- network ---------- */
|
|
231
|
+
|
|
232
|
+
const serverFrom=s=>String(s||process.env.MLAI_URL||process.env.MLAI_SERVER||DEFAULT_SERVER).replace(/\/+$/,'');
|
|
233
|
+
async function get(url,{fetchImpl=globalThis.fetch,timeout=15000}={}){
|
|
234
|
+
const ac=new AbortController();const t=setTimeout(()=>ac.abort(),timeout);t.unref?.();
|
|
235
|
+
try{const r=await fetchImpl(url,{signal:ac.signal,headers:{'user-agent':'mlai/'+VERSION}});if(!r.ok)throw new Error(`The update server answered ${r.status} for ${new URL(url).pathname}.`);return r}
|
|
236
|
+
catch(e){if(e.name==='AbortError')throw new Error('The update server did not answer in time.');throw e}
|
|
237
|
+
finally{clearTimeout(t)}
|
|
238
|
+
}
|
|
239
|
+
export async function fetchManifest({server,fetchImpl,timeout,publicKey}={}){
|
|
240
|
+
const r=await get(serverFrom(server)+'/api/cli/release',{fetchImpl,timeout});
|
|
241
|
+
return checkManifest(await r.json(),{publicKey});
|
|
242
|
+
}
|
|
243
|
+
export async function downloadBuild(manifest,{server,fetchImpl,timeout=120000}={}){
|
|
244
|
+
const base=serverFrom(server);const url=new URL(manifest.download||'/api/cli/release/download?id='+manifest.id,base+'/');
|
|
245
|
+
if(url.origin!==new URL(base).origin)throw new Error('The update manifest points at another server. Refusing to download.');
|
|
246
|
+
const r=await get(url.href,{fetchImpl,timeout});
|
|
247
|
+
return Buffer.from(await r.arrayBuffer());
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/* ---------- update ---------- */
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Checks for (and, for managed installs, installs) the server's current build.
|
|
254
|
+
* Returns {status, kind, current, latest, command?, message} with status one of
|
|
255
|
+
* current | available | updated | manual (npm / brew / app / source: run `command` or update the app)
|
|
256
|
+
*/
|
|
257
|
+
export async function update({check=false,server,fetchImpl,kind=installKind(),dir=cliDir(),bin=binDir(),platform=process.platform,publicKey}={}){
|
|
258
|
+
const cur=kind==='managed'?currentBuild(dir)||{version:VERSION}:{version:VERSION};
|
|
259
|
+
const m=await fetchManifest({server,fetchImpl,publicKey});
|
|
260
|
+
const base={kind,current:cur.id||cur.version,latest:m.id,version:m.version};
|
|
261
|
+
if(!isUpdate(cur,m))return {...base,status:'current',message:`ML AI Code CLI is up to date (${cur.version||VERSION}).`};
|
|
262
|
+
if(kind!=='managed'){
|
|
263
|
+
const command=manualCommand(kind);
|
|
264
|
+
const how=command?'Run: '+command:kind==='app'?'Update the Mac app to get it.':'This is a development copy; update it with git pull, or install the CLI: '+installCommand(platform);
|
|
265
|
+
return {...base,status:check?'available':'manual',command,message:`ML AI Code CLI ${m.version} is available (you have ${VERSION}). ${how}`};
|
|
266
|
+
}
|
|
267
|
+
if(check)return {...base,status:'available',message:`ML AI Code CLI ${m.version} (${m.id}) is available. Run: mlai update`};
|
|
268
|
+
return withLock(dir,async()=>{
|
|
269
|
+
const bytes=await downloadBuild(m,{server,fetchImpl});
|
|
270
|
+
await installBuild({manifest:m,bytes,dir,bin,platform});
|
|
271
|
+
return {...base,status:'updated',message:`Updated ML AI Code CLI to ${m.version} (${m.id}). It starts with your next mlai command.`};
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** Runs the package manager for npm / brew installs, streaming its output. */
|
|
276
|
+
function runManager(command){
|
|
277
|
+
const [cmd,...args]=command.split(' ');
|
|
278
|
+
return new Promise(res=>{const p=spawn(cmd,args,{stdio:'inherit',shell:process.platform==='win32'});p.on('error',()=>res(127));p.on('exit',c=>res(c??1))});
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/* ---------- background check ---------- */
|
|
282
|
+
|
|
283
|
+
const statePath=(dir=cliDir())=>path.join(dir,'state.json');
|
|
284
|
+
const readState=dir=>readJson(statePath(dir),{});
|
|
285
|
+
const saveState=(dir,s)=>writeJsonAtomic(statePath(dir),s).catch(()=>{});
|
|
286
|
+
|
|
287
|
+
/** Whether `argv` is an interactive session that may check in the background. */
|
|
288
|
+
export function backgroundAllowed(argv,{env=process.env,stdin=process.stdin,stdout=process.stdout,settings}={}){
|
|
289
|
+
if(env.MLAI_AUTO_UPDATE==='0'||env.MLAI_AUTO_UPDATE==='false'||env.CI||env.MLAI_NO_UPDATE_CHECK)return false;
|
|
290
|
+
if(!stdin?.isTTY||!stdout?.isTTY)return false;
|
|
291
|
+
const bad=new Set(['-p','--print','--json','--output-format','--help','-h','--version','-V']);
|
|
292
|
+
if(argv.some(a=>bad.has(a)||a.startsWith('--output-format=')||a.startsWith('--print=')))return false;
|
|
293
|
+
if(argv[0]&&['cloud','serve','runner','acp','login','logout','whoami','sessions','doctor','version','help','update','completion','__install','__complete'].includes(argv[0]))return false;
|
|
294
|
+
return settings?.autoUpdate!==false;
|
|
295
|
+
}
|
|
296
|
+
async function settingsFor(){
|
|
297
|
+
try{const {loadSettings}=await import('./settings.mjs');let root=null;try{root=(await (await import('./project.mjs')).projectRoot(process.cwd())).root}catch{}return loadSettings({root})}catch{return {}}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** One background check: at most once per CHECK_EVERY_MS. Never throws. */
|
|
301
|
+
export async function backgroundCheck({dir=cliDir(),now=Date.now(),kind=installKind(),...opts}={}){
|
|
302
|
+
try{
|
|
303
|
+
const st=readState(dir);
|
|
304
|
+
if(st.checkedAt&&now-Date.parse(st.checkedAt)<CHECK_EVERY_MS)return {status:'skipped'};
|
|
305
|
+
await saveState(dir,{...st,checkedAt:new Date(now).toISOString()});
|
|
306
|
+
const r=await update({check:kind!=='managed',kind,dir,...opts});
|
|
307
|
+
if(r.status==='updated'||r.status==='available'||r.status==='manual'){
|
|
308
|
+
const notice=r.status==='updated'?`ML AI Code CLI was updated to ${r.version}. Run mlai update --check for details.`:
|
|
309
|
+
`ML AI Code CLI ${r.version} is available. ${kind==='managed'?'Run: mlai update':manualCommand(kind)?'Run: '+manualCommand(kind):kind==='app'?'Update the Mac app to get it.':'Run: '+installCommand()}`;
|
|
310
|
+
await saveState(dir,{...readState(dir),notice,noticeFor:r.latest});
|
|
311
|
+
}
|
|
312
|
+
return r;
|
|
313
|
+
}catch(e){return {status:'error',message:e.message}}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/** Called first thing by cli.mjs main(): prints a pending notice and schedules the daily check. */
|
|
317
|
+
export async function startup(argv,{env=process.env,stderr=process.stderr,stdin=process.stdin,stdout=process.stdout,delay=3000,dir=cliDir()}={}){
|
|
318
|
+
try{
|
|
319
|
+
const st=readState(dir);
|
|
320
|
+
const quiet=argv.some(x=>x==='-p'||x==='--print'||x==='--json'||x.startsWith('--output-format'));
|
|
321
|
+
if(st.notice&&stderr?.isTTY&&!quiet){stderr.write(st.notice+'\n');const {notice,noticeFor,...rest}=st;await saveState(dir,rest)}
|
|
322
|
+
if(!backgroundAllowed(argv,{env,stdin,stdout,settings:null}))return false;
|
|
323
|
+
const settings=await settingsFor();
|
|
324
|
+
if(settings?.autoUpdate===false)return false;
|
|
325
|
+
const t=setTimeout(()=>{backgroundCheck({dir}).catch(()=>{})},delay);t.unref?.();
|
|
326
|
+
return true;
|
|
327
|
+
}catch{return false}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/* ---------- `mlai update | completion | __install | __complete` ---------- */
|
|
331
|
+
|
|
332
|
+
const UPDATE_HELP=`Usage: mlai update [--check] [--json] [--server URL]
|
|
333
|
+
Downloads the latest ML AI Code CLI, checks its SHA-256 and switches to it.
|
|
334
|
+
--check only report whether an update is available (exit 0 either way)
|
|
335
|
+
--json one JSON line: {status, kind, current, latest, version, message}
|
|
336
|
+
npm and Homebrew installs are updated with npm or brew; the Mac app updates the CLI it ships with.
|
|
337
|
+
Background checks: set "autoUpdate": false in ~/.mlai/settings.json (or MLAI_AUTO_UPDATE=0) to turn them off.`;
|
|
338
|
+
|
|
339
|
+
export async function distMain(argv,{stdout=process.stdout,stderr=process.stderr}={}){
|
|
340
|
+
const [cmd,...rest]=argv;
|
|
341
|
+
if(cmd==='completion'){
|
|
342
|
+
const {completionScript,SHELLS}=await import('./completion.mjs');
|
|
343
|
+
const shell=rest[0]||'';
|
|
344
|
+
if(!SHELLS.includes(shell)){stderr.write('Usage: mlai completion '+SHELLS.join('|')+'\n'+(await import('./completion.mjs')).INSTALL_HELP+'\n');return 2}
|
|
345
|
+
stdout.write(await completionScript(shell));return 0;
|
|
346
|
+
}
|
|
347
|
+
if(cmd==='__complete'){
|
|
348
|
+
if(rest[0]==='sessions'){try{const {Session}=await import('./session.mjs');for(const m of (await Session.list()).slice(0,50))stdout.write(m.id+'\n')}catch{}}
|
|
349
|
+
return 0;
|
|
350
|
+
}
|
|
351
|
+
if(cmd==='__install'){
|
|
352
|
+
// Called by install.sh / install.ps1 after they verified and extracted a build: __install <id> <version> <sha256>
|
|
353
|
+
const [id,version,sha]=rest;
|
|
354
|
+
try{const r=await activate({id,version,sha256:sha});stdout.write(`Installed ML AI Code CLI ${version} (${id})\n`);if(r.previous)stdout.write('Previous build kept for rollback: '+r.previous+'\n');return 0}
|
|
355
|
+
catch(e){stderr.write(e.message+'\n');return 1}
|
|
356
|
+
}
|
|
357
|
+
if(cmd==='update'){
|
|
358
|
+
const o={check:false,json:false,server:null};
|
|
359
|
+
for(let i=0;i<rest.length;i++){const a=rest[i];
|
|
360
|
+
if(a==='--check')o.check=true;else if(a==='--json')o.json=true;else if(a==='--server'&&rest[i+1])o.server=rest[++i];
|
|
361
|
+
else if(a==='-h'||a==='--help'){stdout.write(UPDATE_HELP+'\n');return 0}
|
|
362
|
+
else{stderr.write('Unknown option '+a+'.\n'+UPDATE_HELP+'\n');return 2}}
|
|
363
|
+
let r;
|
|
364
|
+
try{r=await update({check:o.check,server:o.server})}
|
|
365
|
+
catch(e){if(o.json)stdout.write(JSON.stringify({status:'error',message:e.message})+'\n');else stderr.write(e.message+'\n');return 1}
|
|
366
|
+
await saveState(cliDir(),(({notice,noticeFor,...s})=>s)(readState(cliDir())));
|
|
367
|
+
if(o.json){stdout.write(JSON.stringify(r)+'\n');return 0}
|
|
368
|
+
stdout.write(r.message+'\n');
|
|
369
|
+
if(r.status==='manual'&&r.command){stdout.write('Running: '+r.command+'\n');return await runManager(r.command)}
|
|
370
|
+
return 0;
|
|
371
|
+
}
|
|
372
|
+
stderr.write('Unknown command '+cmd+'\n');return 2;
|
|
373
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/* The Ed25519 public key that signs harness updates (lib/laptop-engine.ts), base64 of the raw 32 bytes.
|
|
2
|
+
Empty: updates are trusted on TLS, the device credential and per-file sha256 only. Once set, the app refuses
|
|
3
|
+
unsigned or badly signed manifests, so set it in the same release that adds MLAI_ENGINE_SIGNING_KEY to the server. */
|
|
4
|
+
export const ENGINE_PUBLIC_KEY='';
|
package/src/engine.mjs
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import {createHash,createPublicKey,verify,randomUUID} from 'node:crypto';
|
|
2
|
+
import {readFile,writeFile,mkdir,rename,rm,readdir,copyFile,stat} from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import {sub,workerDir} from './home.mjs';
|
|
5
|
+
import {ENGINE_PUBLIC_KEY} from './engine-key.mjs';
|
|
6
|
+
|
|
7
|
+
/* Harness auto-update: runs use the newest worker the server publishes (GET /api/laptop/engine, lib/laptop-engine.ts)
|
|
8
|
+
without an app update, and fall back to the copy bundled with the app on any doubt.
|
|
9
|
+
~/.mlai/engine/<version>/ holds verified copies (the current one and the one before it), current.json points at
|
|
10
|
+
the active one and is replaced with a rename, so a run sees either the old or the new harness, never a mix.
|
|
11
|
+
A task start waits at most WAIT_MS for a check; a slow download finishes in the background for the next task. */
|
|
12
|
+
|
|
13
|
+
/** Must match ENGINE_PROTOCOL in lib/laptop-engine.ts; a server with another protocol is ignored. */
|
|
14
|
+
export const ENGINE_PROTOCOL=1;
|
|
15
|
+
export const CHECK_EVERY=10*60*1000,WAIT_MS=3000,DOWNLOAD_MS=60000;
|
|
16
|
+
/** Copy of lib/laptop-engine.ts ENGINE_FILES (parity test): the bundled copy's version is hashed over these. */
|
|
17
|
+
export const ENGINE_FILES=['ask_first.py','attachment_reader.py','background_jobs.py','brief_questions.py','browser_local.py','browser_pane.py','browser_tool.py','build_planner.py','checkpoints.py','command_policy.py','command_safety.py','computer_card.sh','computer_setup.sh','computer_use.py','connector_search.py','context_manager.py','conversation.py','deploy_pack.py','design_director.py','dev_jobs.py','edit_guard.py','env_setup.py','evidence.py','fast_search.py','goals.py','handover.py','harness_memory.py','image_generation.py','intelligence.py','intent_model.py','intent_weights.json','ios_simulator.py','kimi_bridge.py','kimi_compact.py','kimi_gates.py','kimi_hook.py','lifecycle.py','lsp.py','mcp_local.py','memory.py','mlai_auto_review.py','mlai_hooks.py','mlai_mcp.py','mlai_permissions.py','model_stream.py','nested_repos.py','offline_guard.py','preview.py','proc_watch.py','project_context.py','project_rules.py','quality.py','reply_language.py','repo_hygiene.py','repo_memory.py','repo_policy.py','roles.json','roles.py','runner.py','runtime_control.py','secret_scan.py','session_hub.py','skill_library.py','skill_pack.py','subagents.py','task_controller.py','task_skills.py','token_count.py','tool_catalog.py','verification.py','vision.py','wakeups.py','web_tools.py','winproc.py','work_ledger.py','worktrees.py'];
|
|
18
|
+
/** The entry scripts; a manifest without them is not a harness. */
|
|
19
|
+
export const REQUIRED=['runner.py','kimi_bridge.py'];
|
|
20
|
+
const NAME=/^[A-Za-z0-9][A-Za-z0-9_.-]{0,99}$/,SHA=/^[a-f0-9]{64}$/,VERSION=/^[a-f0-9]{32}$/;
|
|
21
|
+
const MAX_FILE=4*1024*1024,MAX_TOTAL=32*1024*1024,MAX_FILES=400;
|
|
22
|
+
const fail=(message,code)=>Object.assign(new Error(message),{code});
|
|
23
|
+
const sha256=b=>createHash('sha256').update(b).digest('hex');
|
|
24
|
+
|
|
25
|
+
/** Same as lib/worker-files.ts bundleHash: names and bytes, independent of order. */
|
|
26
|
+
export function bundleHash(entries){
|
|
27
|
+
const h=createHash('sha256');
|
|
28
|
+
for(const e of [...entries].sort((a,b)=>a.name<b.name?-1:a.name>b.name?1:0)){h.update(e.name);h.update('\0');h.update(String(e.content.length));h.update('\0');h.update(e.content)}
|
|
29
|
+
return h.digest('hex').slice(0,32);
|
|
30
|
+
}
|
|
31
|
+
/** Same as lib/laptop-engine.ts canonicalManifest: the bytes the server signs. */
|
|
32
|
+
export function canonicalManifest(m){
|
|
33
|
+
return JSON.stringify({v:1,protocol:m.protocol,version:m.version,files:[...m.files].sort((a,b)=>a.path<b.path?-1:a.path>b.path?1:0).map(f=>[f.path,f.sha256,f.size])});
|
|
34
|
+
}
|
|
35
|
+
const SPKI=Buffer.from('302a300506032b6570032100','hex');
|
|
36
|
+
/** The pinned Ed25519 public key: MLAI_ENGINE_PUBLIC_KEY, else the one shipped in engine-key.mjs, else none. */
|
|
37
|
+
export function pinnedKey(){
|
|
38
|
+
const raw=String(process.env.MLAI_ENGINE_PUBLIC_KEY||ENGINE_PUBLIC_KEY||'').trim();if(!raw)return null;
|
|
39
|
+
const key=Buffer.from(raw,'base64');if(key.length!==32)throw fail('The pinned engine key is not a 32-byte Ed25519 key','engine_key');
|
|
40
|
+
return createPublicKey({key:Buffer.concat([SPKI,key]),format:'der',type:'spki'});
|
|
41
|
+
}
|
|
42
|
+
/** Shape, limits and (with a pinned key) the signature; returns a clean copy. Throws plain errors with a code. */
|
|
43
|
+
export function checkManifest(m,{key=pinnedKey()}={}){
|
|
44
|
+
if(!m||typeof m!=='object')throw fail('The server sent no engine manifest','engine_manifest');
|
|
45
|
+
if(m.protocol!==ENGINE_PROTOCOL)throw fail('The server harness needs a newer version of this app','engine_protocol');
|
|
46
|
+
if(!VERSION.test(String(m.version)))throw fail('The engine manifest has no valid version','engine_manifest');
|
|
47
|
+
if(!Array.isArray(m.files)||!m.files.length||m.files.length>MAX_FILES)throw fail('The engine manifest has no valid file list','engine_manifest');
|
|
48
|
+
const seen=new Set();let total=0;
|
|
49
|
+
const files=m.files.map(f=>{
|
|
50
|
+
if(!f||!NAME.test(String(f.path))||seen.has(f.path)||!SHA.test(String(f.sha256))||!Number.isInteger(f.size)||f.size<0||f.size>MAX_FILE)throw fail('The engine manifest lists an invalid file','engine_manifest');
|
|
51
|
+
seen.add(f.path);total+=f.size;return {path:f.path,sha256:f.sha256,size:f.size};
|
|
52
|
+
});
|
|
53
|
+
if(total>MAX_TOTAL)throw fail('The engine manifest is too large','engine_manifest');
|
|
54
|
+
for(const r of REQUIRED)if(!seen.has(r))throw fail('The engine manifest is missing '+r,'engine_manifest');
|
|
55
|
+
const clean={version:m.version,protocol:m.protocol,files};
|
|
56
|
+
if(key){
|
|
57
|
+
let ok=false;try{ok=typeof m.signature==='string'&&verify(null,Buffer.from(canonicalManifest(clean)),key,Buffer.from(m.signature,'base64'))}catch{}
|
|
58
|
+
if(!ok)throw fail('The engine manifest signature does not match this app','engine_signature');
|
|
59
|
+
}
|
|
60
|
+
return {...clean,signature:typeof m.signature==='string'?m.signature:null};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const root=()=>sub('engine');
|
|
64
|
+
const readJson=async(f,d)=>{try{return JSON.parse(await readFile(f,'utf8'))}catch{return d}};
|
|
65
|
+
async function writeAtomic(f,data){const tmp=f+'.'+randomUUID()+'.tmp';await writeFile(tmp,data,{mode:0o600});await rename(tmp,f)}
|
|
66
|
+
const bundledVersions=new Map();
|
|
67
|
+
/** The bundled copy's version, hashed like the server's so an up-to-date app never downloads anything. */
|
|
68
|
+
export async function bundledVersion(dir=workerDir()){
|
|
69
|
+
if(!bundledVersions.has(dir))bundledVersions.set(dir,(async()=>{const entries=[];for(const name of ENGINE_FILES){try{entries.push({name,content:await readFile(path.join(dir,name))})}catch{}}return bundleHash(entries)})());
|
|
70
|
+
return bundledVersions.get(dir);
|
|
71
|
+
}
|
|
72
|
+
/** Every file in dir matches its manifest and the manifest's version is the hash of exactly those bytes. */
|
|
73
|
+
async function verifyDir(dir,m){
|
|
74
|
+
const entries=await Promise.all(m.files.map(async f=>{const content=await readFile(path.join(dir,f.path));if(content.length!==f.size||sha256(content)!==f.sha256)throw fail('Engine file '+f.path+' does not match its hash','engine_hash');return {name:f.path,content}}));
|
|
75
|
+
if(bundleHash(entries)!==m.version)throw fail('The engine files do not add up to version '+m.version,'engine_hash');
|
|
76
|
+
}
|
|
77
|
+
async function installed(version){
|
|
78
|
+
if(!VERSION.test(String(version)))return null;
|
|
79
|
+
const dir=path.join(root(),version),m=await readJson(path.join(dir,'manifest.json'),null);
|
|
80
|
+
if(!m||m.version!==version)return null;
|
|
81
|
+
try{await verifyDir(dir,m);return dir}catch{return null}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** The harness the next run uses: {dir, version, source:'updated'|'bundled'|'custom'}. Never throws for update state. */
|
|
85
|
+
export async function activeEngine(){
|
|
86
|
+
if(process.env.MLAI_WORKER_DIR)return {dir:workerDir(),version:'custom',source:'custom'};
|
|
87
|
+
const cur=await readJson(path.join(root(),'current.json'),{});
|
|
88
|
+
const dir=cur.version?await installed(cur.version):null;
|
|
89
|
+
if(dir)return {dir,version:cur.version,source:'updated'};
|
|
90
|
+
const bundled=workerDir();
|
|
91
|
+
return {dir:bundled,version:await bundledVersion(bundled),source:'bundled'};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
let inflight=null;
|
|
95
|
+
/**
|
|
96
|
+
* Called at task start. At most once per CHECK_EVERY (also after a failure) it asks the server for its manifest and,
|
|
97
|
+
* when the version differs, installs it. Waits at most `wait` ms, then returns the harness to use for this task.
|
|
98
|
+
* `cloud` needs engineManifest({signal}) and engineFiles(names,{signal}); without it (offline runs) nothing is fetched.
|
|
99
|
+
*/
|
|
100
|
+
export async function prepareEngine({cloud,wait=WAIT_MS,now=Date.now(),force=false,key}={}){
|
|
101
|
+
const off=!cloud||process.env.MLAI_WORKER_DIR||process.env.MLAI_ENGINE_UPDATES==='0';
|
|
102
|
+
let update='skipped';
|
|
103
|
+
if(!off){
|
|
104
|
+
const stateFile=path.join(root(),'state.json'),state=await readJson(stateFile,{});
|
|
105
|
+
const due=force||!(state.checked<=now&&now-state.checked<CHECK_EVERY);
|
|
106
|
+
if(due&&!inflight){
|
|
107
|
+
await writeAtomic(stateFile,JSON.stringify({checked:now}));
|
|
108
|
+
inflight=install(cloud,key).then(r=>({update:r}),async e=>{await writeAtomic(stateFile,JSON.stringify({checked:now,error:String(e.message).slice(0,300),code:e.code||null})).catch(()=>{});return {update:'failed',error:e.message}}).finally(()=>{inflight=null});
|
|
109
|
+
}
|
|
110
|
+
if(inflight){
|
|
111
|
+
let timer;const late=new Promise(r=>{timer=setTimeout(()=>r({update:'pending'}),wait);timer.unref?.()});
|
|
112
|
+
const r=await Promise.race([inflight,late]);clearTimeout(timer);update=r.update;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
const engine=await activeEngine();
|
|
116
|
+
return {...engine,update};
|
|
117
|
+
}
|
|
118
|
+
/** Resolves when a background install (if any) is done; for tests and for a clean shutdown. */
|
|
119
|
+
export function engineSettled(){return inflight||Promise.resolve()}
|
|
120
|
+
|
|
121
|
+
async function install(cloud,key){
|
|
122
|
+
const signal=AbortSignal.timeout(DOWNLOAD_MS);
|
|
123
|
+
const m=checkManifest(await cloud.engineManifest({signal}),key===undefined?{}:{key});
|
|
124
|
+
const base=root(),curFile=path.join(base,'current.json'),cur=await readJson(curFile,{});
|
|
125
|
+
let bundled=null;try{bundled=workerDir()}catch{}
|
|
126
|
+
const point=async version=>{if(cur.version===version)return 'current';await writeAtomic(curFile,JSON.stringify({version,previous:cur.version||null,switched:new Date().toISOString()}));await prune(base,[version,cur.version]);return 'switched'};
|
|
127
|
+
// The server runs exactly what the app shipped with: use the bundled copy.
|
|
128
|
+
if(bundled&&m.version===await bundledVersion(bundled))return cur.version?point(null):'current';
|
|
129
|
+
if(await installed(m.version))return point(m.version);
|
|
130
|
+
const stage=path.join(base,'.staging-'+randomUUID());await mkdir(stage,{recursive:true,mode:0o700});
|
|
131
|
+
try{
|
|
132
|
+
// Unchanged files are copied from the active or bundled copy (checked by hash); only the rest is downloaded.
|
|
133
|
+
const local=[cur.version?path.join(base,cur.version):null,bundled].filter(Boolean),need=[];
|
|
134
|
+
for(const f of m.files){
|
|
135
|
+
let found=false;
|
|
136
|
+
for(const d of local){try{const b=await readFile(path.join(d,f.path));if(b.length===f.size&&sha256(b)===f.sha256){await writeFile(path.join(stage,f.path),b,{mode:mode(f.path)});found=true;break}}catch{}}
|
|
137
|
+
if(!found)need.push(f);
|
|
138
|
+
}
|
|
139
|
+
if(need.length){
|
|
140
|
+
const got=await cloud.engineFiles(need.map(f=>f.path),{signal});
|
|
141
|
+
if(got?.version!==m.version)throw fail('The server harness changed during the download; it will be retried later','engine_changed');
|
|
142
|
+
const byName=new Map((Array.isArray(got.files)?got.files:[]).map(f=>[f?.path,f?.content]));
|
|
143
|
+
for(const f of need){
|
|
144
|
+
const c=byName.get(f.path);if(typeof c!=='string')throw fail('The server did not send '+f.path,'engine_hash');
|
|
145
|
+
const b=Buffer.from(c,'base64');
|
|
146
|
+
if(b.length!==f.size||sha256(b)!==f.sha256)throw fail('Engine file '+f.path+' does not match its hash','engine_hash');
|
|
147
|
+
await writeFile(path.join(stage,f.path),b,{mode:mode(f.path)});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
await verifyDir(stage,m);
|
|
151
|
+
await writeFile(path.join(stage,'manifest.json'),JSON.stringify(m),{mode:0o600});
|
|
152
|
+
const target=path.join(base,m.version);
|
|
153
|
+
// Another window may have installed the same version meanwhile; keep the complete one.
|
|
154
|
+
if(await installed(m.version))await rm(stage,{recursive:true,force:true});
|
|
155
|
+
else{await rm(target,{recursive:true,force:true});await rename(stage,target)}
|
|
156
|
+
return point(m.version);
|
|
157
|
+
}finally{await rm(stage,{recursive:true,force:true}).catch(()=>{})}
|
|
158
|
+
}
|
|
159
|
+
const mode=name=>name.endsWith('.sh')?0o755:0o644;
|
|
160
|
+
/** Keeps the current and the previous version; removes older ones and staging folders left by a crash. */
|
|
161
|
+
async function prune(base,keep){
|
|
162
|
+
for(const n of await readdir(base).catch(()=>[])){
|
|
163
|
+
const p=path.join(base,n);
|
|
164
|
+
if(VERSION.test(n)&&!keep.includes(n))await rm(p,{recursive:true,force:true}).catch(()=>{});
|
|
165
|
+
else if(n.startsWith('.staging-')){try{if(Date.now()-(await stat(p)).mtimeMs>CHECK_EVERY)await rm(p,{recursive:true,force:true})}catch{}}
|
|
166
|
+
}
|
|
167
|
+
}
|