cloudgpu-mcp 0.10.0 → 0.12.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
@@ -306,7 +306,7 @@ After completion, inspect `get_job.output_collection`: only `succeeded` confirms
306
306
 
307
307
  Version 0.8.0 still has a credential-filter gap in `save_job_output`; upgrading to 0.8.0 did not fix it. The 0.9.0 candidate rejects quoted JSON credential keys, common prefixed/aliased secret fields, authorization schemes, queried/user-info URLs and bounded common escaped representations before any storage operation. Safe logs remain byte-identical. This is a conservative recognizable-pattern check, not a guarantee that arbitrary, encrypted or unknown credentials will be detected. Save only logs you know are sanitized, with explicit user approval; no actual exposure is established by this fix.
308
308
 
309
- ## Reusable task drafts (D16 first slice)
309
+ ## Reusable task drafts (D16)
310
310
 
311
311
  `list_task_templates` and `prepare_task_template` are local, read-only tools: no account is required, no API call is made, and preparing a draft never rents a GPU. Start with `gpu-check`, `csv-summary`, or `workspace-python`. CSV work normally belongs on your local CPU; do not rent a GPU merely to process a small CSV.
312
312
 
@@ -314,6 +314,41 @@ Version 0.8.0 still has a credential-filter gap in `save_job_output`; upgrading
314
314
  {"template_id":"gpu-check","persist_results":false}
315
315
  ```
316
316
 
317
- For an existing project, pass `workspace-python`, an immutable `workspace_version_id`, and a reviewed relative `.py` entrypoint. The script runs in the restored project; prepare its dependencies yourself. Inference/training compatibility is not certified. For ComfyUI model/workflow preparation, keep using `prepare_model_deployment`; complete ComfyUI and LoRA batch recipes are not included in this slice.
317
+ For an existing project, pass `workspace-python`, an immutable `workspace_version_id`, and a reviewed relative `.py` entrypoint. The script runs in the restored project; prepare its dependencies yourself. Inference/training compatibility is not certified. For model/workflow file preparation, `prepare_model_deployment` remains available. The experimental execution recipes below reuse a prepared environment; image/SDXL LoRA is not included.
318
318
 
319
319
  The returned `run_job_draft` deliberately omits GPU selection, quote, budget, time limit, approval and idempotency key. Request a live quote, obtain explicit approval, then call existing `run_job`. Failed tasks are not automatically retried. To preserve results, explicitly request `persist_results`; normal storage eligibility, retention, quota and fees apply. `workspace-python` also requires the exact `output_files` your script writes under `$CLOUDGPU_OUTPUT_DIR`. Storage-fee consent defaults to false and is separate from GPU approval.
320
+
321
+ ### Experimental application execution recipes
322
+
323
+ `comfyui-workflow` runs an API-format workflow saved in an immutable Workspace against an **already prepared ComfyUI** application on GPU loopback (default port 8188). Models and custom nodes must already be available and reviewed. Supply `workflow_path`, `image_count` (1–4) and optionally `comfy_port`/`timeout_seconds`. It checks readiness, submits exactly once, polls that prompt and writes explicit `image-N.png` files plus `task-summary.json`. Lost submission responses, errors, redirects or unexpected output counts stop the task; no second prompt is submitted.
324
+
325
+ `causal-lm-lora` is **small causal-language-model LoRA**, not ComfyUI/image/SDXL training. Supply `dataset_path` (JSONL with a `text` field), a pinned public `model_reference` including configuration/tokenizer/weights, and reviewed `target_modules`. Optional `steps` (1–200), `rank` (1–16), `sequence_length` and `learning_rate` bound the work. Reuse an image with compatible CUDA, torch, transformers, peft and safetensors already installed. Model loading is offline from the D13 model directory with remote model code disabled. It writes `adapter_model.safetensors`, `adapter_config.json` and `task-summary.json`.
326
+
327
+ Both recipes require `persist_results=true` and an immutable Workspace. They do not install software or start servers, and preparation does not select a GPU, infer a budget, claim image compatibility or consent to storage charges. The existing Job API checks output-collection availability before accepting the request. GPU time, downloads and collection share the approved Job cap; existing storage charges are separate. Failed collection does not keep the GPU disk alive. Do not save arbitrary private logs as artifacts.
328
+
329
+ **Validation boundary:** ComfyUI HTTP execution/output and LoRA orchestration have local protocol fixtures, not real GPU generation/training or quality acceptance. These recipes remain experimental. Check the selected image and model before a paid run; no universal compatibility claim is made.
330
+
331
+ ## Bounded batches (D17 first stage)
332
+
333
+ 1. Prepare each reviewed Job with a fresh `quote_compute` quote, its own GPU spending cap and time limit. Select output storage explicitly.
334
+ 2. Call `prepare_job_batch` with a caller-retained `batch_key`, `jobs` (1–8) and `total_budget_usd`. Preparation is local and read-only. The sum of per-Job maximum spending must fit the total and client cap; this is a **GPU budget**, excluding separately approved storage charges.
335
+ 3. Present all tasks, their quotes, time limits and combined cap to the user. Only after explicit approval, call `run_job_batch` with the identical input, returned `approved_plan_sha256` and `user_confirmed=true`.
336
+ 4. Retain the manifest, batch key and response receipt. Partial acceptance is possible; this is not an atomic transaction or an independent scheduler. A lost acknowledgement stops new submissions. To reconcile, replay only the **identical** approved manifest/key: existing server-side idempotency returns the original Jobs. Never invent replacement keys or silently change an expired quote. Changing a task requires a new review; an old batch key is not permission for extra spending.
337
+ 5. Poll `get_job_batch` with the explicit Job IDs. `stopping`, `needs_attention`, unknown or unavailable state requires reconciliation. Do not treat a failed status or a destroy request as independent supplier release evidence. Cancel individual Jobs only through the existing `cancel_job` tool when authorized.
338
+
339
+ There are **no automatic failed-execution retries**. Each submitted Job survives a client disconnect using the existing durable backend queue. Unsubmitted members do not continue in the background if the MCP process exits. Fresh-attempt retry automation and a durable batch-level coordinator are not included. No warm GPU pool or new infrastructure is required.
340
+
341
+
342
+ ### Private local batch receipts (0.12.0)
343
+
344
+ `run_job_batch` now journals submission intents and accepted Job IDs under `CLOUDGPU_CONFIG_DIR` (default `~/.config/cloudgpu`), in a private `batch-receipts-v1` directory. `get_job_batch_receipt({batch_key})` reads this local metadata without an API request. It records no commands, complete manifests, API keys, model URLs or signed links. Keep your original reviewed manifest separately.
345
+
346
+ After a client restart, reconnect with the **same scoped API key and endpoint**. Inspect the receipt, then use `get_job_batch` for live status. Replaying the exact approved manifest queries known accepted Jobs; only an uncertain acknowledgement is reconciled by the original create request/key. Unavailable, `stopping` or `needs_attention` stops progress. A local historical status is not a current supplier release audit. A missing receipt (another device/key, an older client, or lost files) is never proof that no GPU exists.
347
+
348
+ The local store must support private file permissions, no-follow file opens and atomic hard links. An unsafe/unavailable store refuses new batch submissions; a write failure after acceptance returns the known Job ID and stops later members. Do not silently switch to a new key or discard uncertain records. New entries are refused once 1,024 batch directories are observed for a key/endpoint; this local housekeeping check is not a concurrent server quota. Archive **verified terminal** receipts explicitly. Original single-Job tools remain available.
349
+
350
+ This is client-local receipt durability, not a server-side batch coordinator. Unsubmitted members do not run while the client is closed. No automatic retry of failed execution, new-attempt spending, refund or cancellation is added. Preparing a plan still creates no GPU and requires no account; paid submission still requires exact plan approval, fresh quotes and existing GPU/storage limits. Scopes on different keys or endpoints are separate; inspect existing resources before intentionally changing either.
351
+
352
+ Keep the exact original manifest outside the receipt store. Expired quotes for unsubmitted members may require reconciliation followed by a newly reviewed plan for the remaining work; do not overwrite the old ledger or regenerate keys merely to bypass expiry. Terminal status without final cost evidence also stops a journaled resume.
353
+
354
+ One in-flight batch retains its original API credential and endpoint even if another tool connects a different account concurrently. `not_submitted` in a returned call receipt means unvisited **in that invocation only**; those entries may have accepted Jobs from an earlier call. Inspect the saved receipt and live Job IDs before deciding what remains.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cloudgpu-mcp",
3
- "version": "0.10.0",
3
+ "version": "0.12.0",
4
4
  "mcpName": "io.github.mrhy7176/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 per minute, run a job, fetch the results and shut it down, with spending limits the platform enforces.",
6
6
  "type": "module",
package/rules/cloudgpu.md CHANGED
@@ -56,3 +56,9 @@ If anything goes wrong mid-way, do not leave the instance running. Download what
56
56
  ## Only need a model, not a GPU?
57
57
 
58
58
  The same API key works for CloudGPU's OpenAI-compatible model API at `https://cloudgpu.app/v1`. Call `list_models` to see the models. If you only need inference on a hosted model, use the API instead of renting a GPU.
59
+
60
+ ## Multi-Job batches
61
+
62
+ Use prepare_job_batch to review all tasks and the sum of their GPU budget caps before any submission. Storage charges are separate. run_job_batch requires explicit approval and the exact plan hash. Preserve the full approved manifest, stable batch key and receipt. A partial or unknown submission never permits a replacement key: replay the identical manifest to recover original Jobs. Never automatically retry failed execution or create a replacement GPU while release is uncertain. Batch preparation is not a reservation, and accepted Jobs are independent, not an atomic transaction. Unsubmitted members do not survive a client exit.
63
+
64
+ Application recipes reuse prepared environments. ComfyUI protocol fixtures and LoRA orchestration fixtures are not real GPU or model-quality acceptance. No image/SDXL LoRA recipe is supplied. Never tell a user a template is production-validated based on these fixtures, or ask for credentials in workflow files.
@@ -0,0 +1,27 @@
1
+ // 复用已准备的环境,不替用户安装软件、不托管模型、不自动租卡或重试。
2
+ import fs from 'node:fs';
3
+ import {createHash} from 'node:crypto';
4
+ import {z} from 'zod';
5
+ const relative=z.string().min(1).max(240).regex(/^[A-Za-z0-9_][A-Za-z0-9_./-]*$/).refine(p=>p.split('/').every(s=>s&&s!=='.'&&s!=='..'&&!s.startsWith('.')));
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
+ export const APPLICATION_TASKS=[
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'},
10
+ ];
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
+ export function prepareApplicationRecipe(p){
13
+ const t=APPLICATION_TASKS.find(t=>t.id===p.template_id);if(!t)return null;
14
+ if(!p.workspace_version_id||!p.persist_results)throw Error('Select an immutable Workspace and explicitly request result persistence; binary results would otherwise be erased');
15
+ if(p.input_path||p.entrypoint||p.output_files)throw Error('This recipe uses its own explicit input and output fields');
16
+ const comfy=t.id==='comfyui-workflow';
17
+ const allowed=comfy?['workflow_path','comfy_port','timeout_seconds','image_count']:['model_reference','dataset_path','target_modules','steps','rank','sequence_length','learning_rate'];
18
+ for(const key of Object.keys(applicationFields))if(p[key]!==undefined&&!allowed.includes(key))throw Error('Field does not belong to this recipe: '+key);
19
+ if(comfy?!p.workflow_path:(!p.model_reference||!p.dataset_path||!p.target_modules))throw Error('Supply the recipe inputs and reviewed model configuration');
20
+ const options=comfy?{workflow_path:p.workflow_path,comfy_port:p.comfy_port??8188,timeout_seconds:p.timeout_seconds??600,image_count:p.image_count??1,image_max_bytes:20*1024*1024}:{model_reference:p.model_reference,dataset_path:p.dataset_path,target_modules:p.target_modules,steps:p.steps??10,rank:p.rank??4,sequence_length:p.sequence_length??256,learning_rate:p.learning_rate??0.0002};
21
+ const recipe=fs.readFileSync(new URL(comfy?'./recipes/comfy-workflow.py':'./recipes/causal-lora.py',import.meta.url),'utf8');
22
+ const common=`import json,os,pathlib,stat,sys\noptions=json.loads(${JSON.stringify(JSON.stringify(options))})\ndef project_file(relative,limit):\n root=pathlib.Path.cwd().resolve();p=root/relative;check=root\n for part in p.relative_to(root).parts:\n check=check/part\n if check.is_symlink(): raise ValueError('Project input must not use symlinks')\n meta=p.stat()\n if not stat.S_ISREG(meta.st_mode) or meta.st_size>limit: raise ValueError('Project input exceeds limits')\n return p\n`;
23
+ const main=`\ntry:\n out=pathlib.Path(os.environ['CLOUDGPU_OUTPUT_DIR'])\n if out.is_symlink() or not out.is_dir(): raise ValueError('Output directory unavailable')\n if any(out.iterdir()): raise ValueError('Output directory must be empty')\n result=run(options,project_file,out)\n if sum(p.stat().st_size for p in out.iterdir())>90*1024*1024: raise ValueError('Result size exceeds the task limit')\n with (out/'task-summary.json').open('x',encoding='utf-8') as f: json.dump(result,f,sort_keys=True)\n print(json.dumps({'task':result['task'],'result':'written_to_explicit_outputs'}))\nexcept Exception:\n print('Task failed; inspect the reviewed workflow, model, dataset and environment. No automatic retry.',file=sys.stderr)\n sys.exit(2)\n`;
24
+ const command="set -eu\numask 077\npython3 - <<'CGPU_APPLICATION_RECIPE'\n"+common+recipe+main+"CGPU_APPLICATION_RECIPE\n";
25
+ const files=comfy?[...Array.from({length:options.image_count},(_,i)=>`image-${i}.png`),'task-summary.json']:t.result_files;
26
+ return {preparation_only:true,starts_gpu:false,template_id:t.id,template_version:t.version,validation:t.validation,command_sha256:createHash('sha256').update(command).digest('hex'),run_job_draft:{command,workspace_version_id:p.workspace_version_id,...(!comfy?{model_references:[p.model_reference]}:{}),outputs:{files,max_bytes:100*1024*1024,accept_storage_charges:p.accept_storage_charges===true}},requirements:t.requirements,missing_fields:['compatible image/template and fresh quote_id','max_spend_usd','max_hours','user_confirmed','idempotency_key'],next_steps:['Review the workflow, model licence, dependencies and prepared GPU image. No dependencies are installed by this recipe.','Obtain a fresh compatible quote and explicit budget/time approval. Preparation is not GPU acceptance.','Result storage uses existing eligibility, quota, retention and charges; never infer paid-storage consent.','Run once with a stable idempotency key. Poll the Job, output collection and release evidence; do not automatically retry.'],storage_note:'Binary outputs require the existing output collector. The GPU is released after the Job; failed or oversized collection does not preserve the GPU disk.'};
27
+ }
@@ -0,0 +1,78 @@
1
+ // 本地受理回执:只保存哈希、Job ID与状态,不保存命令、凭证、签名URL或完整任务清单。
2
+ import fs from 'node:fs/promises';
3
+ import {constants} from 'node:fs';
4
+ import path from 'node:path';
5
+ import {createHash,randomBytes} from 'node:crypto';
6
+ import {z} from 'zod';
7
+ import {configDir} from './credentials.js';
8
+ const sha=s=>createHash('sha256').update(s).digest('hex');
9
+ const states=z.enum(['queued','provisioning','running','stopping','needs_attention','succeeded','failed','cancelled']);
10
+ const key=z.string().regex(/^batch_[a-f0-9]{64}$/);
11
+ const meta=z.object({version:z.literal(1),plan_sha256:z.string().regex(/^[a-f0-9]{64}$/),total_budget_usd:z.number().positive(),item_keys:z.array(key).min(1).max(8)}).strict();
12
+ const intent=z.object({version:z.literal(1),index:z.number().int().min(0).max(7),idempotency_key:key}).strict();
13
+ const accepted=intent.extend({job_id:z.string().regex(/^job_[a-f0-9]{32}$/),status_at_acceptance:states}).strict();
14
+ const safeError=()=>new Error('Local batch receipt is unavailable or inconsistent. No replacement key or GPU may be created; retain the original manifest and reconcile existing Jobs.');
15
+ async function directory(p,create=false){
16
+ try{const s=await fs.lstat(p);if(s.isSymbolicLink()||!s.isDirectory()||(s.mode&0o077)!==0||(process.getuid&&s.uid!==process.getuid()))throw safeError();}
17
+ catch(e){if(e.code!=='ENOENT'||!create)throw e;await fs.mkdir(p,{mode:0o700}).catch(e=>{if(e.code!=='EEXIST')throw e;});return directory(p);}
18
+ }
19
+ async function read(p,schema){
20
+ let f;try{f=await fs.open(p,constants.O_RDONLY|constants.O_NOFOLLOW);const s=await f.stat();if(!s.isFile()||s.size>8192||(s.mode&0o077)!==0||(process.getuid&&s.uid!==process.getuid()))throw safeError();return schema.parse(JSON.parse(await f.readFile('utf8')));}
21
+ catch(e){if(e.code==='ENOENT')return null;throw safeError();}finally{await f?.close();}
22
+ }
23
+ // 独立不可变文件避免进程退出或并发覆盖已受理ID。后端幂等仍是创建资源的最终防线。
24
+ async function put(p,value,schema,equal=(a,b)=>JSON.stringify(a)===JSON.stringify(b)){
25
+ schema.parse(value);
26
+ const tmp=p+'.tmp-'+randomBytes(12).toString('hex');let f;
27
+ try{
28
+ f=await fs.open(tmp,constants.O_WRONLY|constants.O_CREAT|constants.O_EXCL|constants.O_NOFOLLOW,0o600);
29
+ await f.writeFile(JSON.stringify(value)+'\n');await f.sync();await f.close();f=null;
30
+ try{await fs.link(tmp,p);}catch(e){if(e.code!=='EEXIST')throw e;const old=await read(p,schema);if(!equal(old,value))throw safeError();}
31
+ const d=await fs.open(path.dirname(p),constants.O_RDONLY|constants.O_DIRECTORY|constants.O_NOFOLLOW);try{await d.sync();}finally{await d.close();}
32
+ }catch{throw safeError();}finally{await f?.close();await fs.unlink(tmp).catch(()=>{});}
33
+ }
34
+
35
+ export class BatchReceipts{
36
+ constructor({apiKey,baseUrl,root=configDir(),maxBatches=1024}){this.apiKey=apiKey;this.root=root;this.scope=sha(String(baseUrl||'https://cloudgpu.app')+'\0'+String(apiKey||''));this.maxBatches=maxBatches;}
37
+ async locate(batchKey,create){
38
+ if(!this.apiKey)throw Error('Connect the original scoped account before reading or submitting a batch receipt. Never paste the key into chat.');
39
+ if(!/^[A-Za-z0-9_-]{1,80}$/.test(batchKey))throw Error('Invalid batch key');
40
+ // 配置目录由用户选择;回执树及下级目录必须为当前用户私有,拒绝符号链接。
41
+ if(create)await fs.mkdir(this.root,{recursive:true,mode:0o700}).catch(()=>{throw safeError();});
42
+ const base=path.join(this.root,'batch-receipts-v1');
43
+ try{await directory(base,create);}catch(e){if(e.code==='ENOENT')return null;throw safeError();}
44
+ const scope=path.join(base,this.scope);
45
+ try{await directory(scope,create);}catch(e){if(e.code==='ENOENT')return null;throw safeError();}
46
+ const dir=path.join(scope,sha(batchKey));
47
+ try{await directory(dir,false);}catch(e){if(e.code!=='ENOENT')throw safeError();if(!create)return null;
48
+ if((await fs.readdir(scope).catch(()=>{throw safeError();})).length>=this.maxBatches)throw Error('Local batch receipt limit reached. Archive verified terminal receipts explicitly; do not discard uncertain receipts.');
49
+ await directory(dir,true);
50
+ }
51
+ return dir;
52
+ }
53
+ async bind(plan){
54
+ const dir=await this.locate(plan.batch_key,true);
55
+ const value={version:1,plan_sha256:plan.plan_sha256,total_budget_usd:plan.total_budget_usd,item_keys:plan.items.map(x=>x.idempotency_key)};
56
+ await put(path.join(dir,'plan.json'),value,meta);
57
+ return this.handle(dir,value);
58
+ }
59
+ handle(dir,value){return {
60
+ accepted:async index=>{const row=await read(path.join(dir,index+'.accepted.json'),accepted);if(row&&(row.index!==index||row.idempotency_key!==value.item_keys[index]))throw safeError();return row;},
61
+ intent:async item=>put(path.join(dir,item.index+'.intent.json'),{version:1,index:item.index,idempotency_key:item.idempotency_key},intent),
62
+ accept:async(item,job)=>{
63
+ const p=path.join(dir,item.index+'.accepted.json'),row={version:1,index:item.index,idempotency_key:item.idempotency_key,job_id:job.id,status_at_acceptance:job.status};
64
+ const prior=await read(p,accepted);if(prior){if(prior.job_id!==job.id||prior.idempotency_key!==item.idempotency_key)throw safeError();return;}
65
+ await put(p,row,accepted,(a,b)=>a?.job_id===b.job_id&&a?.idempotency_key===b.idempotency_key&&a?.index===b.index);
66
+ },
67
+ };}
68
+ async snapshot(batchKey){
69
+ const dir=await this.locate(batchKey,false);
70
+ if(!dir)return {found:false,items:[],note:'No local receipt was found. This does not prove that a GPU was never created; older clients or another key/device may hold the receipt.'};
71
+ const value=await read(path.join(dir,'plan.json'),meta);if(!value)throw safeError();const h=this.handle(dir,value),items=[];
72
+ for(let i=0;i<value.item_keys.length;i++){
73
+ const a=await h.accepted(i),n=await read(path.join(dir,i+'.intent.json'),intent);if(n&&(n.index!==i||n.idempotency_key!==value.item_keys[i]))throw safeError();
74
+ items.push({index:i,idempotency_key:value.item_keys[i],job_id:a?.job_id??null,local_state:a?'accepted':n?'submission_uncertain':'not_observed_locally',observed_status_at_acceptance:a?.status_at_acceptance??null});
75
+ }
76
+ return {found:true,plan_sha256:value.plan_sha256,total_budget_usd:value.total_budget_usd,items,live_status:false,execution_retries:0,note:'Private local metadata only, scoped to the original key and API endpoint. Query get_job_batch for live status. Resume only the identical approved manifest; no absent local entry proves non-creation. No automatic execution retry or durable server batch coordinator.'};
77
+ }
78
+ }
package/src/index.js CHANGED
@@ -1,4 +1,6 @@
1
1
  #!/usr/bin/env node
2
+ import {registerBatchTools} from './job-batches.js';
3
+ import {jobInputShape} from './job-input.js';
2
4
  import {registerTaskTemplateTools} from './task-templates.js';
3
5
  import {resolveModelReference} from "./model-references.js";
4
6
  // CloudGPU MCP server (stdio). Lets an AI coding assistant rent a GPU on cloudgpu.app, run a job,
@@ -33,7 +35,7 @@ import {
33
35
  shq,
34
36
  } from "./guards.js";
35
37
 
36
- const VERSION = "0.10.0";
38
+ const VERSION = "0.12.0";
37
39
  const HERE = path.dirname(fileURLToPath(import.meta.url));
38
40
  const RULES = fs.readFileSync(path.join(HERE, "..", "rules", "cloudgpu.md"), "utf8");
39
41
 
@@ -52,13 +54,14 @@ function num(v, d) {
52
54
  return Number.isFinite(n) && n > 0 ? n : d;
53
55
  }
54
56
 
55
- const api = new CloudGpuApi({ apiKey: cfg.apiKey, baseUrl: cfg.baseUrl });
57
+ const api = new CloudGpuApi({ apiKey: cfg.apiKey, baseUrl: cfg.baseUrl, userAgent: `cloudgpu-mcp/${VERSION}` });
56
58
  const pool = new SshPool();
57
59
  const quotes = new Quotes();
58
60
  // Instances this session touched: did we run anything, did we download anything.
59
61
  const session = new Map();
60
62
 
61
63
  const INSTRUCTIONS = `CloudGPU (cloudgpu.app) rents GPUs billed per minute from a prepaid balance.
64
+ Batches: prepare_job_batch reviews up to eight individually quoted Jobs and their combined GPU budget. run_job_batch requires an exact approved plan hash; partial submission is possible. Retain the manifest and stable batch key to reconcile lost responses. No automatic execution retries; get_job_batch never proves independent supplier release. Storage charges are additional to GPU caps.
62
65
  Task recipes: list_task_templates and prepare_task_template return local read-only drafts, never a paid submission. Check prerequisites, quote and obtain explicit approval before run_job. Prefer local CPU processing when a GPU is unnecessary.
63
66
  Workflow: get_account and list_gpus -> estimate_cost -> show the quote to the user and get an explicit yes -> create_instance -> get_status until running -> upload -> run_command (background for long jobs) -> tail_logs -> download results -> stop_instance -> report the actual cost.
64
67
  Rules: never rent without the user's yes on a quote; always set a budget and time limit (the platform shuts the instance down when either is hit); download results before stop_instance because destroying an instance erases its disk; never change passwords, SSH config, the firewall or the platform's monitoring agent on the instance; if anything fails, download what exists and stop the instance rather than leaving it running.
@@ -68,6 +71,7 @@ The same API key also works for the OpenAI-compatible model API at ${cfg.baseUrl
68
71
 
69
72
  const server = new McpServer({ name: "cloudgpu", version: VERSION }, { instructions: INSTRUCTIONS });
70
73
  registerTaskTemplateTools(server, {wrap, ok});
74
+ registerBatchTools(server,{api,cfg,wrap,ok});
71
75
  registerStorageTools(server, { api, wrap, ok, fail, readOnly: cfg.readOnly, expandHome });
72
76
  registerJobResultTools(server, { api, wrap, ok, readOnly: cfg.readOnly });
73
77
 
@@ -510,25 +514,7 @@ if (!cfg.readOnly) {
510
514
  "max_spend_usd and max_hours are enforced by the servers. Retrying with the same idempotency_key never starts a second GPU; this tool generates one if you leave it out and returns it. " +
511
515
  "The disk is erased when the job ends. When automatic_output_collection is available, declare outputs and write only those files under $CLOUDGPU_OUTPUT_DIR for persistent storage before release. Otherwise upload results yourself or print them. Storage retention and fees follow the account policy; collection consumes the same GPU budget and may fail. " +
512
516
  "Requires the user's explicit yes on the GPU, budget and time limit.",
513
- inputSchema: {
514
- command: z.string().min(1).max(32768).describe("bash command or script to run on the GPU"),
515
- gpu_model: z.string().optional().describe("GPU model exactly as list_gpus returns it"),
516
- template: z.string().optional().describe("Template id from list_templates"),
517
- gpu_count: z.number().int().min(1).max(8).default(1),
518
- disk_gb: z.number().int().optional().describe("Larger system disk: 100, 200 or 500; omit for the standard disk"),
519
- workspace_version_id: z.string().optional().describe("Immutable Workspace version to restore before the command; requires enabled persistent storage"),
520
- model_references: z.array(z.object({name:z.string(),repo_id:z.string(),revision:z.string().regex(/^[0-9a-f]{40}$/),files:z.array(z.object({path:z.string(),size_bytes:z.number().int().positive(),sha256:z.string().regex(/^[0-9a-f]{64}$/).optional()})).min(1).max(32)})).max(4).optional().describe("Pinned public model references from resolve_model_reference. Downloads into $CLOUDGPU_MODEL_DIR/<name> before the command; download time consumes the same GPU budget. No R2 cache or remote repository code execution."),
521
- outputs: z.object({
522
- files: z.array(z.string().min(1).max(512)).min(1).max(16),
523
- max_bytes: z.number().int().min(1).max(104857600).optional(),
524
- accept_storage_charges: z.boolean().optional(),
525
- }).strict().optional().describe("Explicit relative output files under $CLOUDGPU_OUTPUT_DIR; no globs, symlinks, hidden or credential files. Default total limit 10 MiB, maximum 100 MiB. Only set accept_storage_charges=true after the user agrees to existing paid storage terms. Inspect output_collection after completion; the GPU is released even if saving fails."),
526
- quote_id: z.string().optional().describe("Server quote from quote_compute, starting quote_; binds configuration and checks price at submission"),
527
- max_spend_usd: z.number().positive().describe(`Budget for this job in USD (at most ${cfg.maxSpend})`),
528
- max_hours: z.number().positive().describe(`Hard time limit in hours (at most ${cfg.maxHours})`),
529
- user_confirmed: z.boolean().describe("true only if the user explicitly approved the GPU, budget and time limit"),
530
- idempotency_key: z.string().max(128).optional().describe("Reuse the same value when retrying the same job"),
531
- },
517
+ inputSchema: jobInputShape(cfg),
532
518
  annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: true, openWorldHint: true },
533
519
  },
534
520
  wrap(async ({ command, gpu_model, template, gpu_count = 1, disk_gb, workspace_version_id, model_references, outputs, quote_id, max_spend_usd, max_hours, user_confirmed, idempotency_key }) => {
@@ -0,0 +1,79 @@
1
+ // D17第一阶段:复用现有持久化Job队列。部分提交可恢复,不伪装成原子事务。
2
+ import {createHash} from 'node:crypto';
3
+ import {z} from 'zod';
4
+ import {CloudGpuApi} from './api.js';
5
+ import {BatchReceipts} from './batch-receipts.js';
6
+ import {jobInputShape} from './job-input.js';
7
+ import {checkCommand} from './guards.js';
8
+ const sha=value=>createHash('sha256').update(value).digest('hex');
9
+ function stable(value){if(Array.isArray(value))return '['+value.map(stable).join(',')+']';if(value&&typeof value==='object')return '{'+Object.keys(value).filter(k=>value[k]!==undefined).sort().map(k=>JSON.stringify(k)+':'+stable(value[k])).join(',')+'}';return JSON.stringify(value);}
10
+ function micros(value){const s=String(value);if(!/^\d+(?:\.\d{1,6})?$/.test(s))throw Error('Budgets require at most six decimal places');const [whole,decimal='']=s.split('.');return BigInt(whole)*1000000n+BigInt(decimal.padEnd(6,'0'));}
11
+ function schema(cfg){return z.object({
12
+ batch_key:z.string().min(1).max(80).regex(/^[A-Za-z0-9_-]+$/),
13
+ total_budget_usd:z.number().positive(),
14
+ jobs:z.array(z.object(jobInputShape(cfg)).omit({user_confirmed:true,idempotency_key:true}).extend({quote_id:z.string().regex(/^quote_[a-f0-9]{32}$/)}).strict()).min(1).max(8),
15
+ }).strict();}
16
+ function bodyOf(item){const {max_hours,...rest}=item;return {...rest,max_minutes:Math.ceil(max_hours*60)};}
17
+ export function prepareJobBatch(raw,cfg){
18
+ const p=schema(cfg).parse(raw);const cap=micros(p.total_budget_usd);
19
+ if(cap>micros(cfg.maxSpend))throw Error('Batch total exceeds this client spending cap');
20
+ let reserved=0n;
21
+ const items=p.jobs.map((job,index)=>{
22
+ if(job.max_hours>cfg.maxHours||micros(job.max_spend_usd)>micros(cfg.maxSpend))throw Error('Job exceeds client budget or time cap');
23
+ const refused=checkCommand(job.command);if(refused)throw Error(refused);
24
+ reserved+=micros(job.max_spend_usd);
25
+ return {index,idempotency_key:'batch_'+sha(p.batch_key+':'+index),body:bodyOf(job)};
26
+ });
27
+ if(reserved>cap)throw Error('Sum of per-Job maximum spending exceeds the approved batch budget');
28
+ const plan_sha256=sha(stable(p));
29
+ return {preparation_only:true,starts_gpu:false,batch_key:p.batch_key,plan_sha256,total_budget_usd:p.total_budget_usd,reserved_maximum_usd:Number(reserved)/1e6,items,
30
+ atomic:false,execution_retries:0,scope:'GPU Job spending caps only; separately approved storage charges are additional. Immutable manifest; unused GPU budget is not automatically reassigned.',
31
+ next:'Review every command, GPU quote, time limit and storage choice. Retain this exact manifest, plan hash and batch key. Submission may partially succeed; resume the identical manifest, never invent new keys after a lost response.'};
32
+ }
33
+ export async function submitJobBatch(api,raw,cfg,receipts=null){
34
+ // 固定本次批量调用的凭证/端点,避免并行connect或轮换让重试跨账户。
35
+ if(api instanceof CloudGpuApi)api=new CloudGpuApi({apiKey:api.apiKey,baseUrl:api.baseUrl,fetchImpl:api.fetch,userAgent:api.userAgent});
36
+ const {approved_plan_sha256,user_confirmed,...input}=raw;
37
+ if(user_confirmed!==true)throw Error('Obtain explicit approval for all Jobs and the aggregate budget before submission');
38
+ const plan=prepareJobBatch(input,cfg);
39
+ if(approved_plan_sha256!==plan.plan_sha256)throw Error('The approved batch manifest changed; review it again before submitting');
40
+ const journal=receipts?await receipts.bind(plan):null;
41
+ const results=[];
42
+ for(const item of plan.items){
43
+ let known;
44
+ if(journal){
45
+ try{known=await journal.accepted(item.index);if(!known)await journal.intent(item);}
46
+ catch{return receipt('receipt_unavailable',results,item.index);}
47
+ }
48
+ try{
49
+ const job=known?await api.job(known.job_id):await api.createJob(item.body,item.idempotency_key);
50
+ if(known&&job?.id!==known.job_id)throw Error('Mismatched recorded Job');
51
+ if(!job||!/^job_[a-f0-9]{32}$/.test(job.id)||!['queued','provisioning','running','stopping','needs_attention','succeeded','failed','cancelled'].includes(job.status))throw Error('Invalid submission response');
52
+ results.push({index:item.index,idempotency_key:item.idempotency_key,job_id:job.id,status:job.status});
53
+ if(journal&&!known){try{await journal.accept(item,job);}catch{return receipt('receipt_persistence_failed',results,item.index+1);}}
54
+ if(job.status==='needs_attention'||job.status==='stopping'||(journal&&['succeeded','failed','cancelled'].includes(job.status)&&job.cost_status!=='final'))return receipt('reconciliation_required',results,item.index+1);
55
+ }catch{
56
+ // Even a server error may follow an accepted create. Never turn uncertainty into another GPU.
57
+ results.push({index:item.index,idempotency_key:item.idempotency_key,job_id:known?.job_id??null,status:known?'status_unavailable':'submission_uncertain'});
58
+ return receipt('submission_stopped',results,item.index+1);
59
+ }
60
+ }
61
+ return receipt('submitted',results,plan.items.length);
62
+ function receipt(status,items,next){return {batch_key:plan.batch_key,plan_sha256:plan.plan_sha256,status,atomic:false,total_budget_usd:plan.total_budget_usd,items,not_submitted_scope:'this_invocation_only; entries may already exist from an earlier invocation',not_submitted:plan.items.slice(next).map(i=>({index:i.index,idempotency_key:i.idempotency_key})),execution_retries:0,local_receipts_enabled:!!journal,next:status==='submitted'?'Poll get_job_batch. A failed command is not automatically retried.':'Reconcile or replay only the identical approved manifest with the same batch key. No automatic new-attempt retry or cancellation was performed.'};}
63
+ }
64
+ export async function getJobBatch(api,ids){
65
+ z.array(z.string().regex(/^job_[a-f0-9]{32}$/)).min(1).max(8).parse(ids);
66
+ if(new Set(ids).size!==ids.length)throw Error('Job IDs must be distinct');
67
+ const items=[];
68
+ for(const id of ids){try{const j=await api.job(id);if(j.id!==id||!['queued','provisioning','running','stopping','needs_attention','succeeded','failed','cancelled'].includes(j.status))throw Error('Unrecognized Job response');items.push({job_id:id,status:j.status,cost_usd:j.cost_usd??null,cost_status:j.cost_status??null,automatic_retry_allowed:false,release_risk:j.status==='needs_attention'||j.status==='stopping'});}catch{items.push({job_id:id,status:'unavailable',automatic_retry_allowed:false,release_risk:true});}}
69
+ return {items,all_terminal:items.every(j=>['succeeded','failed','cancelled'].includes(j.status)),requires_reconciliation:items.some(j=>j.release_risk),automatic_retry_allowed:false,note:'Status and costs come from the existing Job API. Terminal state is not an independent supplier audit; stopping, needs_attention or unavailable must never trigger a new GPU.'};
70
+ }
71
+ export function registerBatchTools(server,{api,cfg,wrap,ok}){
72
+ const input=schema(cfg);
73
+ const receipts=()=>new BatchReceipts({apiKey:'apiKey' in api?api.apiKey:cfg.apiKey,baseUrl:api.baseUrl||cfg.baseUrl});
74
+ server.registerTool('get_job_batch_receipt',{title:'Read a private local batch receipt',description:'Read only saved accepted Job IDs and uncertain submission intents for the original batch key, API key and endpoint. No API request or GPU creation. Local state is historical, not live release evidence; a missing entry does not prove non-creation. Commands, tokens and signed URLs are never stored.',inputSchema:{batch_key:z.string().min(1).max(80).regex(/^[A-Za-z0-9_-]+$/)},annotations:{readOnlyHint:true,destructiveHint:false,idempotentHint:true,openWorldHint:false}},wrap(async({batch_key})=>ok(await receipts().snapshot(batch_key))));
75
+ server.registerTool('prepare_job_batch',{title:'Review a bounded Job batch',description:'Local, read-only preparation for up to eight Jobs with fresh per-Job quotes and one aggregate cap. No API requests, reservations, installations or automatic execution retries. Retain the immutable plan hash and batch key.',inputSchema:input.shape,annotations:{readOnlyHint:true,destructiveHint:false,idempotentHint:true,openWorldHint:false}},wrap(async p=>ok(prepareJobBatch(p,cfg))));
76
+ server.registerTool('get_job_batch',{title:'Inspect a Job batch',description:'Read-only status and cost summary for explicit Job IDs. Does not retry, cancel, create or claim independent supplier release verification.',inputSchema:{job_ids:z.array(z.string().regex(/^job_[a-f0-9]{32}$/)).min(1).max(8)},annotations:{readOnlyHint:true,destructiveHint:false,idempotentHint:true,openWorldHint:true}},wrap(async({job_ids})=>ok(await getJobBatch(api,job_ids))));
77
+ if(cfg.readOnly)return;
78
+ server.registerTool('run_job_batch',{title:'Submit an approved bounded Job batch',description:'Paid: submit an explicitly approved immutable manifest using existing durable Jobs, each with a quote, budget and deadline. Aggregate worst-case budget is capped. Partial success is possible. On lost responses replay the IDENTICAL manifest/key; never create replacement GPUs or retry failed execution automatically. Persist the returned receipt. No transaction rollback or independent batch scheduler.',inputSchema:{...input.shape,approved_plan_sha256:z.string().regex(/^[a-f0-9]{64}$/),user_confirmed:z.boolean()},annotations:{readOnlyHint:false,destructiveHint:true,idempotentHint:true,openWorldHint:true}},wrap(async p=>ok(await submitJobBatch(api,p,cfg,receipts()))));
79
+ }
@@ -0,0 +1,21 @@
1
+ // 单个与批量提交共用同一字段校验,不能为批量削弱原有门禁。
2
+ import {z} from "zod";
3
+ export function jobInputShape(cfg){return {
4
+ command: z.string().min(1).max(32768).describe("bash command or script to run on the GPU"),
5
+ gpu_model: z.string().optional().describe("GPU model exactly as list_gpus returns it"),
6
+ template: z.string().optional().describe("Template id from list_templates"),
7
+ gpu_count: z.number().int().min(1).max(8).default(1),
8
+ disk_gb: z.number().int().optional().describe("Larger system disk: 100, 200 or 500; omit for the standard disk"),
9
+ workspace_version_id: z.string().optional().describe("Immutable Workspace version to restore before the command; requires enabled persistent storage"),
10
+ model_references: z.array(z.object({name:z.string(),repo_id:z.string(),revision:z.string().regex(/^[0-9a-f]{40}$/),files:z.array(z.object({path:z.string(),size_bytes:z.number().int().positive(),sha256:z.string().regex(/^[0-9a-f]{64}$/).optional()})).min(1).max(32)})).max(4).optional().describe("Pinned public model references from resolve_model_reference. Downloads into $CLOUDGPU_MODEL_DIR/<name> before the command; download time consumes the same GPU budget. No R2 cache or remote repository code execution."),
11
+ outputs: z.object({
12
+ files: z.array(z.string().min(1).max(512)).min(1).max(16),
13
+ max_bytes: z.number().int().min(1).max(104857600).optional(),
14
+ accept_storage_charges: z.boolean().optional(),
15
+ }).strict().optional().describe("Explicit relative output files under $CLOUDGPU_OUTPUT_DIR; no globs, symlinks, hidden or credential files. Default total limit 10 MiB, maximum 100 MiB. Only set accept_storage_charges=true after the user agrees to existing paid storage terms. Inspect output_collection after completion; the GPU is released even if saving fails."),
16
+ quote_id: z.string().optional().describe("Server quote from quote_compute, starting quote_; binds configuration and checks price at submission"),
17
+ max_spend_usd: z.number().positive().describe(`Budget for this job in USD (at most ${cfg.maxSpend})`),
18
+ max_hours: z.number().positive().describe(`Hard time limit in hours (at most ${cfg.maxHours})`),
19
+ user_confirmed: z.boolean().describe("true only if the user explicitly approved the GPU, budget and time limit"),
20
+ idempotency_key: z.string().max(128).optional().describe("Reuse the same value when retrying the same job"),
21
+ };}
@@ -0,0 +1,46 @@
1
+ import importlib.util,json,math,os,pathlib,stat
2
+
3
+ def run(options, project_file, output_dir):
4
+ # Reuse a reviewed image; do not reinstall CUDA/torch or run remote model code.
5
+ if any(importlib.util.find_spec(m) is None for m in ['torch','transformers','peft','safetensors']):
6
+ raise ValueError('Select an image with torch, transformers, peft and safetensors installed')
7
+ import torch
8
+ from transformers import AutoTokenizer,AutoModelForCausalLM
9
+ from peft import LoraConfig,get_peft_model
10
+ if not torch.cuda.is_available(): raise ValueError('A CUDA training environment is required')
11
+ source=project_file(options['dataset_path'],33554432)
12
+ texts=[]
13
+ with source.open(encoding='utf-8') as f:
14
+ for line in f:
15
+ if len(texts)>=1000: raise ValueError('Dataset exceeds 1000 records')
16
+ row=json.loads(line);text=row.get('text') if isinstance(row,dict) else None
17
+ if not isinstance(text,str) or not text.strip() or len(text)>8192: raise ValueError('Use JSONL records with a bounded text field')
18
+ texts.append(text)
19
+ if not texts: raise ValueError('Dataset is empty')
20
+ ref=options['model_reference']
21
+ model_root=pathlib.Path(os.environ['CLOUDGPU_MODEL_DIR']).resolve()/ref['name']
22
+ if not model_root.is_dir() or model_root.is_symlink(): raise ValueError('Pinned model directory is unavailable')
23
+ os.environ['HF_HUB_OFFLINE']='1';os.environ['TRANSFORMERS_OFFLINE']='1';os.environ['HF_HUB_DISABLE_TELEMETRY']='1'
24
+ torch.manual_seed(0)
25
+ 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
27
+ model=AutoModelForCausalLM.from_pretrained(str(model_root),local_files_only=True,trust_remote_code=False,torch_dtype=dtype).to('cuda')
28
+ 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
+ model.train();optimizer=torch.optim.AdamW((p for p in model.parameters() if p.requires_grad),lr=options['learning_rate'])
30
+ loss_value=None
31
+ for step in range(options['steps']):
32
+ batch=tokenizer(texts[step%len(texts)],return_tensors='pt',truncation=True,max_length=options['sequence_length'])
33
+ if batch['input_ids'].shape[-1]<2: raise ValueError('Training record has too few tokens')
34
+ batch={k:v.to('cuda') for k,v in batch.items()};batch['labels']=batch['input_ids'].clone()
35
+ optimizer.zero_grad(set_to_none=True);loss=model(**batch).loss
36
+ loss_value=float(loss.detach().float().cpu())
37
+ if not math.isfinite(loss_value): raise ValueError('Training loss is not finite')
38
+ loss.backward();torch.nn.utils.clip_grad_norm_(model.parameters(),1.0);optimizer.step()
39
+ model.peft_config['default'].base_model_name_or_path=ref['repo_id']
40
+ model.peft_config['default'].revision=ref['revision']
41
+ model.save_pretrained(str(output_dir),safe_serialization=True)
42
+ for name in ['adapter_config.json','adapter_model.safetensors']:
43
+ p=output_dir/name
44
+ if p.is_symlink() or not stat.S_ISREG(p.stat().st_mode) or p.stat().st_size<=0:
45
+ 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']}
@@ -0,0 +1,60 @@
1
+ import hashlib,json,os,pathlib,re,time,urllib.request,urllib.parse,uuid
2
+
3
+ def run(options, project_file, output_dir):
4
+ path=project_file(options['workflow_path'],2097152)
5
+ workflow=json.loads(path.read_text(encoding='utf-8'))
6
+ if not isinstance(workflow,dict) or not 1<=len(workflow)<=128:
7
+ raise ValueError('Use a bounded ComfyUI API-format workflow')
8
+ if any(not isinstance(n,dict) or not isinstance(n.get('class_type'),str) or not isinstance(n.get('inputs'),dict) for n in workflow.values()):
9
+ raise ValueError('Export the workflow in API format')
10
+ # Only the already selected instance's loopback application; never follow redirects.
11
+ class NoRedirect(urllib.request.HTTPRedirectHandler):
12
+ def redirect_request(self,*args,**kwargs):
13
+ raise ValueError('Application redirect refused')
14
+ client=urllib.request.build_opener(urllib.request.ProxyHandler({}),NoRedirect)
15
+ origin='http://127.0.0.1:'+str(options['comfy_port'])
16
+ deadline=time.monotonic()+options['timeout_seconds']
17
+ def request(path,body=None,limit=1048576):
18
+ remaining=deadline-time.monotonic()
19
+ if remaining<=0: raise TimeoutError('Application deadline reached')
20
+ req=urllib.request.Request(origin+path,data=None if body is None else json.dumps(body).encode(),headers={'Content-Type':'application/json'})
21
+ with client.open(req,timeout=min(15,remaining)) as response:
22
+ if response.status!=200: raise ValueError('Application request failed')
23
+ data=response.read(limit+1)
24
+ if len(data)>limit: raise ValueError('Application response exceeds limit')
25
+ return data
26
+ stats=json.loads(request('/system_stats'))
27
+ if not isinstance(stats,dict) or not isinstance(stats.get('system'),dict) or not isinstance(stats.get('devices'),list):
28
+ raise ValueError('ComfyUI readiness not confirmed')
29
+ # One submission only. An uncertain POST is not an excuse to submit a second prompt.
30
+ queued=json.loads(request('/prompt',{'prompt':workflow,'client_id':uuid.uuid4().hex}))
31
+ prompt=queued.get('prompt_id')
32
+ if queued.get('node_errors') or not isinstance(prompt,str) or not re.fullmatch(r'[A-Za-z0-9_-]{1,80}',prompt):
33
+ raise ValueError('Workflow was not accepted')
34
+ while True:
35
+ history=json.loads(request('/history/'+urllib.parse.quote(prompt,safe='')))
36
+ result=history.get(prompt) if isinstance(history,dict) else None
37
+ if result:
38
+ status=result.get('status',{})
39
+ if status.get('status_str')=='error': raise ValueError('Workflow execution failed')
40
+ if status.get('completed') is True: break
41
+ if deadline-time.monotonic()<=1: raise TimeoutError('Application deadline reached')
42
+ time.sleep(min(1,deadline-time.monotonic()))
43
+ images=[]
44
+ for node in (result.get('outputs') or {}).values():
45
+ images.extend(node.get('images',[]) if isinstance(node,dict) else [])
46
+ if len(images)!=options['image_count']:
47
+ raise ValueError('Workflow output count differs from the reviewed task')
48
+ manifest=[]
49
+ for i,image in enumerate(images):
50
+ filename=image.get('filename','');subfolder=image.get('subfolder','')
51
+ if image.get('type')!='output' or not re.fullmatch(r'[A-Za-z0-9_.-]+[.]png',filename) or filename.startswith('.'):
52
+ raise ValueError('Only explicit generated PNG outputs are supported')
53
+ if subfolder and (not re.fullmatch(r'[A-Za-z0-9_/-]+',subfolder) or any(p in ('','.','..') for p in subfolder.split('/'))):
54
+ raise ValueError('Unsafe application output path')
55
+ data=request('/view?'+urllib.parse.urlencode({'filename':filename,'subfolder':subfolder,'type':'output'}),limit=options['image_max_bytes'])
56
+ if not data.startswith(b'\x89PNG\r\n\x1a\n'): raise ValueError('Application output is not PNG data')
57
+ name='image-'+str(i)+'.png'
58
+ with (output_dir/name).open('xb') as f: f.write(data)
59
+ manifest.append({'file':name,'bytes':len(data),'sha256':hashlib.sha256(data).hexdigest()})
60
+ return {'task':'comfyui-workflow','image_count':len(manifest),'files':manifest}
@@ -1,16 +1,19 @@
1
1
  // 只读任务准备:不访问 API、不租 GPU、不安装依赖,也不推断预算或存储同意。
2
2
  import {createHash} from 'node:crypto';
3
3
  import {z} from 'zod';
4
+ import {APPLICATION_TASKS,applicationFields,prepareApplicationRecipe} from './application-recipes.js';
4
5
 
5
6
  const TASKS = [
6
7
  {id:'gpu-check',version:1,title:'Check the allocated GPU',requires_workspace:false,requirements:['Python 3','nvidia-smi'],result_files:['gpu-check.json'],description:'Report visible GPU names and total memory. No model download or benchmark.'},
7
8
  {id:'csv-summary',version:1,title:'Summarize a Workspace CSV',requires_workspace:true,requirements:['Python 3','Immutable Workspace version','UTF-8 CSV, at most 32 MiB'],result_files:['csv-summary.json'],description:'Count rows, columns and inconsistent row widths without logging cell values. Usually a CPU task; prefer local execution when a GPU is unnecessary.'},
8
9
  {id:'workspace-python',version:1,title:'Run a reviewed Workspace Python script',requires_workspace:true,requirements:['Python 3','Immutable Workspace version','User-reviewed Python entrypoint and dependencies already prepared'],result_files:[],description:'Reuse your own inference or training script. Does not install packages, supply a model, or certify application compatibility.'},
9
10
  ];
11
+ TASKS.push(...APPLICATION_TASKS);
10
12
  const id=z.enum(TASKS.map(t=>t.id));
11
13
  const relative=z.string().min(1).max(240).regex(/^[A-Za-z0-9_][A-Za-z0-9_./-]*$/).refine(p=>p.split('/').every(s=>s && s!=='.' && s!=='..' && !s.startsWith('.')),'Use a relative path without traversal or hidden components');
12
14
  const schema=z.object({
13
15
  template_id:id,
16
+ ...applicationFields,
14
17
  workspace_version_id:z.string().regex(/^wsv_[0-9a-f]{32}$/).optional(),
15
18
  input_path:relative.optional(),
16
19
  entrypoint:relative.refine(p=>p.endsWith('.py'),'Entrypoint must be a Python file').optional(),
@@ -68,11 +71,13 @@ os.execv(sys.executable,[sys.executable,str(p)])
68
71
 
69
72
  export function listTaskTemplates(){
70
73
  return {preparation_only:true,starts_gpu:false,templates:structuredClone(TASKS),
71
- note:'Preparation costs no GPU time. Running a Job costs money and requires a fresh quote, explicit GPU/budget/time approval and a new idempotency key. No automatic retry or dependency installation. ComfyUI workflow preparation remains available through prepare_model_deployment; these are not prevalidated ComfyUI or LoRA end-to-end recipes.'};
74
+ note:'Preparation costs no GPU time. Running a Job costs money and requires a fresh quote, explicit GPU/budget/time approval and a new idempotency key. No automatic retry or dependency installation. Application execution recipes are experimental, reuse prepared environments and require persistent outputs. They are not real-GPU or model-quality acceptance. Image/SDXL LoRA is not included.'};
72
75
  }
73
76
 
74
77
  export function prepareTaskTemplate(raw){
75
78
  const p=schema.parse(raw),t=TASKS.find(t=>t.id===p.template_id);
79
+ const application=prepareApplicationRecipe(p);if(application)return application;
80
+ if(Object.keys(applicationFields).some(k=>p[k]!==undefined))throw Error('Application-specific fields do not belong to this task');
76
81
  if(t.requires_workspace && !p.workspace_version_id)throw Error('Select an immutable Workspace version before preparing this task');
77
82
  if(!t.requires_workspace && p.workspace_version_id)throw Error('gpu-check does not use a Workspace');
78
83
  if(p.template_id==='csv-summary' ? !p.input_path : p.input_path!==undefined)throw Error('input_path is required only for csv-summary');