Run a long Lua job
lua_jobRuns Lua scripts as frame-by-frame coroutines so long scans, traces, and tests do not freeze the game while reporting progress.
Instructions
Runs a Lua script as a coroutine resumed once per frame, so long work (scans, batches of traces, multi-second tests) does not freeze the game and reports progress while it runs. The script MUST cooperate through the job object (global inside the chunk, also its first argument):
job:progress(done, total, message) or job:progress(message): forwarded to the client as MCP progress (and as the task status when run as a task).
job:step(): call it inside long loops; it yields only when this frame's time slice (slice_ms, default 8 ms) is used up.
job:yield() / job:frames(n) / job:sleep(seconds) / job:wait(fn, timeout): resume on a later frame.
job:report(value [, channel]): intermediate result, returned at the end and readable live with reports_read (channel "job:"). Each report is bounded: depth 8, 5000 entries per table, strings of 1 MiB (an item the serializer cannot take, over 20000 values, becomes a {__type="report_too_large"} marker); a cut part is marked truncated and counted in reports_truncated. At most 1000 reports are kept (more are counted in reports_dropped), 32 MiB of strings in all: put big data in the returned values or a file instead.
args (a global, same table as job.args): the
argsobject of this call. job:elapsed(): seconds since start.return value, ...ends the job: the values are the tool result. Realm: server by default (a map must be loaded); menu always exists in client mode. The code iscode, orpath(a file, found like lua_exec_file finds it). By default the call waits for the end and returns {job_id, values, n, reports, output (printed text), frames, resumes, cpu_ms, elapsed_s, generation, truncated?, concurrent_jobs?}; timeout_s (default 300) cancels a job that is still running. Errors: lua_syntax_error, lua_error (with traceback, reports and output so far), realm_reset (the map changed: the job died with its Lua state), cancelled, timeout. Globals the script assigns go to the realm's _G (they outlive the job and the next job sees them) unless fresh_env: true. async: true starts the job and returns {job_id, state, realm, generation} at once: poll it with lua_job_status (wait_s to wait), stop it with lua_job_cancel; two jobs can then run side by side, e.g. one per realm. A job started while another runs in the same realm is reported in concurrent_jobs: their timings and state mix. Returned values come back whole (no cut unless depth/limit/max_string; then the result says truncated). The LuaJIT compiler stays on (timings are real). A resume that never yields freezes the game: polling it then fails with main_thread_timeout (about 10 s) and status shows main_thread_stalled. resume_limit_s (optional, best effort) aborts a resume that runs that long without yielding, for loops written in the script itself. Cancelling the request (or the task) stops the coroutine at the next frame. The job dies with its realm (map change). Example: local hits = 0 for i = 1, 5000 do local tr = util.TraceLine({ start = Vector(0, 0, 64), endpos = VectorRand() * 4096 }) if tr.Hit then hits = hits + 1 end if i % 250 == 0 then job:progress(i, 5000, hits .. " hits") end job:step() end return { hits = hits } called as {"realm": "server", "code": "", "timeout_s": 120}.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| jit | No | Obsolete: the compiler is never turned off any more. jit: true now only ignores resume_limit_s. | |
| args | No | Arbitrary data exposed to the script as job.args. | |
| code | No | Lua source. Use the `job` object described above. | |
| path | No | Instead of code: a file, absolute (e.g. in your git worktree) or relative (lua/mcpagent_tmp/, the mounted Lua folder if any, then the game folder, then lua/, gamemodes/, addons/*/lua/). Errors then point to the file. Prefer it to pasting a file's content into code. | |
| async | No | true: start the job and return its id at once (poll with lua_job_status). Default false. | |
| depth | No | Cut returned tables deeper than this (default: no cut). | |
| limit | No | Cut returned tables longer than this (default: no cut). | |
| realm | No | Realm (default server). | |
| fields | No | Keep only these keys of each returned table (e.g. ["pass", "fails"]); fields_note says so when no returned value is a table. | |
| instance | No | Optional. Instance identifier (see instances_list). If present and different from the local instance, the call is relayed by the coordinator to that instance. | |
| slice_ms | No | Time per frame before job:step() yields (default 8). | |
| chunkname | No | Chunk name shown by debug.getinfo and tracebacks (default: the file's path or '=mcpagent:job:<realm>'). | |
| fresh_env | No | Keep the globals the script assigns in its own table instead of _G (no leak into later jobs; reads still see _G). Default false. | |
| timeout_s | No | Cancel the job after this many seconds (default 300). 15 s or more: runs as an MCP task when the client supports it. | |
| max_string | No | Cut returned strings longer than this (default: no cut). | |
| resume_limit_s | No | Optional, best effort: abort a resume that runs this long without yielding (loops written in the script itself). None by default. |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| n | No | ||
| next | No | ||
| note | No | ||
| realm | No | ||
| state | No | ||
| cpu_ms | No | ||
| frames | No | ||
| job_id | Yes | ||
| output | No | ||
| values | No | ||
| reports | No | ||
| resumes | No | ||
| warning | No | ||
| elapsed_s | No | ||
| next_call | No | ||
| projected | No | ||
| truncated | No | ||
| generation | No | ||
| answered_by | No | ||
| fields_note | No | ||
| truncated_note | No | ||
| concurrent_jobs | No | ||
| reports_dropped | No | ||
| version_warning | No | ||
| reports_truncated | No | ||
| blocked_console_commands | No |