cloudgpu-mcp 0.15.0 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -147,6 +147,10 @@ The same guidance is available as two [Agent Skills](https://agentskills.io):
147
147
  | `run_job` | Starts a GPU, runs one command and destroys the GPU, all on CloudGPU's servers; safe to retry with the same `idempotency_key` |
148
148
  | `get_job` / `list_jobs` | Job status, exit code, cost, the last ~4 KB of output and the job's history |
149
149
  | `cancel_job` | Stops a job and destroys its GPU |
150
+ | `get_agent_readiness` / `assess_task_fit` | Anonymous capability, billing, task suitability and evidence checks; no GPU is created |
151
+ | `request_top_up` / `wait_for_funds` | Human-only payment link and bounded polling for actual credit; no automatic charge or Job |
152
+ | `quote_storage_budget` / `confirm_storage_budget` | Quote and explicitly approve an earmarked storage transfer; backend rollout and owner permissions apply |
153
+ | `resume_artifact_upload` | Resume the same owned multipart reservation with the original local file and checksum |
150
154
  | `connect_account` / `finish_connect` | Gets a key without copying one: the user approves this assistant in the browser with a spending limit and a number of GPUs at once |
151
155
 
152
156
  The server also provides a `train_on_cloudgpu` prompt.
@@ -389,3 +393,15 @@ Preparation makes no API request or reservation. Review the returned command, ob
389
393
  For an existing rental, the reviewed command can instead be passed to `run_command` in the approved working directory, within its original limits. Apply a Workspace separately when needed. The draft never stops or extends an existing rental. Whole-hour sources remain usable through the existing explicitly approved rental path; this release does not remove their current automatic-Job compatibility gate.
390
394
 
391
395
  With `persist_summary=true`, the Job draft explicitly collects only `pipeline-summary.json` through the existing output collector, using current storage quota/retention and `accept_storage_charges=false`. The summary contains step IDs, exit codes and state, not raw logs, commands or arbitrary task results. Result-file persistence must be configured and approved separately. The summary must not already exist; missing output collection or a conflicting file aborts before commands run.
396
+
397
+ ### Candidate 0.16.0: funded connection and storage growth
398
+
399
+ This branch is a candidate, not a claim that npm, Registry or production have been updated. The current stdio inventory is 54 tools; remote MCP intentionally exposes only operations safe without local filesystem/SSH access. Always check live backend capabilities.
400
+
401
+ `connect_account` returns a direct Google link and email alternative. Pending connection state is private, origin-bound and survives restart; the server candidate extends approval time to 30 minutes. Browser payment does not approve compute. `request_top_up` creates only a short-lived owner link; `wait_for_funds` requires a real credit signal and sufficient live balance. Neither creates a Job.
402
+
403
+ Dedicated storage budget is a separate, explicitly confirmed wallet reservation, consumed only by actual UTC daily peak usage. Unused surplus can return after existing files, uploads and pending daily charges remain covered. It is not revenue or an automatic refill. Capacity can grow with budget, subject to published technical limits, and retention remains 30 days. Budget-limited Agent keys cannot reserve funds or consent to paid storage. Disabling new self-service transfers must not disable accounting for existing reserves.
404
+
405
+ Multipart resume keeps the same reservation and original file hash. GPU restore renewal is file-scoped, temporary and feature-gated; no general account key is installed on a GPU. Local fixtures do not certify TB-sized transfers. Backend automatic public-model preparation currently has a 600-second deadline: a 9.8GB Wan video case exceeded it on the measured link. Prepared images or explicitly reviewed bounded download commands need their own acceptance; do not promise that every size below the model byte limit loads in time.
406
+
407
+ The Python SDK candidate and optional framework adapters are under [`python/`](python/README.md). PyPI publication and actual third-party framework acceptance remain separate from package construction.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cloudgpu-mcp",
3
- "version": "0.15.0",
3
+ "version": "0.16.0",
4
4
  "mcpName": "io.github.cloudgpu-app/cloudgpu-mcp",
5
5
  "description": "MCP server and command-line tool for cloudgpu.app: let Claude Code, Cursor, Cline, opencode, goose and other MCP clients (or you, from a terminal) rent a GPU with explicit billing quotes, run a job, fetch the results and shut it down, with spending limits the platform enforces.",
6
6
  "type": "module",
@@ -0,0 +1,6 @@
1
+ import { z } from "zod";
2
+ /** Anonymous tools intentionally omit Authorization even when this client has a key. */
3
+ export function registerAgentReadinessTools(server,{api,wrap,ok}){
4
+ server.registerTool("get_agent_readiness",{title:"Read-only Agent self-check",description:"No login or spending: capabilities, source-specific billing, task prerequisites, limitations, recent aggregate Job evidence and known issues. Unknown data is not a healthy or verified result.",inputSchema:{},annotations:{readOnlyHint:true,openWorldHint:true}},wrap(async()=>ok(await api.request("GET","/v1/public/agent-self-check",undefined,{noAuth:true}))));
5
+ server.registerTool("assess_task_fit",{title:"Assess whether a task fits CloudGPU",description:"Read-only task suitability before quoting a GPU. Returns blockers, unverified requirements and evidence status; does not benchmark, approve spending or guarantee availability.",inputSchema:{task_type:z.enum(["gpu_environment_check","python_batch","comfyui_video","causal_lm_lora"]),needs_persistent_outputs:z.boolean().default(true),requires_kernel_control:z.boolean().default(false),requires_nested_virtualization:z.boolean().default(false),requires_resale:z.boolean().default(false)},annotations:{readOnlyHint:true,openWorldHint:true}},wrap(async input=>ok(await api.request("POST","/v1/public/task-fit",input,{noAuth:true}))));
6
+ }
@@ -6,7 +6,7 @@ const relative=z.string().min(1).max(240).regex(/^[A-Za-z0-9_][A-Za-z0-9_./-]*$/
6
6
  const reference=z.object({name:z.string().regex(/^[A-Za-z][A-Za-z0-9_-]{0,47}$/),repo_id:z.string().regex(/^[A-Za-z0-9][A-Za-z0-9_.-]{0,95}\/[A-Za-z0-9][A-Za-z0-9_.-]{0,95}$/),revision:z.string().regex(/^[a-f0-9]{40}$/),files:z.array(z.object({path:relative,size_bytes:z.number().int().positive(),sha256:z.string().regex(/^[a-f0-9]{64}$/)}).strict()).min(1).max(32)}).strict();
7
7
  export const APPLICATION_TASKS=[
8
8
  {id:'comfyui-workflow',version:1,title:'Run a reviewed ComfyUI API workflow',requires_workspace:true,requirements:['Existing ComfyUI application on GPU loopback','Reviewed API-format workflow and prepared models','Explicit output persistence'],result_files:['image-0.png','task-summary.json'],description:'Submit one workflow, poll its history and collect explicit PNG files. Uses an existing compatible image; does not install custom nodes or claim all workflows are supported.',validation:'Local HTTP protocol fixtures; real GPU generation not yet accepted'},
9
- {id:'causal-lm-lora',version:1,title:'Train a small causal-model LoRA adapter',requires_workspace:true,requirements:['CUDA, torch, transformers, peft and safetensors already installed','Pinned public causal model reference and JSONL text dataset','Explicit output persistence'],result_files:['adapter_config.json','adapter_model.safetensors','task-summary.json'],description:'Bounded single-GPU causal-language-model LoRA, not an image/SDXL trainer. Loads only local pinned model files, disables remote model code and does not install dependencies.',validation:'Orchestration fixtures only; real GPU training and model quality not yet accepted'},
9
+ {id:'causal-lm-lora',version:2,title:'Train a small causal-model LoRA adapter',requires_workspace:true,requirements:['CUDA, torch, transformers, peft and safetensors already installed','Pinned public causal model reference and JSONL text dataset','Explicit output persistence'],result_files:['adapter_config.json','adapter_model.safetensors','task-summary.json'],description:'Bounded single-GPU causal-language-model LoRA, not an image/SDXL trainer. Loads only local pinned model files, disables remote model code and does not install dependencies.',validation:'Real internal CUDA smoke: SmolLM2-135M, RTX 3090, 8 synthetic-text steps, trained adapter and R2 SHA-256 verified; 190 seconds total, USD 0.0097 for the successful run. Four attempts total, including failures; not model-quality or image-LoRA validation. See /agent-evidence.json'},
10
10
  ];
11
11
  export const applicationFields={workflow_path:relative.optional(),comfy_port:z.number().int().min(1024).max(65535).optional(),timeout_seconds:z.number().int().min(30).max(7200).optional(),image_count:z.number().int().min(1).max(4).optional(),model_reference:reference.optional(),dataset_path:relative.optional(),target_modules:z.array(z.string().regex(/^[A-Za-z_][A-Za-z0-9_]{0,63}$/)).min(1).max(8).optional(),steps:z.number().int().min(1).max(200).optional(),rank:z.number().int().min(1).max(16).optional(),sequence_length:z.number().int().min(32).max(2048).optional(),learning_rate:z.number().positive().max(0.01).optional()};
12
12
  export function prepareApplicationRecipe(p){
@@ -11,7 +11,10 @@ export function configDir(env = process.env) {
11
11
 
12
12
  export function loadStoredKey(env = process.env) {
13
13
  try {
14
- const j = JSON.parse(fs.readFileSync(path.join(configDir(env), "credentials.json"), "utf8"));
14
+ const file=path.join(configDir(env),"credentials.json"),stat=fs.lstatSync(file);
15
+ if(!stat.isFile()||stat.isSymbolicLink()||(stat.mode&0o077)||(process.getuid&&stat.uid!==process.getuid()))return null;
16
+ const j = JSON.parse(fs.readFileSync(file,"utf8"));
17
+ if(j.base_url!==(env.CLOUDGPU_BASE_URL||"https://cloudgpu.app"))return null;
15
18
  if (j && typeof j.api_key === "string" && j.api_key.trim()) {
16
19
  if (j.expires_at && Number(j.expires_at) * 1000 < Date.now()) return null;
17
20
  return j;
@@ -23,12 +26,29 @@ export function loadStoredKey(env = process.env) {
23
26
  }
24
27
 
25
28
  export function saveStoredKey(cred, env = process.env) {
29
+ cred={...cred,base_url:cred.base_url||(env.CLOUDGPU_BASE_URL||"https://cloudgpu.app")};
26
30
  const dir = configDir(env);
27
31
  fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
32
+ const info=fs.lstatSync(dir);
33
+ if(!info.isDirectory()||info.isSymbolicLink()||(process.getuid&&info.uid!==process.getuid())||(info.mode&0o077))throw new Error('Credential directory must be private and owned by this user.');
28
34
  const file = path.join(dir, "credentials.json");
29
- const tmp = file + ".tmp";
30
- fs.writeFileSync(tmp, JSON.stringify(cred, null, 2), { mode: 0o600 });
31
- fs.renameSync(tmp, file);
32
- fs.chmodSync(file, 0o600);
35
+ const tmp = file + "."+process.pid+"."+Date.now()+".tmp";
36
+ try{fs.writeFileSync(tmp,JSON.stringify(cred,null,2),{flag:'wx',mode:0o600});fs.renameSync(tmp,file);fs.chmodSync(file,0o600);}finally{try{fs.unlinkSync(tmp);}catch{}}
33
37
  return file;
34
38
  }
39
+
40
+ export function loadPendingConnect(baseUrl,env=process.env){
41
+ try{
42
+ const file=path.join(configDir(env),'pending-connect.json'),stat=fs.lstatSync(file);
43
+ if(!stat.isFile()||stat.isSymbolicLink()||(stat.mode&0o077)||(process.getuid&&stat.uid!==process.getuid()))return null;
44
+ const value=JSON.parse(fs.readFileSync(file,'utf8'));
45
+ return value.base_url===baseUrl&&/^[a-f0-9]{64}$/.test(value.device_code)&&Number.isFinite(value.until)&&value.until>Date.now()&&value.until<=Date.now()+1801000&&Number.isFinite(value.interval)&&value.interval>=5?value:null;
46
+ }catch{return null;}
47
+ }
48
+ export function savePendingConnect(value,env=process.env){
49
+ const dir=configDir(env);fs.mkdirSync(dir,{recursive:true,mode:0o700});
50
+ if(fs.lstatSync(dir).isSymbolicLink())throw new Error('Credential directory must not be a symlink.');
51
+ const file=path.join(dir,'pending-connect.json'),tmp=file+'.'+process.pid+'.'+Date.now()+'.tmp';
52
+ try{fs.writeFileSync(tmp,JSON.stringify(value),{flag:'wx',mode:0o600});fs.renameSync(tmp,file);fs.chmodSync(file,0o600);}finally{try{fs.unlinkSync(tmp);}catch{}}
53
+ }
54
+ export function clearPendingConnect(env=process.env){try{fs.unlinkSync(path.join(configDir(env),'pending-connect.json'));}catch{}}
@@ -0,0 +1,11 @@
1
+ import {z} from 'zod';
2
+ export function registerFundingTools(server,{api,wrap,ok,readOnly,sleep=ms=>new Promise(r=>setTimeout(r,ms))}){
3
+ if(!readOnly)server.registerTool('request_top_up',{title:'Request an owner payment link',description:'Creates a short-lived link only. No checkout, card charge or transfer is made. Show the link to the human owner; never complete payment for them. Payment completion does not approve a GPU quote.',inputSchema:{amount_usd:z.number().min(1).max(10000)},annotations:{readOnlyHint:false,destructiveHint:false,openWorldHint:true}},wrap(async({amount_usd})=>ok(await api.request('POST','/v1/billing/top-up-link',{amount_usd}))));
4
+ server.registerTool('wait_for_funds',{title:'Wait for credited account funds',description:'Read-only polling of actual account balance and real-top-up signal for up to 60 seconds. No payment, job submission or authorization. If still pending, return to the owner; do not blindly repeat rentals.',inputSchema:{min_balance_usd:z.number().positive().max(100000),timeout:z.number().int().min(0).max(60).default(30)},annotations:{readOnlyHint:true,openWorldHint:true}},wrap(async({min_balance_usd,timeout=30})=>{
5
+ const deadline=Date.now()+Math.min(60,Math.max(0,timeout))*1000;
6
+ for(;;){const account=await api.account();const amount=Number(account.balance_usd);const funded=account.funded===true&&Number.isFinite(amount)&&amount>=min_balance_usd;
7
+ if(funded||Date.now()>=deadline)return ok({ready:funded,balance_usd:account.balance_usd,last_top_up_at:account.last_top_up_at??null,top_up_url:account.top_up_url??null,next:funded?'Funds are credited. Obtain or recheck the exact quote and user approval before spending.':'Waiting for credited funds; ask the owner to finish payment. Do not submit a paid task.'});
8
+ await sleep(Math.min(5000,deadline-Date.now()));
9
+ }
10
+ }));
11
+ }
package/src/index.js CHANGED
@@ -1,4 +1,6 @@
1
1
  #!/usr/bin/env node
2
+ import {registerFundingTools} from "./funding-tools.js";
3
+ import {registerAgentReadinessTools} from "./agent-readiness.js";
2
4
  import {rentalBilling} from "./rental-billing.js";
3
5
  import {registerComputePlanningTools} from './compute-planning.js';
4
6
  import {registerTaskPipelineTools} from './task-pipelines.js';
@@ -23,7 +25,7 @@ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"
23
25
  import { z } from "zod";
24
26
  import { CloudGpuApi, ApiError } from "./api.js";
25
27
  import { SshPool, exec, upload, download, writeFile } from "./ssh.js";
26
- import { loadStoredKey, saveStoredKey } from "./credentials.js";
28
+ import { loadStoredKey, saveStoredKey, loadPendingConnect, savePendingConnect, clearPendingConnect } from "./credentials.js";
27
29
  import crypto from "node:crypto";
28
30
  import { registerStorageTools } from "./storage.js";
29
31
  import { registerJobResultTools } from "./job-results.js";
@@ -38,7 +40,7 @@ import {
38
40
  shq,
39
41
  } from "./guards.js";
40
42
 
41
- const VERSION = "0.15.0";
43
+ const VERSION = "0.16.0";
42
44
  const HERE = path.dirname(fileURLToPath(import.meta.url));
43
45
  const RULES = fs.readFileSync(path.join(HERE, "..", "rules", "cloudgpu.md"), "utf8");
44
46
 
@@ -75,6 +77,8 @@ No API key yet? connect_account gives the user a link to approve this assistant
75
77
  The same API key also works for the OpenAI-compatible model API at ${cfg.baseUrl}/v1 (see list_models).`;
76
78
 
77
79
  const server = new McpServer({ name: "cloudgpu", version: VERSION }, { instructions: INSTRUCTIONS });
80
+ registerFundingTools(server,{api,wrap,ok,readOnly:cfg.readOnly});
81
+ registerAgentReadinessTools(server,{api,wrap,ok});
78
82
  registerTaskTemplateTools(server, {wrap, ok});
79
83
  registerComputePlanningTools(server,{api,wrap,ok});
80
84
  registerTaskPipelineTools(server,{cfg,wrap,ok});
@@ -101,7 +105,7 @@ function wrap(fn) {
101
105
  if (err instanceof ApiError) {
102
106
  let hint = "";
103
107
  if (err.status === 401) hint = " Check CLOUDGPU_API_KEY (create one at https://cloudgpu.app/api).";
104
- if (err.status === 402) hint = " Top up at https://cloudgpu.app/billing.";
108
+ if (err.status === 402) hint = " Call request_top_up to give the owner a payment link, then wait_for_funds. Never submit or repeat a paid task before approval.";
105
109
  return fail(`CloudGPU API error ${err.status} ${err.code}: ${err.message}${hint}`);
106
110
  }
107
111
  return fail(`Error: ${err && err.message ? err.message : String(err)}`);
@@ -245,7 +249,7 @@ server.registerTool(
245
249
  if (offer.available === "low") warnings.push("Only a few of these are free; renting may fail.");
246
250
  if (balance != null && balance < estimate * 1.1) {
247
251
  warnings.push(
248
- `Balance $${balance} does not cover the estimate plus a 10% buffer. When the balance runs out the instance is destroyed and its disk erased. Top up at https://cloudgpu.app/billing.`,
252
+ `Balance $${balance} does not cover the estimate plus a 10% buffer. When the balance runs out the instance is destroyed and its disk erased. Call request_top_up and wait_for_funds; obtain approval before continuing.`,
249
253
  );
250
254
  }
251
255
  const quote_id = quotes.put({ gpu_model: offer.gpu_model, gpu_count, template, hourly, hours, estimate, billingQuoteId:billing.billingQuoteId, billingExpiresAt:billing.expiresAt, minimumChargeUsd:Number(billing.minimumChargeUsd), billingIncrementSeconds:increment });
@@ -293,7 +297,7 @@ server.registerTool(
293
297
 
294
298
  // ── connect (get a key with a budget, approved in the browser) ──────────────
295
299
 
296
- let pendingConnect = null;
300
+ let pendingConnect = loadPendingConnect(cfg.baseUrl);
297
301
 
298
302
  server.registerTool(
299
303
  "connect_account",
@@ -312,8 +316,11 @@ server.registerTool(
312
316
  },
313
317
  wrap(async ({ agent_name, spend_limit_usd, max_concurrent = 1, expires_in_days = 30 }) => {
314
318
  const r = await api.connectStart({ agent_name, spend_limit_usd, max_concurrent, expires_in_days });
315
- pendingConnect = { device_code: r.device_code, until: Date.now() + Number(r.expires_in || 600) * 1000, interval: Number(r.interval || 5) };
319
+ pendingConnect = { device_code: r.device_code, until: Date.now() + Number(r.expires_in || 1800) * 1000, interval: Number(r.interval || 5) };
320
+ savePendingConnect({...pendingConnect,base_url:cfg.baseUrl});
316
321
  return ok({
322
+ google_link: r.google_link || r.verification_uri_complete,
323
+ email_link: r.verification_uri_complete+"&provider=email",
317
324
  link: r.verification_uri_complete,
318
325
  code: r.user_code,
319
326
  expires_in_seconds: r.expires_in,
@@ -349,7 +356,7 @@ server.registerTool(
349
356
  });
350
357
  cfg.apiKey = cred.api_key;
351
358
  api.apiKey = cred.api_key;
352
- pendingConnect = null;
359
+ pendingConnect = null;clearPendingConnect();
353
360
  return ok({
354
361
  connected: true,
355
362
  agent_name: cred.agent_name,
@@ -367,7 +374,7 @@ server.registerTool(
367
374
  if (err instanceof ApiError && err.code === "authorization_pending") {
368
375
  return fail("Not approved yet. Ask the user to open the link and approve, then call finish_connect again.");
369
376
  }
370
- if (err instanceof ApiError && (err.code === "access_denied" || err.code === "expired_token")) pendingConnect = null;
377
+ if (err instanceof ApiError && (err.code === "access_denied" || err.code === "expired_token")){pendingConnect = null;clearPendingConnect();}
371
378
  throw err;
372
379
  }
373
380
  }
@@ -482,7 +489,7 @@ if (!cfg.readOnly) {
482
489
  const needed = round2(Math.min(max_spend_usd, quote.estimate) * 1.1);
483
490
  if (balance < needed) {
484
491
  return fail(
485
- `Not rented: balance $${balance} is below $${needed} (the estimate or budget plus 10%). If the balance runs out the instance is destroyed and its disk erased. Top up at https://cloudgpu.app/billing.`,
492
+ `Not rented: balance $${balance} is below $${needed} (the estimate or budget plus 10%). If the balance runs out the instance is destroyed and its disk erased. Call request_top_up and wait_for_funds; obtain approval before continuing.`,
486
493
  );
487
494
  }
488
495
  const body = {
@@ -23,10 +23,10 @@ export async function resolveModelReference({repo_id, revision = "main", name, f
23
23
  return {path,size_bytes:size,...(sha?{sha256:sha}:{})};
24
24
  });
25
25
  if(selected.reduce((n,f)=>n+f.size_bytes,0)>100_000_000_000)throw new Error("Model exceeds the first-version 100 GB job limit");
26
- // Git-backed config files have no LFS SHA. Hash bounded public JSON metadata
26
+ // Git-backed tokenizer/config files have no LFS SHA. Bound each to 4 MiB and all to 8 MiB. Hash bounded public JSON metadata
27
27
  // on the origin so the GPU can verify it even when using its existing mirror.
28
- const configs=selected.filter(f=>!f.sha256&&f.path.endsWith('.json')&&f.size_bytes<=65536);
29
- if(configs.length>8)throw new Error('Too many unhashed JSON metadata files; supply an independently verified manifest');
28
+ const configs=selected.filter(f=>!f.sha256&&/\.(json|txt|model|tiktoken)$/.test(f.path)&&f.size_bytes<=4*1024*1024);
29
+ if(configs.length>8||configs.reduce((total,f)=>total+f.size_bytes,0)>8*1024*1024)throw new Error('Too many unhashed JSON metadata files; supply an independently verified manifest');
30
30
  const deadline=Date.now()+15000;
31
31
  for(const file of configs){
32
32
  const remaining=deadline-Date.now();
@@ -1,5 +1,15 @@
1
1
  import importlib.util,json,math,os,pathlib,stat
2
2
 
3
+ def training_dtype(torch):
4
+ # Device capability alone does not guarantee the installed torch CUDA kernels.
5
+ if not torch.cuda.is_bf16_supported(): return torch.float32
6
+ try:
7
+ torch.triu(torch.ones((2,2),device='cuda',dtype=torch.bfloat16),diagonal=1)
8
+ except RuntimeError as error:
9
+ if 'not implemented' not in str(error): raise
10
+ return torch.float32
11
+ return torch.bfloat16
12
+
3
13
  def run(options, project_file, output_dir):
4
14
  # Reuse a reviewed image; do not reinstall CUDA/torch or run remote model code.
5
15
  if any(importlib.util.find_spec(m) is None for m in ['torch','transformers','peft','safetensors']):
@@ -23,7 +33,7 @@ def run(options, project_file, output_dir):
23
33
  os.environ['HF_HUB_OFFLINE']='1';os.environ['TRANSFORMERS_OFFLINE']='1';os.environ['HF_HUB_DISABLE_TELEMETRY']='1'
24
34
  torch.manual_seed(0)
25
35
  tokenizer=AutoTokenizer.from_pretrained(str(model_root),local_files_only=True,trust_remote_code=False)
26
- dtype=torch.bfloat16 if torch.cuda.is_bf16_supported() else torch.float32
36
+ dtype=training_dtype(torch)
27
37
  model=AutoModelForCausalLM.from_pretrained(str(model_root),local_files_only=True,trust_remote_code=False,torch_dtype=dtype).to('cuda')
28
38
  model=get_peft_model(model,LoraConfig(r=options['rank'],lora_alpha=options['rank']*2,lora_dropout=0.05,bias='none',task_type='CAUSAL_LM',target_modules=options['target_modules']))
29
39
  model.train();optimizer=torch.optim.AdamW((p for p in model.parameters() if p.requires_grad),lr=options['learning_rate'])
@@ -43,4 +53,4 @@ def run(options, project_file, output_dir):
43
53
  p=output_dir/name
44
54
  if p.is_symlink() or not stat.S_ISREG(p.stat().st_mode) or p.stat().st_size<=0:
45
55
  raise ValueError('Expected adapter output missing')
46
- return {'task':'causal-lm-lora','steps_completed':options['steps'],'records':len(texts),'last_loss':loss_value,'base_model':ref['repo_id'],'revision':ref['revision'],'rank':options['rank']}
56
+ return {'task':'causal-lm-lora','steps_completed':options['steps'],'records':len(texts),'last_loss':loss_value,'base_model':ref['repo_id'],'revision':ref['revision'],'rank':options['rank'],'training_dtype':str(dtype)}
package/src/storage.js CHANGED
@@ -1,62 +1,95 @@
1
1
  // 持久文件直接流向对象存储;长期 key 只发给 CloudGPU,不发给签名 URL 的主机。
2
2
  import fs from "node:fs";
3
+ import { createHash } from "node:crypto";
3
4
  import fsp from "node:fs/promises";
4
5
  import path from "node:path";
5
- import { pipeline } from "node:stream/promises";
6
- import { Readable, Transform } from "node:stream";
6
+
7
+ import { Readable } from "node:stream";
7
8
  import { sensitiveReason } from "./guards.js";
8
9
  import { z } from "zod";
9
10
 
10
11
  export class PersistentStorage {
11
12
  constructor(api, fetchImpl = globalThis.fetch) { this.api = api; this.fetch = fetchImpl; }
12
- async upload(local, purpose = "artifact", name = path.basename(local), acceptStorageCharges = false) {
13
- const stat = await fsp.stat(local);
14
- if (!stat.isFile()) throw new Error("Upload needs a regular file.");
15
- const file = await this.api.request("POST", "/v1/files", { name, purpose, size_bytes: stat.size, accept_storage_charges: acceptStorageCharges === true });
16
- try {
17
- for (let i = 1, offset = 0; i <= file.part_count; i++) {
18
- const part = await this.api.request("POST", `/v1/files/${file.file_id}/parts/${i}`, {});
19
- if (!String(part.url).startsWith("https://")) throw new Error("Storage must use HTTPS.");
20
- const len = Number(part.content_length);
21
- if (!Number.isSafeInteger(len) || len < 0 || offset + len > stat.size || (len === 0 && stat.size !== 0)) throw new Error("Invalid upload reservation.");
22
- const stream = len === 0 ? Buffer.alloc(0) : fs.createReadStream(local, { start: offset, end: offset + len - 1 });
23
- try {
24
- const res = await this.fetch(part.url, { method: "PUT", headers: { "Content-Length": String(len) }, body: stream, duplex: "half", redirect: "error" });
25
- if (!res.ok) throw new Error("Part upload failed.");
26
- if (res.body) await res.body.cancel();
27
- } catch { throw new Error("Storage part upload failed; no credentials were sent to the API response."); }
28
- finally { if (stream.destroy) stream.destroy(); }
29
- offset += len;
13
+ async digest(local) {
14
+ const hash = createHash("sha256");
15
+ for await (const chunk of fs.createReadStream(local)) hash.update(chunk);
16
+ return hash.digest("hex");
17
+ }
18
+ async upload(local, purpose = "artifact", name = path.basename(local), acceptStorageCharges = false, resume) {
19
+ const stat = await fsp.lstat(local);
20
+ if (!stat.isFile() || stat.isSymbolicLink()) throw new Error("Upload needs a regular file.");
21
+ const digest = await this.digest(local);
22
+ if (resume && digest !== resume.sha256) throw new Error("Local file differs from the original upload; do not mix file versions.");
23
+ const file = resume
24
+ ? await this.api.request("GET", `/v1/files/${encodeURIComponent(resume.file_id)}/upload`)
25
+ : await this.api.request("POST", "/v1/files", { name, purpose, size_bytes: stat.size, accept_storage_charges: acceptStorageCharges === true });
26
+ if (resume && (Number(file.size_bytes) !== stat.size || file.name !== name)) throw new Error("Upload reservation differs from local file.");
27
+ const uploaded = new Set((file.uploaded_parts || []).map(p => Number(p.part_number)));
28
+ let offset = 0;
29
+ for (let i = 1; i <= file.part_count; i++) {
30
+ const expected = Math.min(Number(file.part_size_bytes), stat.size - offset);
31
+ if (!uploaded.has(i)) {
32
+ let done = false;
33
+ for (let attempt = 0; attempt < 3 && !done; attempt++) {
34
+ try {
35
+ const part = await this.api.request("POST", `/v1/files/${file.file_id}/parts/${i}`, {});
36
+ if (!String(part.url).startsWith("https://") || Number(part.content_length) !== expected) throw new Error("Invalid upload reservation.");
37
+ const stream = expected === 0 ? Buffer.alloc(0) : fs.createReadStream(local, { start: offset, end: offset + expected - 1 });
38
+ try {
39
+ const response = await this.fetch(part.url, { method: "PUT", headers: { "Content-Length": String(expected) }, body: stream, duplex: "half", redirect: "error", signal: AbortSignal.timeout(300000) });
40
+ if (response.body) await response.body.cancel();
41
+ if (!response.ok) throw new Error("Part failed.");
42
+ done = true;
43
+ } finally { if (stream.destroy) stream.destroy(); }
44
+ } catch { /* Renew only this part; never allocate a second reservation. */ }
45
+ }
46
+ if (!done) throw new Error(`Upload paused. Resume file_id=${file.file_id}, sha256=${digest}; reservation expires after 24 hours. No signed URL is retained.`);
30
47
  }
31
- const after = await fsp.stat(local);
32
- if (after.size !== stat.size || after.mtimeMs !== stat.mtimeMs) throw new Error("File changed during upload; snapshot was not saved.");
33
- return await this.api.request("POST", `/v1/files/${file.file_id}/complete`, {});
34
- } catch (e) {
48
+ offset += expected;
49
+ }
50
+ const after = await fsp.stat(local);
51
+ if (after.size !== stat.size || after.mtimeMs !== stat.mtimeMs || await this.digest(local) !== digest) {
35
52
  await this.api.request("DELETE", `/v1/files/${file.file_id}`).catch(() => {});
36
- throw e;
53
+ throw new Error("File changed during upload; snapshot was not saved.");
37
54
  }
55
+ try { return await this.api.request("POST", `/v1/files/${file.file_id}/complete`, {}); }
56
+ catch { throw new Error(`Upload completion uncertain for file_id=${file.file_id}. Check list_files and retry completion; do not delete or create another upload.`); }
38
57
  }
39
58
  async download(id, local) {
40
- const transfer = await this.api.request("POST", `/v1/files/${encodeURIComponent(id)}/download`, {});
41
- if (!String(transfer.url).startsWith("https://")) throw new Error("Storage must use HTTPS.");
42
- const expected = Number(transfer.size_bytes);
43
- if (!Number.isSafeInteger(expected) || expected < 0) throw new Error("Invalid file size.");
44
59
  await fsp.mkdir(path.dirname(local), { recursive: true });
45
- const handle = await fsp.open(local, "wx", 0o600); // 不覆盖已有结果。
60
+ const handle = await fsp.open(local, "wx", 0o600);
61
+ let received = 0, expected;
46
62
  try {
47
- const res = await this.fetch(transfer.url, { redirect: "error" });
48
- if (!res.ok || !res.body) throw new Error("Download failed.");
49
- let received = 0;
50
- const bounded = new Transform({ transform(chunk, _encoding, done) {
51
- received += chunk.length;
52
- done(received > expected ? new Error("Storage returned excess bytes.") : null, received > expected ? undefined : chunk);
53
- } });
54
- await pipeline(Readable.fromWeb(res.body), bounded, handle.createWriteStream());
55
- if ((await fsp.stat(local)).size !== Number(transfer.size_bytes)) throw new Error("Downloaded size mismatch.");
56
- return { local_path: local, bytes: transfer.size_bytes };
63
+ for (let attempt = 0; attempt < 4; attempt++) {
64
+ try {
65
+ const transfer = await this.api.request("POST", `/v1/files/${encodeURIComponent(id)}/download`, {});
66
+ if (!String(transfer.url).startsWith("https://")) throw new Error("HTTPS required.");
67
+ const size = Number(transfer.size_bytes);
68
+ if (!Number.isSafeInteger(size) || size < 0 || (expected !== undefined && expected !== size)) throw new Error("Invalid immutable file size.");
69
+ expected = size;
70
+ const response = await this.fetch(transfer.url, { redirect: "error", headers: received ? { Range: `bytes=${received}-` } : {}, signal: AbortSignal.timeout(300000) });
71
+ try {
72
+ if (!response.ok || !response.body) throw new Error("Download failed.");
73
+ if (received && (response.status !== 206 || response.headers.get("content-range") !== `bytes ${received}-${expected - 1}/${expected}`)) throw new Error("Invalid resume range.");
74
+ for await (const chunk of Readable.fromWeb(response.body)) {
75
+ if (received + chunk.length > expected) throw new Error("Excess file bytes.");
76
+ let written = 0;
77
+ while (written < chunk.length) {
78
+ const result = await handle.write(chunk, written, chunk.length - written, received + written);
79
+ if (!result.bytesWritten) throw new Error("Local write failed.");
80
+ written += result.bytesWritten;
81
+ }
82
+ received += chunk.length;
83
+ }
84
+ } finally { if (response.body && !response.body.locked) await response.body.cancel().catch(() => {}); }
85
+ if (received !== expected) throw new Error("Incomplete download.");
86
+ await handle.close();
87
+ return { local_path: local, bytes: expected };
88
+ } catch { if (attempt === 3) throw new Error("Transfer failed."); }
89
+ }
57
90
  } catch {
58
91
  await handle.close().catch(() => {}); await fsp.unlink(local).catch(() => {});
59
- throw new Error("Storage download failed; the partial file was removed.");
92
+ throw new Error("Storage download failed after bounded resume attempts; the partial file was removed.");
60
93
  }
61
94
  }
62
95
  async saveWorkspace(localDir, name, workspaceId, acceptStorageCharges = false) {
@@ -100,6 +133,18 @@ export function registerStorageTools(server, { api, wrap, ok, fail, readOnly, ex
100
133
  server.registerTool("list_workspace_instances", { title: "Running GPUs for Workspace restore", description: "Lists your running GPUs that this key may access. Budgeted Agent keys only see instances created by the same key.", inputSchema: {}, annotations: { readOnlyHint: true } }, wrap(async () => ok(await api.request("GET", "/v1/workspace-instances"))));
101
134
  server.registerTool("get_workspace_application", { title: "Workspace application status", description: "Check file restoration on an existing GPU. This does not create, stop or restart a GPU. An uncertain result must be checked, never automatically resubmitted.", inputSchema: { version_id: z.string(), instance_id: z.string().regex(/^[0-9]{1,32}$/) }, annotations: { readOnlyHint: true } }, wrap(async ({ version_id, instance_id }) => ok(await api.request("GET", `/v1/workspace-versions/${encodeURIComponent(version_id)}/applications/${encodeURIComponent(instance_id)}`))));
102
135
  if (readOnly) return;
136
+ server.registerTool("quote_storage_budget", {
137
+ title: "Quote separate storage budget", description: "Quote reserving GPU wallet funds for persistent storage, or returning unused surplus. No transfer yet. Shows USD rate, budget-based capacity and 30-day file expiry. Budget-limited keys cannot move funds; ask owner to use Web or unrestricted key.",
138
+ inputSchema: { action: z.enum(["reserve", "release"]), amount_usd: z.number().positive().max(1000000) }, annotations: { readOnlyHint: false, destructiveHint: false, openWorldHint: true },
139
+ }, wrap(async ({ action, amount_usd }) => ok(await api.request("POST", "/v1/storage/budget/quotes", { action, amount_usd }))));
140
+ server.registerTool("confirm_storage_budget", {
141
+ title: "Confirm storage budget transfer", description: "Moves wallet funds only after the user approves the exact quote. Retrying the same quote cannot transfer twice. Reservation is not billed usage; daily actual peak consumes the budget. Never approve automatically.",
142
+ inputSchema: { quote_id: z.string().regex(/^sbq_[a-f0-9]{32}$/), user_confirmed: z.boolean() }, annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: true },
143
+ }, wrap(async ({ quote_id, user_confirmed }) => user_confirmed === true ? ok(await api.request("POST", `/v1/storage/budget/quotes/${quote_id}/confirm`, { user_confirmed })) : fail("Ask the user to approve the exact storage quote first.")));
144
+ server.registerTool("resume_artifact_upload", {
145
+ title: "Resume interrupted file upload", description: "Resume the same unexpired multipart reservation. Requires the original SHA-256 reported when upload paused. Skips completed parts; renews expiring URLs. Never mixes changed files or creates another reservation.",
146
+ inputSchema: { local_path: z.string(), file_id: z.string().regex(/^file_[a-f0-9]{32}$/), sha256: z.string().regex(/^[a-f0-9]{64}$/) }, annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: true },
147
+ }, wrap(async ({ local_path, file_id, sha256 }) => { const local = path.resolve(expandHome(local_path)); if (sensitiveReason(local)) return fail("Credential files cannot be uploaded."); return ok(await client.upload(local, "artifact", path.basename(local), false, { file_id, sha256 })); }));
103
148
  server.registerTool("apply_workspace", {
104
149
  title: "Apply Workspace to running GPU", description: "Restore project files to a new private directory on your existing running GPU. Does not install dependencies, execute code, overwrite existing files, rent, restart or stop a GPU. GPU rental billing continues; transfer consumes its network and disk. Ask the user before applying. Repeat requests for the same version and instance return the existing result; failures require inspection, not automatic retries.",
105
150
  inputSchema: { version_id: z.string(), instance_id: z.string().regex(/^[0-9]{1,32}$/), user_confirmed: z.boolean() }, annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: true },