cloudgpu-mcp 0.8.0 → 0.10.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 +22 -0
- package/package.json +1 -1
- package/src/api.js +9 -0
- package/src/index.js +13 -4
- package/src/job-results.js +26 -4
- package/src/task-templates.js +109 -0
package/README.md
CHANGED
|
@@ -295,3 +295,25 @@ This branch adds `diagnose_job` (read-only) and `save_job_output` (explicit stor
|
|
|
295
295
|
`diagnose_job` reports the error category, platform instance status and whether platform charges are final. It does not prove supplier release, infer that command failures are always user errors, or automatically rerun work. `needs_attention` requires resource reconciliation before a new task. A rerun requires a fresh quote, explicit approval and a new idempotency key.
|
|
296
296
|
|
|
297
297
|
Registry configuration allows discovery and connect-link approval without pre-entering a key. A tool-enabled MCP client is still required; a link does not install a client or grant unlimited spending. No new fixed infrastructure or model storage is introduced.
|
|
298
|
+
|
|
299
|
+
### Optional Job output collection (requires backend capability)
|
|
300
|
+
|
|
301
|
+
Call `run_job` with `outputs: {"files":["metrics.json"],"max_bytes":1048576}` and a command that writes to `"$CLOUDGPU_OUTPUT_DIR/metrics.json"`. Files are relative to this dedicated directory; the platform does not scan the disk. The server refuses unsupported backends rather than silently starting a Job without saving results. GPU download/execution/upload time shares the same time and spend limits.
|
|
302
|
+
|
|
303
|
+
After completion, inspect `get_job.output_collection`: only `succeeded` confirms collection. Each saved entry gives `file_id`, `sha256`, `size_bytes` and status; use existing file download tools. Partial failure may retain ready files, but is not overall success. Cancellation, budget exhaustion, instance loss or interrupted collection can prevent saving; GPU release still proceeds with no automatic re-run. Files follow the existing account quota, storage pricing and 30-day retention. `accept_storage_charges` defaults false and requires explicit user agreement to paid storage. The 10 MiB default/100 MiB maximum is a transfer bound, not a new user quota. File-name protections cannot guarantee that user-selected file contents contain no secrets.
|
|
304
|
+
|
|
305
|
+
### Job log snapshot safety
|
|
306
|
+
|
|
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 first slice)
|
|
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 ComfyUI model/workflow preparation, keep using `prepare_model_deployment`; complete ComfyUI and LoRA batch recipes are not included in this slice.
|
|
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.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cloudgpu-mcp",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.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/src/api.js
CHANGED
|
@@ -106,9 +106,18 @@ export class CloudGpuApi {
|
|
|
106
106
|
// ── jobs: start a GPU, run one command, destroy the GPU (server-side) ──
|
|
107
107
|
createJob(body, idempotencyKey) {
|
|
108
108
|
if (!idempotencyKey) throw new Error("createJob needs an idempotency key");
|
|
109
|
+
if (body.outputs) return this.createOutputJob(body,idempotencyKey);
|
|
109
110
|
if (body.model_references?.length) return this.createModelJob(body,idempotencyKey);
|
|
110
111
|
return this.request("POST", "/v1/jobs", body, { idempotencyKey });
|
|
111
112
|
}
|
|
113
|
+
async createOutputJob(body,idempotencyKey) {
|
|
114
|
+
const caps=await this.request("GET","/v1/public/compute-capabilities",undefined,{noAuth:true});
|
|
115
|
+
if(caps.automatic_output_collection!==true || caps.storage!==true)
|
|
116
|
+
throw new ApiError(503,"outputs_unavailable","Automatic output collection is not enabled; no Job was submitted. Do not silently remove outputs.");
|
|
117
|
+
if(body.model_references?.length && caps.model_references!==true)
|
|
118
|
+
throw new ApiError(503,"model_references_unavailable","Pinned model loading is not enabled; no Job was submitted.");
|
|
119
|
+
return this.request("POST","/v1/jobs",body,{idempotencyKey});
|
|
120
|
+
}
|
|
112
121
|
async createModelJob(body,idempotencyKey) {
|
|
113
122
|
const caps=await this.request("GET","/v1/public/compute-capabilities",undefined,{noAuth:true});
|
|
114
123
|
if(caps.model_references!==true) throw new ApiError(503,"model_references_unavailable","Backend has not enabled pinned model loading; no Job was submitted.");
|
package/src/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
import {registerTaskTemplateTools} from './task-templates.js';
|
|
2
3
|
import {resolveModelReference} from "./model-references.js";
|
|
3
4
|
// CloudGPU MCP server (stdio). Lets an AI coding assistant rent a GPU on cloudgpu.app, run a job,
|
|
4
5
|
// fetch the results and shut the GPU down.
|
|
@@ -32,7 +33,7 @@ import {
|
|
|
32
33
|
shq,
|
|
33
34
|
} from "./guards.js";
|
|
34
35
|
|
|
35
|
-
const VERSION = "0.
|
|
36
|
+
const VERSION = "0.10.0";
|
|
36
37
|
const HERE = path.dirname(fileURLToPath(import.meta.url));
|
|
37
38
|
const RULES = fs.readFileSync(path.join(HERE, "..", "rules", "cloudgpu.md"), "utf8");
|
|
38
39
|
|
|
@@ -58,13 +59,15 @@ const quotes = new Quotes();
|
|
|
58
59
|
const session = new Map();
|
|
59
60
|
|
|
60
61
|
const INSTRUCTIONS = `CloudGPU (cloudgpu.app) rents GPUs billed per minute from a prepaid balance.
|
|
62
|
+
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
63
|
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
64
|
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
|
-
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
|
|
65
|
+
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.
|
|
64
66
|
No API key yet? connect_account gives the user a link to approve this assistant with a spending limit; then finish_connect stores the key on this computer.
|
|
65
67
|
The same API key also works for the OpenAI-compatible model API at ${cfg.baseUrl}/v1 (see list_models).`;
|
|
66
68
|
|
|
67
69
|
const server = new McpServer({ name: "cloudgpu", version: VERSION }, { instructions: INSTRUCTIONS });
|
|
70
|
+
registerTaskTemplateTools(server, {wrap, ok});
|
|
68
71
|
registerStorageTools(server, { api, wrap, ok, fail, readOnly: cfg.readOnly, expandHome });
|
|
69
72
|
registerJobResultTools(server, { api, wrap, ok, readOnly: cfg.readOnly });
|
|
70
73
|
|
|
@@ -505,7 +508,7 @@ if (!cfg.readOnly) {
|
|
|
505
508
|
description:
|
|
506
509
|
"Costs money. CloudGPU starts a GPU, runs one bash command on it, then destroys the GPU, all on its servers, so it finishes even if this assistant disconnects. " +
|
|
507
510
|
"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
|
-
"The disk is erased when the job ends
|
|
511
|
+
"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
512
|
"Requires the user's explicit yes on the GPU, budget and time limit.",
|
|
510
513
|
inputSchema: {
|
|
511
514
|
command: z.string().min(1).max(32768).describe("bash command or script to run on the GPU"),
|
|
@@ -515,6 +518,11 @@ if (!cfg.readOnly) {
|
|
|
515
518
|
disk_gb: z.number().int().optional().describe("Larger system disk: 100, 200 or 500; omit for the standard disk"),
|
|
516
519
|
workspace_version_id: z.string().optional().describe("Immutable Workspace version to restore before the command; requires enabled persistent storage"),
|
|
517
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."),
|
|
518
526
|
quote_id: z.string().optional().describe("Server quote from quote_compute, starting quote_; binds configuration and checks price at submission"),
|
|
519
527
|
max_spend_usd: z.number().positive().describe(`Budget for this job in USD (at most ${cfg.maxSpend})`),
|
|
520
528
|
max_hours: z.number().positive().describe(`Hard time limit in hours (at most ${cfg.maxHours})`),
|
|
@@ -523,7 +531,7 @@ if (!cfg.readOnly) {
|
|
|
523
531
|
},
|
|
524
532
|
annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: true, openWorldHint: true },
|
|
525
533
|
},
|
|
526
|
-
wrap(async ({ command, gpu_model, template, gpu_count = 1, disk_gb, workspace_version_id, model_references, quote_id, max_spend_usd, max_hours, user_confirmed, idempotency_key }) => {
|
|
534
|
+
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 }) => {
|
|
527
535
|
if (user_confirmed !== true) {
|
|
528
536
|
return fail("Not started: tell the user the GPU, budget and time limit, wait for an explicit yes, then call again with user_confirmed=true.");
|
|
529
537
|
}
|
|
@@ -542,6 +550,7 @@ if (!cfg.readOnly) {
|
|
|
542
550
|
if (template) body.template = template;
|
|
543
551
|
if (disk_gb) body.disk_gb = disk_gb;
|
|
544
552
|
if (workspace_version_id) body.workspace_version_id = workspace_version_id;
|
|
553
|
+
if (outputs) body.outputs = outputs;
|
|
545
554
|
if (model_references?.length) body.model_references = model_references;
|
|
546
555
|
if (quote_id) body.quote_id = quote_id;
|
|
547
556
|
const job = await api.createJob(body, key);
|
package/src/job-results.js
CHANGED
|
@@ -9,11 +9,33 @@ const terminal=new Set(['succeeded','failed','cancelled']);
|
|
|
9
9
|
const idSchema=z.string().regex(/^job_[a-f0-9]{32}$/);
|
|
10
10
|
const locks=new Map();
|
|
11
11
|
function assertId(id){if(!/^job_[a-f0-9]{32}$/.test(id))throw Error('Invalid Job ID.');}
|
|
12
|
+
// Inspect bounded decoded views but preserve the original safe log byte-for-byte.
|
|
13
|
+
// This is a conservative refusal filter, not proof that arbitrary log content is secret-free.
|
|
14
|
+
const credentialField=/(?:^|[^A-Za-z0-9_])["']?[A-Za-z0-9_.-]*(?:password|passwd|pwd|passphrase|secret|client[_-]?secret|secret[_-]?(?:access[_-]?)?key|api[_-]?key|access[_-]?key(?:[_-]?id)?|aws[_-]?(?:access[_-]?key[_-]?id|secret[_-]?access[_-]?key|session[_-]?token)|(?:access|refresh|id|auth|session|csrf|xsrf)[_-]?token|token|authorization|proxy[_-]?authorization|cookie|set[_-]?cookie|x[_-]?(?:api[_-]?key|auth[_-]?token))["']?\s*[:=]\s*(?=\S)/i;
|
|
15
|
+
const credentialForms=[credentialField,/-----BEGIN [^-]*PRIVATE KEY-----/i,/\bsk-[A-Za-z0-9_-]{16,}/,/\b(?:Bearer|Basic)\s+[A-Za-z0-9_+/.=-]{8,}/i,/\beyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\b/,/https?:\/\/[^\s"'<>]*\?[^\s"'<>]*/i,/https?:\/\/[^/\s"'<>@]+@/i];
|
|
16
|
+
function decodedTailView(text){
|
|
17
|
+
const named={quot:'"',apos:"'",colon:':',equals:'=',amp:'&',sol:'/',quest:'?'};
|
|
18
|
+
return text.normalize('NFKC')
|
|
19
|
+
.replace(/\x1b\[[0-?]*[ -/]*[@-~]/g,'')
|
|
20
|
+
.replace(/[\u200b-\u200f\u202a-\u202e\u2060-\u206f\ufeff]/g,'')
|
|
21
|
+
.replace(/\\u([0-9a-f]{4})|\\x([0-9a-f]{2})|\\(["'\\/:=])/gi,(_m,u,x,c)=>u?String.fromCharCode(parseInt(u,16)):x?String.fromCharCode(parseInt(x,16)):c)
|
|
22
|
+
.replace(/(?:%[0-9a-f]{2})+/gi,part=>Buffer.from(part.replaceAll('%',''),'hex').toString('utf8'))
|
|
23
|
+
.replace(/&#(x[0-9a-f]+|[0-9]+);|&(quot|apos|colon|equals|amp|sol|quest);/gi,(_m,n,name)=>{
|
|
24
|
+
if(name)return named[name.toLowerCase()];const value=n[0].toLowerCase()==='x'?parseInt(n.slice(1),16):parseInt(n,10);
|
|
25
|
+
if(value>0x10ffff)throw Error('Output normalization is unsafe; nothing was uploaded.');return String.fromCodePoint(value);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
12
28
|
export function checkedTail(text,apiKey){
|
|
13
29
|
if(typeof text!=='string'||Buffer.byteLength(text)>16384)throw Error('Job output tail exceeds the bounded snapshot limit.');
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
30
|
+
let view=text;
|
|
31
|
+
for(let depth=0;depth<5;depth++){
|
|
32
|
+
if((apiKey&&apiKey.length>=4&&view.includes(apiKey))||credentialForms.some(re=>re.test(view)))throw Error('Output may contain credentials or a signed URL. Sanitize it before saving; nothing was uploaded.');
|
|
33
|
+
const next=decodedTailView(view);
|
|
34
|
+
if(Buffer.byteLength(next)>65536)throw Error('Output normalization exceeds the bounded snapshot limit.');
|
|
35
|
+
if(next===view)return text;
|
|
36
|
+
view=next;
|
|
37
|
+
}
|
|
38
|
+
throw Error('Output normalization is too deeply encoded; nothing was uploaded.');
|
|
17
39
|
}
|
|
18
40
|
export async function diagnoseJob(api,id){
|
|
19
41
|
assertId(id);const job=await api.job(id);const code=job.error?.code||null;
|
|
@@ -50,5 +72,5 @@ export function registerJobResultTools(server,{api,wrap,ok,readOnly}){
|
|
|
50
72
|
const storage=new PersistentStorage(api);
|
|
51
73
|
server.registerTool('diagnose_job',{title:'Inspect Job failure and cost state',description:'Read-only Job diagnosis and observed platform instance state. Never creates, stops or retries a GPU. Does not claim supplier release verification or classify every command failure as user error.',inputSchema:{job_id:idSchema},annotations:{readOnlyHint:true,openWorldHint:true}},wrap(async({job_id})=>ok(await diagnoseJob(api,job_id))));
|
|
52
74
|
if(readOnly)return;
|
|
53
|
-
server.registerTool('save_job_output',{title:'Save ended Job output tail',description:'With approval, persist the ended Job log tail as a small Artifact using existing storage quota and retention. This is not the full log or binary output collection. Refuses recognizable credentials and queried URLs. Never touches GPU resources. Reuses a matching verified snapshot when found; cross-client concurrent calls may create duplicates.',inputSchema:{job_id:idSchema,user_confirmed:z.boolean(),accept_storage_charges:z.boolean().default(false)},annotations:{readOnlyHint:false,destructiveHint:false,idempotentHint:false,openWorldHint:true}},wrap(async({job_id,user_confirmed,accept_storage_charges})=>ok(await saveJobOutput(api,storage,job_id,user_confirmed,accept_storage_charges))));
|
|
75
|
+
server.registerTool('save_job_output',{title:'Save ended Job output tail',description:'With approval, persist the ended Job log tail as a small Artifact using existing storage quota and retention. This is not the full log or binary output collection. Refuses recognizable credentials, quoted/aliased fields and bounded escaped representations or queried URLs. This is not a guarantee that arbitrary logs contain no secrets; save only logs you know are sanitized. Never touches GPU resources. Reuses a matching verified snapshot when found; cross-client concurrent calls may create duplicates.',inputSchema:{job_id:idSchema,user_confirmed:z.boolean(),accept_storage_charges:z.boolean().default(false)},annotations:{readOnlyHint:false,destructiveHint:false,idempotentHint:false,openWorldHint:true}},wrap(async({job_id,user_confirmed,accept_storage_charges})=>ok(await saveJobOutput(api,storage,job_id,user_confirmed,accept_storage_charges))));
|
|
54
76
|
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// 只读任务准备:不访问 API、不租 GPU、不安装依赖,也不推断预算或存储同意。
|
|
2
|
+
import {createHash} from 'node:crypto';
|
|
3
|
+
import {z} from 'zod';
|
|
4
|
+
|
|
5
|
+
const TASKS = [
|
|
6
|
+
{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
|
+
{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
|
+
{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
|
+
const id=z.enum(TASKS.map(t=>t.id));
|
|
11
|
+
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
|
+
const schema=z.object({
|
|
13
|
+
template_id:id,
|
|
14
|
+
workspace_version_id:z.string().regex(/^wsv_[0-9a-f]{32}$/).optional(),
|
|
15
|
+
input_path:relative.optional(),
|
|
16
|
+
entrypoint:relative.refine(p=>p.endsWith('.py'),'Entrypoint must be a Python file').optional(),
|
|
17
|
+
persist_results:z.boolean().default(false),
|
|
18
|
+
output_files:z.array(relative).min(1).max(16).optional(),
|
|
19
|
+
accept_storage_charges:z.boolean().default(false),
|
|
20
|
+
}).strict();
|
|
21
|
+
const programs={
|
|
22
|
+
'gpu-check':`import json,subprocess
|
|
23
|
+
r=subprocess.run(['nvidia-smi','--query-gpu=name,memory.total','--format=csv,noheader,nounits'],capture_output=True,text=True,timeout=15)
|
|
24
|
+
if r.returncode: raise RuntimeError('GPU query failed; no automatic retry')
|
|
25
|
+
gpus=[]
|
|
26
|
+
for line in r.stdout.splitlines():
|
|
27
|
+
name,memory=line.rsplit(',',1)
|
|
28
|
+
gpus.append({'name':name.strip(),'memory_mib':int(memory.strip())})
|
|
29
|
+
if not gpus: raise RuntimeError('No visible GPU; no automatic retry')
|
|
30
|
+
result={'gpus':gpus}
|
|
31
|
+
`,
|
|
32
|
+
'csv-summary':`import csv,json,os,pathlib,stat
|
|
33
|
+
root=pathlib.Path.cwd().resolve()
|
|
34
|
+
p=root.joinpath(options['input_path'])
|
|
35
|
+
for component in p.relative_to(root).parts:
|
|
36
|
+
root=root/component
|
|
37
|
+
if root.is_symlink(): raise ValueError('Input must not use symlinks')
|
|
38
|
+
flags=os.O_RDONLY|getattr(os,'O_NOFOLLOW',0)|os.O_NONBLOCK
|
|
39
|
+
fd=os.open(str(p),flags)
|
|
40
|
+
meta=os.fstat(fd)
|
|
41
|
+
if not stat.S_ISREG(meta.st_mode) or meta.st_size>33554432:
|
|
42
|
+
os.close(fd)
|
|
43
|
+
raise ValueError('Input must be a regular CSV of at most 32 MiB')
|
|
44
|
+
csv.field_size_limit(1048576)
|
|
45
|
+
with os.fdopen(fd,'r',encoding='utf-8',newline='') as f:
|
|
46
|
+
reader=csv.reader(f)
|
|
47
|
+
header=next(reader,None)
|
|
48
|
+
columns=len(header) if header is not None else 0
|
|
49
|
+
rows=0
|
|
50
|
+
inconsistent=0
|
|
51
|
+
for row in reader:
|
|
52
|
+
rows+=1
|
|
53
|
+
inconsistent+=int(len(row)!=columns)
|
|
54
|
+
if rows>1000000: raise ValueError('CSV row limit exceeded')
|
|
55
|
+
result={'rows':rows,'columns':columns,'inconsistent_rows':inconsistent,'input_bytes':meta.st_size,'header_assumed':True}
|
|
56
|
+
`,
|
|
57
|
+
'workspace-python':`import os,pathlib,stat,sys
|
|
58
|
+
root=pathlib.Path.cwd().resolve()
|
|
59
|
+
p=root.joinpath(options['entrypoint'])
|
|
60
|
+
check=root
|
|
61
|
+
for component in p.relative_to(root).parts:
|
|
62
|
+
check=check/component
|
|
63
|
+
if check.is_symlink(): raise ValueError('Entrypoint must not use symlinks')
|
|
64
|
+
if not stat.S_ISREG(p.stat().st_mode): raise ValueError('Entrypoint must be a regular file')
|
|
65
|
+
os.execv(sys.executable,[sys.executable,str(p)])
|
|
66
|
+
`,
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export function listTaskTemplates(){
|
|
70
|
+
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.'};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function prepareTaskTemplate(raw){
|
|
75
|
+
const p=schema.parse(raw),t=TASKS.find(t=>t.id===p.template_id);
|
|
76
|
+
if(t.requires_workspace && !p.workspace_version_id)throw Error('Select an immutable Workspace version before preparing this task');
|
|
77
|
+
if(!t.requires_workspace && p.workspace_version_id)throw Error('gpu-check does not use a Workspace');
|
|
78
|
+
if(p.template_id==='csv-summary' ? !p.input_path : p.input_path!==undefined)throw Error('input_path is required only for csv-summary');
|
|
79
|
+
if(p.template_id==='workspace-python' ? !p.entrypoint : p.entrypoint!==undefined)throw Error('entrypoint is required only for workspace-python');
|
|
80
|
+
if(p.accept_storage_charges && !p.persist_results)throw Error('Storage consent is only relevant when persisting results');
|
|
81
|
+
if(p.output_files && (!p.persist_results || p.template_id!=='workspace-python'))throw Error('Explicit output_files are only for persistent workspace-python results');
|
|
82
|
+
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');
|
|
83
|
+
if(p.output_files && new Set(p.output_files).size!==p.output_files.length)throw Error('Result paths must be distinct');
|
|
84
|
+
const options={...(p.input_path?{input_path:p.input_path}:{}),...(p.entrypoint?{entrypoint:p.entrypoint}:{})};
|
|
85
|
+
let program='import json\noptions=json.loads('+JSON.stringify(JSON.stringify(options))+')\n'+programs[p.template_id];
|
|
86
|
+
if(p.template_id!=='workspace-python'){
|
|
87
|
+
program+=`import os,pathlib\nencoded=json.dumps(result,sort_keys=True)\n`;
|
|
88
|
+
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`;
|
|
89
|
+
program+="print(encoded)\n";
|
|
90
|
+
}
|
|
91
|
+
const command="set -eu\numask 077\npython3 - <<'CGPU_TASK_TEMPLATE'\n"+program+"CGPU_TASK_TEMPLATE\n";
|
|
92
|
+
const draft={command,...(p.workspace_version_id?{workspace_version_id:p.workspace_version_id}:{})};
|
|
93
|
+
if(p.persist_results)draft.outputs={files:p.output_files||t.result_files,max_bytes:10485760,accept_storage_charges:p.accept_storage_charges};
|
|
94
|
+
return {preparation_only:true,starts_gpu:false,template_id:t.id,template_version:t.version,
|
|
95
|
+
command_sha256:createHash('sha256').update(command).digest('hex'),run_job_draft:draft,
|
|
96
|
+
requirements:t.requirements,missing_fields:['gpu_model or quote_id','max_spend_usd','max_hours','user_confirmed','idempotency_key'],
|
|
97
|
+
next_steps:['Review the task, Workspace expiry and required dependencies; prefer local CPU execution for CSV-only work.',
|
|
98
|
+
'Check live compute capabilities and request a fresh quote for a compatible GPU. Do not assume image compatibility from this draft.',
|
|
99
|
+
'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.',
|
|
100
|
+
'Call run_job only after approval with a new idempotency key; retain that key for transport retries of the SAME submission.',
|
|
101
|
+
'Poll get_job until terminal, inspect output_collection if requested and verify GPU release. Do not automatically retry failed tasks.'],
|
|
102
|
+
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.'};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function registerTaskTemplateTools(server,{wrap,ok}){
|
|
106
|
+
const annotations={readOnlyHint:true,destructiveHint:false,idempotentHint:true,openWorldHint:false};
|
|
107
|
+
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())));
|
|
108
|
+
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))));
|
|
109
|
+
}
|