{
  "name": "jobs",
  "title": "Jobs (CPU + GPU compute)",
  "description": "Long-running async jobs - CPU or GPU via Gipity Jobs. Submit, poll, stream logs, retry, cancel. Per-second billing by compute class.",
  "guid": "sk_plat_jobs",
  "category": "App development",
  "requiredTools": [
    "job_list",
    "job_submit",
    "job_status",
    "job_runs",
    "job_logs",
    "job_cancel"
  ],
  "content": "# Jobs - long-running CPU & GPU compute\n\nJobs are deployed, named, async compute. Submit returns immediately with a run guid; the work runs on a sandbox container (CPU) or on Modal (GPU). Use jobs for anything that doesn't fit in a function (functions are 30 s / 128 MB; jobs go up to 60 min and 8 GB CPU / a full GPU).\n\nCommon shapes:\n- **Audio / video pipelines** (ffmpeg, sox, whisper)\n- **ML inference** that needs a GPU (Flux, LTX-Video, protein folding, transcription)\n- **Fine-tuning / training** runs\n- **Batch processing** (OCR, document conversion, embedding millions of items)\n- **Long ETL** that exceeds function limits\n\n## Declaring a job\n\nAdd a `jobs:` phase to `gipity.yaml`:\n\n```yaml\nversion: 1\ndeploy:\n  phases:\n    - name: my-jobs\n      type: jobs\n      job_definitions:\n        - name: transcribe\n          handler: jobs/transcribe/main.py\n          runtime: python-3.11\n          compute: gpu-small        # L4 GPU\n          timeout_ms: 300000        # 5 minutes\n          deps: jobs/transcribe/requirements.txt\n          retries: 1\n          description: \"Whisper transcription of an audio file\"\n```\n\nThen drop the handler at `jobs/transcribe/main.py`. Deploy with `gipity deploy dev`.\n\n### Inline annotations (optional)\n\nInstead of declaring everything in `gipity.yaml`, you can write `# @gipity:job` at the top of the handler:\n\n```python\n# @gipity:job runtime=python-3.11 compute=gpu-small timeout=5m\nimport whisper\n# ...\n```\n\nYAML wins on conflict; annotations seed defaults only.\n\n## Compute classes\n\nEach class maps to a substrate + resource shape. Billed **per second** based on actual wall time of the run (deps install included).\n\n| Class | Substrate | RAM | Notes |\n|---|---|---|---|\n| `cpu-small` | Docker sandbox host | 1 GB | Default. Fine for ffmpeg / pandoc / pip-installed Python work |\n| `cpu-large` | Docker sandbox host | 8 GB | Heavy CPU work - large encodes, document batches, ETL |\n| `gpu-small` | Modal L4 (24 GB VRAM) | per Modal | Most inference; the cheapest GPU |\n| `gpu-medium` | Modal A10G (24 GB VRAM) | per Modal | Slightly faster than L4 for some workloads |\n| `gpu-large` | Modal A100-40GB | per Modal | Large model training / fine-tuning |\n| `gpu-huge` | Modal H100 | per Modal | Heaviest training, big batched inference |\n\n`timeout_ms` caps the run inside the container (max 60 min). Modal kills the underlying call if our soft deadline is exceeded.\n\n## Runtimes & the fat image\n\nPick a runtime with `runtime:` (or the `# @gipity:job runtime=...` annotation). Valid values: **`python-3.11`**, **`node-24`** (Node 24; `node-20` is still accepted as a back-compat alias for the same runtime), and **`bash`**. Omit it and it's inferred from the handler extension (`.py`->python, `.js`->node-24, `.sh`->bash).\n\n**Node is CPU-only.** The GPU image ships Python and Bash but no Node, so a `node-*` runtime must pair with a `cpu-*` compute class. Declaring `runtime: node-24` (or `node-20`) with a `gpu-*` compute is rejected at deploy and at submit with a clear error - for GPU work, use `python-3.11`.\n\nGPU runs execute in a Modal container preloaded with:\n- **System**: ffmpeg, sox (incl. mp3/ogg/flac via libsox-fmt-all), imagemagick (PDF coder enabled), poppler-utils, tesseract, build-essential, git, git-lfs, libsndfile, libgl\n- **Python**: torch, transformers, accelerate, diffusers, peft, datasets, safetensors, sentencepiece, tokenizers, librosa, soundfile, openai-whisper, **demucs**, opencv-python-headless, scikit-image, numpy, scipy, pandas, pillow, httpx, pydantic, requests, boto3, huggingface_hub\n- Plus torchaudio (bundled in the pytorch base) - so `torchaudio.pipelines.MMS_FA_BUNDLE` works without pip install\n\nIf your job only needs the above, **omit `deps:`** entirely - the container starts faster.\n\nCPU runs use the sandbox image (`web-app-basics` for the full list).\n\n## Persistent cache\n\nBoth substrates mount a project-shared volume at `/cache`. HF/torch model weights cache there by default (env: `HF_HOME=/cache/hf`, `TORCH_HOME=/cache/torch`). First job downloads the weights; subsequent jobs are instant.\n\nUse `/cache/<your-key>/` for anything you want persisted across runs (intermediate artifacts, downloaded datasets). Don't put per-run output there - use `app-files` to upload final results.\n\n## Handler contract\n\nThe handler is invoked with these env vars set:\n\n| Var | Use |\n|---|---|\n| `GIPITY_RUN_GUID` | Pass to log/progress callbacks |\n| `GIPITY_RUN_INPUT` | JSON of the `input` arg from submit |\n| `GIPITY_APP_GUID` | Project short_guid |\n| `GIPITY_APP_TOKEN` | JWT for calling `/api/{appGuid}/services/*` |\n| `GIPITY_PROGRESS_URL` | POST progress updates here |\n| `GIPITY_OUTPUT_URL` | POST your structured result here (or use `ctx.set_output`) |\n| `GIPITY_PROGRESS_TOKEN` | Auth token for progress + output + log endpoints |\n| `GIPITY_API_BASE` | Base URL (e.g. `https://a.gipity.ai`) |\n\n**Identical on CPU and GPU.** Every var above - plus the `gipity_ctx` helper (`gipity_ctx.py` / `gipity_ctx.js`, see below) - is injected the same way on the Docker (CPU) and Modal (GPU) substrates, so `set_output`, `progress`, and the output channel work on a `gpu-*` job exactly as on a `cpu-*` one; don't gate any of this on compute class.\n\n### Job output contract\n\n**Set your result with `ctx.set_output(...)` - don't print it to stdout.** stdout and stderr are captured as **logs** (streamed live via `gipity job logs <runGuid>`, and the final stdout is stored on `output` only as a fallback). The clean way to return a structured result is the dedicated output channel:\n\n```python\nfrom gipity_ctx import ctx\nctx.set_output({\"stems\": stem_urls})   # this becomes the run's output\n```\n\n```js\nconst { ctx } = require('./gipity_ctx');\nawait ctx.set_output({ stems: stemUrls });\n```\n\n`set_output` blocks until the platform has stored the result and throws if it can't, so a lost result fails loudly instead of vanishing. Whatever you pass is exactly what an `on_complete` hook receives as `output` - **independent of stdout**, so you can `print()`/`console.log` diagnostics and let libraries log freely without corrupting it. Call it once; the last call wins.\n\n**256 KB cap.** The serialized output must be ≤256 KB; a larger payload is rejected (HTTP 413 `RESULT_TOO_LARGE`) and `set_output` throws. For a big result (large PDB/CSV/JSON), upload it via `app-files` and `set_output` the URL, or compress to fit (e.g. gzip+base64). Logs (stdout/stderr) share the same 256 KB cap; the live log stream is unbounded.\n\n**Before / after** - the trap `set_output` removes:\n\n```python\n# ❌ Before: result on stdout. One stray log line (yours or a library's) and the\n# platform can't parse stdout as JSON, so output becomes {stdout: \"...logs...\"}\n# and your on_complete hook sees output.stems as undefined.\nprint(\"loading model…\")            # pollutes stdout\nprint(json.dumps({\"stems\": urls})) # intended result\n\n# ✅ After: result on its own channel; logs go anywhere.\nprint(\"loading model…\")            # just a log - harmless\nctx.set_output({\"stems\": urls})    # the result\n```\n\nIf a job never calls `set_output`, the platform still falls back to parsing stdout - the whole stream as one JSON value, or a single JSON object/array at the very end - and otherwise stores the raw stream as `{stdout: \"...\"}`. Prefer `set_output`; the fallback is best-effort.\n\nstderr is captured but not streamed live (yet); it surfaces in `error_message` on failure.\n\n### Progress callbacks\n\nPOST to `$GIPITY_PROGRESS_URL` with `{\"pct\": 0.5, \"message\": \"halfway\"}` - `pct` is 0-1 (not 0-100); a value >1 is rejected with HTTP 400. Updates `progress_pct` and `progress_message` on the run row. Best-effort: log any failure to stderr, never stdout (stdout is the result).\n\n**`gipity_ctx` is the blessed pattern** - prefer it over hand-rolling these POSTs. The helper (`gipity_ctx.py` for python, `gipity_ctx.js` for node) is injected alongside the handler on **both** substrates, so use it on GPU jobs too:\n\n```python\nfrom gipity_ctx import ctx\nctx.progress(0.5, \"halfway\")\nctx.set_output({\"pdb_url\": url})\n```\n\n(An older handler that hand-rolls the raw `$GIPITY_PROGRESS_URL` / `$GIPITY_OUTPUT_URL` POSTs predates SDK injection on Modal - not the pattern to copy.)\n\n## CLI surface\n\n```bash\ngipity job list                          # all jobs in current project\ngipity job submit <name> [body]          # returns a run guid\ngipity job status <runGuid>              # snapshot\ngipity job runs <name> [--limit 20]      # recent runs of one job\ngipity job logs <runGuid> [--follow]     # SSE stream by default\ngipity job cancel <runGuid>              # queued or running -> cancelled\ngipity job run-local <name>              # Docker-on-your-machine for dev iteration\n```\n\nSubmit accepts `--data '<json>'` for input (or pass a JSON arg directly) and `--idempotency-key <key>` for replay.\n\n## Agent tools\n\nThe same six verbs are exposed as agent tools, with the same parameter shapes:\n\n- `job_list` (VIEWER) - list jobs in current project\n- `job_submit` (EDITOR) - submit a job; returns `{run_guid, status, replayed}`\n- `job_status` (VIEWER) - short status snapshot\n- `job_runs` (VIEWER) - recent runs of a named job\n- `job_logs` (VIEWER) - captured stdout / stderr / output, truncated at 5 KB\n- `job_cancel` (EDITOR) - flip a queued/running run to cancelled\n\nAgents and the CLI hit the same `/projects/{guid}/jobs/...` endpoints; their behavior matches.\n\n## REST endpoints (project-scoped, JWT auth)\n\n| Method | Path | Purpose |\n|---|---|---|\n| `GET`  | `/projects/{guid}/jobs` | list jobs |\n| `POST` | `/projects/{guid}/jobs/{name}/submit` | submit (returns runGuid) |\n| `GET`  | `/projects/{guid}/jobs/runs/{runGuid}` | one run's status |\n| `GET`  | `/projects/{guid}/jobs/{name}/runs` | list runs of one job |\n| `GET`  | `/projects/{guid}/jobs/runs/{runGuid}/logs/stream` | SSE log stream |\n| `POST` | `/projects/{guid}/jobs/runs/{runGuid}/cancel` | best-effort cancel |\n\nSubmit body: `{ \"input\": {...}, \"idempotency_key\": \"...\" }`.\n\n### In-function jobs API\n\nA deployed function handler submits and polls jobs through `ctx.jobs` - no HTTP layer, same concurrency caps / GPU gating / idempotency replay as the CLI. All three throw on error:\n\n```js\nconst { run_guid, status, replayed } = await ctx.jobs.submit('fold', { seq }, { idempotency_key: 'opt' });\nconst s = await ctx.jobs.status(run_guid);\n// s -> { run_guid, status, progress_pct, progress_message, attempt, started_at, completed_at, duration_ms, error, output }\n//   output is populated once status === 'success' - the value the handler passed to set_output.\nawait ctx.jobs.cancel(run_guid);   // -> { status }\n```\n\nA function returns in ≤30 s - don't poll a long job to completion inside it. Submit and return `run_guid`, or set `on_complete` (below) to fire a function when the run finishes.\n\n## State machine\n\n`queued → running → success | failed | cancelled`\n\nStatus writes are atomic - only one transition wins. Retries (`retries: N` in the YAML) re-claim a `failed` row back to `running` for the next attempt, with exponential backoff. Deps-install failures are **terminal** - no retries (the user's setup is wrong; retrying won't help).\n\nFailed and cancelled runs are billed for the time they actually ran (Modal / Docker charged us). Successful runs are billed for the same.\n\n## Billing\n\nPer-second metering by compute class. Underlying USD/sec rates track Modal's published prices; 100 % platform margin applied. Both lines visible on the credits Pricing tab (Jobs category).\n\nA 30-second L4 (`gpu-small`) job at 100 % margin ≈ $0.013 in credits (Modal's $0.80 / hr × 30 s × 2).\n\n## Auto-scaling: parallel jobs run in parallel\n\nSubmit N jobs concurrently and they execute concurrently. Modal's worker fleet spins up a fresh container per submission within seconds (cached image), runs your handler, and tears down - no per-job queue, each submission gets its own container.\n\nThe only ceiling is the **project-level cap of 10 in-flight jobs** (queued + running). If you submit an 11th while 10 are already in flight you get `RATE_LIMITED` (HTTP 429); wait for some to finish. This cap is per project, not per compute class - 5 L4s and 5 H100s in flight = cap reached.\n\nPractical implications:\n\n- **Batch workloads**: submit 10 jobs in a tight loop and watch them all start. Modal handles the fan-out at the substrate layer.\n- **Warm-pool freebie**: containers are kept alive ~60 s after a job finishes, so a second submission within that window starts in <5 s instead of paying the ~10-30 s cold-start.\n- **Image is cached fleet-wide**: the 15GB fat image is pulled to each Modal worker once. Steady-state cold starts are bounded by Python interpreter + your handler's imports.\n- **What we don't expose yet**: `fn.map()` fan-out (one submission → N parallel runs), `min_containers` (keep N warm at idle cost), per-container `concurrent_inputs` for IO-bound serving. All are Modal-native and would land in future PRs if you hit the use case.\n\n## Limits\n\n- Max concurrent (queued + running) jobs per project: 10\n- Max `timeout_ms`: 60 min (3,600,000)\n- Max retries: 10\n- Default 5-min deps install timeout\n- Result stdout / stderr capped at 256 KB (truncation marker preserved). Live log stream is unbounded - store large outputs via `app-files`.\n\n## Common patterns\n\n### Audio job that reads input + uploads result\n\n```python\nimport json, os, urllib.request\n\nrun_input = json.loads(os.environ['GIPITY_RUN_INPUT'])\ninput_url = run_input['audio_url']\n\n# Download to /tmp, process with ffmpeg or whisper or whatever\n# ...\n\n# Upload result via the app's file service (project-scoped JWT)\napp_token = os.environ['GIPITY_APP_TOKEN']\napp_guid  = os.environ['GIPITY_APP_GUID']\napi_base  = os.environ['GIPITY_API_BASE']\n# (use app-files skill for the upload flow)\n\nprint(json.dumps({\"transcript_url\": \"...\"}))\n```\n\n### Fan-out via on_complete\n\n```yaml\n- name: align\n  handler: jobs/align/main.py\n  compute: gpu-small\n  on_complete: render-preview     # name of a deployed function\n```\n\nWhen the align job hits a terminal state the platform fires the named function with `{ run_guid, status, output, error }`. The render function can then submit a new render job.\n\n## Testing a job end-to-end\n\nA job needs a public input URL. Make one without any external host: build a fixture in the sandbox (it syncs back to project files), publish it via `gipity deploy dev`, then submit and watch.\n\n```bash\ngipity sandbox run bash 'ffmpeg -f lavfi -i sine=frequency=440:duration=5 /work/test.wav'  # fixture -> project files\ngipity deploy dev                              # served at https://dev.gipity.ai/<acct>/<proj>/test.wav\ngipity job submit <name> --data '{\"audio_url\":\"https://dev.gipity.ai/<acct>/<proj>/test.wav\"}'\n# or, if a function starts the job: gipity fn call <trigger-fn> '{\"audio_url\":\"…\"}'\ngipity job status <runGuid>\ngipity job logs <runGuid> --follow\n```\n\nFirst GPU run downloads model weights to `/cache` (tens of seconds to minutes) - not a hang; subsequent runs are instant.\n\n## Common gotchas\n\n- **GPU without Modal config** - submit returns `MODAL_NOT_CONFIGURED` (503). Operator must set `MODAL_APP_URL` + `MODAL_INVOKE_SECRET` on the server.\n- **Deps that conflict with the fat image** - e.g. pinning `torch==2.3` when the image ships a different minor. `requirements.txt` wins via pip override, but CUDA/torch mismatches may produce subtle bugs. Prefer no `deps:` if the fat image already has what you need.\n- **Forgetting `set -e` in bash** - without it, only the LAST command's exit code propagates.\n- **`output` is whatever you `set_output`** - for an agent or function calling `job_submit`, the run's `output` (and what an `on_complete` hook receives) is the value the handler passed to `ctx.set_output(...)`. See \"Job output contract\" above.\n\n## See also\n\n- [deploy](deploy.md) - how the jobs phase fits into the rest of the deploy pipeline\n- [app-development](app-development.md) - functions vs jobs, when to use which\n- [app-files](app-files.md) - uploading artifacts the job produces\n- [app-debugging](app-debugging.md) - `page inspect`, function logs, run logs\n"
}
