cloudgpu-mcp 0.9.0 → 0.11.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 +32 -0
- package/package.json +1 -1
- package/rules/cloudgpu.md +6 -0
- package/src/application-recipes.js +27 -0
- package/src/index.js +10 -21
- package/src/job-batches.js +65 -0
- package/src/job-input.js +21 -0
- package/src/recipes/causal-lora.py +46 -0
- package/src/recipes/comfy-workflow.py +60 -0
- package/src/task-templates.js +114 -0
package/README.md
CHANGED
|
@@ -305,3 +305,35 @@ After completion, inspect `get_job.output_collection`: only `succeeded` confirms
|
|
|
305
305
|
### Job log snapshot safety
|
|
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
|
+
|
|
309
|
+
## Reusable task drafts (D16)
|
|
310
|
+
|
|
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
|
+
|
|
313
|
+
```json
|
|
314
|
+
{"template_id":"gpu-check","persist_results":false}
|
|
315
|
+
```
|
|
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 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
|
+
|
|
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.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cloudgpu-mcp",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.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
|
+
}
|
package/src/index.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
import {registerBatchTools} from './job-batches.js';
|
|
3
|
+
import {jobInputShape} from './job-input.js';
|
|
4
|
+
import {registerTaskTemplateTools} from './task-templates.js';
|
|
2
5
|
import {resolveModelReference} from "./model-references.js";
|
|
3
6
|
// CloudGPU MCP server (stdio). Lets an AI coding assistant rent a GPU on cloudgpu.app, run a job,
|
|
4
7
|
// fetch the results and shut the GPU down.
|
|
@@ -32,7 +35,7 @@ import {
|
|
|
32
35
|
shq,
|
|
33
36
|
} from "./guards.js";
|
|
34
37
|
|
|
35
|
-
const VERSION = "0.
|
|
38
|
+
const VERSION = "0.11.0";
|
|
36
39
|
const HERE = path.dirname(fileURLToPath(import.meta.url));
|
|
37
40
|
const RULES = fs.readFileSync(path.join(HERE, "..", "rules", "cloudgpu.md"), "utf8");
|
|
38
41
|
|
|
@@ -51,13 +54,15 @@ function num(v, d) {
|
|
|
51
54
|
return Number.isFinite(n) && n > 0 ? n : d;
|
|
52
55
|
}
|
|
53
56
|
|
|
54
|
-
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}` });
|
|
55
58
|
const pool = new SshPool();
|
|
56
59
|
const quotes = new Quotes();
|
|
57
60
|
// Instances this session touched: did we run anything, did we download anything.
|
|
58
61
|
const session = new Map();
|
|
59
62
|
|
|
60
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.
|
|
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.
|
|
61
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.
|
|
62
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.
|
|
63
68
|
Jobs: for a batch task (one command that writes its results somewhere), run_job does the whole thing on CloudGPU's servers: start a GPU, run the command, destroy the GPU, with the same per-job budget and time limit. Check it with get_job. The GPU disk is erased on release. When automatic_output_collection is enabled, declare explicit outputs and write them under $CLOUDGPU_OUTPUT_DIR; inspect output_collection and download ready file IDs afterward. Otherwise upload results yourself or print them; get_job returns the last ~4 KB of logs. Saving output shares the same GPU budget and existing storage quota/fees; never infer storage-fee acceptance.
|
|
@@ -65,6 +70,8 @@ No API key yet? connect_account gives the user a link to approve this assistant
|
|
|
65
70
|
The same API key also works for the OpenAI-compatible model API at ${cfg.baseUrl}/v1 (see list_models).`;
|
|
66
71
|
|
|
67
72
|
const server = new McpServer({ name: "cloudgpu", version: VERSION }, { instructions: INSTRUCTIONS });
|
|
73
|
+
registerTaskTemplateTools(server, {wrap, ok});
|
|
74
|
+
registerBatchTools(server,{api,cfg,wrap,ok});
|
|
68
75
|
registerStorageTools(server, { api, wrap, ok, fail, readOnly: cfg.readOnly, expandHome });
|
|
69
76
|
registerJobResultTools(server, { api, wrap, ok, readOnly: cfg.readOnly });
|
|
70
77
|
|
|
@@ -507,25 +514,7 @@ if (!cfg.readOnly) {
|
|
|
507
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. " +
|
|
508
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. " +
|
|
509
516
|
"Requires the user's explicit yes on the GPU, budget and time limit.",
|
|
510
|
-
inputSchema:
|
|
511
|
-
command: z.string().min(1).max(32768).describe("bash command or script to run on the GPU"),
|
|
512
|
-
gpu_model: z.string().optional().describe("GPU model exactly as list_gpus returns it"),
|
|
513
|
-
template: z.string().optional().describe("Template id from list_templates"),
|
|
514
|
-
gpu_count: z.number().int().min(1).max(8).default(1),
|
|
515
|
-
disk_gb: z.number().int().optional().describe("Larger system disk: 100, 200 or 500; omit for the standard disk"),
|
|
516
|
-
workspace_version_id: z.string().optional().describe("Immutable Workspace version to restore before the command; requires enabled persistent storage"),
|
|
517
|
-
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."),
|
|
518
|
-
outputs: z.object({
|
|
519
|
-
files: z.array(z.string().min(1).max(512)).min(1).max(16),
|
|
520
|
-
max_bytes: z.number().int().min(1).max(104857600).optional(),
|
|
521
|
-
accept_storage_charges: z.boolean().optional(),
|
|
522
|
-
}).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."),
|
|
523
|
-
quote_id: z.string().optional().describe("Server quote from quote_compute, starting quote_; binds configuration and checks price at submission"),
|
|
524
|
-
max_spend_usd: z.number().positive().describe(`Budget for this job in USD (at most ${cfg.maxSpend})`),
|
|
525
|
-
max_hours: z.number().positive().describe(`Hard time limit in hours (at most ${cfg.maxHours})`),
|
|
526
|
-
user_confirmed: z.boolean().describe("true only if the user explicitly approved the GPU, budget and time limit"),
|
|
527
|
-
idempotency_key: z.string().max(128).optional().describe("Reuse the same value when retrying the same job"),
|
|
528
|
-
},
|
|
517
|
+
inputSchema: jobInputShape(cfg),
|
|
529
518
|
annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: true, openWorldHint: true },
|
|
530
519
|
},
|
|
531
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,65 @@
|
|
|
1
|
+
// D17第一阶段:复用现有持久化Job队列。部分提交可恢复,不伪装成原子事务。
|
|
2
|
+
import {createHash} from 'node:crypto';
|
|
3
|
+
import {z} from 'zod';
|
|
4
|
+
import {jobInputShape} from './job-input.js';
|
|
5
|
+
import {checkCommand} from './guards.js';
|
|
6
|
+
const sha=value=>createHash('sha256').update(value).digest('hex');
|
|
7
|
+
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);}
|
|
8
|
+
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'));}
|
|
9
|
+
function schema(cfg){return z.object({
|
|
10
|
+
batch_key:z.string().min(1).max(80).regex(/^[A-Za-z0-9_-]+$/),
|
|
11
|
+
total_budget_usd:z.number().positive(),
|
|
12
|
+
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),
|
|
13
|
+
}).strict();}
|
|
14
|
+
function bodyOf(item){const {max_hours,...rest}=item;return {...rest,max_minutes:Math.ceil(max_hours*60)};}
|
|
15
|
+
export function prepareJobBatch(raw,cfg){
|
|
16
|
+
const p=schema(cfg).parse(raw);const cap=micros(p.total_budget_usd);
|
|
17
|
+
if(cap>micros(cfg.maxSpend))throw Error('Batch total exceeds this client spending cap');
|
|
18
|
+
let reserved=0n;
|
|
19
|
+
const items=p.jobs.map((job,index)=>{
|
|
20
|
+
if(job.max_hours>cfg.maxHours||micros(job.max_spend_usd)>micros(cfg.maxSpend))throw Error('Job exceeds client budget or time cap');
|
|
21
|
+
const refused=checkCommand(job.command);if(refused)throw Error(refused);
|
|
22
|
+
reserved+=micros(job.max_spend_usd);
|
|
23
|
+
return {index,idempotency_key:'batch_'+sha(p.batch_key+':'+index),body:bodyOf(job)};
|
|
24
|
+
});
|
|
25
|
+
if(reserved>cap)throw Error('Sum of per-Job maximum spending exceeds the approved batch budget');
|
|
26
|
+
const plan_sha256=sha(stable(p));
|
|
27
|
+
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,
|
|
28
|
+
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.',
|
|
29
|
+
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.'};
|
|
30
|
+
}
|
|
31
|
+
export async function submitJobBatch(api,raw,cfg){
|
|
32
|
+
const {approved_plan_sha256,user_confirmed,...input}=raw;
|
|
33
|
+
if(user_confirmed!==true)throw Error('Obtain explicit approval for all Jobs and the aggregate budget before submission');
|
|
34
|
+
const plan=prepareJobBatch(input,cfg);
|
|
35
|
+
if(approved_plan_sha256!==plan.plan_sha256)throw Error('The approved batch manifest changed; review it again before submitting');
|
|
36
|
+
const results=[];
|
|
37
|
+
for(const item of plan.items){
|
|
38
|
+
try{
|
|
39
|
+
const job=await api.createJob(item.body,item.idempotency_key);
|
|
40
|
+
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');
|
|
41
|
+
results.push({index:item.index,idempotency_key:item.idempotency_key,job_id:job.id,status:job.status});
|
|
42
|
+
if(job.status==='needs_attention'||job.status==='stopping')return receipt('reconciliation_required',results,item.index+1);
|
|
43
|
+
}catch{
|
|
44
|
+
// Even a server error may follow an accepted create. Never turn uncertainty into another GPU.
|
|
45
|
+
results.push({index:item.index,idempotency_key:item.idempotency_key,job_id:null,status:'submission_uncertain'});
|
|
46
|
+
return receipt('submission_stopped',results,item.index+1);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return receipt('submitted',results,plan.items.length);
|
|
50
|
+
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:plan.items.slice(next).map(i=>({index:i.index,idempotency_key:i.idempotency_key})),execution_retries:0,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.'};}
|
|
51
|
+
}
|
|
52
|
+
export async function getJobBatch(api,ids){
|
|
53
|
+
z.array(z.string().regex(/^job_[a-f0-9]{32}$/)).min(1).max(8).parse(ids);
|
|
54
|
+
if(new Set(ids).size!==ids.length)throw Error('Job IDs must be distinct');
|
|
55
|
+
const items=[];
|
|
56
|
+
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});}}
|
|
57
|
+
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.'};
|
|
58
|
+
}
|
|
59
|
+
export function registerBatchTools(server,{api,cfg,wrap,ok}){
|
|
60
|
+
const input=schema(cfg);
|
|
61
|
+
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))));
|
|
62
|
+
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))));
|
|
63
|
+
if(cfg.readOnly)return;
|
|
64
|
+
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))));
|
|
65
|
+
}
|
package/src/job-input.js
ADDED
|
@@ -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}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// 只读任务准备:不访问 API、不租 GPU、不安装依赖,也不推断预算或存储同意。
|
|
2
|
+
import {createHash} from 'node:crypto';
|
|
3
|
+
import {z} from 'zod';
|
|
4
|
+
import {APPLICATION_TASKS,applicationFields,prepareApplicationRecipe} from './application-recipes.js';
|
|
5
|
+
|
|
6
|
+
const TASKS = [
|
|
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.'},
|
|
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.'},
|
|
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.'},
|
|
10
|
+
];
|
|
11
|
+
TASKS.push(...APPLICATION_TASKS);
|
|
12
|
+
const id=z.enum(TASKS.map(t=>t.id));
|
|
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');
|
|
14
|
+
const schema=z.object({
|
|
15
|
+
template_id:id,
|
|
16
|
+
...applicationFields,
|
|
17
|
+
workspace_version_id:z.string().regex(/^wsv_[0-9a-f]{32}$/).optional(),
|
|
18
|
+
input_path:relative.optional(),
|
|
19
|
+
entrypoint:relative.refine(p=>p.endsWith('.py'),'Entrypoint must be a Python file').optional(),
|
|
20
|
+
persist_results:z.boolean().default(false),
|
|
21
|
+
output_files:z.array(relative).min(1).max(16).optional(),
|
|
22
|
+
accept_storage_charges:z.boolean().default(false),
|
|
23
|
+
}).strict();
|
|
24
|
+
const programs={
|
|
25
|
+
'gpu-check':`import json,subprocess
|
|
26
|
+
r=subprocess.run(['nvidia-smi','--query-gpu=name,memory.total','--format=csv,noheader,nounits'],capture_output=True,text=True,timeout=15)
|
|
27
|
+
if r.returncode: raise RuntimeError('GPU query failed; no automatic retry')
|
|
28
|
+
gpus=[]
|
|
29
|
+
for line in r.stdout.splitlines():
|
|
30
|
+
name,memory=line.rsplit(',',1)
|
|
31
|
+
gpus.append({'name':name.strip(),'memory_mib':int(memory.strip())})
|
|
32
|
+
if not gpus: raise RuntimeError('No visible GPU; no automatic retry')
|
|
33
|
+
result={'gpus':gpus}
|
|
34
|
+
`,
|
|
35
|
+
'csv-summary':`import csv,json,os,pathlib,stat
|
|
36
|
+
root=pathlib.Path.cwd().resolve()
|
|
37
|
+
p=root.joinpath(options['input_path'])
|
|
38
|
+
for component in p.relative_to(root).parts:
|
|
39
|
+
root=root/component
|
|
40
|
+
if root.is_symlink(): raise ValueError('Input must not use symlinks')
|
|
41
|
+
flags=os.O_RDONLY|getattr(os,'O_NOFOLLOW',0)|os.O_NONBLOCK
|
|
42
|
+
fd=os.open(str(p),flags)
|
|
43
|
+
meta=os.fstat(fd)
|
|
44
|
+
if not stat.S_ISREG(meta.st_mode) or meta.st_size>33554432:
|
|
45
|
+
os.close(fd)
|
|
46
|
+
raise ValueError('Input must be a regular CSV of at most 32 MiB')
|
|
47
|
+
csv.field_size_limit(1048576)
|
|
48
|
+
with os.fdopen(fd,'r',encoding='utf-8',newline='') as f:
|
|
49
|
+
reader=csv.reader(f)
|
|
50
|
+
header=next(reader,None)
|
|
51
|
+
columns=len(header) if header is not None else 0
|
|
52
|
+
rows=0
|
|
53
|
+
inconsistent=0
|
|
54
|
+
for row in reader:
|
|
55
|
+
rows+=1
|
|
56
|
+
inconsistent+=int(len(row)!=columns)
|
|
57
|
+
if rows>1000000: raise ValueError('CSV row limit exceeded')
|
|
58
|
+
result={'rows':rows,'columns':columns,'inconsistent_rows':inconsistent,'input_bytes':meta.st_size,'header_assumed':True}
|
|
59
|
+
`,
|
|
60
|
+
'workspace-python':`import os,pathlib,stat,sys
|
|
61
|
+
root=pathlib.Path.cwd().resolve()
|
|
62
|
+
p=root.joinpath(options['entrypoint'])
|
|
63
|
+
check=root
|
|
64
|
+
for component in p.relative_to(root).parts:
|
|
65
|
+
check=check/component
|
|
66
|
+
if check.is_symlink(): raise ValueError('Entrypoint must not use symlinks')
|
|
67
|
+
if not stat.S_ISREG(p.stat().st_mode): raise ValueError('Entrypoint must be a regular file')
|
|
68
|
+
os.execv(sys.executable,[sys.executable,str(p)])
|
|
69
|
+
`,
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export function listTaskTemplates(){
|
|
73
|
+
return {preparation_only:true,starts_gpu:false,templates:structuredClone(TASKS),
|
|
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.'};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function prepareTaskTemplate(raw){
|
|
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');
|
|
81
|
+
if(t.requires_workspace && !p.workspace_version_id)throw Error('Select an immutable Workspace version before preparing this task');
|
|
82
|
+
if(!t.requires_workspace && p.workspace_version_id)throw Error('gpu-check does not use a Workspace');
|
|
83
|
+
if(p.template_id==='csv-summary' ? !p.input_path : p.input_path!==undefined)throw Error('input_path is required only for csv-summary');
|
|
84
|
+
if(p.template_id==='workspace-python' ? !p.entrypoint : p.entrypoint!==undefined)throw Error('entrypoint is required only for workspace-python');
|
|
85
|
+
if(p.accept_storage_charges && !p.persist_results)throw Error('Storage consent is only relevant when persisting results');
|
|
86
|
+
if(p.output_files && (!p.persist_results || p.template_id!=='workspace-python'))throw Error('Explicit output_files are only for persistent workspace-python results');
|
|
87
|
+
if(p.persist_results && p.template_id==='workspace-python' && !p.output_files)throw Error('List the exact result files your reviewed script writes under CLOUDGPU_OUTPUT_DIR');
|
|
88
|
+
if(p.output_files && new Set(p.output_files).size!==p.output_files.length)throw Error('Result paths must be distinct');
|
|
89
|
+
const options={...(p.input_path?{input_path:p.input_path}:{}),...(p.entrypoint?{entrypoint:p.entrypoint}:{})};
|
|
90
|
+
let program='import json\noptions=json.loads('+JSON.stringify(JSON.stringify(options))+')\n'+programs[p.template_id];
|
|
91
|
+
if(p.template_id!=='workspace-python'){
|
|
92
|
+
program+=`import os,pathlib\nencoded=json.dumps(result,sort_keys=True)\n`;
|
|
93
|
+
if(p.persist_results)program+=`dest=pathlib.Path(os.environ['CLOUDGPU_OUTPUT_DIR'])/${JSON.stringify(t.result_files[0])}\nwith dest.open('x',encoding='utf-8') as f: f.write(encoded+'\\n')\n`;
|
|
94
|
+
program+="print(encoded)\n";
|
|
95
|
+
}
|
|
96
|
+
const command="set -eu\numask 077\npython3 - <<'CGPU_TASK_TEMPLATE'\n"+program+"CGPU_TASK_TEMPLATE\n";
|
|
97
|
+
const draft={command,...(p.workspace_version_id?{workspace_version_id:p.workspace_version_id}:{})};
|
|
98
|
+
if(p.persist_results)draft.outputs={files:p.output_files||t.result_files,max_bytes:10485760,accept_storage_charges:p.accept_storage_charges};
|
|
99
|
+
return {preparation_only:true,starts_gpu:false,template_id:t.id,template_version:t.version,
|
|
100
|
+
command_sha256:createHash('sha256').update(command).digest('hex'),run_job_draft:draft,
|
|
101
|
+
requirements:t.requirements,missing_fields:['gpu_model or quote_id','max_spend_usd','max_hours','user_confirmed','idempotency_key'],
|
|
102
|
+
next_steps:['Review the task, Workspace expiry and required dependencies; prefer local CPU execution for CSV-only work.',
|
|
103
|
+
'Check live compute capabilities and request a fresh quote for a compatible GPU. Do not assume image compatibility from this draft.',
|
|
104
|
+
'Ask the user to approve GPU, spending cap and time limit. Separately obtain paid-storage consent if needed; scoped keys may not enable paid storage.',
|
|
105
|
+
'Call run_job only after approval with a new idempotency key; retain that key for transport retries of the SAME submission.',
|
|
106
|
+
'Poll get_job until terminal, inspect output_collection if requested and verify GPU release. Do not automatically retry failed tasks.'],
|
|
107
|
+
storage_note:p.persist_results?'Existing storage eligibility, quota, retention and charges apply; transfer uses the same GPU time budget. Explicit result paths are not a content-safety guarantee.':'No persistent storage requested; only the ended Job log tail remains available. GPU disk is erased at release.'};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function registerTaskTemplateTools(server,{wrap,ok}){
|
|
111
|
+
const annotations={readOnlyHint:true,destructiveHint:false,idempotentHint:true,openWorldHint:false};
|
|
112
|
+
server.registerTool('list_task_templates',{title:'List reusable Job task drafts',description:'Public, local and read-only. Inspect small task recipes and requirements before quoting. Does not call an API, rent a GPU, install dependencies or reserve capacity.',inputSchema:{},annotations},wrap(async()=>ok(listTaskTemplates())));
|
|
113
|
+
server.registerTool('prepare_task_template',{title:'Prepare a Job task draft',description:'Local preparation only. Returns a command and optional Workspace/output fields; never submits or spends money. Quote, explicit GPU/budget/time approval and existing storage consent are still required by run_job. No guaranteed application compatibility or automatic retry.',inputSchema:schema.shape,annotations},wrap(async input=>ok(prepareTaskTemplate(input))));
|
|
114
|
+
}
|