@evolvingmachines/modal 0.0.51 → 0.0.52-project-sable.20260729.8632c49

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/dist/index.cjs CHANGED
@@ -1,4 +1,4 @@
1
- 'use strict';var modal=require('modal'),tarStream=require('tar-stream');var b={"evolve-all":"evolvingmachines/evolve-all"},h=new Set([".xlsx",".xls",".docx",".doc",".pptx",".ppt",".pdf",".zip",".tar",".gz",".7z",".rar",".png",".jpg",".jpeg",".gif",".webp",".ico",".bmp",".mp3",".wav",".ogg",".flac",".aac",".mp4",".avi",".mov",".mkv",".webm",".woff",".woff2",".ttf",".otf",".eot",".exe",".dll",".so",".dylib",".sqlite",".db",".pickle",".pkl",".parquet"]);function y(c){let t=c.substring(c.lastIndexOf(".")).toLowerCase();return h.has(t)}var u=class{constructor(t){this.sandbox=t;}wrapAsUser(t,e,n){let r="";n&&Object.keys(n).length>0&&(r=Object.entries(n).filter(([,o])=>o!=null).map(([o,i])=>`export ${o}='${String(i).replace(/'/g,"'\\''")}'`).join("; ")+"; ");let s=e?`cd '${e.replace(/'/g,"'\\''")}' && ${r}${t}`:`${r}${t}`;return ["su","user","-c",`echo ${Buffer.from(s).toString("base64")} | base64 -d | bash`]}async run(t,e){let n=this.wrapAsUser(t,e?.cwd,e?.envs),r=await this.sandbox.exec(n,{timeoutMs:e?.timeoutMs}),{stdout:s,stderr:a}=await this.accumulateStreams(r,e?.onStdout,e?.onStderr);return {exitCode:await r.wait(),stdout:s,stderr:a}}async spawn(t,e){let n=this.wrapAsUser(t,e?.cwd,e?.envs),r=await this.sandbox.exec(n,{timeoutMs:e?.timeoutMs}),s="",a="",o=this.accumulateStreams(r,e?.onStdout?d=>{e.onStdout(d);}:void 0,e?.onStderr?d=>{e.onStderr(d);}:void 0).then(({stdout:d,stderr:m})=>{s=d,a=m;}).catch(()=>{});return {processId:`modal-${Date.now()}-${Math.random().toString(36).slice(2)}`,wait:async()=>(await o,{exitCode:await r.wait(),stdout:s,stderr:a}),kill:async()=>false}}async list(){let t=await this.sandbox.exec(["ps","-eo","pid,comm,args"],{timeoutMs:1e4});return await t.wait(),(await t.stdout.readText()).trim().split(`
2
- `).slice(1).map(r=>{let s=r.trim().split(/\s+/);return {processId:s[0],cmd:s[1]||"",args:s.slice(2),envs:{}}})}async connect(t,e){throw new Error("Modal does not support connecting to existing processes")}async sendStdin(t,e){throw new Error("Modal does not support sendStdin by process ID")}async kill(t){return await(await this.sandbox.exec(["kill","-9",t],{timeoutMs:1e4})).wait()===0}async accumulateStreams(t,e,n){let r="",s="",a=[];return a.push((async()=>{try{for await(let o of t.stdout){let i=typeof o=="string"?o:new TextDecoder().decode(o);r+=i,e?.(i);}}catch{}})()),a.push((async()=>{try{for await(let o of t.stderr){let i=typeof o=="string"?o:new TextDecoder().decode(o);s+=i,n?.(i);}}catch{}})()),await Promise.all(a),{stdout:r,stderr:s}}},g=class{constructor(t){this.sandbox=t;}async read(t){if(y(t)){let r=await this.sandbox.exec(["cat",t],{timeoutMs:3e5,mode:"binary"}),s=await r.wait();if(s!==0){let a=await r.stderr.readText();throw new Error(`Failed to read file ${t}: ${a||`exit code ${s}`}`)}return await r.stdout.readBytes()}let e=await this.sandbox.exec(["cat",t],{timeoutMs:3e5}),n=await e.wait();if(n!==0){let r=await e.stderr.readText();throw new Error(`Failed to read file ${t}: ${r||`exit code ${n}`}`)}return await e.stdout.readText()}async write(t,e){let n=this.toBuffer(e),r=t.substring(0,t.lastIndexOf("/"));r&&await this.makeDir(r);let s=t.replace(/'/g,"'\\''"),a=await this.sandbox.exec(["bash","-c",`cat > '${s}'`],{mode:"binary"});await a.stdin.writeBytes(new Uint8Array(n)),await a.stdin.getWriter().close(),await a.wait(),await(await this.sandbox.exec(["chown","user:user",t],{timeoutMs:1e4})).wait();}async writeBatch(t){let e=tarStream.pack(),n=[],r=new Set;for(let i of t){let d=this.toBuffer(i.data),m=i.path.startsWith("/")?i.path.slice(1):i.path;e.entry({name:m},d);let p=i.path.substring(0,i.path.lastIndexOf("/"));p&&r.add(p);}e.finalize();for await(let i of e)n.push(Buffer.from(i));let s=Buffer.concat(n),a=await this.sandbox.exec(["tar","-xf","-","-C","/"],{mode:"binary"});if(await a.stdin.writeBytes(new Uint8Array(s)),await a.stdin.getWriter().close(),await a.wait(),r.size>0){let i=Array.from(r),d=new Set(i.map(m=>m.split("/").slice(0,4).join("/")));for(let m of d)await(await this.sandbox.exec(["chown","-R","user:user",m],{timeoutMs:3e4})).wait();}}async makeDir(t){await(await this.sandbox.exec(["mkdir","-p",t],{timeoutMs:1e4})).wait(),await(await this.sandbox.exec(["chown","-R","user:user",t],{timeoutMs:1e4})).wait();}async exists(t){return await(await this.sandbox.exec(["test","-e",t],{timeoutMs:1e4})).wait()===0}async list(t){let e=t.replace(/'/g,"'\\''"),n=await this.sandbox.exec(["bash","-c",`ls -la '${e}' | tail -n +2`],{timeoutMs:3e4});await n.wait();let r=await n.stdout.readText(),s=[];for(let a of r.trim().split(`
3
- `)){if(!a)continue;let o=a.split(/\s+/);if(o.length<9)continue;let i=o[0],d=o.slice(8).join(" ");d==="."||d===".."||s.push({name:d,path:t.endsWith("/")?`${t}${d}`:`${t}/${d}`,type:i.startsWith("d")?"dir":"file"});}return s}async remove(t){await(await this.sandbox.exec(["rm","-rf",t],{timeoutMs:3e4})).wait();}async rename(t,e){await(await this.sandbox.exec(["mv",t,e],{timeoutMs:3e4})).wait();}async readStream(t){return (await this.sandbox.exec(["cat",t],{timeoutMs:3e5,mode:"binary"})).stdout}async writeStream(t,e){let n=t.substring(0,t.lastIndexOf("/"));n&&await this.makeDir(n);let r=t.replace(/'/g,"'\\''"),s=await this.sandbox.exec(["bash","-c",`cat > '${r}'`],{mode:"binary"}),a=e.getReader();try{for(;;){let{done:i,value:d}=await a.read();if(i)break;await s.stdin.writeBytes(d);}}finally{a.releaseLock();}await s.stdin.getWriter().close(),await s.wait();}async uploadUrl(t,e){throw new Error("Modal does not support pre-signed upload URLs")}async downloadUrl(t,e){throw new Error("Modal does not support pre-signed download URLs")}async watchDir(t,e,n){throw new Error("Modal does not support watchDir")}toBuffer(t){if(typeof t=="string")return Buffer.from(t,"utf-8");if(t instanceof Buffer)return t;if(t instanceof ArrayBuffer||t instanceof Uint8Array)return Buffer.from(t);throw new Error(`Unsupported data type: ${typeof t}`)}},l=class{constructor(t,e){this.sandbox=t;this.commands=new u(t),this.files=new g(t),this.image=e,this.startTime=new Date;}commands;files;image;startTime;get sandboxId(){return this.sandbox.sandboxId}async getHost(t){let n=(await this.sandbox.tunnels())[t];if(!n)throw new Error(`No tunnel found for port ${t}`);return n.url}async isRunning(){try{return await(await this.sandbox.exec(["echo","ping"],{timeoutMs:5e3})).wait(),!0}catch{return false}}async getInfo(){return {sandboxId:this.sandbox.sandboxId,image:this.image,metadata:{},startedAt:this.startTime.toISOString()}}async kill(){try{await this.sandbox.terminate();}catch{await new Promise(t=>setTimeout(t,500)),await this.sandbox.terminate();}}async pause(){throw new Error("Modal does not support pause. Use kill() instead.")}},w=class{providerType="modal";name="Modal";client;appName;defaultTimeoutMs;imageName;_app;constructor(t){!t.endpoint&&process.env.MODAL_SERVER_URL?.startsWith("unix:")?process.env.MODAL_SERVER_URL="https://api.modal.com:443":t.endpoint&&(process.env.MODAL_SERVER_URL=t.endpoint),this.client=new modal.ModalClient({tokenId:t.tokenId,tokenSecret:t.tokenSecret}),this.appName=t.appName??"evolve-sandbox",this.defaultTimeoutMs=t.defaultTimeoutMs??36e5,this.imageName=t.imageName??"evolve-all";}async getApp(){return this._app||(this._app=await this.client.apps.fromName(this.appName,{createIfMissing:true})),this._app}async create(t){let e=await this.getApp(),n=t.timeoutMs??this.defaultTimeoutMs,r=t.image||this.imageName,s=b[r]??r,o=await this.client.images.fromRegistry(s).build(e),i=t.envs?Object.fromEntries(Object.entries(t.envs).filter(([,p])=>p!=null)):void 0,d=i&&Object.keys(i).length>0?i:void 0,m=await this.client.sandboxes.create(e,o,{cpu:4,memoryMiB:4096,timeoutMs:n,workdir:t.workingDirectory,env:d});return t.workingDirectory&&await(await m.exec(["chown","-R","user:user",t.workingDirectory],{timeoutMs:3e4})).wait(),new l(m,s)}async connect(t,e){let n=await this.client.sandboxes.fromId(t);return new l(n,"unknown")}async list(t){let e=[],n=t?.limit??100;try{for await(let r of this.client.sandboxes.list())if(e.push({sandboxId:r.sandboxId,image:"unknown",metadata:{},startedAt:new Date().toISOString()}),e.length>=n)break}catch{}return e}};function k(c={}){let t=c.tokenId??process.env.MODAL_TOKEN_ID,e=c.tokenSecret??process.env.MODAL_TOKEN_SECRET;if(!t||!e)throw new Error("Modal credentials required. Set MODAL_TOKEN_ID and MODAL_TOKEN_SECRET environment variables, or pass tokenId/tokenSecret in config. Get your token at https://modal.com/settings/tokens");return new w({...c,tokenId:t,tokenSecret:e})}
4
- exports.ModalProvider=w;exports.createModalProvider=k;
1
+ 'use strict';var modal=require('modal'),tarStream=require('tar-stream');var H={"evolve-all":"evolvingmachines/evolve-all"},E=1440*60*1e3,A=8*1024*1024,k="user",R="evolve.image",_="evolve.startedAt",j=new Set([".xlsx",".xls",".docx",".doc",".pptx",".ppt",".pdf",".zip",".tar",".gz",".7z",".rar",".png",".jpg",".jpeg",".gif",".webp",".ico",".bmp",".mp3",".wav",".ogg",".flac",".aac",".mp4",".avi",".mov",".mkv",".webm",".woff",".woff2",".ttf",".otf",".eot",".exe",".dll",".so",".dylib",".sqlite",".db",".pickle",".pkl",".parquet"]);function q(n){let t=n.substring(n.lastIndexOf(".")).toLowerCase();return j.has(t)}var x=class extends Error{requestedTimeoutMs;constructor(t){let e=(t/36e5).toFixed(1);super(`Modal sandboxes have a hard 24h lifetime cap; requested timeout was ${e}h. For sessions longer than 24h, persist progress with Evolve checkpoints and resume in a fresh sandbox instead of extending the timeout.`),this.name="ModalSandboxLifetimeError",this.requestedTimeoutMs=t;}};function C(n){if(n>E)throw new x(n)}var p=class extends Error{requestedIdleTimeoutMs;constructor(t,e){super(`Modal idleTimeoutMs of ${t}ms is invalid: ${e}`),this.name="ModalIdleTimeoutError",this.requestedIdleTimeoutMs=t;}};function O(n){if(n===void 0)return {};if(!Number.isFinite(n)||n<=0)throw new p(n,"it must be a positive number of milliseconds");if(n>E)throw new p(n,"it exceeds Modal's 24h lifetime cap, so the sandbox would always die of the lifetime first");return {idleTimeoutMs:n}}function b(n,t,e,r){let s="";r&&Object.keys(r).length>0&&(s=Object.entries(r).filter(([,d])=>d!=null).map(([d,a])=>`export ${d}='${String(a).replace(/'/g,"'\\''")}'`).join("; ")+"; ");let i=e?`cd '${e.replace(/'/g,"'\\''")}' && ${s}${n}`:`${s}${n}`;if(t==="root")return ["bash","-c",i];let o=Buffer.from(i).toString("base64");return ["su",t,"-c",`echo ${o} | base64 -d | bash`]}var S=class extends Error{constructor(t){super(t),this.name="ModalResourcesError";}},z=4,G=4096;function N(n){if(n?.disk!==void 0)throw new S(`Modal's JS SDK has no create-time disk-size parameter, so a ${n.disk} GiB disk request cannot be enforced. Drop \`resources.disk\` (containers get Modal's default disk quota) or run on a provider that sizes disk.`);return {cpu:n?.cpu??z,memoryMiB:n?.memory!==void 0?Math.ceil(n.memory*1024):G}}var u=class extends Error{reason;destination;constructor(t,e,r){super(e),this.name="ModalNetworkPolicyError",this.reason=t,this.destination=r;}};function D(n){let t=/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})(?:\/(\d{1,3}))?$/.exec(n);return !(!t||[t[1],t[2],t[3],t[4]].some(e=>Number(e)>255)||t[5]!==void 0&&Number(t[5])>32)}function K(n){return /^\d{1,3}(\.\d{1,3}){3}(\/\d+)?$/.test(n)&&!D(n)}function X(n){return n.startsWith("[")?true:(n.match(/:/g)?.length??0)>=2}function Y(n){return /^[^:]+:\d+$/.test(n)}function T(n){if(!n||n.outbound==="open"){if(n?.allowedDestinations?.length)throw new Error("network.allowedDestinations is only valid when outbound is blocked");return {}}let t=n.allowedDestinations??[];if(t.length===0)return {blockNetwork:true};let e=[],r=[];for(let s of t)if(D(s))e.push(s.includes("/")?s:`${s}/32`);else {if(K(s))throw new u("invalid-ipv4",`"${s}" is not a valid IPv4 address or CIDR (octets must be 0-255, prefix 0-32). Fix the address or list a hostname instead.`,s);if(X(s))e.push(s.includes("/")?s:`${s}/128`);else {if(Y(s))throw new u("port-unsupported",`Modal's network allowlist filters hosts and IPs only and cannot match a port; drop the ":<port>" from "${s}" and list just the host or IP.`,s);r.push(s);}}return {outboundCidrAllowlist:e,outboundDomainAllowlist:r}}function $(n){let t=n.split("/")[0];return /^\d{12}\.dkr\.ecr\.[a-z0-9-]+\.amazonaws\.com$/.test(t)?"aws-ecr":t==="gcr.io"||t.endsWith(".gcr.io")||t.endsWith("-docker.pkg.dev")?"gcp-artifact-registry":"registry"}function I(n,t,e){let{[R]:r,[_]:s,...i}=t;return {sandboxId:n,image:r??e??"",metadata:i,startedAt:s??""}}var M=1e4,y=class{constructor(t,e){this.sandbox=t;this.user=e;}async run(t,e){let r=b(t,this.user,e?.cwd,e?.envs),s=await this.sandbox.exec(r,{timeoutMs:e?.timeoutMs}),{stdout:i,stderr:o}=await this.accumulateStreams(s,e?.onStdout,e?.onStderr);return {exitCode:await s.wait(),stdout:i,stderr:o}}async spawn(t,e){let r=b(t,this.user,e?.cwd,e?.envs),s=await this.sandbox.exec(r,{timeoutMs:e?.timeoutMs}),i="",o="",d=this.accumulateStreams(s,e?.onStdout?c=>{e.onStdout(c);}:void 0,e?.onStderr?c=>{e.onStderr(c);}:void 0).then(({stdout:c,stderr:m})=>{i=c,o=m;}).catch(()=>{});return {processId:`modal-${Date.now()}-${Math.random().toString(36).slice(2)}`,wait:async()=>(await d,{exitCode:await s.wait(),stdout:i,stderr:o}),kill:async()=>false}}async list(){let t=await this.sandbox.exec(["ps","-eo","pid,comm,args"],{timeoutMs:1e4});return await t.wait(),(await t.stdout.readText()).trim().split(`
2
+ `).slice(1).map(s=>{let i=s.trim().split(/\s+/);return {processId:i[0],cmd:i[1]||"",args:i.slice(2),envs:{}}})}async connect(t,e){throw new Error("Modal does not support connecting to existing processes")}async sendStdin(t,e){throw new Error("Modal does not support sendStdin by process ID")}async kill(t){return await(await this.sandbox.exec(["kill","-9",t],{timeoutMs:1e4})).wait()===0}async accumulateStreams(t,e,r){let s="",i="",o=[];return o.push((async()=>{try{for await(let d of t.stdout){let a=typeof d=="string"?d:new TextDecoder().decode(d);s+=a,e?.(a);}}catch{}})()),o.push((async()=>{try{for await(let d of t.stderr){let a=typeof d=="string"?d:new TextDecoder().decode(d);i+=a,r?.(a);}}catch{}})()),await Promise.all(o),{stdout:s,stderr:i}}},v=class{constructor(t,e){this.sandbox=t;this.user=e;}async chownToUser(t,e=false){if(this.user==="root")return;let r=e?["chown","-R",`${this.user}:${this.user}`,t]:["chown",`${this.user}:${this.user}`,t];await(await this.sandbox.exec(r,{timeoutMs:e?3e4:1e4})).wait();}async writeStdinChunked(t,e){for(let r=0;r<e.length;r+=A)await t.writeBytes(e.subarray(r,r+A));}async read(t){if(q(t)){let s=await this.sandbox.exec(["cat",t],{timeoutMs:3e5,mode:"binary"}),i=await s.wait();if(i!==0){let o=await s.stderr.readText();throw new Error(`Failed to read file ${t}: ${o||`exit code ${i}`}`)}return await s.stdout.readBytes()}let e=await this.sandbox.exec(["cat",t],{timeoutMs:3e5}),r=await e.wait();if(r!==0){let s=await e.stderr.readText();throw new Error(`Failed to read file ${t}: ${s||`exit code ${r}`}`)}return await e.stdout.readText()}async write(t,e){let r=this.toBuffer(e),s=t.substring(0,t.lastIndexOf("/"));s&&await this.makeDir(s);let i=t.replace(/'/g,"'\\''"),o=await this.sandbox.exec(["bash","-c",`cat > '${i}'`],{mode:"binary"});await this.writeStdinChunked(o.stdin,new Uint8Array(r)),await o.stdin.getWriter().close(),await o.wait(),await this.chownToUser(t);}async writeBatch(t){let e=tarStream.pack(),r=[],s=new Set;for(let a of t){let c=this.toBuffer(a.data),m=a.path.startsWith("/")?a.path.slice(1):a.path;e.entry({name:m},c);let l=a.path.substring(0,a.path.lastIndexOf("/"));l&&s.add(l);}e.finalize();for await(let a of e)r.push(Buffer.from(a));let i=Buffer.concat(r),o=await this.sandbox.exec(["tar","-xf","-","-C","/"],{mode:"binary"});if(await this.writeStdinChunked(o.stdin,new Uint8Array(i)),await o.stdin.getWriter().close(),await o.wait(),s.size>0){let a=Array.from(s),c=new Set(a.map(m=>m.split("/").slice(0,4).join("/")));for(let m of c)await this.chownToUser(m,true);}}async makeDir(t){await(await this.sandbox.exec(["mkdir","-p",t],{timeoutMs:1e4})).wait(),await this.chownToUser(t,true);}async exists(t){return await(await this.sandbox.exec(["test","-e",t],{timeoutMs:1e4})).wait()===0}async list(t){let e=t.replace(/'/g,"'\\''"),r=await this.sandbox.exec(["bash","-c",`ls -la '${e}' | tail -n +2`],{timeoutMs:3e4});await r.wait();let s=await r.stdout.readText(),i=[];for(let o of s.trim().split(`
3
+ `)){if(!o)continue;let d=o.split(/\s+/);if(d.length<9)continue;let a=d[0],c=d.slice(8).join(" ");c==="."||c===".."||i.push({name:c,path:t.endsWith("/")?`${t}${c}`:`${t}/${c}`,type:a.startsWith("d")?"dir":"file"});}return i}async remove(t){await(await this.sandbox.exec(["rm","-rf",t],{timeoutMs:3e4})).wait();}async rename(t,e){await(await this.sandbox.exec(["mv",t,e],{timeoutMs:3e4})).wait();}async readStream(t){return (await this.sandbox.exec(["cat",t],{timeoutMs:3e5,mode:"binary"})).stdout}async writeStream(t,e){let r=t.substring(0,t.lastIndexOf("/"));r&&await this.makeDir(r);let s=t.replace(/'/g,"'\\''"),i=await this.sandbox.exec(["bash","-c",`cat > '${s}'`],{mode:"binary"}),o=e.getReader();try{for(;;){let{done:a,value:c}=await o.read();if(a)break;await this.writeStdinChunked(i.stdin,c);}}finally{o.releaseLock();}await i.stdin.getWriter().close(),await i.wait(),await this.chownToUser(t);}async writeFromPath(t,e){let{createReadStream:r}=await import('fs'),{Readable:s}=await import('stream'),i=s.toWeb(r(e));await this.writeStream(t,i);}async uploadUrl(t,e){throw new Error("Modal does not support pre-signed upload URLs")}async downloadUrl(t,e){throw new Error("Modal does not support pre-signed download URLs")}async watchDir(t,e,r){throw new Error("Modal does not support watchDir")}toBuffer(t){if(typeof t=="string")return Buffer.from(t,"utf-8");if(t instanceof Buffer)return t;if(t instanceof ArrayBuffer||t instanceof Uint8Array)return Buffer.from(t);throw new Error(`Unsupported data type: ${typeof t}`)}},g=class{constructor(t,e,r){this.sandbox=t;this.commands=new y(t,r),this.files=new v(t,r),this.image=e;}commands;files;image;get sandboxId(){return this.sandbox.sandboxId}async getHost(t){let r=(await this.sandbox.tunnels())[t];if(!r)throw new Error(`No tunnel found for port ${t}`);return r.url}async isRunning(){try{return await this.sandbox.poll()===null}catch{return false}}async getInfo(){let t=await this.sandbox.getTags();return I(this.sandbox.sandboxId,t,this.image)}async kill(){try{await this.sandbox.terminate();}catch{await new Promise(t=>setTimeout(t,500)),await this.sandbox.terminate();}}async pause(){throw new Error("Modal does not support pause/resume. Persist progress with Evolve checkpoints and resume in a fresh sandbox, or use kill() to terminate.")}},P=class{providerType="modal";name="Modal";client;appName;defaultTimeoutMs;imageName;imageSecretName;_app;sandboxUsers=new Map;constructor(t){!t.endpoint&&process.env.MODAL_SERVER_URL?.startsWith("unix:")?process.env.MODAL_SERVER_URL="https://api.modal.com:443":t.endpoint&&(process.env.MODAL_SERVER_URL=t.endpoint),this.client=new modal.ModalClient({tokenId:t.tokenId,tokenSecret:t.tokenSecret}),this.appName=t.appName??"evolve-sandbox",this.defaultTimeoutMs=t.defaultTimeoutMs??36e5,this.imageName=t.imageName??"evolve-all",this.imageSecretName=t.imageSecretName;}async getApp(){return this._app||(this._app=await this.client.apps.fromName(this.appName,{createIfMissing:true})),this._app}async resolveImage(t){let e=$(t);if(e==="registry"){let s=this.imageSecretName?await this.client.secrets.fromName(this.imageSecretName):void 0;return this.client.images.fromRegistry(t,s)}if(!this.imageSecretName)throw new Error(`Private registry image "${t}" requires config.imageSecretName \u2014 the name of a Modal Secret holding registry credentials (AWS ECR: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION with read-only ECR IAM). Create one at https://modal.com/secrets`);let r=await this.client.secrets.fromName(this.imageSecretName);return e==="aws-ecr"?this.client.images.fromAwsEcr(t,r):this.client.images.fromGcpArtifactRegistry(t,r)}async create(t){let e=t.timeoutMs??this.defaultTimeoutMs;C(e);let r=O(t.idleTimeoutMs),s=T(t.network),i=N(t.resources),o=t.user??k,d=await this.getApp(),a=t.image||this.imageName,c=H[a]??a,l=await(await this.resolveImage(c)).build(d),f=t.envs?Object.fromEntries(Object.entries(t.envs).filter(([,h])=>h!=null)):void 0,B=f&&Object.keys(f).length>0?f:void 0,U={...t.metadata,[R]:c,[_]:new Date().toISOString()},w=await this.client.sandboxes.create(d,l,{cpu:i.cpu,memoryMiB:i.memoryMiB,timeoutMs:e,...r,workdir:t.workingDirectory,env:B,tags:U,...s});return t.workingDirectory&&o!=="root"&&await(await w.exec(["chown","-R",`${o}:${o}`,t.workingDirectory],{timeoutMs:3e4})).wait(),this.sandboxUsers.set(w.sandboxId,o),new g(w,c,o)}async connect(t,e){let r=await this.client.sandboxes.fromId(t),s=this.sandboxUsers.get(t)??k;return new g(r,void 0,s)}async list(t){let e=await this.walk(t);if(e.error&&!e.stoppedAtLimit)throw new Error(e.error);return e.sandboxes}async listAll(t){let{stoppedAtLimit:e,...r}=await this.walk(t);return r}async walk(t){if(t?.state&&!t.state.includes("running"))return {sandboxes:[],complete:true,pagesFetched:0,stoppedAtLimit:false};let e;try{e=await this.getApp();}catch(r){return {sandboxes:[],complete:false,pagesFetched:0,stoppedAtLimit:false,error:`sandbox list failed: ${r instanceof Error?r.message:String(r)}`}}return L(()=>this.client.sandboxes.list({appId:e.appId,tags:t?.metadata}),t?.limit)}async listSandboxIds(){let t=new Set;try{let e=await this.getApp();for await(let r of this.client.sandboxes.list({appId:e.appId}))t.add(r.sandboxId);return {ids:t,complete:!0}}catch{return {ids:new Set,complete:false}}}};async function L(n,t){let e=[];try{for await(let r of n()){if(t!==void 0&&e.length>=t)return {sandboxes:e,complete:!1,pagesFetched:e.length,stoppedAtLimit:!0,error:`stopped at the requested limit of ${t} with more sandboxes available`};if(e.length>=M)return {sandboxes:e,complete:!1,pagesFetched:e.length,stoppedAtLimit:!1,error:`sandbox list exceeded ${M} sandboxes`};let s=await r.getTags();e.push(I(r.sandboxId,s));}}catch(r){return {sandboxes:e,complete:false,pagesFetched:e.length,stoppedAtLimit:false,error:`sandbox list failed: ${r instanceof Error?r.message:String(r)}`}}return {sandboxes:e,complete:true,pagesFetched:e.length,stoppedAtLimit:false}}function rt(n={}){let t=n.tokenId??process.env.MODAL_TOKEN_ID,e=n.tokenSecret??process.env.MODAL_TOKEN_SECRET;if(!t||!e)throw new Error("Modal credentials required. Set MODAL_TOKEN_ID and MODAL_TOKEN_SECRET environment variables, or pass tokenId/tokenSecret in config. Get your token at https://modal.com/settings/tokens");return new P({...n,tokenId:t,tokenSecret:e})}var st=b,nt=T,it=N,ot=$,at=I,dt=L,ct=C,mt=O;
4
+ exports.MODAL_MAX_LIFETIME_MS=E;exports.MODAL_MAX_LIST_SANDBOXES=M;exports.MODAL_STDIN_CHUNK_BYTES=A;exports.ModalCommands=y;exports.ModalFiles=v;exports.ModalIdleTimeoutError=p;exports.ModalNetworkPolicyError=u;exports.ModalProvider=P;exports.ModalResourcesError=S;exports.ModalSandboxLifetimeError=x;exports._testBuildSandboxInfo=at;exports._testCollectSandboxes=dt;exports._testMapIdleTimeout=mt;exports._testMapNetworkPolicy=nt;exports._testMapResources=it;exports._testResolveImageRegistry=ot;exports._testValidateTimeout=ct;exports._testWrapCommand=st;exports.createModalProvider=rt;
package/dist/index.d.cts CHANGED
@@ -1,7 +1,9 @@
1
+ import { Sandbox } from 'modal';
2
+
1
3
  /**
2
4
  * Modal Sandbox Provider - Clean Architecture
3
5
  *
4
- * @requires modal >= 0.3.0
6
+ * @requires modal >= 0.9.0
5
7
  * @requires Node.js >= 18 (for ReadableStream support)
6
8
  *
7
9
  * Design principles:
@@ -13,9 +15,149 @@
13
15
  *
14
16
  * Modal-specific notes:
15
17
  * - No native file APIs - uses exec() with stdin/stdout
16
- * - pause() not supported - throws error
18
+ * - pause() not supported - throws error (use Evolve checkpoints for persistence)
17
19
  * - Requires app context for sandbox creation
20
+ * - Hard 24h sandbox lifetime cap (ModalSandboxLifetimeError when exceeded)
21
+ * - Everything executes as root inside the sandbox; the `user` option is
22
+ * enforced through an `su <user> -c` wrapper (default user: "user")
23
+ * - Network policy maps to Modal's blockNetwork / outboundDomainAllowlist /
24
+ * outboundCidrAllowlist (domain allowlist admits TLS on port 443 only —
25
+ * plaintext destinations must be listed as IPs/CIDRs)
26
+ * - Modal exposes no metadata or public timestamps on sandboxes; both are
27
+ * stamped into sandbox tags at create time and read back via getTags()
28
+ */
29
+
30
+ /**
31
+ * Modal's hard cap on sandbox lifetime (24 hours).
32
+ * Requests beyond this throw ModalSandboxLifetimeError.
33
+ */
34
+ declare const MODAL_MAX_LIFETIME_MS: number;
35
+ /**
36
+ * Chunk size for stdin uploads. Modal's gRPC transport rejects any single
37
+ * TaskExecStdinWrite message larger than 100MiB (RESOURCE_EXHAUSTED at
38
+ * 104,857,600 bytes), so file payloads are split into 8MiB writeBytes()
39
+ * calls — the same per-chunk pattern writeStream() uses.
40
+ */
41
+ declare const MODAL_STDIN_CHUNK_BYTES: number;
42
+ /**
43
+ * Typed error for Modal's hard 24h sandbox lifetime cap.
44
+ * Long-running sessions must persist progress with Evolve checkpoints and
45
+ * resume in a fresh sandbox instead of extending the timeout.
46
+ */
47
+ declare class ModalSandboxLifetimeError extends Error {
48
+ readonly requestedTimeoutMs: number;
49
+ constructor(requestedTimeoutMs: number);
50
+ }
51
+ /** Throws ModalSandboxLifetimeError when the timeout exceeds Modal's 24h cap. */
52
+ declare function validateTimeout(timeoutMs: number): void;
53
+ /**
54
+ * Typed error for an idle timeout Modal could not act on. Both bounds are
55
+ * refusals rather than clamps: silently raising a zero, or lowering a value past
56
+ * the lifetime cap, would hand back a box that dies on a schedule the caller
57
+ * never asked for.
18
58
  */
59
+ declare class ModalIdleTimeoutError extends Error {
60
+ readonly requestedIdleTimeoutMs: number;
61
+ constructor(requestedIdleTimeoutMs: number, reason: string);
62
+ }
63
+ /**
64
+ * Evolve's idle bound -> Modal's create params, same shape as mapNetworkPolicy
65
+ * and mapResources: provider-neutral option in, Modal fragment out.
66
+ *
67
+ * ABSENT MEANS ABSENT. Modal's own default is no idle timer at all, so an unset
68
+ * option must spread to nothing — inventing a default here would start killing
69
+ * boxes that today live out their lifetime, for every caller who never asked.
70
+ *
71
+ * An idle timeout has to be a positive span, and one above the 24h lifetime cap
72
+ * can never fire because the sandbox is already gone. Both are caller mistakes,
73
+ * and both throw rather than clamp: silently raising a zero or lowering an
74
+ * over-cap value hands back a box that dies on a schedule nobody chose.
75
+ */
76
+ declare function mapIdleTimeout(idleTimeoutMs?: number): {
77
+ idleTimeoutMs?: number;
78
+ };
79
+ /**
80
+ * Wrap a command with cwd + env handling and (when not root) an
81
+ * `su <user> -c` wrapper.
82
+ *
83
+ * Modal sandboxes run as root by default (ignoring the Dockerfile USER
84
+ * directive), but Claude CLI and other tools refuse certain operations when
85
+ * running as root.
86
+ *
87
+ * Uses `su <user> -c` instead of `sudo -u <user>` because Claude CLI's
88
+ * --dangerously-skip-permissions flag refuses to run when it detects sudo.
89
+ *
90
+ * Uses base64 encoding to avoid shell escaping issues with complex commands
91
+ * that contain quotes, special characters, etc. Env vars are inlined because
92
+ * su does not preserve the environment the way `sudo -E` does.
93
+ */
94
+ declare function wrapCommand(command: string, user: string, cwd?: string, envs?: Record<string, string>): string[];
95
+ /**
96
+ * Typed error for sizing requests Modal's create() cannot enforce.
97
+ * The installed Modal JS SDK sizes cpu (cores) and memoryMiB at create time
98
+ * only — there is no disk-size parameter, so a requested disk size would be
99
+ * silently ignored. Per the provider law (reject what you cannot enforce,
100
+ * never silently ignore) it is refused loudly here.
101
+ */
102
+ declare class ModalResourcesError extends Error {
103
+ constructor(message: string);
104
+ }
105
+ /**
106
+ * Map Evolve's provider-neutral resources (cpu cores, memory GiB, disk GiB)
107
+ * onto Modal's create() params (cpu cores, memoryMiB). Fractional GiB rounds
108
+ * UP so the sandbox never gets less memory than requested. `disk` throws
109
+ * ModalResourcesError — the SDK cannot express it.
110
+ */
111
+ declare function mapResources(resources?: SandboxCreateOptions["resources"]): {
112
+ cpu: number;
113
+ memoryMiB: number;
114
+ };
115
+ /** Modal create() params derived from Evolve's provider-neutral network policy. */
116
+ interface ModalNetworkCreateParams {
117
+ blockNetwork?: boolean;
118
+ outboundCidrAllowlist?: string[];
119
+ outboundDomainAllowlist?: string[];
120
+ }
121
+ /** Why a network destination cannot be mapped onto Modal's allowlist. */
122
+ type ModalNetworkPolicyReason = "port-unsupported" | "invalid-ipv4";
123
+ /**
124
+ * Typed error for destinations Modal's allowlist cannot express.
125
+ *
126
+ * Modal's allowlist filters hosts (domain allowlist) and IPs/CIDRs (CIDR
127
+ * allowlist) only — it has no notion of a port, and an invalid IPv4/CIDR
128
+ * would be silently forwarded to the API. Both are rejected loudly here
129
+ * instead of weakening or mangling the sandbox's egress policy.
130
+ */
131
+ declare class ModalNetworkPolicyError extends Error {
132
+ readonly reason: ModalNetworkPolicyReason;
133
+ /** The offending destination. */
134
+ readonly destination?: string;
135
+ constructor(reason: ModalNetworkPolicyReason, message: string, destination?: string);
136
+ }
137
+ /**
138
+ * Map Evolve's provider-neutral network policy onto Modal create() params.
139
+ *
140
+ * - outbound "open" (or no policy) → no restrictions
141
+ * - outbound "blocked", no allowlist → blockNetwork: true (drops all egress)
142
+ * - outbound "blocked" with allowlist → outboundDomainAllowlist (hostnames,
143
+ * wildcards like "*.example.com") + outboundCidrAllowlist (IPs/CIDRs; bare
144
+ * IPs get /32 or /128 appended). Both lists are always set because Modal
145
+ * treats an unset list as "allow all" — an empty array means "allow none"
146
+ * for that class of destination. Note: Modal's domain allowlist only admits
147
+ * TLS traffic on port 443; plaintext destinations must be listed as CIDRs.
148
+ */
149
+ declare function mapNetworkPolicy(network?: SandboxCreateOptions["network"]): ModalNetworkCreateParams;
150
+ /** Container registry family for an image tag. */
151
+ type ImageRegistry = "aws-ecr" | "gcp-artifact-registry" | "registry";
152
+ /** Detect which Modal image constructor an image tag needs. */
153
+ declare function resolveImageRegistry(tag: string): ImageRegistry;
154
+ /**
155
+ * Build a SandboxInfo from a sandbox's tags. Modal exposes no metadata or
156
+ * public timestamps, so image and startedAt come from the tags stamped at
157
+ * create time; for sandboxes not created by this SDK they are empty strings
158
+ * (never fabricated). endAt is always undefined — Modal does not expose it.
159
+ */
160
+ declare function buildSandboxInfo(sandboxId: string, tags: Record<string, string>, fallbackImage?: string): SandboxInfo;
19
161
  /** Result of a completed sandbox command */
20
162
  interface SandboxCommandResult {
21
163
  exitCode: number;
@@ -93,15 +235,83 @@ interface SandboxCreateOptions {
93
235
  image?: string;
94
236
  envs?: Record<string, string>;
95
237
  metadata?: Record<string, string>;
238
+ /** Sandbox lifetime in ms. Modal hard-caps lifetime at 24h (MODAL_MAX_LIFETIME_MS). */
96
239
  timeoutMs?: number;
240
+ /**
241
+ * Terminate the sandbox after this long with nothing running in it — the
242
+ * bound that reclaims a box whose client died, without waiting out the whole
243
+ * lifetime. Modal is the only provider with both clocks.
244
+ *
245
+ * OMITTED BY DEFAULT: Modal runs no idle timer unless asked. Modal counts a
246
+ * sandbox active while an exec is running, while its stdin is being written,
247
+ * or while a tunnel connection is open — file operations are not named in
248
+ * that list, and this adapter is safe only because it routes reads and writes
249
+ * through exec (`cat` / `cat >`). A future native filesystem path would need
250
+ * this re-checked.
251
+ */
252
+ idleTimeoutMs?: number;
97
253
  workingDirectory?: string;
254
+ /**
255
+ * Per-sandbox compute sizing: cpu in cores, memory in GiB — mapped to
256
+ * Modal's create-time cpu / memoryMiB requests (defaults when omitted:
257
+ * 4 cores / 4 GiB). `disk` is REJECTED with ModalResourcesError: the Modal
258
+ * JS SDK exposes no disk-size parameter, so a specific disk size cannot be
259
+ * enforced (containers get Modal's default disk quota).
260
+ */
261
+ resources?: {
262
+ cpu?: number;
263
+ memory?: number;
264
+ disk?: number;
265
+ };
266
+ /**
267
+ * Provider-neutral outbound network policy, enforced by Modal's network
268
+ * stack. "blocked" with no allowedDestinations drops all egress; with
269
+ * allowedDestinations, hostnames go to Modal's domain allowlist (TLS/443
270
+ * only) and IPs/CIDRs to the CIDR allowlist.
271
+ */
272
+ network?: {
273
+ outbound: "open" | "blocked";
274
+ allowedDestinations?: string[];
275
+ };
276
+ /**
277
+ * Run all commands and file operations as this user (default "user"),
278
+ * enforced via an `su <user> -c` wrapper since Modal executes everything as
279
+ * root. Pass "root" to run directly as root with no wrapper.
280
+ */
281
+ user?: string;
282
+ /** Home directory used by the SDK for agent config paths; not consumed by the provider. */
283
+ homeDir?: string;
98
284
  }
99
285
  /** Options for listing sandboxes */
100
286
  interface SandboxListOptions {
287
+ /** Modal has no paused state; filters that exclude "running" match nothing. */
101
288
  state?: ("running" | "paused")[];
102
289
  metadata?: Record<string, string>;
103
290
  limit?: number;
104
291
  }
292
+ /**
293
+ * A COMPLETE (or admittedly incomplete) enumeration of the app's fleet.
294
+ *
295
+ * `complete` is the load-bearing field. Callers that need a whole fleet —
296
+ * orphan sweeps, lifecycle reconciliation — read a sandbox's ABSENCE from the
297
+ * list as evidence it is gone, so a truncated walk and a small fleet must never
298
+ * be the same answer. That includes a walk stopped by the caller's own `limit`:
299
+ * "you asked for ten and there are more" is a truncated fleet.
300
+ */
301
+ interface SandboxListPage {
302
+ sandboxes: SandboxInfo[];
303
+ complete: boolean;
304
+ pagesFetched: number;
305
+ error?: string;
306
+ }
307
+ /**
308
+ * Sandboxes a single enumeration will walk before it gives up and reports
309
+ * itself incomplete. Modal's list is an async generator with no page size we
310
+ * control, so the ceiling is counted in SANDBOXES rather than pages — same
311
+ * purpose as the other providers' page caps: never return a short list that
312
+ * reads like a whole one.
313
+ */
314
+ declare const MODAL_MAX_LIST_SANDBOXES = 10000;
105
315
  /** Command execution capabilities */
106
316
  interface SandboxCommands {
107
317
  /** Run command and wait for completion */
@@ -132,6 +342,8 @@ interface SandboxFiles {
132
342
  readStream(path: string): Promise<ReadableStream<Uint8Array>>;
133
343
  /** Write from stream */
134
344
  writeStream(path: string, stream: ReadableStream<Uint8Array>): Promise<void>;
345
+ /** Upload a local file by path, streamed off disk (never buffered whole) */
346
+ writeFromPath(sandboxPath: string, localPath: string): Promise<void>;
135
347
  /** Get pre-signed upload URL for large files (expiration in seconds) */
136
348
  uploadUrl(path: string, expiresInSeconds?: number): Promise<string>;
137
349
  /** Get pre-signed download URL for large files (expiration in seconds) */
@@ -169,12 +381,17 @@ interface SandboxInstance {
169
381
  interface SandboxProvider {
170
382
  /** Provider type identifier */
171
383
  readonly providerType: string;
384
+ /** Human-readable provider name for logging */
385
+ readonly name?: string;
172
386
  /** Create new sandbox */
173
387
  create(options: SandboxCreateOptions): Promise<SandboxInstance>;
174
388
  /** Connect to existing sandbox */
175
389
  connect(sandboxId: string, timeoutMs?: number): Promise<SandboxInstance>;
176
390
  /** List sandboxes (first page only, up to limit) */
391
+ /** List sandboxes, walking the whole app. `limit` bounds items returned. */
177
392
  list(options?: SandboxListOptions): Promise<SandboxInfo[]>;
393
+ /** The same enumeration for fleet bookkeeping: never throws, reports completeness. */
394
+ listAll(options?: SandboxListOptions): Promise<SandboxListPage>;
178
395
  }
179
396
  interface ModalConfig {
180
397
  /** Modal app name. Default: "evolve-sandbox" */
@@ -189,6 +406,13 @@ interface ModalConfig {
189
406
  endpoint?: string;
190
407
  /** Docker image name (default: 'evolve-all'). Resolved through IMAGE_MAP or used as-is for custom images. */
191
408
  imageName?: string;
409
+ /**
410
+ * Name of a Modal Secret holding registry credentials for private images.
411
+ * Required for AWS ECR (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY,
412
+ * AWS_REGION with read-only ECR IAM) and GCP Artifact Registry; optional
413
+ * for private Docker Hub images. Create one at https://modal.com/secrets
414
+ */
415
+ imageSecretName?: string;
192
416
  }
193
417
  /** Internal resolved config with required credentials */
194
418
  interface ResolvedModalConfig {
@@ -198,6 +422,64 @@ interface ResolvedModalConfig {
198
422
  defaultTimeoutMs?: number;
199
423
  endpoint?: string;
200
424
  imageName?: string;
425
+ imageSecretName?: string;
426
+ }
427
+ declare class ModalCommands implements SandboxCommands {
428
+ private sandbox;
429
+ private user;
430
+ constructor(sandbox: Sandbox, user: string);
431
+ run(command: string, options?: SandboxRunOptions): Promise<SandboxCommandResult>;
432
+ spawn(command: string, options?: SandboxSpawnOptions): Promise<SandboxCommandHandle>;
433
+ list(): Promise<ProcessInfo[]>;
434
+ connect(_processId: string, _options?: SandboxConnectOptions): Promise<SandboxCommandHandle>;
435
+ sendStdin(_processId: string, _data: string): Promise<void>;
436
+ kill(processId: string): Promise<boolean>;
437
+ /**
438
+ * Accumulate stdout/stderr using for-await pattern (more reliable with Modal streams).
439
+ * Based on vibekit's approach which works correctly with Modal SDK.
440
+ */
441
+ private accumulateStreams;
442
+ }
443
+ declare class ModalFiles implements SandboxFiles {
444
+ private sandbox;
445
+ private user;
446
+ constructor(sandbox: Sandbox, user: string);
447
+ /**
448
+ * Chown a path to the sandbox user so agent CLIs (running via the su
449
+ * wrapper) can access files created by root-level exec. No-op when the
450
+ * sandbox user is root.
451
+ */
452
+ private chownToUser;
453
+ /**
454
+ * Write a payload to a process's stdin in MODAL_STDIN_CHUNK_BYTES slices.
455
+ * Each writeBytes() call becomes one gRPC TaskExecStdinWrite message and
456
+ * Modal rejects messages over 100MiB, so large files must be chunked
457
+ * (multi-hundred-MB payloads are common).
458
+ */
459
+ private writeStdinChunked;
460
+ read(path: string): Promise<string | Uint8Array>;
461
+ write(path: string, content: string | Buffer | ArrayBuffer | Uint8Array): Promise<void>;
462
+ writeBatch(files: Array<{
463
+ path: string;
464
+ data: string | Buffer | ArrayBuffer | Uint8Array;
465
+ }>): Promise<void>;
466
+ makeDir(path: string): Promise<void>;
467
+ exists(path: string): Promise<boolean>;
468
+ list(path: string): Promise<FileInfo[]>;
469
+ remove(path: string): Promise<void>;
470
+ rename(oldPath: string, newPath: string): Promise<void>;
471
+ readStream(path: string): Promise<ReadableStream<Uint8Array>>;
472
+ writeStream(path: string, stream: ReadableStream<Uint8Array>): Promise<void>;
473
+ /**
474
+ * Upload a local file by PATH, chunk by chunk into the same `cat >` sink
475
+ * writeStream() uses — so peak memory is one chunk rather than the whole
476
+ * file, which is what makes a large artifact safe under concurrency.
477
+ */
478
+ writeFromPath(sandboxPath: string, localPath: string): Promise<void>;
479
+ uploadUrl(_path: string, _expiresInSeconds?: number): Promise<string>;
480
+ downloadUrl(_path: string, _expiresInSeconds?: number): Promise<string>;
481
+ watchDir(_path: string, _onEvent: (event: FilesystemEvent) => void | Promise<void>, _options?: WatchOptions): Promise<WatchHandle>;
482
+ private toBuffer;
201
483
  }
202
484
  declare class ModalProvider implements SandboxProvider {
203
485
  readonly providerType: "modal";
@@ -206,13 +488,103 @@ declare class ModalProvider implements SandboxProvider {
206
488
  private readonly appName;
207
489
  private readonly defaultTimeoutMs;
208
490
  private readonly imageName;
491
+ private readonly imageSecretName?;
209
492
  private _app;
493
+ /**
494
+ * Sandbox user configured at create time, reapplied on connect() so the su
495
+ * wrapper keeps targeting the same account. In-memory only: a connect()
496
+ * from a fresh process falls back to the default "user" account — callers
497
+ * reconnecting across processes must recreate the provider and sandbox with
498
+ * the same user, or operations on user-owned files fail loudly.
499
+ */
500
+ private readonly sandboxUsers;
210
501
  constructor(config: ResolvedModalConfig);
211
502
  private getApp;
503
+ /**
504
+ * Build a Modal Image for the resolved tag, routing private registries
505
+ * (AWS ECR, GCP Artifact Registry) through the configured Modal Secret.
506
+ */
507
+ private resolveImage;
212
508
  create(options: SandboxCreateOptions): Promise<SandboxInstance>;
213
509
  connect(sandboxId: string, _timeoutMs?: number): Promise<SandboxInstance>;
214
- list(_options?: SandboxListOptions): Promise<SandboxInfo[]>;
510
+ /**
511
+ * List sandboxes, walking the whole app.
512
+ *
513
+ * This used to stop at a hardcoded default of 100 regardless of fleet size,
514
+ * which silently truncated any app with more — and said nothing about it
515
+ * while the shared SandboxProvider interface promised exhaustive listing.
516
+ * `limit` still bounds the sandboxes RETURNED, so a caller wanting one cheap
517
+ * sample asks for one; without it the answer is the whole app.
518
+ *
519
+ * The O(N)-round-trips warning below is unchanged and is the reason `limit`
520
+ * matters here more than on the other providers.
521
+ */
522
+ list(options?: SandboxListOptions): Promise<SandboxInfo[]>;
523
+ /**
524
+ * The fleet-bookkeeping enumeration: same walk, never throws.
525
+ *
526
+ * Modal has no lifecycle webhooks, so absence from a list is the ONLY
527
+ * termination signal either lane gets — which makes the difference between
528
+ * "the app is empty" and "the enumeration stopped early" the difference
529
+ * between a quiet fleet and one about to be reclaimed.
530
+ *
531
+ * NOTE the divergence from `listSandboxIds` below, which returns an EMPTY set
532
+ * on failure on the grounds that partial results are worse than none for a
533
+ * terminal-state decision. This one returns what it saw alongside
534
+ * `complete: false`. Both are safe because `complete` is what callers branch
535
+ * on, and the shared type documents the choice; do not align one to the other
536
+ * without deciding which rule you want.
537
+ */
538
+ listAll(options?: SandboxListOptions): Promise<SandboxListPage>;
539
+ private walk;
540
+ /**
541
+ * Live sandbox ids for the whole app, in ONE streamed call and O(1) round
542
+ * trips — the fleet-bookkeeping counterpart to `list()`.
543
+ *
544
+ * Exists because `list()` cannot be made cheap without dropping metadata from
545
+ * its contract: it owes callers a populated `SandboxInfo.metadata`, and Modal
546
+ * only serves tags per sandbox. Anything that just needs "which ids are
547
+ * alive" — lifecycle polling, orphan sweeps, reconciliation — uses this and
548
+ * pays one request no matter how large the fleet is.
549
+ *
550
+ * `complete` is the load-bearing field, not a nicety: absence from this list
551
+ * is what callers read as "terminated", so a truncated or errored enumeration
552
+ * MUST NOT be mistaken for an empty fleet. A caller that sees complete=false
553
+ * has to leave rows alone rather than mass-marking live sandboxes dead.
554
+ */
555
+ listSandboxIds(): Promise<{
556
+ ids: Set<string>;
557
+ complete: boolean;
558
+ }>;
559
+ }
560
+ /** The streamed sandbox surface this walk needs — Modal's list() satisfies it. */
561
+ interface ModalSandboxStream {
562
+ sandboxId: string;
563
+ getTags(): Promise<Record<string, string>>;
215
564
  }
565
+ /**
566
+ * Drain Modal's sandbox generator into one answer, with an honest completeness
567
+ * verdict.
568
+ *
569
+ * Separate from the provider because everything worth getting wrong lives here
570
+ * and none of it needs a gRPC connection: the difference between "the caller
571
+ * asked for ten" and "the app ran out", the ceiling that stops an unbounded
572
+ * walk, and the rule that a failure mid-walk yields what it saw marked
573
+ * INCOMPLETE rather than an exception or a short complete list.
574
+ *
575
+ * COST WARNING — this loop is O(N) ROUND TRIPS, not O(1). `list()` itself is one
576
+ * streamed call, but `getTags()` is a separate gRPC request per sandbox
577
+ * (`sandboxTagsGet`), so listing N sandboxes costs N+1 calls. That is fine for a
578
+ * user listing their handful of boxes with metadata, and NOT fine for fleet-wide
579
+ * bookkeeping: anything that only needs to know WHICH ids are alive must use
580
+ * `listSandboxIds`, never this. Please do not "optimize" by reintroducing tag
581
+ * reads into those paths.
582
+ *
583
+ * Exported for its test (`_testCollectSandboxes`).
584
+ */
585
+ declare function collectSandboxes(iterate: () => AsyncIterable<ModalSandboxStream>, wanted?: number): Promise<SandboxListPage & {
586
+ stoppedAtLimit: boolean;
587
+ }>;
216
588
  /**
217
589
  * Create Modal sandbox provider.
218
590
  *
@@ -222,5 +594,13 @@ declare class ModalProvider implements SandboxProvider {
222
594
  * @see https://github.com/evolving-machines-lab/evolve/issues/8
223
595
  */
224
596
  declare function createModalProvider(config?: ModalConfig): SandboxProvider;
597
+ declare const _testWrapCommand: typeof wrapCommand;
598
+ declare const _testMapNetworkPolicy: typeof mapNetworkPolicy;
599
+ declare const _testMapResources: typeof mapResources;
600
+ declare const _testResolveImageRegistry: typeof resolveImageRegistry;
601
+ declare const _testBuildSandboxInfo: typeof buildSandboxInfo;
602
+ declare const _testCollectSandboxes: typeof collectSandboxes;
603
+ declare const _testValidateTimeout: typeof validateTimeout;
604
+ declare const _testMapIdleTimeout: typeof mapIdleTimeout;
225
605
 
226
- export { type FileInfo, type FilesystemEvent, type ModalConfig, ModalProvider, type ProcessInfo, type SandboxCommandHandle, type SandboxCommandResult, type SandboxCommands, type SandboxConnectOptions, type SandboxCreateOptions, type SandboxFiles, type SandboxInfo, type SandboxInstance, type SandboxListOptions, type SandboxProvider, type SandboxRunOptions, type SandboxSpawnOptions, type WatchHandle, type WatchOptions, createModalProvider };
606
+ export { type FileInfo, type FilesystemEvent, MODAL_MAX_LIFETIME_MS, MODAL_MAX_LIST_SANDBOXES, MODAL_STDIN_CHUNK_BYTES, ModalCommands, type ModalConfig, ModalFiles, ModalIdleTimeoutError, ModalNetworkPolicyError, type ModalNetworkPolicyReason, ModalProvider, ModalResourcesError, ModalSandboxLifetimeError, type ModalSandboxStream, type ProcessInfo, type SandboxCommandHandle, type SandboxCommandResult, type SandboxCommands, type SandboxConnectOptions, type SandboxCreateOptions, type SandboxFiles, type SandboxInfo, type SandboxInstance, type SandboxListOptions, type SandboxListPage, type SandboxProvider, type SandboxRunOptions, type SandboxSpawnOptions, type WatchHandle, type WatchOptions, _testBuildSandboxInfo, _testCollectSandboxes, _testMapIdleTimeout, _testMapNetworkPolicy, _testMapResources, _testResolveImageRegistry, _testValidateTimeout, _testWrapCommand, createModalProvider };
package/dist/index.d.ts CHANGED
@@ -1,7 +1,9 @@
1
+ import { Sandbox } from 'modal';
2
+
1
3
  /**
2
4
  * Modal Sandbox Provider - Clean Architecture
3
5
  *
4
- * @requires modal >= 0.3.0
6
+ * @requires modal >= 0.9.0
5
7
  * @requires Node.js >= 18 (for ReadableStream support)
6
8
  *
7
9
  * Design principles:
@@ -13,9 +15,149 @@
13
15
  *
14
16
  * Modal-specific notes:
15
17
  * - No native file APIs - uses exec() with stdin/stdout
16
- * - pause() not supported - throws error
18
+ * - pause() not supported - throws error (use Evolve checkpoints for persistence)
17
19
  * - Requires app context for sandbox creation
20
+ * - Hard 24h sandbox lifetime cap (ModalSandboxLifetimeError when exceeded)
21
+ * - Everything executes as root inside the sandbox; the `user` option is
22
+ * enforced through an `su <user> -c` wrapper (default user: "user")
23
+ * - Network policy maps to Modal's blockNetwork / outboundDomainAllowlist /
24
+ * outboundCidrAllowlist (domain allowlist admits TLS on port 443 only —
25
+ * plaintext destinations must be listed as IPs/CIDRs)
26
+ * - Modal exposes no metadata or public timestamps on sandboxes; both are
27
+ * stamped into sandbox tags at create time and read back via getTags()
28
+ */
29
+
30
+ /**
31
+ * Modal's hard cap on sandbox lifetime (24 hours).
32
+ * Requests beyond this throw ModalSandboxLifetimeError.
33
+ */
34
+ declare const MODAL_MAX_LIFETIME_MS: number;
35
+ /**
36
+ * Chunk size for stdin uploads. Modal's gRPC transport rejects any single
37
+ * TaskExecStdinWrite message larger than 100MiB (RESOURCE_EXHAUSTED at
38
+ * 104,857,600 bytes), so file payloads are split into 8MiB writeBytes()
39
+ * calls — the same per-chunk pattern writeStream() uses.
40
+ */
41
+ declare const MODAL_STDIN_CHUNK_BYTES: number;
42
+ /**
43
+ * Typed error for Modal's hard 24h sandbox lifetime cap.
44
+ * Long-running sessions must persist progress with Evolve checkpoints and
45
+ * resume in a fresh sandbox instead of extending the timeout.
46
+ */
47
+ declare class ModalSandboxLifetimeError extends Error {
48
+ readonly requestedTimeoutMs: number;
49
+ constructor(requestedTimeoutMs: number);
50
+ }
51
+ /** Throws ModalSandboxLifetimeError when the timeout exceeds Modal's 24h cap. */
52
+ declare function validateTimeout(timeoutMs: number): void;
53
+ /**
54
+ * Typed error for an idle timeout Modal could not act on. Both bounds are
55
+ * refusals rather than clamps: silently raising a zero, or lowering a value past
56
+ * the lifetime cap, would hand back a box that dies on a schedule the caller
57
+ * never asked for.
18
58
  */
59
+ declare class ModalIdleTimeoutError extends Error {
60
+ readonly requestedIdleTimeoutMs: number;
61
+ constructor(requestedIdleTimeoutMs: number, reason: string);
62
+ }
63
+ /**
64
+ * Evolve's idle bound -> Modal's create params, same shape as mapNetworkPolicy
65
+ * and mapResources: provider-neutral option in, Modal fragment out.
66
+ *
67
+ * ABSENT MEANS ABSENT. Modal's own default is no idle timer at all, so an unset
68
+ * option must spread to nothing — inventing a default here would start killing
69
+ * boxes that today live out their lifetime, for every caller who never asked.
70
+ *
71
+ * An idle timeout has to be a positive span, and one above the 24h lifetime cap
72
+ * can never fire because the sandbox is already gone. Both are caller mistakes,
73
+ * and both throw rather than clamp: silently raising a zero or lowering an
74
+ * over-cap value hands back a box that dies on a schedule nobody chose.
75
+ */
76
+ declare function mapIdleTimeout(idleTimeoutMs?: number): {
77
+ idleTimeoutMs?: number;
78
+ };
79
+ /**
80
+ * Wrap a command with cwd + env handling and (when not root) an
81
+ * `su <user> -c` wrapper.
82
+ *
83
+ * Modal sandboxes run as root by default (ignoring the Dockerfile USER
84
+ * directive), but Claude CLI and other tools refuse certain operations when
85
+ * running as root.
86
+ *
87
+ * Uses `su <user> -c` instead of `sudo -u <user>` because Claude CLI's
88
+ * --dangerously-skip-permissions flag refuses to run when it detects sudo.
89
+ *
90
+ * Uses base64 encoding to avoid shell escaping issues with complex commands
91
+ * that contain quotes, special characters, etc. Env vars are inlined because
92
+ * su does not preserve the environment the way `sudo -E` does.
93
+ */
94
+ declare function wrapCommand(command: string, user: string, cwd?: string, envs?: Record<string, string>): string[];
95
+ /**
96
+ * Typed error for sizing requests Modal's create() cannot enforce.
97
+ * The installed Modal JS SDK sizes cpu (cores) and memoryMiB at create time
98
+ * only — there is no disk-size parameter, so a requested disk size would be
99
+ * silently ignored. Per the provider law (reject what you cannot enforce,
100
+ * never silently ignore) it is refused loudly here.
101
+ */
102
+ declare class ModalResourcesError extends Error {
103
+ constructor(message: string);
104
+ }
105
+ /**
106
+ * Map Evolve's provider-neutral resources (cpu cores, memory GiB, disk GiB)
107
+ * onto Modal's create() params (cpu cores, memoryMiB). Fractional GiB rounds
108
+ * UP so the sandbox never gets less memory than requested. `disk` throws
109
+ * ModalResourcesError — the SDK cannot express it.
110
+ */
111
+ declare function mapResources(resources?: SandboxCreateOptions["resources"]): {
112
+ cpu: number;
113
+ memoryMiB: number;
114
+ };
115
+ /** Modal create() params derived from Evolve's provider-neutral network policy. */
116
+ interface ModalNetworkCreateParams {
117
+ blockNetwork?: boolean;
118
+ outboundCidrAllowlist?: string[];
119
+ outboundDomainAllowlist?: string[];
120
+ }
121
+ /** Why a network destination cannot be mapped onto Modal's allowlist. */
122
+ type ModalNetworkPolicyReason = "port-unsupported" | "invalid-ipv4";
123
+ /**
124
+ * Typed error for destinations Modal's allowlist cannot express.
125
+ *
126
+ * Modal's allowlist filters hosts (domain allowlist) and IPs/CIDRs (CIDR
127
+ * allowlist) only — it has no notion of a port, and an invalid IPv4/CIDR
128
+ * would be silently forwarded to the API. Both are rejected loudly here
129
+ * instead of weakening or mangling the sandbox's egress policy.
130
+ */
131
+ declare class ModalNetworkPolicyError extends Error {
132
+ readonly reason: ModalNetworkPolicyReason;
133
+ /** The offending destination. */
134
+ readonly destination?: string;
135
+ constructor(reason: ModalNetworkPolicyReason, message: string, destination?: string);
136
+ }
137
+ /**
138
+ * Map Evolve's provider-neutral network policy onto Modal create() params.
139
+ *
140
+ * - outbound "open" (or no policy) → no restrictions
141
+ * - outbound "blocked", no allowlist → blockNetwork: true (drops all egress)
142
+ * - outbound "blocked" with allowlist → outboundDomainAllowlist (hostnames,
143
+ * wildcards like "*.example.com") + outboundCidrAllowlist (IPs/CIDRs; bare
144
+ * IPs get /32 or /128 appended). Both lists are always set because Modal
145
+ * treats an unset list as "allow all" — an empty array means "allow none"
146
+ * for that class of destination. Note: Modal's domain allowlist only admits
147
+ * TLS traffic on port 443; plaintext destinations must be listed as CIDRs.
148
+ */
149
+ declare function mapNetworkPolicy(network?: SandboxCreateOptions["network"]): ModalNetworkCreateParams;
150
+ /** Container registry family for an image tag. */
151
+ type ImageRegistry = "aws-ecr" | "gcp-artifact-registry" | "registry";
152
+ /** Detect which Modal image constructor an image tag needs. */
153
+ declare function resolveImageRegistry(tag: string): ImageRegistry;
154
+ /**
155
+ * Build a SandboxInfo from a sandbox's tags. Modal exposes no metadata or
156
+ * public timestamps, so image and startedAt come from the tags stamped at
157
+ * create time; for sandboxes not created by this SDK they are empty strings
158
+ * (never fabricated). endAt is always undefined — Modal does not expose it.
159
+ */
160
+ declare function buildSandboxInfo(sandboxId: string, tags: Record<string, string>, fallbackImage?: string): SandboxInfo;
19
161
  /** Result of a completed sandbox command */
20
162
  interface SandboxCommandResult {
21
163
  exitCode: number;
@@ -93,15 +235,83 @@ interface SandboxCreateOptions {
93
235
  image?: string;
94
236
  envs?: Record<string, string>;
95
237
  metadata?: Record<string, string>;
238
+ /** Sandbox lifetime in ms. Modal hard-caps lifetime at 24h (MODAL_MAX_LIFETIME_MS). */
96
239
  timeoutMs?: number;
240
+ /**
241
+ * Terminate the sandbox after this long with nothing running in it — the
242
+ * bound that reclaims a box whose client died, without waiting out the whole
243
+ * lifetime. Modal is the only provider with both clocks.
244
+ *
245
+ * OMITTED BY DEFAULT: Modal runs no idle timer unless asked. Modal counts a
246
+ * sandbox active while an exec is running, while its stdin is being written,
247
+ * or while a tunnel connection is open — file operations are not named in
248
+ * that list, and this adapter is safe only because it routes reads and writes
249
+ * through exec (`cat` / `cat >`). A future native filesystem path would need
250
+ * this re-checked.
251
+ */
252
+ idleTimeoutMs?: number;
97
253
  workingDirectory?: string;
254
+ /**
255
+ * Per-sandbox compute sizing: cpu in cores, memory in GiB — mapped to
256
+ * Modal's create-time cpu / memoryMiB requests (defaults when omitted:
257
+ * 4 cores / 4 GiB). `disk` is REJECTED with ModalResourcesError: the Modal
258
+ * JS SDK exposes no disk-size parameter, so a specific disk size cannot be
259
+ * enforced (containers get Modal's default disk quota).
260
+ */
261
+ resources?: {
262
+ cpu?: number;
263
+ memory?: number;
264
+ disk?: number;
265
+ };
266
+ /**
267
+ * Provider-neutral outbound network policy, enforced by Modal's network
268
+ * stack. "blocked" with no allowedDestinations drops all egress; with
269
+ * allowedDestinations, hostnames go to Modal's domain allowlist (TLS/443
270
+ * only) and IPs/CIDRs to the CIDR allowlist.
271
+ */
272
+ network?: {
273
+ outbound: "open" | "blocked";
274
+ allowedDestinations?: string[];
275
+ };
276
+ /**
277
+ * Run all commands and file operations as this user (default "user"),
278
+ * enforced via an `su <user> -c` wrapper since Modal executes everything as
279
+ * root. Pass "root" to run directly as root with no wrapper.
280
+ */
281
+ user?: string;
282
+ /** Home directory used by the SDK for agent config paths; not consumed by the provider. */
283
+ homeDir?: string;
98
284
  }
99
285
  /** Options for listing sandboxes */
100
286
  interface SandboxListOptions {
287
+ /** Modal has no paused state; filters that exclude "running" match nothing. */
101
288
  state?: ("running" | "paused")[];
102
289
  metadata?: Record<string, string>;
103
290
  limit?: number;
104
291
  }
292
+ /**
293
+ * A COMPLETE (or admittedly incomplete) enumeration of the app's fleet.
294
+ *
295
+ * `complete` is the load-bearing field. Callers that need a whole fleet —
296
+ * orphan sweeps, lifecycle reconciliation — read a sandbox's ABSENCE from the
297
+ * list as evidence it is gone, so a truncated walk and a small fleet must never
298
+ * be the same answer. That includes a walk stopped by the caller's own `limit`:
299
+ * "you asked for ten and there are more" is a truncated fleet.
300
+ */
301
+ interface SandboxListPage {
302
+ sandboxes: SandboxInfo[];
303
+ complete: boolean;
304
+ pagesFetched: number;
305
+ error?: string;
306
+ }
307
+ /**
308
+ * Sandboxes a single enumeration will walk before it gives up and reports
309
+ * itself incomplete. Modal's list is an async generator with no page size we
310
+ * control, so the ceiling is counted in SANDBOXES rather than pages — same
311
+ * purpose as the other providers' page caps: never return a short list that
312
+ * reads like a whole one.
313
+ */
314
+ declare const MODAL_MAX_LIST_SANDBOXES = 10000;
105
315
  /** Command execution capabilities */
106
316
  interface SandboxCommands {
107
317
  /** Run command and wait for completion */
@@ -132,6 +342,8 @@ interface SandboxFiles {
132
342
  readStream(path: string): Promise<ReadableStream<Uint8Array>>;
133
343
  /** Write from stream */
134
344
  writeStream(path: string, stream: ReadableStream<Uint8Array>): Promise<void>;
345
+ /** Upload a local file by path, streamed off disk (never buffered whole) */
346
+ writeFromPath(sandboxPath: string, localPath: string): Promise<void>;
135
347
  /** Get pre-signed upload URL for large files (expiration in seconds) */
136
348
  uploadUrl(path: string, expiresInSeconds?: number): Promise<string>;
137
349
  /** Get pre-signed download URL for large files (expiration in seconds) */
@@ -169,12 +381,17 @@ interface SandboxInstance {
169
381
  interface SandboxProvider {
170
382
  /** Provider type identifier */
171
383
  readonly providerType: string;
384
+ /** Human-readable provider name for logging */
385
+ readonly name?: string;
172
386
  /** Create new sandbox */
173
387
  create(options: SandboxCreateOptions): Promise<SandboxInstance>;
174
388
  /** Connect to existing sandbox */
175
389
  connect(sandboxId: string, timeoutMs?: number): Promise<SandboxInstance>;
176
390
  /** List sandboxes (first page only, up to limit) */
391
+ /** List sandboxes, walking the whole app. `limit` bounds items returned. */
177
392
  list(options?: SandboxListOptions): Promise<SandboxInfo[]>;
393
+ /** The same enumeration for fleet bookkeeping: never throws, reports completeness. */
394
+ listAll(options?: SandboxListOptions): Promise<SandboxListPage>;
178
395
  }
179
396
  interface ModalConfig {
180
397
  /** Modal app name. Default: "evolve-sandbox" */
@@ -189,6 +406,13 @@ interface ModalConfig {
189
406
  endpoint?: string;
190
407
  /** Docker image name (default: 'evolve-all'). Resolved through IMAGE_MAP or used as-is for custom images. */
191
408
  imageName?: string;
409
+ /**
410
+ * Name of a Modal Secret holding registry credentials for private images.
411
+ * Required for AWS ECR (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY,
412
+ * AWS_REGION with read-only ECR IAM) and GCP Artifact Registry; optional
413
+ * for private Docker Hub images. Create one at https://modal.com/secrets
414
+ */
415
+ imageSecretName?: string;
192
416
  }
193
417
  /** Internal resolved config with required credentials */
194
418
  interface ResolvedModalConfig {
@@ -198,6 +422,64 @@ interface ResolvedModalConfig {
198
422
  defaultTimeoutMs?: number;
199
423
  endpoint?: string;
200
424
  imageName?: string;
425
+ imageSecretName?: string;
426
+ }
427
+ declare class ModalCommands implements SandboxCommands {
428
+ private sandbox;
429
+ private user;
430
+ constructor(sandbox: Sandbox, user: string);
431
+ run(command: string, options?: SandboxRunOptions): Promise<SandboxCommandResult>;
432
+ spawn(command: string, options?: SandboxSpawnOptions): Promise<SandboxCommandHandle>;
433
+ list(): Promise<ProcessInfo[]>;
434
+ connect(_processId: string, _options?: SandboxConnectOptions): Promise<SandboxCommandHandle>;
435
+ sendStdin(_processId: string, _data: string): Promise<void>;
436
+ kill(processId: string): Promise<boolean>;
437
+ /**
438
+ * Accumulate stdout/stderr using for-await pattern (more reliable with Modal streams).
439
+ * Based on vibekit's approach which works correctly with Modal SDK.
440
+ */
441
+ private accumulateStreams;
442
+ }
443
+ declare class ModalFiles implements SandboxFiles {
444
+ private sandbox;
445
+ private user;
446
+ constructor(sandbox: Sandbox, user: string);
447
+ /**
448
+ * Chown a path to the sandbox user so agent CLIs (running via the su
449
+ * wrapper) can access files created by root-level exec. No-op when the
450
+ * sandbox user is root.
451
+ */
452
+ private chownToUser;
453
+ /**
454
+ * Write a payload to a process's stdin in MODAL_STDIN_CHUNK_BYTES slices.
455
+ * Each writeBytes() call becomes one gRPC TaskExecStdinWrite message and
456
+ * Modal rejects messages over 100MiB, so large files must be chunked
457
+ * (multi-hundred-MB payloads are common).
458
+ */
459
+ private writeStdinChunked;
460
+ read(path: string): Promise<string | Uint8Array>;
461
+ write(path: string, content: string | Buffer | ArrayBuffer | Uint8Array): Promise<void>;
462
+ writeBatch(files: Array<{
463
+ path: string;
464
+ data: string | Buffer | ArrayBuffer | Uint8Array;
465
+ }>): Promise<void>;
466
+ makeDir(path: string): Promise<void>;
467
+ exists(path: string): Promise<boolean>;
468
+ list(path: string): Promise<FileInfo[]>;
469
+ remove(path: string): Promise<void>;
470
+ rename(oldPath: string, newPath: string): Promise<void>;
471
+ readStream(path: string): Promise<ReadableStream<Uint8Array>>;
472
+ writeStream(path: string, stream: ReadableStream<Uint8Array>): Promise<void>;
473
+ /**
474
+ * Upload a local file by PATH, chunk by chunk into the same `cat >` sink
475
+ * writeStream() uses — so peak memory is one chunk rather than the whole
476
+ * file, which is what makes a large artifact safe under concurrency.
477
+ */
478
+ writeFromPath(sandboxPath: string, localPath: string): Promise<void>;
479
+ uploadUrl(_path: string, _expiresInSeconds?: number): Promise<string>;
480
+ downloadUrl(_path: string, _expiresInSeconds?: number): Promise<string>;
481
+ watchDir(_path: string, _onEvent: (event: FilesystemEvent) => void | Promise<void>, _options?: WatchOptions): Promise<WatchHandle>;
482
+ private toBuffer;
201
483
  }
202
484
  declare class ModalProvider implements SandboxProvider {
203
485
  readonly providerType: "modal";
@@ -206,13 +488,103 @@ declare class ModalProvider implements SandboxProvider {
206
488
  private readonly appName;
207
489
  private readonly defaultTimeoutMs;
208
490
  private readonly imageName;
491
+ private readonly imageSecretName?;
209
492
  private _app;
493
+ /**
494
+ * Sandbox user configured at create time, reapplied on connect() so the su
495
+ * wrapper keeps targeting the same account. In-memory only: a connect()
496
+ * from a fresh process falls back to the default "user" account — callers
497
+ * reconnecting across processes must recreate the provider and sandbox with
498
+ * the same user, or operations on user-owned files fail loudly.
499
+ */
500
+ private readonly sandboxUsers;
210
501
  constructor(config: ResolvedModalConfig);
211
502
  private getApp;
503
+ /**
504
+ * Build a Modal Image for the resolved tag, routing private registries
505
+ * (AWS ECR, GCP Artifact Registry) through the configured Modal Secret.
506
+ */
507
+ private resolveImage;
212
508
  create(options: SandboxCreateOptions): Promise<SandboxInstance>;
213
509
  connect(sandboxId: string, _timeoutMs?: number): Promise<SandboxInstance>;
214
- list(_options?: SandboxListOptions): Promise<SandboxInfo[]>;
510
+ /**
511
+ * List sandboxes, walking the whole app.
512
+ *
513
+ * This used to stop at a hardcoded default of 100 regardless of fleet size,
514
+ * which silently truncated any app with more — and said nothing about it
515
+ * while the shared SandboxProvider interface promised exhaustive listing.
516
+ * `limit` still bounds the sandboxes RETURNED, so a caller wanting one cheap
517
+ * sample asks for one; without it the answer is the whole app.
518
+ *
519
+ * The O(N)-round-trips warning below is unchanged and is the reason `limit`
520
+ * matters here more than on the other providers.
521
+ */
522
+ list(options?: SandboxListOptions): Promise<SandboxInfo[]>;
523
+ /**
524
+ * The fleet-bookkeeping enumeration: same walk, never throws.
525
+ *
526
+ * Modal has no lifecycle webhooks, so absence from a list is the ONLY
527
+ * termination signal either lane gets — which makes the difference between
528
+ * "the app is empty" and "the enumeration stopped early" the difference
529
+ * between a quiet fleet and one about to be reclaimed.
530
+ *
531
+ * NOTE the divergence from `listSandboxIds` below, which returns an EMPTY set
532
+ * on failure on the grounds that partial results are worse than none for a
533
+ * terminal-state decision. This one returns what it saw alongside
534
+ * `complete: false`. Both are safe because `complete` is what callers branch
535
+ * on, and the shared type documents the choice; do not align one to the other
536
+ * without deciding which rule you want.
537
+ */
538
+ listAll(options?: SandboxListOptions): Promise<SandboxListPage>;
539
+ private walk;
540
+ /**
541
+ * Live sandbox ids for the whole app, in ONE streamed call and O(1) round
542
+ * trips — the fleet-bookkeeping counterpart to `list()`.
543
+ *
544
+ * Exists because `list()` cannot be made cheap without dropping metadata from
545
+ * its contract: it owes callers a populated `SandboxInfo.metadata`, and Modal
546
+ * only serves tags per sandbox. Anything that just needs "which ids are
547
+ * alive" — lifecycle polling, orphan sweeps, reconciliation — uses this and
548
+ * pays one request no matter how large the fleet is.
549
+ *
550
+ * `complete` is the load-bearing field, not a nicety: absence from this list
551
+ * is what callers read as "terminated", so a truncated or errored enumeration
552
+ * MUST NOT be mistaken for an empty fleet. A caller that sees complete=false
553
+ * has to leave rows alone rather than mass-marking live sandboxes dead.
554
+ */
555
+ listSandboxIds(): Promise<{
556
+ ids: Set<string>;
557
+ complete: boolean;
558
+ }>;
559
+ }
560
+ /** The streamed sandbox surface this walk needs — Modal's list() satisfies it. */
561
+ interface ModalSandboxStream {
562
+ sandboxId: string;
563
+ getTags(): Promise<Record<string, string>>;
215
564
  }
565
+ /**
566
+ * Drain Modal's sandbox generator into one answer, with an honest completeness
567
+ * verdict.
568
+ *
569
+ * Separate from the provider because everything worth getting wrong lives here
570
+ * and none of it needs a gRPC connection: the difference between "the caller
571
+ * asked for ten" and "the app ran out", the ceiling that stops an unbounded
572
+ * walk, and the rule that a failure mid-walk yields what it saw marked
573
+ * INCOMPLETE rather than an exception or a short complete list.
574
+ *
575
+ * COST WARNING — this loop is O(N) ROUND TRIPS, not O(1). `list()` itself is one
576
+ * streamed call, but `getTags()` is a separate gRPC request per sandbox
577
+ * (`sandboxTagsGet`), so listing N sandboxes costs N+1 calls. That is fine for a
578
+ * user listing their handful of boxes with metadata, and NOT fine for fleet-wide
579
+ * bookkeeping: anything that only needs to know WHICH ids are alive must use
580
+ * `listSandboxIds`, never this. Please do not "optimize" by reintroducing tag
581
+ * reads into those paths.
582
+ *
583
+ * Exported for its test (`_testCollectSandboxes`).
584
+ */
585
+ declare function collectSandboxes(iterate: () => AsyncIterable<ModalSandboxStream>, wanted?: number): Promise<SandboxListPage & {
586
+ stoppedAtLimit: boolean;
587
+ }>;
216
588
  /**
217
589
  * Create Modal sandbox provider.
218
590
  *
@@ -222,5 +594,13 @@ declare class ModalProvider implements SandboxProvider {
222
594
  * @see https://github.com/evolving-machines-lab/evolve/issues/8
223
595
  */
224
596
  declare function createModalProvider(config?: ModalConfig): SandboxProvider;
597
+ declare const _testWrapCommand: typeof wrapCommand;
598
+ declare const _testMapNetworkPolicy: typeof mapNetworkPolicy;
599
+ declare const _testMapResources: typeof mapResources;
600
+ declare const _testResolveImageRegistry: typeof resolveImageRegistry;
601
+ declare const _testBuildSandboxInfo: typeof buildSandboxInfo;
602
+ declare const _testCollectSandboxes: typeof collectSandboxes;
603
+ declare const _testValidateTimeout: typeof validateTimeout;
604
+ declare const _testMapIdleTimeout: typeof mapIdleTimeout;
225
605
 
226
- export { type FileInfo, type FilesystemEvent, type ModalConfig, ModalProvider, type ProcessInfo, type SandboxCommandHandle, type SandboxCommandResult, type SandboxCommands, type SandboxConnectOptions, type SandboxCreateOptions, type SandboxFiles, type SandboxInfo, type SandboxInstance, type SandboxListOptions, type SandboxProvider, type SandboxRunOptions, type SandboxSpawnOptions, type WatchHandle, type WatchOptions, createModalProvider };
606
+ export { type FileInfo, type FilesystemEvent, MODAL_MAX_LIFETIME_MS, MODAL_MAX_LIST_SANDBOXES, MODAL_STDIN_CHUNK_BYTES, ModalCommands, type ModalConfig, ModalFiles, ModalIdleTimeoutError, ModalNetworkPolicyError, type ModalNetworkPolicyReason, ModalProvider, ModalResourcesError, ModalSandboxLifetimeError, type ModalSandboxStream, type ProcessInfo, type SandboxCommandHandle, type SandboxCommandResult, type SandboxCommands, type SandboxConnectOptions, type SandboxCreateOptions, type SandboxFiles, type SandboxInfo, type SandboxInstance, type SandboxListOptions, type SandboxListPage, type SandboxProvider, type SandboxRunOptions, type SandboxSpawnOptions, type WatchHandle, type WatchOptions, _testBuildSandboxInfo, _testCollectSandboxes, _testMapIdleTimeout, _testMapNetworkPolicy, _testMapResources, _testResolveImageRegistry, _testValidateTimeout, _testWrapCommand, createModalProvider };
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import {ModalClient}from'modal';import {pack}from'tar-stream';var b={"evolve-all":"evolvingmachines/evolve-all"},h=new Set([".xlsx",".xls",".docx",".doc",".pptx",".ppt",".pdf",".zip",".tar",".gz",".7z",".rar",".png",".jpg",".jpeg",".gif",".webp",".ico",".bmp",".mp3",".wav",".ogg",".flac",".aac",".mp4",".avi",".mov",".mkv",".webm",".woff",".woff2",".ttf",".otf",".eot",".exe",".dll",".so",".dylib",".sqlite",".db",".pickle",".pkl",".parquet"]);function y(c){let t=c.substring(c.lastIndexOf(".")).toLowerCase();return h.has(t)}var u=class{constructor(t){this.sandbox=t;}wrapAsUser(t,e,n){let r="";n&&Object.keys(n).length>0&&(r=Object.entries(n).filter(([,o])=>o!=null).map(([o,i])=>`export ${o}='${String(i).replace(/'/g,"'\\''")}'`).join("; ")+"; ");let s=e?`cd '${e.replace(/'/g,"'\\''")}' && ${r}${t}`:`${r}${t}`;return ["su","user","-c",`echo ${Buffer.from(s).toString("base64")} | base64 -d | bash`]}async run(t,e){let n=this.wrapAsUser(t,e?.cwd,e?.envs),r=await this.sandbox.exec(n,{timeoutMs:e?.timeoutMs}),{stdout:s,stderr:a}=await this.accumulateStreams(r,e?.onStdout,e?.onStderr);return {exitCode:await r.wait(),stdout:s,stderr:a}}async spawn(t,e){let n=this.wrapAsUser(t,e?.cwd,e?.envs),r=await this.sandbox.exec(n,{timeoutMs:e?.timeoutMs}),s="",a="",o=this.accumulateStreams(r,e?.onStdout?d=>{e.onStdout(d);}:void 0,e?.onStderr?d=>{e.onStderr(d);}:void 0).then(({stdout:d,stderr:m})=>{s=d,a=m;}).catch(()=>{});return {processId:`modal-${Date.now()}-${Math.random().toString(36).slice(2)}`,wait:async()=>(await o,{exitCode:await r.wait(),stdout:s,stderr:a}),kill:async()=>false}}async list(){let t=await this.sandbox.exec(["ps","-eo","pid,comm,args"],{timeoutMs:1e4});return await t.wait(),(await t.stdout.readText()).trim().split(`
2
- `).slice(1).map(r=>{let s=r.trim().split(/\s+/);return {processId:s[0],cmd:s[1]||"",args:s.slice(2),envs:{}}})}async connect(t,e){throw new Error("Modal does not support connecting to existing processes")}async sendStdin(t,e){throw new Error("Modal does not support sendStdin by process ID")}async kill(t){return await(await this.sandbox.exec(["kill","-9",t],{timeoutMs:1e4})).wait()===0}async accumulateStreams(t,e,n){let r="",s="",a=[];return a.push((async()=>{try{for await(let o of t.stdout){let i=typeof o=="string"?o:new TextDecoder().decode(o);r+=i,e?.(i);}}catch{}})()),a.push((async()=>{try{for await(let o of t.stderr){let i=typeof o=="string"?o:new TextDecoder().decode(o);s+=i,n?.(i);}}catch{}})()),await Promise.all(a),{stdout:r,stderr:s}}},g=class{constructor(t){this.sandbox=t;}async read(t){if(y(t)){let r=await this.sandbox.exec(["cat",t],{timeoutMs:3e5,mode:"binary"}),s=await r.wait();if(s!==0){let a=await r.stderr.readText();throw new Error(`Failed to read file ${t}: ${a||`exit code ${s}`}`)}return await r.stdout.readBytes()}let e=await this.sandbox.exec(["cat",t],{timeoutMs:3e5}),n=await e.wait();if(n!==0){let r=await e.stderr.readText();throw new Error(`Failed to read file ${t}: ${r||`exit code ${n}`}`)}return await e.stdout.readText()}async write(t,e){let n=this.toBuffer(e),r=t.substring(0,t.lastIndexOf("/"));r&&await this.makeDir(r);let s=t.replace(/'/g,"'\\''"),a=await this.sandbox.exec(["bash","-c",`cat > '${s}'`],{mode:"binary"});await a.stdin.writeBytes(new Uint8Array(n)),await a.stdin.getWriter().close(),await a.wait(),await(await this.sandbox.exec(["chown","user:user",t],{timeoutMs:1e4})).wait();}async writeBatch(t){let e=pack(),n=[],r=new Set;for(let i of t){let d=this.toBuffer(i.data),m=i.path.startsWith("/")?i.path.slice(1):i.path;e.entry({name:m},d);let p=i.path.substring(0,i.path.lastIndexOf("/"));p&&r.add(p);}e.finalize();for await(let i of e)n.push(Buffer.from(i));let s=Buffer.concat(n),a=await this.sandbox.exec(["tar","-xf","-","-C","/"],{mode:"binary"});if(await a.stdin.writeBytes(new Uint8Array(s)),await a.stdin.getWriter().close(),await a.wait(),r.size>0){let i=Array.from(r),d=new Set(i.map(m=>m.split("/").slice(0,4).join("/")));for(let m of d)await(await this.sandbox.exec(["chown","-R","user:user",m],{timeoutMs:3e4})).wait();}}async makeDir(t){await(await this.sandbox.exec(["mkdir","-p",t],{timeoutMs:1e4})).wait(),await(await this.sandbox.exec(["chown","-R","user:user",t],{timeoutMs:1e4})).wait();}async exists(t){return await(await this.sandbox.exec(["test","-e",t],{timeoutMs:1e4})).wait()===0}async list(t){let e=t.replace(/'/g,"'\\''"),n=await this.sandbox.exec(["bash","-c",`ls -la '${e}' | tail -n +2`],{timeoutMs:3e4});await n.wait();let r=await n.stdout.readText(),s=[];for(let a of r.trim().split(`
3
- `)){if(!a)continue;let o=a.split(/\s+/);if(o.length<9)continue;let i=o[0],d=o.slice(8).join(" ");d==="."||d===".."||s.push({name:d,path:t.endsWith("/")?`${t}${d}`:`${t}/${d}`,type:i.startsWith("d")?"dir":"file"});}return s}async remove(t){await(await this.sandbox.exec(["rm","-rf",t],{timeoutMs:3e4})).wait();}async rename(t,e){await(await this.sandbox.exec(["mv",t,e],{timeoutMs:3e4})).wait();}async readStream(t){return (await this.sandbox.exec(["cat",t],{timeoutMs:3e5,mode:"binary"})).stdout}async writeStream(t,e){let n=t.substring(0,t.lastIndexOf("/"));n&&await this.makeDir(n);let r=t.replace(/'/g,"'\\''"),s=await this.sandbox.exec(["bash","-c",`cat > '${r}'`],{mode:"binary"}),a=e.getReader();try{for(;;){let{done:i,value:d}=await a.read();if(i)break;await s.stdin.writeBytes(d);}}finally{a.releaseLock();}await s.stdin.getWriter().close(),await s.wait();}async uploadUrl(t,e){throw new Error("Modal does not support pre-signed upload URLs")}async downloadUrl(t,e){throw new Error("Modal does not support pre-signed download URLs")}async watchDir(t,e,n){throw new Error("Modal does not support watchDir")}toBuffer(t){if(typeof t=="string")return Buffer.from(t,"utf-8");if(t instanceof Buffer)return t;if(t instanceof ArrayBuffer||t instanceof Uint8Array)return Buffer.from(t);throw new Error(`Unsupported data type: ${typeof t}`)}},l=class{constructor(t,e){this.sandbox=t;this.commands=new u(t),this.files=new g(t),this.image=e,this.startTime=new Date;}commands;files;image;startTime;get sandboxId(){return this.sandbox.sandboxId}async getHost(t){let n=(await this.sandbox.tunnels())[t];if(!n)throw new Error(`No tunnel found for port ${t}`);return n.url}async isRunning(){try{return await(await this.sandbox.exec(["echo","ping"],{timeoutMs:5e3})).wait(),!0}catch{return false}}async getInfo(){return {sandboxId:this.sandbox.sandboxId,image:this.image,metadata:{},startedAt:this.startTime.toISOString()}}async kill(){try{await this.sandbox.terminate();}catch{await new Promise(t=>setTimeout(t,500)),await this.sandbox.terminate();}}async pause(){throw new Error("Modal does not support pause. Use kill() instead.")}},w=class{providerType="modal";name="Modal";client;appName;defaultTimeoutMs;imageName;_app;constructor(t){!t.endpoint&&process.env.MODAL_SERVER_URL?.startsWith("unix:")?process.env.MODAL_SERVER_URL="https://api.modal.com:443":t.endpoint&&(process.env.MODAL_SERVER_URL=t.endpoint),this.client=new ModalClient({tokenId:t.tokenId,tokenSecret:t.tokenSecret}),this.appName=t.appName??"evolve-sandbox",this.defaultTimeoutMs=t.defaultTimeoutMs??36e5,this.imageName=t.imageName??"evolve-all";}async getApp(){return this._app||(this._app=await this.client.apps.fromName(this.appName,{createIfMissing:true})),this._app}async create(t){let e=await this.getApp(),n=t.timeoutMs??this.defaultTimeoutMs,r=t.image||this.imageName,s=b[r]??r,o=await this.client.images.fromRegistry(s).build(e),i=t.envs?Object.fromEntries(Object.entries(t.envs).filter(([,p])=>p!=null)):void 0,d=i&&Object.keys(i).length>0?i:void 0,m=await this.client.sandboxes.create(e,o,{cpu:4,memoryMiB:4096,timeoutMs:n,workdir:t.workingDirectory,env:d});return t.workingDirectory&&await(await m.exec(["chown","-R","user:user",t.workingDirectory],{timeoutMs:3e4})).wait(),new l(m,s)}async connect(t,e){let n=await this.client.sandboxes.fromId(t);return new l(n,"unknown")}async list(t){let e=[],n=t?.limit??100;try{for await(let r of this.client.sandboxes.list())if(e.push({sandboxId:r.sandboxId,image:"unknown",metadata:{},startedAt:new Date().toISOString()}),e.length>=n)break}catch{}return e}};function k(c={}){let t=c.tokenId??process.env.MODAL_TOKEN_ID,e=c.tokenSecret??process.env.MODAL_TOKEN_SECRET;if(!t||!e)throw new Error("Modal credentials required. Set MODAL_TOKEN_ID and MODAL_TOKEN_SECRET environment variables, or pass tokenId/tokenSecret in config. Get your token at https://modal.com/settings/tokens");return new w({...c,tokenId:t,tokenSecret:e})}
4
- export{w as ModalProvider,k as createModalProvider};
1
+ import {ModalClient}from'modal';import {pack}from'tar-stream';var H={"evolve-all":"evolvingmachines/evolve-all"},E=1440*60*1e3,A=8*1024*1024,k="user",R="evolve.image",_="evolve.startedAt",j=new Set([".xlsx",".xls",".docx",".doc",".pptx",".ppt",".pdf",".zip",".tar",".gz",".7z",".rar",".png",".jpg",".jpeg",".gif",".webp",".ico",".bmp",".mp3",".wav",".ogg",".flac",".aac",".mp4",".avi",".mov",".mkv",".webm",".woff",".woff2",".ttf",".otf",".eot",".exe",".dll",".so",".dylib",".sqlite",".db",".pickle",".pkl",".parquet"]);function q(n){let t=n.substring(n.lastIndexOf(".")).toLowerCase();return j.has(t)}var x=class extends Error{requestedTimeoutMs;constructor(t){let e=(t/36e5).toFixed(1);super(`Modal sandboxes have a hard 24h lifetime cap; requested timeout was ${e}h. For sessions longer than 24h, persist progress with Evolve checkpoints and resume in a fresh sandbox instead of extending the timeout.`),this.name="ModalSandboxLifetimeError",this.requestedTimeoutMs=t;}};function C(n){if(n>E)throw new x(n)}var p=class extends Error{requestedIdleTimeoutMs;constructor(t,e){super(`Modal idleTimeoutMs of ${t}ms is invalid: ${e}`),this.name="ModalIdleTimeoutError",this.requestedIdleTimeoutMs=t;}};function O(n){if(n===void 0)return {};if(!Number.isFinite(n)||n<=0)throw new p(n,"it must be a positive number of milliseconds");if(n>E)throw new p(n,"it exceeds Modal's 24h lifetime cap, so the sandbox would always die of the lifetime first");return {idleTimeoutMs:n}}function b(n,t,e,r){let s="";r&&Object.keys(r).length>0&&(s=Object.entries(r).filter(([,d])=>d!=null).map(([d,a])=>`export ${d}='${String(a).replace(/'/g,"'\\''")}'`).join("; ")+"; ");let i=e?`cd '${e.replace(/'/g,"'\\''")}' && ${s}${n}`:`${s}${n}`;if(t==="root")return ["bash","-c",i];let o=Buffer.from(i).toString("base64");return ["su",t,"-c",`echo ${o} | base64 -d | bash`]}var S=class extends Error{constructor(t){super(t),this.name="ModalResourcesError";}},z=4,G=4096;function N(n){if(n?.disk!==void 0)throw new S(`Modal's JS SDK has no create-time disk-size parameter, so a ${n.disk} GiB disk request cannot be enforced. Drop \`resources.disk\` (containers get Modal's default disk quota) or run on a provider that sizes disk.`);return {cpu:n?.cpu??z,memoryMiB:n?.memory!==void 0?Math.ceil(n.memory*1024):G}}var u=class extends Error{reason;destination;constructor(t,e,r){super(e),this.name="ModalNetworkPolicyError",this.reason=t,this.destination=r;}};function D(n){let t=/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})(?:\/(\d{1,3}))?$/.exec(n);return !(!t||[t[1],t[2],t[3],t[4]].some(e=>Number(e)>255)||t[5]!==void 0&&Number(t[5])>32)}function K(n){return /^\d{1,3}(\.\d{1,3}){3}(\/\d+)?$/.test(n)&&!D(n)}function X(n){return n.startsWith("[")?true:(n.match(/:/g)?.length??0)>=2}function Y(n){return /^[^:]+:\d+$/.test(n)}function T(n){if(!n||n.outbound==="open"){if(n?.allowedDestinations?.length)throw new Error("network.allowedDestinations is only valid when outbound is blocked");return {}}let t=n.allowedDestinations??[];if(t.length===0)return {blockNetwork:true};let e=[],r=[];for(let s of t)if(D(s))e.push(s.includes("/")?s:`${s}/32`);else {if(K(s))throw new u("invalid-ipv4",`"${s}" is not a valid IPv4 address or CIDR (octets must be 0-255, prefix 0-32). Fix the address or list a hostname instead.`,s);if(X(s))e.push(s.includes("/")?s:`${s}/128`);else {if(Y(s))throw new u("port-unsupported",`Modal's network allowlist filters hosts and IPs only and cannot match a port; drop the ":<port>" from "${s}" and list just the host or IP.`,s);r.push(s);}}return {outboundCidrAllowlist:e,outboundDomainAllowlist:r}}function $(n){let t=n.split("/")[0];return /^\d{12}\.dkr\.ecr\.[a-z0-9-]+\.amazonaws\.com$/.test(t)?"aws-ecr":t==="gcr.io"||t.endsWith(".gcr.io")||t.endsWith("-docker.pkg.dev")?"gcp-artifact-registry":"registry"}function I(n,t,e){let{[R]:r,[_]:s,...i}=t;return {sandboxId:n,image:r??e??"",metadata:i,startedAt:s??""}}var M=1e4,y=class{constructor(t,e){this.sandbox=t;this.user=e;}async run(t,e){let r=b(t,this.user,e?.cwd,e?.envs),s=await this.sandbox.exec(r,{timeoutMs:e?.timeoutMs}),{stdout:i,stderr:o}=await this.accumulateStreams(s,e?.onStdout,e?.onStderr);return {exitCode:await s.wait(),stdout:i,stderr:o}}async spawn(t,e){let r=b(t,this.user,e?.cwd,e?.envs),s=await this.sandbox.exec(r,{timeoutMs:e?.timeoutMs}),i="",o="",d=this.accumulateStreams(s,e?.onStdout?c=>{e.onStdout(c);}:void 0,e?.onStderr?c=>{e.onStderr(c);}:void 0).then(({stdout:c,stderr:m})=>{i=c,o=m;}).catch(()=>{});return {processId:`modal-${Date.now()}-${Math.random().toString(36).slice(2)}`,wait:async()=>(await d,{exitCode:await s.wait(),stdout:i,stderr:o}),kill:async()=>false}}async list(){let t=await this.sandbox.exec(["ps","-eo","pid,comm,args"],{timeoutMs:1e4});return await t.wait(),(await t.stdout.readText()).trim().split(`
2
+ `).slice(1).map(s=>{let i=s.trim().split(/\s+/);return {processId:i[0],cmd:i[1]||"",args:i.slice(2),envs:{}}})}async connect(t,e){throw new Error("Modal does not support connecting to existing processes")}async sendStdin(t,e){throw new Error("Modal does not support sendStdin by process ID")}async kill(t){return await(await this.sandbox.exec(["kill","-9",t],{timeoutMs:1e4})).wait()===0}async accumulateStreams(t,e,r){let s="",i="",o=[];return o.push((async()=>{try{for await(let d of t.stdout){let a=typeof d=="string"?d:new TextDecoder().decode(d);s+=a,e?.(a);}}catch{}})()),o.push((async()=>{try{for await(let d of t.stderr){let a=typeof d=="string"?d:new TextDecoder().decode(d);i+=a,r?.(a);}}catch{}})()),await Promise.all(o),{stdout:s,stderr:i}}},v=class{constructor(t,e){this.sandbox=t;this.user=e;}async chownToUser(t,e=false){if(this.user==="root")return;let r=e?["chown","-R",`${this.user}:${this.user}`,t]:["chown",`${this.user}:${this.user}`,t];await(await this.sandbox.exec(r,{timeoutMs:e?3e4:1e4})).wait();}async writeStdinChunked(t,e){for(let r=0;r<e.length;r+=A)await t.writeBytes(e.subarray(r,r+A));}async read(t){if(q(t)){let s=await this.sandbox.exec(["cat",t],{timeoutMs:3e5,mode:"binary"}),i=await s.wait();if(i!==0){let o=await s.stderr.readText();throw new Error(`Failed to read file ${t}: ${o||`exit code ${i}`}`)}return await s.stdout.readBytes()}let e=await this.sandbox.exec(["cat",t],{timeoutMs:3e5}),r=await e.wait();if(r!==0){let s=await e.stderr.readText();throw new Error(`Failed to read file ${t}: ${s||`exit code ${r}`}`)}return await e.stdout.readText()}async write(t,e){let r=this.toBuffer(e),s=t.substring(0,t.lastIndexOf("/"));s&&await this.makeDir(s);let i=t.replace(/'/g,"'\\''"),o=await this.sandbox.exec(["bash","-c",`cat > '${i}'`],{mode:"binary"});await this.writeStdinChunked(o.stdin,new Uint8Array(r)),await o.stdin.getWriter().close(),await o.wait(),await this.chownToUser(t);}async writeBatch(t){let e=pack(),r=[],s=new Set;for(let a of t){let c=this.toBuffer(a.data),m=a.path.startsWith("/")?a.path.slice(1):a.path;e.entry({name:m},c);let l=a.path.substring(0,a.path.lastIndexOf("/"));l&&s.add(l);}e.finalize();for await(let a of e)r.push(Buffer.from(a));let i=Buffer.concat(r),o=await this.sandbox.exec(["tar","-xf","-","-C","/"],{mode:"binary"});if(await this.writeStdinChunked(o.stdin,new Uint8Array(i)),await o.stdin.getWriter().close(),await o.wait(),s.size>0){let a=Array.from(s),c=new Set(a.map(m=>m.split("/").slice(0,4).join("/")));for(let m of c)await this.chownToUser(m,true);}}async makeDir(t){await(await this.sandbox.exec(["mkdir","-p",t],{timeoutMs:1e4})).wait(),await this.chownToUser(t,true);}async exists(t){return await(await this.sandbox.exec(["test","-e",t],{timeoutMs:1e4})).wait()===0}async list(t){let e=t.replace(/'/g,"'\\''"),r=await this.sandbox.exec(["bash","-c",`ls -la '${e}' | tail -n +2`],{timeoutMs:3e4});await r.wait();let s=await r.stdout.readText(),i=[];for(let o of s.trim().split(`
3
+ `)){if(!o)continue;let d=o.split(/\s+/);if(d.length<9)continue;let a=d[0],c=d.slice(8).join(" ");c==="."||c===".."||i.push({name:c,path:t.endsWith("/")?`${t}${c}`:`${t}/${c}`,type:a.startsWith("d")?"dir":"file"});}return i}async remove(t){await(await this.sandbox.exec(["rm","-rf",t],{timeoutMs:3e4})).wait();}async rename(t,e){await(await this.sandbox.exec(["mv",t,e],{timeoutMs:3e4})).wait();}async readStream(t){return (await this.sandbox.exec(["cat",t],{timeoutMs:3e5,mode:"binary"})).stdout}async writeStream(t,e){let r=t.substring(0,t.lastIndexOf("/"));r&&await this.makeDir(r);let s=t.replace(/'/g,"'\\''"),i=await this.sandbox.exec(["bash","-c",`cat > '${s}'`],{mode:"binary"}),o=e.getReader();try{for(;;){let{done:a,value:c}=await o.read();if(a)break;await this.writeStdinChunked(i.stdin,c);}}finally{o.releaseLock();}await i.stdin.getWriter().close(),await i.wait(),await this.chownToUser(t);}async writeFromPath(t,e){let{createReadStream:r}=await import('fs'),{Readable:s}=await import('stream'),i=s.toWeb(r(e));await this.writeStream(t,i);}async uploadUrl(t,e){throw new Error("Modal does not support pre-signed upload URLs")}async downloadUrl(t,e){throw new Error("Modal does not support pre-signed download URLs")}async watchDir(t,e,r){throw new Error("Modal does not support watchDir")}toBuffer(t){if(typeof t=="string")return Buffer.from(t,"utf-8");if(t instanceof Buffer)return t;if(t instanceof ArrayBuffer||t instanceof Uint8Array)return Buffer.from(t);throw new Error(`Unsupported data type: ${typeof t}`)}},g=class{constructor(t,e,r){this.sandbox=t;this.commands=new y(t,r),this.files=new v(t,r),this.image=e;}commands;files;image;get sandboxId(){return this.sandbox.sandboxId}async getHost(t){let r=(await this.sandbox.tunnels())[t];if(!r)throw new Error(`No tunnel found for port ${t}`);return r.url}async isRunning(){try{return await this.sandbox.poll()===null}catch{return false}}async getInfo(){let t=await this.sandbox.getTags();return I(this.sandbox.sandboxId,t,this.image)}async kill(){try{await this.sandbox.terminate();}catch{await new Promise(t=>setTimeout(t,500)),await this.sandbox.terminate();}}async pause(){throw new Error("Modal does not support pause/resume. Persist progress with Evolve checkpoints and resume in a fresh sandbox, or use kill() to terminate.")}},P=class{providerType="modal";name="Modal";client;appName;defaultTimeoutMs;imageName;imageSecretName;_app;sandboxUsers=new Map;constructor(t){!t.endpoint&&process.env.MODAL_SERVER_URL?.startsWith("unix:")?process.env.MODAL_SERVER_URL="https://api.modal.com:443":t.endpoint&&(process.env.MODAL_SERVER_URL=t.endpoint),this.client=new ModalClient({tokenId:t.tokenId,tokenSecret:t.tokenSecret}),this.appName=t.appName??"evolve-sandbox",this.defaultTimeoutMs=t.defaultTimeoutMs??36e5,this.imageName=t.imageName??"evolve-all",this.imageSecretName=t.imageSecretName;}async getApp(){return this._app||(this._app=await this.client.apps.fromName(this.appName,{createIfMissing:true})),this._app}async resolveImage(t){let e=$(t);if(e==="registry"){let s=this.imageSecretName?await this.client.secrets.fromName(this.imageSecretName):void 0;return this.client.images.fromRegistry(t,s)}if(!this.imageSecretName)throw new Error(`Private registry image "${t}" requires config.imageSecretName \u2014 the name of a Modal Secret holding registry credentials (AWS ECR: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION with read-only ECR IAM). Create one at https://modal.com/secrets`);let r=await this.client.secrets.fromName(this.imageSecretName);return e==="aws-ecr"?this.client.images.fromAwsEcr(t,r):this.client.images.fromGcpArtifactRegistry(t,r)}async create(t){let e=t.timeoutMs??this.defaultTimeoutMs;C(e);let r=O(t.idleTimeoutMs),s=T(t.network),i=N(t.resources),o=t.user??k,d=await this.getApp(),a=t.image||this.imageName,c=H[a]??a,l=await(await this.resolveImage(c)).build(d),f=t.envs?Object.fromEntries(Object.entries(t.envs).filter(([,h])=>h!=null)):void 0,B=f&&Object.keys(f).length>0?f:void 0,U={...t.metadata,[R]:c,[_]:new Date().toISOString()},w=await this.client.sandboxes.create(d,l,{cpu:i.cpu,memoryMiB:i.memoryMiB,timeoutMs:e,...r,workdir:t.workingDirectory,env:B,tags:U,...s});return t.workingDirectory&&o!=="root"&&await(await w.exec(["chown","-R",`${o}:${o}`,t.workingDirectory],{timeoutMs:3e4})).wait(),this.sandboxUsers.set(w.sandboxId,o),new g(w,c,o)}async connect(t,e){let r=await this.client.sandboxes.fromId(t),s=this.sandboxUsers.get(t)??k;return new g(r,void 0,s)}async list(t){let e=await this.walk(t);if(e.error&&!e.stoppedAtLimit)throw new Error(e.error);return e.sandboxes}async listAll(t){let{stoppedAtLimit:e,...r}=await this.walk(t);return r}async walk(t){if(t?.state&&!t.state.includes("running"))return {sandboxes:[],complete:true,pagesFetched:0,stoppedAtLimit:false};let e;try{e=await this.getApp();}catch(r){return {sandboxes:[],complete:false,pagesFetched:0,stoppedAtLimit:false,error:`sandbox list failed: ${r instanceof Error?r.message:String(r)}`}}return L(()=>this.client.sandboxes.list({appId:e.appId,tags:t?.metadata}),t?.limit)}async listSandboxIds(){let t=new Set;try{let e=await this.getApp();for await(let r of this.client.sandboxes.list({appId:e.appId}))t.add(r.sandboxId);return {ids:t,complete:!0}}catch{return {ids:new Set,complete:false}}}};async function L(n,t){let e=[];try{for await(let r of n()){if(t!==void 0&&e.length>=t)return {sandboxes:e,complete:!1,pagesFetched:e.length,stoppedAtLimit:!0,error:`stopped at the requested limit of ${t} with more sandboxes available`};if(e.length>=M)return {sandboxes:e,complete:!1,pagesFetched:e.length,stoppedAtLimit:!1,error:`sandbox list exceeded ${M} sandboxes`};let s=await r.getTags();e.push(I(r.sandboxId,s));}}catch(r){return {sandboxes:e,complete:false,pagesFetched:e.length,stoppedAtLimit:false,error:`sandbox list failed: ${r instanceof Error?r.message:String(r)}`}}return {sandboxes:e,complete:true,pagesFetched:e.length,stoppedAtLimit:false}}function rt(n={}){let t=n.tokenId??process.env.MODAL_TOKEN_ID,e=n.tokenSecret??process.env.MODAL_TOKEN_SECRET;if(!t||!e)throw new Error("Modal credentials required. Set MODAL_TOKEN_ID and MODAL_TOKEN_SECRET environment variables, or pass tokenId/tokenSecret in config. Get your token at https://modal.com/settings/tokens");return new P({...n,tokenId:t,tokenSecret:e})}var st=b,nt=T,it=N,ot=$,at=I,dt=L,ct=C,mt=O;
4
+ export{E as MODAL_MAX_LIFETIME_MS,M as MODAL_MAX_LIST_SANDBOXES,A as MODAL_STDIN_CHUNK_BYTES,y as ModalCommands,v as ModalFiles,p as ModalIdleTimeoutError,u as ModalNetworkPolicyError,P as ModalProvider,S as ModalResourcesError,x as ModalSandboxLifetimeError,at as _testBuildSandboxInfo,dt as _testCollectSandboxes,mt as _testMapIdleTimeout,nt as _testMapNetworkPolicy,it as _testMapResources,ot as _testResolveImageRegistry,ct as _testValidateTimeout,st as _testWrapCommand,rt as createModalProvider};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@evolvingmachines/modal",
3
- "version": "0.0.51",
3
+ "version": "0.0.52-project-sable.20260729.8632c49",
4
4
  "keywords": [
5
5
  "ai",
6
6
  "agents",
@@ -33,10 +33,11 @@
33
33
  "scripts": {
34
34
  "build": "tsup --minify",
35
35
  "dev": "tsup --watch",
36
- "type-check": "tsc --noEmit"
36
+ "type-check": "tsc --noEmit",
37
+ "test:unit": "tsx tests/unit/modal-provider.test.ts && tsx tests/unit/modal-list-pagination.test.ts"
37
38
  },
38
39
  "dependencies": {
39
- "modal": "^0.6.0",
40
+ "modal": "^0.9.0",
40
41
  "tar-stream": "^3.1.7"
41
42
  },
42
43
  "devDependencies": {